From 270dbeba6e7d4eb72302079a801b27b32d932af2 Mon Sep 17 00:00:00 2001 From: Hans Christian Schmitz Date: Tue, 31 Aug 2021 23:48:28 +0200 Subject: [PATCH] Fix GLSL output for non-fallthrough switch cases (#1310) * Fix GLSL output for non-fallthrough switch cases Partially reverts 02c74b50021379092a9d4567fd11aeb0879c5405 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. --- src/back/glsl/mod.rs | 15 +++ tests/in/control-flow.wgsl | 97 +++++++++++-------- tests/out/glsl/control-flow.main.Compute.glsl | 17 ++++ tests/out/hlsl/control-flow.hlsl | 24 +++++ tests/out/msl/control-flow.msl | 21 ++++ tests/out/spv/control-flow.spvasm | 26 ++++- tests/out/wgsl/control-flow.wgsl | 19 ++++ 7 files changed, 177 insertions(+), 42 deletions(-) diff --git a/src/back/glsl/mod.rs b/src/back/glsl/mod.rs index bd0979afe..7b23d851a 100644 --- a/src/back/glsl/mod.rs +++ b/src/back/glsl/mod.rs @@ -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 diff --git a/tests/in/control-flow.wgsl b/tests/in/control-flow.wgsl index 50e8b2229..679ad3d8a 100644 --- a/tests/in/control-flow.wgsl +++ b/tests/in/control-flow.wgsl @@ -1,59 +1,80 @@ [[stage(compute), workgroup_size(1)]] fn main([[builtin(global_invocation_id)]] global_id: vec3) { - //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; + } + } + } } diff --git a/tests/out/glsl/control-flow.main.Compute.glsl b/tests/out/glsl/control-flow.main.Compute.glsl index 2a42aa55c..e8b566b9d 100644 --- a/tests/out/glsl/control-flow.main.Compute.glsl +++ b/tests/out/glsl/control-flow.main.Compute.glsl @@ -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: diff --git a/tests/out/hlsl/control-flow.hlsl b/tests/out/hlsl/control-flow.hlsl index 3dd0ab7a2..d425b0991 100644 --- a/tests/out/hlsl/control-flow.hlsl +++ b/tests/out/hlsl/control-flow.hlsl @@ -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; diff --git a/tests/out/msl/control-flow.msl b/tests/out/msl/control-flow.msl index a2a80ac7b..52f990134 100644 --- a/tests/out/msl/control-flow.msl +++ b/tests/out/msl/control-flow.msl @@ -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; diff --git a/tests/out/spv/control-flow.spvasm b/tests/out/spv/control-flow.spvasm index fc79a6c7b..e8f0e2cf1 100644 --- a/tests/out/spv/control-flow.spvasm +++ b/tests/out/spv/control-flow.spvasm @@ -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 \ No newline at end of file diff --git a/tests/out/wgsl/control-flow.wgsl b/tests/out/wgsl/control-flow.wgsl index 903ad9923..2859edd61 100644 --- a/tests/out/wgsl/control-flow.wgsl +++ b/tests/out/wgsl/control-flow.wgsl @@ -39,6 +39,25 @@ fn main([[builtin(global_invocation_id)]] global_id: vec3) { } 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;