perf(recompiler): memoize SRT evaluation in a flat table and inline IrValue's hot accessors #415

Merged
oneandonlydean merged 1 commit from upstream-pr/415 into main 2026-10-03 22:31:51 +00:00
oneandonlydean commented 2026-10-02 22:06:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-02T22:06:20Z.


What

Every draw stage's capture walks the shader's SRT plan with Detail::Evaluator. Its per-value memo was a std::unordered_map<IrValue*, std::uint64_t>: one node allocation per evaluated value. Every step of the walk also made out-of-line calls to IrValue::Opcode, IsIdentity, Resolve and Argument. In a profile of my integration branch at PPSA21564's title, the walk took about 17 us of every capture, about 1.25 s of the graphics queue worker's 10 s.

  • The memo is now Detail::EvaluatedValues, an open-addressing table keyed by the value pointer (linear probing, power-of-two size starting at 64, grown at half load). Insert keeps the first value for a key, as emplace did, and failed evaluations are still not memoized.
  • IrValue::Opcode, Type, HasImmediate, IsIdentity, ArgumentCount, Argument and Resolve move to the header as inline definitions, unchanged, including their throws.
  • <unordered_map> is no longer used by SrtEvaluator.hpp and is dropped.

Evaluation order, results and failures are unchanged. In the same profile the capture walk drops to about 10 us (title capture total about 1.25 s to 0.7 s per 10 s).

Test: verifyEvaluatedValues in agc_shader_memory_tests inserts 1000 keys (several grows), finds each with its value (including a stored 0), checks that a second insert does not replace the first, and that empty tables and absent keys find nothing. The existing SRT walker tests cover the evaluator itself.

Relation to #218 (slick-daddy, "cut hot-path overhead in recompile cache lookups"): the two are complementary. #218 changes the evaluator's cycle stack (_visiting, a vector to an unordered_set) and the cache key, snapshot hash and memo lookups in Recompiler.cpp. This PR changes the evaluator's value memo (_cache) and the IrValue accessors. They touch adjacent lines in SrtEvaluator.hpp only: the member pair and the include block. Whichever lands second resolves to EvaluatedValues _cache; beside #218's std::unordered_set<IrValue*> _visiting;, with <unordered_set> kept. SrtEvaluator.cpp merges cleanly.

Shader disk cache: on main the cache's SourceVersion is a hash of the recompiler and AGC driver sources, so this change, like any edit there, gives a new version and cached shaders are recompiled once. The disk key format and the cached data are unchanged.

Tested

  • Linux (CachyOS, GCC 16), RTX 4090: ctest 125/125 pass, including the new verifyEvaluatedValues in agc_shader_memory_tests.
  • PPSA21564: on my integration branch through repeated passes of my visual regression check (intro, title, menu and a level). The timings above come from my profiling runs there.

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 (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4721969910 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/415). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-02T22:06:20Z. --- ### What Every draw stage's capture walks the shader's SRT plan with `Detail::Evaluator`. Its per-value memo was a `std::unordered_map<IrValue*, std::uint64_t>`: one node allocation per evaluated value. Every step of the walk also made out-of-line calls to `IrValue::Opcode`, `IsIdentity`, `Resolve` and `Argument`. In a profile of my integration branch at PPSA21564's title, the walk took about 17 us of every capture, about 1.25 s of the graphics queue worker's 10 s. - The memo is now `Detail::EvaluatedValues`, an open-addressing table keyed by the value pointer (linear probing, power-of-two size starting at 64, grown at half load). `Insert` keeps the first value for a key, as `emplace` did, and failed evaluations are still not memoized. - `IrValue::Opcode`, `Type`, `HasImmediate`, `IsIdentity`, `ArgumentCount`, `Argument` and `Resolve` move to the header as inline definitions, unchanged, including their throws. - `<unordered_map>` is no longer used by `SrtEvaluator.hpp` and is dropped. Evaluation order, results and failures are unchanged. In the same profile the capture walk drops to about 10 us (title capture total about 1.25 s to 0.7 s per 10 s). Test: `verifyEvaluatedValues` in `agc_shader_memory_tests` inserts 1000 keys (several grows), finds each with its value (including a stored 0), checks that a second insert does not replace the first, and that empty tables and absent keys find nothing. The existing SRT walker tests cover the evaluator itself. Relation to #218 (slick-daddy, "cut hot-path overhead in recompile cache lookups"): the two are complementary. #218 changes the evaluator's cycle stack (`_visiting`, a vector to an `unordered_set`) and the cache key, snapshot hash and memo lookups in `Recompiler.cpp`. This PR changes the evaluator's value memo (`_cache`) and the `IrValue` accessors. They touch adjacent lines in `SrtEvaluator.hpp` only: the member pair and the include block. Whichever lands second resolves to `EvaluatedValues _cache;` beside #218's `std::unordered_set<IrValue*> _visiting;`, with `<unordered_set>` kept. `SrtEvaluator.cpp` merges cleanly. Shader disk cache: on `main` the cache's `SourceVersion` is a hash of the recompiler and AGC driver sources, so this change, like any edit there, gives a new version and cached shaders are recompiled once. The disk key format and the cached data are unchanged. ### Tested - Linux (CachyOS, GCC 16), RTX 4090: `ctest` 125/125 pass, including the new `verifyEvaluatedValues` in `agc_shader_memory_tests`. - PPSA21564: on my integration branch through repeated passes of my visual regression check (intro, title, menu and a level). The timings above come from my profiling runs there. ### 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 (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-02 23:47:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-02T23:47:25Z.


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
#218 core/shader/recompiler/Optimization/include/Optimization/SrtWalker/SrtEvaluator.hpp L9, L36-37
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5963246587 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/415#issuecomment-5963246587). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-02T23:47:25Z. --- <!-- 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 | |---|---|---|---| | #218 | `core/shader/recompiler/Optimization/include/Optimization/SrtWalker/SrtEvaluator.hpp` [L9](https://github.com/boykopovar/AnyPS5/blob/27c502636734563f5ccda670c87443fe6551c40a/core/shader/recompiler/Optimization/include/Optimization/SrtWalker/SrtEvaluator.hpp#L9), [L36-37](https://github.com/boykopovar/AnyPS5/blob/27c502636734563f5ccda670c87443fe6551c40a/core/shader/recompiler/Optimization/include/Optimization/SrtWalker/SrtEvaluator.hpp#L36-L37) | | |
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!415
No description provided.