Skip to content

perf(recompiler): look up dominance by binary search and track visited blocks in a bitmap - #2276

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
hugodearaujo:perf/recompiler-dominance-lookups
Oct 10, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
hugodearaujo:perf/recompiler-dominance-lookups

Conversation

@hugodearaujo

@hugodearaujo hugodearaujo commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

What

Two lookups inside Structurize searched vectors linearly:

  • ControlFlowGraph::Dominates and PostDominates used std::find on a block's dominators / postDominators. Those vectors are always sorted: both analyses end every update with sortUnique or intersectSorted, and applyBlockOrder re-sorts them through remapIds after renumbering. They now use std::binary_search.
  • dominatedBlocks checked whether a block was already visited with contains(blocks, id) on the growing result vector, so each call was quadratic in the region size. It now keeps a std::vector<bool> indexed by block id. The result is still sorted with sortUnique before it is returned.

Nothing else changes, and the structured graph is identical (see below).

This does not get any title further. It only makes the structurizer faster. I found it while investigating Hades (PPSA03355), which still crashes at the same place with this change. It is a separate problem: the title's own texture upload pool fills up, which I am still investigating.

Measured

Hades (PPSA03355 v01.003) on Linux, Ryzen 7 7800X3D. On main it stops earlier (#1798, then #861), so the runs below are on main + #1798 + #861. Most of the time is spent in Structurize for one pixel shader (code 0x3d0087300, 258 blocks, 511 after splitSharedMergeBlocks), on the game's render thread at registration. This is the same path that #2105 speeds up from another side.

base before after
main + #1798 + #861 ~14.4 s ~13.2 s (−8 %)
same + #2105 ~1.67 s ~1.06 s (−37 %)

Two runs per row, timed around Structurize in PrepareResourceProgram (local instrumentation, not part of this PR). Without #2105, the post-dominator iteration that #2105 fixes takes most of the time, so the gain here is small. With #2105, the lookups changed here are the largest remaining cost: before this change, dominatedBlocks alone took ~735 ms of 1.6 s, over 32 386 calls.

The text dump of the structured graph (GraphToString, 688 843 bytes) has the same FNV-1a hash (0782e3d419460587) in all four configurations.

Tested

On main f6af6d11d with this commit:

  • Linux (Arch, GCC 16.2, Release): ctest 678/678, including agc_structurizer and agc_shader_control_flow.
  • Windows (Server 2025 VM, WinLibs GCC 15.2, CI flags, no GPU): 688 of 689 passed. The one failure is agc_driver_prepared_shaders_deferred_undecodable, which stops at vkCreateInstance: Vulkan result -9 because the VM has no Vulkan device. The other Vulkan tests skip in that case; this one does not.
  • Hades: same structured output as above. The title still crashes in its upload pool, as on the base without this change.

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. perf(recompiler): compute post-dominators visiting successors first #2105 touches computePostDominators in the same file; the two merge without conflict.
  • AI-assisted: yes

🤖 Generated with Claude Code

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the idea is sound: the dominators/postDominators vectors are always sorted (sortUnique/intersectSorted, and remapIds re-sorts), so binary_search is equivalent, and the bitmap in dominatedBlocks keeps the result identical.

One blocker: the PR doesn't link.

  • core/shader/recompiler/ControlFlow/src/ControlFlowGraph.cpp:17: removing contains is wrong, it is still declared in ControlFlow/ControlFlowHelpers.hpp:12 and used about 20 times in Structurizer.cpp (e.g. lines 182, 278, 296, 359, 465, 608, 743). On the PR merged onto current main, agc_structurizer_tests and the other test executables fail with undefined reference to ShaderRecompiler::contains(std::vector<unsigned int> const&, unsigned int).

Please keep contains (or move all its callers to something else), rebuild, and rerun ctest; the "513/513" in Tested can't hold with this diff. Happy to look again once it builds.

…d blocks in a bitmap

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hugodearaujo
hugodearaujo force-pushed the perf/recompiler-dominance-lookups branch from 7ec027c to 8285335 Compare October 10, 2026 08:26
@hugodearaujo

Copy link
Copy Markdown
Contributor Author

Thanks, you're right. contains became a shared helper declared in ControlFlowHelpers.hpp after this branch was cut, and removing it breaks the link on current main. Rebased onto main f6af6d11d with contains kept as it is there, so the diff is now only the binary_search in Dominates/PostDominates and the bitmap in dominatedBlocks (head 82853352). ctest 678/678 on Linux, agc_structurizer included. The Tested section is updated.

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked the rebased head: dominators/postDominators are always sorted (allBlockIds, intersectSorted, remapIds), so binary_search is equivalent, and the bitmap in dominatedBlocks keeps the result identical. Build + ctest of libSceAgc/libSceAgcDriver (including agc_structurizer) green on the PR merged onto current main; conventions CI green. Merging.

@adriadam10
adriadam10 merged commit c5d77d6 into boykopovar:main Oct 10, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants