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

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

📋 Pull Request Information

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

Base: main ← Head: perf/rt-rgba4-fragment-flat-scan-and-tests


📝 Commits (1)

  • 68903a6 perf(rgba4_fragment): flat preamble/body scan, shared scalar constants, and portable CPU test

📊 Changes

3 files changed (+450 additions, -65 deletions)

View changed files

📝 runtime/CMakeLists.txt (+3 -0)
📝 runtime/src/rgba4_fragment.h (+219 -65)
➕ runtime/test/rgba4_fragment_test.cpp (+228 -0)

📄 Description

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.

🔄 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/1976 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/rt-rgba4-fragment-flat-scan-and-tests` --- ### 📝 Commits (1) - [`68903a6`](https://github.com/ubiquitarianism/leonida/commit/68903a671f822db6446c50f852898b843c7d3c17) perf(rgba4_fragment): flat preamble/body scan, shared scalar constants, and portable CPU test ### 📊 Changes **3 files changed** (+450 additions, -65 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+3 -0) 📝 `runtime/src/rgba4_fragment.h` (+219 -65) ➕ `runtime/test/rgba4_fragment_test.cpp` (+228 -0) </details> ### 📄 Description 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. --- <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#1983
No description provided.