perf(shader): discard helper functions unreachable after specialization #5064

Closed
Scramblrrrr wants to merge 3 commits from upstream-pr/5064 into main
Scramblrrrr commented 2026-10-13 10:17:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-13T10:17:10Z.


What

Specialization can remove a call in a folded-away branch while leaving the callee and its helper chain in the emitted SPIR-V. Remove whole function bodies that are no longer reachable after folding and dead-computation cleanup.

The pass starts from every entry point and module-level function reference, then follows function references transitively. It preserves exported/imported linkage declarations and functions whose local results are referenced by global group/ID/string annotations. Modules without entry points are left alone. Removed result IDs use the existing name/decoration cleanup. The existing ID-operand visitor distinguishes literal words from IDs, including 64-bit switch literals; unknown operands conservatively retain references.

This changes function reachability cleanup, not shader instruction semantics or the structurizer. It is independent of the larger 007 cross-test stack. No title stop, rendering fix, compile-time reduction or FPS improvement is claimed.

Tested

Windows 11, MinGW GCC 15.2 Release, Ryzen 7 7800X3D, RTX 5070 Ti, SPIRV-Tools OFF. Head c94673ecd086cc4f9d98692200194e0189710e09, based on main 77b9f7270c25518d26052671311f43497328fdc7.

  • Built the patched graphics runtime and the four CPU shader test targets, native BDA device target and recorder target against this isolated branch.
  • Eight selected CPU/Vulkan checks passed in 6.34 s with required native Vulkan, no skips: agc_spirv_module, agc_spirv_branch_targets, agc_shader_memory, agc_shader_disk_cache, agc_driver_bda_device, agc_pipeline_cache, agc_storage_baseline_settled, agc_storage_upload_lifetime.
  • The synthetic regression checks a folded-away call and its dead helper chain, live transitive helpers, two entry points, an integer-width literal colliding with a dead function ID, removed names, preservation of an exported function and a group annotation to a local result, modules without entry points, and stable repeated specialization.
  • Production-only negative: disable only removeUnusedFunctions(), keeping the fixtures. The module test fails its dead-helper and dangling-name checks; the other three CPU controls pass. Restore the pass and disable only the linkage/string annotation guard: the module fixture fails because the exported function is lost. Restoring the source and rebuilding passes the eight checks above.
  • Conventions and whitespace checks pass; source is clean. SPIRV-Tools validator/optimizer fixtures, the full suite and Linux/macOS execution were not run. The string-annotation guard is conservative; the focused annotation fixture exercises group annotations and linkage exports.

An exploratory 007 run on a separate combined branch did not establish a performance gain: the largest startup modules retained the same word counts, and cold area pipelines were still compiling when that test ended. That is not an isolated-main title benchmark. No title code, captures, assets or raw title logs are uploaded.

