Skip to content

rdna2spirv: wave64 emulation resets shared words with OpAtomicStore (SHADER-W64-SINK-RACE) - #382

Closed
natterman12 wants to merge 1 commit into
ProjectLeonida:mainfrom
natterman12:rdna2spirv/w64-emulate-atomic-sink
Closed

natterman12 wants to merge 1 commit into
ProjectLeonida:mainfrom
natterman12:rdna2spirv/w64-emulate-atomic-sink

Conversation

@natterman12

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-W64-SINK-RACE (#14)

What and why

Under --wave64-emulate the scheduler resets words of %x_buf every round. The lane that owns a word stores to it; every other lane aims the same store at one sink word:

%x_m5  = OpSelect %uint %x_ml %x_m3 <sink>
%x_mp5 = OpAccessChain %_ptr_WG_uint_x %x_buf %x_m5
         OpStore %x_mp5 %c0

x_prologue zeroes the mask and schedule words with an index clamped by UMin, which lands several invocations on the last word in the same way.

So several invocations store to one Workgroup word with no synchronization between them. That is a data race. It is harmless in effect, because the sink is never read and the clamped stores all write 0, but GPU-assisted validation reports it in every emulated shader:

vkCmdDispatch(): A data race was detected on the shared memory variable "[No OpName found, ID 5]" in local
invocation index 1 while performing a store operation. (Likely against local invocation index 63)
This race is between two invocations executing the same instruction.
SPIR-V Instruction: OpStore %273 %44

docs/testing.md documents this run (LEONIDA_VK_VALIDATION=1 VK_KHRONOS_VALIDATION_GPUAV_ENABLE=1 python scripts/run_gpu_tests.py) as the way to see what the translated shaders do at run time. With 30+ fixtures failing on this one report, a real race in the emulation would not stand out.

The 16 stores (in Gen.x_round_pre, x_round_head, x_mini_scheduler and x_prologue) become OpAtomicStore with Workgroup scope and relaxed semantics, the operands the OpAtomicOr on the same buffer already uses. The values written, the buffer layout and the control flow are unchanged, and native wave64 translation is untouched (a test pins that). No contract-table row: no instruction's translation rule changes, only how the emulation's own bookkeeping stores are emitted; say so if you want a row anyway.

Tests

  • tests/semantic/test_wave64_emulate_opt.py, new SharedWordStoresTests: for one wave and for two, no plain OpStore may target a %x_buf element whose index comes from an OpSelect (the sink) or the prologue's UMin, and the atomic ones must carry the expected operands. Fails on main with the four OpStore %x_mp5 / %x_mp6 / ... lines.
  • The interpreters local to test_wave64_emulate_opt.py and test_loop_ballot_barrier.py (the latter also used by test_lds_addtid.py) did not know OpAtomicStore; they execute it as a store. tests/semantic/spirv_mini_interp.py already did.

Tested

Windows 11 10.0.26300.9974, NVIDIA GeForce RTX 3080 Ti (subgroup 32..32), driver 621.24, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8, Python 3.15.2. main d478a92. No game files.

run_gpu_tests.py, LEONIDA_W64_EMULATE=1 before after
no validation 229/248 passed, 17 skipped 229/248 passed, 17 skipped
LEONIDA_VK_VALIDATION=1 229/248 229/248
plus VK_KHRONOS_VALIDATION_GPUAV_ENABLE=1 187/248, 44 failed 219/248, 12 failed

All 257 fixtures translate and pass spirv-val in each run. The two failures without GPU-assisted validation are cs_imgload_tfe and cs_imgload_d16_tfe (#31).

The 12 left under GPU-assisted validation are not this report: those two, nine LDS fixtures (cs_lds_atomics_ext, cs_lds_b64_cmpst, cs_lds_xchg64_ext, cs_w32_fcmpswap, cs_w32_lds_subdword, cs_w32_lds_u8_d16, cs_w32_lds_wide, cs_w32_misc_ext, cs_w32_new_ops), where the layer reports a race between an OpAtomicLoad and a plain OpLoad of the LDS variable, and cs_pk_mul_lo_const, which I have not looked into. I am posting those as a finding rather than widening this change.

  • python -I scripts/run_semantic_tests.py tests/semantic: 463 files, 6075 tests, 1 file failed: test_image_cube_store_glc (FileNotFoundError in its llvm-mc round trip), which fails the same way on main here.
  • python -I tests/test_text_write_line_endings.py, test_text_encoding.py: OK.
  • Not run: the pytest-based files (tests/test_translate.py and others; no pytest on this machine), any other GPU, and the game's own shader corpus (no game files here), so I have no translated/valid counts for it before and after.

AI-assisted: yes (Claude Code)

Checklist

  • 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. (no new rule; see above)

🤖 Generated with Claude Code

The emulated scheduler resets %x_buf words every round. The lane that owns
a word stores to it and every other lane aims the same store at one sink
word; the prologue's zeroing clamps its index the same way. Several
invocations therefore store to one Workgroup word at once: a data race,
harmless because the word is never read, but GPU-assisted validation
reports it in every emulated shader and so hides any real race.

Those 16 stores are now OpAtomicStore (Workgroup scope, relaxed), the same
operands as the OpAtomicOr already used on this buffer. The values written
are unchanged.

On an RTX 3080 Ti, run_gpu_tests.py with LEONIDA_W64_EMULATE=1 under
GPU-assisted validation goes from 44 failed fixtures to 12; without it the
result is unchanged (229/248).

Two test-local interpreters learn OpAtomicStore, and a new case checks that
no plain OpStore targets a shared %x_buf word.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Sangonapou

Copy link
Copy Markdown
Contributor

Tested 5cb32c2 on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, subgroup 32..32), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300): pass, regression check

