Book Image

Getting started with LLVM core libraries

Book Image

Getting started with LLVM core libraries

Overview of this book

Table of Contents (17 chapters)
Getting Started with LLVM Core Libraries
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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 
sum: 12 

Alternatively, use the MCJIT engine:

$ lli -use-mcjit sum-main.bc 
sum: 12

There is also a flag to use the interpreter, which is usually much slower:

$ lli -force-interpreter sum-main.bc
sum:12

Using the llvm-rtdyld tool

The llvm...