$npx -y skills add mohitmishra786/low-level-dev-skills --skill mlirMLIR skill for multi-level intermediate representation. Use when writing custom dialects, defining ops with ODS, writing lowering passes, running mlir-opt, or building ML compilers with Torch-MLIR/IREE. Activates on queries about MLIR, dialect, ODS, mlir-opt, linalg, lowering pas
| 1 | # MLIR |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | Guide agents through MLIR (Multi-Level IR): ops, regions, blocks, and values; built-in dialects (arith, func, memref, affine, linalg); writing custom dialects with ODS; lowering passes with `ConversionPattern`; `mlir-opt` CLI; and ML compiler use cases (Torch-MLIR, IREE). |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Building a domain-specific compiler IR (graphics, ML, hardware DSL) |
| 10 | - Lowering high-level ops to LLVM or GPU dialects |
| 11 | - Writing progressive lowering pipelines (linalg → loops → LLVM) |
| 12 | - Integrating with IREE or Torch-MLIR for ML deployment |
| 13 | - Creating reusable transformation passes across dialects |
| 14 | - Prototyping compiler optimizations at the right abstraction level |
| 15 | |
| 16 | ## Workflow |
| 17 | |
| 18 | ### 1. MLIR structure |
| 19 | |
| 20 | ``` |
| 21 | Module |
| 22 | └── func.func @main() |
| 23 | └── region |
| 24 | └── block ^bb0: |
| 25 | └── operations (ops) producing SSA values |
| 26 | ``` |
| 27 | |
| 28 | Key concepts: |
| 29 | - **Operation** — instruction-like node (`arith.addi`, `memref.load`) |
| 30 | - **Region** — container of blocks (functions, control flow) |
| 31 | - **Block** — CFG node with ordered ops |
| 32 | - **Value** — SSA result of an op or block argument |
| 33 | |
| 34 | ### 2. Built-in dialects |
| 35 | |
| 36 | | Dialect | Purpose | |
| 37 | |---------|---------| |
| 38 | | `arith` | Integer/float arithmetic | |
| 39 | | `func` | Function definitions and calls | |
| 40 | | `memref` | Buffer abstraction with shapes/strides | |
| 41 | | `affine` | Affine loop nests, map/set constraints | |
| 42 | | `linalg` | Structured linear algebra ops | |
| 43 | | `scf` | Structured control flow (for, if) | |
| 44 | | `llvm` | LLVM IR dialect for final lowering | |
| 45 | | `gpu` | GPU kernel launches | |
| 46 | |
| 47 | ```mlir |
| 48 | // example.mlir |
| 49 | func.func @add(%a: memref<4xf32>, %b: memref<4xf32>, %c: memref<4xf32>) { |
| 50 | %c0 = arith.constant 0 : index |
| 51 | %c4 = arith.constant 4 : index |
| 52 | scf.for %i = %c0 to %c4 step %c1 { |
| 53 | %av = memref.load %a[%i] : memref<4xf32> |
| 54 | %bv = memref.load %b[%i] : memref<4xf32> |
| 55 | %sum = arith.addf %av, %bv : f32 |
| 56 | memref.store %sum, %c[%i] : memref<4xf32> |
| 57 | } |
| 58 | return |
| 59 | } |
| 60 | ``` |
| 61 | |
| 62 | ### 3. mlir-opt CLI |
| 63 | |
| 64 | ```bash |
| 65 | # Parse and print |
| 66 | mlir-opt example.mlir |
| 67 | |
| 68 | # Run canonicalization |
| 69 | mlir-opt example.mlir -canonicalize |
| 70 | |
| 71 | # Lower affine to scf |
| 72 | mlir-opt affine.mlir -lower-affine |
| 73 | |
| 74 | # Full pipeline toward LLVM |
| 75 | mlir-opt input.mlir \ |
| 76 | --linalg-bufferize \ |
| 77 | --convert-linalg-to-loops \ |
| 78 | --convert-scf-to-cf \ |
| 79 | --convert-arith-to-llvm \ |
| 80 | --convert-memref-to-llvm \ |
| 81 | --convert-func-to-llvm \ |
| 82 | -o llvm.mlir |
| 83 | ``` |
| 84 | |
| 85 | ### 4. ODS — Operation Definition Specification |
| 86 | |
| 87 | ```tablegen |
| 88 | // MyOps.td |
| 89 | include "mlir/IR/OpBase.td" |
| 90 | |
| 91 | def My_Dialect : Dialect { |
| 92 | let name = "my"; |
| 93 | let summary = "My custom dialect"; |
| 94 | } |
| 95 | |
| 96 | class My_Op<string mnemonic, list<Trait> traits = []> : |
| 97 | Op<My_Dialect, mnemonic, traits>; |
| 98 | |
| 99 | def AddOp : My_Op<"add", [Pure]> { |
| 100 | let summary = "Add two values"; |
| 101 | let arguments = (ins AnyType:$lhs, AnyType:$rhs); |
| 102 | let results = (outs AnyType:$result); |
| 103 | let assemblyFormat = "$lhs `,` $rhs attr-dict `:` type($result)"; |
| 104 | } |
| 105 | ``` |
| 106 | |
| 107 | ```bash |
| 108 | # Generate C++ from TableGen |
| 109 | mlir-tblgen -gen-op-defs MyOps.td -I include/ -o MyOps.cpp.inc |
| 110 | ``` |
| 111 | |
| 112 | ### 5. Custom dialect C++ implementation |
| 113 | |
| 114 | ```cpp |
| 115 | #include "mlir/IR/DialectImplementation.h" |
| 116 | #include "MyDialect.h" |
| 117 | |
| 118 | #include "MyOps.cpp.inc" |
| 119 | |
| 120 | void MyDialect::initialize() { |
| 121 | addOperations< |
| 122 | #define GET_OP_LIST |
| 123 | #include "MyOps.cpp.inc" |
| 124 | >(); |
| 125 | } |
| 126 | |
| 127 | #define GET_OP_CLASSES |
| 128 | #include "MyOps.cpp.inc" |
| 129 | ``` |
| 130 | |
| 131 | ### 6. Lowering passes |
| 132 | |
| 133 | ```cpp |
| 134 | #include "mlir/Conversion/LLVMCommon/ConversionTarget.h" |
| 135 | #include "mlir/Transforms/DialectConversion.h" |
| 136 | |
| 137 | struct AddOpLowering : OpConversionPattern<my::AddOp> { |
| 138 | using OpConversionPattern::OpConversionPattern; |
| 139 | |
| 140 | LogicalResult matchAndRewrite(my::AddOp op, OpAdaptor adaptor, |
| 141 | ConversionPatternRewriter &rewriter) const override { |
| 142 | rewriter.replaceOpWithNewOp<arith::AddIOp>(op, adaptor.getLhs(), adaptor.getRhs()); |
| 143 | return success(); |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | void populateLoweringPatterns(RewritePatternSet &patterns) { |
| 148 | patterns.add<AddOpLowering>(patterns.getContext()); |
| 149 | } |
| 150 | |
| 151 | // In pass: |
| 152 | mlir::ConversionTarget target(*context); |
| 153 | target.addIllegalDialect<my::MyDialect>(); |
| 154 | target.addLegalDialect<arith::ArithDialect>(); |
| 155 | |
| 156 | if (failed(applyPartialConversion(module, target, std::move(patterns)))) |
| 157 | signalPassFailure(); |
| 158 | ``` |
| 159 | |
| 160 | ### 7. linalg for ML compilers |
| 161 | |
| 162 | ```mlir |
| 163 | %0 = linalg.matmul ins(%A, %B : tensor<128x256xf32>, tensor<256x64xf32>) |
| 164 | outs(%C : tensor<128x64xf32>) -> tensor<128x64xf32> |
| 165 | ``` |
| 166 | |
| 167 | Lowering path: `linalg` → `scf` loops → `affine` → `llvm` |
| 168 | |
| 169 | ### 8. Torch-MLIR and IREE |
| 170 | |
| 171 | ```bash |
| 172 | # Torch-MLIR: PyTorch → MLIR |
| 173 | python -m torch_mlir.tools.import-onnx --onnx-model model.onnx -o model.mlir |
| 174 | |
| 175 | # IREE: MLIR → GPU/CPU executable |
| 176 | iree-compile --iree-hal-target-backends=llvm-cpu model.mlir -o model.vmfb |
| 177 | iree-run |