Using LLVM JIT compilation tools

LLVM provides a few tools to work with JIT engines. The examples of such tools are lli and llvm-rtdyld.

Using the lli tool

The interpreter tool (lli) implements an LLVM bitcode interpreter and JIT compiler as well by using the LLVM execution engines studied in this chapter. Let's consider the source file, sum-main.c:

#include <stdio.h>

int sum(int a, int b) {
  return a + b;
}

int main() {
  printf("sum: %d\n", sum(2, 3) + sum(3, 4));
  return 0;
}

The lli tool is capable of running bitcode files when a main function is provided. Generate the sum-main.bc bitcode file by using clang:

$ clang -emit-llvm -c sum-main.c -o sum-main.bc

Now, run the bitcode through lli by using the old JIT compilation engine:

$ lli sum-main.bc ...

Get Getting Started with LLVM Core Libraries now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.