perf(rgba4_fragment): flat preamble/body scan eliminating per-instruction vectors & 7 RB-trees, plus CPU unit test #1976

Closed
IlyassRS3 wants to merge 1 commit from upstream-pr/1976 into main
IlyassRS3 commented 2026-11-25 17:15:50 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T17:15:50Z.


Claim: RT-RGBA4-FRAGMENT-FLAT-SCAN-AND-TESTS (claim comment #7169189780 on #1608).

Optimizations & Fixes (runtime/src/rgba4_fragment.h)

  1. Eliminate per-instruction std::vector<uint32_t> allocations and 7 std::map RB-trees:
    • Previously quantize() allocated a separate std::vector<uint32_t> for every instruction in the entire fragment shader (even when no masked output existed or no store matched) and populated 7 std::map trees.
    • Now Pass 1 performs a flat preamble scan up to the first OpFunction using compact sorted vectors, returning true in O(preamble) with zero body scan when no masked color output exists.
    • Pass 2 validates body framing, tracks OpAccessChain/OpInBoundsAccessChain/OpPtrAccessChain output aliases, and records OpStore patch offsets without copying or mutating words unless at least one store is rewritten.
    • Single-pass emission bulk-copies unchanged slices into one pre-reserved output buffer while preserving any extra OpStore memory operands.
  2. Share scalar OpConstants across scalar and vector output types:
    • Previously components() emitted a fresh set of 4 scalar OpConstants (0.f, 1.f, 15.f, 1/15.f) for every distinct output type (e.g. both scalar float via access chain and vec4). Now emits the 4 scalar constants once and reuses them in all OpConstantComposite declarations.
  3. Refuse missing OpMemoryModel when GLSL.std.450 must be injected: prevents emitting a dangling import_id if a malformed module lacks OpMemoryModel.
  4. Add portable CPU-only CTest rgba4_fragment (runtime/test/rgba4_fragment_test.cpp): previously no standalone CPU unit test covered rgba4_fragment.h.

Verification

  • AI-assisted: yes.
  • GPUs tested: none (host is macOS arm64; no retail files).
  • Added runtime/test/rgba4_fragment_test.cpp covering zero mask, unused-location fast path, GLSL.std.450 injection before OpMemoryModel, extra OpStore operand preservation, existing import reuse + shared scalar constants across scalar access-chain and vec4 stores, and refusal cases (OpCopyMemory, non-float output, no matching store, bad magic, short header, truncated preamble/body, zero-word instruction, missing OpMemoryModel).
  • Ran rgba4_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 vs main (best of 5 pre-allocated batches, Apple Silicon):
small_rw_200w      (   202 words): base=    4.37 us  new=  0.77 us  speedup=   5.65x
med_rw_12kw        ( 12052 words): base=   68.08 us  new=  4.62 us  speedup=  14.74x
large_rw_50kw      ( 50052 words): base=  223.11 us  new= 13.60 us  speedup=  16.41x
large_miss_50kw    ( 50052 words): base=  143.09 us  new=  0.11 us  speedup=1268.16x
  • Ran python3 -I scripts/check_no_game_data.py on all 3 changed files and python3 -I tests/test_text_write_line_endings.py: all passed.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5495842202 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1976). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T17:15:50Z. --- Claim: `RT-RGBA4-FRAGMENT-FLAT-SCAN-AND-TESTS` (claim comment `#7169189780` on #1608). ### Optimizations & Fixes (`runtime/src/rgba4_fragment.h`) 1. **Eliminate per-instruction `std::vector<uint32_t>` allocations and 7 `std::map` RB-trees**: - Previously `quantize()` allocated a separate `std::vector<uint32_t>` for every instruction in the entire fragment shader (even when no masked output existed or no store matched) and populated 7 `std::map` trees. - Now Pass 1 performs a flat preamble scan up to the first `OpFunction` using compact sorted vectors, returning `true` in O(preamble) with zero body scan when no masked color output exists. - Pass 2 validates body framing, tracks `OpAccessChain`/`OpInBoundsAccessChain`/`OpPtrAccessChain` output aliases, and records `OpStore` patch offsets without copying or mutating `words` unless at least one store is rewritten. - Single-pass emission bulk-copies unchanged slices into one pre-reserved output buffer while preserving any extra `OpStore` memory operands. 2. **Share scalar `OpConstant`s across scalar and vector output types**: - Previously `components()` emitted a fresh set of 4 scalar `OpConstant`s (`0.f, 1.f, 15.f, 1/15.f`) for every distinct output type (e.g. both scalar `float` via access chain and `vec4`). Now emits the 4 scalar constants once and reuses them in all `OpConstantComposite` declarations. 3. **Refuse missing `OpMemoryModel` when `GLSL.std.450` must be injected**: prevents emitting a dangling `import_id` if a malformed module lacks `OpMemoryModel`. 4. **Add portable CPU-only CTest `rgba4_fragment`** (`runtime/test/rgba4_fragment_test.cpp`): previously no standalone CPU unit test covered `rgba4_fragment.h`. ### Verification - **AI-assisted**: yes. - **GPUs tested**: none (host is macOS arm64; no retail files). - Added `runtime/test/rgba4_fragment_test.cpp` covering zero mask, unused-location fast path, `GLSL.std.450` injection before `OpMemoryModel`, extra `OpStore` operand preservation, existing import reuse + shared scalar constants across scalar access-chain and `vec4` stores, and refusal cases (`OpCopyMemory`, non-float output, no matching store, bad magic, short header, truncated preamble/body, zero-word instruction, missing `OpMemoryModel`). - Ran `rgba4_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` vs `main` (best of 5 pre-allocated batches, Apple Silicon): ```text small_rw_200w ( 202 words): base= 4.37 us new= 0.77 us speedup= 5.65x med_rw_12kw ( 12052 words): base= 68.08 us new= 4.62 us speedup= 14.74x large_rw_50kw ( 50052 words): base= 223.11 us new= 13.60 us speedup= 16.41x large_miss_50kw ( 50052 words): base= 143.09 us new= 0.11 us speedup=1268.16x ``` - Ran `python3 -I scripts/check_no_game_data.py` on all 3 changed files and `python3 -I tests/test_text_write_line_endings.py`: all passed.
copilot-pull-request-reviewer[bot] commented 2026-11-25 17:16:01 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T17:16:01Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6061891025 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1976#pullrequestreview-6061891025). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T17:16:01Z. **Review:** COMMENTED --- 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:50:48 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T12:50:48Z.


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 + 68903a671f (claim #2570#issuecomment-7239425776):

  • Line-by-line diff audit: Clean (runtime/CMakeLists.txt, runtime/src/rgba4_fragment.h, runtime/test/rgba4_fragment_test.cpp); replaces per-instruction std::vector and 7 std::map RB-trees in rgba4::quantize with a 2-pass flat preamble/body scan, emits shared scalar OpConstants once across scalar and vector output stores, preserves extra OpStore memory operands, and refuses modules lacking OpMemoryModel when GLSL.std.450 must be injected without mutating words. check_no_game_data.py clean.
  • Red (d48a957bb + PR test): rgba4_fragment_test.exe fails (rc=1, line 172: CHECK failed: s.consts_before_function && s.scalar_consts == 4 && s.composite_consts == 4 because main emits duplicate scalar OpConstant sets across scalar + vec4 outputs and accepts missing OpMemoryModel).
  • Green (d48a957bb + 68903a671f merge): Auto-merges cleanly (rc=0), compiles with 0 warnings under /W4 /permissive-, and passes (rgba4_fragment_test: all checks passed, rc=0).

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7239507518 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1976#issuecomment-7239507518). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T12:50:48Z. --- 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` + `68903a671f` (claim `#2570#issuecomment-7239425776`): - **Line-by-line diff audit**: Clean (`runtime/CMakeLists.txt`, `runtime/src/rgba4_fragment.h`, `runtime/test/rgba4_fragment_test.cpp`); replaces per-instruction `std::vector` and 7 `std::map` RB-trees in `rgba4::quantize` with a 2-pass flat preamble/body scan, emits shared scalar `OpConstant`s once across scalar and vector output stores, preserves extra `OpStore` memory operands, and refuses modules lacking `OpMemoryModel` when `GLSL.std.450` must be injected without mutating `words`. `check_no_game_data.py` clean. - **Red (`d48a957bb` + PR test)**: `rgba4_fragment_test.exe` fails (`rc=1`, `line 172: CHECK failed: s.consts_before_function && s.scalar_consts == 4 && s.composite_consts == 4` because `main` emits duplicate scalar `OpConstant` sets across scalar + `vec4` outputs and accepts missing `OpMemoryModel`). - **Green (`d48a957bb` + `68903a671f` merge)**: Auto-merges cleanly (`rc=0`), compiles with `0` warnings under `/W4 /permissive-`, and passes (`rgba4_fragment_test: all checks passed`, `rc=0`). AI-assisted.
OreoFR commented 2026-11-29 02:40:23 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-29T02:40:23Z.


Verified by OreoFR

  • Base: main aa918e5d9 + PR head 68903a671
  • Test: ctest -R ^rgba4_fragment$
  • Red on main: fails at line 172: a scalar and a vec4 output each get their own 4 scalar constants instead of sharing one set
  • Green with the PR: passes; package builds, full ctest shows no new failure
  • Verdict: READY
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

Note: shares runtime/CMakeLists.txt with the open #2566; a rebase may be needed if #2566 lands first.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7249575060 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1976#issuecomment-7249575060). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-29T02:40:23Z. --- **Verified by OreoFR** - Base: main `aa918e5d9` + PR head `68903a671` - Test: `ctest -R ^rgba4_fragment$` - Red on main: fails at line 172: a scalar and a vec4 output each get their own 4 scalar constants instead of sharing one set - Green with the PR: passes; package builds, full ctest shows no new failure - Verdict: READY - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 Note: shares `runtime/CMakeLists.txt` with the open #2566; a rebase may be needed if #2566 lands first. AI-assisted (Claude Code, Claude Opus 5.5).
aslan2142 commented 2026-12-01 16:40:30 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:40:30Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7318011012 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1976#issuecomment-7318011012). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:40:30Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!1976
No description provided.