interplane.Get Omega
Read in your language

omega-bridge

Omega-first language interoperability: checked conversion backends and native language bindings

Brought by drakestapleton

branch main Branches Tags History

omega-bridge / tests / seeded.py

153 lines · 6898 bytes Raw Blame History
1"""Additive e5b32567 frontend: native/C/Python differential and pin mutants.
2Run after the baseline tools/test.sh; this does not replace baseline coverage.
3No GPU, CUDA runtime, driver or hardware access.
4"""
5import os
6import pathlib
7import shutil
8import subprocess
9import sys
10
11root = pathlib.Path(__file__).resolve().parents[1]
12os.chdir(root)
13b = root / "build/seeded"
14b.mkdir(parents=True, exist_ok=True)
15seed = os.environ.get("OMEGA_SEEDED_OMC")
16if not seed:
17 sys.exit("NOT_RUN: set OMEGA_SEEDED_OMC to the pinned e5b32567 compiler")
18
19def run(args, data=None, env=None):
20 return subprocess.run([str(a) for a in args], input=data, capture_output=True,
21 timeout=60, env=env)
22
23def checked(args, data=None):
24 p = run(args, data)
25 assert p.returncode == 0, (args, p.returncode, p.stderr.decode())
26 return p.stdout
27
28def result(p):
29 return p.returncode, p.stdout, p.stderr
30
31source = "const N ulong 16777216\n"
32source += "fn sum6 a ulong b ulong c ulong d ulong e ulong f ulong to ulong\nret add add add a b add c d add e f\nend\n"
33source += "fn big a view uchar N to ulong\nret len a\nend\n"
34for i in range(40):
35 body = "add x 1" if i == 0 else f"chain{i-1} add x 1"
36 source += f"fn chain{i} x ulong to ulong\nret {body}\nend\n"
37source += """fn first a view ulong 4 to ulong
38ret get a 0
39end
40fn main to int
41n = sum6 1 2 3 4 5 6
42for i ulong 64
43if gt i 7
44break
45end
46n = add n i
47end
48ignored = chain0 n
49a = new ulong 4 0
50put a 0 chain39 n
51ret int first view a
52end
53"""
54fixture = b / "expanded.om"
55fixture.write_text(source)
56# A wrong compiler refuses before semantic checking or producing target output.
57wrong = dict(os.environ, OMEGA_SEEDED_OMC=os.environ.get("OMEGA_SEED", "build/bootstrap/omc"))
58p = run(["sh", "tools/bridge-seeded", "c", fixture], env=wrong)
59assert p.returncode == 65 and not p.stdout and b"fingerprint mismatch" in p.stderr
60# Baseline mode remains unable to adopt new capabilities implicitly.
61p = run(["sh", "tools/bridge", "c", fixture])
62assert p.returncode != 0 and not p.stdout
63native = b / "native"
64native.write_bytes(checked([seed], source.encode()))
65native.chmod(0o755)
66expected = result(run([native]))
67assert expected[0] not in range(101, 115), expected
68outputs = {}
69for target, ext in [("c", "c"), ("python", "py")]:
70 text = checked(["sh", "tools/bridge-seeded", target, fixture])
71 path = b / ("expanded." + ext)
72 path.write_bytes(text)
73 if target == "c":
74 exe = b / "portable"
75 checked(["gcc", "-std=gnu11", "-O2", "-Wall", "-Wextra", "-Werror", path, "-o", exe])
76 got = run([exe])
77 else:
78 got = run([sys.executable, path])
79 assert result(got) == expected, (target, expected, result(got))
80 outputs[target] = text
81# Strict-host control: losing the unused-source-local annotation must fail.
82assert b"__attribute__((unused))" in outputs["c"]
83warning_mutant = b / "warning-mutant.c"
84warning_mutant.write_bytes(outputs["c"].replace(b"__attribute__((unused)) ", b""))
85p = run(["gcc", "-std=gnu11", "-O2", "-Wall", "-Wextra", "-Werror",
86 warning_mutant, "-o", b / "warning-mutant"])
87assert p.returncode != 0 and b"unused variable" in p.stderr
88# Control: the same differential rejects an actual arithmetic emission defect.
89assert b"(a+b)&mask" in outputs["c"]
90mutant = b / "mutant.c"
91mutant.write_bytes(outputs["c"].replace(b"(a+b)&mask", b"(a-b)&mask"))
92checked(["gcc", "-std=gnu11", "-O2", mutant, "-o", b / "mutant"])
93assert result(run([b / "mutant"])) != expected
94# Loop guards: generated C and Python must trap 4 (exit 104) exactly where omc does.
95loops = {
96 "while": "fn main to int\nn = ulong 0\nwhile lt n {k}\nn = add n 1\nend\nret 7\nend\n",
97 "for": "fn count k ulong to ulong\ns = ulong 0\nfor i ulong k\ns = add s 1\nend\nret s\n"
98 "end\nfn main to int\nr = count {k}\nret int and r 255\nend\n",
99 "forcontinue": "fn count k ulong to ulong\ns = ulong 0\nfor i ulong k\ns = add s 1\n"
100 "if gt s 0\ncontinue\nend\nend\nret s\nend\nfn main to int\n"
101 "r = count {k}\nret int and r 255\nend\n",
102}
103for name, shape in loops.items():
104 for k, want in [(999999, False), (1000000, False), (1000001, True), (1500000, True)]:
105 src = b / f"loop-{name}-{k}.om"
106 src.write_text(shape.replace("{k}", str(k)))
107 exe = b / f"loop-{name}-{k}.native"
108 exe.write_bytes(checked([seed], src.read_bytes()))
109 exe.chmod(0o755)
110 native_loop = result(run([exe]))
111 assert (native_loop[0] == 104) == want, (name, k, native_loop)
112 c = b / f"loop-{name}-{k}.c"
113 c.write_bytes(checked(["sh", "tools/bridge-seeded", "c", src]))
114 checked(["gcc", "-std=gnu11", "-O2", "-Wall", "-Wextra", "-Werror", c, "-o", b / "loop-c"])
115 assert result(run([b / "loop-c"])) == native_loop, (name, k, "c", native_loop)
116 if name == "forcontinue": # seeded `continue` is C-only; Python refuses it
117 p = run(["sh", "tools/bridge-seeded", "python", src])
118 assert p.returncode != 0 and not p.stdout, (name, k, "python", result(p))
119 continue
120 py = b / f"loop-{name}-{k}.py"
121 py.write_bytes(checked(["sh", "tools/bridge-seeded", "python", src]))
122 assert result(run([sys.executable, py])) == native_loop, (name, k, "python", native_loop)
123# Explicit native x86 instruction-set execution, not AArch64 emulation.
124if not shutil.which("x86_64-linux-gnu-gcc") or not shutil.which("qemu-x86_64"):
125 sys.exit("NOT_RUN: install x86_64-linux-gnu-gcc and qemu-x86_64 for x86 differential")
126checked(["x86_64-linux-gnu-gcc", "-static", "-std=gnu11", "-O2", "-Wall", "-Wextra", "-Werror",
127 b / "expanded.c", "-o", b / "portable-x86"])
128assert result(run(["qemu-x86_64", b / "portable-x86"])) == expected
129unit = b / "unit.om"
130unit.write_text("""const L ulong 4
131fn seedunit a view ulong L o edit ulong L to ulong
132put o 0 get a 0
133ret get o 0
134end
135""")
136(b / "unit.c").write_bytes(checked(["sh", "tools/bridge-seeded", "c", unit]))
137header = checked(["sh", "tools/bridge-seeded", "h", unit])
138assert b"omega_seedunit" in header and b"const void *" in header and b"{" not in header
139(b / "unit.h").write_bytes(b"#include <stdint.h>\n" + header)
140(b / "caller.c").write_text('''#include "unit.h"
141int main(void) {
142 const uint64_t a[4] = {42};
143 uint64_t out[4] = {0}, result = 0;
144 if (omega_seedunit(0,a,4,out,4) != 32) return 1;
145 if (omega_seedunit(&result,a,3,out,4) != 32) return 2;
146 if (omega_seedunit(&result,out,4,out,4) != 32) return 3;
147 if (omega_seedunit(&result,a,4,out,4) != 0 || result != 42 || out[0] != 42) return 4;
148 return 0;
149}
150''')
151checked(["gcc", "-std=gnu11", "-O2", "-Wall", "-Wextra", "-Werror", b / "caller.c", b / "unit.c", "-o", b / "caller"])
152assert result(run([b / "caller"])) == (0, b"", b"")
153print("SEEDED CONVERTER differential C/Python/native/x86 and pin/arithmetic/ABI controls PASS; device NOT_RUN")