cfg_structure / rdna2spirv: unwrap tautological %pc guards in structured CFGs (ms_wave_info_s3 AMD crash, SHADER-STRUCTURED-PC-UNWRAP) #808

Manually merged
github merged 4 commits from gh-pr/808/head into gh-pr/808/base 2026-11-26 09:22:22 +00:00
Owner

@⁠whoalin1 on GitHub · 2026-11-24 14:03 UTC · original
whoalin1:shader-structured-pc-unwrap → main · +337 −79 · 7 files · 4 commits · merged 2026-11-25 16:46 UTC

Claim: #14 (SHADER-STRUCTURED-PC-UNWRAP)

Problem

On AMD's proprietary Windows Vulkan driver (24.10.24, LLPC, AMD Ryzen 5 7600 RDNA2 iGPU, native wave64), ms_wave_info_s3 segfaults (0xC0000005) during vkCreateGraphicsPipelines.

In ms_wave_info_s3.asm, blk0 conditionally skips blk1 (MSG_GS_ALLOC_REQ), and both blk0 and blk1 converge at blk2 (s_barrier). cfg_structure.structure() wraps blk2 in ("guard", 2, [("block", 2)]), emitting OpBranchConditional around %blk2's OpControlBarrier even though %pc == 2 is tautologically true on every incoming path.

  • Under --wave64-emulate (e.g., 32-wide NVIDIA GPUs), wave64_emulate_opt._unwrap_known_pc_guards already unwrapped that guard, so ms_wave_info_s3 passed there.
  • On native wave64 (AMD RDNA2), rdna2spirv.capped_tree() used cfg_structure.structure() directly without unwrapping tautological %pc guards, leaving OpControlBarrier inside conditional control flow and crashing AMD Windows LLPC (0xC0000005).

Fix

  • tools/cfg_structure.py: add unwrap_known_pc_guards(tree, out_pcs, entry_pc=0) (moved from tools/wave64_emulate_opt.py, keeping cfg_structure.structure() unchanged for callers like _drop_sink_block).
  • tools/wave64_emulate_opt.py: delegate _unwrap_known_pc_guards directly to cfg_structure.unwrap_known_pc_guards.
  • tools/rdna2spirv.py (capped_tree): apply cfg_structure.unwrap_known_pc_guards to structured trees before computing nesting depth, so both assemble() and needs_barrier_rounds() see post-dominating merge/barrier blocks at top level.
  • tests/test_cfg_structure.py: add test_unwrap_known_pc_guards (single-arm if-then, diamond, loop exit, early-exit guard preservation) and differentially check unwrap_known_pc_guards across all 18,228 random reducible graphs × 6 seeds.
  • tests/test_translate.py: assert %blk2 in ms_wave_info_s3 is emitted without a conditional %pc guard before its OpControlBarrier.
  • docs/design.md: document the pass in the Control flow contract row.

Verification (before/after on main 47c651174, LEONIDA_VK_VALIDATION=1)

  • AMD Radeon(TM) Graphics (Ryzen 5 7600 RDNA2 iGPU, Windows 11, driver 24.10.24, 128B push constants, LEONIDA_PC_TAIL_UBO=1):
    • Before (main): ms_wave_info_s3 FAIL (vk_harness crash 0xC0000005)
    • After: ms_wave_info_s3 PASS (4096/4096 pixels exact, 0 validation errors); cs_barrier_divergent, cs_barrier_early_end, cs_barrier_loop 3/3 PASS
  • NVIDIA GeForce RTX 4070 SUPER (Windows 11, driver 610.88):
    • ms_wave_info_s3 PASS (4096/4096 pixels exact, 0 validation errors); cs_barrier_* 3/3 PASS
  • CPU / unit tests:
    • python -I tests/test_cfg_structure.py — 18,228 random reducible graphs × 6 seeds (both structured and unwrapped trees match walk()), plus shape & deep-chain tests
    • python -I tests/test_runner_calls_all_tests.py — 3/3 OK
    • python -I tests/test_cfg_run_limit.py — 4/4 OK
    • pytest tests/test_translate.py — 669 passed
    • unittest discover -s tests/semantic -p test_wave64_emulate_opt.py — 31/31 OK
    • unittest discover -s tests/semantic -p "test_*barrier*.py" — 53/53 OK
    • unittest discover -s tests/semantic -p "test_exec*.py" — 2/2 OK
    • unittest discover -s tests/semantic -p "test_ms_*.py" — 6/6 OK
    • python -I tests/test_text_write_line_endings.py — 3/3 OK
    • python -I scripts/check_no_game_data.py ... — clean (rc=0)

AI-assisted (Gemini 4 Neon).

**@⁠whoalin1** on GitHub · 2026-11-24 14:03 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/808) `whoalin1:shader-structured-pc-unwrap` → `main` · +337 −79 · 7 files · 4 commits · merged 2026-11-25 16:46 UTC Claim: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14 (`SHADER-STRUCTURED-PC-UNWRAP`) ### Problem On AMD's proprietary Windows Vulkan driver (`24.10.24`, LLPC, AMD Ryzen 5 7600 RDNA2 iGPU, native wave64), `ms_wave_info_s3` segfaults (`0xC0000005`) during `vkCreateGraphicsPipelines`. In `ms_wave_info_s3.asm`, `blk0` conditionally skips `blk1` (`MSG_GS_ALLOC_REQ`), and both `blk0` and `blk1` converge at `blk2` (`s_barrier`). `cfg_structure.structure()` wraps `blk2` in `("guard", 2, [("block", 2)])`, emitting `OpBranchConditional` around `%blk2`'s `OpControlBarrier` even though `%pc == 2` is tautologically true on every incoming path. - Under `--wave64-emulate` (e.g., 32-wide NVIDIA GPUs), `wave64_emulate_opt._unwrap_known_pc_guards` already unwrapped that guard, so `ms_wave_info_s3` passed there. - On native wave64 (AMD RDNA2), `rdna2spirv.capped_tree()` used `cfg_structure.structure()` directly without unwrapping tautological `%pc` guards, leaving `OpControlBarrier` inside conditional control flow and crashing AMD Windows LLPC (`0xC0000005`). ### Fix - `tools/cfg_structure.py`: add `unwrap_known_pc_guards(tree, out_pcs, entry_pc=0)` (moved from `tools/wave64_emulate_opt.py`, keeping `cfg_structure.structure()` unchanged for callers like `_drop_sink_block`). - `tools/wave64_emulate_opt.py`: delegate `_unwrap_known_pc_guards` directly to `cfg_structure.unwrap_known_pc_guards`. - `tools/rdna2spirv.py` (`capped_tree`): apply `cfg_structure.unwrap_known_pc_guards` to structured trees before computing nesting depth, so both `assemble()` and `needs_barrier_rounds()` see post-dominating merge/barrier blocks at top level. - `tests/test_cfg_structure.py`: add `test_unwrap_known_pc_guards` (single-arm if-then, diamond, loop exit, early-exit guard preservation) and differentially check `unwrap_known_pc_guards` across all 18,228 random reducible graphs × 6 seeds. - `tests/test_translate.py`: assert `%blk2` in `ms_wave_info_s3` is emitted without a conditional `%pc` guard before its `OpControlBarrier`. - `docs/design.md`: document the pass in the Control flow contract row. ### Verification (before/after on `main` `47c651174`, `LEONIDA_VK_VALIDATION=1`) - **AMD Radeon(TM) Graphics (Ryzen 5 7600 RDNA2 iGPU, Windows 11, driver 24.10.24, 128B push constants, `LEONIDA_PC_TAIL_UBO=1`)**: - Before (`main`): `ms_wave_info_s3` **FAIL** (`vk_harness` crash `0xC0000005`) - After: `ms_wave_info_s3` **PASS** (`4096/4096 pixels exact`, 0 validation errors); `cs_barrier_divergent`, `cs_barrier_early_end`, `cs_barrier_loop` **3/3 PASS** - **NVIDIA GeForce RTX 4070 SUPER (Windows 11, driver 610.88)**: - `ms_wave_info_s3` **PASS** (`4096/4096 pixels exact`, 0 validation errors); `cs_barrier_*` **3/3 PASS** - **CPU / unit tests**: - `python -I tests/test_cfg_structure.py` — **18,228 random reducible graphs × 6 seeds** (both structured and unwrapped trees match `walk()`), plus shape & deep-chain tests - `python -I tests/test_runner_calls_all_tests.py` — **3/3 OK** - `python -I tests/test_cfg_run_limit.py` — **4/4 OK** - `pytest tests/test_translate.py` — **669 passed** - `unittest discover -s tests/semantic -p test_wave64_emulate_opt.py` — **31/31 OK** - `unittest discover -s tests/semantic -p "test_*barrier*.py"` — **53/53 OK** - `unittest discover -s tests/semantic -p "test_exec*.py"` — **2/2 OK** - `unittest discover -s tests/semantic -p "test_ms_*.py"` — **6/6 OK** - `python -I tests/test_text_write_line_endings.py` — **3/3 OK** - `python -I scripts/check_no_game_data.py ...` — clean (`rc=0`) AI-assisted (Gemini 4 Neon).
cfg_structure.structure() wraps post-dominating join blocks after a diamond or loop in a ('guard', g, ...) node even when %pc == g is tautologically true on every incoming path. wave64_emulate_opt._unwrap_known_pc_guards already unwraps these guards for --wave64-emulate, which is why ms_wave_info_s3 passed on 32-wide NVIDIA GPUs while crashing with 0xC0000005 on native wave64 AMD RDNA2 Windows (driver 24.10.24, LLPC) where blk2's OpControlBarrier stayed trapped inside a conditional OpBranchConditional.

