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 / no_posix_native.py

133 lines · 6642 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3"""Run only inside the mandatory device-free bwrap boundary."""
4import hashlib
5import os
6from pathlib import Path
7import subprocess
8
9ROOT = Path(__file__).resolve().parents[1]
10OUT = ROOT / 'build/no_posix/check'
11OUT.mkdir(parents=True, exist_ok=True)
12BRIDGE = Path(os.environ['OMEGA_BRIDGE']).resolve()
13ENV = dict(os.environ, OMEGA_NATIVE_OMC2=os.environ['OMEGA_OMC'])
14subprocess.run(['sh', ROOT / 'om/no-posix.sh', '--tools', OUT], check=True)
15ENV.update(OMEGA_NATIVE_OMC=str(OUT / 'tools/omc-gen'),
16 OMEGA_NATIVE_X86_OMC=str(OUT / 'tools/omc-x86'))
17LOG = OUT / 'commands.log'
18LOG.write_text('')
19
20def run(args, expected=0):
21 p = subprocess.run([str(x) for x in args], env=ENV, cwd=OUT, capture_output=True)
22 with LOG.open('ab') as f:
23 f.write((' '.join(map(str, args)) + '\nrc=' + str(p.returncode) + '\n').encode())
24 for raw in (p.stdout, p.stderr):
25 if '--dump' in args:
26 f.write(('dump sha256=' + hashlib.sha256(raw).hexdigest() + '\n').encode())
27 continue
28 printable = all(32 <= b <= 126 or b in (9, 10, 13) for b in raw)
29 f.write((raw if printable else b'HEX:' + raw.hex().encode()) + b'\n')
30 assert p.returncode == expected, (args, p.returncode, p.stdout, p.stderr)
31 return p.stdout
32
33sources = sorted((ROOT / 'build/no_posix/sources').glob('*.om'))
34assert {p.stem for p in sources} == {
35 'bw_enc_sm121', 'cubin_elf', 'no_bf16', 'admit', 'words', 'park', 'book', 'streams', 'no_avbf',
36 'regalloc', 'regalloc_ir', 'regalloc_live', 'cg_matmul', 'cg_matmul_params',
37 'cg_matmul_i32', 'cg_matmul_tensor', 'cg_matmul_loop', 'cg_matmul_frag', 'cg_matmul_block',
38 'cg_vecadd', 'cg_bind', 'cg_pack_len', 'cg_pack_h', 'cg_sentin', 'bw_identity',
39 'bw_qmd', 'amp_qmd',
40}
41native = ROOT / 'build/no_posix/native'
42host = OUT / 'host'
43run([BRIDGE / 'tools/native', host, *sources])
44flags = ['gcc', '-std=gnu11', '-O2', '-Wall', '-Wextra', '-Werror', '-ffreestanding', '-fno-builtin', '-fno-stack-protector', '-ffp-contract=off']
45caller = ROOT / 'tests/no_posix_native.c'
46
47def bare(lib, path, red=False):
48 run([*flags, '-DNO_POSIX_BARE', *(['-DNO_POSIX_RED'] if red else []), '-I', lib,
49 caller, lib / 'omega_native.o', lib / 'omega_api_defs.o', '-nostdlib', '-static', '-no-pie', '-Wl,-e,_start', '-o', path])
50
51binary = OUT / 'bare'
52bare(native, binary)
53assert not run(['nm', '-u', binary]).strip()
54assert b'INTERP' not in run(['readelf', '-lW', binary])
55assert b'.tdata' not in run(['readelf', '-SW', binary])
56assert b'.tbss' not in run(['readelf', '-SW', binary])
57data = run([binary])
58(OUT / 'bare.bin').write_bytes(data)
59hosted = OUT / 'hosted'
60run([*flags, '-I', host, caller, '-L', host, '-Wl,-rpath,' + str(host), '-lomega_native', '-o', hosted])
61reference = run([hosted])
62(OUT / 'hosted.bin').write_bytes(reference)
63assert data == reference
64wrong = OUT / 'red'
65bare(native, wrong, True)
66assert not run([wrong], expected=90)
67print('PASS bare ELF: no imports/interpreter/TLS; hosted bytes identical; red exits 90')
68mutants = [
69 ('nop-word', 'bw_enc_sm121.om', 'const NOPW ulong 31000', 'const NOPW ulong 31001', 2),
70 ('nop-control', 'bw_enc_sm121.om', 'const NOPC ulong 1032192', 'const NOPC ulong 1032193', 2),
71 ('pad-floor', 'bw_enc_sm121.om', 'cnt = 32', 'cnt = 31', 1),
72 ('byte-size', 'bw_enc_sm121.om', 'ret mul cnt 16', 'ret mul cnt 15', 1),
73 ('nan-quiet', 'no_bf16.om', 'ret ulong or shr u 16 64', 'ret ulong shr u 16', 7),
74 ('rne-tie', 'no_bf16.om', 'and shr u 16 1', 'and shr u 16 0', 7),
75]
76for name, unit, before, after, rc in mutants:
77 work = OUT / name
78 work.mkdir(exist_ok=True)
79 copies = []
80 for source in sources:
81 content = source.read_text()
82 if source.name == unit:
83 assert content.count(before) == 1
84 content = content.replace(before, after)
85 dest = work / source.name
86 dest.write_text(content)
87 copies.append(dest)
88 lib = work / 'native'
89 run([BRIDGE / 'tools/native', '--freestanding', lib, *copies])
90 executable = work / 'bare'
91 bare(lib, executable)
92 run([executable], expected=rc)
93 print('PASS killed source mutant', name, 'rc', rc)
94print('PASS NO_POSIX native differential, red control and six killed mutants')
95
96# Exercise the existing complete C-vs-Omega packer sweep through the NO_POSIX
97# production shell, including exact refusal messages and complete image bytes.
98for mode, extra, expected in (
99 ('cubin', [], 0),
100 ('cubin-red', ['-DCUDAPP_CUBIN_DIFF_SELFTEST'], -6),
101):
102 executable = OUT / mode
103 run([*flags, '-O0', '--coverage', '-DOMEGA_GPU_NO_POSIX', '-DCUDAPP_MIGRATION_PENDING_CUBIN_DIFF',
104 *extra, '-I', ROOT / 'src', '-I', native,
105 ROOT / 'tests/no_posix_cubin.c', ROOT / 'src/omega_cubin.c',
106 ROOT / 'src/cudapp_freest_core_cubinmsg.c', ROOT / 'src/cudapp_freest_core_cubadmit.c',
107 native / 'omega_native.o', native / 'omega_api_defs.o', '-o', executable])
108 run([executable], expected=expected)
109 if mode == 'cubin':
110 run(['gcov', '-b', '-f', '-o', OUT / 'cubin-omega_cubin.gcno',
111 ROOT / 'src/omega_cubin.c'])
112print('PASS NO_POSIX cubin production shell: C differential and injected-byte red control')
113
114oracle = OUT / 'codegen-oracle'
115run([*flags, '-O0', '--coverage', '-DOMEGA_GPU_NO_POSIX',
116 '-DCUDAPP_MIGRATION_PENDING_C_REGALLOC', '-DCUDAPP_MIGRATION_PENDING_C_CODEGEN_MATMUL',
117 '-DCUDAPP_MIGRATION_PENDING_C_BIND', '-DCUDAPP_MIGRATION_PENDING_C_PACK',
118 '-DCUDAPP_MIGRATION_PENDING_C_NO_POSIX', '-I', ROOT / 'src', '-I', native,
119 ROOT / 'tests/test_omega_codegen_freestanding.c',
120 ROOT / 'src/omega_blackwell_codegen.c', ROOT / 'src/omega_blackwell_matmul.c',
121 ROOT / 'src/sha256.c', ROOT / 'src/cudapp_freest_f16.c',
122 ROOT / 'src/cudapp_freest_bw_scalar.c', ROOT / 'src/cudapp_freest_cg_fields.c',
123 ROOT / 'src/cudapp_freest_fixture_verify.c',
124 native / 'omega_native.o', native / 'omega_api_defs.o', '-o', oracle])
125# NO_POSIX's --no-sweep runs buffer/golden fixtures but not a second exhaustive
126# bf16 sweep. The normal test-codegen-freestanding target retains that sweep.
127run([oracle, '--no-sweep'])
128run([ROOT / 'build/test_codegen_freestanding', '--no-sweep'])
129observed = run([oracle, '--dump'])
130assert observed == run([ROOT / 'build/test_codegen_freestanding', '--dump'])
131run(['gcov', '-b', '-f', '-o', OUT / 'codegen-oracle-omega_blackwell_codegen.gcno',
132 ROOT / 'src/omega_blackwell_codegen.c'])
133print('PASS C_NO_POSIX encoder oracle dump equals native dump; gcov recorded')