Step 07 · When to reach for MLIR

MLIR is heavyweight. Reach for it when at least one of these is true:

  1. 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.
  2. You'll write many compilers, sharing infrastructure. A team building five DSLs benefits from a shared dialect ecosystem.
  3. You need polyhedral / loop-nest transforms — the affine and linalg dialects have no LLVM-only equivalent.
  4. You target heterogeneous hardware — GPU, TPU, FPGA. The gpu, nvvm, rocdl, spirv, and circt dialects 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 the llvm dialect. 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.