Fix GLSL output for non-fallthrough switch cases (#1310)

* Fix GLSL output for non-fallthrough switch cases

Partially reverts 02c74b5002 and
fixes #1309.

* Fix indentation of control-flow WGSL code

* Clean up glsl-out switch case fallthrough handling

Only insert a break if needed
and if a case is fallthrough, insert a comment indicating this.
This commit is contained in:
Hans Christian Schmitz authored and Dzmitry Malyshau committed 2021-09-01 17:52:28 -04:00
1 parent 914cfda696
commit 270dbeba6e
7 files changed
+177 -42

No files matched your search

+15
View File
@@ -1441,6 +1441,21 @@ impl<'a, W: Write> Writer<'a, W> {
for sta in case.body.iter() {
self.write_stmt(sta, ctx, indent + 2)?;
}
// Write fallthrough comment if the case is fallthrough,
// otherwise write a break, if the case is not already
// broken out of at the end of its body.
if case.fall_through {
writeln!(self.out, "{}/* fallthrough */", INDENT.repeat(indent + 2))?;
} else if !matches!(
case.body.last(),
Some(&Statement::Break)
| Some(&Statement::Continue)
| Some(&Statement::Return { .. })
| Some(&Statement::Kill)
) {
writeln!(self.out, "{}break;", INDENT.repeat(indent + 2))?;
}
}
// Only write the default block if the block isn't empty
+59 -38
View File
@@ -1,59 +1,80 @@
[[stage(compute), workgroup_size(1)]]
fn main([[builtin(global_invocation_id)]] global_id: vec3<u32>) {
//TODO: execution-only barrier?
storageBarrier();
workgroupBarrier();
//TODO: execution-only barrier?
storageBarrier();
workgroupBarrier();
var pos: i32;
// switch without cases
var pos: i32;
// switch without cases
switch (1) {
default: {
pos = 1;
}
pos = 1;
}
}
switch (pos) {
case 1: {
pos = 0;
break;
}
case 2: {
pos = 1;
}
case 3: {
pos = 2;
fallthrough;
}
case 4: {}
// non-empty switch *not* in last-statement-in-function position
// (return statements might be inserted into the switch cases otherwise)
switch (pos) {
case 1: {
pos = 0;
break;
}
case 2: {
pos = 1;
}
case 3: {
pos = 2;
fallthrough;
}
case 4: {}
default: {
pos = 3;
}
pos = 3;
}
}
// non-empty switch in last-statement-in-function position
switch (pos) {
case 1: {
pos = 0;
break;
}
case 2: {
pos = 1;
}
case 3: {
pos = 2;
fallthrough;
}
case 4: {}
default: {
pos = 3;
}
}
}
fn switch_default_break(i: i32) {
switch (i) {
default: {
break;
switch (i) {
default: {
break;
}
}
}
}
fn switch_case_break() {
switch(0) {
case 0: {
break;
switch(0) {
case 0: {
break;
}
}
}
return;
return;
}
fn loop_switch_continue(x: i32) {
loop {
switch (x) {
case 1: {
continue;
}
}
}
loop {
switch (x) {
case 1: {
continue;
}
}
}
}
@@ -42,6 +42,22 @@ void main() {
}
int _e4 = pos;
switch(_e4) {
case 1:
pos = 0;
break;
case 2:
pos = 1;
break;
case 3:
pos = 2;
/* fallthrough */
case 4:
break;
default:
pos = 3;
}
int _e9 = pos;
switch(_e9) {
case 1:
pos = 0;
break;
@@ -50,6 +66,7 @@ void main() {
return;
case 3:
pos = 2;
/* fallthrough */
case 4:
return;
default:
+24
View File
@@ -46,6 +46,30 @@ void main(uint3 global_id : SV_DispatchThreadID)
}
int _expr4 = pos;
switch(_expr4) {
case 1: {
pos = 0;
break;
break;
}
case 2: {
pos = 1;
break;
}
case 3: {
/* fallthrough */
{
pos = 2;
}
}
case 4: {
break;
}
default: {
pos = 3;
}
}
int _expr9 = pos;
switch(_expr9) {
case 1: {
pos = 0;
break;
+21
View File
@@ -57,6 +57,27 @@ kernel void main1(
}
int _e4 = pos;
switch(_e4) {
case 1: {
pos = 0;
break;
break;
}
case 2: {
pos = 1;
break;
}
case 3: {
pos = 2;
}
case 4: {
break;
}
default: {
pos = 3;
}
}
int _e9 = pos;
switch(_e9) {
case 1: {
pos = 0;
break;
+22 -4
View File
@@ -1,7 +1,7 @@
; SPIR-V
; Version: 1.1
; Generator: rspirv
; Bound: 56
; Bound: 63
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
@@ -95,15 +95,33 @@ OpStore %35 %5
OpBranch %50
%53 = OpLabel
OpStore %35 %3
OpReturn
OpBranch %50
%54 = OpLabel
OpStore %35 %6
OpBranch %55
%55 = OpLabel
OpReturn
OpBranch %50
%51 = OpLabel
OpStore %35 %7
OpReturn
OpBranch %50
%50 = OpLabel
%56 = OpLoad %4 %35
OpSelectionMerge %57 None
OpSwitch %56 %58 1 %59 2 %60 3 %61 4 %62
%59 = OpLabel
OpStore %35 %5
OpBranch %57
%60 = OpLabel
OpStore %35 %3
OpReturn
%61 = OpLabel
OpStore %35 %6
OpBranch %62
%62 = OpLabel
OpReturn
%58 = OpLabel
OpStore %35 %7
OpReturn
%57 = OpLabel
OpReturn
OpFunctionEnd
+19
View File
@@ -39,6 +39,25 @@ fn main([[builtin(global_invocation_id)]] global_id: vec3<u32>) {
}
let _e4: i32 = pos;
switch(_e4) {
case 1: {
pos = 0;
break;
}
case 2: {
pos = 1;
}
case 3: {
pos = 2;
fallthrough;
}
case 4: {
}
default: {
pos = 3;
}
}
let _e9: i32 = pos;
switch(_e9) {
case 1: {
pos = 0;
break;