Skip to content

rdna2spirv: cache per-block %active loads and fast-path no-EXEC dst() writes - #4473

Open
SweatierKey wants to merge 2 commits into
ItsNotRexed:mainfrom
SweatierKey:feat/cache-active-exec
Open

SweatierKey wants to merge 2 commits into
ItsNotRexed:mainfrom
SweatierKey:feat/cache-active-exec

Conversation

@SweatierKey

Copy link
Copy Markdown
Contributor

Claim: #4421 (comment).

What changes

Measured impact (spirv-as + spirv-val --target-env vulkan1.2)

Benchmark Before (03b09a08) After Delta
Divergent loop (2.5k RDNA2 insns, 40 s_cbranch_execz blocks) 35,738 lines / 631,184 B .spv / 2,566 OpLoad %active 33,256 lines / 591,472 B .spv / 84 OpLoad %active −6.9% lines, −6.3% .spv bytes, −96.7% %active loads
Straight-line ALU (1k v_fma_f32, no EXEC writes) 16,264 lines / 296,540 B .spv / 1,005 OpLoad %active 13,251 lines / 240,300 B .spv / 0 OpLoad %active −18.5% lines, −19.0% .spv bytes, −100% %active loads
234 real GPU-harness .asm fixtures (runtime/test/shaders/*.asm) 362,312 lines (234/234 valid) 349,510 lines (234/234 valid) −12,802 lines (−3.5% incl. fixed headers/prologues)

Verification (Linux CPU, Python 3.13.5, LLVM 18.1.8, SPIRV-Tools, no game files)

  • tests/semantic/test_active_cache.py: 6/6 OK (mid-block s_and_saveexec_b32/s_or_b32 cache updates on 32-lane run_wave, divergent loop caching, no-EXEC fast path, DPP row_mask preservation under exec_static_true, --wave64-emulate validation, direct Gen() default).
  • tests/test_translate.py: 343/343 passed (1 skipped, 0 failed) + 234/234 GPU .asm fixtures validated with spirv-val --target-env vulkan1.2 (1 pre-existing LLVM 18 operand error on ps_sdwa_f16_const, identical to 03b09a08).
  • Full tests/semantic suite (7,456 tests): zero new failures vs 03b09a08; tests/test_semantic_translator_use.py: 3/3 OK; all 74 non-pytest translator/SPIR-V tests/test_*.py: 74/74 passed.
  • git diff --check clean.

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 aa813789 on Windows 11: test_active_cache.py 6 OK, test_carry_exec_mask.py 9 OK, test_valu_permlane16.py 16 OK, test_f64_transcendentals.py 2 OK; git diff --check clean.

The per-block %active loads are cached and no-EXEC dst() writes take the fast path; the updated tests (including the carry/EXEC mask and permlane cases) pass. No blocking issue.

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

AI-assisted Windows CPU verification at aa813789, uv CPython 3.12.13, SPIRV-Tools v2026.4 and LLVM 21.1.8 through WSL. Two fresh 32-lane pixel shaders passed Vulkan 1.2 validation, matched hand-calculated outputs and agreed with exact parent 03b09a08 on every lane.

  • A shader using save-EXEC, EXECZ branches, a restore and a second mask change reduced active loads from 25 to 6 (489 to 470 SPIR-V lines). Masked lanes 5 and 20 retained the expected words through both regions.
  • A straight-line no-EXEC shader reduced active loads from 7 to zero and uint selects from 6 to zero (290 to 271 lines).

The selected touched tests passed, including 7 bpermute cases with AMDGPU LLVM. Synthetic interpreter and offline validation only.

@Atomic258

Copy link
Copy Markdown

Tested aa813789 on Linux (Ubuntu), CPython 3.10.12, pytest 9.2.0, CPU only, synthetic .asm fixtures, no game files: pass.

  • python -I: tests/semantic/test_active_cache.py 6/6 OK, test_carry_exec_mask.py 9/9 OK, test_valu_permlane16.py 16/16 OK, test_f64_transcendentals.py 2/2 OK, test_rcp_iflag_mods.py 5/5 OK.
  • Negative control: with main's tools/rdna2spirv.py, test_active_cache.py fails 4/6 — %active is re-loaded before every OpSelect instead of being cached per block.
  • tests/test_translate.py runs 585 skipped here — SPIRV-Tools isn't installed, so the spirv-as/spirv-val validation paths didn't run. Worth a maintainer/reviewer run with the toolchain present.
  • git diff --check clean; compileall clean.

AI-assisted (Devin).

@mrbean56

Copy link
Copy Markdown

Tested aa81378929 on Linux (Debian 13), Python 3.13.5, SPIRV-Tools v2026.4: pass, no output change observed.

  • pytest tests/semantic/test_active_cache.py tests/semantic/test_carry_exec_mask.py tests/semantic/test_valu_permlane16.py tests/test_f64_transcendentals.py tests/test_rcp_iflag_mods.py tests/test_translate.py -q: 619 passed, 4 skipped, 144 subtests passed.
  • Semantic differential against main: translated all 40 PS fixtures that translate here with this branch and with 03b09a08, ran each through the mini interpreter with the same (empty) inputs, and compared every output word: 40 / 40 identical.
  • exec_static_true safety: writes_exec catches every EXEC writer I can name — v_cmpx* by prefix (covers _e32/_e64/_sdwa/_dpp), any saveexec/wrexec opcode (including s_andn2_saveexec, s_xor_saveexec), and any SALU op whose first operand is an exec* destination (s_mov_b64 exec, s_wqm_b64 exec, s_cselect_b64 exec, …). The s_cmp*/s_bitcmp* exclusions are right because their first operand is a source. Every OpStore %active in the file goes through emit() with that exact prefix, so the assert covers the model's only way to change %active; dst()'s fast path keeps the dpp_guard check, and a DPP write therefore still builds the OpLoad/OpSelect guard.
  • Spot check: a synthetic PS with no EXEC write emits 0 OpLoad %bool %active and direct OpStores; the same PS with v_cmpx_gt_f32 or s_and_saveexec_b64 emits the %active load/store machinery (2 loads, 1 store each), so the fast path is off exactly where it must be.

One process note: the assert is the safety net, and it disappears under python -O. The repo runs -I, so this is only a caution for anyone running the translator with optimizations on.

No GPU, no game data. AI-assisted.

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

Reviewed aa81378: %active values are cached only within a block and updated when stored, while labels and split regions clear the cache. The no-EXEC fast path retains DPP guards and is enabled only after the translator finds no EXEC-writing instruction. Independent semantic, SPIR-V validation, and differential reports pass.

@paratustra

Copy link
Copy Markdown

Checked aa81378 against main 03b09a0 (macOS, Python 3.15.2, tests/semantic/support.translate_lines). A shader that writes EXEC through a VALU carry-out now fails to translate. writes_exec() (tools/rdna2spirv.py:12983-12991) only looks at the first operand. In VOP3b ops, though, the carry-out SGPR is the second operand, and it can be EXEC. So exec_static_true is set for a shader that does write EXEC.

v_mov_b32 v0, 5
v_mov_b32 v1, -1
v_add_co_u32 v2, exec_lo, v1, v0
exp mrt0 v0, v2, v2, v2 done vm
s_endpgm
result
main translates (the carry-out is stored to %active)
this PR AssertionError: OpStore %active emitted with exec_static_true: OpStore %active %t18

It fails loudly only because of the assert in emit(). Under python -O it translates while exec_static_true is still set, so the no-EXEC fast path is chosen for a shader that writes EXEC. Checking every destination operand would close this. That means the VOP3b carry-out (v_add_co/v_sub_co/v_subrev_co/*_co_ci, v_mad_u64_u32/v_mad_i64_i32, v_div_scale), or any operand the op writes, not only a[0]. A one-line test with this listing would pin it.

I found no EXEC writer that the per-block %active cache misses: every store goes through emit(), which resets on OpLabel. The full semantic suite gives the same failures on this head and on main, apart from the PR's new tests.

No game files. AI-assisted (Claude Code).

@bobcatchris

Copy link
Copy Markdown
Contributor

Apple M2 no-regression check at head aa81378 (macOS 26.4, MoltenVK 1.4.2 direct, subgroup 4..32, llvm-mc 23.1.6, clean full build exit 0).

python -I scripts/run_gpu_tests.py --build runtime/build/linux: 213 shaders translated, 213 pass spirv-val; 127/210 passed, 76 skipped, 7 failed — identical to upstream/main aa2988c on this M2 (same seven known MoltenVK expected failures, no new failure or skip). Head fetched read-only, no rebase/edit/push. Synthetic shaders and counts only; no game files.

AI-assisted.

@theswiss24

Copy link
Copy Markdown

Tested aa813789 (contains main 03b09a08) on NVIDIA GeForce RTX 2060 SUPER (Turing), driver 580.178.04, Ubuntu 24.04.5, llvm-mc 20.1.2, test programs built from main 03b09a08 (this PR changes no C++): pass.

LEONIDA_W64_EMULATE=1 python3 -I scripts/run_gpu_tests.py --build <main build>: 195/210 passed, 15 skipped, 0 failed, the same result on every test as main 03b09a08. I'm also measuring this PR on the corpus's largest modules for SHADER-774 and will post that in the coordination issue.

No game files.

Tested and written by Claude (Claude Code), posting from @theswiss24's account.

@sperm

sperm commented Nov 23, 2026

Copy link
Copy Markdown

Tested aa81378 on AMD Radeon RX 9070 XT (RADV GFX1201) and the Ryzen 7 9800X3D iGPU (RADV RAPHAEL_MENDOCINO, gfx1036), Mesa 26.2.6, Linux 7.3.4: pass, no regressions.

  • Full python -I scripts/run_gpu_tests.py (Linux build of this branch's runtime/, LEONIDA_VK_DEVICE per GPU): 196/210 passed, 14 skipped, 0 failed on both GPUs, with every test's result the same as at the merge base 03b09a0. Every GPU fixture goes through the changed dst()/%active paths.
  • Corpus slice (my own 1.0.2.1 work folder, python -I tools/rdna2spirv.py <work>/shaders/dxbc/disasm --limit 2000), this PR against 03b09a0:
    • Translated: 2,000 against 2,000. spirv-val: 2,000 against 2,000. No regression in either count.
    • All 2,000 modules change, and the slice's SPIR-V goes from 1,110.1 MB to 1,080.1 MB (−2.7%). That is smaller than your fixture figure (−3.5% lines), which fits real shaders writing EXEC more often.
    • The two runs were concurrent, so I don't report a time difference.
  • tests/test_translate.py: 581 passed, 4 skipped. tests/test_f64_transcendentals.py: 2 OK. tests/test_rcp_iflag_mods.py: 5 OK.
  • python -m unittest discover -s tests/semantic: 7462 run, 7 failures and 2 errors. That is exactly main's set at 03b09a0, compared by test name, so test_active_cache.py and the relaxed tests pass.

No game files or shader names; counts and sizes only. AI-assisted.

@SandimL

SandimL commented Nov 23, 2026

Copy link
Copy Markdown

Tested aa813789 on AMD Radeon RX 6750 XT (gfx1031, Navi 22, RDNA2, subgroup 32..64), AMD proprietary driver 26.11.2 (Vulkan 2.0.353 / API 1.4.315), Windows 11 Pro build 26300.

Translation (runtime/test/shaders/build.py, llvm-mc 21.1.8 + SPIRV-Tools 2025.1 via WSL): 234/234 translated, 234/234 pass spirv-val — identical with and without this PR's rdna2spirv.py change.

GPU (check.py, test programs from the runtime-windows CI artifact of main 4eb6155d): 178/184 pass, 4 skipped, 2 unrunnable with these binaries, and the exact same split appears on the parent commit aa813789~1, so this PR changes nothing on RDNA2 here.

  • Skipped (expected): ms_w64x_ballot, ms_wave_info_s3, ms_v5_workgroup (mesh fixtures build.py does not build) and ps_w64x_readlane (this device cannot require a 64-wide fragment subgroup).
  • The 2 that don't run are a stale-binary mismatch, not a regression: the 4eb6155d artifact predates VKC_HEAP_INIT (61a9adc2), so cs_heap_init reads the untouched 0xEEEEEEEE fill; ps_array_resinfo (4096/4096 pixels differ) is also newer than that artifact. Both fail identically with and without this PR — I A/B'd by swapping only tools/rdna2spirv.py to its parent. I could not build the test programs at this commit locally (no MSVC/Vulkan SDK on this machine).

Net: no RDNA2 regression from this change, and the whole corpus translates and validates clean. AI-assisted (Claude Code).

@SandimL

SandimL commented Nov 23, 2026

Copy link
Copy Markdown

Scope note on my check above: I reproduced @paratustra's regression on this setup too. Their listing fails to translate on aa813789 with AssertionError: OpStore %active emitted with exec_static_true: OpStore %active %t18, while the parent aa813789~1 translates it fine (Python 3.11.9, Windows; swapping only tools/rdna2spirv.py).

So my "no regression / corpus clean" result above is scoped to the 234 runtime/test/shaders fixtures build.py builds — none of them write EXEC through a VOP3b carry-out, so that run does not clear this PR. The writes_exec() fix @paratustra describes (consider every destination operand, e.g. the VOP3b carry-out SGPR, not just a[0]) is needed first. AI-assisted (Claude Code).

@SweatierKey

Copy link
Copy Markdown
Contributor Author

Thanks @paratustra, @SandimL, and @mrbean56 for catching this! Pushed fix in 453fec25:

  • writes_exec() now checks base_op(op) in VOP3_TWO_DESTS for a[1] targeting exec* (covering v_add_co_u32, v_sub_co_u32, v_subrev_co_u32, *_co_ci_u32, v_mad_u64_u32, v_mad_i64_i32, v_div_scale_f32/f64), while still returning False when exec* is only read as a source operand (e.g. s_mov_b32 s0, exec_lo). Audited every OpStore %active, set_exec(), exec_half_write(), put_mask(), and mask_dst() callsite in tools/rdna2spirv.py to confirm no other operand position can write EXEC.
  • Replaced assert not self.exec_static_true in Gen.emit() with an explicit RuntimeError so the safety net remains active even under python -O.
  • Added test_vop3b_carry_out_to_exec_disables_static_true in tests/semantic/test_active_cache.py covering @paratustra's exact listing + writes_exec() checks.
  • Verified: test_active_cache.py (7/7), test_carry_exec_mask.py, test_valu_permlane16.py, test_f64_transcendentals.py, test_rcp_iflag_mods.py (39/39 OK), test_cube_sample.py (18/18 OK), python3 -O guard check OK, spirv-val --target-env vulkan1.2 OK.

@paratustra

Copy link
Copy Markdown

Re-checked 453fec2 on macOS, Python 3.15.2. This resolves my note:

  • v_add_co_u32 v2, exec_lo, v1, v0 and v_sub_co_u32 v2, exec_lo, … now translate and store the carry-out to %active, with and without python -O;
  • a plain read (s_mov_b32 s0, exec_lo) still takes the no-EXEC path;
  • tests/semantic/test_active_cache.py passes.

No game files. AI-assisted (Claude Code).

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.