feat: add WAMR WebAssembly engine support for Halide JIT#9116
Conversation
|
I sent this pr a little early but i would love feedback. v8 is a very large dep and is causing issues and we cant use wabt. wamr is a perfect drop in. |
4df6609 to
6223372
Compare
|
I just tried this... it is possible to use standard mechanisms to use WAMR. I would push directly, but I notice you prefer to force-push, @soldair, so to avoid losing work, I'll provide you with a patch file instead. This boils down to two changes:
I've also opened bytecodealliance/wasm-micro-runtime#4932 to nudge upstream into also using standard mechanisms for pulling in dependencies. 0001-Use-standard-mechanisms-to-use-WAMR.patch Patch contentsFrom 6c5c846544ba9e0a3775e3aeefc744705809072f Mon Sep 17 00:00:00 2001
From: Alex Reinking <areinking@adobe.com>
Date: Thu, 30 Apr 2026 15:30:06 -0400
Subject: [PATCH] Use standard mechanisms to use WAMR
- Rather than building WAMR in-line with Halide, add a vcpkg port
to manage building WAMR. The WAMR build uses FetchContent to
acquire simde, which is unnecessary, so we patch around it.
- Use find_package to import WAMR into the build, mirroring the
approach for V8 and wabt.
---
cmake/vcpkg/wamr/portfile.cmake | 21 +++++++++++++++
cmake/vcpkg/wamr/remove-fetchcontent.patch | 19 ++++++++++++++
cmake/vcpkg/wamr/vcpkg.json | 18 +++++++++++++
src/CMakeLists.txt | 30 +++++-----------------
vcpkg.json | 12 ++++++---
5 files changed, 73 insertions(+), 27 deletions(-)
create mode 100644 cmake/vcpkg/wamr/portfile.cmake
create mode 100644 cmake/vcpkg/wamr/remove-fetchcontent.patch
create mode 100644 cmake/vcpkg/wamr/vcpkg.json
diff --git a/cmake/vcpkg/wamr/portfile.cmake b/cmake/vcpkg/wamr/portfile.cmake
new file mode 100644
index 000000000..05593b8de
--- /dev/null
+++ b/cmake/vcpkg/wamr/portfile.cmake
@@ -0,0 +1,21 @@
+vcpkg_check_linkage(ONLY_STATIC_LIBRARY)
+
+vcpkg_from_github(
+ OUT_SOURCE_PATH SOURCE_PATH
+ REPO bytecodealliance/wasm-micro-runtime
+ REF 8c18e3f68b16c4bcaf05996b2636f6ed2b4cf629 # WAMR-2.4.4
+ SHA512 2378ab44e6ea3cd9bfede86a413c5d5503b8cd0d072bbee7099bd149897a58d74b57c06214f6b163242f5ac8bcdbb81a59632016ebd4c12a717786e1c387c9e3
+ PATCHES remove-fetchcontent.patch
+)
+
+vcpkg_cmake_configure(SOURCE_PATH "${SOURCE_PATH}")
+
+vcpkg_cmake_install()
+vcpkg_cmake_config_fixup(PACKAGE_NAME iwasm CONFIG_PATH lib/cmake/iwasm)
+
+vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE")
+
+file(REMOVE_RECURSE
+ "${CURRENT_PACKAGES_DIR}/debug/include"
+ "${CURRENT_PACKAGES_DIR}/debug/share"
+)
diff --git a/cmake/vcpkg/wamr/remove-fetchcontent.patch b/cmake/vcpkg/wamr/remove-fetchcontent.patch
new file mode 100644
index 000000000..936f4dbf2
--- /dev/null
+++ b/cmake/vcpkg/wamr/remove-fetchcontent.patch
@@ -0,0 +1,19 @@
+diff --git a/core/iwasm/libraries/simde/simde.cmake b/core/iwasm/libraries/simde/simde.cmake
+--- a/core/iwasm/libraries/simde/simde.cmake
++++ b/core/iwasm/libraries/simde/simde.cmake
+@@ -15,14 +15,5 @@ add_definitions (-DWASM_ENABLE_SIMDE=1)
+
+ include_directories(${LIB_SIMDE_DIR} ${LIB_SIMDE_DIR}/simde)
+
+-include(FetchContent)
+-
+-FetchContent_Declare(
+- simde
+- GIT_REPOSITORY https://github.com/simd-everywhere/simde
+- GIT_TAG v0.8.2
+-)
+-
+-message("-- Fetching simde ..")
+-FetchContent_MakeAvailable(simde)
++find_path(simde_SOURCE_DIR simde/wasm/simd128.h)
+ include_directories("${simde_SOURCE_DIR}")
diff --git a/cmake/vcpkg/wamr/vcpkg.json b/cmake/vcpkg/wamr/vcpkg.json
new file mode 100644
index 000000000..b4a61bb6d
--- /dev/null
+++ b/cmake/vcpkg/wamr/vcpkg.json
@@ -0,0 +1,18 @@
+{
+ "name": "wamr",
+ "version-semver": "2.4.4",
+ "description": "WebAssembly Micro Runtime (WAMR)",
+ "homepage": "https://github.com/bytecodealliance/wasm-micro-runtime",
+ "license": "Apache-2.0",
+ "dependencies": [
+ "simde",
+ {
+ "name": "vcpkg-cmake",
+ "host": true
+ },
+ {
+ "name": "vcpkg-cmake-config",
+ "host": true
+ }
+ ]
+}
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 1e1f8e9b5..18624abc9 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -591,38 +591,20 @@ if (Halide_WASM_BACKEND STREQUAL "wabt")
find_package(wabt REQUIRED)
_Halide_pkgdep(wabt)
- target_link_libraries(Halide PRIVATE wabt::wabt)
target_compile_definitions(Halide PRIVATE WITH_WABT)
+ target_link_libraries(Halide PRIVATE wabt::wabt)
elseif (Halide_WASM_BACKEND STREQUAL "V8")
find_package(V8 REQUIRED)
_Halide_pkgdep(V8)
+
target_compile_definitions(Halide PRIVATE WITH_V8)
target_link_libraries(Halide PRIVATE V8::V8)
elseif (Halide_WASM_BACKEND STREQUAL "WAMR")
- set(Halide_WAMR_ROOT_DIR "" CACHE PATH "Path to root directory of WAMR repository sources")
-
- if ("${Halide_WAMR_ROOT_DIR}" STREQUAL "")
- if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../../wasm-micro-runtime/build-scripts/runtime_lib.cmake")
- set(WAMR_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../wasm-micro-runtime")
- else()
- message(FATAL_ERROR "WAMR backend requires specifying Halide_WAMR_ROOT_DIR or cloning wasm-micro-runtime adjacent to the Halide repository.")
- endif()
- else()
- set(WAMR_ROOT_DIR "${Halide_WAMR_ROOT_DIR}")
- endif()
-
- set(WAMR_BUILD_PLATFORM "linux")
- set(WAMR_BUILD_TARGET "X86_64")
- set(WAMR_BUILD_INTERP 1)
- set(WAMR_BUILD_FAST_INTERP 1)
- set(WAMR_BUILD_REF_TYPES 1)
- set(WAMR_BUILD_SIMD 1)
- set(WAMR_BUILD_LIB_SIMDE 1)
- include(${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
- add_library(wamr_lib STATIC ${WAMR_RUNTIME_LIB_SOURCE})
- target_link_libraries(Halide PRIVATE wamr_lib)
+ find_package(iwasm REQUIRED)
+ _Halide_pkgdep(iwasm)
+
target_compile_definitions(Halide PRIVATE WITH_WAMR)
- target_include_directories(Halide PRIVATE "${WAMR_ROOT_DIR}/core/iwasm/include")
+ target_link_libraries(Halide PRIVATE iwasm::vmlib)
elseif (Halide_WASM_BACKEND)
message(FATAL_ERROR "Unknown Halide_WASM_BACKEND `${Halide_WASM_BACKEND}`")
endif ()
diff --git a/vcpkg.json b/vcpkg.json
index 6a404753c..5d7b4c293 100644
--- a/vcpkg.json
+++ b/vcpkg.json
@@ -45,7 +45,7 @@
"default-features": false,
"features": [
"serialization",
- "wasm-executor"
+ "wasm-executor-wabt"
]
}
]
@@ -180,11 +180,17 @@
}
]
},
- "wasm-executor": {
- "description": "Include built-in WASM executor",
+ "wasm-executor-wabt": {
+ "description": "Include wabt-based WASM executor",
"dependencies": [
"wabt"
]
+ },
+ "wasm-executor-wamr": {
+ "description": "Include wamr-based WASM executor",
+ "dependencies": [
+ "wamr"
+ ]
}
}
}
--
2.50.1 (Apple Git-155) |
|
please feel free to push any time. i'm not typically a force pusher just a product of a tool i was using. 😅 i'll apply this patch |
|
Testing this locally, I'm seeing issues with finding Halide's extern funcs feature. Also seeing issues with some surprisingly moderate allocation sizes (e.g. 48KB). |
|
Thanks for looking. If you have a command handy ill make sure everything passes and @ you when i think we're doing ok. I thought i had been running all the tests. I'm very new to this project 😅 |
|
Here's how I'm testing locally. You'll have to adapt my paths to yours. $ source ~/dev/emsdk/emsdk_env.sh
$ export VCPKG_ROOT=$HOME/dev/vcpkg
$ cmake --preset macOS-vcpkg -DHalide_WASM_BACKEND=WAMR -DHalide_TARGET=wasm-32-wasmrt-wasm_simd128
$ cmake --build build/macOS-vcpkg
$ ctest --test-dir build/macOS-vcpkg -j$(sysctl -n hw.physicalcpu)Even after your latest commit, I'm still seeing a few failures: Complete CTest failures & outputOnce everything works locally, I can add a buildbot configuration for this backend. |
…jit_externs test - Support dynamic extern callbacks in WAMR using template native dispatched wrappers. - Raise WAMR memory heap size threshold, synchronize stack overrides configuration using environment variables HL_WASM_HEAP_SIZE, HL_WASM_STACK_SIZE. - Renamed test to test/correctness/wasm_jit_externs.cpp with Wasm executor capability checks.
|
I reproed the errors locally. it seems like i had the wrong jit target cached. They should be resolved. I'm on linux so im not sure if i should have a vcpkg preset but im hoping im fine? if not ill learn about it. |
|
opps haha |
|
Note that CTest does not honor the HL_JIT_TARGET environment variable. You have to go through the Halide_TARGET cache variable. |
|
I'm still seeing a few test failures: (halide) areinking@Alexanders-MacBook-Pro Halide % ctest --test-dir build/macOS-vcpkg --rerun-failed --output-on-failure
Test project /Users/areinking/dev/Halide/build/macOS-vcpkg
Start 37: correctness_align_bounds
1/3 Test #37: correctness_align_bounds .........Subprocess aborted***Exception: 0.34 sec
Internal error at /Users/areinking/dev/Halide/src/WasmExecutor.cpp:3232
Condition failed: call_success
Error: wasm_runtime_call_wasm_a failed: Exception: invalid local type
Start 69: correctness_callable_errors
2/3 Test #69: correctness_callable_errors ......Subprocess aborted***Exception: 0.10 sec
Saw expected: (NO ERROR)
Internal error at /Users/areinking/dev/Halide/src/WasmExecutor.cpp:3198
Condition failed: wbuf
Error: hostbuf_to_wasmbuf_wamr failed
Start 128: correctness_exception
3/3 Test #128: correctness_exception ............Subprocess aborted***Exception: 0.10 sec
Expected compile error:
Error: Implicit cast from float32 to int in argument 1 in call to "f0" is not allowed. Use an explicit cast.
Expected compile error:
Error: Can't index into a reference to Func "f0", because it does not return a Tuple.
Expected compile error:
Error: In update definition 0 of Func "f0":
Tuple element 0 of update definition has type float32, but pure definition has type int32
Expected compile error:
Error: In update definition 0 of Func "f0":
Undefined expression in right-hand-side of update.
Expected internal error:
Internal error at /Users/areinking/dev/Halide/src/IR.cpp:40
Condition failed: a.defined()
Error: Add of undefined
Expected internal error:
Internal error at /Users/areinking/dev/Halide/src/ModulusRemainder.cpp:161
Error: modulus_remainder of bool
Internal error at /Users/areinking/dev/Halide/src/WasmExecutor.cpp:3198
Condition failed: wbuf
Error: hostbuf_to_wasmbuf_wamr failed
0% tests passed, 3 tests failed out of 3
Label Time Summary:
correctness = 0.53 sec*proc (3 tests)
Total Test time (real) = 0.55 sec
The following tests FAILED:
37 - correctness_align_bounds (Subprocess aborted) correctness
69 - correctness_callable_errors (Subprocess aborted) correctness
128 - correctness_exception (Subprocess aborted) correctness
Errors while running CTest |
|
@soldair - I'm seeing syntax errors in CI? Are you still interested in advancing this PR? |
|
I am definitely interested. I'll sync and get these fixed today. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #9116 +/- ##
=======================================
Coverage ? 69.34%
=======================================
Files ? 254
Lines ? 78219
Branches ? 18714
=======================================
Hits ? 54241
Misses ? 18488
Partials ? 5490 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
CI was failing due to #9133 renaming |
|
Some kind of build issue with wamr on 32-bit targets. Will investigate. |
Explicitly set WAMR_BUILD_TARGET in the WAMR vcpkg port based on VCPKG_TARGET_ARCHITECTURE. This bypasses a bug in WAMR's automatic target detection when cross-compiling or building for 32-bit targets, which incorrectly fallback-classified 32-bit ARM (arm-linux) as X86_32, causing compiler errors. TAG=agy CONV=61a7f0a6-c030-4b48-8580-77f4afa4c8a1
|
Looks like we need to set |
This prevents WAMR from defaulting to the host OS when cross-compiling, which previously caused compile/link failures when building Linux targets on non-Linux hosts. TAG=agy CONV=61a7f0a6-c030-4b48-8580-77f4afa4c8a1
…to turn on some asm features for windows. this should also fix the arm-64 build. ill check the any new ailures give it another click for me =) and any suggestions would be appreciated.
This PR introduces Wasm Micro Runtime (WAMR) integration as an execution engine backend alternative inside the
WasmModuleJIT runtime harness, in addition to supporting standard co-existence of fallback configurations alongside legacy engines (wabt/v8).Design Background
Halide WebAssembly testing has historically targeted
wabtorV8modules. WAMR provides smaller execution footprint constraints suitable for constrained evaluation targets and embedding environments.Technical Enhancements
src/WasmExecutor.cpp.FetchContentmodule downloads.__extendhfsf2,__truncsfhf2half-float native implementations and mathematical POSIX wrappers.Fixes #
Breaking changes
None. Code coexistence preserves logic paths for previous backends
wabt/v8.Checklist