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 / regalloc_om / mutants.py

176 lines · 10330 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3"""Lower each mutated .om with its unit's pinned portable bridge profile, then relink only that unit's generated C.
4
5Run inside the same device-free namespace as run.sh, after run.sh (it reuses oracle.o and successor.o).
6Compilation refusal is not credited as a killed semantic mutant. The unmodified executables must pass first.
7"""
8import hashlib
9import os
10from pathlib import Path
11import subprocess
12
13ROOT = Path(__file__).resolve().parents[2]
14OUT = ROOT / 'build/regalloc-om'
15ENV = dict(os.environ)
16OMC = Path(ENV.get('OMEGA_OMC', str(Path.home() / 'work/cudastack/omega-mv/seed/out/omc')))
17BRIDGE = Path(ENV.get('OMEGA_BRIDGE', str(Path.home() / 'work/cudastack/omega-bridge')))
18SEED = ENV.get('OMEGA_SEED') or str(BRIDGE / 'build/bootstrap/omc')
19CC = ENV.get('CC', 'cc')
20UNITS = ['regalloc', 'regalloc_ir', 'regalloc_live', 'cg_matmul', 'cg_matmul_params', 'cg_matmul_i32',
21 'cg_matmul_tensor', 'cg_matmul_loop', 'cg_matmul_frag', 'cg_matmul_block', 'cg_vecadd', 'cg_bind',
22 'bw_hazard', 'bw_hazard_dec', 'cg_life', 'cg_replay']
23FIXED = ['src/cudapp_om_bw_enc_sm121.c', 'src/cudapp_freest_f16.c',
24 'src/cudapp_om_core_sha.c', 'src/cudapp_freest_leftovers.c', 'src/cudapp_freest_pathcopy.c',
25 'src/cudapp_om_bw_identity.c', 'src/cudapp_freest_bw_scalar.c', 'src/cudapp_freest_fixture_verify.c', 'src/cudapp_om_bw_cpui.c',
26 'src/cudapp_om_bw_cpuf16.c', 'src/cudapp_om_bw_cpubf16.c', 'src/cudapp_om_bw_shape.c',
27 'src/cudapp_om_bw_name.c', 'src/cudapp_om_cg_pack_len.c', 'src/cudapp_om_cg_pack_h.c',
28 'src/cudapp_om_cg_em.c', 'src/cudapp_om_cg_reconv.c', 'src/cudapp_om_cg_sentin.c',
29 'src/cudapp_freest_cg_fields.c', 'src/cudapp_freest_cg_invar.c', 'src/cudapp_om_cg_budget.c']
30FLAGS = ['-std=gnu11', '-O2', '-Wall', '-Wextra', '-Werror', '-ffunction-sections', '-fdata-sections', '-Isrc']
31LINK = {
32 'liveness': ['tests/regalloc_om/liveness.c', str(OUT / 'successor.o'), 'src/omega_gpu_om_ir.c'],
33 'differential': ['tests/regalloc_om/differential.c', str(OUT / 'oracle.o'), str(OUT / 'successor.o'),
34 'src/omega_gpu_om_ir.c', 'src/omega_blackwell_matmul.c', 'src/sha256.c'],
35 'hazard': ['tests/regalloc_om/hazard_diff.c', str(OUT / 'hz_oracle.o'), 'src/omega_bw_hazard.c'],
36 'bind': ['tests/regalloc_om/bind_diff.c', str(OUT / 'bind_oracle_matmul.o'), str(OUT / 'bind_oracle_realize.o'),
37 str(OUT / 'oracle.o'), str(OUT / 'successor.o'), 'src/omega_blackwell_matmul.c',
38 'src/omega_blackwell_realize.c', 'src/omega_blackwell_encoder.c', 'src/omega_gpu_om_ir.c', 'src/sha256.c'],
39}
40# om/units.sh concatenates these parts after the mutated file into a second generated unit.
41TAILS = {'bw_hazard.om': [('bw_hazard_dec', 'bw_hazard_dec.om')]}
42CASES = [
43 ('pair-alignment', 'regalloc.om', 'step = 2\n', 'step = 1\n', 'random'),
44 ('reserved-gpr', 'regalloc.om', 'start = long 2\n', 'start = long 0\n', 'random'),
45 ('equal-endpoint-release', 'regalloc.om', 'if lt ra_get m add b 2 first\n', 'if le ra_get m add b 2 first\n', 'random'),
46 ('sort-order', 'regalloc.om', 'ret gt ra_get m ai ra_get m bi\n', 'ret lt ra_get m ai ra_get m bi\n', 'random'),
47 ('reserved-ugpr', 'regalloc.om', 'cur = long 4\n', 'cur = long 0\n', 'random'),
48 ('quad-alignment', 'regalloc.om', 'start = 4\n', 'start = 2\n', 'random'),
49 ('shortened-override-check', 'cg_life.om', 'if or lt h ulong fd or lt h ulong lu ge h n\n', 'if gt ulong 0 ulong 1\n', 'live'),
50 ('v159-boundary', 'cg_replay.om', 'if and eq get meta 7 1 gt get meta 1 160\n', 'if and eq get meta 7 1 ge get meta 1 160\n', 'live'),
51 ('override-ignored', 'cg_matmul_loop.om', 'put m ulong 7 ulong 1\n', 'put m ulong 7 ulong 0\n', 'builders'),
52 ('override-shortened', 'cg_matmul_loop.om', 'put m add ulong 352 r x\n', 'put m add ulong 352 r sub x ulong 1\n', 'builders'),
53 ('spec-m-limit', 'cg_bind.om', 'gt m 8192 ', 'gt m 8193 ', 'bind'),
54 ('spec-precision-byte', 'cg_bind.om', 'cb_le32 w 12 and p 255\n', 'cb_le32 w 12 and p 65535\n', 'bind'),
55 ('dispatch-bf16', 'cg_bind.om', 'if or eq p 1 eq p 2\n', 'if eq p 1\n', 'bind'),
56 ('vector-ceil-div', 'cg_bind.om', 'g = div add c uint 63 uint 64\n', 'g = div add c uint 62 uint 64\n', 'bind'),
57 ('pad-minimum', 'cg_bind.om', 'if lt p 32\n', 'if lt p 16\n', 'bind'),
58 ('fit-boundary', 'cg_bind.om', 'if lt b need\n', 'if le b need\n', 'bind'),
59 ('hazard-size-6', 'bw_hazard.om', 'ret long 4\n', 'ret long 8\n', 'hazard'),
60 ('hazard-size-class', 'bw_hazard.om', 'if le s 4\n', 'if lt s 4\n', 'hazard'),
61 ('hazard-op-2433', 'bw_hazard.om', 'if eq op 2433\n', 'if eq op 2434\n', 'hazard'),
62 ('arch-le', 'cg_bind.om', 'cb_le32 w 96 and a 4294967295\n', 'cb_le32 w 96 and a 255\n', 'bind'),
63]
64
65# Listed explicitly, never credited as killed. The exact source formula first
66# shifts the extension left six and masks to uint32, erasing their only differing bit.
67EQUIVALENTS = [
68 ('hazard-branch-sign', 'bw_hazard.om', 's = or s 3221225472\n', 's = or s 1073741824\n',
69 'tests/regalloc_om/hazard-branch-sign-proof.txt'),
70]
71
72
73def run(command, path, env=ENV):
74 with path.open('w') as log:
75 return subprocess.run(command, cwd=ROOT, env=env, stdout=log, stderr=subprocess.STDOUT, timeout=600).returncode
76
77# Each unit's checked .ident (written by om/units.sh) names the profile that lowered it. A mutant is lowered by
78# the same frontend, converter and flags, and links in place of the same generated file name.
79PROFILES = {
80 'bridge-seeded': ('bridge-seeded', [], 'src/toc.om', 'cudapp_om_'),
81 'bridge-seeded-runtime': ('bridge-seeded', ['--runtime-views'], 'src/toc.om', 'cudapp_om_'),
82 'bridge-freest': ('bridge-freest', [], 'src/freest.om', 'cudapp_freest_'),
83}
84
85
86def sha(path):
87 return hashlib.sha256(Path(path).read_bytes()).hexdigest()
88
89
90def ident(stem):
91 pins = dict(p.split('=', 1) for p in (ROOT / f'src/cudapp_om_{stem}.ident').read_text().split() if '=' in p)
92 if pins.get('abi') not in PROFILES:
93 raise RuntimeError(f'NOT_RUN: {stem} has unknown generated profile {pins.get("abi")}')
94 return pins
95
96
97def unit_file(stem):
98 return f'src/{PROFILES[ident(stem)["abi"]][3]}{stem}.c'
99
100
101def pinned():
102 omc = sha(OMC)
103 for stem in sorted({*UNITS, *(c[1][:-3] for c in CASES), *(t[0] for ts in TAILS.values() for t in ts)}):
104 pins = ident(stem)
105 frontend, _, converter, _ = PROFILES[pins['abi']]
106 if omc != pins['omc']:
107 raise RuntimeError('NOT_RUN: OMEGA_OMC is not the pinned compiler')
108 if sha(BRIDGE / 'tools' / frontend) != pins['frontend']:
109 raise RuntimeError(f'NOT_RUN: {frontend} is not the pinned frontend of {stem}')
110 if sha(BRIDGE / converter) != pins['converter']:
111 raise RuntimeError(f'NOT_RUN: {converter} is not the pinned converter of {stem}')
112 if sha(ROOT / unit_file(stem)) != pins['c']:
113 raise RuntimeError(f'NOT_RUN: {unit_file(stem)} is not the checked output in its .ident')
114 if frontend == 'bridge-freest' and not Path(SEED).is_file():
115 raise RuntimeError(f'NOT_RUN: {stem} uses bridge-freest; set OMEGA_SEED to the baseline compiler')
116
117
118def lower(stem, source, generated, log_mode):
119 frontend, flags, _, _ = PROFILES[ident(stem)['abi']]
120 env = dict(ENV, OMEGA_SEEDED_OMC=str(OMC))
121 with generated.open('w') as c, (generated.parent / 'compile.log').open(log_mode) as log:
122 return subprocess.run(['sh', str(BRIDGE / 'tools' / frontend), *flags, 'c', str(source)], cwd=ROOT,
123 env=env, stdout=c, stderr=log).returncode
124
125
126def main():
127 pinned()
128 summary = []
129 for name, unit, before, after, mode in CASES:
130 source = (ROOT / 'om/codegen' / unit).read_text()
131 if source.count(before) != 1:
132 raise RuntimeError(f'{name}: mutation anchor is not unique')
133 work = OUT / 'mutants' / name
134 work.mkdir(parents=True, exist_ok=True)
135 mutated = work / unit
136 mutated.write_text(source.replace(before, after))
137 stem = unit[:-3]
138 generated = work / Path(unit_file(stem)).name
139 rc = lower(stem, mutated, generated, 'w')
140 if rc:
141 raise RuntimeError(f'{name}: NOT_KILLED compiler rc={rc}, see {work}/compile.log')
142 regenerated = {stem: generated}
143 for tail_stem, tail in TAILS.get(unit, []):
144 tail_om = work / f'{tail_stem}.om'
145 tail_om.write_text(mutated.read_text() + (ROOT / 'om/codegen' / tail).read_text())
146 regenerated[tail_stem] = work / Path(unit_file(tail_stem)).name
147 rc = lower(tail_stem, tail_om, regenerated[tail_stem], 'a')
148 if rc:
149 raise RuntimeError(f'{name}: NOT_KILLED compiler rc={rc}, see {work}/compile.log')
150 target = {'live': 'liveness', 'hazard': 'hazard', 'bind': 'bind'}.get(mode, 'differential')
151 units = [str(regenerated[u]) if u in regenerated else unit_file(u) for u in UNITS]
152 binary = work / target
153 rc = run([CC, *FLAGS, *LINK[target], *units, *FIXED, '-Wl,--gc-sections', '-o', str(binary)], work / 'link.log')
154 if rc:
155 raise RuntimeError(f'{name}: NOT_KILLED link rc={rc}, see {work}/link.log')
156 command = [str(binary)]
157 if mode == 'random':
158 command.append('random-only')
159 rc = run(command, work / 'exercise.log')
160 if rc != 1:
161 raise RuntimeError(f'{name}: NOT_KILLED expected differential rc1, got {rc}; see {work}/exercise.log')
162 final = (work / 'exercise.log').read_text().splitlines()[-1]
163 summary.append(f'{name}\tcompile=0\texercise={rc}\t{final}')
164 print('KILLED', summary[-1], flush=True)
165 (OUT / 'mutants.tsv').write_text('\n'.join(summary) + '\n')
166 for name, unit, before, after, proof in EQUIVALENTS:
167 if (ROOT / 'om/codegen' / unit).read_text().count(before) != 1:
168 raise RuntimeError(f'{name}: equivalent anchor is not unique')
169 if not (ROOT / proof).is_file():
170 raise RuntimeError(f'{name}: missing proof')
171 print(f'EQUIVALENT {name} not counted as killed; proof={proof}', flush=True)
172 print(f'PASS recompiled Omega semantic mutants={len(summary)}')
173
174
175if __name__ == '__main__':
176 main()