Skip to content

Commit f7e2745

Browse files
cfallinGuy Bedford
andauthored
Support weval-based ahead-of-time compilation of JavaScript. (#91)
* Support weval-based ahead-of-time compilation of JavaScript. When the `WEVAL` option is turned on (`cmake -DWEVAL=ON`), this PR adds: - Integration to the CMake machinery to fetch a PBL+weval-ified version of SpiderMonkey artifacts; - Likewise, to fetch weval binaries; - A rule to pre-build a compilation cache of IC bodies specific to the StarlingMonkey build, so weval can use this cache and spend time only on user-provided code on first run; - Integration in `componentize.sh`. When built with: ``` $ mkdir build/; cd build/ $ cmake .. -DCMAKE_BUILD_TYPE=Release -DUSE_WASM_OPT=OFF -DWEVAL=ON $ make ``` We can then do: ``` $ build/componentize.sh file.js --aot -o file.wasm $ wasmtime serve -S cli=y file.wasm ``` Using the Richards Octane benchmark adapted slightly with a `main()` for the HTTP server world [1], I get the following results: ``` % build/componentize.sh richards.js --aot -o weval.wasm Componentizing richards.js into weval.wasm [ verbose weval progress output ] % wasmtime serve -S cli=y weval.wasm Serving HTTP on http://0.0.0.0:8080/ stdout [0] :: Log: Richards: 676 stdout [0] :: Log: ---- stdout [0] :: Log: Score (version 9): 676 % wasmtime serve -S cli=y base.wasm Serving HTTP on http://0.0.0.0:8080/ stdout [0] :: Log: Richards: 189 stdout [0] :: Log: ---- stdout [0] :: Log: Score (version 9): 189 ``` [1]: https://gist.github.com/cfallin/4b18da12413e93f7e88568a92d09e4b7 * support weval testing * add ci workflow for Weval test suite * Update weval, resolving two test failures. This commit updates to weval v0.2.7 which has no output by default, allowing the expected-failure tests checking syntax error messages to pass; we now pass 9/10 integration tests. It also updates the flags on `componentize.sh`: a `--verbose` flag to allow the user to see weval progress messages (perhaps useful if the build is taking a long time, or to see the stats on function specializations); and removal of the `--aot` flag, because there is only one valid setting for a build configuration and it is not baked into the shell script automatically. * Update SpiderMonkey version to include two fixes (bytecodealliance/spidermonkey-wasi-embedding#19). --------- Co-authored-by: Guy Bedford <gbedford@fastly.com>
1 parent 1a1dc3d commit f7e2745

9 files changed

Lines changed: 101 additions & 9 deletions

File tree

.github/workflows/main.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,34 @@ jobs:
4444
run: |
4545
CTEST_OUTPUT_ON_FAILURE=1 ctest --test-dir cmake-build-debug -j4
4646
47+
test-weval:
48+
name: Test Weval
49+
strategy:
50+
fail-fast: false
51+
matrix:
52+
os: [ubuntu-latest]
53+
runs-on: ${{ matrix.os }}
54+
steps:
55+
- uses: actions/checkout@v2
56+
57+
- name: Install Rust 1.77.1
58+
run: |
59+
rustup toolchain install 1.77.1
60+
rustup target add wasm32-wasi --toolchain 1.77.1
61+
62+
- uses: actions/setup-node@v2
63+
with:
64+
node-version: 'lts/*'
65+
66+
- name: Build StarlingMonkey
67+
run: |
68+
cmake -S . -B cmake-build-weval -DCMAKE_BUILD_TYPE=Release -DUSE_WASM_OPT=OFF -DWEVAL=ON
69+
cmake --build cmake-build-weval --parallel 4 --target all integration-test-server
70+
71+
- name: StarlingMonkey E2E & Integration Tests
72+
run: |
73+
CTEST_OUTPUT_ON_FAILURE=1 ctest --test-dir cmake-build-weval -j4
74+
4775
wpt:
4876
name: Web Platform Tests
4977
strategy:

CMakeLists.txt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ include("init-corrosion")
2828
include("wasm-tools")
2929
include("binaryen")
3030
include("wizer")
31+
include("weval")
3132
include("wasmtime")
3233

3334
include("fmt")
@@ -90,9 +91,28 @@ endif()
9091

9192
target_link_libraries(starling.wasm PRIVATE host_api extension_api builtins spidermonkey rust-url)
9293

94+
# build a compilation cache of ICs
95+
if(WEVAL)
96+
include("weval")
97+
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/null.js "function main() {}")
98+
add_custom_target(
99+
starling-ics.wevalcache
100+
ALL
101+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
102+
COMMAND rm -f starling-ics.wevalcache
103+
COMMAND echo ./null.js | ${WEVAL_BIN} weval --dir . --show-stats --cache starling-ics.wevalcache -w -i starling.wasm -o /dev/null
104+
DEPENDS starling.wasm
105+
VERBATIM
106+
)
107+
set(WEVAL_CACHE_FILE "starling-ics.wevalcache")
108+
set(AOT 1)
109+
else()
110+
set(AOT 0)
111+
endif()
112+
93113
set(RUNTIME_FILE "starling.wasm")
94114
set(ADAPTER_FILE "preview1-adapter.wasm")
95-
configure_file("componentize.sh" "${CMAKE_CURRENT_BINARY_DIR}/componentize.sh" COPYONLY)
115+
configure_file("componentize.sh" "${CMAKE_CURRENT_BINARY_DIR}/componentize.sh" @ONLY)
96116
configure_file(${ADAPTER} "${CMAKE_CURRENT_BINARY_DIR}/${ADAPTER_FILE}" COPYONLY)
97117
configure_file(spin.toml spin.toml COPYONLY)
98118

