runtime: stop spirv_stores scan at OpFunction and use flat sorted tables #1948

Closed
IlyassRS3 wants to merge 1 commit from upstream-pr/1948 into main
IlyassRS3 commented 2026-11-25 16:30:47 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T16:30:47Z.


Item ID: RT-SPIRV-STORES-PREAMBLE-AND-FLAT-PERF (claimed in #1608 comment #7168043831).

Summary

Optimises leonida::spirv_declares_writable_storage() (runtime/src/spirv_stores.h) by stopping at the SPIR-V preamble boundary (OpFunction) and replacing seven per-call hash containers with compact flat vectors and early exits:

  • Stop at OpFunction (opcode 54):
    • In SPIR-V's logical module layout, all annotations (OpDecorate, OpMemberDecorate, OpGroupDecorate, OpGroupMemberDecorate), types, and global OpVariables (UniformConstant, Uniform, StorageBuffer) precede the first OpFunction. Previously, spirv_declares_writable_storage() walked the entire function body (95–99% of every translated RDNA2 shader) and also collected local Function-storage variables.
    • Also hardened the word-count check against addition wrap (count > words.size() - at).
  • Filter OpVariable and exit early before sorting:
    • Only records UniformConstant, Uniform, and StorageBuffer variables, and exits immediately when no storage-image / storage-buffer / BufferBlock candidate exists (e.g., ordinary sampled-image + UBO shaders).
  • Replace 7 std::unordered_* tables with flat sorted std::vectors:
    • Eliminates per-call hash-bucket and per-node allocations, resolves OpGroupDecorate / OpGroupMemberDecorate once before checking variables, and checks full [0, total) NonWritable member coverage in O(1) after binary-searching the deduplicated sorted (struct_id, member) slice ((hi - lo) >= total && lo[total - 1].second == total - 1).
  • runtime/test/spirv_stores_test.cpp:
    • Added regression coverage for shaders with OpFunction bodies and local Function OpVariables (both read-only and writable), Uniform BufferBlock via OpGroupDecorate, and duplicate OpMemberDecorate entries on member 0 not masking a writable member 1.

A/B Benchmark (-O2, 50k-word shaders with 10,000-instruction function bodies, 5,000 iters, 2 runs)

Shader kind Base (main) Head (this PR) Speedup Result
Sampled textures + UBO (Uniform Block) 17,682 / 15,802 ns 257.2 / 106.6 ns 68.8× – 148.2× false
Read-only SSBO (StorageBuffer, 4 NonWritable members) 12,243 / 11,965 ns 541.7 / 168.9 ns 22.6× – 70.8× false
Writable storage image runtime array 12,237 / 12,216 ns 353.6 / 151.5 ns 34.6× – 80.6× true

Verification

  • AI-assisted: yes
  • GPUs tested: none (host-only portable C++ tests on macOS arm64; no game files used)
  • clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror: spirv_stores_test PASS
  • clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror: spirv_stores_test PASS
  • clang++ -std=c++20 -O2 -Wall -Wextra -Werror: spirv_stores_test × 3 runs PASS
  • Hygiene: check_no_game_data.py PASS, tests/test_text_write_line_endings.py PASS (3/3)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5495147992 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1948). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T16:30:47Z. --- Item ID: `RT-SPIRV-STORES-PREAMBLE-AND-FLAT-PERF` (claimed in #1608 comment #7168043831). ### Summary Optimises `leonida::spirv_declares_writable_storage()` (`runtime/src/spirv_stores.h`) by stopping at the SPIR-V preamble boundary (`OpFunction`) and replacing seven per-call hash containers with compact flat vectors and early exits: - **Stop at `OpFunction` (opcode 54)**: - In SPIR-V's logical module layout, all annotations (`OpDecorate`, `OpMemberDecorate`, `OpGroupDecorate`, `OpGroupMemberDecorate`), types, and global `OpVariable`s (`UniformConstant`, `Uniform`, `StorageBuffer`) precede the first `OpFunction`. Previously, `spirv_declares_writable_storage()` walked the entire function body (95–99% of every translated RDNA2 shader) and also collected local `Function`-storage variables. - Also hardened the word-count check against addition wrap (`count > words.size() - at`). - **Filter `OpVariable` and exit early before sorting**: - Only records `UniformConstant`, `Uniform`, and `StorageBuffer` variables, and exits immediately when no storage-image / storage-buffer / `BufferBlock` candidate exists (e.g., ordinary sampled-image + UBO shaders). - **Replace 7 `std::unordered_*` tables with flat sorted `std::vector`s**: - Eliminates per-call hash-bucket and per-node allocations, resolves `OpGroupDecorate` / `OpGroupMemberDecorate` once before checking variables, and checks full `[0, total)` `NonWritable` member coverage in O(1) after binary-searching the deduplicated sorted `(struct_id, member)` slice (`(hi - lo) >= total && lo[total - 1].second == total - 1`). - **`runtime/test/spirv_stores_test.cpp`**: - Added regression coverage for shaders with `OpFunction` bodies and local `Function` `OpVariable`s (both read-only and writable), `Uniform` `BufferBlock` via `OpGroupDecorate`, and duplicate `OpMemberDecorate` entries on member 0 not masking a writable member 1. ### A/B Benchmark (`-O2`, 50k-word shaders with 10,000-instruction function bodies, 5,000 iters, 2 runs) | Shader kind | Base (`main`) | Head (this PR) | Speedup | Result | |---|---:|---:|---:|:---:| | Sampled textures + UBO (`Uniform` `Block`) | 17,682 / 15,802 ns | **257.2 / 106.6 ns** | **68.8× – 148.2×** | `false` | | Read-only SSBO (`StorageBuffer`, 4 `NonWritable` members) | 12,243 / 11,965 ns | **541.7 / 168.9 ns** | **22.6× – 70.8×** | `false` | | Writable storage image runtime array | 12,237 / 12,216 ns | **353.6 / 151.5 ns** | **34.6× – 80.6×** | `true` | ### Verification - AI-assisted: yes - GPUs tested: none (host-only portable C++ tests on macOS arm64; no game files used) - `clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror`: `spirv_stores_test` PASS - `clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror`: `spirv_stores_test` PASS - `clang++ -std=c++20 -O2 -Wall -Wextra -Werror`: `spirv_stores_test` × 3 runs PASS - Hygiene: `check_no_game_data.py` PASS, `tests/test_text_write_line_endings.py` PASS (3/3)
copilot-pull-request-reviewer[bot] commented 2026-11-25 16:41:58 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T16:41:58Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6061373894 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1948#pullrequestreview-6061373894). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T16:41:58Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
zmasi commented 2026-11-28 21:24:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T21:24:59Z.


#1948 | head 039f8934d7 (merged cleanly with main 1bc055e6c2) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(spirv_stores|d3d12_stores_atomics)$" --output-on-failure (LEONIDA_VK_VALIDATION=1) + 5,000-iteration 50k-word SPIR-V A/B benchmark | baseline on main: 18,071.8 ns (sampled image + UBO), 18,101.1 ns (read-only SSBO), and 18,135.7 ns (writable storage image array) per call on MSVC RelWithDebInfo | green on PR+main: 2/2 CTests pass (spirv_stores including OpFunction-body skip, OpGroupDecorate BufferBlock, and duplicate OpMemberDecorate checks, plus d3d12_stores_atomics across 2 devices on RTX 3090 with 0 Khronos validation errors), cutting 50k-word scan time to 91.2 ns (198.2x faster), 250.5 ns (72.3x faster), and 187.5 ns (96.7x faster); check_no_game_data.py 0 violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7246002974 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1948#issuecomment-7246002974). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T21:24:59Z. --- #1948 | head 039f8934d7de9aef7e8aaa0d184a4afde1653b14 (merged cleanly with main 1bc055e6c2dedaa0de23ec79d8c4007c1dff1fb3) | `ctest --test-dir runtime/build -C RelWithDebInfo -R "^(spirv_stores|d3d12_stores_atomics)$" --output-on-failure` (`LEONIDA_VK_VALIDATION=1`) + 5,000-iteration 50k-word SPIR-V A/B benchmark | baseline on main: `18,071.8 ns` (sampled image + UBO), `18,101.1 ns` (read-only SSBO), and `18,135.7 ns` (writable storage image array) per call on MSVC `RelWithDebInfo` | green on PR+main: 2/2 CTests pass (`spirv_stores` including `OpFunction`-body skip, `OpGroupDecorate` `BufferBlock`, and duplicate `OpMemberDecorate` checks, plus `d3d12_stores_atomics` across 2 devices on RTX 3090 with `0` Khronos validation errors), cutting 50k-word scan time to `91.2 ns` (**198.2x faster**), `250.5 ns` (**72.3x faster**), and `187.5 ns` (**96.7x faster**); `check_no_game_data.py` `0` violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes
OreoFR commented 2026-11-28 21:56:17 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T21:56:17Z.


Verified by OreoFR

  • Base: main c1ecf04ed + PR head 039f8934d; green run in a merge package of 15 PRs, no new ctest failure vs main
  • Test: ctest -R ^spirv_stores$
  • Red on main: OK
  • Green with the PR: OK
  • Verdict: NO RED (optimisation (stop at OpFunction, sorted tables); the added test also passes on main)
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7246380705 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1948#issuecomment-7246380705). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-28T21:56:17Z. --- **Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `039f8934d`; green run in a merge package of 15 PRs, no new ctest failure vs main - Test: `ctest -R ^spirv_stores$` - Red on main: OK - Green with the PR: OK - Verdict: NO RED (optimisation (stop at OpFunction, sorted tables); the added test also passes on main) - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
aslan2142 commented 2026-12-01 16:39:54 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:39:54Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7317991429 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1948#issuecomment-7317991429). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:39:54Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

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
ProjectLeonida/mandy!1948
No description provided.