Skip to content

Translator: branch on the run-time buffer format, 4-6x faster format loads (SHADER-620) - #3496

Open
tibzejoker wants to merge 1 commit into
ItsNotRexed:mainfrom
tibzejoker:translator/format-decode-branch
Open

tibzejoker wants to merge 1 commit into
ItsNotRexed:mainfrom
tibzejoker:translator/format-decode-branch

Conversation

@tibzejoker

@tibzejoker tibzejoker commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-620 (claim)

What and why

A buffer_load_format_* whose format comes from the V# decoded every format in FORMATS and selected one, on every lane. docs/d3d12_vulkan.md counts 84,908 such loads in the corpus. aa2988c already moved the element size into a table; the decode itself was still all formats.

  • Gen.decode_any_format is now a binary search over the codes, with DontFlatten selections, so only the chosen decode runs. It uses plain conditional branches because spirv_mini_interp rejects OpSwitch.

The format table, the size table, the per-format decode and the DST_SEL chain are untouched.

Result

RTX 4090 (621.01), hand-written compute shader, 4.19M threads x 16 loads, buffer in VRAM, median of 60 dispatches, median of 5 runs, both sides on aa2988cc:

V# format main this PR faster
32_FLOAT 2.443 ms 0.424 ms 5.8x
8_8_8_8_UNORM 2.354 ms 0.524 ms 4.5x
2_10_10_10_UNORM 2.357 ms 0.546 ms 4.3x
the shader without loads 0.075 ms

The format is the same for the whole dispatch here, as with a descriptor that is uniform over the draw. A format that varies inside one subgroup runs each of the decodes present.

Tested

Windows 11, rebased on aa2988cc.

  • RTX 4090 differential against main: identical output buffers for all 128 format codes in four configurations (four byte alignments, half the records out of range, constant DST_SEL), xyzw and d16_xyzw, 65,536 different elements each.
  • python -m unittest discover -s tests/semantic: same result as aa2988cc (its 9 failures in test_loop_ballot_barrier, test_cmp_class_f64 and test_mutations fail the same way there), plus 2 new tests. The new structure test fails on main.
  • The translate, format, buffer, decode, f16, make_game and docs test files under pytest: 772 passed, 598 skipped (including tests that need an amdgcn llvm-mc), 0 failed.
  • spirv-val: 130 of 130 modules, before and after (the semantic fixtures and dynamic-load probes, compute ones also with --wave64-emulate).

Not run: AMD and Intel GPUs, the GPU test suite, and the game's shader corpus (so no before/after translated and valid counts, and no effect on the #884 compile times). An AMD run would be very welcome.

Prior art: boykopovar/AnyPS5#3656 hit the same cost and used a table. Only the observation is used here, no code.

AI-assisted (Claude Code).

Checklist

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

@shdann

shdann commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 511041b on Windows, CPython 3.14.7, CPU only (no GPU, no game files): pass for the part that needs no SPIRV-Tools or GPU.

  • python -m unittest discover -s tests/semantic -p test_format_decode_branch.py: 2 OK.
  • Existing tests of the same code, all OK: the tests/semantic/test_buffer_*.py files and test_norm_decode.py, plus tests/test_rdna2spirv_decode.py, test_d16_format_probe.py (2 skipped), test_feature_scan.py and test_shader_profile.py.
  • Negative control: with tools/rdna2spirv.py from the merge base, test_only_the_chosen_decode_runs fails; the value test passes on both, as it should for an equivalent rewrite.
  • Equivalence probe: a hand-written pixel shader with one buffer_load_format_xyzw whose FORMAT comes from an SGPR, translated by this head and by the merge base and run in spirv_mini_interp: every format code 0..127, five data patterns (mixed, all ones, zeros, f16/f32 specials, random), byte offsets 0-4 and 13, 3,840 cases; then _x/_xy/_xyz with five DST_SEL values on every third code, 645 cases. Head and base give identical results in all of them, with no error.

Not checked here: spirv-val on the new nested selections (no SPIRV-Tools on this machine), a GPU run, the timing table and the corpus counts. No defect found. AI-assisted.

@kotorvr

kotorvr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 511041b on NVIDIA GeForce RTX 4090 (Ada, subgroup 32..32), 621.14, Windows 11 (26200): pass. Same GPU model as the author's, a different driver (621.14 here, 621.01 there) and my own copy of the package for the game-copy part (counts only).

Test programs: MSVC 19.44 RelWithDebInfo build of main f847748, the branch's base (the PR changes nothing under runtime/). llvm-mc 21.1.8, Vulkan SDK 1.4.363.0, Python 3.13.7.

