blob: 4a38dfd1f9ec6fa8e0371615f5b13aeba9371ed4 [file] [log] [blame] [view]
Kojo Acquah31357022021-07-16 15:55:31 -07001# IREE "Static Library" sample
2
3This sample shows how to:
41. Produce a static library and bytecode module with IREE's compiler
52. Compile the static library into a program using the `static_library_loader`
63. Run the demo with the module using functions exported by the static library
7
8The model compiled into the static library exports a single function
9`simple_mul` that returns the multiplication of two tensors:
10
11```mlir
Scott Todd7c4fdc32022-07-19 13:12:54 -070012func.func @simple_mul(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>) -> tensor<4xf32> {
13 %0 = "arith.mulf"(%arg0, %arg1) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32>
Kojo Acquah31357022021-07-16 15:55:31 -070014 return %0 : tensor<4xf32>
15}
16```
17
18## Background
19
20IREE's `static_library_loader` allows applications to inject a set of static
21libraries that can be resolved at runtime by name. This can be particularly
22useful on "bare metal" or embedded systems running IREE that lack operating
23systems or the ability to load shared libraries in binaries.
24
Stella Laurenzo7f2972c2022-03-19 14:09:43 -070025When static library output is enabled, `iree-compile` produces a separate
Kojo Acquah31357022021-07-16 15:55:31 -070026static library to compile into the target program. At runtime bytecode module
27instructs the VM which static libraries to load exported functions from the
28model.
29
30## Instructions
31_Note: run the following commands from IREE's github repo root._
32
331. Configure CMake for building the static library then demo. You'll need to set
Scott Todd55bec0d2022-07-28 11:45:18 -070034the flags building samples, the compiler, the `llvm-cpu`
35compiler target backend, and the `local-sync` runtime HAL driver (see
Scott Todd8c34b972023-10-24 09:34:49 -070036[the getting started guide](https://iree.dev/building-from-source/getting-started/)
Kojo Acquah31357022021-07-16 15:55:31 -070037for general instructions on building using CMake):
38
39 ```shell
Scott Todde3297372023-10-27 11:54:41 -070040 cmake -B ../iree-build/ -G Ninja \
Scott Todd55bec0d2022-07-28 11:45:18 -070041 -DCMAKE_BUILD_TYPE=RelWithDebInfo .
Kojo Acquah31357022021-07-16 15:55:31 -070042 -DIREE_BUILD_SAMPLES=ON \
Scott Toddd1620f02021-12-22 14:55:38 -080043 -DIREE_TARGET_BACKEND_DEFAULTS=OFF \
Scott Todd352da3f2022-07-20 15:25:11 -070044 -DIREE_TARGET_BACKEND_LLVM_CPU=ON \
Scott Toddd1620f02021-12-22 14:55:38 -080045 -DIREE_HAL_DRIVER_DEFAULTS=OFF \
Ben Vanik6e64b6e2022-06-07 09:14:53 -070046 -DIREE_HAL_DRIVER_LOCAL_SYNC=ON \
47 -DIREE_HAL_EXECUTABLE_LOADER_DEFAULTS=OFF \
Ben Vanike19fc8e2023-04-14 16:08:01 -070048 -DIREE_HAL_EXECUTABLE_PLUGIN_DEFAULTS=OFF \
Kojo Acquah31357022021-07-16 15:55:31 -070049 -DIREE_BUILD_COMPILER=ON \
Kojo Acquah31357022021-07-16 15:55:31 -070050 ```
51
522. Build the `static_library_demo` CMake target to create the static demo. This
Scott Todd07057c82022-04-13 13:40:56 -070053target has several dependencies that will compile `simple_mul.mlir` into a
Kojo Acquah31357022021-07-16 15:55:31 -070054static library (`simple_mul.h` & `simple_mul.c`) as well as a bytecode module
55(`simple_mul.vmfb`) which are finally built into the demo binary:
56
57 ```shell
Scott Todd55bec0d2022-07-28 11:45:18 -070058 cmake --build ../iree-build/ --target iree_samples_static_library_static_library_demo
Kojo Acquah31357022021-07-16 15:55:31 -070059 ```
60
613. Run the sample binary:
62
63 ```shell
Scott Todd55bec0d2022-07-28 11:45:18 -070064 ../iree-build/samples/static_library/static_library_demo
Kojo Acquah31357022021-07-16 15:55:31 -070065
Scott Todd55bec0d2022-07-28 11:45:18 -070066 # Output: static_library_run_bytecode passed
Kojo Acquah31357022021-07-16 15:55:31 -070067 ```
68
69### Changing compilation options
70
71The steps above build both the compiler for the host (machine doing the
72compiling) and the demo for the target using same options as the host machine.
Scott Todd55bec0d2022-07-28 11:45:18 -070073If you wish to target a different platform other than the host, you'll need to
Kojo Acquah31357022021-07-16 15:55:31 -070074compile the library and demo with different options.
75
76For example, see
Scott Todd8c34b972023-10-24 09:34:49 -070077[this documentation](https://iree.dev/building-from-source/android/)
Kojo Acquah31357022021-07-16 15:55:31 -070078on cross compiling on Android.
79
80Note: separating the target from the host will require modifying dependencies in
81the demos `CMakeLists.txt`. See included comments for more info.