View Full Version : Amd GLSL switch-case bug

04-19-2016, 09:21 AM
I am using fglrx on Debian stable. fglrxinfo:
display: :0 screen: 0
OpenGL vendor string: Advanced Micro Devices, Inc.
OpenGL renderer string: AMD Radeon R7 200 Series
OpenGL version string: 4.5.13399 Compatibility Profile Context 15.201.1151

Yesterday I spent a lot of time debugging a seemingly correct switch-case statement similar to this:

switch (a) {
case 1,2,3: i++; break;
case 4,5,6: i--;

It turned out that for some values of a between 1 and 6 no case inside the switch is triggered.
If written like this:

switch (a) {
case 1:
case 2:
case 3: i++; break;
case 4:
case 5:
case 6: i--;

the expected results are produced. So I turned to the spec and it seems that case statements with
multiple case labels are generally not allowed in GLSL.

So there are one and a half bugs:
1. Multiple case labels should lead to compile-time errors but don't. (not nice)
1.5 The technically illegal case statement behaves in unexpected ways. (really bad)

Besides getting this documented somewhere, the purpose of this post is to ask where to report this.
There is always ati.cchtml.com, but that is fairly unofficial and I can't make an account (I don't
receive the confirmation message).

Also, can anyone reproduce this? I'm pretty sure it is buggy on my machine and it looks like a
compiler problem so I would expect that it affects at least all GCN families, but it never hurts to try...

04-19-2016, 06:17 PM
Reading the GLSL grammar, a switch case_label can be "CASE expression COLON". And expression can be "expression COMMA assignment_expression". Assignment_expression and expression can both simplify to INTCONSTANT. The comma operator returns the value of the rightmost expression.

So, what you're actually getting with your first test case (which is completely valid syntax) is:

switch (a) {
case 3: i++; break;
case 6: i--;

It's just not at all what you're expecting. (see GLSL spec sections 5.9 and 9)

04-20-2016, 01:54 AM
Mmh. Yes, I missed that. Does this have a potential use?

04-20-2016, 02:41 AM
Mmh. Yes, I missed that. Does this have a potential use?
Does what have a potential use? Comma expressions? The most common example I can think of is assigning a variable and testing its value in a context where you're limited to an expression, e.g.

while (x = get_value(), x < 10) {

I can't think of any situation where you'd use it in a constant expression, but constant expressions don't have distinct syntactic rules from expressions in general.

04-20-2016, 07:12 AM
Mmh. Yes, I missed that. Does this have a potential use?

Not really for constants, because a 1,2,3 expression is effectively dead code for 1,2. A warning would be nice, but that's up to the GLSL compiler development team.