Repository navigation
rdna2spirv: restrict %ms_counts store in mesh shaders to invocation 0 (SHADER-766) - #4272
Conversation
… (SHADER-766) - tools/rdna2spirv.py: when emitting s_sendmsg(MSG_GS_ALLOC_REQ) in stage ms/msc, guard OpStore %ms_counts with local_index == 0 to eliminate SharedMemoryDataRace-RaceOnStore. The counts are uniform across the workgroup (from SGPR m0), and all invocations synchronize at the subsequent workgroup barrier before loading %ms_counts. - tests/test_mesh_alloc_single_writer.py: add unit tests verifying the invocation-0 guard and valid SPIR-V assembly for native mesh and --mesh-compute.
|
Automated test:
Same machine and setup as my earlier manual reports. AI-assisted (Claude Code). |
Kam471
left a comment
There was a problem hiding this comment.
Ran this at 862d3e91 on Windows 11: python -I tests/test_mesh_alloc_single_writer.py -> 1 test, OK; compileall and git diff --check clean.
The MSG_GS_ALLOC_REQ path now guards the %ms_counts store with local_index == 0 using OpSelectionMerge/OpBranchConditional, so only the first invocation writes the counts while every invocation still reaches the post-alloc barrier. The new test checks the emitted block structure and the single writer. No blocking issue.
|
Tested 862d3e9 on NVIDIA GeForce RTX 3070 (VK_EXT_mesh_shader, 32-wide), driver 617.42, Windows 11 24H2: pass, no change in the mesh results against
So guarding the No game files. AI-assisted (Claude). |
|
Tested Windows 11 (26200), test programs built from
The comment in the change says the counts are uniform; that holds for the value, but not for who runs the store. #4314 (SHADER-766, which the coordinator recorded for me as finder) makes the store an Not checked: your new test file on its own, and GPU-assisted validation of this head. AI-assisted; measured on my PC. |
Kam471
left a comment
There was a problem hiding this comment.
This change breaks the second-wave allocation case. I cherry-picked 862d3e91 onto #4298's 5079b487 and ran its ms_wave_info_s3 fixture (the case where only the second wave sends MSG_GS_ALLOC_REQ) on Windows 11, RTX 5070 Ti with LEONIDA_W64_EMULATE=1:
FAIL ms_wave_info_s3 at (0,0): got (-1.0000,-1.0000,-1.0000,-1.0000),
want (0.0078,0.0078,0.5,1.0)
The guard local_index == 0 assumes the sending wave contains invocation 0. It does not when the chosen thread is in a later wave (the fixture's whole point), so the store is skipped and the output is uninitialized.
For comparison, #4314 (19bc3599, OpAtomicStore) passes the same fixture 4096/4096 exact, and its test_ms_counts_atomic.py (3 OK) / test_wave64_emulate_opt.py (22 OK) pass. Its argument in the code — "One writer cannot be picked here: which waves send is the program's own control flow" — is the correct one.
Please either change this PR to the atomic store or close it in favor of #4314; as written it is a correctness regression, not just a race fix. The emitted-shape test here (test_mesh_alloc_single_writer.py) cannot catch this.
|
Tested 862d3e9 (merge base main aa2988c) on NVIDIA GeForce RTX 5080, Linux 580.65.06, with GPU-assisted validation (Khronos validation layer, SDK 1.4.363.1): fail Result: on the fixtures that are on Setup. Test programs built once from Mesh fixtures under GPU-AV (the layer's race check is compute-only, so
The six fixtures pass in all of those runs as on the merge base (native: 5 pass, Suite, no validation (
Every one of the 210 result lines is the same before and after (compared after removing the work-folder path). The same change with #4298's fixtures (
The failure line is GPU-AV on #4272 + #4298, mesh as compute: 0 reports natively, 36 emulated (again #4247's Also run: Not tested: other GPUs; the 9,282-shader corpus (zzEmmazz's claim that every one sends behind AI-assisted. No game files. |
|
Full-corpus check on the game copy (Xbox Series 1.0.2.1; Windows 11, RX 9070 XT). Counts only, no shader names or data. PR head
I did not rerun the GPU-AV race check on this head. This is the translator-side corpus check only. |
Kam471
left a comment
There was a problem hiding this comment.
Independent check with the exact head 862d3e918.
Synthetic: repo ms_wave_info_s3.asm with s_cmp_eq_u32 s4, 0 / s_cbranch_scc1 1 before the send, so only wave 1 sends. Translator output:
%t37 = OpBitFieldUExtract %uint %t36 %c18 %c4 ; s4 = s_bfe(s3,0x40018), wave id
%t261 = OpIEqual %bool %t260 %c0 ; s_cmp_eq s4,0
%t265 = OpSelect %uint %t264 %c2 %c1 ; wave 0 -> pc 2, wave 1 -> pc 1
%t267 = OpLoad %uint %local_index
%t268 = OpIEqual %bool %t267 %c0
OpBranchConditional %t268 %t269_alloc_do %t270_alloc_end
%t269_alloc_do = OpLabel
%t266 = OpLoad %uint %m0
OpStore %ms_counts %t266
The path to the store needs pc==1 (local index >= 64) and local_index==0: mutually exclusive, so nothing stores. On RTX 5070 Ti vk_harness gives 0/4096 pixels, got (-1,-1,-1,-1), want (0.0078,0.0078,0.5,1.0); #4314 at its head gives the expected image.
Corpus carve: 566 MS listings, 566 sends; 566/566 guard with v_lshl_add_u32 v0,s3,6,v0 + v_cmpx_eq_u32 0,v0, i.e. local index 0, so this carve loses no write. It is still not safe for arbitrary control flow.
tests/test_mesh_alloc_single_writer.py: 1 test OK. Suggest closing this in favour of #4314, or making the gate fall back to the atomic store when the sender is not invocation 0.
kacper-serewis
left a comment
There was a problem hiding this comment.
AI-assisted independent review. At 862d3e9 and parent aa2988c on Windows RTX 5070 driver 616.56, the same cached mesh with four groups and a zero heap yielded four native primitives and four compute headers of (0,0). All eight sentinel words survived when dispatch was skipped. A positive control wrote all eight expected words. The single-writer change leaves this separate count discrepancy unresolved.
Kam471
left a comment
There was a problem hiding this comment.
Correction to my corpus count above: 566 was a filename filter (_MS_), not the stage map. shaders-carve/index.json has 9,282 stage-ms entries; mapping each to its file stem gives 9,282 listings.
- 9,282/9,282 contain exactly one
MSG_GS_ALLOC_REQ. - 9,282/9,282 are guarded by
v_cmpx_eq_u32 0, <local index>+s_cbranch_execz(9,265 compare v0, 17 v2), with local index =s_bfe(s3,0x40018) << 6 | lane. - 0/9,282 select a sender other than invocation 0.
So the corrected denominator is 9,282/9,282, not 566/566; the carve conclusion is unchanged (the gate is redundant on the whole carve and drops no corpus write). The wave-1-only synthetic still shows a dropped write for valid control flow, so the general-safety verdict is unchanged.
Kam471
left a comment
There was a problem hiding this comment.
Merged, and corpus-safe: all 9,282 game mesh shaders send the alloc from local invocation 0, so the new gate is a no-op for them.
One note for future control flow, not a request to reopen: the invocation-0 gate is not a general rule. On a synthetic variant of ms_wave_info_s3.asm where the send is executed only by wave 1 (local indices 64..127), the translated module still emits OpStore %ms_counts but under local_index == 0, which that wave can never satisfy - the mesh renders 0/4096 pixels. The atomic-store form (#4314) renders 4096/4096 there. If a later ISA or shader puts the send in another wave, this gate silently drops the counts. A follow-up could switch to OpAtomicStore without changing the corpus output.
… store, from whichever wave sends (SHADER-766) s_sendmsg(MSG_GS_ALLOC_REQ) is reached only by the invocations of a wave that sends. Since round 44 the store of M0 to %ms_counts was gated on LocalInvocationIndex == 0 (ItsNotRexed#4272), so it ran only when invocation 0 is in a sending wave. In ms_wave_info_s3 as ItsNotRexed#4298 left it, only wave 1 sends: no invocation stored the counts, and on a 32-wide GPU the fixture drew nothing (RTX 3070: at (0,0): got (-1,-1,-1,-1) in all four modes). The store is now ungated and an OpAtomicStore (Workgroup scope, relaxed): every invocation of a sending wave stores the same M0, which is defined for any number of writers and is not a data race. The counts are still loaded once after the epilogue's barrier. tests/test_mesh_alloc_single_writer.py pinned the gate and is removed; tests/semantic/test_ms_counts_atomic.py takes its place (three hand-written programs, native and emulated, as a mesh stage and as a compute stage). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> (cherry picked from commit b40aa12)
Summary
Addresses SHADER-766 (finding on #4084 by @zzEmmazz):
In mesh shaders (both native mesh and
--mesh-compute),s_sendmsg(MSG_GS_ALLOC_REQ)loads the output vertex and primitive counts from scalar registerm0and stores them into the shared Workgroup variable%ms_counts. Previously, every invocation in the workgroup unconditionally executedOpStore %ms_counts %m, causing Vulkan GPU-assisted validation to reportSharedMemoryDataRace-RaceOnStoreon all 6 mesh fixtures.Changes
tools/rdna2spirv.py:s_sendmsg(MSG_GS_ALLOC_REQ)in stagems(including--mesh-compute), restrictOpStore %ms_countstoLocalInvocationIndex == 0via a structured branch (OpSelectionMerge/OpBranchConditional).m0), and all invocations synchronize at the subsequent workgroup barrier (OpControlBarrier) before reading%ms_countsin the epilogue.tests/test_mesh_alloc_single_writer.py:s_sendmsg(MSG_GS_ALLOC_REQ)emits the invocation-0 check and that the resulting SPIR-V forms a valid structured CFG and assembles cleanly withspirv_asmfor both native mesh and--mesh-compute.Verification
python -I tests/test_mesh_alloc_single_writer.py-> 1 test OKpython -I tests/test_mesh_compute_vs.py-> 12 tests OK (skipped=2)python -I tests/test_meshlet_page.py-> 59 tests OKspirv_asmonly; no game data or GPU required.