This experimental sample demonstrates one way to target the web platform with IREE. The output artifact is a web page that loads separately provided IREE .vmfb
(compiled ML model) files and allows for calling functions on them.
install
target with CMake)emsdk_env.bat
)bash ./build_sample.sh [path to install] && bash ./serve_sample.sh
To rebuild most parts of the sample (C runtime, sample HTML, CMake config, etc.), just control + C
to stop the local webserver and rerun the script.
Emscripten is used (via the emcmake
CMake wrapper) to compile the runtime into WebAssembly and JavaScript files.
Any supported IREE program, such as simple_abs.mlir, is compiled using the “system library” linking mode. This creates a shared object (typically .so/.dll, .wasm in this case). When the runtime attempts to load this file using dlopen()
and dlsym()
, Emscripten makes use of its runtime dynamic linking support to instantiate a new WebAssembly.Instance
which shares memory with the main runtime then resolve each export provided by the new Wasm module.
iree_api.js
exposes a Promise-based API to the hosting application in index.html
iree_api.js
creates a worker running iree_worker.js, which includes Emscripten's JS code and instantiates the WebAssembly moduleiree_api.js
and iree_worker.js
internallyMultithreading is not supported yet. Emscripten only has experimental support for dynamic linking + pthreads: https://emscripten.org/docs/compiling/Dynamic-Linking.html#pthreads-support. Compiled programs produced by IREE link with wasm-ld
, while Emscripten expects programs to be linked using emcc
with the -s SIDE_MODULE
option, which includes several Emscripten-pthreads-specific module exported functions such as emscripten_tls_init
.