OLD-7450: restore old upstream PR 7450 - wave64 emulate: a cross-lane read of a missing lane in a partial wave gives 0 #833

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

@⁠aslan2142 on GitHub · 2026-11-24 14:21 UTC · original
aslan2142:wave64-partial-wave-zero → main · +272 −12 · 8 files · 4 commits · merged 2026-11-25 22:19 UTC

Claimed in the coordination issue as: OLD-7450 #14

Restoration of my original pull request, old upstream PR 7450 in ItsNotRexed/Leonida, which was closed unmerged when that repository went private on 2026-11-23 (old numbers link nowhere now). Ported onto main at d0da7dade; the translator change applied unchanged, and the port adds the subgroup-32 pin in scripts/run_gpu_tests.py (below) and the RX 7900 XTX measurements to the contract row.

Depends on: #793 (line-level, docs/design.md), #364, #366, #373 (line-level, scripts/run_gpu_tests.py)

What and why

  • With --wave64-emulate, a cross-lane read of another lane's exchange slot gives 0 when the source lane doesn't exist in a partial last wave. This covers ds_swizzle_b32, ds_bpermute_b32, ds_permute_b32, v_readlane_b32, v_readfirstlane_b32, DPP and the other reads behind Gen.x_round. A source lane that isn't running reads 0 on the hardware.
  • New helper Gen.x_lane_word: x_wbase + lane < threads, else 0. It is used by the five slot reads in x_round.
  • Why the prologue's zeroed slots weren't enough: RADV (Mesa 26.3.1) turns the store, barrier and load of an exchange behind a branch into masked_swizzle_amd with fetch_inactive. In a 40-thread group, lanes 32-39 of ds_swizzle_b32 offset:swizzle(SWAP,8) then read the missing lanes' registers: garbage that changes between runs.
  • Workgroups whose size is a multiple of 64 emit the same module as before.
  • New GPU fixture cs_w64x_partial_swizzle (40 threads: SWAP 8, BROADCAST 16,15 and ds_bpermute_b32 from lanes 40-47). Its 8 words were measured natively on gfx1036 through tools/hw_oracle.
  • New semantic test tests/semantic/test_w64x_partial_swizzle.py with a hand-written fixture. It covers 40 and 104 threads, with the prologue's zero stores replaced by a poison word in the workgroup interpreter.
  • Part of this change: scripts/run_gpu_tests.py now runs the partial-wave fixtures emulated at subgroup 32, as check.py's partial_wave_test already does.
    • Before, it left the size to the driver, and RADV picks 64 for compute.
    • At 64 the new fixture passes on main even without the fix, while check.py shows it wrong.
    • Without the pin the runner would not catch this bug.
  • New row in the docs/design.md contract table: "Wave64 emulation: cross-lane reads of missing lanes", with how it was confirmed.

Tested

Mesa 26.3.1 RADV on Linux. "Before" is main d0da7dade with only this PR's tests and runner change added.

  • test_w64x_partial_swizzle.py: before 2 fail and 1 error; after 3/3 OK.
  • Whole semantic suite (run_semantic_tests.py): before 465 files and 6,108 tests, after 466 files and 6,111 tests; 0 files fail on either.
  • cs_w64x_partial_swizzle, emulated at subgroup 32, 20 runs of vk_compute_test per device:
    • gfx1036 iGPU: before, words 2 and 3 wrong in 20/20 runs; after, 20/20 right.
    • RX 7900 XTX: before, words 2 and 3 wrong in 19/20 runs; after, 20/20 right.
    • Native wave64 and emulated at subgroup 64 were right before and after.
  • run_gpu_tests.py --filter cs_w64x_partial:
    • before, emulated: 2/3 on each GPU (cs_w64x_partial_swizzle fails);
    • after: 3/3 on each GPU, native and emulated.
  • check.py, emulated: cs_w64x_partial_swizzle fails on both GPUs before and is ok after.
  • Full run_gpu_tests.py, the same on both GPUs, native and LEONIDA_W64_EMULATE=1:
    • main: 231/249 passed, 16 skipped;
    • this branch: 232/250 passed, 16 skipped;
    • the same two failures on both, cs_imgload_tfe and cs_imgload_d16_tfe. They fail on main too and this change doesn't touch them.
  • crosslane_fuzz.py --wave 64 --ext --dpp --no-maskdata --programs 1000 --seed 48315 --modes emulate:
    • gfx1036 iGPU: before 993 match and 7 hyp_only; after 1000/1000.
    • RX 7900 XTX: before 995 match and 5 hyp_only; after 1000/1000.
  • Translator corpus, counts only:
    • --limit 2000 slice, native and --wave64-emulate: 2000 translated and 2000 pass spirv-val, before and after, with identical modules. That slice has no compute shaders.
    • The 40 wave64 compute/mesh shaders whose workgroup is not a multiple of 64, with --wave64-emulate: 40 translated and 40 pass spirv-val, before and after. 25 of the 40 modules change (the ones that read across lanes).
  • Other checks:
    • python -m compileall -q tools tests scripts is clean;
    • git diff --check is clean;
    • check_no_game_data.py passes on every changed file;
    • tests/test_shader_checks.py, tests/test_partial_wave_subgroup.py, tests/test_run_gpu_*.py and tests/test_text_write_line_endings.py pass.

