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

Merged
hugodearaujo merged 1 commit from upstream-pr/2276 into main 2026-10-10 08:43:48 +00:00
hugodearaujo commented 2026-10-09 14:17:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hugodearaujo on 2026-10-09T14:17:18Z.


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. #2105 touches computePostDominators in the same file; the two merge without conflict.
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4801662660 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2276). Originally posted by [hugodearaujo](https://github.com/hugodearaujo) on 2026-10-09T14:17:18Z. --- ### 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 - [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. #2105 touches `computePostDominators` in the same file; the two merge without conflict. - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
adriadam10 commented 2026-10-10 06:01:23 +00:00 (Migrated from github.com)

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

Review: CHANGES_REQUESTED


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5477884562 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2276#pullrequestreview-5477884562). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T06:01:23Z. **Review:** CHANGES_REQUESTED --- 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.
hugodearaujo commented 2026-10-10 08:32:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hugodearaujo on 2026-10-10T08:32:51Z.


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6095712160 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2276#issuecomment-6095712160). Originally posted by [hugodearaujo](https://github.com/hugodearaujo) on 2026-10-10T08:32:51Z. --- 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 commented 2026-10-10 08:43:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T08:43:45Z.

Review: APPROVED


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5478356573 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2276#pullrequestreview-5478356573). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T08:43:45Z. **Review:** APPROVED --- 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.
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!2276
No description provided.