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