Structurizer accepts a loop whose selection shares a tail block with a nested selection; the SPIR-V then fails validation #2762
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by orishamir on 2026-10-10T13:15:34Z.
Summary
On
maina29ef592Structurizer::Structurizesucceeds on a loop whose first selection shares a tail block with a nested selection, but the output has a conditional branch without a selection merge whose arms are not the loop's merge or continue block.Recompilethen fails in SPIRV-Tools:requireStructuredBranchesintests/Structurizer.cppalready rejects this output. The structurizer's final verification andverifyStructuredintests/ControlFlow.cppaccept it.Minimal reproduction (graph shape only, no shader code)
Using
makeGraphfromcore/libs/prx/libSceAgcDriver/tests/Structurizer.cpp, added tomain()before thediamondsblock:Block 1 is the loop header: it branches to the nested selection 3 or to the shared tail 2. Block 3 branches to the same tail 2 or to 4. Block 4 branches to the return 6 or to the latch 5. The tail 2 goes to the return 6.
As a program, through
Recompile(wave64 compute, the request ofrecompile()intests/ControlFlow.cpp;GraphBuilderbuilds exactly the graph above from these words):What happens
Structurized output for the graph above (ids 1 and 7 are new):
In the final merge assignment (
Structurizer.cpp~1294-1310 ata29ef592) a conditional is exempt from needing a selection merge whenisInnermostLoopControlConditional(line 456) is true, which it is for block 4 because exactly one of its targets is outsideloop->blocks(if (trueInBody != falseInBody) return true;), without requiring that target to be the loop's merge or continue block.exitTailSelectionMerge(line 483) then gives it no merge because the tail, block 3, is not dominated by block 4: it is shared with selection 2. SPIR-V only allows a merge-lessOpBranchConditionalas a direct break or continue, and block 3 is the true arm of selection 2 whose merge is block 4, so the merge of selection 2 branches back into its own construct, which is what the validator reports.Notes
requireStructuredBranchesrule in the structurizer's final verification so this fails as anunsupported CFGthrow instead of an invalid module, and keep the graph above as the regression; (2) clone the two-instruction shared tail for the edge 4 -> 3, as shared tails are cloned outside loops (Split::Cloneprograms intests/ControlFlow.cpp) and asprivatizeSharedReturnsclones small return blocks since #2201, so that block 4's leaving arm can be routed to the loop merge.main, 225 fail therequireStructuredBranchesrule; 203 of those run throughRecompilegave 199 SPIR-V validation failures, 2 valid modules and 2 structurizer refusals. #2471 widens the same class on that corpus (numbers in a comment there).ANYPS5_ENABLE_SPIRV_TOOLS=ON; the shader words were assembled with LLVM 21llvm-mc --triple=amdgcn --mcpu=gfx1013 --mattr=+wavefrontsize64.AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-10T13:53:13Z.
I revised the candidate after checking it against the open #2471: the first version rejected one of that PR's valid shared-loop cases. The current check accepts exits to merges of enclosing structured constructs, and rejects the #2762 graph because its merge-less conditional has no valid enclosing exit.
The updated branch is fix/structurizer-shared-loop-tail-2762, now at
d70036e0, based on current upstreammain(8b812318).main: buildagc_structurizer_testsandagc_shader_control_flow_testsmain: CTestsagc_structurizerandagc_shader_control_flowThe #2762 graph is now rejected during structurization before SPIR-V emission. The candidate is pushed to my fork; the intake workflow still closes new PRs below its merged-PR threshold, so I have not opened an upstream PR. AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by orishamir on 2026-10-10T14:02:19Z.
Measured
d70036e0'sStructurizer.cpp(ControlFlow sources are unchanged betweena29ef592and8b812318) with the fuzzer from the issue: 30,000 random structured programs at 0/16/1,000 words per block and with one 1,000-word block, plus 903 of them as real compute shaders throughRecompilewith SPIRV-Tools validation. Harness: https://github.com/orishamir/anyps5-cfgfuzz.Candidate alone, against
mainThe guard fires only on graphs that
requireStructuredBranchesflags (222 of 225 at 16 words, 227 of 230 at 1,000); it changes no other outcome in 30,000 graphs, and rejects none of the 283 sampled shaders that produce valid SPIR-V onmain.Of 203 sampled shaders that produce invalid SPIR-V on
main, the guard stops 196 before emission. The 3 it misses fail a different validator rule,block exits the loop headed by X, but not via a structured exit. Minimal, asGraphBuilderbuilds it (structurizes onmainand on the candidate, passesrequireStructuredBranchesandverifyStructured):After structurization block 5 is a conditional inside the inner loop (header 3, merge 7, continue 6) that branches to 6 or to 10, the outer loop's continue block. One arm is the inner loop's continue, so both checks accept it; the other arm leaves the inner loop without passing its merge, which SPIR-V rejects. The rule the validator applies is per target: every target of a merge-less conditional must stay inside the innermost construct or be that construct's own merge or continue block.
Candidate merged with #2471 (clean three-way merge)
Of 200 sampled shaders that #2471 moves from the budget error to accepted output, 104 produce invalid SPIR-V with #2471; the guard stops 98 of them, and #2471's own reproducer from my comment there now throws in the structurizer. (On 2 of 30,000 graphs the guard's throw also triggers the retry path, which then succeeds or hits the budget instead; no graph that structurized before is lost.) The 6 it misses all fail
block exits the selection headed by X, but not via a structured exit. Minimal,GraphBuilderform (budget onmain, invalid SPIR-V with #2471, still invalid with #2471 + this guard):After structurization block 3 is a selection with merge 5 and block 4 inside it is a selection with merge 6; block 4 branches to 6 or straight to 5. The jump to the enclosing selection's merge is what the relaxed check accepts ("exits to merges of enclosing structured constructs"); the validator only accepts the innermost construct's merge, or a loop break/continue. Restricting the exemption to the innermost construct would close this one; the loop case above needs the per-target rule.
AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by BharatYadav007 on 2026-10-12T04:12:45Z.
Retested the original seven-block graph from the issue on
mainatd146ec1b6(Windows, MinGW-w64 GCC 15.2.0, Release). I added the exactmakeGraph({{1}, {2, 3}, {6}, {2, 4}, {5, 6}, {1}, {}})reproducer toagc_structurizer_testsand ran it throughStructurizer::Structurizefollowed byrequireStructuredBranches. The test passes; CTestagc_structurizercompleted in 0.07 s.The original graph failure no longer reproduces on this revision. This check does not establish that all graphs from the wider fuzz corpus are fixed, and I have not rerun that corpus or the original RDNA word stream through SPIR-V validation.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T06:18:18Z.
The seven-block graph structures on main since #2471 (retest above). Does your fuzz corpus still produce a case on current main? If not, this can close.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T00:09:02Z.
Closing: the seven-block graph structures on main since #2471 (retest above). If the fuzz corpus still finds a failing graph on current main, comment here with it.