interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / tests / amd-exec / semantics.py

366 lines · 21824 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3"""Independent manual-derived single-instruction expectations, CDNA3 PDF pages in each case.
4Python is a test instrument only. No executor source is read to derive an expectation.
5Integer arithmetic below is the recorded calculation of the manual's pseudocode.
6"""
7import pathlib
8import struct
9import subprocess
10import sys
11
12U32 = (1 << 32) - 1
13U64 = (1 << 64) - 1
14CASES = []
15
16def s(r): return (0, r)
17def v(r, lane=0): return (1, r * 64 + lane)
18def t(r): return (2, r)
19def sop(op, a=4, b=5, d=8): return [0x80000000 | op << 23 | d << 16 | b << 8 | a]
20def sop1(op, a=4, d=8): return [0xbe800000 | d << 16 | op << 8 | a]
21def vop(op, a=257, b=2, d=3): return [op << 25 | d << 17 | b << 9 | a]
22def sm(op, d=8, offset=0): return [0xc0020000 | op << 18 | d << 6, offset]
23def glob(op, offset=0, addr=4):
24 return [0xdc008000 | op << 18 | (offset & 8191),
25 addr | (3 << 8 if op == 28 else 0) | 127 << 16 | (3 << 24 if op == 20 else 0)]
26
27def case(name, page, words, seed=None, result=None, *, ex=1, vc=U64, cc=1,
28 nex=None, nvc=None, ncc=None, pc=None, running=1, mem=None, memory=None, refusal=0):
29 expect = {t(14): ex if nex is None else nex, t(15): vc if nvc is None else nvc,
30 t(16): cc if ncc is None else ncc, t(5): len(words)*4 if pc is None else pc}
31 expect.update(seed or {}) # inputs and untouched registers must survive unless overwritten
32 expect.update(result or {})
33 CASES.append(dict(name=name, page=page, words=words, seed=seed or {}, expect=expect,
34 ex=ex, vc=vc, cc=cc, running=running, mem=mem, memory=memory, refusal=refusal))
35
36# VOP1 readfirstlane writes an SGPR even when EXEC is empty (CDNA3 physical p187).
37for mask, lane in ((0, 0), (1, 0), (2, 1), (1 << 63, 63),
38 ((1 << 63) | 8, 3), (U64, 0)):
39 seed = {v(1, i): 0xabc00000 + i for i in range(64)}
40 seed[s(8)] = 0xdeadbeef
41 seed[v(8)] = 0x11223344
42 case(f'readfirst-{mask}', 187, [0x7e000000 | 8 << 17 | 2 << 9 | 257],
43 seed, {s(8): 0xabc00000 + lane}, ex=mask)
44for source, dest in ((4, 8), (255, 8), (257, 102), (257, 126), (257, 255)):
45 case(f'readfirst-refuse-{source}-{dest}', 187,
46 [0x7e000000 | dest << 17 | 2 << 9 | source], refusal=66)
47
48# Scalar arithmetic: full unsigned temporary, destination low32; SUB sets borrow, not carry/no-borrow.
49for a, b in [(0, 1), (1, 1), (2, 1), (U32, 1), (0, U32)]:
50 case(f'sub-{a}-{b}', 97, sop(1), {s(4): a, s(5): b}, {s(8): (a-b)&U32}, ncc=int(b>a))
51for op, label, carry_in in [(0, 'add', 0), (4, 'addc', 1)]:
52 for a, b in [(0, 0), (U32, 0), (U32, 1), (U32, U32)]:
53 total = a+b+carry_in
54 case(f'{label}-{a}-{b}', 97 if op == 0 else 98, sop(op), {s(4): a, s(5): b},
55 {s(8): total&U32}, cc=carry_in, ncc=int(total>U32))
56for width, op in [(32, 28), (64, 29)]:
57 for a, b in [(0, 1), (1, width), (0x80000001, width+1), (U32, width-1)]:
58 value = a | (0x12345678 << 32 if width == 64 else 0)
59 shifted = (value << (b & (width-1))) & ((1 << width)-1)
60 seed = {s(4): a, s(6): b}
61 result = {s(8): shifted & U32}
62 if width == 64:
63 seed[s(5)] = value >> 32
64 result[s(9)] = shifted >> 32
65 case(f'lshl{width}-{a}-{b}', 103, sop(op, b=6), seed, result, ncc=int(shifted != 0))
66for a, b in [(0, 1), (1, 32), (0x80000001, 33), (U32, 31)]:
67 shifted = a >> (b & 31)
68 case(f'lshr32-{a}-{b}', 103, sop(30), {s(4): a, s(5): b},
69 {s(8): shifted}, ncc=int(shifted != 0))
70for a, b, expected in [(0x80000000, 0, 0), (0, 0x80000000, 0),
71 (0x7fc01234, 0x3f800000, 0x3f800000),
72 (0x7f801234, 0x3f800000, 0x7fc01234),
73 (0xbf800000, 0xc0000000, 0xbf800000)]:
74 case(f'fmax-{a:x}-{b:x}', 172, vop(11), {v(1): a, v(2): b}, {v(3): expected})
75case('fsub-denorm', 169, vop(2), {v(1): 3, v(2): 2}, {v(3): 1})
76for op, a, expected in [(32, 0xff800000, 0), (32, 0x80000000, 0x3f800000),
77 (32, 0x7f800000, 0x7f800000), (32, 1, 0x3f800000),
78 (32, 0x3f800000, 0x40000000), (32, 0xc2fc0000, 0x800000),
79 (32, 0xc2fe0000, 0), (34, 0xff800000, 0x80000000),
80 (34, 0xc0000000, 0xbf000000), (34, 0x80000000, 0xff800000),
81 (34, 0, 0x7f800000), (34, 0x7f800000, 0), (34, 1, 0x7f800000)]:
82 case(f'trans-{op}-{a:x}', 195 if op == 32 else 196,
83 [0x7e000000 | 3 << 17 | op << 9 | 257], {v(1): a}, {v(3): expected})
84for a, expected in [(0xff800000, 0xffc00000), (0x80000000, 0xff800000),
85 (0, 0x7f800000), (0x40800000, 0x3f000000),
86 (0x7f800000, 0), (1, 0x7f800000), (0x80000001, 0xff800000),
87 (0xbf800000, 0xffc00000)]:
88 case(f'rsq-{a:x}', 197, [0x7e000000 | 3 << 17 | 36 << 9 | 257],
89 {v(1): a, v(3,1): 0xdeadbeef, v(1,63): a},
90 {v(3): expected, v(3,63): expected}, ex=(1 << 63)|1)
91for a, expected in [(0xff800000,0xffc00000),(0xbf800000,0xffc00000),
92 (0x80000000,0xff800000),(0,0xff800000),
93 (1,0xff800000),(0x80000001,0xff800000),
94 (0x3f800000,0),(0x40000000,0x3f800000),
95 (0x3f000000,0xbf800000),(0x7f800000,0x7f800000)]:
96 case(f'log-{a:x}',187,[0x7e000000 | 3 << 17 | 33 << 9 | 257],
97 {v(1):a,v(3,1):0xdeadbeef,v(1,63):a},
98 {v(3):expected,v(3,63):expected},ex=(1<<63)|1)
99for op, label in [(36, 'mul-low'), (44, 'mul-high')]:
100 for a, b in [(U32, U32), (0x80000000, 3), (0, 17)]:
101 product = a*b
102 result = product & U32 if op == 36 else product >> 32
103 case(f'{label}-{a}-{b}', 104 if op == 36 else 107, sop(op), {s(4): a, s(5): b}, {s(8): result})
104case('mov-preserves-scc', 115, sop1(0), {s(4): U32}, {s(8): U32})
105case('mov-literal', 115, sop1(0, a=255)+[0x81234567], result={s(8): 0x81234567})
106for mask in [0, 0x8000000000000001]:
107 case(f'saveexec-{mask}', 125, sop1(32, a=106), result={s(8): 3, s(9): 0x80000000},
108 ex=0x8000000000000003, vc=mask, nex=mask & 0x8000000000000003, ncc=int(mask != 0))
109case('saveexec-source-destination-alias', 125, sop1(32, a=8), {s(8): 2, s(9): 0},
110 {s(8): 3, s(9): 0}, ex=3, nex=2)
111# Branch targets are signed DWORD displacements relative to next PC; padding is never executed.
112for op, label in [(4, 'scc0'), (5, 'scc1')]:
113 for cc in [0, 1]:
114 take = (cc == (op-4))
115 case(f'branch-{label}-{cc}', 139, [0xbf800001 | op << 16, 0xbf810000, 0xbf810000], cc=cc,
116 pc=8 if take else 4)
117case('branch-negative', 139, [0xbf84ffff], cc=0, pc=0)
118for ex in [0, 1]:
119 case(f'branch-execz-{ex}', 140, [0xbf880001, 0xbf810000, 0xbf810000], ex=ex, pc=4 if ex else 8)
120for wait in [0xc07f, 0x0f70]:
121 case(f'wait-{wait}', 141, [0xbf8c0000 | wait])
122case('endpgm', 138, [0xbf810000], running=0)
123for immediate in (0,15,65535):
124 case(f'nop-{immediate}',138,[0xbf800000 | immediate])
125# Scalar memory components individually ignore low two bits (p68), not just the final sum.
126mem = struct.pack('<16I', *range(0x12340000, 0x12340010))
127for op in [0, 1, 2]:
128 case(f'smem-x{1<<op}', 146 if op < 2 else 147, sm(op, offset=4), {s(0): 4, s(1): 0},
129 {s(8+i): 0x12340002+i for i in range(1 << op)}, mem=mem)
130case('smem-component-low-bits', 68, sm(0, offset=3), {s(0): 3, s(1): 0}, {s(8): 0x12340000}, mem=mem)
131for op, dest in [(1, 9), (2, 10)]:
132 case(f'smem-bad-destination-{op}', 70, sm(op, d=dest), refusal=66)
133case('smem-offset-bit20-refused', 68, sm(0, offset=1 << 20), refusal=66)
134# Vector integer operations: lane0/63 active, lane1 destination is untouched.
135for op, label, a, b, answer in [
136 (19, 'and', 0xa5, 0x5a, 0), (52, 'add-wrap', U32, 2, 1),
137 (18, 'lshl-mask', 33, 0x80000001, 2), (16, 'lshr-mask', 33, 0x80000001, 0x40000000)]:
138 case(label, {19:174, 52:183, 18:173, 16:173}[op], vop(op),
139 {v(1): a, v(2): b, v(3,1): 0xdeadbeef, v(1,63): a, v(2,63): b},
140 {v(3): answer, v(3,63): answer}, ex=(1 << 63)|1)
141for op, label in [(25, 'add-co'), (28, 'addc-co')]:
142 carry_in = 1 if op == 28 else 0
143 case(label, 175 if op == 25 else 176, vop(op),
144 {v(1): U32, v(2): 1, v(1,63): 0, v(2,63): 0, v(3,1): 0xdeadbeef},
145 {v(3): carry_in, v(3,63): carry_in}, ex=(1 << 63)|1, nvc=1)
146 case(label+'-zero-exec', 22, vop(op), ex=0, nvc=0)
147# Input VCC must be captured before the instruction writes it, including scalar VCC sources.
148case('add-co-vcc-source-snapshot', 22, vop(25, a=106),
149 {v(2): U32, v(2,1): 0}, {v(3): 1, v(3,1): 2}, ex=3, vc=2, nvc=1)
150cmpword = 0x7c000000 | 204 << 17 | 2 << 9
151case('cmp-unsigned-and-exec', 251, [cmpword | 257],
152 {v(1): U32, v(2): 0, v(1,63): 0, v(2,63): U32}, ex=(1 << 63)|1, nvc=1)
153case('cmp-vcc-source-snapshot', 22, [cmpword | 106], {v(2): 0}, vc=2, nvc=1)
154case('cmp-zero-exec', 22, [cmpword | 257], ex=0, nvc=0)
155case('vmov-inline-negative', 186, [0x7e000200 | 3 << 17 | 193], result={v(3): U32})
156case('vmov-literal', 186, [0x7e000200 | 3 << 17 | 255, 0x80000001], result={v(3): 0x80000001})
157case('vmov-exec-hi', 186, [0x7e000200 | 3 << 17 | 127], result={v(3): 0x80000000, v(3,63): 0x80000000}, ex=(1 << 63)|1)
158# Table23 p42 specifies the floating inline selector bits, including exact 1/(2*pi).
159for selector, value in enumerate((0.5,-0.5,1.0,-1.0,2.0,-2.0,4.0,-4.0,None),240):
160 bits = 0x3e22f983 if value is None else struct.unpack('<I',struct.pack('<f',value))[0]
161 case(f'vmov-inline-float-{selector}',42,[0x7e000200 | 3 << 17 | selector],
162 {v(3,1):0xdeadbeef},{v(3):bits,v(3,63):bits},ex=(1 << 63)|1)
163 case(f'smov-inline-float-{selector}',42,sop1(0,a=selector),result={s(8):bits})
164case('vadd-inline-one',42,vop(1,a=242),{v(2):0x3f800000},{v(3):0x40000000})
165# FP bit patterns: exact denormal addition, halfway-to-even, signed zero, exact fused cancellation.
166# The exhaustive Fraction oracle separately covers the numerical algorithms and special values.
167for name, op, a, b, c, answer, page in [
168 ('fadd-denorm', 1, 1, 1, 0, 2, 169),
169 ('fadd-halfway-even', 1, 0x3f800000, 0x33800000, 0, 0x3f800000, 169),
170 ('fadd-minus-zero', 1, 0x80000000, 0x80000000, 0, 0x80000000, 169),
171 ('fmul-denorm', 5, 1, 0x3f800000, 0, 1, 170),
172 ('fmul-negative-zero', 5, 0x80000000, 0x3f800000, 0, 0x80000000, 170),
173 ('fmac-fused', 59, 0x3f800001, 0x3f7ffffe, 0xbf800000, 0xa8800000, 185)]:
174 case(name, page, vop(op), {v(1): a, v(2): b, v(3): c}, {v(3): answer})
175for offset, address in [(4, 4), (-4, 12)]:
176 case(f'global-load-{offset}', 504, glob(20, offset), {v(4): address, v(5): 0},
177 {v(3): 0x12340002}, mem=mem)
178 changed = bytearray(mem)
179 struct.pack_into('<I', changed, 8, 0xabcdef01)
180 case(f'global-store-{offset}', 505, glob(28, offset), {v(4): address, v(5): 0, v(3): 0xabcdef01},
181 mem=mem, memory=bytes(changed))
182case('global-odd-address-pair', 21, glob(20, addr=5), refusal=66)
183# FP8 additions are separately derived from the manual, not the executor implementation.
184import runpy
185fp8_decode = runpy.run_path(str(pathlib.Path(__file__).with_name('fp8gemv.py')))['decode']
186cv = 0x7e000000 | 3 << 17 | 84 << 9 | 249
187for byte in range(256):
188 case(f'fp8-byte-{byte:02x}', '58,211-212', [cv, 0x601],
189 {v(1): 0xabcdef00 | byte, v(3,1): 0xdeadbeef, v(1,63): byte},
190 {v(3): fp8_decode(byte), v(3,63): fp8_decode(byte)}, ex=(1 << 63)|1)
191case('fp8-zero-exec', 211, [cv, 0x601], {v(3): 0xdeadbeef}, ex=0)
192for control in [0x10601, 0x20601, 0x30601, 0x1601, 0x800601]:
193 case(f'fp8-unsupported-sdwa-{control:x}', 212, [cv, control], refusal=66)
194case('fp8-non-sdwa', 211, [(cv & ~511) | 257], refusal=66)
195for op in [645, 646]:
196 for a, b in [(U32, U32), (0x80000000, 3), (0, 17)]:
197 answer = (a*b >> (32 if op == 646 else 0)) & U32
198 case(f'vmul-{op}-{a}-{b}', 361, [0xd0000003 | op << 16, 257 | 4 << 9],
199 {v(1): a, s(4): b, v(3,1): 0xdeadbeef}, {v(3): answer})
200case('vmul-vector-source', 361, [0xd2850003, 257 | 258 << 9],
201 {v(1): U32, v(2): 2}, {v(3): 0xfffffffe})
202case('vmul-modifier-refused', 361, [0xd2850103, 257 | 4 << 9], refusal=66)
203case('vmul-src2-refused', 361, [0xd2850003, 257 | 4 << 9 | 1 << 18], refusal=66)
204case('global-ubyte-last-byte', 503, [0xdc408000, 3 << 24 | 127 << 16 | 4],
205 {v(4): 0, v(5): 0}, {v(3): 255}, mem=b'\xff')
206case('global-ubyte-negative-offset', 503, [0xdc409fff, 3 << 24 | 127 << 16 | 4],
207 {v(4): 1, v(5): 0}, {v(3): 128}, mem=b'\x80')
208case('global-ubyte-range-refused', 503, [0xdc408000, 3 << 24 | 127 << 16 | 4],
209 {v(4): 1, v(5): 0}, mem=b'\xff', refusal=67)
210# Fixed target profile mi300x-vf-rocm722-d16-zero. Loaded high16 comes from PDF p507;
211# unused low16 follows raw MI300X VF / ROCm7.2.2 observations, not generic pseudocode.
212# Evidence: AMD-MI300X-CODEX-20261113T0315Z/amdserve-d16probe.txt
213# SHA256 af7365ee4f2f0da6db6e995cda83c4ac4a6ee6da3df3446936621ca96c4ac0c7.
214bf16 = runpy.run_path(str(pathlib.Path(__file__).with_name('bf16gemv.py')))
215half = [0xdc008000 | 37 << 18, 3 << 24 | 127 << 16 | 4]
216for bits in bf16['VALUES']:
217 case(f'global-d16-hi-{bits:04x}', 507, half,
218 {v(4): 2, v(5): 0, v(3): 0x1234beef, v(3,1): 0xdeadbeef,
219 v(4,63): 2, v(5,63): 0, v(3,63): 0xffff0000},
220 {v(3): bits << 16, v(3,63): bits << 16},
221 ex=(1 << 63)|1, mem=b'\xa5\xa5'+struct.pack('<H', bits))
222case('global-d16-hi-negative-offset', 507, [half[0] | 8190, half[1]],
223 {v(4): 2, v(5): 0, v(3): 0x12345678}, {v(3): 0x803f0000}, mem=b'\x3f\x80')
224case('global-d16-hi-zero-exec', 507, half, {v(3): 0xdeadbeef}, ex=0, mem=b'')
225case('global-d16-hi-short-memory', 507, half, mem=b'\x01', refusal=67)
226case('global-d16-hi-partial-tail', 507, half, {v(4): 2}, mem=b'\x01\x02\x03', refusal=67)
227case('global-d16-hi-low-form-refused', 507, [half[0] ^ 1 << 18, half[1]], refusal=66)
228case('global-d16-hi-modifier-refused', 87, [half[0] | 1 << 16, half[1]], refusal=66)
229
230# New GEMV2 operations: literal expectations follow CDNA3 pseudocode, not executor output.
231for mask in (0, 1, 0x8000000000000001, U64):
232 case(f'mov64-exec-{mask}', 115, sop1(1, a=4, d=126),
233 {s(4): mask & U32, s(5): mask >> 32}, nex=mask, cc=1)
234case('mov64-odd-pair-refused', 115, sop1(1, a=5, d=126), refusal=66)
235for a, b in ((0, U32), (0xaaaaaaaa, 0x55555555), (0x80000001, 0x80000000)):
236 case(f'xor-{a}-{b}', 174, vop(21), {v(1): a, v(2): b}, {v(3): a ^ b})
237for op, width in ((21, 2), (23, 4)):
238 words = [0xdc008000 | op << 18, 4 | 127 << 16 | 8 << 24]
239 seed = {v(4): 4, v(5): 0}
240 payload = struct.pack('<16I', *range(0x11223340, 0x11223350))
241 case(f'global-wide-{width}', 504, words, seed,
242 {v(8+i): 0x11223341+i for i in range(width)}, mem=payload)
243 case(f'global-wide-{width}-boundary', 504, words, {v(4): 64-4*width, v(5): 0},
244 {v(8+i): 0x11223350-width+i for i in range(width)}, mem=payload)
245 case(f'global-wide-{width}-out-of-memory', 504, words, {v(4): 65-4*width, v(5): 0},
246 mem=payload, refusal=67)
247 bad = [words[0], 4 | 127 << 16 | (257-width) << 24]
248 case(f'global-wide-{width}-register-overflow', 504, bad, refusal=66)
249def ds(offset=0, addr=1, data=2, dest=2):
250 return [0xd87e0000 | offset, addr | data << 8 | dest << 24]
251# Aliasing destination DATA0 must observe the original wave, never earlier writes.
252seed = {v(1, lane): (lane ^ 1)*4 for lane in range(64)}
253seed.update({v(2, lane): 1000+lane for lane in range(64)})
254case('bpermute-alias-snapshot', 437, ds(), seed,
255 {v(2, lane): 1000+(lane ^ 1) for lane in range(64)}, ex=U64)
256case('bpermute-inactive-source', 437, ds(dest=3),
257 {v(1): 4, v(2, 1): 1234, v(3, 1): 0xfeed}, {v(3): 0}, ex=1)
258case('bpermute-byte-offset-wrap', 437, ds(7, dest=3),
259 {v(1): U32, v(2, 1): 456}, {v(3): 456, v(3, 1): 456}, ex=3)
260case('bpermute-inactive-destination', 437, ds(dest=3),
261 {v(1): 0, v(2): 9, v(3): 0xfeed}, ex=0)
262for modifier in (1 << 16, 1 << 25):
263 words = ds()
264 words[0] |= modifier
265 case(f'bpermute-unsupported-modifier-{modifier}', 437, words, refusal=66)
266words = ds()
267words[1] |= 1 << 16
268case('bpermute-unused-data1', 437, words, refusal=66)
269
270MUTATIONS = {
271 'readfirst-lane-zero': ('put sr get st 13 vg sub get st 11 256 first',
272 'put sr get st 13 vg sub get st 11 256 0'),
273 'lshr-left': ('r = shr a and b 31', 'r = and shl a and b 31 0xffffffff'),
274 'fsub-add': ('r = fpadd a xor b 0x80000000', 'r = fpadd a b'),
275 'fmax-ignores-snan': ('ret or a 0x400000', 'ret b'),
276 'exp-returns-input': ('r = fpexp r', 'r = r'),
277 'rcp-returns-input': ('r = fprcp r', 'r = r'),
278 'rsq-is-rcp': ('r = fprsq r', 'r = fprcp r'),
279 'log-returns-input': ('r = fplog r', 'r = r'),
280 'inline-float-refused': ('if and ge s 240 le s 248\nret\nend',
281 'if and ge s 240 le s 248\nfail 66\nend'),
282 'nop-clears-scc': ('if eq cl 4\nop = get st 10\ncc = get st 16',
283 'if eq cl 4\nop = get st 10\nif eq op 0\nput st 16 0\nend\ncc = get st 16'),
284 'sub-no-borrow': ('put st 16 fmbool lt a b', 'put st 16 fmbool ge a b'),
285 'inactive-vcc': ('if or eq cl 6 and eq cl 5 or eq get st 10 25 eq get st 10 28', 'if eq cl 6'),
286 'vcc-source': ('get st 17', 'get st 15'),
287 'smem-low-bits': ('p = or and get sr sb 0xfffffffc shl get sr add sb 1 32', 'p = or get sr sb shl get sr add sb 1 32'),
288 'smem-offset-low-bits': ('put st 12 and x 0xfffffffc', 'put st 12 x'),
289 'smem-dest-alignment': ('if ne and sd sub shl ulong 1 op 1 0', 'if false'),
290 'smem-offset-bit20': ('if ne shr x 20 0', 'if ne shr x 21 0'),
291 'vgpr-pair-alignment': ('if or ge va 255 ne and va 1 0', 'if ge va 255'),
292 'd16-low-preserved': ('vs get st 13 lane shl h 16',
293 'vs get st 13 lane or and vg get st 13 lane 65535 shl h 16'),
294 'mov64-loses-scc': ('put st 14 or get sr ss shl get sr add ss 1 32',
295 'put st 14 or get sr ss shl get sr add ss 1 32\nput st 16 0'),
296 'xor-uses-or': ('r = xor a b', 'r = or a b'),
297 'wide-drops-last': ('for i ulong n\nvs add get st 13 i lane mread add p mul i 4',
298 'for i ulong sub n 1\nvs add get st 13 i lane mread add p mul i 4'),
299 'bpermute-live-alias': ('vs get st 13 lane get perm lane',
300 'src = and shr add vg get st 11 lane get st 7 2 63\nvs get st 13 lane vg get st 12 src'),
301}
302MUTANT_CASE = {
303 'readfirst-lane-zero': 'readfirst-2',
304 'lshr-left': 'lshr32-2147483649-33', 'fsub-add': 'fsub-denorm',
305 'fmax-ignores-snan': 'fmax-7f801234-3f800000',
306 'exp-returns-input': 'trans-32-3f800000', 'rcp-returns-input': 'trans-34-c0000000',
307 'rsq-is-rcp': 'rsq-40800000',
308 'log-returns-input': 'log-40000000',
309 'inline-float-refused': 'vmov-inline-float-242',
310 'nop-clears-scc': 'nop-0',
311 'sub-no-borrow': 'sub-0-1', 'inactive-vcc': 'add-co',
312 'vcc-source': 'cmp-vcc-source-snapshot', 'smem-low-bits': 'smem-component-low-bits',
313 'smem-offset-low-bits': 'smem-component-low-bits',
314 'smem-dest-alignment': 'smem-bad-destination-1', 'smem-offset-bit20': 'smem-offset-bit20-refused',
315 'vgpr-pair-alignment': 'global-odd-address-pair',
316 'd16-low-preserved': 'global-d16-hi-3f80',
317 'mov64-loses-scc': 'mov64-exec-0', 'xor-uses-or': 'xor-2147483649-2147483648',
318 'wide-drops-last': 'global-wide-4', 'bpermute-live-alias': 'bpermute-alias-snapshot',
319}
320
321def run_case(exe, out, c):
322 code, memory, state = [out / ('single.'+suffix) for suffix in ['text', 'memory', 'state']]
323 code.write_bytes(struct.pack('<'+'I'*len(c['words']), *c['words']))
324 before = c['mem'] if c['mem'] is not None else bytes(64)
325 memory.write_bytes(before)
326 init = [c['ex'] & U32, c['ex'] >> 32, c['vc'] & U32, c['vc'] >> 32, c['cc'], len(c['seed'])]
327 for (bank, index), value in c['seed'].items(): init += [bank, index, value]
328 init += [len(c['expect'])]
329 for bank, index in c['expect']: init += [bank, index]
330 state.write_bytes(struct.pack('<'+'I'*len(init), *init))
331 result = subprocess.run([exe, str(code), str(memory), str(state)], capture_output=True, timeout=15)
332 if result.returncode != c['refusal']:
333 return f'exit={result.returncode} expected={c["refusal"]}'
334 if c['refusal']: return None
335 expected = [c['running'], *c['expect'].values()]
336 expected_bytes = struct.pack('<'+'Q'*len(expected), *expected)
337 if result.stdout != expected_bytes:
338 actual = struct.unpack('<'+'Q'*(len(result.stdout)//8), result.stdout) if len(result.stdout)%8 == 0 else result.stdout
339 return f'state={actual} expected={expected}'
340 if memory.read_bytes() != (c['memory'] if c['memory'] is not None else before): return 'memory differs'
341 return None
342
343def main():
344 mode = sys.argv[1]
345 if mode == 'mutate':
346 source, out = pathlib.Path(sys.argv[2]), pathlib.Path(sys.argv[3])
347 text = source.read_text()
348 for name, (old, new) in MUTATIONS.items():
349 if old not in text: raise AssertionError(f'mutation missing: {name}')
350 (out / (name+'.om')).write_text(text.replace(old, new))
351 print(' '.join(MUTATIONS))
352 return
353 exe, out = sys.argv[2], pathlib.Path(sys.argv[3])
354 if mode == 'control':
355 name = sys.argv[4]
356 c = next(c for c in CASES if c['name'] == MUTANT_CASE[name])
357 error = run_case(exe, out, c)
358 if error is None: raise AssertionError(f'control not caught: {name}')
359 print(f'CONTROL manual-{name} CAUGHT case={c["name"]} pdf-page={c["page"]} {error}')
360 return
361 for c in CASES:
362 error = run_case(exe, out, c)
363 if error: raise AssertionError(f'{c["name"]} PDF page {c["page"]}: {error}')
364 print(f'PASS manual single-instruction semantics cases={len(CASES)} all implemented instructions flags/masks/addressing hardware=NOT_RUN')
365
366if __name__ == '__main__': main()