Files
wgpu/naga/tests/out/spv/break-if.spvasm
T
Jamie Nicol b7d1f4c6cf [naga spv-out] Ensure loops generated by SPIRV backend are bounded (#7080)
If it is undefined behaviour for loops to be infinite, then, when
encountering an infinite loop, downstream compilers are able to make
certain optimizations that may be unsafe. For example, omitting bounds
checks. To prevent this, we must ensure that any loops emitted by our
backends are provably bounded. We already do this for both the MSL and
HLSL backends. This patch makes us do so for SPIRV as well.

The construct used is the same as for HLSL and MSL backends: use a
vec2<u32> to emulate a 64-bit counter, which is incremented every
iteration and breaks after 2^64 iterations.

While the implementation is fairly verbose for the SPIRV backend, the
logic is simple enough. The one point of note is that SPIRV requires
`OpVariable` instructions with a `Function` storage class to be
located at the start of the first block of the function. We therefore
remember the IDs generated for each loop counter variable in a
function whilst generating the function body's code. The instructions
to declare these variables are then emitted in `Function::to_words()`
prior to emitting the function's body.

As this may negatively impact shader performance, this workaround can
be disabled using the same mechanism as for other backends: eg calling
Device::create_shader_module_trusted() and setting the
ShaderRuntimeChecks::force_loop_bounding flag to false.
2025-02-25 15:23:44 +01:00

180 lines
4.0 KiB
Plaintext

; SPIR-V
; Version: 1.1
; Generator: rspirv
; Bound: 117
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %115 "main"
OpExecutionMode %115 LocalSize 1 1 1
%2 = OpTypeVoid
%3 = OpTypeBool
%4 = OpTypeInt 32 0
%7 = OpTypeFunction %2
%8 = OpConstantTrue %3
%14 = OpTypeVector %4 2
%15 = OpTypePointer Function %14
%16 = OpTypeVector %3 2
%17 = OpConstant %4 0
%18 = OpConstantComposite %14 %17 %17
%19 = OpConstant %4 1
%20 = OpConstant %4 4294967295
%21 = OpConstantComposite %14 %20 %20
%36 = OpTypeFunction %2 %3
%38 = OpTypePointer Function %3
%39 = OpConstantNull %3
%41 = OpConstantNull %3
%66 = OpConstantNull %3
%68 = OpConstantNull %3
%91 = OpConstant %4 5
%93 = OpTypePointer Function %4
%6 = OpFunction %2 None %7
%5 = OpLabel
%22 = OpVariable %15 Function %18
OpBranch %9
%9 = OpLabel
OpBranch %10
%10 = OpLabel
OpLoopMerge %11 %13 None
OpBranch %23
%23 = OpLabel
%24 = OpLoad %14 %22
%25 = OpIEqual %16 %21 %24
%26 = OpAll %3 %25
OpSelectionMerge %27 None
OpBranchConditional %26 %11 %27
%27 = OpLabel
%28 = OpCompositeExtract %4 %24 1
%29 = OpIEqual %3 %28 %20
%30 = OpSelect %4 %29 %19 %17
%31 = OpCompositeConstruct %14 %30 %19
%32 = OpIAdd %14 %24 %31
OpStore %22 %32
OpBranch %12
%12 = OpLabel
OpBranch %13
%13 = OpLabel
OpBranchConditional %8 %11 %10
%11 = OpLabel
OpReturn
OpFunctionEnd
%35 = OpFunction %2 None %36
%34 = OpFunctionParameter %3
%33 = OpLabel
%37 = OpVariable %38 Function %39
%40 = OpVariable %38 Function %41
%47 = OpVariable %15 Function %18
OpBranch %42
%42 = OpLabel
OpBranch %43
%43 = OpLabel
OpLoopMerge %44 %46 None
OpBranch %48
%48 = OpLabel
%49 = OpLoad %14 %47
%50 = OpIEqual %16 %21 %49
%51 = OpAll %3 %50
OpSelectionMerge %52 None
OpBranchConditional %51 %44 %52
%52 = OpLabel
%53 = OpCompositeExtract %4 %49 1
%54 = OpIEqual %3 %53 %20
%55 = OpSelect %4 %54 %19 %17
%56 = OpCompositeConstruct %14 %55 %19
%57 = OpIAdd %14 %49 %56
OpStore %47 %57
OpBranch %45
%45 = OpLabel
OpBranch %46
%46 = OpLabel
OpStore %37 %34
%58 = OpLoad %3 %37
%59 = OpLogicalNotEqual %3 %34 %58
OpStore %40 %59
%60 = OpLoad %3 %40
%61 = OpLogicalEqual %3 %34 %60
OpBranchConditional %61 %44 %43
%44 = OpLabel
OpReturn
OpFunctionEnd
%64 = OpFunction %2 None %36
%63 = OpFunctionParameter %3
%62 = OpLabel
%65 = OpVariable %38 Function %66
%67 = OpVariable %38 Function %68
%74 = OpVariable %15 Function %18
OpBranch %69
%69 = OpLabel
OpBranch %70
%70 = OpLabel
OpLoopMerge %71 %73 None
OpBranch %75
%75 = OpLabel
%76 = OpLoad %14 %74
%77 = OpIEqual %16 %21 %76
%78 = OpAll %3 %77
OpSelectionMerge %79 None
OpBranchConditional %78 %71 %79
%79 = OpLabel
%80 = OpCompositeExtract %4 %76 1
%81 = OpIEqual %3 %80 %20
%82 = OpSelect %4 %81 %19 %17
%83 = OpCompositeConstruct %14 %82 %19
%84 = OpIAdd %14 %76 %83
OpStore %74 %84
OpBranch %72
%72 = OpLabel
OpStore %65 %63
%85 = OpLoad %3 %65
%86 = OpLogicalNotEqual %3 %63 %85
OpStore %67 %86
OpBranch %73
%73 = OpLabel
%87 = OpLoad %3 %67
%88 = OpLogicalEqual %3 %63 %87
OpBranchConditional %88 %71 %70
%71 = OpLabel
OpReturn
OpFunctionEnd
%90 = OpFunction %2 None %7
%89 = OpLabel
%92 = OpVariable %93 Function %17
%99 = OpVariable %15 Function %18
OpBranch %94
%94 = OpLabel
OpBranch %95
%95 = OpLabel
OpLoopMerge %96 %98 None
OpBranch %100
%100 = OpLabel
%101 = OpLoad %14 %99
%102 = OpIEqual %16 %21 %101
%103 = OpAll %3 %102
OpSelectionMerge %104 None
OpBranchConditional %103 %96 %104
%104 = OpLabel
%105 = OpCompositeExtract %4 %101 1
%106 = OpIEqual %3 %105 %20
%107 = OpSelect %4 %106 %19 %17
%108 = OpCompositeConstruct %14 %107 %19
%109 = OpIAdd %14 %101 %108
OpStore %99 %109
OpBranch %97
%97 = OpLabel
%110 = OpLoad %4 %92
%111 = OpIAdd %4 %110 %19
OpStore %92 %111
OpBranch %98
%98 = OpLabel
%112 = OpLoad %4 %92
%113 = OpIEqual %3 %112 %91
OpBranchConditional %113 %96 %95
%96 = OpLabel
OpReturn
OpFunctionEnd
%115 = OpFunction %2 None %7
%114 = OpLabel
OpBranch %116
%116 = OpLabel
OpReturn
OpFunctionEnd