runtime: refuse duplicate Location 1 outputs and avoid full-body RB-tree scan in dual_source_fragment #1961

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

@⁠IlyassRS3 on GitHub · 2026-11-25 16:48 UTC · original
IlyassRS3:fix/rt-dual-source-fragment-perf-and-dup-loc → main · +106 −28 · 2 files · 1 commits · open

Claim: RT-DUAL-SOURCE-FRAGMENT-PERF-AND-DUP-LOC (claim comment #7168523390 on #1608).

Correctness & performance fixes (runtime/src/dual_source_fragment.h)

  1. Refuse duplicate Location 1 outputs instead of silently overwriting second: previously if (loc->second == 1) second = id; silently remapped only the last Output variable at Location 1 and left any earlier one at Location 1. Now a second Output variable at Location 1 is refused cleanly with words unchanged.
  2. Refuse ambiguous duplicate Location decorations on an Output variable: previously location_at[id] / location_of[id] silently overwrote earlier OpDecorate %id Location entries, leaving stale decorations behind if patched. Now duplicate Location decorations on an Output are refused cleanly with words unchanged.
  3. Stop scanning at OpFunction (preamble-only): OpDecorate and global Output OpVariable declarations live before the first OpFunction (SPIR-V logical layout §2.4); skipping the function body avoids scanning tens of thousands of body words on large fragment shaders.
  4. Drop dead output_pointers set and 5 red-black trees: output_pointers was populated on every OpTypePointer and never read. Replacing the 5 std::map/std::set trees and the temporary std::vector insertion buffer with 3 small flat vectors and a stack std::array<uint32_t, 4> eliminates tree node heap allocations.

Verification

  • AI-assisted: yes.
  • GPUs tested: none (host is macOS arm64; no retail files; 0 CMakeLists.txt changes).
  • Expanded runtime/test/dual_source_fragment_test.cpp (19 checks: short header, zero-word instruction, idempotence on already-remapped module, two distinct Output variables at Location 1, duplicate Location decoration on the same Output, and preamble stop with verbatim function-body preservation).
  • Ran dual_source_fragment_test cleanly under:
    • clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror
    • clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror
    • clang++ -std=c++20 -O2 -Wall -Wextra -Werror
  • Bit-exact A/B benchmark at -O2 (best of 7 pre-allocated runs, Apple Silicon):
small_dual_235w    (   235 words): base=   1.38 us  new=  0.53 us  speedup=2.59x
med_dual_12kw      ( 12185 words): base=   7.48 us  new=  2.01 us  speedup=3.72x
large_dual_50kw    ( 50185 words): base=  17.92 us  new=  4.20 us  speedup=4.27x
large_miss_50kw    ( 50177 words): base=  13.30 us  new=  0.24 us  speedup=54.32x
  • Ran python3 -I scripts/check_no_game_data.py on both files and python3 -I tests/test_text_write_line_endings.py: all passed.
**@⁠IlyassRS3** on GitHub · 2026-11-25 16:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1961) `IlyassRS3:fix/rt-dual-source-fragment-perf-and-dup-loc` → `main` · +106 −28 · 2 files · 1 commits · open Claim: `RT-DUAL-SOURCE-FRAGMENT-PERF-AND-DUP-LOC` (claim comment `#7168523390` on #1608). ### Correctness & performance fixes (`runtime/src/dual_source_fragment.h`) 1. **Refuse duplicate `Location 1` outputs instead of silently overwriting `second`**: previously `if (loc->second == 1) second = id;` silently remapped only the last `Output` variable at Location 1 and left any earlier one at Location 1. Now a second `Output` variable at Location 1 is refused cleanly with `words` unchanged. 2. **Refuse ambiguous duplicate `Location` decorations on an `Output` variable**: previously `location_at[id]` / `location_of[id]` silently overwrote earlier `OpDecorate %id Location` entries, leaving stale decorations behind if patched. Now duplicate `Location` decorations on an `Output` are refused cleanly with `words` unchanged. 3. **Stop scanning at `OpFunction` (preamble-only)**: `OpDecorate` and global `Output` `OpVariable` declarations live before the first `OpFunction` (SPIR-V logical layout §2.4); skipping the function body avoids scanning tens of thousands of body words on large fragment shaders. 4. **Drop dead `output_pointers` set and 5 red-black trees**: `output_pointers` was populated on every `OpTypePointer` and never read. Replacing the 5 `std::map`/`std::set` trees and the temporary `std::vector` insertion buffer with 3 small flat vectors and a stack `std::array<uint32_t, 4>` eliminates tree node heap allocations. ### Verification - **AI-assisted**: yes. - **GPUs tested**: none (host is macOS arm64; no retail files; 0 `CMakeLists.txt` changes). - Expanded `runtime/test/dual_source_fragment_test.cpp` (19 checks: short header, zero-word instruction, idempotence on already-remapped module, two distinct `Output` variables at Location 1, duplicate `Location` decoration on the same `Output`, and preamble stop with verbatim function-body preservation). - Ran `dual_source_fragment_test` cleanly under: - `clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror` - `clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror` - `clang++ -std=c++20 -O2 -Wall -Wextra -Werror` - Bit-exact A/B benchmark at `-O2` (best of 7 pre-allocated runs, Apple Silicon): ```text small_dual_235w ( 235 words): base= 1.38 us new= 0.53 us speedup=2.59x med_dual_12kw ( 12185 words): base= 7.48 us new= 2.01 us speedup=3.72x large_dual_50kw ( 50185 words): base= 17.92 us new= 4.20 us speedup=4.27x large_miss_50kw ( 50177 words): base= 13.30 us new= 0.24 us speedup=54.32x ``` - Ran `python3 -I scripts/check_no_game_data.py` on both files and `python3 -I tests/test_text_write_line_endings.py`: all passed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:12:54 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 16:48 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:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1961#pullrequestreview-6061487914) · **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/1961/head:github-bot/gh-1961'
git switch 'github-bot/gh-1961'

Merge

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