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 / native / v2 / test_single_edit.py

65 lines · 3080 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3"""CPU-only qualification of all three zero-initialized native span templates."""
4import os
5from pathlib import Path
6import subprocess
7import sys
8
9ROOT = Path(__file__).resolve().parents[3]
10FIX = Path(__file__).resolve().parent
11OUT = Path(sys.argv[1] if len(sys.argv) > 1 else ROOT / 'build/native-single-edit').resolve()
12OUT.mkdir(parents=True, exist_ok=True)
13CC = os.environ.get('CC', 'cc')
14OLD = 'ombr_span sp[7];'
15NEW = 'ombr_span sp[7] = {{0}};'
16
17def run(args, expected=0):
18 p = subprocess.run([str(x) for x in args], cwd=ROOT, capture_output=True)
19 if p.returncode != expected:
20 sys.stderr.buffer.write(p.stdout + p.stderr)
21 raise RuntimeError(f'{args}: rc={p.returncode}, expected={expected}')
22 return p
23
24source = (FIX / 'oneedit.om').read_text()
25assert source.count('om2\n') == 1 and source.count('export fn ') == 1
26v1 = OUT / 'oneedit.om'
27# OM1 has fixed-size views, not the OM2 dynamic '*' view.
28v1.write_text(source.replace('om2\n', '', 1).replace('export fn ', 'fn ', 1)
29 .replace('edit uchar *', 'edit uchar 3', 1)
30 .replace('ret long len b', 'ret long 3', 1))
31profiles = [
32 ('v1', v1, False, 1, False),
33 ('v2', FIX / 'oneedit.om', False, 1, False),
34 ('v2-device', FIX / 'dev/onehost.om', True, 4, False),
35 ('v2-context', FIX / 'dev/onehost.om', True, 4, True),
36]
37for name, unit, host, count, context in profiles:
38 directory = OUT / name
39 run(['tools/native', *(['--host'] if host else []), directory, unit])
40 text = (directory / 'omega_native.c').read_text()
41 assert text.count(NEW) == count and OLD not in text, name + ' declaration count'
42 caller = directory / 'singleedit'
43 flags = ['-DSINGLE_OM1'] if name == 'v1' else []
44 if context:
45 flags.append('-DSINGLE_DEVICE')
46 run([CC, '-std=gnu11', '-O2', '-Wall', '-Wextra', '-Werror', '-pthread', *flags,
47 '-I' + str(directory), FIX / 'singleedit.c', '-L' + str(directory),
48 '-Wl,-rpath,' + str(directory), '-lomega_native', '-o', caller])
49 print(run([caller]).stdout.decode(), end='')
50 print(f'PASS {name}: exact zero-init declarations={count}')
51
52# Revert only the pure-v2 declaration. GCC13.3 must reproduce this reported
53# false-positive warning, not an arbitrary compiler/link/launch failure.
54directory = OUT / 'v2'
55text = (directory / 'omega_native.c').read_text()
56assert text.count(NEW) == 1
57mutant = directory / 'uninitialized.c'
58mutant.write_text(text.replace(NEW, OLD))
59p = run([CC, '-std=gnu11', '-O2', '-Wall', '-Wextra', '-Werror',
60 '-Wno-unused-variable', '-fPIC', '-shared', '-pthread', mutant,
61 '-o', directory / 'uninitialized.so'], expected=1)
62assert b'maybe-uninitialized' in p.stderr and b'sp[1].lo' in p.stderr, p.stderr
63(directory / 'uninitialized.stderr').write_bytes(p.stderr)
64print('PASS single-edit warning mutant: GCC refuses sp[1].lo with maybe-uninitialized, rc=1')
65print('SINGLE-EDIT PASS (v1, v2, device-default, device-context; four declaration counts; GCC warning mutant)')