"""Semantic tests for RDNA2 global_atomic_fmin and global_atomic_fmax (32-bit float). Verifies the CAS loop lowering (OpAtomicCompareExchange + GLSL.std.450 NMin/NMax): - IEEE 754 float semantics: positives, negatives, signed zeros (-0.0 < +0.0), infinities, subnormals, and NaN handling (NaN operand returns the other operand). - Pre-op value return in vdst when 'glc' is present, and preservation of vdst when 'glc' is absent. - Addressing modes: saddr SGPR pair, saddr='off' with 64-bit vaddr, and signed 12-bit offsets (-2048..2044). - EXEC masking: inactive lanes bypass the CAS loop entirely, leave memory untouched, and retain vdst. - Multi-lane CAS contention: multiple active lanes racing on a single shared memory location. - Rejection of invalid modifiers (slc, dlc, lds), operand counts, and multi-dword registers. - Dependency analysis (read_before_write_vgprs). Note on hardware fidelity (measured natively on gfx1036 for #2361 by @Pierre-Kookis): - The expectations below verify what rdna2spirv's CAS lowering with GLSL.std.450 NMin/NMax produces today with denormals kept (Vulkan default). - On native hardware under the game's MODE (f32 denormals flushed), a denormal operand counts as a signed zero. - On native hardware, an sNaN in either operand wins and is stored quieted (0x7fc00001), whereas GLSL NMin/NMax returns the non-NaN operand. Standard library only; runs through spirv_mini_interp on the CPU without GPU, LLVM, or game files. """ from __future__ import annotations 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)) sys.dont_write_bytecode = True sys.path.insert(0, os.environ.get("LEONIDA_TOOLS_DIR") or os.path.join(ROOT, "tools")) sys.path.insert(0, HERE) import rdna2spirv # noqa: E402 import spirv_mini_interp as si # noqa: E402 from shader_ir import parse_line as parse # noqa: E402 STUB_DXBC = b"DXBC" + bytes(24) + struct.pack(" str: with tempfile.TemporaryDirectory() as tmp: for d in ("disasm", "fssm"): os.mkdir(os.path.join(tmp, d)) with open(os.path.join(tmp, "cs_gatomic_f.dxbc"), "wb") as f: f.write(STUB_DXBC) with open(os.path.join(tmp, "fssm", "cs_gatomic_f.fssm"), "wb") as f: f.write(FSSM) src = os.path.join(tmp, "disasm", "cs_gatomic_f.s") with open(src, "w", encoding="utf-8", newline="\n") as f: f.write("; cs_gatomic_f\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 run_cs(body: list[str], memory: dict[int, int], nlanes: int = 1) -> dict[int, int]: spv = translate(body) mem = dict(memory) si.run_workgroup(spv, nlanes, 32, inputs_for=lambda i: {"%wg_id": (0, 0, 0), "%local_id": (i, 0, 0), "%sg_lane": i}, push_constants=[0] * 36, memory=mem) return mem def run_atomic(kind: str, mem_init: int, data_val: int, glc: bool = True, offset: int = 0, vaddr: int = TARGET) -> tuple[int | None, int | None]: body = [ f"v_mov_b32_e32 v1, {vaddr - offset:#x}", f"v_mov_b32_e32 v2, {data_val:#x}", "v_mov_b32_e32 v5, 0xdeadbeef" ] off_str = f" offset:{offset}" if offset else "" if glc: body.append(f"global_atomic_{kind} v5, v1, v2, s[0:1]{off_str} glc") else: body.append(f"global_atomic_{kind} v1, v2, s[0:1]{off_str}") body += [ "s_waitcnt vmcnt(0)", f"v_mov_b32_e32 v6, {OUT:#x}", "global_store_dword v6, v5, s[0:1]" ] res_mem = run_cs(body, {TARGET: mem_init}) return res_mem.get(OUT), res_mem.get(TARGET) class GlobalAtomicFloatSemantics(unittest.TestCase): def test_fmin_fmax_standard_values(self): cases = [ # (kind, init_mem, data, expected_final) # Basic ordered values ("fmin", ONE, TWO, ONE), ("fmin", TWO, ONE, ONE), ("fmin", ONE, NEG_TWO, NEG_TWO), ("fmin", NEG_TWO, ONE, NEG_TWO), ("fmin", NEG_ONE, NEG_TWO, NEG_TWO), ("fmax", ONE, TWO, TWO), ("fmax", TWO, ONE, TWO), ("fmax", ONE, NEG_TWO, ONE), ("fmax", NEG_TWO, ONE, ONE), ("fmax", NEG_ONE, NEG_TWO, NEG_ONE), # Signed zeros (-0.0 < +0.0) ("fmin", POS_ZERO, NEG_ZERO, NEG_ZERO), ("fmin", NEG_ZERO, POS_ZERO, NEG_ZERO), ("fmax", POS_ZERO, NEG_ZERO, POS_ZERO), ("fmax", NEG_ZERO, POS_ZERO, POS_ZERO), # Infinities ("fmin", ONE, POS_INF, ONE), ("fmin", POS_INF, ONE, ONE), ("fmin", ONE, NEG_INF, NEG_INF), ("fmin", NEG_INF, ONE, NEG_INF), ("fmin", POS_INF, NEG_INF, NEG_INF), ("fmax", ONE, POS_INF, POS_INF), ("fmax", POS_INF, ONE, POS_INF), ("fmax", ONE, NEG_INF, ONE), ("fmax", NEG_INF, ONE, ONE), ("fmax", POS_INF, NEG_INF, POS_INF), # Subnormal numbers: holds with denormals kept (Vulkan default). # On gfx1036 in denormals-flushed MODE, denormals flush to signed zero (#2361). ("fmin", POS_ZERO, SUBNORM_MIN, POS_ZERO), ("fmin", SUBNORM_MAX, SUBNORM_MIN, SUBNORM_MIN), ("fmax", POS_ZERO, SUBNORM_MIN, SUBNORM_MIN), ("fmax", SUBNORM_MAX, SUBNORM_MIN, SUBNORM_MAX), # NaN handling: GLSL.std.450 NMin/NMax returns the non-NaN operand. # On native RDNA2 hardware (#2361), sNaN in either operand wins and is stored quieted. ("fmin", QNAN, ONE, ONE), ("fmin", ONE, QNAN, ONE), ("fmin", SNAN, NEG_TWO, NEG_TWO), ("fmin", NEG_TWO, SNAN, NEG_TWO), ("fmin", NEG_QNAN, HALF, HALF), ("fmax", QNAN, ONE, ONE), ("fmax", ONE, QNAN, ONE), ("fmax", SNAN, NEG_TWO, NEG_TWO), ("fmax", NEG_TWO, SNAN, NEG_TWO), ("fmax", NEG_QNAN, HALF, HALF), ] for kind, m_init, d_val, exp_final in cases: with self.subTest(op=kind, m=hex(m_init), d=hex(d_val)): out_vdst, final_mem = run_atomic(kind, m_init, d_val, glc=True) self.assertEqual(out_vdst, m_init, "glc must return pre-op memory value in vdst") self.assertEqual(final_mem, exp_final, "memory must reflect atomic fmin/fmax result") def test_both_nan_leaves_nan_in_memory(self): # When both memory and data are NaN, the result is NaN for kind in ("fmin", "fmax"): with self.subTest(op=kind): out_vdst, final_mem = run_atomic(kind, QNAN, SNAN, glc=True) self.assertEqual(out_vdst, QNAN) # Unpack as float32 to check NaN property f_val = struct.unpack("