cmake/spidermonkey.cmake

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
set(SM_REV ffbf1c4641440e74174199def6558c710b3ac323)
1+
set(SM_REV 99d3bca2aa1f477d3f7f3dcbad2ef2218c14f41b)
22

33
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
44
set(SM_BUILD_TYPE debug)
55
else()
66
set(SM_BUILD_TYPE release)
77
endif()
8+
set(SM_BUILD_TYPE_DASH ${SM_BUILD_TYPE})
9+
10+
option(WEVAL "Build with a SpiderMonkey variant that supports weval-based AOT compilation" OFF)
11+
12+
if (WEVAL)
13+
set(SM_BUILD_TYPE_DASH "${SM_BUILD_TYPE}-weval")
14+
set(SM_BUILD_TYPE "${SM_BUILD_TYPE}_weval")
15+
endif()
816

917
# If the developer has specified an alternate local set of SpiderMonkey
1018
# artifacts, use them. This allows for local/in-tree development without

cmake/weval.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
set(WEVAL_VERSION v0.2.7)
2+
3+
set(WEVAL_URL https://github.com/cfallin/weval/releases/download/${WEVAL_VERSION}/weval-${WEVAL_VERSION}-${HOST_ARCH}-${HOST_OS}.tar.xz)
4+
CPMAddPackage(NAME weval URL ${WEVAL_URL} DOWNLOAD_ONLY TRUE)
5+
set(WEVAL_DIR ${CPM_PACKAGE_weval_SOURCE_DIR})
6+
set(WEVAL_BIN ${WEVAL_DIR}/weval)

componentize.sh

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
wizer="${WIZER:-wizer}"
66
wasm_tools="${WASM_TOOLS:-wasm-tools}"
7+
weval="${WEVAL:-@WEVAL_BIN@}"
8+
aot=@AOT@
79

810
usage() {
9-
echo "Usage: $(basename "$0") [input.js] [-o output.wasm]"
11+
echo "Usage: $(basename "$0") [input.js] [--verbose] [-o output.wasm]"
1012
echo " Providing an input file but no output uses the input base name with a .wasm extension"
1113
echo " Providing an output file but no input creates a component without running any top-level script"
1214
exit 1
@@ -19,6 +21,7 @@ fi
1921

2022
IN_FILE=""
2123
OUT_FILE=""
24+
VERBOSE=0
2225

2326
while [ $# -gt 0 ]
2427
do
@@ -27,6 +30,10 @@ do
2730
OUT_FILE="$2"
2831
shift 2
2932
;;
33+
--verbose)
34+
VERBOSE=1
35+
shift
36+
;;
3037
*)
3138
if [ -n "$IN_FILE" ] && [ -z "$OUT_FILE" ] && [ $# -eq 1 ]
3239
then
@@ -62,8 +69,20 @@ else
6269
echo "Creating runtime-script component $OUT_FILE"
6370
fi
6471

72+
if [[ $aot -ne 0 ]]; then
73+
WEVAL_VERBOSE=""
74+
if [[ $VERBOSE -ne 0 ]]; then
75+
WEVAL_VERBOSE="--verbose --show-stats"
76+
fi
6577

66-
echo "$IN_FILE" | WASMTIME_BACKTRACE_DETAILS=1 $wizer --allow-wasi --wasm-bulk-memory true \
67-
--inherit-stdio true --inherit-env true $PREOPEN_DIR -o "$OUT_FILE" \
68-
-- "$(dirname "$0")/starling.wasm"
78+
echo "$IN_FILE" | WASMTIME_BACKTRACE_DETAILS=1 $weval weval -w $PREOPEN_DIR \
79+
--cache-ro "$(dirname "$0")/starling-ics.wevalcache" \
80+
$WEVAL_VERBOSE \
81+
-o "$OUT_FILE" \
82+
-i "$(dirname "$0")/starling.wasm"
83+
else
84+
echo "$IN_FILE" | WASMTIME_BACKTRACE_DETAILS=1 $wizer --allow-wasi --wasm-bulk-memory true \
85+
--inherit-stdio true --inherit-env true $PREOPEN_DIR -o "$OUT_FILE" \
86+
-- "$(dirname "$0")/starling.wasm"
87+
fi
6988
$wasm_tools component new -v --adapt "wasi_snapshot_preview1=$(dirname "$0")/preview1-adapter.wasm" --output "$OUT_FILE" "$OUT_FILE"

tests/test.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test_runtime="$1"
55
test_component="${3:-}"
66
test_name="$(basename $test_dir)"
77
test_serve_path="${4:-}"
8+
componentize_flags="${COMPONENTIZE_FLAGS:-}"
89

910
wasmtime="${WASMTIME:-wasmtime}"
1011

@@ -24,7 +25,7 @@ if [ -z "$test_component" ]; then
2425

2526
# Run Wizer
2627
set +e
27-
"$test_runtime/componentize.sh" "$test_dir/$test_name.js" "$test_component" 1> "$stdout_log" 2> "$stderr_log"
28+
"$test_runtime/componentize.sh" $componentize_flags "$test_dir/$test_name.js" "$test_component" 1> "$stdout_log" 2> "$stderr_log"
2829
wizer_result=$?
2930
set -e
3031

tests/tests.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ enable_testing()
33
find_program(BASH_PROGRAM bash)
44
include("wizer")
55
include("wasmtime")
6+
include("weval")
67

78
function(test_e2e TEST_NAME)
89
get_target_property(RUNTIME_DIR starling.wasm BINARY_DIR)

tests/wpt-harness/build-wpt-runtime.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
set -euo pipefail
44

5+
componentize_flags="${COMPONENTIZE_FLAGS:-}"
6+
57
if [ -z "${WPT_ROOT:-}" ]; then
68
echo "The WPT_ROOT environment variable is not set"
79
exit 1
@@ -16,4 +18,4 @@ inputs=(
1618
)
1719

1820
cat "${inputs[@]}" > wpt-test-runner.js
19-
./componentize.sh wpt-test-runner.js wpt-runtime.wasm
21+
./componentize.sh $componentize_flags wpt-test-runner.js wpt-runtime.wasm

tests/wpt-harness/wpt.cmake

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
enable_testing()
22

33
include("wasmtime")
4+
include("weval")
5+
6+
if(WEVAL)
7+
set(COMPONENTIZE_FLAGS "--aot")
8+
else()
9+
set(COMPONENTIZE_FLAGS "")
10+
endif()
411

512
if(DEFINED ENV{WPT_ROOT})
613
set(WPT_ROOT ENV{WPT_ROOT})
@@ -21,7 +28,7 @@ add_builtin(wpt_support
2128
add_custom_command(
2229
OUTPUT wpt-runtime.wasm
2330
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
24-
COMMAND ${CMAKE_COMMAND} -E env PATH=${WASM_TOOLS_DIR}:${WIZER_DIR}:$ENV{PATH} WPT_ROOT=${WPT_ROOT} ${CMAKE_CURRENT_SOURCE_DIR}/tests/wpt-harness/build-wpt-runtime.sh
31+
COMMAND ${CMAKE_COMMAND} -E env PATH=${WASM_TOOLS_DIR}:${WIZER_DIR}:$ENV{PATH} env "COMPONENTIZE_FLAGS=${COMPONENTIZE_FLAGS}" WPT_ROOT=${WPT_ROOT} ${CMAKE_CURRENT_SOURCE_DIR}/tests/wpt-harness/build-wpt-runtime.sh
2532
DEPENDS starling.wasm componentize.sh tests/wpt-harness/build-wpt-runtime.sh tests/wpt-harness/pre-harness.js tests/wpt-harness/post-harness.js
2633
VERBATIM
2734
)

0 commit comments

Comments
 (0)