MLIR Backend (Experimental)
Status: 1D scaffold, off by default behind the mlir cargo feature
Crate: compiler/mlir-backend
Library: melior 0.25.1 (Rust MLIR bindings, LLVM/MLIR 20)
Overview
The MLIR backend is the future tensor / autodiff / GPU lowering path (Phase 2+). It consumes the
same typed High-Level IR (neuro-hir) the LLVM backend consumes. As of the
1D scaffold it emits a trivial, verifier-clean MLIR module, one func.func declaration per
function and impl method, proving the HIR → melior → verified-MLIR pipeline end-to-end. Real
body lowering (linalg / tensor dialects) is Phase 2+.
Feature Gate
The path is opt-in behind the off-by-default mlir feature
(mlir = ["dep:melior", "dep:thiserror", "dep:neuro-hir"]):
- Disabled (default): the crate compiles to an empty placeholder and pulls in no MLIR toolchain
(nor
neuro-hir), socargo build/test --workspaceworks on a stock LLVM 20 install with no MLIR on every CI OS. - Enabled: pulls in
melior+neuro-hirand exposes the entry points below. CI provisions MLIR only on Linux, where the--all-featureslint job and a dedicatedcargo test -p mlir-backend --features mlirsmoke step exercise the gated code; the Windows/macOS legs build the placeholder.
See Installation → Optional: MLIR Backend for the MLIR 20 + libclang 20 toolchain setup.
Entry Points (feature mlir)
pub fn lower_program(program: &HirProgram) -> Result<String, MlirError>;pub fn emit_smoke_module() -> Result<String, MlirError>;lower_program, the HIR → MLIR scaffold: registers all dialects, walks the typed HIR, and returns the textual form of a verified module offunc.funcdeclarations.emit_smoke_module, the HIR-independentmeliorwiring check: builds + verifiesfunc.func @neuro_smoke(index, index) -> indexwith anarith.addibody.
Lowering Rules (scaffold)
- Free functions and
implmethods becomefunc.funcdeclarations (empty region, private visibility, external symbols, not definitions). A method receiver lowers to a pointer parameter. Structs and constants are skipped. - HIR scalar types map to MLIR scalars:
i8toi64,i1forbool,i32forchar,f16/bf16/f32/f64. - Every aggregate / reference / string type maps to an opaque
!llvm.ptruntil real tensor and struct lowering lands (Phase 2+). voidis the empty result list in return position; anywhere else it is aMlirError::UnsupportedType.- Function bodies are intentionally not lowered yet; that is the Phase 2 linalg/tensor work. The module is run through the MLIR verifier before its textual form is returned.
Coexistence with inkwell
mlir-sys carries no llvm-sys dependency and links its own MLIR key, so it coexists with
inkwell's llvm-20 link without a Cargo links conflict. Pointing MLIR_SYS_200_PREFIX /
TABLEGEN_200_PREFIX at the same LLVM 20 build as LLVM_SYS_201_PREFIX makes both bindings share one
libLLVM-20 dylib. That prefix must include MLIR (mlir-c headers + libMLIR*); Arch's stock
llvm20 omits MLIR, so build LLVM 20 with -DLLVM_ENABLE_PROJECTS=mlir.
melior 0.25.1 is the newest release targeting MLIR 20 (via mlir-sys 0.5.0); melior 0.26+ moved
to MLIR 21/22.
Resources
- mlir-backend CONTEXT, slice contract
- melior, Rust MLIR bindings
- MLIR, Multi-Level Intermediate Representation