refactor(shader): drop unused folded constants and unreachable blocks after specialization #4431
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4431"
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 AlxcBTW on 2026-10-12T10:56:42Z.
What
SpecializeSpirvfolds the specialization constants of a prepared module, but leaves two kinds of debris in the module it returns:fold()hoists every instruction it folds into a module-levelOpConstant/OpConstantTrue/OpConstantFalseand keeps it after the instructions that read it have folded away too. In one compute module 58,834 such constants remain, 1,286 of them referenced.prune()turns every block a folded branch cuts off intoOpLabel; OpUnreachableand never removes it, even when no instruction names the label. A formatted buffer access with a known descriptor leaves about 1,800 of them from its 77-case format switch.This change emits only the folded constants that a remaining instruction refers to, and removes
OpLabel; OpUnreachableblocks that no instruction names. Merge and continue targets of live constructs are named by their header, so they stay. Behaviour is unchanged; the specialized modules get smaller.agc_formatted_buffer_oncecompared the words two more formatted loads add with a tenth of the module. That module was mostly debris; without it, the module has 1,219 words and the two call sites add 225. The check now requires the two extra loads to add fewer words than the first one, which also brings the shared function. A second copy of the function per load still fails both this check and the existing function-count checks.No compile-time or speed claim. The NVIDIA driver removes the same dead code itself: pipeline creation of the same modules before and after this change took the same time within noise, with the same binary size and register count (RTX 5070 Laptop, driver 616.92: 49/46 ms, 686/744 ms, 330/361 ms, 3368/3019 ms). The gain is smaller modules in memory, in the hashing of prepared modules and in pipeline caches.
Tested
Windows 11, offline on the shader disk cache of one title (Sword of the Sea, 2,865 prepared modules). For every module the harness applies the same spec-constant replacement as
buildSpecializedModule, thenSpecializeSpirvfrommainand from this branch. The harness synthesizes the buffer descriptor values; the vertex-input and fragment-output specializations are not applied.mainspirv-val --target-env vulkan1.3: 80 randomly chosen modules under 200k words and 4 modules of 300k to 640k words are valid withmainand with this branch.tests/SpirvModule.cpp(a constant switch whose dead cases fold) fails three checks onmainand passes on this branch, built with GCC 15.2.maine7c70a2d.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T11:01:03Z.
Code review location:
core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp; line 514referencedIds()currently treats every operand word as an ID. Literal operands can collide with result IDs and keep dead constants alive—for example, anOpTypeIntwidth literal of32will mark an otherwise-unused constant with result ID32as referenced. Could this use SPIR-V operand-kind/def-use information, with a regression case for a literal/result-ID collision? That would verify the pass removes ordinary dead constants even when their numeric ID appears as a literal elsewhere.Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-12T15:17:51Z.
Code review location:
core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp; line 514Thanks, that was a real leak. Fixed in
676622fd7.referencedIds()now visits only<id>operands. For instructions that carry literals, the literal words are skipped:OpArrayLengthmember;OpSwitchcase values (two words for a 64-bit selector);An opcode without a listed layout still counts every word. An instruction the table doesn't know can therefore only keep debris alive, never drop something that is used.
Regression case in
tests/SpirvModule.cpp: the folded switch now also runs with its dead constant at id 32, theOpTypeIntwidth from your example, and with the dead blocks' labels asLocalSizeliterals. On the previous head this case fails three checks: the unreachable blocks are kept, there are 5 labels instead of 3, and%32is kept. With the fix it passes.On the census of prepared modules from the PR description (2,890 modules now; the cache has grown), the change removes another 88,726 words in total (at most 223 per module) and no module grows.
spirv-valpasses on 80 random modules.The full Linux and Windows suite passes on the fork (Linux 965 of 965, Windows 971 of 971), merged into current
main.AI-assisted: yes (Claude Code)