perf(recompiler): compute post-dominators visiting successors first #2105

Merged
Raghav314 merged 1 commit from upstream-pr/2105 into main 2026-10-10 06:13:07 +00:00
Raghav314 commented 2026-10-09 08:40:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Raghav314 on 2026-10-09T08:40:37Z.


What

Structurizer::computePostDominators iterates the post-dominator sets to a fixed point, visiting the blocks in id order. Post-dominance flows from a block's successors back to the block, and successors mostly have higher ids, so each pass moved the sets back by about one block. A long program needed about as many passes as it has blocks, each pass costing O(blocks²). recomputeAnalyses runs it again after every split in splitSharedMergeBlocks, which allows up to four splits per block.

  • The blocks are now visited in post-order of the CFG (successors first), followed by any block the entry does not reach, in id order.
  • Nothing else changes: same equations, same starting sets. The sets start at every block and only shrink, so the iteration converges to the same greatest fixed point whatever the visiting order. It now takes a few passes instead of about one per block.

PPSA36082

After the main menu, loading a level shows a black screen for about 20 s while the game prepares a shader through sceAgcCreateInterpolantMapping → Driver::ResolveShaderAbi → Structurize. All 12 thread stacks I sampled during it were in splitSharedMergeBlocks → recomputeAnalyses → computePostDominators. With this change the black screen lasts about 5 s.

Tested

