diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 19:14:12 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 19:14:12 -0500 |
| commit | 61dbe21d97e72d8d837f7db3d76e4556a3f0cd17 (patch) | |
| tree | 021851631c360b9328fa9032b3b380566956870d | |
| parent | 0d278bbcbdee4aba0b0b7fbd7f2832d8aa8cd140 (diff) | |
| parent | e8b1469b7dd2b6bd35df4a45f33eb2fa294113b4 (diff) | |
Merge pull request #480 from slopgobblercobbler
| -rw-r--r-- | tests/semantic/test_scalar_alu_bitfield.py | 719 |
1 files changed, 719 insertions, 0 deletions
diff --git a/tests/semantic/test_scalar_alu_bitfield.py b/tests/semantic/test_scalar_alu_bitfield.py new file mode 100644 index 000000000..83f0bb7f7 --- /dev/null +++ b/tests/semantic/test_scalar_alu_bitfield.py | |||
| @@ -0,0 +1,719 @@ | |||
| 1 | """Semantic interpreter tests for RDNA2 scalar bitfield operations. | ||
| 2 | |||
| 3 | Instructions tested: | ||
| 4 | - s_bfe_u32 D, S0, S1: Unsigned scalar bitfield extract. | ||
| 5 | - s_bfe_i32 D, S0, S1: Signed scalar bitfield extract (sign-extended). | ||
| 6 | - s_bfm_b32 D, S0, S1: Scalar bitfield mask generation. | ||
| 7 | |||
| 8 | RDNA2 ISA rules: | ||
| 9 | - s_bfe_u32 / s_bfe_i32: | ||
| 10 | * S0 is the source dword (SGPR, immediate, or constant). | ||
| 11 | * S1 is the packed control word: | ||
| 12 | offset = S1[4:0] (bits [4:0], 0..31). | ||
| 13 | width = S1[22:16] (bits [22:16], 0..127). | ||
| 14 | Other bits of S1 are ignored. | ||
| 15 | * Width limit / clamping: SPIR-V specifies that offset + count <= 32. In RDNA2 | ||
| 16 | hardware and LLVM constantFoldBFE, an extract running past bit 31 is clamped | ||
| 17 | such that the effective width is min(width, 32 - offset). | ||
| 18 | * If width == 0 or offset >= 32: result is 0. | ||
| 19 | * s_bfe_u32 zero-extends the extracted field into D. | ||
| 20 | * s_bfe_i32 sign-extends the extracted field from bit (effective_width - 1). | ||
| 21 | * Condition Code (SCC): SCC = 1 if D != 0 else 0 (both signed and unsigned). | ||
| 22 | - s_bfm_b32: | ||
| 23 | * S0 is the width operand (only low 5 bits S0[4:0] are used). | ||
| 24 | * S1 is the offset operand (only low 5 bits S1[4:0] are used). | ||
| 25 | * D = ((1 << (S0 & 31)) - 1) << (S1 & 31). | ||
| 26 | * If (S0 & 31) == 0, result is 0. | ||
| 27 | * s_bfm_b32 does NOT alter the SCC flag. | ||
| 28 | - Scalar bitfield insert composition: | ||
| 29 | * Scalar bitfield insert is composed via s_bfm_b32 and bitwise selection: | ||
| 30 | mask = s_bfm_b32(width, offset) | ||
| 31 | result = (base & ~mask) | ((insert << offset) & mask) | ||
| 32 | * Matches SPIR-V OpBitFieldInsert semantics when offset + count <= 32. | ||
| 33 | |||
| 34 | Verification approach: | ||
| 35 | 1. Pure Python reference models verifying exact RDNA2 bitfield math. | ||
| 36 | 2. Deterministic ScalarAluInterpreter tracking SGPR state and SCC. | ||
| 37 | 3. rdna2spirv translation emission checks via rdna2spirv.Gen. | ||
| 38 | 4. spirv_mini_interp execution cross-checking lowered SPIR-V modules. | ||
| 39 | 5. Negative controls ensuring test sensitivity to subtle bitfield errors. | ||
| 40 | """ | ||
| 41 | from __future__ import annotations | ||
| 42 | |||
| 43 | import os | ||
| 44 | import sys | ||
| 45 | import unittest | ||
| 46 | |||
| 47 | HERE = os.path.dirname(os.path.abspath(__file__)) | ||
| 48 | ROOT = os.path.dirname(os.path.dirname(HERE)) | ||
| 49 | TOOLS_DIR = os.environ.get("LEONIDA_TOOLS_DIR") or os.path.join(ROOT, "tools") | ||
| 50 | |||
| 51 | if ROOT not in sys.path: | ||
| 52 | sys.path.insert(0, ROOT) | ||
| 53 | if TOOLS_DIR not in sys.path: | ||
| 54 | sys.path.insert(0, TOOLS_DIR) | ||
| 55 | |||
| 56 | if HERE not in sys.path: | ||
| 57 | sys.path.insert(0, HERE) | ||
| 58 | |||
| 59 | import rdna2spirv | ||
| 60 | import spirv_mini_interp as si | ||
| 61 | |||
| 62 | MASK32 = 0xFFFFFFFF | ||
| 63 | |||
| 64 | |||
| 65 | def pack_bfe_ctl(offset: int, width: int) -> int: | ||
| 66 | """Pack bitfield offset [4:0] and width [22:16] into an RDNA scalar control word.""" | ||
| 67 | return ((width & 0x7F) << 16) | (offset & 0x1F) | ||
| 68 | |||
| 69 | |||
| 70 | def unpack_bfe_ctl(ctl: int) -> tuple[int, int]: | ||
| 71 | """Unpack offset [4:0] and width [22:16] from an RDNA scalar control word.""" | ||
| 72 | offset = ctl & 0x1F | ||
| 73 | width = (ctl >> 16) & 0x7F | ||
| 74 | return offset, width | ||
| 75 | |||
| 76 | |||
| 77 | def ref_s_bfe_u32(src: int, ctl: int) -> tuple[int, int]: | ||
| 78 | """Reference implementation of RDNA2 s_bfe_u32. | ||
| 79 | |||
| 80 | Returns: | ||
| 81 | (result, scc): 32-bit unsigned extracted field and SCC flag (1 if res != 0 else 0). | ||
| 82 | """ | ||
| 83 | src &= MASK32 | ||
| 84 | offset, width = unpack_bfe_ctl(ctl) | ||
| 85 | if width == 0 or offset >= 32: | ||
| 86 | return 0, 0 | ||
| 87 | eff_width = min(width, 32 - offset) | ||
| 88 | mask = (1 << eff_width) - 1 | ||
| 89 | res = (src >> offset) & mask | ||
| 90 | scc = 1 if res != 0 else 0 | ||
| 91 | return res & MASK32, scc | ||
| 92 | |||
| 93 | |||
| 94 | def ref_s_bfe_i32(src: int, ctl: int) -> tuple[int, int]: | ||
| 95 | """Reference implementation of RDNA2 s_bfe_i32. | ||
| 96 | |||
| 97 | Returns: | ||
| 98 | (result, scc): 32-bit sign-extended extracted field and SCC flag (1 if res != 0 else 0). | ||
| 99 | """ | ||
| 100 | src &= MASK32 | ||
| 101 | offset, width = unpack_bfe_ctl(ctl) | ||
| 102 | if width == 0 or offset >= 32: | ||
| 103 | return 0, 0 | ||
| 104 | eff_width = min(width, 32 - offset) | ||
| 105 | mask = (1 << eff_width) - 1 | ||
| 106 | val = (src >> offset) & mask | ||
| 107 | sign_bit = 1 << (eff_width - 1) | ||
| 108 | if val & sign_bit: | ||
| 109 | val -= (1 << eff_width) | ||
| 110 | res = val & MASK32 | ||
| 111 | scc = 1 if res != 0 else 0 | ||
| 112 | return res, scc | ||
| 113 | |||
| 114 | |||
| 115 | def ref_s_bfm_b32(width: int, offset: int) -> int: | ||
| 116 | """Reference implementation of RDNA2 s_bfm_b32. | ||
| 117 | |||
| 118 | Mask generator: ((1 << (width & 31)) - 1) << (offset & 31). | ||
| 119 | Note: Does NOT alter SCC. | ||
| 120 | """ | ||
| 121 | w = width & 31 | ||
| 122 | o = offset & 31 | ||
| 123 | if w == 0: | ||
| 124 | return 0 | ||
| 125 | mask = ((1 << w) - 1) & MASK32 | ||
| 126 | return (mask << o) & MASK32 | ||
| 127 | |||
| 128 | |||
| 129 | def ref_bitfield_insert(base: int, insert: int, offset: int, count: int) -> int: | ||
| 130 | """Reference implementation of bitfield insert matching SPIR-V OpBitFieldInsert.""" | ||
| 131 | base &= MASK32 | ||
| 132 | insert &= MASK32 | ||
| 133 | if count == 0: | ||
| 134 | return base | ||
| 135 | mk = (((1 << count) - 1) << offset) & MASK32 | ||
| 136 | return ((base & ~mk) | ((insert << offset) & mk)) & MASK32 | ||
| 137 | |||
| 138 | |||
| 139 | class ScalarAluInterpreter: | ||
| 140 | """Deterministic semantic interpreter for RDNA2 scalar ALU bitfield instructions. | ||
| 141 | |||
| 142 | Models SGPR file (106 registers) and 1-bit Scalar Condition Code (SCC). | ||
| 143 | """ | ||
| 144 | |||
| 145 | def __init__(self, num_sgprs: int = 106) -> None: | ||
| 146 | self.num_sgprs = num_sgprs | ||
| 147 | self.sgpr = [0] * num_sgprs | ||
| 148 | self.scc = 0 | ||
| 149 | |||
| 150 | def reset(self) -> None: | ||
| 151 | self.sgpr = [0] * self.num_sgprs | ||
| 152 | self.scc = 0 | ||
| 153 | |||
| 154 | def get_sgpr(self, r: int) -> int: | ||
| 155 | return self.sgpr[r] & MASK32 | ||
| 156 | |||
| 157 | def set_sgpr(self, r: int, val: int) -> None: | ||
| 158 | self.sgpr[r] = val & MASK32 | ||
| 159 | |||
| 160 | def resolve_src(self, opnd: int | dict | str) -> int: | ||
| 161 | """Resolve scalar operand to 32-bit unsigned value.""" | ||
| 162 | if isinstance(opnd, int): | ||
| 163 | return opnd & MASK32 | ||
| 164 | if isinstance(opnd, dict): | ||
| 165 | k = opnd.get("k") | ||
| 166 | if k == "imm": | ||
| 167 | return opnd["v"] & MASK32 | ||
| 168 | if k == "sgpr": | ||
| 169 | return self.get_sgpr(opnd["r"]) | ||
| 170 | raise ValueError(f"Unknown operand dict: {opnd}") | ||
| 171 | if isinstance(opnd, str): | ||
| 172 | if opnd.startswith("s"): | ||
| 173 | return self.get_sgpr(int(opnd[1:])) | ||
| 174 | return int(opnd, 0) & MASK32 | ||
| 175 | raise TypeError(f"Unsupported operand type: {type(opnd)}") | ||
| 176 | |||
| 177 | def resolve_dst(self, dst: int | dict | str) -> int: | ||
| 178 | """Resolve scalar destination to SGPR register index.""" | ||
| 179 | if isinstance(dst, int): | ||
| 180 | return dst | ||
| 181 | if isinstance(dst, dict) and dst.get("k") == "sgpr": | ||
| 182 | return dst["r"] | ||
| 183 | if isinstance(dst, str) and dst.startswith("s"): | ||
| 184 | return int(dst[1:]) | ||
| 185 | raise ValueError(f"Unsupported destination operand: {dst}") | ||
| 186 | |||
| 187 | def step(self, op: str, dst: int | dict | str, s0: int | dict | str, s1: int | dict | str) -> int: | ||
| 188 | """Execute a single scalar ALU instruction.""" | ||
| 189 | v0 = self.resolve_src(s0) | ||
| 190 | v1 = self.resolve_src(s1) | ||
| 191 | d_idx = self.resolve_dst(dst) | ||
| 192 | |||
| 193 | if op == "s_bfe_u32": | ||
| 194 | res, scc = ref_s_bfe_u32(v0, v1) | ||
| 195 | self.set_sgpr(d_idx, res) | ||
| 196 | self.scc = scc | ||
| 197 | return res | ||
| 198 | if op == "s_bfe_i32": | ||
| 199 | res, scc = ref_s_bfe_i32(v0, v1) | ||
| 200 | self.set_sgpr(d_idx, res) | ||
| 201 | self.scc = scc | ||
| 202 | return res | ||
| 203 | if op == "s_bfm_b32": | ||
| 204 | res = ref_s_bfm_b32(v0, v1) | ||
| 205 | self.set_sgpr(d_idx, res) | ||
| 206 | # RDNA2 s_bfm_b32 does not update SCC | ||
| 207 | return res | ||
| 208 | if op == "s_mov_b32": | ||
| 209 | self.set_sgpr(d_idx, v0) | ||
| 210 | return v0 | ||
| 211 | if op == "s_and_b32": | ||
| 212 | res = (v0 & v1) & MASK32 | ||
| 213 | self.set_sgpr(d_idx, res) | ||
| 214 | self.scc = 1 if res != 0 else 0 | ||
| 215 | return res | ||
| 216 | if op == "s_or_b32": | ||
| 217 | res = (v0 | v1) & MASK32 | ||
| 218 | self.set_sgpr(d_idx, res) | ||
| 219 | self.scc = 1 if res != 0 else 0 | ||
| 220 | return res | ||
| 221 | if op == "s_lshl_b32": | ||
| 222 | sh = v1 & 31 | ||
| 223 | res = (v0 << sh) & MASK32 | ||
| 224 | self.set_sgpr(d_idx, res) | ||
| 225 | self.scc = 1 if res != 0 else 0 | ||
| 226 | return res | ||
| 227 | raise NotImplementedError(f"Instruction {op} not implemented in interpreter") | ||
| 228 | |||
| 229 | def run_sequence(self, instructions: list[tuple[str, int | dict | str, int | dict | str, int | dict | str]]) -> int: | ||
| 230 | """Run a sequence of instructions (op, dst, s0, s1).""" | ||
| 231 | last_res = 0 | ||
| 232 | for op, dst, s0, s1 in instructions: | ||
| 233 | last_res = self.step(op, dst, s0, s1) | ||
| 234 | return last_res | ||
| 235 | |||
| 236 | |||
| 237 | class ReferenceModelTests(unittest.TestCase): | ||
| 238 | """Validation of pure-Python bitfield reference models.""" | ||
| 239 | |||
| 240 | def test_pack_unpack_ctl(self) -> None: | ||
| 241 | ctl = pack_bfe_ctl(5, 12) | ||
| 242 | off, wid = unpack_bfe_ctl(ctl) | ||
| 243 | self.assertEqual(off, 5) | ||
| 244 | self.assertEqual(wid, 12) | ||
| 245 | self.assertEqual(ctl, (12 << 16) | 5) | ||
| 246 | |||
| 247 | # Upper bits [15:5] and [31:23] ignored on unpack | ||
| 248 | dirty = ctl | 0xFF80FFE0 | ||
| 249 | off_dirty, wid_dirty = unpack_bfe_ctl(dirty) | ||
| 250 | self.assertEqual(off_dirty, 5) | ||
| 251 | self.assertEqual(wid_dirty, 12) | ||
| 252 | |||
| 253 | def test_ref_s_bfe_u32_basic(self) -> None: | ||
| 254 | # Extract 8 bits from offset 8 | ||
| 255 | ctl = pack_bfe_ctl(8, 8) | ||
| 256 | res, scc = ref_s_bfe_u32(0x12345678, ctl) | ||
| 257 | self.assertEqual(res, 0x56) | ||
| 258 | self.assertEqual(scc, 1) | ||
| 259 | |||
| 260 | def test_ref_s_bfe_u32_zero_width(self) -> None: | ||
| 261 | ctl = pack_bfe_ctl(8, 0) | ||
| 262 | res, scc = ref_s_bfe_u32(0xFFFFFFFF, ctl) | ||
| 263 | self.assertEqual(res, 0) | ||
| 264 | self.assertEqual(scc, 0) | ||
| 265 | |||
| 266 | def test_ref_s_bfe_u32_full_width(self) -> None: | ||
| 267 | ctl = pack_bfe_ctl(0, 32) | ||
| 268 | res, scc = ref_s_bfe_u32(0xDEADBEEF, ctl) | ||
| 269 | self.assertEqual(res, 0xDEADBEEF) | ||
| 270 | self.assertEqual(scc, 1) | ||
| 271 | |||
| 272 | def test_ref_s_bfe_u32_clamping_past_bit_31(self) -> None: | ||
| 273 | # Offset 28, width 8 runs to bit 36: effective width is min(8, 4) = 4 bits | ||
| 274 | ctl = pack_bfe_ctl(28, 8) | ||
| 275 | res, scc = ref_s_bfe_u32(0xF0000000, ctl) | ||
| 276 | self.assertEqual(res, 0xF) | ||
| 277 | self.assertEqual(scc, 1) | ||
| 278 | |||
| 279 | # Width up to 127 | ||
| 280 | ctl127 = pack_bfe_ctl(16, 127) | ||
| 281 | res127, scc127 = ref_s_bfe_u32(0xABCD0000, ctl127) | ||
| 282 | self.assertEqual(res127, 0xABCD) | ||
| 283 | self.assertEqual(scc127, 1) | ||
| 284 | |||
| 285 | def test_ref_s_bfe_i32_positive(self) -> None: | ||
| 286 | ctl = pack_bfe_ctl(4, 4) | ||
| 287 | # 0x70 >> 4 is 0x7 (sign bit 3 is 0 -> positive 7) | ||
| 288 | res, scc = ref_s_bfe_i32(0x70, ctl) | ||
| 289 | self.assertEqual(res, 7) | ||
| 290 | self.assertEqual(scc, 1) | ||
| 291 | |||
| 292 | def test_ref_s_bfe_i32_negative(self) -> None: | ||
| 293 | ctl = pack_bfe_ctl(4, 4) | ||
| 294 | # 0x80 >> 4 is 0x8 (sign bit 3 is 1 -> negative, -8 = 0xFFFFFFF8) | ||
| 295 | res, scc = ref_s_bfe_i32(0x80, ctl) | ||
| 296 | self.assertEqual(res, 0xFFFFFFF8) | ||
| 297 | self.assertEqual(scc, 1) | ||
| 298 | |||
| 299 | # 0xF0 >> 4 is 0xF (sign bit 3 is 1 -> -1 = 0xFFFFFFFF) | ||
| 300 | res, scc = ref_s_bfe_i32(0xF0, ctl) | ||
| 301 | self.assertEqual(res, 0xFFFFFFFF) | ||
| 302 | self.assertEqual(scc, 1) | ||
| 303 | |||
| 304 | def test_ref_s_bfe_i32_zero_width(self) -> None: | ||
| 305 | ctl = pack_bfe_ctl(10, 0) | ||
| 306 | res, scc = ref_s_bfe_i32(0xFFFFFFFF, ctl) | ||
| 307 | self.assertEqual(res, 0) | ||
| 308 | self.assertEqual(scc, 0) | ||
| 309 | |||
| 310 | def test_ref_s_bfe_i32_clamping_past_bit_31(self) -> None: | ||
| 311 | # Offset 28, width 8: effective width is 4. Value 0x80000000 >> 28 is 8 (negative in 4-bit) | ||
| 312 | ctl = pack_bfe_ctl(28, 8) | ||
| 313 | res, scc = ref_s_bfe_i32(0x80000000, ctl) | ||
| 314 | self.assertEqual(res, 0xFFFFFFF8) | ||
| 315 | self.assertEqual(scc, 1) | ||
| 316 | |||
| 317 | def test_ref_s_bfm_b32_basic(self) -> None: | ||
| 318 | # width = 8, offset = 8 -> 0x0000FF00 | ||
| 319 | mask = ref_s_bfm_b32(8, 8) | ||
| 320 | self.assertEqual(mask, 0x0000FF00) | ||
| 321 | |||
| 322 | # width = 4, offset = 28 -> 0xF0000000 | ||
| 323 | mask = ref_s_bfm_b32(4, 28) | ||
| 324 | self.assertEqual(mask, 0xF0000000) | ||
| 325 | |||
| 326 | # width = 0 -> 0 | ||
| 327 | mask = ref_s_bfm_b32(0, 16) | ||
| 328 | self.assertEqual(mask, 0) | ||
| 329 | |||
| 330 | # width = 31, offset = 0 -> 0x7FFFFFFF | ||
| 331 | mask = ref_s_bfm_b32(31, 0) | ||
| 332 | self.assertEqual(mask, 0x7FFFFFFF) | ||
| 333 | |||
| 334 | # width = 32 (32 & 31 = 0) -> 0 | ||
| 335 | mask = ref_s_bfm_b32(32, 0) | ||
| 336 | self.assertEqual(mask, 0) | ||
| 337 | |||
| 338 | # width modulo 32 and offset modulo 32 | ||
| 339 | self.assertEqual(ref_s_bfm_b32(8 + 32, 8 + 64), 0x0000FF00) | ||
| 340 | |||
| 341 | def test_ref_bitfield_insert(self) -> None: | ||
| 342 | base = 0x12345678 | ||
| 343 | insert = 0xAB | ||
| 344 | # insert 8 bits at offset 8 | ||
| 345 | res = ref_bitfield_insert(base, insert, 8, 8) | ||
| 346 | self.assertEqual(res, 0x1234AB78) | ||
| 347 | |||
| 348 | # count = 0 returns base unchanged | ||
| 349 | self.assertEqual(ref_bitfield_insert(base, insert, 8, 0), base) | ||
| 350 | |||
| 351 | # full 32-bit replace | ||
| 352 | self.assertEqual(ref_bitfield_insert(base, 0xCAFEBABE, 0, 32), 0xCAFEBABE) | ||
| 353 | |||
| 354 | |||
| 355 | class ScalarAluInterpreterTests(unittest.TestCase): | ||
| 356 | """Execution tests for the ScalarAluInterpreter modeling RDNA2 scalar instructions.""" | ||
| 357 | |||
| 358 | def setUp(self) -> None: | ||
| 359 | self.interp = ScalarAluInterpreter() | ||
| 360 | |||
| 361 | def test_s_bfe_u32_all_single_bit_positions(self) -> None: | ||
| 362 | # Verify single bit extraction across all 32 positions (0..31) | ||
| 363 | for pos in range(32): | ||
| 364 | pattern = 1 << pos | ||
| 365 | ctl = pack_bfe_ctl(pos, 1) | ||
| 366 | self.interp.set_sgpr(0, pattern) | ||
| 367 | self.interp.set_sgpr(1, ctl) | ||
| 368 | |||
| 369 | res = self.interp.step("s_bfe_u32", 2, {"k": "sgpr", "r": 0}, {"k": "sgpr", "r": 1}) | ||
| 370 | self.assertEqual(res, 1) | ||
| 371 | self.assertEqual(self.interp.get_sgpr(2), 1) | ||
| 372 | self.assertEqual(self.interp.scc, 1) | ||
| 373 | |||
| 374 | # Inverted bit test (bit is 0) | ||
| 375 | self.interp.set_sgpr(0, pattern ^ (1 << pos)) | ||
| 376 | res_zero = self.interp.step("s_bfe_u32", 2, {"k": "sgpr", "r": 0}, {"k": "sgpr", "r": 1}) | ||
| 377 | self.assertEqual(res_zero, 0) | ||
| 378 | self.assertEqual(self.interp.get_sgpr(2), 0) | ||
| 379 | self.assertEqual(self.interp.scc, 0) | ||
| 380 | |||
| 381 | def test_s_bfe_u32_scc_transitions(self) -> None: | ||
| 382 | # Start with scc = 0 | ||
| 383 | self.interp.scc = 0 | ||
| 384 | self.interp.set_sgpr(0, 0xFF) | ||
| 385 | self.interp.set_sgpr(1, pack_bfe_ctl(0, 4)) | ||
| 386 | self.interp.step("s_bfe_u32", "s2", "s0", "s1") | ||
| 387 | self.assertEqual(self.interp.scc, 1) | ||
| 388 | |||
| 389 | # Non-zero to zero: SCC must clear | ||
| 390 | self.interp.set_sgpr(0, 0x00) | ||
| 391 | self.interp.step("s_bfe_u32", "s2", "s0", "s1") | ||
| 392 | self.assertEqual(self.interp.scc, 0) | ||
| 393 | |||
| 394 | def test_s_bfe_i32_sign_extension_matrix(self) -> None: | ||
| 395 | cases = [ | ||
| 396 | # (val, offset, width, expected_result, expected_scc) | ||
| 397 | (0x00000007, 0, 4, 7, 1), | ||
| 398 | (0x00000008, 0, 4, 0xFFFFFFF8, 1), | ||
| 399 | (0x0000000F, 0, 4, 0xFFFFFFFF, 1), | ||
| 400 | (0x00000000, 0, 4, 0, 0), | ||
| 401 | (0x00008000, 8, 8, 0xFFFFFF80, 1), | ||
| 402 | (0x00007F00, 8, 8, 0x0000007F, 1), | ||
| 403 | (0x80000000, 16, 16, 0xFFFF8000, 1), | ||
| 404 | (0x7FFF0000, 16, 16, 0x00007FFF, 1), | ||
| 405 | (0x00000001, 0, 1, 0xFFFFFFFF, 1), | ||
| 406 | (0x00000000, 0, 1, 0x00000000, 0), | ||
| 407 | ] | ||
| 408 | for val, off, wid, want_res, want_scc in cases: | ||
| 409 | with self.subTest(val=hex(val), off=off, wid=wid): | ||
| 410 | ctl = pack_bfe_ctl(off, wid) | ||
| 411 | self.interp.set_sgpr(0, val) | ||
| 412 | self.interp.set_sgpr(1, ctl) | ||
| 413 | res = self.interp.step("s_bfe_i32", "s2", "s0", "s1") | ||
| 414 | self.assertEqual(res, want_res) | ||
| 415 | self.assertEqual(self.interp.get_sgpr(2), want_res) | ||
| 416 | self.assertEqual(self.interp.scc, want_scc) | ||
| 417 | |||
| 418 | def test_s_bfm_b32_mask_generation(self) -> None: | ||
| 419 | test_vectors = [ | ||
| 420 | # (width, offset, expected_mask) | ||
| 421 | (1, 0, 0x00000001), | ||
| 422 | (1, 31, 0x80000000), | ||
| 423 | (8, 0, 0x000000FF), | ||
| 424 | (8, 8, 0x0000FF00), | ||
| 425 | (8, 16, 0x00FF0000), | ||
| 426 | (8, 24, 0xFF000000), | ||
| 427 | (16, 0, 0x0000FFFF), | ||
| 428 | (16, 16, 0xFFFF0000), | ||
| 429 | (31, 0, 0x7FFFFFFF), | ||
| 430 | (0, 0, 0x00000000), | ||
| 431 | (0, 16, 0x00000000), | ||
| 432 | (32, 0, 0x00000000), # 32 & 31 == 0 | ||
| 433 | ] | ||
| 434 | for w, o, want in test_vectors: | ||
| 435 | with self.subTest(w=w, o=o): | ||
| 436 | self.interp.set_sgpr(0, w) | ||
| 437 | self.interp.set_sgpr(1, o) | ||
| 438 | res = self.interp.step("s_bfm_b32", "s2", "s0", "s1") | ||
| 439 | self.assertEqual(res, want) | ||
| 440 | self.assertEqual(self.interp.get_sgpr(2), want) | ||
| 441 | |||
| 442 | def test_s_bfm_b32_preserves_scc(self) -> None: | ||
| 443 | # s_bfm_b32 must not overwrite SCC whether SCC is 0 or 1 | ||
| 444 | for initial_scc in (0, 1): | ||
| 445 | self.interp.scc = initial_scc | ||
| 446 | self.interp.set_sgpr(0, 8) | ||
| 447 | self.interp.set_sgpr(1, 4) | ||
| 448 | self.interp.step("s_bfm_b32", "s2", "s0", "s1") | ||
| 449 | self.assertEqual(self.interp.scc, initial_scc) | ||
| 450 | |||
| 451 | def test_in_place_destination_aliasing(self) -> None: | ||
| 452 | # Destination aliases source operand | ||
| 453 | ctl = pack_bfe_ctl(8, 8) | ||
| 454 | self.interp.set_sgpr(0, 0x12345678) | ||
| 455 | self.interp.set_sgpr(1, ctl) | ||
| 456 | res = self.interp.step("s_bfe_u32", "s0", "s0", "s1") | ||
| 457 | self.assertEqual(res, 0x56) | ||
| 458 | self.assertEqual(self.interp.get_sgpr(0), 0x56) | ||
| 459 | |||
| 460 | # Dest aliases control word | ||
| 461 | self.interp.set_sgpr(0, 0x12345678) | ||
| 462 | self.interp.set_sgpr(1, ctl) | ||
| 463 | res = self.interp.step("s_bfe_u32", "s1", "s0", "s1") | ||
| 464 | self.assertEqual(res, 0x56) | ||
| 465 | self.assertEqual(self.interp.get_sgpr(1), 0x56) | ||
| 466 | |||
| 467 | def test_adjacent_registers_untouched(self) -> None: | ||
| 468 | self.interp.set_sgpr(0, 0x11111111) | ||
| 469 | self.interp.set_sgpr(1, 0x22222222) | ||
| 470 | self.interp.set_sgpr(2, 0x33333333) | ||
| 471 | self.interp.set_sgpr(3, 0x44444444) | ||
| 472 | |||
| 473 | ctl = pack_bfe_ctl(4, 4) | ||
| 474 | self.interp.step("s_bfe_u32", "s1", "s2", ctl) | ||
| 475 | self.assertEqual(self.interp.get_sgpr(0), 0x11111111) | ||
| 476 | self.assertEqual(self.interp.get_sgpr(2), 0x33333333) | ||
| 477 | self.assertEqual(self.interp.get_sgpr(3), 0x44444444) | ||
| 478 | |||
| 479 | def test_scalar_bitfield_insert_composition_pipeline(self) -> None: | ||
| 480 | # Simulate scalar bitfield insert: | ||
| 481 | # s_bfm_b32 s_mask, count, offset | ||
| 482 | # s_lshl_b32 s_ins_shifted, s_ins, offset | ||
| 483 | # s_and_b32 s_ins_masked, s_ins_shifted, s_mask | ||
| 484 | # s_and_b32 s_base_cleared, s_base, ~s_mask | ||
| 485 | # s_or_b32 s_res, s_base_cleared, s_ins_masked | ||
| 486 | base_val = 0xAA55AA55 | ||
| 487 | ins_val = 0x3F | ||
| 488 | offset = 12 | ||
| 489 | count = 6 | ||
| 490 | |||
| 491 | mask = ref_s_bfm_b32(count, offset) | ||
| 492 | expected = ref_bitfield_insert(base_val, ins_val, offset, count) | ||
| 493 | |||
| 494 | self.interp.set_sgpr(0, base_val) | ||
| 495 | self.interp.set_sgpr(1, ins_val) | ||
| 496 | self.interp.set_sgpr(2, count) | ||
| 497 | self.interp.set_sgpr(3, offset) | ||
| 498 | |||
| 499 | seq = [ | ||
| 500 | ("s_bfm_b32", "s4", "s2", "s3"), # s4 = mask | ||
| 501 | ("s_lshl_b32", "s5", "s1", "s3"), # s5 = ins << offset | ||
| 502 | ("s_and_b32", "s5", "s5", "s4"), # s5 = (ins << offset) & mask | ||
| 503 | ("s_and_b32", "s6", "s0", {"k": "imm", "v": ~mask}), # s6 = base & ~mask | ||
| 504 | ("s_or_b32", "s7", "s6", "s5"), # s7 = s6 | s5 | ||
| 505 | ] | ||
| 506 | result = self.interp.run_sequence(seq) | ||
| 507 | self.assertEqual(result, expected) | ||
| 508 | self.assertEqual(self.interp.get_sgpr(7), expected) | ||
| 509 | |||
| 510 | # Roundtrip: extract the inserted field back out with s_bfe_u32 | ||
| 511 | ctl = pack_bfe_ctl(offset, count) | ||
| 512 | extracted = self.interp.step("s_bfe_u32", "s8", "s7", ctl) | ||
| 513 | self.assertEqual(extracted, ins_val) | ||
| 514 | |||
| 515 | |||
| 516 | class TranslatorEmissionTests(unittest.TestCase): | ||
| 517 | """Verify that rdna2spirv.Gen correctly emits SPIR-V for s_bfe and s_bfm instructions.""" | ||
| 518 | |||
| 519 | def test_translate_s_bfe_u32_emission(self) -> None: | ||
| 520 | g = rdna2spirv.Gen("ps") | ||
| 521 | ins = { | ||
| 522 | "op": "s_bfe_u32", | ||
| 523 | "args": [ | ||
| 524 | {"k": "sgpr", "r": 2}, | ||
| 525 | {"k": "sgpr", "r": 0}, | ||
| 526 | {"k": "sgpr", "r": 1}, | ||
| 527 | ], | ||
| 528 | "mods": [], | ||
| 529 | } | ||
| 530 | g.insn(ins) | ||
| 531 | emitted = "\n".join(g.body) | ||
| 532 | |||
| 533 | # Unpack offset [4:0] and width [22:16] | ||
| 534 | self.assertIn("OpBitwiseAnd %uint", emitted) | ||
| 535 | self.assertIn("OpBitFieldUExtract %uint", emitted) | ||
| 536 | # Clamping via bfe_width | ||
| 537 | self.assertIn("OpISub %uint", emitted) | ||
| 538 | self.assertIn("OpULessThan %bool", emitted) | ||
| 539 | # Store result and SCC | ||
| 540 | self.assertIn("OpStore %scc", emitted) | ||
| 541 | |||
| 542 | def test_translate_s_bfe_i32_emission(self) -> None: | ||
| 543 | g = rdna2spirv.Gen("ps") | ||
| 544 | ins = { | ||
| 545 | "op": "s_bfe_i32", | ||
| 546 | "args": [ | ||
| 547 | {"k": "sgpr", "r": 3}, | ||
| 548 | {"k": "sgpr", "r": 4}, | ||
| 549 | {"k": "sgpr", "r": 5}, | ||
| 550 | ], | ||
| 551 | "mods": [], | ||
| 552 | } | ||
| 553 | g.insn(ins) | ||
| 554 | emitted = "\n".join(g.body) | ||
| 555 | |||
| 556 | self.assertIn("OpBitFieldSExtract %int", emitted) | ||
| 557 | self.assertIn("OpBitcast %int", emitted) | ||
| 558 | self.assertIn("OpBitcast %uint", emitted) | ||
| 559 | self.assertIn("OpStore %scc", emitted) | ||
| 560 | |||
| 561 | def test_translate_s_bfm_b32_emission(self) -> None: | ||
| 562 | g = rdna2spirv.Gen("ps") | ||
| 563 | ins = { | ||
| 564 | "op": "s_bfm_b32", | ||
| 565 | "args": [ | ||
| 566 | {"k": "sgpr", "r": 6}, | ||
| 567 | {"k": "sgpr", "r": 7}, | ||
| 568 | {"k": "sgpr", "r": 8}, | ||
| 569 | ], | ||
| 570 | "mods": [], | ||
| 571 | } | ||
| 572 | g.insn(ins) | ||
| 573 | emitted = "\n".join(g.body) | ||
| 574 | |||
| 575 | self.assertIn("OpShiftLeftLogical %uint", emitted) | ||
| 576 | self.assertIn("OpISub %uint", emitted) | ||
| 577 | self.assertIn("OpBitwiseAnd %uint", emitted) | ||
| 578 | # s_bfm_b32 must NOT emit SCC store | ||
| 579 | self.assertNotIn("OpStore %scc", emitted) | ||
| 580 | |||
| 581 | def test_translation_determinism(self) -> None: | ||
| 582 | def emit_sequence() -> str: | ||
| 583 | g = rdna2spirv.Gen("ps") | ||
| 584 | for op in ("s_bfe_u32", "s_bfe_i32", "s_bfm_b32"): | ||
| 585 | g.insn({ | ||
| 586 | "op": op, | ||
| 587 | "args": [ | ||
| 588 | {"k": "sgpr", "r": 0}, | ||
| 589 | {"k": "sgpr", "r": 1}, | ||
| 590 | {"k": "sgpr", "r": 2}, | ||
| 591 | ], | ||
| 592 | "mods": [], | ||
| 593 | }) | ||
| 594 | return "\n".join(g.body) | ||
| 595 | |||
| 596 | self.assertEqual(emit_sequence(), emit_sequence()) | ||
| 597 | |||
| 598 | |||
| 599 | SPIRV_HEADER = """\ | ||
| 600 | OpCapability Shader | ||
| 601 | %glsl = OpExtInstImport "GLSL.std.450" | ||
| 602 | OpMemoryModel Logical GLSL450 | ||
| 603 | OpEntryPoint Fragment %main "main" %out | ||
| 604 | %void = OpTypeVoid | ||
| 605 | %fn = OpTypeFunction %void | ||
| 606 | %uint = OpTypeInt 32 0 | ||
| 607 | %int = OpTypeInt 32 1 | ||
| 608 | %bool = OpTypeBool | ||
| 609 | %_ptr_Output_uint = OpTypePointer Output %uint | ||
| 610 | %out = OpVariable %_ptr_Output_uint Output | ||
| 611 | {constants} | ||
| 612 | %main = OpFunction %void None %fn | ||
| 613 | %entry = OpLabel | ||
| 614 | {body} | ||
| 615 | OpStore %out %res | ||
| 616 | OpReturn | ||
| 617 | OpFunctionEnd | ||
| 618 | """ | ||
| 619 | |||
| 620 | |||
| 621 | def run_spv(body: str, const_defs: dict[str, int]) -> int: | ||
| 622 | """Assemble and run a SPIR-V fragment module defining %res.""" | ||
| 623 | consts_text = "\n".join(f"%{name} = OpConstant %uint {val & MASK32}" for name, val in const_defs.items()) | ||
| 624 | mod_text = SPIRV_HEADER.format(constants=consts_text, body=body) | ||
| 625 | return si.run_fragment(mod_text)["%out"][0] | ||
| 626 | |||
| 627 | |||
| 628 | class SpirvMiniInterpTests(unittest.TestCase): | ||
| 629 | """Cross-validation between interpreter models and direct spirv_mini_interp execution.""" | ||
| 630 | |||
| 631 | def test_direct_uextract_in_spirv(self) -> None: | ||
| 632 | consts = {"x": 0xDEADBEEF, "off": 8, "wid": 8} | ||
| 633 | body = "%res = OpBitFieldUExtract %uint %x %off %wid\n" | ||
| 634 | self.assertEqual(run_spv(body, consts), 0xBE) | ||
| 635 | |||
| 636 | def test_direct_sextract_in_spirv(self) -> None: | ||
| 637 | # Negative 4-bit field: 0xF0 >> 4 is 0xF -> sign extended to 0xFFFFFFFF | ||
| 638 | consts = {"x": 0xF0, "off": 4, "wid": 4} | ||
| 639 | body = """\ | ||
| 640 | %xi = OpBitcast %int %x | ||
| 641 | %ri = OpBitFieldSExtract %int %xi %off %wid | ||
| 642 | %res = OpBitcast %uint %ri | ||
| 643 | """ | ||
| 644 | self.assertEqual(run_spv(body, consts), 0xFFFFFFFF) | ||
| 645 | |||
| 646 | def test_direct_insert_in_spirv(self) -> None: | ||
| 647 | consts = {"base": 0xFFFFFFFF, "ins": 0x12, "off": 8, "cnt": 8} | ||
| 648 | body = "%res = OpBitFieldInsert %uint %base %ins %off %cnt\n" | ||
| 649 | self.assertEqual(run_spv(body, consts), 0xFFFF12FF) | ||
| 650 | |||
| 651 | def test_lowered_s_bfe_u32_with_large_width_avoids_rejection(self) -> None: | ||
| 652 | # SPIR-V rejects offset + count > 32 if passed directly to OpBitFieldUExtract. | ||
| 653 | # But rdna2spirv lowers width via bfe_width: | ||
| 654 | # room = 32 - off; fits = wid < room; clamped_wid = fits ? wid : room | ||
| 655 | # Here offset = 28, raw width = 16 (28 + 16 = 44 > 32). | ||
| 656 | # Clamped width becomes 4, extracting the top 4 bits (0xA). | ||
| 657 | ctl = pack_bfe_ctl(28, 16) | ||
| 658 | consts = {"x": 0xA0000000, "ctl": ctl, "c31": 31, "c16": 16, "c7": 7, "c32": 32} | ||
| 659 | body = """\ | ||
| 660 | %off = OpBitwiseAnd %uint %ctl %c31 | ||
| 661 | %raw_wid = OpBitFieldUExtract %uint %ctl %c16 %c7 | ||
| 662 | %room = OpISub %uint %c32 %off | ||
| 663 | %fits = OpULessThan %bool %raw_wid %room | ||
| 664 | %clamped_wid = OpSelect %uint %fits %raw_wid %room | ||
| 665 | %res = OpBitFieldUExtract %uint %x %off %clamped_wid | ||
| 666 | """ | ||
| 667 | got = run_spv(body, consts) | ||
| 668 | self.assertEqual(got, 0xA) | ||
| 669 | expected, _ = ref_s_bfe_u32(0xA0000000, ctl) | ||
| 670 | self.assertEqual(got, expected) | ||
| 671 | |||
| 672 | def test_direct_out_of_bounds_raises_unsupported(self) -> None: | ||
| 673 | # Direct raw OpBitFieldUExtract with off + wid > 32 must be rejected by spirv_mini_interp | ||
| 674 | consts = {"x": 0xFF, "off": 30, "wid": 4} | ||
| 675 | body = "%res = OpBitFieldUExtract %uint %x %off %wid\n" | ||
| 676 | with self.assertRaises(si.UnsupportedConstruct): | ||
| 677 | run_spv(body, consts) | ||
| 678 | |||
| 679 | consts_ins = {"base": 0, "ins": 1, "off": 25, "cnt": 10} | ||
| 680 | body_ins = "%res = OpBitFieldInsert %uint %base %ins %off %cnt\n" | ||
| 681 | with self.assertRaises(si.UnsupportedConstruct): | ||
| 682 | run_spv(body_ins, consts_ins) | ||
| 683 | |||
| 684 | |||
| 685 | class NegativeControlTests(unittest.TestCase): | ||
| 686 | """Sensitivity tests ensuring mutations in bitfield logic are detected.""" | ||
| 687 | |||
| 688 | def test_signed_vs_unsigned_mutation_caught(self) -> None: | ||
| 689 | # Extracting 0x00008000 with offset 8, width 8 gives 0x80. | ||
| 690 | # Signed gives 0xFFFFFF80, unsigned gives 0x00000080. | ||
| 691 | ctl = pack_bfe_ctl(8, 8) | ||
| 692 | signed_res, _ = ref_s_bfe_i32(0x00008000, ctl) | ||
| 693 | unsigned_res, _ = ref_s_bfe_u32(0x00008000, ctl) | ||
| 694 | self.assertEqual(signed_res, 0xFFFFFF80) | ||
| 695 | self.assertEqual(unsigned_res, 0x00000080) | ||
| 696 | self.assertNotEqual(signed_res, unsigned_res) | ||
| 697 | |||
| 698 | def test_dropped_offset_mutation_caught(self) -> None: | ||
| 699 | ctl_good = pack_bfe_ctl(4, 8) | ||
| 700 | ctl_mutated = pack_bfe_ctl(0, 8) | ||
| 701 | val = 0x12345678 | ||
| 702 | res_good, _ = ref_s_bfe_u32(val, ctl_good) | ||
| 703 | res_mutated, _ = ref_s_bfe_u32(val, ctl_mutated) | ||
| 704 | self.assertEqual(res_good, 0x67) | ||
| 705 | self.assertEqual(res_mutated, 0x78) | ||
| 706 | self.assertNotEqual(res_good, res_mutated) | ||
| 707 | |||
| 708 | def test_scc_zero_flag_mutation_caught(self) -> None: | ||
| 709 | # An implementation that fails to clear SCC when the result is 0 | ||
| 710 | ctl = pack_bfe_ctl(0, 4) | ||
| 711 | res_nonzero, scc_nonzero = ref_s_bfe_u32(0x5, ctl) | ||
| 712 | res_zero, scc_zero = ref_s_bfe_u32(0x0, ctl) | ||
| 713 | self.assertEqual(scc_nonzero, 1) | ||
| 714 | self.assertEqual(scc_zero, 0) | ||
| 715 | self.assertNotEqual(scc_nonzero, scc_zero) | ||
| 716 | |||
| 717 | |||
| 718 | if __name__ == "__main__": | ||
| 719 | unittest.main() | ||