From 559ce6717b92e620bfca97474d5f50337b5ec03c Mon Sep 17 00:00:00 2001 From: Luiz Neves Date: Sun, 22 Nov 2026 21:52:31 -0300 Subject: [PATCH 1/2] GPU test cs_w32_atomic_contend: buffer_atomic_inc/dec under contention All 32 lanes of a wave32 group run each op on the same dwords, so the translated CAS loops race for real on a GPU (the CPU interpreter runs lanes in lockstep). Same op on every lane: the final words are order-independent, including inc/dec wrapping at a small D. Each lane adds 1 << (old - fill) into another word, so a lost or doubled update changes the sum; umax/umin of the returned values check the extremes. Registered in check.py, run_gpu_tests.py and regen_fixtures.py; tests/semantic/ test_atomic_contend_fixture.py runs it on the CPU against 32 sequential ops. --- runtime/test/shaders/check.py | 4 +- .../test/shaders/cs_w32_atomic_contend.asm | 45 +++++++++ scripts/run_gpu_tests.py | 6 +- .../fixtures/disasm/cs_w32_atomic_contend.s | 29 ++++++ tests/semantic/regen_fixtures.py | 2 +- tests/semantic/test_atomic_contend_fixture.py | 93 +++++++++++++++++++ 6 files changed, 175 insertions(+), 4 deletions(-) create mode 100644 runtime/test/shaders/cs_w32_atomic_contend.asm create mode 100644 tests/semantic/fixtures/disasm/cs_w32_atomic_contend.s create mode 100644 tests/semantic/test_atomic_contend_fixture.py diff --git a/runtime/test/shaders/check.py b/runtime/test/shaders/check.py index 330af9f4f3..44c3bc0519 100644 --- a/runtime/test/shaders/check.py +++ b/runtime/test/shaders/check.py @@ -85,7 +85,7 @@ def not_found_message(build, missing): "cs_w32_lds_subdword", "cs_w32_lds_wide", "cs_w32_partial_scmp", "cs_exec_const", "cs_w32_mask_imm", "cs_w32_int_ext", "cs_w32_salu64", "cs_w32_misc_ext", "cs_w32_new_ops", "cs_w32_sincos", "cs_lds_b64_cmpst", "cs_w32_andn2_exec", - "cs_mask_vgpr_exec") + "cs_mask_vgpr_exec", "cs_w32_atomic_contend") # Wave32 lane-mask dumps: their EXEC/VCC masks and lane selects cover 32 lanes, so a device whose widest subgroup is # narrower (Mesa llvmpipe: 8) can't produce their words. They are skipped there, as ps_bitmask_perm is. They run with # 32 lanes requested (WAVE32_LANE_ENV), as run_gpu_tests.py does: a device with a range like 8..32 (Intel ANV) may @@ -115,6 +115,8 @@ def not_found_message(build, missing): # 64-bit scalar data ops and v_mad_u64_u32 (derivations in cs_w32_salu64.asm) # more ops with CPU-only checks until now (derivations in cs_w32_misc_ext.asm) # ops merged on 2026-11-22 with CPU-only checks (derivations in cs_w32_new_ops.asm) + # buffer_atomic_inc/dec with all 32 lanes on the same dwords (cs_w32_atomic_contend.asm) + ("cs_w32_atomic_contend", [0xEEEEEF0E, 0xEEEEEEED, 0xA, 0xEEEEEECE, 0x4, 0xEEEEEF0D, 0xEEEEEECF, 0xEEEEEEED]), ("cs_w32_new_ops", [0x55, 0x8001, 0xC0000000, 0x80014000, 0x4000FFFF, 0x77, 0x99, 0x1234]), ("cs_w32_misc_ext", [0x0FFF0FFF, 0xE2C4A688, 0x0007FFFF, 0x7FFF8000, 0xFFFE0002, 0xFFFF0005, 0x509, 0xCAFEF00D]), ("cs_w32_salu64", [0x1E6A2C48, 0x1234, 0xFFFFFFFF, 0x100, 47, 48, 0xFFFF8000, 0xFFFFFFFE]), diff --git a/runtime/test/shaders/cs_w32_atomic_contend.asm b/runtime/test/shaders/cs_w32_atomic_contend.asm new file mode 100644 index 0000000000..8fb52433b6 --- /dev/null +++ b/runtime/test/shaders/cs_w32_atomic_contend.asm @@ -0,0 +1,45 @@ +; buffer_atomic_inc / buffer_atomic_dec (#2448) under contention: all 32 lanes of a wave32 group run every op on the +; same dwords, so the translated OpAtomicCompareExchange loops race for real (the CPU interpreter runs lanes in +; lockstep). The words start as the harness's 0xEE fill, F = 0xEEEEEEEE. Every lane applies the same op, so the final +; word does not depend on the order; the returned old values of one op are all different, so each lane adds +; 1 << (its old value's distance from F) into another word, and a lost or doubled update changes that sum. +; out[0] = 32 x inc, D = ~0: F + 32 = 0xEEEEEF0E +; out[1] = F + sum of 1 << (old - F) over out[0]'s returned values: F + 0xFFFFFFFF iff each of 0..31 came once +; = 0xEEEEEEED +; out[2] = 32 x inc, D = 20: the first gives 0 (F >= D), then it wraps every 21: 31 mod 21 = 0x0000000A +; out[3] = 32 x dec, D = ~0: F - 32 = 0xEEEEEECE +; out[4] = 32 x dec, D = 5: the first gives 5 (F > D), then 5,4,..,0 repeats: 5 - 31 mod 6 = 0x00000004 +; out[5] = umax of F and out[0]'s returned values: F + 31 = 0xEEEEEF0D +; out[6] = umin of F and out[3]'s returned values: F - 31 = 0xEEEEEECF +; out[7] = F + sum of 1 << (F - old) over out[3]'s returned values = 0xEEEEEEED +; group: 32,1,1 +; Run: vk_compute_test cs_w32_atomic_contend.spv 1 dump -> out[0..7] = 4008636174 4008636141 10 4008636110 4 4008636173 4008636111 4008636141 +; mattr: +wavefrontsize32,-wavefrontsize64 +v_mov_b32_e32 v3, 0xeeeeeeee +v_mov_b32_e32 v5, 1 +v_mov_b32_e32 v20, 0 +v_mov_b32_e32 v1, -1 +buffer_atomic_inc v1, v20, s[0:3], 0 offen glc +v_mov_b32_e32 v20, 8 +v_mov_b32_e32 v8, 20 +buffer_atomic_inc v8, v20, s[0:3], 0 offen +v_mov_b32_e32 v20, 12 +v_mov_b32_e32 v6, -1 +buffer_atomic_dec v6, v20, s[0:3], 0 offen glc +v_mov_b32_e32 v20, 16 +v_mov_b32_e32 v9, 5 +buffer_atomic_dec v9, v20, s[0:3], 0 offen +s_waitcnt vmcnt(0) +v_sub_nc_u32_e32 v2, v1, v3 +v_lshlrev_b32_e32 v4, v2, v5 +v_mov_b32_e32 v20, 4 +buffer_atomic_add v4, v20, s[0:3], 0 offen +v_mov_b32_e32 v20, 20 +buffer_atomic_umax v1, v20, s[0:3], 0 offen +v_sub_nc_u32_e32 v7, v3, v6 +v_lshlrev_b32_e32 v10, v7, v5 +v_mov_b32_e32 v20, 28 +buffer_atomic_add v10, v20, s[0:3], 0 offen +v_mov_b32_e32 v20, 24 +buffer_atomic_umin v6, v20, s[0:3], 0 offen +s_endpgm diff --git a/scripts/run_gpu_tests.py b/scripts/run_gpu_tests.py index 94de20f26f..e2670ad2b5 100644 --- a/scripts/run_gpu_tests.py +++ b/scripts/run_gpu_tests.py @@ -944,7 +944,7 @@ def run_suite(a, work: str) -> int: # Whole-wave mask test, as in check.py: needs a 64-wide subgroup, skipped where the device has none. # With LEONIDA_W64_EMULATE=1 it was translated with --wave64-emulate and runs on any subgroup size. results.append((name,) + wavemask_check(vk_compute, spv, cs_env, w64_emulated())) - elif name in ("cs_store", "cs_signed_zero_nan", "cs_smem_soffset_imm", "cs_w32_lane_wrap", "cs_movrelsd_2", "cs_w32_permute", "cs_w32_sincos", "cs_exec_const", "cs_w32_mask_imm", "cs_w64_mask_imm", "cs_w32_lds_u8_d16", "cs_w32_lds_subdword", "cs_w32_lds_wide", "cs_w32_int_ext", "cs_w32_salu64", "cs_w32_misc_ext", "cs_w32_new_ops", "cs_wave64_ballot", "cs_wm_half_ops", "cs_wm_half_cmp", "cs_wm_bfm64", "cs_wm_scc_rfl", "cs_wqm_w64", "cs_wqm_w32", "cs_wqm_w64_half", + elif name in ("cs_store", "cs_signed_zero_nan", "cs_smem_soffset_imm", "cs_w32_lane_wrap", "cs_movrelsd_2", "cs_w32_permute", "cs_w32_sincos", "cs_w32_atomic_contend", "cs_exec_const", "cs_w32_mask_imm", "cs_w64_mask_imm", "cs_w32_lds_u8_d16", "cs_w32_lds_subdword", "cs_w32_lds_wide", "cs_w32_int_ext", "cs_w32_salu64", "cs_w32_misc_ext", "cs_w32_new_ops", "cs_wave64_ballot", "cs_wm_half_ops", "cs_wm_half_cmp", "cs_wm_bfm64", "cs_wm_scc_rfl", "cs_wqm_w64", "cs_wqm_w32", "cs_wqm_w64_half", "cs_dpp", "cs_dpp_row", "cs_dpp_mask", "cs_dpp8", "cs_dpp_ops", "cs_dpp_bank", "cs_dpp_fi", "cs_dpp_bound", "cs_dpp_w64", "cs_w64_mask_detect", @@ -971,6 +971,8 @@ def run_suite(a, work: str) -> int: # 64-bit scalar data ops and v_mad_u64_u32 (cs_w32_salu64.asm) # more ops with CPU-only checks until now (cs_w32_misc_ext.asm) # ops merged on 2026-11-22 with CPU-only checks (cs_w32_new_ops.asm) + # buffer_atomic_inc/dec with all 32 lanes on the same dwords (cs_w32_atomic_contend.asm) + "cs_w32_atomic_contend": [0xEEEEEF0E, 0xEEEEEEED, 0xA, 0xEEEEEECE, 0x4, 0xEEEEEF0D, 0xEEEEEECF, 0xEEEEEEED], "cs_w32_new_ops": [0x55, 0x8001, 0xC0000000, 0x80014000, 0x4000FFFF, 0x77, 0x99, 0x1234], "cs_w32_misc_ext": [0x0FFF0FFF, 0xE2C4A688, 0x0007FFFF, 0x7FFF8000, 0xFFFE0002, 0xFFFF0005, 0x509, 0xCAFEF00D], "cs_w32_salu64": [0x1E6A2C48, 0x1234, 0xFFFFFFFF, 0x100, 47, 48, 0xFFFF8000, 0xFFFFFFFE], @@ -1058,7 +1060,7 @@ def run_suite(a, work: str) -> int: "cs_w32_permute", "cs_barrier_divergent", "cs_barrier_early_end", "cs_barrier_loop", "cs_w32_lds_u8_d16", "cs_w32_int_ext", "cs_w32_salu64", "cs_w32_misc_ext", "cs_w32_new_ops", "cs_w32_lds_subdword", "cs_w32_lds_wide", - "cs_bpermute32", "cs_w32_partial_scmp", "cs_w32_sincos", + "cs_bpermute32", "cs_w32_partial_scmp", "cs_w32_sincos", "cs_w32_atomic_contend", "cs_lds_b64_cmpst", "cs_exec_const", "cs_w32_mask_imm", "cs_mask_vgpr_exec", "cs_dpp", "cs_dpp_row", "cs_dpp_mask", "cs_dpp8", "cs_dpp_ops", "cs_dpp_bound", "cs_dpp_bank", "cs_dpp_fi", "cs_w32_andn2_exec") and not emulated and not partial diff --git a/tests/semantic/fixtures/disasm/cs_w32_atomic_contend.s b/tests/semantic/fixtures/disasm/cs_w32_atomic_contend.s new file mode 100644 index 0000000000..72399f55d3 --- /dev/null +++ b/tests/semantic/fixtures/disasm/cs_w32_atomic_contend.s @@ -0,0 +1,29 @@ +; cs_w32_atomic_contend test fixture generated by tests/semantic/regen_fixtures.py from runtime/test/shaders/cs_w32_atomic_contend.asm + v_mov_b32_e32 v3, 0xeeeeeeee + v_mov_b32_e32 v5, 1 + v_mov_b32_e32 v20, 0 + v_mov_b32_e32 v1, -1 + buffer_atomic_inc v1, v20, s[0:3], 0 offen glc + v_mov_b32_e32 v20, 8 + v_mov_b32_e32 v8, 20 + buffer_atomic_inc v8, v20, s[0:3], 0 offen + v_mov_b32_e32 v20, 12 + v_mov_b32_e32 v6, -1 + buffer_atomic_dec v6, v20, s[0:3], 0 offen glc + v_mov_b32_e32 v20, 16 + v_mov_b32_e32 v9, 5 + buffer_atomic_dec v9, v20, s[0:3], 0 offen + s_waitcnt vmcnt(0) + v_sub_nc_u32_e32 v2, v1, v3 + v_lshlrev_b32_e32 v4, v2, v5 + v_mov_b32_e32 v20, 4 + buffer_atomic_add v4, v20, s[0:3], 0 offen + v_mov_b32_e32 v20, 20 + buffer_atomic_umax v1, v20, s[0:3], 0 offen + v_sub_nc_u32_e32 v7, v3, v6 + v_lshlrev_b32_e32 v10, v7, v5 + v_mov_b32_e32 v20, 28 + buffer_atomic_add v10, v20, s[0:3], 0 offen + v_mov_b32_e32 v20, 24 + buffer_atomic_umin v6, v20, s[0:3], 0 offen + s_endpgm diff --git a/tests/semantic/regen_fixtures.py b/tests/semantic/regen_fixtures.py index d633f563cd..6b92190da7 100644 --- a/tests/semantic/regen_fixtures.py +++ b/tests/semantic/regen_fixtures.py @@ -21,7 +21,7 @@ "ps_sdwa_dword_hi", # 16-bit SDWA results with dst_sel:DWORD zero the high half "ps_sdwa", "ps_ops", "ps_ops2", "ps_ops3", "ps_ops4", "ps_ops5", "ps_ops6", "ps_ops7", "ps_ops8", "ps_legacy_mul", "ps_vcc_x2", "ps_vcmpx_forms", "ps_flat", "cs_bounds", "cs_fmt_1010102", "cs_fmt_sint32", "cs_fmt_int_narrow", "cs_fmt_d16_xyz", "cs_w64x_readlane", - "cs_w64x_writelane", "cs_w64x_bpermute", "cs_w32_permute", "cs_w32_int_ext", "cs_w32_salu64", "cs_w32_misc_ext", "cs_w32_new_ops", "cs_w64x_permute", "cs_w64x_ballot", "cs_w64x_readfirstlane", + "cs_w64x_writelane", "cs_w64x_bpermute", "cs_w32_permute", "cs_w32_atomic_contend", "cs_w32_int_ext", "cs_w32_salu64", "cs_w32_misc_ext", "cs_w32_new_ops", "cs_w64x_permute", "cs_w64x_ballot", "cs_w64x_readfirstlane", "cs_w64x_load_ballot_load", "cs_w64x_partial", "cs_wave64_ballot", "cs_wm_bfm64", "cs_wm_half_cmp", "cs_wavemask", "cs_cvt_f32_sat", "ps_scc_minmax", "ps_fma_literal", "ps_f16_vop2", "ps_outmods_f16", "ps_f32_mods", "ps_f64_mods", "ps_sff_imm", "cs_w32_lds_u8_d16", "cs_w32_lane_wrap", "ps_bary", diff --git a/tests/semantic/test_atomic_contend_fixture.py b/tests/semantic/test_atomic_contend_fixture.py new file mode 100644 index 0000000000..cd1331814b --- /dev/null +++ b/tests/semantic/test_atomic_contend_fixture.py @@ -0,0 +1,93 @@ +"""cs_w32_atomic_contend: the GPU test of buffer_atomic_inc / buffer_atomic_dec under contention, run on the CPU too. + +Standard library only; no GPU, no LLVM, no game files. + +runtime/test/shaders/cs_w32_atomic_contend.asm (derivations in its header) has all 32 lanes of a wave32 group run +every op on the same dwords of the harness buffer (every byte 0xEE, a V# with num_records 0xFFFFFFFF). spirv_mini_interp +runs the lanes in lockstep, so their compare-exchanges collide and retry here as well; the GPU is where they race +for real. The reference applies the 32 ops one after another: the same op on every lane leaves the same final word +in any order, and the returned old values are the sequential ones in some order, which is all the sums and the +umax / umin depend on. The test also checks that check.py and scripts/run_gpu_tests.py expect the same words. +""" +import os +import re +import struct +import unittest + +import support +import spirv_mini_interp as si + +HERE = os.path.dirname(os.path.abspath(__file__)) +ROOT = os.path.dirname(os.path.dirname(HERE)) +M32 = 0xFFFFFFFF +F = 0xEEEEEEEE # the harness's fill +LANES = 32 + +FSSM = bytearray(0x148) +struct.pack_into("<4I", FSSM, 0x138, 0x88, 32, 1, 1) # 4 user SGPRs, wg id in s4, no LDS, 32x1x1 +FSSM = bytes(FSSM) +VSHARP = [0, 0, 0xFFFFFFFF, 0x31016FAC] # vk_compute_test's V#: base 0, num_records max, raw + + +def inc(m, d): + return 0 if m >= d else (m + 1) & M32 + + +def dec(m, d): + return d if m == 0 or m > d else m - 1 + + +def sequential(fn, d): + """(final word, returned old values) of LANES fn ops on a word starting at F.""" + m, olds = F, [] + for _ in range(LANES): + olds.append(m) + m = fn(m, d) + return m, olds + + +INC_MAX, INC_OLDS = sequential(inc, M32) +DEC_MAX, DEC_OLDS = sequential(dec, M32) +EXPECTED = [ + INC_MAX, + (F + sum(1 << (o - F) for o in INC_OLDS)) & M32, + sequential(inc, 20)[0], + DEC_MAX, + sequential(dec, 5)[0], + max([F] + INC_OLDS), + min([F] + DEC_OLDS), + (F + sum(1 << (F - o) for o in DEC_OLDS)) & M32, +] + + +class AtomicContendFixture(unittest.TestCase): + def test_reference(self): + self.assertEqual(EXPECTED, [0xEEEEEF0E, 0xEEEEEEED, 0xA, 0xEEEEEECE, 0x4, 0xEEEEEF0D, 0xEEEEEECF, 0xEEEEEEED]) + + def test_a_lost_update_changes_the_sums(self): + # one returned value twice and another missing: the bit sum is not F + ~0 any more + olds = INC_OLDS[:-1] + [INC_OLDS[0]] + self.assertNotEqual((F + sum(1 << (o - F) for o in olds)) & M32, EXPECTED[1]) + + def test_translated_shader(self): + spv = support.translate_fixture("cs_w32_atomic_contend", fssm=FSSM) + mem = {4 * k: F for k in range(64)} + si.run_workgroup(spv, LANES, 32, inputs_for=lambda i: {"%wg_id": (0, 0, 0), "%local_id": (i, 0, 0), + "%sg_lane": i}, + push_constants=VSHARP + [0] * 28, memory=mem) + for k, want in enumerate(EXPECTED): + with self.subTest(word=k): + self.assertEqual(mem[4 * k], want) + + def test_gpu_runners_expect_the_same_words(self): + for rel in (("runtime", "test", "shaders", "check.py"), ("scripts", "run_gpu_tests.py")): + with open(os.path.join(ROOT, *rel), encoding="utf-8") as f: + text = f.read() + m = re.search(r'"cs_w32_atomic_contend"\)?[,:]\s*\[([^\]]*)\]', text) + with self.subTest(file="/".join(rel)): + self.assertIsNotNone(m) + self.assertEqual([int(v, 0) for v in m.group(1).split(",")], EXPECTED) + + +if __name__ == "__main__": + unittest.main() From f4734c0ad169cefc96edb8243bbef0bd00116fe5 Mon Sep 17 00:00:00 2001 From: Luiz Neves Date: Sun, 22 Nov 2026 22:33:10 -0300 Subject: [PATCH 2/2] test_atomic_contend_fixture: runs under python -I python -I on the file failed with ModuleNotFoundError: support, as the folder was not on sys.path (found by @justr4mzi on #4323, the same template). It is now inserted, as test_cs_shared_memory_limit does. --- tests/semantic/test_atomic_contend_fixture.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/semantic/test_atomic_contend_fixture.py b/tests/semantic/test_atomic_contend_fixture.py index cd1331814b..2e25d4704b 100644 --- a/tests/semantic/test_atomic_contend_fixture.py +++ b/tests/semantic/test_atomic_contend_fixture.py @@ -12,12 +12,16 @@ import os import re import struct +import sys import unittest -import support -import spirv_mini_interp as si - HERE = os.path.dirname(os.path.abspath(__file__)) +if HERE not in sys.path: + sys.path.insert(0, HERE) # python -I leaves the script's folder off sys.path + +import support # noqa: E402 (support puts tools/ on sys.path, honouring LEONIDA_TOOLS_DIR) +import spirv_mini_interp as si # noqa: E402 + ROOT = os.path.dirname(os.path.dirname(HERE)) M32 = 0xFFFFFFFF F = 0xEEEEEEEE # the harness's fill