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.matmulandarith.addfIR programmatically - Count and classify ops in an MLIR module (with regex fallback for environments without MLIR installed)
- Convert PyTorch FX graph to MLIR
func.funcdialect - Apply MLIR transformation passes
Key concepts
| Concept | What to understand |
|---|---|
| Dialects | Namespaced op sets: func, arith, linalg, memref, tosa |
| MLIR vs LLVM IR | MLIR is multi-level; ops can be progressively lowered |
tosa dialect | Target-agnostic ops used by CoreML, QNN, IREE |
| Lowering passes | linalg → loops → llvm — each step is a dialect transformation |
Files
| File | Purpose |
|---|---|
lab.py | Stubs with TODO markers |
solution.py | Complete working implementation |
test_lab.py | pytest suite |
requirements.txt | Dependencies |
Run
pip install -r requirements.txt # mlir-python-bindings optional
python lab.py
pytest test_lab.py -v