Move unwrap_known_pc_guards into tools/cfg_structure.py (reused by wave64_emulate_opt with zero duplication) and apply it in rdna2spirv.capped_tree() so both assemble() and needs_barrier_rounds() see post-dominating join blocks at top level.

SHADER-STRUCTURED-PC-UNWRAP

Co-Authored-By: Gemini 4 Neon <[email protected]>
Co-Authored-By: Gemini 4 Neon <[email protected]>
A conditional branch in the last basic block (no fallthrough block, so
len(raw_edges[b]) == 1) emits OpSelect(%cond, tgt, len(blocks)) and can
leave %pc at either tgt or cfg_structure.END.

Compute exact per-block out_pcs in rdna2spirv._block_out_pcs (matching
wave64_emulate_opt), pass it through capped_tree() into both
cfg_structure.structure() (so single-target fall-off-the-end blocks also
keep their dominated target guard) and unwrap_known_pc_guards(), and
pass compact_out_pcs into cfg_structure.structure() in wave64_emulate_opt.

Add unit and semantic regression tests covering both the loop back-edge
repro reported in review and the non-loop dominated-target variant.

Co-Authored-By: Gemini 4 Neon <[email protected]>
A block with len(raw_edges[u]) == 1 ending in a conditional branch (no
fallthrough block, so falling through writes %pc = len(blocks) = END)
cannot safely sit inside a folded s_cbranch_execz/nz region whose exit
unconditionally sets %pc = skip.

Reject such blocks during the region walk in find_local_exec_folds
(matching the existing
ot succ[u] check) and add semantic regression
tests covering both s_cbranch_scc1 X and s_cbranch_scc1 skip in
structured and --dispatch-loop --wave64-emulate modes.

Co-Authored-By: Gemini 4 Neon <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:22:19 +00:00
github manually merged commit 33431d6fe5 into gh-pr/808/base 2026-11-26 09:22:22 +00:00
Author
Owner

@⁠qhukz13 on GitHub · 2026-11-24 15:30 UTC · original · reviewed

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

The moved logic holds up:

  • tests/test_cfg_structure.py: OK (18,228 random graphs).
  • Under --wave64-emulate the pass now runs twice (in capped_tree, then in wave64_emulate_opt at line 1501). On 19,689 random structured graphs a second unwrap_known_pc_guards never changed the tree, so that is safe.
  • Five hand-written mutations of unwrap_known_pc_guards, run against test_cfg_structure.py: four are caught (unwrapping when g is only one of the possible values, keeping h possible after a loop, dropping the skip path at a guard exit, ignoring skip_pc in the sibling-guard merge). One survives: starting a loop body from cur_pc instead of cur_pc | {h}. That looks equivalent if a structured loop is always entered with %pc == h; a line saying so, or a graph showing otherwise, would settle it.

The thing I would ask for: until now this pass only ran under --wave64-emulate; with the capped_tree call it rewrites the tree of every structured shader on native wave32/wave64 too, and capped_tree also feeds needs_barrier_rounds and the MAX_STRUCTURED_DEPTH fallback. The verification covers ms_wave_info_s3 and the three cs_barrier_* fixtures on hardware plus the unit tests, but not the corpus. Per AGENTS.md ("count over the whole corpus"), could you add before/after numbers over the game's listings: translated and spirv-val-valid counts, how many modules' SPIR-V changes, and how many shaders change their barrier-rounds or depth-fallback decision? A shader that leaves barrier rounds because its barrier is now top-level is the intended effect, but the count would show it is only that.

**@⁠qhukz13** on GitHub · 2026-11-24 15:30 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/808#pullrequestreview-6042248677) · **reviewed** Checked 185220e4a (Linux, CPU only, no game files). Merges cleanly with current `main`. The moved logic holds up: - `tests/test_cfg_structure.py`: OK (18,228 random graphs). - Under `--wave64-emulate` the pass now runs twice (in `capped_tree`, then in `wave64_emulate_opt` at line 1501). On 19,689 random structured graphs a second `unwrap_known_pc_guards` never changed the tree, so that is safe. - Five hand-written mutations of `unwrap_known_pc_guards`, run against `test_cfg_structure.py`: four are caught (unwrapping when `g` is only one of the possible values, keeping `h` possible after a loop, dropping the skip path at a guard exit, ignoring `skip_pc` in the sibling-guard merge). One survives: starting a loop body from `cur_pc` instead of `cur_pc | {h}`. That looks equivalent if a structured loop is always entered with `%pc == h`; a line saying so, or a graph showing otherwise, would settle it. The thing I would ask for: until now this pass only ran under `--wave64-emulate`; with the `capped_tree` call it rewrites the tree of every structured shader on native wave32/wave64 too, and `capped_tree` also feeds `needs_barrier_rounds` and the `MAX_STRUCTURED_DEPTH` fallback. The verification covers `ms_wave_info_s3` and the three `cs_barrier_*` fixtures on hardware plus the unit tests, but not the corpus. Per AGENTS.md ("count over the whole corpus"), could you add before/after numbers over the game's listings: translated and `spirv-val`-valid counts, how many modules' SPIR-V changes, and how many shaders change their barrier-rounds or depth-fallback decision? A shader that leaves barrier rounds because its barrier is now top-level is the intended effect, but the count would show it is only that.
Author
Owner

@⁠whoalin1 on GitHub · 2026-11-24 15:36 UTC · original

