|
13 | 13 | #include "mlir/Transforms/WalkPatternRewriteDriver.h"
|
14 | 14 |
|
15 | 15 | #include "mlir/IR/MLIRContext.h"
|
| 16 | +#include "mlir/IR/Operation.h" |
16 | 17 | #include "mlir/IR/OperationSupport.h"
|
17 | 18 | #include "mlir/IR/PatternMatch.h"
|
18 | 19 | #include "mlir/IR/Verifier.h"
|
19 | 20 | #include "mlir/IR/Visitors.h"
|
20 | 21 | #include "mlir/Rewrite/PatternApplicator.h"
|
21 |
| -#include "llvm/Support/Debug.h" |
| 22 | +#include "llvm/ADT/STLExtras.h" |
| 23 | +#include "llvm/Support/DebugLog.h" |
22 | 24 | #include "llvm/Support/ErrorHandling.h"
|
23 | 25 |
|
24 | 26 | #define DEBUG_TYPE "walk-rewriter"
|
@@ -88,20 +90,97 @@ void walkAndApplyPatterns(Operation *op,
|
88 | 90 | PatternApplicator applicator(patterns);
|
89 | 91 | applicator.applyDefaultCostModel();
|
90 | 92 |
|
| 93 | + // Iterator on all reachable operations in the region. |
| 94 | + // Also keep track if we visited the nested regions of the current op |
| 95 | + // already to drive the post-order traversal. |
| 96 | + struct RegionReachableOpIterator { |
| 97 | + RegionReachableOpIterator(Region *region) : region(region) { |
| 98 | + regionIt = region->begin(); |
| 99 | + if (regionIt != region->end()) |
| 100 | + blockIt = regionIt->begin(); |
| 101 | + } |
| 102 | + // Advance the iterator to the next reachable operation. |
| 103 | + void advance() { |
| 104 | + assert(regionIt != region->end()); |
| 105 | + hasVisitedRegions = false; |
| 106 | + if (blockIt == regionIt->end()) { |
| 107 | + ++regionIt; |
| 108 | + if (regionIt != region->end()) |
| 109 | + blockIt = regionIt->begin(); |
| 110 | + return; |
| 111 | + } |
| 112 | + ++blockIt; |
| 113 | + if (blockIt != regionIt->end()) { |
| 114 | + LDBG() << "Incrementing block iterator, next op: " |
| 115 | + << OpWithFlags(&*blockIt, OpPrintingFlags().skipRegions()); |
| 116 | + } |
| 117 | + } |
| 118 | + // The region we're iterating over. |
| 119 | + Region *region; |
| 120 | + // The Block currently being iterated over. |
| 121 | + Region::iterator regionIt; |
| 122 | + // The Operation currently being iterated over. |
| 123 | + Block::iterator blockIt; |
| 124 | + // Whether we've visited the nested regions of the current op already. |
| 125 | + bool hasVisitedRegions = false; |
| 126 | + }; |
| 127 | + |
| 128 | + // Worklist of regions to visit to drive the post-order traversal. |
| 129 | + SmallVector<RegionReachableOpIterator> worklist; |
| 130 | + |
| 131 | + LDBG() << "Starting walk-based pattern rewrite driver"; |
91 | 132 | ctx->executeAction<WalkAndApplyPatternsAction>(
|
92 | 133 | [&] {
|
| 134 | + // Perform a post-order traversal of the regions, visiting each |
| 135 | + // reachable operation. |
93 | 136 | for (Region ®ion : op->getRegions()) {
|
94 |
| - region.walk([&](Operation *visitedOp) { |
95 |
| - LLVM_DEBUG(llvm::dbgs() << "Visiting op: "; visitedOp->print( |
96 |
| - llvm::dbgs(), OpPrintingFlags().skipRegions()); |
97 |
| - llvm::dbgs() << "\n";); |
| 137 | + assert(worklist.empty()); |
| 138 | + if (region.empty()) |
| 139 | + continue; |
| 140 | + |
| 141 | + // Prime the worklist with the entry block of this region. |
| 142 | + worklist.push_back({®ion}); |
| 143 | + while (!worklist.empty()) { |
| 144 | + RegionReachableOpIterator &it = worklist.back(); |
| 145 | + if (it.regionIt == it.region->end()) { |
| 146 | + // We're done with this region. |
| 147 | + worklist.pop_back(); |
| 148 | + continue; |
| 149 | + } |
| 150 | + if (it.blockIt == it.regionIt->end()) { |
| 151 | + // We're done with this block. |
| 152 | + it.advance(); |
| 153 | + continue; |
| 154 | + } |
| 155 | + Operation *op = &*it.blockIt; |
| 156 | + // If we haven't visited the nested regions of this op yet, |
| 157 | + // enqueue them. |
| 158 | + if (!it.hasVisitedRegions) { |
| 159 | + it.hasVisitedRegions = true; |
| 160 | + for (Region &nestedRegion : llvm::reverse(op->getRegions())) { |
| 161 | + if (nestedRegion.empty()) |
| 162 | + continue; |
| 163 | + worklist.push_back({&nestedRegion}); |
| 164 | + } |
| 165 | + } |
| 166 | + // If we're not at the back of the worklist, we've enqueued some |
| 167 | + // nested region for processing. We'll come back to this op later |
| 168 | + // (post-order) |
| 169 | + if (&it != &worklist.back()) |
| 170 | + continue; |
| 171 | + |
| 172 | + // Preemptively increment the iterator, in case the current op |
| 173 | + // would be erased. |
| 174 | + it.advance(); |
| 175 | + |
| 176 | + LDBG() << "Visiting op: " |
| 177 | + << OpWithFlags(op, OpPrintingFlags().skipRegions()); |
98 | 178 | #if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
|
99 |
| - erasedListener.visitedOp = visitedOp; |
| 179 | + erasedListener.visitedOp = op; |
100 | 180 | #endif // MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
|
101 |
| - if (succeeded(applicator.matchAndRewrite(visitedOp, rewriter))) { |
102 |
| - LLVM_DEBUG(llvm::dbgs() << "\tOp matched and rewritten\n";); |
103 |
| - } |
104 |
| - }); |
| 181 | + if (succeeded(applicator.matchAndRewrite(op, rewriter))) |
| 182 | + LDBG() << "\tOp matched and rewritten"; |
| 183 | + } |
105 | 184 | }
|
106 | 185 | },
|
107 | 186 | {op});
|
|
0 commit comments