Runtime built at this head with scripts\build_runtime.ps1 (MSVC 19.51, VS Build Tools 2026); llvm-mc 23.1.6 (conda-forge), SPIRV-Tools 2026.4, Vulkan SDK 1.4.363.0. No game files. This PR adds no GPU fixture, so this checks the translator change on the GPU suite.

@quartz-heron-58

Copy link
Copy Markdown
Contributor

Tested 5cb32c2 (cherry-picked onto main d42773d) on an NVIDIA GeForce RTX 4080 (Ada, subgroup 32..32), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Home (build 26300): the targeted reports are gone, no regression.

Runtime built from main d42773d with scripts\build_runtime.ps1 (MSVC 14.44, VS 2022 Build Tools); this PR does not touch runtime/. llvm-mc 21.1.8 (built by scripts\build_llvm_mc.ps1), SPIRV-Tools v2026.4, Vulkan SDK 1.4.363.0. No game files.

Full run_gpu_tests.py with this commit: 257 translated, 257 pass spirv-val; native 197/248 passed, 49 skipped; LEONIDA_W64_EMULATE=1 229/248 passed, 17 skipped. Both are the same counts as main on this machine; the 2 failures in each are cs_imgload_tfe and cs_imgload_d16_tfe.

With LEONIDA_W64_EMULATE=1 LEONIDA_VK_VALIDATION=1 VK_KHRONOS_VALIDATION_GPUAV_ENABLE=1:

passed FAIL data-race reports naming a plain OpStore %a %b
main d42773d 187/248 44 185
this PR 219/248 12 0

32 tests stop failing and none start. The 12 that still fail are the same on main: the two tfe fixtures, and ten that are not about the sink stores: cs_lds_atomics_ext, cs_lds_b64_cmpst, cs_lds_xchg64_ext, cs_pk_mul_lo_const, cs_w32_fcmpswap, cs_w32_lds_subdword, cs_w32_lds_u8_d16, cs_w32_lds_wide, cs_w32_misc_ext, cs_w32_new_ops. Their remaining race reports name OpStore/OpLoad with an alignment operand and a few atomics, identical in count on main and on this PR. I did not look into those.

python -I -m unittest discover -s tests/semantic -p test_wave64_emulate_opt.py: 33 tests OK.

AI-assisted (Claude Code ran the tests and wrote this comment).

@trigger337

Copy link
Copy Markdown
Contributor

Tested 5cb32c2 on AMD Radeon RX 6900 XT (RADV NAVI21 / GFX1030, Mesa 26.2.5, Linux 7.3.1 CachyOS, Vulkan 1.4.328, subgroup 32..64): pass.

Discrete desktop RDNA2 verification:

AI-assisted: yes
agent: yes

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.

4 participants