perf(shader): size the result memo from host memory #12828

Open
QRMOE wants to merge 2 commits from upstream-pr/12828 into main
QRMOE commented 2026-10-29 11:48:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by QRMOE on 2026-10-29T11:48:18Z.


What

The shader result memo kept at most 256 MiB of results unless APS5_RESULT_MEMO_MB said otherwise. A title whose stages patch many result variants keeps that memo full: every insert past the budget trims it back to three quarters, and the trimmed results come back as full materializations.

In RESIDENT EVIL requiem (PPSA31246), the [recompile] result memo line (APS5_PROFILE_DRAW) in the first level shows this, per 10 s:

  • At 256 MiB: 674–869 misses at about 2 ms each, so 1.3–1.8 s of materialization on the queue worker, and 2,800–4,450 results trimmed.
  • At 2048 MiB (APS5_RESULT_MEMO_MB=2048): 101, then 17, then 1 miss; materialization near 0; nothing trimmed; about 2 GiB held.

With this change, when APS5_RESULT_MEMO_MB is unset the device sets the budget to a sixteenth of the host's memory, between 256 MiB and 4 GiB (ShaderRecompiler::ResultMemoBudgetFor). That is 2 GiB on a 32 GiB host and 512 MiB on 8 GiB. An explicit APS5_RESULT_MEMO_MB still wins.

  • The host size is the smaller of physical memory and the process's memory limit.
    • Physical memory comes from the query the trace summary already uses (memoryMiB in PerformanceTrace.cpp, exported as HostMemoryMiB). It returns 0 when the query fails, which keeps the 256 MiB floor, so device creation does not depend on it.
    • The limit (HostMemoryLimitBytes) is cgroup v2 memory.max or v1 memory.limit_in_bytes on Linux (max means none), and the job object's job or process memory limit on Windows.
  • ResultMemoBudgetFor and SetResultMemoBudget are added to shaderCacheReviewedNames. They only decide how many results are kept in memory, never what a recompile produces, so the disk cache key is unchanged.

Tested

