From 26fa4add1448e6fbb921255a57647193d65efd6f Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Wed, 18 Feb 2026 19:16:22 -0800 Subject: [PATCH] dart2wasm: tool cleanup DRY'd up some helpers and logic Deleted validate_wasm_test.sh Added `--run` and `--write-temp` flags to compile_benchmark Change-Id: I666b25d13d0837a2b9f9fc40488882f1cfc6aff5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/478703 Reviewed-by: Nate Biggs Commit-Queue: Kevin Moore --- pkg/dart2wasm/tool/compile_benchmark | 94 +++++++++++++++--------- pkg/dart2wasm/tool/run_benchmark | 27 +------ pkg/dart2wasm/tool/utils.sh | 37 ++++++++++ pkg/dart2wasm/tool/validate_wasm_test.sh | 36 --------- tests/web/wasm/simd/simd_smoke_test.dart | 1 + tests/web/wasm/simd/simd_test.dart | 1 + 6 files changed, 101 insertions(+), 95 deletions(-) create mode 100644 pkg/dart2wasm/tool/utils.sh delete mode 100755 pkg/dart2wasm/tool/validate_wasm_test.sh diff --git a/pkg/dart2wasm/tool/compile_benchmark b/pkg/dart2wasm/tool/compile_benchmark index 5ecf3f39fe1..bb797259252 100755 --- a/pkg/dart2wasm/tool/compile_benchmark +++ b/pkg/dart2wasm/tool/compile_benchmark @@ -17,28 +17,12 @@ function follow_links() { echo "$file" } -function host_arch() { - # Use uname to determine the host architecture. - case `uname -m` in - x86_64) - echo "X64" - ;; - aarch64 | arm64 | armv8*) - echo "ARM64" - ;; - *) - echo "Unknown host architecture" `uname -m` >&2 - exit 1 - ;; - esac -} - # Unlike $0, $BASH_SOURCE points to the absolute path of this file. PROG_NAME="$(follow_links "$BASH_SOURCE")" # Handle the case where dart-sdk/bin has been symlinked to. PROG_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)" -SDK_DIR="$(cd "${PROG_DIR}/../../.." ; pwd -P)" +source "$PROG_DIR/utils.sh" # Locate build directory, containing executables, snapshots and platform dill. if [[ `uname` == 'Darwin' ]]; then @@ -46,7 +30,7 @@ if [[ `uname` == 'Darwin' ]]; then else OUT_DIR="$SDK_DIR/out" fi -HOST_ARCH="$(host_arch)" +HOST_ARCH="$(get_host_arch | tr '[:lower:]' '[:upper:]')" DART_CONFIGURATION=${DART_CONFIGURATION:-Release$HOST_ARCH} BIN_DIR="$OUT_DIR/$DART_CONFIGURATION" @@ -61,6 +45,8 @@ GENERATE_SOURCE_MAP=1 COMPILE_BENCHMARK_BASE_NAME="" PLATFORM_FILENAME="$BIN_DIR/dart2wasm_platform.dill" SNAPSHOT_NAME="dart2wasm" +RUN_BENCHMARK=0 +USE_TEMP_OUTPUT=0 # All arguments will be passed along to dart2wasm except specially recognized # flags. @@ -81,6 +67,23 @@ while [ $# -gt 0 ]; do shift ;; + --run) + RUN_BENCHMARK=1 + RUN_BENCHMARK_SHELL="d8" + shift + ;; + + --run=*) + RUN_BENCHMARK=1 + RUN_BENCHMARK_SHELL="${1#--run=}" + shift + ;; + + --temp-output) + USE_TEMP_OUTPUT=1 + shift + ;; + --js-compatibility) PLATFORM_FILENAME="$BIN_DIR/dart2wasm_js_compatibility_platform.dill" DART2WASM_ARGS+=("--js-compatibility") @@ -159,11 +162,29 @@ while [ $# -gt 0 ]; do esac done -if [ -z "$DART_FILE" -o -z "$OUTPUT_FILE" ]; then +if [ -z "$DART_FILE" ]; then + echo "Expected [file.wasm]" + exit 1 +fi + +if [ $USE_TEMP_OUTPUT -eq 1 ] && [ -n "$OUTPUT_FILE" ]; then + echo "Cannot specify both --temp-output and an explicit output file!" + exit 1 +fi + +if [ $USE_TEMP_OUTPUT -eq 0 ] && [ -z "$OUTPUT_FILE" ]; then echo "Expected " exit 1 fi +if [ $USE_TEMP_OUTPUT -eq 1 ]; then + BASENAME=$(basename "$DART_FILE" .dart) + TEMP_DIR=$(mktemp -d) + trap "rm -rf $TEMP_DIR" EXIT + OUTPUT_FILE="$TEMP_DIR/${BASENAME}.wasm" + echo "Compiling to temporary file: $OUTPUT_FILE" +fi + if [ -z "$PHASES" ]; then PHASES="cfe,tfa,codegen" fi @@ -175,7 +196,7 @@ DART2WASM_SRC="$SDK_DIR/pkg/dart2wasm/bin/dart2wasm.dart" function measure() { set +e - RESULT=$( { /usr/bin/time --format="\nMemory: %M KB, Time: %e s" $@; } 2>&1 ) + RESULT=$( { /usr/bin/time --format="\nMemory: %M KB, Time: %e s" "$@"; } 2>&1 ) EXIT=$? if [ $EXIT -ne 0 ]; then echo "Running $@ resulted in exit code $EXIT" @@ -195,31 +216,33 @@ function measure_size() { function run_if_binaryen_enabled() { if [ $RUN_BINARYEN -eq 1 ] && [[ $OUTPUT_FILE == *.wasm ]]; then - $@ + "$@" fi } COMPILER_SIZE=0 COMPILER_GZIP_SIZE=0 +if [ $RUN_SRC -eq 1 ]; then + BASE_CMD=("$DART" "${VM_ARGS[@]}" "$DART2WASM_SRC" "$LIBRARIES_JSON_ARG") +else + BASE_CMD=("$DART_AOT_RUNTIME" "${VM_ARGS[@]}" "$DART2WASM_AOT_SNAPSHOT" "$PLATFORM_ARG") +fi + function run_compiler() { - if [ $RUN_SRC -eq 1 ]; then - dart2wasm_command=("$DART" "${VM_ARGS[@]}" "$DART2WASM_SRC" "$LIBRARIES_JSON_ARG" "${DART2WASM_ARGS[@]}" "--phases=$PHASES" "$DART_FILE" "$OUTPUT_FILE") - else - dart2wasm_command=("$DART_AOT_RUNTIME" "${VM_ARGS[@]}" "$DART2WASM_AOT_SNAPSHOT" "$PLATFORM_ARG" "${DART2WASM_ARGS[@]}" "--phases=$PHASES" "$DART_FILE" "$OUTPUT_FILE") - fi + local dart2wasm_command=("${BASE_CMD[@]}" "${DART2WASM_ARGS[@]}" "--phases=$PHASES" "$DART_FILE" "$OUTPUT_FILE") if [ -n "$COMPILE_BENCHMARK_BASE_NAME" ]; then measure "${dart2wasm_command[@]}" COMPILER_TIME=$TIME COMPILER_MEMORY=$MEMORY - measure_size ${OUTPUT_FILE%.wasm}.mjs + measure_size "${OUTPUT_FILE%.wasm}.mjs" MJS_SIZE=$SIZE MJS_GZIP_SIZE=$GZIP_SIZE for OUTPUT_FILE in "${OUTPUT_FILE%.wasm}"*.wasm; do - measure_size $OUTPUT_FILE + measure_size "$OUTPUT_FILE" (( COMPILER_SIZE+=$SIZE )) (( COMPILER_GZIP_SIZE+=$GZIP_SIZE )) done @@ -234,17 +257,14 @@ BINARYEN_SIZE=0 BINARYEN_GZIP_SIZE=0 function run_binaryen() { - if [ $RUN_SRC -eq 1 ]; then - opt_command=("$DART" "${VM_ARGS[@]}" "$DART2WASM_SRC" "$LIBRARIES_JSON_ARG" "${DART2WASM_ARGS[@]}" "--phases=opt" "--wasm-opt=$BINARYEN" "$OUTPUT_FILE" "$OUTPUT_FILE") - else - opt_command=("$DART_AOT_RUNTIME" "${VM_ARGS[@]}" "$DART2WASM_AOT_SNAPSHOT" "$PLATFORM_ARG" "${DART2WASM_ARGS[@]}" "--phases=opt" "--wasm-opt=$BINARYEN" "$OUTPUT_FILE" "$OUTPUT_FILE") - fi + local opt_command=("${BASE_CMD[@]}" "${DART2WASM_ARGS[@]}" "--phases=opt" "--wasm-opt=$BINARYEN" "$OUTPUT_FILE" "$OUTPUT_FILE") + if [ -n "$COMPILE_BENCHMARK_BASE_NAME" ]; then # If we're measuring run each binaryen command sequentially. measure "${opt_command[@]}" BINARYEN_TIME=$(echo "$BINARYEN_TIME + $TIME" | bc) BINARYEN_MEMORY=$(($BINARYEN_MEMORY > $MEMORY ? $BINARYEN_MEMORY : $MEMORY )) - measure_size $OUTPUT_FILE + measure_size "$OUTPUT_FILE" BINARYEN_SIZE=$(echo "$BINARYEN_SIZE + $SIZE" | bc) BINARYEN_GZIP_SIZE=$(echo "$BINARYEN_GZIP_SIZE + $GZIP_SIZE" | bc) MAX_MEMORY=$(($COMPILER_MEMORY > $BINARYEN_MEMORY ? $COMPILER_MEMORY : $BINARYEN_MEMORY )) @@ -276,3 +296,9 @@ if [ -n "$COMPILE_BENCHMARK_BASE_NAME" ]; then echo "$COMPILE_BENCHMARK_BASE_NAME.MemoryUse.Dart2Wasm(MemoryUse): $COMPILER_MEMORY bytes" run_if_binaryen_enabled echo "$COMPILE_BENCHMARK_BASE_NAME.MemoryUse.Wasm2WasmOpt(MemoryUse): $BINARYEN_MEMORY bytes" fi + +if [ $RUN_BENCHMARK -eq 1 ]; then + echo "" + echo "RUNNING $OUTPUT_FILE" + "$PROG_DIR/run_benchmark" "--$RUN_BENCHMARK_SHELL" "$OUTPUT_FILE" +fi diff --git a/pkg/dart2wasm/tool/run_benchmark b/pkg/dart2wasm/tool/run_benchmark index ddbbeae005a..68e10c5713a 100755 --- a/pkg/dart2wasm/tool/run_benchmark +++ b/pkg/dart2wasm/tool/run_benchmark @@ -17,38 +17,15 @@ function follow_links() { echo "$file" } -function get_realpath() { - file="$1" - if [ -f "$file" ]; then - file="$(cd $(dirname "$file"); pwd -P)/$(basename "$file")" - fi - echo $(follow_links "$file") -} - # Unlike $0, $BASH_SOURCE points to the absolute path of this file. PROG_NAME="$(follow_links "$BASH_SOURCE")" # Handle the case where dart-sdk/bin has been symlinked to. PROG_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)" -SDK_DIR="$(cd "${PROG_DIR}/../../.." ; pwd -P)" +source "$PROG_DIR/utils.sh" RUN_WASM="$SDK_DIR/pkg/dart2wasm/bin/run_wasm.js" -function host_arch() { - # Use uname to determine the host architecture. - case `uname -m` in - x86_64) - echo "x64" - ;; - aarch64 | arm64 | armv8*) - echo "arm64" - ;; - *) - echo "Unsupported host architecture" `uname -m` >&2 - exit 1 - ;; - esac -} function host_os() { if [[ `uname` == 'Darwin' ]]; then @@ -58,7 +35,7 @@ function host_os() { fi } -D8_BIN="$SDK_DIR/third_party/d8/$(host_os)/$(host_arch)/d8" +D8_BIN="$SDK_DIR/third_party/d8/$(host_os)/$(get_host_arch)/d8" JSSHELL_BIN="$SDK_DIR/third_party/firefox_jsshell/js" JSC_BIN="$SDK_DIR/third_party/jsc/jsc" diff --git a/pkg/dart2wasm/tool/utils.sh b/pkg/dart2wasm/tool/utils.sh new file mode 100644 index 00000000000..9ffa7bda643 --- /dev/null +++ b/pkg/dart2wasm/tool/utils.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +# Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file +# for details. All rights reserved. Use of this source code is governed by a +# BSD-style license that can be found in the LICENSE file. + +function follow_links() { + file="$1" + while [ -h "$file" ]; do + # On Mac OS, readlink -f doesn't work. + file="$(readlink "$file")" + done + echo "$file" +} + +function get_realpath() { + file="$1" + if [ -f "$file" ]; then + file="$(cd "$(dirname "$file")"; pwd -P)/$(basename "$file")" + fi + echo "$(follow_links "$file")" +} + +# Unlike $0, $BASH_SOURCE points to the absolute path of this file. +UTILS_SCRIPT="$(follow_links "${BASH_SOURCE[0]}")" +UTILS_DIR="$(cd "${UTILS_SCRIPT%/*}" ; pwd -P)" +SDK_DIR="$(cd "${UTILS_DIR}/../../.." ; pwd -P)" + +function get_host_arch() { + case `uname -m` in + x86_64) echo "x64" ;; + aarch64 | arm64 | armv8*) echo "arm64" ;; + *) + echo "Unsupported host architecture" `uname -m` >&2 + exit 1 + ;; + esac +} diff --git a/pkg/dart2wasm/tool/validate_wasm_test.sh b/pkg/dart2wasm/tool/validate_wasm_test.sh deleted file mode 100755 index 6f66490a917..00000000000 --- a/pkg/dart2wasm/tool/validate_wasm_test.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env bash -set -e -set -x - -# Usage: ./validate_wasm_test.sh -if [ -z "$1" ]; then - echo "Usage: $0 " - exit 1 -fi - -INPUT_FILE="$1" -# Get absolute path of input file -if [[ "$INPUT_FILE" != /* ]]; then - INPUT_FILE="$(pwd)/$INPUT_FILE" -fi - -BASENAME=$(basename "$INPUT_FILE" .dart) -OUTPUT_WASM="${INPUT_FILE%.dart}.wasm" - -# Resolve script directory to handle running from anywhere -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -# Assuming script is in pkg/dart2wasm/tool -DART2WASM_TOOL_DIR="$SCRIPT_DIR" - -# Compile -"$DART2WASM_TOOL_DIR/compile_benchmark" \ - --src \ - -O0 \ - --extra-compiler-option=--enable-experimental-wasm-interop \ - "$INPUT_FILE" \ - "$OUTPUT_WASM" - -# Run -"$DART2WASM_TOOL_DIR/run_benchmark" \ - --d8 \ - "$OUTPUT_WASM" diff --git a/tests/web/wasm/simd/simd_smoke_test.dart b/tests/web/wasm/simd/simd_smoke_test.dart index 3b017e3dda4..6c6fd82230b 100644 --- a/tests/web/wasm/simd/simd_smoke_test.dart +++ b/tests/web/wasm/simd/simd_smoke_test.dart @@ -6,6 +6,7 @@ import 'package:expect/expect.dart'; +// ignore: import_internal_library import 'dart:_wasm'; void main() { diff --git a/tests/web/wasm/simd/simd_test.dart b/tests/web/wasm/simd/simd_test.dart index 0477a833673..2157a2a39a9 100644 --- a/tests/web/wasm/simd/simd_test.dart +++ b/tests/web/wasm/simd/simd_test.dart @@ -5,6 +5,7 @@ // dart2wasmOptions=--extra-compiler-option=--enable-experimental-wasm-interop import 'package:expect/expect.dart'; +// ignore: import_internal_library import 'dart:_wasm'; void main() {