[naga wgsl-in] Fix parsing break ifs

Closes https://github.com/gfx-rs/wgpu/issues/4982.
This commit is contained in:
Patryk Wychowaniec 2024-01-05 19:06:55 +01:00 committed by Teodor Tanasoaia
parent a2dced12b3
commit e7c7017d2e
7 changed files with 169 additions and 61 deletions

View File

@ -1328,7 +1328,9 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
let mut emitter = Emitter::default(); let mut emitter = Emitter::default();
emitter.start(&ctx.function.expressions); emitter.start(&ctx.function.expressions);
let break_if = break_if let break_if = break_if
.map(|expr| self.expression(expr, &mut ctx.as_expression(block, &mut emitter))) .map(|expr| {
self.expression(expr, &mut ctx.as_expression(&mut continuing, &mut emitter))
})
.transpose()?; .transpose()?;
continuing.extend(emitter.finish(&ctx.function.expressions)); continuing.extend(emitter.finish(&ctx.function.expressions));

View File

@ -30,3 +30,15 @@ fn breakIf(a: bool) {
} }
} }
} }
fn breakIfSeparateVariable() {
var counter = 0u;
loop {
counter += 1u;
continuing {
break if counter == 5u;
}
}
}

View File

@ -57,6 +57,23 @@ void breakIf(bool a_1) {
return; return;
} }
void breakIfSeparateVariable() {
uint counter = 0u;
bool loop_init_3 = true;
while(true) {
if (!loop_init_3) {
uint _e5 = counter;
if ((_e5 == 5u)) {
break;
}
}
loop_init_3 = false;
uint _e3 = counter;
counter = (_e3 + 1u);
}
return;
}
void main() { void main() {
return; return;
} }

View File

