KhronosGroup / KhronosGroup/SPIRV-Tools

Merge return produces an invalid cfg

Open
#3,265 0 comments 0 reactions 0 assignees View on GitHub
bug component:optimization
Dominant language
C++
Stars
1.4k
Forks
709
Avg merge
1d 22h
Merged PRs (30d)
28

Description

```
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %gl_FragCoord %_GLF_color
OpExecutionMode %main OriginUpperLeft
OpSource ESSL 310
OpName %main "main"
OpName %nb_mod_f1_ "nb_mod(f1;"
OpName %limit "limit"
OpName %_injected_loop_counter "_injected_loop_counter"
OpName %_injected_loop_counter_0 "_injected_loop_counter"
OpName %gl_FragCoord "gl_FragCoord"
OpName %param "param"
OpName %_GLF_color "_GLF_color"
OpDecorate %_injected_loop_counter RelaxedPrecision
OpDecorate %_injected_loop_counter_0 RelaxedPrecision
OpDecorate %gl_FragCoord BuiltIn FragCoord
OpDecorate %_GLF_color Location 0
%void = OpTypeVoid
%11 = OpTypeFunction %void
%float = OpTypeFloat 32
%_ptr_Function_float = OpTypePointer Function %float
%14 = OpTypeFunction %float %_ptr_Function_float
%float_1 = OpConstant %float 1
%bool = OpTypeBool
%int = OpTypeInt 32 1
%_ptr_Function_int = OpTypePointer Function %int
%int_0 = OpConstant %int 0
%int_2 = OpConstant %int 2
%int_1 = OpConstant %int 1
%v4float = OpTypeVector %float 4
%_ptr_Input_v4float = OpTypePointer Input %v4float
%gl_FragCoord = OpVariable %_ptr_Input_v4float Input
%uint = OpTypeInt 32 0
%uint_0 = OpConstant %uint 0
%_ptr_Input_float = OpTypePointer Input %float
%_ptr_Output_v4float = OpTypePointer Output %v4float
%_GLF_color = OpVariable %_ptr_Output_v4float Output
%float_0 = OpConstant %float 0
%29 = OpConstantComposite %v4float %float_1 %float_0 %float_0 %float_1
%30 = OpUndef %int
%true = OpConstantTrue %bool
%main = OpFunction %void None %11
%32 = OpLabel
%param = OpVariable %_ptr_Function_float Function
%33 = OpAccessChain %_ptr_Input_float %gl_FragCoord %uint_0
%34 = OpLoad %float %33
OpStore %param %34
%35 = OpFunctionCall %float %nb_mod_f1_ %param
OpStore %_GLF_color %29
OpReturn
OpFunctionEnd
%nb_mod_f1_ = OpFunction %float None %14
%limit = OpFunctionParameter %_ptr_Function_float
%36 = OpLabel
%_injected_loop_counter = OpVariable %_ptr_Function_int Function
%_injected_loop_counter_0 = OpVariable %_ptr_Function_int Function
%37 = OpLoad %float %limit
%38 = OpFOrdGreaterThanEqual %bool %float_1 %37
OpSelectionMerge %39 None
OpBranchConditional %38 %40 %39
%40 = OpLabel
OpReturnValue %float_1
%39 = OpLabel
OpStore %_injected_loop_counter %int_0
OpBranch %41
%41 = OpLabel
%42 = OpSLessThan %bool %int_0 %int_2
OpLoopMerge %43 %44 None
OpBranch %45
%45 = OpLabel
OpStore %_injected_loop_counter_0 %int_0
OpBranch %46
%46 = OpLabel
%47 = OpSLessThan %bool %int_0 %int_1
OpLoopMerge %44 %48 None
OpBranch %49
%49 = OpLabel
OpReturnValue %float_1
%48 = OpLabel
OpBranch %46
%44 = OpLabel
OpBranch %41
%43 = OpLabel
OpUnreachable
OpFunctionEnd
```

Run `--merge-return` and validate the result. You should see the error:

>error: line 93: Header block 44[%44] is contained in the loop construct headed by 41[%41], but its merge block 57[%57] is not
> %44 = OpLabel

A selection introduced by merge return declares its merge as the continue target of a loop.

Contributor guide

Open the contributing guide

Research direction

Start with the supplied SPIR-V module, run --merge-return, and validate the transformed result to reproduce the invalid CFG diagnostic. Trace the merge-return transformation that makes selection block 44 a loop continue target; done means the transformed module validates without the reported containment error.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.