GPUs tested: AMD Radeon RX 7900 XTX (RDNA3, RADV, Mesa 26.3.1, Linux); AMD Radeon Graphics iGPU of the Ryzen 9 7950X3D (gfx1036, RDNA2, RADV, Mesa 26.3.1, Linux)

AI-assisted: yes

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.

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

**@⁠aslan2142** on GitHub · 2026-11-24 14:21 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/833) `aslan2142:wave64-partial-wave-zero` → `main` · +272 −12 · 8 files · 4 commits · merged 2026-11-25 22:19 UTC Claimed in the coordination issue as: OLD-7450 https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14 Restoration of my original pull request, old upstream PR 7450 in `ItsNotRexed/Leonida`, which was closed unmerged when that repository went private on 2026-11-23 (old numbers link nowhere now). Ported onto main at d0da7dade; the translator change applied unchanged, and the port adds the subgroup-32 pin in `scripts/run_gpu_tests.py` (below) and the RX 7900 XTX measurements to the contract row. Depends on: #793 (line-level, docs/design.md), #364, #366, #373 (line-level, scripts/run_gpu_tests.py) ### What and why - With `--wave64-emulate`, a cross-lane read of another lane's exchange slot gives 0 when the source lane doesn't exist in a partial last wave. This covers `ds_swizzle_b32`, `ds_bpermute_b32`, `ds_permute_b32`, `v_readlane_b32`, `v_readfirstlane_b32`, DPP and the other reads behind `Gen.x_round`. A source lane that isn't running reads 0 on the hardware. - New helper `Gen.x_lane_word`: `x_wbase + lane < threads`, else 0. It is used by the five slot reads in `x_round`. - Why the prologue's zeroed slots weren't enough: RADV (Mesa 26.3.1) turns the store, barrier and load of an exchange behind a branch into `masked_swizzle_amd` with `fetch_inactive`. In a 40-thread group, lanes 32-39 of `ds_swizzle_b32 offset:swizzle(SWAP,8)` then read the missing lanes' registers: garbage that changes between runs. - Workgroups whose size is a multiple of 64 emit the same module as before. - New GPU fixture `cs_w64x_partial_swizzle` (40 threads: SWAP 8, BROADCAST 16,15 and `ds_bpermute_b32` from lanes 40-47). Its 8 words were measured natively on gfx1036 through `tools/hw_oracle`. - New semantic test `tests/semantic/test_w64x_partial_swizzle.py` with a hand-written fixture. It covers 40 and 104 threads, with the prologue's zero stores replaced by a poison word in the workgroup interpreter. - **Part of this change: `scripts/run_gpu_tests.py` now runs the partial-wave fixtures emulated at subgroup 32**, as `check.py`'s `partial_wave_test` already does. - Before, it left the size to the driver, and RADV picks 64 for compute. - At 64 the new fixture passes on main even without the fix, while `check.py` shows it wrong. - Without the pin the runner would not catch this bug. - New row in the `docs/design.md` contract table: "Wave64 emulation: cross-lane reads of missing lanes", with how it was confirmed. ### Tested Mesa 26.3.1 RADV on Linux. "Before" is main d0da7dade with only this PR's tests and runner change added. - **`test_w64x_partial_swizzle.py`:** before 2 fail and 1 error; after 3/3 OK. - **Whole semantic suite** (`run_semantic_tests.py`): before 465 files and 6,108 tests, after 466 files and 6,111 tests; 0 files fail on either. - **`cs_w64x_partial_swizzle`, emulated at subgroup 32,** 20 runs of `vk_compute_test` per device: - gfx1036 iGPU: before, words 2 and 3 wrong in 20/20 runs; after, 20/20 right. - RX 7900 XTX: before, words 2 and 3 wrong in 19/20 runs; after, 20/20 right. - Native wave64 and emulated at subgroup 64 were right before and after. - **`run_gpu_tests.py --filter cs_w64x_partial`:** - before, emulated: 2/3 on each GPU (`cs_w64x_partial_swizzle` fails); - after: 3/3 on each GPU, native and emulated. - **`check.py`, emulated:** `cs_w64x_partial_swizzle` fails on both GPUs before and is `ok` after. - **Full `run_gpu_tests.py`,** the same on both GPUs, native and `LEONIDA_W64_EMULATE=1`: - main: 231/249 passed, 16 skipped; - this branch: 232/250 passed, 16 skipped; - the same two failures on both, `cs_imgload_tfe` and `cs_imgload_d16_tfe`. They fail on main too and this change doesn't touch them. - **`crosslane_fuzz.py --wave 64 --ext --dpp --no-maskdata --programs 1000 --seed 48315 --modes emulate`:** - gfx1036 iGPU: before 993 match and 7 hyp_only; after 1000/1000. - RX 7900 XTX: before 995 match and 5 hyp_only; after 1000/1000. - **Translator corpus, counts only:** - `--limit 2000` slice, native and `--wave64-emulate`: 2000 translated and 2000 pass `spirv-val`, before and after, with identical modules. That slice has no compute shaders. - The 40 wave64 compute/mesh shaders whose workgroup is not a multiple of 64, with `--wave64-emulate`: 40 translated and 40 pass `spirv-val`, before and after. 25 of the 40 modules change (the ones that read across lanes). - **Other checks:** - `python -m compileall -q tools tests scripts` is clean; - `git diff --check` is clean; - `check_no_game_data.py` passes on every changed file; - `tests/test_shader_checks.py`, `tests/test_partial_wave_subgroup.py`, `tests/test_run_gpu_*.py` and `tests/test_text_write_line_endings.py` pass. GPUs tested: AMD Radeon RX 7900 XTX (RDNA3, RADV, Mesa 26.3.1, Linux); AMD Radeon Graphics iGPU of the Ryzen 9 7950X3D (gfx1036, RDNA2, RADV, Mesa 26.3.1, Linux) AI-assisted: yes ### 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. AI-assisted (Claude Code, Claude Opus 5.5).
Restores old upstream PR 7450, ported onto main.

