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

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

📋 Pull Request Information

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

Base: main ← Head: fix/rt-dual-source-fragment-perf-and-dup-loc


📝 Commits (1)

  • 0120ea1 runtime: refuse duplicate Location 1 outputs and speed up dual_source_fragment preamble scan

📊 Changes

2 files changed (+106 additions, -28 deletions)

View changed files

📝 runtime/src/dual_source_fragment.h (+57 -28)
📝 runtime/test/dual_source_fragment_test.cpp (+49 -0)

📄 Description

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.

🔄 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/1961 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/rt-dual-source-fragment-perf-and-dup-loc` --- ### 📝 Commits (1) - [`0120ea1`](https://github.com/ubiquitarianism/leonida/commit/0120ea194c8e778504c4eeda0c8c71a9da943858) runtime: refuse duplicate Location 1 outputs and speed up dual_source_fragment preamble scan ### 📊 Changes **2 files changed** (+106 additions, -28 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/dual_source_fragment.h` (+57 -28) 📝 `runtime/test/dual_source_fragment_test.cpp` (+49 -0) </details> ### 📄 Description 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. --- <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#1967
No description provided.