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 / tests / multitarget / run.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | # Portable-backend host gate (docs/ARCHITECTURE.md "Portable backend"). Everything under test that |
| 4 | # makes a decision is seeded Omega: backend/targets.om (target table, code-object loader checks), |
| 5 | # backend/spirv/emit.om (SPIR-V kernel builder), backend/spirv/conform.om (host reference and bit |
| 6 | # compare). This script only coordinates and compares them with independent oracles: |
| 7 | # spirv-val (SPIRV-Tools) every emitted module, plus corrupted copies that must fail |
| 8 | # llvm-mc + ld.lld + llvm-readelf ORACLE gfx1151 code object (backend/amd/vadd-gfx1151.s) |
| 9 | # hello_gpu --sim CUDA++'s own warp-simulator output digest for the same vadd |
| 10 | # PoCL / Mesa lavapipe CPU implementations that execute the SPIR-V (via the |
| 11 | # migration-pending C launcher tools/multitarget/spirv-host.c) |
| 12 | # It opens no GPU: the launcher pins CPU-only drivers and fails if any nvidia path is mapped or open. |
| 13 | # Results are host EXERCISED at most; every GPU row stays hardware NOT_RUN. |
| 14 | # |
| 15 | # usage: run.sh /abs/path/to/pinned-omc |
| 16 | # Environment (absolute paths; a missing tool makes its checks NOT_RUN, never PASS): |
| 17 | # SPIRV_VAL spirv-val LLVM_BIN dir with llvm-mc, llvm-readelf |
| 18 | # LD_LLD ld.lld HELLO_GPU build/hello_gpu (for --sim digest) |
| 19 | # CUDAPP_MIGRATION_PENDING_SPIRV_HOST=1 builds and runs the C launcher (default 0: run NOT_RUN) |
| 20 | # MT_CC (cc), MT_CFLAGS (extra -I for CL/ and vulkan/ headers), POCL_LIBRARY (absolute libpocl.so), |
| 21 | # LVP_ICD (lavapipe manifest), MT_OUT (output dir to keep; default a new temp dir) |
| 22 | # Exit: 0 MULTITARGET PASS (NOT_RUN checks listed); 1 FAIL; 64 usage; 69 pin refused; 74 I/O; |
| 23 | # 77 NOT_RUN (not Linux AArch64). |
| 24 | set -u |
| 25 | LC_ALL=C |
| 26 | export LC_ALL |
| 27 | |
| 28 | if [ "$(uname -s)" != Linux ] || [ "$(uname -m)" != aarch64 ]; then |
| 29 | echo 'NOT_RUN: the multitarget host gate needs Linux AArch64 and the pinned seeded omc' >&2 |
| 30 | exit 77 |
| 31 | fi |
| 32 | if [ "$#" -ne 1 ]; then echo 'usage: run.sh /absolute/path/to/pinned-omc' >&2; exit 64; fi |
| 33 | omc=$1 |
| 34 | case $omc in /*) ;; *) echo 'usage: omc path must be absolute' >&2; exit 64 ;; esac |
| 35 | # omc: omega-mv 369dbc96 seed/out/omc, equal to its committed seed/out/omc.sha256 (AGENTS.md pin). |
| 36 | omc_pin=2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f |
| 37 | digest() { d=$(sha256sum < "$1") || return 1; printf '%s\n' "${d%% *}"; } |
| 38 | [ "$(digest "$omc")" = "$omc_pin" ] || { echo "REFUSED: $omc digest differs from pin $omc_pin" >&2; exit 69; } |
| 39 | |
| 40 | root=$(CDPATH= cd -- "$(dirname -- "$0")/../.." && pwd) || exit 74 |
| 41 | out=${MT_OUT:-$(mktemp -d "${TMPDIR:-/tmp}/cudapp-multitarget.XXXXXX")} || exit 74 |
| 42 | mkdir -p "$out" || exit 74 |
| 43 | python3 -I "$root/tests/amd/receipt.py" write "$root" "$out" || exit 1 |
| 44 | echo "PIN omc $omc_pin" |
| 45 | echo "Outputs: $out" |
| 46 | |
| 47 | npass=0; nfail=0; nnr=0 |
| 48 | ok() { npass=$((npass + 1)); echo "PASS $*"; } |
| 49 | bad() { nfail=$((nfail + 1)); echo "FAIL $*"; } |
| 50 | notrun() { nnr=$((nnr + 1)); echo "NOT_RUN $*"; } |
| 51 | # expect RC NAME cmd...: the command must exit RC (stdout+stderr kept in $out/NAME.log) |
| 52 | expect() { |
| 53 | want=$1; name=$2; shift 2 |
| 54 | "$@" > "$out/$name.log" 2>&1 |
| 55 | got=$? |
| 56 | if [ "$got" -eq "$want" ]; then ok "$name exit=$got $(tail -n 1 "$out/$name.log")"; else bad "$name exit=$got want=$want $(tail -n 1 "$out/$name.log")"; fi |
| 57 | } |
| 58 | # poke FILE OFFSET OCTALBYTE: overwrite one byte in place |
| 59 | poke() { printf "\\$3" | dd of="$1" bs=1 seek="$2" conv=notrunc status=none; } |
| 60 | |
| 61 | # 1. Compile the three Omega units with the pinned omc. |
| 62 | for u in targets:backend/targets.om emit:backend/spirv/emit.om conform:backend/spirv/conform.om; do |
| 63 | name=${u%%:*}; src=$root/${u#*:} |
| 64 | timeout 120 "$omc" < "$src" > "$out/$name" 2> "$out/$name.compile.err" |
| 65 | rc=$? |
| 66 | if [ "$rc" -ne 0 ] || [ -s "$out/$name.compile.err" ] || [ ! -s "$out/$name" ]; then |
| 67 | cat "$out/$name.compile.err"; echo "FAIL compile $name exit $rc"; exit 1 |
| 68 | fi |
| 69 | chmod 700 "$out/$name" || exit 74 |
| 70 | echo "COMPILE $name src=$(digest "$src") bin=$(digest "$out/$name")" |
| 71 | done |
| 72 | T=$out/targets; E=$out/emit; C=$out/conform |
| 73 | |
| 74 | # 2. Target table: 22 rows; only nvidia-sm_121 SUPPORTED; every PLANNED row hardware NOT_RUN; |
| 75 | # host rows say not-a-GPU. The same checker must reject a table with one NOT_RUN turned into PASS. |
| 76 | check_table() { |
| 77 | rows=$(grep -v '^TARGET id vendor ' "$1" | grep -c '^TARGET [a-z]') |
| 78 | [ "$rows" -eq 22 ] || return 1 |
| 79 | [ "$(grep -c ' SUPPORTED ' "$1")" -eq 1 ] || return 1 |
| 80 | grep -q '^TARGET nvidia-sm_121 .* SUPPORTED ' "$1" || return 1 |
| 81 | [ "$(grep -c ' PLANNED NOT_RUN ' "$1")" -eq 19 ] || return 1 |
| 82 | [ "$(grep -c ' HOST-ONLY(not-a-GPU) ' "$1")" -eq 2 ] || return 1 |
| 83 | return 0 |
| 84 | } |
| 85 | "$T" doctor > "$out/doctor.txt" 2>&1 || bad "targets doctor exit $?" |
| 86 | if check_table "$out/doctor.txt"; then ok "table rows=22 supported=nvidia-sm_121 planned_not_run=19 host=2"; else bad table; fi |
| 87 | sed '0,/ PLANNED NOT_RUN /s// PLANNED PASS /' "$out/doctor.txt" > "$out/doctor-mutant.txt" |
| 88 | if check_table "$out/doctor-mutant.txt"; then bad "table checker accepted a row claiming PASS"; else ok "table-negative row claiming PASS rejected"; fi |
| 89 | |
| 90 | # 3. Emit the SPIR-V modules (and the planted-wrong mutants) from Omega. |
| 91 | for k in vadd fsum16; do for a in cl vk; do |
| 92 | "$E" $k $a > "$out/$k.$a.spv" || bad "emit $k $a" |
| 93 | "$E" $k $a mutant > "$out/$k.$a.mutant.spv" || bad "emit $k $a mutant" |
| 94 | echo "MODULE $k.$a.spv sha256=$(digest "$out/$k.$a.spv") bytes=$(wc -c < "$out/$k.$a.spv")" |
| 95 | done; done |
| 96 | |
| 97 | # 4. spirv-val on every module; corrupted copies must fail it. |
| 98 | if [ -n "${SPIRV_VAL:-}" ] && [ -x "$SPIRV_VAL" ]; then |
| 99 | echo "TOOL spirv-val $("$SPIRV_VAL" --version 2>&1 | head -n 1)" |
| 100 | for k in vadd fsum16; do for m in "" .mutant; do |
| 101 | expect 0 "spirv-val-$k.cl$m" "$SPIRV_VAL" --target-env opencl2.1 "$out/$k.cl$m.spv" |
| 102 | expect 0 "spirv-val-$k.vk$m" "$SPIRV_VAL" --target-env vulkan1.1 "$out/$k.vk$m.spv" |
| 103 | done; done |
| 104 | else |
| 105 | notrun "spirv-val: SPIRV_VAL not set to an executable" |
| 106 | fi |
| 107 | head -c $(( $(wc -c < "$out/vadd.vk.spv") - 4 )) "$out/vadd.vk.spv" > "$out/vadd.vk.trunc.spv" |
| 108 | cp "$out/vadd.vk.spv" "$out/vadd.vk.badmagic.spv" && poke "$out/vadd.vk.badmagic.spv" 0 000 |
| 109 | # first instruction's word count (high byte of word 5) set to 0x7f..: runs past the end |
| 110 | cp "$out/vadd.vk.spv" "$out/vadd.vk.overrun.spv" && poke "$out/vadd.vk.overrun.spv" 23 177 |
| 111 | if [ -n "${SPIRV_VAL:-}" ] && [ -x "$SPIRV_VAL" ]; then |
| 112 | for c in trunc badmagic overrun; do |
| 113 | if "$SPIRV_VAL" --target-env vulkan1.1 "$out/vadd.vk.$c.spv" > "$out/spirv-val-$c.log" 2>&1; then |
| 114 | bad "spirv-val accepted corrupted module ($c)"; else ok "spirv-val-negative $c rejected: $(head -n 1 "$out/spirv-val-$c.log")"; fi |
| 115 | done |
| 116 | fi |
| 117 | |
| 118 | # 5. Omega loader: accepts, and refuses every wrong format, corruption and entry. |
| 119 | for k in vadd fsum16; do |
| 120 | expect 0 "load-cl-$k" "$T" load SPIRV_CL "$out/$k.cl.spv" $k |
| 121 | expect 0 "load-vk-$k" "$T" load SPIRV_VK "$out/$k.vk.spv" $k |
| 122 | done |
| 123 | expect 21 load-neg-truncated "$T" load SPIRV_VK "$out/vadd.vk.trunc.spv" |
| 124 | expect 21 load-neg-overrun "$T" load SPIRV_VK "$out/vadd.vk.overrun.spv" |
| 125 | expect 20 load-neg-badmagic "$T" load SPIRV_VK "$out/vadd.vk.badmagic.spv" |
| 126 | expect 11 load-neg-aarch64 "$T" load AARCH64 "$out/vadd.vk.spv" |
| 127 | expect 22 load-neg-cl-as-vk "$T" load SPIRV_VK "$out/vadd.cl.spv" |
| 128 | expect 22 load-neg-vk-as-cl "$T" load SPIRV_CL "$out/vadd.vk.spv" |
| 129 | expect 23 load-neg-entry "$T" load SPIRV_VK "$out/vadd.vk.spv" nosuch |
| 130 | expect 23 load-neg-entry-prefix "$T" load SPIRV_CL "$out/fsum16.cl.spv" fsum |
| 131 | expect 11 load-neg-msl "$T" load MSL_SOURCE "$out/vadd.vk.spv" |
| 132 | expect 12 load-neg-cubin "$T" load CUBIN "$out/vadd.vk.spv" |
| 133 | expect 10 load-neg-unknown "$T" load PTX "$out/vadd.vk.spv" |
| 134 | expect 40 load-neg-spv-as-sm121 "$T" load SM121_RAW "$out/vadd.vk.spv" |
| 135 | head -c 32 /dev/zero > "$out/sm121-two.bin" |
| 136 | expect 0 load-sm121-shape "$T" load SM121_RAW "$out/sm121-two.bin" |
| 137 | head -c 20 /dev/zero > "$out/sm121-odd.bin" |
| 138 | expect 40 load-neg-sm121-odd "$T" load SM121_RAW "$out/sm121-odd.bin" |
| 139 | |
| 140 | # 6. Host reference (conform.om) and its cross-check against hello_gpu --sim. |
| 141 | "$C" in vadd "$out/a.bin" "$out/b.bin" && "$C" in fsum16 "$out/x.bin" \ |
| 142 | && "$C" ref vadd "$out/vadd.ref" && "$C" ref fsum16 "$out/fsum16.ref" || bad "conform inputs/reference" |
| 143 | echo "REFERENCE vadd sha256=$(digest "$out/vadd.ref") fsum16 sha256=$(digest "$out/fsum16.ref")" |
| 144 | if [ -n "${HELLO_GPU:-}" ] && [ -x "$HELLO_GPU" ]; then |
| 145 | hs=$("$HELLO_GPU" --sim | sed -n 's/^output sha256 //p') |
| 146 | if [ "$hs" = "$(digest "$out/vadd.ref")" ]; then ok "reference = hello_gpu --sim output sha256 $hs"; else bad "reference $(digest "$out/vadd.ref") != hello_gpu --sim $hs"; fi |
| 147 | else |
| 148 | notrun "hello_gpu --sim cross-check: HELLO_GPU not set" |
| 149 | fi |
| 150 | cp "$out/vadd.ref" "$out/vadd.planted.out" && poke "$out/vadd.planted.out" 4096 377 |
| 151 | expect 1 conform-neg-planted "$C" check vadd "$out/vadd.planted.out" |
| 152 | expect 0 conform-self "$C" check vadd "$out/vadd.ref" |
| 153 | |
| 154 | # 7. Execute on CPU implementations (migration-pending C launcher, default OFF). |
| 155 | if [ "${CUDAPP_MIGRATION_PENDING_SPIRV_HOST:-0}" != 1 ]; then |
| 156 | notrun "spirv-host runs on PoCL and lavapipe: CUDAPP_MIGRATION_PENDING_SPIRV_HOST is not 1 (migration-pending C, default OFF)" |
| 157 | else |
| 158 | cc=${MT_CC:-cc} |
| 159 | pocl=${POCL_LIBRARY:-}; lvp=${LVP_ICD:-/usr/share/vulkan/icd.d/lvp_icd.json} |
| 160 | build() { # NAME FORBID |
| 161 | $cc -std=c11 -O2 -Wall -Wextra -Werror -DCUDAPP_MIGRATION_PENDING_SPIRV_HOST=1 \ |
| 162 | -DMT_POCL_LIBRARY="\"$pocl\"" -DMT_LVP_ICD="\"$lvp\"" -DMT_FORBID="\"$2\"" ${MT_CFLAGS:-} \ |
| 163 | "$root/tools/multitarget/spirv-host.c" -o "$out/$1" -l:libOpenCL.so.1 -l:libvulkan.so.1 |
| 164 | } |
| 165 | if ! build spirv-host nvidia > "$out/spirv-host.build.log" 2>&1; then |
| 166 | cat "$out/spirv-host.build.log"; bad "spirv-host build" |
| 167 | else |
| 168 | H=$out/spirv-host |
| 169 | for a in cl vk; do |
| 170 | case $a in cl) [ -n "$pocl" ] || { notrun "cl runs: POCL_LIBRARY not set"; continue; } ;; esac |
| 171 | for m in "" .mutant; do |
| 172 | "$H" $a "$out/vadd.$a$m.spv" vadd 1048576 "$out/a.bin" "$out/b.bin" "$out/vadd.$a$m.out" > "$out/run-vadd.$a$m.log" 2>&1 || bad "run vadd $a$m exit $?" |
| 173 | "$H" $a "$out/fsum16.$a$m.spv" fsum16 65536 "$out/x.bin" "$out/fsum16.$a$m.out" > "$out/run-fsum16.$a$m.log" 2>&1 || bad "run fsum16 $a$m exit $?" |
| 174 | done |
| 175 | grep -h 'device=\|platform=' "$out/run-vadd.$a.log" |
| 176 | expect 0 "conform-vadd-$a" "$C" check vadd "$out/vadd.$a.out" |
| 177 | expect 0 "conform-fsum16-$a" "$C" check fsum16 "$out/fsum16.$a.out" |
| 178 | expect 1 "conform-neg-vadd-$a-mutant" "$C" check vadd "$out/vadd.$a.mutant.out" |
| 179 | expect 1 "conform-neg-fsum16-$a-mutant" "$C" check fsum16 "$out/fsum16.$a.mutant.out" |
| 180 | for k in vadd fsum16; do |
| 181 | if cmp -s "$out/$k.$a.out" "$out/$k.ref"; then ok "bytes $k.$a.out = $k.ref sha256=$(digest "$out/$k.ref")"; else bad "bytes $k.$a.out != $k.ref"; fi |
| 182 | done |
| 183 | done |
| 184 | # Isolation guard control: a copy forbidding the CPU driver it must load has to exit 9. |
| 185 | if build spirv-host-forbid-pocl libpocl > "$out/forbid.build.log" 2>&1 && build spirv-host-forbid-lvp vulkan_lvp >> "$out/forbid.build.log" 2>&1; then |
| 186 | [ -n "$pocl" ] && expect 9 isolation-neg-cl "$out/spirv-host-forbid-pocl" cl "$out/vadd.cl.spv" vadd 1048576 "$out/a.bin" "$out/b.bin" "$out/iso.out" |
| 187 | expect 9 isolation-neg-vk "$out/spirv-host-forbid-lvp" vk "$out/vadd.vk.spv" vadd 1048576 "$out/a.bin" "$out/b.bin" "$out/iso.out" |
| 188 | else |
| 189 | bad "isolation control build" |
| 190 | fi |
| 191 | fi |
| 192 | fi |
| 193 | |
| 194 | # 8. AMDGPU_HSACO: ORACLE gfx1151 code object (llvm-mc + ld.lld), Omega loader validation and refusals. |
| 195 | mc=${LLVM_BIN:-/usr/lib/llvm-18/bin}/llvm-mc; re=${LLVM_BIN:-/usr/lib/llvm-18/bin}/llvm-readelf; lld=${LD_LLD:-} |
| 196 | if [ -x "$mc" ] && [ -x "$re" ] && [ -n "$lld" ] && [ -x "$lld" ]; then |
| 197 | echo "TOOL llvm-mc $("$mc" --version | sed -n 's/.*LLVM version //p' | head -n 1); ld.lld $("$lld" --version)" |
| 198 | hs=$out/vadd-gfx1151.hsaco |
| 199 | if "$mc" -triple amdgcn-amd-amdhsa -mcpu=gfx1151 -filetype=obj "$root/backend/amd/vadd-gfx1151.s" -o "$out/vadd-gfx1151.o" \ |
| 200 | && "$lld" -shared "$out/vadd-gfx1151.o" -o "$hs"; then |
| 201 | echo "HSACO ORACLE sha256=$(digest "$hs") bytes=$(wc -c < "$hs")" |
| 202 | "$re" -h --notes --dyn-syms "$hs" > "$out/hsaco.readelf.txt" 2>&1 |
| 203 | if grep -q 'Flags: *0x4A, gfx1151' "$out/hsaco.readelf.txt" && grep -q 'amdhsa.target: *amdgcn-amd-amdhsa--gfx1151' "$out/hsaco.readelf.txt" \ |
| 204 | && grep -q 'OS/ABI: *AMDGPU - HSA' "$out/hsaco.readelf.txt" && grep -q ' vadd.kd$' "$out/hsaco.readelf.txt"; then |
| 205 | ok "llvm-readelf: gfx1151 flags 0x4A, HSA OSABI, NT_AMDGPU_METADATA target gfx1151, vadd.kd" |
| 206 | else bad "llvm-readelf did not show the expected gfx1151 code object"; fi |
| 207 | expect 0 load-hsaco "$T" load AMDGPU_HSACO "$hs" gfx1151 |
| 208 | expect 33 load-neg-hsaco-wrong-gfx "$T" load AMDGPU_HSACO "$hs" gfx1100 |
| 209 | expect 37 load-neg-hsaco-unknown-gfx "$T" load AMDGPU_HSACO "$hs" gfx9999 |
| 210 | expect 2 load-neg-hsaco-no-gfx "$T" load AMDGPU_HSACO "$hs" |
| 211 | expect 30 load-neg-spv-as-hsaco "$T" load AMDGPU_HSACO "$out/vadd.cl.spv" gfx1151 |
| 212 | expect 20 load-neg-hsaco-as-spv "$T" load SPIRV_VK "$hs" |
| 213 | expect 40 load-neg-hsaco-as-sm121 "$T" load SM121_RAW "$hs" |
| 214 | # Wrong note: the metadata target says gfx1150 while e_flags still says gfx1151. |
| 215 | t=$(grep -obUa 'amdgcn-amd-amdhsa--gfx1151' "$hs" | head -n 1 | cut -d: -f1) |
| 216 | cp "$hs" "$out/neg-note-target.hsaco" && poke "$out/neg-note-target.hsaco" $((t + 25)) 060 |
| 217 | if "$re" --notes "$out/neg-note-target.hsaco" 2>/dev/null | grep -q 'amdgcn-amd-amdhsa--gfx1150'; then |
| 218 | ok "oracle sees the planted note target gfx1150"; else bad "planted note target not visible to llvm-readelf"; fi |
| 219 | expect 36 load-neg-note-target "$T" load AMDGPU_HSACO "$out/neg-note-target.hsaco" gfx1151 |
| 220 | # Wrong note type: 32 (NT_AMDGPU_METADATA) becomes 33. |
| 221 | n=$(grep -obUa 'AMDGPU' "$hs" | head -n 1 | cut -d: -f1) |
| 222 | cp "$hs" "$out/neg-note-type.hsaco" && poke "$out/neg-note-type.hsaco" $((n - 4)) 041 |
| 223 | expect 34 load-neg-note-type "$T" load AMDGPU_HSACO "$out/neg-note-type.hsaco" gfx1151 |
| 224 | cp "$hs" "$out/neg-eflags.hsaco" && poke "$out/neg-eflags.hsaco" 48 101 |
| 225 | expect 33 load-neg-eflags "$T" load AMDGPU_HSACO "$out/neg-eflags.hsaco" gfx1151 |
| 226 | cp "$hs" "$out/neg-osabi.hsaco" && poke "$out/neg-osabi.hsaco" 7 000 |
| 227 | expect 31 load-neg-osabi "$T" load AMDGPU_HSACO "$out/neg-osabi.hsaco" gfx1151 |
| 228 | cp "$hs" "$out/neg-abi6.hsaco" && poke "$out/neg-abi6.hsaco" 8 004 |
| 229 | expect 31 load-neg-abi-v6 "$T" load AMDGPU_HSACO "$out/neg-abi6.hsaco" gfx1151 |
| 230 | head -c 600 "$hs" > "$out/neg-trunc.hsaco" |
| 231 | expect 34 load-neg-hsaco-truncated "$T" load AMDGPU_HSACO "$out/neg-trunc.hsaco" gfx1151 |
| 232 | else |
| 233 | bad "ORACLE hsaco assemble/link" |
| 234 | fi |
| 235 | else |
| 236 | notrun "AMDGPU_HSACO checks: llvm-mc/llvm-readelf under LLVM_BIN or LD_LLD missing" |
| 237 | fi |
| 238 | |
| 239 | echo "hardware NOT_RUN: every GPU target except the GB10 (no AMD, Intel, Apple, Qualcomm, Arm, Imagination, Broadcom or Vortex device on this host; PoCL and lavapipe are CPUs)" |
| 240 | if [ "$nfail" -ne 0 ]; then echo "MULTITARGET FAIL pass=$npass fail=$nfail not_run=$nnr"; exit 1; fi |
| 241 | python3 -I "$root/tests/amd/receipt.py" check "$root" "$out" || exit 1 |
| 242 | echo "MULTITARGET PASS pass=$npass fail=0 not_run=$nnr" |
| 243 | exit 0 |