feat(recompiler): structure every control-flow graph the structurizer refuses #4926

Merged
nikowolf123 merged 2 commits from upstream-pr/4926 into main 2026-10-14 07:44:10 +00:00
nikowolf123 commented 2026-10-13 04:36:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-13T04:36:26Z.


What

A structurizer refusal stops the title: Recompile memoizes it per source and the dispatch or draw that needs the shader turns it into a sticky driver failure. The structurizer has grown a ladder of retries (two attempt series of up to four configurations) that each refuse some shapes. Measured on main with the oracle described under Tested, on 200,000 random graphs of 2 to 32 blocks: of the 82,743 reducible graphs whose every block reaches a return, 3,041 (3.7%) are refused. Irreducible graphs and graphs with a cycle that never exits are refused by design (114,667 in that corpus).

This PR adds a construction that structures every graph built from branch, conditional branch and return blocks, and uses it only when the ladder refuses:

  • The graph is decomposed into strongly connected components, recursively: a component's head is its member entered from outside (or the entry), and the rest is decomposed again. Components are emitted in topological order.
  • Each original block keeps exactly one copy and runs behind a guard on its own reach flag (a route variable, as the ladder already uses). Every edge of a block sets the reach flag of its target. Each cyclic component becomes a loop whose single latch repeats it while a back edge set its again flag; an edge out of a loop just sets its target's flag, and the loop's latch exits.
  • Exactly one reach flag is set at any time and it names the next original block of the input execution, so the original blocks run in the same order and each conditional consumes the same decision as in the input, for every branch decision. No block is cloned, so a block with s_barrier keeps one copy.
  • The output uses only selections with a merge, loops with one latch and returns. An empty return ends the function so that every block reaches one; it never runs.
  • Blocks that start the top-level order unconditionally run unguarded. Otherwise SSA would see a static path around the entry block and merge its results with initial values.
  • The ladder runs first, unchanged, and its output is unchanged. If the construction itself fails, the ladder's error is reported as before. APS5_FORCE_STRUCTURIZER_FALLBACK=1 uses the construction for every graph; it is part of the shader cache key.

This is the textbook state-variable construction (Böhm and Jacopini; Ashcroft and Manna) applied to the structurizer's own route variables. It costs about 8 times the input's blocks and one variable per block, which the technical debt entry records with the other limits below.

Limits:

  • Time is roughly quadratic in the block count, from the existing analyses over the larger output: 4.8 ms mean at up to 200 blocks, 0.45 s mean at 500 to 1000.
  • The ladder still runs first. On 1,149 of the 200,000 graphs (1,220 on main) it spends more than 10 s before it accepts or refuses, and this PR does not shorten that.
  • Guarded blocks give resource planning static paths that skip them, so it can plan extra descriptor candidates on graphs that use the fallback.

No open PR changes Structurizer.cpp. #2762 concerns ladder outputs that are invalid SPIR-V; this PR does not change ladder outputs.

Tested

WSL2 Ubuntu 26.04, GCC 15.2.0, configured like CI (Release, SPIRV-Tools ON, lavapipe), and native Windows 11 with WinLibs GCC 15.2.0 on an RTX 5080 (NVIDIA 616.92). No title.

