Skip to content

JIT Execution of LLVM IR Modules

Following on from the previous blog post on emitting LLVM IR using xDSL, we now describe how to run it. Previously, going from an xDSL module to an executable meant first emitting the LLVM IR and then compiling. Setting up these two additional steps is tedious, depends on large dependencies and slows down iteration cycles. We wanted to be able to call the function from Python directly, without going via .ll files and shell scripting. By mapping xDSL’s LLVM backend to the llvmlite IR module we get to just-in-time compile our xDSL module. This is done by using the MC Just-in-Time compiler which can emit and execute assembly of an llvmlite IR module. xDSL is now able to execute native code, without a separate lowering and compilation step, opening up new doors for JIT development, benchmarking and end-to-end testing.

For example, to just-in-time compile this add.mlir file with jit.py:

builtin.module {
  llvm.func @main() -> i32 {
    %left = llvm.mlir.constant(21 : i32) : i32
    %right = llvm.mlir.constant(21 : i32) : i32
    %result = llvm.add %left, %right : i32
    llvm.return %result : i32
  }
}
# /// script
# requires-python = ">=3.11"
# dependencies = ["xdsl[llvm]"]
# ///
import sys

from xdsl.context import Context
from xdsl.dialects import builtin, llvm
from xdsl.jit.llvm.backend import LLVMJITBackend
from xdsl.parser import Parser

ctx = Context()
ctx.load_dialect(builtin.Builtin)
ctx.load_dialect(llvm.LLVM)

with open(sys.argv[1]) as f:
    module = Parser(ctx, f.read()).parse_module()

backend = LLVMJITBackend(lowering=())
main = backend.jit(module, "main", ctx)

print(main.c_func())
uv run jit.py add.mlir  # 42

This functionality is also provided directly through the CPython AST, in a frontend called “PyJIT”, as shown in the following Python script:

# /// script
# requires-python = ">=3.11"
# dependencies = ["xdsl[llvm]"]
# ///

from typing import Callable

from xdsl.dialects import arith, builtin, func
from xdsl.jit.context import JITContext, register_builtin_type_maps
from xdsl.jit.llvm.backend import LLVMJITBackend

ctx = JITContext(LLVMJITBackend())

ctx.pyast_ctx.register_function(float.__add__, arith.AddfOp)
ctx.pyast_ctx.register_dialect(arith.Arith)
ctx.pyast_ctx.register_dialect(builtin.Builtin)
ctx.pyast_ctx.register_dialect(func.Func)

register_builtin_type_maps(ctx)

@ctx.jit(Callable[[float, float], float])
def plus(a: float, b: float) -> float:
    return a + b

print(f"{plus(2.0, 2.0) = }")  # plus(2.0, 2.0) = 4.0

The backend takes care of selecting the native target, applying LLVM optimizations, compiling the module, deriving the function signature, and keeping the execution engine alive for as long as the compiled function can be called.

Please find a detailed user guide on the documentation page.