Skip to content

Commit d724f85

Browse files
committed
fx
1 parent 88f649d commit d724f85

File tree

5 files changed

+91
-83
lines changed

5 files changed

+91
-83
lines changed

mlir/include/mlir/IR/Remarks.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace mlir::remark {
2929
/// Define an the set of categories to accept. By default none are, the provided
3030
/// regex matches against the category names for each kind of remark.
3131
struct RemarkCategories {
32-
std::optional<std::string> passed, missed, analysis, failed;
32+
std::optional<std::string> all, passed, missed, analysis, failed;
3333
};
3434

3535
/// Categories describe the outcome of an transformation, not the mechanics of

mlir/lib/IR/Remarks.cpp

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,17 +248,56 @@ RemarkEngine::initialize(std::unique_ptr<MLIRRemarkStreamerBase> streamer,
248248
return success();
249249
}
250250

251+
/// Returns true if filter is already anchored like ^...$
252+
static bool isAnchored(llvm::StringRef s) {
253+
s = s.trim();
254+
return s.starts_with("^") && s.ends_with("$"); // note: startswith/endswith
255+
}
256+
257+
/// Anchor the entire pattern so it matches the whole string.
258+
static std::string anchorWhole(llvm::StringRef filter) {
259+
if (isAnchored(filter))
260+
return filter.str();
261+
return (llvm::Twine("^(") + filter + ")$").str();
262+
}
263+
264+
/// Build a combined filter from cats.all and a category-specific pattern.
265+
/// If neither is present, return std::nullopt. Otherwise "(all|specific)"
266+
/// and anchor once. Also validate before returning.
267+
static std::optional<llvm::Regex>
268+
buildFilter(const mlir::remark::RemarkCategories &cats,
269+
const std::optional<std::string> &specific) {
270+
llvm::SmallVector<llvm::StringRef, 2> parts;
271+
if (cats.all && !cats.all->empty())
272+
parts.emplace_back(*cats.all);
273+
if (specific && !specific->empty())
274+
parts.emplace_back(*specific);
275+
276+
if (parts.empty())
277+
return std::nullopt;
278+
279+
std::string joined = llvm::join(parts, "|");
280+
std::string anchored = anchorWhole(joined);
281+
282+
llvm::Regex rx(anchored);
283+
std::string err;
284+
if (!rx.isValid(err))
285+
return std::nullopt;
286+
287+
return rx;
288+
}
289+
251290
RemarkEngine::RemarkEngine(bool printAsEmitRemarks,
252291
const RemarkCategories &cats)
253292
: printAsEmitRemarks(printAsEmitRemarks) {
254293
if (cats.passed)
255-
passedFilter = llvm::Regex(cats.passed.value());
294+
passedFilter = buildFilter(cats, cats.passed);
256295
if (cats.missed)
257-
missFilter = llvm::Regex(cats.missed.value());
296+
missFilter = buildFilter(cats, cats.missed);
258297
if (cats.analysis)
259-
analysisFilter = llvm::Regex(cats.analysis.value());
298+
analysisFilter = buildFilter(cats, cats.analysis);
260299
if (cats.failed)
261-
failedFilter = llvm::Regex(cats.failed.value());
300+
failedFilter = buildFilter(cats, cats.failed);
262301
}
263302

264303
llvm::LogicalResult mlir::remark::enableOptimizationRemarks(

mlir/lib/Tools/mlir-opt/MlirOptMain.cpp

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -517,51 +517,39 @@ performActions(raw_ostream &os,
517517

518518
context->enableMultithreading(wasThreadingEnabled);
519519

520-
// Set up optimization remarks.
521-
if (config.shouldEmitRemarks()) {
522-
auto combine = [](const std::string &a, const std::string &b) {
523-
if (a.empty())
524-
return b;
525-
if (b.empty())
526-
return a;
527-
return a + "|" + b;
528-
};
529-
530-
remark::RemarkCategories cats{
531-
combine(config.getRemarksAllFilter(), config.getRemarksPassedFilter()),
532-
combine(config.getRemarksAllFilter(), config.getRemarksMissedFilter()),
533-
combine(config.getRemarksAllFilter(), config.getRemarksAnalyseFilter()),
534-
combine(config.getRemarksAllFilter(), config.getRemarksFailedFilter())};
520+
remark::RemarkCategories cats{
521+
config.getRemarksAllFilter(), config.getRemarksPassedFilter(),
522+
config.getRemarksMissedFilter(), config.getRemarksAnalyseFilter(),
523+
config.getRemarksFailedFilter()};
535524

536-
mlir::MLIRContext &ctx = *context;
525+
mlir::MLIRContext &ctx = *context;
537526

538-
switch (config.getRemarkFormat()) {
539-
case REMARK_FORMAT_STDOUT:
540-
if (failed(mlir::remark::enableOptimizationRemarks(
541-
ctx, nullptr, cats, true /*printAsEmitRemarks*/)))
542-
return failure();
543-
break;
544-
545-
case REMARK_FORMAT_YAML: {
546-
std::string file = config.getRemarksOutputFile().empty()
547-
? "mlir-remarks.yaml"
548-
: config.getRemarksOutputFile();
549-
if (failed(mlir::remark::enableOptimizationRemarksWithLLVMStreamer(
550-
ctx, file, llvm::remarks::Format::YAML, cats)))
551-
return failure();
552-
break;
553-
}
527+
switch (config.getRemarkFormat()) {
528+
case REMARK_FORMAT_STDOUT:
529+
if (failed(mlir::remark::enableOptimizationRemarks(
530+
ctx, nullptr, cats, true /*printAsEmitRemarks*/)))
531+
return failure();
532+
break;
533+
534+
case REMARK_FORMAT_YAML: {
535+
std::string file = config.getRemarksOutputFile().empty()
536+
? "mlir-remarks.yaml"
537+
: config.getRemarksOutputFile();
538+
if (failed(mlir::remark::enableOptimizationRemarksWithLLVMStreamer(
539+
ctx, file, llvm::remarks::Format::YAML, cats)))
540+
return failure();
541+
break;
542+
}
554543

555-
case REMARK_FORMAT_BITSTREAM: {
556-
std::string file = config.getRemarksOutputFile().empty()
557-
? "mlir-remarks.bitstream"
558-
: config.getRemarksOutputFile();
559-
if (failed(mlir::remark::enableOptimizationRemarksWithLLVMStreamer(
560-
ctx, file, llvm::remarks::Format::Bitstream, cats)))
561-
return failure();
562-
break;
563-
}
564-
}
544+
case REMARK_FORMAT_BITSTREAM: {
545+
std::string file = config.getRemarksOutputFile().empty()
546+
? "mlir-remarks.bitstream"
547+
: config.getRemarksOutputFile();
548+
if (failed(mlir::remark::enableOptimizationRemarksWithLLVMStreamer(
549+
ctx, file, llvm::remarks::Format::Bitstream, cats)))
550+
return failure();
551+
break;
552+
}
565553
}
566554

567555
// Prepare the pass manager, applying command-line and reproducer options.

mlir/test/Pass/remarks.mlir

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,28 @@
1-
// RUN: mlir-opt %s --test-remark --remarks-passed="category" 2>&1 | FileCheck %s -check-prefix=CHECK-PASSED
2-
// RUN: mlir-opt %s --test-remark --remarks-missed="category" 2>&1 | FileCheck %s -check-prefix=CHECK-MISSED
3-
// RUN: mlir-opt %s --test-remark --remarks-failed="category" 2>&1 | FileCheck %s -check-prefix=CHECK-FAILED
4-
// RUN: mlir-opt %s --test-remark --remarks-analyse="category" 2>&1 | FileCheck %s -check-prefix=CHECK-ANALYSIS
5-
// RUN: mlir-opt %s --test-remark --remarks="category" 2>&1 | FileCheck %s -check-prefix=CHECK-ALL
6-
// RUN: mlir-opt %s --test-remark --remarks="category-1" 2>&1 | FileCheck %s -check-prefix=CHECK-ALL1
1+
// RUN: mlir-opt %s --test-remark --remarks-passed="category-1-passed" 2>&1 | FileCheck %s -check-prefix=CHECK-PASSED
2+
// RUN: mlir-opt %s --test-remark --remarks-missed="a-category-1-missed" 2>&1 | FileCheck %s -check-prefix=CHECK-MISSED
3+
// RUN: mlir-opt %s --test-remark --remarks-failed="category-2-failed" 2>&1 | FileCheck %s -check-prefix=CHECK-FAILED
4+
// RUN: mlir-opt %s --test-remark --remarks-analyse="category-2-analysis" 2>&1 | FileCheck %s -check-prefix=CHECK-ANALYSIS
5+
// RUN: mlir-opt %s --test-remark --remarks="category.*" 2>&1 | FileCheck %s -check-prefix=CHECK-ALL
6+
// RUN: mlir-opt %s --test-remark --remarks="category-1.*" 2>&1 | FileCheck %s -check-prefix=CHECK-ALL1
77
module @foo {
88
"test.op"() : () -> ()
9-
"test.op2"() : () -> ()
9+
1010
}
1111

1212

1313
// CHECK-PASSED: remarks.mlir:8:3: remark: [Passed] test-remark | Category:category-1-passed | Reason="because we are testing the remark pipeline", Remark="This is a test passed remark", Suggestion="try using the remark pipeline feature"
14-
// CHECK-PASSED: remarks.mlir:9:3: remark: [Passed] test-remark | Category:category-1-passed | Reason="because we are testing the remark pipeline", Remark="This is a test passed remark", Suggestion="try using the remark pipeline feature"
15-
// CHECK-PASSED: remarks.mlir:7:1: remark: [Passed] test-remark | Category:category-1-passed | Reason="because we are testing the remark pipeline", Remark="This is a test passed remark", Suggestion="try using the remark pipeline feature"
16-
17-
// CHECK-MISSED:remarks.mlir:8:3: remark: [Missed] test-remark | Category:category-1-missed | Reason="because we are testing the remark pipeline", Remark="This is a test missed remark", Suggestion="try using the remark pipeline feature"
18-
// CHECK-MISSED:remarks.mlir:9:3: remark: [Missed] test-remark | Category:category-1-missed | Reason="because we are testing the remark pipeline", Remark="This is a test missed remark", Suggestion="try using the remark pipeline feature"
19-
// CHECK-MISSED:remarks.mlir:7:1: remark: [Missed] test-remark | Category:category-1-missed | Reason="because we are testing the remark pipeline", Remark="This is a test missed remark", Suggestion="try using the remark pipeline feature"
20-
14+
// CHECK-MISSED:remarks.mlir:8:3: remark: [Missed] test-remark | Category:a-category-1-missed | Reason="because we are testing the remark pipeline", Remark="This is a test missed remark", Suggestion="try using the remark pipeline feature"
2115
// CHECK-FAILED: remarks.mlir:8:3: remark: [Failure] test-remark | Category:category-2-failed | Reason="because we are testing the remark pipeline", Remark="This is a test failed remark", Suggestion="try using the remark pipeline feature"
22-
// CHECK-FAILED: remarks.mlir:9:3: remark: [Failure] test-remark | Category:category-2-failed | Reason="because we are testing the remark pipeline", Remark="This is a test failed remark", Suggestion="try using the remark pipeline feature"
23-
// CHECK-FAILED: remarks.mlir:7:1: remark: [Failure] test-remark | Category:category-2-failed | Reason="because we are testing the remark pipeline", Remark="This is a test failed remark", Suggestion="try using the remark pipeline feature"
24-
2516
// CHECK-ANALYSIS: remarks.mlir:8:3: remark: [Analysis] test-remark | Category:category-2-analysis | Remark="This is a test analysis remark"
26-
// CHECK-ANALYSIS: remarks.mlir:9:3: remark: [Analysis] test-remark | Category:category-2-analysis | Remark="This is a test analysis remark"
27-
// CHECK-ANALYSIS: remarks.mlir:7:1: remark: [Analysis] test-remark | Category:category-2-analysis | Remark="This is a test analysis remark"
2817

2918

30-
// CHECK-ALL: remarks.mlir:8:3: remark: [Missed] test-remark | Category:category-1-missed | Reason="because we are testing the remark pipeline", Remark="This is a test missed remark", Suggestion="try using the remark pipeline feature"
3119
// CHECK-ALL: remarks.mlir:8:3: remark: [Passed] test-remark | Category:category-1-passed | Reason="because we are testing the remark pipeline", Remark="This is a test passed remark", Suggestion="try using the remark pipeline feature"
3220
// CHECK-ALL: remarks.mlir:8:3: remark: [Failure] test-remark | Category:category-2-failed | Reason="because we are testing the remark pipeline", Remark="This is a test failed remark", Suggestion="try using the remark pipeline feature"
3321
// CHECK-ALL: remarks.mlir:8:3: remark: [Analysis] test-remark | Category:category-2-analysis | Remark="This is a test analysis remark"
34-
// CHECK-ALL: remarks.mlir:9:3: remark: [Missed] test-remark | Category:category-1-missed | Reason="because we are testing the remark pipeline", Remark="This is a test missed remark", Suggestion="try using the remark pipeline feature"
35-
// CHECK-ALL: remarks.mlir:9:3: remark: [Passed] test-remark | Category:category-1-passed | Reason="because we are testing the remark pipeline", Remark="This is a test passed remark", Suggestion="try using the remark pipeline feature"
36-
// CHECK-ALL: remarks.mlir:9:3: remark: [Failure] test-remark | Category:category-2-failed | Reason="because we are testing the remark pipeline", Remark="This is a test failed remark", Suggestion="try using the remark pipeline feature"
37-
// CHECK-ALL: remarks.mlir:9:3: remark: [Analysis] test-remark | Category:category-2-analysis | Remark="This is a test analysis remark"
38-
// CHECK-ALL: remarks.mlir:7:1: remark: [Missed] test-remark | Category:category-1-missed | Reason="because we are testing the remark pipeline", Remark="This is a test missed remark", Suggestion="try using the remark pipeline feature"
39-
// CHECK-ALL: remarks.mlir:7:1: remark: [Passed] test-remark | Category:category-1-passed | Reason="because we are testing the remark pipeline", Remark="This is a test passed remark", Suggestion="try using the remark pipeline feature"
40-
// CHECK-ALL: remarks.mlir:7:1: remark: [Failure] test-remark | Category:category-2-failed | Reason="because we are testing the remark pipeline", Remark="This is a test failed remark", Suggestion="try using the remark pipeline feature"
41-
// CHECK-ALL: remarks.mlir:7:1: remark: [Analysis] test-remark | Category:category-2-analysis | Remark="This is a test analysis remark"
4222

43-
// CHECK-ALL1: remarks.mlir:8:3: remark: [Missed] test-remark | Category:category-1-missed | Reason="because we are testing the remark pipeline", Remark="This is a test missed remark", Suggestion="try using the remark pipeline feature"
4423
// CHECK-ALL1: remarks.mlir:8:3: remark: [Passed] test-remark | Category:category-1-passed | Reason="because we are testing the remark pipeline", Remark="This is a test passed remark", Suggestion="try using the remark pipeline feature"
45-
// CHECK-ALL1: remarks.mlir:9:3: remark: [Missed] test-remark | Category:category-1-missed | Reason="because we are testing the remark pipeline", Remark="This is a test missed remark", Suggestion="try using the remark pipeline feature"
46-
// CHECK-ALL1: remarks.mlir:9:3: remark: [Passed] test-remark | Category:category-1-passed | Reason="because we are testing the remark pipeline", Remark="This is a test passed remark", Suggestion="try using the remark pipeline feature"
47-
// CHECK-ALL1: remarks.mlir:7:1: remark: [Missed] test-remark | Category:category-1-missed | Reason="because we are testing the remark pipeline", Remark="This is a test missed remark", Suggestion="try using the remark pipeline feature"
48-
// CHECK-ALL1: remarks.mlir:7:1: remark: [Passed] test-remark | Category:category-1-passed | Reason="because we are testing the remark pipeline", Remark="This is a test passed remark", Suggestion="try using the remark pipeline feature"
24+
// CHECK-ALL1-NOT: remarks.mlir:8:3: remark: [Missed]
4925
// CHECK-ALL1-NOT: remarks.mlir:8:3: remark: [Failure]
5026
// CHECK-ALL1-NOT: remarks.mlir:8:3: remark: [Analysis]
27+
28+

mlir/test/lib/Pass/TestRemarksPass.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "mlir/IR/Remarks.h"
1616
#include "mlir/Pass/Pass.h"
1717
#include "mlir/Pass/PassManager.h"
18+
#include "mlir/Support/WalkResult.h"
1819

1920
using namespace mlir;
2021

@@ -34,10 +35,11 @@ class TestRemarkPass : public PassWrapper<TestRemarkPass, OperationPass<>> {
3435
void runOnOperation() override {
3536

3637
getOperation()->walk([](Operation *op) {
38+
if (isa<ModuleOp>(op))
39+
return WalkResult::advance();
3740
Location loc = op->getLoc();
38-
mlir::remark::missed(
39-
loc,
40-
remark::RemarkOpts::name("test-remark").category("category-1-missed"))
41+
mlir::remark::missed(loc, remark::RemarkOpts::name("test-remark")
42+
.category("a-category-1-missed"))
4143
<< remark::add("This is a test missed remark")
4244
<< remark::reason("because we are testing the remark pipeline")
4345
<< remark::suggest("try using the remark pipeline feature");
@@ -59,6 +61,7 @@ class TestRemarkPass : public PassWrapper<TestRemarkPass, OperationPass<>> {
5961
mlir::remark::analysis(loc, remark::RemarkOpts::name("test-remark")
6062
.category("category-2-analysis"))
6163
<< remark::add("This is a test analysis remark");
64+
return WalkResult::advance();
6265
});
6366
}
6467
};

0 commit comments

Comments
 (0)