rdna2spirv: demand-driven %fmt_dec_dyn for 1/2/3-component buffer_load_format #811

Closed
SweatierKey wants to merge 2 commits from upstream-pr/811 into main
SweatierKey commented 2026-11-24 14:06:08 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by SweatierKey on 2026-11-24T14:06:08Z.


Claimed in the coordination issue as: SHADER-FMT-DEC-DEMAND (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7133732630)

What and why

Previously, every dynamic-format buffer_load_format_{x,xy,xyz,xyzw} (and _d16_{x,xy,xyz,xyzw}) called format_decode_call, which invoked all four per-component functions %fmt_dec_c0..c3 even for 1-component buffer_load_format_x loads, then swizzled by DST_SEL and discarded 1–3 decoded components (75% dead work for _x, 50% for _xy, 25% for _xyz). Large compute shaders with thousands of buffer_load_format_x instructions emitted up to 13,124 %fmt_dec_c* calls per shader.

This change introduces a single shared branch-free dynamic-channel helper %fmt_dec_dyn(fmt, words, chan) (reusing Gen.fmt_dec_component(None, params) with branch-free chan % ncomp selection) used on demand by 1/2/3-component dynamic-format loads:

  • buffer_load_format_x / _d16_x emits 1 %fmt_dec_dyn call instead of 4 (-75%).
  • buffer_load_format_xy / _d16_xy emits 2 calls instead of 4 (-50%).
  • buffer_load_format_xyz / _d16_xyz emits 3 calls instead of 4 (-25%).
  • buffer_load_format_xyzw / _d16_xyzw continues using %fmt_dec_c0..c3 unchanged.
  • Shaders containing only <4-component loads omit %fmt_dec_c0..c3 entirely; shaders mixing _xyzw and <4-component loads share a single %fmt_norm_bits definition.
  • DST_SEL handling (0 -> 0, 1 -> dst1 even when OOB per SHADER-429, reserved 2/3 -> 0, 4..7 -> in_b ? dec : 0 with chan = sel & 3) is bit-exact with the 4-component path.

Aggregate impact across 15 cached large compute shaders (counts only):

  • Dynamic format decode calls drop from 35,200 -> 8,849 (-74.9%); SPIR-V lines drop from 7,017,898 -> 6,915,894 (-102,004 lines). All modules pass spirv-val --target-env vulkan1.2.

