fix(shader): release specialized modules that no result uses #10473

Merged
milescoviello merged 2 commits from upstream-pr/10473 into main 2026-10-25 10:58:43 +00:00
milescoviello commented 2026-10-24 02:21:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-24T02:21:48Z.


What

Three caches in Recompiler.cpp keep every specialized module for the rest of the run: specializeModule's map (by variant and specialization constants), each prepared binding plan's modules, and registrationModule's map. Since #8340, the SPIR-V interning table holds every module's words as well. A module's words outlive the results that used them, so the memory only grows. Once the draw caches start evicting (#9129's byte budget, for example), most of it belongs to no result.

In Ghost of Tsushima (PPSA02225), with a diagnostic that counts the interned modules held only by these caches (Linux, RTX 4070 Ti, the stack the title needs to reach its opening plus #8340):

time interned modules MiB held only by the module caches
4 min 843 223.5 271 modules, 85.8 MiB
6 min 1,350 384.1 803 modules, 170.0 MiB
8 min 1,679 464.4 1,035 modules, 220.1 MiB
10 min 2,183 578.9 1,658 modules, 344.8 MiB
12 min 2,197 588.6 1,548 modules, 323.2 MiB

The title was killed by its 20 GB cap at 633 s in its opening. The same count in lighter titles: MGS4 (PPSA18194) 24 MiB idle of 44 MiB, MXGP 26 (PPSA30839) 2 of 110 MiB, Super Meat Boy 3D (PPSA31162) 7 of 123 MiB, at 300 s each.

How

  • SharedSpirv::Weak is a reference that does not keep the words alive: Lock() returns them while any SharedSpirv still holds them, and an empty module after that.
  • InternSpirv keeps weak references and drops the expired ones from the bucket it searches. Equal words are still shared while a holder lives, as #8340 does.
  • SpecializedModule keeps the interface: its bindings, push data use, specializationId, the content hash of its words, and a weak reference to the words. buildSpecializedModule returns the module together with the words it built.
  • specializeModule, registrationModule and the prepared plan's module cache return the words with the module. When the words were released, they are built again from the same key under the module's mutex. That gives the same module, because the key carries the variant and every specialization constant, including the vertex input classes. The content hash is checked (a mismatch throws), so the specializationId that names the module's pipelines stays valid.

Tested

Ubuntu 26.04.1, GCC 15.2, Release, RTX 4070 Ti, NVIDIA 615.71.09. The commit was tested as 09a7d25150 on main 9d4fb4fb70 and is unchanged (git range-diff) on the current head 611daf97e8, which @adriadam10 rebased onto main aeeaefad98 and which adds 611daf97e8: two test helpers (DenormalFlushElimination.cpp, RedundantValueElimination.cpp) bound a reference to the words of a temporary result, which this change now frees. The 38 tests below pass 3 of 3 on aeeaefad98 and on 611daf97e8; on the earlier head 460029a65a they passed 3 of 3 on main 379b1dca32 too.

  • New verifySpecializedModuleRelease in agc_shader_memory: a cached pixel request that gets specialized is recompiled. A weak reference to its result's words expires once the result is gone. Recompiling it then gives the same specializationId and the same words, and a second live result shares them. New testInternedSpirvReleasesUnusedWords in agc_spirv_module: interned words are released after their last holder, and interning them again gives the same words.
  • With the interning table changed to pin every module (test/specweak-mut), both fail 3 of 3: specialized module release: the module caches kept the words of a module no result uses and the SPIR-V interning table kept words no holder uses.
  • The 38 tests matching ^agc_shader_memory$|^agc_spirv_module$|^agc_shader_|shader_disk_cache|^agc_driver_graphics$|pipeline_journal pass 3 of 3 on main 9d4fb4fb70 and on 09a7d25150.
  • Khronos validation layer (LunarG SDK 1.4.363), the 5 tests matching ^agc_shader_memory$|^agc_spirv_module$|^agc_shader_disk_cache$|^agc_driver_graphics$|^agc_driver_pipeline_journal$: 5 of 5 on main 9d4fb4fb70 and on 09a7d25150. Neither log reports the layer missing, and neither has a VUID.
  • Windows 11 VM (GeForce GT 1030, driver 582.66, WinLibs GCC 15.2.0 r7, Release): the same 5 pass 3 of 3 on main at the merge base and on 09a7d25150.
  • Ghost of Tsushima with this change and #10438 (the cache key digest) on the same stack, seeded with the same pipeline cache, against the run above. The title was killed by its 20 GB cap at 1,260 s instead of 633 s. RssAnon at 4 / 6 / 8 / 10 / 12 min: 4,577 / 5,533 / 5,833 / 6,266 / 6,745 MB against 4,892 / 6,152 / 6,990 / 7,763 / 8,095 MB, up to 1.5 GB lower in that pair. A third run with both changes (19 GB cap, killed at 913 s) came out between the two at 6 to 12 min, so the per-minute difference is within this title's run-to-run spread; the run lengths and the direct count above are the firmer evidence. The two changes are measured together. NVIDIA's own shader cache was warmer for the second run, which helps its progress, not its memory.

Not checked by me: AMD, Intel, macOS.

Overlaps: #6959 (JJenkx) adds a specialization list to SpecializedModule, read from the built words, and copies it into each result, in the same functions this PR splits. Under this PR, that list belongs with the interface the module keeps: a rebuild gives the same words, so it stays valid. Whichever lands second carries it across. #9989 (mine) adds a test next to the same line of ShaderMemory.cpp.

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 (it lowers the memory Ghost of Tsushima, PPSA02225, needs in its opening)
  • GPUs tested: NVIDIA GeForce RTX 4070 Ti, driver 615.71.09, Ubuntu 26.04.1; NVIDIA GeForce GT 1030, driver 582.66, Windows 11 VM
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4991745874 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10473). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-24T02:21:48Z. --- ### What Three caches in `Recompiler.cpp` keep every specialized module for the rest of the run: `specializeModule`'s map (by variant and specialization constants), each prepared binding plan's `modules`, and `registrationModule`'s map. Since #8340, the SPIR-V interning table holds every module's words as well. A module's words outlive the results that used them, so the memory only grows. Once the draw caches start evicting (#9129's byte budget, for example), most of it belongs to no result. In Ghost of Tsushima (PPSA02225), with a diagnostic that counts the interned modules held only by these caches (Linux, RTX 4070 Ti, the stack the title needs to reach its opening plus #8340): | time | interned modules | MiB | held only by the module caches | |---|---|---|---| | 4 min | 843 | 223.5 | 271 modules, 85.8 MiB | | 6 min | 1,350 | 384.1 | 803 modules, 170.0 MiB | | 8 min | 1,679 | 464.4 | 1,035 modules, 220.1 MiB | | 10 min | 2,183 | 578.9 | 1,658 modules, 344.8 MiB | | 12 min | 2,197 | 588.6 | 1,548 modules, 323.2 MiB | The title was killed by its 20 GB cap at 633 s in its opening. The same count in lighter titles: MGS4 (PPSA18194) 24 MiB idle of 44 MiB, MXGP 26 (PPSA30839) 2 of 110 MiB, Super Meat Boy 3D (PPSA31162) 7 of 123 MiB, at 300 s each. ### How - `SharedSpirv::Weak` is a reference that does not keep the words alive: `Lock()` returns them while any `SharedSpirv` still holds them, and an empty module after that. - `InternSpirv` keeps weak references and drops the expired ones from the bucket it searches. Equal words are still shared while a holder lives, as #8340 does. - `SpecializedModule` keeps the interface: its bindings, push data use, `specializationId`, the content hash of its words, and a weak reference to the words. `buildSpecializedModule` returns the module together with the words it built. - `specializeModule`, `registrationModule` and the prepared plan's module cache return the words with the module. When the words were released, they are built again from the same key under the module's mutex. That gives the same module, because the key carries the variant and every specialization constant, including the vertex input classes. The content hash is checked (a mismatch throws), so the `specializationId` that names the module's pipelines stays valid. ### Tested Ubuntu 26.04.1, GCC 15.2, Release, RTX 4070 Ti, NVIDIA 615.71.09. The commit was tested as `09a7d25150` on `main` 9d4fb4fb70 and is unchanged (`git range-diff`) on the current head `611daf97e8`, which @adriadam10 rebased onto `main` aeeaefad98 and which adds `611daf97e8`: two test helpers (`DenormalFlushElimination.cpp`, `RedundantValueElimination.cpp`) bound a reference to the words of a temporary result, which this change now frees. The 38 tests below pass 3 of 3 on aeeaefad98 and on `611daf97e8`; on the earlier head `460029a65a` they passed 3 of 3 on `main` 379b1dca32 too. - New `verifySpecializedModuleRelease` in `agc_shader_memory`: a cached pixel request that gets specialized is recompiled. A weak reference to its result's words expires once the result is gone. Recompiling it then gives the same `specializationId` and the same words, and a second live result shares them. New `testInternedSpirvReleasesUnusedWords` in `agc_spirv_module`: interned words are released after their last holder, and interning them again gives the same words. - With the interning table changed to pin every module (`test/specweak-mut`), both fail 3 of 3: `specialized module release: the module caches kept the words of a module no result uses` and `the SPIR-V interning table kept words no holder uses`. - The 38 tests matching `^agc_shader_memory$|^agc_spirv_module$|^agc_shader_|shader_disk_cache|^agc_driver_graphics$|pipeline_journal` pass 3 of 3 on `main` 9d4fb4fb70 and on `09a7d25150`. - Khronos validation layer (LunarG SDK 1.4.363), the 5 tests matching `^agc_shader_memory$|^agc_spirv_module$|^agc_shader_disk_cache$|^agc_driver_graphics$|^agc_driver_pipeline_journal$`: 5 of 5 on `main` 9d4fb4fb70 and on `09a7d25150`. Neither log reports the layer missing, and neither has a VUID. - Windows 11 VM (GeForce GT 1030, driver 582.66, WinLibs GCC 15.2.0 r7, Release): the same 5 pass 3 of 3 on `main` at the merge base and on `09a7d25150`. - **Ghost of Tsushima with this change and #10438** (the cache key digest) on the same stack, seeded with the same pipeline cache, against the run above. The title was killed by its 20 GB cap at 1,260 s instead of 633 s. `RssAnon` at 4 / 6 / 8 / 10 / 12 min: 4,577 / 5,533 / 5,833 / 6,266 / 6,745 MB against 4,892 / 6,152 / 6,990 / 7,763 / 8,095 MB, up to 1.5 GB lower in that pair. A third run with both changes (19 GB cap, killed at 913 s) came out between the two at 6 to 12 min, so the per-minute difference is within this title's run-to-run spread; the run lengths and the direct count above are the firmer evidence. The two changes are measured together. NVIDIA's own shader cache was warmer for the second run, which helps its progress, not its memory. Not checked by me: AMD, Intel, macOS. **Overlaps:** #6959 (JJenkx) adds a `specialization` list to `SpecializedModule`, read from the built words, and copies it into each result, in the same functions this PR splits. Under this PR, that list belongs with the interface the module keeps: a rebuild gives the same words, so it stays valid. Whichever lands second carries it across. #9989 (mine) adds a test next to the same line of `ShaderMemory.cpp`. ### 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 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 (it lowers the memory Ghost of Tsushima, PPSA02225, needs in its opening) - [x] GPUs tested: NVIDIA GeForce RTX 4070 Ti, driver 615.71.09, Ubuntu 26.04.1; NVIDIA GeForce GT 1030, driver 582.66, Windows 11 VM - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-24 02:29:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T02:29:58Z.

