[dart2wasm] Scripts for compiling and running benchmarks
Change-Id: I01d8653e60c9667aefcbcef7f1871f408a539482
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/271721
Reviewed-by: Jonas Termansen <sortie@google.com>
diff --git a/pkg/dart2wasm/tool/compile_benchmark b/pkg/dart2wasm/tool/compile_benchmark
new file mode 100755
index 0000000..807c7f5
--- /dev/null
+++ b/pkg/dart2wasm/tool/compile_benchmark
@@ -0,0 +1,42 @@
+#!/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.
+
+# Script to compile a benchmark using dart2wasm. Assumes the Dart repo's
+# directory structure.
+
+set -e
+
+function follow_links() {
+ file="$1"
+ while [ -h "$file" ]; do
+ # On Mac OS, readlink -f doesn't work.
+ file="$(readlink "$file")"
+ done
+ echo "$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)"
+
+# Locate build directory, containing executables, snapshots and platform dill.
+if [[ `uname` == 'Darwin' ]]; then
+ OUT_DIR="$SDK_DIR/xcodebuild"
+else
+ OUT_DIR="$SDK_DIR/out"
+fi
+DART_CONFIGURATION=${DART_CONFIGURATION:-ReleaseX64}
+BIN_DIR="$OUT_DIR/$DART_CONFIGURATION"
+
+DART2WASM="$SDK_DIR/sdk/bin/dart2wasm"
+BINARYEN="$BIN_DIR/wasm-opt"
+
+TEMPFILE="$(mktemp --suffix=.wasm)"
+
+"$DART2WASM" "$1" "$TEMPFILE"
+"$BINARYEN" -all -g --closed-world --nominal -tnh -O3 --gufa -O3 "$TEMPFILE" -o "$2"
diff --git a/pkg/dart2wasm/tool/run_benchmark b/pkg/dart2wasm/tool/run_benchmark
new file mode 100755
index 0000000..59ffb9b
--- /dev/null
+++ b/pkg/dart2wasm/tool/run_benchmark
@@ -0,0 +1,33 @@
+#!/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.
+
+# Script to run a benchmark compiled using dart2wasm. Assumes the Dart repo's
+# directory structure.
+
+set -e
+
+function follow_links() {
+ file="$1"
+ while [ -h "$file" ]; do
+ # On Mac OS, readlink -f doesn't work.
+ file="$(readlink "$file")"
+ done
+ echo "$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)"
+
+# Hardcoded to x64 Linux for now.
+D8="$SDK_DIR/third_party/d8/linux/x64/d8"
+D8_OPTIONS="--experimental-wasm-gc --experimental-wasm-stack-switching --experimental-wasm-type-reflection"
+
+RUN_WASM="$SDK_DIR/pkg/dart2wasm/bin/run_wasm.js"
+
+exec "$D8" $D8_OPTIONS "$RUN_WASM" -- "$@"