fix(shader): share the words of identical specialized modules #8340
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8340"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T10:32:33Z.
What
specializeModulekeeps one specialized module per artifact variant and constant set, in a process-wide map that is never pruned (registrationModuledoes 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.buildSpecializedModulenow passes its result throughInternSpirv. That function looks the words up by bothSharedSpirvcontent 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 ownspecializationId, soPipelineVariantId()and everything keyed on it (pipelines, resource-cache templates) is unchanged.SharedSpirvcopies 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,
mainwith a counting diagnostic inspecializeModule: 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
main02fb1ce532and of this PR (220e00917e). Host anonymous RSS (RssAnon) of the title:mainThat'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_modulegainstestInternedSpirvSharesEqualWords: 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. WithInternSpirvchanged to return a fresh copy every time, it fails ("two equal specialized modules kept separate copies of their words").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)' -Vonmain02fb1ce532and 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.main01e5f8f199(9d4c7cf42b). None of the three files changed onmainin between, andagc_spirv_modulepasses 3 of 3 on the rebased head.9d4c7cf42:agc_spirv_modulepasses 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/mainpasses.tools/pr_overlap.pylists 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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
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, head9d4c7cf42:agc_spirv_modulepasses 3 of 3 on both GPUs.AI-assisted: yes (Claude Code).
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)