Lab 03 — MLIR Python Bindings

Phase: 05 — ML Compilers & IR | Difficulty: ⭐⭐⭐⭐⭐ | Time: 4–5 hours

MLIR is the compiler infrastructure backing IREE, XLA, and Qualcomm's QNN compiler.

What you build

  • Create MLIR modules and functions using Python bindings
  • Build linalg.matmul and arith.addf IR programmatically
  • Count and classify ops in an MLIR module (with regex fallback for environments without MLIR installed)
  • Convert PyTorch FX graph to MLIR func.func dialect
  • Apply MLIR transformation passes

Key concepts

ConceptWhat to understand
DialectsNamespaced op sets: func, arith, linalg, memref, tosa
MLIR vs LLVM IRMLIR is multi-level; ops can be progressively lowered
tosa dialectTarget-agnostic ops used by CoreML, QNN, IREE
Lowering passeslinalg → loops → llvm — each step is a dialect transformation

Files

FilePurpose
lab.pyStubs with TODO markers
solution.pyComplete working implementation
test_lab.pypytest suite
requirements.txtDependencies

Run

pip install -r requirements.txt  # mlir-python-bindings optional
python lab.py
pytest test_lab.py -v