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
CUDApp / om / codegen / gdn_mutants.py
| 1 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | """Host verification oracle: mutate the seeded Omega GDN sources, never the checked C output. |
| 3 | |
| 4 | Each mutant is one textual swap at a site that occurs exactly once across the unit sources. Every unit that contains the |
| 5 | mutated file is regenerated with tools/bridge-seeded and linked into codegen_gdn_diff_test, which must then fail with a |
| 6 | GDN DIFF (IR/regalloc/bytes or refusal) or GDN SHAPE DIFF against the retained C oracle.""" |
| 7 | import os |
| 8 | from pathlib import Path |
| 9 | import subprocess |
| 10 | import tempfile |
| 11 | |
| 12 | ROOT = Path(__file__).resolve().parents[2] |
| 13 | OMC = os.environ["OMEGA_OMC"] |
| 14 | BRIDGE = Path(os.environ["OMEGA_BRIDGE"]) |
| 15 | UNITS = ( |
| 16 | ("CG_GDN_SRC", ("cg_gdn_shape.om", "cg_gdn_common.om", "cg_gdn.om")), |
| 17 | ("CG_GDN_POLICY_SRC", ("cg_gdn_shape.om", "cg_gdn_policy.om")), |
| 18 | ) |
| 19 | MUTANTS = ( |
| 20 | # name, source file, before, after |
| 21 | ("shape-value-heads", "cg_gdn_shape.om", "vh = mul 16 r", "vh = mul 17 r"), |
| 22 | ("control-word-wait", "cg_gdn_common.om", "shl wt 20", "shl wt 21"), |
| 23 | ("branch-patch-offset", "cg_gdn_common.om", "and sub tg at 4294967295", "and sub add tg 1 at 4294967295"), |
| 24 | ("butterfly-step", "cg_gdn_common.om", "o = mul o 2", "o = mul o 4"), |
| 25 | ("mutant-selector-bound", "cg_gdn.om", "if or ne c 0 gt a 4", "if or ne c 0 gt a 5"), |
| 26 | ("one-minus-qs", "cg_gdn.om", "get s 33 1063870306", "get s 33 1063870307"), |
| 27 | ("prep-prefetch-offset", "cg_gdn.om", "get s 187 32768 0", "get s 187 32769 0"), |
| 28 | ) |
| 29 | |
| 30 | |
| 31 | def run(argv, **kwargs): |
| 32 | return subprocess.run(argv, cwd=ROOT, text=True, capture_output=True, **kwargs) |
| 33 | |
| 34 | |
| 35 | def main(): |
| 36 | baseline = ROOT / "build/codegen_gdn_diff_test" |
| 37 | r = run([str(baseline)]) |
| 38 | if r.returncode or "CODEGEN GDN DIFF PASS" not in r.stdout: |
| 39 | raise RuntimeError(f"baseline: rc={r.returncode}\n{r.stdout}{r.stderr}") |
| 40 | src = ROOT / "om/codegen" |
| 41 | killed = 0 |
| 42 | for name, source, before, after in MUTANTS: |
| 43 | sites = sum((src / part).read_text().count(before) for part in {p for _, parts in UNITS for p in parts}) |
| 44 | if sites != 1 or (src / source).read_text().count(before) != 1: |
| 45 | raise RuntimeError(f"{name}: mutation site found {sites} times") |
| 46 | with tempfile.TemporaryDirectory(prefix="omega-gdn-mutant-") as tmp: |
| 47 | tmp = Path(tmp) |
| 48 | overrides = [] |
| 49 | for variable, parts in UNITS: |
| 50 | if source not in parts: |
| 51 | continue |
| 52 | data = "\n".join((src / part).read_text() for part in parts) |
| 53 | om = tmp / f"{variable}.om" |
| 54 | om.write_text(data.replace(before, after)) |
| 55 | env = dict(os.environ, OMEGA_SEEDED_OMC=OMC) |
| 56 | r = run(["sh", str(BRIDGE / "tools/bridge-seeded"), "c", str(om)], env=env) |
| 57 | if r.returncode: |
| 58 | raise RuntimeError(f"{name}: mutant must compile: rc={r.returncode}\n{r.stderr}") |
| 59 | c = tmp / f"{variable}.c" |
| 60 | c.write_text(r.stdout) |
| 61 | overrides.append(f"{variable}={c}") |
| 62 | out = tmp / "build" |
| 63 | r = run(["make", "-j3", f"OUT_DIR={out}", *overrides, |
| 64 | "CFLAGS=-std=gnu11 -Wall -Wextra -Werror -O0 -Isrc", str(out / "codegen_gdn_diff_test")]) |
| 65 | if r.returncode: |
| 66 | raise RuntimeError(f"{name}: mutant C must build: rc={r.returncode}\n{r.stdout}{r.stderr}") |
| 67 | r = run([str(out / "codegen_gdn_diff_test")]) |
| 68 | if r.returncode != 1 or "GDN DIFF" not in r.stderr and "GDN SHAPE DIFF" not in r.stderr: |
| 69 | raise RuntimeError(f"{name}: mutant survived or crashed: rc={r.returncode}\n{r.stdout}{r.stderr}") |
| 70 | print(f"KILLED Omega {name} rc=1: {r.stderr.splitlines()[0]}", flush=True) |
| 71 | killed += 1 |
| 72 | print(f"CODEGEN GDN OMEGA MUTANTS PASS ({killed} source mutants killed)") |
| 73 | |
| 74 | |
| 75 | if __name__ == "__main__": |
| 76 | main() |