JIT Execution of LLVM IR Modules with xDSL
xDSL previously required emitting LLVM IR and then compiling to create an executable from a module. This two-step process depended on large dependencies and slowed iteration cycles. The goal became calling functions from Python directly without going via .ll files and shell scripting.
Direct Python Calls Without LLVM IR Files
By mapping xDSL's LLVM backend to the llvmlite IR module the system achieves just-in-time compilation of xDSL modules. This uses the MC JIT compiler which can emit and execute assembly of an llvmlite IR module. Now xDSL can execute native code without a separate lowering and compilation step. This opens new doors for JIT development benchmarking and end-to-end testing.
MC JIT Compiler and llvmlite IR Integration
The MC Just-in-Time compiler handles emitting and executing assembly of an llvmlite IR module. This integration means the compiler can take an xDSL module and produce runnable code directly. The approach removes the traditional pipeline of emitting IR then invoking a compiler.
Example: JIT-Compiling add.mlir
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 script loads an MLIR module file parses it and runs the JIT backend to compile and execute the function. The example adds 21 and 21 producing 42.
Example: PyJIT Frontend With CPython AST
# /// 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
This approach uses the CPython AST through a frontend called PyJIT. It registers arithmetic and function dialects then defines a plus function that the JIT backend compiles. The printed result shows two point zero plus two point zero equals four point zero.
Backend Automation: Optimizations and Execution
The backend handles 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. This automation means developers do not need to manage these steps manually.
Jargon Explained
- MC JIT compiler – a just-in-time compiler that can emit and execute assembly of an llvmlite IR module
- llvmlite IR module – an intermediate representation used as a bridge between high-level code and native machine code
- lowering – the process of converting high-level constructs into lower-level representations suitable for compilation