Recompiler: MemoryIndexBelongsTo walks the whole program on every call; about half of a warm start-up's CPU on 007 First Light #2774

Closed
opened 2026-10-10 14:07:38 +00:00 by SleglJan · 1 comment
SleglJan commented 2026-10-10 14:07:38 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-10T14:07:38Z.


Measured

PPSA11386 (007 First Light) v01.018.000, 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 SPIRV-Tools OFF. The base is main a29ef592 with the PRs that get 007 to its first flip (#2523), with ResourceTracker.cpp as on main. The runs are warm: about 1,950 shader-cache hits and no misses. Each measures exec to the first flip, where 007 stops (#2523). Two separate warm runs were used, one per tool.

  • CPU time. A /proc sample of every thread every 0.25 s counts 319 s of CPU in a 23.1 s run. 17 threads each use more than half a core from 1 s to 18 s, and 5 to 7 after that. The main thread uses 14.9 s.
  • Where it goes. perf record -e cycles:u -F 499 attached to the process for 22 s of a 25.1 s run (163,328 samples) puts 76.5% of the samples in libSceAgcDriver.prx:
Symbol Samples
ShaderRecompiler::AddressOpcodeInfoOf(IrOpcode) 22.2%
(anonymous namespace)::Tracker::TryMakeTableImage(IrValue&, …) 21.3%
ShaderRecompiler::ImageOpcodeInfoOf(IrOpcode) 8.7%

MemoryIndexBelongsTo is inlined into TryMakeTableImage, and frame-pointer call graphs did not resolve in this build. The attribution therefore rests on the symbols and the A/B below.

Cause

MemoryIndexBelongsTo (Optimization/src/ResourceTracker.cpp) walks every instruction of every block and calls BufferAccessOf, AddressOpcodeInfoOf and ImageOpcodeInfoOf on 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 can cost up to nine walks of the whole program.

This runs once per shader per process, also when the shader disk cache hits: the resource plan, including ResourceTracker::Track, is built before ShaderDiskCache::Load is consulted. For three of 007's shaders, one offline recompile takes 18 to 20 s with main's code, and 2.2 to 2.3 s with the fix below.

Fix

#2813 (from SleglJan/AnyPS5#23; one commit, +25/−2 in ResourceTracker.cpp, now on main 57f96fd2): PlanIndirectImages indexes the memory-accessing instructions by memory index once, and MemoryIndexBelongsTo looks the index up during that pass. The pass adds, removes and changes no instruction. #757 and #758 restructure this pass (PlanTableColumns) and conflict with the commit in one hunk (#757) and two (#758); their planning pass also emits no IR, so the same index should apply there.

007, exec to first flip Without With the fix
warm, 4 + 4 interleaved runs median 23.5 s median 15.6 s
first run, cold cache 27.4 s 16.0 s
CPU time, all threads / main thread 319 s / 14.9 s 169 s / 7.6 s
  • 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 differ from each other only in address fields too.
  • Failure log. In every run, the build with the fix logs 3 more preparation failures than the build without (79 vs 76). They are shaders that the build without the fix 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 the fix and 2.2 to 2.3 s with it, ending in the same offline error).

Since #2808 it is open as #2813. A/B numbers from other titles (warm boot with and without the commit) would show how much of this is general.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5791952087 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2774). Originally posted by [SleglJan](https://github.com/SleglJan) on 2026-10-10T14:07:38Z. --- ### Measured PPSA11386 (007 First Light) v01.018.000, 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 SPIRV-Tools OFF. The base is `main` a29ef592 with the PRs that get 007 to its first flip (#2523), with `ResourceTracker.cpp` as on `main`. The runs are warm: about 1,950 shader-cache hits and no misses. Each measures exec to the first flip, where 007 stops (#2523). Two separate warm runs were used, one per tool. - **CPU time.** A /proc sample of every thread every 0.25 s counts 319 s of CPU in a 23.1 s run. 17 threads each use more than half a core from 1 s to 18 s, and 5 to 7 after that. The main thread uses 14.9 s. - **Where it goes.** `perf record -e cycles:u -F 499` attached to the process for 22 s of a 25.1 s run (163,328 samples) puts 76.5% of the samples in `libSceAgcDriver.prx`: | Symbol | Samples | |---|---| | `ShaderRecompiler::AddressOpcodeInfoOf(IrOpcode)` | 22.2% | | `(anonymous namespace)::Tracker::TryMakeTableImage(IrValue&, …)` | 21.3% | | `ShaderRecompiler::ImageOpcodeInfoOf(IrOpcode)` | 8.7% | `MemoryIndexBelongsTo` is inlined into `TryMakeTableImage`, and frame-pointer call graphs did not resolve in this build. The attribution therefore rests on the symbols and the A/B below. ### Cause `MemoryIndexBelongsTo` (`Optimization/src/ResourceTracker.cpp`) walks every instruction of every block and calls `BufferAccessOf`, `AddressOpcodeInfoOf` and `ImageOpcodeInfoOf` on 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 can cost up to nine walks of the whole program. This runs once per shader per process, also when the shader disk cache hits: the resource plan, including `ResourceTracker::Track`, is built before `ShaderDiskCache::Load` is consulted. For three of 007's shaders, one offline recompile takes 18 to 20 s with `main`'s code, and 2.2 to 2.3 s with the fix below. ### Fix #2813 (from SleglJan/AnyPS5#23; one commit, +25/−2 in `ResourceTracker.cpp`, now on `main` 57f96fd2): `PlanIndirectImages` indexes the memory-accessing instructions by memory index once, and `MemoryIndexBelongsTo` looks the index up during that pass. The pass adds, removes and changes no instruction. #757 and #758 restructure this pass (`PlanTableColumns`) and conflict with the commit in one hunk (#757) and two (#758); their planning pass also emits no IR, so the same index should apply there. | 007, exec to first flip | Without | With the fix | |---|---|---| | warm, 4 + 4 interleaved runs | median 23.5 s | median 15.6 s | | first run, cold cache | 27.4 s | 16.0 s | | CPU time, all threads / main thread | 319 s / 14.9 s | 169 s / 7.6 s | - **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 differ from each other only in address fields too. - **Failure log.** In every run, the build with the fix logs 3 more preparation failures than the build without (79 vs 76). They are shaders that the build without the fix 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 the fix and 2.2 to 2.3 s with it, ending in the same offline error). Since #2808 it is open as #2813. A/B numbers from other titles (warm boot with and without the commit) would show how much of this is general. AI-assisted: yes (Claude Code)
oneandonlydean commented 2026-10-11 01:08:39 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T01:08:39Z.


Fixed by #2813.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104054905 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2774#issuecomment-6104054905). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-11T01:08:39Z. --- Fixed by #2813.
Sign in to join this conversation.
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#2774
No description provided.