Windows 11 (26200), Ryzen 7 9800X3D, 32 GiB, RX 9070 XT (AMD 26.6.2 / 32.0.31021.1015), MinGW GCC 15.2, Release. The base is main 578b8311ac.

  • New test agc_shader_result_memo_budget_sizing (verifyResultMemoBudgetSizing in tests/ShaderMemory.cpp):
    • 32 GiB gives 2 GiB and 8 GiB gives 512 MiB; 4 GiB lands on the 256 MiB floor, 2 GiB stays there, 64 GiB lands on the 4 GiB ceiling and 256 GiB stays there.
    • An unknown host size (0) keeps 256 MiB.
    • A 4 GiB limit on a 64 GiB host gives 256 MiB, 16 GiB on 32 GiB gives 1 GiB, and a limit above physical memory changes nothing.
    • The agc_shader_result_memo* tests pass 4 of 4 on cd4d01c, and the rest of agc_shader_memory passed on the first head.
  • Validation layer, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer ctest --test-dir build -R "agc_shader_memory|agc_driver_sampled_cache_budget|agc_driver_resource_cache" -V, same machine and GPU:
    • main: 10 of 10 pass, 0 VUID-, no "was not found but was requested by env var VK_INSTANCE_LAYERS".
    • This PR: 10 of 10 pass, 0 VUID-, no such line.
    • The loader log shows the layer inserted in both runs.
  • Title: requiem's first level on a local integration branch, with APS5_PROFILE_DRAW; the memo counters are above. This PR makes no frame-time claim: a tools/trace_digest.py comparison of main and this PR from the same machine and scene, and a sweep of 256, 512, 1024 and 2048 MiB, are still to be done.

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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: AMD Radeon RX 9070 XT, AMD 26.6.2 (32.0.31021.1015), Windows 11 26200
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5067425512 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12828). Originally posted by [QRMOE](https://github.com/QRMOE) on 2026-10-29T11:48:18Z. --- ### What The shader result memo kept at most 256 MiB of results unless `APS5_RESULT_MEMO_MB` said otherwise. A title whose stages patch many result variants keeps that memo full: every insert past the budget trims it back to three quarters, and the trimmed results come back as full materializations. In RESIDENT EVIL requiem (PPSA31246), the `[recompile] result memo` line (`APS5_PROFILE_DRAW`) in the first level shows this, per 10 s: - **At 256 MiB:** 674–869 misses at about 2 ms each, so 1.3–1.8 s of materialization on the queue worker, and 2,800–4,450 results trimmed. - **At 2048 MiB (`APS5_RESULT_MEMO_MB=2048`):** 101, then 17, then 1 miss; materialization near 0; nothing trimmed; about 2 GiB held. With this change, when `APS5_RESULT_MEMO_MB` is unset the device sets the budget to a sixteenth of the host's memory, between 256 MiB and 4 GiB (`ShaderRecompiler::ResultMemoBudgetFor`). That is 2 GiB on a 32 GiB host and 512 MiB on 8 GiB. An explicit `APS5_RESULT_MEMO_MB` still wins. - The host size is the smaller of physical memory and the process's memory limit. - Physical memory comes from the query the trace summary already uses (`memoryMiB` in `PerformanceTrace.cpp`, exported as `HostMemoryMiB`). It returns 0 when the query fails, which keeps the 256 MiB floor, so device creation does not depend on it. - The limit (`HostMemoryLimitBytes`) is cgroup v2 `memory.max` or v1 `memory.limit_in_bytes` on Linux (`max` means none), and the job object's job or process memory limit on Windows. - `ResultMemoBudgetFor` and `SetResultMemoBudget` are added to `shaderCacheReviewedNames`. They only decide how many results are kept in memory, never what a recompile produces, so the disk cache key is unchanged. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11 (26200), Ryzen 7 9800X3D, 32 GiB, RX 9070 XT (AMD 26.6.2 / 32.0.31021.1015), MinGW GCC 15.2, Release. The base is `main` 578b8311ac. - **New test `agc_shader_result_memo_budget_sizing`** (`verifyResultMemoBudgetSizing` in `tests/ShaderMemory.cpp`): - 32 GiB gives 2 GiB and 8 GiB gives 512 MiB; 4 GiB lands on the 256 MiB floor, 2 GiB stays there, 64 GiB lands on the 4 GiB ceiling and 256 GiB stays there. - An unknown host size (0) keeps 256 MiB. - A 4 GiB limit on a 64 GiB host gives 256 MiB, 16 GiB on 32 GiB gives 1 GiB, and a limit above physical memory changes nothing. - The `agc_shader_result_memo*` tests pass 4 of 4 on cd4d01c, and the rest of `agc_shader_memory` passed on the first head. - **Validation layer**, `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer ctest --test-dir build -R "agc_shader_memory|agc_driver_sampled_cache_budget|agc_driver_resource_cache" -V`, same machine and GPU: - `main`: 10 of 10 pass, 0 `VUID-`, no "was not found but was requested by env var VK_INSTANCE_LAYERS". - This PR: 10 of 10 pass, 0 `VUID-`, no such line. - The loader log shows the layer inserted in both runs. - **Title:** requiem's first level on a local integration branch, with `APS5_PROFILE_DRAW`; the memo counters are above. This PR makes no frame-time claim: a `tools/trace_digest.py` comparison of `main` and this PR from the same machine and scene, and a sweep of 256, 512, 1024 and 2048 MiB, are still to be done. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: AMD Radeon RX 9070 XT, AMD 26.6.2 (32.0.31021.1015), Windows 11 26200 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
CnRJay commented 2026-11-01 19:50:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:50:57Z.

Review: COMMENTED


  1. The speed claim is built on APS5_PROFILE_DRAW output. The miss and materialization counts (674-869 misses at about 2 ms per 10 s, then 101/17/1) come from the profile line, and the frame time is "15.5-16.3 s to 14.2-15.8 s, one run each", flagged as "not a clean A/B yet". The ranges overlap. Please provide tools/trace_digest.py summaries of main and of this change from the same machine and scene, or drop the speed claim and keep the memory-budget argument.
  2. The budget is a heuristic fitted to one title. One sixteenth of host RAM clamped to 256 MiB-4 GiB comes from watching RESIDENT EVIL requiem's memo fill at 256 MiB. Nothing shows that 1/16 is right for another title, and a 64 GiB host will now hold 4 GiB of recompiled results by default. What is the memo's eviction behaviour under host memory pressure (another process, a lower cgroup limit that changes after start)? The limit is read once at device creation.
  3. Behaviour now differs by machine. Two users running the same title get different hit rates and different frame pacing depending on RAM. A dump of the memo's size versus miss rate across a few budgets would show whether the knee is real or whether a smaller step (512 MiB) captures most of it.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5729927246 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12828#pullrequestreview-5729927246). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-11-01T19:50:57Z. **Review:** COMMENTED --- 1. **The speed claim is built on `APS5_PROFILE_DRAW` output.** The miss and materialization counts (674-869 misses at about 2 ms per 10 s, then 101/17/1) come from the profile line, and the frame time is "15.5-16.3 s to 14.2-15.8 s, one run each", flagged as "not a clean A/B yet". The ranges overlap. Please provide `tools/trace_digest.py` summaries of `main` and of this change from the same machine and scene, or drop the speed claim and keep the memory-budget argument. 2. **The budget is a heuristic fitted to one title.** One sixteenth of host RAM clamped to 256 MiB-4 GiB comes from watching RESIDENT EVIL requiem's memo fill at 256 MiB. Nothing shows that 1/16 is right for another title, and a 64 GiB host will now hold 4 GiB of recompiled results by default. What is the memo's eviction behaviour under host memory pressure (another process, a lower cgroup limit that changes after start)? The limit is read once at device creation. 3. **Behaviour now differs by machine.** Two users running the same title get different hit rates and different frame pacing depending on RAM. A dump of the memo's size versus miss rate across a few budgets would show whether the knee is real or whether a smaller step (512 MiB) captures most of it.
MarcZX10 commented 2026-11-20 06:57:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T06:57:43Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the remaining performance comparison is still pending.

What I ran

Reviewed head cd4d01c4429b7939dd4aabedced22e37b6766857. I read the prepared description, commits, manifest, diff, and the full text of the changed files listed as included. I read the excluded ShaderMemory.cpp hunks through repo_read.py.

No commands or tests were run. The reported test and hardware results are author supplied.

Results

Check Result Note
Code review passed Changed logic and relevant callers inspected
Tests not run Results in the description are author reported
Performance comparison not run Description says matched trace comparison remains pending

Findings

Blocking

None.

Non-blocking

None.

Not verified

  • Runtime behavior and tests were not independently verified.
  • The Linux memory-limit lookup checks the standard cgroup v1/v2 paths shown in PerformanceTrace.cpp; behavior with nested cgroup mount layouts was not verified.
  • Files reviewed: core/libs/prx/libSceAgcDriver/CMakeLists.txt, core/libs/prx/libSceAgcDriver/Execution/include/PerformanceTrace.hpp, core/libs/prx/libSceAgcDriver/Execution/src/PerformanceTrace.cpp, core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp, core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp, core/shader/recompiler/CMakeLists.txt, core/shader/recompiler/Recompiler.cpp, and core/shader/recompiler/Recompiler.hpp.
  • files.json was not available in the review folder, so no additional excluded files could be checked against it.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5988707699 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12828#pullrequestreview-5988707699). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T06:57:43Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the remaining performance comparison is still pending. ## What I ran Reviewed head `cd4d01c4429b7939dd4aabedced22e37b6766857`. I read the prepared description, commits, manifest, diff, and the full text of the changed files listed as included. I read the excluded `ShaderMemory.cpp` hunks through `repo_read.py`. No commands or tests were run. The reported test and hardware results are author supplied. ## Results | Check | Result | Note | |---|---|---| | Code review | passed | Changed logic and relevant callers inspected | | Tests | not run | Results in the description are author reported | | Performance comparison | not run | Description says matched trace comparison remains pending | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified - Runtime behavior and tests were not independently verified. - The Linux memory-limit lookup checks the standard cgroup v1/v2 paths shown in `PerformanceTrace.cpp`; behavior with nested cgroup mount layouts was not verified. - Files reviewed: `core/libs/prx/libSceAgcDriver/CMakeLists.txt`, `core/libs/prx/libSceAgcDriver/Execution/include/PerformanceTrace.hpp`, `core/libs/prx/libSceAgcDriver/Execution/src/PerformanceTrace.cpp`, `core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp`, `core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp`, `core/shader/recompiler/CMakeLists.txt`, `core/shader/recompiler/Recompiler.cpp`, and `core/shader/recompiler/Recompiler.hpp`. - `files.json` was not available in the review folder, so no additional excluded files could be checked against it. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 22:54:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T22:54:38Z.

Review: COMMENTED


HostMemoryLimitBytes reads only the two fixed root cgroup files. A process can belong to a nested cgroup whose effective limit is lower than those files describe. The memo can then size itself from host memory and exceed the container's actual limit. Please resolve the process membership and mounted hierarchy, then apply the tightest applicable limit. A test with an unlimited parent and a limited child would cover the missed limit. The trial merge conflicts in the shader CMake, device setup and shader-memory code. Linux cgroup v2 hierarchy documentation

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6180075426 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12828#pullrequestreview-6180075426). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T22:54:38Z. **Review:** COMMENTED --- `HostMemoryLimitBytes` reads only the two fixed root cgroup files. A process can belong to a nested cgroup whose effective limit is lower than those files describe. The memo can then size itself from host memory and exceed the container's actual limit. Please resolve the process membership and mounted hierarchy, then apply the tightest applicable limit. A test with an unlimited parent and a limited child would cover the missed limit. The trial merge conflicts in the shader CMake, device setup and shader-memory code. [Linux cgroup v2 hierarchy documentation](https://docs.kernel.org/admin-guide/cgroup-v2.html#organizing-cgroups-and-threads) AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/12828:upstream-pr/12828'
git switch 'upstream-pr/12828'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/12828'
git switch 'upstream-pr/12828'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/12828'
git switch 'upstream-pr/12828'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/12828'
git switch 'main'
git merge --squash 'upstream-pr/12828'
git switch 'main'
git merge --ff-only 'upstream-pr/12828'
git switch 'main'
git merge 'upstream-pr/12828'
git push origin 'main'
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!12828
No description provided.