With --wave64-emulate, every read of another lane's exchange slot (ds_swizzle,
ds_bpermute, ds_permute, readlane, ...) now selects 0 when the source lane does
not exist in a partial last wave (Gen.x_lane_word), instead of relying on the
prologue's zeroed slot. RADV (Mesa 26.3.1) turned the store, barrier and load of
an exchange behind a branch into masked_swizzle_amd with fetch_inactive, so in a
40-thread group lanes 32-39 of a SWAP 8 swizzle read the missing lanes'
registers. Full waves emit the same module as before.

scripts/run_gpu_tests.py now runs the partial-wave fixtures emulated at subgroup
32, as check.py's partial_wave_test does. It left the subgroup size to the
driver, and RADV picks 64 for compute, where the new fixture passed even without
the fix.

GPU test cs_w64x_partial_swizzle (words measured on gfx1036 through
tools/hw_oracle), semantic test test_w64x_partial_swizzle with a hand-written
fixture, and a contract-table row in docs/design.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Conflict in scripts/run_gpu_tests.py: the dump-test name list gained
cs_w64x_cmp_sdst on main and cs_w64x_partial_swizzle here; both are kept.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
main now resets the missing lanes' exchange slots with OpAtomicStore
(19e42d6de) instead of OpStore, so the test's poison substitution matched
nothing and both partial-wave cases failed on their own guard. Match the
atomic store's value operand instead. Without x_lane_word the 40-thread
case still fails, so the test keeps covering the fix.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
scripts/run_gpu_tests.py: main added cs_w32_wrxchg2_ext and
cs_w32_wrxchg2st64 to the same name list; keep main's list and add
cs_w64x_partial_swizzle to it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:23:38 +00:00
github manually merged commit 0bae6f545a into gh-pr/833/base 2026-11-26 09:23:40 +00:00
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 14:28 UTC · original · approved these changes

