|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright 2026 The ExecuTorch Authors. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +# RISC-V Phase 1 smoke test driver (pytorch/executorch#18991): |
| 8 | +# 1. Export a tiny model to a BundledProgram (.bpte) on the x86_64 host. |
| 9 | +# 2. Cross-compile executor_runner for riscv64 Linux glibc. |
| 10 | +# 3. Invoke the runner under qemu-user-static and grep its stdout for the |
| 11 | +# Test_result: PASS marker emitted by the bundled-IO comparison path. |
| 12 | + |
| 13 | +set -eu |
| 14 | + |
| 15 | +script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd) |
| 16 | +et_root_dir=$(realpath "${script_dir}/../..") |
| 17 | + |
| 18 | +build_only=false |
| 19 | +build_dir="${et_root_dir}/cmake-out" |
| 20 | +output_dir="${et_root_dir}/riscv_test" |
| 21 | +qemu="qemu-riscv64-static" |
| 22 | +qemu_timeout="600" |
| 23 | + |
| 24 | +usage() { |
| 25 | + cat <<EOF |
| 26 | +Usage: $(basename "$0") [options] |
| 27 | +Options: |
| 28 | + --build_only Only export and cross-compile; do not invoke QEMU |
| 29 | + --build_dir=<DIR> CMake build directory (default: ${build_dir}) |
| 30 | + --output_dir=<DIR> Directory for the exported .bpte (default: ${output_dir}) |
| 31 | + --qemu=<BIN> qemu-user binary (default: ${qemu}) |
| 32 | + --timeout=<SECONDS> Maximum QEMU runtime; matches run_fvp.sh --timelimit (default: ${qemu_timeout}) |
| 33 | + -h, --help Show this help |
| 34 | +EOF |
| 35 | +} |
| 36 | + |
| 37 | +for arg in "$@"; do |
| 38 | + case $arg in |
| 39 | + --build_only) build_only=true ;; |
| 40 | + --build_dir=*) build_dir="${arg#*=}" ;; |
| 41 | + --output_dir=*) output_dir="${arg#*=}" ;; |
| 42 | + --qemu=*) qemu="${arg#*=}" ;; |
| 43 | + --timeout=*) qemu_timeout="${arg#*=}" ;; |
| 44 | + -h|--help) usage; exit 0 ;; |
| 45 | + *) echo "Unknown option: $arg" >&2; usage; exit 1 ;; |
| 46 | + esac |
| 47 | +done |
| 48 | + |
| 49 | +mkdir -p "${output_dir}" |
| 50 | +bpte_path="${output_dir}/add_riscv.bpte" |
| 51 | + |
| 52 | +echo "[run.sh] Step 1/3: AOT export on host" |
| 53 | +python "${script_dir}/aot_riscv.py" --output "${bpte_path}" |
| 54 | + |
| 55 | +echo "[run.sh] Step 2/3: cross-compile executor_runner for riscv64-linux" |
| 56 | +cmake -S "${et_root_dir}" -B "${build_dir}" \ |
| 57 | + --preset riscv64-linux \ |
| 58 | + -DCMAKE_BUILD_TYPE=Release |
| 59 | +cmake --build "${build_dir}" -j"$(nproc)" --target executor_runner |
| 60 | + |
| 61 | +runner="${build_dir}/executor_runner" |
| 62 | +[[ -x "${runner}" ]] || { echo "[run.sh] runner not found at ${runner}" >&2; exit 1; } |
| 63 | + |
| 64 | +if file "${runner}" | grep -q "RISC-V"; then |
| 65 | + echo "[run.sh] runner is a RISC-V ELF: $(file -b "${runner}")" |
| 66 | +else |
| 67 | + echo "[run.sh] WARNING: ${runner} does not look like a RISC-V ELF" |
| 68 | + file "${runner}" |
| 69 | +fi |
| 70 | + |
| 71 | +if ${build_only}; then |
| 72 | + echo "[run.sh] --build_only set, skipping QEMU invocation" |
| 73 | + exit 0 |
| 74 | +fi |
| 75 | + |
| 76 | +echo "[run.sh] Step 3/3: run under ${qemu}" |
| 77 | +hash "${qemu}" 2>/dev/null || { |
| 78 | + echo "[run.sh] ${qemu} not found on PATH; install with examples/riscv/setup.sh" >&2 |
| 79 | + exit 1 |
| 80 | +} |
| 81 | + |
| 82 | +# QEMU_LD_PREFIX points qemu-user at the riscv64 sysroot so the dynamic |
| 83 | +# linker (ld-linux-riscv64-lp64d.so.1) referenced in the ELF resolves. |
| 84 | +export QEMU_LD_PREFIX="${QEMU_LD_PREFIX:-/usr/riscv64-linux-gnu}" |
| 85 | + |
| 86 | +log_file=$(mktemp) |
| 87 | +trap 'rm -f "${log_file}"' EXIT |
| 88 | + |
| 89 | +set +e |
| 90 | +timeout --signal=KILL "${qemu_timeout}" "${qemu}" "${runner}" \ |
| 91 | + --model_path="${bpte_path}" \ |
| 92 | + 2>&1 | tee "${log_file}" |
| 93 | +qemu_status=${PIPESTATUS[0]} |
| 94 | +set -e |
| 95 | + |
| 96 | +echo "[run.sh] qemu exit status: ${qemu_status}" |
| 97 | + |
| 98 | +if grep -q "Test_result: PASS" "${log_file}"; then |
| 99 | + echo "[run.sh] Bundled I/O check PASSED" |
| 100 | + exit 0 |
| 101 | +elif grep -q "Test_result: FAIL" "${log_file}"; then |
| 102 | + echo "[run.sh] Bundled I/O check FAILED" |
| 103 | + exit 1 |
| 104 | +else |
| 105 | + echo "[run.sh] No Test_result line found in QEMU output" |
| 106 | + exit 1 |
| 107 | +fi |
0 commit comments