Skip to content

Commit

Permalink
WIP: weval support.
Browse files Browse the repository at this point in the history
(Stacked on top of bytecodealliance#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.
  • Loading branch information
cfallin committed Jul 29, 2024
1 parent ccc5742 commit 250e44a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 13 deletions.
41 changes: 34 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ include("init-corrosion")
include("wasm-tools")
include("binaryen")
include("wizer")
include("weval")
include("wasmtime")

include("fmt")
Expand Down Expand Up @@ -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)
Expand All @@ -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()

Expand Down
10 changes: 9 additions & 1 deletion cmake/spidermonkey.cmake
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 6 additions & 0 deletions cmake/weval.cmake
Original file line number Diff line number Diff line change
@@ -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)
22 changes: 17 additions & 5 deletions componentize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,6 +19,7 @@ fi

IN_FILE=""
OUT_FILE=""
AOT=0

while [ $# -gt 0 ]
do
Expand All @@ -27,6 +28,10 @@ do
OUT_FILE="$2"
shift 2
;;
--aot)
AOT=1
shift
;;
*)
if [ -n "$IN_FILE" ] && [ -z "$OUT_FILE" ] && [ $# -eq 1 ]
then
Expand Down Expand Up @@ -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"

0 comments on commit 250e44a

Please sign in to comment.