Checklist

  • Based on current main; searches found no equivalent open helper-function reachability change
  • One topic per PR
  • No new code comments
  • Unsupported paths still throw; no stubs added
  • No investigation files, logs, images or title data added to the repository
  • No new third-party code
  • Generic behavior
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes (Codex)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4841731787 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5064). Originally posted by [Scramblrrrr](https://github.com/Scramblrrrr) on 2026-10-13T10:17:10Z. --- ### What Specialization can remove a call in a folded-away branch while leaving the callee and its helper chain in the emitted SPIR-V. Remove whole function bodies that are no longer reachable after folding and dead-computation cleanup. The pass starts from every entry point and module-level function reference, then follows function references transitively. It preserves exported/imported linkage declarations and functions whose local results are referenced by global group/ID/string annotations. Modules without entry points are left alone. Removed result IDs use the existing name/decoration cleanup. The existing ID-operand visitor distinguishes literal words from IDs, including 64-bit switch literals; unknown operands conservatively retain references. This changes function reachability cleanup, not shader instruction semantics or the structurizer. It is independent of the larger 007 cross-test stack. No title stop, rendering fix, compile-time reduction or FPS improvement is claimed. ### Tested Windows 11, MinGW GCC 15.2 Release, Ryzen 7 7800X3D, RTX 5070 Ti, SPIRV-Tools OFF. Head `c94673ecd086cc4f9d98692200194e0189710e09`, based on main `77b9f7270c25518d26052671311f43497328fdc7`. - Built the patched graphics runtime and the four CPU shader test targets, native BDA device target and recorder target against this isolated branch. - Eight selected CPU/Vulkan checks passed in 6.34 s with required native Vulkan, no skips: `agc_spirv_module`, `agc_spirv_branch_targets`, `agc_shader_memory`, `agc_shader_disk_cache`, `agc_driver_bda_device`, `agc_pipeline_cache`, `agc_storage_baseline_settled`, `agc_storage_upload_lifetime`. - The synthetic regression checks a folded-away call and its dead helper chain, live transitive helpers, two entry points, an integer-width literal colliding with a dead function ID, removed names, preservation of an exported function and a group annotation to a local result, modules without entry points, and stable repeated specialization. - Production-only negative: disable only `removeUnusedFunctions()`, keeping the fixtures. The module test fails its dead-helper and dangling-name checks; the other three CPU controls pass. Restore the pass and disable only the linkage/string annotation guard: the module fixture fails because the exported function is lost. Restoring the source and rebuilding passes the eight checks above. - Conventions and whitespace checks pass; source is clean. SPIRV-Tools validator/optimizer fixtures, the full suite and Linux/macOS execution were not run. The string-annotation guard is conservative; the focused annotation fixture exercises group annotations and linkage exports. An exploratory 007 run on a separate combined branch did not establish a performance gain: the largest startup modules retained the same word counts, and cold area pipelines were still compiling when that test ended. That is not an isolated-main title benchmark. No title code, captures, assets or raw title logs are uploaded. ### Checklist - [x] Based on current `main`; searches found no equivalent open helper-function reachability change - [x] One topic per PR - [x] No new code comments - [x] Unsupported paths still throw; no stubs added - [x] No investigation files, logs, images or title data added to the repository - [x] No new third-party code - [x] Generic behavior - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes (Codex)
github-actions[bot] commented 2026-10-13 10:31:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T10:31:03Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#4772 core/libs/prx/libSceAgcDriver/tests/SpirvModule.cpp L287-289
core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp L732-734
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6143583695 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5064#issuecomment-6143583695). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-13T10:31:03Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #4772 | `core/libs/prx/libSceAgcDriver/tests/SpirvModule.cpp` [L287-289](https://github.com/boykopovar/AnyPS5/blob/0df6e295c52e7cd6afa394ed35513bd252045c74/core/libs/prx/libSceAgcDriver/tests/SpirvModule.cpp#L287-L289)<br>`core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp` [L732-734](https://github.com/boykopovar/AnyPS5/blob/0df6e295c52e7cd6afa394ed35513bd252045c74/core/shader/recompiler/SpirvBackend/src/SpirvSpecialization.cpp#L732-L734) | | |
Scramblrrrr commented 2026-10-13 12:34:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-13T12:34:13Z.


Reviewed the overlap report against #4772 head007721d43e0511e446cd59d2854fe6ff80ff9a69. The behaviors are complementary: #4772 replaces specialization id maps/sets with bound-indexed containers, while this PR adds function reachability after specialization and removes helpers unreachable from entry/export/annotation roots. Neither implementation is a prerequisite for the other.

There is an integration adjustment beyond the adjacent test insertions: removeUnusedFunctions uses the existing map-style resultTypes.find iterator for OpSwitch literal width; #4772 changes find to a pointer. If #4772 lands first I will rebase and adapt that lookup, retain both fixture sets, and rerun CPU/native controls. Combined build/tests have not been run yet. No title benefit claimed. AI-assisted (Codex).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6145647591 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5064#issuecomment-6145647591). Originally posted by [Scramblrrrr](https://github.com/Scramblrrrr) on 2026-10-13T12:34:13Z. --- Reviewed the overlap report against #4772 head007721d43e0511e446cd59d2854fe6ff80ff9a69. The behaviors are complementary: #4772 replaces specialization id maps/sets with bound-indexed containers, while this PR adds function reachability after specialization and removes helpers unreachable from entry/export/annotation roots. Neither implementation is a prerequisite for the other. There is an integration adjustment beyond the adjacent test insertions: removeUnusedFunctions uses the existing map-style resultTypes.find iterator for OpSwitch literal width; #4772 changes find to a pointer. If #4772 lands first I will rebase and adapt that lookup, retain both fixture sets, and rerun CPU/native controls. Combined build/tests have not been run yet. No title benefit claimed. AI-assisted (Codex).
coltken95 commented 2026-10-13 18:52:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by coltken95 on 2026-10-13T18:52:26Z.


Tested on Linux x86-64 (Release build), with c94673ecd merged into main 9d0a41d5a:

  • With the PR: agc_spirv_module passes.
  • With main's sources and the PR's test: agc_spirv_module fails.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6152815108 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5064#issuecomment-6152815108). Originally posted by [coltken95](https://github.com/coltken95) on 2026-10-13T18:52:26Z. --- Tested on Linux x86-64 (Release build), with c94673ecd merged into main 9d0a41d5a: - With the PR: `agc_spirv_module` passes. - With main's sources and the PR's test: `agc_spirv_module` fails. AI-assisted: yes (Claude Code)
Scramblrrrr commented 2026-10-13 19:25:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-13T19:25:17Z.


Thanks for the independent Linux cross-test and production-negative with the PR fixture retained. That complements the Windows native and reachability-negative controls in the description. No source revision is needed for this result; current head remains c94673ecd0. AI-assisted: yes (Codex).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6153415518 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5064#issuecomment-6153415518). Originally posted by [Scramblrrrr](https://github.com/Scramblrrrr) on 2026-10-13T19:25:17Z. --- Thanks for the independent Linux cross-test and production-negative with the PR fixture retained. That complements the Windows native and reachability-negative controls in the description. No source revision is needed for this result; current head remains c94673ecd086cc4f9d98692200194e0189710e09. AI-assisted: yes (Codex).
CnRJay commented 2026-10-14 00:59:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T00:59:10Z.

Review: COMMENTED


The reachability logic looks careful. Roots are entry points, module-level references, linkage exports and annotated local results; unknown operands stay conservative; 64-bit switch literals are handled. The mutation tests are good.

My concern is cost against benefit, since this runs inside SpecializeSpirv for every pipeline variant:

  • The pass walks every instruction twice and builds std::map<id, std::set<id>> / std::set structures over the whole module, with a log-n lookup (functions.contains) per ID operand. For the 1M+ word compute modules mentioned in other PRs (Yōtei, GTA V RT), that is a noticeable amount of allocation and pointer chasing on the path that creates a variant, which may run on a queue worker when a variant is first used.
  • By the description's own exploratory run, the largest startup modules kept the same word counts, so no title has shown a module that gets smaller yet.

Before merging, I'd suggest:

  1. Time SpecializeSpirv on a large captured module (agc_compute_pipeline_replay captures work) with and without the pass. Flat std::vector<uint32_t> tables indexed by ID (the module's bound is known), as #4772 does for the specialization maps, would make it close to free.
  2. Find one title module where a helper is actually removed, and report the word count and pipeline creation time before and after. Without one, the pass only adds work.

If neither shows a gain, a cheap early-out (skip the pass when the module has one function, or when specialization removed no OpFunctionCall) would at least keep the common case at zero cost.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5511847293 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5064#pullrequestreview-5511847293). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-10-14T00:59:10Z. **Review:** COMMENTED --- The reachability logic looks careful. Roots are entry points, module-level references, linkage exports and annotated local results; unknown operands stay conservative; 64-bit switch literals are handled. The mutation tests are good. My concern is cost against benefit, since this runs inside `SpecializeSpirv` for **every** pipeline variant: - The pass walks every instruction twice and builds `std::map<id, std::set<id>>` / `std::set` structures over the whole module, with a log-n lookup (`functions.contains`) per ID operand. For the 1M+ word compute modules mentioned in other PRs (Yōtei, GTA V RT), that is a noticeable amount of allocation and pointer chasing on the path that creates a variant, which may run on a queue worker when a variant is first used. - By the description's own exploratory run, the largest startup modules kept the same word counts, so no title has shown a module that gets smaller yet. Before merging, I'd suggest: 1. Time `SpecializeSpirv` on a large captured module (`agc_compute_pipeline_replay` captures work) with and without the pass. Flat `std::vector<uint32_t>` tables indexed by ID (the module's `bound` is known), as #4772 does for the specialization maps, would make it close to free. 2. Find one title module where a helper is actually removed, and report the word count and pipeline creation time before and after. Without one, the pass only adds work. If neither shows a gain, a cheap early-out (skip the pass when the module has one function, or when specialization removed no `OpFunctionCall`) would at least keep the common case at zero cost.
oneandonlydean commented 2026-10-14 02:33:39 +00:00 (Migrated from github.com)

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


Closing: no measured gain in a title yet; the only run is agc_spirv_module. Reopen with before/after numbers from one title scene showing the specialized helpers matter.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159595851 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5064#issuecomment-6159595851). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:33:39Z. --- Closing: no measured gain in a title yet; the only run is `agc_spirv_module`. Reopen with before/after numbers from one title scene showing the specialized helpers matter.

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