perf(recompiler): index memory users once per indirect image planning pass #2813

Merged
SleglJan merged 1 commit from upstream-pr/2813 into main 2026-10-10 22:45:26 +00:00
SleglJan commented 2026-10-10 16:00:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-10T16:00:22Z.


What

MemoryIndexBelongsTo in ResourceTracker.cpp walked every instruction of the program on each call and asked BufferAccessOf, AddressOpcodeInfoOf and ImageOpcodeInfoOf about each one. TryMakeTableImage calls it for each of a table handle's eight descriptor dwords and once more for a material key. PlanIndirectImages calls TryMakeTableImage for every image instruction whose handle is not planned yet, so each such instruction could cost up to nine walks of the whole program. This runs once per shader per process, also when the shader disk cache hits.

  • PlanIndirectImages now indexes the instructions that access memory by their memory index once, before it plans. While the pass runs, MemoryIndexBelongsTo looks the index up instead of walking the program; the predicate is the same.
  • The planning pass adds, removes and changes no instruction, so the index is valid until the pass drops it at its end. Outside the pass, where the function has no caller, it walks the program as before.

Issue: #2774. Overlap: #757 and #758 restructure this pass (PlanTableColumns) and conflict with this commit in one hunk (#757) and two (#758). #1184 and #2653 also change ResourceTracker.cpp, in other functions, and merge with this commit without conflict in that file.

