This sample shows how to:
static_library_loader
The model compiled into the static library exports a single function simple_mul
that returns the multiplication of two tensors:
func.func @simple_mul(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>) -> tensor<4xf32> { %0 = "arith.mulf"(%arg0, %arg1) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> return %0 : tensor<4xf32> }
IREE's static_library_loader
allows applications to inject a set of static libraries that can be resolved at runtime by name. This can be particularly useful on “bare metal” or embedded systems running IREE that lack operating systems or the ability to load shared libraries in binaries.
When static library output is enabled, iree-compile
produces a separate static library to compile into the target program. At runtime bytecode module instructs the VM which static libraries to load exported functions from the model.
Note: run the following commands from IREE's github repo root.
llvm-cpu
compiler target backend, and the local-sync
runtime HAL driver (see the getting started guide for general instructions on building using CMake):cmake -B ../iree-build/ -G Ninja \ -DCMAKE_BUILD_TYPE=RelWithDebInfo . -DIREE_BUILD_SAMPLES=ON \ -DIREE_TARGET_BACKEND_DEFAULTS=OFF \ -DIREE_TARGET_BACKEND_LLVM_CPU=ON \ -DIREE_HAL_DRIVER_DEFAULTS=OFF \ -DIREE_HAL_DRIVER_LOCAL_SYNC=ON \ -DIREE_HAL_EXECUTABLE_LOADER_DEFAULTS=OFF \ -DIREE_HAL_EXECUTABLE_PLUGIN_DEFAULTS=OFF \ -DIREE_BUILD_COMPILER=ON \
static_library_demo
CMake target to create the static demo. This target has several dependencies that will compile simple_mul.mlir
into a static library (simple_mul.h
& simple_mul.c
) as well as a bytecode module (simple_mul.vmfb
) which are finally built into the demo binary:cmake --build ../iree-build/ --target iree_samples_static_library_static_library_demo
../iree-build/samples/static_library/static_library_demo # Output: static_library_run_bytecode passed
The steps above build both the compiler for the host (machine doing the compiling) and the demo for the target using same options as the host machine. If you wish to target a different platform other than the host, you'll need to compile the library and demo with different options.
For example, see this documentation on cross compiling on Android.
Note: separating the target from the host will require modifying dependencies in the demos CMakeLists.txt
. See included comments for more info.