Thanks for the thorough review and mutation testing, @⁠qhukz13!

  1. Loop entry cur_pc | {h}: Added a clarifying comment in tools/cfg_structure.py (6c52f6c44). In any tree produced by cfg_structure.structure(), lbody[0] is always (block, h), which immediately overwrites cur_pc with out_pcs[h] — keeping cur_pc | {h} is defensive so back-edge iterations stay sound even if unwrap_known_pc_guards is ever called on a non-structure() tree.

  2. Corpus before/after counts: I do not have the retail 1.0.2.1 game files / shader corpus on this machine, so I cannot run the 26,503-shader corpus count myself — if a reviewer with a local corpus build can run a before/after check against 47c651174, I'd really appreciate it.

    What I did measure before/after (47c651174 vs this branch) across all 258 synthetic shaders in runtime/test/shaders/:

    • Translated: 258/258 → 258/258
    • spirv-val valid: 258/258 → 258/258
    • SPIR-V changed: 13 modules (cs_bench_mix, cs_w64x_branch, cs_w64x_chain_barrier, cs_w64x_mask_reuse2, cs_w64x_sbarrier, cs_w64x_wg128_branches, cs_wm_scc_rfl, ms_wave_info_s3, ps_far_branch, ps_mandelbrot, ps_scc, ps_scc_mask32, ps_uniform_branch)
    • control_flow (barrier-rounds / depth-fallback) changed: 0
    • Full GPU suite on NVIDIA GeForce RTX 4070 SUPER (driver 610.88): 198/249 passed, 49 skipped (exact parity with main; only the two known #31 tfe fixtures fail, and all 13 changed shaders pass).
**@⁠whoalin1** on GitHub · 2026-11-24 15:36 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/808#issuecomment-7136302301) Thanks for the thorough review and mutation testing, @⁠qhukz13! 1. **Loop entry `cur_pc | {h}`:** Added a clarifying comment in `tools/cfg_structure.py` (`6c52f6c44`). In any tree produced by `cfg_structure.structure()`, `lbody[0]` is always `(block, h)`, which immediately overwrites `cur_pc` with `out_pcs[h]` — keeping `cur_pc | {h}` is defensive so back-edge iterations stay sound even if `unwrap_known_pc_guards` is ever called on a non-`structure()` tree. 2. **Corpus before/after counts:** I do not have the retail 1.0.2.1 game files / shader corpus on this machine, so I cannot run the 26,503-shader corpus count myself — if a reviewer with a local corpus build can run a before/after check against `47c651174`, I'd really appreciate it. What I did measure before/after (`47c651174` vs this branch) across all **258** synthetic shaders in `runtime/test/shaders/`: - **Translated:** `258/258` → `258/258` - **`spirv-val` valid:** `258/258` → `258/258` - **SPIR-V changed:** `13` modules (`cs_bench_mix`, `cs_w64x_branch`, `cs_w64x_chain_barrier`, `cs_w64x_mask_reuse2`, `cs_w64x_sbarrier`, `cs_w64x_wg128_branches`, `cs_wm_scc_rfl`, `ms_wave_info_s3`, `ps_far_branch`, `ps_mandelbrot`, `ps_scc`, `ps_scc_mask32`, `ps_uniform_branch`) - **`control_flow` (barrier-rounds / depth-fallback) changed:** `0` - **Full GPU suite on NVIDIA GeForce RTX 4070 SUPER (driver 610.88):** `198/249 passed, 49 skipped` (exact parity with `main`; only the two known #31 `tfe` fixtures fail, and all 13 changed shaders pass).
Author
Owner

@⁠towade on GitHub · 2026-11-24 18:22 UTC · original

checked: 6c52f6c44 (the same two patches as the current head cc7b94777, per git range-diff; only the base moved) on RDNA4 with native wave64, as a regression check: AMD Radeon RX 9070 XT (gfx1201, AMD Windows driver 32.0.32029.2010, LLPC), Windows 11 26300, main 3aa975421 with this PR merged, scripts/run_gpu_tests.py with llvm-mc 21.1.8.

  • ms_wave_info_s3: passes on main and with this PR, native and with LEONIDA_W64_EMULATE=1 (4096/4096 pixels exact). This driver neither crashes nor gives a wrong value on main (the RX 9070 XT record in gpu-test-results/ from 23 November still lists a wrong value there; current main passes), so I can't confirm the fix itself here, only that the unwrapped guard is fine on RDNA4.
  • Because this changes cfg_structure for every shader: full GPU suite, native: 207 pass, 16 skipped, identical test by test on main and with this PR; no test fails for another reason. 29 pixel-shader tests did not run because I had not built vk_texture_test, vk_bindless_test and vk_sampler_test.

AI-assisted (Claude Code).

**@⁠towade** on GitHub · 2026-11-24 18:22 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/808#issuecomment-7140544614) checked: `6c52f6c44` (the same two patches as the current head `cc7b94777`, per `git range-diff`; only the base moved) on RDNA4 with native wave64, as a regression check: **AMD Radeon RX 9070 XT** (gfx1201, AMD Windows driver 32.0.32029.2010, LLPC), Windows 11 26300, `main` `3aa975421` with this PR merged, `scripts/run_gpu_tests.py` with `llvm-mc` 21.1.8. - `ms_wave_info_s3`: passes on `main` and with this PR, native and with `LEONIDA_W64_EMULATE=1` (4096/4096 pixels exact). This driver neither crashes nor gives a wrong value on `main` (the RX 9070 XT record in `gpu-test-results/` from 23 November still lists a wrong value there; current `main` passes), so I can't confirm the fix itself here, only that the unwrapped guard is fine on RDNA4. - Because this changes `cfg_structure` for every shader: full GPU suite, native: 207 pass, 16 skipped, **identical test by test** on `main` and with this PR; no test fails for another reason. 29 pixel-shader tests did not run because I had not built `vk_texture_test`, `vk_bindless_test` and `vk_sampler_test`. AI-assisted (Claude Code).
Author
Owner

@⁠towade on GitHub · 2026-11-24 18:42 UTC · original

Follow-up to my check above: I built vk_texture_test, vk_bindless_test and vk_sampler_test and ran the full GPU suite again, natively on the same RX 9070 XT (AMD 32.0.32029.2010), on main 3aa975421 and with this PR merged. Result: 236 of 252 pass, 16 skipped, 0 fail on both, identical test by test, so the 29 pixel-shader tests that had not run pass too.

AI-assisted (Claude Code).

**@⁠towade** on GitHub · 2026-11-24 18:42 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/808#issuecomment-7141034366) Follow-up to my check above: I built `vk_texture_test`, `vk_bindless_test` and `vk_sampler_test` and ran the full GPU suite again, natively on the same RX 9070 XT (AMD 32.0.32029.2010), on `main` `3aa975421` and with this PR merged. Result: **236 of 252 pass, 16 skipped, 0 fail** on both, identical test by test, so the 29 pixel-shader tests that had not run pass too. AI-assisted (Claude Code).
Author
Owner

@⁠Sangonapou on GitHub · 2026-11-24 18:45 UTC · original

Tested cc7b9477 on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, subgroup 32..32), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300): pass, the 32-wide side of @⁠towade's RDNA4 regression check

Runtime built at this head with scripts\build_runtime.ps1 (MSVC 19.51); llvm-mc 23.1.6, SPIRV-Tools 2026.4. No game files.

  • tests/test_cfg_structure.py: ok: 18228 graphs structured and run, 1772 irreducible (1755 split-structured, 17 capped), 12692 with loops.
  • GPU suite: 261 shaders, 261 translated, 261 pass spirv-val; native 203/252 passed, 49 skipped, 0 failed; LEONIDA_W64_EMULATE=1 235/252 passed, 17 skipped, 0 failed.
