Skip to content

test(recompiler): check seeded structurizer outputs for exact equivalence - #4964

Closed
nikowolf123 wants to merge 3 commits into
boykopovar:mainfrom
nikowolf123:test/structurizer-equivalence
Closed

nikowolf123 wants to merge 3 commits into
boykopovar:mainfrom
nikowolf123:test/structurizer-equivalence

Conversation

@nikowolf123

@nikowolf123 nikowolf123 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

What

#4926 makes Structurizer::Structurize structure every graph built from branch, conditional branch and return blocks: the existing ladder runs first, and a reach-flag construction takes over when the ladder refuses. The in-tree tests check hand-picked graphs (agc_structurizer) and RDNA programs (agc_shader_control_flow). The evidence that both paths keep every execution of the input came from an oracle outside the repository. This PR adds a condensed version of that oracle as a test, so every CI run checks the same properties on 2000 graphs.

agc_structurizer_equivalence builds its graphs from fixed seeds (no clock, no random_device). Each graph has 2 to 24 blocks:

  • 1000 random graphs: a random spanning tree from a random entry block (not block 0 in 785 of them), plus forward edges, back edges to tree ancestors, returns, and conditional branches whose two targets are the same block.
  • 1000 graphs laid out like compiled code: fall-through, forward branches of up to six blocks, back edges to the same block or one of the seven before it, and early returns. Unreachable blocks are removed.
  • Across both sets, 277 graphs are irreducible and 506 have blocks that never reach a return. Word estimates are zero in 972 graphs. The others have one block or every block at 1000 words, or a mix from 0 to 2000 per block, so the ladder both clones blocks and routes edges. In the default configuration, 683 of the 2000 graphs end up in the fallback.

Structurize must succeed on a copy of each graph. The output must then pass three checks, none of which reuses structurizer code:

  • Graph contract:
    • ids are dense and the entry is block 0;
    • successors are the sorted, unique branch targets, and predecessors are their exact inverse;
    • every block is reachable, reaches a return, and comes after its immediate dominator, and the graph is reducible;
    • each loop header is an empty branch with valid, distinct merge and continue blocks and exactly one back edge, which comes from the continue block; the continue block branches only to the header or the merge;
    • merges are unique and sit only on loop headers and conditional branches, and a conditional branch without a merge leaves its innermost loop;
    • empty blocks only forward, set or test a route variable, except for at most one empty return;
    • every original block has a copy, and every non-empty block is an unchanged copy of an original block.
  • Exact equivalence. A breadth-first search runs over pairs of input block and output block, together with the values of the route variables that are still live. Between original blocks it follows the empty blocks: an assignment writes before a dispatch reads, and reading an unset variable or looping through empty blocks fails. At every conditional it takes both decisions on both sides, loops included, and compares the next original block by instruction range, program counters, terminator kind and condition. The state space is finite, so this covers every decision sequence of any length. A mismatch reports the shortest decision sequence, for example after decisions "--F-T" through original blocks 9 13 8 5 6 15: the input runs original block 15 but the structured graph runs block 32 with instructions [22, 24).
  • SPIR-V, when the build has SPIRV-Tools (ANYPS5_ENABLE_SPIRV_TOOLS, the same switch under which Recompile validates the modules of agc_shader_control_flow). The test emits a skeleton module with the blocks laid out the way SpirvFlowEmitter emits them: the two-block entry prologue, OpLoopMerge on loop headers, OpSelectionMerge on the other conditional branches that have a merge, OpBranchConditional on OpUndef booleans, and OpReturn for returns. The module must validate for SPIR-V 1.3 (Vulkan 1.1) and 1.4. Since fix(shader): never branch conditionally to one label twice #4580 the emitter writes OpBranch for a conditional branch whose two targets are the same block, and the skeleton does the same. The SPIR-V 1.6 rule against equal labels therefore cannot fire, and 1.6 is not checked separately.

agc_structurizer_equivalence_forced_fallback runs the same graphs with APS5_FORCE_STRUCTURIZER_FALLBACK=1. Both tests are registered like the neighbouring structurizer tests: ANYPS5_NO_SHADER_CACHE=1, the source tree as working directory, and libc on PATH on Windows. SPIRV-Tools is linked only when it is enabled.

The corpus has limits that keep the default run at about 2 s:

  • at most six conditional branches per graph;
  • small random graphs are more frequent than large ones (1297 graphs of 2 to 8 blocks, 601 of 9 to 16, 102 of 17 to 24);
  • no targets drawn uniformly at random.

Without these limits, the ladder can spend tens of seconds on a single graph of 11 to 24 blocks before it accepts or refuses it (42 s on one 20-block graph). That is the ladder runtime limit #4926 lists, and this test does not measure it. GraphBuilder shapes (jump-table compare chains, the empty end block) stay covered by agc_shader_control_flow.