Tested

  • python3 -I tests/test_rdna2spirv_decode.py: 26/26 passed (including new test_format_decode_dyn_call_builds_and_caches_function).
  • LEONIDA_REQUIRE_SPIRV_TOOLS=1 PYTHONPATH=tests/semantic:tools python3 tests/semantic/test_format_decode_table.py: 7/7 passed (exhaustive check across all 128 format codes with rotated DST_SEL on _xyz, all 8 DST_SEL values in-bounds & OOB on _x, mixed _xyzw + _x deduplication of %fmt_norm_bits, branch-free verification, and exact call-count assertions).
  • LEONIDA_REQUIRE_SPIRV_TOOLS=1 python3 -I scripts/run_semantic_tests.py tests/semantic: 465 files, 6111 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 816/816 exact readback words (cs_buf_swizzle 8/8, cs_fmt_1010102 64/64, cs_fmt_int_narrow 136/136, cs_fmt_sint32 64/64 exercising mixed %fmt_dec_dyn + %fmt_dec_c*, cs_fmt_all 336/336, cs_fmt_xy_xyz 128/128 exercising 22 %fmt_dec_dyn calls, cs_fmt_d16_xyz 80/80 exercising 6 %fmt_dec_dyn calls).

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.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5473688573 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/811). Originally posted by [SweatierKey](https://github.com/SweatierKey) on 2026-11-24T14:06:08Z. --- Claimed in the coordination issue as: SHADER-FMT-DEC-DEMAND (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7133732630) ### What and why Previously, every dynamic-format `buffer_load_format_{x,xy,xyz,xyzw}` (and `_d16_{x,xy,xyz,xyzw}`) called `format_decode_call`, which invoked all four per-component functions `%fmt_dec_c0..c3` even for 1-component `buffer_load_format_x` loads, then swizzled by `DST_SEL` and discarded 1–3 decoded components (75% dead work for `_x`, 50% for `_xy`, 25% for `_xyz`). Large compute shaders with thousands of `buffer_load_format_x` instructions emitted up to `13,124` `%fmt_dec_c*` calls per shader. This change introduces a single shared branch-free dynamic-channel helper `%fmt_dec_dyn(fmt, words, chan)` (reusing `Gen.fmt_dec_component(None, params)` with branch-free `chan % ncomp` selection) used on demand by 1/2/3-component dynamic-format loads: - `buffer_load_format_x` / `_d16_x` emits **1** `%fmt_dec_dyn` call instead of 4 (`-75%`). - `buffer_load_format_xy` / `_d16_xy` emits **2** calls instead of 4 (`-50%`). - `buffer_load_format_xyz` / `_d16_xyz` emits **3** calls instead of 4 (`-25%`). - `buffer_load_format_xyzw` / `_d16_xyzw` continues using `%fmt_dec_c0..c3` unchanged. - Shaders containing only `<4`-component loads omit `%fmt_dec_c0..c3` entirely; shaders mixing `_xyzw` and `<4`-component loads share a single `%fmt_norm_bits` definition. - `DST_SEL` handling (`0 -> 0`, `1 -> dst1` even when OOB per SHADER-429, reserved `2/3 -> 0`, `4..7 -> in_b ? dec : 0` with `chan = sel & 3`) is bit-exact with the 4-component path. Aggregate impact across 15 cached large compute shaders (counts only): - Dynamic format decode calls drop from `35,200 -> 8,849` (`-74.9%`); SPIR-V lines drop from `7,017,898 -> 6,915,894` (`-102,004` lines). All modules pass `spirv-val --target-env vulkan1.2`. ### Tested - `python3 -I tests/test_rdna2spirv_decode.py`: 26/26 passed (including new `test_format_decode_dyn_call_builds_and_caches_function`). - `LEONIDA_REQUIRE_SPIRV_TOOLS=1 PYTHONPATH=tests/semantic:tools python3 tests/semantic/test_format_decode_table.py`: 7/7 passed (exhaustive check across all 128 format codes with rotated `DST_SEL` on `_xyz`, all 8 `DST_SEL` values in-bounds & OOB on `_x`, mixed `_xyzw` + `_x` deduplication of `%fmt_norm_bits`, branch-free verification, and exact call-count assertions). - `LEONIDA_REQUIRE_SPIRV_TOOLS=1 python3 -I scripts/run_semantic_tests.py tests/semantic`: 465 files, 6111 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 **816/816** exact readback words (`cs_buf_swizzle` 8/8, `cs_fmt_1010102` 64/64, `cs_fmt_int_narrow` 136/136, `cs_fmt_sint32` 64/64 exercising mixed `%fmt_dec_dyn` + `%fmt_dec_c*`, `cs_fmt_all` 336/336, `cs_fmt_xy_xyz` 128/128 exercising 22 `%fmt_dec_dyn` calls, `cs_fmt_d16_xyz` 80/80 exercising 6 `%fmt_dec_dyn` calls). 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.
GeorgiyDemo commented 2026-11-24 14:20:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by GeorgiyDemo on 2026-11-24T14:20:59Z.


Tested 7bddb1bce 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_decode_table.py: 7 tests OK; tests/test_rdna2spirv_decode.py: 26 tests OK, all at this head.
  • Against main's translator the decode-table file fails 2 of 7, so the demand-driven %fmt_dec_dyn path is covered.
  • GPU, --filter cs_fmt: 5/5 passed on MoltenVK 1.4.2 and 5/5 on KosmicKrisp (Mesa 26.3.1), every case "dumped words exact" — no regression on either driver.
  • cs_fmt_all is not in that set: it has no runner row on main (see #800), so it is not covered here either way.

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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7134229410 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/811#issuecomment-7134229410). Originally posted by [GeorgiyDemo](https://github.com/GeorgiyDemo) on 2026-11-24T14:20:59Z. --- Tested `7bddb1bce` 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_decode_table.py`: 7 tests OK; `tests/test_rdna2spirv_decode.py`: 26 tests OK, all at this head. - Against `main`'s translator the decode-table file fails 2 of 7, so the demand-driven `%fmt_dec_dyn` path is covered. - GPU, `--filter cs_fmt`: 5/5 passed on **MoltenVK 1.4.2** and 5/5 on **KosmicKrisp** (Mesa 26.3.1), every case "dumped words exact" — no regression on either driver. - `cs_fmt_all` is not in that set: it has no runner row on `main` (see #800), so it is not covered here either way. AI-assisted; synthetic fixtures only, no game data.
qhukz13 commented 2026-11-24 16:25:01 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by qhukz13 on 2026-11-24T16:25:01Z.

Review: COMMENTED


Checked 7bddb1bce (Linux, CPU only, no game files). Merges cleanly with current main.

  • Tests pass: tests/test_rdna2spirv_decode.py (26), and through unittest discover (these files set up no sys.path of their own) test_format_decode_table.py, test_buffer_formats_all.py, test_buffer_load_format.py, test_buffer_format_narrow_int.py, test_buffer_tbuffer_format_d16.py, test_tbuffer_load_format.py, test_buffer_format_d16_hi_absent.py.
  • The new DST_SEL branch keeps the old mapping: 0 -> 0, 1 -> dst1 (not gated by in_b, SHADER-429), 2/3 -> 0, 4..7 -> the decoded channel gated by in_b.
  • Mutations with semantic_mutants (with #734's logic mutations), all seven format files run together:
    • the dynamic-channel block in fmt_dec_component (k is None, lines 3329-3350): 30 of 30 caught;
    • the demand-driven DST_SEL branch (lines 3698-3708): 17 of 19 caught, the d16 path included.

The two survivors are both on line 3698, the OpBitFieldUExtract of each output's DST_SEL:

  • UExtract -> SExtract is equivalent: the value is compared unsigned (>= 4) and masked with & 3 afterwards.
  • A field width of 4 instead of 3 survives, and that one is real: it mixes the low bit of the next output's DST_SEL into this one, so an output with DST_SEL 0..3 would read the decoded channel whenever the next selector is odd. One case pins it: a dynamic-format buffer_load_format_x (or _xy) whose x selector is 0 (or 1) and whose y selector is 5 must give 0 (or 1.0 / 1) in x, not the decoded channel.

Otherwise LGTM; the call-count drop on the large compute shaders is a nice win.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6043200169 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/811#pullrequestreview-6043200169). Originally posted by [qhukz13](https://github.com/qhukz13) on 2026-11-24T16:25:01Z. **Review:** COMMENTED --- Checked 7bddb1bce (Linux, CPU only, no game files). Merges cleanly with current `main`. - Tests pass: `tests/test_rdna2spirv_decode.py` (26), and through `unittest discover` (these files set up no `sys.path` of their own) `test_format_decode_table.py`, `test_buffer_formats_all.py`, `test_buffer_load_format.py`, `test_buffer_format_narrow_int.py`, `test_buffer_tbuffer_format_d16.py`, `test_tbuffer_load_format.py`, `test_buffer_format_d16_hi_absent.py`. - The new `DST_SEL` branch keeps the old mapping: 0 -> 0, 1 -> `dst1` (not gated by `in_b`, SHADER-429), 2/3 -> 0, 4..7 -> the decoded channel gated by `in_b`. - Mutations with `semantic_mutants` (with #734's logic mutations), all seven format files run together: - the dynamic-channel block in `fmt_dec_component` (`k is None`, lines 3329-3350): 30 of 30 caught; - the demand-driven `DST_SEL` branch (lines 3698-3708): 17 of 19 caught, the d16 path included. The two survivors are both on line 3698, the `OpBitFieldUExtract` of each output's `DST_SEL`: - `UExtract` -> `SExtract` is equivalent: the value is compared unsigned (`>= 4`) and masked with `& 3` afterwards. - A field width of 4 instead of 3 survives, and that one is real: it mixes the low bit of the next output's `DST_SEL` into this one, so an output with `DST_SEL` 0..3 would read the decoded channel whenever the next selector is odd. One case pins it: a dynamic-format `buffer_load_format_x` (or `_xy`) whose x selector is 0 (or 1) and whose y selector is 5 must give 0 (or 1.0 / 1) in x, not the decoded channel. Otherwise LGTM; the call-count drop on the large compute shaders is a nice win.
SweatierKey commented 2026-11-24 16:43:29 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by SweatierKey on 2026-11-24T16:43:29Z.


Thanks @qhukz13 for running semantic_mutants and catching the 3 -> 4 OpBitFieldUExtract width survivor! Pushed a follow-up commit setting DST_SEL_Y = 5 (odd) across the _x 8-selector matrix in tests/semantic/test_format_decode_table.py and confirmed locally that mutating the extract width from 3 to 4 now fails test_format_decode_table.py.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7138109256 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/811#issuecomment-7138109256). Originally posted by [SweatierKey](https://github.com/SweatierKey) on 2026-11-24T16:43:29Z. --- Thanks @qhukz13 for running `semantic_mutants` and catching the 3 -> 4 `OpBitFieldUExtract` width survivor! Pushed a follow-up commit setting `DST_SEL_Y = 5` (odd) across the `_x` 8-selector matrix in `tests/semantic/test_format_decode_table.py` and confirmed locally that mutating the extract width from `3` to `4` now fails `test_format_decode_table.py`.
qhukz13 commented 2026-11-24 17:48:57 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by qhukz13 on 2026-11-24T17:48:57Z.


Thanks — confirmed on 5c7a8bbd8: test_format_decode_table.py passes, and the field-width mutant on the DST_SEL extract (3 -> 4) now fails it; the UExtract -> SExtract one that still survives is the equivalent one. LGTM.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7139744180 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/811#issuecomment-7139744180). Originally posted by [qhukz13](https://github.com/qhukz13) on 2026-11-24T17:48:57Z. --- Thanks — confirmed on 5c7a8bbd8: `test_format_decode_table.py` passes, and the field-width mutant on the DST_SEL extract (3 -> 4) now fails it; the `UExtract` -> `SExtract` one that still survives is the equivalent one. LGTM.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!811
No description provided.