1 //===- GreedyPatternRewriteDriver.cpp - A greedy rewriter -----------------===// 2 // 3 // Copyright 2019 The MLIR Authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 // ============================================================================= 17 // 18 // This file implements mlir::applyPatternsGreedily. 19 // 20 //===----------------------------------------------------------------------===// 21 22 #include "mlir/Dialect/StandardOps/Ops.h" 23 #include "mlir/IR/Builders.h" 24 #include "mlir/IR/PatternMatch.h" 25 #include "mlir/Transforms/FoldUtils.h" 26 #include "llvm/ADT/DenseMap.h" 27 #include "llvm/Support/CommandLine.h" 28 #include "llvm/Support/Debug.h" 29 #include "llvm/Support/raw_ostream.h" 30 31 using namespace mlir; 32 33 #define DEBUG_TYPE "pattern-matcher" 34 35 static llvm::cl::opt<unsigned> maxPatternMatchIterations( 36 "mlir-max-pattern-match-iterations", 37 llvm::cl::desc("Max number of iterations scanning for pattern match"), 38 llvm::cl::init(10)); 39 40 namespace { 41 42 /// This is a worklist-driven driver for the PatternMatcher, which repeatedly 43 /// applies the locally optimal patterns in a roughly "bottom up" way. 44 class GreedyPatternRewriteDriver : public PatternRewriter { 45 public: 46 explicit GreedyPatternRewriteDriver(MLIRContext *ctx, 47 const OwningRewritePatternList &patterns) 48 : PatternRewriter(ctx), matcher(patterns), folder(ctx) { 49 worklist.reserve(64); 50 } 51 52 /// Perform the rewrites. Return true if the rewrite converges in 53 /// `maxIterations`. 54 bool simplify(MutableArrayRef<Region> regions, int maxIterations); 55 56 void addToWorklist(Operation *op) { 57 // Check to see if the worklist already contains this op. 58 if (worklistMap.count(op)) 59 return; 60 61 worklistMap[op] = worklist.size(); 62 worklist.push_back(op); 63 } 64 65 Operation *popFromWorklist() { 66 auto *op = worklist.back(); 67 worklist.pop_back(); 68 69 // This operation is no longer in the worklist, keep worklistMap up to date. 70 if (op) 71 worklistMap.erase(op); 72 return op; 73 } 74 75 /// If the specified operation is in the worklist, remove it. If not, this is 76 /// a no-op. 77 void removeFromWorklist(Operation *op) { 78 auto it = worklistMap.find(op); 79 if (it != worklistMap.end()) { 80 assert(worklist[it->second] == op && "malformed worklist data structure"); 81 worklist[it->second] = nullptr; 82 } 83 } 84 85 // These are hooks implemented for PatternRewriter. 86 protected: 87 // Implement the hook for creating operations, and make sure that newly 88 // created ops are added to the worklist for processing. 89 Operation *createOperation(const OperationState &state) override { 90 auto *result = OpBuilder::createOperation(state); 91 addToWorklist(result); 92 return result; 93 } 94 95 // If an operation is about to be removed, make sure it is not in our 96 // worklist anymore because we'd get dangling references to it. 97 void notifyOperationRemoved(Operation *op) override { 98 addToWorklist(op->getOperands()); 99 op->walk([this](Operation *operation) { 100 removeFromWorklist(operation); 101 folder.notifyRemoval(operation); 102 }); 103 } 104 105 // When the root of a pattern is about to be replaced, it can trigger 106 // simplifications to its users - make sure to add them to the worklist 107 // before the root is changed. 108 void notifyRootReplaced(Operation *op) override { 109 for (auto *result : op->getResults()) 110 for (auto *user : result->getUsers()) 111 addToWorklist(user); 112 } 113 114 private: 115 // Look over the provided operands for any defining operations that should 116 // be re-added to the worklist. This function should be called when an 117 // operation is modified or removed, as it may trigger further 118 // simplifications. 119 template <typename Operands> void addToWorklist(Operands &&operands) { 120 for (Value *operand : operands) { 121 // If the use count of this operand is now < 2, we re-add the defining 122 // operation to the worklist. 123 // TODO(riverriddle) This is based on the fact that zero use operations 124 // may be deleted, and that single use values often have more 125 // canonicalization opportunities. 126 if (!operand->use_empty() && !operand->hasOneUse()) 127 continue; 128 if (auto *defInst = operand->getDefiningOp()) 129 addToWorklist(defInst); 130 } 131 } 132 133 /// The low-level pattern matcher. 134 RewritePatternMatcher matcher; 135 136 /// The worklist for this transformation keeps track of the operations that 137 /// need to be revisited, plus their index in the worklist. This allows us to 138 /// efficiently remove operations from the worklist when they are erased, even 139 /// if they aren't the root of a pattern. 140 std::vector<Operation *> worklist; 141 DenseMap<Operation *, unsigned> worklistMap; 142 143 /// Non-pattern based folder for operations. 144 OperationFolder folder; 145 }; 146 } // end anonymous namespace 147 148 /// Perform the rewrites. 149 bool GreedyPatternRewriteDriver::simplify(MutableArrayRef<Region> regions, 150 int maxIterations) { 151 // Add the given operation to the worklist. 152 auto collectOps = [this](Operation *op) { addToWorklist(op); }; 153 154 bool changed = false; 155 int i = 0; 156 do { 157 // Add all nested operations to the worklist. 158 for (auto ®ion : regions) 159 region.walk(collectOps); 160 161 // These are scratch vectors used in the folding loop below. 162 SmallVector<Value *, 8> originalOperands, resultValues; 163 164 changed = false; 165 while (!worklist.empty()) { 166 auto *op = popFromWorklist(); 167 168 // Nulls get added to the worklist when operations are removed, ignore 169 // them. 170 if (op == nullptr) 171 continue; 172 173 // If the operation has no side effects, and no users, then it is 174 // trivially dead - remove it. 175 if (op->hasNoSideEffect() && op->use_empty()) { 176 // Be careful to update bookkeeping. 177 notifyOperationRemoved(op); 178 op->erase(); 179 continue; 180 } 181 182 // Collects all the operands and result uses of the given `op` into work 183 // list. Also remove `op` and nested ops from worklist. 184 originalOperands.assign(op->operand_begin(), op->operand_end()); 185 auto preReplaceAction = [&](Operation *op) { 186 // Add the operands to the worklist for visitation. 187 addToWorklist(originalOperands); 188 189 // Add all the users of the result to the worklist so we make sure 190 // to revisit them. 191 for (auto *result : op->getResults()) 192 for (auto *operand : result->getUsers()) 193 addToWorklist(operand); 194 195 notifyOperationRemoved(op); 196 }; 197 198 // Try to fold this op. 199 if (succeeded(folder.tryToFold(op, collectOps, preReplaceAction))) { 200 changed |= true; 201 continue; 202 } 203 204 // Make sure that any new operations are inserted at this point. 205 setInsertionPoint(op); 206 207 // Try to match one of the patterns. The rewriter is automatically 208 // notified of any necessary changes, so there is nothing else to do here. 209 changed |= matcher.matchAndRewrite(op, *this); 210 } 211 } while (changed && ++i < maxIterations); 212 // Whether the rewrite converges, i.e. wasn't changed in the last iteration. 213 return !changed; 214 } 215 216 /// Rewrite the regions of the specified operation, which must be isolated from 217 /// above, by repeatedly applying the highest benefit patterns in a greedy 218 /// work-list driven manner. Return true if no more patterns can be matched in 219 /// the result operation regions. 220 /// Note: This does not apply patterns to the top-level operation itself. 221 /// 222 bool mlir::applyPatternsGreedily(Operation *op, 223 const OwningRewritePatternList &patterns) { 224 return applyPatternsGreedily(op->getRegions(), patterns); 225 } 226 227 /// Rewrite the given regions, which must be isolated from above. 228 bool mlir::applyPatternsGreedily(MutableArrayRef<Region> regions, 229 const OwningRewritePatternList &patterns) { 230 if (regions.empty()) 231 return true; 232 233 // The top-level operation must be known to be isolated from above to 234 // prevent performing canonicalizations on operations defined at or above 235 // the region containing 'op'. 236 auto regionIsIsolated = [](Region ®ion) { 237 return region.getParentOp()->isKnownIsolatedFromAbove(); 238 }; 239 (void)regionIsIsolated; 240 assert(llvm::all_of(regions, regionIsIsolated) && 241 "patterns can only be applied to operations IsolatedFromAbove"); 242 243 // Start the pattern driver. 244 GreedyPatternRewriteDriver driver(regions[0].getContext(), patterns); 245 bool converged = driver.simplify(regions, maxPatternMatchIterations); 246 LLVM_DEBUG(if (!converged) { 247 llvm::dbgs() << "The pattern rewrite doesn't converge after scanning " 248 << maxPatternMatchIterations << " times"; 249 }); 250 return converged; 251 } 252