Skip to content

Instantly share code, notes, and snippets.

@cwfitzgerald
Last active July 15, 2025 03:56
Show Gist options
  • Select an option

  • Save cwfitzgerald/3d84db28b44d76ca05c0a8ddad77af55 to your computer and use it in GitHub Desktop.

Select an option

Save cwfitzgerald/3d84db28b44d76ca05c0a8ddad77af55 to your computer and use it in GitHub Desktop.
With loop bounding enabled, we will generate the following spv from this wgsl. The spirv-cross'd glsl is also shown
@compute @workgroup_size(1)
fn main() {
for (var i: i32 = 0; i < 15; i += 1) {
}
}
; SPIR-V
; Version: 1.1
; Generator: rspirv
; Bound: 47
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %6 "main"
OpExecutionMode %6 LocalSize 1 1 1
%3 = OpString "control-flow.wgsl"
OpSource Unknown 0 %3 "@compute @workgroup_size(1)
fn main() {
for (var i: i32 = 0; i < 15; i += 1) {
}
}
"
OpName %6 "main"
OpName %11 "i"
OpName %28 "loop_bound"
%2 = OpTypeVoid
%4 = OpTypeInt 32 1
%7 = OpTypeFunction %2
%8 = OpConstant %4 0
%9 = OpConstant %4 15
%10 = OpConstant %4 1
%12 = OpTypePointer Function %4
%18 = OpTypeInt 32 0
%19 = OpTypeVector %18 2
%20 = OpTypePointer Function %19
%21 = OpTypeBool
%22 = OpTypeVector %21 2
%23 = OpConstant %18 0
%24 = OpConstantComposite %19 %23 %23
%25 = OpConstant %18 1
%26 = OpConstant %18 4294967295
%27 = OpConstantComposite %19 %26 %26
%6 = OpFunction %2 None %7
%5 = OpLabel
%11 = OpVariable %12 Function %8
%28 = OpVariable %20 Function %27
OpBranch %13
%13 = OpLabel
OpBranch %14
%14 = OpLabel
OpLine %3 3 5
OpLoopMerge %15 %17 None
OpBranch %29
%29 = OpLabel
%30 = OpLoad %19 %28
%31 = OpIEqual %22 %24 %30
%32 = OpAll %21 %31
OpSelectionMerge %33 None
OpBranchConditional %32 %15 %33
%33 = OpLabel
%34 = OpCompositeExtract %18 %30 1
%35 = OpIEqual %21 %34 %23
%36 = OpSelect %18 %35 %25 %23
%37 = OpCompositeConstruct %19 %36 %25
%38 = OpISub %19 %30 %37
OpStore %28 %38
OpBranch %16
%16 = OpLabel
OpLine %3 1 1
%39 = OpLoad %4 %11
OpLine %3 3 26
%40 = OpSLessThan %21 %39 %9
OpLine %3 3 25
OpSelectionMerge %41 None
OpBranchConditional %40 %41 %42
%42 = OpLabel
OpBranch %15
%41 = OpLabel
OpBranch %43
%43 = OpLabel
OpBranch %44
%44 = OpLabel
OpBranch %17
%17 = OpLabel
OpLine %3 3 34
%45 = OpLoad %4 %11
%46 = OpIAdd %4 %45 %10
OpLine %3 3 34
OpStore %11 %46
OpBranch %14
%15 = OpLabel
OpReturn
OpFunctionEnd
#version 450
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main()
{
int i = 0;
uvec2 loop_bound = uvec2(4294967295u);
for (;;)
{
if (all(equal(uvec2(0u), loop_bound)))
{
break;
}
loop_bound -= uvec2(uint(loop_bound.y == 0u), 1u);
if (!(i < 15))
{
break;
}
i++;
continue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment