diff options
| author | Diesel <dev.diesel@proton.me> | 2026-11-21 22:44:07 +0100 |
|---|---|---|
| committer | Diesel <dev.diesel@proton.me> | 2026-11-21 22:44:07 +0100 |
| commit | 5c1a1fbb6bf6f609eb4e61baa4961ce382743a4e (patch) | |
| tree | ac8ad90058b97ab241fdc65edae3d4628d20ee1e /tests/semantic | |
| parent | c45309e29f21c1d5b75678db0e2dc2dd11cff25b (diff) | |
| parent | 4e390e0871d7265e629b51aca8ff99a15a6a6845 (diff) | |
Merge main to include the compute Y/Z harness test fix
Diffstat (limited to 'tests/semantic')
| -rw-r--r-- | tests/semantic/test_mutations.py | 7 | ||||
| -rw-r--r-- | tests/semantic/test_scalar_mem_smem.py | 203 |
2 files changed, 208 insertions, 2 deletions
diff --git a/tests/semantic/test_mutations.py b/tests/semantic/test_mutations.py index cce2b723a..0caca1d46 100644 --- a/tests/semantic/test_mutations.py +++ b/tests/semantic/test_mutations.py | |||
| @@ -103,8 +103,11 @@ MUTATIONS = [ | |||
| 103 | 103 | ||
| 104 | 104 | ||
| 105 | def run_semantics(tools_dir): | 105 | def run_semantics(tools_dir): |
| 106 | env = dict(os.environ, LEONIDA_TOOLS_DIR=tools_dir, PYTHONDONTWRITEBYTECODE="1") | 106 | # The child finds test_ps_semantics through PYTHONPATH, not cwd=HERE: Windows refuses a working directory longer |
| 107 | p = subprocess.run([sys.executable, "-m", "unittest", "test_ps_semantics", "-v"], cwd=HERE, env=env, | 107 | # than 258 characters (WinError 267), which a checkout in a deep folder reaches. |
| 108 | path = os.pathsep.join(filter(None, [HERE, os.environ.get("PYTHONPATH")])) | ||
| 109 | env = dict(os.environ, LEONIDA_TOOLS_DIR=tools_dir, PYTHONDONTWRITEBYTECODE="1", PYTHONPATH=path) | ||
| 110 | p = subprocess.run([sys.executable, "-m", "unittest", "test_ps_semantics", "-v"], env=env, | ||
| 108 | capture_output=True, text=True, timeout=300) | 111 | capture_output=True, text=True, timeout=300) |
| 109 | return p.returncode, p.stdout + p.stderr | 112 | return p.returncode, p.stdout + p.stderr |
| 110 | 113 | ||
diff --git a/tests/semantic/test_scalar_mem_smem.py b/tests/semantic/test_scalar_mem_smem.py index a8e6cbde1..b9329793d 100644 --- a/tests/semantic/test_scalar_mem_smem.py +++ b/tests/semantic/test_scalar_mem_smem.py | |||
| @@ -13,6 +13,8 @@ Covers AMD RDNA2 / GFX10 SMEM (Scalar Memory) ISA semantics and tools/rdna2spirv | |||
| 13 | * Multi-dword scalar loads (s_load_dword, s_load_dwordx2, x4, x8, x16) with sequential 4-byte address strides | 13 | * Multi-dword scalar loads (s_load_dword, s_load_dwordx2, x4, x8, x16) with sequential 4-byte address strides |
| 14 | * Destination register targeting: SGPR ranges and special registers (vcc_lo, vcc_hi, vcc pair, m0) | 14 | * Destination register targeting: SGPR ranges and special registers (vcc_lo, vcc_hi, vcc pair, m0) |
| 15 | * tools/rdna2spirv.py translator validation (Gen.scalar_load instruction sequences and operand validation) | 15 | * tools/rdna2spirv.py translator validation (Gen.scalar_load instruction sequences and operand validation) |
| 16 | * SMEM loads with a register offset and an immediate at once ("sOFF offset:IMM"): address = base + sOFF + IMM, | ||
| 17 | checked by running the emitted code (SmemBodyEvaluator, SmemSoffsetImmTranslatorTests) | ||
| 16 | * spirv_mini_interp strictness validation (rejecting unmodelled 64-bit types and PhysicalStorageBuffer) | 18 | * spirv_mini_interp strictness validation (rejecting unmodelled 64-bit types and PhysicalStorageBuffer) |
| 17 | """ | 19 | """ |
| 18 | import os | 20 | import os |
| @@ -372,6 +374,207 @@ class SmemTranslatorGenTests(unittest.TestCase): | |||
| 372 | g.scalar_load(ins_vgpr_base["op"], ins_vgpr_base["args"], ins_vgpr_base["mods"]) | 374 | g.scalar_load(ins_vgpr_base["op"], ins_vgpr_base["args"], ins_vgpr_base["mods"]) |
| 373 | 375 | ||
| 374 | 376 | ||
| 377 | class SmemBodyEvaluator: | ||
| 378 | """Runs the straight-line SPIR-V that Gen.scalar_load emits, against concrete SGPR values and a dword memory. | ||
| 379 | |||
| 380 | spirv_mini_interp refuses 64-bit integers and PhysicalStorageBuffer by design, so the address a scalar load | ||
| 381 | reads cannot be checked there. This evaluator knows only the handful of instructions scalar_load uses and | ||
| 382 | raises on anything else, so a change in the emitted code cannot be approximated silently. The heap base (push | ||
| 383 | constant dwords 32..33) is 0 unless given, which makes every address read an Xbox VA. | ||
| 384 | """ | ||
| 385 | |||
| 386 | def __init__(self, gen, sgpr, memory, heap_base=0): | ||
| 387 | self.consts = {name: value for value, name in gen.consts.items()} | ||
| 388 | self.sgpr = dict(sgpr) | ||
| 389 | self.memory = memory | ||
| 390 | self.heap_base = heap_base | ||
| 391 | self.ids = {} | ||
| 392 | self.reads = [] # byte address of every PhysicalStorageBuffer dword load, in order | ||
| 393 | for line in gen.body: | ||
| 394 | self._step(line.split()) | ||
| 395 | |||
| 396 | def _value(self, name): | ||
| 397 | if name == "%c64_32": | ||
| 398 | return 32 | ||
| 399 | if name in self.ids: | ||
| 400 | return self.ids[name] | ||
| 401 | if name in self.consts: | ||
| 402 | return self.consts[name] | ||
| 403 | raise AssertionError(f"unknown id {name}") | ||
| 404 | |||
| 405 | def _step(self, words): | ||
| 406 | if words[0] == "OpStore": | ||
| 407 | kind, index = self._value(words[1]) | ||
| 408 | if kind != "sgpr": | ||
| 409 | raise AssertionError(f"store through {kind}") | ||
| 410 | self.sgpr[index] = self._value(words[2]) | ||
| 411 | return | ||
| 412 | if len(words) < 4 or words[1] != "=": | ||
| 413 | raise AssertionError("unmodelled line: " + " ".join(words)) | ||
| 414 | result, op, ty, args = words[0], words[2], words[3], words[4:] | ||
| 415 | if op == "OpAccessChain" and ty == "%_ptr_Function_uint" and args[0] == "%sgpr": | ||
| 416 | value = ("sgpr", self._value(args[1])) | ||
| 417 | elif op == "OpAccessChain" and ty == "%_ptr_PSB_uint": | ||
| 418 | kind, address = self._value(args[0]) | ||
| 419 | if kind != "psb": | ||
| 420 | raise AssertionError(f"PSB access chain on {kind}") | ||
| 421 | value = ("mem", (address + 4 * self._value(args[1])) & MASK64) | ||
| 422 | elif op == "OpLoad" and ty == "%uint" and args[0] == "%ud_base_lo_p": | ||
| 423 | value = self.heap_base & MASK32 | ||
| 424 | elif op == "OpLoad" and ty == "%uint" and args[0] == "%ud_base_hi_p": | ||
| 425 | value = (self.heap_base >> 32) & MASK32 | ||
| 426 | elif op == "OpLoad" and ty == "%uint": | ||
| 427 | kind, where = self._value(args[0]) | ||
| 428 | if kind == "sgpr": | ||
| 429 | value = self.sgpr.get(where, 0) & MASK32 | ||
| 430 | elif kind == "mem" and args[1:] == ["Aligned", "4"]: | ||
| 431 | self.reads.append(where) | ||
| 432 | value = self.memory.read_dword(where) | ||
| 433 | else: | ||
| 434 | raise AssertionError("unmodelled load: " + " ".join(words)) | ||
| 435 | elif op == "OpBitwiseAnd" and ty == "%uint": | ||
| 436 | value = self._value(args[0]) & self._value(args[1]) | ||
| 437 | elif op == "OpIAdd" and ty == "%uint": | ||
| 438 | value = (self._value(args[0]) + self._value(args[1])) & MASK32 | ||
| 439 | elif op == "OpUConvert" and ty == "%ulong": | ||
| 440 | value = self._value(args[0]) & MASK32 | ||
| 441 | elif op == "OpShiftLeftLogical" and ty == "%ulong": | ||
| 442 | value = (self._value(args[0]) << self._value(args[1])) & MASK64 | ||
| 443 | elif op == "OpBitwiseOr" and ty == "%ulong": | ||
| 444 | value = self._value(args[0]) | self._value(args[1]) | ||
| 445 | elif op == "OpIAdd" and ty == "%ulong": | ||
| 446 | value = (self._value(args[0]) + self._value(args[1])) & MASK64 | ||
| 447 | elif op == "OpConvertUToPtr" and ty == "%_ptr_PSB_arr": | ||
| 448 | value = ("psb", self._value(args[0])) | ||
| 449 | else: | ||
| 450 | raise AssertionError("unmodelled instruction: " + " ".join(words)) | ||
| 451 | self.ids[result] = value | ||
| 452 | |||
| 453 | |||
| 454 | class SmemSoffsetImmTranslatorTests(unittest.TestCase): | ||
| 455 | """SMEM loads that carry a register offset and an immediate at once: llvm-mc prints "sOFF offset:IMM". | ||
| 456 | |||
| 457 | Rule: address = base + soffset + imm (+ 4*i for dword i). | ||
| 458 | Measured on an AMD Radeon RX 6800 (gfx1030) by running s_buffer_load_dword/x2/x4/x8 as a raw compute kernel: | ||
| 459 | soffset 8 + imm 4 reads the dword at +12. s_load_dword* with both offsets was not measured; it shares the SMEM | ||
| 460 | encoding (soffset_en + imm), so the same rule is applied to it as an inference. | ||
| 461 | The hardware also truncates base, soffset and imm each to a multiple of 4 and returns 0 past NUM_RECORDS; the | ||
| 462 | translator does neither, so every case here is dword-aligned and in bounds. | ||
| 463 | |||
| 464 | The instruction lines are hand-written. Each case runs the code Gen.scalar_load emits with SmemBodyEvaluator | ||
| 465 | and asserts both the addresses read and the values that land in the destination SGPRs. | ||
| 466 | """ | ||
| 467 | |||
| 468 | VSHARP_BASE = 0x0000000200001000 # s24 = 0x00001000, s25 = 0x80040002 (flags above bit 15 are not address) | ||
| 469 | POINTER = 0x0000000300004000 # s[24:25] for s_load | ||
| 470 | |||
| 471 | def run_load(self, line, pointer=False): | ||
| 472 | ins = parse_line(line) | ||
| 473 | g = rdna2spirv.Gen("ps") | ||
| 474 | g.scalar_load(ins["op"], ins["args"], ins["mods"]) | ||
| 475 | base = self.POINTER if pointer else self.VSHARP_BASE | ||
| 476 | bank = SmemMemoryBank() | ||
| 477 | for off in range(0, 0x40, 4): # every dword near the base holds its own offset, tagged | ||
| 478 | bank.write_dword(base + off, 0x5EED0000 | off) | ||
| 479 | sgpr = {40: 8} # the register offset used by every case | ||
| 480 | if pointer: | ||
| 481 | sgpr.update({24: 0x00004000, 25: 0x00000003}) | ||
| 482 | else: | ||
| 483 | sgpr.update({24: 0x00001000, 25: 0x80040002, 26: 0x00010000, 27: 0x00140000}) | ||
| 484 | ev = SmemBodyEvaluator(g, sgpr, bank) | ||
| 485 | return ev, [a - base for a in ev.reads], g | ||
| 486 | |||
| 487 | def test_s_buffer_load_dword_soffset_plus_imm(self): | ||
| 488 | # The measured case: soffset 8 + imm 4 reads the dword at +12 (not +8, which is what dropping the imm gives). | ||
| 489 | ev, offsets, _ = self.run_load("s_buffer_load_dword s12, s[24:27], s40 offset:0x4") | ||
| 490 | self.assertEqual(offsets, [12]) | ||
| 491 | self.assertEqual(ev.sgpr[12], 0x5EED000C) | ||
| 492 | |||
| 493 | def test_s_load_dword_soffset_plus_imm(self): | ||
| 494 | # Inferred from the shared SMEM encoding; not measured on hardware. | ||
| 495 | ev, offsets, _ = self.run_load("s_load_dword s12, s[24:25], s40 offset:0x4", pointer=True) | ||
| 496 | self.assertEqual(offsets, [12]) | ||
| 497 | self.assertEqual(ev.sgpr[12], 0x5EED000C) | ||
| 498 | |||
| 499 | def test_multi_dword_soffset_plus_imm(self): | ||
| 500 | cases = [ | ||
| 501 | ("s_buffer_load_dwordx2 s[12:13], s[24:27], s40 offset:0x4", False, 2, 0x0C), | ||
| 502 | ("s_buffer_load_dwordx4 s[12:15], s[24:27], s40 offset:0x10", False, 4, 0x18), | ||
| 503 | ("s_buffer_load_dwordx8 s[12:19], s[24:27], s40 offset:0x14", False, 8, 0x1C), | ||
| 504 | ("s_load_dwordx2 s[12:13], s[24:25], s40 offset:0x4", True, 2, 0x0C), | ||
| 505 | ("s_load_dwordx4 s[12:15], s[24:25], s40 offset:0x10", True, 4, 0x18), | ||
| 506 | ] | ||
| 507 | for line, pointer, n, first in cases: | ||
| 508 | with self.subTest(line=line): | ||
| 509 | ev, offsets, _ = self.run_load(line, pointer) | ||
| 510 | self.assertEqual(offsets, [first + 4 * i for i in range(n)]) | ||
| 511 | self.assertEqual([ev.sgpr[12 + i] for i in range(n)], [0x5EED0000 | (first + 4 * i) for i in range(n)]) | ||
| 512 | |||
| 513 | def test_imm_only_and_soffset_only_addresses_unchanged(self): | ||
| 514 | cases = [ | ||
| 515 | ("s_buffer_load_dword s12, s[24:27], 0x4", False, [0x04]), | ||
| 516 | ("s_buffer_load_dword s12, s[24:27], s40", False, [0x08]), | ||
| 517 | ("s_buffer_load_dwordx2 s[12:13], s[24:27], 0x10", False, [0x10, 0x14]), | ||
| 518 | ("s_buffer_load_dwordx4 s[12:15], s[24:27], s40", False, [0x08, 0x0C, 0x10, 0x14]), | ||
| 519 | ("s_load_dword s12, s[24:25], 0x4", True, [0x04]), | ||
| 520 | ("s_load_dword s12, s[24:25], s40", True, [0x08]), | ||
| 521 | ("s_load_dwordx2 s[12:13], s[24:25], 0", True, [0x00, 0x04]), | ||
| 522 | ("s_load_dwordx2 s[12:13], s[24:25], s40", True, [0x08, 0x0C]), | ||
| 523 | ] | ||
| 524 | for line, pointer, want in cases: | ||
| 525 | with self.subTest(line=line): | ||
| 526 | ev, offsets, _ = self.run_load(line, pointer) | ||
| 527 | self.assertEqual(offsets, want) | ||
| 528 | self.assertEqual([ev.sgpr[12 + i] for i in range(len(want))], [0x5EED0000 | o for o in want]) | ||
| 529 | |||
| 530 | def test_zero_imm_with_soffset_reads_the_soffset_address(self): | ||
| 531 | ev, offsets, _ = self.run_load("s_buffer_load_dword s12, s[24:27], s40 offset:0x0") | ||
| 532 | self.assertEqual(offsets, [8]) | ||
| 533 | self.assertEqual(ev.sgpr[12], 0x5EED0008) | ||
| 534 | |||
| 535 | def test_non_sgpr_scalar_register_offset_plus_imm(self): | ||
| 536 | # llvm-mc also accepts m0, vcc_lo or null as the register offset. null reads as zero; m0 is not an SGPR slot, | ||
| 537 | # so only check that the form still translates and that the immediate changes the code. | ||
| 538 | ev, offsets, _ = self.run_load("s_buffer_load_dword s12, s[24:27], null offset:0x14") | ||
| 539 | self.assertEqual(offsets, [0x14]) | ||
| 540 | self.assertEqual(ev.sgpr[12], 0x5EED0014) | ||
| 541 | bodies = [] | ||
| 542 | for line in ("s_buffer_load_dword s12, s[24:27], m0", "s_buffer_load_dword s12, s[24:27], m0 offset:0x4"): | ||
| 543 | ins = parse_line(line) | ||
| 544 | g = rdna2spirv.Gen("ps") | ||
| 545 | g.scalar_load(ins["op"], ins["args"], ins["mods"]) | ||
| 546 | bodies.append(g.body) | ||
| 547 | self.assertNotEqual(bodies[0], bodies[1]) | ||
| 548 | |||
| 549 | def test_offset_modifier_changes_the_emitted_code(self): | ||
| 550 | for line, pointer in (("s_buffer_load_dword s12, s[24:27], s40", False), | ||
| 551 | ("s_load_dword s12, s[24:25], s40", True)): | ||
| 552 | with self.subTest(line=line): | ||
| 553 | _, _, plain = self.run_load(line, pointer) | ||
| 554 | _, _, both = self.run_load(line + " offset:0x4", pointer) | ||
| 555 | self.assertNotEqual(plain.body, both.body) | ||
| 556 | |||
| 557 | def test_heap_base_is_added_once(self): | ||
| 558 | ins = parse_line("s_buffer_load_dword s12, s[24:27], s40 offset:0x4") | ||
| 559 | g = rdna2spirv.Gen("ps") | ||
| 560 | g.scalar_load(ins["op"], ins["args"], ins["mods"]) | ||
| 561 | ev = SmemBodyEvaluator(g, {24: 0x1000, 25: 0, 40: 8}, SmemMemoryBank(), heap_base=0x0000007F00000000) | ||
| 562 | self.assertEqual(ev.reads, [0x0000007F00000000 + 0x1000 + 12]) | ||
| 563 | |||
| 564 | def test_offset_forms_the_assembler_rejects_raise(self): | ||
| 565 | # llvm-mc accepts none of these; hand-written or damaged listings must not translate to a guessed address. | ||
| 566 | for line in ("s_buffer_load_dword s12, s[24:27], 0x8 offset:0x4", # immediate operand and offset modifier | ||
| 567 | "s_buffer_load_dword s12, s[24:27], s40 offset:zz", # unparseable immediate | ||
| 568 | "s_buffer_load_dword s12, s[24:27], s40 offset:0x4 offset:0x8", | ||
| 569 | "s_buffer_load_dword s12, s[24:27], s40, 0x4", # a fourth operand | ||
| 570 | "s_load_dword s12, s[24:25], 0x8 offset:0x4"): | ||
| 571 | with self.subTest(line=line): | ||
| 572 | ins = parse_line(line) | ||
| 573 | g = rdna2spirv.Gen("ps") | ||
| 574 | with self.assertRaises(rdna2spirv.Unsupported): | ||
| 575 | g.scalar_load(ins["op"], ins["args"], ins["mods"]) | ||
| 576 | |||
| 577 | |||
| 375 | class SmemMiniInterpStrictnessTests(unittest.TestCase): | 578 | class SmemMiniInterpStrictnessTests(unittest.TestCase): |
| 376 | """Verifies that spirv_mini_interp rejects storage classes and constructs it does not model. | 579 | """Verifies that spirv_mini_interp rejects storage classes and constructs it does not model. |
| 377 | 580 | ||