[PR #829] [CLOSED] rdna2spirv: branch-free table-driven %fmt_encode_fn and %fmt_size_tab reuse #865

Closed
opened 2026-11-26 20:19:18 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/829
Author: @SweatierKey
Created: 11/24/2026
Status: ❌ Closed

Base: main ← Head: feat/fmt-encode-table


📝 Commits (1)

  • ff1dd9c rdna2spirv: branch-free table-driven %fmt_encode_fn and %fmt_size_tab reuse

📊 Changes

3 files changed (+460 additions, -47 deletions)

View changed files

📝 docs/design.md (+1 -1)
➕ tests/semantic/test_format_encode_table.py (+230 -0)
📝 tools/rdna2spirv.py (+229 -46)

📄 Description

Claimed in the coordination issue as: SHADER-FMT-ENCODE-TABLE (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7134102086)

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.

🔄 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/829 **Author:** [@SweatierKey](https://github.com/SweatierKey) **Created:** 11/24/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feat/fmt-encode-table` --- ### 📝 Commits (1) - [`ff1dd9c`](https://github.com/ubiquitarianism/leonida/commit/ff1dd9caef48337ca4956bc569c51f2a9b77a184) rdna2spirv: branch-free table-driven %fmt_encode_fn and %fmt_size_tab reuse ### 📊 Changes **3 files changed** (+460 additions, -47 deletions) <details> <summary>View changed files</summary> 📝 `docs/design.md` (+1 -1) ➕ `tests/semantic/test_format_encode_table.py` (+230 -0) 📝 `tools/rdna2spirv.py` (+229 -46) </details> ### 📄 Description Claimed in the coordination issue as: SHADER-FMT-ENCODE-TABLE (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7134102086) ### 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:19:18 +01:00
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#865
No description provided.