Skip to content
Merged
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
129 changes: 129 additions & 0 deletions tests/semantic/test_vopc_const_ordered.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
"""Semantic interpreter tests for the constant and ordered/unordered RDNA2 VOPC compares.

v_cmp_f_{i32,u32}, v_cmp_f_{f32,f16}: always false
v_cmp_t_{i32,u32}, v_cmp_tru_{f32,f16}: always true
v_cmp_o_{f32,f16}: ordered, true when neither source is NaN
v_cmp_u_{f32,f16}: unordered, true when either source is NaN
The result goes to VCC (VOPC), to an SGPR mask (VOP3) or, for v_cmpx, to EXEC. Each case translates a synthetic pixel
shader with tools/rdna2spirv.py, runs it in tests/semantic/spirv_mini_interp.py and compares lane 0's bit with a
plain-Python reference. The bit leaves through exp as 0x40000000 or 0x40000001 (always a normal float).
"""
import math
import os
import struct
import sys
import tempfile
import unittest
import warnings

HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(os.path.dirname(HERE))
TOOLS_DIR = os.environ.get("LEONIDA_TOOLS_DIR") or os.path.join(ROOT, "tools")

sys.dont_write_bytecode = True
sys.path.insert(0, TOOLS_DIR)
sys.path.insert(0, HERE)

import rdna2spirv # noqa: E402
import spirv_mini_interp as si # noqa: E402

STUB_DXBC = b"DXBC" + bytes(24) + struct.pack("<I", 0) + bytes(4)
F32 = [0x00000000, 0x80000000, 0x3F800000, 0xBF800000, 0x7F800000, 0xFF800000, 0x7FC00000, 0xFFC00000, 0x7F800001,
0x00000001]
F16 = [0x0000, 0x8000, 0x3C00, 0xBC00, 0x7C00, 0xFC00, 0x7E00, 0xFE00, 0x7C01, 0x0001]


def is_nan32(u: int) -> bool:
return (u & 0x7F800000) == 0x7F800000 and (u & 0x7FFFFF) != 0


def is_nan16(u: int) -> bool:
return (u & 0x7C00) == 0x7C00 and (u & 0x3FF) != 0


def ref(cond: str, nan_x: bool, nan_y: bool) -> int:
return int({"f": False, "t": True, "tru": True, "o": not (nan_x or nan_y), "u": nan_x or nan_y}[cond])


_TMP_DIR = None
_SRC_PATH = None


def setUpModule():
global _TMP_DIR, _SRC_PATH
_TMP_DIR = tempfile.TemporaryDirectory()
disasm_dir = os.path.join(_TMP_DIR.name, "disasm")
os.mkdir(disasm_dir)
_SRC_PATH = os.path.join(disasm_dir, "ps_vopc_const.s")
with open(os.path.join(_TMP_DIR.name, "ps_vopc_const.dxbc"), "wb") as f:
f.write(STUB_DXBC)


def tearDownModule():
global _TMP_DIR
if _TMP_DIR is not None:
_TMP_DIR.cleanup()
_TMP_DIR = None


def run_bit(x: int, y: int, line: str, mask: str = "vcc_lo", preset: int = 0) -> int:
"""v1 = x, v2 = y, the mask preset to all ones or zero, then ``line``; return lane 0's bit of ``mask``."""
if _TMP_DIR is None:
setUpModule()
lines = [f"v_mov_b32 v1, {x:#x}", f"v_mov_b32 v2, {y:#x}", f"s_mov_b32 vcc_lo, {-1 if preset else 0}",
f"s_mov_b32 s10, {-1 if preset else 0}", line, f"v_cndmask_b32 v0, 0x40000000, 0x40000001, {mask}"]
asm = "; test_vopc_const\n" + "".join(f"\t{ln}\n" for ln in lines) + "\texp mrt0 v0, v0, v0, v0 done vm\n\ts_endpgm\n"
with open(_SRC_PATH, "w", encoding="utf-8") as f:
f.write(asm)
with warnings.catch_warnings():
warnings.simplefilter("ignore", ResourceWarning)
spv = rdna2spirv.translate(_SRC_PATH)
w = si.run_fragment(spv).get("%out_mrt0")
if w is None:
return None # the lane was switched off (v_cmpx with a false result)
assert w[0] >> 1 == 0x20000000, hex(w[0])
return w[0] & 1


