1//===- TestIRVisitorsGeneric.cpp - Pass to test the Generic IR visitors ---===//
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#include "TestOpsCore.h"
10#include "mlir/Pass/Pass.h"
11
12using namespace mlir;
13
14static std::string getStageDescription(const WalkStage &stage) {
15 if (stage.isBeforeAllRegions())
16 return "before all regions";
17 if (stage.isAfterAllRegions())
18 return "after all regions";
19 return "before region #" + std::to_string(val: stage.getNextRegion());
20}
21
22namespace {
23/// This pass exercises generic visitor with void callbacks and prints the order
24/// and stage in which operations are visited.
25struct TestGenericIRVisitorPass
26 : public PassWrapper<TestGenericIRVisitorPass, OperationPass<>> {
27 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestGenericIRVisitorPass)
28
29 StringRef getArgument() const final { return "test-generic-ir-visitors"; }
30 StringRef getDescription() const final { return "Test generic IR visitors."; }
31 void runOnOperation() override {
32 Operation *outerOp = getOperation();
33 int stepNo = 0;
34 outerOp->walk(callback: [&](Operation *op, const WalkStage &stage) {
35 llvm::outs() << "step " << stepNo++ << " op '" << op->getName() << "' "
36 << getStageDescription(stage) << "\n";
37 });
38
39 // Exercise static inference of operation type.
40 outerOp->walk(callback: [&](test::TwoRegionOp op, const WalkStage &stage) {
41 llvm::outs() << "step " << stepNo++ << " op '" << op->getName() << "' "
42 << getStageDescription(stage) << "\n";
43 });
44 }
45};
46
47/// This pass exercises the generic visitor with non-void callbacks and prints
48/// the order and stage in which operations are visited. It will interrupt the
49/// walk based on attributes peesent in the IR.
50struct TestGenericIRVisitorInterruptPass
51 : public PassWrapper<TestGenericIRVisitorInterruptPass, OperationPass<>> {
52 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(
53 TestGenericIRVisitorInterruptPass)
54
55 StringRef getArgument() const final {
56 return "test-generic-ir-visitors-interrupt";
57 }
58 StringRef getDescription() const final {
59 return "Test generic IR visitors with interrupts.";
60 }
61 void runOnOperation() override {
62 Operation *outerOp = getOperation();
63 int stepNo = 0;
64
65 auto walker = [&](Operation *op, const WalkStage &stage) {
66 if (auto interruptBeforeAall =
67 op->getDiscardableAttrOfType<BoolAttr>(name: "interrupt_before_all"))
68 if (interruptBeforeAall.getValue() && stage.isBeforeAllRegions())
69 return WalkResult::interrupt();
70
71 if (auto interruptAfterAll =
72 op->getDiscardableAttrOfType<BoolAttr>(name: "interrupt_after_all"))
73 if (interruptAfterAll.getValue() && stage.isAfterAllRegions())
74 return WalkResult::interrupt();
75
76 if (auto interruptAfterRegion = op->getDiscardableAttrOfType<IntegerAttr>(
77 name: "interrupt_after_region"))
78 if (stage.isAfterRegion(
79 region: static_cast<int>(interruptAfterRegion.getInt())))
80 return WalkResult::interrupt();
81
82 if (auto skipBeforeAall =
83 op->getDiscardableAttrOfType<BoolAttr>(name: "skip_before_all"))
84 if (skipBeforeAall.getValue() && stage.isBeforeAllRegions())
85 return WalkResult::skip();
86
87 if (auto skipAfterAll =
88 op->getDiscardableAttrOfType<BoolAttr>(name: "skip_after_all"))
89 if (skipAfterAll.getValue() && stage.isAfterAllRegions())
90 return WalkResult::skip();
91
92 if (auto skipAfterRegion =
93 op->getDiscardableAttrOfType<IntegerAttr>(name: "skip_after_region"))
94 if (stage.isAfterRegion(region: static_cast<int>(skipAfterRegion.getInt())))
95 return WalkResult::skip();
96
97 llvm::outs() << "step " << stepNo++ << " op '" << op->getName() << "' "
98 << getStageDescription(stage) << "\n";
99 return WalkResult::advance();
100 };
101
102 // Interrupt the walk based on attributes on the operation.
103 auto result = outerOp->walk(callback&: walker);
104
105 if (result.wasInterrupted())
106 llvm::outs() << "step " << stepNo++ << " walk was interrupted\n";
107
108 // Exercise static inference of operation type.
109 result = outerOp->walk([&](test::TwoRegionOp op, const WalkStage &stage) {
110 return walker(op, stage);
111 });
112
113 if (result.wasInterrupted())
114 llvm::outs() << "step " << stepNo++ << " walk was interrupted\n";
115 }
116};
117
118struct TestGenericIRBlockVisitorInterruptPass
119 : public PassWrapper<TestGenericIRBlockVisitorInterruptPass,
120 OperationPass<ModuleOp>> {
121 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(
122 TestGenericIRBlockVisitorInterruptPass)
123
124 StringRef getArgument() const final {
125 return "test-generic-ir-block-visitors-interrupt";
126 }
127 StringRef getDescription() const final {
128 return "Test generic IR visitors with interrupts, starting with Blocks.";
129 }
130
131 void runOnOperation() override {
132 int stepNo = 0;
133
134 auto walker = [&](Block *block) {
135 for (Operation &op : *block)
136 if (op.getDiscardableAttrOfType<BoolAttr>(name: "interrupt"))
137 return WalkResult::interrupt();
138
139 llvm::outs() << "step " << stepNo++ << "\n";
140 return WalkResult::advance();
141 };
142
143 auto result = getOperation()->walk(callback&: walker);
144 if (result.wasInterrupted())
145 llvm::outs() << "step " << stepNo++ << " walk was interrupted\n";
146 }
147};
148
149struct TestGenericIRRegionVisitorInterruptPass
150 : public PassWrapper<TestGenericIRRegionVisitorInterruptPass,
151 OperationPass<ModuleOp>> {
152 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(
153 TestGenericIRRegionVisitorInterruptPass)
154
155 StringRef getArgument() const final {
156 return "test-generic-ir-region-visitors-interrupt";
157 }
158 StringRef getDescription() const final {
159 return "Test generic IR visitors with interrupts, starting with Regions.";
160 }
161
162 void runOnOperation() override {
163 int stepNo = 0;
164
165 auto walker = [&](Region *region) {
166 for (Operation &op : region->getOps())
167 if (op.getDiscardableAttrOfType<BoolAttr>(name: "interrupt"))
168 return WalkResult::interrupt();
169
170 llvm::outs() << "step " << stepNo++ << "\n";
171 return WalkResult::advance();
172 };
173
174 auto result = getOperation()->walk(callback&: walker);
175 if (result.wasInterrupted())
176 llvm::outs() << "step " << stepNo++ << " walk was interrupted\n";
177 }
178};
179
180} // namespace
181
182namespace mlir {
183namespace test {
184void registerTestGenericIRVisitorsPass() {
185 PassRegistration<TestGenericIRVisitorPass>();
186 PassRegistration<TestGenericIRVisitorInterruptPass>();
187 PassRegistration<TestGenericIRBlockVisitorInterruptPass>();
188 PassRegistration<TestGenericIRRegionVisitorInterruptPass>();
189}
190
191} // namespace test
192} // namespace mlir
193