-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[MLIR] Introduce RemarkEngine + pluggable remark streaming (YAML/Bitstream) #152474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,538
−0
Merged
Changes from 8 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
e4292d1
[MLIR] MLIR Integrate LLVM Optimization Remarks Infrastructure
grypp 066fe47
address copilot
grypp fa75d31
add doc
grypp d7a3c03
address comments
grypp f22137f
Split Streamer from RemarlEngine
grypp 521c676
fix doc
grypp d851e0e
add callback
grypp f4d7bd6
Add mlir::remark. RemarkBase->Remark
grypp 514d429
Pass->Passed
grypp 6e9b734
Add detail. Add Arg.
grypp 2bd50fd
move RemarkCategories-> mlir::remark
grypp a825d7f
fix copilot
grypp 2fbb84a
fx
grypp 0c4b386
fix comment
grypp 0b43c0c
add test
grypp a58c20f
improve the doc
grypp 77da6ed
address @joker-eph comments
grypp e113886
Improve document for Failure
grypp 39d4fff
Set better API for enableOptimizationRemarksWithLLVMStreamer
grypp c6970b9
few fixes
grypp 219308f
add LLVM_UNLIKELY
grypp c3be28a
address comments
grypp 3e57446
more test
grypp a7de476
address @razvanlupusoru comments
grypp 24690b2
Slightly change emitRemarks print
grypp 2634a9e
Use functionName
grypp 0c90ab1
add doc
grypp 4746b78
fx
grypp 203355c
add remarkoptions
grypp 9302c26
xf
grypp 3b94875
fx
grypp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| # Remark Infrastructure | ||
|
|
||
| [TOC] | ||
|
|
||
| Remarks are structured, human- and machine-readable notes emitted by passes to | ||
| explain what was optimized, what was missed, and why. The `RemarkEngine` | ||
grypp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| collects finalized remarks during compilation and forwards them to a pluggable | ||
| streamer. A default streamer integrates LLVM’s `llvm::remarks` so you can stream | ||
| while a pass runs and serialize to disk (YAML or LLVM bitstream) for tooling. | ||
|
|
||
| **Key points** | ||
|
|
||
| - **Opt-in**: Disabled by default; zero overhead unless enabled. | ||
| - **Per-context**: Configured on `MLIRContext`. | ||
| - **Formats**: Custom streamers, or LLVM’s Remark engine (YAML / Bitstream). | ||
| - **Kinds**: `Pass`, `Missed`, `Failure`, `Analysis`. | ||
grypp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - **API**: Lightweight streaming interface with `<<` (similar to diagnostics). | ||
|
|
||
| ## How it works | ||
|
|
||
| Remarks has two important classes: | ||
|
|
||
| - **`RemarkEngine`** (owned by `MLIRContext`): receives finalized | ||
| `InFlightRemark`s, optionally mirrors them to the `DiagnosticEngine`, then | ||
| dispatches to the installed streamer. | ||
| - **`MLIRRemarkStreamerBase`** (abstract): backend interface with a single hook | ||
| `streamOptimizationRemark(const Remark &)`. | ||
|
|
||
| **Default backend – `MLIRLLVMRemarkStreamer`** Adapts `mlir::Remark` to | ||
| `llvm::remarks::Remark` and writes YAML/bitstream via | ||
| `llvm::remarks::RemarkStreamer` to a `ToolOutputFile`. | ||
|
|
||
| **Ownership**: `MLIRContext` → `RemarkEngine` → `MLIRRemarkStreamerBase`. | ||
|
|
||
| ## Enable Remarks via mlir::emitRemarks (No Streamer) | ||
|
|
||
| Enable once per `MLIRContext` (e.g., where you build your pass pipeline or in | ||
| your tool). If `printAsEmitRemarks` is true, each remark is also mirrored to the | ||
| context’s `DiagnosticEngine` under the provided category labels—handy for | ||
| interactive tools and tests. | ||
|
|
||
| ```c++ | ||
grypp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| mlir::MLIRContext::RemarkCategories cats{/*passed=*/categoryLoopunroll, | ||
| /*missed=*/std::nullopt, | ||
| /*analysis=*/std::nullopt, | ||
| /*failed=*/categoryLoopunroll}; | ||
|
|
||
| context.enableOptimizationRemarks(/*streamer=*/nullptr, | ||
| cats, | ||
| /*printAsEmitRemarks=*/true); | ||
| ``` | ||
| ## Enable Remarks with LLVMRemarkStreamer (YAML/Bitstream) | ||
| If you want to persist remarks to a file in YAML or bitstream format, use | ||
| `mlir::remark::LLVMRemarkStreamer` (helper shown below): | ||
| You can read more information about [LLVM's Remark from here](https://llvm.org/docs/Remarks.html). | ||
| ```c++ | ||
| #include "mlir/Remark/RemarkStreamer.h" | ||
| mlir::MLIRContext::RemarkCategories cats{/*passed=*/categoryLoopunroll, | ||
| /*missed=*/std::nullopt, | ||
| /*analysis=*/std::nullopt, | ||
| /*failed=*/categoryLoopunroll}; | ||
| mlir::remark::enableOptimizationRemarksToFile( | ||
| context, yamlFile, llvm::remarks::Format::YAML, cats); | ||
| ``` | ||
|
|
||
| ## Emitting remarks from a pass | ||
|
|
||
| The `reportOptimization*` functions return an in-flight remark object (like MLIR | ||
| diagnostics). Append strings and key–value pairs with `<<`. | ||
|
|
||
| ```c++ | ||
| #include "mlir/IR/Remarks.h" | ||
|
|
||
| using namespace mlir; | ||
|
|
||
| LogicalResult MyPass::runOnOperation() { | ||
| Operation *op = getOperation(); | ||
| Location loc = op->getLoc(); | ||
|
|
||
| // PASS: something succeeded | ||
| reportOptimizationPass(loc, /*category=*/"vectorizer", /*passName=*/"MyPass") | ||
| << "vectorized loop." | ||
| << Remark::RemarkKeyValue("tripCount", 128); | ||
|
|
||
| // ANALYSIS: neutral insight | ||
grypp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| reportOptimizationAnalysis(loc, "unroll", "MyPass") | ||
| << "estimated cost: " << Remark::RemarkKeyValue("cost", 42); | ||
|
|
||
| // MISSED: explain why + suggest a fix | ||
| reportOptimizationMiss(loc, "unroll", "MyPass", | ||
| /*suggestion=*/[&](){ return "increase unroll factor to >=4"; }) | ||
| << "not profitable at this size"; | ||
|
|
||
| // FAILURE: action attempted but failed | ||
| if (failed(doThing(op))) { | ||
| reportOptimizationFail(loc, "pipeline", "MyPass") | ||
| << "failed due to unsupported pattern"; | ||
| return failure(); | ||
| } | ||
| return success(); | ||
| } | ||
| ``` | ||
|
|
||
| ### Output formats | ||
|
|
||
| #### YAML | ||
|
|
||
| Readable, easy to diff and grep. | ||
|
|
||
| ```yaml | ||
| --- !Passed | ||
| pass: MyPass | ||
| name: vectorizer | ||
| function: myFunc | ||
| loc: myfile.mlir:12:3 | ||
| args: | ||
| - key: tripCount | ||
| value: 128 | ||
| message: "vectorized loop with tripCount=128" | ||
| ``` | ||
| #### Bitstream | ||
| Compact binary format supported by LLVM’s remark tooling. Prefer this for large | ||
| production runs or when existing infrastructure already consumes LLVM remarks. | ||
| ## Enable Remarks with a Custom Streamer | ||
| `RemarkEngine` talks to `MLIRRemarkStreamerBase`. Implement your own streamer to | ||
| consume remarks in any format you like: | ||
|
|
||
| ```c++ | ||
| class MyStreamer : public MLIRRemarkStreamerBase { | ||
| public: | ||
| void streamOptimizationRemark(const Remark &remark) override { | ||
| // Convert Remark to your format and write it out. | ||
| } | ||
| }; | ||
| // ... | ||
| auto myStreamer = std::make_unique<MyStreamer>(); | ||
| context.setupOptimizationRemarks(path, | ||
| std::move(myStreamer), | ||
| /*printAsEmitRemarks=*/false, | ||
| /*categories=*/cat); | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.