In-tree:

  • New cases in agc_structurizer structure eight graphs that main refuses (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 #2642 two-latch loop, which main accepts. Each must give structured branches, every original block exactly once and the same executions as the input for every branch decision.
  • New agc_structurizer_forced_fallback and agc_shader_control_flow_forced_fallback run the in-tree graphs and the 35 RDNA programs through the fallback, through Recompile and SPIR-V validation before and after spirv-opt, for 32 and 64 lanes.
  • Full ctest in the CI configuration on main e51428bf2: 1014 tests, 0 failed. tools/check_skips.py flags only guest_video_out_high_frame_rate, which skips because the WSL display has no 120 Hz mode, as it does on main.
  • Native full ctest (ANYPS5_REQUIRE_VULKAN=1): 1030 tests, 0 failed, 7 skipped as tools/check_skips.py --job windows expects.
  • The same native suite with APS5_FORCE_STRUCTURIZER_FALLBACK=1 ANYPS5_NO_SHADER_CACHE=1, so that every shader of every GPU execution test is structured by the fallback and runs on the RTX 5080: 1028 of 1030 pass. The two failures check analysis output, not GPU results. agc_shader_memory expects a descriptor phi with two candidates and gets a third from a guarded path that never runs (the planning limit above). agc_scalar_select_mask runs programs through its own evaluator, which does not model route variables.

Out of tree, with an oracle that shares no code with the structurizer (outside the repository; I can attach it). For every output it checks the graph contract (dense ids, exact successors and predecessors, reachability, dominator order, one latch per loop, unique merges, synthetic block shapes), runs SPIRV-Tools on a skeleton module emitted exactly as SpirvFlowEmitter lays out blocks and merges, at SPIR-V 1.3, 1.4 and 1.6, and decides semantic equivalence exactly: a search over pairs of (input block, output block and live route-variable values) that compares the original block reached next for every branch decision, loops included. It was checked against 593,159 mutated outputs and an independent simulator, with no missed difference.

  • main, 200,000 graphs: every one of the 81,072 accepted graphs is valid at 1.3 and 1.4 and equivalent; 30,220 of them contain a conditional with equal targets, which only 1.6 rejects (the #4580 topic). 117,708 graphs are refused, 3,041 of them reducible and terminating, and 1,220 run past 10 s.
  • This PR, same 200,000 graphs: the 81,072 graphs main accepts give byte-identical outputs. Every graph main refuses (117,708) gives a valid, equivalent output. No graph is refused.
  • Every graph forced through the fallback: 2,800,000 graphs over three seeds (up to 64 blocks), all valid at 1.3, 1.4 and 1.6 and equivalent, including irreducible and non-terminating graphs; 20,000 of them also under AddressSanitizer and UBSan. The only rule the outputs break is the oracle's own list of synthetic block shapes, which did not include the final empty return.
  • Rebased onto main cac4e9688: full ctest in the CI configuration, with this PR and its follow-up on top, 1032 tests, 0 failed. Then onto 4bb2c7f39, where only the technical debt file conflicted: the structurizer, control-flow and shader tests, 18 of 18.

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: none
  • Unblocks: none
  • GPUs tested: lavapipe (Mesa 26.0.8, llvmpipe) on WSL2 Ubuntu 26.04; NVIDIA GeForce RTX 5080, driver 616.92, Windows 11
  • AI-assisted: yes. Written with Claude Code; I reviewed every line and ran the tests above.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4838419882 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4926). Originally posted by [nikowolf123](https://github.com/nikowolf123) on 2026-10-13T04:36:26Z. --- ### What A structurizer refusal stops the title: `Recompile` memoizes it per source and the dispatch or draw that needs the shader turns it into a sticky driver failure. The structurizer has grown a ladder of retries (two attempt series of up to four configurations) that each refuse some shapes. Measured on `main` with the oracle described under Tested, on 200,000 random graphs of 2 to 32 blocks: of the 82,743 reducible graphs whose every block reaches a return, 3,041 (3.7%) are refused. Irreducible graphs and graphs with a cycle that never exits are refused by design (114,667 in that corpus). This PR adds a construction that structures every graph built from branch, conditional branch and return blocks, and uses it only when the ladder refuses: - The graph is decomposed into strongly connected components, recursively: a component's head is its member entered from outside (or the entry), and the rest is decomposed again. Components are emitted in topological order. - Each original block keeps exactly one copy and runs behind a guard on its own reach flag (a route variable, as the ladder already uses). Every edge of a block sets the reach flag of its target. Each cyclic component becomes a loop whose single latch repeats it while a back edge set its again flag; an edge out of a loop just sets its target's flag, and the loop's latch exits. - Exactly one reach flag is set at any time and it names the next original block of the input execution, so the original blocks run in the same order and each conditional consumes the same decision as in the input, for every branch decision. No block is cloned, so a block with `s_barrier` keeps one copy. - The output uses only selections with a merge, loops with one latch and returns. An empty return ends the function so that every block reaches one; it never runs. - Blocks that start the top-level order unconditionally run unguarded. Otherwise SSA would see a static path around the entry block and merge its results with initial values. - The ladder runs first, unchanged, and its output is unchanged. If the construction itself fails, the ladder's error is reported as before. `APS5_FORCE_STRUCTURIZER_FALLBACK=1` uses the construction for every graph; it is part of the shader cache key. This is the textbook state-variable construction (Böhm and Jacopini; Ashcroft and Manna) applied to the structurizer's own route variables. It costs about 8 times the input's blocks and one variable per block, which the technical debt entry records with the other limits below. Limits: - Time is roughly quadratic in the block count, from the existing analyses over the larger output: 4.8 ms mean at up to 200 blocks, 0.45 s mean at 500 to 1000. - The ladder still runs first. On 1,149 of the 200,000 graphs (1,220 on `main`) it spends more than 10 s before it accepts or refuses, and this PR does not shorten that. - Guarded blocks give resource planning static paths that skip them, so it can plan extra descriptor candidates on graphs that use the fallback. No open PR changes `Structurizer.cpp`. #2762 concerns ladder outputs that are invalid SPIR-V; this PR does not change ladder outputs. ### Tested WSL2 Ubuntu 26.04, GCC 15.2.0, configured like CI (Release, SPIRV-Tools ON, lavapipe), and native Windows 11 with WinLibs GCC 15.2.0 on an RTX 5080 (NVIDIA 616.92). No title. In-tree: - New cases in `agc_structurizer` structure eight graphs that `main` refuses (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 #2642 two-latch loop, which `main` accepts. Each must give structured branches, every original block exactly once and the same executions as the input for every branch decision. - New `agc_structurizer_forced_fallback` and `agc_shader_control_flow_forced_fallback` run the in-tree graphs and the 35 RDNA programs through the fallback, through `Recompile` and SPIR-V validation before and after spirv-opt, for 32 and 64 lanes. - Full `ctest` in the CI configuration on `main` e51428bf2: 1014 tests, 0 failed. `tools/check_skips.py` flags only `guest_video_out_high_frame_rate`, which skips because the WSL display has no 120 Hz mode, as it does on `main`. - Native full `ctest` (`ANYPS5_REQUIRE_VULKAN=1`): 1030 tests, 0 failed, 7 skipped as `tools/check_skips.py --job windows` expects. - The same native suite with `APS5_FORCE_STRUCTURIZER_FALLBACK=1 ANYPS5_NO_SHADER_CACHE=1`, so that every shader of every GPU execution test is structured by the fallback and runs on the RTX 5080: 1028 of 1030 pass. The two failures check analysis output, not GPU results. `agc_shader_memory` expects a descriptor phi with two candidates and gets a third from a guarded path that never runs (the planning limit above). `agc_scalar_select_mask` runs programs through its own evaluator, which does not model route variables. Out of tree, with an oracle that shares no code with the structurizer (outside the repository; I can attach it). For every output it checks the graph contract (dense ids, exact successors and predecessors, reachability, dominator order, one latch per loop, unique merges, synthetic block shapes), runs SPIRV-Tools on a skeleton module emitted exactly as `SpirvFlowEmitter` lays out blocks and merges, at SPIR-V 1.3, 1.4 and 1.6, and decides semantic equivalence exactly: a search over pairs of (input block, output block and live route-variable values) that compares the original block reached next for every branch decision, loops included. It was checked against 593,159 mutated outputs and an independent simulator, with no missed difference. - `main`, 200,000 graphs: every one of the 81,072 accepted graphs is valid at 1.3 and 1.4 and equivalent; 30,220 of them contain a conditional with equal targets, which only 1.6 rejects (the #4580 topic). 117,708 graphs are refused, 3,041 of them reducible and terminating, and 1,220 run past 10 s. - This PR, same 200,000 graphs: the 81,072 graphs `main` accepts give byte-identical outputs. Every graph `main` refuses (117,708) gives a valid, equivalent output. No graph is refused. - Every graph forced through the fallback: 2,800,000 graphs over three seeds (up to 64 blocks), all valid at 1.3, 1.4 and 1.6 and equivalent, including irreducible and non-terminating graphs; 20,000 of them also under AddressSanitizer and UBSan. The only rule the outputs break is the oracle's own list of synthetic block shapes, which did not include the final empty return. - Rebased onto `main` cac4e9688: full `ctest` in the CI configuration, with this PR and its follow-up on top, 1032 tests, 0 failed. Then onto 4bb2c7f39, where only the technical debt file conflicted: the structurizer, control-flow and shader tests, 18 of 18. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] GPUs tested: lavapipe (Mesa 26.0.8, llvmpipe) on WSL2 Ubuntu 26.04; NVIDIA GeForce RTX 5080, driver 616.92, Windows 11 - [x] AI-assisted: yes. Written with Claude Code; I reviewed every line and ran the tests above. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-14 01:37:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T01:37:49Z.


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 L117
#5049 docs/dev/debt/shader.md L117
#5461 docs/dev/debt/shader.md L117
#5472 docs/dev/debt/shader.md L117
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6158941832 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4926#issuecomment-6158941832). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-14T01:37:49Z. --- <!-- pr-overlap --> 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` [L117](https://github.com/boykopovar/AnyPS5/blob/b27d18c3e4a89f37072130971aa917eaa4f3f5f6/docs/dev/debt/shader.md#L117) | | | | #5049 | `docs/dev/debt/shader.md` [L117](https://github.com/boykopovar/AnyPS5/blob/b27d18c3e4a89f37072130971aa917eaa4f3f5f6/docs/dev/debt/shader.md#L117) | | | | #5461 | `docs/dev/debt/shader.md` [L117](https://github.com/boykopovar/AnyPS5/blob/b27d18c3e4a89f37072130971aa917eaa4f3f5f6/docs/dev/debt/shader.md#L117) | | | | #5472 | `docs/dev/debt/shader.md` [L117](https://github.com/boykopovar/AnyPS5/blob/b27d18c3e4a89f37072130971aa917eaa4f3f5f6/docs/dev/debt/shader.md#L117) | | |
oneandonlydean commented 2026-10-14 02:36:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:36:07Z.


Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159624895 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4926#issuecomment-6159624895). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:36:07Z. --- Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!4926
No description provided.