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

Open
github-bot wants to merge 1 commit from github-bot/gh-1948 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 16:30 UTC · original
IlyassRS3:perf/rt-spirv-stores-preamble-flat → main · +157 −48 · 2 files · 1 commits · open

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)
**@⁠IlyassRS3** on GitHub · 2026-11-25 16:30 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1948) `IlyassRS3:perf/rt-spirv-stores-preamble-flat` → `main` · +157 −48 · 2 files · 1 commits · open 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)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:12:22 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 16:41 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 16:41 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1948#pullrequestreview-6061373894) · **reviewed** 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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1948/head:github-bot/gh-1948'
git switch 'github-bot/gh-1948'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1948'
git switch 'github-bot/gh-1948'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1948'
git switch 'github-bot/gh-1948'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1948'
git switch 'main'
git merge --squash 'github-bot/gh-1948'
git switch 'main'
git merge --ff-only 'github-bot/gh-1948'
git switch 'main'
git merge 'github-bot/gh-1948'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.