Skip to content

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

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
Raghav314:perf/recompiler-post-dominators-postorder
Oct 10, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
Raghav314:perf/recompiler-post-dominators-postorder

Conversation

@Raghav314

Copy link
Copy Markdown
Contributor

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

@github-actions github-actions Bot added shader core/shader agc libSceAgc* labels Oct 9, 2026
@Raghav314

Copy link
Copy Markdown
Contributor Author

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 6cc0503 fails the same way. #2093 / #2099 fix it, and I'll rebase once one of them lands.

computePostDominators iterates its sets to a fixed point, visiting the
blocks in id order. Post-dominance flows from a block's successors back to
it, 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 costing O(blocks^2). recomputeAnalyses runs it after every
split of splitSharedMergeBlocks, up to four times per block.

Visit the blocks in post-order of the CFG (successors first), then any the
entry does not reach. The sets start from every block and only shrink, so
the iteration still converges to the same greatest fixed point, now in a
few passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Raghav314
Raghav314 force-pushed the perf/recompiler-post-dominators-postorder branch from 437337e to f57d164 Compare October 9, 2026 09:14
@Raghav314

Copy link
Copy Markdown
Contributor Author

Rebased on main d9484c7 (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

Copy link
Copy Markdown
Contributor

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 43972e6 + #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):

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

github-actions Bot commented Oct 9, 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
#2201 core/libs/prx/libSceAgcDriver/tests/Structurizer.cpp L112
#2521 core/libs/prx/libSceAgcDriver/tests/Structurizer.cpp L76, L112

@mrk1987

mrk1987 commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Collaborator

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.

@adriadam10
adriadam10 merged commit 63cd22f into boykopovar:main Oct 10, 2026
8 checks passed
SleglJan pushed a commit to SleglJan/AnyPS5 that referenced this pull request Oct 10, 2026
…t-dominators-postorder

perf(recompiler): compute post-dominators visiting successors first
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants