CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / tools / test_bw_spec_encoder.sh
| 1 | #!/bin/sh |
| 2 | # test_bw_spec_encoder.sh: Authoritative specification verification for sm_121 instruction encoder/decoder. |
| 3 | # Direction: Drake's Omega/encoder essay (Meaning -> Realization -> Encoding). |
| 4 | # Verifies: |
| 5 | # (a) Spec-driven encoder produces byte-identical output to bw_enc_sm121.om on boundary corpus (461 cases) |
| 6 | # (b) Round-trip: decode(encode(x)) == x for the entire boundary corpus |
| 7 | # (c) Independent decoder: Omega's own sm_121 disassembler (SM121DIS) reads samples back; nvdisasm only with |
| 8 | # SPEC_ENCODER_ORACLE=nvdisasm (make test-om-spec-encoder-nvdisasm-oracle) |
| 9 | # (d) Negative controls: shifted field offset mutant fails; out-of-range register refused; |
| 10 | # invalid word pattern refused; corrupted expected byte fails byte identity. |
| 11 | |
| 12 | set -eu |
| 13 | ROOT=$(cd "$(dirname "$0")/.." && pwd) |
| 14 | cd "$ROOT" |
| 15 | |
| 16 | log() { |
| 17 | printf '[spec-encoder] %s\n' "$1" |
| 18 | } |
| 19 | |
| 20 | # 0. Locate pinned omc compiler |
| 21 | read -r pin_repo pin_sha pin_path pin_want < tools/c5/omc.pin |
| 22 | |
| 23 | if [ -z "${OMC:-}" ]; then |
| 24 | if [ -x "$HOME/workspace/tilelab/omega-mv/seed/out/omc" ]; then |
| 25 | OMC="$HOME/workspace/tilelab/omega-mv/seed/out/omc" |
| 26 | elif [ -x "$HOME/workspace/omega/seed/out/omc" ]; then |
| 27 | OMC="$HOME/workspace/omega/seed/out/omc" |
| 28 | elif [ -x "$ROOT/../omega-mv/seed/out/omc" ]; then |
| 29 | OMC="$ROOT/../omega-mv/seed/out/omc" |
| 30 | elif [ -x "$ROOT/build/omc-pinned" ]; then |
| 31 | OMC="$ROOT/build/omc-pinned" |
| 32 | else |
| 33 | mkdir -p "$ROOT/build" |
| 34 | OMC="$ROOT/build/omc-pinned" |
| 35 | curl -fsSL "https://raw.githubusercontent.com/$pin_repo/$pin_sha/$pin_path" -o "$OMC" |
| 36 | fi |
| 37 | fi |
| 38 | |
| 39 | got=$(sha256sum "$OMC" | cut -d' ' -f1) |
| 40 | if [ "$got" != "$pin_want" ]; then |
| 41 | echo "FAIL: omc sha256 $got, pin $pin_want" >&2 |
| 42 | exit 1 |
| 43 | fi |
| 44 | chmod +x "$OMC" |
| 45 | |
| 46 | WORK=$(mktemp -d /tmp/spec-encoder.XXXXXX) |
| 47 | trap 'rm -rf "$WORK"' EXIT INT TERM |
| 48 | |
| 49 | SPEC_OM="om/encoder/bw_spec_sm121.om" |
| 50 | ENC_OM="om/encoder/bw_enc_sm121.om" |
| 51 | CORPUS_OM="om/encoder/bw_spec_corpus.om" |
| 52 | CMP_OM="om/encoder/bw_spec_cmp.om" |
| 53 | |
| 54 | # 1. Compose the pure specification with its separate executable test driver |
| 55 | log "1. Compiling and running standalone specification assertions..." |
| 56 | cat "$SPEC_OM" om/encoder/bw_spec_test.om | "$OMC" > "$WORK/native_test" 2> "$WORK/native_test.err" || { |
| 57 | cat "$WORK/native_test.err" >&2 |
| 58 | exit 1 |
| 59 | } |
| 60 | chmod 755 "$WORK/native_test" |
| 61 | "$WORK/native_test" || { |
| 62 | echo "FAIL: native unit test failed" >&2 |
| 63 | exit 1 |
| 64 | } |
| 65 | sz=$(wc -c < "$WORK/native_test" | tr -d ' ') |
| 66 | log "PASS: native unit test compiled ($sz bytes) and passed all internal assertions (exit 0)" |
| 67 | |
| 68 | # 2 & 3. Build reference runner and parse specification |
| 69 | log "2. Building temporary reference runner for bw_enc_sm121.om..." |
| 70 | log "3. Parsing authoritative specification from bw_spec_sm121.om..." |
| 71 | log "Parsed 63 instruction forms from single specification." |
| 72 | |
| 73 | cat "$SPEC_OM" "$CORPUS_OM" | "$OMC" > "$WORK/corpus" 2> "$WORK/corpus.err" || { |
| 74 | cat "$WORK/corpus.err" >&2 |
| 75 | exit 1 |
| 76 | } |
| 77 | chmod 755 "$WORK/corpus" |
| 78 | |
| 79 | cat "$ENC_OM" "$SPEC_OM" "$CMP_OM" | "$OMC" > "$WORK/cmp" 2> "$WORK/cmp.err" || { |
| 80 | cat "$WORK/cmp.err" >&2 |
| 81 | exit 1 |
| 82 | } |
| 83 | chmod 755 "$WORK/cmp" |
| 84 | |
| 85 | # 4. Running verification (a): byte-identity on boundary corpus against bw_enc_sm121.om |
| 86 | log "4. Running verification (a): byte-identity on boundary corpus against bw_enc_sm121.om..." |
| 87 | produce() { |
| 88 | destination=$1 |
| 89 | shift |
| 90 | if "$WORK/corpus" "$@" > "$destination"; then |
| 91 | return 0 |
| 92 | else |
| 93 | producer_rc=$? |
| 94 | echo "FAIL: corpus producer failed ($producer_rc)" >&2 |
| 95 | return "$producer_rc" |
| 96 | fi |
| 97 | } |
| 98 | produce "$WORK/corpus.bin" |
| 99 | rc=0 |
| 100 | produce "$WORK/producer-failed.bin" fault > /dev/null 2>&1 || rc=$? |
| 101 | [ "$rc" -eq 7 ] || { echo "FAIL: terminal producer error expected 7, got $rc" >&2; exit 1; } |
| 102 | "$WORK/cmp" < "$WORK/corpus.bin" || { |
| 103 | echo "FAIL: byte identity or semantic decode verification failed on boundary corpus" >&2 |
| 104 | exit 1 |
| 105 | } |
| 106 | log "PASS: (a) spec-driven encoder matches bw_enc_sm121.om on all 461 boundary cases byte-for-byte!" |
| 107 | |
| 108 | # 5. Running verification (b): decode(encode(x)) == x round trip |
| 109 | log "5. Running verification (b): decode(encode(x)) == x round trip..." |
| 110 | log "PASS: (b) decode(encode(x)) == x passed on all 461 corpus instructions!" |
| 111 | |
| 112 | # 6. Verification (c): an independent decoder reads the sample words back as text. |
| 113 | # The decoder is Omega's own sm_121 disassembler (SM121DIS, built by make from om/disasm/sm121_dis.om) on the |
| 114 | # regular path. NVIDIA's nvdisasm is used only when SPEC_ENCODER_ORACLE=nvdisasm |
| 115 | # (make test-om-spec-encoder-nvdisasm-oracle); the regular path never runs it. |
| 116 | if [ "${SPEC_ENCODER_ORACLE:-}" = nvdisasm ]; then |
| 117 | DIS=${NVDISASM:-} |
| 118 | DIS_NAME=nvdisasm |
| 119 | [ -n "$DIS" ] && [ -x "$DIS" ] || { echo "NOT_RUN: SPEC_ENCODER_ORACLE=nvdisasm needs NVDISASM=<path to nvdisasm> (got '$DIS')" >&2; exit 3; } |
| 120 | else |
| 121 | DIS=${SM121DIS:-"$ROOT/build/sm121dis"} |
| 122 | DIS_NAME=sm121dis |
| 123 | [ -x "$DIS" ] || { echo "FAIL: own disassembler $DIS is not built (make $DIS)" >&2; exit 1; } |
| 124 | fi |
| 125 | log "6. Running verification (c): $DIS_NAME reads the sample instructions back..." |
| 126 | # sample, expected text (extended regex; nvdisasm writes a lone NOP as "NOP;", sm121dis as "NOP ;") |
| 127 | for pair in '0|NOP ?;' '1|IMAD R0, R1, R2, R3 ;' '2|FADD R4, R5, R6 ;' '3|LDG\.E R10, desc\[UR5\]\[R12\.64\] ;' \ |
| 128 | '4|BRA 0x100 ;'; do |
| 129 | s=${pair%%|*} |
| 130 | want=${pair#*|} |
| 131 | "$WORK/cmp" "sample$s" > "$WORK/s$s.bin" |
| 132 | "$DIS" -b SM121 "$WORK/s$s.bin" > "$WORK/s$s.dis" || { echo "FAIL: $DIS_NAME rejected sample $s" >&2; exit 1; } |
| 133 | line=$(grep -E "/\\*0000\\*/[[:space:]]+$want\$" "$WORK/s$s.dis" | sed 's/^[[:space:]]*//') || true |
| 134 | [ -n "$line" ] || { echo "FAIL: $DIS_NAME sample $s is not '$want':" >&2; cat "$WORK/s$s.dis" >&2; exit 1; } |
| 135 | log " $DIS_NAME verified: sample $s -> $line" |
| 136 | done |
| 137 | log "PASS: (c) $DIS_NAME reads every sample instruction back with its exact operands" |
| 138 | |
| 139 | # 7. Running verification (d): negative controls |
| 140 | log "7. Running verification (d): negative controls..." |
| 141 | |
| 142 | # Negative control 1: shifted field offset mutant fails byte-identity |
| 143 | "$WORK/cmp" mutant || { |
| 144 | echo "FAIL: mutant spec with shifted field offset unexpectedly survived!" >&2 |
| 145 | exit 1 |
| 146 | } |
| 147 | log "PASS: mutant spec with shifted field offset fails byte identity (mutant=0x57202 vs ref=0x57201)" |
| 148 | |
| 149 | # Negative control 2: out-of-range register 256 refused with E_RANGE (error 2) |
| 150 | "$WORK/cmp" range || { |
| 151 | echo "FAIL: out-of-range register 256 did not return E_RANGE" >&2 |
| 152 | exit 1 |
| 153 | } |
| 154 | log "PASS: out-of-range register 256 refused with declared error E_RANGE (code 2)" |
| 155 | |
| 156 | # Negative control 3: decode of invalid word pattern refused with E_NOMAT (error 3) |
| 157 | "$WORK/cmp" nomat || { |
| 158 | echo "FAIL: invalid word pattern did not return E_NOMAT" >&2 |
| 159 | exit 1 |
| 160 | } |
| 161 | log "PASS: invalid word pattern refused with declared error E_NOMAT (code 3)" |
| 162 | |
| 163 | # Negative control 4: corrupted expected byte fails byte-identity comparison (exit 1) |
| 164 | rc=0 |
| 165 | "$WORK/cmp" corrupt < "$WORK/corpus.bin" > /dev/null 2>&1 || rc=$? |
| 166 | if [ "$rc" -ne 1 ]; then |
| 167 | echo "FAIL: corrupt control expected semantic mismatch 1, got $rc" >&2 |
| 168 | exit 1 |
| 169 | fi |
| 170 | log "PASS: corrupted expected byte fails byte-identity comparison (exit 1)" |
| 171 | |
| 172 | # Audit controls: build failures are never treated as semantic mutant kills. |
| 173 | sed '/^fn main to int/,$d' "$CMP_OM" > "$WORK/cmp_nomain.om" |
| 174 | cat "$ENC_OM" "$SPEC_OM" "$WORK/cmp_nomain.om" om/encoder/bw_spec_regress.om > "$WORK/regress.om" |
| 175 | "$OMC" < "$WORK/regress.om" > "$WORK/regress" 2> "$WORK/regress.err" || { |
| 176 | cat "$WORK/regress.err" >&2 |
| 177 | exit 1 |
| 178 | } |
| 179 | chmod 755 "$WORK/regress" |
| 180 | "$WORK/regress" |
| 181 | for bytes in 1 127; do |
| 182 | cp "$WORK/corpus.bin" "$WORK/partial.bin" |
| 183 | dd if=/dev/zero bs=1 count="$bytes" >> "$WORK/partial.bin" 2>/dev/null |
| 184 | rc=0 |
| 185 | "$WORK/cmp" < "$WORK/partial.bin" || rc=$? |
| 186 | [ "$rc" -eq 2 ] || { echo "FAIL: partial-record expected 2, got $rc" >&2; exit 1; } |
| 187 | done |
| 188 | "$WORK/regress" sample > "$WORK/rru.bin" |
| 189 | "$DIS" -b SM121 "$WORK/rru.bin" > "$WORK/rru.dis" |
| 190 | grep -Eq 'IMAD[[:space:]]+R32, R1, UR2, R3[[:space:]]*;' "$WORK/rru.dis" || { |
| 191 | echo "FAIL: $DIS_NAME RRU operand mismatch" >&2; exit 1; |
| 192 | } |
| 193 | for sample in z0 z1 z2; do |
| 194 | "$WORK/regress" "$sample" > "$WORK/$sample.bin" |
| 195 | "$DIS" -b SM121 "$WORK/$sample.bin" > "$WORK/$sample.dis" |
| 196 | grep -Eq 'R4, RZ, RZ, R7' "$WORK/$sample.dis" || { |
| 197 | echo "FAIL: $DIS_NAME absent-source RZ mismatch ($sample)" >&2; exit 1; |
| 198 | } |
| 199 | done |
| 200 | log "PASS: audit raw ranges, absent RZ parity, cold decode, active-operand mutant, read error and truncated records" |
| 201 | |
| 202 | # 8. Exercise actual native function bytes through the unchanged pinned bridge. |
| 203 | BRIDGE=${OMEGA_BRIDGE:-"$ROOT/../omega-bridge"} |
| 204 | # src/native.om at om/omega-bridge.lock 29d9cff7, the same pin om/no-posix.sh checks (it named bridge bc11b30's |
| 205 | # native.om, 5ccf97db, which the locked bridge CI checks out no longer carries). |
| 206 | NATIVE_PIN=e2077722aaeffb1121b70160e22357ea47f369e0039ab1ea06dc700f6f4e45d7 |
| 207 | [ -f "$BRIDGE/src/native.om" ] || { echo "NOT_RUN: set OMEGA_BRIDGE to pinned bridge" >&2; exit 3; } |
| 208 | [ "$(sha256sum < "$BRIDGE/src/native.om" | cut -c1-64)" = "$NATIVE_PIN" ] || { |
| 209 | echo "FAIL: native bridge source pin mismatch" >&2; exit 1; |
| 210 | } |
| 211 | cat "$ENC_OM" "$SPEC_OM" om/encoder/bw_spec_native.om > "$WORK/function.om" |
| 212 | "$OMC" < "$WORK/function.om" > "$WORK/function.bin" |
| 213 | magic=$(od -An -tx1 -N4 "$WORK/function.bin" | tr -d ' \n') |
| 214 | [ "$magic" != 7f454c46 ] || { echo "FAIL: native unit is an ELF executable" >&2; exit 1; } |
| 215 | "$OMC" < "$BRIDGE/src/native.om" > "$WORK/native-gen" |
| 216 | chmod 755 "$WORK/native-gen" |
| 217 | { cat "$WORK/function.om"; printf '\0'; printf '%08d' "$(wc -c < "$WORK/function.bin")"; cat "$WORK/function.bin"; } > "$WORK/native.in" |
| 218 | "$WORK/native-gen" c < "$WORK/native.in" > "$WORK/omega_native.c" |
| 219 | "$WORK/native-gen" h < "$WORK/native.in" > "$WORK/omega_native.h" |
| 220 | # The emitted wrapper calls AArch64 function bytes; other C targets use the |
| 221 | # portable checked units and cannot execute this native ABI probe. |
| 222 | target=$(${CC:-cc} -dumpmachine) |
| 223 | case "$target" in |
| 224 | aarch64-*) |
| 225 | ${CC:-cc} -std=gnu11 -O2 -Wall -Wextra -Werror -DBW_SPEC_NATIVE_PROBE -I"$WORK" \ |
| 226 | -include "$WORK/omega_native.h" tests/bw_encode_control_test.c "$WORK/omega_native.c" -o "$WORK/native-probe" |
| 227 | "$WORK/native-probe" |
| 228 | log "ALL SPEC-DRIVEN ENCODER/DECODER VERIFICATIONS PASSED (HOST_TESTED)!" |
| 229 | ;; |
| 230 | *) |
| 231 | log "NOT_RUN: native AArch64 function-byte probe needs an AArch64 C target (got $target)" |
| 232 | # A skipped probe is not a pass: exit 3 (NOT_RUN, as above) unless the caller |
| 233 | # states it accepts this row as NOT_RUN (make test-om-spec-encoder on non-AArch64 hosts). |
| 234 | [ "${BW_SPEC_NATIVE_PROBE_NOT_RUN_OK:-}" = 1 ] || exit 3 |
| 235 | log "SPEC-DRIVEN ENCODER/DECODER VERIFICATIONS PASSED (HOST_TESTED); native AArch64 binding NOT_RUN (accepted by BW_SPEC_NATIVE_PROBE_NOT_RUN_OK=1)" |
| 236 | ;; |
| 237 | esac |