Skip to main content
Runs the full Enigma compilation pipeline and returns a CompiledKernel.

Signature

enigma.compile(
    fn,
    *jit_args,
    vec_width: int = 0,
    dump_ir: bool = False,
    dump_mlir: bool = False,
    keep_metal_source: bool = False,
    work_dir: str | None = None,
    emit_only: bool | None = None,
) -> CompiledKernel

Parameters

ParameterTypeDefaultDescription
fn@enigma.kernel or @enigma.jitrequiredThe kernel or JIT function to compile
*jit_argsTensor, …For @enigma.jit: positional arguments passed to the JIT function at compile time
vec_widthint0Vectorization lane width. 0 = scalar. 4 = float4 paths
dump_irboolFalsePrint the traced IR to stdout
dump_mlirboolFalsePrint the Enigma dialect MLIR to stdout
keep_metal_sourceboolFalseWrite .metal source file to work_dir
work_dirstr | NoneNoneDirectory for build artifacts. Defaults to a temp directory
emit_onlybool | NoneNoneSkip Metal compilation. None = auto (True on non-macOS). True = emit MSL only (no .metallib)

Returns: CompiledKernel

Attributes

AttributeTypeDescription
kernel_namestrMetal function name, e.g. enigma_kernel_vector_add
metallib_pathstrAbsolute path to the compiled .metallib file
metallib_bytesbytesRaw bytes of the .metallib
metal_sourcestrGenerated Metal Shading Language source
mlir_sourcestr | NoneEnigma dialect MLIR (when dump_mlir=True)
gridtuple[int, int, int] | NoneFor @jit kernels: launch grid computed at compile time
blocktuple[int, int, int] | NoneFor @jit kernels: threadgroup size computed at compile time

Methods

export_metal(path: str) -> str

Write the generated Metal source to a file. Returns the resolved path.
saved = compiled.export_metal("./artifacts/my_kernel.metal")

Usage

Compiling a @enigma.kernel

@enigma.kernel
def add(A: enigma.f32, B: enigma.f32, C: enigma.f32):
    tid = enigma.thread_position_in_grid
    C[tid] = A[tid] + B[tid]

compiled = enigma.compile(add)

With debug output

compiled = enigma.compile(
    add,
    dump_ir=True,
    dump_mlir=True,
    keep_metal_source=True,
    work_dir="./build/enigma",
)
print(compiled.metal_source)

Vectorized compilation

compiled = enigma.compile(add, vec_width=4)
# kernel uses float4 loads/stores when alignment permits

Compiling a @enigma.jit function

Pass the Tensor arguments that the JIT function receives at compile time:
m, n = 256, 512
A = enigma.Tensor("A", 0, "float", enigma.Layout((m, n), (n, 1)))
B = enigma.Tensor("B", 1, "float", enigma.Layout((m, n), (n, 1)))
C = enigma.Tensor("C", 2, "float", enigma.Layout((m, n), (n, 1)))

compiled = enigma.compile(tiled_add, A, B, C)
# compiled.grid and compiled.block are set

Errors

ErrorCause
xcrun exit code errorMetal compiler toolchain not found. Run xcrun -sdk macosx metal -v to diagnose.
EnigmaError: tiler exceeds tensorA layout division produced a tiler larger than the tensor in some mode.
TypeError in tracerInvalid operand types — check that you are not mixing Python values with IRValue in kernel bodies.