Skip to content

rdna2spirv: restrict %ms_counts store in mesh shaders to invocation 0 (SHADER-766) - #4272

Open
wxrrry wants to merge 1 commit into
ItsNotRexed:mainfrom
wxrrry:feature/shader-766-ms-counts-single-writer
Open

wxrrry wants to merge 1 commit into
ItsNotRexed:mainfrom
wxrrry:feature/shader-766-ms-counts-single-writer

Conversation

@wxrrry

@wxrrry wxrrry commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

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 register m0 and stores them into the shared Workgroup variable %ms_counts. Previously, every invocation in the workgroup unconditionally executed OpStore %ms_counts %m, causing Vulkan GPU-assisted validation to report SharedMemoryDataRace-RaceOnStore on all 6 mesh fixtures.

Changes

  • tools/rdna2spirv.py:
    • When emitting s_sendmsg(MSG_GS_ALLOC_REQ) in stage ms (including --mesh-compute), restrict OpStore %ms_counts to LocalInvocationIndex == 0 via a structured branch (OpSelectionMerge / OpBranchConditional).
    • The counts are uniform (from SGPR m0), and all invocations synchronize at the subsequent workgroup barrier (OpControlBarrier) before reading %ms_counts in the epilogue.
  • tests/test_mesh_alloc_single_writer.py:
    • Add unit tests verifying that 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 with spirv_asm for both native mesh and --mesh-compute.

Verification

  • python -I tests/test_mesh_alloc_single_writer.py -> 1 test OK
  • python -I tests/test_mesh_compute_vs.py -> 12 tests OK (skipped=2)
  • python -I tests/test_meshlet_page.py -> 59 tests OK
  • Standard library / spirv_asm only; no game data or GPU required.

