[PR #1948] runtime: stop spirv_stores scan at OpFunction and use flat sorted tables #1955

Open
opened 2026-11-26 21:19:31 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1948
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: perf/rt-spirv-stores-preamble-flat


📝 Commits (1)

  • 039f893 runtime: stop spirv_stores scan at OpFunction and use flat sorted tables

📊 Changes

2 files changed (+157 additions, -48 deletions)

View changed files

📝 runtime/src/spirv_stores.h (+108 -48)
📝 runtime/test/spirv_stores_test.cpp (+49 -0)

📄 Description

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)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1948 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/rt-spirv-stores-preamble-flat` --- ### 📝 Commits (1) - [`039f893`](https://github.com/ubiquitarianism/leonida/commit/039f8934d7de9aef7e8aaa0d184a4afde1653b14) runtime: stop spirv_stores scan at OpFunction and use flat sorted tables ### 📊 Changes **2 files changed** (+157 additions, -48 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/spirv_stores.h` (+108 -48) 📝 `runtime/test/spirv_stores_test.cpp` (+49 -0) </details> ### 📄 Description 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#1955
No description provided.