Skip to content

Translator: order scalar loads with the wave's stores under --wave64-emulate (#1587) - #1716

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
AGVibecoder:w64x-scalar-load-order
Nov 22, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
AGVibecoder:w64x-scalar-load-order

Conversation

@AGVibecoder

Copy link
Copy Markdown
Contributor

Fixes #1587
Item: #1587 (no tracker ID yet; claimed in #1548)

What: Under --wave64-emulate, Gen.x_memory_order now treats s_load* / s_buffer_load* as buffer-memory reads. A buffer_store followed by a scalar load of the same wave (read after write), or a scalar load followed by a store (write after read), now runs in separate rounds, so the workgroup barrier between rounds orders them. Loads alone still share a round, and the native (non-emulated) output doesn't change. The docs/design.md contract row "Wave64 emulation" drops "not ordered: scalar loads of memory the wave wrote" and records the rule and how it was confirmed. New test: tests/semantic/test_w64x_scalar_load_order.py (+ README entry).

Why: The bug is in the translator. The generator, cpu_ref and the emulated v_readfirstlane_b32 are all correct. valu_fuzz's model (f = t - t % wave, lane 0 of the whole 64-lane wave) matches the hardware. The emulated v_readfirstlane_b32 already reads the first active lane of the whole wave through %x_buf (rdna2spirv.py, v_readfirstlane_b32 under if self.emulate), so both halves get slot 0 and the reporter's guess (32 in the second half) doesn't hold. The actual cause is memory ordering. On hardware the wave issues buffer_store → s_waitcnt vmcnt(0) → s_buffer_load in order, so lane 0's store is done before the wave's scalar load. In the emulation the two halves can be two 32-wide subgroups that only synchronise at the barrier between rounds. x_memory_order cut rounds at conflicting vector/LDS accesses but left scalar loads out (its docstring and design.md said so). So the store and the scalar load shared a segment, and lanes 32–63 could load before lane 0 had stored, which gives the fill pattern 0xeeeeeeee, as seen on RDNA1/RADV (#1587) and on the RTX 5060 (@EmreO33's comment).

Tested (RX 7900 XTX, AMD 26.10.1 LLPC, Windows 11 Pro, Ryzen 9 5900X):

  • python -m compileall -q make_game.py tools tests, python -I make_game.py --help: OK
  • semantic suite (python -m unittest discover -s tests/semantic): 1343 OK
  • new test_w64x_scalar_load_order.py: 5/5 with the fix; with upstream/main's rdna2spirv.py (via LEONIDA_TOOLS_DIR) 3/5 fail (store→s_load, store→s_buffer_load at the v_readfirstlane slot, s_load→store each land in one segment)
  • tests/test_valu_fuzz.py: 66 passed, 6 skipped; tests/test_translate.py: 435 passed, 2 skipped (llvm-mc 21.1.8 AMDGPU, SPIRV-Tools from Vulkan SDK)
  • valu_fuzz.py --family mem --seed 5 --programs 21 --wave 64 --emulate: 21/21 before and after (3 runs each). The race doesn't show on RDNA3. After the fix also --seed 1 --programs 33 --wave 64 --emulate 33/33 and native --wave 64 21/21.

