wave64_emulate_opt: fold a region at most one lane of the wave runs (SHADER-W64-ONE-LANE-FOLD) #2447

Open
github-bot wants to merge 3 commits from github-bot/gh-2447 into main AGit
Owner

@⁠DadaCap on GitHub · 2026-11-27 10:17 UTC · original
DadaCap:w64-one-lane-fold → main · +278 −13 · 4 files · 3 commits · open

Claimed in the coordination issue as SHADER-W64-ONE-LANE-FOLD (jointly with @⁠OreoFR): #1608 (narrowed from SHADER-W64-UNIFORM-EXECZ-BARRIER).

Depends on: none (#2102, which it was built on, is merged).

What and why

Under --wave64-emulate, a compute program that picks one thread for a tail (clear LDS, then after a barrier sum it up and store) does it with v_cmpx_eq_u32 c, v0 or v_cmp_eq_u32 m, c, v0 + s_and_saveexec_b64 _, m. Such an s_cbranch_execz region could not be folded: it has several stores, sometimes a loop, and v_readfirstlane_b32 inside, so it went through the emulated rounds. On one 1,024-thread library program this takes the round switch from 639 cases to 33 (spirv-val valid). A program it changes, lib_cs_00962, was the one active in 3 of 4 compute TDR timeouts @⁠OreoFR saw on NVIDIA.

A region whose EXEC holds at most one lane of the workgroup can be folded: no other lane exists to order against, and a v_readfirstlane_b32 in it reads that lane's own value.

The rule

_one_lane_in (tools/wave64_emulate_opt.py) is a forward must-analysis that proves EXEC holds at most one lane, intersected at joins:

  • Seed: v_cmp(x)_eq_{i,u}32 <constant>, v0, only while v0 (LocalInvocationId.x) has not been written since entry, and only when no two lanes of a wave can share v0 (a one-row group, or rows of whole waves). Compares with modifiers or SDWA are excluded.
  • Kept through: s_mov/s_and/s_andn2_b64 of that mask, saveexecs and later compares.
  • Killed by: any write to that SGPR pair or VCC (a wave32-printed vcc_lo destination counts), any VGPR write to v0 (movrel, swaps, memory results), and any EXEC write that is not a recognised narrowing.
  • In a folded one-lane region: no x_order_barrier between its accesses, and v_readfirstlane_b32 copies the lane's own value when the region writes no EXEC. Scalar and VCC results of the region must already be dead at its end. The memory kinds it touches still go into x_dirty, so the next conflicting access after the fold still splits.
  • The constant lane may be in the upper half (32-63): masks are per-lane bits, and each invocation runs the fold behind its own %active.

The contract row is in docs/design.md.

Tested

  • tests/semantic/test_wave64_one_lane_fold.py (hand-written programs, 128 threads in two waves, no game data): the workgroup interpreter of test_wave64_emulate_opt.py, both invocation orders, compared with the plain round emulation; the constant picks a lane of wave 0, of wave 1, or none, through v_cmpx_eq_u32 and through v_cmp_eq_u32 + s_and_saveexec_b64. Negatives (a compare against another VGPR than the thread id, an overwritten v0, a compare that is not an equality, rows of 32 threads) must not take the one-lane path and must still match.
  • Whole translator suite, python -I scripts/run_semantic_tests.py tests/semantic: 501 files, 6,377 tests, 0 failed (351 s, 24 jobs, Windows 11).
  • Library corpus (2,684 listings, --no-asm --wave64-emulate, 8 shards, spirv-val on), main against this branch: 2,479 translated and spirv-val-valid both times, the same set; exactly 20 modules change, the other 2,459 are byte-identical.
  • Equivalence on the 20 library programs @⁠OreoFR swapped (local CPU differential, not in the PR): all 20 translate and pass spirv-val on both; 36 one-lane folds in total. A lockstep interpreter run at each program's real workgroup size, comparing memory writes, image texels and LDS between main and this branch, found no divergence in any completed run; 16 of the 20 programs reached a fold (the rest rely on the static argument).
  • Retail measurements by others on the same library (thanks!):
    • @⁠OreoFR, RTX 4070 Laptop 8 GB, NVIDIA 621.40, TdrDelay=60, GPU timestamps around each dispatch: lib_cs_00962 1,412 ms mean / 1,754 ms max over 12 dispatches → 212 ms / 231 ms over 9 (about 6.7x). Before this library, lib_cs_00962 was the program active in 3 of his 4 device losses (Aftermath timeout). With it: 6 runs, 0 device lost, 0 early crash, the world drawn in 4 of them: #1608 and #1608
    • @⁠quartz-heron-58, RTX 4080 16 GB, NVIDIA 621.24: 53.1 ms → 3.2 ms per dispatch (about 17x), same number of dispatches, no device loss or exception in either 630 s run: #1608
    • Both are n=1 per library; picture correctness was not compared.
  • python -I scripts/check_no_game_data.py on the changed files: clean.

GPUs tested: the measurements above (RTX 4070 Laptop, RTX 4080); my own runs on an RTX 5080 (621.24) used a library with this change. No GPU fixture is added: the rule is checked on the CPU interpreter against the round emulation.

AI-assisted: yes (Claude Code, Opus 5.5)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr

**@⁠DadaCap** on GitHub · 2026-11-27 10:17 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2447) `DadaCap:w64-one-lane-fold` → `main` · +278 −13 · 4 files · 3 commits · open Claimed in the coordination issue as SHADER-W64-ONE-LANE-FOLD (jointly with @⁠OreoFR): https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 (narrowed from SHADER-W64-UNIFORM-EXECZ-BARRIER). Depends on: none (#2102, which it was built on, is merged). ### What and why Under `--wave64-emulate`, a compute program that picks one thread for a tail (clear LDS, then after a barrier sum it up and store) does it with `v_cmpx_eq_u32 c, v0` or `v_cmp_eq_u32 m, c, v0` + `s_and_saveexec_b64 _, m`. Such an `s_cbranch_execz` region could not be folded: it has several stores, sometimes a loop, and `v_readfirstlane_b32` inside, so it went through the emulated rounds. On one 1,024-thread library program this takes the round switch from 639 cases to 33 (`spirv-val` valid). A program it changes, `lib_cs_00962`, was the one active in 3 of 4 compute TDR timeouts @⁠OreoFR saw on NVIDIA. A region whose EXEC holds at most one lane of the workgroup can be folded: no other lane exists to order against, and a `v_readfirstlane_b32` in it reads that lane's own value. ### The rule `_one_lane_in` (`tools/wave64_emulate_opt.py`) is a forward must-analysis that proves EXEC holds at most one lane, intersected at joins: - **Seed:** `v_cmp(x)_eq_{i,u}32 <constant>, v0`, only while `v0` (LocalInvocationId.x) has not been written since entry, and only when no two lanes of a wave can share `v0` (a one-row group, or rows of whole waves). Compares with modifiers or SDWA are excluded. - **Kept through:** `s_mov`/`s_and`/`s_andn2_b64` of that mask, saveexecs and later compares. - **Killed by:** any write to that SGPR pair or VCC (a wave32-printed `vcc_lo` destination counts), any VGPR write to `v0` (movrel, swaps, memory results), and any EXEC write that is not a recognised narrowing. - **In a folded one-lane region:** no `x_order_barrier` between its accesses, and `v_readfirstlane_b32` copies the lane's own value when the region writes no EXEC. Scalar and VCC results of the region must already be dead at its end. The memory kinds it touches still go into `x_dirty`, so the next conflicting access after the fold still splits. - The constant lane may be in the upper half (32-63): masks are per-lane bits, and each invocation runs the fold behind its own `%active`. The contract row is in `docs/design.md`. ### Tested - `tests/semantic/test_wave64_one_lane_fold.py` (hand-written programs, 128 threads in two waves, no game data): the workgroup interpreter of `test_wave64_emulate_opt.py`, both invocation orders, compared with the plain round emulation; the constant picks a lane of wave 0, of wave 1, or none, through `v_cmpx_eq_u32` and through `v_cmp_eq_u32` + `s_and_saveexec_b64`. Negatives (a compare against another VGPR than the thread id, an overwritten `v0`, a compare that is not an equality, rows of 32 threads) must not take the one-lane path and must still match. - Whole translator suite, `python -I scripts/run_semantic_tests.py tests/semantic`: 501 files, 6,377 tests, 0 failed (351 s, 24 jobs, Windows 11). - Library corpus (2,684 listings, `--no-asm --wave64-emulate`, 8 shards, spirv-val on), `main` against this branch: **2,479 translated and `spirv-val`-valid both times, the same set**; exactly 20 modules change, the other 2,459 are byte-identical. - Equivalence on the 20 library programs @⁠OreoFR swapped (local CPU differential, not in the PR): all 20 translate and pass `spirv-val` on both; 36 one-lane folds in total. A lockstep interpreter run at each program's real workgroup size, comparing memory writes, image texels and LDS between `main` and this branch, found no divergence in any completed run; 16 of the 20 programs reached a fold (the rest rely on the static argument). - Retail measurements by others on the same library (thanks!): - @⁠OreoFR, RTX 4070 Laptop 8 GB, NVIDIA 621.40, `TdrDelay=60`, GPU timestamps around each dispatch: `lib_cs_00962` **1,412 ms mean / 1,754 ms max over 12 dispatches → 212 ms / 231 ms over 9** (about 6.7x). Before this library, `lib_cs_00962` was the program active in 3 of his 4 device losses (Aftermath timeout). With it: **6 runs, 0 device lost, 0 early crash**, the world drawn in 4 of them: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 and https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 - @⁠quartz-heron-58, RTX 4080 16 GB, NVIDIA 621.24: **53.1 ms → 3.2 ms** per dispatch (about 17x), same number of dispatches, no device loss or exception in either 630 s run: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 - Both are `n=1` per library; picture correctness was not compared. - `python -I scripts/check_no_game_data.py` on the changed files: clean. GPUs tested: the measurements above (RTX 4070 Laptop, RTX 4080); my own runs on an RTX 5080 (621.24) used a library with this change. No GPU fixture is added: the rule is checked on the CPU interpreter against the round emulation. AI-assisted: yes (Claude Code, Opus 5.5) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr
A forward must-analysis proves EXEC at most one lane after v_cmp(x)_eq_{i,u}32 <constant>, v0 (v0 the thread id,
not written since entry; the group one row of x or rows of whole waves) and through copies, ANDs and saveexecs of
that mask. Such an s_cbranch_execz region folds with several stores and in a cycle, orders no accesses inside, and
when it writes no EXEC its v_readfirstlane_b32 is the lane's own value. One 1024-thread library compute program:
639 -> 33 OpSwitch cases, spirv-val valid.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Gt9hwRYcpySx4P4ncQ3wD5
# Conflicts:
#	tools/rdna2spirv.py
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 10:34:50 +00:00
Author
Owner

@⁠OreoFR on GitHub · 2026-11-27 10:18 UTC · original

Retail test of this exact head (7ccbbd876): the 20 library modules this rule changes, swapped into an otherwise current pack. RTX 4070 Laptop 8 GB (profile M), NVIDIA 621.40, Windows 11, TdrDelay=60.

RUN onelane3 vs std3 | 2026-11-27 | OreoFR | AI-assisted: Claude Code (Claude Opus 5.5)
BASE:      main d1dff7949 + #2412 #2418 #2260 #2020 #2166 #1851 #2432 (+ #2440 in later runs) | shaders: current pack, library A = current, B = this PR
MACHINE:   RTX 4070 Laptop 8 GB | NVIDIA 621.40 | Windows 11 | 40 GB RAM
RECIPE:    DEVIATIONS: ASYNC_PIPELINES=1, VALUE_WAIT_TIMEOUT_MS=30000, MAP_READBACK=1, HOST_IMAGE_COPY=0, DEVICE_TYPE=6, SERIAL_BIG_COMPILES=0, PIPELINE_PREWARM_ORDER=1, LEONIDA_DEBUG_TIME_SHADER=lib_cs_00962 (local timer)
INSTRUMENTS: GPU timestamps around each dispatch of one program, memory probe; no Aftermath
TARGET:    lib_cs_00962 GPU time per dispatch
MEASURED:  A 1,412 ms mean / 1,754 max (12 dispatches) | B 212 ms mean / 231 max (9 dispatches)
END:       A time limit 900 s | B memory guard at 640 s
CACHES:    warm | RUN 1 of 1 each
  • About 6.7x less GPU time per dispatch for lib_cs_00962. In earlier runs it was the program active in 3 of 4 compute timeouts.
  • 6 runs with this library, all on newer main + #2418: no device loss, and no early crash (the 0xffffffffffffffff read I saw once with an older runtime didn't come back). With #2440, the world draws in these runs.

Caveats:

  • The timing is n=1 per side.
  • The baseline run didn't lose the device either, so this run shows speed, not survival.
  • Every run had the other open PRs listed in BASE.

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

**@⁠OreoFR** on GitHub · 2026-11-27 10:18 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2447#issuecomment-7215734400) Retail test of this exact head (7ccbbd876): the 20 library modules this rule changes, swapped into an otherwise current pack. RTX 4070 Laptop 8 GB (profile M), NVIDIA 621.40, Windows 11, `TdrDelay=60`. ``` RUN onelane3 vs std3 | 2026-11-27 | OreoFR | AI-assisted: Claude Code (Claude Opus 5.5) BASE: main d1dff7949 + #2412 #2418 #2260 #2020 #2166 #1851 #2432 (+ #2440 in later runs) | shaders: current pack, library A = current, B = this PR MACHINE: RTX 4070 Laptop 8 GB | NVIDIA 621.40 | Windows 11 | 40 GB RAM RECIPE: DEVIATIONS: ASYNC_PIPELINES=1, VALUE_WAIT_TIMEOUT_MS=30000, MAP_READBACK=1, HOST_IMAGE_COPY=0, DEVICE_TYPE=6, SERIAL_BIG_COMPILES=0, PIPELINE_PREWARM_ORDER=1, LEONIDA_DEBUG_TIME_SHADER=lib_cs_00962 (local timer) INSTRUMENTS: GPU timestamps around each dispatch of one program, memory probe; no Aftermath TARGET: lib_cs_00962 GPU time per dispatch MEASURED: A 1,412 ms mean / 1,754 max (12 dispatches) | B 212 ms mean / 231 max (9 dispatches) END: A time limit 900 s | B memory guard at 640 s CACHES: warm | RUN 1 of 1 each ``` - **About 6.7x less GPU time per dispatch** for `lib_cs_00962`. In earlier runs it was the program active in 3 of 4 compute timeouts. - **6 runs with this library**, all on newer `main` + #2418: no device loss, and no early crash (the `0xffffffffffffffff` read I saw once with an older runtime didn't come back). With #2440, the world draws in these runs. Caveats: - The timing is n=1 per side. - The baseline run didn't lose the device either, so this run shows speed, not survival. - Every run had the other open PRs listed in BASE. AI-assisted (Claude Code, Claude Opus 5.5).
Author
Owner

@⁠zmasi on GitHub · 2026-11-27 11:31 UTC · original · reviewed

Windows x64 / RTX 3090 GPU and semantic check of #2447 (7ccbbd876).

Environment: Windows 11 x64, NVIDIA GeForce RTX 3090 (driver 610.88, Vulkan 1.4.341, compute subgroup 32..32), VK_LAYER_KHRONOS_validation 1.4.363, llvm-mc 21.1.8, SPIRV-Tools 1.4.363.1.

Code & invariant check

  • Entry tid uniqueness (tools/rdna2spirv.py:18873): assemble() unconditionally initializes %vgpr[0] (v0) to LocalInvocationId.x (c = 0 in range(gen.cs["tid"] + 1)). With linear invocation index t = x + sx * y + sx * sy * z and 64-lane waves [64w, 64w + 63], sy * sz == 1 gives x = t, and sx % 64 == 0 keeps y, z, and x // 64 constant within each wave, so v0 is distinct across all 64 lanes of a wave in both cases (and can collide only when sy * sz > 1 and sx % 64 != 0, such as 32 x 4 x 1).
  • Forward must-analysis (_one_lane_step / _one_lane_in in tools/wave64_emulate_opt.py):
    • v_movrel/v_swaprel clear tid, v_swap kills both VGPR operands, and non-v_ VGPR-writing ops kill n + 1 registers so tfe destinations cannot leave v0 live in tid.
    • _TID_EQ rejects mods and _sdwa (which also excludes _dpp, as DPP ops carry modifier tokens and take VGPR sources), requires an integer immediate paired with a single-VGPR tid operand, and kills any tracked scalar pair when either half (3 << k) or VCC (1 << _BIT_VCC) may be written.
    • s_andn2_b64 and s_andn2_saveexec_b64 propagate single-lane status only from src0 (src0 & ~src1 / ssrc & ~exec), while s_and_b64 accepts either source.
  • Memory ordering & v_readfirstlane_b32 (tools/rdna2spirv.py:6139, 8106, 18945):
    • emit_folded_region still emits g.x_order_barrier() in uniform control flow before lbl_do when g.x_dirty conflicts with fd["mem_reads"] / fd["mem_writes"], and x_memory_order continues recording (kind, "r"/"w") in g.x_dirty inside the fold so subsequent wave accesses after lbl_end remain ordered.
    • v_readfirstlane_b32 is permitted only when one_lane_in[run] holds and no block in region writes EXEC (any(blk_info[u][3] for u in region) is false); because lbl_do executes only when %active is true, EXEC contains solely the executing lane throughout the fold, making self.dst(a[0], self.src_u(a[1])) exact for both SGPR and vcc_lo/vcc_hi destinations.

GPU verification on RTX 3090 (subgroup 32, VK_LAYER_KHRONOS_validation)

Adapted the 128-thread two-wave synthetic program from tests/semantic/test_wave64_one_lane_fold.py (3 LDS writes including two to one address, s_barrier, LDS readback, v_readfirstlane_b32 into s12 and vcc_lo, and two buffer stores to s[0:3] offset 0, plus lane 127's control store to offset 4) and ran all four cases through spirv-as, spirv-val --target-env vulkan1.2, and vk_compute_test <spv> 1 dump under VK_LAYER_KHRONOS_validation:

  • v_cmpx_eq_u32_e32 0, v0 (wave 0 lane 0, 2 one_lane folds): out[0..1] = 25, 134 — PASS (0 validation errors).
  • v_cmpx_eq_u32_e32 65, v0 (wave 1 lane 1, 2 one_lane folds): out[0..1] = 90, 134 — PASS (0 validation errors).
  • v_cmpx_eq_u32_e32 200, v0 (no active lane, 2 one_lane folds): out[0..1] = 0xeeeeeeee (untouched sentinel), 134 — PASS (0 validation errors).
  • v_cmp_eq_u32_e64 s[42:43], 3, v0 + s_and_saveexec_b64 s[44:45], s[42:43] (2 one_lane folds): out[0..1] = 28, 134 — PASS (0 validation errors).

Semantic suites, negative controls & SPIR-V size check

  • python -I tests/semantic/test_wave64_one_lane_fold.py -v: 3/3 OK.
  • python -I tests/semantic/test_wave64_emulate_opt.py: OK.
  • Negative control: dropping and (sy * sz == 1 or sx % 64 == 0) in tools/rdna2spirv.py makes test_not_one_lane_keeps_the_rounds (rows of 32 threads) fail immediately (FAILED (failures=1)), confirming the multi-row wave-sharing guard is pinned. Restored.
  • Assembled SPIR-V size: across two 1.32 MB emulated wave64 compute modules in a local build, assembled SPIR-V drops from 1,323,800 B to 850,816 B (-35.7%, 235 total OpSwitch cases) and 1,320,680 B to 847,712 B (-35.8%, 235 total OpSwitch cases), both spirv-val clean.
  • python -I scripts/check_no_game_data.py on all 4 changed files: clean.

No game data. AI-assisted.

**@⁠zmasi** on GitHub · 2026-11-27 11:31 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2447#pullrequestreview-6087479956) · **reviewed** **Windows x64 / RTX 3090 GPU and semantic check of #2447** (`7ccbbd876`). Environment: Windows 11 x64, NVIDIA GeForce RTX 3090 (driver `610.88`, Vulkan `1.4.341`, compute subgroup `32..32`), `VK_LAYER_KHRONOS_validation` `1.4.363`, `llvm-mc` `21.1.8`, SPIRV-Tools `1.4.363.1`. ### Code & invariant check - **Entry `tid` uniqueness (`tools/rdna2spirv.py:18873`)**: `assemble()` unconditionally initializes `%vgpr[0]` (`v0`) to `LocalInvocationId.x` (`c = 0` in `range(gen.cs["tid"] + 1)`). With linear invocation index `t = x + sx * y + sx * sy * z` and 64-lane waves `[64w, 64w + 63]`, `sy * sz == 1` gives `x = t`, and `sx % 64 == 0` keeps `y`, `z`, and `x // 64` constant within each wave, so `v0` is distinct across all 64 lanes of a wave in both cases (and can collide only when `sy * sz > 1` and `sx % 64 != 0`, such as `32 x 4 x 1`). - **Forward must-analysis (`_one_lane_step` / `_one_lane_in` in `tools/wave64_emulate_opt.py`)**: - `v_movrel`/`v_swaprel` clear `tid`, `v_swap` kills both VGPR operands, and non-`v_` VGPR-writing ops kill `n + 1` registers so `tfe` destinations cannot leave `v0` live in `tid`. - `_TID_EQ` rejects `mods` and `_sdwa` (which also excludes `_dpp`, as DPP ops carry modifier tokens and take VGPR sources), requires an integer immediate paired with a single-VGPR `tid` operand, and kills any tracked scalar pair when either half (`3 << k`) or `VCC` (`1 << _BIT_VCC`) may be written. - `s_andn2_b64` and `s_andn2_saveexec_b64` propagate single-lane status only from `src0` (`src0 & ~src1` / `ssrc & ~exec`), while `s_and_b64` accepts either source. - **Memory ordering & `v_readfirstlane_b32` (`tools/rdna2spirv.py:6139, 8106, 18945`)**: - `emit_folded_region` still emits `g.x_order_barrier()` in uniform control flow before `lbl_do` when `g.x_dirty` conflicts with `fd["mem_reads"]` / `fd["mem_writes"]`, and `x_memory_order` continues recording `(kind, "r"/"w")` in `g.x_dirty` inside the fold so subsequent wave accesses after `lbl_end` remain ordered. - `v_readfirstlane_b32` is permitted only when `one_lane_in[run]` holds and no block in `region` writes `EXEC` (`any(blk_info[u][3] for u in region)` is false); because `lbl_do` executes only when `%active` is true, `EXEC` contains solely the executing lane throughout the fold, making `self.dst(a[0], self.src_u(a[1]))` exact for both SGPR and `vcc_lo`/`vcc_hi` destinations. ### GPU verification on RTX 3090 (subgroup 32, `VK_LAYER_KHRONOS_validation`) Adapted the 128-thread two-wave synthetic program from `tests/semantic/test_wave64_one_lane_fold.py` (3 LDS writes including two to one address, `s_barrier`, LDS readback, `v_readfirstlane_b32` into `s12` and `vcc_lo`, and two buffer stores to `s[0:3]` offset `0`, plus lane `127`'s control store to offset `4`) and ran all four cases through `spirv-as`, `spirv-val --target-env vulkan1.2`, and `vk_compute_test <spv> 1 dump` under `VK_LAYER_KHRONOS_validation`: - `v_cmpx_eq_u32_e32 0, v0` (wave 0 lane 0, `2` `one_lane` folds): `out[0..1] = 25, 134` — **PASS** (`0` validation errors). - `v_cmpx_eq_u32_e32 65, v0` (wave 1 lane 1, `2` `one_lane` folds): `out[0..1] = 90, 134` — **PASS** (`0` validation errors). - `v_cmpx_eq_u32_e32 200, v0` (no active lane, `2` `one_lane` folds): `out[0..1] = 0xeeeeeeee (untouched sentinel), 134` — **PASS** (`0` validation errors). - `v_cmp_eq_u32_e64 s[42:43], 3, v0` + `s_and_saveexec_b64 s[44:45], s[42:43]` (`2` `one_lane` folds): `out[0..1] = 28, 134` — **PASS** (`0` validation errors). ### Semantic suites, negative controls & SPIR-V size check - `python -I tests/semantic/test_wave64_one_lane_fold.py -v`: **3/3 OK**. - `python -I tests/semantic/test_wave64_emulate_opt.py`: **OK**. - **Negative control**: dropping `and (sy * sz == 1 or sx % 64 == 0)` in `tools/rdna2spirv.py` makes `test_not_one_lane_keeps_the_rounds` (`rows of 32 threads`) fail immediately (`FAILED (failures=1)`), confirming the multi-row wave-sharing guard is pinned. Restored. - **Assembled SPIR-V size**: across two `1.32 MB` emulated wave64 compute modules in a local build, assembled SPIR-V drops from `1,323,800 B` to `850,816 B` (`-35.7%`, `235` total `OpSwitch` cases) and `1,320,680 B` to `847,712 B` (`-35.8%`, `235` total `OpSwitch` cases), both `spirv-val` clean. - `python -I scripts/check_no_game_data.py` on all 4 changed files: clean. No game data. AI-assisted.
Author
Owner

@⁠timbot103 on GitHub · 2026-11-27 11:41 UTC · original

Independently checked head 7ccbbd876 on Windows 11 Pro 26200, Vulkan SDK 1.4.363.0, WSL llvm-mc 21.1.8, across NVIDIA GeForce RTX 5080 (616.64) and AMD Radeon(TM) Graphics (25.10.30.02), no game files.

What passed:

  • Semantic suites (52/52 OK): test_wave64_one_lane_fold.py (3/3), test_wave64_emulate_opt.py (34/34), test_loop_ballot_barrier.py (15/15).
  • Dual-GPU run_gpu_tests.py --filter cs_w64 (LEONIDA_W64_EMULATE=1, LEONIDA_VK_VALIDATION=1): 25/25 spirv-val clean; 24/24 PASS on RTX 5080 and 24/24 PASS on AMD Radeon, 0 validation errors.
  • Dual-GPU valu_fuzz.py --family mem --wave 64 --emulate --programs 120 --seed 20261126: 120/120 match on RTX 5080 and 120/120 match on AMD Radeon.

Three soundness bugs reproduced live (spirv_mini_interp at 7ccbbd876 vs origin/main 50ce8f51a):

  1. A one-block self-loop inside a single-lane region loses its back-edge (tools/wave64_emulate_opt.py:1428-1443, 1457-1471; tools/rdna2spirv.py:18905-18911):

    • Cyclic stores are now admitted under singleton EXEC, but a single-block region gets no loop tree and its emitter stops at the branch.
    • Live repro: select lane 0 (v_cmpx_eq_u32_e32 vcc, 0, v0; s_cbranch_execz .join), then in one .loop: block increment v1 (init 10), store to mem[0], decrement s10 (init 2), s_cmp_lg_u32 s10, 0; s_cbranch_scc1 .loop:
      • origin/main (50ce8f51a): mem[0] = 12 (2 iterations)
      • PR #2447 (7ccbbd876): mem[0] = 11 (2nd iteration silently dropped)
    • Fix: reject single-block singleton regions with a self-edge, or emit their loop.
  2. A compare writing exec_lo certifies all 64 lanes as a singleton (tools/wave64_emulate_opt.py:1165-1169, 1176-1202; tools/rdna2spirv.py:6315-6319, 6944-6948, 8106-8109):

    • Starting from full 64-lane EXEC, v_cmp_eq_u32_e64 exec_lo, 0, v0 leaves lane 0 and lanes 32–63 active (the emitter preserves exec_hi), but the analysis marks all of EXEC as single-lane so a folded v_readfirstlane_b32 s14, v0 reads each upper lane's own v0 instead of 0.
    • Live repro (store s14 per lane):
      • origin/main (50ce8f51a): lane0=0, lane1=0xee, lane32=0, lane33=0
      • PR #2447 (7ccbbd876): lane0=0, lane1=0xee, lane32=32, lane33=33
    • Fix: only record whole-EXEC singleton when the untouched half is also proven empty (or the compare writes full 64-bit exec).
  3. Implicit VCC carry writes leave stale singleton VCC facts (tools/wave64_emulate_opt.py:528-534, 1165-1169, 1189-1191; tools/rdna2spirv.py:10376-10383, 10421-10426):

    • After v_cmp_eq_u32_e32 vcc, 0, v0, the accepted shorthand v_add_co_ci_u32 v3, v1, v2 (v1=0xffffffff, v2=1) implicitly updates VCC for every carrying lane in the emitter, while the analysis only tracks the VGPR destination; a subsequent s_and_saveexec_b64 then misclassifies full EXEC as single-lane. (Specific to the accepted shorthand; canonical LLVM disassembly prints explicit VCC operands.)
    • Live repro (v_readfirstlane_b32 s14, v0 stored per lane):
      • origin/main (50ce8f51a): lane0=0, lane1=0, lane32=0
      • PR #2447 (7ccbbd876): lane0=0, lane1=1, lane32=32
    • Fix: model implicit VCC writes for shorthand carry ops (or invalidate VCC facts on those forms).

Adding these three cases (plus a positive selector from lanes 32–63, e.g. lane 65 in a 2-wave workgroup) to test_wave64_one_lane_fold.py would make the fold airtight. AI-assisted.

**@⁠timbot103** on GitHub · 2026-11-27 11:41 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2447#issuecomment-7217359221) Independently checked head `7ccbbd876` on Windows 11 Pro 26200, Vulkan SDK `1.4.363.0`, WSL `llvm-mc 21.1.8`, across **NVIDIA GeForce RTX 5080** (`616.64`) and **AMD Radeon(TM) Graphics** (`25.10.30.02`), no game files. **What passed:** - Semantic suites (**52/52 OK**): `test_wave64_one_lane_fold.py` (3/3), `test_wave64_emulate_opt.py` (34/34), `test_loop_ballot_barrier.py` (15/15). - Dual-GPU `run_gpu_tests.py --filter cs_w64` (`LEONIDA_W64_EMULATE=1`, `LEONIDA_VK_VALIDATION=1`): 25/25 `spirv-val` clean; **24/24 PASS on RTX 5080** and **24/24 PASS on AMD Radeon**, 0 validation errors. - Dual-GPU `valu_fuzz.py --family mem --wave 64 --emulate --programs 120 --seed 20261126`: **120/120 match on RTX 5080** and **120/120 match on AMD Radeon**. **Three soundness bugs reproduced live (`spirv_mini_interp` at `7ccbbd876` vs `origin/main` `50ce8f51a`):** 1. **A one-block self-loop inside a single-lane region loses its back-edge (`tools/wave64_emulate_opt.py:1428-1443, 1457-1471`; `tools/rdna2spirv.py:18905-18911`):** - Cyclic stores are now admitted under singleton EXEC, but a single-block region gets no loop tree and its emitter stops at the branch. - **Live repro:** select lane 0 (`v_cmpx_eq_u32_e32 vcc, 0, v0; s_cbranch_execz .join`), then in one `.loop:` block increment `v1` (init 10), store to `mem[0]`, decrement `s10` (init 2), `s_cmp_lg_u32 s10, 0; s_cbranch_scc1 .loop`: - `origin/main` (`50ce8f51a`): `mem[0] = 12` (2 iterations) - PR `#2447` (`7ccbbd876`): `mem[0] = 11` (2nd iteration silently dropped) - **Fix:** reject single-block singleton regions with a self-edge, or emit their loop. 2. **A compare writing `exec_lo` certifies all 64 lanes as a singleton (`tools/wave64_emulate_opt.py:1165-1169, 1176-1202`; `tools/rdna2spirv.py:6315-6319, 6944-6948, 8106-8109`):** - Starting from full 64-lane EXEC, `v_cmp_eq_u32_e64 exec_lo, 0, v0` leaves lane 0 **and** lanes 32–63 active (the emitter preserves `exec_hi`), but the analysis marks all of EXEC as single-lane so a folded `v_readfirstlane_b32 s14, v0` reads each upper lane's own `v0` instead of `0`. - **Live repro** (store `s14` per lane): - `origin/main` (`50ce8f51a`): `lane0=0, lane1=0xee, lane32=0, lane33=0` - PR `#2447` (`7ccbbd876`): `lane0=0, lane1=0xee, lane32=32, lane33=33` - **Fix:** only record whole-EXEC singleton when the untouched half is also proven empty (or the compare writes full 64-bit `exec`). 3. **Implicit VCC carry writes leave stale singleton VCC facts (`tools/wave64_emulate_opt.py:528-534, 1165-1169, 1189-1191`; `tools/rdna2spirv.py:10376-10383, 10421-10426`):** - After `v_cmp_eq_u32_e32 vcc, 0, v0`, the accepted shorthand `v_add_co_ci_u32 v3, v1, v2` (`v1=0xffffffff, v2=1`) implicitly updates VCC for every carrying lane in the emitter, while the analysis only tracks the VGPR destination; a subsequent `s_and_saveexec_b64` then misclassifies full EXEC as single-lane. (Specific to the accepted shorthand; canonical LLVM disassembly prints explicit VCC operands.) - **Live repro** (`v_readfirstlane_b32 s14, v0` stored per lane): - `origin/main` (`50ce8f51a`): `lane0=0, lane1=0, lane32=0` - PR `#2447` (`7ccbbd876`): `lane0=0, lane1=1, lane32=32` - **Fix:** model implicit VCC writes for shorthand carry ops (or invalidate VCC facts on those forms). Adding these three cases (plus a positive selector from lanes 32–63, e.g. lane 65 in a 2-wave workgroup) to `test_wave64_one_lane_fold.py` would make the fold airtight. AI-assisted.
Author
Owner

@⁠DadaCap on GitHub · 2026-11-27 11:59 UTC · original

@⁠timbot103 thank you, these are real gaps in the analysis, and the repros make them easy to pin. I'm fixing all three in the rule itself (a self-edge region is rejected or gets its loop, a compare that writes only exec_lo/exec_hi no longer certifies the whole EXEC unless the other half is proven empty, and the shorthand carry ops invalidate VCC facts), with your three cases plus a positive selector in lanes 32-63 added to test_wave64_one_lane_fold.py. I'll also report how many corpus modules each fix changes, so it is clear whether @⁠OreoFR's and @⁠quartz-heron-58's retail numbers still apply to the same 20 modules. @⁠zmasi thanks for the GPU and invariant check. AI-assisted (Claude Code).

**@⁠DadaCap** on GitHub · 2026-11-27 11:59 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2447#issuecomment-7217711972) @⁠timbot103 thank you, these are real gaps in the analysis, and the repros make them easy to pin. I'm fixing all three in the rule itself (a self-edge region is rejected or gets its loop, a compare that writes only `exec_lo`/`exec_hi` no longer certifies the whole EXEC unless the other half is proven empty, and the shorthand carry ops invalidate VCC facts), with your three cases plus a positive selector in lanes 32-63 added to `test_wave64_one_lane_fold.py`. I'll also report how many corpus modules each fix changes, so it is clear whether @⁠OreoFR's and @⁠quartz-heron-58's retail numbers still apply to the same 20 modules. @⁠zmasi thanks for the GPU and invariant check. AI-assisted (Claude Code).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2447/head:github-bot/gh-2447'
git switch 'github-bot/gh-2447'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2447'
git switch 'github-bot/gh-2447'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2447'
git switch 'github-bot/gh-2447'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2447'
git switch 'main'
git merge --squash 'github-bot/gh-2447'
git switch 'main'
git merge --ff-only 'github-bot/gh-2447'
git switch 'main'
git merge 'github-bot/gh-2447'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.