mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 09:08:38 +00:00
Merge pull request #1110 from klikso/carry/shader-w64-sink-race
rdna2spirv: wave64 emulation resets shared words with OpAtomicStore (SHADER-W64-SINK-RACE-CARRY)
This commit is contained in:
@ -435,6 +435,9 @@ def run_compute_workgroup(asm, threads, subgroup_size, push_constants=None, memo
|
||||
elif op == "OpStore":
|
||||
p = base_ptr(inv, args[0])
|
||||
write_ptr(inv, p, val_of(inv, args[1]))
|
||||
elif op == "OpAtomicStore": # pointer, scope, semantics, value: a store with no result
|
||||
p = base_ptr(inv, args[0])
|
||||
write_ptr(inv, p, val_of(inv, args[3]))
|
||||
elif op == "OpConvertUToPtr":
|
||||
inv.ssa[res] = ("phys", val_of(inv, args[1]))
|
||||
elif op == "OpUConvert":
|
||||
|
||||
@ -411,6 +411,14 @@ def run_emulated_workgroup(spvasm, local_size, push_constants=None, memory=None,
|
||||
memory[p[1]] = v & MASK32
|
||||
else:
|
||||
p[0][p[1]] = v
|
||||
elif op == "OpAtomicStore": # pointer, scope, semantics, value: a store with no result
|
||||
p = ptrs[a[0]]
|
||||
v = vals[a[3]]
|
||||
if p[0] == "psb":
|
||||
assert p[1] % 4 == 0, f"unaligned PSB store {p[1]:#x}"
|
||||
memory[p[1]] = v & MASK32
|
||||
else:
|
||||
p[0][p[1]] = v
|
||||
elif op in ("OpAtomicOr", "OpAtomicIAdd", "OpAtomicAnd", "OpAtomicXor", "OpAtomicExchange"):
|
||||
p = ptrs[a[1]]
|
||||
old = memory.get(p[1], 0) if p[0] == "psb" else p[0][p[1]]
|
||||
@ -1760,5 +1768,57 @@ class StructuredEmulationTests(unittest.TestCase):
|
||||
self.assertEqual(plan["dirty_in"][4], frozenset({("mem", "r")}))
|
||||
|
||||
|
||||
class SharedWordStoresTests(unittest.TestCase):
|
||||
"""The emulation resets %x_buf words every round. Lanes with nothing to reset aim the same store at one sink
|
||||
word, and the prologue clamps its zeroing index, so several invocations write one word at once. A plain OpStore
|
||||
there is a data race (GPU-assisted validation reports it in every emulated shader); OpAtomicStore is not."""
|
||||
|
||||
INSNS = [
|
||||
("v_cmp_lt_u32_e64 s[6:7], 20, v0", 8),
|
||||
("s_and_saveexec_b64 s[8:9], s[6:7]", 4),
|
||||
("s_cselect_b32 s14, 111, 222", 4),
|
||||
("s_mov_b64 exec, -1", 4),
|
||||
("v_mov_b32_e32 v5, s14", 4),
|
||||
("v_lshlrev_b32_e32 v6, 2, v0", 4),
|
||||
("buffer_store_dword v5, v6, s[0:3], 0 offen", 8),
|
||||
("s_endpgm", 4),
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def shared_word_stores(spv):
|
||||
"""(plain, atomic) stores into %x_buf whose index is not one invocation's own: it comes from an OpSelect
|
||||
(the sink) or from the prologue's UMin clamp."""
|
||||
defined_by, element = {}, {}
|
||||
for line in spv.splitlines():
|
||||
m = re.match(r"\s*(%\w+) = (\w+) (.*)", line)
|
||||
if not m:
|
||||
continue
|
||||
defined_by[m.group(1)] = m.group(2)
|
||||
chain = re.match(r"%_ptr_WG_uint_x %x_buf (%\w+)$", m.group(3)) if m.group(2) == "OpAccessChain" else None
|
||||
if chain:
|
||||
element[m.group(1)] = chain.group(1)
|
||||
plain, atomic = [], []
|
||||
for line in spv.splitlines():
|
||||
m = re.match(r"\s*(OpStore|OpAtomicStore) (%\w+) ", line)
|
||||
if m and defined_by.get(element.get(m.group(2))) in ("OpSelect", "OpExtInst"):
|
||||
(plain if m.group(1) == "OpStore" else atomic).append(line.strip())
|
||||
return plain, atomic
|
||||
|
||||
def test_stores_several_invocations_aim_at_one_word_are_atomic(self):
|
||||
for threads in (64, 128): # one wave, and two (the per-wave and per-workgroup resets differ)
|
||||
with self.subTest(threads=threads):
|
||||
spv, _ = _translate_encoded(f"cs_shared_words_{threads}", self.INSNS, (threads, 1, 1), emulate=True)
|
||||
plain, atomic = self.shared_word_stores(spv)
|
||||
self.assertEqual(plain, [])
|
||||
self.assertTrue(atomic, "the emulated translation has no shared-word resets to check")
|
||||
# Workgroup scope, relaxed semantics, value 0: the same operands as the OpAtomicOr on this buffer.
|
||||
for line in atomic:
|
||||
self.assertRegex(line, r"^OpAtomicStore %\w+ %c2 %c0 %c0$")
|
||||
|
||||
def test_native_wave64_translation_is_unchanged(self):
|
||||
spv, _ = _translate_encoded("cs_shared_words_native", self.INSNS, (64, 1, 1), emulate=False)
|
||||
self.assertNotIn("OpAtomicStore", spv)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -5499,6 +5499,10 @@ class Gen:
|
||||
# [2L, 2L + 6W) two mask words per wave, ORed atomically, 3 slots by round mod 3 (W = waves)
|
||||
# [2L + 6W, +6) per round slot: lanes not done, lanes neither done nor waiting at s_barrier
|
||||
# last word a sink for the stores of lanes that have nothing to reset
|
||||
# A reset store goes to its own word in one lane and to the sink in all the others, and the prologue's zeroing
|
||||
# clamps its index, so several invocations write one word at once. Those stores are OpAtomicStore: with a plain
|
||||
# OpStore that is a data race, harmless here (the word is never read) but reported by GPU-assisted validation in
|
||||
# every emulated shader, where it would bury a real one.
|
||||
def x_sizes(self):
|
||||
w = self.x_lanes // 64
|
||||
mask = 2 * self.x_lanes
|
||||
@ -5605,9 +5609,9 @@ class Gen:
|
||||
self.emit(f"{m5} = OpSelect %uint {ml} {m3} {self.cu(sink)}")
|
||||
self.emit(f"{m6} = OpSelect %uint {ml} {m4} {self.cu(sink)}")
|
||||
self.emit(f"{mp5} = OpAccessChain %_ptr_WG_uint_x %x_buf {m5}")
|
||||
self.emit(f"OpStore {mp5} {self.cu(0)}")
|
||||
self.emit(f"OpAtomicStore {mp5} {self.cu(2)} {self.cu(0)} {self.cu(0)}")
|
||||
self.emit(f"{mp6} = OpAccessChain %_ptr_WG_uint_x %x_buf {m6}")
|
||||
self.emit(f"OpStore {mp6} {self.cu(0)}")
|
||||
self.emit(f"OpAtomicStore {mp6} {self.cu(2)} {self.cu(0)} {self.cu(0)}")
|
||||
else:
|
||||
p_lane, p_wave = params["lane"], params["wave"]
|
||||
m0, m1, m2, m3, m4, ml, m5, m6, mp5, mp6 = (self.tmp() for _ in range(10))
|
||||
@ -5620,9 +5624,9 @@ class Gen:
|
||||
self.emit(f"{m5} = OpSelect %uint {ml} {m3} {self.cu(sink)}")
|
||||
self.emit(f"{m6} = OpSelect %uint {ml} {m4} {self.cu(sink)}")
|
||||
self.emit(f"{mp5} = OpAccessChain %_ptr_WG_uint_x %x_buf {m5}")
|
||||
self.emit(f"OpStore {mp5} {self.cu(0)}")
|
||||
self.emit(f"OpAtomicStore {mp5} {self.cu(2)} {self.cu(0)} {self.cu(0)}")
|
||||
self.emit(f"{mp6} = OpAccessChain %_ptr_WG_uint_x %x_buf {m6}")
|
||||
self.emit(f"OpStore {mp6} {self.cu(0)}")
|
||||
self.emit(f"OpAtomicStore {mp6} {self.cu(2)} {self.cu(0)} {self.cu(0)}")
|
||||
q0, q1, q2, ql, q3, q4, qp3, qp4 = (self.tmp() for _ in range(8))
|
||||
self.emit(f"{q0} = OpIMul %uint {sn} {self.cu(2)}")
|
||||
self.emit(f"{q1} = OpIAdd %uint {q0} {self.cu(sched)}")
|
||||
@ -5631,9 +5635,9 @@ class Gen:
|
||||
self.emit(f"{q3} = OpSelect %uint {ql} {q1} {self.cu(sink)}")
|
||||
self.emit(f"{q4} = OpSelect %uint {ql} {q2} {self.cu(sink)}")
|
||||
self.emit(f"{qp3} = OpAccessChain %_ptr_WG_uint_x %x_buf {q3}")
|
||||
self.emit(f"OpStore {qp3} {self.cu(0)}")
|
||||
self.emit(f"OpAtomicStore {qp3} {self.cu(2)} {self.cu(0)} {self.cu(0)}")
|
||||
self.emit(f"{qp4} = OpAccessChain %_ptr_WG_uint_x %x_buf {q4}")
|
||||
self.emit(f"OpStore {qp4} {self.cu(0)}")
|
||||
self.emit(f"OpAtomicStore {qp4} {self.cu(2)} {self.cu(0)} {self.cu(0)}")
|
||||
if kind in ("data", "both"):
|
||||
par, data_base, i, p = (self.tmp() for _ in range(4))
|
||||
self.emit(f"{par} = OpBitwiseAnd %uint {p_it} {self.cu(1)}")
|
||||
@ -18631,7 +18635,7 @@ def x_prologue(gen, threads):
|
||||
L += [f"%x_z{k} = OpIAdd %uint %x_li {c(k * threads)}",
|
||||
f"%x_zm{k} = OpExtInst %uint %glsl UMin %x_z{k} {c(zero - 1)}",
|
||||
f"%x_zi{k} = OpIAdd %uint %x_zm{k} {c(x_mask)}",
|
||||
f"%x_zp{k} = OpAccessChain %_ptr_WG_uint_x %x_buf %x_zi{k}", f"OpStore %x_zp{k} {c(0)}"]
|
||||
f"%x_zp{k} = OpAccessChain %_ptr_WG_uint_x %x_buf %x_zi{k}", f"OpAtomicStore %x_zp{k} {c(2)} {c(0)} {c(0)}"]
|
||||
# A partial last wave: its missing lanes never write their data slots. They read as 0, as a lane that is
|
||||
# off does for ds_bpermute (and v_readlane of a lane that doesn't exist has no defined value anyway).
|
||||
missing = gen.x_lanes - threads
|
||||
@ -18641,7 +18645,7 @@ def x_prologue(gen, threads):
|
||||
f"%x_ym{k}_{par} = OpExtInst %uint %glsl UMin %x_y{k}_{par} {c(missing - 1)}",
|
||||
f"%x_yi{k}_{par} = OpIAdd %uint %x_ym{k}_{par} {c(base)}",
|
||||
f"%x_yp{k}_{par} = OpAccessChain %_ptr_WG_uint_x %x_buf %x_yi{k}_{par}",
|
||||
f"OpStore %x_yp{k}_{par} {c(0)}"]
|
||||
f"OpAtomicStore %x_yp{k}_{par} {c(2)} {c(0)} {c(0)}"]
|
||||
L.append(f"OpControlBarrier {c(2)} {c(2)} {c(0x948)}")
|
||||
return L
|
||||
|
||||
@ -18661,13 +18665,13 @@ def x_round_loop_head(gen, seg_ids):
|
||||
f"%x_m2 = OpIMul %uint %x_m1 {c(2)}", f"%x_m3 = OpIAdd %uint %x_m2 {c(x_mask)}",
|
||||
f"%x_m4 = OpIAdd %uint %x_m3 {c(1)}", f"%x_ml = OpIEqual %bool %x_lane {c(0)}",
|
||||
f"%x_m5 = OpSelect %uint %x_ml %x_m3 {c(x_sink)}", f"%x_m6 = OpSelect %uint %x_ml %x_m4 {c(x_sink)}",
|
||||
"%x_mp5 = OpAccessChain %_ptr_WG_uint_x %x_buf %x_m5", f"OpStore %x_mp5 {c(0)}",
|
||||
"%x_mp6 = OpAccessChain %_ptr_WG_uint_x %x_buf %x_m6", f"OpStore %x_mp6 {c(0)}",
|
||||
"%x_mp5 = OpAccessChain %_ptr_WG_uint_x %x_buf %x_m5", f"OpAtomicStore %x_mp5 {c(2)} {c(0)} {c(0)}",
|
||||
"%x_mp6 = OpAccessChain %_ptr_WG_uint_x %x_buf %x_m6", f"OpAtomicStore %x_mp6 {c(2)} {c(0)} {c(0)}",
|
||||
f"%x_q0 = OpIMul %uint %x_sn {c(2)}", f"%x_q1 = OpIAdd %uint %x_q0 {c(x_sched)}",
|
||||
f"%x_q2 = OpIAdd %uint %x_q1 {c(1)}", f"%x_ql = OpIEqual %bool %x_li {c(0)}",
|
||||
f"%x_q3 = OpSelect %uint %x_ql %x_q1 {c(x_sink)}", f"%x_q4 = OpSelect %uint %x_ql %x_q2 {c(x_sink)}",
|
||||
"%x_qp3 = OpAccessChain %_ptr_WG_uint_x %x_buf %x_q3", f"OpStore %x_qp3 {c(0)}",
|
||||
"%x_qp4 = OpAccessChain %_ptr_WG_uint_x %x_buf %x_q4", f"OpStore %x_qp4 {c(0)}",
|
||||
"%x_qp3 = OpAccessChain %_ptr_WG_uint_x %x_buf %x_q3", f"OpAtomicStore %x_qp3 {c(2)} {c(0)} {c(0)}",
|
||||
"%x_qp4 = OpAccessChain %_ptr_WG_uint_x %x_buf %x_q4", f"OpAtomicStore %x_qp4 {c(2)} {c(0)} {c(0)}",
|
||||
"%x_d = OpLoad %bool %done", "%x_b = OpLoad %bool %x_bar", "%x_nd = OpLogicalNot %bool %x_d",
|
||||
"%x_nb = OpLogicalNot %bool %x_b", "%x_run = OpLogicalAnd %bool %x_nd %x_nb",
|
||||
"OpSelectionMerge %x_ran None", "OpBranchConditional %x_run %dispatch %x_ran",
|
||||
@ -18694,14 +18698,14 @@ def x_mini_scheduler(gen, midx, seg_ids):
|
||||
f"{p}_m4 = OpIAdd %uint {p}_m3 {c(1)}", f"{p}_ml = OpIEqual %bool %x_lane {c(0)}",
|
||||
f"{p}_m5 = OpSelect %uint {p}_ml {p}_m3 {c(x_sink)}",
|
||||
f"{p}_m6 = OpSelect %uint {p}_ml {p}_m4 {c(x_sink)}",
|
||||
f"{p}_mp5 = OpAccessChain %_ptr_WG_uint_x %x_buf {p}_m5", f"OpStore {p}_mp5 {c(0)}",
|
||||
f"{p}_mp6 = OpAccessChain %_ptr_WG_uint_x %x_buf {p}_m6", f"OpStore {p}_mp6 {c(0)}",
|
||||
f"{p}_mp5 = OpAccessChain %_ptr_WG_uint_x %x_buf {p}_m5", f"OpAtomicStore {p}_mp5 {c(2)} {c(0)} {c(0)}",
|
||||
f"{p}_mp6 = OpAccessChain %_ptr_WG_uint_x %x_buf {p}_m6", f"OpAtomicStore {p}_mp6 {c(2)} {c(0)} {c(0)}",
|
||||
f"{p}_q0 = OpIMul %uint {p}_sn {c(2)}", f"{p}_q1 = OpIAdd %uint {p}_q0 {c(x_sched)}",
|
||||
f"{p}_q2 = OpIAdd %uint {p}_q1 {c(1)}", f"{p}_ql = OpIEqual %bool %x_li {c(0)}",
|
||||
f"{p}_q3 = OpSelect %uint {p}_ql {p}_q1 {c(x_sink)}",
|
||||
f"{p}_q4 = OpSelect %uint {p}_ql {p}_q2 {c(x_sink)}",
|
||||
f"{p}_qp3 = OpAccessChain %_ptr_WG_uint_x %x_buf {p}_q3", f"OpStore {p}_qp3 {c(0)}",
|
||||
f"{p}_qp4 = OpAccessChain %_ptr_WG_uint_x %x_buf {p}_q4", f"OpStore {p}_qp4 {c(0)}",
|
||||
f"{p}_qp3 = OpAccessChain %_ptr_WG_uint_x %x_buf {p}_q3", f"OpAtomicStore {p}_qp3 {c(2)} {c(0)} {c(0)}",
|
||||
f"{p}_qp4 = OpAccessChain %_ptr_WG_uint_x %x_buf {p}_q4", f"OpAtomicStore {p}_qp4 {c(2)} {c(0)} {c(0)}",
|
||||
f"{p}_d = OpLoad %bool %x_ldone", f"{p}_run = OpLogicalNot %bool {p}_d",
|
||||
f"OpSelectionMerge {p}_ran None", f"OpBranchConditional {p}_run {p}_dispatch {p}_ran",
|
||||
f"{p}_dispatch = OpLabel", f"OpBranch {p}_ch_head", f"{p}_ch_head = OpLabel",
|
||||
|
||||
Reference in New Issue
Block a user