class ConstOrderedCompareTests(unittest.TestCase):
def test_reference(self):
self.assertTrue(is_nan32(0x7F800001) and not is_nan32(0x7F800000))
self.assertTrue(is_nan16(0x7C01) and not is_nan16(0x7C00))
self.assertEqual(ref("o", False, True), 0)
self.assertEqual(ref("u", False, True), 1)

def test_float_compares(self):
for ty, values, nan in (("f32", F32, is_nan32), ("f16", F16, is_nan16)):
for cond in ("f", "tru", "o", "u"):
for x in values:
for y in values[::3]:
with self.subTest(op=f"v_cmp_{cond}_{ty}", x=hex(x), y=hex(y)):
want = ref(cond, nan(x), nan(y))
got = run_bit(x, y, f"v_cmp_{cond}_{ty} vcc_lo, v1, v2", preset=1 - want)
self.assertEqual(got, want)

def test_integer_constant_compares(self):
for ty in ("u32", "i32"):
for cond in ("f", "t"):
for x, y in ((0, 0), (1, 2), (0xFFFFFFFF, 0)):
with self.subTest(op=f"v_cmp_{cond}_{ty}", x=x, y=y):
want = ref(cond, False, False)
self.assertEqual(run_bit(x, y, f"v_cmp_{cond}_{ty} vcc_lo, v1, v2", preset=1 - want), want)

def test_vop3_form_writes_the_named_mask(self):
self.assertEqual(run_bit(0x7FC00000, 0x3F800000, "v_cmp_u_f32_e64 s10, v1, v2", mask="s10"), 1)
self.assertEqual(run_bit(0x7FC00000, 0x3F800000, "v_cmp_o_f32_e64 s10, v1, v2", mask="s10", preset=1), 0)
self.assertEqual(run_bit(5, 6, "v_cmp_t_u32_e64 s10, v1, v2", mask="s10"), 1)
# VCC is left alone when the VOP3 form names another mask.
self.assertEqual(run_bit(5, 6, "v_cmp_t_u32_e64 s10, v1, v2", mask="vcc_lo"), 0)

def test_cmpx_writes_exec(self):
# v_cmpx: EXEC &= result. A true result keeps the lane (it exports); a false one switches it off.
self.assertEqual(run_bit(0x7FC00000, 0, "v_cmpx_u_f32 v1, v2", preset=1), 1)
self.assertIsNone(run_bit(0x3F800000, 0, "v_cmpx_u_f32 v1, v2", preset=1))
self.assertEqual(run_bit(0, 0, "v_cmpx_t_i32 v1, v2", preset=1), 1)
self.assertIsNone(run_bit(0, 0, "v_cmpx_f_u32 v1, v2", preset=1))


if __name__ == "__main__":
unittest.main()
22 changes: 19 additions & 3 deletions tools/rdna2spirv.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,10 +694,26 @@ def compare(self, op, a, scalar, exec_only=False):
"eq": "OpIEqual", "ne": "OpINotEqual", "lg": "OpINotEqual"}
else:
raise Unsupported(op)
if cond not in ops:
raise Unsupported(op)
is_float = ty in ("f32", "f16")
b, r = self.tmp(), self.tmp()
self.emit(f"{b} = {ops[cond]} %bool {x} {y}")
if cond in (("f", "tru", "o", "u") if is_float else ("f", "t")):
# Constant compares: f is always false, t / tru always true. o (ordered) is true when neither source
# is NaN, u (unordered) when either is.
if cond in ("o", "u"):
nx, ny, either = self.tmp(), self.tmp(), self.tmp()
self.emit(f"{nx} = OpIsNan %bool {x}")
self.emit(f"{ny} = OpIsNan %bool {y}")
self.emit(f"{either} = OpLogicalOr %bool {nx} {ny}")
if cond == "u":
b = either
else:
self.emit(f"{b} = OpLogicalNot %bool {either}")
else:
self.emit(f"{b} = OpCopyObject %bool {'%false' if cond == 'f' else '%true'}")
elif cond not in ops:
raise Unsupported(op)
else:
self.emit(f"{b} = {ops[cond]} %bool {x} {y}")
mask_halves = [self.mask_half(s) for s in srcs if self.holds_lane_mask(s)]
if scalar and mask_halves and ty in ("u32", "i32") and cond in ("eq", "ne", "lg"):
half = mask_halves[0] if (self.wave64 and len(set(mask_halves)) == 1) else None
Expand Down
Loading