… (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.
@CaravelaLabs

Copy link
Copy Markdown
Contributor

Automated test: 862d3e91 merged onto main aa2988cc on AMD Radeon 8060S (RADV GFX1151, RDNA 3.5), Mesa 25.2.8, Ubuntu 24.04, runtime rebuilt from this branch in a clean sandbox. Result: pass.

Same machine and setup as my earlier manual reports. AI-assisted (Claude Code).

@Kam471 Kam471 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.

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.

@MKmajster

Copy link
Copy Markdown

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 main aa2988c (same runtime build, only tools/ differs).

run_gpu_tests.py --filter ms_ this branch main
native 5/6, ms_w64x_ballot skipped (no 64-wide) 5/6, same skip
LEONIDA_W64_EMULATE=1 6/6 6/6

So guarding the %ms_counts store to invocation 0 keeps every mesh fixture's output on NVIDIA, native and emulated. I didn't confirm the GPU-AV race report itself here (run_gpu_tests.py doesn't surface the layer's messages for vk_harness).

No game files. AI-assisted (Claude).

@zzEmmazz

Copy link
Copy Markdown
Contributor

Tested 862d3e91, applied on top of #4247 (5f16276d) together with the two fixtures #4298 changes: one finding.

Windows 11 (26200), test programs built from main aa2988cc. Ryzen 7 9700X iGPU (Radeon gfx1036, AMD 32.0.21042.62) and RTX 3070 (NVIDIA 610.74), picked with LEONIDA_VK_DEVICE. scripts/run_gpu_tests.py --filter ms_, as mesh stages and with LEONIDA_MESH_COMPUTE=1, native and with LEONIDA_W64_EMULATE=1.

  • ms_wave_info_s3 as test(shader): cover mesh allocation across a wave64 barrier (SHADER-761) #4298 has it (only wave 1 sends the allocation) draws nothing on the RTX 3070, in all four modes: 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). Invocation 0 is in wave 0, which branches past the send, so with the store behind LocalInvocationIndex == 0 no invocation stores the counts and the epilogue reads a word nobody wrote.
  • On the Radeon the same fixture passes in all four modes. Nothing writes the word there either, so I read that as the load returning what the memory already held (hypothesis, not measured).
  • The other five fixtures pass on both GPUs in all four modes (natively the RTX 3070 skips ms_w64x_ballot).
  • Over my copy of the package all 9,282 mesh shaders send exactly once, behind v_cmpx_eq_u32 and s_cbranch_execz: one wave sends and the other branches past. Which wave that is depends on the thread the shader picks, so invocation 0 being in the sending wave is not something the translator can rely on.

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 OpAtomicStore instead, which is right for any set of senders; with the same fixtures it passes all four modes on both GPUs and GPU-assisted validation reports nothing. Your test file's idea of pinning the emitted store is good, and a run of #4314 on your GPU would be welcome.

Not checked: your new test file on its own, and GPU-assisted validation of this head.

AI-assisted; measured on my PC.

@Kam471 Kam471 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.

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.

@tedin7

tedin7 commented Nov 23, 2026

Copy link
Copy Markdown

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 main today this change removes the %ms_counts race reports and keeps the suite unchanged. With #4298's ms_wave_info_s3 (only wave 1 sends the allocation) it breaks: the fixture draws nothing, in all four modes. That reproduces what zzEmmazz reported on an RTX 3070; I see it on an RTX 5080 too. Failing here because of that second result.

Setup. Test programs built once from main 03b09a08 (differs from the merge base only in vk_pipeline_check and large_module_gate, not used here): cmake -S runtime -B runtime/build/v -G Ninja -DCMAKE_BUILD_TYPE=Release -DVulkan_INCLUDE_DIR=$SDK/include -DLEONIDA_VULKAN_HEADERS_FETCH=OFF, cmake --build runtime/build/v -j8. Sanity: LEONIDA_VK_VALIDATION=1 vk_queue_test prints validation errors: 0. One git worktree per revision (aa2988cc for "before", the PR head for "after"); tools/ and scripts/ come from the worktree, the binaries are shared. Layer from SDK 1.4.363.1 (VK_LAYER_PATH); gpuav_shared_memory_data_race is on by default in this layer.

Mesh fixtures under GPU-AV (the layer's race check is compute-only, so LEONIDA_MESH_COMPUTE=1): for each of the 6 ms_* fixtures, LEONIDA_MESH_COMPUTE=1 [LEONIDA_W64_EMULATE=1] VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_KHRONOS_VALIDATION_GPUAV_ENABLE=1 VK_KHRONOS_VALIDATION_DEBUG_ACTION=VK_DBG_LAYER_ACTION_LOG_MSG VK_KHRONOS_VALIDATION_LOG_FILENAME=F python -I scripts/run_gpu_tests.py ... --filter FIXTURE, then count SharedMemoryDataRace in F (one log per fixture and mode).

mesh fixtures as compute, GPU-AV, 6 fixtures merge base #4272 862d3e91
LEONIDA_MESH_COMPUTE=1: SharedMemoryDataRace reports 36 (6 each, the cap) 0
LEONIDA_MESH_COMPUTE=1 LEONIDA_W64_EMULATE=1: reports 36 36 (the %x_buf races of #4247 are not in this branch)

The six fixtures pass in all of those runs as on the merge base (native: 5 pass, ms_w64x_ballot skipped; emulated: 6/6). Natively the whole 36 are %ms_counts, so the claim holds there.

Suite, no validation (python -I scripts/run_gpu_tests.py --build runtime/build/v --llvm-mc /usr/lib/llvm-21/bin/llvm-mc, once as is and once with LEONIDA_W64_EMULATE=1):

merge base PR head
native 164/210, 46 skipped, 0 failed 164/210, 46 skipped, 0 failed
LEONIDA_W64_EMULATE=1 195/210, 15 skipped, 0 failed 195/210, 15 skipped, 0 failed

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 (5079b487, cherry-picked onto this head), run_gpu_tests.py --filter ms_:

mode merge base + #4298 #4272 + #4298
mesh stage, native 5/6, 1 skipped 4/6, 1 skipped: ms_wave_info_s3 FAIL
mesh stage, LEONIDA_W64_EMULATE=1 6/6 5/6: ms_wave_info_s3 FAIL
LEONIDA_MESH_COMPUTE=1, native 5/6, 1 skipped 4/6, 1 skipped: ms_wave_info_s3 FAIL
LEONIDA_MESH_COMPUTE=1 LEONIDA_W64_EMULATE=1 4/6 (ms_tri, ms_v5_workgroup FAIL, see #4247) 3/6 (ms_tri, ms_v5_workgroup, ms_wave_info_s3 FAIL)

The failure line is at (0,0): got (-1.0000,-1.0000,-1.0000,-1.0000), want (0.0078,0.0078,0.5,1.0), the same in every mode. Reason, as in the comment above: invocation 0 is in wave 0, which branches past the send, so nobody stores %ms_counts and the epilogue reads a word nobody wrote. Four runs of the compute+emulated mode gave the same result each time. Without #4298, #4272 in that compute+emulated mode gives 4/6 like the merge base (ms_v5_workgroup, ms_wave_info_s3 fail on the emulated %x_buf race, fixed by #4247, not by this change). #4314 (atomic store) with the same fixtures passes all four modes (5/6 + 1 skipped natively, 6/6 emulated).

GPU-AV on #4272 + #4298, mesh as compute: 0 reports natively, 36 emulated (again #4247's %x_buf); no other validation error.

Also run: tests/test_mesh_alloc_single_writer.py (1 test) and tests/test_mesh_compute_vs.py (12): OK.

Not tested: other GPUs; the 9,282-shader corpus (zzEmmazz's claim that every one sends behind v_cmpx_eq_u32 and s_cbranch_execz); the Windows runtime; the native mesh stage under GPU-AV (compute only).

AI-assisted. No game files.

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.

6 participants