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 <natebiggs@google.com>
Commit-Queue: Kevin Moore <kevmoo@google.com>
This commit is contained in:
Kevin Moore
2026-02-18 19:16:22 -08:00
committed by Commit Queue
parent c82716b216
commit 26fa4add14
6 changed files with 101 additions and 95 deletions
+60 -34
View File
@@ -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.dart> [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 <file.dart> <file.wasm>"
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
+2 -25
View File
@@ -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"
+37
View File
@@ -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
}
-36
View File
@@ -1,36 +0,0 @@
#!/usr/bin/env bash
set -e
set -x
# Usage: ./validate_wasm_test.sh <dart_file>
if [ -z "$1" ]; then
echo "Usage: $0 <dart_file>"
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"
+1
View File
@@ -6,6 +6,7 @@
import 'package:expect/expect.dart';
// ignore: import_internal_library
import 'dart:_wasm';
void main() {
+1
View File
@@ -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() {