@ -54,6 +54,25 @@ void breakIf(bool a_1)
return; return;
} }
void breakIfSeparateVariable()
{
uint counter = 0u;
bool loop_init_3 = true;
while(true) {
if (!loop_init_3) {
uint _expr5 = counter;
if ((_expr5 == 5u)) {
break;
}
}
loop_init_3 = false;
uint _expr3 = counter;
counter = (_expr3 + 1u);
}
return;
}
[numthreads(1, 1, 1)] [numthreads(1, 1, 1)]
void main() void main()
{ {

View File

@ -61,6 +61,24 @@ void breakIf(
return; return;
} }
void breakIfSeparateVariable(
) {
uint counter = 0u;
bool loop_init_3 = true;
while(true) {
if (!loop_init_3) {
uint _e5 = counter;
if (counter == 5u) {
break;
}
}
loop_init_3 = false;
uint _e3 = counter;
counter = _e3 + 1u;
}
return;
}
kernel void main_( kernel void main_(
) { ) {
return; return;

View File

@ -1,88 +1,114 @@
; SPIR-V ; SPIR-V
; Version: 1.1 ; Version: 1.1
; Generator: rspirv ; Generator: rspirv
; Bound: 50 ; Bound: 67
OpCapability Shader OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450" %1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450 OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %48 "main" OpEntryPoint GLCompute %65 "main"
OpExecutionMode %48 LocalSize 1 1 1 OpExecutionMode %65 LocalSize 1 1 1
%2 = OpTypeVoid %2 = OpTypeVoid
%3 = OpTypeBool %3 = OpTypeBool
%6 = OpTypeFunction %2 %4 = OpTypeInt 32 0
%7 = OpConstantTrue %3 %7 = OpTypeFunction %2
%16 = OpTypeFunction %2 %3 %8 = OpConstantTrue %3
%18 = OpTypePointer Function %3 %17 = OpTypeFunction %2 %3
%19 = OpConstantNull %3 %19 = OpTypePointer Function %3
%21 = OpConstantNull %3 %20 = OpConstantNull %3
%35 = OpConstantNull %3 %22 = OpConstantNull %3
%37 = OpConstantNull %3 %36 = OpConstantNull %3
%5 = OpFunction %2 None %6 %38 = OpConstantNull %3
%4 = OpLabel %50 = OpConstant %4 0
OpBranch %8 %51 = OpConstant %4 1
%8 = OpLabel %52 = OpConstant %4 5
%54 = OpTypePointer Function %4
%6 = OpFunction %2 None %7
%5 = OpLabel
OpBranch %9 OpBranch %9
%9 = OpLabel %9 = OpLabel
OpLoopMerge %10 %12 None OpBranch %10
OpBranch %11 %10 = OpLabel
%11 = OpLabel OpLoopMerge %11 %13 None
OpBranch %12 OpBranch %12
%12 = OpLabel %12 = OpLabel
OpBranchConditional %7 %10 %9 OpBranch %13
%10 = OpLabel %13 = OpLabel
OpBranchConditional %8 %11 %10
%11 = OpLabel
OpReturn OpReturn
OpFunctionEnd OpFunctionEnd
%15 = OpFunction %2 None %16 %16 = OpFunction %2 None %17
%14 = OpFunctionParameter %3 %15 = OpFunctionParameter %3
%13 = OpLabel %14 = OpLabel
%17 = OpVariable %18 Function %19 %18 = OpVariable %19 Function %20
%20 = OpVariable %18 Function %21 %21 = OpVariable %19 Function %22
OpBranch %22
%22 = OpLabel
OpBranch %23 OpBranch %23
%23 = OpLabel %23 = OpLabel
OpLoopMerge %24 %26 None OpBranch %24
OpBranch %25 %24 = OpLabel
%25 = OpLabel OpLoopMerge %25 %27 None
OpBranch %26 OpBranch %26
%26 = OpLabel %26 = OpLabel
OpStore %17 %14 OpBranch %27
%27 = OpLoad %3 %17 %27 = OpLabel
%28 = OpLogicalNotEqual %3 %14 %27 OpStore %18 %15
OpStore %20 %28 %28 = OpLoad %3 %18
%29 = OpLoad %3 %20 %29 = OpLogicalNotEqual %3 %15 %28
%30 = OpLogicalEqual %3 %14 %29 OpStore %21 %29
OpBranchConditional %30 %24 %23 %30 = OpLoad %3 %21
%24 = OpLabel %31 = OpLogicalEqual %3 %15 %30
OpBranchConditional %31 %25 %24
%25 = OpLabel
OpReturn OpReturn
OpFunctionEnd OpFunctionEnd
%33 = OpFunction %2 None %16 %34 = OpFunction %2 None %17
%32 = OpFunctionParameter %3 %33 = OpFunctionParameter %3
%31 = OpLabel %32 = OpLabel
%34 = OpVariable %18 Function %35 %35 = OpVariable %19 Function %36
%36 = OpVariable %18 Function %37 %37 = OpVariable %19 Function %38
OpBranch %38
%38 = OpLabel
OpBranch %39 OpBranch %39
%39 = OpLabel %39 = OpLabel
OpLoopMerge %40 %42 None OpBranch %40
OpBranch %41 %40 = OpLabel
%41 = OpLabel OpLoopMerge %41 %43 None
OpStore %34 %32
%43 = OpLoad %3 %34
%44 = OpLogicalNotEqual %3 %32 %43
OpStore %36 %44
OpBranch %42 OpBranch %42
%42 = OpLabel %42 = OpLabel
%45 = OpLoad %3 %36 OpStore %35 %33
%46 = OpLogicalEqual %3 %32 %45 %44 = OpLoad %3 %35
OpBranchConditional %46 %40 %39 %45 = OpLogicalNotEqual %3 %33 %44
%40 = OpLabel OpStore %37 %45
OpBranch %43
%43 = OpLabel
%46 = OpLoad %3 %37
%47 = OpLogicalEqual %3 %33 %46
OpBranchConditional %47 %41 %40
%41 = OpLabel
OpReturn OpReturn
OpFunctionEnd OpFunctionEnd
%48 = OpFunction %2 None %6 %49 = OpFunction %2 None %7
%47 = OpLabel %48 = OpLabel
OpBranch %49 %53 = OpVariable %54 Function %50
%49 = OpLabel OpBranch %55
%55 = OpLabel
OpBranch %56
%56 = OpLabel
OpLoopMerge %57 %59 None
OpBranch %58
%58 = OpLabel
%60 = OpLoad %4 %53
%61 = OpIAdd %4 %60 %51
OpStore %53 %61
OpBranch %59
%59 = OpLabel
%62 = OpLoad %4 %53
%63 = OpIEqual %3 %62 %52
OpBranchConditional %63 %57 %56
%57 = OpLabel
OpReturn
OpFunctionEnd
%65 = OpFunction %2 None %7
%64 = OpLabel
OpBranch %66
%66 = OpLabel
OpReturn OpReturn
OpFunctionEnd OpFunctionEnd

View File

@ -39,6 +39,20 @@ fn breakIf(a_1: bool) {
return; return;
} }
fn breakIfSeparateVariable() {
var counter: u32 = 0u;
loop {
let _e3 = counter;
counter = (_e3 + 1u);
continuing {
let _e5 = counter;
break if (_e5 == 5u);
}
}
return;
}
@compute @workgroup_size(1, 1, 1) @compute @workgroup_size(1, 1, 1)
fn main() { fn main() {
return; return;