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 / omega / test-barrier-contract.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | # The production barrier is dsb sy on AArch64 and a real fence on other hosts. |
| 4 | # A raw dsb sy outside an __aarch64__ region fails this check. A fixture with |
| 5 | # an unguarded dsb sy is the negative control. |
| 6 | set -eu |
| 7 | R=$(cd "$(dirname "$0")/../.." && pwd) |
| 8 | T=$(mktemp -d) |
| 9 | trap 'rm -rf "$T"' EXIT HUP INT TERM |
| 10 | fail() { echo "barrier-contract: $*" >&2; exit 1; } |
| 11 | |
| 12 | # shellcheck disable=SC2016 |
| 13 | scan() { |
| 14 | awk ' |
| 15 | function in_aarch64( i) { |
| 16 | for (i = 1; i <= sp; i++) if (st[i]) return 1 |
| 17 | return 0 |
| 18 | } |
| 19 | /^[[:space:]]*#[[:space:]]*if/ { |
| 20 | sp++ |
| 21 | st[sp] = ($0 ~ /__aarch64__/) ? 1 : 0 |
| 22 | next |
| 23 | } |
| 24 | /^[[:space:]]*#[[:space:]]*else/ { |
| 25 | if (sp >= 1) st[sp] = st[sp] ? 0 : 1 |
| 26 | next |
| 27 | } |
| 28 | /^[[:space:]]*#[[:space:]]*elif/ { |
| 29 | if (sp >= 1) st[sp] = ($0 ~ /__aarch64__/) ? 1 : 0 |
| 30 | next |
| 31 | } |
| 32 | /^[[:space:]]*#[[:space:]]*endif/ { |
| 33 | if (sp >= 1) sp-- |
| 34 | next |
| 35 | } |
| 36 | /__asm__ volatile\("dsb sy"/ { |
| 37 | if (!in_aarch64()) { |
| 38 | printf "%s:%d: dsb sy is outside #if defined(__aarch64__)\n", FILENAME, FNR |
| 39 | bad = 1 |
| 40 | } |
| 41 | } |
| 42 | END { exit bad ? 1 : 0 } |
| 43 | ' "$@" |
| 44 | } |
| 45 | |
| 46 | hdr=$R/nvrm/cpu_barrier.h |
| 47 | [ -f "$hdr" ] || fail "missing $hdr" |
| 48 | awk ' |
| 49 | /#if defined\(__aarch64__\)/ { arm=1; other=0; next } |
| 50 | /#else/ { if (arm) { arm=0; other=1 } next } |
| 51 | /#endif/ { arm=0; other=0; next } |
| 52 | arm && /dsb sy/ { saw_dsb=1 } |
| 53 | other && /__atomic_thread_fence\(__ATOMIC_SEQ_CST\)/ { saw_fence=1 } |
| 54 | other && /\(void\)0/ { saw_nop=1 } |
| 55 | END { |
| 56 | if (!saw_dsb) { print "AArch64 branch has no dsb sy"; exit 1 } |
| 57 | if (!saw_fence) { print "portable branch has no seq_cst fence"; exit 1 } |
| 58 | if (saw_nop) { print "portable branch contains (void)0"; exit 1 } |
| 59 | } |
| 60 | ' "$hdr" || fail "nvrm/cpu_barrier.h broke the production split" |
| 61 | grep -q 'nvrm_cpu_barrier();' "$R/src/omega_blackwell_engine.c" || fail "engine does not call nvrm_cpu_barrier" |
| 62 | grep -q 'nvrm_cpu_barrier();' "$R/src/omega_blackwell_submit.c" || fail "submit does not call nvrm_cpu_barrier" |
| 63 | |
| 64 | set -- |
| 65 | for dir in src nvrm m16; do |
| 66 | for f in $(find "$R/$dir" -type f \( -name '*.c' -o -name '*.h' \)); do |
| 67 | set -- "$@" "$f" |
| 68 | done |
| 69 | done |
| 70 | scan "$@" || fail "tree has an unguarded dsb sy" |
| 71 | |
| 72 | printf '%s\n' '__asm__ volatile("dsb sy" ::: "memory");' > "$T/unguarded.c" |
| 73 | if scan "$T/unguarded.c" >"$T/neg.log" 2>&1; then |
| 74 | fail "negative control accepted an unguarded dsb sy" |
| 75 | fi |
| 76 | grep -q 'outside #if defined(__aarch64__)' "$T/neg.log" || fail "negative control diagnostic" |
| 77 | printf '%s\n' '#if defined(__aarch64__)' '__asm__ volatile("dsb sy" ::: "memory");' '#endif' > "$T/guarded.c" |
| 78 | scan "$T/guarded.c" || fail "guarded fixture was rejected" |
| 79 | |
| 80 | cat > "$T/use.c" <<EOF |
| 81 | #include "cpu_barrier.h" |
| 82 | void use_barrier(void) { nvrm_cpu_barrier(); } |
| 83 | EOF |
| 84 | cc=${CC:-gcc} |
| 85 | "$cc" -std=gnu11 -Wall -Wextra -Werror -c -I"$R/nvrm" -o "$T/use.o" "$T/use.c" |
| 86 | objdump -d "$T/use.o" > "$T/use.s" |
| 87 | host=$(uname -m) |
| 88 | case "$host" in |
| 89 | x86_64) |
| 90 | grep -Eq 'mfence|[[:space:]]lock[[:space:]]' "$T/use.s" || fail "x86-64 object has no hardware fence" |
| 91 | if grep -q 'dsb' "$T/use.s"; then fail "x86-64 object contains dsb"; fi |
| 92 | echo "barrier codegen: PASS ($host seq_cst fence, no dsb)" |
| 93 | ;; |
| 94 | aarch64|arm64) |
| 95 | grep -Eq 'dsb[[:space:]]+sy' "$T/use.s" || fail "AArch64 object has no dsb sy" |
| 96 | echo "barrier codegen: PASS ($host dsb sy)" |
| 97 | ;; |
| 98 | *) |
| 99 | echo "barrier codegen: NOT_RUN (host is $host; source contract still checked)" |
| 100 | ;; |
| 101 | esac |
| 102 | |
| 103 | if command -v aarch64-linux-gnu-gcc >/dev/null 2>&1 && command -v aarch64-linux-gnu-objdump >/dev/null 2>&1; then |
| 104 | aarch64-linux-gnu-gcc -std=gnu11 -Wall -Wextra -Werror -c -I"$R/nvrm" -o "$T/use-arm.o" "$T/use.c" |
| 105 | aarch64-linux-gnu-objdump -d "$T/use-arm.o" > "$T/use-arm.s" |
| 106 | grep -Eq 'dsb[[:space:]]+sy' "$T/use-arm.s" || fail "AArch64 cross object has no dsb sy" |
| 107 | echo "barrier codegen: PASS (aarch64-linux-gnu-gcc cross compile emits dsb sy)" |
| 108 | elif [ "$host" != aarch64 ] && [ "$host" != arm64 ]; then |
| 109 | echo "AArch64 barrier codegen: NOT_RUN (aarch64-linux-gnu-gcc is not installed; source contract checked)" |
| 110 | fi |
| 111 | |
| 112 | echo "barrier-contract: PASS" |