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

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

@⁠IlyassRS3 on GitHub · 2026-11-25 17:15 UTC · original
IlyassRS3:perf/rt-rgba4-fragment-flat-scan-and-tests → main · +450 −65 · 3 files · 1 commits · open

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.
**@⁠IlyassRS3** on GitHub · 2026-11-25 17:15 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1976) `IlyassRS3:perf/rt-rgba4-fragment-flat-scan-and-tests` → `main` · +450 −65 · 3 files · 1 commits · open 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:13:23 +00:00
Author
Owner

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

Merge

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