/iree-template-compiler-cmake

Template repository for using the IREE's C compiler API via CMake

Primary LanguageCApache License 2.0Apache-2.0

IREE Compiler Hello World with CMake

Build with Latest IREE Release

Instructions

Cloning the repository

Use GitHub's "Use this template" feature to create a new repository or clone it manually:

$ git clone https://github.com/iree-org/iree-template-compiler-cmake.git
$ cd iree-template-compiler-cmake
$ git submodule update --init --recursive

The only requirement is that the main IREE repository is added as a submodule. If working in an existing repository then add the submodule and ensure it has its submodules initialized:

$ git submodule add https://github.com/openxla/iree.git third_party/iree/
$ git submodule update --init --recursive

Acquire libIREECompiler.so

Option 1: Build the compiler from source

This lets you use static linking rather than dynamic linking or choose how the compiler is composed (which optional components are enabled, using toolchain features like sanitizers, etc.).

$ cmake -B build/ -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo .
$ cmake --build build/ --target iree-compile

# Find the compiler shared object that the compiler binary uses.
$ ldd build/third_party/iree/tools/iree-compile
$ ls build/third_party/iree/lib/
libIREECompiler.so  libIREECompiler.so.0

Option 2: Installing the compiler from a binary distribution

Refer to the IREE Python bindings documentation for more details on PIP packages.

# Create a virtual environment to download packages into.
$ python -m venv .venv
$ source .venv/bin/activate

# Install the iree-compiler package from a release (stable or nightly).
$ python -m pip install iree-compiler

# Find where the compiler was installed to.
# E.g. `./.venv/lib/python3.11/site-packages/iree/compiler/`
$ COMPILER_PATH=$(python -c "import iree.compiler as _; print(_.__path__[0])")

# Find the compiler shared object.
$ ls $COMPILER_PATH/_mlir_libs/
iree-compile  libIREECompiler.so

Running the sample

# Build with CMake.
$ cmake -B build/ -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo .
$ cmake --build build/ --target hello-compiler

# Run the sample.
$ ./build/hello_compiler/hello-compiler </path/to/libIREECompiler.so>