Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 185 additions & 0 deletions tests/semantic/test_buffer_atomic_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
"""Synthetic validation tests for RDNA2 buffer_atomic* vdata register kind/width and lds/tfe modifiers.

Verifies that tools/rdna2spirv.py:
- Validates vdata register kind (must be VGPR) and width (2 VGPRs for cmpswap/fcmpswap and _x2,
1 VGPR for other 32-bit ops and fmin/fmax) across buffer_atomic, buffer_atomic_float, and
buffer_atomic_x2, raising rdna2spirv.Unsupported instead of KeyError or silent out-of-range VGPR
reads.
- Rejects unsupported `lds` and `tfe` modifiers with rdna2spirv.Unsupported across all three
buffer_atomic* handlers.
- Translates valid buffer_atomic* forms cleanly and passes spirv-val --target-env vulkan1.2.

Standard library only (spirv-as / spirv-val used when on PATH); no GPU, no LLVM, no game files.
"""
import os
import shutil
import struct
import subprocess
import sys
import tempfile
import unittest
import warnings

HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(os.path.dirname(HERE))
sys.dont_write_bytecode = True
sys.path.insert(0, os.environ.get("LEONIDA_TOOLS_DIR") or os.path.join(ROOT, "tools"))

import rdna2spirv # noqa: E402

STUB_DXBC = b"DXBC" + bytes(24) + struct.pack("<I", 0) + bytes(4)
FSSM = bytearray(0x148)
struct.pack_into("<4I", FSSM, 0x138, 0x88, 32, 1, 1) # 4 user SGPRs, workgroup id in s4, 32x1x1
FSSM = bytes(FSSM)

SETUP_LINES = [
"s_mov_b32 s8, 0",
"s_mov_b32 s9, 0",
"s_mov_b32 s10, 0x100",
"s_mov_b32 s11, 0x31016fac",
"v_mov_b32_e32 v1, 0x40",
"v_mov_b32_e32 v2, 1",
"v_mov_b32_e32 v3, 2",
"v_mov_b32_e32 v4, 3",
"v_mov_b32_e32 v5, 4",
]

SINGLE_DWORD_OPS = (
"buffer_atomic_add",
"buffer_atomic_sub",
"buffer_atomic_umin",
"buffer_atomic_umax",
"buffer_atomic_smin",
"buffer_atomic_smax",
"buffer_atomic_and",
"buffer_atomic_or",
"buffer_atomic_xor",
"buffer_atomic_swap",
"buffer_atomic_inc",
"buffer_atomic_dec",
"buffer_atomic_csub",
)

CMPSWAP_OPS = (
"buffer_atomic_cmpswap",
"buffer_atomic_fcmpswap",
)

FLOAT_OPS = (
"buffer_atomic_fmin",
"buffer_atomic_fmax",
)

X2_OPS = (
"buffer_atomic_and_x2",
"buffer_atomic_or_x2",
"buffer_atomic_xor_x2",
"buffer_atomic_swap_x2",
)


def translate(body):
with tempfile.TemporaryDirectory() as tmp:
for d in ("disasm", "fssm"):
os.mkdir(os.path.join(tmp, d))
with open(os.path.join(tmp, "cs_baval.dxbc"), "wb") as f:
f.write(STUB_DXBC)
with open(os.path.join(tmp, "fssm", "cs_baval.fssm"), "wb") as f:
f.write(FSSM)
src = os.path.join(tmp, "disasm", "cs_baval.s")
with open(src, "w", encoding="utf-8", newline="\n") as f:
f.write("; cs_baval\n" + "".join(f"\t{ln}\n" for ln in body) + "\ts_endpgm\n")
with warnings.catch_warnings():
warnings.simplefilter("ignore", ResourceWarning)
return rdna2spirv.translate(src)


def validate_spirv(spv_text):
spirv_as = shutil.which("spirv-as")
spirv_val = shutil.which("spirv-val")
if not (spirv_as and spirv_val):
return False
with tempfile.TemporaryDirectory() as tmp:
asm_path = os.path.join(tmp, "shader.spvasm")
spv_path = os.path.join(tmp, "shader.spv")
with open(asm_path, "w", encoding="utf-8", newline="\n") as f:
f.write(spv_text)
r1 = subprocess.run([spirv_as, "--target-env", "vulkan1.2", asm_path, "-o", spv_path],
capture_output=True, text=True)
if r1.returncode != 0:
raise AssertionError(f"spirv-as failed:\n{r1.stderr}")
r2 = subprocess.run([spirv_val, "--target-env", "vulkan1.2", spv_path],
capture_output=True, text=True)
if r2.returncode != 0:
raise AssertionError(f"spirv-val failed:\n{r2.stderr}")
return True


class BufferAtomicValidationTest(unittest.TestCase):
def test_non_vgpr_vdata_rejected(self):
cases = (
[(op, bad) for op in ("buffer_atomic_add", "buffer_atomic_sub", "buffer_atomic_swap",
"buffer_atomic_inc", "buffer_atomic_dec", "buffer_atomic_csub")
for bad in ("s0", "0", "1", "off")]
+ [(op, bad) for op in CMPSWAP_OPS for bad in ("s[0:1]", "s0", "0", "off")]
+ [(op, bad) for op in FLOAT_OPS for bad in ("s0", "0", "off")]
+ [(op, bad) for op in X2_OPS for bad in ("s[0:1]", "s0", "0", "off")]
)
for op, bad_vd in cases:
line = f"{op} {bad_vd}, v1, s[8:11], 0 offen glc"
with self.subTest(line=line):
with self.assertRaises(rdna2spirv.Unsupported):
translate(SETUP_LINES + [line])

