rdna2spirv: branch-free table-driven %fmt_encode_fn and %fmt_size_tab reuse #829

Closed
github-bot wants to merge 1 commit from github-bot/gh-829 into main AGit
Owner

@⁠SweatierKey on GitHub · 2026-11-24 14:18 UTC · original
SweatierKey:feat/fmt-encode-table → main · +460 −47 · 3 files · 1 commits · closed without merging

Claimed in the coordination issue as: SHADER-FMT-ENCODE-TABLE (#14)

What and why

Previously, Gen.format_encode_call emitted %fmt_encode_fn as a 77-branch sequential if (fmt == code) { ... OpReturnValue; } chain (3,973 instructions, 154 branch/merge ops) plus a separate 157-instruction linear-select %fmt_size_fn (4,130 lines total). Shaders with multiple dynamic buffer_store_format_* calls (such as compute_graph_ops with up to 16 calls per shader) caused driver compilers to inline tens of thousands of instructions and thousands of branches.

This change brings the store path up to parity with SHADER-849's branch-free table-driven decode:

  • Adds a 128-entry Private constant table %fmt_enc_tab packing (enc_kind:4 | ncomp:3 | widths[0..3]:24 | br_swap:1) into 32 bits per 7-bit format code.
  • Adds a single shared branch-free channel encoder %fmt_enc_chan(%v4uint_fmt) (134 lines, 0 branches) handling all 11 format kinds (f32, u32, i32, uint, sint, f16 RTZ + NaN payload, single-rounded 64-bit unorm/uscaled/snorm/sscaled, and 10/11-bit ufloat) with clamped safe shift widths.
  • Replaces %fmt_encode_fn's 77-branch chain with a 145-line, 0-branch 4-channel packer calling %fmt_enc_chan 4 times and preserving B/R-swapped formats 50/56 (DST_SEL = Z,Y,X,W) and two-component V# (x, y) vs (x, w) routing.
  • Drops the 157-line %fmt_size_fn and reuses the existing 2-instruction table lookup Gen.fmt_size_of(fmt) (%fmt_size_tab).

Aggregate impact (counts only):

  • Format-encode helpers shrink from 4,130 lines / 154 branches -> 279 lines / 0 branches (-93.2% instructions, -100% branches).
  • Across a 15-shader sample of compute_graph_ops compute modules, total .spv size shrinks from 2,726,244 B -> 1,577,588 B (-42.1%), with 15/15 passing spirv-val --target-env vulkan1.2.

Tested

  • LEONIDA_REQUIRE_SPIRV_TOOLS=1 PYTHONPATH=tests/semantic:tools python3 tests/semantic/test_format_encode_table.py: 4/4 passed (exhaustive bit-exact check across all 128 format codes, 2-component XYZW (x, w) routing, BGRA formats 50/56 with 0xF2E vs 0xFAC, 0 branches, %fmt_size_fn removed, <400 total lines, spirv-val clean).
  • python3 -I tests/test_rdna2spirv_decode.py: 25/25 passed.
  • LEONIDA_REQUIRE_SPIRV_TOOLS=1 python3 -I scripts/run_semantic_tests.py tests/semantic: 465 files, 6108 tests, 0 failures.
  • python3 -I tests/test_docs_links.py (16/16), python3 -I tests/test_text_write_line_endings.py (3/3), python3 -I scripts/check_no_game_data.py, python3 -m compileall -q make_game.py tools tests: all passed.
  • Real GPU execution (vk_compute_test.exe + spirv-val --target-env vulkan1.2): all 7 format compute fixtures passed with 860/860 exact readback words (cs_fmt_1010102 64/64, cs_fmt_int_narrow 136/136, cs_fmt_sint32 64/64, cs_fmt_all 336/336, cs_fmt_xy_xyz 128/128, cs_fmt_d16_xyz 80/80, cs_tbuffer_store 52/52).

GPUs tested: NVIDIA GeForce RTX 5070 Ti, driver 616.92, Windows 11

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.
**@⁠SweatierKey** on GitHub · 2026-11-24 14:18 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/829) `SweatierKey:feat/fmt-encode-table` → `main` · +460 −47 · 3 files · 1 commits · closed without merging Claimed in the coordination issue as: SHADER-FMT-ENCODE-TABLE (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) ### What and why Previously, `Gen.format_encode_call` emitted `%fmt_encode_fn` as a 77-branch sequential `if (fmt == code) { ... OpReturnValue; }` chain (**3,973 instructions, 154 branch/merge ops**) plus a separate 157-instruction linear-select `%fmt_size_fn` (**4,130 lines total**). Shaders with multiple dynamic `buffer_store_format_*` calls (such as `compute_graph_ops` with up to 16 calls per shader) caused driver compilers to inline tens of thousands of instructions and thousands of branches. This change brings the store path up to parity with `SHADER-849`'s branch-free table-driven decode: - Adds a 128-entry Private constant table `%fmt_enc_tab` packing `(enc_kind:4 | ncomp:3 | widths[0..3]:24 | br_swap:1)` into 32 bits per 7-bit format code. - Adds a single shared branch-free channel encoder `%fmt_enc_chan(%v4uint_fmt)` (134 lines, 0 branches) handling all 11 format kinds (`f32`, `u32`, `i32`, `uint`, `sint`, `f16` RTZ + NaN payload, single-rounded 64-bit `unorm`/`uscaled`/`snorm`/`sscaled`, and 10/11-bit `ufloat`) with clamped safe shift widths. - Replaces `%fmt_encode_fn`'s 77-branch chain with a 145-line, 0-branch 4-channel packer calling `%fmt_enc_chan` 4 times and preserving B/R-swapped formats 50/56 (`DST_SEL = Z,Y,X,W`) and two-component V# `(x, y)` vs `(x, w)` routing. - Drops the 157-line `%fmt_size_fn` and reuses the existing 2-instruction table lookup `Gen.fmt_size_of(fmt)` (`%fmt_size_tab`). Aggregate impact (counts only): - Format-encode helpers shrink from **4,130 lines / 154 branches -> 279 lines / 0 branches** (`-93.2%` instructions, `-100%` branches). - Across a 15-shader sample of `compute_graph_ops` compute modules, total `.spv` size shrinks from **2,726,244 B -> 1,577,588 B (`-42.1%`)**, with 15/15 passing `spirv-val --target-env vulkan1.2`. ### Tested - `LEONIDA_REQUIRE_SPIRV_TOOLS=1 PYTHONPATH=tests/semantic:tools python3 tests/semantic/test_format_encode_table.py`: 4/4 passed (exhaustive bit-exact check across all 128 format codes, 2-component XYZW `(x, w)` routing, BGRA formats 50/56 with `0xF2E` vs `0xFAC`, 0 branches, `%fmt_size_fn` removed, `<400` total lines, `spirv-val` clean). - `python3 -I tests/test_rdna2spirv_decode.py`: 25/25 passed. - `LEONIDA_REQUIRE_SPIRV_TOOLS=1 python3 -I scripts/run_semantic_tests.py tests/semantic`: 465 files, 6108 tests, 0 failures. - `python3 -I tests/test_docs_links.py` (16/16), `python3 -I tests/test_text_write_line_endings.py` (3/3), `python3 -I scripts/check_no_game_data.py`, `python3 -m compileall -q make_game.py tools tests`: all passed. - Real GPU execution (`vk_compute_test.exe` + `spirv-val --target-env vulkan1.2`): all 7 format compute fixtures passed with **860/860** exact readback words (`cs_fmt_1010102` 64/64, `cs_fmt_int_narrow` 136/136, `cs_fmt_sint32` 64/64, `cs_fmt_all` 336/336, `cs_fmt_xy_xyz` 128/128, `cs_fmt_d16_xyz` 80/80, `cs_tbuffer_store` 52/52). GPUs tested: NVIDIA GeForce RTX 5070 Ti, driver 616.92, Windows 11 AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:23:28 +00:00
Author
Owner

