Skip to content

rdna2spirv: typed loads through the V# FORMAT read each channel from a table; compute modules compile 2x faster than with #4777 alone - #5125

Open
kapitche wants to merge 4 commits into
ItsNotRexed:mainfrom
kapitche:dynload-arith-load
Open

kapitche wants to merge 4 commits into
ItsNotRexed:mainfrom
kapitche:dynload-arith-load

Conversation

@kapitche

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue: #4683 (comment)

Depends on: #4777 (this branch is stacked on it; the diff against main also shows #4777's commit until it merges. The new part is commits ef25b4e47 and the two doc commits after it.)

What broke

A buffer_load_format_* whose format comes from the V# decodes every format of FORMATS and selects one: a binary search of DontFlatten branches since #3496 (about 900 SPIR-V instructions, plus about 500 inline for d16). The NVIDIA driver inlines every function, so each such load costs 0.5 to 0.8 s of driver compile time. Reviewers of #3496 measured the same on NVIDIA and RADV. Compute modules with 66 to 96 of them are among the slowest to compile.

Fix

The load is a call to one function per (component count, d16). The function has no branch. Each destination register decodes only the source channel its DST_SEL names, from %fmt_load_tab (per format code and channel: bit offset, width, kind, plus the format's flags). Raw bits, f16, UNORM/SNORM (the same exact quotient, its constant selected by width) and SINT are straight-line arithmetic and the kind picks the result. tbuffer_load_format_* has a fixed format and keeps its inline decode. There is no dynamic-format image_load path to change. test_format_decode_branch.py no longer pins the binary-search shape (its value test stays, and it now puts its own folder on sys.path); the two unit tests of decode_any_format/format_decode_call in tests/test_rdna2spirv_decode.py are replaced.

Results (counts only; own dump, RTX 4080 SUPER, one module at a time, --jobs 1, no cache file, fresh generator word per module and run)

Six compute modules with 66 to 96 loads (1.6 to 4.8 MB on main), summed compile time, two rounds (round 1 ran while the machine was busy):

main #4777 only this PR
round 1 383 s 215 s 91 s
round 2 189 s 100 s 43 s

Peak driver memory per module: 1.5 to 3.3 GB on main, 0.6 to 1.05 GB with #4777, 0.2 to 0.6 GB here. SPIR-V: the 1.6 to 1.8 MB modules go from 81-91k to 36-40k instructions (-56%), the 4 to 5 MB ones about -13%. Six pixel shaders with 7 to 113 loads: 61 s main, 56 to 66 s #4777, 33 to 40 s here (6 modules, 0 failed in all). Six 7 to 13 MB compute modules (85 loads each) went over 8 GB on main and on both changes and were not timed; they shrink 1.1% in bytes. All 18 translate and pass spirv-val on all three.

Tested

Not run: AMD hardware, the #884 giants.

AI-assisted.

…a table, with no branch per format

A buffer_load_format_* whose format comes from the V# decoded every format of FORMATS and selected one (a binary
search of DontFlatten branches since ItsNotRexed#3496), about 900 instructions behind each load and 500 more inline for d16.
The NVIDIA driver inlines every function and took about 0.8 s to compile each load. The load is now a call to one
function per (component count, d16): each destination register reads the channel its DST_SEL names from a
per-format table with straight-line arithmetic. Values identical to the previous translation for all 128 codes x
widths x d16 x DST_SEL x alignment x bounds (193,536 loads, interpreter).
@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 1a4a2bd (the stack head: #4777's head 434b98e is in the PR's history and #4777 is still open) on AMD Radeon 610M (RADV RAPHAEL_MENDOCINO, subgroup 32..64), CachyOS Linux, LEONIDA_VK_DEVICE=RADV: pass.

After a full build, scripts/run_gpu_tests.py per fixture, all exact:

  • cs_fmt_1010102 64/64; cs_fmt_d16_xyz 80/80 and 128/128; cs_fmt_int_narrow 136/136; cs_fmt_sint32 64/64; cs_fmt_xy_xyz 128/128; cs_buf_fmt8 "0x7f 0x80 0xff 0x1 0x0 0x0 0xff 0x1"
  • LEONIDA_VK_DEVICE=NoSuchDevice on cs_fmt_sint32: FAIL, "no Vulkan physical device matches".
  • tests/semantic test_typed_load_flat 4 tests, test_typed_store_flat 8 tests and test_format_decode_branch 1 test: OK. tests/test_rdna2spirv_decode.py: 24 tests, OK. tests/test_docs_*.py: 30 tests, OK.

The compile-time claim, the spirv_mini_interp differential and the full semantic suite were not run here.

AI-assisted.

@Kam471 Kam471 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.

Ran this at 1a4a2bd5b on Windows 11, RTX 5070 Ti.

  • tests/semantic/test_typed_load_flat.py -> 4 tests, OK (8.3 s); test_typed_store_flat.py -> 8 tests, OK; test_format_decode_branch.py -> 1 test, OK.
  • pytest tests/test_rdna2spirv_decode.py -q -> 24 passed, 66 subtests passed.
  • GPU run_gpu_tests.py --filter cs_fmt -> 6/6 passed, every dumped word exact: cs_fmt_1010102 64/64, cs_fmt_d16_xyz 80/80 and 128/128, cs_fmt_int_narrow 136/136, cs_fmt_sint32 64/64, cs_fmt_xy_xyz 128/128.
  • git diff --check clean.

Typed loads through the V# FORMAT now pick each channel from a per-format table instead of a branch per shape, mirroring the #4777 store change; the interpreter, the decode tests and the GPU fixtures all keep the measured words. The docs record the compile-time/size measurement. No blocking issue.

@Atomic258

Copy link
Copy Markdown
Contributor

Independent check — docs reviewed (measurement correction, no code).

The corrected figures are internally consistent (1.6–4.8 MB modules on main, 12–14% shrink on the 4–5 MB tier, 56–57% on the small ones) and match the direction the typed-load rewrite implies. I can't reproduce the RTX 4080 SUPER timings on my machine (RTX 5090/617.42 — different driver compile-time profile), so treat this as a plausibility check, not a re-measurement.

AI-assisted (Devin); docs-only.

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 1a4a2bd on Intel UHD 630 (CFL GT2), Mesa 25.2.8 ANV, Ubuntu 24.04: cs_fmt_1010102 (64/64 words exact), cs_fmt_d16_xyz (80/80 and 128/128), cs_fmt_sint32 (64/64), cs_fmt_xy_xyz (128/128) and cs_buf_fmt8 pass. cs_fmt_int_narrow still fails (word 34: expected 0xffffffff, got 0x00000000), the same failure as the main 3faac55 baseline on this GPU. Full suite 154/217 passed, 61 skipped, with only cs_fmt_int_narrow and cs_signed_zero_nan failing, the same counts and failures as that baseline. The control run with a nonexistent device name fails on cs_fmt_sint32, so the filter applied.

AI-assisted.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested 1a4a2bd on Windows Server 2022, CPython 3.13.12: pass

  • python -I tests/test_rdna2spirv_decode.py -> Ran 24 tests, OK; python -I -m unittest discover -s tests/semantic -p "test_typed_load_flat.py" -> Ran 4 tests, OK
  • Negative control (tools/rdna2spirv.py swapped to upstream/main): FAILED (failures=6)

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

@ramenbellic

Copy link
Copy Markdown

Tested 1a4a2bd on NVIDIA GeForce RTX 3070, driver 620.70.06, Linux (Bazzite, Fedora 45 base, kernel 7.3.4): pass

  • GPU suite (scripts/run_gpu_tests.py), this PR merged onto main 3faac55: 170/217 passed, 47 skipped, 0 failed native; 201/217 passed, 16 skipped, 0 failed with LEONIDA_W64_EMULATE=1
  • The PR's tests: tests/semantic/test_format_decode_branch.py, tests/semantic/test_typed_load_flat.py, tests/semantic/test_typed_store_flat.py, tests/test_rdna2spirv_decode.py: OK
  • tests/semantic: failures=3, errors=2, all in test_loop_ballot_barrier (wave64-emulate cases); main 3faac55 alone gives the same counts on this machine, so not from this PR
  • Whole shader corpus (26,503 game shaders, --wave64-emulate), this PR merged onto main 3faac55: 26503/26503 translated, 26503 pass spirv-val (main alone: 26503/26503); 11,591 of the .spv outputs differ from main's, none newly invalid

AI-assisted run (Claude Code); counts only, no game data.

@AwkGrep256

Copy link
Copy Markdown
Contributor

A data point from RADV for this PR (SHADER-774 / #4315). AMD RX 9070 XT, RADV/Mesa 26.3.1, CachyOS, my own 1.0.2.1 copy, counts only. ScreenProbes_ActivateRays translated alone and compiled alone with vk_pipeline_check --start 0 --count 1 --jobs 1, under an 18 GiB memory cap and a 15-minute limit:

Translator SPIR-V size Result
this PR's base 98fa503f 18.0 MB still compiling at 15 min, peak 4.6 GB
this PR's head 1a4a2bd5 15.4 MB OOM-killed at the 18 GiB cap after 92 s

So on RADV the table-based typed loads make this module's compile run out of memory much faster, although the module is smaller. (On main 561b65a8 the same module, 15.8 MB, also doesn't finish in 15 min, peaking at 8.9 GB.) I haven't measured the other modules or NVIDIA. Happy to rerun a newer head on the same setup.

AI-assisted.

@wasonidas

Copy link
Copy Markdown

Windows GPU review of 1a4a2bd5bed0b888284fe445c2521f5607be688d against #4777 434b98e552e63ed6897d8adde1595ce90edeaa0c and main 65965423684cd94e1c1a96f649d87e7942a14113. Synthetic inputs only. The PR matched #4777 on all 3,120 Radeon sweep readback words with equal format coverage, and all 11 common modules per revision passed Vulkan 1.2 SPIR-V validation.

Cold pipeline creation on a bounded one-load-site fixture, milliseconds:

Revision SPIR-V bytes Integrated Radeon 32.0.21042.62 RTX 5080 621.24
main 90,060 1292.2, 1238.4, 1205.5 106.5, 108.8, 98.4
#4777 53,756 511.2, 513.8, 507.8 67.3, 61.2, 67.9
#5125 24,312 1494.4, 1565.9, 1500.4 36.3, 36.4, 36.6

Three interleaved fresh-process runs per revision used the same harness, a null Vulkan pipeline cache, and a changed SPIR-V generator word. Immediate repeats took 0.14–0.46 ms. A second harness reproduced the ordering. On this Radeon/Windows driver, the PR takes 2.94× the #4777 median despite emitting 55% fewer bytes. On RTX it takes 0.54×. A separate #4777 store control showed overlapping Radeon timings before and after.

The regression is for this synthetic load fixture and driver. Main and the stack differ in supported format coverage, so #4777 is the like-for-like comparison here. Please recheck this case after rebasing onto current main. AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor Author

Thanks @AwkGrep256 and @wasonidas for the AMD numbers. They change my reading of this PR, so, plainly: on the Windows Radeon driver the one-load-site fixture takes 2.94x the #4777 median (despite fewer bytes), and on RADV the ScreenProbes_ActivateRays module is OOM-killed at 18 GiB after 92 s where the base was still compiling at 15 min. Against that, NVIDIA improves (0.54x of #4777 on the RTX 5080, and the 2x on the RTX 4080 SUPER in the description). I have no AMD hardware here, and I did not re-measure on AMD, so I cannot say why the table lookup is slow there, only that the reports are consistent with each other. So this should not be merged as it stands: the table-based loads are a win on one vendor and a loss on another, and the AMD data point is not covered by my numbers. Separately, the PR is CONFLICTING and depends on #4777, which itself needs a re-port onto main's new fmt_encode_fn (see my comment there), so I am not rebasing this until #4777 has landed or been re-ported. Waiting on that; if the maintainers prefer, I can close this one and keep only #4777.

@WebIdentity1

Copy link
Copy Markdown

Independent check of the mini-interpreter OpReturn hunk only at head 1a4a2bd5bed0b888284fe445c2521f5607be688d, applied to main 1599683. This is not a check of the full typed-buffer translation change or its GPU behavior.

I independently reproduced the void-return defect, then found your existing implementation here and in #4777 during the file-diff overlap check. I released my claim in #8344 (comment); no duplicate fix PR.

Three local synthetic self-tests in test_interp.py exercise:

  • A void helper stores 1 and returns; the caller then stores 2. Main returns 1 before the fix, 2 with it.
  • A void helper returns to a caller that branches to a phi. The phi must use the caller's predecessor label; expected output 4, main returns 1.
  • A value-returning helper calls a void helper before returning 8 to main. Main returns 1 before the fix, 8 with it.

All three fail on unchanged main and pass with the OpReturn frame-pop change. I applied the exact interpreter hunk from this PR and confirmed its Python AST is identical to the tested implementation (the only textual difference was its explanatory comment).

macOS 27.2 / Apple Silicon / Python 3.14.7, CPU only:

  • python -m pytest -q tests/semantic/test_interp.py tests/semantic/test_interp_merge_check.py tests/test_semantic_translator_use.py: 67 passed, 116 subtests passed.
  • Full semantic runner: 447 files, 5,959 tests, with the same 48 baseline failure identifiers in test_image_atomics_guard and test_image_store_d16; no new failures.
  • Compileall, make_game --help, changed-file no-game-data scan and diff check passed.

No GPU, game files or proprietary inputs. AI-assisted: yes.

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.

8 participants