From 34cc5589508e71807885d5473df18f359a8878aa Mon Sep 17 00:00:00 2001 From: Luiz Neves Date: Tue, 24 Nov 2026 21:13:43 -0300 Subject: [PATCH] GPU test cs_w32_late_ext: ds_mskor, v_add/sub_nc_i16 clamp, s_buffer_load constant offset - GPU test cs_w32_late_ext: ds_mskor, v_add/sub_nc_i16 clamp, s_buffer_load constant offset - test_late_ext_fixture: runs under python -I Carried over from old-pr/9908 (old upstream PR 9908), rebased on main. --- runtime/test/shaders/check.py | 4 +- runtime/test/shaders/cs_w32_late_ext.asm | 70 +++++++++++++++ scripts/run_gpu_tests.py | 6 +- .../fixtures/disasm/cs_w32_late_ext.s | 54 ++++++++++++ tests/semantic/test_late_ext_fixture.py | 88 +++++++++++++++++++ 5 files changed, 219 insertions(+), 3 deletions(-) create mode 100644 runtime/test/shaders/cs_w32_late_ext.asm create mode 100644 tests/semantic/fixtures/disasm/cs_w32_late_ext.s create mode 100644 tests/semantic/test_late_ext_fixture.py diff --git a/runtime/test/shaders/check.py b/runtime/test/shaders/check.py index 3b73d736fb..51c74648c2 100644 --- a/runtime/test/shaders/check.py +++ b/runtime/test/shaders/check.py @@ -108,7 +108,7 @@ def not_found_message(build, missing): "cs_w32_lds_subdword", "cs_w32_lds_wide", "cs_w32_partial_scmp", "cs_w32_partial_permute", "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_lds_xchg64_ext", "cs_lds_atomics_ext", "cs_w32_andn2_exec", - "cs_mask_vgpr_exec", "cs_w32_buffer_sub16", "cs_w32_buffer_store_sub", "cs_w32_fcmpswap", "cs_w32_permlane_fi", + "cs_mask_vgpr_exec", "cs_w32_late_ext", "cs_w32_buffer_sub16", "cs_w32_buffer_store_sub", "cs_w32_fcmpswap", "cs_w32_permlane_fi", "cs_w32_exec_computed") # 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 @@ -149,6 +149,8 @@ def not_found_message(build, missing): # ds_cmpst_f32 and buffer_atomic_fcmpswap: float compare (cs_w32_fcmpswap.asm) ("cs_w32_fcmpswap", [0, 0x3F800000, 0x7FC00000, 0x3F800000, 0x80000000, 0x40400000, 0x3F800000, 0x7FC00000]), ("cs_w32_new_ops", [0x55, 0x8001, 0xC0000000, 0x80014000, 0x4000FFFF, 0x77, 0x99, 0x1234]), + # ds_mskor, v_add/sub_nc_i16 clamp, s_buffer_load constant SGPR offset (cs_w32_late_ext.asm) + ("cs_w32_late_ext", [0x12345678, 0x1234AB78, 0xFFFF0001, 0x7FFF, 0x8000, 0x8000, 0, 0xEEEEEEEE]), ("cs_w32_misc_ext", [0x0FFF0FFF, 0xE2C4A688, 0x0007FFFF, 0x7FFF8000, 0xFFFE0002, 0xFFFF0005, 0x509, 0xCAFEF00D]), ("cs_w32_salu64", [0x1E6A2C48, 0x1234, 0xFFFFFFFF, 0x100, 47, 48, 0xFFFF8000, 0xFFFFFFFE]), # SHADER-85: 8-bit LDS reads into one half of a VGPR, the other half kept diff --git a/runtime/test/shaders/cs_w32_late_ext.asm b/runtime/test/shaders/cs_w32_late_ext.asm new file mode 100644 index 0000000000..ceff19d521 --- /dev/null +++ b/runtime/test/shaders/cs_w32_late_ext.asm @@ -0,0 +1,70 @@ +; regen_fixtures: this source has a translator fixture in tests/semantic/fixtures/disasm +; Ops merged with CPU-only checks (#1954 ds_mskor, #1974 v_add/sub_nc_i16 clamp, #1948 s_buffer_load constant +; SGPR offset), run once on a GPU. Lane 0 of a wave32 group stores eight dwords: +; out[0] = ds_mskor_rtn_b32 old value (LDS 0x12345678, DATA 0xFF00, DATA2 0xAB00) = 0x12345678 +; out[1] = the LDS word after it: (0x12345678 & ~0xFF00) | 0xAB00 = 0x1234AB78 +; out[2] = the LDS word after ds_mskor_b32 (LDS ~0, DATA 0xFFFF, DATA2 1) = 0xFFFF0001 +; out[3] = v_add_nc_i16 0x7FFF + 1 clamp into a zeroed register = 0x00007FFF +; out[4] = v_sub_nc_i16 0x8000 - 1 clamp into a zeroed register = 0x00008000 +; out[5] = v_add_nc_i16 0x7FFF + 1 without clamp (wraps) = 0x00008000 +; out[6] = s_buffer_load_dword with s12 = -4, offset:20 on the output V# (num_records 0xFFFFFFFF): the sum +; 0xFFFFFFFC + 20 is not reduced mod 2^32, so it is out of range and reads 0 = 0 +; (before #1948 the folded constant wrapped to 16 and read the 0xEE fill) +; out[7] = the same load with s12 = 12 (byte 32, never written: the harness's 0xEE fill) = 0xEEEEEEEE +; The scalar loads run before any store. EXEC is cut to lane 0 before the LDS ops (one LDS address, #1850). +; group: 32,1,1 +; Run: vk_compute_test cs_w32_late_ext.spv 1 dump -> out[0..7] = 305419896 305441656 4294901761 32767 32768 32768 0 4008636142 +; mattr: +wavefrontsize32,-wavefrontsize64 +s_mov_b32 s12, -4 +s_buffer_load_dword s20, s[0:3], s12 offset:20 +s_mov_b32 s13, 12 +s_buffer_load_dword s21, s[0:3], s13 offset:20 +s_waitcnt lgkmcnt(0) +v_mov_b32_e32 v1, 0x7fff +v_mov_b32_e32 v2, 1 +v_mov_b32_e32 v3, 0 +v_add_nc_i16 v3, v1, v2 clamp +v_mov_b32_e32 v4, 0x8000 +v_mov_b32_e32 v5, 0 +v_sub_nc_i16 v5, v4, v2 clamp +v_mov_b32_e32 v6, 0 +v_add_nc_i16 v6, v1, v2 +v_cmpx_eq_u32_e32 0, v0 +v_mov_b32_e32 v10, 0x40 +v_mov_b32_e32 v11, 0x12345678 +ds_write_b32 v10, v11 +s_waitcnt lgkmcnt(0) +v_mov_b32_e32 v12, 0xff00 +v_mov_b32_e32 v13, 0xab00 +ds_mskor_rtn_b32 v14, v10, v12, v13 +s_waitcnt lgkmcnt(0) +ds_read_b32 v15, v10 +v_mov_b32_e32 v16, 0x80 +v_mov_b32_e32 v17, -1 +ds_write_b32 v16, v17 +s_waitcnt lgkmcnt(0) +v_mov_b32_e32 v12, 0xffff +v_mov_b32_e32 v13, 1 +ds_mskor_b32 v16, v12, v13 +s_waitcnt lgkmcnt(0) +ds_read_b32 v18, v16 +s_waitcnt lgkmcnt(0) +v_mov_b32_e32 v20, 0 +buffer_store_dword v14, v20, s[0:3], 0 offen +v_mov_b32_e32 v20, 4 +buffer_store_dword v15, v20, s[0:3], 0 offen +v_mov_b32_e32 v20, 8 +buffer_store_dword v18, v20, s[0:3], 0 offen +v_mov_b32_e32 v20, 12 +buffer_store_dword v3, v20, s[0:3], 0 offen +v_mov_b32_e32 v20, 16 +buffer_store_dword v5, v20, s[0:3], 0 offen +v_mov_b32_e32 v20, 20 +buffer_store_dword v6, v20, s[0:3], 0 offen +v_mov_b32_e32 v20, 24 +v_mov_b32_e32 v21, s20 +buffer_store_dword v21, v20, s[0:3], 0 offen +v_mov_b32_e32 v20, 28 +v_mov_b32_e32 v21, s21 +buffer_store_dword v21, v20, s[0:3], 0 offen +s_endpgm diff --git a/scripts/run_gpu_tests.py b/scripts/run_gpu_tests.py index fef03e9c94..4262349b17 100644 --- a/scripts/run_gpu_tests.py +++ b/scripts/run_gpu_tests.py @@ -1195,7 +1195,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_w32_buffer_sub16", "cs_w32_buffer_store_sub", "cs_w32_fcmpswap", "cs_w32_permlane_fi", "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_late_ext", "cs_w32_buffer_sub16", "cs_w32_buffer_store_sub", "cs_w32_fcmpswap", "cs_w32_permlane_fi", "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_cmp", "cs_dpp_w64", "cs_w64_mask_detect", @@ -1229,6 +1229,8 @@ def run_suite(a, work: str) -> int: # ds_cmpst_f32 and buffer_atomic_fcmpswap: float compare (cs_w32_fcmpswap.asm) "cs_w32_fcmpswap": [0, 0x3F800000, 0x7FC00000, 0x3F800000, 0x80000000, 0x40400000, 0x3F800000, 0x7FC00000], "cs_w32_new_ops": [0x55, 0x8001, 0xC0000000, 0x80014000, 0x4000FFFF, 0x77, 0x99, 0x1234], + # ds_mskor, v_add/sub_nc_i16 clamp, s_buffer_load constant SGPR offset (cs_w32_late_ext.asm) + "cs_w32_late_ext": [0x12345678, 0x1234AB78, 0xFFFF0001, 0x7FFF, 0x8000, 0x8000, 0, 0xEEEEEEEE], "cs_w32_misc_ext": [0x0FFF0FFF, 0xE2C4A688, 0x0007FFFF, 0x7FFF8000, 0xFFFE0002, 0xFFFF0005, 0x509, 0xCAFEF00D], "cs_w32_salu64": [0x1E6A2C48, 0x1234, 0xFFFFFFFF, 0x100, 47, 48, 0xFFFF8000, 0xFFFFFFFE], # SHADER-85: ds_read_u8/i8_d16[_hi] (cs_w32_lds_u8_d16.asm header) @@ -1330,7 +1332,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_partial_permute", "cs_w32_permlane_fi", "cs_w32_sincos", "cs_w32_buffer_sub16", "cs_w32_buffer_store_sub", "cs_w32_fcmpswap", + "cs_bpermute32", "cs_w32_partial_scmp", "cs_w32_partial_permute", "cs_w32_permlane_fi", "cs_w32_sincos", "cs_w32_late_ext", "cs_w32_buffer_sub16", "cs_w32_buffer_store_sub", "cs_w32_fcmpswap", "cs_lds_b64_cmpst", "cs_lds_xchg64_ext", "cs_lds_atomics_ext", "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_dpp_cmp", "cs_w32_andn2_exec", diff --git a/tests/semantic/fixtures/disasm/cs_w32_late_ext.s b/tests/semantic/fixtures/disasm/cs_w32_late_ext.s new file mode 100644 index 0000000000..520c47fae3 --- /dev/null +++ b/tests/semantic/fixtures/disasm/cs_w32_late_ext.s @@ -0,0 +1,54 @@ +; cs_w32_late_ext test fixture generated by tests/semantic/regen_fixtures.py from runtime/test/shaders/cs_w32_late_ext.asm + s_mov_b32 s12, -4 + s_buffer_load_dword s20, s[0:3], s12 offset:20 + s_mov_b32 s13, 12 + s_buffer_load_dword s21, s[0:3], s13 offset:20 + s_waitcnt lgkmcnt(0) + v_mov_b32_e32 v1, 0x7fff + v_mov_b32_e32 v2, 1 + v_mov_b32_e32 v3, 0 + v_add_nc_i16 v3, v1, v2 clamp + v_mov_b32_e32 v4, 0x8000 + v_mov_b32_e32 v5, 0 + v_sub_nc_i16 v5, v4, v2 clamp + v_mov_b32_e32 v6, 0 + v_add_nc_i16 v6, v1, v2 + v_cmpx_eq_u32_e32 0, v0 + v_mov_b32_e32 v10, 0x40 + v_mov_b32_e32 v11, 0x12345678 + ds_write_b32 v10, v11 + s_waitcnt lgkmcnt(0) + v_mov_b32_e32 v12, 0xff00 + v_mov_b32_e32 v13, 0xab00 + ds_mskor_rtn_b32 v14, v10, v12, v13 + s_waitcnt lgkmcnt(0) + ds_read_b32 v15, v10 + v_mov_b32_e32 v16, 0x80 + v_mov_b32_e32 v17, -1 + ds_write_b32 v16, v17 + s_waitcnt lgkmcnt(0) + v_mov_b32_e32 v12, 0xffff + v_mov_b32_e32 v13, 1 + ds_mskor_b32 v16, v12, v13 + s_waitcnt lgkmcnt(0) + ds_read_b32 v18, v16 + s_waitcnt lgkmcnt(0) + v_mov_b32_e32 v20, 0 + buffer_store_dword v14, v20, s[0:3], 0 offen + v_mov_b32_e32 v20, 4 + buffer_store_dword v15, v20, s[0:3], 0 offen + v_mov_b32_e32 v20, 8 + buffer_store_dword v18, v20, s[0:3], 0 offen + v_mov_b32_e32 v20, 12 + buffer_store_dword v3, v20, s[0:3], 0 offen + v_mov_b32_e32 v20, 16 + buffer_store_dword v5, v20, s[0:3], 0 offen + v_mov_b32_e32 v20, 20 + buffer_store_dword v6, v20, s[0:3], 0 offen + v_mov_b32_e32 v20, 24 + v_mov_b32_e32 v21, s20 + buffer_store_dword v21, v20, s[0:3], 0 offen + v_mov_b32_e32 v20, 28 + v_mov_b32_e32 v21, s21 + buffer_store_dword v21, v20, s[0:3], 0 offen + s_endpgm diff --git a/tests/semantic/test_late_ext_fixture.py b/tests/semantic/test_late_ext_fixture.py new file mode 100644 index 0000000000..14f7958010 --- /dev/null +++ b/tests/semantic/test_late_ext_fixture.py @@ -0,0 +1,88 @@ +"""cs_w32_late_ext: the GPU test of ds_mskor, v_add/sub_nc_i16 clamp and the s_buffer_load constant SGPR offset, +run here on the CPU too. + +Standard library only; no GPU, no LLVM, no game files. + +runtime/test/shaders/cs_w32_late_ext.asm (derivations in its header) stores eight words from lane 0. This runs its +fixture as one wave32 group through spirv_mini_interp with the buffer vk_compute_test gives it: every byte 0xEE and +a V# with num_records 0xFFFFFFFF (runtime/test/vk_compute_test.cpp). It checks the words against values computed +here, that check.py and scripts/run_gpu_tests.py expect the same words, and that EXEC is cut to lane 0 before the LDS +ops (spirv_mini_interp gives each invocation its own LDS, so lanes racing on one address are invisible here, #1850). +""" +import os +import re +import struct +import sys +import unittest + +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 + +FSSM = bytearray(0x148) +struct.pack_into("<4I", FSSM, 0x138, 0x8088, 32, 1, 1) # 4 user SGPRs, wg id in s4, 512 bytes of LDS, 32x1x1 +FSSM = bytes(FSSM) +VSHARP = [0, 0, 0xFFFFFFFF, 0x31016FAC] # vk_compute_test's V#: base 0, num_records max, raw +FILL = 0xEEEEEEEE + + +def i16(x): + x &= 0xFFFF + return x - 0x10000 if x >> 15 else x + + +def sbuf(sgpr_off, imm, num_records=0xFFFFFFFF): + off = (sgpr_off & 0xFFFFFFFC) + (imm & 0xFFFFFFFC) # no 32-bit wrap (#1948) + return 0 if off + 4 > num_records else FILL + + +EXPECTED = [ + 0x12345678, # ds_mskor_rtn_b32: old value + (0x12345678 & ~0xFF00 | 0xAB00) & M32, # the LDS word after it + (M32 & ~0xFFFF | 1) & M32, # ds_mskor_b32 + max(-32768, min(32767, i16(0x7FFF) + 1)) & 0xFFFF, # v_add_nc_i16 clamp + max(-32768, min(32767, i16(0x8000) - 1)) & 0xFFFF, # v_sub_nc_i16 clamp + (i16(0x7FFF) + 1) & 0xFFFF, # v_add_nc_i16, wraps + sbuf(-4, 20), # s_buffer_load, s12 = -4: out of range + sbuf(12, 20), # s_buffer_load, s12 = 12: the fill +] + + +class LateExtFixture(unittest.TestCase): + def test_reference(self): + self.assertEqual(EXPECTED, [0x12345678, 0x1234AB78, 0xFFFF0001, 0x7FFF, 0x8000, 0x8000, 0, 0xEEEEEEEE]) + + def test_translated_shader(self): + spv = support.translate_fixture("cs_w32_late_ext", fssm=FSSM) + mem = {4 * k: FILL for k in range(64)} + si.run_workgroup(spv, 32, 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_late_ext"\)?[,:]\s*\[([^\]]*)\]', text) + with self.subTest(file="/".join(rel)): + self.assertIsNotNone(m) + self.assertEqual([int(v, 0) for v in m.group(1).split(",")], EXPECTED) + + def test_lds_ops_run_in_lane_0_only(self): + with open(os.path.join(ROOT, "runtime", "test", "shaders", "cs_w32_late_ext.asm"), encoding="utf-8") as f: + lines = [ln.strip() for ln in f if ln.strip() and not ln.startswith(";")] + self.assertLess(lines.index("v_cmpx_eq_u32_e32 0, v0"), + min(i for i, ln in enumerate(lines) if ln.startswith("ds_"))) + + +if __name__ == "__main__": + unittest.main()