1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
"""Tests for tools/mem_diff.py, the FUZZ-5 memory differential fuzzer (standard library only; no game data, GPU or
llvm-mc).
python -I tests/test_mem_diff.py
"""
import os
import random
import sys
import unittest
from fractions import Fraction
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "tools"))
sys.dont_write_bytecode = True
import mem_diff as md # noqa: E402
def program(lines, memory):
p = md.Program()
p.lines, p.memory = lines, dict(memory)
return p
class Harness(unittest.TestCase):
def test_self_test_passes(self):
self.assertTrue(md.self_test())
def test_generation_is_deterministic(self):
a = md.generate_program(random.Random(42), 3)
b = md.generate_program(random.Random(42), 3)
self.assertEqual(a.lines, b.lines)
self.assertEqual(a.memory, b.memory)
def test_every_family_runs_clean(self):
for family in sorted(md.GENERATORS):
with self.subTest(family=family):
counts, first = md.run_suite(60, seed=20261122, max_ops=2, families=[family])
self.assertEqual(counts["mismatch"], 0, first)
self.assertEqual(counts["harness-error"], 0, first)
self.assertEqual(counts["skip"], {}, counts["skip"])
self.assertEqual(counts["pass"], 60)
class Reference(unittest.TestCase):
"""The reference model on hand-made cases (the expected words are worked out by hand)."""
def run_ref(self, lines, memory):
ref = md.RefMachine(dict(memory))
ref.run(lines)
return ref
def test_structured_buffer_address(self):
# base 0x3000, stride 16, index 2, voffset 4, soffset 8, offset:4 -> 0x3000 + 8 + 4 + 32 + 4 = 0x3030
ref = self.run_ref(["s_mov_b32 s0, 0x3000", "s_mov_b32 s1, 0x100000", "s_mov_b32 s2, 4", "s_mov_b32 s3, 0",
"v_mov_b32 v2, 2", "v_mov_b32 v3, 4",
"buffer_load_dword v4, v[2:3], s[0:3], 8 idxen offen offset:4"], {0x3030: 0xABCD})
self.assertEqual(ref.v[4], 0xABCD)
def test_s_buffer_load_masks_the_descriptor_flags(self):
# V# word1 bits 31:16 (stride/flags) are not part of the base address.
ref = self.run_ref(["s_mov_b32 s0, 0x4000", "s_mov_b32 s1, 0xFFFF0001", "s_mov_b32 s2, 0x100", "s_mov_b32 s3, 0",
"s_buffer_load_dword s8, s[0:3], 0x8"], {(1 << 32) + 0x4008: 0x55})
self.assertEqual(ref.s[8], 0x55)
def test_global_negative_offset_and_sign_extension(self):
ref = self.run_ref(["v_mov_b32 v2, 0x5010", "v_mov_b32 v3, 0",
"global_load_sbyte v4, v[2:3], off offset:-15"], {0x5000: 0x0000_8000})
self.assertEqual(ref.v[4], 0xFFFFFF80) # byte 1 of the word at 0x5000, sign-extended
def test_cmpswap_and_glc(self):
memory = {0x6000: 7}
lines = ["s_mov_b32 s0, 0x6000", "s_mov_b32 s1, 0", "s_mov_b32 s2, 0x10", "s_mov_b32 s3, 0",
"v_mov_b32 v4, 9", "v_mov_b32 v5, 7", "buffer_atomic_cmpswap v[4:5], off, s[0:3], 0 glc"]
ref = self.run_ref(lines, memory)
self.assertEqual((ref.mem[0x6000], ref.v[4]), (9, 7))
class FormatDecode(unittest.TestCase):
"""The reference decode of typed-buffer formats, from the GFX10 names (expected words worked out by hand)."""
def test_names_list_widths_from_the_top_bit(self):
self.assertEqual(md.parse_format("GFX10_FORMAT_2_10_10_10_UNORM"), ([10, 10, 10, 2], "UNORM"))
self.assertEqual(md.parse_format("GFX10_FORMAT_16_16_SNORM"), ([16, 16], "SNORM"))
self.assertIsNone(md.parse_format("GFX10_FORMAT_10_11_11_FLOAT")) # packed float: not modelled
def test_correct_rounding(self):
self.assertEqual(md.f32_round(Fraction(3, 255)), 0x3C40C0C1) # the multiply-by-reciprocal gives ...C2
self.assertEqual(md.f32_round(Fraction(1, 1)), 0x3F800000)
self.assertEqual(md.f32_round(Fraction(1, 3)), 0x3EAAAAAB)
def test_decode_examples(self):
# 8_8_8_8_UINT: X in the low byte; four channels, nothing missing.
self.assertEqual(md.decode_element(0xDDCCBBAA, [8, 8, 8, 8], "UINT"), [0xAA, 0xBB, 0xCC, 0xDD])
# 16_16_SNORM: X = 0x8000 clamps to -1.0, Y = 0x7FFF is 1.0; Z/W repeat XYXY (SHADER-86).
self.assertEqual(md.decode_element(0x7FFF8000, [16, 16], "SNORM"),
[0xBF800000, 0x3F800000, 0xBF800000, 0x3F800000])
# 2_10_10_10_UNORM: X10 at bit 0 ... W2 at bit 30. All ones in X, W = 3 -> 1.0, Y = Z = 0.
self.assertEqual(md.decode_element(0xC00003FF, [10, 10, 10, 2], "UNORM"),
[0x3F800000, 0, 0, 0x3F800000])
# 32_UINT: missing Y/Z/W repeat X (SHADER-86).
self.assertEqual(md.decode_element(7, [32], "UINT"), [7, 7, 7, 7])
def test_shader86_closed_open_questions(self):
"""Missing W repeats X; UNORM raw 3 matches correctly rounded 0x3c40c0c1 (no 1-ULP split)."""
base = ["s_mov_b32 s0, 0x7000", "s_mov_b32 s1, 0", "s_mov_b32 s2, 0x100"]
w32 = program(base + [f"s_mov_b32 s3, {22 << 12 | md.XYZW:#x}", "v_mov_b32 v2, 0",
"buffer_load_format_xyzw v[4:7], v2, s[0:3], 0 offen", "s_waitcnt vmcnt(0)",
"v_mov_b32 v8, v4", "v_mov_b32 v9, v5", "v_mov_b32 v10, v6", "v_mov_b32 v11, v7"],
{0x7000: 0x40000000})
self.assertEqual(md.run_one("ps_mem_w32", w32)[0], "match")
unorm = program(base + [f"s_mov_b32 s3, {56 << 12 | md.XYZW:#x}", "v_mov_b32 v2, 0",
"buffer_load_format_x v4, v2, s[0:3], 0 offen", "s_waitcnt vmcnt(0)",
"v_mov_b32 v8, v4", "v_mov_b32 v9, v4", "v_mov_b32 v10, v4", "v_mov_b32 v11, v4"],
{0x7000: 3})
self.assertEqual(md.run_one("ps_mem_unorm3", unorm)[0], "match")
class GlobalOffsetRegression(unittest.TestCase):
"""llvm-mc prints a gfx10 global_* immediate as signed decimal ('offset:-16'). The translation must add -16, not
sign-extend the printed value a second time (which reads 4 KiB lower)."""
def test_negative_global_offset(self):
memory = {0x8000 - 16: 0x11111111, 0x8000 - 16 - 0x1000: 0x22222222}
for form in (["v_mov_b32 v2, 0x8000", "v_mov_b32 v3, 0", "global_load_dword v8, v[2:3], off offset:-16"],
["s_mov_b32 s4, 0x8000", "s_mov_b32 s5, 0", "v_mov_b32 v2, 0",
"global_load_dword v8, v2, s[4:5] offset:-16"]):
with self.subTest(form=form[-1]):
status, detail = md.run_one("ps_mem_global_neg", program(
form + ["s_waitcnt vmcnt(0)", "v_mov_b32 v9, 0", "v_mov_b32 v10, 0", "v_mov_b32 v11, 0"], memory))
self.assertEqual(status, "match", detail)
def test_negative_global_store_offset(self):
status, detail = md.run_one("ps_mem_global_neg_store", program(
["v_mov_b32 v2, 0x9000", "v_mov_b32 v3, 0", "v_mov_b32 v4, 0xC0FFEE",
"global_store_dword v[2:3], v4, off offset:-2048", "v_mov_b32 v8, 0", "v_mov_b32 v9, 0",
"v_mov_b32 v10, 0", "v_mov_b32 v11, 0"], {}))
self.assertEqual(status, "match", detail)
if __name__ == "__main__":
unittest.main()
|