main f847748 511041b
python -I scripts/run_gpu_tests.py 153/199 passed, 46 skipped, 0 failed 153/199 passed, 46 skipped, 0 failed
the same with LEONIDA_W64_EMULATE=1 184/199 passed, 15 skipped 184/199 passed, 15 skipped
python -I tools/rdna2spirv.py <work>/shaders/dxbc/disasm --limit 200 --no-asm 200 translated, 200 pass spirv-val 200 translated, 200 pass spirv-val
vk_pipeline_check <that slice> cache.bin --jobs 8 (fresh cache each) 171 ms + 29 ps modules and pipelines ok, 0 failed, 0 skipped the same: 200 of 200, 0 failed, 0 skipped
driver compile time of the 171 mesh pipelines, summed 735 s (slowest 123 s) 773 s (slowest 130 s)

In that 200-shader slice, 171 of the 218 .spv files differ from main's and the folder grows by 0.3% (60.56 MB to 60.74 MB). All of them still create a pipeline on this driver, so the DontFlatten search does not trip NVIDIA's compiler in this sample. The compile-time difference (+5%) is one run each on a machine that was busy with other work, so I would not read more into it than "no large change"; I did not measure run-time speed.

On the branch:

  • python -m unittest discover -s tests/semantic -p test_format_decode_branch.py: 2 tests OK
  • -p "test_buffer*.py": 156 OK; -p "test_*format*.py": 85 OK
  • python -I -m compileall -q make_game.py tools tests: exit 0

Not run: the full tests/semantic suite and the full corpus (26,503 shaders); this is a 200-shader slice. No game data quoted. AI-assisted check (Claude Code).

@ttd0x

ttd0x commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 511041b2 on NVIDIA GeForce RTX 2080 Ti (Turing, subgroup 32..32, driver 610.88), Windows 11: pass, no regression.

  • tests/semantic/test_format_decode_branch.py: 2 tests OK.
  • scripts/run_gpu_tests.py (default, no emulation), 204 shaders translated, all pass spirv-val: 152/199 passed, 46 skipped (native wave64 tests, expected on a 32-wide device), 1 failed. Main f847748c gives the same result lines; the one failure (ps_sdwa_f16_const) also fails on main, so it is not from this PR.
  • Not measured: the 4-6x speed claim.

Synthetic fixtures only, no game data. AI-assisted (Claude Code).

@kacper-serewis kacper-serewis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

SHADER-620 at 511041b. For one aligned synthetic xyzw payload all 128 descriptor selectors matched the parent in the CPU interpreter, including all 87 zero-result selectors. Both modules passed Vulkan 1.2 spirv-val, the 81 nested selections had the expected child exit predecessors, and both focused tests passed. Timing was outside this review. AI-assisted.

@kotorvr

kotorvr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 511041b (merge base f847748c) on Windows 11, Python 3.13, spirv-val from Vulkan SDK 1.4.363, my own 1.0.2.1 copy: pass for the corpus counts the description lists as not run. Counts only, no game data.

Whole corpus at this head. The disassembly make_game.py produced (26,503 shaders: 25,030 clean, 1,473 with invalid words), python -I tools/rdna2spirv.py <disasm> --out <scratch> --shard i/8 --wave64-emulate --no-asm for i = 0..7, then --merge:

  • 26,503 translated, 26,503 pass spirv-val, 0 Unsupported, 0 tracebacks; the merged index has 26,503 entries (12,101 PS, 9,282 mesh, 2,689 CS, 2,236 VS, 195 GS).
  • main at f847748c, same command earlier today: 26,503 / 26,503. No regression in either count.

Before/after on one slice (--shard 0/64, 415 shaders, same flags, base f847748c vs this head):

base head
translated / pass spirv-val 415 / 415 415 / 415
.spv bytes 274,007,012 272,690,964 (-0.48%)
modules byte-identical to base 189
modules changed 226 (size change median +0.2%, range -5.1% to +12.5%)
translation time, one process 4 m 32 s 3 m 30 s