**@⁠Sangonapou** on GitHub · 2026-11-24 18:45 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/808#issuecomment-7141100053) Tested cc7b9477 on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, subgroup 32..32), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300): pass, the 32-wide side of @⁠towade's RDNA4 regression check Runtime built at this head with `scripts\build_runtime.ps1` (MSVC 19.51); `llvm-mc` 23.1.6, SPIRV-Tools 2026.4. No game files. - `tests/test_cfg_structure.py`: `ok: 18228 graphs structured and run, 1772 irreducible (1755 split-structured, 17 capped), 12692 with loops`. - GPU suite: 261 shaders, 261 translated, 261 pass spirv-val; native 203/252 passed, 49 skipped, **0 failed**; `LEONIDA_W64_EMULATE=1` 235/252 passed, 17 skipped, **0 failed**.
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-24 21:42 UTC · original

Reviewed cc7b94777 applied onto main 66d4f5aa3, and ran it on an AMD Radeon RX 7900 XT. The GPU results are below. Before that, a CPU-only repro of a miscompile the move to the native path introduces.

The unwrap drops a guard when the last block ends in a conditional branch

capped_tree builds out_pcs[b] = set(succ[b]) (tools/rdna2spirv.py, the new code around line 18093) and adds END only when succ[b] is empty. A last block that ends in a conditional branch, with no s_endpgm after it, gets fall = [], so succ is [tgt] (around line 17790/17798). But emission stores OpSelect(cond, tgt, len(blocks)) into %pc (around line 18026), so %pc can also be past the end. cfg_structure.unwrap_known_pc_guards then believes %pc is always tgt after the loop and removes a guard it needs.

wave64_emulate_opt.py already handles this (main, lines 1551-1563): it adds -1 (END) when the tail has one raw edge and its terminator is a conditional branch. That rule did not come along when the function moved to cfg_structure and the native path.

Hand-written wave32 compute shader (no game data). s4 and s5 come from the push constants and s[0:3] is a raw buffer V#:

s_cmp_eq_u32 s4, 0                    ; pc 0   B0, loop header
s_cbranch_scc1 6                      ;        -> pc 32 (B2), else fall into B1
v_lshlrev_b32_e32 v2, 2, v0           ; pc 8   B1
v_mov_b32_e32 v1, 0x1111
buffer_store_dword v1, v2, s[0:3], 0 offen
s_endpgm
s_mov_b32 s4, 1                       ; pc 32  B2
s_cmp_eq_u32 s5, 1
s_cbranch_scc1 -11                    ;        -> pc 0, the last instruction

succ is [[2, 1], [], [0]]. main's structured tree is [loop 0 [b0, guard 2 [b2]], guard 1 [b1]]; with this PR it is [loop 0 [b0, guard 2 [b2]], block 1]. On the tests/semantic interpreter (spirv_mini_interp.run_workgroup, 32 invocations; test_loop_ballot_barrier.run_compute_workgroup gives the same):

s4 s5 ISA main this PR
1 0 stores 0x1111 stores 0x1111 stores 0x1111
0 1 stores 0x1111 stores 0x1111 stores 0x1111
0 0 no store (falls off the end) no store stores 0x1111 in all 32 lanes
  • With s4 = 0, s5 = 0, B2 runs, the branch is not taken, and the program ends. With this PR, %pc is 3, the loop exits, and %blk1 runs unguarded.
  • All four outputs (main and PR, structured and dispatch-loop) pass spirv-val, so this is a semantic difference only.
  • The PR's own --dispatch-loop form matches the ISA on both trees.
  • Fix: adding END to out_pcs[2] makes the PR's output byte-identical to main's for this shader, and all three cases match the ISA. I tried it as an in-memory patch of capped_tree; I did not touch the tree.
  • Where to compute it: capped_tree(succ) only sees succ. So the set probably has to be computed in translate() from raw_edges and the terminator, as wave64_emulate_opt.py does, and passed to both capped_tree calls.
  • Why not every one-successor block: adding END to all of them would undo the unwrap this PR is for.

How exposed this is: all 392 listings in the repository end in s_endpgm, so none of them hits it. A listing has to end in a conditional branch with nothing after it. I did not check the game corpus. tests/test_cfg_structure.py builds out_pcs from succ too, so its random graphs never contain this edge. A semantic test with this shader, and a test_unwrap_known_pc_guards case [[2,1],[],[0]] with out_pcs[2] = {0, END} that keeps guard 1, would cover it.

GPU, RX 7900 XT

AMD Radeon RX 7900 XT (RDNA3, gfx1100), AMD driver 32.0.32029.2010 (26.11.2), Windows 11 build 26300, Vulkan SDK 1.4.363.0, llvm-mc 23.1.5, test programs from a main 66d4f5aa3 build.

main 66d4f5aa3 this PR
scripts/run_gpu_tests.py, whole suite 236/252 passed, 16 skipped, 0 failed 236/252 passed, 16 skipped, 0 failed, the same status for every fixture
--filter ms_wave_info_s3, native wave64, 3 runs PASS 3/3 (4096/4096 exact) PASS 3/3
--filter ms_wave_info_s3, LEONIDA_W64_EMULATE=1 PASS PASS

On this card and driver, ms_wave_info_s3 already passes natively on main, so the crash or wrong output this PR targets does not reproduce here. That is different from the RX 9070 XT record on the same driver, which was taken at 5d74c27. The PR causes no regression on RDNA3.

No game files.

**@⁠adrianmjim** on GitHub · 2026-11-24 21:42 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/808#issuecomment-7145290608) Reviewed cc7b94777 applied onto main 66d4f5aa3, and ran it on an AMD Radeon RX 7900 XT. The GPU results are below. Before that, a CPU-only repro of a miscompile the move to the native path introduces. ### The unwrap drops a guard when the last block ends in a conditional branch `capped_tree` builds `out_pcs[b] = set(succ[b])` (`tools/rdna2spirv.py`, the new code around line 18093) and adds `END` only when `succ[b]` is empty. A last block that ends in a conditional branch, with no `s_endpgm` after it, gets `fall = []`, so `succ` is `[tgt]` (around line 17790/17798). But emission stores `OpSelect(cond, tgt, len(blocks))` into `%pc` (around line 18026), so `%pc` can also be past the end. `cfg_structure.unwrap_known_pc_guards` then believes `%pc` is always `tgt` after the loop and removes a guard it needs. `wave64_emulate_opt.py` already handles this (main, lines 1551-1563): it adds `-1` (END) when the tail has one raw edge and its terminator is a conditional branch. That rule did not come along when the function moved to `cfg_structure` and the native path. Hand-written wave32 compute shader (no game data). `s4` and `s5` come from the push constants and `s[0:3]` is a raw buffer V#: ``` s_cmp_eq_u32 s4, 0 ; pc 0 B0, loop header s_cbranch_scc1 6 ; -> pc 32 (B2), else fall into B1 v_lshlrev_b32_e32 v2, 2, v0 ; pc 8 B1 v_mov_b32_e32 v1, 0x1111 buffer_store_dword v1, v2, s[0:3], 0 offen s_endpgm s_mov_b32 s4, 1 ; pc 32 B2 s_cmp_eq_u32 s5, 1 s_cbranch_scc1 -11 ; -> pc 0, the last instruction ``` `succ` is `[[2, 1], [], [0]]`. main's structured tree is `[loop 0 [b0, guard 2 [b2]], guard 1 [b1]]`; with this PR it is `[loop 0 [b0, guard 2 [b2]], block 1]`. On the `tests/semantic` interpreter (`spirv_mini_interp.run_workgroup`, 32 invocations; `test_loop_ballot_barrier.run_compute_workgroup` gives the same): | s4 | s5 | ISA | main | this PR | |---|---|---|---|---| | 1 | 0 | stores 0x1111 | stores 0x1111 | stores 0x1111 | | 0 | 1 | stores 0x1111 | stores 0x1111 | stores 0x1111 | | 0 | 0 | no store (falls off the end) | no store | **stores 0x1111 in all 32 lanes** | - With `s4 = 0, s5 = 0`, B2 runs, the branch is not taken, and the program ends. With this PR, `%pc` is 3, the loop exits, and `%blk1` runs unguarded. - All four outputs (main and PR, structured and dispatch-loop) pass `spirv-val`, so this is a semantic difference only. - The PR's own `--dispatch-loop` form matches the ISA on both trees. - **Fix:** adding `END` to `out_pcs[2]` makes the PR's output byte-identical to main's for this shader, and all three cases match the ISA. I tried it as an in-memory patch of `capped_tree`; I did not touch the tree. - **Where to compute it:** `capped_tree(succ)` only sees `succ`. So the set probably has to be computed in `translate()` from `raw_edges` and the terminator, as `wave64_emulate_opt.py` does, and passed to both `capped_tree` calls. - **Why not every one-successor block:** adding `END` to all of them would undo the unwrap this PR is for. How exposed this is: all 392 listings in the repository end in `s_endpgm`, so none of them hits it. A listing has to end in a conditional branch with nothing after it. I did not check the game corpus. `tests/test_cfg_structure.py` builds `out_pcs` from `succ` too, so its random graphs never contain this edge. A semantic test with this shader, and a `test_unwrap_known_pc_guards` case `[[2,1],[],[0]]` with `out_pcs[2] = {0, END}` that keeps `guard 1`, would cover it. ### GPU, RX 7900 XT AMD Radeon RX 7900 XT (RDNA3, gfx1100), AMD driver 32.0.32029.2010 (26.11.2), Windows 11 build 26300, Vulkan SDK 1.4.363.0, llvm-mc 23.1.5, test programs from a `main` `66d4f5aa3` build. | | main `66d4f5aa3` | this PR | |---|---|---| | `scripts/run_gpu_tests.py`, whole suite | 236/252 passed, 16 skipped, 0 failed | 236/252 passed, 16 skipped, 0 failed, the same status for every fixture | | `--filter ms_wave_info_s3`, native wave64, 3 runs | PASS 3/3 (4096/4096 exact) | PASS 3/3 | | `--filter ms_wave_info_s3`, `LEONIDA_W64_EMULATE=1` | PASS | PASS | On this card and driver, `ms_wave_info_s3` already passes natively on `main`, so the crash or wrong output this PR targets does not reproduce here. That is different from the RX 9070 XT record on the same driver, which was taken at `5d74c27`. The PR causes no regression on RDNA3. No game files.
Author
Owner

@⁠whoalin1 on GitHub · 2026-11-24 21:56 UTC · original

Great catch, @⁠adrianmjim — thank you both for the RX 7900 XT verification and for spotting that!

Fixed in b630a9b1e:

  1. tools/rdna2spirv.py: Added _block_out_pcs(blocks, raw_edges, succ) (matching wave64_emulate_opt.py) so any block with len(raw_edges[b]) == 1 whose terminator is a conditional branch (op in BRANCHES and op != s_branch, i.e. the last block with no following block, or a split_irreducible clone of it) includes cfg_structure.END alongside succ[b]. Stored it on g.out_pcs before needs_barrier_rounds() and passed it to capped_tree(succ, out_pcs) at both call sites (needs_barrier_rounds() and assemble()).
  2. tools/cfg_structure.py & tools/wave64_emulate_opt.py: While tracing this, I also caught the sibling case in cfg_structure.structure() where a last-block conditional branch directly dominates its single explicit target (succ = [[2], [], [1]], out_pcs[2] = {1, END}): line 161 previously skipped the guard on block 1 before unwrap_known_pc_guards() even ran. structure(succ, out_pcs=None) now checks set(out_pcs[x]) == {below[0][1]} when out_pcs is provided, and both capped_tree() and wave64_emulate_opt.plan_structured_emulation() pass out_pcs.
  3. Tests:
    • tests/test_cfg_structure.py: added both the loop back-edge falloff case ([[2, 1], [], [0]], out_pcs[2] = {0, END}) and the non-loop dominated-target falloff case ([[2], [], [1]], out_pcs[2] = {1, END}).
    • tests/semantic/test_structured_pc_unwrap.py: end-to-end semantic execution test covering your exact 9-instruction loop shader across (s4=1, s5=0), (s4=0, s5=1), and (s4=0, s5=0) (matching --dispatch-loop and ISA — (0, 0) performs no store), the non-loop variant in both wave32 structured and --wave64-emulate structured, and the post-dominating join s_barrier unwrap case.

Verified locally on b630a9b1e:

  • tests/test_cfg_structure.py (18,228 graphs + unit tests): pass
  • tests/semantic/test_structured_pc_unwrap.py: 3/3 pass
  • tests/semantic/test_wave64_emulate_opt.py: 31/31 pass
  • tests/test_translate.py: 675/675 pass
  • tests/test_runner_calls_all_tests.py (3/3), tests/test_ci_registration.py (15/15), tests/test_text_write_line_endings.py (3/3), scripts/check_no_game_data.py: pass
  • scripts/run_gpu_tests.py --filter ms_wave_info_s3 (spirv-val + LEONIDA_VK_VALIDATION=1): 4096/4096 pixels exact on both AMD Ryzen 5 7600 RDNA2 iGPU (24.10.24, LEONIDA_PC_TAIL_UBO=1) and NVIDIA RTX 4070 SUPER (610.88).
**@⁠whoalin1** on GitHub · 2026-11-24 21:56 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/808#issuecomment-7145600520) Great catch, @⁠adrianmjim — thank you both for the RX 7900 XT verification and for spotting that! Fixed in `b630a9b1e`: 1. **`tools/rdna2spirv.py`:** Added `_block_out_pcs(blocks, raw_edges, succ)` (matching `wave64_emulate_opt.py`) so any block with `len(raw_edges[b]) == 1` whose terminator is a conditional branch (`op in BRANCHES and op != s_branch`, i.e. the last block with no following block, or a `split_irreducible` clone of it) includes `cfg_structure.END` alongside `succ[b]`. Stored it on `g.out_pcs` before `needs_barrier_rounds()` and passed it to `capped_tree(succ, out_pcs)` at both call sites (`needs_barrier_rounds()` and `assemble()`). 2. **`tools/cfg_structure.py` & `tools/wave64_emulate_opt.py`:** While tracing this, I also caught the sibling case in `cfg_structure.structure()` where a last-block conditional branch directly dominates its single explicit target (`succ = [[2], [], [1]]`, `out_pcs[2] = {1, END}`): line 161 previously skipped the guard on block 1 before `unwrap_known_pc_guards()` even ran. `structure(succ, out_pcs=None)` now checks `set(out_pcs[x]) == {below[0][1]}` when `out_pcs` is provided, and both `capped_tree()` and `wave64_emulate_opt.plan_structured_emulation()` pass `out_pcs`. 3. **Tests:** - `tests/test_cfg_structure.py`: added both the loop back-edge falloff case (`[[2, 1], [], [0]]`, `out_pcs[2] = {0, END}`) and the non-loop dominated-target falloff case (`[[2], [], [1]]`, `out_pcs[2] = {1, END}`). - `tests/semantic/test_structured_pc_unwrap.py`: end-to-end semantic execution test covering your exact 9-instruction loop shader across `(s4=1, s5=0)`, `(s4=0, s5=1)`, and `(s4=0, s5=0)` (matching `--dispatch-loop` and ISA — `(0, 0)` performs no store), the non-loop variant in both wave32 structured and `--wave64-emulate` structured, and the post-dominating join `s_barrier` unwrap case. Verified locally on `b630a9b1e`: - `tests/test_cfg_structure.py` (18,228 graphs + unit tests): pass - `tests/semantic/test_structured_pc_unwrap.py`: 3/3 pass - `tests/semantic/test_wave64_emulate_opt.py`: 31/31 pass - `tests/test_translate.py`: 675/675 pass - `tests/test_runner_calls_all_tests.py` (3/3), `tests/test_ci_registration.py` (15/15), `tests/test_text_write_line_endings.py` (3/3), `scripts/check_no_game_data.py`: pass - `scripts/run_gpu_tests.py --filter ms_wave_info_s3` (`spirv-val` + `LEONIDA_VK_VALIDATION=1`): `4096/4096 pixels exact` on both AMD Ryzen 5 7600 RDNA2 iGPU (`24.10.24`, `LEONIDA_PC_TAIL_UBO=1`) and NVIDIA RTX 4070 SUPER (`610.88`).
Author
Owner

