fix(shader): drop module constants nothing references after specialization #5315

Closed
AlxcBTW wants to merge 1 commit from upstream-pr/5315 into main
AlxcBTW commented 2026-10-13 17:02:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-13T17:02:04Z.


What

Two pipeline variants of one program that differ only in a folded specialization value now produce the same SPIR-V. The second variant is then served by the pipeline cache instead of a cold compile.

buildSpecializedModule materializes a variant's specialization values as OpConstant, and SpecializeSpirv folds them into the code. The folding left the materialized constants in the module with no use.

Image channel selectors (RuntimeImageSwizzle) are such values. So two variants of a program whose only difference was an image's channel order, 4,5,6,7 vs 6,5,4,7, had identical code but different SPIR-V, and the second one was created and compiled again.

removeUnusedConstants now also removes scalar and null module constants (OpConstant, OpConstantTrue/False, OpConstantNull) that no instruction references any more. OpConstantComposite is not removed: VisitIdOperands counts a composite's own result id as a reference, so a composite and its members always stay. Run already drops the names and decorations of removed ids.

Evidence from Sword of the Sea (PPSA25344), loading on a cold cache, compute programs of 100k+ SPIR-V words. Every pipeline's SPIR-V was dumped (APS5_DUMP_COMPUTE_PIPELINES) and diffed with spirv-dis --raw-id:

  • Dead-constant pairs. In a 202 s window, 15 programs were created twice. In 14 of the 15 pairs, the two modules differ only in 2 to 7 OpConstant values that nothing in the module uses: each id occurs once, at its definition. The values are 4/5/6/7 in another order. The 15th pair is a real variant (10,738 differing lines).
  • Repeat creations. In a longer load window, 439 pipelines were created for 168 programs. 163 creations took over 1 s:
    • 7 first creations of a program: 85 s;
    • 156 repeat creations of an already compiled program: 611 s.

Test change: agc_formatted_buffer_once

@adriadam10, this touches the bound from 56d5a4985. That bound checks that a formatted load from a descriptor with a known format does not carry every format's decoder in registration form, measured against the specialized module.

This change shrinks both modules, the specialized one more, so the ratio between them moves. Sizes in words, from this PR's fork CI and from main 0214069e3 on the same runners:

Linux, SPIRV-Tools on: main Linux: this PR Windows: main Windows: this PR
vertex, specialized 1348 1126 2130 1915
vertex, registration 1917 1739 2927 2756
registration / specialized 1.42 1.54 1.37 1.44
compute, 1 load (known descriptor) 1209 987 1947 1732
compute, GPU-selected descriptor (every decoder) 85973 85945 114447 114419

