Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 1 | # IREE "Static Library" sample |
| 2 | |
| 3 | This sample shows how to: |
| 4 | 1. Produce a static library and bytecode module with IREE's compiler |
| 5 | 2. Compile the static library into a program using the `static_library_loader` |
| 6 | 3. Run the demo with the module using functions exported by the static library |
| 7 | |
| 8 | The model compiled into the static library exports a single function |
| 9 | `simple_mul` that returns the multiplication of two tensors: |
| 10 | |
| 11 | ```mlir |
Scott Todd | 7c4fdc3 | 2022-07-19 13:12:54 -0700 | [diff] [blame] | 12 | func.func @simple_mul(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>) -> tensor<4xf32> { |
| 13 | %0 = "arith.mulf"(%arg0, %arg1) : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32> |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 14 | return %0 : tensor<4xf32> |
| 15 | } |
| 16 | ``` |
| 17 | |
| 18 | ## Background |
| 19 | |
| 20 | IREE's `static_library_loader` allows applications to inject a set of static |
| 21 | libraries that can be resolved at runtime by name. This can be particularly |
| 22 | useful on "bare metal" or embedded systems running IREE that lack operating |
| 23 | systems or the ability to load shared libraries in binaries. |
| 24 | |
Stella Laurenzo | 7f2972c | 2022-03-19 14:09:43 -0700 | [diff] [blame] | 25 | When static library output is enabled, `iree-compile` produces a separate |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 26 | static library to compile into the target program. At runtime bytecode module |
| 27 | instructs the VM which static libraries to load exported functions from the |
| 28 | model. |
| 29 | |
| 30 | ## Instructions |
| 31 | _Note: run the following commands from IREE's github repo root._ |
| 32 | |
| 33 | 1. Configure CMake for building the static library then demo. You'll need to set |
Scott Todd | 55bec0d | 2022-07-28 11:45:18 -0700 | [diff] [blame] | 34 | the flags building samples, the compiler, the `llvm-cpu` |
| 35 | compiler target backend, and the `local-sync` runtime HAL driver (see |
Scott Todd | 8c34b97 | 2023-10-24 09:34:49 -0700 | [diff] [blame] | 36 | [the getting started guide](https://iree.dev/building-from-source/getting-started/) |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 37 | for general instructions on building using CMake): |
| 38 | |
| 39 | ```shell |
Scott Todd | e329737 | 2023-10-27 11:54:41 -0700 | [diff] [blame] | 40 | cmake -B ../iree-build/ -G Ninja \ |
Scott Todd | 55bec0d | 2022-07-28 11:45:18 -0700 | [diff] [blame] | 41 | -DCMAKE_BUILD_TYPE=RelWithDebInfo . |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 42 | -DIREE_BUILD_SAMPLES=ON \ |
Scott Todd | d1620f0 | 2021-12-22 14:55:38 -0800 | [diff] [blame] | 43 | -DIREE_TARGET_BACKEND_DEFAULTS=OFF \ |
Scott Todd | 352da3f | 2022-07-20 15:25:11 -0700 | [diff] [blame] | 44 | -DIREE_TARGET_BACKEND_LLVM_CPU=ON \ |
Scott Todd | d1620f0 | 2021-12-22 14:55:38 -0800 | [diff] [blame] | 45 | -DIREE_HAL_DRIVER_DEFAULTS=OFF \ |
Ben Vanik | 6e64b6e | 2022-06-07 09:14:53 -0700 | [diff] [blame] | 46 | -DIREE_HAL_DRIVER_LOCAL_SYNC=ON \ |
| 47 | -DIREE_HAL_EXECUTABLE_LOADER_DEFAULTS=OFF \ |
Ben Vanik | e19fc8e | 2023-04-14 16:08:01 -0700 | [diff] [blame] | 48 | -DIREE_HAL_EXECUTABLE_PLUGIN_DEFAULTS=OFF \ |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 49 | -DIREE_BUILD_COMPILER=ON \ |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 50 | ``` |
| 51 | |
| 52 | 2. Build the `static_library_demo` CMake target to create the static demo. This |
Scott Todd | 07057c8 | 2022-04-13 13:40:56 -0700 | [diff] [blame] | 53 | target has several dependencies that will compile `simple_mul.mlir` into a |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 54 | static 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 Todd | 55bec0d | 2022-07-28 11:45:18 -0700 | [diff] [blame] | 58 | cmake --build ../iree-build/ --target iree_samples_static_library_static_library_demo |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 59 | ``` |
| 60 | |
| 61 | 3. Run the sample binary: |
| 62 | |
| 63 | ```shell |
Scott Todd | 55bec0d | 2022-07-28 11:45:18 -0700 | [diff] [blame] | 64 | ../iree-build/samples/static_library/static_library_demo |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 65 | |
Scott Todd | 55bec0d | 2022-07-28 11:45:18 -0700 | [diff] [blame] | 66 | # Output: static_library_run_bytecode passed |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 67 | ``` |
| 68 | |
| 69 | ### Changing compilation options |
| 70 | |
| 71 | The steps above build both the compiler for the host (machine doing the |
| 72 | compiling) and the demo for the target using same options as the host machine. |
Scott Todd | 55bec0d | 2022-07-28 11:45:18 -0700 | [diff] [blame] | 73 | If you wish to target a different platform other than the host, you'll need to |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 74 | compile the library and demo with different options. |
| 75 | |
| 76 | For example, see |
Scott Todd | 8c34b97 | 2023-10-24 09:34:49 -0700 | [diff] [blame] | 77 | [this documentation](https://iree.dev/building-from-source/android/) |
Kojo Acquah | 3135702 | 2021-07-16 15:55:31 -0700 | [diff] [blame] | 78 | on cross compiling on Android. |
| 79 | |
| 80 | Note: separating the target from the host will require modifying dependencies in |
| 81 | the demos `CMakeLists.txt`. See included comments for more info. |