Skip to content

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

Open
IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:perf/rt-rgba4-fragment-flat-scan-and-tests
Open

IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:perf/rt-rgba4-fragment-flat-scan-and-tests

Conversation

@IlyassRS3

Copy link
Copy Markdown
Contributor

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.

Copilot AI balanced review requested due to automatic review settings November 25, 2026 17:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

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.

@OreoFR

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants