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 / spirv / gate-irspirv.sh

284 lines · 14735 bytes Raw Blame History
1#!/bin/sh
2# Host-only CPU verification of seed/spirv/irspirv.om. spirv-val, the OpenCL CPU
3# host and parity.py are read-only oracles, never generators of IR or SPIR-V.
4# POCL_LIBRARY is a required build-time absolute PoCL .so path. Optional: OMC,
5# SPIRV_VAL, OPENCL_HOST, PYTHON, CC, OPENCL_CFLAGS and OPENCL_LIBS.
6# Runtime vendor/layer environment variables cannot override the compiled oracle.
7# Exit 0 PASS, 1 FAIL, 3 NOT_RUN. Every artifact stays under build/seed/spirv.
8set -eu
9cd "$(dirname "$0")/../.."
10B=build/seed/spirv
11mkdir -p "$B"
12# Drop the previous run's artifacts, keeping only a prebuilt default host.
13find "$B" -mindepth 1 -maxdepth 1 ! -name opencl-host -exec rm -rf {} +
14: > "$B/gate.log"
15say() { printf '%s\n' "$*" | tee -a "$B/gate.log"; }
16fail() { say "FAIL $*"; exit 1; }
17notrun() { say "NOT_RUN $*"; exit 3; }
18[ "$(uname -m)" = aarch64 ] || notrun 'requires AArch64 Linux'
19OMC=${OMC:-seed/out/omc}
20SPIRV_VAL=${SPIRV_VAL:-spirv-val}
21PYTHON=${PYTHON:-python3}
22command -v "$SPIRV_VAL" >/dev/null || notrun "requires spirv-val ($SPIRV_VAL)"
23command -v "$PYTHON" >/dev/null || notrun "requires Python parity oracle ($PYTHON)"
24pin=26540e6de62402d5784b30b24d7c6faae488137c1f8a4b3e784fd0b5d5528991
25[ -f "$OMC" ] || fail "compiler $OMC missing"
26[ "$(sha256sum "$OMC" | cut -d ' ' -f 1)" = "$pin" ] || fail 'compiler pin differs'
27case "${POCL_LIBRARY:-}" in /*/libpocl.so*) ;; *) notrun 'POCL_LIBRARY must name the absolute trusted PoCL shared library' ;; esac
28CC=${CC:-cc}
29command -v "$CC" >/dev/null || notrun "requires a C compiler ($CC) for the OpenCL host controls"
30if [ -z "${OPENCL_HOST:-}" ]; then
31 # Demo/verification host only; the translator never links or calls it.
32 # The build configuration, not inherited runtime discovery, selects the oracle.
33 [ -f seed/spirv/opencl-host.c ] || fail 'seed/spirv/opencl-host.c missing'
34 # shellcheck disable=SC2086 # flag lists are intentionally word-split
35 "$CC" -std=c11 -O2 -Wall -Wextra ${OPENCL_CFLAGS:-} \
36 "-DIRSPIRV_POCL_LIBRARY=\"$POCL_LIBRARY\"" seed/spirv/opencl-host.c \
37 -o "$B/opencl-host" ${OPENCL_LIBS:--lOpenCL} > "$B/opencl-host.build.log" 2>&1 ||
38 { cat "$B/opencl-host.build.log"; fail 'OpenCL host build failed'; }
39 OPENCL_HOST=$B/opencl-host
40 say "HOST_BUILD $("$CC" --version | sed -n 1p) flags=${OPENCL_CFLAGS:-} libs=${OPENCL_LIBS:--lOpenCL}"
41fi
42[ -x "$OPENCL_HOST" ] || notrun "OpenCL host $OPENCL_HOST is not executable"
43say "SOURCE $(git rev-parse HEAD)"
44say "COMPILER $OMC sha256=$pin"
45say "VALIDATOR $(command -v "$SPIRV_VAL") $("$SPIRV_VAL" --version 2>&1 | tr '\n' ' ')"
46say "OPENCL_HOST $OPENCL_HOST sha256=$(sha256sum "$OPENCL_HOST" | cut -d ' ' -f 1)"
47say "ORACLE $("$PYTHON" --version 2>&1) parity.py sha256=$(sha256sum seed/spirv/parity.py | cut -d ' ' -f 1)"
48say "RUNTIME_CONFIG POCL_LIBRARY=$POCL_LIBRARY inherited_vendor_and_layer_discovery=overridden"
49say "PLATFORM $(uname -srm)"
50nice -n 19 "$OMC" < seed/spirv/irspirv.om > "$B/irspirv" 2> "$B/compile.err"
51[ ! -s "$B/compile.err" ] || fail 'translator compiler stderr'
52chmod +x "$B/irspirv"
53validate() {
54 "$SPIRV_VAL" --target-env opencl2.1 "$1" > "$1.val.log" 2>&1 ||
55 { cat "$1.val.log"; fail "spirv-val rejected $1"; }
56 validated=$((validated+1))
57}
58modules=0; deterministic=0; validated=0
59for source in tests/one_layer/omegese/dev/*.omegese seed/wasm/*.unit.om seed/spirv/*.unit.om; do
60 id=$(basename "$source" .omegese)
61 id=${id%.unit.om}
62 [ ! -e "$B/$id.source" ] || fail "duplicate unit id $id"
63 { printf 'ir\n'; cat "$source"; } > "$B/$id.input"
64 nice -n 19 "$OMC" < "$B/$id.input" > "$B/$id.ir" || fail "$id IR compile exited $?"
65 nice -n 19 "$B/irspirv" < "$B/$id.ir" > "$B/$id.spv" 2> "$B/$id.translate.err" ||
66 fail "$id translator exited $?"
67 [ ! -s "$B/$id.translate.err" ] || fail "$id translator stderr"
68 nice -n 19 "$B/irspirv" < "$B/$id.ir" > "$B/$id.again.spv" 2> "$B/$id.again.err" ||
69 fail "$id translator rerun exited $?"
70 [ ! -s "$B/$id.again.err" ] || fail "$id translator stderr on rerun"
71 modules=$((modules+1))
72 cmp "$B/$id.spv" "$B/$id.again.spv" || fail "$id nondeterministic"
73 deterministic=$((deterministic+1))
74 validate "$B/$id.spv"
75 printf '%s\n' "$source" > "$B/$id.source"
76 say "PASS $id deterministic spirv-val=opencl2.1"
77done
78# The former environment-selected manifest had a check/open/re-open race.
79# Inherited discovery must not select any driver or layer, even before enumeration.
80env OCL_ICD_VENDORS=/nonexistent/irspirv-vendors \
81 OCL_ICD_FILENAMES=/nonexistent/irspirv-icd OPENCL_VENDOR_PATH=/nonexistent/irspirv-vendor \
82 OPENCL_LAYERS=/nonexistent/irspirv-layer OPENCL_LAYER_PATH=/nonexistent/irspirv-layers \
83 POCL_DEVICES=untrusted-device "$OPENCL_HOST" "$B/demo.spv" avgu32 7 10 \
84 > "$B/host-environment.out" 2> "$B/host-environment.err" ||
85 fail 'inherited discovery changed the configured CPU oracle'
86printf '8 0\n' > "$B/host-environment.expected"
87cmp "$B/host-environment.out" "$B/host-environment.expected" || fail 'environment control changed result'
88for control in unconfigured missing-library; do
89 definition='-DIRSPIRV_POCL_LIBRARY=""'
90 if [ "$control" = missing-library ]; then
91 definition='-DIRSPIRV_POCL_LIBRARY="/nonexistent/irspirv/libpocl.so"'
92 fi
93 # shellcheck disable=SC2086
94 "$CC" -std=c11 -O2 -Wall -Wextra ${OPENCL_CFLAGS:-} "$definition" \
95 seed/spirv/opencl-host.c -o "$B/host-$control" ${OPENCL_LIBS:--lOpenCL} \
96 > "$B/host-$control.build.log" 2>&1 || fail "host $control control build failed"
97 set +e
98 (ulimit -c 0; "$B/host-$control" "$B/demo.spv" avgu32 7 10) \
99 > "$B/host-$control.out" 2> "$B/host-$control.err"
100 rc=$?
101 set -e
102 [ "$rc" -ne 0 ] || fail "$control silently fell back to a working ICD"
103 [ ! -s "$B/host-$control.out" ] || fail "$control printed a result"
104 if [ "$control" = unconfigured ]; then
105 [ "$rc" = 3 ] || fail "unconfigured host exited $rc rather than 3"
106 fi
107done
108say 'PASS host controls: inherited discovery ignored; unconfigured and missing fixed library fail closed'
109# Each table row gets a real native program calling the same function and inputs.
110# The dumper preserves every result bit, rather than comparing process-status bytes.
111tab=$(printf '\t')
112native_cases() {
113 table=$1; prefix=$2; i=0; traps=0
114 while IFS="$tab" read -r unit function result typed status; do
115 case "$unit" in ''|'#'*) continue ;; esac
116 i=$((i+1))
117 [ "$status" = 0 ] || traps=$((traps+1))
118 [ -f "$B/$unit.source" ] || fail "$table row $i names unknown unit $unit"
119 case_om=$B/$prefix-$i.om
120 cat "$(cat "$B/$unit.source")" > "$case_om"
121 cat >> "$case_om" <<'DUMP'
122global dumpbuf uchar 8
123fn ir_dump x ulong to void
124for j ulong 8
125put dumpbuf j uchar and shr x mul j 8 255
126end
127p = ulong 0
128while lt p 8 bound 8
129k = write 1 dumpbuf p sub ulong 8 p
130if le k 0
131exit 90
132end
133p = add p ulong k
134end
135end
136fn main to int
137DUMP
138 actual=
139 if [ "$typed" != - ]; then
140 for arg in $(printf '%s' "$typed" | tr ',' ' '); do
141 type=${arg%%:*}; value=${arg#*:}
142 if [ "$type" = bool ]; then actual="$actual $value"
143 else actual="$actual $type $value"; fi
144 done
145 fi
146 case "$result" in
147 bool) printf 'if %s %s\nir_dump ulong 1\nelse\nir_dump ulong 0\nend\n' "$function" "$actual" ;;
148 void) printf '%s %s\nir_dump ulong 0\n' "$function" "$actual" ;;
149 byte|short|int|long) printf 'ir_dump bitcast ulong long %s %s\n' "$function" "$actual" ;;
150 uchar|ushort|uint|ulong) printf 'ir_dump ulong %s %s\n' "$function" "$actual" ;;
151 *) fail "unknown result $result" ;;
152 esac >> "$case_om"
153 printf 'ret 0\nend\n' >> "$case_om"
154 nice -n 19 "$OMC" < "$case_om" > "$B/$prefix-$i.native"
155 chmod +x "$B/$prefix-$i.native"
156 set +e
157 timeout 10 "$B/$prefix-$i.native" > "$B/$prefix-$i.out"
158 rc=$?
159 set -e
160 printf '%s\n' "$rc" > "$B/$prefix-$i.status"
161 [ "$rc" = "$status" ] || fail "$prefix $i $unit.$function native exit $rc expected $status"
162 done < "$table"
163}
164native_cases seed/wasm/cases.tsv case
165[ "$i" = 129 ] || fail "seed/wasm/cases.tsv denominator is $i, expected 129"
166cases=$i; case_traps=$traps
167native_cases seed/spirv/demo.tsv demo-case
168demo_cases=$i
169native_cases seed/spirv/regression.tsv regression-case
170regression_cases=$i
171say "PASS native dumps cases=$cases traps=$case_traps demo=$demo_cases"
172# The pinned simple fixture has BIN ADD at offsets 38,39. Assert before mutating.
173[ "$(od -An -j 38 -N 2 -t u1 "$B/mutant.ir" | tr -s ' ' | tr -d '\n')" = ' 3 1' ] || fail 'mutant IR layout changed'
174cp "$B/mutant.ir" "$B/mutant-flipped.ir"
175printf '\002' | dd of="$B/mutant-flipped.ir" bs=1 seek=39 conv=notrunc status=none
176"$B/irspirv" < "$B/mutant-flipped.ir" > "$B/mutant-flipped.spv" || fail "mutant translator exited $?"
177cmp -s "$B/mutant.spv" "$B/mutant-flipped.spv" && fail 'mutant IR byte did not change the module'
178validate "$B/mutant-flipped.spv"
179say 'PASS add-to-sub mutant module emitted and accepted by spirv-val'
180# Validator negative control: a valid module always ends in OpFunctionEnd; dropping it must be rejected.
181size=$(wc -c < "$B/mutant.spv")
182[ "$(od -An -t x4 -j $((size-4)) -N 4 "$B/mutant.spv" | tr -d ' \n')" = 00010038 ] ||
183 fail 'mutant module does not end in OpFunctionEnd'
184dd if="$B/mutant.spv" of="$B/validator-negative.spv" bs=4 count=$((size/4-1)) status=none
185if "$SPIRV_VAL" --target-env opencl2.1 "$B/validator-negative.spv" > "$B/validator-negative.log" 2>&1; then
186 fail 'spirv-val accepted a module without OpFunctionEnd'
187fi
188say 'PASS spirv-val rejects truncated module (negative control)'
189oracle() {
190 set +e
191 "$PYTHON" seed/spirv/parity.py "$B" "$@" > "$B/$log" 2>&1
192 rc=$?
193 set -e
194 tee -a "$B/gate.log" < "$B/$log"
195}
196log=parity.log; oracle seed/wasm/cases.tsv "$OPENCL_HOST" case
197[ "$rc" = 0 ] || fail "parity checker exited $rc"
198say "RUNTIME $(tr '\n' ' ' < "$B/case-1.spirv.err")"
199matched=$(grep -c "${tab}match\$" "$B/case-spirv-parity.tsv" || :)
200[ "$matched" = "$cases" ] || fail "parity table matched=$matched expected $cases"
201log=demo.log; oracle seed/spirv/demo.tsv "$OPENCL_HOST" demo-case
202[ "$rc" = 0 ] || fail "demo parity checker exited $rc"
203log=regression.log; oracle seed/spirv/regression.tsv "$OPENCL_HOST" regression-case
204[ "$rc" = 0 ] || fail "regression parity checker exited $rc"
205log=negative.log; oracle seed/wasm/cases.tsv "$OPENCL_HOST" case --mutant-baseline
206[ "$rc" = 1 ] || fail "mutant comparison exited $rc instead of 1"
207grep -q '^ERROR' "$B/negative.log" && fail 'mutant run hit an oracle or API failure'
208[ "$(grep -c '^result mismatch' "$B/negative.log")" = 1 ] || fail 'mutant must mismatch exactly one row'
209grep -q '^result mismatch at case [0-9]* mutant\.mutadd:' "$B/negative.log" || fail 'mutant failed for unrelated reason'
210say 'PASS parity checker rejects successfully executed mutant substituted as baseline (exit 1)'
211refusals=0
212refuse() {
213 name=$1; expected=$2; class=$3
214 set +e
215 "$B/irspirv" < "$B/$name.ir" > "$B/$name.refused" 2> "$B/$name.err"
216 rc=$?
217 set -e
218 [ "$rc" = "$expected" ] || fail "$name exit $rc expected $expected"
219 [ ! -s "$B/$name.refused" ] || fail "$name emitted partial module"
220 printf 'irspirv: %s\n' "$class" > "$B/expected.err"
221 cmp "$B/expected.err" "$B/$name.err" || fail "$name wrong refusal name"
222 refusals=$((refusals+1))
223 say "PASS $name $class exit=$rc stdout=empty"
224}
225bytecase() {
226 name=$1; offset=$2; bytes=$3; code=$4; class=$5
227 cp "$B/mutant.ir" "$B/$name.ir"
228 printf '%b' "$bytes" | dd of="$B/$name.ir" bs=1 seek="$offset" conv=notrunc status=none
229 refuse "$name" "$code" "$class"
230}
231bytecase bad-magic 0 '\130' 20 MAGIC
232bytecase version2 7 '\002' 21 VERSION
233bytecase version6 7 '\006' 21 VERSION
234bytecase unsupported-op 38 '\007' 24 UNSUPPORTED_OP
235bytecase unknown-op 38 '\036' 24 UNSUPPORTED_OP
236bytecase unsupported-tag 18 '\012' 25 UNSUPPORTED_TAG
237bytecase unknown-tag 18 '\015' 25 UNSUPPORTED_TAG
238bytecase bad-register 44 '\003' 26 INVALID
239bytecase undefined-register 42 '\002' 26 INVALID
240bytecase nonascii-name 11 '\377' 26 INVALID
241bytecase oversized-count 8 '\041' 23 CAPACITY
242size=$(wc -c < "$B/mutant.ir")
243dd if="$B/mutant.ir" of="$B/truncated.ir" bs=1 count=$((size-1)) status=none
244refuse truncated 22 TRUNCATED
245: > "$B/empty.ir"
246refuse empty 22 TRUNCATED
247cp "$B/mutant.ir" "$B/trailing.ir"
248printf x >> "$B/trailing.ir"
249refuse trailing 27 TRAILING
250dd if=/dev/zero of="$B/oversized.ir" bs=2620252 count=1 status=none
251refuse oversized 23 CAPACITY
252# A valid OSC CFG may number a dominator after its dominated block.
253# This independent wire fixture is 0 -> 2 -> 1, with a void return in block 1.
254"$PYTHON" - "$B" <<'PY'
255import pathlib, struct, sys
256b = pathlib.Path(sys.argv[1])
257header = b'OSC1IR\0\x01'
258unit = header + struct.pack('<H', 1)
259unit += b'\x05reord' + struct.pack('<BBHH', 0, 0, 0, 3)
260unit += b''.join(struct.pack('<II', first, 1) for first in (0, 1, 2))
261unit += struct.pack('<I', 3)
262unit += struct.pack('<BH', 13, 2) + struct.pack('<BH', 15, 65535) + struct.pack('<BH', 13, 1)
263unit += struct.pack('<HH', 0, 0)
264(b / 'reordered.ir').write_bytes(unit)
265(b / 'zero-functions.ir').write_bytes(header + struct.pack('<H', 0))
266PY
267refuse zero-functions 26 INVALID
268"$B/irspirv" < "$B/reordered.ir" > "$B/reordered.spv"
269"$B/irspirv" < "$B/reordered.ir" > "$B/reordered.again.spv"
270cmp "$B/reordered.spv" "$B/reordered.again.spv" || fail 'reordered CFG nondeterministic'
271modules=$((modules+1)); deterministic=$((deterministic+1))
272validate "$B/reordered.spv"
273"$OPENCL_HOST" "$B/reordered.spv" reord > "$B/reordered.out" 2> "$B/reordered.err" ||
274 fail 'reordered CFG CPU execution failed'
275printf '0 0\n' > "$B/reordered.expected"
276cmp "$B/reordered.out" "$B/reordered.expected" || fail 'reordered CFG result differs'
277say 'PASS reordered CFG 0->2->1 deterministic validated CPU result=0 status=0'
278say "SUPPLEMENTAL nested_trap_cases=$regression_cases reordered_cfg=1"
279say "SUMMARY cases=$cases matched=$matched traps=$case_traps modules=$modules deterministic=$deterministic validated=$validated validator_negative=rejected mutant=rejected refusals=$refusals demo=$demo_cases"
280say "PASS IRSPIRV CPU OpenCL parity cases=$cases deterministic=$deterministic validated=$validated refusals=$refusals"
281sha256sum seed/spirv/irspirv.om "$B/irspirv" seed/spirv/gate-irspirv.sh \
282 seed/wasm/cases.tsv seed/spirv/demo.tsv seed/spirv/regression.tsv \
283 seed/spirv/demo.unit.om seed/spirv/traps.unit.om seed/spirv/parity.py \
284 seed/spirv/opencl-host.c "$OPENCL_HOST" | tee -a "$B/gate.log"