Not covered: a GPU reproduction where the race shows. @bobcatchris, could you run the before/after on your V540s (RDNA1/RADV)? --family mem --seed 5 --programs 21 --wave 64 --emulate and --seed 7 --programs 20 ..., with upstream/main and with this branch. @EmreO33's RTX 5060 command (--seed 1 --programs 33 --wave 64 --emulate, 5/5 failing) would confirm it on NVIDIA too. Not measured: how many extra rounds this costs on the game corpus (no work folder on this PC). The extra round only appears where a scalar load and a buffer store conflict inside one segment. Sampling an image the wave wrote is still not ordered (unchanged).
Depends on: none (#1608 edits next to x_memory_order but not the same lines)
Game files: none

— Claude Opus 5.5 (high effort), AI-assisted

🤖 Generated with Claude Code

AGVibecoder and others added 2 commits November 22, 2026 14:29
…emulate (ItsNotRexed#1587)

x_memory_order cut a round at buffer/image accesses that conflict within a
segment, but left s_load/s_buffer_load out, so a buffer_store and a later
scalar load of the same address shared a segment and the half of the
emulated wave in the other subgroup could load before this half stored.
valu_fuzz --family mem --emulate showed it as lanes 32-63 reading the fill
pattern. A scalar load now counts as a buffer read (RAW and WAR start a new
round); loads alone still share one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@IAmNotSiob IAmNotSiob left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch, an s_load reading memory the other half hasn't stored yet is exactly what lanes 32-63 seeing the fill pattern would look like. treating s_load/s_buffer_load as reads in the conflict check is the minimal fix. semantic tests pass merged with main ^^ AI-assisted review (Claude Code).

@evilclanker9000

Copy link
Copy Markdown
Contributor

Tested 5816d32 on AMD Radeon RDNA2 iGPU (Ryzen 9 9950X3D, driver 26.10.1, subgroup 32..64) and NVIDIA GeForce RTX 5080 (616.92, subgroup 32), Windows 11 10.0.26200: pass. The GPU race itself did not reproduce on either card, so the GPU runs show no regression but not the fix.

Runtime build from a recent main (the PR changes no runtime C++), LEONIDA_VK_DEVICE set to radeon or 5080 (vk_compute_test confirms the device line), one device at a time.

  • python -I tests/semantic/test_w64x_scalar_load_order.py: 5/5 OK. With tools/rdna2spirv.py swapped for the PR's base (a0acd33), 3/5 fail (test_s_load_then_store, test_store_then_s_load, test_store_then_s_buffer_load_at_the_first_lane_slot), as described.
  • python -I tools/valu_fuzz.py --family mem --wave 64 --emulate with the PR: seed 5 / 21 programs 21/21, seed 1 / 33 programs 33/33, on both Radeon and RTX 5080. With the base translator the same three runs (plus seed 7 / 20 programs) are also all matching on both, so there is no GPU red-on-main here; the 5/5 failure @EmreO33 saw on an RTX 5060 for seed 1 does not appear on a 5080 or on this iGPU. A GPU reproduction is still only on RDNA1/RADV and the 5060.
  • LEONIDA_W64_EMULATE=1 python -I scripts/run_gpu_tests.py --filter w64x: 19/19 on both devices (all cs_w64x_* plus ms_w64x_ballot 4096/4096), so the extra rounds change no result there.
  • python -m unittest discover -s tests/semantic -p "test_w64*.py" and -p "test_wave64*.py": OK (5 and 82 tests). pytest tests/test_translate.py -k "w64 or wave64 or emul": 137 passed, 1 skipped. I did not run the full semantic suite.

Diff skimmed: the change is limited to Gen.x_memory_order treating s_load*/s_buffer_load* as buffer reads under --wave64-emulate; nothing changes without the flag. Looks fine to me.

AI-assisted.

@Yourrespawn

Copy link
Copy Markdown
Contributor

GPU run on the hardware you asked for — NVIDIA this time: RTX 4080 (Ada, subgroup 32..32), driver 617.14, Windows 11 Pro 10.0.26200.

valu_fuzz --family mem --wave 64 --emulate upstream/main (89b047cd) this branch (5816d32d)
--seed 1 --programs 33 33/33 match, exit 0 33/33 match, exit 0
--seed 7 --programs 20 20/20 match, exit 0 20/20 match, exit 0
--seed 5 --programs 200 — 200/200 match, exit 0

The race does not reproduce on this card even before the fix — unlike @EmreO33's RTX 5060 (5/5 failing) and the RDNA1/RADV report in #1587, this 4080/617.14 combination runs main's single-segment ordering clean on all 53 before-programs. So the window is timing- or driver-dependent between NVIDIA generations too; the before-data point may still be useful for whoever characterises it.

With the fix: the branch is clean on the same commands plus the 200-program run, the new test_w64x_scalar_load_order is 5/5, and the full semantic suite is 1403 OK on Python 3.12.10. No native-path change to check here (32-lane device, everything runs emulated).

AI-assisted (ZCode agent): builds and runs executed by a coding agent on this machine.

@Dom-in

Dom-in commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

GPU check on AMD Radeon RX 9070 (RDNA4, gfx1201, wave32 and native wave64), Windows 11, Vulkan SDK 1.4.363. Tested this PR's head 5816d32 merged onto main 89b047c.

scripts/run_gpu_tests.py: 158/166 native and 158/166 with LEONIDA_W64_EMULATE=1, 0 failures. The 8 skips are the run by d3d12_*_test shaders (ps_m2, ps_m3, vs_m15, ...). Same result as main alone.

My first run reported ps_sample_implicit_sint failing with usage: vk_texture_test .... That came from my stale vk_texture_test, built before the test's new mode existed. After a rebuild at 89b047c it passes, native and emulated.

Disclosure: tested with an AI assistant (Claude Code); runs done on my machine.

@adrbn

adrbn commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 5816d32 on Apple M1 (16 GB, 32-wide subgroups), Homebrew MoltenVK 1.4.2, macOS 27.0.1, Python 3.14.7: pass. As on the RX RDNA2 iGPU / RTX 5080 / RTX 4080 runs above, the race does not reproduce here even on main, so this shows no regression, not the fix.

valu_fuzz --family mem --wave 64 --emulate main (89b047cd) this branch (5816d32d)
--seed 1 --programs 33 33/33 match 33/33 match
--seed 7 --programs 20 20/20 match 20/20 match
--seed 5 --programs 200 200/200 match 200/200 match
  • python -I tests/semantic/test_w64x_scalar_load_order.py: 5/5 OK. With LEONIDA_TOOLS_DIR pointing at main's tools/: 3/5 fail (test_s_load_then_store, test_store_then_s_load, test_store_then_s_buffer_load_at_the_first_lane_slot).
  • python -m unittest discover -s tests/semantic: OK.
  • vk_compute_test built from runtime/ on macOS, loaded through MoltenVK directly; LLVM_MC = Homebrew LLVM (amdgcn).

@Pierre-Kookis

Copy link
Copy Markdown
Contributor

A before/after that reproduces on Ada. The race shows on main here and is gone with this branch.

Setup: RTX 4070 Ti SUPER (Ada, subgroup 32), driver 32.0.16.1088 (Windows), Windows 11. vk_compute_test from main 89b047c, llvm-mc 23.1.6.

valu_fuzz --family mem --seed 12 --emulate main 89b047cd this branch 5816d32d
result 99/100, cs_memfuzz_s12_46_w64 mismatches (2 of 2 runs) 100/100

The failing words are all s_buffer_load results in threads 32 and up, the second half:

thread 32 v20: got 0xeeeeeeee, want 0xf3564801 (last written by op 8: sbuf n=1 offset=40 soffset=0 v20)
thread 32 v28: got 0xeeeeeeee, want 0x94f52978 (last written by op 5: sbuf n=4 offset=32 soffset=0 v28)
thread 32 v32: got 0x3401db54, want 0x0f34e662 (last written by op 7: sbuf n=1 offset=28 soffset=0 v32)

The 0xeeeeeeee fill means the second half's scalar load ran before the first half's store, which is the ordering this PR fixes. Print the program with --family mem --show 12:46:64. The first run was part of a seeds 10..13 campaign on main, and seeds 10 and 11 were clean.

(AI-assisted run; synthetic fuzzer programs, no game files.)

@Impre-visible

Copy link
Copy Markdown

Tested 5816d32 on NVIDIA GeForce GTX 1660 (Turing, TU116, subgroup 32..32), driver 620.73.04 (proprietary, Vulkan 1.4.357), Arch Linux (kernel 6.18): pass, no regression. A Turing datapoint for the emulated path this PR changes.

Native build of runtime/ from this head (Ninja, RelWithDebInfo, clang 23.1.5), llvm-mc 23.1.5, SPIRV-Tools v2026.4.

  • spirv-val on the modules the PR's semantic tests translate (test_w64x_scalar_load_order.py): distinct modules: 5, pass spirv-val: 5, rejected: 0
  • tests/semantic: 1403 OK
  • build.py: 175 shaders, 175 translated, 175 pass spirv-val; check.py: 114 of 147 pass, 33 skipped (wave64 without emulation, mesh fixtures)
  • run_gpu_tests.py: 168 shaders: 168 translated, 168 pass spirv-val; 125/163 passed, 38 skipped (fixtures run by the d3d12 tests), 0 failed
  • LEONIDA_W64_EMULATE=1 run_gpu_tests.py: 155/163 passed, 8 skipped (fixtures run by the d3d12 tests), 0 failed

AI-assisted run (Claude). Synthetic programs only, no game files involved.

@ItsNotRexed
ItsNotRexed merged commit 8240b17 into ItsNotRexed:main Nov 22, 2026
2 of 4 checks passed
@Yourrespawn

Copy link
Copy Markdown
Contributor

The race reproduces on NVIDIA after all — and the fix kills it. RTX 4080 (Ada, subgroup 32..32), driver 617.14, Windows 11 Pro 10.0.26200, Python 3.12.10, llvm-mc 21.1.8 native.

My earlier comment said seeds 1/7/5 ran clean on this card; seed 8 (--family mem --programs 200 --seed 8 --emulate) reproduces it on upstream/main (89b047cd): 3 of 400 programs mismatch, 397/400, all in the second emulated half (threads 32..39), all on scalar loads of memory the wave wrote — the exact signature from #1587:

program symptom
cs_memfuzz_s8_20_w64 threads 32-39 v20 = 0xeeeeeeee (the fill pattern), want 0x469da34d; last write op 8 sload offset=32
cs_memfuzz_s8_52_w64 threads 32-39 v34 = 0xeeeeeeee, want 0x812063cb; last write op 2 sload offset=20
cs_memfuzz_s8_183_w64 threads 32-39 v24 = 0x261c3099, want 0xd36a6610; last write op 9 sbuf offset=12 — a stale value rather than the fill pattern, same shape

On this branch (5816d32d): the same command runs 400/400, 0 mismatches, exit 0. Seeds 1/5/7 remain clean on both sides, so the window is program-order dependent, not card-wide: between this card and the RTX 5060 the difference was the seed, not the hardware.

Reproduce before/after with --family mem --programs 200 --seed 8 --emulate. This is the GPU reproduction that was "not covered" in the PR description — happy to leave the fill-pattern programs for anyone who wants them; names and data stay on my machine (counts and symptoms only, per the rules).

AI-assisted (ZCode agent): the runs were executed by a coding agent on this machine.

@sombraSoft

Copy link
Copy Markdown
Contributor

Ampere before/after verification: the #1587 race reproduces deterministically (10/10) on main here and is resolved (0/10 fail, 400/400 full sweep) by this branch (5816d32d).

Setup: NVIDIA GeForce RTX 3060 Ti (Ampere, subgroup 32..32), driver 621.24 (Vulkan 1.4.357), Windows 11 (10.0.26300), vk_compute_test built from main 41f44826 (MSVC 19.51.36246, RelWithDebInfo, fetched Khronos Vulkan-Headers), llvm-mc 21.1.8 + SPIRV-Tools v2025.1 (WSL Debian), Python 3.14.5. Synthetic programs only.

Check main (41f44826) PR #1716 (5816d32d)
valu_fuzz.py --family mem --seed 7 --programs 200 --wave both --emulate 399/400 match, 1 mismatch (cs_memfuzz_s7_100_w64, threads 32–63 v25: 0xeeeeeeee vs 0x2e4cfbf5, op 2 s_load_dwordx4) 400/400 match, 0 mismatched, 0 skipped, 0 harness errors
Direct 10× repeat of cs_memfuzz_s7_100_w64.spv (vk_compute_test, VKC_SUBGROUP_SIZE=32) 10/10 FAIL (t0.v25=0x2e4cfbf5, t32.v25=0xeeeeeeee) 10/10 PASS (t0.v25=0x2e4cfbf5, t32.v25=0x2e4cfbf5)

AI-assisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

valu_fuzz --family mem --emulate: scalar loads in the second emulated wave read the fill pattern (RDNA1/RADV)

10 participants