Repository navigation
rdna2spirv: wave64 emulation clears and frees %x_buf with one writer per word (SHADER-708, data race under GPU-assisted validation) - #4247
Conversation
… (SHADER-708) Under --wave64-emulate the scheduler cleared parts of %x_buf (Workgroup memory) with plain stores that several invocations aimed at one word: the zeroing before the first barrier clamped its index with UMin, so every invocation past the end of a range stored to its last word, and at the top of each round every invocation with nothing to free stored 0 to a shared sink word. All stores wrote 0, so results did not depend on it, but it is a data race, and GPU-assisted validation reports SharedMemoryDataRace-RaceOnStore on every emulated compute shader. Invocation i now clears word i of each row, a short last row is stored only by the invocations that have a word in it, and in a round only lane 0 of a wave (mask words) and invocation 0 (counts) store, each behind a selection that ends before the barrier. The last word of %x_buf is no longer written; it stays allocated so the buffer size does not change. No module changes without --wave64-emulate. tests/semantic/test_w64x_clear_single_writer.py runs the scheduler's head for every invocation at nine workgroup sizes. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…-writer clears main split x_scheduler into x_prologue and x_round_loop_head and added structured emulation (x_mini_scheduler, the x_pre_* helper functions). The one-writer clears are ported to all four places that stored to the sink word. The helper functions now have a selection, so the inliner of tests/semantic/test_wave64_emulate_opt.py keeps their labels, and its lockstep interpreter fails when two invocations store to one %x_buf word between two barriers. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…t fails without it (SHADER-708) test_w64x_clear_single_writer.py gains a mesh shader and whole-workgroup runs of programs that take the inline helpers (one wave and several), two local round loops, and a round loop after a local one. The lockstep interpreter of test_wave64_emulate_opt.py also fails when one invocation plain-stores a %x_buf word that another loads or updates between two barriers. With any one of the four places put back, both files fail. The design.md row has the numbers measured on main aa2988c. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Tested
AI-assisted review. |
|
Tested 5f16276 on NVIDIA GeForce RTX 3070 (32-wide subgroups, so GPU-assisted validation (VK_LAYER_KHRONOS_validation 1.4.350 with
No result change, with
No game files. AI-assisted (Claude). |
TG0000
left a comment
There was a problem hiding this comment.
Tested 5f16276d against aa2988cc on an AMD Radeon RX 6800 (AMD 26.11.2 / 32.0.21045.29003, Vulkan 1.4.315, Windows 11 Pro build 26300).
With LEONIDA_W64_EMULATE=1, build.py translated all four selected synthetic fixtures and spirv-val accepted them on both commits. vk_compute_test <module> 1 dump with Khronos validation 1.4.363.0 and VK_KHRONOS_VALIDATION_GPUAV_ENABLE=1 reported 6 SharedMemoryDataRace-RaceOnStore errors per fixture on base for cs_w64x_ballot, cs_w64x_bpermute, and cs_w64x_wg128_branches (18 total). This head reported 0 races and 0 validation errors across the three. The first eight words in each readback matched. The wave32 cs_barrier_loop control reported 0 validation errors on base.
Each dispatch completed and printed its readback. The process hung at shutdown with the validation layer loaded, including the control with GPU-assisted validation disabled, so the wrapper timed it out after collecting the output.
AI-assisted (OpenAI Codex). Synthetic inputs only.
|
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 5f16276d on Windows 11:
tests/semantic/test_w64x_clear_single_writer.py-> 11 tests, OK.tests/semantic/test_wave64_emulate_opt.py-> 22 tests, OK.compileallandgit diff --checkclean.
The new tests pin every clear of %x_buf to one writer per word (including the x_buf free paths), and the CFG analysis cases still pass. The PR is a dependency of #4314; no blocking issue.
|
Tested GPU-assisted validation. I ran every
GPU suite. Full corpus,
Code read (independent, CPU, synthetic programs only).
AI-assisted (Claude Code). |
|
Tested 5f16276 (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): pass Result: the Setup. Test programs built once from Compute fixtures under GPU-AV. Translate with
The 32 modules: cs_bench_mix, cs_dpp_w64, cs_exec64, cs_w64_mask_detect, cs_w64_mask_imm, cs_w64x_andn2_exec, cs_w64x_ballot, cs_w64x_bpermute, cs_w64x_branch, cs_w64x_chain_barrier, cs_w64x_lds_raw, cs_w64x_lds_war, cs_w64x_load_ballot_load, cs_w64x_mask_reuse, cs_w64x_mask_reuse2, cs_w64x_partial, cs_w64x_partial_scmp, cs_w64x_permute, cs_w64x_readfirstlane, cs_w64x_readlane, cs_w64x_saveexec_b64, cs_w64x_sbarrier, cs_w64x_wg128_branches, cs_w64x_writelane, cs_wave64_ballot, cs_wavemask, cs_wm_bfm64, cs_wm_half_cmp, cs_wm_half_ops, cs_wm_scc_rfl, cs_wqm_w64, cs_wqm_w64_half. The 6 modules that still report after the change are the same 6 before, with the same counts, and are not I compared the error count of every one of the 104 modules before and after: only the 32 above change. Suite, no validation (
Every one of the 210 result lines is the same before and after (compared after removing the work-folder path). Mesh as compute (not what this PR targets, for the record). Without validation, the emulated race shows up as wrong output on this GPU.
Same four runs with the two fixtures of #4298 applied on top ( Also run: Not tested: other GPUs or drivers; the Windows runtime; the translated game corpus (only the 210 fixtures); AI-assisted. No game files. |
SHADER-708, claimed in the coordination issue: #3796 (comment)
What and why
--wave64-emulatekeeps its exchange buffer%x_bufin Workgroup memory and clears parts of it with plainOpStores. Onmainseveral invocations store to one word with no barrier between them. Every such store writes 0, but two unordered plain stores to one word are a data race by the SPIR-V memory rules, and GPU-assisted validation reportsSharedMemoryDataRace-RaceOnStoreon every emulated compute shader (@Vonsek's finding).Since #2089 (structured emulation) the racing stores are in four places of
tools/rdna2spirv.py, not two:x_prologue, the clear before the first barrierUMin, so every invocation past the end of a range stored to the range's last word (64 invocations and 12 words: invocations 11 to 63 all stored to word 11)x_round_loop_head, the start of a round of the round loopx_mini_scheduler, a local round loop of structured emulationGen._ensure_x_pre_fn, the%x_pre_*_fnhelper functions of the inline exchangeAll four now have one writer per word, still as plain stores:
x_clear_ranges,x_clear_rows). A last row shorter than the workgroup is stored only by the invocations that have a word in it (OpULessThanand a selection).Every selection ends before the
OpControlBarrierthat follows, so the barriers stay in uniform control flow. The last word of%x_bufis no longer written but stays allocated, so the buffer keeps its size (2·L + 6·W + 7 dwords) and the SHADER-309 numbers do not move. Without--wave64-emulateno module changes.The first version of this change covered the first three places only; GPU-assisted validation then still reported the 7 fixtures that are translated in structured mode, which is how the fourth was found. To make sure there is no fifth, every access to
%x_bufthe translator emits was listed with who can run it between two barriers (twelve kinds: the four clears, the lane's own data slot, the atomics on the masks and counts, the loads after a barrier, and the wave32 barrier rounds, which were already right). In every mode an interval between two barriers has plain loads of the round before, atomics on the current slot, and plain stores only to the next slot by its one owner and to the lane's own data word.Two changes to an existing test file come with it (
tests/semantic/test_wave64_emulate_opt.py): its inliner of helper functions skipped everyOpLabel, which only works for one-block helpers, and now keeps the labels of the other blocks; and its lockstep interpreter fails when two invocations plain-store one%x_bufword between two barriers, or one plain-stores a word another loads or updates.Measured
Windows 11 (26200), Vulkan SDK 1.4.363 (validation layer from the SDK), test programs built from
mainaa2988cc. Two GPUs, picked withLEONIDA_VK_DEVICE: Ryzen 7 9700X iGPU (Radeon gfx1036, AMD 32.0.21042.62, subgroup 32..64) and RTX 3070 (NVIDIA 610.74, subgroup 32). Before =mainaa2988cc.GPU-assisted validation, the GPU fixtures.
vk_compute_testcreates its instance without layers, so the layer is loaded from the environment (VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,VK_KHRONOS_VALIDATION_GPUAV_ENABLE=1), thenvk_compute_test <module>.spv 1 dumpfor each of the 35 compute fixtures that have a%x_bufunderLEONIDA_W64_EMULATE=1:SharedMemoryDataRace-RaceOnStorereportsThe 32 are the emulated ones (25 in rounds, 7 structured), 6 reports each, which is the most the layer gave in any run. The other three are the wave32
cs_barrier_*fixtures, whose three words already had one writer.GPU fixture suite.
scripts/run_gpu_tests.py, 214 shaders translated andspirv-val-valid before and after; every test has the same result line before and after:LEONIDA_W64_EMULATE=1LEONIDA_W64_EMULATE=1Random programs.
tools/crosslane_fuzz.py --wave 64 --modes emulate --no-maskdata, five batches (seeds 1 to 4 and 11, with and without--dpp, 12 to 40 ops; one batch with the workgroup sizes replaced by 1, 3, 7, 31, 33, 63, 65, 96, 127, 129, 200, 250 and 320), 2,390 programs, each translated by both translators and run on both GPUs:tools/lds_fuzz.py --wave 64 --emulate(LDS ands_barrier), 69 translated programs: on the RTX 3070 69 of 69 match on both sides with identical dumps, and under validation 69 of 69 report before and 0 after. On the Radeon 47 of the 69 crashvk_compute_test(0xC0000005) before and after alike, not looked into here; the 22 that run match.--wave64-emulateare byte-identical before and after.Two programs where
main's module misbehaves and this one does not. Cause not established for either; they are reported as observed, not as something this change is known to repair.On the Radeon, a 7-op program (the 11 instructions below, after the fuzzer's prologue) gives
s8 = 0x40instead of0x39in every thread but thread 0 when the workgroup has 5 to 11 or 16 invocations:bcnt0of av_cmpmask reads the mask as all zero. It is right at 12, 13, 14, 31, 33, 40 and 64 invocations, right on the RTX 3070 at the sizes tried (7, 8, 11, 12, 16, 64), and right with this change at every size tried on both GPUs. The racing stores alone do not explain it:main's module with every zero store to%x_bufmade anOpAtomicStore(no race report left) is still wrong on the Radeon, andmain's module run under the validation layer is right. So it looks like the AMD compiler and theOpSelect-indexed orUMin-clamped store, which this change no longer emits; that is a hypothesis.On the RTX 3070, one 40-op program (seed 3, 100 invocations, structured) loses the device with
main's module (vkQueueWaitIdlereturns -4, 10 runs of 10) and gives the right dump with this change (10 of 10) and with either module on the Radeon. Not minimised, since every failing run makes the driver log an error.Mesh shaders. The layer checks this in compute shaders only (its
gpuav_shared_memory_data_racesetting says "Compute shaders only"), so it is silent on a mesh stage before and after. The six mesh fixtures run as compute stages (LEONIDA_MESH_COMPUTE=1) do report, but a second store uses up the layer's 6 reports:OpStore %ms_counts(s_sendmsg(MSG_GS_ALLOC_REQ)), which every invocation of a mesh shader makes whether emulated or not. With that one store made atomic in scratch copies of the modules, the six fixtures report%x_bufbefore and nothing after, on both GPUs.%ms_countsis not part of this change; it is posted as a finding. The six mesh fixtures pass before and after on both GPUs, as mesh stages and as compute stages.Corpus. My copy of the package (26,503 carved shaders), translated with
--wave64-emulateby both translators: every wave64 compute and mesh shader, which is every shader this change can touch, and a quarter of the rest (two 1-in-8 slices of the other 16,067).spirv-val-valid, before / afterindex.jsonis the same on both sides for every entry. The 14 invalid modules fail on both sides with the same message, anOpTypeArraythat names a constant with no definition, which this change does not touch (it looks like what Translator: constants assemble asks for late are defined too #3902 is about).%x_bufwhose index is anOpSelectwith a constant alternative (the sink): 45,724 before, 0 after; with aUMin-clamped index: 19,166 before, 0 after. After, every zero store is a first-clear row or sits in the true arm of a lane-0 or invocation-0 test, and in every module the first-clear rows are pairwise disjoint and cover exactly the ranges to clear (99 modules have a partial last wave).Not run: the other 12,050 non-emulated shaders, and any run without
--wave64-emulate.Cost.
scripts/bench_wave64.py, 6 runs a side, alternating, while other jobs used both GPUs. Median of the run medians, in ms:cs_bench_mix(structured)cs_w64x_ballot(rounds)cs_bench_mix(structured)cs_w64x_ballot(rounds)Module size over the 32 emulated compute fixtures: 867,416 to 868,052 bytes (+0.07%), at most +64 bytes for one module.
Tested
python -m unittest discover -s tests/semantic: 7,461 tests; 10 fail, intest_loop_ballot_barrier.py(5),test_cmp_class_f64.py(2),test_mutations.py(2) andtest_vcmp16_opsel.py(1). The same four files give the same counts onmainaa2988ccalone.tests/semantic/test_w64x_clear_single_writer.py(new, 11 tests) andtests/semantic/test_wave64_emulate_opt.py(22 tests): OK. Withmain's translator (LEONIDA_TOOLS_DIR) 37 checks of the first and 23 of the second fail.main's code put back in that place only: each of the four makes both files fail, and so does each half of a place (mask words or counts, one wave or several).tests/semantic/test_cs_shared_memory_limit.py: 15 tests OK.python -m pytest tests/test_translate.py tests/test_crosslane_fuzz.py tests/test_lds_fuzz.py tests/test_valu_fuzz.py tests/test_bench_wave64.py tests/test_mesh_compute.py tests/test_partial_wave_subgroup.py tests/test_wave64_halves_fixtures.py tests/test_run_gpu_tests.py tests/test_run_gpu_wavemask.py tests/test_fssm_wave_size.py: 839 passed, 11 skipped, 0 failed (before the last test-only commit).python -I tests/test_shader_checks.py: 75 tests OK.spirv-val --target-env vulkan1.3on the 38 emulated fixture modules (32 compute, 6 mesh): 38 valid before and after.python -I tests/test_docs_links.py,test_docs_repo_paths.py,test_docs_list_items.py,test_docs_design_rows.py,test_text_write_line_endings.py: OK.Checklist
docs/design.md, with how it was confirmed.Not checked: Linux, RADV, Intel and MoltenVK; the cause of the two
main-only failures above; a mesh shader in structured mode (the six fixtures and the test program are all translated in rounds); race checking of a real mesh stage on a GPU, which the layer does not do; the benchmark on more than one shader per mode. One known gap in the tests: leaving out the counts clear of the helper functions for several waves (no race, a slot reused without being freed) passes every test; read, the worst it does is one extra round.AI-assisted.