diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 23:06:33 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 23:06:33 -0500 |
| commit | 66fae9fb8867a80347330aa255ab9f74af0e4fc9 (patch) | |
| tree | 6efe937e2c61b3a0130f8a7e0cf7ff50bf7ea528 /tests | |
| parent | 0fd05a431a780304a18d468af7636e66e95b2f70 (diff) | |
| parent | ef777d4b73da0be91b533846c377626896274f24 (diff) | |
Merge pull request #2977 from omardev29
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
# docs/design.md
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/semantic/test_fma_mix_literal.py | 120 | ||||
| -rw-r--r-- | tests/test_fma_mix_literal_probe.py | 102 |
2 files changed, 222 insertions, 0 deletions
diff --git a/tests/semantic/test_fma_mix_literal.py b/tests/semantic/test_fma_mix_literal.py new file mode 100644 index 000000000..9719af593 --- /dev/null +++ b/tests/semantic/test_fma_mix_literal.py | |||
| @@ -0,0 +1,120 @@ | |||
| 1 | """A v_fma_mix* literal that llvm-mc prints cut to its low half is read from the blob. | ||
| 2 | |||
| 3 | A v_fma_mix source read as f16 (op_sel_hi set) takes the half op_sel names, so one 32-bit literal can give src0 its | ||
| 4 | low half and src2 its high half. llvm-mc prints that literal as its low 16 bits and re-encodes it with a zero high | ||
| 5 | half, so the printed-encoding check (#2026) refused such an instruction, and LLVM's text alone reads the high half as | ||
| 6 | +0.0. The translator now takes the literal from the blob when only its high half differs. Hand-built encodings | ||
| 7 | (registers and values made up for the test), no game data. | ||
| 8 | |||
| 9 | python -I tests/semantic/test_fma_mix_literal.py -v | ||
| 10 | """ | ||
| 11 | import os | ||
| 12 | import struct | ||
| 13 | import sys | ||
| 14 | import unittest | ||
| 15 | |||
| 16 | HERE = os.path.dirname(os.path.abspath(__file__)) | ||
| 17 | if HERE not in sys.path: | ||
| 18 | sys.path.insert(0, HERE) | ||
| 19 | |||
| 20 | from support import mrt0_words, rdna2spirv, translate_listing # noqa: E402 | ||
| 21 | |||
| 22 | NAME = "ps_fma_mix_literal" | ||
| 23 | CODE_START = 0x100 | ||
| 24 | # v_mov_b32 v2, 2.0 (VOP1); v_fma_mix_f32 v1, lit, v2, lit with op_sel:[0,0,1] op_sel_hi:[1,0,1]: src0 = the | ||
| 25 | # literal's low half as f16, src1 = v2 as f32, src2 = the literal's high half as f16. The stored literal is | ||
| 26 | # 0x40004200 (low 3.0, high 2.0); llvm-mc prints 0x4200 and encodes 0x00004200. The text and the printed encodings | ||
| 27 | # here are llvm-mc 23.1.5's for these bytes (a low half that is an inline f16 constant, such as 0x3c00, prints as | ||
| 28 | # lit(0x...) instead, so it is avoided). | ||
| 29 | MOV = ("v_mov_b32_e32 v2, 2.0", "f4 02 04 7e") | ||
| 30 | MIX_TEXT = "v_fma_mix_f32 v1, 0x4200, v2, 0x4200 op_sel:[0,0,1] op_sel_hi:[1,0,1]" | ||
| 31 | MIX_WORDS = "01 60 20 cc ff 04 fe 0b" | ||
| 32 | EXP = ("exp mrt0 v1, v1, v1, v1 done vm", "0f 18 00 f8 01 01 01 01") | ||
| 33 | END = ("s_endpgm", "00 00 81 bf") | ||
| 34 | |||
| 35 | |||
| 36 | def lit(value): | ||
| 37 | return struct.pack("<I", value).hex(" ") | ||
| 38 | |||
| 39 | |||
| 40 | def program(literal, words=MIX_WORDS, text=MIX_TEXT): | ||
| 41 | return [MOV, (text, f"{words} {lit(literal)}"), EXP, END] | ||
| 42 | |||
| 43 | |||
| 44 | def listing(prog, invalid=False): | ||
| 45 | size = sum(len(bytes.fromhex(enc)) for _, enc in prog) | ||
| 46 | lines = [f"; {NAME} code 0x{CODE_START:x}..0x{CODE_START + size:x} (scan), gfx1030, " | ||
| 47 | f"{1 if invalid else 0} invalid words"] | ||
| 48 | for text, enc in prog: | ||
| 49 | printed = ",".join(f"0x{b:02x}" for b in bytes.fromhex(enc)) | ||
| 50 | lines.append(f"\t{text:<40}; encoding: [{printed}]") | ||
| 51 | if invalid: | ||
| 52 | # translate() takes the re-anchoring path when the listing reports an undecodable word. | ||
| 53 | lines += ["", "; --- llvm-mc messages ---", "; <stdin>:1:1: warning: invalid instruction encoding"] | ||
| 54 | return ("\n".join(lines) + "\n").encode("utf-8") | ||
| 55 | |||
| 56 | |||
| 57 | def blob(prog): | ||
| 58 | return bytes(CODE_START) + b"".join(bytes.fromhex(enc) for _, enc in prog) | ||
| 59 | |||
| 60 | |||
| 61 | def f2u(x): | ||
| 62 | return struct.unpack("<I", struct.pack("<f", x))[0] | ||
| 63 | |||
| 64 | |||
| 65 | PRINTED = program(0x00004200) | ||
| 66 | STORED = program(0x40004200) | ||
| 67 | |||
| 68 | |||
| 69 | class FmaMixLiteralTest(unittest.TestCase): | ||
| 70 | def run_ps(self, data, fssm): | ||
| 71 | return mrt0_words(translate_listing(NAME, data, fssm=fssm)) | ||
| 72 | |||
| 73 | def test_high_half_comes_from_the_blob(self): | ||
| 74 | # fma(3.0, 2.0, 2.0) = 8.0 | ||
| 75 | self.assertEqual(self.run_ps(listing(PRINTED), blob(STORED)), [f2u(8.0)] * 4) | ||
| 76 | |||
| 77 | def test_reanchored_listing_takes_it_too(self): | ||
| 78 | self.assertEqual(self.run_ps(listing(PRINTED, invalid=True), blob(STORED)), [f2u(8.0)] * 4) | ||
| 79 | |||
| 80 | def test_without_the_blob_the_printed_literal_is_all_there_is(self): | ||
| 81 | # LLVM's text alone: the high half reads +0.0, fma(3.0, 2.0, 0.0) = 6.0 (what the listing says). | ||
| 82 | self.assertEqual(self.run_ps(listing(PRINTED), None), [f2u(6.0)] * 4) | ||
| 83 | |||
| 84 | def test_printed_low_half_must_match(self): | ||
| 85 | with self.assertRaises(rdna2spirv.Unsupported) as cm: | ||
| 86 | self.run_ps(listing(PRINTED), blob(program(0x40004201))) | ||
| 87 | self.assertIn("v_fma_mix_f32 at 0x104", str(cm.exception)) | ||
| 88 | |||
| 89 | def test_any_other_difference_is_still_refused(self): | ||
| 90 | # The stored instruction reads v3 as src1 (SRC1 field 0x103), not the printed v2. | ||
| 91 | other = program(0x40004200, words="01 60 20 cc ff 06 fe 0b") | ||
| 92 | with self.assertRaises(rdna2spirv.Unsupported) as cm: | ||
| 93 | self.run_ps(listing(PRINTED), blob(other)) | ||
| 94 | self.assertIn("dropped", str(cm.exception)) | ||
| 95 | |||
| 96 | def test_a_live_source_printed_as_inline_zero_is_refused(self): | ||
| 97 | # Stored SRC1 0x000 (s0, a source v_fma_mix reads) against a printed inline 0 (0x080). same_encoding forgives | ||
| 98 | # that for an unused source field; the repair must not, since all three v_fma_mix sources are live. | ||
| 99 | text = "v_fma_mix_f32 v1, 0x4200, 0, 0x4200 op_sel:[0,0,1] op_sel_hi:[1,0,1]" | ||
| 100 | printed = program(0x00004200, words="01 60 20 cc ff 00 fd 0b", text=text) | ||
| 101 | stored = program(0x40004200, words="01 60 20 cc ff 00 fc 0b", text=text) | ||
| 102 | with self.assertRaises(rdna2spirv.Unsupported) as cm: | ||
| 103 | self.run_ps(listing(printed), blob(stored)) | ||
| 104 | self.assertIn("v_fma_mix_f32 at 0x104", str(cm.exception)) | ||
| 105 | |||
| 106 | def test_an_inline_constant_with_the_printed_value_keeps_it(self): | ||
| 107 | # src0 = v2 (2.0) as f32, src1 = the inline integer 1 as f32, src2 = the literal's high half (op_sel:[0,0,1] | ||
| 108 | # op_sel_hi:[0,0,1]). The stored literal 0x3c000001 prints as 0x1, the same value as src1, so only the | ||
| 109 | # SRC field (0xff) tells the literal's user apart. Hand-written text: llvm-mc 23.1.5 prints this literal as | ||
| 110 | # lit(0x3c000001). fma(2.0, 1.4e-45, 1.0) = 1.0, also with the denormal flushed; src1 overwritten with the | ||
| 111 | # literal would give 2 * 0.0078125 + 1.0. | ||
| 112 | text = "v_fma_mix_f32 v1, v2, 1, 0x1 op_sel:[0,0,1] op_sel_hi:[0,0,1]" | ||
| 113 | words = "01 60 20 cc 02 03 fd 03" | ||
| 114 | printed = program(0x00000001, words=words, text=text) | ||
| 115 | stored = program(0x3C000001, words=words, text=text) | ||
| 116 | self.assertEqual(self.run_ps(listing(printed), blob(stored)), [f2u(1.0)] * 4) | ||
| 117 | |||
| 118 | |||
| 119 | if __name__ == "__main__": | ||
| 120 | unittest.main() | ||
diff --git a/tests/test_fma_mix_literal_probe.py b/tests/test_fma_mix_literal_probe.py new file mode 100644 index 000000000..7b87a0e2a --- /dev/null +++ b/tests/test_fma_mix_literal_probe.py | |||
| @@ -0,0 +1,102 @@ | |||
| 1 | """tools/fma_mix_literal_probe.py: the cases tell the two readings of a split v_fma_mix literal apart, the encodings | ||
| 2 | are the ones described, the translator side follows the halves reading, and a missing GPU is exit 77. Standard | ||
| 3 | library only; no GPU, no LLVM, no game files. | ||
| 4 | |||
| 5 | The expected values are worked out by hand (src1 is v2 = 2.0; f16 0x4000 = 2.0, 0x4200 = 3.0, 0xbc00 = -1.0; f32 | ||
| 6 | 0x40400000 = 3.0). No low half is an inline f16 constant, so llvm-mc prints each literal as the plain hex the listing | ||
| 7 | uses (an inline one, such as 0x3c00, prints as lit(0x...)): | ||
| 8 | |||
| 9 | A fma(lo 3.0, 2.0, hi 2.0) = 8.0 low reading: fma(3.0, 2.0, 0.0) = 6.0 | ||
| 10 | B fma(hi 3.0, 2.0, hi 3.0) = 9.0 low reading: fma(0.0, 2.0, 0.0) = 0.0 | ||
| 11 | E fma(2.0, 2.0, hi -1.0) = 3.0 low reading: fma(2.0, 2.0, 0.0) = 4.0 | ||
| 12 | C fma(3.0, 2.0, 3.0) = 9.0 either way | ||
| 13 | D fma(3.0, 2.0, 2.0) = 8.0 either way | ||
| 14 | """ | ||
| 15 | import contextlib | ||
| 16 | import io | ||
| 17 | import os | ||
| 18 | import struct | ||
| 19 | import sys | ||
| 20 | import unittest | ||
| 21 | from unittest import mock | ||
| 22 | |||
| 23 | ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
| 24 | sys.dont_write_bytecode = True | ||
| 25 | sys.path.insert(0, os.path.join(ROOT, "tools")) | ||
| 26 | sys.path.insert(0, os.path.join(ROOT, "tests", "semantic")) | ||
| 27 | |||
| 28 | import fma_mix_literal_probe as probe # noqa: E402 | ||
| 29 | |||
| 30 | BY_HAND = {"A": (8.0, 6.0), "B": (9.0, 0.0), "E": (3.0, 4.0), "C": (9.0, 9.0), "D": (8.0, 8.0)} | ||
| 31 | |||
| 32 | |||
| 33 | def f(word): | ||
| 34 | return struct.unpack("<f", struct.pack("<I", word))[0] | ||
| 35 | |||
| 36 | |||
| 37 | class Cases(unittest.TestCase): | ||
| 38 | def test_model_matches_hand_derivation(self): | ||
| 39 | self.assertEqual(sorted(c.name for c in probe.CASES), sorted(BY_HAND)) | ||
| 40 | for case in probe.CASES: | ||
| 41 | with self.subTest(case=case.name): | ||
| 42 | self.assertEqual((f(case.expected(probe.HALVES)), f(case.expected(probe.LOW))), BY_HAND[case.name]) | ||
| 43 | |||
| 44 | def test_only_the_controls_agree(self): | ||
| 45 | for case in probe.CASES: | ||
| 46 | halves, low = BY_HAND[case.name] | ||
| 47 | self.assertEqual(halves == low, case.what.startswith("control"), case.name) | ||
| 48 | |||
| 49 | def test_printed_literal_is_the_low_half(self): | ||
| 50 | # llvm-mc 23.1.5 re-encodes a v_fma_mix literal with its low 16 bits, also one read as f32 (D). | ||
| 51 | by = {c.name: c for c in probe.CASES} | ||
| 52 | self.assertEqual(by["A"].printed_literal(), 0x4200) | ||
| 53 | self.assertEqual(by["D"].printed_literal(), 0x0000) | ||
| 54 | |||
| 55 | |||
| 56 | class Encoding(unittest.TestCase): | ||
| 57 | def test_case_a_fields(self): | ||
| 58 | a = next(c for c in probe.CASES if c.name == "A") | ||
| 59 | w0, w1, lit = a.words(8, 2, a.literal) | ||
| 60 | self.assertEqual(w0 >> 26, 0b110011) # VOP3P | ||
| 61 | self.assertEqual((w0 >> 16) & 0x7F, 0x20) # v_fma_mix_f32 | ||
| 62 | self.assertEqual(w0 & 0xFF, 8) # vdst v8 | ||
| 63 | self.assertEqual([(w0 >> b) & 1 for b in (11, 12, 13)], [0, 0, 1]) # op_sel | ||
| 64 | self.assertEqual([(w1 >> 27) & 1, (w1 >> 28) & 1, (w0 >> 14) & 1], [1, 0, 1]) # op_sel_hi | ||
| 65 | self.assertEqual([(w1 >> (9 * i)) & 0x1FF for i in range(3)], [0xFF, 0x102, 0xFF]) | ||
| 66 | self.assertEqual(lit, 0x40004200) | ||
| 67 | |||
| 68 | def test_hw_kernel_carries_the_exact_words(self): | ||
| 69 | a = next(c for c in probe.CASES if c.name == "A") | ||
| 70 | w0, w1, lit = a.words(8, 2, a.literal) | ||
| 71 | self.assertIn(f".long 0x{w0:08x}, 0x{w1:08x}, 0x{lit:08x}", probe.hw_lines(a)) | ||
| 72 | |||
| 73 | def test_listing_prints_llvm_mc_text_and_the_blob_holds_the_literal(self): | ||
| 74 | a = next(c for c in probe.CASES if c.name == "A") | ||
| 75 | text, blob = probe.ps_program(a) | ||
| 76 | self.assertIn("v_fma_mix_f32 v0, 0x4200, v2, 0x4200 op_sel:[0,0,1] op_sel_hi:[1,0,1]", text) | ||
| 77 | self.assertIn(struct.pack("<I", 0x40004200), blob) | ||
| 78 | self.assertNotIn(struct.pack("<I", 0x40004200), text.encode()) | ||
| 79 | |||
| 80 | |||
| 81 | class Sides(unittest.TestCase): | ||
| 82 | def run_main(self, *argv): | ||
| 83 | out = io.StringIO() | ||
| 84 | with contextlib.redirect_stdout(out): | ||
| 85 | code = probe.main(list(argv)) | ||
| 86 | return code, out.getvalue() | ||
| 87 | |||
| 88 | def test_translator_side_follows_the_halves_reading(self): | ||
| 89 | code, out = self.run_main() | ||
| 90 | self.assertEqual(code, 0, out) | ||
| 91 | self.assertIn("translator: follows the halves reading", out) | ||
| 92 | |||
| 93 | def test_missing_hardware_is_77(self): | ||
| 94 | from hw_oracle import runner | ||
| 95 | with mock.patch.object(runner, "open_oracle", side_effect=runner.Unavailable("no AMD GPU")): | ||
| 96 | code, out = self.run_main("--oracle", "hw") | ||
| 97 | self.assertEqual(code, 77) | ||
| 98 | self.assertIn("hardware oracle unavailable", out) | ||
| 99 | |||
| 100 | |||
| 101 | if __name__ == "__main__": | ||
| 102 | unittest.main() | ||