"""Semantic tests for plain 64-bit scalar data in VCC (and null) on 64-bit scalar bit-count and bit-scan ops. Covers s_bcnt1_i32_b64, s_bcnt0_i32_b64, s_ff1_i32_b64, s_ff0_i32_b64, s_flbit_i32_b64, and s_flbit_i32_i64 when VCC holds plain 64-bit scalar data rather than a lane mask, in both wave64 and wave32, plus null sources, lane-mask-to-data transitions, and a real lane-mask control. """ import gc import json 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 for p in (HERE, TOOLS_DIR): if p not in sys.path: sys.path.insert(0, p) import rdna2spirv # noqa: E402 import spirv_mini_interp as si # noqa: E402 M32 = 0xFFFFFFFF STUB_DXBC = b"DXBC" + bytes(24) + struct.pack("> 63) else v flbit_i64 = (64 - signed_src.bit_length()) if signed_src else M32 return [bcnt1, bcnt1_scc, bcnt0, bcnt0_scc, ff1, ff0, flbit_b64, flbit_i64] def _store_words(sregs): out = ["v_mov_b32_e32 v2, 0"] for i, sr in enumerate(sregs): out.append(f"v_mov_b32_e32 v1, {sr}") out.append(f"buffer_store_dword v1, v2, s[0:3], 0 offen offset:{4 * i}") out.append("s_endpgm") return out class B64BitopsVccDataTests(unittest.TestCase): PAIRS = ( (0x00000003, 0x0000000F), (0x00000000, 0x00000008), (0x00000000, 0x80000000), (0xFFFFFFFF, 0xFFFFFFFF), (0xFFFFFFFF, 0xFFFFFFF7), (0x00000000, 0x00100000), (0xFFFFFFFF, 0xFFEFFFFF), (0x00008000, 0x00000000), (0x00000000, 0x00000000), ) def _program_vcc_data(self, prefix=()): return list(prefix) + [ "s_mov_b32 vcc_lo, s4", "s_mov_b32 vcc_hi, s5", "s_bcnt1_i32_b64 s6, vcc", "s_cselect_b32 s7, 1, 0", "s_bcnt0_i32_b64 s8, vcc", "s_cselect_b32 s9, 1, 0", "s_ff1_i32_b64 s10, vcc", "s_ff0_i32_b64 s11, vcc", "s_flbit_i32_b64 s12, vcc", "s_flbit_i32_i64 s13, vcc", ] + _store_words(["s6", "s7", "s8", "s9", "s10", "s11", "s12", "s13"]) def test_plain_vcc_data_wave64(self): spv = _translate_cs("cs_w64_b64_vcc_data", self._program_vcc_data(), wave32=False) for lo, hi in self.PAIRS: with self.subTest(lo=hex(lo), hi=hex(hi)): self.assertEqual(_run_cs(spv, lo, hi, wave32=False), _ref_b64_ops(lo, hi)) def test_bcnt_and_ff_plain_vcc_data_reads_high_word(self): # Isolates s_bcnt[01]_i32_b64 and s_ff[01]_i32_b64 (without s_flbit) so a missing %vcc_hi load # fails with wrong numeric outputs rather than an earlier translation error. lines = [ "s_mov_b32 vcc_lo, s4", "s_mov_b32 vcc_hi, s5", "s_bcnt1_i32_b64 s6, vcc", "s_cselect_b32 s7, 1, 0", "s_bcnt0_i32_b64 s8, vcc", "s_cselect_b32 s9, 1, 0", "s_ff1_i32_b64 s10, vcc", "s_ff0_i32_b64 s11, vcc", ] + _store_words(["s6", "s7", "s8", "s9", "s10", "s11"]) spv = _translate_cs("cs_w64_bcnt_ff_vcc_data", lines, wave32=False) for lo, hi in ((0x3, 0xF), (0, 0x8), (M32, M32), (M32, 0xFFFFFFF7)): with self.subTest(lo=hex(lo), hi=hex(hi)): self.assertEqual(_run_cs(spv, lo, hi, wave32=False, n_words=6), _ref_b64_ops(lo, hi)[:6]) def test_plain_vcc_data_wave64_emulate(self): spv = _translate_cs("cs_w64e_b64_vcc_data", self._program_vcc_data(), wave32=False, emulate=True) for lo, hi in ((0x3, 0xF), (0, 0x8), (M32, M32), (0, 0)): with self.subTest(lo=hex(lo), hi=hex(hi)): self.assertEqual(_run_cs(spv, lo, hi, wave32=False, emulate=True), _ref_b64_ops(lo, hi)) def test_plain_vcc_data_flbit_wave32_fspx(self): # FSPX wave32 compute allows plain-data VCC pairs with s_flbit_i32_{b64,i64} # (s_bcnt/s_ff _b64 are strict wave64 markers in WAVE64_RE). lines = [ "s_mov_b32 vcc_lo, s4", "s_mov_b32 vcc_hi, s5", "s_flbit_i32_b64 s6, vcc", "s_flbit_i32_i64 s7, vcc", "s_flbit_i32_b64 s8, null", "s_flbit_i32_i64 s9, null", ] + _store_words(["s6", "s7", "s8", "s9"]) spv = _translate_cs("cs_w32_flbit_vcc_data", lines, wave32=True) for lo, hi in self.PAIRS: with self.subTest(lo=hex(lo), hi=hex(hi)): ref = _ref_b64_ops(lo, hi) self.assertEqual(_run_cs(spv, lo, hi, wave32=True, n_words=4), [ref[6], ref[7], M32, M32]) def test_vcc_transition_from_lane_mask_to_plain_data(self): # VCC first holds a real lane mask, then is overwritten with arbitrary plain 64-bit scalar data. spv = _translate_cs( "cs_w64_b64_vcc_mask_then_data", self._program_vcc_data(prefix=("v_cmp_lt_u32_e64 vcc, v0, 37",)), wave32=False, ) for lo, hi in ((0x3, 0xF), (0, 0x8), (M32, M32), (0, 0)): with self.subTest(lo=hex(lo), hi=hex(hi)): self.assertEqual(_run_cs(spv, lo, hi, wave32=False), _ref_b64_ops(lo, hi)) def test_null_source_b64_bitops(self): lines = [ "s_bcnt1_i32_b64 s6, null", "s_cselect_b32 s7, 1, 0", "s_bcnt0_i32_b64 s8, null", "s_cselect_b32 s9, 1, 0", "s_ff1_i32_b64 s10, null", "s_ff0_i32_b64 s11, null", "s_flbit_i32_b64 s12, null", "s_flbit_i32_i64 s13, null", ] + _store_words(["s6", "s7", "s8", "s9", "s10", "s11", "s12", "s13"]) spv = _translate_cs("cs_b64_null_64", lines, wave32=False) self.assertEqual(_run_cs(spv, 0, 0, wave32=False), _ref_b64_ops(0, 0)) def test_vcc_lane_mask_control_still_uses_wave_mask(self): # Negative control: when VCC holds a real lane mask (lanes 5..36 = 32 set bits), # the wave ballot path stays active. lines = [ "v_cmp_ge_u32_e64 vcc, v0, 5", "v_cmp_lt_u32_e64 s[14:15], v0, 37", "s_and_b64 vcc, vcc, s[14:15]", "s_bcnt1_i32_b64 s6, vcc", "s_bcnt0_i32_b64 s7, vcc", "s_ff1_i32_b64 s8, vcc", "s_ff0_i32_b64 s9, vcc", "s_flbit_i32_b64 s10, vcc", ] + _store_words(["s6", "s7", "s8", "s9", "s10"]) spv = _translate_cs("cs_w64_b64_vcc_mask_ctrl", lines, wave32=False) # Bits 5..36 set: 32 ones, 32 zeros, lowest 1 at 5, lowest 0 at 0, leading zeros = 64 - 37 = 27. self.assertEqual(_run_cs(spv, 0, 0, wave32=False, n_words=5), [32, 32, 5, 0, 27]) if __name__ == "__main__": unittest.main()