Skip to content

Backend

backend

LLVMRawJITFunc dataclass

Bases: RawJITFunc

:class:RawJITFunc that retains LLVM MCJIT runtime objects.

The engine and related objects must remain referenced for as long as c_func may be called.

Source code in xdsl/jit/llvm/backend.py
25
26
27
28
29
30
31
32
33
34
35
36
37
@dataclass(slots=True)
class LLVMRawJITFunc(RawJITFunc):
    """
    :class:`RawJITFunc` that retains LLVM MCJIT runtime objects.

    The engine and related objects must remain referenced for as long as
    ``c_func`` may be called.
    """

    target: Target
    target_machine: TargetMachine
    backing_mod: ModuleRef
    engine: ExecutionEngine

target: Target instance-attribute

target_machine: TargetMachine instance-attribute

backing_mod: ModuleRef instance-attribute

engine: ExecutionEngine instance-attribute

__init__(c_func_type: CFuncSignature, c_func: CFunc, target: Target, target_machine: TargetMachine, backing_mod: ModuleRef, engine: ExecutionEngine) -> None

LLVMJITBackend

Bases: JITBackend

:class:JITBackend using xDSL's LLVM converter and llvmlite MCJIT.

Runs :attr:lowering, requires symbol to name an llvm.FuncOp, then converts the module and exposes the entry point through CFFI ABI mode.

Source code in xdsl/jit/llvm/backend.py
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
class LLVMJITBackend(JITBackend):
    """
    :class:`JITBackend` using xDSL's LLVM converter and llvmlite MCJIT.

    Runs :attr:`lowering`, requires ``symbol`` to name an ``llvm.FuncOp``, then
    converts the module and exposes the entry point through CFFI ABI mode.
    """

    lowering: tuple[ModulePass, ...]
    """Pass pipeline applied before resolving ``symbol``."""

    def __init__(
        self,
        lowering: tuple[ModulePass, ...] = (
            MLIROptPass(
                arguments=("--convert-arith-to-llvm", "--convert-func-to-llvm"),
                generic=True,
            ),
        ),
    ):
        """Construct the backend with the given ``lowering`` pipeline."""
        super().__init__()
        register_builtin_types(self.c_type_context)
        register_llvm_types(self.c_type_context)
        self.lowering = lowering

    def jit(
        self,
        mlir_module: builtin.ModuleOp,
        symbol: str,
        ir_context: Context,
    ) -> RawJITFunc:
        """Lower ``mlir_module``, bind ``symbol``, and return an :class:`LLVMRawJITFunc`."""
        # `jit` may be called more than once against the same context
        if llvm.LLVM.name not in ir_context.registered_dialect_names:
            ir_context.load_dialect(llvm.LLVM)
        PassPipeline(self.lowering).apply(ir_context, mlir_module)
        func_op = SymbolTable.lookup_symbol(mlir_module, symbol)
        if func_op is None:
            raise JITException(f"No symbol to JIT compile: {symbol}")
        if not isinstance(func_op, llvm.FuncOp):
            raise JITException(
                f"Symbol {symbol} is a {func_op.name}, not an llvm.func: "
                "the lowering must leave it in the LLVM dialect"
            )
        c_func_type = to_c_func_type(self.c_type_context, func_op.function_type)
        target, target_machine = _create_target_machine()
        llvm_module = convert_module(mlir_module, fallback_target_triple=None)
        return _compile_module(
            llvm_module,
            symbol,
            c_func_type,
            target=target,
            target_machine=target_machine,
        )

lowering: tuple[ModulePass, ...] = lowering instance-attribute

Pass pipeline applied before resolving symbol.

__init__(lowering: tuple[ModulePass, ...] = (MLIROptPass(arguments=('--convert-arith-to-llvm', '--convert-func-to-llvm'), generic=True),))

Construct the backend with the given lowering pipeline.

Source code in xdsl/jit/llvm/backend.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def __init__(
    self,
    lowering: tuple[ModulePass, ...] = (
        MLIROptPass(
            arguments=("--convert-arith-to-llvm", "--convert-func-to-llvm"),
            generic=True,
        ),
    ),
):
    """Construct the backend with the given ``lowering`` pipeline."""
    super().__init__()
    register_builtin_types(self.c_type_context)
    register_llvm_types(self.c_type_context)
    self.lowering = lowering

jit(mlir_module: builtin.ModuleOp, symbol: str, ir_context: Context) -> RawJITFunc

Lower mlir_module, bind symbol, and return an :class:LLVMRawJITFunc.

Source code in xdsl/jit/llvm/backend.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
def jit(
    self,
    mlir_module: builtin.ModuleOp,
    symbol: str,
    ir_context: Context,
) -> RawJITFunc:
    """Lower ``mlir_module``, bind ``symbol``, and return an :class:`LLVMRawJITFunc`."""
    # `jit` may be called more than once against the same context
    if llvm.LLVM.name not in ir_context.registered_dialect_names:
        ir_context.load_dialect(llvm.LLVM)
    PassPipeline(self.lowering).apply(ir_context, mlir_module)
    func_op = SymbolTable.lookup_symbol(mlir_module, symbol)
    if func_op is None:
        raise JITException(f"No symbol to JIT compile: {symbol}")
    if not isinstance(func_op, llvm.FuncOp):
        raise JITException(
            f"Symbol {symbol} is a {func_op.name}, not an llvm.func: "
            "the lowering must leave it in the LLVM dialect"
        )
    c_func_type = to_c_func_type(self.c_type_context, func_op.function_type)
    target, target_machine = _create_target_machine()
    llvm_module = convert_module(mlir_module, fallback_target_triple=None)
    return _compile_module(
        llvm_module,
        symbol,
        c_func_type,
        target=target,
        target_machine=target_machine,
    )