Skip to content

fix(translator): use atomic exchange for mesh alloc request - #8608

Open
Cryptoteep wants to merge 1 commit into
ItsNotRexed:mainfrom
Cryptoteep:fix/shader-ms-alloc-req-atomic
Open

Cryptoteep wants to merge 1 commit into
ItsNotRexed:mainfrom
Cryptoteep:fix/shader-ms-alloc-req-atomic

Conversation

@Cryptoteep

@Cryptoteep Cryptoteep commented Nov 23, 2026 •

Copy link
Copy Markdown

Summary

Fixes s_sendmsg(MSG_GS_ALLOC_REQ) in tools/rdna2spirv.py when non-zero waves emit the mesh allocation request, resolving the issue observed in ms_wave_info_s3 on NVIDIA RTX 5080 / 5070 Ti and Intel Arc (#8306, #8369).

Root Cause

Commit SHADER-766 guarded OpStore %ms_counts inside if (local_index == 0). In multi-wave mesh shaders where wave 0 branches around the allocation request (such as ms_wave_info_s3.asm where wave 0 branches over s_sendmsg and only wave 1 executes MSG_GS_ALLOC_REQ), invocation 0 is never active at the callsite. Because all invocations in wave 1 have local_index >= 32/64, local_index == 0 was never true for any executing thread, causing %ms_counts to remain unwritten (0). The epilogue consequently emitted OpSetMeshOutputsEXT 0 0, producing no primitives and leaving all pixels at the clear value -1.0.

Changes

  • Replace the local_index == 0 store guard with OpAtomicExchange %uint %ms_counts %c2 %c0 %m0.
  • Using an atomic exchange with Workgroup scope (%c2) and Relaxed semantics (%c0):
    1. Prevents SharedMemoryDataRace-RaceOnStore without requiring local_index == 0 to be active.
    2. Guarantees that whichever wave executes s_sendmsg(MSG_GS_ALLOC_REQ) successfully records the uniform allocation counts into %ms_counts.
  • Updates tests/test_mesh_alloc_single_writer.py to verify OpAtomicExchange generation and assemble complete valid SPIR-V modules under both mesh-shading and mesh-compute.

Verification

  • python -m unittest discover -s tests -p test_mesh_alloc_single_writer.py: 1/1 passed.
  • python -I tests/test_mesh_compute_vs.py: 13/13 passed.
  • python -I scripts/check_no_game_data.py tools/rdna2spirv.py tests/test_mesh_alloc_single_writer.py: clean exit 0.
  • Synthetic fixtures only, zero game data.

@Cryptoteep

Copy link
Copy Markdown
Author

Closing duplicate in favor of #8617.

@Cryptoteep Cryptoteep closed this Nov 23, 2026
@Cryptoteep

Copy link
Copy Markdown
Author

Reopening PR #8608: fixes multi-wave mesh shader allocation (#8306, #8369).

@KoertLichtendonk

Copy link
Copy Markdown
Contributor

Coordinator note: this looks like the same change as #5855 by @liamhob (opened 12:20 UTC, still open). Please compare the two and either close this one or say here what it adds. The maintainer decides which one is merged.

@wavehs

wavehs commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

macOS arm64 verification (Apple M3 Pro) + the real fixture, validated

Tested head 5f788db3 merged onto main 867d922f0 (merge 5f788db3), no conflicts.

Host: macOS 27.2.1, Apple M3 Pro (arm64), CPython 3.15.2, SPIRV-Tools v2026.4.

Check Result
python3 -I tests/test_mesh_alloc_single_writer.py 1/1 pass (both MESH_COMPUTE modes)
runtime/test/shaders/ms_wave_info_s3.asm translated emits %t185 = OpAtomicExchange %uint %ms_counts %c2 %c0 %t184
spirv-as + spirv-val --target-env vulkan1.2 on that module OK

The bug this fixes is real, and the fixture in-tree demonstrates it

I translated ms_wave_info_s3 itself (via tests/conftest.py's Shader), not just the synthetic operand:

tree %ms_counts write
main %t186 = OpIEqual %bool %t185 %c0 … OpStore %ms_counts %t184
this PR %t185 = OpAtomicExchange %uint %ms_counts %c2 %c0 %t184

The fixture's own header says "Only wave 1 sends the mesh allocation", and the code agrees: s_cmp_eq_u32 s4, 0 / s_cbranch_scc1 1 / s_sendmsg sendmsg(MSG_GS_ALLOC_REQ). Wave 1's lanes have local_index 64..127, so on main the local_index == 0 guard is false for every lane that executes the store — the alloc request is dropped, %ms_counts stays at the value the earlier ds_write_b32 … offset:0 put there, and the shader's v_xor 64 check reads the wrong count. That is not a theoretical race; it is a dead store on the exact shader the change cites. Good find.

On the replacement

  • Scope %c2 (Workgroup) and semantics %c0 (Relaxed) are legal for an OpAtomicExchange on a Workgroup variable, and spirv-val agrees on the real module.
  • Every lane exchanges the same uniform m0 value, so the result is order-independent: the final %ms_counts is m no matter how the 128 lanes interleave. That is strictly better than picking a winner by local_index, and it removes the OpSelectionMerge/OpBranchConditional pair.
  • One cosmetic note: OpAtomicStore would also do it and would not leave the unused %t185 result. Keeping OpAtomicExchange is fine — just wanted to note it is deliberate-looking, not accidental.

Test quality, minor: the unit test builds its own module with spirv_asm.assemble, which only checks the Python assembler, and that module is not spirv-val-clean as written (a module-scope %m0 = OpVariable … Function, and MeshShadingEXT needs an extension-enabled target env). The real fixture already exists and validates cleanly — a shader.validate(spvasm) assertion on ms_wave_info_s3 would cover the actual shader and catch a future regression in the op selection, which the assertNotIn("OpIEqual %bool") cannot (that only sees the two instructions this handler emits, not the rest of the module).

No functional issues found.

No game files. AI-assisted.

@yuricfg

yuricfg commented Nov 23, 2026

Copy link
Copy Markdown

Tested 5f788db34 on NVIDIA GeForce RTX 5060 (Blackwell, desktop, subgroup 32..32), NVIDIA 621.01, Vulkan loader 1.4.357, Windows 11 (10.0.26300): pass.

A/B with the same test programs (built from main 867d922f0, which is this PR's base; the PR only touches tools/rdna2spirv.py and its test), llvm-mingw clang 23.1.6, conda-forge llvm-mc 23.1.6, SPIRV-Tools 2026.4:

@Diskuss

Diskuss commented Nov 23, 2026

Copy link
Copy Markdown

Tested 5f788db34 on top of main 867d922f0 (a fast-forward) on AMD Radeon RX 9070 XT (RDNA4, gfx1201), Adrenalin 26.10.1 (Windows driver 32.0.32015.7029), Windows 11 Pro 26200, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8: pass, no regressions.

The PR only touches tools/rdna2spirv.py and the new test, and run_gpu_tests.py retranslates every fixture on each run, so the runtime build is the same as for main.

ms_wave_info_s3, 10 runs each, same binaries:

native wave64 LEONIDA_W64_EMULATE=1
main 867d922f0 8/10 9/10
this PR 10/10 10/10

Every failure on main is the known symptom: at (0,0): got (-1,-1,-1,-1), want (0.0078,0.0078,0.5,1.0). Every PR run read back 4096/4096 pixels exact.

Please read this carefully: on this card main mostly passes, as #5741 describes for AMD, where the load can return whatever is already in memory. So 10/10 against 8/10 is consistent with the fix, but on its own it is weak evidence. If the true rate on main were 80%, a clean 10/10 would still happen about 1 time in 9. The cards where main fails every time (the NVIDIA reports in #8306 and #8369) are the stronger test for this PR.

Also run on the PR:

  • run_gpu_tests.py --filter ms_: 6/6 native and 6/6 emulated (ms_clip, ms_f16_pack, ms_tri, ms_v5_workgroup, ms_w64x_ballot, ms_wave_info_s3).
  • Full run_gpu_tests.py, native: 225/241 passed, 16 skipped, 0 failed; 247/247 shaders pass spirv-val.
  • tests/test_mesh_alloc_single_writer.py: 1/1. tests/test_mesh_compute_vs.py: 13/13.
  • scripts/run_semantic_tests.py tests/semantic: 447 files, 5957 tests. Two files are red, test_image_atomics_guard (47 failures) and test_image_store_d16 (1). Both are red on main too with the same counts, so neither comes from this PR.

Code read (not a test):

Not covered:

  • No GPU where main fails every time; the stronger test for this PR needs one.
  • No Linux / RADV.
  • Only 10 runs per configuration.

Correction to my comment on #8563: the 3/3 failure I reported there was three unlucky runs. Measured over 10 runs, main passes 8/10 here.

No game files. AI-assisted.

@jeanklann

Copy link
Copy Markdown

Tested 5f788db34 against 867d922f0 on Linux x86-64, NVIDIA GeForce RTX 4070 (subgroup 32), driver 620.73.04, LLVM 23.1.5 and SPIRV-Tools v2026.4. Pass in both mesh paths.

LEONIDA_W64_EMULATE=1 with scripts/run_gpu_tests.py --filter ms_wave_info_s3 --require-pass, first with native Vulkan mesh shading and then with LEONIDA_MESH_COMPUTE=1. On the base both runs returned the clear color. On the PR both matched 4,096/4,096 pixels, passed spirv-val, and produced no diagnostics in the Vulkan synchronization validation reruns.

I also tested #5855 (0b094ae8 merged locally onto 867d922f0). Its OpAtomicStore version matched 4,096/4,096 pixels in both paths with the same validation result. The fixture gives the same result for both PRs on this driver.

Synthetic inputs only. AI-assisted (Codex).

@comtek21516

Copy link
Copy Markdown

Tested 5f788db34 merged onto main at 867d922f0 on Quadro RTX 4000 (Turing, subgroup 32..32), NVIDIA 615.71.09, Ubuntu 22.04.5 (kernel 5.15), g++ 13.4, llvm-mc 21.1.8, Vulkan SDK 1.4.363.1, Python 3.12.16: pass, and it fixes ms_wave_info_s3 here.

scripts/run_gpu_tests.py main 867d922f0 this PR
ms_wave_info_s3 FAIL at (0,0): got (-1.0000,-1.0000,-1.0000,-1.0000), want (0.0078,0.0078,0.5,1.0) PASS 4096/4096 pixels exact
whole suite 192/241 passed, 48 skipped, 1 failed 193/241 passed, 48 skipped, 0 failed
  • Compared test by test, ms_wave_info_s3 is the only status that differs from main; no test got worse.
  • tests/test_mesh_alloc_single_writer.py: 1 test, OK.
  • The 48 skips are the fixtures that need a 64-wide subgroup; I did not run them under LEONIDA_W64_EMULATE=1.

This is a Linux data point on Turing next to the Windows reports in #8306, #8369 and #8677. Not run: the full tests/semantic suite, validation layers, and any AMD device. No game files. AI-assisted (Claude Code).

@mralanbourne

Copy link
Copy Markdown

Second check, GPU + CPU, Linux (no game files). Run by an agent (Hermes) on mralanbourne's machine; AI-assisted.

Tested 5f788db34 on Intel UHD Graphics 770 (Alder Lake-S GT1, Xe-LP), Mesa 26.3.1 ANV, Vulkan 1.4.363, CachyOS kernel 7.3.5: pass. Tools: llvm-mc 23.1.5, SPIRV-Tools v2026.4, CPython 3.14.10; test programs built from main dfe4e4a (this PR touches no C++).

Full python3 -I scripts/run_gpu_tests.py --build … --require-pass on this head, against main dfe4e4a on the same machine: native 181/241 passed, 60 skipped; LEONIDA_W64_EMULATE=1 211/241 passed, 30 skipped; 0 failed; 246/247 translated, spirv-val pass. No change in any result or skip against main.

CPU: tests/test_mesh_alloc_single_writer.py OK.

@SpeeDY341

Copy link
Copy Markdown

Tested 5f788db34 on NVIDIA GeForce RTX 5090 (Blackwell, subgroup 32..32), driver 621.24 (Windows 32.0.16.2124), Windows 11 Pro (26200): pass.

Same test programs (runtime/build from main 867d922f0, VS 2022 Build Tools), Vulkan SDK 1.4.363.0, llvm-mc 23.1.6 (conda-forge llvm-tools); only the translator differs.

main 867d922f0 this PR 5f788db34
run_gpu_tests.py --filter ms_wave_info_s3 FAIL 5/5 (all pixels -1) PASS 5/5
other ms_* fixtures, 5 runs PASS (ms_w64x_ballot skipped, no 64-wide subgroup) unchanged
full suite, native 192/241, 48 skipped, 1 fail 193/241, 48 skipped, 0 fail
full suite, LEONIDA_W64_EMULATE=1 223/241, 17 skipped, 1 fail 224/241, 17 skipped, 0 fail
tests/test_mesh_alloc_single_writer.py — OK
run_semantic_tests.py tests/semantic 2 files fail the same 2 files fail (test_image_atomics_guard, test_image_store_d16, both already red on main); no new failures

Earlier data point for this device on main: #8306 (comment)

AI-assisted: yes (run and summarised by a coding agent; numbers are from the logs on this machine).

@MiguelMatMar

MiguelMatMar commented Nov 23, 2026 •

Copy link
Copy Markdown

Tested on NVIDIA GeForce RTX 4080 SUPER (Ada, subgroup 32..32), driver 621.24, Windows 11 Pro 26300, on main 867d922, the first Windows Ada data point here, side by side with #5855 for the coordinator's question:

ms_wave_info_s3 (3 runs) --filter ms_
main 867d922 FAIL 3/3: (0,0) = (-1,-1,-1,-1) 4/6 passed, 1 skipped
#8608 5f788db (OpAtomicExchange) PASS 3/3, 4096/4096 exact 5/6 passed, 1 skipped
#5855 0b094ae (OpAtomicStore) PASS 3/3, 4096/4096 exact 5/6 passed, 1 skipped

tests/test_mesh_alloc_single_writer.py passes on each head. On this GPU the two fixes behave the same: both draw the fixture exactly. They differ only in the op (OpAtomicExchange here, OpAtomicStore in #5855). Each ships its own version of tests/test_mesh_alloc_single_writer.py asserting its own op, so only one of the two can land as it is. With either PR the ms_ subset has no failures left (5 passed plus 1 skipped, the 64-wide one this 32-wide device can't run).

(AI-assisted check, Claude Code.)

@Kerwinss

Copy link
Copy Markdown

Tested 5f788db on Windows 11 Pro 10.0.26300, MSVC 14.51 (VS Build Tools 2026), Windows SDK 10.0.26100, NVIDIA GeForce RTX 4060 / 621.24, Khronos Vulkan headers via -DLEONIDA_VULKAN_HEADERS_FETCH=ON: pass, no regression.

Merged onto main 867d922, python -I scripts/run_semantic_tests.py tests/semantic -j 6 (Python 3.15.2, no spirv-val/llvm-mc on PATH): 447 files, 5957 tests, the two failing files are the ones main has (test_image_atomics_guard, test_image_store_d16, fixed by #7608), as on main. tests/semantic/test_mesh_alloc_single_writer.py passes. CPU-side check only; no GPU mesh fixture run.

AI-assisted (Claude Code). No game files.

@Numbers-iwnl

Copy link
Copy Markdown

RDNA4 note for whoever picks between the three open ms_counts fixes: tested on an RX 9070 XT (RDNA4, driver 26.11.2) — this PR, #5741 and #5855 all make ms_wave_info_s3 pass 4096/4096 pixels exact, where main@867d922 fails with got (-1,-1,-1,-1). Comparison table with commits: #5741 (comment). The decision is about approach, not RDNA4 correctness.

AI-assisted — 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.