fix(shader): share the words of identical specialized modules #8340

Merged
milescoviello merged 1 commit from upstream-pr/8340 into main 2026-10-23 18:08:23 +00:00
milescoviello commented 2026-10-20 10:32:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T10:32:33Z.


What

specializeModule keeps one specialized module per artifact variant and constant set, in a process-wide map that is never pruned (registrationModule does the same per variant and module key). When several artifacts have the same SPIR-V and get the same constants, each builds the same specialized words and keeps its own copy of them. buildSpecializedModule now passes its result through InternSpirv. That function looks the words up by both SharedSpirv content hashes and then compares the words themselves, and an equal earlier result hands back its holder, so equal modules share one copy of their words. Nothing else changes. Each module keeps its own specializationId, so PipelineVariantId() and everything keyed on it (pipelines, resource-cache templates) is unchanged. SharedSpirv copies on write while its holder is shared, so a module that edits its words gets its own copy and leaves the others alone.

Why

Crimson Desert (PPSA25286), cold start, main with a counting diagnostic in specializeModule: in the first 235 s, 4,500 specialized modules were built, 916.6 MiB of words, and only 516 distinct, 133.5 MiB. A second run (150 s) logged the first 40 duplicates against their first copy. Every one had the same constant set but a different artifact variant, so the duplicates come from artifacts with the same SPIR-V, not from constants that don't matter. That run had 3,750 modules, 336 distinct (685.8 MiB against 72.0 MiB). The title is OOM-killed at a 12 GiB limit about 238 s in, with 7.3 GB of host memory, before its first draw.

Crimson Desert A/B, cold start (no shader disk cache), pad input, 12 GiB limit, 210 s, two runs each in the order base, fix, base, fix. Release libraries of main 02fb1ce532 and of this PR (220e00917e). Host anonymous RSS (RssAnon) of the title:

time main this PR
60 s 1717 / 1750 MB 1207 / 1217 MB
120 s 3450 / 3909 MB 2669 / 2633 MB
150 s 3284 / 3466 MB 2326 / 2387 MB
180 s 4302 / 3729 MB 2328 / 3374 MB

That's about 1 GB less from 120 s on, more than the duplicated words the diagnostic counted. I haven't measured where the rest of the difference comes from. All four runs reached 210 s, so none of them got far enough to show whether the title now survives past the 238 s OOM.

Tested

Ubuntu 24.04 (Linux 6.8), GCC 13.3, NVIDIA RTX 4070 Ti, driver 595.99.02.

  • agc_spirv_module gains testInternedSpirvSharesEqualWords: equal words interned twice share one copy, different words don't, and a write through one copy leaves the interned words and the other holders alone. 3 of 3 runs pass. With InternSpirv changed to return a fresh copy every time, it fails ("two equal specialized modules kept separate copies of their words").
  • Khronos validation layer (LunarG SDK 1.4.363), NVIDIA RTX 4070 Ti (595.99.02), Ubuntu 24.04: VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1 ctest -R '^agc_spirv_module$|^agc_driver_(graphics$|prepared|compute|dispatch|address_passes|draw_cache|image_table)' -V on main 02fb1ce532 and on this PR (220e00917e). 49 of 49 pass on each. Neither log reports the layer missing. Both have the same VUID, VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605 ×14, and none is new.
  • Rebased onto main 01e5f8f199 (9d4c7cf42b). None of the three files changed on main in between, and agc_spirv_module passes 3 of 3 on the rebased head.
  • Windows 10 Pro 22H2, WinLibs GCC 15.2.0, Release, by @TheSovietWaffle on 9d4c7cf42: agc_spirv_module passes 3 of 3 on an NVIDIA RTX 3080 (617.42) and on an AMD Radeon RDNA 2 iGPU (1002:164e, 32.0.21045.5002).
  • tools/check_conventions.py --base upstream/main passes. tools/pr_overlap.py lists no conflicting lines.