Not checked here: pipeline creation over the corpus (vk_pipeline_check, so no effect on the #884 compile times), the GPU timing table, and AMD or Intel GPUs. No defect found.

AI-assisted (Claude Code).

@Thatters

Thatters commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Tested 511041b2 (base f847748c) on an RTX 5070 Ti (Blackwell, subgroup 32), NVIDIA 621.24, Windows 11, SPIRV-Tools from Vulkan SDK 1.4.363, with my own 1.0.2.1 copy: translation passes, but compute compiles slower on NVIDIA, and the larger #884 giant goes from compiling (22.2 GB) to out of memory at a 32 GB cap (see the edit below). This adds what @kotorvr's runs didn't cover: changed modules across the whole corpus, the full semantic suite, and cache-proof compile times that include compute. Counts only.

Whole corpus (--wave64-emulate --no-asm, 8 shards per side): 26,503 / 26,503 translate and pass spirv-val on both, as @kotorvr found. 14,401 modules change (9,268 ms, 2,251 ps, 1,748 cs, 1,134 vs) and 12,102 are byte-identical. Total .spv size goes down 0.52%, and the changed modules' median size ratio is 1.002 (0.889 to 1.221). No module changes its control-flow form or capabilities.

Tests: tests/semantic complete, 7,324 tests OK (1 expected failure, as on main). test_format_decode_branch passes through unittest discover. Run directly as python -I tests/semantic/test_format_decode_branch.py, it fails with No module named 'support', because it doesn't put its own folder on sys.path the way its neighbours do. A nit only.

NVIDIA compile time. 300 random changed modules (seed 3496, up to 4 MB, so #884's giants are left out): 208 ms, 39 ps, 28 cs, 25 vs. vk_pipeline_check --jobs 4 --slow 0, no cache file, and a fresh SPIR-V generator word per module and run so the driver's disk cache can't answer. Run order: base, PR, PR, base. All 300 pipelines are created in every run. Summed per-pipeline times:

stage base r1 PR r1 PR r2 base r2 PR / base
cs (28) 151.8 s 191.2 s 190.8 s 143.6 s 1.29 (per-module median 1.27, range 0.90-1.52)
ms (208) 1,044.1 s 992.2 s 990.6 s 1,019.6 s 0.96 (median 1.00)
ps (39) 118.6 s 116.6 s 119.5 s 109.6 s 1.04 (median 1.03)
vs (25) 1.15 of only 8.8 s
all 300 1,324 s 1,310 s 1,311 s 1,281 s 1.006

Wall time is 333.6 / 329.5 / 329.5 / 322.7 s, and peak private memory is 8.25-8.46 GB in every run. So overall it's neutral, with mesh slightly faster. Compute is about 29% slower to compile, the same in both rounds, which is well outside the 3% round-to-round noise. My guess is the binary search's DontFlatten branches in larger compute bodies, but I haven't confirmed it. That matters for #884: the four largest compute modules in the corpus all change, including the two giants. The larger one is timed below.

Edit: the larger #884 giant, timed. Same card and driver. The module alone, --jobs 1, no cache file, a fresh generator word, each run in a Windows job object with a 32 GB per-process memory cap, private bytes sampled every 5 s:

larger giant .spv result time peak private
base f847748c 52.4 MB compiles, cs pipelines: 1 ok 6,566 s 22.2 GB
PR 511041b2 47.3 MB LLVM ERROR: out of memory at the cap, exit 0xC0000409 524 s 31.25 GB

The memory curves differ in shape, not just in peak. Base holds at 3.1-3.2 GB for about 52 min, then climbs in steps (5.4, 9.3, 10.6, 11.8 GB) to 22.2 GB before it finishes. The PR's run sits at 4.4-5.7 GB for 8 min, then jumps to 31 GB within about 36 s. So on this NVIDIA setup the change turns the giant from compilable into one that needs more than 32 GB, which matches the +29% on the sampled compute modules. A likely direction, untested: keep the select-based decode (or bound the search) for compute modules, or for modules over a size limit, and keep the search for graphics, where mesh even got faster. The branch has since been force-pushed to 9ecdb939, but its tools/rdna2spirv.py change against its new base is line for line the same as 511041b2's, so these numbers stand.

Not measured: run-time speed of the format loads, and AMD or Intel. AI-assisted (Claude Code).

A buffer_load_format with a V# format decoded every format and selected one.
A binary search now runs only that format's decode. Identical output on an RTX
4090 for all 128 codes in four configurations, 4.3x to 5.8x faster in a load
benchmark on top of the format size table (aa2988c).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@ligmaSec ligmaSec left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I reproduced the compute compile-time increase with hand-written shaders on Linux and headless RADV GFX1201. I compared 03b09a08 with only the translator diff from 9ecdb939 applied to that same base. All fixtures passed spirv-val for both translations.

Dynamic buffer_load_format_x loads Base pipeline compile With this change
8 0.1 s 0.3 s
32 0.6 s 3.6 s
128 7.7 s 50.6 s

All three pipelines compiled on each side. Peak RSS for the whole three-pipeline checker process was 176 MiB on the base and 526 MiB with the change. For the 128-load shader, SPIR-V size moved from 521,036 to 532,480 bytes. On llvmpipe with the same fixtures the 128-load compile rose from 5.2 to 34.2 seconds.

The shader accumulates buffer_load_format_x v2, v0, s[0:3], 0 idxen offset:<4*i> into v1 after each s_waitcnt vmcnt(0), then uses buffer_store_dword to store the sum through s[4:7]. Both descriptors come from user data, so the format and selector remain dynamic. Translation used the same options on both sides (--no-float-controls, native wave64).

On Debian 13 with Mesa 25.0.7, VK_DRIVER_FILES=/usr/share/vulkan/icd.d/radeon_icd.json RADV_FORCE_FAMILY=GFX1201 MESA_SHADER_CACHE_DISABLE=true vk_pipeline_check <spirv-dir> --count 3 --jobs 1 --slow 0 ran the ACO compiler with no physical GPU. I used a fresh process and no supplied pipeline cache. This measures compilation only. It exercises the repeated typed-load pattern discussed for SHADER-774 using synthetic inputs. AI-assisted.

Single-component decoder experiment

I put an alternative in a research branch. It uses DST_SEL_X to identify the source channel, loads its bit offset and numeric kind from a format table, and reuses the existing exact UNORM/SNORM conversion. The _x instruction can still select Y, Z or W, including the existing wrap for missing channels. D16 and wider loads retain their current decoder.

The helper is 86 SPIR-V instructions including its function bounds, down from 1,353 on the base. On the same headless RADV setup it compiled the 128-load fixture in 0.5 seconds. A fresh run with only the 512-load fixture compiled it in 3.86 seconds, while the base was still compiling when the 90-second limit stopped it.

The prototype passed 157 buffer semantic tests. A differential run covered all 128 format codes and all eight selector values with four input alignments and an out-of-bounds case. All 5,120 outputs matched the base. Six typed-buffer fixtures validated and matched their expected readbacks on Mesa llvmpipe. The compile comparison uses synthetic shaders and CPU-side driver compilation. The game module and physical AMD execution are still unverified. This is available for the SHADER-774 contributors to try independently of the binary-search change.

I repeated the comparison with Mesa 26.1.6 and the matching AMDGPU DRM shim configured for GFX1201. The 128-load pipeline compiled in 7.26 seconds on the base, 42.03 seconds with the binary-search change and 0.42 seconds with the single-component decoder. Each process compiled all three requested pipelines without a supplied cache.

A separate 3,280-load synthetic shader passed spirv-val with the single-component decoder and compiled in 201.77 seconds on Mesa 25.0.7, peaking at 3,346 MiB of RSS. The original decoder also passed validation and was still compiling when the 400-second limit expired. Both runs used the same 400-second wall-time limit and 8 GiB process address-space limit. The cutoff gives a lower bound on the original compile time. The 3,280 case isolates typed loads and does not reproduce the game module's full control flow.

The full semantic suite also ran with this prototype, SPIRV-Tools 2026.4, strict tool checks, and the independent baseline fixes from #3883, #3885, #3995 and #4356. It completed 7,457 tests with one expected failure and zero unexpected failures.

@akimfomin

Copy link
Copy Markdown

Bounded NVIDIA Ada compile comparison for the typed single-result loads discussed above. Windows 11 28000.3501, RTX 4070 Ti SUPER / 617.14. I used the unmodified 03b09a08 vk_pipeline_check to compile shaders from 03b09a08, the current 9ecdb939 translator and the x-only experiment at ca539372. Same synthetic compute fixtures, each with a runtime descriptor and independent observable result.

buffer_load_format_x, non-D16 Main median Current PR median x-only median
8 loads 0.210 s 0.314 s 0.052 s
32 loads 0.970 s 2.101 s 0.166 s

Three distinct shader samples per size and revision, one shader per fresh checker process, one worker, alternating main/PR order. The x-only runs followed the A/B. All 18 modules passed Vulkan 1.2 spirv-val and all 18 pipeline creations passed. The application cache feedback reported zero hits. At 32 loads the SPIR-V size was 155,832 / 167,276 / 131,108 bytes respectively.

This corroborates a compile-time cost for the current PR even on a modest synthetic Ada workload. The x-only candidate specializes non-D16 single-result loads. The wider formats, dispatch time and the large retail shader remain outside this check. Synthetic inputs only. AI-assisted (Codex).

@memorypressure

Copy link
Copy Markdown

Verified against current main (merges cleanly). The binary-search decode itself is solid: test_buffer_load_format (34), test_norm_decode (6), test_buffer_format_narrow_int (3), test_rdna2spirv_decode (24) and test_value_semantics (39 + 282 subtests) all pass and the translator-use guard is green. One real defect, reproduced here: the new test_format_decode_branch.py fails on Python 3.14 — its reference() decodes f16 with struct.unpack("<e"), which since 3.14 keeps the NaN payload (gh-130317), so format 71's sNaN half 0x7F01 compares 0x7FE02000 against the canonical 0x7FC00000 the interpreter pins. spirv_mini_interp.half_to_float exists precisely to avoid version-dependent <e NaNs — please use it in the reference. Minor: the file does a bare "import support", so it does not even start under python -I on 3.14; recent sibling tests insert their own dir first. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

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.

9 participants