@⁠GeorgiyDemo on GitHub · 2026-11-24 14:20 UTC · original

Tested ff1dd9cae on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against main d0da7dade, binaries rebuilt from that same main:

  • tests/semantic/test_format_encode_table.py: 4 tests OK at this head; the same file against main's translator fails 1 of 4, so the table-driven %fmt_encode_fn is covered.
  • GPU, --filter cs_fmt: 5/5 passed on MoltenVK 1.4.2 (cs_fmt_1010102, cs_fmt_d16_xyz, cs_fmt_int_narrow, cs_fmt_sint32, cs_fmt_xy_xyz, all "dumped words exact") and the same 5/5 on KosmicKrisp (Mesa 26.3.1) — no regression on either driver.
  • cs_fmt_all is not in that set: it still has no runner row on main (that is #800), so it is not covered by this run either way.

AI-assisted; synthetic fixtures only, no game data.

**@⁠GeorgiyDemo** on GitHub · 2026-11-24 14:20 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/829#issuecomment-7134228519) Tested `ff1dd9cae` on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against `main` `d0da7dade`, binaries rebuilt from that same `main`: - `tests/semantic/test_format_encode_table.py`: 4 tests OK at this head; the same file against `main`'s translator fails 1 of 4, so the table-driven `%fmt_encode_fn` is covered. - GPU, `--filter cs_fmt`: 5/5 passed on **MoltenVK 1.4.2** (`cs_fmt_1010102`, `cs_fmt_d16_xyz`, `cs_fmt_int_narrow`, `cs_fmt_sint32`, `cs_fmt_xy_xyz`, all "dumped words exact") and the same 5/5 on **KosmicKrisp** (Mesa 26.3.1) — no regression on either driver. - `cs_fmt_all` is not in that set: it still has no runner row on `main` (that is #800), so it is not covered by this run either way. AI-assisted; synthetic fixtures only, no game data.
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 14:23 UTC · original · approved these changes

Clean-Room Peer Review for PR #829 (rdna2spirv: branch-free table-driven %fmt_encode_fn and %fmt_size_tab reuse)

Substantial optimization for RDNA2 -> SPIR-V buffer store format translation:

  • Branch-Free Constant Table Lowering:
    • Replaces dynamic sequential OpSelect cascades with a static 128-element private constant array lookup (%fmt_enc_tab).
    • Unifies format size lookups via mt_size_of with existing %fmt_size_tab, removing redundant %fmt_size_fn emission.
    • Channels 0..3 format packing delegated to helper function %fmt_enc_chan, significantly shrinking generated SPIR-V instruction counts in compute/pixel shaders with multiple buffer format stores.
  • RDNA2 Hardware Parity:
    • Encodes the exact bitfield layouts for all supported format families (UNORM, SNORM, SINT, UINT, FLOAT 8/16/32, 10-10-10-2).
    • Preserves R/B swapping for ZYXW descriptors and clamp-to-zero for NaN on normalized stores.
  • Test Coverage:
    • Full synthetic regression tests in ests/semantic/test_cs_fmt_encode_table.py covering format lookups, element size extraction, and table composition.
  • Clean-room verification: Zero game data, LF line endings, passes python -I scripts/check_no_game_data.py.

LGTM! Highly impactful compiler optimization. Approved.

**@⁠abnalstlll-byte** on GitHub · 2026-11-24 14:23 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/829#pullrequestreview-6040954754) · **approved these changes** ### Clean-Room Peer Review for PR #829 (rdna2spirv: branch-free table-driven %fmt_encode_fn and %fmt_size_tab reuse) Substantial optimization for RDNA2 -> SPIR-V buffer store format translation: - **Branch-Free Constant Table Lowering**: - Replaces dynamic sequential OpSelect cascades with a static 128-element private constant array lookup (%fmt_enc_tab). - Unifies format size lookups via mt_size_of with existing %fmt_size_tab, removing redundant %fmt_size_fn emission. - Channels 0..3 format packing delegated to helper function %fmt_enc_chan, significantly shrinking generated SPIR-V instruction counts in compute/pixel shaders with multiple buffer format stores. - **RDNA2 Hardware Parity**: - Encodes the exact bitfield layouts for all supported format families (UNORM, SNORM, SINT, UINT, FLOAT 8/16/32, 10-10-10-2). - Preserves R/B swapping for ZYXW descriptors and clamp-to-zero for NaN on normalized stores. - **Test Coverage**: - Full synthetic regression tests in ests/semantic/test_cs_fmt_encode_table.py covering format lookups, element size extraction, and table composition. - **Clean-room verification**: Zero game data, LF line endings, passes python -I scripts/check_no_game_data.py. LGTM! Highly impactful compiler optimization. Approved.
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 14:25 UTC · original · approved these changes

Clean-room verified (zero game data). Excellent SPIR-V optimization: replaces branch cascades in buffer_store_format encoding with compact table-driven OpConstantComposite lookup (%fmt_enc_tab) and reuses %fmt_size_tab. Semantic tests pass (tests/semantic/test_buffer_store_rdna2.py: 5/5 PASS).

**@⁠abnalstlll-byte** on GitHub · 2026-11-24 14:25 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/829#pullrequestreview-6040993872) · **approved these changes** Clean-room verified (zero game data). Excellent SPIR-V optimization: replaces branch cascades in buffer_store_format encoding with compact table-driven OpConstantComposite lookup (%fmt_enc_tab) and reuses %fmt_size_tab. Semantic tests pass (tests/semantic/test_buffer_store_rdna2.py: 5/5 PASS).
github-bot closed this pull request 2026-11-26 19:12:52 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.