Clean-Room Peer Review for PR #833 (d3d12: support x_lane_word boundary checks for cross-lane exchanges on partial waves)

Brilliant driver-level workaround and ISA conformance fix:

  • RADV Subgroup Swizzle Optimization Guard:
    • Addresses Mesa RADV optimization behavior where a workgroup store/barrier/load exchange sequence is lowered directly to a subgroup swizzle instruction. On partial waves, reading a non-existent invocation fetched undefined register data instead of the zeroed workgroup buffer slot.
    • Implements x_lane_word(idx, lane) which emits an OpSelect %uint (lane_index < total_threads) val 0.
  • Zero Overhead for Full Waves:
    • if threads >= self.x_lanes: return v guarantees that full 64-lane waves emit zero extra instructions or comparisons.
  • Unit Test Coverage:
    • ests/semantic/test_partial_wave_swizzle.py validates partial waves (40 threads, 104 threads) and verifies bit-for-bit identity for full waves (64 threads).
  • Clean-room verification: Standard library only, LF line endings, documentation updated in docs/design.md.

LGTM! Highly valuable RDNA2 shader lowering fix. Approved.

**@⁠abnalstlll-byte** on GitHub · 2026-11-24 14:28 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/833#pullrequestreview-6041048919) · **approved these changes** ### Clean-Room Peer Review for PR #833 (d3d12: support x_lane_word boundary checks for cross-lane exchanges on partial waves) Brilliant driver-level workaround and ISA conformance fix: - **RADV Subgroup Swizzle Optimization Guard**: - Addresses Mesa RADV optimization behavior where a workgroup store/barrier/load exchange sequence is lowered directly to a subgroup swizzle instruction. On partial waves, reading a non-existent invocation fetched undefined register data instead of the zeroed workgroup buffer slot. - Implements x_lane_word(idx, lane) which emits an OpSelect %uint (lane_index < total_threads) val 0. - **Zero Overhead for Full Waves**: - if threads >= self.x_lanes: return v guarantees that full 64-lane waves emit zero extra instructions or comparisons. - **Unit Test Coverage**: - ests/semantic/test_partial_wave_swizzle.py validates partial waves (40 threads, 104 threads) and verifies bit-for-bit identity for full waves (64 threads). - **Clean-room verification**: Standard library only, LF line endings, documentation updated in docs/design.md. LGTM! Highly valuable RDNA2 shader lowering fix. Approved.
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 14:41 UTC · original · approved these changes

Audited and verified. Restores upstream PR 7450: under --wave64-emulate, Gen.x_lane_word selects 0 when a source lane does not exist in a partial workgroup, preventing RADV's masked_swizzle_amd optimization from reading uninitialized inactive registers. Contract table updated in docs/design.md with hardware measurements on gfx1036. Clean-room compliant. Approved.

**@⁠abnalstlll-byte** on GitHub · 2026-11-24 14:41 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/833#pullrequestreview-6041314919) · **approved these changes** Audited and verified. Restores upstream PR 7450: under --wave64-emulate, Gen.x_lane_word selects 0 when a source lane does not exist in a partial workgroup, preventing RADV's masked_swizzle_amd optimization from reading uninitialized inactive registers. Contract table updated in docs/design.md with hardware measurements on gfx1036. Clean-room compliant. Approved.
Author
Owner

@⁠Sangonapou on GitHub · 2026-11-24 16:03 UTC · original

Tested 5668f16b on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, subgroup 32..32, mesh shaders), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300), native Windows: pass

Built at this head with scripts\build_runtime.ps1 (MSVC 19.51, VS Build Tools 2026, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0); llvm-mc 23.1.6, SPIRV-Tools 2026.4. No game files.

run_gpu_tests.py --filter cs_w64x_partial_swizzle: with LEONIDA_W64_EMULATE=1 PASS (508 523 0 0 528 0 531 0); natively SKIP (no 64-wide compute subgroup on this device), as expected on a 32-wide GPU.