@⁠towade on GitHub · 2026-11-24 22:42 UTC · original · edited 2026-11-24 22:45 UTC

checked: the corpus before/after count asked for above, native path, no --wave64-emulate. Before = 3aa975421 (this branch's merge base with main, not 47c651174), after = b630a9b1e. Windows 11 26300, 14 shards of rdna2spirv.py --no-asm plus --merge, SPIRV-Tools from Vulkan SDK 1.4.363.0. CPU only, no GPU run. Counts only.

before 3aa975421 after b630a9b1e
translated 26,503 26,503
pass spirv-val 26,503 26,503
control_flow: structured / straight line / barrier rounds / dispatch loop 23,213 / 3,249 / 39 / 2 23,249 / 3,249 / 3 / 2
modules with different .spv bytes 22,071 (9,282 mesh, 9,117 pixel, 2,468 compute, 1,204 vertex)
smaller / larger 22,052 / 19 (total bytes −0.55 %)
index entries that differ 36, only control_flow (and lds_bytes in 34 of them)

So nothing is lost or newly rejected. The change touches most of the structured modules. The 36 modules that move from barrier rounds to structured are all compute shaders.

What the bytes change is (opcode counts, not a GPU check):

  • In a random sample of 300 changed modules, 299 have fewer OpBranchConditional. In total there are 113,289 → 102,936 OpBranchConditional and 111,864 → 101,510 OpSelectionMerge, about 9 % fewer. Loads also drop slightly (−0.8 %). OpLoopMerge and OpSelect stay the same, which is what removing the guards should look like.
  • The 19 larger modules are all among the 36 barrier rounds → structured compute shaders. They have more OpLoopMerge and barriers than the rounds lowering produced.
  • Barriers: across the 11,750 changed modules that are compute or mesh or use LDS, 16 have more OpControlBarrier after and 9 have fewer (2 → 1). All 9 are among the 36 barrier rounds → structured modules, and each of their listings contains exactly one s_barrier. The rounds lowering emitted it twice, the structured path emits it once. I don't see a lost barrier in that.

The falloff case from the review above. I checked the corpus for it: all 26,503 listings end in s_endpgm (last instruction of each listing), so no game shader ends in a conditional branch with nothing after it. The fix in b630a9b1e therefore shouldn't change any corpus module through that edge directly; clones from split_irreducible I didn't look for.

Not checked: the corpus with --wave64-emulate, running any of the changed modules on a GPU, and the pipeline-library, FSPX and BVH sets.

AI-assisted (Claude Code).

**@⁠towade** on GitHub · 2026-11-24 22:42 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/808#issuecomment-7146542462) · edited 2026-11-24 22:45 UTC checked: the corpus before/after count asked for above, native path, no `--wave64-emulate`. Before = `3aa975421` (this branch's merge base with `main`, not `47c651174`), after = `b630a9b1e`. Windows 11 26300, 14 shards of `rdna2spirv.py --no-asm` plus `--merge`, SPIRV-Tools from Vulkan SDK 1.4.363.0. CPU only, no GPU run. Counts only. | | before `3aa975421` | after `b630a9b1e` | |---|---|---| | translated | 26,503 | 26,503 | | pass `spirv-val` | 26,503 | 26,503 | | `control_flow`: structured / straight line / barrier rounds / dispatch loop | 23,213 / 3,249 / 39 / 2 | 23,249 / 3,249 / **3** / 2 | | modules with different `.spv` bytes | | **22,071** (9,282 mesh, 9,117 pixel, 2,468 compute, 1,204 vertex) | | smaller / larger | | 22,052 / 19 (total bytes −0.55 %) | | index entries that differ | | 36, only `control_flow` (and `lds_bytes` in 34 of them) | So nothing is lost or newly rejected. The change touches most of the structured modules. The 36 modules that move from `barrier rounds` to `structured` are all compute shaders. **What the bytes change is** (opcode counts, not a GPU check): - In a random sample of 300 changed modules, 299 have fewer `OpBranchConditional`. In total there are 113,289 → 102,936 `OpBranchConditional` and 111,864 → 101,510 `OpSelectionMerge`, about 9 % fewer. Loads also drop slightly (−0.8 %). `OpLoopMerge` and `OpSelect` stay the same, which is what removing the guards should look like. - The 19 larger modules are all among the 36 `barrier rounds` → `structured` compute shaders. They have more `OpLoopMerge` and barriers than the rounds lowering produced. - Barriers: across the 11,750 changed modules that are compute or mesh or use LDS, 16 have more `OpControlBarrier` after and 9 have fewer (2 → 1). All 9 are among the 36 `barrier rounds` → `structured` modules, and each of their listings contains exactly one `s_barrier`. The rounds lowering emitted it twice, the structured path emits it once. I don't see a lost barrier in that. **The falloff case from the review above.** I checked the corpus for it: all 26,503 listings end in `s_endpgm` (last instruction of each listing), so no game shader ends in a conditional branch with nothing after it. The fix in `b630a9b1e` therefore shouldn't change any corpus module through that edge directly; clones from `split_irreducible` I didn't look for. Not checked: the corpus with `--wave64-emulate`, running any of the changed modules on a GPU, and the pipeline-library, FSPX and BVH sets. AI-assisted (Claude Code).
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-25 07:54 UTC · original

Rechecked at 8e5cf5aef, merged onto main 42537e7b6 (CPU) and run on the RX 7900 XT (GPU, below).

The fix works. My earlier repro now matches the ISA in all three cases, structured and --dispatch-loop. The structured output has the same control flow as the in-memory patch I tried, with guard 1 kept. tests/semantic/test_structured_pc_unwrap.py passes 3/3, tests/test_cfg_structure.py passes (18,228 graphs), and tests/semantic/test_wave64_emulate_opt.py is OK.

The plan_structured_emulation half of the fix also matters. Without exec folds, this wave64 shader runs wrongly on main and correctly with this PR:

s_mov_b64 s[10:11], exec          ; makes it a wave64 shader
s_cbranch_execz skip
s_branch last
X:    v_lshlrev_b32_e32 v2, 2, v0
      v_mov_b32_e32 v1, 0x3333
      buffer_store_dword v1, v2, s[0:3], 0 offen
      s_branch skip
skip: v_lshlrev_b32_e32 v2, 2, v0
      v_mov_b32_e32 v1, 0x4444
      buffer_store_dword v1, v2, s[0:3], 0 offen offset:256
      s_endpgm
last: s_cmp_eq_u32 s4, 1
      s_cbranch_scc1 X              ; last instruction, nothing after it

With s4 = 0, the branch is not taken and the program ends, so nothing is stored. In --wave64-emulate structured form with find_local_exec_folds disabled, main stores 0x3333 and 0x4444 in all 64 lanes, and this PR stores nothing.

One path the fix does not reach yet: exec-fold regions

With exec folds on, as they are by default, the same shader still stores 0x3333 and 0x4444 with s4 = 0, on both main and this PR, in the structured and dispatch emulate forms. find_local_exec_folds returns {0: run 1, skip 3, region [1, 2, 4]}, so the region contains the last block. A variant whose last block branches straight to skip (last: s_cmp_eq_u32 s4, 1; s_cbranch_scc1 skip) wrongly stores 0x4444 with s4 = 0.

s4 = 0, 64 lanes, --wave64-emulate main 42537e7b6 this PR
folds on (default), structured or dispatch stores 0x3333 and 0x4444 stores 0x3333 and 0x4444
folds off (find_local_exec_folds returns none), structured stores 0x3333 and 0x4444 no store (correct)
folds off, dispatch no store no store

This is the same missing END edge, in tools/wave64_emulate_opt.py's fold path, and it predates this PR:

  • find_local_exec_folds works only from succ, which has no END edge for the last block, so _postdominators(succ) can accept skip as its post-dominator.
  • The region walk only rejects blocks with no successors, so the last block (succ = [tgt]) gets into the region.
  • sub_succ is built from succ, and cfg_structure.structure(sub_succ) is still called without out_pcs.
  • After the region, the fold head stores %pc = skip, so "the program is over" becomes "continue at skip".

Passing out_pcs there too, and refusing a fold whose region contains a block that can write END, would probably cover it. As with the original case, every listing in the repository ends in s_endpgm, so the corpus is unlikely to hit it. It fits in this PR or in a follow-up; your call.

The repro drives test_loop_ballot_barrier._translate_cs(..., threads=64, emulate=True) and run_compute_workgroup, with find_local_exec_folds wrapped to switch folds off. No game data.

GPU, RX 7900 XT

AMD Radeon RX 7900 XT (RDNA3, gfx1100), AMD driver 32.0.32029.2010 (26.11.2), Windows 11 build 26300, Vulkan SDK 1.4.363.0, llvm-mc 23.1.5. Test programs come from a main 42537e7b6 build; this PR changes no C++.

main 42537e7b6 this PR merged onto it
scripts/run_gpu_tests.py, whole suite 244/263 passed, 18 skipped, 1 failed 244/263 passed, 18 skipped, 1 failed, the same status for every fixture
--filter ms_wave_info_s3, native, 3 runs PASS 3/3 (4096/4096 exact)
--filter ms_wave_info_s3, LEONIDA_W64_EMULATE=1 PASS

The one failure is cs_imgatomic_ext on both, the fixture problem #1518 fixes. No regression on RDNA3.

No game files.

**@⁠adrianmjim** on GitHub · 2026-11-25 07:54 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/808#issuecomment-7156379744) Rechecked at `8e5cf5aef`, merged onto `main` `42537e7b6` (CPU) and run on the RX 7900 XT (GPU, below). **The fix works.** My earlier repro now matches the ISA in all three cases, structured and `--dispatch-loop`. The structured output has the same control flow as the in-memory patch I tried, with `guard 1` kept. `tests/semantic/test_structured_pc_unwrap.py` passes 3/3, `tests/test_cfg_structure.py` passes (18,228 graphs), and `tests/semantic/test_wave64_emulate_opt.py` is OK. The `plan_structured_emulation` half of the fix also matters. Without exec folds, this wave64 shader runs wrongly on `main` and correctly with this PR: ``` s_mov_b64 s[10:11], exec ; makes it a wave64 shader s_cbranch_execz skip s_branch last X: v_lshlrev_b32_e32 v2, 2, v0 v_mov_b32_e32 v1, 0x3333 buffer_store_dword v1, v2, s[0:3], 0 offen s_branch skip skip: v_lshlrev_b32_e32 v2, 2, v0 v_mov_b32_e32 v1, 0x4444 buffer_store_dword v1, v2, s[0:3], 0 offen offset:256 s_endpgm last: s_cmp_eq_u32 s4, 1 s_cbranch_scc1 X ; last instruction, nothing after it ``` With `s4 = 0`, the branch is not taken and the program ends, so nothing is stored. In `--wave64-emulate` structured form with `find_local_exec_folds` disabled, `main` stores 0x3333 and 0x4444 in all 64 lanes, and this PR stores nothing. ### One path the fix does not reach yet: exec-fold regions With exec folds on, as they are by default, the same shader still stores 0x3333 and 0x4444 with `s4 = 0`, on both `main` and this PR, in the structured and dispatch emulate forms. `find_local_exec_folds` returns `{0: run 1, skip 3, region [1, 2, 4]}`, so the region contains the last block. A variant whose last block branches straight to `skip` (`last: s_cmp_eq_u32 s4, 1; s_cbranch_scc1 skip`) wrongly stores 0x4444 with `s4 = 0`. | `s4 = 0`, 64 lanes, `--wave64-emulate` | `main` `42537e7b6` | this PR | |---|---|---| | folds on (default), structured or dispatch | stores 0x3333 and 0x4444 | stores 0x3333 and 0x4444 | | folds off (`find_local_exec_folds` returns none), structured | stores 0x3333 and 0x4444 | **no store** (correct) | | folds off, dispatch | no store | no store | This is the same missing `END` edge, in `tools/wave64_emulate_opt.py`'s fold path, and it predates this PR: - `find_local_exec_folds` works only from `succ`, which has no `END` edge for the last block, so `_postdominators(succ)` can accept `skip` as its post-dominator. - The region walk only rejects blocks with no successors, so the last block (`succ = [tgt]`) gets into the region. - `sub_succ` is built from `succ`, and `cfg_structure.structure(sub_succ)` is still called without `out_pcs`. - After the region, the fold head stores `%pc = skip`, so "the program is over" becomes "continue at skip". Passing `out_pcs` there too, and refusing a fold whose region contains a block that can write `END`, would probably cover it. As with the original case, every listing in the repository ends in `s_endpgm`, so the corpus is unlikely to hit it. It fits in this PR or in a follow-up; your call. The repro drives `test_loop_ballot_barrier._translate_cs(..., threads=64, emulate=True)` and `run_compute_workgroup`, with `find_local_exec_folds` wrapped to switch folds off. No game data. ### GPU, RX 7900 XT AMD Radeon RX 7900 XT (RDNA3, gfx1100), AMD driver 32.0.32029.2010 (26.11.2), Windows 11 build 26300, Vulkan SDK 1.4.363.0, llvm-mc 23.1.5. Test programs come from a `main` `42537e7b6` build; this PR changes no C++. | | `main` `42537e7b6` | this PR merged onto it | |---|---|---| | `scripts/run_gpu_tests.py`, whole suite | 244/263 passed, 18 skipped, 1 failed | 244/263 passed, 18 skipped, 1 failed, the same status for every fixture | | `--filter ms_wave_info_s3`, native, 3 runs | | PASS 3/3 (4096/4096 exact) | | `--filter ms_wave_info_s3`, `LEONIDA_W64_EMULATE=1` | | PASS | The one failure is `cs_imgatomic_ext` on both, the fixture problem #1518 fixes. No regression on RDNA3. No game files.
Author
Owner

@⁠whoalin1 on GitHub · 2026-11-25 08:06 UTC · original

Thanks again for the thorough recheck and full RX 7900 XT pass, @⁠adrianmjim!

Since the exec-fold sibling fix was only two lines, I folded it into this PR as 33431d6fe:

  • tools/wave64_emulate_opt.py (find_local_exec_folds): reject any block u in the region walk where len(raw_edges[u]) == 1 and terms[u][0] == br and terms[u][1] != s_branch (matching the existing not succ[u] guard), so a region that can fall off the end to END is never folded into a region whose exit unconditionally sets %pc = skip.
  • tests/semantic/test_structured_pc_unwrap.py: added test_wave64_emulate_exec_fold_refuses_last_block_cbranch_falloff covering both of your variants (s_cbranch_scc1 X and s_cbranch_scc1 skip) with default folds enabled in both structured and --dispatch-loop --wave64-emulate modes (s4=0 -> no store, s4=1 -> expected stores).

Verified locally on 33431d6fe:

  • tests/semantic/test_structured_pc_unwrap.py: 4/4 pass
  • tests/semantic/test_wave64_emulate_opt.py: 31/31 pass
  • tests/test_cfg_structure.py: 18,228 graphs + unit tests pass
  • tests/test_runner_calls_all_tests.py (3/3), tests/test_ci_registration.py (16/16), scripts/check_no_game_data.py: pass.
**@⁠whoalin1** on GitHub · 2026-11-25 08:06 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/808#issuecomment-7156624606) Thanks again for the thorough recheck and full RX 7900 XT pass, @⁠adrianmjim! Since the exec-fold sibling fix was only two lines, I folded it into this PR as `33431d6fe`: - **`tools/wave64_emulate_opt.py` (`find_local_exec_folds`):** reject any block `u` in the region walk where `len(raw_edges[u]) == 1 and terms[u][0] == br and terms[u][1] != s_branch` (matching the existing `not succ[u]` guard), so a region that can fall off the end to `END` is never folded into a region whose exit unconditionally sets `%pc = skip`. - **`tests/semantic/test_structured_pc_unwrap.py`:** added `test_wave64_emulate_exec_fold_refuses_last_block_cbranch_falloff` covering both of your variants (`s_cbranch_scc1 X` and `s_cbranch_scc1 skip`) with default folds enabled in both structured and `--dispatch-loop` `--wave64-emulate` modes (`s4=0` -> no store, `s4=1` -> expected stores). Verified locally on `33431d6fe`: - `tests/semantic/test_structured_pc_unwrap.py`: 4/4 pass - `tests/semantic/test_wave64_emulate_opt.py`: 31/31 pass - `tests/test_cfg_structure.py`: 18,228 graphs + unit tests pass - `tests/test_runner_calls_all_tests.py` (3/3), `tests/test_ci_registration.py` (16/16), `scripts/check_no_game_data.py`: pass.
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-25 12:48 UTC · original

Rechecked at 33431d6fe (CPU, the tests/semantic interpreter, Windows 11; no game files). The fold gap is closed.

Both wave64 shaders from my last comment, the "dominated X" form and the "to skip" form, under --wave64-emulate, with folds on as by default:

find_local_exec_folds s4 = 0 s4 = 1
ISA no store the program's stores
8e5cf5aef (before) {0: run 1, skip 3, region [1, 2, 4]} stores 0x3333 and 0x4444 stores
33431d6fe {}, the region is refused no store stores 0x3333/0x4444 as the ISA does

That holds in the structured and dispatch emulate forms, and for the "to skip" variant: s4 = 0 no store, s4 = 1 0x4444. The original non-emulated END repro still matches the ISA in all three cases. test_structured_pc_unwrap.py and test_wave64_emulate_opt.py pass.

Thanks for picking that up.

**@⁠adrianmjim** on GitHub · 2026-11-25 12:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/808#issuecomment-7162890468) Rechecked at `33431d6fe` (CPU, the `tests/semantic` interpreter, Windows 11; no game files). The fold gap is closed. Both wave64 shaders from my last comment, the "dominated X" form and the "to skip" form, under `--wave64-emulate`, with folds on as by default: | | `find_local_exec_folds` | `s4 = 0` | `s4 = 1` | |---|---|---|---| | ISA | | no store | the program's stores | | `8e5cf5aef` (before) | `{0: run 1, skip 3, region [1, 2, 4]}` | **stores 0x3333 and 0x4444** | stores | | `33431d6fe` | `{}`, the region is refused | **no store** | stores 0x3333/0x4444 as the ISA does | That holds in the structured and dispatch emulate forms, and for the "to skip" variant: `s4 = 0` no store, `s4 = 1` 0x4444. The original non-emulated END repro still matches the ISA in all three cases. `test_structured_pc_unwrap.py` and `test_wave64_emulate_opt.py` pass. Thanks for picking that up.
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-25 16:46 UTC · original

Merged. Thank you, @⁠whoalin1, and thanks to @⁠adrianmjim for the fall-off bug, and to @⁠qhukz13 and @⁠towade for the corpus counts.

  • Translator suite: 6,292 tests, 0 failed on main + this branch. The new test_structured_pc_unwrap fails on main (5 failures, 2 errors) and passes here. test_cfg_structure (18,228 graphs) and test_wave64_emulate_opt pass.
  • Windows suite under Wine (llvmpipe and an RDNA2 iGPU): nothing goes from pass to fail.
  • CI's "GPU tests on llvmpipe" step: this run's linux-gpu-tools job failed, and GitHub hasn't released its logs yet. I ran the same step (scripts/run_gpu_tests.py, native build, llvmpipe) on main and on main + this branch: the results are identical, 176/264 pass on both, and the same one test fails on both (cs_imgatomic_ext, which already fails on main). So this change adds no GPU-test failure.
  • Optional follow-up: the description's Verification bullets are empty, and structure() now takes out_pcs.
  • Checked on main 9e7720c9e + 33431d6fe: the semantic suite passes (490 files, 6,292 tests, 0 files failed); test_cfg_structure, test_translate (pytest; 2 failing as on main) pass; the 14 docs and writer tests pass; check_no_game_data.py is clean on the 7 changed files.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠aslan2142** on GitHub · 2026-11-25 16:46 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/808#issuecomment-7168513710) Merged. Thank you, @⁠whoalin1, and thanks to @⁠adrianmjim for the fall-off bug, and to @⁠qhukz13 and @⁠towade for the corpus counts. - **Translator suite:** 6,292 tests, 0 failed on `main` + this branch. The new `test_structured_pc_unwrap` fails on `main` (5 failures, 2 errors) and passes here. `test_cfg_structure` (18,228 graphs) and `test_wave64_emulate_opt` pass. - **Windows suite under Wine** (llvmpipe and an RDNA2 iGPU): nothing goes from pass to fail. - **CI's "GPU tests on llvmpipe" step:** this run's `linux-gpu-tools` job failed, and GitHub hasn't released its logs yet. I ran the same step (`scripts/run_gpu_tests.py`, native build, llvmpipe) on `main` and on `main` + this branch: the results are identical, 176/264 pass on both, and the same one test fails on both (`cs_imgatomic_ext`, which already fails on `main`). So this change adds no GPU-test failure. - **Optional follow-up:** the description's Verification bullets are empty, and `structure()` now takes `out_pcs`. - **Checked on `main` `9e7720c9e` + `33431d6fe`:** the semantic suite passes (490 files, 6,292 tests, 0 files failed); `test_cfg_structure`, `test_translate (pytest; 2 failing as on main)` pass; the 14 docs and writer tests pass; `check_no_game_data.py` is clean on the 7 changed files. AI-assisted (Claude Code, Claude Opus 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.