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

Merged
leonida/mandy
quietfold merged 1 commit from fix/rt-dual-source-fragment-perf-and-dup-loc into main 2026-11-28 17:56:50 +00:00

Pull request successfully merged and closed

quietfold commented 2026-11-25 16:48:39 +00:00

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.
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.
copilot-pull-request-reviewer[bot] reviewed 2026-11-25 16:48:46 +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 12:09:18 +00:00

Verified on Windows 11 (10.0.26200.9974) with MSVC 19.44.35228 (x64, /std:c++20 /EHsc /W4 /permissive- /utf-8 /O2) against main d48a957bb + 0120ea194c (claim #2570#issuecomment-7238954420):

  • Line-by-line diff audit: Clean (runtime/src/dual_source_fragment.h, runtime/test/dual_source_fragment_test.cpp); stops the SPIR-V preamble scan at the first OpFunction, replaces RB-tree maps/sets with sorted flat vectors, removes dead output_pointers tracking, and refuses modules with multiple Output variables at Location 1 or duplicate Location decorations on an Output variable without mutating words. check_no_game_data.py clean.
  • Red (d48a957bb + PR test): dual_source_fragment_test.exe fails (rc=1, FAIL two Output variables at Location 1 refused unchanged and FAIL duplicate Location decoration on Output refused unchanged).
  • Green (d48a957bb + 0120ea194c merge): Auto-merges cleanly (rc=0), compiles with 0 warnings under /W4 /permissive-, and passes all 19 checks (all passed, rc=0).

AI-assisted.

Verified on Windows 11 (`10.0.26200.9974`) with MSVC `19.44.35228` (`x64`, `/std:c++20 /EHsc /W4 /permissive- /utf-8 /O2`) against `main` `d48a957bb` + `0120ea194c` (claim `#2570#issuecomment-7238954420`): - **Line-by-line diff audit**: Clean (`runtime/src/dual_source_fragment.h`, `runtime/test/dual_source_fragment_test.cpp`); stops the SPIR-V preamble scan at the first `OpFunction`, replaces RB-tree maps/sets with sorted flat vectors, removes dead `output_pointers` tracking, and refuses modules with multiple `Output` variables at `Location 1` or duplicate `Location` decorations on an `Output` variable without mutating `words`. `check_no_game_data.py` clean. - **Red (`d48a957bb` + PR test)**: `dual_source_fragment_test.exe` fails (`rc=1`, `FAIL two Output variables at Location 1 refused unchanged` and `FAIL duplicate Location decoration on Output refused unchanged`). - **Green (`d48a957bb` + `0120ea194c` merge)**: Auto-merges cleanly (`rc=0`), compiles with `0` warnings under `/W4 /permissive-`, and passes all 19 checks (`all passed`, `rc=0`). AI-assisted.
MoonEyes commented 2026-11-28 17:56:47 +00:00

Red/green: On main dual_source_fragment_test fails 2 checks (two Location 1 outputs and a duplicate Location decoration are not refused), and with the PR ctest -R "^dual_source_fragment$" passes.
Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.

Red/green: On main dual_source_fragment_test fails 2 checks (two Location 1 outputs and a duplicate Location decoration are not refused), and with the PR ctest -R "^dual_source_fragment$" passes. Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.
OreoFR commented 2026-11-28 21:56:49 +00:00

Verified by OreoFR

  • Base: main c1ecf04ed + PR head 0120ea194; green run in a merge package of 16 PRs, no new ctest failure vs main
  • Test: ctest -R ^dual_source_fragment$
  • Red on main: 2 failures (two outputs at Location 1 and a duplicate Location not refused)
  • Green with the PR: OK
  • Verdict: READY
  • 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 `0120ea194`; green run in a merge package of 16 PRs, no new ctest failure vs main - Test: `ctest -R ^dual_source_fragment$` - Red on main: 2 failures (two outputs at Location 1 and a duplicate Location not refused) - Green with the PR: OK - Verdict: READY - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
Sign in to join this conversation.
No description provided.