Tested

WSL2 Ubuntu 26.04 with GCC 15.2.0, configured like CI (Release, SPIRV-Tools ON, ccache), on top of #4926. Native Windows 11 with WinLibs GCC 15.2.0, SPIRV-Tools OFF as in the Windows CI job. No title.

  • Linux, ctest -R 'structurizer|control_flow': 10 of 10 pass in three runs. agc_structurizer_equivalence takes 2.0 s and agc_structurizer_equivalence_forced_fallback 1.0 s, with the same times under --parallel 4.
  • Windows, the same 10 tests: all pass. The two new tests take 2.7 s and 0.3 s.
  • tools/check_test_registration.py passes on both build trees. tools/check_conventions.py --base origin/main passes, with both the branch's checker and main's.
  • Both platforms structure the 2000 graphs into identical outputs in both configurations.
  • False positives: the test's three checks and the out-of-tree oracle (its block-shape rule extended to the fallback's empty return) agree on every output of the 2000 graphs in both configurations and of 5983 graphs from the oracle's own generators (up to 20 blocks, default configuration). On 247,054 outputs mutated by the oracle's 22 mutation kinds they disagree only on 7, which the structurizer's own hasStructuredConstructs rejects and SPIRV-Tools accepts.
  • Sensitivity: I built each mutant below into a scratch copy of Structurizer.cpp and ran both tests on it. The counts are failing graphs out of 2000. On the same outputs I also ran the contract, the equivalence search and SPIR-V validation one at a time, to show which check catches which mutant.
Mutant in Structurizer.cpp agc_structurizer_equivalence ..._forced_fallback Caught by
Fallback: swap the true and false targets of every original conditional 616 fail 1556 fail equivalence
Fallback: invert the initial reach flags 589 fail 1358 fail equivalence
Fallback: a guard sets its reach flag instead of clearing it 426 fail 803 fail equivalence
Fallback: the loop latch repeats when its again flag is clear 679 fail 1365 fail equivalence, partly as "loops forever through empty blocks"
Ladder: swap the targets of route dispatches in appendJunction 170 fail pass (ladder not run) equivalence
Ladder: routed edges assign the inverted route value 46 fail pass equivalence
Ladder: loop exits assign 0 instead of 1 138 fail pass equivalence
Fallback: guards lose their selection merge, structurizer self-check off 664 fail 1963 fail contract and SPIR-V 1.3 and 1.4
Ladder: exit-tail selections lose their merge, self-check off 123 fail pass contract and SPIR-V
Ladder: every selection loses its merge, self-check off 655 fail pass contract and SPIR-V
hasStructuredConstructs always returns true 26 fail pass contract 22, SPIR-V 25
orderByDominance never reorders 673 fail pass contract 673, equivalence 500, SPIR-V 388
Fallback: skip orderByDominance pass pass none

"Self-check off" means hasStructuredConstructs also returns true. Without that, the structurizer rejects its own output, Structurize throws, and the test fails on that instead.

The last mutant survives because it changes nothing: the fallback already emits blocks in dominance order, and orderByDominance returns early on such graphs. When orderByDominance never reorders, 500 outputs keep their entry at another block than block 0; the equivalence search starts at block 0, as the translator does, and catches all of them.

Four outputs of the hasStructuredConstructs mutant break construct rules that only SPIRV-Tools checks: a branch into a selection construct that bypasses its header, and a loop exit that is not a structured exit. Builds without SPIRV-Tools, such as the Windows CI job, do not check these two rules. In the other direction, the contract rejects one output that SPIRV-Tools accepts: a conditional branch without a merge that does not leave its loop.

