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
omega-mv / seed / spirv / gate-irspirv.sh
| 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. |
| 8 | set -eu |
| 9 | cd "$(dirname "$0")/../.." |
| 10 | B=build/seed/spirv |
| 11 | mkdir -p "$B" |
| 12 | # Drop the previous run's artifacts, keeping only a prebuilt default host. |
| 13 | find "$B" -mindepth 1 -maxdepth 1 ! -name opencl-host -exec rm -rf {} + |
| 14 | : > "$B/gate.log" |
| 15 | say() { printf '%s\n' "$*" | tee -a "$B/gate.log"; } |
| 16 | fail() { say "FAIL $*"; exit 1; } |
| 17 | notrun() { say "NOT_RUN $*"; exit 3; } |
| 18 | [ "$(uname -m)" = aarch64 ] || notrun 'requires AArch64 Linux' |
| 19 | OMC=${OMC:-seed/out/omc} |
| 20 | SPIRV_VAL=${SPIRV_VAL:-spirv-val} |
| 21 | PYTHON=${PYTHON:-python3} |
| 22 | command -v "$SPIRV_VAL" >/dev/null || notrun "requires spirv-val ($SPIRV_VAL)" |
| 23 | command -v "$PYTHON" >/dev/null || notrun "requires Python parity oracle ($PYTHON)" |
| 24 | pin=26540e6de62402d5784b30b24d7c6faae488137c1f8a4b3e784fd0b5d5528991 |
| 25 | [ -f "$OMC" ] || fail "compiler $OMC missing" |
| 26 | [ "$(sha256sum "$OMC" | cut -d ' ' -f 1)" = "$pin" ] || fail 'compiler pin differs' |
| 27 | case "${POCL_LIBRARY:-}" in /*/libpocl.so*) ;; *) notrun 'POCL_LIBRARY must name the absolute trusted PoCL shared library' ;; esac |
| 28 | CC=${CC:-cc} |
| 29 | command -v "$CC" >/dev/null || notrun "requires a C compiler ($CC) for the OpenCL host controls" |
| 30 | if [ -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}" |
| 41 | fi |
| 42 | [ -x "$OPENCL_HOST" ] || notrun "OpenCL host $OPENCL_HOST is not executable" |
| 43 | say "SOURCE $(git rev-parse HEAD)" |
| 44 | say "COMPILER $OMC sha256=$pin" |
| 45 | say "VALIDATOR $(command -v "$SPIRV_VAL") $("$SPIRV_VAL" --version 2>&1 | tr '\n' ' ')" |
| 46 | say "OPENCL_HOST $OPENCL_HOST sha256=$(sha256sum "$OPENCL_HOST" | cut -d ' ' -f 1)" |
| 47 | say "ORACLE $("$PYTHON" --version 2>&1) parity.py sha256=$(sha256sum seed/spirv/parity.py | cut -d ' ' -f 1)" |
| 48 | say "RUNTIME_CONFIG POCL_LIBRARY=$POCL_LIBRARY inherited_vendor_and_layer_discovery=overridden" |
| 49 | say "PLATFORM $(uname -srm)" |
| 50 | nice -n 19 "$OMC" < seed/spirv/irspirv.om > "$B/irspirv" 2> "$B/compile.err" |
| 51 | [ ! -s "$B/compile.err" ] || fail 'translator compiler stderr' |
| 52 | chmod +x "$B/irspirv" |
| 53 | validate() { |
| 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 | } |
| 58 | modules=0; deterministic=0; validated=0 |
| 59 | for 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" |
| 77 | done |
| 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. |
| 80 | env 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' |
| 86 | printf '8 0\n' > "$B/host-environment.expected" |
| 87 | cmp "$B/host-environment.out" "$B/host-environment.expected" || fail 'environment control changed result' |
| 88 | for 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 |
| 107 | done |
| 108 | say '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. |
| 111 | tab=$(printf '\t') |
| 112 | native_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' |
| 122 | global dumpbuf uchar 8 |
| 123 | fn ir_dump x ulong to void |
| 124 | for j ulong 8 |
| 125 | put dumpbuf j uchar and shr x mul j 8 255 |
| 126 | end |
| 127 | p = ulong 0 |
| 128 | while lt p 8 bound 8 |
| 129 | k = write 1 dumpbuf p sub ulong 8 p |
| 130 | if le k 0 |
| 131 | exit 90 |
| 132 | end |
| 133 | p = add p ulong k |
| 134 | end |
| 135 | end |
| 136 | fn main to int |
| 137 | DUMP |
| 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 | } |
| 164 | native_cases seed/wasm/cases.tsv case |
| 165 | [ "$i" = 129 ] || fail "seed/wasm/cases.tsv denominator is $i, expected 129" |
| 166 | cases=$i; case_traps=$traps |
| 167 | native_cases seed/spirv/demo.tsv demo-case |
| 168 | demo_cases=$i |
| 169 | native_cases seed/spirv/regression.tsv regression-case |
| 170 | regression_cases=$i |
| 171 | say "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' |
| 174 | cp "$B/mutant.ir" "$B/mutant-flipped.ir" |
| 175 | printf '\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 $?" |
| 177 | cmp -s "$B/mutant.spv" "$B/mutant-flipped.spv" && fail 'mutant IR byte did not change the module' |
| 178 | validate "$B/mutant-flipped.spv" |
| 179 | say '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. |
| 181 | size=$(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' |
| 184 | dd if="$B/mutant.spv" of="$B/validator-negative.spv" bs=4 count=$((size/4-1)) status=none |
| 185 | if "$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' |
| 187 | fi |
| 188 | say 'PASS spirv-val rejects truncated module (negative control)' |
| 189 | oracle() { |
| 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 | } |
| 196 | log=parity.log; oracle seed/wasm/cases.tsv "$OPENCL_HOST" case |
| 197 | [ "$rc" = 0 ] || fail "parity checker exited $rc" |
| 198 | say "RUNTIME $(tr '\n' ' ' < "$B/case-1.spirv.err")" |
| 199 | matched=$(grep -c "${tab}match\$" "$B/case-spirv-parity.tsv" || :) |
| 200 | [ "$matched" = "$cases" ] || fail "parity table matched=$matched expected $cases" |
| 201 | log=demo.log; oracle seed/spirv/demo.tsv "$OPENCL_HOST" demo-case |
| 202 | [ "$rc" = 0 ] || fail "demo parity checker exited $rc" |
| 203 | log=regression.log; oracle seed/spirv/regression.tsv "$OPENCL_HOST" regression-case |
| 204 | [ "$rc" = 0 ] || fail "regression parity checker exited $rc" |
| 205 | log=negative.log; oracle seed/wasm/cases.tsv "$OPENCL_HOST" case --mutant-baseline |
| 206 | [ "$rc" = 1 ] || fail "mutant comparison exited $rc instead of 1" |
| 207 | grep -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' |
| 209 | grep -q '^result mismatch at case [0-9]* mutant\.mutadd:' "$B/negative.log" || fail 'mutant failed for unrelated reason' |
| 210 | say 'PASS parity checker rejects successfully executed mutant substituted as baseline (exit 1)' |
| 211 | refusals=0 |
| 212 | refuse() { |
| 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 | } |
| 225 | bytecase() { |
| 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 | } |
| 231 | bytecase bad-magic 0 '\130' 20 MAGIC |
| 232 | bytecase version2 7 '\002' 21 VERSION |
| 233 | bytecase version6 7 '\006' 21 VERSION |
| 234 | bytecase unsupported-op 38 '\007' 24 UNSUPPORTED_OP |
| 235 | bytecase unknown-op 38 '\036' 24 UNSUPPORTED_OP |
| 236 | bytecase unsupported-tag 18 '\012' 25 UNSUPPORTED_TAG |
| 237 | bytecase unknown-tag 18 '\015' 25 UNSUPPORTED_TAG |
| 238 | bytecase bad-register 44 '\003' 26 INVALID |
| 239 | bytecase undefined-register 42 '\002' 26 INVALID |
| 240 | bytecase nonascii-name 11 '\377' 26 INVALID |
| 241 | bytecase oversized-count 8 '\041' 23 CAPACITY |
| 242 | size=$(wc -c < "$B/mutant.ir") |
| 243 | dd if="$B/mutant.ir" of="$B/truncated.ir" bs=1 count=$((size-1)) status=none |
| 244 | refuse truncated 22 TRUNCATED |
| 245 | : > "$B/empty.ir" |
| 246 | refuse empty 22 TRUNCATED |
| 247 | cp "$B/mutant.ir" "$B/trailing.ir" |
| 248 | printf x >> "$B/trailing.ir" |
| 249 | refuse trailing 27 TRAILING |
| 250 | dd if=/dev/zero of="$B/oversized.ir" bs=2620252 count=1 status=none |
| 251 | refuse 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' |
| 255 | import pathlib, struct, sys |
| 256 | b = pathlib.Path(sys.argv[1]) |
| 257 | header = b'OSC1IR\0\x01' |
| 258 | unit = header + struct.pack('<H', 1) |
| 259 | unit += b'\x05reord' + struct.pack('<BBHH', 0, 0, 0, 3) |
| 260 | unit += b''.join(struct.pack('<II', first, 1) for first in (0, 1, 2)) |
| 261 | unit += struct.pack('<I', 3) |
| 262 | unit += struct.pack('<BH', 13, 2) + struct.pack('<BH', 15, 65535) + struct.pack('<BH', 13, 1) |
| 263 | unit += struct.pack('<HH', 0, 0) |
| 264 | (b / 'reordered.ir').write_bytes(unit) |
| 265 | (b / 'zero-functions.ir').write_bytes(header + struct.pack('<H', 0)) |
| 266 | PY |
| 267 | refuse zero-functions 26 INVALID |
| 268 | "$B/irspirv" < "$B/reordered.ir" > "$B/reordered.spv" |
| 269 | "$B/irspirv" < "$B/reordered.ir" > "$B/reordered.again.spv" |
| 270 | cmp "$B/reordered.spv" "$B/reordered.again.spv" || fail 'reordered CFG nondeterministic' |
| 271 | modules=$((modules+1)); deterministic=$((deterministic+1)) |
| 272 | validate "$B/reordered.spv" |
| 273 | "$OPENCL_HOST" "$B/reordered.spv" reord > "$B/reordered.out" 2> "$B/reordered.err" || |
| 274 | fail 'reordered CFG CPU execution failed' |
| 275 | printf '0 0\n' > "$B/reordered.expected" |
| 276 | cmp "$B/reordered.out" "$B/reordered.expected" || fail 'reordered CFG result differs' |
| 277 | say 'PASS reordered CFG 0->2->1 deterministic validated CPU result=0 status=0' |
| 278 | say "SUPPLEMENTAL nested_trap_cases=$regression_cases reordered_cfg=1" |
| 279 | say "SUMMARY cases=$cases matched=$matched traps=$case_traps modules=$modules deterministic=$deterministic validated=$validated validator_negative=rejected mutant=rejected refusals=$refusals demo=$demo_cases" |
| 280 | say "PASS IRSPIRV CPU OpenCL parity cases=$cases deterministic=$deterministic validated=$validated refusals=$refusals" |
| 281 | sha256sum 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" |