**@⁠Sangonapou** on GitHub · 2026-11-24 16:03 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/833#issuecomment-7137055477) Tested 5668f16b on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, subgroup 32..32, mesh shaders), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300), native Windows: pass Built at this head with `scripts\build_runtime.ps1` (MSVC 19.51, VS Build Tools 2026, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0); `llvm-mc` 23.1.6, SPIRV-Tools 2026.4. No game files. `run_gpu_tests.py --filter cs_w64x_partial_swizzle`: with `LEONIDA_W64_EMULATE=1` PASS (`508 523 0 0 528 0 531 0`); natively SKIP (`no 64-wide compute subgroup on this device`), as expected on a 32-wide GPU.
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-24 19:30 UTC · original

Thanks @⁠Sangonapou for the 3080 Ti run: cs_w64x_partial_swizzle passing under LEONIDA_W64_EMULATE=1 and skipping natively is what a 32-wide device should do.

The red checks are main's, not this PR's:

  • windows: gta6rt.exe link errors __imp_WaitOnAddress, WakeByAddressAll and WakeByAddressSingle from leonida_xma2_decoder.lib. Fixed on main by #877; this PR doesn't touch the runtime build.
  • linux-gpu-tools / macos: cs_imgload_tfe and cs_imgload_d16_tfe are issue #31, fixed on main by #892.

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

**@⁠aslan2142** on GitHub · 2026-11-24 19:30 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/833#issuecomment-7142208243) Thanks @⁠Sangonapou for the 3080 Ti run: `cs_w64x_partial_swizzle` passing under `LEONIDA_W64_EMULATE=1` and skipping natively is what a 32-wide device should do. The red checks are `main`'s, not this PR's: - `windows`: `gta6rt.exe` link errors `__imp_WaitOnAddress`, `WakeByAddressAll` and `WakeByAddressSingle` from `leonida_xma2_decoder.lib`. Fixed on `main` by #877; this PR doesn't touch the runtime build. - `linux-gpu-tools` / `macos`: `cs_imgload_tfe` and `cs_imgload_d16_tfe` are issue #31, fixed on `main` by #892. AI-assisted (Claude Code, Claude Opus 5.5).
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-25 22:19 UTC · original

Merged: wave64 emulate: a cross-lane read of a missing lane in a partial wave gives 0.

  • Changed since review: main merged in (0bae6f545). One conflict in scripts/run_gpu_tests.py, where main added cs_w32_wrxchg2_ext and cs_w32_wrxchg2st64 to the same name list. main's list is kept, with cs_w64x_partial_swizzle added to it.
  • Tested: the translator suite passes on the merged tree (492 files, 6,315 tests, 0 failed), and so do test_run_gpu_tests, test_run_gpu_tests_python_assembler, the docs tests and test_ci_registration (Linux).
  • Checked on main d3b8251bc + 0bae6f545: the semantic suite passes (492 files, 6,315 tests, 0 files failed); test_run_gpu_tests, test_shader_checks pass; the 15 docs and writer tests pass; check_no_game_data.py is clean on the 8 changed files.

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

**@⁠aslan2142** on GitHub · 2026-11-25 22:19 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/833#issuecomment-7176211287) Merged: wave64 emulate: a cross-lane read of a missing lane in a partial wave gives 0. - **Changed since review:** `main` merged in (`0bae6f545`). One conflict in `scripts/run_gpu_tests.py`, where `main` added `cs_w32_wrxchg2_ext` and `cs_w32_wrxchg2st64` to the same name list. `main`'s list is kept, with `cs_w64x_partial_swizzle` added to it. - **Tested:** the translator suite passes on the merged tree (492 files, 6,315 tests, 0 failed), and so do `test_run_gpu_tests`, `test_run_gpu_tests_python_assembler`, the docs tests and `test_ci_registration` (Linux). - **Checked on `main` `d3b8251bc` + `0bae6f545`:** the semantic suite passes (492 files, 6,315 tests, 0 files failed); `test_run_gpu_tests`, `test_shader_checks` pass; the 15 docs and writer tests pass; `check_no_game_data.py` is clean on the 8 changed files. AI-assisted (Claude Code, Claude Opus 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.