Tested

  • Tests. Linux (CachyOS, kernel 7.2.9, GCC 16.2.1), Ryzen 9 7950X3D (16 cores, 32 threads), RTX 3080 (NVIDIA 615.71.09), Release with BUILD_TESTING=ON and ANYPS5_ENABLE_SPIRV_TOOLS=ON, this branch on main 57f96fd2: of 701 tests, 696 pass, four video_out_flip_* tests (lifetime, reopen, controls, overlay) skip (no Vulkan in SDL's offscreen video driver), and agc_driver_dcc_keys_first_write fails, which is #2767 (NVIDIA, Linux). agc_shader_bindless, agc_shader_memory and agc_driver_bindless_images cover the table-planning path. tools/check_conventions.py --base upstream/main and git diff --check pass.
  • 007 First Light. PPSA11386 v01.018.000 in a bubblewrap sandbox, on the RTX 3080, Release with SPIRV-Tools and testing OFF.
    • The base is main a29ef592 with the stack listed in #2523 that gets 007 to its first flip, with ResourceTracker.cpp as on main; that file is the same at a29ef592 and 57f96fd2.
    • The commit is the only difference between the two builds. It was measured in an earlier revision that differs only in form: the fallback walk now calls AccessesMemory (the same condition), the lookup uses std::ranges::all_of, and a redundant clear() was dropped.
    • The measure is the process lifetime from exec to the first flip, where 007 currently stops.
Without With this commit
warm shader cache, 4 + 4 interleaved runs (s) 22.9 24.9 24.1 22.7 (median 23.5) 15.6 15.2 16.4 15.6 (median 15.6)
first run, cold shader cache (s) 27.4 16.0
CPU time of all threads, one warm run (s) 319 169
CPU time of the main thread, same run (s) 14.9 7.6
  • Same output. Both builds' shader caches hold the same 1,956 requests. Paired by request, the stored results are byte-identical except CompiledShaderInfo::shaderHash, which holds the shader's code address; 8 pairs are fully identical. As a control, two cold runs of one build with #1184's tracker hunk (below) differ from each other only in address fields too.
  • Failure log. In every run, the build with this commit logs 3 more preparation failures than the build without (79 vs 76). They are shaders that the build without this commit is still preparing when 007 stops (replaying each of their three captured requests offline with agc_shader_replay, 2 runs each on an idle machine, takes 18.3 to 19.9 s without this commit and 2.2 to 2.3 s with it, ending in the same offline error).
  • With #1184's tracker hunk. On the 007 stack's own ResourceTracker.cpp, which carries part of #1184, the same change (the same earlier revision) gives a warm median of 24.6 → 16.2 s over 4 + 4 runs. With a two-line logging probe, both builds started 2,514 background preparations, of which 2,478 succeeded.

Checklist

  • Based on current main; no other open PR implements the same change
  • 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 (Claude Code)

🤖 Generated with Claude Code

https://claude.ai/code/session_01Fnrk5Dazjb2tLooVSpdRQq

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4812537322 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2813). Originally posted by [SleglJan](https://github.com/SleglJan) on 2026-10-10T16:00:22Z. --- ### What `MemoryIndexBelongsTo` in `ResourceTracker.cpp` walked every instruction of the program on each call and asked `BufferAccessOf`, `AddressOpcodeInfoOf` and `ImageOpcodeInfoOf` about each one. `TryMakeTableImage` calls it for each of a table handle's eight descriptor dwords and once more for a material key. `PlanIndirectImages` calls `TryMakeTableImage` for every image instruction whose handle is not planned yet, so each such instruction could cost up to nine walks of the whole program. This runs once per shader per process, also when the shader disk cache hits. - `PlanIndirectImages` now indexes the instructions that access memory by their memory index once, before it plans. While the pass runs, `MemoryIndexBelongsTo` looks the index up instead of walking the program; the predicate is the same. - The planning pass adds, removes and changes no instruction, so the index is valid until the pass drops it at its end. Outside the pass, where the function has no caller, it walks the program as before. Issue: #2774. Overlap: #757 and #758 restructure this pass (`PlanTableColumns`) and conflict with this commit in one hunk (#757) and two (#758). #1184 and #2653 also change `ResourceTracker.cpp`, in other functions, and merge with this commit without conflict in that file. ### Tested - **Tests.** Linux (CachyOS, kernel 7.2.9, GCC 16.2.1), Ryzen 9 7950X3D (16 cores, 32 threads), RTX 3080 (NVIDIA 615.71.09), Release with `BUILD_TESTING=ON` and `ANYPS5_ENABLE_SPIRV_TOOLS=ON`, this branch on `main` 57f96fd2: of 701 tests, 696 pass, four `video_out_flip_*` tests (lifetime, reopen, controls, overlay) skip (no Vulkan in SDL's offscreen video driver), and `agc_driver_dcc_keys_first_write` fails, which is #2767 (NVIDIA, Linux). `agc_shader_bindless`, `agc_shader_memory` and `agc_driver_bindless_images` cover the table-planning path. `tools/check_conventions.py --base upstream/main` and `git diff --check` pass. - **007 First Light.** PPSA11386 v01.018.000 in a bubblewrap sandbox, on the RTX 3080, Release with SPIRV-Tools and testing OFF. - The base is `main` a29ef592 with the stack listed in #2523 that gets 007 to its first flip, with `ResourceTracker.cpp` as on `main`; that file is the same at a29ef592 and 57f96fd2. - The commit is the only difference between the two builds. It was measured in an earlier revision that differs only in form: the fallback walk now calls `AccessesMemory` (the same condition), the lookup uses `std::ranges::all_of`, and a redundant `clear()` was dropped. - The measure is the process lifetime from exec to the first flip, where 007 currently stops. | | Without | With this commit | |---|---|---| | warm shader cache, 4 + 4 interleaved runs (s) | 22.9 24.9 24.1 22.7 (median 23.5) | 15.6 15.2 16.4 15.6 (median 15.6) | | first run, cold shader cache (s) | 27.4 | 16.0 | | CPU time of all threads, one warm run (s) | 319 | 169 | | CPU time of the main thread, same run (s) | 14.9 | 7.6 | - **Same output.** Both builds' shader caches hold the same 1,956 requests. Paired by request, the stored results are byte-identical except `CompiledShaderInfo::shaderHash`, which holds the shader's code address; 8 pairs are fully identical. As a control, two cold runs of one build with #1184's tracker hunk (below) differ from each other only in address fields too. - **Failure log.** In every run, the build with this commit logs 3 more preparation failures than the build without (79 vs 76). They are shaders that the build without this commit is still preparing when 007 stops (replaying each of their three captured requests offline with `agc_shader_replay`, 2 runs each on an idle machine, takes 18.3 to 19.9 s without this commit and 2.2 to 2.3 s with it, ending in the same offline error). - **With #1184's tracker hunk.** On the 007 stack's own `ResourceTracker.cpp`, which carries part of #1184, the same change (the same earlier revision) gives a warm median of 24.6 → 16.2 s over 4 + 4 runs. With a two-line logging probe, both builds started 2,514 background preparations, of which 2,478 succeeded. ### Checklist - [x] Based on current `main`; no other open PR implements the same change - [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 (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01Fnrk5Dazjb2tLooVSpdRQq
Kikoarl commented 2026-10-10 16:11:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-10T16:11:56Z.


Independent platform validation on real hardware (Linux x86-64)

Built and tested standalone PR head 7eba9f5b on Linux x86-64:

  • Host: Linux, GCC 16.2.1, Release, BUILD_TESTING=ON.
  • Base: main 57f96fd2.
  • GPU / Driver: Intel(R) Graphics (RPL-S), Intel open-source Mesa driver (ANV), subgroup 32.

Verification Results:

  • Clean compilation with Ninja in RAM with zero warnings.
  • The bindless and shader planning test suites pass 100%:
    • agc_shader_bindless — Passed (0.02s)
    • agc_shader_memory — Passed (0.06s)
    • agc_driver_bindless_images — Passed (0.03s)
  • Conventional commits and diff checks pass cleanly.

Solid O(N) index optimization in ResourceTracker.cpp that maintains exact behavioral parity across table image planning passes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6099540690 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2813#issuecomment-6099540690). Originally posted by [Kikoarl](https://github.com/Kikoarl) on 2026-10-10T16:11:56Z. --- **Independent platform validation on real hardware (Linux x86-64)** Built and tested standalone PR head `7eba9f5b` on Linux x86-64: - **Host:** Linux, GCC 16.2.1, Release, `BUILD_TESTING=ON`. - **Base:** `main` `57f96fd2`. - **GPU / Driver:** Intel(R) Graphics (RPL-S), Intel open-source Mesa driver (ANV), subgroup 32. ### Verification Results: - Clean compilation with Ninja in RAM with zero warnings. - The bindless and shader planning test suites pass 100%: - `agc_shader_bindless` — **Passed** (`0.02s`) - `agc_shader_memory` — **Passed** (`0.06s`) - `agc_driver_bindless_images` — **Passed** (`0.03s`) - Conventional commits and diff checks pass cleanly. Solid $O(N)$ index optimization in `ResourceTracker.cpp` that maintains exact behavioral parity across table image planning passes.
github-actions[bot] commented 2026-10-10 17:25:09 +00:00 (Migrated from github.com)

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


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
#757 core/shader/recompiler/Optimization/src/ResourceTracker.cpp L1286
#758 core/shader/recompiler/Optimization/src/ResourceTracker.cpp L276-277, L1286
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6100221211 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2813#issuecomment-6100221211). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T17:25:09Z. --- <!-- 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 | |---|---|---|---| | #757 | `core/shader/recompiler/Optimization/src/ResourceTracker.cpp` [L1286](https://github.com/boykopovar/AnyPS5/blob/99a82eb0e41d381f057fb7ced6b9a8f8c4589f1c/core/shader/recompiler/Optimization/src/ResourceTracker.cpp#L1286) | | | | #758 | `core/shader/recompiler/Optimization/src/ResourceTracker.cpp` [L276-277](https://github.com/boykopovar/AnyPS5/blob/99a82eb0e41d381f057fb7ced6b9a8f8c4589f1c/core/shader/recompiler/Optimization/src/ResourceTracker.cpp#L276-L277), [L1286](https://github.com/boykopovar/AnyPS5/blob/99a82eb0e41d381f057fb7ced6b9a8f8c4589f1c/core/shader/recompiler/Optimization/src/ResourceTracker.cpp#L1286) | | |
adriadam10 commented 2026-10-10 22:45:23 +00:00 (Migrated from github.com)

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


Checked the equivalence of the indexed lookup with the previous walk (same predicate, index only live during PlanIndirectImages, which adds/removes/changes no instruction), built on top of current main and ran agc_shader_bindless, agc_shader_memory and agc_driver_bindless_images: all pass. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6102989422 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2813#issuecomment-6102989422). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T22:45:23Z. --- Checked the equivalence of the indexed lookup with the previous walk (same predicate, index only live during `PlanIndirectImages`, which adds/removes/changes no instruction), built on top of current main and ran `agc_shader_bindless`, `agc_shader_memory` and `agc_driver_bindless_images`: all pass. 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!2813
No description provided.