This document explains the design and implementation of Julia's JIT, after codegen has finished and unoptimized LLVM IR has been produced. The JIT is responsible for optimizing and compiling this IR to machine code, and for linking it into the current process and making the code available for execution.
The JIT is responsible for managing compilation resources, looking up previously compiled code, and compiling new code. It is primarily built on LLVM'sOn-Request-Compilation (ORCv2) technology, which provides support for a number of useful features such as concurrent compilation, lazy compilation, and the ability to compile code in a separate process. Though LLVM provides a basic JIT compiler in the form of LLJIT, Julia uses many ORCv2 APIs directly to create its own custom JIT compiler.
Codegen produces an LLVM module containing IR for one or more Julia functions from the original Julia SSA IR produced by type inference (labeled as translate on the compiler diagram above). It also produces a mapping of code-instance to LLVM function name. However, though some optimizations have been applied by the Julia-based compiler on Julia IR, the LLVM IR produced by codegen still contains many opportunities for optimization. Thus, the first step the JIT takes is to run a target-independent optimization pipeline[tdp] on the LLVM module. Then, the JIT runs a target-dependent optimization pipeline, which includes target-specific optimizations and code generation, and outputs an object file. Finally, the JIT links the resulting object file into the current process and makes the code available for execution. All of this is controlled by code insrc/jitlayers.cpp
.
Currently, only one thread at a time is permitted to enter the optimize-compile-link pipeline at a time, due to restrictions imposed by one of our linkers (RuntimeDyld). However, the JIT is designed to support concurrent optimization and compilation, and the linker restriction is expected to be lifted in the future when RuntimeDyld has been fully superseded on all platforms.
The optimization pipeline is based off LLVM's new pass manager, but the pipeline is customized for Julia's needs. The pipeline is defined insrc/pipeline.cpp
, and broadly proceeds through a number of stages as detailed below.
SimplifyCFG
(simplify control flow graph),DCE
(dead code elimination), andSROA
(scalar replacement of aggregates) are some of the key players here.EarlyCSE
is used to perform common subexpression elimination, andInstCombine
andInstSimplify
perform a number of small peephole optimizations to make operations less expensive.LoopRotate
,LICM
, andLoopFullUnroll
. Some bounds check elimination also happens here, as a result of theIRCE
pass which can prove certain bounds are never exceeded.GVN
(global value numbering),SCCP
(sparse conditional constant propagation), and another round of bounds check elimination. These passes are expensive, but they can often remove large amounts of code and make vectorization much more successful and effective. Several other simplification and optimization passes intersperse the more expensive ones to reduce the amount of work they have to do.LLVM provides target-dependent optimization and machine code generation in the same pipeline, located in the TargetMachine for a given platform. These passes include instruction selection, instruction scheduling, register allocation, and machine code emission. The LLVM documentation provides a good overview of the process, and the LLVM source code is the best place to look for details on the pipeline and passes.
Currently, Julia is transitioning between two linkers: the older RuntimeDyld linker, and the newerJITLink linker. JITLink contains a number of features that RuntimeDyld does not have, such as concurrent and reentrant linking, but currently lacks good support for profiling integrations and does not yet support all of the platforms that RuntimeDyld supports. Over time, JITLink is expected to replace RuntimeDyld entirely. Further details on JITLink can be found in the LLVM documentation.
Once the code has been linked into the current process, it is available for execution. This fact is made known to the generating codeinst by updating theinvoke
,specsigflags
, andspecptr
fields appropriately. Codeinsts support upgradinginvoke
,specsigflags
, andspecptr
fields, so long as every combination of these fields that exists at any given point in time is valid to be called. This allows the JIT to update these fields without invalidating existing codeinsts, supporting a potential future concurrent JIT. Specifically, the following states may be valid:
invoke
is NULL,specsigflags
is 0b00,specptr
is NULLinvoke
is non-null,specsigflags
is 0b00,specptr
is NULLinvoke
does not read either thespecsigflags
orspecptr
fields, and therefore they may be modified without invalidating theinvoke
pointer.invoke
is non-null,specsigflags
is 0b10,specptr
is non-nullinvoke
is non-null,specsigflags
is 0b11,specptr
is non-nullspecptr
field contains a pointer to the specialized function signature. Theinvoke
pointer is permitted to read bothspecsigflags
andspecptr
fields.In addition, there are a number of different transitional states that occur during the update process. To account for these potential situations, the following write and read patterns should be used when dealing with these codeinst fields.
invoke
,specsigflags
, andspecptr
:specptr
was non-null, cease the write operation and wait for bit 0b10 ofspecsigflags
to be written.specsigflags
to its final value. This may be a relaxed write.invoke
pointer to its final value. This must have at least a release memory ordering to synchronize with reads ofinvoke
.specsigflags
to 1. This must be at least a release memory ordering to synchronize with reads ofspecsigflags
. This step completes the write operation and announces to all other threads that all fields have been set.invoke
,specsigflags
, andspecptr
:invoke
field with at least an acquire memory ordering. This load will be referred to asinitial_invoke
.initial_invoke
is NULL, the codeinst is not yet executable.invoke
is NULL,specsigflags
may be treated as 0b00,specptr
may be treated as NULL.specptr
field with at least an acquire memory ordering.specptr
is NULL, then theinitial_invoke
pointer must not be relying onspecptr
to guarantee correct execution. Therefore,invoke
is non-null,specsigflags
may be treated as 0b00,specptr
may be treated as NULL.specptr
is non-null, theninitial_invoke
might not be the finalinvoke
field that usesspecptr
. This can occur ifspecptr
has been written, butinvoke
has not yet been written. Therefore, spin on the second bit ofspecsigflags
until it is set to 1 with at least acquire memory ordering.invoke
field with at least an acquire memory ordering. This load will be referred to asfinal_invoke
.specsigflags
field with any memory ordering.invoke
isfinal_invoke
,specsigflags
is the value read in step 7,specptr
is the value read in step 3.specptr
to a different but equivalent function pointer:specptr
. Races here must be benign, as the old function pointer is required to still be valid, and any new ones are also required to be valid as well. Once a pointer has been written tospecptr
, it must always be callable whether or not it is later overwritten.Although these write, read, and update steps are complicated, they ensure that the JIT can update codeinsts without invalidating existing codeinsts, and that the JIT can update codeinsts without invalidating existinginvoke
pointers. This allows the JIT to potentially reoptimize functions at higher optimization levels in the future, and also will allow the JIT to support concurrent compilation of functions in the future.
Settings
This document was generated withDocumenter.jl version 1.8.0 onWednesday 9 July 2025. Using Julia version 1.11.6.