This sample shows how to:
- Produce a static library and bytecode module with IREE's compiler
- Compile the static library into a program using the
static_library_loader - Run the demo with the module using functions exported by the static library
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.
- Configure CMake for building the static library then demo. You'll need to set
the flags building samples, the compiler, the
llvm-cpucompiler target backend, and thelocal-syncruntime 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 \- Build the
static_library_demoCMake target to create the static demo. This target has several dependencies that will compilesimple_mul.mlirinto 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- Run the sample binary:
../iree-build/samples/static_library/static_library_demo
# Output: static_library_run_bytecode passedThe 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.