The bound moves from 1.5x to 1.6x of the specialized module. A registration module that kept every decoder would be about as large as the GPU-selected compute module, about 14,000 words on current main (13,897; it was 85,945 when this PR was opened, before #3656), so 1.6x still catches it.

I first tried bounding the registration module against a vertex module with a GPU-selected descriptor instead, which would have measured the property directly. The recompiler rejects that request in both forms ("buffer specialization has an unsupported format"), so the test keeps its original comparison.

Tested

  • agc_shader_disk_cache, new verifySpecializationDropsDeadConstants:
    • two modules that differ only in a constant the folding consumes specialize to the same words;
    • that constant and its OpName are gone;
    • an array length constant stays;
    • the store still reads the selected value;
    • the result is stable under a second pass.
  • Fork CI, full ctest on this branch (run 38432808073):
    • Linux, lavapipe, SPIRV-Tools on: 1077/1077.
    • Windows: 1095/1095.
  • The before/after of the cold load on one machine and scene follows as a comment (the Sword of the Sea dump is gone; it is measured on Minecraft Dungeons II PPSA16064).

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
  • Unblocks: none
  • GPUs tested: lavapipe (fork CI, Linux). The Sword of the Sea dumps and timings above are from an RTX 5070 Laptop GPU, NVIDIA 617.42, Windows 11
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4846682942 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5315). Originally posted by [AlxcBTW](https://github.com/AlxcBTW) on 2026-10-13T17:02:04Z. --- ### What Two pipeline variants of one program that differ only in a folded specialization value now produce the same SPIR-V. The second variant is then served by the pipeline cache instead of a cold compile. `buildSpecializedModule` materializes a variant's specialization values as `OpConstant`, and `SpecializeSpirv` folds them into the code. The folding left the materialized constants in the module with no use. Image channel selectors (`RuntimeImageSwizzle`) are such values. So two variants of a program whose only difference was an image's channel order, 4,5,6,7 vs 6,5,4,7, had identical code but different SPIR-V, and the second one was created and compiled again. `removeUnusedConstants` now also removes scalar and null module constants (`OpConstant`, `OpConstantTrue`/`False`, `OpConstantNull`) that no instruction references any more. `OpConstantComposite` is not removed: `VisitIdOperands` counts a composite's own result id as a reference, so a composite and its members always stay. `Run` already drops the names and decorations of removed ids. Evidence from Sword of the Sea (PPSA25344), loading on a cold cache, compute programs of 100k+ SPIR-V words. Every pipeline's SPIR-V was dumped (`APS5_DUMP_COMPUTE_PIPELINES`) and diffed with `spirv-dis --raw-id`: - **Dead-constant pairs.** In a 202 s window, 15 programs were created twice. In 14 of the 15 pairs, the two modules differ only in 2 to 7 `OpConstant` values that nothing in the module uses: each id occurs once, at its definition. The values are 4/5/6/7 in another order. The 15th pair is a real variant (10,738 differing lines). - **Repeat creations.** In a longer load window, 439 pipelines were created for 168 programs. 163 creations took over 1 s: - 7 first creations of a program: 85 s; - 156 repeat creations of an already compiled program: 611 s. ### Test change: agc_formatted_buffer_once @adriadam10, this touches the bound from 56d5a4985. That bound checks that a formatted load from a descriptor with a known format does not carry every format's decoder in registration form, measured against the specialized module. This change shrinks both modules, the specialized one more, so the ratio between them moves. Sizes in words, from this PR's fork CI and from `main` 0214069e3 on the same runners: | | Linux, SPIRV-Tools on: `main` | Linux: this PR | Windows: `main` | Windows: this PR | |---|---|---|---|---| | vertex, specialized | 1348 | 1126 | 2130 | 1915 | | vertex, registration | 1917 | 1739 | 2927 | 2756 | | registration / specialized | 1.42 | **1.54** | 1.37 | 1.44 | | compute, 1 load (known descriptor) | 1209 | 987 | 1947 | 1732 | | compute, GPU-selected descriptor (every decoder) | 85973 | 85945 | 114447 | 114419 | The bound moves from 1.5x to 1.6x of the specialized module. A registration module that kept every decoder would be about as large as the GPU-selected compute module, about 14,000 words on current `main` (13,897; it was 85,945 when this PR was opened, before #3656), so 1.6x still catches it. I first tried bounding the registration module against a vertex module with a GPU-selected descriptor instead, which would have measured the property directly. The recompiler rejects that request in both forms ("buffer specialization has an unsupported format"), so the test keeps its original comparison. ### Tested - `agc_shader_disk_cache`, new `verifySpecializationDropsDeadConstants`: - two modules that differ only in a constant the folding consumes specialize to the same words; - that constant and its `OpName` are gone; - an array length constant stays; - the store still reads the selected value; - the result is stable under a second pass. - Fork CI, full ctest on this branch (run 38432808073): - Linux, lavapipe, SPIRV-Tools on: 1077/1077. - Windows: 1095/1095. - The before/after of the cold load on one machine and scene follows as a comment (the Sword of the Sea dump is gone; it is measured on Minecraft Dungeons II PPSA16064). ### 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] Unblocks: none - [x] GPUs tested: lavapipe (fork CI, Linux). The Sword of the Sea dumps and timings above are from an RTX 5070 Laptop GPU, NVIDIA 617.42, Windows 11 - [x] AI-assisted: yes
oneandonlydean commented 2026-10-14 06:18:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T06:18:57Z.


Conflicts with main in tests/ShaderDiskCache.cpp; please rebase.
The description says the cold-load before/after follows as a comment; it isn't here yet. Please add it (main against main plus this PR, same machine and scene), since the gain is the reason for the change.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6162225118 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5315#issuecomment-6162225118). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T06:18:57Z. --- Conflicts with main in `tests/ShaderDiskCache.cpp`; please rebase. The description says the cold-load before/after follows as a comment; it isn't here yet. Please add it (main against main plus this PR, same machine and scene), since the gain is the reason for the change.
oneandonlydean commented 2026-10-14 21:33:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T21:33:21Z.


Still needs the rebase for tests/ShaderDiskCache.cpp (10-14) and the cold-load before/after the description promises: main against main plus this PR, same machine and scene.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6178982488 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5315#issuecomment-6178982488). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T21:33:21Z. --- Still needs the rebase for `tests/ShaderDiskCache.cpp` (10-14) and the cold-load before/after the description promises: main against main plus this PR, same machine and scene.
AlxcBTW commented 2026-10-15 18:38:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-15T18:38:21Z.


@oneandonlydean rebased onto main 03c4cb5e7.

Conflict. tests/ShaderDiskCache.cpp: main added the resource plan fixtures at the place where this PR adds
verifySpecializationDropsDeadConstants. Both are kept, the new test stays right after verifySpecializationLiveness. The
#include <set> was not used by the file and is gone. SpirvSpecialization.cpp and FormattedBufferOnce.cpp applied
without changes. tools/check_conventions.py --base origin/main passes.

Tests (fork, Linux, lavapipe, SPIRV-Tools on, all 605 agc_ tests):

  • this PR (run 38781694714): 604 pass. agc_driver_mesh_index_cache aborts with LLVM ERROR: Cannot emit physreg copy instruction inside llvmpipe's JIT.
  • main 03c4cb5e7 (run 38781698319): the same test aborts the same way, 604 pass. It is not related to this change.
  • agc_shader_disk_cache (with the new test), agc_formatted_buffer_once, agc_spirv_module, agc_spirv_optimizer and
    the other recompiler tests pass in both runs.
  • Windows: the libs and relinker targets compile (fork run 38781966955, MinGW). The Windows ctest run was not repeated on this base.

Sizes in agc_formatted_buffer_once, words, Linux with SPIRV-Tools, measured on the rebased tree:

main 03c4cb5e7 this PR
vertex, specialized 1348 1126
vertex, registration 1917 1739
registration / specialized 1.42 1.54
compute, 1 load (known descriptor) 1209 987
compute, GPU-selected descriptor 13925 13897

The 1.6x bound is still needed: 1.5x of 1126 is 1689 words and the registration module has 1739; 1.6x gives 1801. The
GPU-selected compute module is 13,897 words now (it was 85,945 when this PR was opened; #3656 decodes it from a format
table), so "tens of thousands of words" in the description no longer fits. A registration module that kept the decoders of
every format would be about as large as that compute module, roughly 14,000 words, far above 1801.

Cold load. The Sword of the Sea dump the description was written against is gone, so the before/after runs on Minecraft Dungeons II (PPSA16064) at its title screen, one machine (RTX 5070 Laptop 8 GB, NVIDIA 617.42, Windows 11), main 03c4cb5e7 against main plus this PR, four alternating cold rows with the shader cache emptied before each. It is queued on the lab machine tonight; the numbers follow in the next comment.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6199534552 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5315#issuecomment-6199534552). Originally posted by [AlxcBTW](https://github.com/AlxcBTW) on 2026-10-15T18:38:21Z. --- @oneandonlydean rebased onto `main` 03c4cb5e7. **Conflict.** `tests/ShaderDiskCache.cpp`: `main` added the resource plan fixtures at the place where this PR adds `verifySpecializationDropsDeadConstants`. Both are kept, the new test stays right after `verifySpecializationLiveness`. The `#include <set>` was not used by the file and is gone. `SpirvSpecialization.cpp` and `FormattedBufferOnce.cpp` applied without changes. `tools/check_conventions.py --base origin/main` passes. **Tests** (fork, Linux, lavapipe, SPIRV-Tools on, all 605 `agc_` tests): - this PR ([run 38781694714](https://github.com/AlxcBTW/AnyPS5/actions/runs/38781694714)): 604 pass. `agc_driver_mesh_index_cache` aborts with `LLVM ERROR: Cannot emit physreg copy instruction` inside llvmpipe's JIT. - `main` 03c4cb5e7 ([run 38781698319](https://github.com/AlxcBTW/AnyPS5/actions/runs/38781698319)): the same test aborts the same way, 604 pass. It is not related to this change. - `agc_shader_disk_cache` (with the new test), `agc_formatted_buffer_once`, `agc_spirv_module`, `agc_spirv_optimizer` and the other recompiler tests pass in both runs. - Windows: the `libs` and `relinker` targets compile ([fork run 38781966955](https://github.com/AlxcBTW/AnyPS5/actions/runs/38781966955), MinGW). The Windows ctest run was not repeated on this base. **Sizes** in `agc_formatted_buffer_once`, words, Linux with SPIRV-Tools, measured on the rebased tree: | | `main` 03c4cb5e7 | this PR | |---|---|---| | vertex, specialized | 1348 | 1126 | | vertex, registration | 1917 | 1739 | | registration / specialized | 1.42 | 1.54 | | compute, 1 load (known descriptor) | 1209 | 987 | | compute, GPU-selected descriptor | 13925 | 13897 | The 1.6x bound is still needed: 1.5x of 1126 is 1689 words and the registration module has 1739; 1.6x gives 1801. The GPU-selected compute module is 13,897 words now (it was 85,945 when this PR was opened; #3656 decodes it from a format table), so "tens of thousands of words" in the description no longer fits. A registration module that kept the decoders of every format would be about as large as that compute module, roughly 14,000 words, far above 1801. **Cold load.** The Sword of the Sea dump the description was written against is gone, so the before/after runs on Minecraft Dungeons II (PPSA16064) at its title screen, one machine (RTX 5070 Laptop 8 GB, NVIDIA 617.42, Windows 11), `main` 03c4cb5e7 against `main` plus this PR, four alternating cold rows with the shader cache emptied before each. It is queued on the lab machine tonight; the numbers follow in the next comment.
oneandonlydean commented 2026-10-17 00:41:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T00:41:25Z.


The rebase is right, both tests kept. The agc_driver_mesh_index_cache llvmpipe abort also happens on main, so it isn't this PR. The size table is useful (specialized vertex 1348 to 1126 words, compute with a known descriptor 1209 to 987). What's left is the four alternating cold rows with the cache emptied before each.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6227471483 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5315#issuecomment-6227471483). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-17T00:41:25Z. --- The rebase is right, both tests kept. The `agc_driver_mesh_index_cache` llvmpipe abort also happens on `main`, so it isn't this PR. The size table is useful (specialized vertex 1348 to 1126 words, compute with a known descriptor 1209 to 987). What's left is the four alternating cold rows with the cache emptied before each.
AlxcBTW commented 2026-10-17 05:45:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-17T05:45:18Z.


Cold-load numbers from Minecraft Dungeons II (PPSA16064) on Windows 11, NVIDIA GeForce RTX 5070 Laptop GPU (617.42): two rows per arm, main and this PR alternating, each with an empty AnyPS5 shader cache, boot to the title.

main (2 rows) this PR (2 rows)
distinct SPIR-V modules after specialization 23 / 23 21 / 21
words of those modules 4.02 M 3.60 M (-10 %)
creations that needed no new module 0 2
groups that differed only in dead constants 2 0
large pipelines / programs / graphics pipelines 23 / 21 / 110 23 / 21 / 110
pipeline creation time 0.7 s / 0.7 s 15.7 s / 0.7 s
vk-pipelines cache file 26.7 MiB 25.7 MiB

So the change does what it says: the two specializations that differed only in constants nothing references now share one module, and the modules are 10 % smaller. The 15.7 s in the PR's first row is the NVIDIA driver compiling the new modules once: the driver keeps its own cache outside the row's LOCALAPPDATA, so the main rows started with modules it had already seen, and the PR's second row is back at 0.7 s. That row does not measure the change, and I'd not claim a load-time win from these numbers; the gain is fewer and smaller modules.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6230326544 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5315#issuecomment-6230326544). Originally posted by [AlxcBTW](https://github.com/AlxcBTW) on 2026-10-17T05:45:18Z. --- Cold-load numbers from Minecraft Dungeons II (PPSA16064) on Windows 11, NVIDIA GeForce RTX 5070 Laptop GPU (617.42): two rows per arm, `main` and this PR alternating, each with an empty AnyPS5 shader cache, boot to the title. | | main (2 rows) | this PR (2 rows) | |---|---|---| | distinct SPIR-V modules after specialization | 23 / 23 | 21 / 21 | | words of those modules | 4.02 M | 3.60 M (-10 %) | | creations that needed no new module | 0 | 2 | | groups that differed only in dead constants | 2 | 0 | | large pipelines / programs / graphics pipelines | 23 / 21 / 110 | 23 / 21 / 110 | | pipeline creation time | 0.7 s / 0.7 s | 15.7 s / 0.7 s | | `vk-pipelines` cache file | 26.7 MiB | 25.7 MiB | So the change does what it says: the two specializations that differed only in constants nothing references now share one module, and the modules are 10 % smaller. The 15.7 s in the PR's first row is the NVIDIA driver compiling the new modules once: the driver keeps its own cache outside the row's `LOCALAPPDATA`, so the `main` rows started with modules it had already seen, and the PR's second row is back at 0.7 s. That row does not measure the change, and I'd not claim a load-time win from these numbers; the gain is fewer and smaller modules. AI-assisted: yes (Claude Code)
oneandonlydean commented 2026-10-19 10:54:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-19T10:54:55Z.


@AlxcBTW Since your 15 Oct rebase, main gained 3e1f932d41 (#6176, "share folded SPIR-V constants and drop identity bitcasts"). It already removes unused folded constants, but it keeps the definitions that undecoded consumers (subgroup and extended instructions) still name.

Merged onto current main, this PR's sweep doesn't keep those, so two tests fail:

  • main's agc_spirv_optimizer: "specialization kept an unreferenced folded constant or lost a referenced one"
  • your own verifySpecializationDropsDeadConstants in agc_shader_disk_cache: "variants that differ only in a folded constant specialized to different SPIR-V"

With main's SpirvSpecialization.cpp, the optimizer test passes. The text merge is clean, so CI on the old base doesn't show this.

Could you rebase onto current main, keep the constants that main's removal keeps, and show both tests passing? Please also say what this PR still adds on top of 3e1f932d41, for example module word counts before and after on main. Thanks!

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6265563020 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5315#issuecomment-6265563020). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-19T10:54:55Z. --- @AlxcBTW Since your 15 Oct rebase, `main` gained 3e1f932d417 (#6176, "share folded SPIR-V constants and drop identity bitcasts"). It already removes unused folded constants, but it keeps the definitions that undecoded consumers (subgroup and extended instructions) still name. Merged onto current `main`, this PR's sweep doesn't keep those, so two tests fail: - `main`'s `agc_spirv_optimizer`: "specialization kept an unreferenced folded constant or lost a referenced one" - your own `verifySpecializationDropsDeadConstants` in `agc_shader_disk_cache`: "variants that differ only in a folded constant specialized to different SPIR-V" With `main`'s `SpirvSpecialization.cpp`, the optimizer test passes. The text merge is clean, so CI on the old base doesn't show this. Could you rebase onto current `main`, keep the constants that `main`'s removal keeps, and show both tests passing? Please also say what this PR still adds on top of 3e1f932d417, for example module word counts before and after on `main`. Thanks!
THEMANnt commented 2026-10-26 13:50:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by THEMANnt on 2026-10-26T13:50:24Z.

Review: APPROVED


Reviewed 1ea471b57089 against merge base 03c4cb5e78e0. Recommend approval; no actionable finding in the complete three-file patch.

The iterative cleanup removes unused specialized constants and associated metadata while retaining referenced array lengths and the composite operands preserved by the current visitor. Reviewed the specialization-to-cache/compilation flow and the adjusted formatted-buffer size bound.

Validation: Built agc_shader_disk_cache_tests on candidate and base with Windows GCC 15.3; both CTest runs pass. An additional review-only harness passes checks for dead scalar/bool/null constants, consumed-value/name removal, retained array/composite operands, unchanged input and idempotence. Its initially incorrect composite-removal expectation was corrected before the final passing run; no production source was changed.

Scope: ANYPS5_ENABLE_SPIRV_TOOLS=0, so no independent SPIR-V validator ran. No local Linux/lavapipe execution; hosted product jobs were skipped. The baseline's original-suite pass is not a failing-old-code regression control.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5653001623 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5315#pullrequestreview-5653001623). Originally posted by [THEMANnt](https://github.com/THEMANnt) on 2026-10-26T13:50:24Z. **Review:** APPROVED --- Reviewed `1ea471b57089` against merge base `03c4cb5e78e0`. Recommend approval; no actionable finding in the complete three-file patch. The iterative cleanup removes unused specialized constants and associated metadata while retaining referenced array lengths and the composite operands preserved by the current visitor. Reviewed the specialization-to-cache/compilation flow and the adjusted formatted-buffer size bound. **Validation:** Built `agc_shader_disk_cache_tests` on candidate and base with Windows GCC 15.3; both CTest runs pass. An additional review-only harness passes checks for dead scalar/bool/null constants, consumed-value/name removal, retained array/composite operands, unchanged input and idempotence. Its initially incorrect composite-removal expectation was corrected before the final passing run; no production source was changed. **Scope:** `ANYPS5_ENABLE_SPIRV_TOOLS=0`, so no independent SPIR-V validator ran. No local Linux/lavapipe execution; hosted product jobs were skipped. The baseline's original-suite pass is not a failing-old-code regression control.
jotitadev commented 2026-10-27 08:25:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-27T08:25:36Z.

Review: COMMENTED


Follow-up to my merge-skew note above, now with ANYPS5_ENABLE_SPIRV_TOOLS=ON (that build did not have it). Linux, i7-8700, Ubuntu 26.04, GCC 15.2, Release. Head 1ea471b57 merged with --no-ff onto main 8df2c3ed0. lavapipe (Mesa 26.0.3).

The disk-cache fix from my earlier comment does not cover the optimizer test @oneandonlydean mentioned:

agc_shader_disk_cache agc_spirv_optimizer (+ _full, _none)
main pass pass
main + this PR fail 3/3 fail 3/3
main + this PR + the 6-line SpirvSpecialization.cpp fix pass 3/3 fail 3/3

Only one check fails in testSpecializedConstantsAreShared: "specialization kept an unreferenced folded constant or lost a referenced one" (constants.size() == 5u). The module itself is fine: ValidateAndOptimizeSpirv accepts it and every other check in that test passes (sharing, no bitcasts or copies, lane + 23, stable second pass).

The two missing constants are not folded ones. main leaves %one = 1 and %three = 3 from the source in place (it only drops unreferenced constants that the folding itself materialized), so the test expects 1, 2, 3, 7, 8. This PR's sweep also removes original module constants that nothing references, so the output has 2, 7, 8. That is the intended effect of the PR, so it comes down to which behaviour main wants:

  • keep the sweep and update the expectation to 3u, or
  • limit the sweep to what main's removeUnusedConstants already removes.

With the disk-cache fix plus 5u → 3u, the ^agc suite on lavapipe with SPIRV-Tools ON (1053 tests, display/flip/lds_atomics64 excluded) matches main: 1051/1053, failing only agc_driver_multisample_retirement and agc_driver_guest_memory_lifetime_converted, which fail on main too.

GPUs tested: lavapipe (Mesa 26.0.3) for everything above. On main, the 10 tests that only exist with SPIRV-Tools ON also pass on an RTX 3070 (NVIDIA 595.99.02).

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5664042278 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5315#pullrequestreview-5664042278). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-27T08:25:36Z. **Review:** COMMENTED --- Follow-up to my merge-skew note above, now with `ANYPS5_ENABLE_SPIRV_TOOLS=ON` (that build did not have it). Linux, i7-8700, Ubuntu 26.04, GCC 15.2, Release. Head `1ea471b57` merged with `--no-ff` onto `main` `8df2c3ed0`. lavapipe (Mesa 26.0.3). The disk-cache fix from my earlier comment does **not** cover the optimizer test @oneandonlydean mentioned: | | `agc_shader_disk_cache` | `agc_spirv_optimizer` (+ `_full`, `_none`) | |---|---|---| | `main` | pass | pass | | `main` + this PR | fail 3/3 | fail 3/3 | | `main` + this PR + the 6-line `SpirvSpecialization.cpp` fix | pass 3/3 | fail 3/3 | Only one check fails in `testSpecializedConstantsAreShared`: "specialization kept an unreferenced folded constant or lost a referenced one" (`constants.size() == 5u`). The module itself is fine: `ValidateAndOptimizeSpirv` accepts it and every other check in that test passes (sharing, no bitcasts or copies, `lane + 23`, stable second pass). The two missing constants are not folded ones. `main` leaves `%one = 1` and `%three = 3` from the source in place (it only drops unreferenced constants that the folding itself materialized), so the test expects 1, 2, 3, 7, 8. This PR's sweep also removes original module constants that nothing references, so the output has 2, 7, 8. That is the intended effect of the PR, so it comes down to which behaviour `main` wants: - keep the sweep and update the expectation to `3u`, or - limit the sweep to what `main`'s `removeUnusedConstants` already removes. With the disk-cache fix plus `5u` → `3u`, the `^agc` suite on lavapipe with SPIRV-Tools ON (1053 tests, display/flip/lds_atomics64 excluded) matches `main`: 1051/1053, failing only `agc_driver_multisample_retirement` and `agc_driver_guest_memory_lifetime_converted`, which fail on `main` too. GPUs tested: lavapipe (Mesa 26.0.3) for everything above. On `main`, the 10 tests that only exist with SPIRV-Tools ON also pass on an RTX 3070 (NVIDIA 595.99.02). Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
BunnyFl commented 2026-11-04 18:14:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-04T18:14:01Z.

Review: CHANGES_REQUESTED


@AlxcBTW the new verifySpecializationDropsDeadConstants fails once this PR is merged into current main (runs below). So on main, the two variants in the test still specialize to different SPIR-V.

What I ran: head 1ea471b570 merged onto main 9ab6a281ea (clean merge, e4a39e6cd3). Windows 11, WinLibs GCC 15.2.0, AMD RX 9070 XT, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation:

  • agc_formatted_buffer_once passes.
  • agc_shader_disk_cache fails with variants that differ only in a folded constant specialized to different SPIR-V.
  • With main's SpirvSpecialization.cpp restored, it fails the same way.
  • No VUID- errors and no missing-layer lines in either run.
  • The head alone, unmerged (base 03c4cb5e78): agc_shader_disk_cache passes. So the failure comes from what main has changed in SpirvSpecialization.cpp since then (13 commits, merges included).

Why, as far as I read it (main 9ab6a281ea):

  • At parse time, each scalar module constant registers as the canonical id for its type and value. The first one wins, because it's an emplace (SpirvSpecialization.cpp#L394).
  • When fold turns an instruction into a constant whose value is already canonical, it records an alias to that id (#L623). shareConstants (#L849) then rewrites the uses to the canonical id. Run calls it (#L428) before removeUnusedConstants (#L431).
  • In the test module, the selector is %6 and the selected constant is %7 = 4. With selector 4, %6 and %7 both hold 4, and %6 is emitted first. By the emplace at #L394 above, the folded OpSelect then aliases to %6, and the store reads %6. With selector 6, the store reads %7.
  • So after this PR removes the unused one, the two modules still differ: OpConstant %3 %6 4 plus OpStore %8 %6, against OpConstant %3 %7 4 plus OpStore %8 %7.
  • Probe: on the same merge, I changed only the test's first variant from module(4u) to module(8u), which gives selector 8, a value no other constant in the test module has. agc_shader_disk_cache then passes. That fits the explanation above.

The channel selectors from the description (4, 5, 6, 7) are the kind of values that may equal another constant in a real module, so the cache misses may persist on main for those programs. I haven't checked that on a real module. For the fix, one option is to make the canonical id independent of the selector's value. For example, re-number the surviving constants after removal, or prefer the constant that the folded instruction's own operands name. Either way, the test needs to pass on the merge with current main.

Also checked:

  • pr_overlap.py --branch on the head: no conflicting lines or shared exports. 20 PRs that conflict with main share files and couldn't be compared. The only one on SpirvSpecialization.cpp is #8004, which moves helpers near VisitInputs and doesn't touch removeUnusedConstants or the constant sharing.
  • Minor point for the description: it says OpConstantComposite is not removed, but the loop does include it in the opcodes it removes. Whether a composite is ever unreferenced depends on VisitIdOperands, which I haven't read through.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5773421951 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5315#pullrequestreview-5773421951). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-04T18:14:01Z. **Review:** CHANGES_REQUESTED --- @AlxcBTW the new `verifySpecializationDropsDeadConstants` fails once this PR is merged into current `main` (runs below). So on `main`, the two variants in the test still specialize to different SPIR-V. **What I ran:** head `1ea471b570` merged onto `main` `9ab6a281ea` (clean merge, `e4a39e6cd3`). Windows 11, WinLibs GCC 15.2.0, AMD RX 9070 XT, `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`: - `agc_formatted_buffer_once` passes. - `agc_shader_disk_cache` fails with `variants that differ only in a folded constant specialized to different SPIR-V`. - With `main`'s `SpirvSpecialization.cpp` restored, it fails the same way. - No `VUID-` errors and no missing-layer lines in either run. - The head alone, unmerged (base `03c4cb5e78`): `agc_shader_disk_cache` passes. So the failure comes from what `main` has changed in `SpirvSpecialization.cpp` since then (13 commits, merges included). **Why, as far as I read it** (`main` `9ab6a281ea`): - At parse time, each scalar module constant registers as the canonical id for its type and value. The first one wins, because it's an `emplace` ([SpirvSpecialization.cpp#L394](https://github.com/boykopovar/AnyPS5/blob/9ab6a281ea61c9e62a7eb07eb4cef8f19ede1c5c/core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp#L394)). - When `fold` turns an instruction into a constant whose value is already canonical, it records an alias to that id ([#L623](https://github.com/boykopovar/AnyPS5/blob/9ab6a281ea61c9e62a7eb07eb4cef8f19ede1c5c/core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp#L623)). `shareConstants` ([#L849](https://github.com/boykopovar/AnyPS5/blob/9ab6a281ea61c9e62a7eb07eb4cef8f19ede1c5c/core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp#L849)) then rewrites the uses to the canonical id. `Run` calls it ([#L428](https://github.com/boykopovar/AnyPS5/blob/9ab6a281ea61c9e62a7eb07eb4cef8f19ede1c5c/core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp#L428)) before `removeUnusedConstants` ([#L431](https://github.com/boykopovar/AnyPS5/blob/9ab6a281ea61c9e62a7eb07eb4cef8f19ede1c5c/core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp#L431)). - In the test module, the selector is `%6` and the selected constant is `%7 = 4`. With selector 4, `%6` and `%7` both hold 4, and `%6` is emitted first. By the `emplace` at #L394 above, the folded `OpSelect` then aliases to `%6`, and the store reads `%6`. With selector 6, the store reads `%7`. - So after this PR removes the unused one, the two modules still differ: `OpConstant %3 %6 4` plus `OpStore %8 %6`, against `OpConstant %3 %7 4` plus `OpStore %8 %7`. - **Probe:** on the same merge, I changed only the test's first variant from `module(4u)` to `module(8u)`, which gives selector 8, a value no other constant in the test module has. `agc_shader_disk_cache` then passes. That fits the explanation above. The channel selectors from the description (4, 5, 6, 7) are the kind of values that may equal another constant in a real module, so the cache misses may persist on `main` for those programs. I haven't checked that on a real module. For the fix, one option is to make the canonical id independent of the selector's value. For example, re-number the surviving constants after removal, or prefer the constant that the folded instruction's own operands name. Either way, the test needs to pass on the merge with current `main`. **Also checked:** - `pr_overlap.py --branch` on the head: no conflicting lines or shared exports. 20 PRs that conflict with `main` share files and couldn't be compared. The only one on `SpirvSpecialization.cpp` is #8004, which moves helpers near `VisitInputs` and doesn't touch `removeUnusedConstants` or the constant sharing. - Minor point for the description: it says `OpConstantComposite` is not removed, but the loop does include it in the opcodes it removes. Whether a composite is ever unreferenced depends on `VisitIdOperands`, which I haven't read through.
MarcZX10 commented 2026-11-23 03:06:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T03:06:51Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the implementation and focused regression test support the stated dead-constant cleanup.

What I ran

Reviewed head 1ea471b57089e902583e51e5cf8aa0a7b8a6a5c2 using repo_read.py to inspect the prepared diff and relevant code. No commands or tests were run in this review.

Results

Check Result Note
Diff and changed code passed Three changed files reviewed
Tests not run CI results are author-reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

Did not run tests or independently verify the reported CI and game performance measurements. Read all three changed files: FormattedBufferOnce.cpp, ShaderDiskCache.cpp, and SpirvSpecialization.cpp. No changed files were excluded; files.json was not available through the reader.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6016124493 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5315#pullrequestreview-6016124493). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-23T03:06:51Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the implementation and focused regression test support the stated dead-constant cleanup. ## What I ran Reviewed head `1ea471b57089e902583e51e5cf8aa0a7b8a6a5c2` using `repo_read.py` to inspect the prepared diff and relevant code. No commands or tests were run in this review. ## Results | Check | Result | Note | |---|---|---| | Diff and changed code | passed | Three changed files reviewed | | Tests | not run | CI results are author-reported | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified Did not run tests or independently verify the reported CI and game performance measurements. Read all three changed files: `FormattedBufferOnce.cpp`, `ShaderDiskCache.cpp`, and `SpirvSpecialization.cpp`. No changed files were excluded; `files.json` was not available through the reader. ## To get this to a good state Nothing is required.

Pull request closed

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!5315
No description provided.