Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/tests/amd-exec/semantics.py # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers 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. 4 Python is a test instrument only. No executor source is read to derive an expectation. 5 Integer arithmetic below is the recorded calculation of the manual's pseudocode. 6 """ 7 import pathlib 8 import struct 9 import subprocess 10 import sys 11 12 U32 = (1 << 32) - 1 13 U64 = (1 << 64) - 1 14 CASES = [] 15 16 def s(r): return (0, r) 17 def v(r, lane=0): return (1, r * 64 + lane) 18 def t(r): return (2, r) 19 def sop(op, a=4, b=5, d=8): return [0x80000000 | op << 23 | d << 16 | b << 8 | a] 20 def sop1(op, a=4, d=8): return [0xbe800000 | d << 16 | op << 8 | a] 21 def vop(op, a=257, b=2, d=3): return [op << 25 | d << 17 | b << 9 | a] 22 def sm(op, d=8, offset=0): return [0xc0020000 | op << 18 | d << 6, offset] 23 def 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 27 def 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). 37 for 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) 44 for 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. 49 for 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)) 51 for 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)) 56 for 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)) 66 for 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)) 70 for 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}) 75 case('fsub-denorm', 169, vop(2), {v(1): 3, v(2): 2}, {v(3): 1}) 76 for 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}) 84 for 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) 91 for 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) 99 for 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}) 104 case('mov-preserves-scc', 115, sop1(0), {s(4): U32}, {s(8): U32}) 105 case('mov-literal', 115, sop1(0, a=255)+[0x81234567], result={s(8): 0x81234567}) 106 for 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)) 109 case('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. 112 for 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) 117 case('branch-negative', 139, [0xbf84ffff], cc=0, pc=0) 118 for ex in [0, 1]: 119 case(f'branch-execz-{ex}', 140, [0xbf880001, 0xbf810000, 0xbf810000], ex=ex, pc=4 if ex else 8) 120 for wait in [0xc07f, 0x0f70]: 121 case(f'wait-{wait}', 141, [0xbf8c0000 | wait]) 122 case('endpgm', 138, [0xbf810000], running=0) 123 for 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. 126 mem = struct.pack('<16I', *range(0x12340000, 0x12340010)) 127 for op in [0, 1, 2]: 128 case(f'smem-x{1<> (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}) 200 case('vmul-vector-source', 361, [0xd2850003, 257 | 258 << 9], 201 {v(1): U32, v(2): 2}, {v(3): 0xfffffffe}) 202 case('vmul-modifier-refused', 361, [0xd2850103, 257 | 4 << 9], refusal=66) 203 case('vmul-src2-refused', 361, [0xd2850003, 257 | 4 << 9 | 1 << 18], refusal=66) 204 case('global-ubyte-last-byte', 503, [0xdc408000, 3 << 24 | 127 << 16 | 4], 205 {v(4): 0, v(5): 0}, {v(3): 255}, mem=b'\xff') 206 case('global-ubyte-negative-offset', 503, [0xdc409fff, 3 << 24 | 127 << 16 | 4], 207 {v(4): 1, v(5): 0}, {v(3): 128}, mem=b'\x80') 208 case('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`. 214 bf16 = runpy.run_path(str(pathlib.Path(__file__).with_name('bf16gemv.py'))) 215 half = [0xdc008000 | 37 << 18, 3 << 24 | 127 << 16 | 4] 216 for 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('> 32}, nex=mask, cc=1) 234 case('mov64-odd-pair-refused', 115, sop1(1, a=5, d=126), refusal=66) 235 for 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}) 237 for 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) 249 def 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. 252 seed = {v(1, lane): (lane ^ 1)*4 for lane in range(64)} 253 seed.update({v(2, lane): 1000+lane for lane in range(64)}) 254 case('bpermute-alias-snapshot', 437, ds(), seed, 255 {v(2, lane): 1000+(lane ^ 1) for lane in range(64)}, ex=U64) 256 case('bpermute-inactive-source', 437, ds(dest=3), 257 {v(1): 4, v(2, 1): 1234, v(3, 1): 0xfeed}, {v(3): 0}, ex=1) 258 case('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) 260 case('bpermute-inactive-destination', 437, ds(dest=3), 261 {v(1): 0, v(2): 9, v(3): 0xfeed}, ex=0) 262 for modifier in (1 << 16, 1 << 25): 263 words = ds() 264 words[0] |= modifier 265 case(f'bpermute-unsupported-modifier-{modifier}', 437, words, refusal=66) 266 words = ds() 267 words[1] |= 1 << 16 268 case('bpermute-unused-data1', 437, words, refusal=66) 269 270 MUTATIONS = { 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 } 302 MUTANT_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 321 def 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 343 def 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 366 if __name__ == '__main__': main()