Linux (CachyOS), x86-64, GCC, 7800X3D / RTX 4090.

  • agc_structurizer (new check): after Structurize, every block's post-dominator set must equal a brute-force one (B post-dominates A when no path from A reaches an exit without passing B). Runs on the existing graphs and a 151-block chain of 50 selections. Starting from the wrong initial sets fails it.
  • The structured output is the same as on main: dumping every block's successors, merge, continue, loop flag and post-dominators for six graphs (a 181-block chain of selections, a chain of 60 branches to one shared block that splitSharedMergeBlocks splits, and four of the existing test graphs) gives identical output, 412 lines.
  • Structurize time, main → this change: a chain of 400 selections (1201 blocks) 3181 ms → 20 ms; 60 branches to one shared block (122 blocks, 181 after splitting) 104 ms → 27 ms.
  • ctest: 501/501.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4798122493 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2105). Originally posted by [Raghav314](https://github.com/Raghav314) on 2026-10-09T08:40:37Z. --- ### What `Structurizer::computePostDominators` iterates the post-dominator sets to a fixed point, visiting the blocks in id order. Post-dominance flows from a block's successors back to the block, and successors mostly have higher ids, so each pass moved the sets back by about one block. A long program needed about as many passes as it has blocks, each pass costing O(blocks²). `recomputeAnalyses` runs it again after every split in `splitSharedMergeBlocks`, which allows up to four splits per block. - The blocks are now visited in post-order of the CFG (successors first), followed by any block the entry does not reach, in id order. - Nothing else changes: same equations, same starting sets. The sets start at every block and only shrink, so the iteration converges to the same greatest fixed point whatever the visiting order. It now takes a few passes instead of about one per block. ### PPSA36082 After the main menu, loading a level shows a black screen for about 20 s while the game prepares a shader through `sceAgcCreateInterpolantMapping` → `Driver::ResolveShaderAbi` → `Structurize`. All 12 thread stacks I sampled during it were in `splitSharedMergeBlocks` → `recomputeAnalyses` → `computePostDominators`. With this change the black screen lasts about 5 s. ### Tested Linux (CachyOS), x86-64, GCC, 7800X3D / RTX 4090. - `agc_structurizer` (new check): after `Structurize`, every block's post-dominator set must equal a brute-force one (B post-dominates A when no path from A reaches an exit without passing B). Runs on the existing graphs and a 151-block chain of 50 selections. Starting from the wrong initial sets fails it. - The structured output is the same as on main: dumping every block's successors, merge, continue, loop flag and post-dominators for six graphs (a 181-block chain of selections, a chain of 60 branches to one shared block that `splitSharedMergeBlocks` splits, and four of the existing test graphs) gives identical output, 412 lines. - `Structurize` time, main → this change: a chain of 400 selections (1201 blocks) 3181 ms → 20 ms; 60 branches to one shared block (122 blocks, 181 after splitting) 104 ms → 27 ms. - ctest: 501/501. ### Checklist - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
Raghav314 commented 2026-10-09 08:58:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Raghav314 on 2026-10-09T08:58:02Z.


The linux and windows failures here are main's own build break, not this change: ShaderFusion.cpp can't find spirv/unified1/spirv.hpp (missing SPIRV-Headers include), and the build stops before anything from this PR is tested. Main's own CI on 6cc0503d fails the same way. #2093 / #2099 fix it, and I'll rebase once one of them lands.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6077754525 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2105#issuecomment-6077754525). Originally posted by [Raghav314](https://github.com/Raghav314) on 2026-10-09T08:58:02Z. --- The linux and windows failures here are main's own build break, not this change: `ShaderFusion.cpp` can't find `spirv/unified1/spirv.hpp` (missing SPIRV-Headers include), and the build stops before anything from this PR is tested. Main's own CI on 6cc0503d fails the same way. #2093 / #2099 fix it, and I'll rebase once one of them lands.
Raghav314 commented 2026-10-09 09:15:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Raghav314 on 2026-10-09T09:15:30Z.


Rebased on main d9484c71 (with #2099), so the SPIRV-Headers build break is gone. Ran the linux job from build.yml locally on the new head (SPIRV tools on, test registration check, build, libs): ctest 504/504 pass.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6078018681 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2105#issuecomment-6078018681). Originally posted by [Raghav314](https://github.com/Raghav314) on 2026-10-09T09:15:30Z. --- Rebased on main d9484c71 (with #2099), so the SPIRV-Headers build break is gone. Ran the linux job from build.yml locally on the new head (SPIRV tools on, test registration check, build, libs): ctest 504/504 pass.
EdouardSence commented 2026-10-09 10:24:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EdouardSence on 2026-10-09T10:24:53Z.


Another title where this is the bottleneck: Rise of the Ronin (PPSA04540), on AMD.

Setup: RX 9070 XT (RADV / Mesa 26.2.4), Ryzen 7 7800X3D, Linux, GCC. Base is main 43972e69 + #1836 + #1837 + #2153, plus local throwing placeholders for a few imports the title never called in these runs. Scripted and headless.

Without this PR (warm shader disk cache):

  • At 200 s the main thread is at 99 % CPU in sceAgcCreateShader → Driver::RegisterShader → PrepareRegistered → PrepareShader → PrepareResourceProgram → Structurize → splitSharedMergeBlocks → recomputeAnalyses → computePostDominators → intersectSorted.
  • Every registration is a cache hit, but after about 800 shaders it only gets through 1 to 6 registrations per 10 s, because Structurize still runs before the cache lookup.
  • After 220 s it has registered 850 shaders and presented no frame.

With this PR (cold cache, since the source version changed):

  • It prepares about 2,470 shaders in the same 220 s, every one a cache miss.
  • The main thread is then out of the recompiler and back in title code.
  • It goes on to the next, unrelated stop (AGC driver: unsupported registered shader type, #1893).

So on this title it goes from stuck at registration to getting through its shader loading.

Tested and written by Claude (AI assistant) on a contributor's machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6079029514 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2105#issuecomment-6079029514). Originally posted by [EdouardSence](https://github.com/EdouardSence) on 2026-10-09T10:24:53Z. --- Another title where this is the bottleneck: Rise of the Ronin (PPSA04540), on AMD. **Setup:** RX 9070 XT (RADV / Mesa 26.2.4), Ryzen 7 7800X3D, Linux, GCC. Base is `main` 43972e69 + #1836 + #1837 + #2153, plus local throwing placeholders for a few imports the title never called in these runs. Scripted and headless. **Without this PR (warm shader disk cache):** - At 200 s the main thread is at 99 % CPU in `sceAgcCreateShader` → `Driver::RegisterShader` → `PrepareRegistered` → `PrepareShader` → `PrepareResourceProgram` → `Structurize` → `splitSharedMergeBlocks` → `recomputeAnalyses` → `computePostDominators` → `intersectSorted`. - Every registration is a cache hit, but after about 800 shaders it only gets through 1 to 6 registrations per 10 s, because `Structurize` still runs before the cache lookup. - After 220 s it has registered 850 shaders and presented no frame. **With this PR (cold cache, since the source version changed):** - It prepares about 2,470 shaders in the same 220 s, every one a cache miss. - The main thread is then out of the recompiler and back in title code. - It goes on to the next, unrelated stop (`AGC driver: unsupported registered shader type`, #1893). So on this title it goes from stuck at registration to getting through its shader loading. *Tested and written by Claude (AI assistant) on a contributor's machine.*
github-actions[bot] commented 2026-10-09 12:39:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T12:39:23Z.


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
#2201 core/libs/prx/libSceAgcDriver/tests/Structurizer.cpp L112
#2521 core/libs/prx/libSceAgcDriver/tests/Structurizer.cpp L76, L112
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6081062772 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2105#issuecomment-6081062772). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-09T12:39:23Z. --- <!-- 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 | |---|---|---|---| | #2201 | `core/libs/prx/libSceAgcDriver/tests/Structurizer.cpp` [L112](https://github.com/boykopovar/AnyPS5/blob/9bb5619c838709d3d2087f1982365bcb201d60f3/core/libs/prx/libSceAgcDriver/tests/Structurizer.cpp#L112) | | | | #2521 | `core/libs/prx/libSceAgcDriver/tests/Structurizer.cpp` [L76](https://github.com/boykopovar/AnyPS5/blob/9bb5619c838709d3d2087f1982365bcb201d60f3/core/libs/prx/libSceAgcDriver/tests/Structurizer.cpp#L76), [L112](https://github.com/boykopovar/AnyPS5/blob/9bb5619c838709d3d2087f1982365bcb201d60f3/core/libs/prx/libSceAgcDriver/tests/Structurizer.cpp#L112) | | |
mrk1987 commented 2026-10-09 14:46:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mrk1987 on 2026-10-09T14:46:02Z.


Independent confirmation from a different title and platform (Windows 11, MinGW-w64 GCC 15.2.0, RTX 5090). I hit the same problem and made the same change (postorder visit, unreachable blocks appended in id order) before finding this PR.

Equivalence on real shaders. I dumped the RecompileRequest of every shader a retail Unreal Engine title registered: 608 compute, pixel and vertex programs. I ran GraphBuilder::Build + Structurizer::Structurize on each with the old and new visit order. I then hashed the result: every block's successors, terminator kind/merge/continue/loop-header, dominators and postDominators. 0 of 608 differ. Total structurize time over the corpus went from 21.7 s to 1.5 s.

Worst cases. These were three ~4,800-instruction compute shaders (589 → 793 blocks after splitting): 6.2–6.3 s → 0.31–0.35 s per Structurize. Per-phase timing put 5.7 s of the 6.2 s in splitSharedMergeBlocks. Over ~200 recomputeAnalyses calls, computePostDominators took 5,866 ms against 119 ms for computeDominatorTree.

Why it matters beyond load time. Driver::RegisterShader holds ShaderPreparationTransaction's process-wide lock while it runs. In this title each of those shaders goes through Structurize three times per registration: the registration pre-check in PrepareRegistered, then getSource's ResourceProgram for each of the two compute variants. That is ~18.5 s under the lock per shader, three shaders back to back, with every UE worker blocked in the ShaderPreparationTransaction constructor. UE's heartbeat hang detector then kills the process. With this change the longest registration in that run drops to 0.8 s.

Tested and written by Claude (Claude Code) on a contributor's machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6083239965 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2105#issuecomment-6083239965). Originally posted by [mrk1987](https://github.com/mrk1987) on 2026-10-09T14:46:02Z. --- Independent confirmation from a different title and platform (Windows 11, MinGW-w64 GCC 15.2.0, RTX 5090). I hit the same problem and made the same change (postorder visit, unreachable blocks appended in id order) before finding this PR. **Equivalence on real shaders.** I dumped the `RecompileRequest` of every shader a retail Unreal Engine title registered: 608 compute, pixel and vertex programs. I ran `GraphBuilder::Build` + `Structurizer::Structurize` on each with the old and new visit order. I then hashed the result: every block's successors, terminator kind/merge/continue/loop-header, `dominators` and `postDominators`. **0 of 608 differ.** Total structurize time over the corpus went from 21.7 s to 1.5 s. **Worst cases.** These were three ~4,800-instruction compute shaders (589 → 793 blocks after splitting): 6.2–6.3 s → 0.31–0.35 s per `Structurize`. Per-phase timing put 5.7 s of the 6.2 s in `splitSharedMergeBlocks`. Over ~200 `recomputeAnalyses` calls, `computePostDominators` took 5,866 ms against 119 ms for `computeDominatorTree`. **Why it matters beyond load time.** `Driver::RegisterShader` holds `ShaderPreparationTransaction`'s process-wide lock while it runs. In this title each of those shaders goes through `Structurize` three times per registration: the registration pre-check in `PrepareRegistered`, then `getSource`'s `ResourceProgram` for each of the two compute variants. That is ~18.5 s under the lock per shader, three shaders back to back, with every UE worker blocked in the `ShaderPreparationTransaction` constructor. UE's heartbeat hang detector then kills the process. With this change the longest registration in that run drops to 0.8 s. *Tested and written by Claude (Claude Code) on a contributor's machine.*
adriadam10 commented 2026-10-10 06:13:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T06:13:04Z.


Checked on current main: build + ctest green (agc_structurizer included). The change only reorders the fixed-point iteration (same equations, same initial sets), so the result is unchanged, as the new brute-force post-dominator check and mrk1987's 608-shader equivalence dump confirm. Timings are confirmed independently by EdouardSence and mrk1987. Merging.

Note: #2201 touches the same spot in tests/Structurizer.cpp; whoever lands second will need a rebase.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094539898 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2105#issuecomment-6094539898). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T06:13:04Z. --- Checked on current main: build + ctest green (agc_structurizer included). The change only reorders the fixed-point iteration (same equations, same initial sets), so the result is unchanged, as the new brute-force post-dominator check and mrk1987's 608-shader equivalence dump confirm. Timings are confirmed independently by EdouardSence and mrk1987. Merging. Note: #2201 touches the same spot in `tests/Structurizer.cpp`; whoever lands second will need a rebase.
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!2105
No description provided.