Step 07 · When to reach for MLIR
MLIR is heavyweight. Reach for it when at least one of these is true:
- Multiple abstraction layers are useful. A SQL planner, a tensor compiler, an HDL toolchain, a DSL with high-level semantic passes — anywhere you want to optimise before losing structure.
- You'll write many compilers, sharing infrastructure. A team building five DSLs benefits from a shared dialect ecosystem.
- You need polyhedral / loop-nest transforms — the
affineandlinalgdialects have no LLVM-only equivalent. - You target heterogeneous hardware — GPU, TPU, FPGA. The
gpu,nvvm,rocdl,spirv, andcirctdialects let you keep one front-end.
When not to use MLIR:
- Simple scripting language → LLVM directly (cp-11/12 path).
- Single-target compiler with no high-level structure to preserve.
- Time/iteration is short — MLIR's per-dialect ceremony is real cost.
What we'd build next in this curriculum
- cp-18 (capstone) sketches a
minilang.*dialect with one custom high-level pass (escape analysis to elide allocations) plus a manual conversion to thellvmdialect. The infrastructure cost is large; the educational payoff is understanding the one-source/many-targets pattern that MLIR enables.
Resources
- The MLIR Toy tutorial (chapters 1–7) is the canonical hands-on introduction — it builds a tiny tensor language end-to-end.
- "The Architecture of Open Source Applications" entry on MLIR.
- The MLIR Open Meeting talks (mlir.llvm.org), especially the dialect spotlights.