Check buffer NaN compare-and-swap memory post-state

This commit is contained in:
Cody Smith
2026-11-23 22:43:27 -08:00
parent c023a365a9
commit a672a59800
3 changed files with 30 additions and 7 deletions

View File

@ -8,7 +8,8 @@
; out[4] = buffer_atomic_fcmpswap glc old value (M -0 at byte 0x40, DATA 3.0, CMP +0) = 0x80000000
; out[5] = the word at byte 0x40 after it: -0 == +0 = 0x40400000
; out[6] = the word at byte 0x44 after fcmpswap (M 1.0, DATA 2.0, CMP 0x3F800001) = 0x3F800000
; out[7] = buffer_atomic_fcmpswap glc old value (M NaN at byte 0x48, DATA 1.0, CMP NaN) = 0x7FC00000
; out[7] = the word at byte 0x48 after fcmpswap (M NaN, DATA 1.0, CMP the same NaN) = 0x7FC00000
; The NaN case reads memory after the atomic: its returned old value is NaN whether or not the comparison matched.
; The atomic words are seeded with buffer_store_dword first. EXEC is cut to lane 0 before any memory op (one address
; per op, #1850).
; group: 32,1,1
@ -65,6 +66,8 @@ v_mov_b32_e32 v20, 0x40
buffer_load_dword v8, v20, s[0:3], 0 offen glc
v_mov_b32_e32 v20, 0x44
buffer_load_dword v9, v20, s[0:3], 0 offen glc
v_mov_b32_e32 v20, 0x48
buffer_load_dword v6, v20, s[0:3], 0 offen glc
s_waitcnt vmcnt(0)
v_mov_b32_e32 v20, 0
buffer_store_dword v14, v20, s[0:3], 0 offen

View File

@ -50,6 +50,8 @@
buffer_load_dword v8, v20, s[0:3], 0 offen glc
v_mov_b32_e32 v20, 0x44
buffer_load_dword v9, v20, s[0:3], 0 offen glc
v_mov_b32_e32 v20, 0x48
buffer_load_dword v6, v20, s[0:3], 0 offen glc
s_waitcnt vmcnt(0)
v_mov_b32_e32 v20, 0
buffer_store_dword v14, v20, s[0:3], 0 offen

View File

@ -45,10 +45,18 @@ EXPECTED = [
cmpst(ONE, TWO, THREE)[1], # ds_cmpst_f32, no match
cmpst(NZ, PZ, THREE)[0], cmpst(NZ, PZ, THREE)[1], # buffer_atomic_fcmpswap glc: old, then memory
cmpst(ONE, 0x3F800001, TWO)[1], # one ulp off: no match
cmpst(NAN, NAN, ONE)[0], # glc old value of the NaN case
cmpst(NAN, NAN, ONE)[1], # memory after the NaN case: unchanged
]
def run_words(spv):
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)
return [mem[4 * k] for k in range(8)]
class FcmpswapFixture(unittest.TestCase):
def test_reference(self):
self.assertEqual(EXPECTED, [0, 0x3F800000, 0x7FC00000, 0x3F800000, 0x80000000, 0x40400000, 0x3F800000,
@ -56,13 +64,23 @@ class FcmpswapFixture(unittest.TestCase):
def test_translated_shader(self):
spv = support.translate_fixture("cs_w32_fcmpswap", 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)
words = run_words(spv)
for k, want in enumerate(EXPECTED):
with self.subTest(word=k):
self.assertEqual(mem[4 * k], want)
self.assertEqual(words[k], want)
def test_nan_case_detects_integer_comparison(self):
# Integer comparison matches identical NaN bits and stores ONE. Both atomics return the old NaN, so this
# negative control catches a fixture that observes only that return instead of memory after the atomic.
with open(os.path.join(support.FIXTURE_DIR, "cs_w32_fcmpswap.s"), "rb") as f:
listing = f.read()
nan_op = b"buffer_atomic_fcmpswap v[6:7], v20, s[0:3], 0 offen glc"
self.assertEqual(listing.count(nan_op), 1)
listing = listing.replace(nan_op, nan_op.replace(b"fcmpswap", b"cmpswap"))
words = run_words(support.translate_listing("cs_w32_fcmpswap", listing, fssm=FSSM))
self.assertEqual(words[:-1], EXPECTED[:-1])
self.assertEqual(words[-1], ONE)
self.assertNotEqual(words, EXPECTED)
def test_gpu_runners_expect_the_same_words(self):
for rel in (("runtime", "test", "shaders", "check.py"), ("scripts", "run_gpu_tests.py")):