Skip to main content

jit_export

Attribute Macro jit_export 

Source
#[jit_export]
Expand description

Annotate a Rust function so it can be called from JIT-compiled Cranelift IR.

Generates a sibling module <fn_name>_jit exposing helpers that hide the boilerplate of (1) registering the symbol with JITBuilder, (2) building the Cranelift Signature, (3) declaring the import in the module, (4) declaring a local FuncRef, and (5) emitting the call.

If the function has no explicit ABI, extern "C" is added automatically.

§Generated API

For fn foo(p1: T1, p2: T2) -> R the macro emits, alongside the function:

pub mod foo_jit {
    pub const NAME: &'static str;
    pub fn symbol_addr() -> *const u8;
    pub fn register(jb: &mut JITBuilder);
    pub fn signature<M: Module>(module: &M) -> Signature;
    pub fn declare<M: Module>(module: &mut M) -> FuncId;
    pub fn call<M, A1, A2>(
        bcx: &mut FunctionBuilder,
        module: &mut M,
        id: FuncId,
        p1: A1, p2: A2,
    ) -> Value;          // or Inst when R is unit
}

Each A_i: JitArg, so users can pass either an already-lowered IR Value or a Rust constant (&'static str, i64, *const T, …).

§Panics

The generated declare helper unwraps declare_function with expect. It will panic if the symbol is already declared under the same name or if the module rejects the declaration for another reason (use the module API directly if you need non-panicking error handling).

§Return value of call

When the annotated function returns a Rust value, call returns the callee’s first SSA result (cranelift_codegen::ir::Value, via inst_results). When the return type is unit (no return / -> ()), call returns the cranelift_codegen::ir::Inst from the emitted call instead; you can discard it for side-effect-only calls or keep it if you need the instruction handle.