Skip to content

wave64_emulate_opt: record destination read for s_bitset0/1_b32/b64 in _scalar_rw (SHADER-767) - #4280

Open
JavierBlanco23 wants to merge 1 commit into
ItsNotRexed:mainfrom
JavierBlanco23:shader-767-bitset-liveness
Open

JavierBlanco23 wants to merge 1 commit into
ItsNotRexed:mainfrom
JavierBlanco23:shader-767-bitset-liveness

Conversation

@JavierBlanco23

Copy link
Copy Markdown
Contributor

Resolves SHADER-767 (claimed in #4257; finding by @lumenpearson).

Summary

s_bitset0_b32, s_bitset0_b64, s_bitset1_b32, and s_bitset1_b64 read-modify-write their destination SGPR(s), preserving all untouched bits. Previously, tools/wave64_emulate_opt.py:_scalar_rw omitted the destination from rd and included it in must_kill, causing find_local_exec_folds to treat a live-out SGPR whose join-block consumer is s_bitset* as dead and fold the guarding EXECZ region — leaving the 63 inactive lanes per wave64 with the stale pre-region value.

Changes

  • tools/wave64_emulate_opt.py: record destination registers in rd (and exclude them from must_kill) for s_bitset0_b32, s_bitset0_b64, s_bitset1_b32, and s_bitset1_b64 in _scalar_rw.
  • tests/semantic/test_wave64_emulate_opt.py: added test_bitset_dest_read_prevents_unsafe_execz_fold, an end-to-end --wave64-emulate regression executing all four opcodes at both 64 and 128 threads and asserting every lane's output word.

Testing

  • python -B -m compileall -q tools/wave64_emulate_opt.py tests/semantic/test_wave64_emulate_opt.py
  • python -B -I tests/semantic/test_wave64_emulate_opt.py -v: all tests OK (Windows 11, Python 3.12, NVIDIA GeForce RTX 5080, driver 616.56; CPU-only synthetic workgroup interpreter, no game files).

AI-assisted.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 1588bd8 on Windows 11, CPython 3.14.7, with NVIDIA GeForce RTX 4090 (driver 32.0.16.2101) and AMD Radeon(TM) Graphics (driver 32.0.21043.5001): pass. I used synthetic shaders only.

python -m pytest -q tests/semantic/test_wave64_emulate_opt.py gives 23 tests passed and 18 subtests passed. I used a virtual environment with tests/requirements.txt.
With tools/wave64_emulate_opt.py from the merge base (aa2988c), the new interpreter test fails all eight cases. Each first difference is in lane 1.

I assembled each of the eight translated modules with tools/spirv_asm.py. I ran vk_compute_test.exe shader.spv 1 dump with LEONIDA_VK_DEVICE set to each GPU and VKC_OUT_WORDS set to the workgroup size.
The four bitset operations give 17, 29, 61 and 67 in each lane, at workgroup sizes 64 and 128. There are zero differences in 768 words on each GPU.

This pull request changes Python only. The GPU runs used the existing runtime harness built from main. I found no defect. 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 1588bd8c on Windows 11: python -I tests/semantic/test_wave64_emulate_opt.py -> 23 tests, OK; compileall and git diff --check clean.

s_bitset0/1_b32/b64 now record their destination read in _scalar_rw, so the liveness/optimizer analysis sees the read-modify-write instead of treating it as a pure write. The new cases cover all four widths; the rest of the CFG suite still passes. No blocking issue.

@TG0000 TG0000 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 ran the eight new synthetic bitset fixtures from 1588bd8c on an AMD Radeon RX 6800 (AMD 26.11.2 / 32.0.21045.29003, Vulkan 1.4.315, Windows 11 Pro build 26300), translating the same fixtures with aa2988cc and with the PR. All 16 outputs passed spirv-val --target-env vulkan1.2. The vk_compute_test ... 1 dump readback had 756/768 wrong words with the base translator (63/64 and 126/128 for each opcode, first difference in lane 1) and 0/768 wrong words with the PR. Both runs used the same harness built from main.

AI-assisted (OpenAI Codex). Synthetic inputs only.

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

The four scalar bitset forms read-modify-write their destination, so counting it as a read in _scalar_rw and removing it from must-kill is correct. The regression covers all forms and wave sizes; independent interpreter and GPU checks confirm the previously stale lanes now match.

@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 review at 1588bd8c with uv CPython 3.12.13. An independent sparse-EXEC fixture active only on lanes 7 and 39 cleared bit 61 and set bit 34 in separate b64 cases. Reading both destination words, the head matched all 768 words against an integer oracle across 64- and 128-thread workgroups. The parent folded the guarded region and left both words stale in the other 62 lanes of the affected wave. Four straight-line controls passed on both.

The stock eight regression subtests failed on the parent and passed at head (768/768 lane outputs). The 23 semantic tests and 26 helper tests from #4263 9ef83e44 passed together against the head optimizer. The seven full-suite failures and two errors reproduced in the corresponding parent suites. Synthetic data only.

@SebSilent

Copy link
Copy Markdown
Contributor

TEST-928 author check on your head 1588bd8, from our unit file (tests/test_wave64_emulate_opt.py, PR #4263): all 26 pass unchanged on top of this fix, and the destination read this PR records is exactly the gap lumenpearson and I reported in _scalar_rw's table - the bitset read masks now include the destination (s_bitset0_b32 s5, s6 -> 0x60; s_bitset1_b64 s[8:9], s[10:11] -> 0xf00), excluded from must_kill as the row in your tests asserts. The helper-level four-opcode assertion offered on #4084 stays available if you want it in this PR; otherwise it goes into #4263 after this lands. CPU only. AI-assisted.

@byPreaZy

Copy link
Copy Markdown

Tested 1588bd8 on AMD Radeon RX 9060 XT (RDNA4, native wave64, subgroup 32..64), Windows 11, driver 32.0.32015.7029, Vulkan SDK 1.4.363.0.

  • python -B -I tests/semantic/test_wave64_emulate_opt.py -v: 23 tests, all OK
  • LEONIDA_W64_EMULATE=1 python -I scripts/run_gpu_tests.py --build runtime/build: 196/210 passed, 14 skipped, 0 failed; 214 shaders translated, 214 pass spirv-val
  • Corpus check under --wave64-emulate (counts only, no shader data): all 647 wave64 cs/ms corpus shaders containing s_bitset0/1_b32/b64 (603 ms + 44 cs) emit byte-identical assembly on this head and on aa2988c. Emulation itself was confirmed engaged (emulated vs plain output differs), so the comparison is real: the unsafe-fold pattern the new test covers does not fire differently on any corpus shader. The fix is correct and latent here, and nothing regresses.

First AMD + 32-wide check for this PR. AI-assisted (Devin).

@Dom-in

Dom-in commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Regression check of c5380e7 on AMD Radeon RX 9070 (RDNA4, subgroup 32..64), AMD driver 26.11.2 (LLPC), Windows 11 Pro 26300, Vulkan SDK 1.4.363, scripts/run_gpu_tests.py natively and with LEONIDA_W64_EMULATE=1, PR merged onto main 3faac55: no regressions.

  • Full GPU suite: 198/217 passed, 15 skipped in both modes, the same counts and failures as main 3faac55.
  • The emulated run exercises wave64_emulate_opt across the wave64 fixtures. The native run covers the AMD side of the "AMD + 32-wide GPU" pairing; the 32-wide half still needs someone with NVIDIA or Intel.

AI-assisted (Claude Code).

@AwkGrep256

Copy link
Copy Markdown
Contributor

Reviewed and tested head c5380e7, applied to main 3faac55. CPU only; I didn't run anything on the GPU.

  • The new test test_bitset_dest_read_prevents_unsafe_execz_fold passes. With the tools/wave64_emulate_opt.py change reverted it fails 8 of 8 (every op at lx 64 and 128, e.g. 0 != 17 : s_bitset0_b32 lx=64 lane=1), so it catches the bug.
  • test_wave64_emulate_opt 23/23 pass. Full tests/semantic (396 files, 6,252 tests) passes except test_loop_ballot_barrier, which fails the same way on main (tests: test_loop_ballot_barrier follows #2089's structured wave64 emulation (main red since 1cc7497b) #3995 fixes it).
  • Code read: the SOPK RMW ops (s_addk_i32, s_mulk_i32, s_cmovk_i32) already keep their destination as a source in the operand split above, so the four s_bitset* were the remaining SOP1 read-modify-writes missing from rd. They are also correctly excluded from must_kill.

AI-assisted (Claude Code). Linux, CachyOS, Python 3.14.

@CuteBooty

Copy link
Copy Markdown

Tested c5380e7 on NVIDIA GeForce RTX 3070 (32-wide subgroups only), driver 617.14, Windows 11: PR merged onto main 3faac55, runtime/test/shaders build.py + check.py natively and with LEONIDA_W64_EMULATE=1, compared per test with main 3faac55 on the same machine.

  • Build: 242 shaders, 242 translated, 242 pass spirv-val (head and base).
  • Native: 153 of 188 pass, 35 skipped, 0 fail; identical per test to base.
  • Emulated: 184 of 188 pass, 4 skipped, 0 fail; identical per test to base.
  • Translated modules byte-identical to base in both modes (496/496). Wave64 graphics tiers unchanged: A 10 / B 7 / C 13.

So no regression on a GPU where wave64 always runs emulated. This is expected rather than a test of the fix: the only suite shader with s_bitset* (cs_w32_salu64.asm:27, one s_bitset1_b64) is wave32 with no EXECZ/EXECNZ branch, so the fold this PR blocks never comes up in the suite. The new regression test's shapes were not run on the GPU (only through the CPU emulator, as in the PR).

One review point on the test: both b64 cases write both SGPRs (s16 and s17) inside the guarded region, so they would also pass if only one half of the destination pair were recorded as read. A b64 case that writes only s17 in the region would cover that.

Not tested: 64-lane hardware, retail shaders.

AI-assisted (Claude Code).

@JavierBlanco23

Copy link
Copy Markdown
Contributor Author

Thanks for running the 32-wide NVIDIA GPU check on c5380e73! Together with @Dom-in's RX 9070 run, both halves of the AMD + 32-wide GPU pairing are now verified on the current head with zero regressions.

Good catch on the b64 test writing both s16 and s17 — keeping c5380e73 untouched for now so the GPU checks stay on the current head, and happy to add the s17-only b64 subcase if a rebase is needed before merge.

@lunarminus

Copy link
Copy Markdown

Tested c5380e73 on its parent main 3faac557 on an AMD Radeon 760M (RDNA3 iGPU, gfx1103, subgroup 32..64), RADV Mesa 26.3.1, CachyOS Linux: pass

  • Base commit: My local main was d0835dd9, an ancestor of the PR's parent 3faac557 (923 commits back), so merging the PR onto it fast-forwarded to the PR head. The tree tested here is therefore c5380e73 itself. The comparison without the PR is a 3faac557 worktree.
  • Build: I built runtime/ from the tested tree (RelWithDebInfo), because it differs from d0835dd9. The control used the same build.
  • PR tests:
    • python -B -I tests/semantic/test_wave64_emulate_opt.py -v: 23 tests OK.
    • python -m unittest discover -s tests/semantic -p 'test_wave64*': 199 tests OK.
    • pytest tests/semantic/test_wave64_emulate_opt.py: 23 passed, 18 subtests passed.
    • The new test run against 3faac557's tools/wave64_emulate_opt.py fails all 8 subtests, with the first bad lane 1 in each.
  • On the GPU (synthetic modules only):
    • I translated the test's eight modules (4 ops at 64 and 128 threads, --wave64-emulate) and assembled them with tools/spirv_asm.py. All pass spirv-val.
    • I ran each with vk_compute_test <spv> 1 dump (VKC_OUT_WORDS = workgroup size), at the default subgroup size and with VKC_SUBGROUP_SIZE 32 and 64.
    • With the PR: 17 / 29 / 61 / 67 in every lane, 0 of 2304 words differ.
    • Translated with 3faac557's tool: 2268 of 2304 words differ (63/64 and 126/128 per module, from lane 1).
    • The 32-wide runs here use required subgroup size 32 on this AMD device, not an NVIDIA or Intel GPU.
  • scripts/run_gpu_tests.py, native and with LEONIDA_W64_EMULATE=1:
    • 202/217 passed, 15 skipped, 0 failed in both modes; 221 shaders translated, 221 pass spirv-val.
    • Per-test comparison with 3faac557 without the PR, same build: identical counts, 0 status differences over all 217 result lines in either mode.
    • Against my earlier d0835dd9 runs (196/210, 14 skipped): 0 status differences on the 209 common tests. The 7 extra tests (6 PASS, ps_m2_literal SKIP) came from main after d0835dd9. The PR adds no GPU fixtures.

AI-assisted (Claude Code).

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested 938e5a57 merged onto main dde82f27 on Windows 11, CPython 3.14.3, CPU only: pass.

  • python -I -m unittest discover -s tests/semantic -p test_wave64_emulate_opt.py: 31 tests OK.
  • Negative control (tools/wave64_emulate_opt.py from main, the PR's tests kept): tests/semantic/test_wave64_emulate_opt.py 31 tests FAILED (failures=10).
  • py_compile on the 2 changed Python files: OK; git diff --check: clean.

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

@carlos-seiffert

Copy link
Copy Markdown

Tested 938e5a5 (merged onto main 976b3ec) on NVIDIA GeForce RTX 2060 (Turing), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 (10.0.26300), subgroup size 32: pass (no regression)

What I ran: python -I scripts/run_gpu_tests.py with the Vulkan tools built from 976b3ec (llvm-mingw + Ninja, -DLEONIDA_BUILD_WINDOWS_RUNTIME=OFF, Vulkan-Headers 6802bb47), llvm-mc 23.1.6 and SPIRV-Tools from conda-forge.

main 976b3ec this PR on main
full GPU suite 192/241 passed, 48 skipped, 1 fail 192/241 passed, 48 skipped, 1 fail (same as main)

No GPU test changed status against main. This PR adds no GPU fixture of its own, so the run checks that the change breaks none of the existing GPU tests; it does not exercise the new rule directly.

Unit tests changed by the PR: tests/semantic/test_wave64_emulate_opt.py: OK.

The one failure on both sides is ms_wave_info_s3, which fails on main on this GPU (10/10 runs) and is fixed by #7136 / #5741 / #5855; it is unrelated to this PR.

AI-assisted run (Claude Code); the numbers are from my machine.

@InCaseOf

Copy link
Copy Markdown

Tested 938e5a5 on RX 5700 XT (RDNA1, gfx1010, RADV NAVI10), Mesa 26.3.1, Linux 6.18: pass

  • scripts/run_gpu_tests.py: 211/239 passed, 28 skipped, 0 failed.
  • w64_graphics.py over all fixtures: 29/29 ok.
  • tests/semantic/test_wave64_emulate_opt.py: Ran 31 tests in 7.488s OK
  • scripts/run_semantic_tests.py tests/semantic -j 8: 442 files, 5887 tests, 1 file failed (test_image_store_d16, which also fails on main here), nothing new.

LLVM llvm-mc 23.1.5, SPIRV-Tools v2026.4, Python 3.14; test programs built from main e36d4c4 unless rebuilt for the PR as noted. main e36d4c4 on the same GPU: run_gpu_tests.py 212/240, 28 skipped, 0 failed; w64_graphics.py 29/29; semantic suite 2 files failing (test_image_atomics_guard, test_image_store_d16). 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.