def test_wrong_vgpr_width_rejected(self):
cases = (
[(op, bad) for op in SINGLE_DWORD_OPS for bad in ("v[2:3]", "v[2:5]")]
+ [(op, bad) for op in CMPSWAP_OPS for bad in ("v2", "v[2:4]", "v[2:5]")]
+ [(op, bad) for op in FLOAT_OPS for bad in ("v[2:3]", "v[2:5]")]
+ [(op, bad) for op in X2_OPS for bad in ("v2", "v[2:4]", "v[2:5]")]
)
for op, bad_vd in cases:
line = f"{op} {bad_vd}, v1, s[8:11], 0 offen glc"
with self.subTest(line=line):
with self.assertRaises(rdna2spirv.Unsupported):
translate(SETUP_LINES + [line])

def test_lds_and_tfe_modifiers_rejected(self):
ops_with_valid_vdata = (
[(op, "v2") for op in ("buffer_atomic_add", "buffer_atomic_sub", "buffer_atomic_swap",
"buffer_atomic_inc", "buffer_atomic_dec", "buffer_atomic_csub")]
+ [(op, "v[2:3]") for op in CMPSWAP_OPS]
+ [(op, "v2") for op in FLOAT_OPS]
+ [(op, "v[2:3]") for op in X2_OPS]
)
for op, vd in ops_with_valid_vdata:
for mod in ("lds", "tfe"):
for extra in ("offen", "offen glc"):
line = f"{op} {vd}, v1, s[8:11], 0 {extra} {mod}"
with self.subTest(line=line):
with self.assertRaises(rdna2spirv.Unsupported):
translate(SETUP_LINES + [line])

def test_valid_forms_translate_and_validate(self):
valid_ops = (
[(op, "v2") for op in ("buffer_atomic_add", "buffer_atomic_sub", "buffer_atomic_swap",
"buffer_atomic_inc", "buffer_atomic_dec", "buffer_atomic_csub")]
+ [(op, "v[2:3]") for op in CMPSWAP_OPS]
+ [(op, "v2") for op in FLOAT_OPS]
+ [(op, "v[2:3]") for op in X2_OPS]
)
validated_any = False
for op, vd in valid_ops:
for suffix in ("offen", "offen glc"):
line = f"{op} {vd}, v1, s[8:11], 0 {suffix}"
with self.subTest(line=line):
spv = translate(SETUP_LINES + [line])
self.assertIn("OpEntryPoint GLCompute", spv)
if validate_spirv(spv):
validated_any = True
if not validated_any:
self.skipTest("spirv-as / spirv-val not on PATH")


if __name__ == "__main__":
unittest.main()
8 changes: 6 additions & 2 deletions tools/rdna2spirv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2882,8 +2882,10 @@ def buffer_atomic(self, op, a, mods):
fcmp = kind == "fcmpswap" # float compare: see float_cmpswap_loop
if (kind not in self.BUF_ATOMICS and not is_inc_dec and not fcmp) or "x2" in kind:
raise Unsupported(op)
if any(m in mods for m in ("lds", "tfe")):
raise Unsupported(op + " form")
vd = a[0]
if is_inc_dec and (vd["k"] != "vgpr" or vd.get("n", 1) != 1):
if vd["k"] != "vgpr" or vd.get("n", 1) != (2 if kind in ("cmpswap", "fcmpswap") else 1):
raise Unsupported(op + " data")
addr, _, in_b, _ = self.buffer_address(a, mods)
val = self.load_reg("vgpr", vd["r"])
Expand Down Expand Up @@ -2927,7 +2929,7 @@ def buffer_atomic_float(self, op, kind, a, mods):
the shared OpAtomicCompareExchange + GLSL.std.450 NMin/NMax CAS loop (no float-atomic extension needed).
Enforces V# bounds and 4-byte natural alignment; with glc returns the old word (0 when OOB/unaligned,
preserved when inactive)."""
if "lds" in mods:
if any(m in mods for m in ("lds", "tfe")):
raise Unsupported(op + " form")
vd = a[0]
if vd["k"] != "vgpr" or vd.get("n", 1) != 1:
Expand All @@ -2951,6 +2953,8 @@ def buffer_atomic_x2(self, op, kind, a, mods):
between them could be seen half done. Measured natively on RDNA2 (gfx1036, wave32 & wave64): the bounds check
covers the whole 8-byte element all or nothing (fixed_fmt=62) and addresses with (addr & 7) != 0 are dropped
(memory untouched, active glc returns (0, 0), inactive preserves vdata)."""
if any(m in mods for m in ("lds", "tfe")):
raise Unsupported(op + " form")
vd = a[0]
if vd["k"] != "vgpr" or vd.get("n", 1) != 2:
raise Unsupported(op + " data")
Expand Down