|
| 1 | +# Remark Infrastructure |
| 2 | + |
| 3 | +[TOC] |
| 4 | + |
| 5 | +Optimization remarks are structured, machine- and human-readable notes emitted by passes to explain what was optimized, what was missed, and why. MLIR integrates LLVM’s **remarks** infrastructure to make these insights easy to produce and consume. |
| 6 | + |
| 7 | +**Key points** |
| 8 | + |
| 9 | +- **Opt-in**: Disabled by default. No cost unless enabled. |
| 10 | +- **Per-context**: Configured on `MLIRContext`. |
| 11 | +- **Formats**: YAML or LLVM remark bitstream. |
| 12 | +- **Kinds**: Pass, Missed, Failure, Analysis. |
| 13 | +- **API**: Lightweight stream interface (similar to diagnostics) with `<<`. |
| 14 | + |
| 15 | +## Enabling remarks |
| 16 | + |
| 17 | +Enable once per `MLIRContext` (e.g., in your tool, pass pipeline setup, or test): |
| 18 | + |
| 19 | +```c++ |
| 20 | +#include "mlir/IR/MLIRContext.h" |
| 21 | + |
| 22 | +// Writes remarks to /tmp/remarks.yaml in YAML format and mirrors them to |
| 23 | +// the DiagnosticEngine as 'remark' diagnostics with the given category labels. |
| 24 | +context.setupOptimizationRemarks( |
| 25 | + /*outputPath=*/"/tmp/remarks.yaml", |
| 26 | + /*outputFormat=*/yaml, // or "bitstream" |
| 27 | + /*printAsEmitRemarks=*/true, |
| 28 | + /*categoryPassName=*/"opt.pass", // optional category labels for mirroring |
| 29 | + /*categoryMissName=*/"opt.missed", |
| 30 | + /*categoryAnalysisName=*/"opt.analysis", |
| 31 | + /*categoryFailedName=*/"opt.failed"); |
| 32 | +``` |
| 33 | + |
| 34 | +### Emitting remarks from a pass |
| 35 | + |
| 36 | +The functions `reportOptimization*` return an in-flight remark object (like MLIR diagnostics). One can append strings and key–value pairs with <<. |
| 37 | + |
| 38 | +```c++ |
| 39 | +#include "mlir/IR/Remarks.h" |
| 40 | + |
| 41 | +using namespace mlir; |
| 42 | + |
| 43 | +LogicalResult MyPass::runOnOperation() { |
| 44 | + Operation *op = getOperation(); |
| 45 | + Location loc = op->getLoc(); |
| 46 | + |
| 47 | + // PASS: something succeeded |
| 48 | + reportOptimizationPass(loc, /*category=*/"vectorizer", /*passName=*/"MyPass") |
| 49 | + << "vectorized loop with tripCount=" |
| 50 | + << RemarkBase::RemarkKeyValue("tripCount", 128); |
| 51 | + |
| 52 | + // ANALYSIS: neutral insight |
| 53 | + reportOptimizationAnalysis(loc, "unroll", "MyPass") |
| 54 | + << "estimated cost: " << RemarkBase::RemarkKeyValue("cost", 42); |
| 55 | + |
| 56 | + // MISSED: explain why + suggest a fix |
| 57 | + reportOptimizationMiss(loc, "unroll", "MyPass", |
| 58 | + /*suggestion=*/"increase unroll factor to >=4") |
| 59 | + << "not profitable at this size"; |
| 60 | + |
| 61 | + // FAILURE: action attempted but failed |
| 62 | + if (failed(doThing(op))) { |
| 63 | + reportOptimizationFail(loc, "pipeline", "MyPass") |
| 64 | + << "failed due to unsupported pattern"; |
| 65 | + return failure(); |
| 66 | + } |
| 67 | + return success(); |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +## Output formats |
| 72 | + |
| 73 | +#### YAML |
| 74 | + |
| 75 | +A typical remark serialized to YAML looks like following. It is Readable, easy to diff and grep. |
| 76 | + |
| 77 | +```yaml |
| 78 | +--- !Passed |
| 79 | +pass: MyPass |
| 80 | +name: vectorizer |
| 81 | +function: myFunc |
| 82 | +loc: myfile.mlir:12:3 |
| 83 | +args: |
| 84 | + - key: tripCount |
| 85 | + value: 128 |
| 86 | +message: "vectorized loop with tripCount=128" |
| 87 | +``` |
| 88 | +
|
| 89 | +#### Bitstream |
| 90 | +
|
| 91 | +Compact binary format supported by LLVM’s remark tooling. Prefer this for large production runs or where existing infrastructure already understands LLVM remarks. |
0 commit comments