Review: DISMISSED


I reviewed the exact head bb9c75b3546e76ae175c55cd30be9d9d2857f338, including the weak interning path, module-cache rebuild path, lifetime tests, and the reported main/change validation. I found no code-level blocker in those paths. The branch is based on db6af9e0fd8c75366dabc2066dbf90117f7490d0, while current main is 379b1dca32ecc4e3570ad0a53c69f019b70dbe59 (30 commits ahead); the repository requires PR branches to stay up to date with main. Please refresh the branch and rerun the focused shader tests on the updated main and change, then I can re-review. I did not independently run the GPU tests in this review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5636426807 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10473#pullrequestreview-5636426807). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T02:29:58Z. **Review:** DISMISSED --- I reviewed the exact head `bb9c75b3546e76ae175c55cd30be9d9d2857f338`, including the weak interning path, module-cache rebuild path, lifetime tests, and the reported main/change validation. I found no code-level blocker in those paths. The branch is based on `db6af9e0fd8c75366dabc2066dbf90117f7490d0`, while current `main` is `379b1dca32ecc4e3570ad0a53c69f019b70dbe59` (30 commits ahead); the repository requires PR branches to stay up to date with main. Please refresh the branch and rerun the focused shader tests on the updated main and change, then I can re-review. I did not independently run the GPU tests in this review.
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!10473
No description provided.