interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / equiv / gate-irequiv.sh

195 lines · 9119 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0
3# Gate for seed/equiv/irequiv.om (issue 826): exhaustive u8/u16 equivalence of
4# OSC IR v1 scalar functions. Host CPU only; no C participates in checking.
5set -eu
6cd "$(dirname "$0")/../.."
7[ "$(uname -m)" = aarch64 ] || { echo "SKIP gate-irequiv: needs aarch64 (omc emits AArch64)"; exit 0; }
8OMC=seed/out/omc
9PIN=26540e6de62402d5784b30b24d7c6faae488137c1f8a4b3e784fd0b5d5528991
10[ "$(sha256sum "$OMC" | cut -d' ' -f1)" = "$PIN" ] || { echo "FAIL pin: $OMC"; exit 1; }
11B=build/seed/equiv
12mkdir -p "$B"
13fails=0
14bad() { echo "FAIL $*"; fails=$((fails + 1)); }
15
16# irequiv.om lines 43-496 are the OSC IR v1 reader and validator, byte-identical to irspirv.om.
17# irwasm.om forked it for IR v7 (#818): its lines 65-843 must equal that v1 reader with the
18# recorded v7 edits of seed/equiv/irwasm-v7.sed applied, byte for byte. A change on either side
19# that is not in the recorded edits is drift.
20sed -n 43,496p seed/equiv/irequiv.om > "$B/r1"
21sed -n 43,496p seed/spirv/irspirv.om > "$B/r2"
22cmp -s "$B/r1" "$B/r2" || bad "reader drift vs seed/spirv/irspirv.om"
23sed -n 65,843p seed/wasm/irwasm.om > "$B/r2"
24sed -f seed/equiv/irwasm-v7.sed "$B/r1" > "$B/r1v7" || bad "apply seed/equiv/irwasm-v7.sed"
25cmp -s "$B/r1v7" "$B/r2" || bad "reader drift vs seed/wasm/irwasm.om (v1 reader + seed/equiv/irwasm-v7.sed)"
26
27"$OMC" < seed/equiv/irequiv.om > "$B/irequiv" 2> "$B/irequiv.err" || { cat "$B/irequiv.err"; echo "FAIL compile irequiv"; exit 1; }
28[ -s "$B/irequiv.err" ] && bad "compile stderr not empty"
29chmod +x "$B/irequiv"
30for u in hexv clamp rot bitrev pop16 wide; do
31 { printf 'ir\n'; cat "seed/equiv/$u.unit.om"; } | "$OMC" > "$B/$u.ir" || bad "ir $u"
32done
33
34# expect UNIT STATUS "line substring" ARGS...
35expect() {
36 u=$1; want=$2; sub=$3; shift 3
37 set +e
38 out=$("$B/irequiv" "$@" < "$B/$u.ir" 2>&1); st=$?
39 set -e
40 echo "$out"
41 [ "$st" = "$want" ] || bad "$u $*: status $st, want $want"
42 case "$out" in *"$sub"*) ;; *) bad "$u $*: missing '$sub'";; esac
43}
44
45# Equivalent pairs: PASS over the full domain, exact counts.
46expect hexv 0 "EQUIV PASS a=js_hexv b=hhexv params=uchar domain=256 checked=256 differing=0" check js_hexv hhexv
47expect hexv 0 "EQUIV PASS a=js_hexv b=hexfold params=uchar domain=256 checked=256 differing=0" check js_hexv hexfold
48expect clamp 0 "domain=65536 checked=65536 differing=0 both_trap=0" check clamp_add clamp_wrap
49expect bitrev 0 "domain=256 checked=256 differing=0" check bitrev8 bitrev8s
50expect rot 0 "domain=65536 checked=65536 differing=0 both_trap=63488" check r8 r8w
51expect pop16 0 "params=ushort domain=65536 checked=65536 differing=0" check pop16 pop16s
52# Negative controls: single-input mutants report that exact input and both results.
53expect hexv 1 "differing=1 first=uchar:70 a_result=value:ulong:15 b_result=value:ulong:16" check js_hexv hexoff1
54expect pop16 1 "differing=1 first=ushort:0 a_result=value:ushort:0 b_result=value:ushort:1" check pop16 pop16z
55expect pop16 1 "differing=1 first=ushort:65535 a_result=value:ushort:16 b_result=value:ushort:15" check pop16 pop16e
56# Trap codes are part of equality: value-for-trap is a difference.
57expect rot 1 "differing=63488 first=uchar:0,uchar:8 a_result=trap:7 b_result=value:uchar:0" check r8 r8m
58expect rot 0 "trap:7" eval r8 uchar:1 uchar:9
59expect rot 0 "value:128" eval r8 uchar:1 uchar:1
60# Wider domains: refused for check, sampled (exit 10, NOT_A_PROOF) otherwise.
61expect wide 5 "irequiv: DOMAIN" check pop32 pop32s
62expect wide 10 "EQUIV SAMPLED" sample pop32 pop32s 100000 7
63expect wide 10 "NOT_A_PROOF" sample pop32 pop32s 100000 7
64expect wide 1 "EQUIV FAIL" sample pop32 pop32t 100000 7
65# Fuel exhaustion is INCONCLUSIVE, never PASS or FAIL.
66expect wide 6 "EQUIV INCONCLUSIVE" check spin spin
67# Refusals.
68expect hexv 3 "UNKNOWN_FUNCTION" check js_hexv nope
69expect wide 4 "SIGNATURE" check pop32 spin
70expect rot 2 "USAGE" eval r8 uchar:1 ushort:1
71expect rot 2 "USAGE" eval r8 uchar:256 uchar:1
72expect rot 2 "USAGE" bogus r8
73expect rot 7 "DUMP_TRAP" dump r8
74head -c 100 "$B/pop16.ir" > "$B/trunc.ir"
75set +e; "$B/irequiv" check pop16 pop16s < "$B/trunc.ir" > /dev/null 2>&1; st=$?; set -e
76[ "$st" = 22 ] || bad "truncated IR: status $st, want 22"
77# Full-domain dump: bitrev8 over 256 inputs is 2048 bytes and reverses 1 to 128.
78"$B/irequiv" dump bitrev8 < "$B/bitrev.ir" > "$B/bitrev.dump"
79[ "$(wc -c < "$B/bitrev.dump")" = 2048 ] || bad "dump size"
80[ "$(od -An -tu8 -j8 -N8 "$B/bitrev.dump" | tr -d ' ')" = 128 ] || bad "dump bitrev8(1)"
81
82# Conformance with native code: every row of the irwasm/irspirv tables is run as a
83# real native program (the dumper of seed/wasm/gate-irwasm.sh, all 64 result bits)
84# and through `irequiv eval` on the same unit's IR. Values must be bit-identical and
85# traps must carry the same code; the native exit must also match the table.
86for source in tests/one_layer/omegese/dev/*.omegese seed/wasm/*.unit.om seed/spirv/*.unit.om; do
87 id=$(basename "$source" .omegese)
88 id=${id%.unit.om}
89 printf '%s\n' "$source" > "$B/nx-$id.source"
90 { printf 'ir\n'; cat "$source"; } | "$OMC" > "$B/nx-$id.ir" || bad "ir $source"
91done
92
93# Reader verdicts: irwasm's v7 reader must accept every v1 unit irequiv accepts and refuse the
94# malformed ones with the same status; a version-7 header stays irwasm's alone (irequiv: VERSION).
95"$OMC" < seed/wasm/irwasm.om > "$B/irwasm" || { echo "FAIL compile irwasm"; exit 1; }
96chmod +x "$B/irwasm"
97{ printf X; tail -c +2 "$B/pop16.ir"; } > "$B/bad-magic.ir"
98{ head -c 7 "$B/pop16.ir"; printf '\002'; tail -c +9 "$B/pop16.ir"; } > "$B/bad-version.ir"
99{ head -c 7 "$B/pop16.ir"; printf '\007'; tail -c +9 "$B/pop16.ir"; } > "$B/v7-header.ir"
100{ cat "$B/pop16.ir"; printf Z; } > "$B/bad-trail.ir"
101rd_units=0
102rd_agree=0
103verdict() {
104 set +e
105 "$B/irwasm" < "$1" > /dev/null 2>&1; w=$?
106 "$B/irequiv" eval ir_reader_probe < "$1" > /dev/null 2>&1; e=$?
107 set -e
108 [ "$e" = 3 ] && e=0
109 rd_units=$((rd_units + 1))
110 if [ "$w" = "$2" ] && [ "$e" = "$2" ]; then rd_agree=$((rd_agree + 1)); else bad "reader verdict $1: irwasm $w, irequiv $e, want $2"; fi
111}
112for u in "$B"/nx-*.ir; do verdict "$u" 0; done
113for u in hexv clamp rot bitrev pop16 wide; do verdict "$B/$u.ir" 0; done
114verdict "$B/bad-magic.ir" 20
115verdict "$B/bad-version.ir" 21
116verdict "$B/trunc.ir" 22
117verdict "$B/bad-trail.ir" 27
118set +e; "$B/irequiv" eval ir_reader_probe < "$B/v7-header.ir" > /dev/null 2>&1; st=$?; set -e
119[ "$st" = 21 ] || bad "v7 header: irequiv status $st, want 21 (VERSION)"
120echo "READERS units=$rd_units agree=$rd_agree"
121tab=$(printf '\t')
122nx_rows=0
123nx_equal=0
124conform() {
125 table=$1; want=$2; rows=0
126 while IFS="$tab" read -r unit function result typed status; do
127 case "$unit" in ''|'#'*) continue ;; esac
128 rows=$((rows + 1)); nx_rows=$((nx_rows + 1))
129 om=$B/nx-row-$nx_rows.om
130 cat "$(cat "$B/nx-$unit.source")" > "$om"
131 cat >> "$om" <<'DUMP'
132global dumpbuf uchar 8
133fn ir_dump x ulong to void
134for j ulong 8
135put dumpbuf j uchar and shr x mul j 8 255
136end
137p = ulong 0
138while lt p 8 bound 8
139k = write 1 dumpbuf p sub ulong 8 p
140if le k 0
141exit 90
142end
143p = add p ulong k
144end
145end
146fn main to int
147DUMP
148 actual=; eargs=
149 if [ "$typed" != - ]; then
150 for a in $(printf '%s' "$typed" | tr ',' ' '); do
151 ty=${a%%:*}; v=${a#*:}
152 if [ "$ty" = bool ]; then actual="$actual $v"; else actual="$actual $ty $v"; fi
153 eargs="$eargs $a"
154 done
155 fi
156 case "$result" in
157 bool|void|byte|short|int|long|uchar|ushort|uint|ulong) ;;
158 *) bad "$table row $rows: unknown result $result"; continue ;;
159 esac
160 case "$result" in
161 bool) printf 'if %s %s\nir_dump ulong 1\nelse\nir_dump ulong 0\nend\n' "$function" "$actual" ;;
162 void) printf '%s %s\nir_dump ulong 0\n' "$function" "$actual" ;;
163 byte|short|int|long) printf 'ir_dump bitcast ulong long %s %s\n' "$function" "$actual" ;;
164 uchar|ushort|uint|ulong) printf 'ir_dump ulong %s %s\n' "$function" "$actual" ;;
165 esac >> "$om"
166 printf 'ret 0\nend\n' >> "$om"
167 "$OMC" < "$om" > "$B/nx-row-$nx_rows" || { bad "$table row $rows: native compile"; continue; }
168 chmod +x "$B/nx-row-$nx_rows"
169 set +e
170 timeout 10 "$B/nx-row-$nx_rows" > "$B/nx-row-$nx_rows.out"; rc=$?
171 ir=$("$B/irequiv" eval "$function" $eargs < "$B/nx-$unit.ir" 2>&1); st=$?
172 set -e
173 [ "$rc" = "$status" ] || bad "$table row $rows: native exit $rc, table $status"
174 if [ "$rc" = 0 ] && [ "$(wc -c < "$B/nx-row-$nx_rows.out")" = 8 ]; then
175 native="value:$(od -An -tu8 "$B/nx-row-$nx_rows.out" | tr -d ' ')"
176 elif [ "$rc" -gt 100 ] && [ "$rc" -lt 200 ]; then
177 native="trap:$((rc - 100))"
178 else
179 native="native-exit:$rc"
180 fi
181 if [ "$st" = 0 ] && [ "$ir" = "$native" ]; then
182 nx_equal=$((nx_equal + 1))
183 else
184 bad "$table row $rows $unit.$function $typed: native $native, irequiv $ir (exit $st)"
185 fi
186 done < "$table"
187 [ "$rows" = "$want" ] || bad "$table denominator $rows, want $want"
188}
189conform seed/wasm/cases.tsv 129
190conform seed/spirv/regression.tsv 8
191conform seed/spirv/demo.tsv 3
192[ "$nx_equal" = 140 ] || bad "native conformance equal=$nx_equal of $nx_rows, want 140"
193echo "CONFORM native rows=$nx_rows equal=$nx_equal"
194
195if [ "$fails" = 0 ]; then echo "GATE-IREQUIV PASS"; else echo "GATE-IREQUIV FAIL ($fails)"; exit 1; fi