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

Closed
leonida/mandy
quietfold wants to merge 0 commits from perf/rt-spirv-stores-preamble-flat into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

quietfold commented 2026-11-25 16:30:47 +00:00

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)
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] reviewed 2026-11-25 16:41:58 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
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

#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

#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

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).

**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

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.

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.
Sign in to join this conversation.
No description provided.