Step 01 · Why MLIR?
LLVM IR is a great low-level representation, but it has one level. By the time a high-level construct (a tensor reshape, a SQL plan, a distributed task) becomes LLVM IR, all its structure is gone and domain-specific optimisation is much harder.
MLIR (Multi-Level IR) solves this by letting you define dialects that live alongside one another in the same module. You can write your compiler as a series of dialect-to-dialect lowerings, each step losing only the structure you no longer need.
Origins
Born out of TensorFlow's compiler stack at Google, contributed to the LLVM project. Today MLIR is the foundation of:
- TensorFlow's XLA / IREE
- Mojo (Modular)
- Triton (OpenAI's GPU DSL)
- CIRCT (hardware design)
- Polygeist (C → MLIR)
- Flang (Fortran)
What we gain
- Composability: pick the right abstraction for each pass.
- Reuse: standard dialects (
arith,cf,memref,linalg,vector,gpu, …) give you an enormous toolbox. - Lower-bar custom dialects: ODS (TableGen) generates op classes.
- Common verifier / printer / parser infrastructure.
What we don't tackle in cp-13
Defining a custom dialect in C++ via ODS. That's a multi-day deep dive
and tightly coupled to LLVM/MLIR version specifics. cp-18 leaves a
spec exercise; here we focus on understanding the IR shape and the
lowering toolchain by emitting the llvm dialect.