From 3823f1c2cb544cbed9ea99c0af3d2d4d216b3bf5 Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Fri, 26 Jul 2024 17:03:47 -0700 Subject: [PATCH] WIP: weval support. (Stacked on top of #90.) When built with: ``` $ mkdir build/; cd build/ $ cmake .. -DCMAKE_BUILD_TYPE=Release -DUSE_WASM_OPT=OFF -DWEVAL=ON $ make ``` This PR will result in a `starling.wasm`, a prebuilt compilation cache of ICs in `starling-ics.wevalcache` (~1.8MiB), and a `componentize.sh` that takes a `--aot` option. I haven't yet quite worked out how to successfully use componentize.sh, so I can't claim this is end-to-end tested yet, but it is invoking weval successfully and ICs are wevaled at least. --- CMakeLists.txt | 41 +++++++++++++++++++++++++++++++++------- cmake/spidermonkey.cmake | 10 +++++++++- cmake/weval.cmake | 6 ++++++ componentize.sh | 22 ++++++++++++++++----- 4 files changed, 66 insertions(+), 13 deletions(-) create mode 100644 cmake/weval.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 4509545d..a9b2bfe7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,7 @@ include("init-corrosion") include("wasm-tools") include("binaryen") include("wizer") +include("weval") include("wasmtime") include("fmt") @@ -96,6 +97,22 @@ configure_file("componentize.sh" "${CMAKE_CURRENT_BINARY_DIR}/componentize.sh" C configure_file(${ADAPTER} "${CMAKE_CURRENT_BINARY_DIR}/${ADAPTER_FILE}" COPYONLY) configure_file(spin.toml spin.toml COPYONLY) +# build a compilation cache of ICs +if(WEVAL) + include("weval") + file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/null.js "function main() {}") + add_custom_target( + starling-ics.wevalcache + ALL + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND rm -f starling-ics.wevalcache + COMMAND echo ./null.js | ${WEVAL_BIN} weval --dir . --show-stats --cache starling-ics.wevalcache -w -i starling.wasm -o /dev/null + DEPENDS starling.wasm + VERBATIM + ) + set(WEVAL_CACHE_FILE "starling-ics.wevalcache") +endif() + function(componentize OUTPUT) set(options) set(oneValueArgs) @@ -105,13 +122,23 @@ function(componentize OUTPUT) list(JOIN ARG_SOURCES " " SOURCES) get_target_property(RUNTIME_DIR starling.wasm BINARY_DIR) - add_custom_command( - OUTPUT ${OUTPUT}.wasm - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} - COMMAND ${CMAKE_COMMAND} -E env "PATH=${WASM_TOOLS_DIR};${WIZER_DIR};$ENV{PATH}" ${RUNTIME_DIR}/componentize.sh ${SOURCES} -o ${OUTPUT}.wasm - DEPENDS ${ARG_SOURCES} ${RUNTIME_DIR}/componentize.sh starling.wasm - VERBATIM - ) + if(WEVAL) + add_custom_command( + OUTPUT ${OUTPUT}.wasm + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND ${CMAKE_COMMAND} -E env "PATH=${WASM_TOOLS_DIR};${WEVAL_DIR};$ENV{PATH}" ${RUNTIME_DIR}/componentize.sh ${SOURCES} --aot -o ${OUTPUT}.wasm + DEPENDS ${ARG_SOURCES} ${RUNTIME_DIR}/componentize.sh starling.wasm starling-ics.wevalcache + VERBATIM + ) + else() + add_custom_command( + OUTPUT ${OUTPUT}.wasm + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND ${CMAKE_COMMAND} -E env "PATH=${WASM_TOOLS_DIR};${WIZER_DIR};$ENV{PATH}" ${RUNTIME_DIR}/componentize.sh ${SOURCES} -o ${OUTPUT}.wasm + DEPENDS ${ARG_SOURCES} ${RUNTIME_DIR}/componentize.sh starling.wasm + VERBATIM + ) + endif() add_custom_target(${OUTPUT} DEPENDS ${OUTPUT}.wasm) endfunction() diff --git a/cmake/spidermonkey.cmake b/cmake/spidermonkey.cmake index 5a5dee84..5a900250 100644 --- a/cmake/spidermonkey.cmake +++ b/cmake/spidermonkey.cmake @@ -1,10 +1,18 @@ -set(SM_REV ffbf1c4641440e74174199def6558c710b3ac323) +set(SM_REV bbaee9532ff88b38c31c7f38d0aba1204a6aa9d6) if (CMAKE_BUILD_TYPE STREQUAL "Debug") set(SM_BUILD_TYPE debug) else() set(SM_BUILD_TYPE release) endif() +set(SM_BUILD_TYPE_DASH ${SM_BUILD_TYPE}) + +option(WEVAL "Build with a SpiderMonkey variant that supports weval-based AOT compilation" OFF) + +if (WEVAL) + set(SM_BUILD_TYPE_DASH "${SM_BUILD_TYPE}-weval") + set(SM_BUILD_TYPE "${SM_BUILD_TYPE}_weval") +endif() # If the developer has specified an alternate local set of SpiderMonkey # artifacts, use them. This allows for local/in-tree development without diff --git a/cmake/weval.cmake b/cmake/weval.cmake new file mode 100644 index 00000000..ea018ae7 --- /dev/null +++ b/cmake/weval.cmake @@ -0,0 +1,6 @@ +set(WEVAL_VERSION v0.2.4) + +set(WEVAL_URL https://github.com/cfallin/weval/releases/download/${WEVAL_VERSION}/weval-${WEVAL_VERSION}-${HOST_ARCH}-${HOST_OS}.tar.xz) +CPMAddPackage(NAME weval URL ${WEVAL_URL} DOWNLOAD_ONLY TRUE) +set(WEVAL_DIR ${CPM_PACKAGE_weval_SOURCE_DIR}) +set(WEVAL_BIN ${WEVAL_DIR}/weval) diff --git a/componentize.sh b/componentize.sh index 0d8419e7..7ca29f20 100755 --- a/componentize.sh +++ b/componentize.sh @@ -6,7 +6,7 @@ wizer="${WIZER:-wizer}" wasm_tools="${WASM_TOOLS:-wasm-tools}" usage() { - echo "Usage: $(basename "$0") [input.js] [-o output.wasm]" + echo "Usage: $(basename "$0") [input.js] [--aot] [-o output.wasm]" echo " Providing an input file but no output uses the input base name with a .wasm extension" echo " Providing an output file but no input creates a component without running any top-level script" exit 1 @@ -19,6 +19,7 @@ fi IN_FILE="" OUT_FILE="" +AOT=0 while [ $# -gt 0 ] do @@ -27,6 +28,10 @@ do OUT_FILE="$2" shift 2 ;; + --aot) + AOT=1 + shift + ;; *) if [ -n "$IN_FILE" ] && [ -z "$OUT_FILE" ] && [ $# -eq 1 ] then @@ -62,8 +67,15 @@ else echo "Creating runtime-script component $OUT_FILE" fi - -echo "$IN_FILE" | WASMTIME_BACKTRACE_DETAILS=1 $wizer --allow-wasi --wasm-bulk-memory true \ - --inherit-stdio true --inherit-env true $PREOPEN_DIR -o "$OUT_FILE" \ - -- "$(dirname "$0")/starling.wasm" +if [[ $AOT -ne 0 ]]; then + echo "$IN_FILE" | WASMTIME_BACKTRACE_DETAILS=1 weval weval -w $PREOPEN_DIR \ + --cache-ro "$(dirname "$0")/starling-ics.wevalcache" \ + --show-stats \ + -o "$OUT_FILE" \ + -i "$(dirname "$0")/starling.wasm" +else + echo "$IN_FILE" | WASMTIME_BACKTRACE_DETAILS=1 $wizer --allow-wasi --wasm-bulk-memory true \ + --inherit-stdio true --inherit-env true $PREOPEN_DIR -o "$OUT_FILE" \ + -- "$(dirname "$0")/starling.wasm" +fi $wasm_tools component new -v --adapt "wasi_snapshot_preview1=$(dirname "$0")/preview1-adapter.wasm" --output "$OUT_FILE" "$OUT_FILE"