omega-kernel-smoke
Minimal Omega GPU kernel smoke verification harness
Brought by drakestapleton
omega-kernel-smoke / tools / check.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Check kernel.om three ways on one AArch64 Linux CPU: |
| 4 | # 1. AArch64: the pinned omc compiles every case to a native program and runs it. |
| 5 | # 2. SPIR-V: omc emits OSC IR, the Omega translator irspirv.om (compiled by the same |
| 6 | # omc) turns it into a SPIR-V OpenCL kernel module, spirv-val checks it, and |
| 7 | # PoCL runs it on the CPU device. No GPU is involved anywhere. |
| 8 | # 3. Reference: reference.py computes the expected value of every case. |
| 9 | # A case passes only when AArch64 equals the reference and SPIR-V equals AArch64 bit for bit. |
| 10 | # Exit 0 PASS, 1 FAIL, 3 NOT_RUN (required tool or platform missing). |
| 11 | # Everything is written under build/; build/receipt/ is the uploaded receipt. |
| 12 | # INTERIM host glue (POSIX sh). Successor: an Omega conductor unit in the style of |
| 13 | # omega-mv seed/qualification/om2/gate.om, once it can spawn the OpenCL host per case. |
| 14 | set -eu |
| 15 | cd "$(dirname "$0")/.." |
| 16 | . ./toolchain.lock |
| 17 | B=build |
| 18 | R=$B/receipt |
| 19 | T=${OKT_TOOLS:-.okt/tools} |
| 20 | PYTHON=${PYTHON:-python3} |
| 21 | CC=${CC:-cc} |
| 22 | SPIRV_VAL=${SPIRV_VAL:-spirv-val} |
| 23 | SPIRV_DIS=${SPIRV_DIS:-spirv-dis} |
| 24 | POCL_LIBRARY=${POCL_LIBRARY:-$PWD/.okt/pocl/$POCL_LIB} |
| 25 | rm -rf "$B" |
| 26 | mkdir -p "$R" |
| 27 | : > "$B/check.log" |
| 28 | say() { printf '%s\n' "$*" | tee -a "$B/check.log"; } |
| 29 | report() { "$PYTHON" tools/cases.py report "$B" || return $?; } |
| 30 | stop() { |
| 31 | # stop VERDICT STAGE MESSAGE: record why the check ended early, then write the receipt. |
| 32 | printf '%s\t%s\t%s\n' "$1" "$2" "$3" > "$B/stop.tsv" |
| 33 | say "$1 $2: $3" |
| 34 | set +e |
| 35 | report |
| 36 | rc=$? |
| 37 | set -e |
| 38 | [ "$1" = NOT_RUN ] && exit 3 |
| 39 | [ "$rc" = 0 ] && exit 1 |
| 40 | exit "$rc" |
| 41 | } |
| 42 | |
| 43 | command -v "$PYTHON" > /dev/null || { echo "NOT_RUN requires $PYTHON" >&2; exit 3; } |
| 44 | [ "$(uname -s)-$(uname -m)" = Linux-aarch64 ] || stop NOT_RUN platform "requires AArch64 Linux, found $(uname -sm)" |
| 45 | command -v "$CC" > /dev/null || stop NOT_RUN toolchain "requires a C compiler ($CC) for the OpenCL host" |
| 46 | command -v "$SPIRV_VAL" > /dev/null || stop NOT_RUN toolchain "requires spirv-val ($SPIRV_VAL)" |
| 47 | case "$POCL_LIBRARY" in /*/libpocl.so*) ;; *) stop NOT_RUN toolchain "POCL_LIBRARY must be an absolute libpocl.so path" ;; esac |
| 48 | [ -f "$POCL_LIBRARY" ] || stop NOT_RUN toolchain "PoCL library $POCL_LIBRARY missing; run sh tools/fetch.sh" |
| 49 | |
| 50 | pinned() { |
| 51 | [ -f "$1" ] || stop NOT_RUN toolchain "$1 missing; run sh tools/fetch.sh" |
| 52 | [ "$(sha256sum "$1" | cut -d ' ' -f 1)" = "$2" ] || stop FAIL toolchain "$1 differs from its pin in toolchain.lock" |
| 53 | } |
| 54 | pinned "$T/omc" "$OMC_SHA256" |
| 55 | pinned "$T/irspirv.om" "$IRSPIRV_OM_SHA256" |
| 56 | pinned "$T/opencl-host.c" "$OPENCL_HOST_C_SHA256" |
| 57 | pinned "$T/parity.py" "$PARITY_PY_SHA256" |
| 58 | OMC=$T/omc |
| 59 | say "PINS omega-mv@$OMEGA_MV_COMMIT omc=$OMC_SHA256 pocl=$POCL_LIBRARY" |
| 60 | say "PLATFORM $(uname -srm)" |
| 61 | |
| 62 | # Toolchain built from pinned sources: the Omega translator and the OpenCL CPU host. |
| 63 | "$OMC" < "$T/irspirv.om" > "$B/irspirv" 2> "$B/irspirv.compile.err" || stop FAIL toolchain 'omc failed to compile irspirv.om' |
| 64 | [ ! -s "$B/irspirv.compile.err" ] || stop FAIL toolchain 'omc wrote stderr while compiling irspirv.om' |
| 65 | chmod +x "$B/irspirv" |
| 66 | # shellcheck disable=SC2086 # flag lists are intentionally word-split |
| 67 | "$CC" -std=c11 -O2 -Wall -Wextra ${OPENCL_CFLAGS:-} "-DIRSPIRV_POCL_LIBRARY=\"$POCL_LIBRARY\"" \ |
| 68 | "$T/opencl-host.c" -o "$B/opencl-host" ${OPENCL_LIBS:--lOpenCL} > "$B/opencl-host.build.log" 2>&1 || |
| 69 | { cat "$B/opencl-host.build.log"; stop NOT_RUN toolchain 'OpenCL host build failed (is ocl-icd-opencl-dev installed?)'; } |
| 70 | |
| 71 | # 1. Kernel to OSC IR, then to SPIR-V, then validation. |
| 72 | [ -f kernel.om ] || stop FAIL input 'kernel.om missing' |
| 73 | { printf 'ir\n'; cat kernel.om; } > "$B/kernel.input" |
| 74 | explain() { |
| 75 | # omc reports "omc: line N: error C" for its input; kernel.om line = N - $1 (prepended lines). |
| 76 | line=$(sed -n 's/.*line \([0-9][0-9]*\): error \([0-9][0-9]*\).*/\1 \2/p' "$2" | head -n 1) |
| 77 | [ -n "$line" ] || { tr '\n' ' ' < "$2"; return; } |
| 78 | set -- "$1" $line |
| 79 | case $3 in |
| 80 | 3) why='unknown name or word not available here' ;; 4) why='line ends early' ;; |
| 81 | 5) why='extra words on the line' ;; 6) why='bad number' ;; 7) why='type mismatch' ;; |
| 82 | 8) why='value out of range' ;; 9) why='word not allowed here (IR mode refuses struct, global, text, floats, host words)' ;; |
| 83 | 10) why='duplicate name' ;; 11) why='ambiguous literal: give the type, e.g. uint 1' ;; |
| 84 | 12) why='register pool exhausted' ;; 13) why='variable name longer than 8 bytes' ;; |
| 85 | 14) why='capacity' ;; 15) why='missing end' ;; 16) why='syntax' ;; 17) why='line after a path end' ;; |
| 86 | 18) why='missing return' ;; 19) why='aliasing' ;; 20) why='readonly' ;; 21) why='constant index' ;; |
| 87 | 22) why='text literal' ;; 23) why='size capacity' ;; *) why='see omega-mv seed/README.md R30' ;; |
| 88 | esac |
| 89 | printf 'kernel.om line %s: error %s (%s)' "$(($2 - $1))" "$3" "$why" |
| 90 | } |
| 91 | if ! "$OMC" < "$B/kernel.input" > "$B/kernel.ir" 2> "$B/kernel.compile.err"; then |
| 92 | stop FAIL compile "omc refused $(explain 1 "$B/kernel.compile.err")" |
| 93 | fi |
| 94 | if ! "$B/irspirv" < "$B/kernel.ir" > "$B/kernel.spv" 2> "$B/kernel.translate.err"; then |
| 95 | stop FAIL spirv "irspirv refused the kernel IR: $(tr '\n' ' ' < "$B/kernel.translate.err")" |
| 96 | fi |
| 97 | [ ! -s "$B/kernel.translate.err" ] || stop FAIL spirv 'irspirv wrote stderr' |
| 98 | "$B/irspirv" < "$B/kernel.ir" > "$B/kernel.again.spv" |
| 99 | cmp -s "$B/kernel.spv" "$B/kernel.again.spv" || stop FAIL spirv 'irspirv output is not deterministic' |
| 100 | "$SPIRV_VAL" --target-env opencl2.1 "$B/kernel.spv" > "$B/spirv-val.log" 2>&1 || |
| 101 | { cat "$B/spirv-val.log"; stop FAIL spirv 'spirv-val rejected kernel.spv'; } |
| 102 | if command -v "$SPIRV_DIS" > /dev/null; then "$SPIRV_DIS" "$B/kernel.spv" -o "$B/kernel.spvasm"; fi |
| 103 | say "PASS kernel.om -> kernel.ir -> kernel.spv (deterministic, spirv-val opencl2.1)" |
| 104 | |
| 105 | # 2. Cases: cases.tsv plus deterministic edge and random inputs, with reference values. |
| 106 | "$PYTHON" tools/cases.py expand "$B" > "$B/expand.log" 2>&1 || |
| 107 | { cat "$B/expand.log"; stop FAIL cases "$(tail -n 1 "$B/expand.log")"; } |
| 108 | cat "$B/expand.log" | tee -a "$B/check.log" |
| 109 | |
| 110 | # 3. AArch64: one native program per case, so a trap in one case cannot hide another. |
| 111 | tab=$(printf '\t') |
| 112 | : > "$B/parity-table.tsv" |
| 113 | while IFS="$tab" read -r i fn ret typed omargs expect_bits expect_status src; do |
| 114 | [ "$omargs" != - ] || omargs= |
| 115 | case_om=$B/case-$i.om |
| 116 | cat kernel.om > "$case_om" |
| 117 | cat >> "$case_om" <<'DUMP' |
| 118 | global dumpbuf uchar 8 |
| 119 | fn ir_dump x ulong to void |
| 120 | for j ulong 8 |
| 121 | put dumpbuf j uchar and shr x mul j 8 255 |
| 122 | end |
| 123 | p = ulong 0 |
| 124 | while lt p 8 bound 8 |
| 125 | k = write 1 dumpbuf p sub ulong 8 p |
| 126 | if le k 0 |
| 127 | exit 90 |
| 128 | end |
| 129 | p = add p ulong k |
| 130 | end |
| 131 | end |
| 132 | fn main to int |
| 133 | DUMP |
| 134 | case "$ret" in |
| 135 | bool) printf 'if %s %s\nir_dump ulong 1\nelse\nir_dump ulong 0\nend\n' "$fn" "$omargs" ;; |
| 136 | void) printf '%s %s\nir_dump ulong 0\n' "$fn" "$omargs" ;; |
| 137 | byte|short|int|long) printf 'ir_dump bitcast ulong long %s %s\n' "$fn" "$omargs" ;; |
| 138 | *) printf 'ir_dump ulong %s %s\n' "$fn" "$omargs" ;; |
| 139 | esac >> "$case_om" |
| 140 | printf 'ret 0\nend\n' >> "$case_om" |
| 141 | "$OMC" < "$case_om" > "$B/case-$i.native" 2> "$B/case-$i.compile.err" || |
| 142 | stop FAIL aarch64 "case $i ($fn $omargs): omc refused the native case program: $(tr '\n' ' ' < "$B/case-$i.compile.err")" |
| 143 | chmod +x "$B/case-$i.native" |
| 144 | set +e |
| 145 | timeout 10 "$B/case-$i.native" > "$B/case-$i.out" |
| 146 | rc=$? |
| 147 | set -e |
| 148 | printf '%s\n' "$rc" > "$B/case-$i.status" |
| 149 | printf 'kernel\t%s\t%s\t%s\t%s\n' "$fn" "$ret" "$typed" "$rc" >> "$B/parity-table.tsv" |
| 150 | done < "$B/plan.tsv" |
| 151 | say "PASS AArch64 native programs ran for $(wc -l < "$B/plan.tsv") cases" |
| 152 | objdump -d "$B/case-1.native" > "$B/kernel-aarch64.s" 2>/dev/null || : |
| 153 | |
| 154 | # 4. SPIR-V on PoCL CPU: pinned omega-mv parity.py compares every case bit for bit with AArch64. |
| 155 | set +e |
| 156 | "$PYTHON" "$T/parity.py" "$B" "$B/parity-table.tsv" "$B/opencl-host" case > "$B/parity.log" 2>&1 |
| 157 | rc=$? |
| 158 | set -e |
| 159 | tee -a "$B/check.log" < "$B/parity.log" |
| 160 | [ "$rc" = 0 ] || [ "$rc" = 1 ] || stop FAIL spirv-run "parity.py could not run every case (exit $rc): $(tail -n 1 "$B/parity.log")" |
| 161 | |
| 162 | # 5. Verdict against reference.py, job summary and receipt. |
| 163 | report |