Of 21 further mutants (edge redirection, clone edges, reuse and clearing of route variables, the fallback's again flags, guards and loop heads, and single hasStructuredConstructs rules), 17 fail the test. The other 4 change no behaviour: the oracle accepts all of their outputs too. On the outputs of 12 of these mutants and of the last six mutants in the table, the oracle rejects no output that the test accepts.

Rebased with #4926 onto main 30f9612. The conflicts were in docs/dev/debt/shader.md, where main added entries next to #4926's, and earlier the test list in core/libs/prx/libSceAgcDriver/CMakeLists.txt; both sides are kept and the changed lines are identical. WSL2 Ubuntu 26.04, lavapipe (Mesa 26.0.8), CI configuration: the structurizer, control-flow and shader tests 20 of 20, full ctest 1087 of 1087.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: feat(recompiler): structure every control-flow graph the structurizer refuses #4926
  • Unblocks: none
  • GPUs tested: n/a (the new test runs on the CPU only)
  • AI-assisted: yes. Written with Claude Code; I reviewed every line and ran the tests above.

🤖 Generated with Claude Code

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed build shader core/shader docs agc libSceAgc* labels Oct 13, 2026
@nikowolf123
nikowolf123 force-pushed the test/structurizer-equivalence branch 2 times, most recently from 3267398 to ff7ab88 Compare October 13, 2026 08:58
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 13, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 13, 2026
@nikowolf123
nikowolf123 force-pushed the test/structurizer-equivalence branch from ff7ab88 to 51c01e4 Compare October 13, 2026 15:35
@nikowolf123
nikowolf123 force-pushed the test/structurizer-equivalence branch from 51c01e4 to 071a735 Compare October 13, 2026 16:21
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 13, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 13, 2026
nikowolf123 and others added 3 commits October 14, 2026 00:59
When every attempt of the structurizer ladder refuses a graph, the title
aborts. Rebuild such a graph from a hierarchical decomposition into
strongly connected components instead: each original block keeps one copy
and runs behind a guard on its own reach flag, and each component becomes
a loop with a single latch that repeats it while a back edge set its again
flag. Every edge sets the reach flag of its target, so exactly one flag
names the next original block and the blocks run in the input's order for
every branch decision. Irreducible and non-terminating graphs are handled
the same way, and no block is cloned.

The ladder runs first and its output is unchanged. The fallback only runs
when both attempt series refuse; if it cannot structure the graph, the
ladder's error is reported as before. Blocks that start the top-level
order unconditionally run unguarded, so the entry block's results reach
later blocks without a stale path. APS5_FORCE_STRUCTURIZER_FALLBACK=1
uses the construction for every supported graph, for tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…zer fallback

Structurize eight graphs the ladder refuses on main (a duplicate merge, a
region that wraps around to its dominator, the merge-splitting budget, a
conditional without a structured merge, an irreducible entry and three
loops that never exit) and the boykopovar#2642 two-latch loop, which the ladder
accepts. Each must give structured branches, every original block exactly
once and the same executions as the input for every branch decision.

Register agc_structurizer_forced_fallback and
agc_shader_control_flow_forced_fallback, which run the in-tree graphs and
RDNA programs, through Recompile and SPIR-V validation, with
APS5_FORCE_STRUCTURIZER_FALLBACK=1. Under that switch the split-kind
checks become checks that no block is cloned and a route variable is
used, and the nested-selection check names blocks by their original
instructions instead of their position.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ence

Structure 2000 seeded graphs of 2 to 24 blocks and check every output
against its input. Half are random graphs with a random entry block,
loops, irreducible cycles and blocks that never return; half are laid
out like compiled code, with fall-through, forward branches and back
edges. Graphs have at most six conditional branches and smaller random
graphs are more frequent: on denser graphs the structurizer's retries
take seconds per graph.

Each output must:
- keep the graph contract: dense ids from entry block 0, successors and
  predecessors that match the branches, every block reachable and
  reaching a return, every block after its immediate dominator, loop
  headers that are empty branches with distinct merge and continue
  blocks and one back edge from the continue block, unique merges,
  conditional branches without a merge that leave their loop, and empty
  blocks that only forward, set or test a route variable, or one empty
  return; every original block keeps at least one unchanged copy;
- run the same original blocks as the input for every branch decision:
  a search over pairs of input block, output block and the live route
  variable values, through the empty blocks between original blocks,
  compares the next original block on both sides of every conditional,
  loops included, and names the shortest decision sequence on a
  mismatch; reading an unset route variable or looping through empty
  blocks fails;
- with SPIRV-Tools, pass validation for SPIR-V 1.3 and 1.4 as a skeleton
  module laid out the way SpirvFlowEmitter emits blocks, merges and
  branches.

agc_structurizer_equivalence_forced_fallback runs the same graphs with
APS5_FORCE_STRUCTURIZER_FALLBACK=1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nikowolf123
nikowolf123 force-pushed the test/structurizer-equivalence branch from 071a735 to f41e2d9 Compare October 13, 2026 23:07
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 13, 2026
@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#4806 docs/dev/debt/shader.md L116
#4932 core/libs/prx/libSceAgcDriver/CMakeLists.txt L1280
#5049 docs/dev/debt/shader.md L116
#5461 docs/dev/debt/shader.md L116
#5472 docs/dev/debt/shader.md L116

@nikowolf123

Copy link
Copy Markdown
Contributor Author

Closing this together with #4926, which it builds on, following the request there to hold off on pull requests that do not name a title needing the change while the queue is this long. The branch stays; if a title stops on refused control flow on current main, I will reopen with the title ID and the log line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build docs shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant