perf(recompiler): iterate structurizer fixpoints in post-order and bound shared merge splitting growth #5028

Merged
nikowolf123 merged 4 commits from upstream-pr/5028 into main 2026-10-14 07:45:03 +00:00
nikowolf123 commented 2026-10-13 08:54:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-13T08:54:14Z.


What

The structurizer's attempts can spend seconds to minutes on a small graph before they accept or refuse it. With the oracle described under Tested, about 1,150 of 200,000 random graphs of 2 to 32 blocks take more than 10 s, on main and with #4926 (1,142 and 1,149; the count depends on machine load), and some take more than 300 s. One 20-block graph takes 10.3 s. In a title, that time is spent while the shader is prepared.

A profile of 41 of these graphs puts 73% of the time in computeDominatorTree, 13% in iterationSelectionMerge, 6% in joinWithinIteration and 5% in mergeBesideReturns, all recomputed while shared merge splitting grows the graph. Every slow attempt ends at the split round budget (4 rounds per block at the start of splitting), in one of two ways:

  • 36 of the 41: each round routes a merge through a new junction and adds 13 blocks in the median attempt (an attempt averages 8 to 43 blocks per round), so the graph reaches 31 to 173 times its block count at the start of splitting (one went from 44 to 4,600 blocks).
  • 5 of the 41, the 20-block graph among them: each round adds one forwarder block, ending at 5 times the size.

In production code, this PR changes only Structurizer.cpp:

  • Dominators, post-dominators and the post-dominator fixpoints in mergeBesideReturns, iterationSelectionMerge and joinWithinIteration sweep the blocks in reverse post-order from the entry (dominators) or in post-order (the others), unreachable blocks last in id order. They swept in id order, but splitting appends its blocks at the end, so a sweep carried a set only one new block further. computePostDominators already used this order; analysisOrder replaces its inline copy. Each fixpoint starts from the full set and its equations are monotone, so every sweep order reaches the same greatest fixed point and no output changes. joinWithinIteration also tests membership with a bit vector instead of a linear search, and mergeBesideReturns inserts into its already sorted set.
  • splitSharedMergeBlocks refuses the attempt once the graph has more than 16 times its block count at the start of splitting, or more than 64 blocks when that is larger (CFG shared merge splitting exceeded growth budget). The budget counts blocks, not time. A refused attempt continues exactly as after the existing round budget: the next attempt, then the reach-flag fallback of #4926. With the sweep-order changes alone, 360 of the 1,220 graphs that took more than 10 s in any run on main or #4926 still do.

The growth budget is a heuristic. Succeeding attempts grew at most 8.6 times on these 200,000 graphs, 9.4 times on 200,000 graphs of another seed and 10.6 times on 12,000 graphs of up to 128 blocks. An attempt that would succeed only beyond 16 times now gives a later attempt's output or the fallback's, which is valid and equivalent but not the same. No such graph was found, but shaders are larger than these graphs.

Splitting that adds one block per round stays under the growth budget and still runs all its rounds. On 2,000 random graphs of up to 128 blocks, 9 still take more than 10 s with this PR, at most 63.5 s; with the sweep-order changes alone, 28 take more than 10 s and 13 more than 300 s. The technical debt entry of #4926 now records the budget and this limit.

Tested

WSL2 Ubuntu 26.04, GCC 15.2.0, configured like CI (Release, SPIRV-Tools ON, lavapipe), on top of #4926. No title.

In-tree:

  • agc_structurizer gains three cases. The 20-block graph and a 12-block loop whose 2000-word latch is too large to clone, so that splitting keeps adding junctions, must give structured branches, every original block once and the same executions as the input. The same loop with an unsupported exit, which keeps the reach-flag fallback out, must be refused at the growth budget. On #4926 alone that case fails: it reports the round budget after the graph reached 676 blocks.
  • Built from the test file alone with g++ -O2, the test takes 0.4 s with this PR. On #4926 alone it fails after 16 s (39 s with APS5_NO_PERF_ITERATION_SELECTION=1).
  • ctest -R "structurizer|control_flow": 8 of 8 pass in three runs. agc_structurizer takes 0.3 s, and under 1 s in each of its variants.
  • Full ctest in the CI configuration: 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.
  • tools/check_test_registration.py passes. tools/check_conventions.py --base origin/main passes with both the branch's checker and main's.
  • Rebased onto main cac4e9688 with #4926: full ctest in the CI configuration, 1032 tests, 0 failed. Then onto 4bb2c7f39, where only the technical debt file conflicted: the structurizer, control-flow and shader tests, 18 of 18.
  • Native Windows 11, WinLibs GCC 15.2.0 (SPIRV-Tools OFF): the structurizer, control-flow and shader tests, 18 of 18; agc_structurizer takes 0.5 s and at most 0.9 s in its variants.

Out of tree, with the oracle described in #4926 (graph contract, SPIRV-Tools on a skeleton module at SPIR-V 1.3, 1.4 and 1.6, and an exact equivalence decision), on the same 200,000 graphs (seed 1, 2 to 32 blocks, 10 s per graph), built against main and against this branch and run on the same machine at 16 jobs:

  • main: 81,078 graphs accepted, 117,780 refused, 1,142 past 10 s. The run took 2,235 s.
  • This PR: all 200,000 graphs structured, none refused, none past 10 s. Every output is valid at 1.3 and 1.4 and equivalent to its input; 30,247 contain a conditional with equal targets, which only 1.6 rejects (#4580). The run took 184 s.
  • Outputs, compared by a digest of the full graph: the 81,078 graphs main accepts give byte-identical outputs. Against the run made for #4926, the 198,851 graphs that #4926 finished give byte-identical outputs too.
  • The 1,220 graphs that took more than 10 s on main or with #4926 in either run give the same outputs as this PR built without the growth budget and run until it finished. That build still took more than 10 s on 360 of them, and up to 203 s (12 graphs at a time).
  • On the 1,149 graphs #4926 did not finish within 10 s, each takes 99 ms in the median, 0.34 s at the 90th percentile, 1.0 s at the 99th and 2.1 s at most (8 graphs at a time on a 32-core VM).

The same code, run earlier on other graphs: on 200,000 graphs of seed 2 (2 to 32 blocks), all 199,892 graphs that a build with the sweep-order changes alone finished within 60 s give the same outputs, and the slowest graph takes 2.3 s. On 2,000 graphs of up to 128 blocks, all 1,987 that build finished within 300 s give the same outputs.

A second check on graphs of seed 7, with this branch at ebcb1068c:

  • 80,000 graphs of 2 to 32 blocks, 20,000 for each weight setting of the oracle (every block 0 words, one block 1000 words, every block 1000 words, and the default mix per graph). The 32,606 graphs main accepts give byte-identical outputs, and so do the 79,632 graphs #4926 finished within 10 s. None is refused or takes more than 10 s; main takes more than 10 s on 366. Two runs of 40,000 of these graphs at different job counts give identical outputs, and an AddressSanitizer build gives the same outputs on 5,000 of them without a report.
  • 2,000 graphs of up to 128 blocks (default and every-block-1000-word weights). The build without the growth budget finished 1,976 of them within 120 s, and all 1,976 give the same outputs as this PR. This PR finishes all 2,000; 5 take more than 10 s.

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: #4926
  • Unblocks: none
  • GPUs tested: lavapipe (Mesa 26.0.8, llvmpipe) on WSL2 Ubuntu 26.04; NVIDIA GeForce RTX 5080, driver 616.92, Windows 11 for the structurizer, control-flow and shader tests
  • 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:4840795574 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5028). Originally posted by [nikowolf123](https://github.com/nikowolf123) on 2026-10-13T08:54:14Z. --- ### What The structurizer's attempts can spend seconds to minutes on a small graph before they accept or refuse it. With the oracle described under Tested, about 1,150 of 200,000 random graphs of 2 to 32 blocks take more than 10 s, on `main` and with #4926 (1,142 and 1,149; the count depends on machine load), and some take more than 300 s. One 20-block graph takes 10.3 s. In a title, that time is spent while the shader is prepared. A profile of 41 of these graphs puts 73% of the time in `computeDominatorTree`, 13% in `iterationSelectionMerge`, 6% in `joinWithinIteration` and 5% in `mergeBesideReturns`, all recomputed while shared merge splitting grows the graph. Every slow attempt ends at the split round budget (4 rounds per block at the start of splitting), in one of two ways: - 36 of the 41: each round routes a merge through a new junction and adds 13 blocks in the median attempt (an attempt averages 8 to 43 blocks per round), so the graph reaches 31 to 173 times its block count at the start of splitting (one went from 44 to 4,600 blocks). - 5 of the 41, the 20-block graph among them: each round adds one forwarder block, ending at 5 times the size. In production code, this PR changes only `Structurizer.cpp`: - Dominators, post-dominators and the post-dominator fixpoints in `mergeBesideReturns`, `iterationSelectionMerge` and `joinWithinIteration` sweep the blocks in reverse post-order from the entry (dominators) or in post-order (the others), unreachable blocks last in id order. They swept in id order, but splitting appends its blocks at the end, so a sweep carried a set only one new block further. `computePostDominators` already used this order; `analysisOrder` replaces its inline copy. Each fixpoint starts from the full set and its equations are monotone, so every sweep order reaches the same greatest fixed point and no output changes. `joinWithinIteration` also tests membership with a bit vector instead of a linear search, and `mergeBesideReturns` inserts into its already sorted set. - `splitSharedMergeBlocks` refuses the attempt once the graph has more than 16 times its block count at the start of splitting, or more than 64 blocks when that is larger (`CFG shared merge splitting exceeded growth budget`). The budget counts blocks, not time. A refused attempt continues exactly as after the existing round budget: the next attempt, then the reach-flag fallback of #4926. With the sweep-order changes alone, 360 of the 1,220 graphs that took more than 10 s in any run on `main` or #4926 still do. The growth budget is a heuristic. Succeeding attempts grew at most 8.6 times on these 200,000 graphs, 9.4 times on 200,000 graphs of another seed and 10.6 times on 12,000 graphs of up to 128 blocks. An attempt that would succeed only beyond 16 times now gives a later attempt's output or the fallback's, which is valid and equivalent but not the same. No such graph was found, but shaders are larger than these graphs. Splitting that adds one block per round stays under the growth budget and still runs all its rounds. On 2,000 random graphs of up to 128 blocks, 9 still take more than 10 s with this PR, at most 63.5 s; with the sweep-order changes alone, 28 take more than 10 s and 13 more than 300 s. The technical debt entry of #4926 now records the budget and this limit. ### Tested WSL2 Ubuntu 26.04, GCC 15.2.0, configured like CI (Release, SPIRV-Tools ON, lavapipe), on top of #4926. No title. In-tree: - `agc_structurizer` gains three cases. The 20-block graph and a 12-block loop whose 2000-word latch is too large to clone, so that splitting keeps adding junctions, must give structured branches, every original block once and the same executions as the input. The same loop with an unsupported exit, which keeps the reach-flag fallback out, must be refused at the growth budget. On #4926 alone that case fails: it reports the round budget after the graph reached 676 blocks. - Built from the test file alone with `g++ -O2`, the test takes 0.4 s with this PR. On #4926 alone it fails after 16 s (39 s with `APS5_NO_PERF_ITERATION_SELECTION=1`). - `ctest -R "structurizer|control_flow"`: 8 of 8 pass in three runs. `agc_structurizer` takes 0.3 s, and under 1 s in each of its variants. - Full `ctest` in the CI configuration: 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. - `tools/check_test_registration.py` passes. `tools/check_conventions.py --base origin/main` passes with both the branch's checker and `main`'s. - Rebased onto `main` cac4e9688 with #4926: full `ctest` in the CI configuration, 1032 tests, 0 failed. Then onto 4bb2c7f39, where only the technical debt file conflicted: the structurizer, control-flow and shader tests, 18 of 18. - Native Windows 11, WinLibs GCC 15.2.0 (SPIRV-Tools OFF): the structurizer, control-flow and shader tests, 18 of 18; `agc_structurizer` takes 0.5 s and at most 0.9 s in its variants. Out of tree, with the oracle described in #4926 (graph contract, SPIRV-Tools on a skeleton module at SPIR-V 1.3, 1.4 and 1.6, and an exact equivalence decision), on the same 200,000 graphs (seed 1, 2 to 32 blocks, 10 s per graph), built against `main` and against this branch and run on the same machine at 16 jobs: - `main`: 81,078 graphs accepted, 117,780 refused, 1,142 past 10 s. The run took 2,235 s. - This PR: all 200,000 graphs structured, none refused, none past 10 s. Every output is valid at 1.3 and 1.4 and equivalent to its input; 30,247 contain a conditional with equal targets, which only 1.6 rejects (#4580). The run took 184 s. - Outputs, compared by a digest of the full graph: the 81,078 graphs `main` accepts give byte-identical outputs. Against the run made for #4926, the 198,851 graphs that #4926 finished give byte-identical outputs too. - The 1,220 graphs that took more than 10 s on `main` or with #4926 in either run give the same outputs as this PR built without the growth budget and run until it finished. That build still took more than 10 s on 360 of them, and up to 203 s (12 graphs at a time). - On the 1,149 graphs #4926 did not finish within 10 s, each takes 99 ms in the median, 0.34 s at the 90th percentile, 1.0 s at the 99th and 2.1 s at most (8 graphs at a time on a 32-core VM). The same code, run earlier on other graphs: on 200,000 graphs of seed 2 (2 to 32 blocks), all 199,892 graphs that a build with the sweep-order changes alone finished within 60 s give the same outputs, and the slowest graph takes 2.3 s. On 2,000 graphs of up to 128 blocks, all 1,987 that build finished within 300 s give the same outputs. A second check on graphs of seed 7, with this branch at ebcb1068c: - 80,000 graphs of 2 to 32 blocks, 20,000 for each weight setting of the oracle (every block 0 words, one block 1000 words, every block 1000 words, and the default mix per graph). The 32,606 graphs `main` accepts give byte-identical outputs, and so do the 79,632 graphs #4926 finished within 10 s. None is refused or takes more than 10 s; `main` takes more than 10 s on 366. Two runs of 40,000 of these graphs at different job counts give identical outputs, and an AddressSanitizer build gives the same outputs on 5,000 of them without a report. - 2,000 graphs of up to 128 blocks (default and every-block-1000-word weights). The build without the growth budget finished 1,976 of them within 120 s, and all 1,976 give the same outputs as this PR. This PR finishes all 2,000; 5 take more than 10 s. ### 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: #4926 - [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 for the structurizer, control-flow and shader tests - [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)
houcineBen commented 2026-10-13 17:21:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by houcineBen on 2026-10-13T17:21:56Z.


A data point from a real shader for the case listed as remaining ("splitting that adds one block per round … still runs all its rounds").

vkQuake (LibreQuake) homebrew PPSA99010 on a RADV (Mesa 26.2) build: compute shader 0x20134f400 (1602 dwords, wave32, 8x8 threads, 155 blocks when splitting starts). Header 119 splits merge 151 again in every round, each round adding one forwarder in front of the previous one, until the round budget. agc_shader_replay --cfg on its dump, Windows 11, RTX 4060 Ti, WinLibs GCC 15.2.0:

Build Time
main 9ea71307a ~312 s
this PR (67bb149ae; 516e92675 has the same commits) 18.4 s
this PR + the change below 0.7 s

The title waits 10 s for the dispatch, so 18.4 s still stops it.

The change (+15/−2 in Structurizer.cpp): splitOneSelectionMerge reports the header and merge it split, and splitSharedMergeBlocks fails the attempt with CFG shared merge splitting repeats after 16 consecutive rounds that split the same header and merge and add exactly one block. I checked with the check changed to only record: every attempt it fires in, 6 in agc_structurizer and 6 in this shader, ran on to exceeded budget anyway. So it only ends doomed attempts sooner, and the structured CFGs of all 12 vkQuake dumps we have are byte-identical to this PR's. A test case uses your 20-block forwarder graph with block 12 unsupported (to keep the reach-flag fallback out). It must be refused with the new reason, and on this PR alone it reports exceeded budget: originalBlocks=43 currentBlocks=216 splitBudget=172. Full ctest on top of this PR: 1082 pass, 7 skipped.

I'll open it as a follow-up with Depends on: #5028 once I can (I'm at the first-time author PR limit), unless you'd rather fold it in here.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6151198720 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5028#issuecomment-6151198720). Originally posted by [houcineBen](https://github.com/houcineBen) on 2026-10-13T17:21:56Z. --- A data point from a real shader for the case listed as remaining ("splitting that adds one block per round … still runs all its rounds"). vkQuake (LibreQuake) homebrew PPSA99010 on a RADV (Mesa 26.2) build: compute shader `0x20134f400` (1602 dwords, wave32, 8x8 threads, 155 blocks when splitting starts). Header 119 splits merge 151 again in every round, each round adding one forwarder in front of the previous one, until the round budget. `agc_shader_replay --cfg` on its dump, Windows 11, RTX 4060 Ti, WinLibs GCC 15.2.0: | Build | Time | |---|---| | `main` 9ea71307a | ~312 s | | this PR (67bb149ae; 516e92675 has the same commits) | 18.4 s | | this PR + the change below | 0.7 s | The title waits 10 s for the dispatch, so 18.4 s still stops it. The change (+15/−2 in `Structurizer.cpp`): `splitOneSelectionMerge` reports the header and merge it split, and `splitSharedMergeBlocks` fails the attempt with `CFG shared merge splitting repeats` after 16 consecutive rounds that split the same header and merge and add exactly one block. I checked with the check changed to only record: every attempt it fires in, 6 in `agc_structurizer` and 6 in this shader, ran on to `exceeded budget` anyway. So it only ends doomed attempts sooner, and the structured CFGs of all 12 vkQuake dumps we have are byte-identical to this PR's. A test case uses your 20-block forwarder graph with block 12 unsupported (to keep the reach-flag fallback out). It must be refused with the new reason, and on this PR alone it reports `exceeded budget: originalBlocks=43 currentBlocks=216 splitBudget=172`. Full `ctest` on top of this PR: 1082 pass, 7 skipped. I'll open it as a follow-up with `Depends on: #5028` once I can (I'm at the first-time author PR limit), unless you'd rather fold it in here.
github-actions[bot] commented 2026-10-14 01:38:21 +00:00 (Migrated from github.com)

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


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:6158948040 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5028#issuecomment-6158948040). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-14T01:38:21Z. --- <!-- 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) | | |
nikowolf123 commented 2026-10-14 07:13:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-14T07:13:42Z.


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6162995516 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5028#issuecomment-6162995516). Originally posted by [nikowolf123](https://github.com/nikowolf123) on 2026-10-14T07:13:42Z. --- 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.
oneandonlydean commented 2026-10-14 07:35:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T07:35:52Z.


Reopened with #4926; reviewing both now.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6163336337 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5028#issuecomment-6163336337). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T07:35:52Z. --- Reopened with #4926; reviewing both now.
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!5028
No description provided.