1//===------ TestRemarkPipeline.cpp --- dynamic pipeline test pass --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements a pass to test the dynamic pipeline feature.
10//
11//===----------------------------------------------------------------------===//
12
13#include "mlir/IR/BuiltinOps.h"
14#include "mlir/IR/Location.h"
15#include "mlir/IR/Remarks.h"
16#include "mlir/Pass/Pass.h"
17#include "mlir/Pass/PassManager.h"
18#include "mlir/Support/WalkResult.h"
19
20using namespace mlir;
21
22namespace {
23
24class TestRemarkPass : public PassWrapper<TestRemarkPass, OperationPass<>> {
25public:
26 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestRemarkPass)
27
28 StringRef getArgument() const final { return "test-remark"; }
29 StringRef getDescription() const final {
30 return "Tests the remark pipeline feature";
31 }
32
33 TestRemarkPass() = default;
34
35 void runOnOperation() override {
36
37 getOperation()->walk(callback: [](Operation *op) {
38 if (isa<ModuleOp>(Val: op))
39 return WalkResult::advance();
40 Location loc = op->getLoc();
41 mlir::remark::missed(loc, opts: remark::RemarkOpts::name(n: "test-remark")
42 .category(v: "a-category-1-missed"))
43 << remark::add(fmt: "This is a test missed remark")
44 << remark::reason(fmt: "because we are testing the remark pipeline")
45 << remark::suggest(fmt: "try using the remark pipeline feature");
46 mlir::remark::passed(
47 loc,
48 opts: remark::RemarkOpts::name(n: "test-remark").category(v: "category-1-passed"))
49 << remark::add(fmt: "This is a test passed remark (should be dropped)")
50 << remark::reason(fmt: "because we are testing the remark pipeline")
51 << remark::suggest(fmt: "try using the remark pipeline feature");
52 mlir::remark::passed(
53 loc,
54 opts: remark::RemarkOpts::name(n: "test-remark").category(v: "category-1-passed"))
55 << remark::add(fmt: "This is a test passed remark")
56 << remark::reason(fmt: "because we are testing the remark pipeline")
57 << remark::suggest(fmt: "try using the remark pipeline feature");
58
59 mlir::remark::failed(
60 loc,
61 opts: remark::RemarkOpts::name(n: "test-remark").category(v: "category-2-failed"))
62 << remark::add(fmt: "This is a test failed remark")
63 << remark::reason(fmt: "because we are testing the remark pipeline")
64 << remark::suggest(fmt: "try using the remark pipeline feature");
65
66 mlir::remark::analysis(loc, opts: remark::RemarkOpts::name(n: "test-remark")
67 .category(v: "category-2-analysis"))
68 << remark::add(fmt: "This is a test analysis remark");
69
70 // Test remark linking: emit an analysis remark, then link a passed
71 // remark to it using relatedTo on RemarkOpts (no manual ID threading).
72 auto analysisRemark = mlir::remark::analysis(
73 loc,
74 opts: remark::RemarkOpts::name(n: "test-remark").category(v: "category-link"));
75 analysisRemark << remark::add(fmt: "Analysis that enables optimization");
76
77 // The passed remark finds the analysis via relatedTo on RemarkOpts:
78 mlir::remark::passed(loc, opts: remark::RemarkOpts::name(n: "test-remark")
79 .category(v: "category-link")
80 .relatedTo(r: analysisRemark))
81 << remark::add(fmt: "Optimization enabled by analysis");
82
83 return WalkResult::advance();
84 });
85 }
86};
87} // namespace
88
89namespace mlir {
90namespace test {
91void registerTestRemarkPass() { PassRegistration<TestRemarkPass>(); }
92} // namespace test
93} // namespace mlir
94