perf(shader): index specialization tables by SPIR-V id #4772

Merged
TideOPL merged 1 commit from upstream-pr/4772 into main 2026-10-14 03:04:51 +00:00
TideOPL commented 2026-10-12 21:26:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TideOPL on 2026-10-12T21:26:31Z.


What

SpecializeSpirv runs whenever a prepared shader is materialized with a new set of specialization constants, on the queue worker that draws or dispatches. It kept all of its per-id data in std::map and std::set: folded values, result types, scalar types, vector types, removed ids, and per pass the blocks, edges, loop headers, live sets, extracts, copies and definitions. Run repeats fold, prune and copy propagation over the whole module until nothing changes, so a large module paid several full passes of tree lookups.

SPIR-V ids are dense below the header's bound. These tables become IdTable<T> (a value vector with a presence flag) and IdSet (a flag vector), indexed by id:

  • Lookups of literals. Values that may be literals rather than ids (the operands propagateCopies erases, the pending list in removeDeadComputations, evaluate's operand lookups) stay bounds-checked and read as absent past the bound.
  • Insertions. Inserting an id at or beyond the bound throws, since such a module is invalid.
  • Iteration. IdTable iterates in ascending id order, as the maps did. The loops over blocks, edges and definitions are unchanged, and the output is byte-identical.

The passes themselves are unchanged.

Tested

  • System: Windows 11 (26200), Ryzen 7 7800X3D, WinLibs GCC, Release, rebased on main c2565f014 (which includes #4600).

  • Real modules: I dumped 60 SpecializeSpirv inputs from Minecraft Dungeons II (PPSA16064) materializations: 2.35M words, the largest about 220k words. A throwaway benchmark (not part of this PR) runs SpecializeSpirv on each, best of 3:

    total combined FNV of the outputs
    main 271.9 / 273.2 ms 2913d850cbdad15d
    this PR 84.2 / 83.9 ms 2913d850cbdad15d

    The outputs are identical for every module. Measured again after the rebase onto #4600.

  • In the title (perf branch, not a clean-main A/B): MCD2 spent about 8.3 s in SpecializeSpirv during the first 40 s of a cold run, across 506 module builds. A single build took up to about 150 ms on a queue worker, which shows as a hitch when new effects appear. Steady-state frame times at the title were unchanged in a same-window A/B, as expected: the change only shortens first-use builds.

  • New check in agc_spirv_module: a module whose ids reach the header's bound is rejected. It fails on main, where the maps accept any id, and passes with this change.

  • Suites: ctest -R "^agc", 460 tests: 457 pass, agc_driver_stencil_ref_export is skipped as on main, and agc_driver_bda_span_reads and agc_driver_pixel_interlock fail with vkDeviceWaitIdle -4 (device lost). Those two fail the same way on main c2565f014 on this machine. tools/check_conventions.py passes.

  • Not run: Linux, AMD.

Rebased onto main after #4600 merged: loopHeaders is an id set next to #4600's continueHeaders map, which stays a std::map.

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
  • AI-assisted: yes. Written with AI assistance (Claude Code); I reviewed and tested it.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4835178271 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4772). Originally posted by [TideOPL](https://github.com/TideOPL) on 2026-10-12T21:26:31Z. --- ### What `SpecializeSpirv` runs whenever a prepared shader is materialized with a new set of specialization constants, on the queue worker that draws or dispatches. It kept all of its per-id data in `std::map` and `std::set`: folded values, result types, scalar types, vector types, removed ids, and per pass the blocks, edges, loop headers, live sets, extracts, copies and definitions. `Run` repeats fold, prune and copy propagation over the whole module until nothing changes, so a large module paid several full passes of tree lookups. SPIR-V ids are dense below the header's bound. These tables become `IdTable<T>` (a value vector with a presence flag) and `IdSet` (a flag vector), indexed by id: - **Lookups of literals.** Values that may be literals rather than ids (the operands `propagateCopies` erases, the pending list in `removeDeadComputations`, `evaluate`'s operand lookups) stay bounds-checked and read as absent past the bound. - **Insertions.** Inserting an id at or beyond the bound throws, since such a module is invalid. - **Iteration.** `IdTable` iterates in ascending id order, as the maps did. The loops over `blocks`, `edges` and `definitions` are unchanged, and the output is byte-identical. The passes themselves are unchanged. ### Tested - **System:** Windows 11 (26200), Ryzen 7 7800X3D, WinLibs GCC, Release, rebased on `main` c2565f014 (which includes #4600). - **Real modules:** I dumped 60 `SpecializeSpirv` inputs from Minecraft Dungeons II (PPSA16064) materializations: 2.35M words, the largest about 220k words. A throwaway benchmark (not part of this PR) runs `SpecializeSpirv` on each, best of 3: | | total | combined FNV of the outputs | |---|---|---| | `main` | 271.9 / 273.2 ms | `2913d850cbdad15d` | | this PR | 84.2 / 83.9 ms | `2913d850cbdad15d` | The outputs are identical for every module. Measured again after the rebase onto #4600. - **In the title (perf branch, not a clean-`main` A/B):** MCD2 spent about 8.3 s in `SpecializeSpirv` during the first 40 s of a cold run, across 506 module builds. A single build took up to about 150 ms on a queue worker, which shows as a hitch when new effects appear. Steady-state frame times at the title were unchanged in a same-window A/B, as expected: the change only shortens first-use builds. - **New check in `agc_spirv_module`:** a module whose ids reach the header's bound is rejected. It **fails on `main`**, where the maps accept any id, and passes with this change. - **Suites:** `ctest -R "^agc"`, 460 tests: 457 pass, `agc_driver_stencil_ref_export` is skipped as on `main`, and `agc_driver_bda_span_reads` and `agc_driver_pixel_interlock` fail with `vkDeviceWaitIdle` -4 (device lost). Those two fail the same way on `main` c2565f014 on this machine. `tools/check_conventions.py` passes. - **Not run:** Linux, AMD. Rebased onto `main` after #4600 merged: `loopHeaders` is an id set next to #4600's `continueHeaders` map, which stays a `std::map`. ### 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] Unblocks: none - [x] AI-assisted: yes. Written with AI assistance (Claude Code); I reviewed and tested it.
xXPBSXx commented 2026-10-13 03:27:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T03:27:15Z.


Windows test (MinGW GCC 15.2, Release, Windows 10, RTX 4080 SUPER). Built on current main 0c3dc9ca2 together with eight other open GPU PRs (their own commits cherry-picked, no merge commits), so only the tests that belong to this PR say something about it:

  • tests/SpirvModule.cpp (agc_spirv_module): 3 of 3 runs passed.

Not tested: no game run. The batch was built once, not per PR; it had no failures, so no bisect was needed.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6137529447 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4772#issuecomment-6137529447). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T03:27:15Z. --- Windows test (MinGW GCC 15.2, Release, Windows 10, RTX 4080 SUPER). Built on current main `0c3dc9ca2` together with eight other open GPU PRs (their own commits cherry-picked, no merge commits), so only the tests that belong to this PR say something about it: - `tests/SpirvModule.cpp` (`agc_spirv_module`): 3 of 3 runs passed. Not tested: no game run. The batch was built once, not per PR; it had no failures, so no bisect was needed. AI-assisted (Claude Code).
adriadam10 commented 2026-10-13 15:27:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T15:27:20Z.


Checked the id-indexed tables against the diff: no pass allocates ids beyond the header bound, literal lookups stay bounds-checked, iteration stays in ascending id order. Built and ran the AGC/AgcDriver tests on the PR merged onto main (only the agc_driver_tessellation segfault that already fails on main); agc_spirv_module passes and its new bound check fails without the change. Merging once the Windows build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6149054278 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4772#issuecomment-6149054278). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T15:27:20Z. --- Checked the id-indexed tables against the diff: no pass allocates ids beyond the header bound, literal lookups stay bounds-checked, iteration stays in ascending id order. Built and ran the AGC/AgcDriver tests on the PR merged onto main (only the `agc_driver_tessellation` segfault that already fails on main); `agc_spirv_module` passes and its new bound check fails without the change. Merging once the Windows build is green.
blawesom commented 2026-10-13 18:48:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-13T18:48:15Z.


Rebase handoff — rebased onto current main (staleness only, applied cleanly), authorship unchanged.

Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, AMD RX 9070 XT / RADV):

  • builds; tools/check_conventions.py --base origin/main passes
  • full suite: 1070/1071 — the only failure is the known main regression (agc_driver_tessellation segfault, #4727)

To pick it up:

git fetch https://github.com/blawesom/AnyPS5 rebase/4772
git checkout <your branch>
git merge --ff-only FETCH_HEAD

Head is 5b3dc805d.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6152738728 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4772#issuecomment-6152738728). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-13T18:48:15Z. --- **Rebase handoff** — rebased onto current `main` (staleness only, applied cleanly), authorship unchanged. Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, `BUILD_TESTING=ON`, AMD RX 9070 XT / RADV): - builds; `tools/check_conventions.py --base origin/main` passes - **full suite: 1070/1071** — the only failure is the known `main` regression (`agc_driver_tessellation` segfault, #4727) **To pick it up:** ``` git fetch https://github.com/blawesom/AnyPS5 rebase/4772 git checkout <your branch> git merge --ff-only FETCH_HEAD ``` Head is `5b3dc805d`.
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!4772
No description provided.