Working with LLVM¶
This is not a replacement for the LLVM documentation, but a collection of tips for working on LLVM for Julia.
Overview of Julia to LLVM Interface¶
Julia statically links in LLVM by default.
USE_LLVM_SHLIB=1 to link dynamically.
The code for lowering Julia AST to LLVM IR or interpreting it directly is in
||Lowering utilities, notably for array and tuple accesses|
||Top-level of code generation, pass list, lowering builtins|
||Tracks debug information for JIT code|
||Handles native object file and JIT code diassembly|
||Custom LLVM pass for
||I/O and operating system utility functions|
Some of the
.cpp files form a group that compile to a single object.
The difference between an intrinsic and a builtin is that a builtin is a first class function that can be used like any other Julia function. An intrinsic can operate only on unboxed data, and therefore its arguments must be statically typed.
Building Julia with a different version of LLVM¶
The default version of LLVM is specified in
You can override it by creating a file called
Make.user in the top-level directory and adding a line to it such as:
LLVM_VER = 3.5.0
Besides the LLVM release numerals, you can
LLVM_VER = svn to bulid against the latest development version
Passing options to LLVM¶
You can pass options to LLVM using debug builds of Julia. To create a debug
make debug. The resulting executable is
You can pass LLVM options to this executable via the environment variable
Here are example settings using
export JULIA_LLVM_ARGS = -print-after-alldumps IR after each pass.
export JULIA_LLVM_ARGS = -debug-only=loop-vectorizedumps LLVM
DEBUG(...)diagnostics for loop vectorizer if you built Julia with
LLVM_ASSERTIONS=1. Otherwise you will get warnings about “Unknown command line argument”. Counter-intuitively, building Julia with
LLVM_DEBUG=1is not enough to dump
DEBUGdiagnostics from a pass.
Improving LLVM optimizations for Julia¶
Improving LLVM code generation usually involves either changing Julia lowering to be more friendly to LLVM’s passes, or improving a pass.
If you are planning to improve a pass, be sure to read the
LLVM developer policy.
The best strategy is to create a code example in a form where you can use LLVM’s
opt tool to study it and the pass of interest in isolation.
- Create an example Julia code of interest.
JULIA_LLVM_ARGS = -print-after-allto dump the IR.
- Pick out the IR at the point just before the pass of interest runs.
- Strip the debug metadata and fix up the TBAA metadata by hand.
The last step is labor intensive. Suggestions on a better way would be appreciated.