The specialized words are equal by construction, since an interned copy is only handed back when its words compare equal, so the SPIR-V sent to the driver doesn't change.

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: NVIDIA GeForce RTX 4070 Ti, driver 595.99.02, Ubuntu 24.04; NVIDIA RTX 3080 (617.42) and AMD Radeon RDNA 2 iGPU (32.0.21045.5002), Windows 10 (@TheSovietWaffle)
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4934473575 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8340). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-20T10:32:33Z. --- ### What `specializeModule` keeps one specialized module per artifact variant and constant set, in a process-wide map that is never pruned (`registrationModule` does the same per variant and module key). When several artifacts have the same SPIR-V and get the same constants, each builds the same specialized words and keeps its own copy of them. `buildSpecializedModule` now passes its result through `InternSpirv`. That function looks the words up by both `SharedSpirv` content hashes and then compares the words themselves, and an equal earlier result hands back its holder, so equal modules share one copy of their words. Nothing else changes. Each module keeps its own `specializationId`, so `PipelineVariantId()` and everything keyed on it (pipelines, resource-cache templates) is unchanged. `SharedSpirv` copies on write while its holder is shared, so a module that edits its words gets its own copy and leaves the others alone. ### Why Crimson Desert (PPSA25286), cold start, `main` with a counting diagnostic in `specializeModule`: in the first 235 s, 4,500 specialized modules were built, 916.6 MiB of words, and only 516 distinct, 133.5 MiB. A second run (150 s) logged the first 40 duplicates against their first copy. Every one had the same constant set but a different artifact variant, so the duplicates come from artifacts with the same SPIR-V, not from constants that don't matter. That run had 3,750 modules, 336 distinct (685.8 MiB against 72.0 MiB). The title is OOM-killed at a 12 GiB limit about 238 s in, with 7.3 GB of host memory, before its first draw. Crimson Desert A/B, cold start (no shader disk cache), pad input, 12 GiB limit, 210 s, two runs each in the order base, fix, base, fix. Release libraries of `main` 02fb1ce532 and of this PR (220e00917e). Host anonymous RSS (`RssAnon`) of the title: | time | `main` | this PR | |---|---|---| | 60 s | 1717 / 1750 MB | 1207 / 1217 MB | | 120 s | 3450 / 3909 MB | 2669 / 2633 MB | | 150 s | 3284 / 3466 MB | 2326 / 2387 MB | | 180 s | 4302 / 3729 MB | 2328 / 3374 MB | That's about 1 GB less from 120 s on, more than the duplicated words the diagnostic counted. I haven't measured where the rest of the difference comes from. All four runs reached 210 s, so none of them got far enough to show whether the title now survives past the 238 s OOM. ### Tested Ubuntu 24.04 (Linux 6.8), GCC 13.3, NVIDIA RTX 4070 Ti, driver 595.99.02. - `agc_spirv_module` gains `testInternedSpirvSharesEqualWords`: equal words interned twice share one copy, different words don't, and a write through one copy leaves the interned words and the other holders alone. 3 of 3 runs pass. With `InternSpirv` changed to return a fresh copy every time, it fails ("two equal specialized modules kept separate copies of their words"). - Khronos validation layer (LunarG SDK 1.4.363), NVIDIA RTX 4070 Ti (595.99.02), Ubuntu 24.04: `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1 ctest -R '^agc_spirv_module$|^agc_driver_(graphics$|prepared|compute|dispatch|address_passes|draw_cache|image_table)' -V` on `main` 02fb1ce532 and on this PR (220e00917e). 49 of 49 pass on each. Neither log reports the layer missing. Both have the same VUID, `VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605` ×14, and none is new. - Rebased onto `main` 01e5f8f199 (9d4c7cf42b). None of the three files changed on `main` in between, and `agc_spirv_module` passes 3 of 3 on the rebased head. - Windows 10 Pro 22H2, WinLibs GCC 15.2.0, Release, by @TheSovietWaffle on `9d4c7cf42`: `agc_spirv_module` passes 3 of 3 on an NVIDIA RTX 3080 (617.42) and on an AMD Radeon RDNA 2 iGPU (`1002:164e`, 32.0.21045.5002). - `tools/check_conventions.py --base upstream/main` passes. `tools/pr_overlap.py` lists no conflicting lines. The specialized words are equal by construction, since an interned copy is only handed back when its words compare equal, so the SPIR-V sent to the driver doesn't change. ### 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: NVIDIA GeForce RTX 4070 Ti, driver 595.99.02, Ubuntu 24.04; NVIDIA RTX 3080 (617.42) and AMD Radeon RDNA 2 iGPU (32.0.21045.5002), Windows 10 (@TheSovietWaffle) - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
TheSovietWaffle commented 2026-10-20 12:03:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheSovietWaffle on 2026-10-20T12:03:53Z.


Windows data point: RTX 3080 (NVIDIA 617.42) and AMD Radeon RDNA 2 iGPU (1002:164e, 32.0.21045.5002), Windows 10 Pro 22H2, WinLibs GCC 15.2.0, Release, head 9d4c7cf42: agc_spirv_module passes 3 of 3 on both GPUs.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6290411006 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8340#issuecomment-6290411006). Originally posted by [TheSovietWaffle](https://github.com/TheSovietWaffle) on 2026-10-20T12:03:53Z. --- Windows data point: RTX 3080 (NVIDIA 617.42) and AMD Radeon RDNA 2 iGPU (`1002:164e`, 32.0.21045.5002), Windows 10 Pro 22H2, WinLibs GCC 15.2.0, Release, head `9d4c7cf42`: `agc_spirv_module` passes 3 of 3 on both GPUs. AI-assisted: yes (Claude Code).
milescoviello commented 2026-10-20 12:15:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T12:15:44Z.


Thanks @TheSovietWaffle. That's the first Windows and AMD run of this one. I added both GPUs to the description.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6290617415 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8340#issuecomment-6290617415). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-20T12:15:44Z. --- Thanks @TheSovietWaffle. That's the first Windows and AMD run of this one. I added both GPUs to the description. AI-assisted: yes (Claude Code)
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!8340
No description provided.