1 //===- GreedyPatternRewriteDriver.cpp - A greedy rewriter -----------------===// 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 mlir::applyPatternsAndFoldGreedily. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "mlir/IR/PatternMatch.h" 14 #include "mlir/Interfaces/SideEffectInterfaces.h" 15 #include "mlir/Transforms/FoldUtils.h" 16 #include "mlir/Transforms/RegionUtils.h" 17 #include "llvm/ADT/DenseMap.h" 18 #include "llvm/Support/CommandLine.h" 19 #include "llvm/Support/Debug.h" 20 #include "llvm/Support/raw_ostream.h" 21 22 using namespace mlir; 23 24 #define DEBUG_TYPE "pattern-matcher" 25 26 /// The max number of iterations scanning for pattern match. 27 static unsigned maxPatternMatchIterations = 10; 28 29 //===----------------------------------------------------------------------===// 30 // GreedyPatternRewriteDriver 31 //===----------------------------------------------------------------------===// 32 33 namespace { 34 /// This is a worklist-driven driver for the PatternMatcher, which repeatedly 35 /// applies the locally optimal patterns in a roughly "bottom up" way. 36 class GreedyPatternRewriteDriver : public PatternRewriter { 37 public: 38 explicit GreedyPatternRewriteDriver(MLIRContext *ctx, 39 const OwningRewritePatternList &patterns) 40 : PatternRewriter(ctx), matcher(patterns), folder(ctx) { 41 worklist.reserve(64); 42 } 43 44 bool simplify(MutableArrayRef<Region> regions, int maxIterations); 45 46 void addToWorklist(Operation *op) { 47 // Check to see if the worklist already contains this op. 48 if (worklistMap.count(op)) 49 return; 50 51 worklistMap[op] = worklist.size(); 52 worklist.push_back(op); 53 } 54 55 Operation *popFromWorklist() { 56 auto *op = worklist.back(); 57 worklist.pop_back(); 58 59 // This operation is no longer in the worklist, keep worklistMap up to date. 60 if (op) 61 worklistMap.erase(op); 62 return op; 63 } 64 65 /// If the specified operation is in the worklist, remove it. If not, this is 66 /// a no-op. 67 void removeFromWorklist(Operation *op) { 68 auto it = worklistMap.find(op); 69 if (it != worklistMap.end()) { 70 assert(worklist[it->second] == op && "malformed worklist data structure"); 71 worklist[it->second] = nullptr; 72 worklistMap.erase(it); 73 } 74 } 75 76 // These are hooks implemented for PatternRewriter. 77 protected: 78 // Implement the hook for inserting operations, and make sure that newly 79 // inserted ops are added to the worklist for processing. 80 void notifyOperationInserted(Operation *op) override { addToWorklist(op); } 81 82 // If an operation is about to be removed, make sure it is not in our 83 // worklist anymore because we'd get dangling references to it. 84 void notifyOperationRemoved(Operation *op) override { 85 addToWorklist(op->getOperands()); 86 op->walk([this](Operation *operation) { 87 removeFromWorklist(operation); 88 folder.notifyRemoval(operation); 89 }); 90 } 91 92 // When the root of a pattern is about to be replaced, it can trigger 93 // simplifications to its users - make sure to add them to the worklist 94 // before the root is changed. 95 void notifyRootReplaced(Operation *op) override { 96 for (auto result : op->getResults()) 97 for (auto *user : result.getUsers()) 98 addToWorklist(user); 99 } 100 101 private: 102 // Look over the provided operands for any defining operations that should 103 // be re-added to the worklist. This function should be called when an 104 // operation is modified or removed, as it may trigger further 105 // simplifications. 106 template <typename Operands> 107 void addToWorklist(Operands &&operands) { 108 for (Value operand : operands) { 109 // If the use count of this operand is now < 2, we re-add the defining 110 // operation to the worklist. 111 // TODO(riverriddle) This is based on the fact that zero use operations 112 // may be deleted, and that single use values often have more 113 // canonicalization opportunities. 114 if (!operand.use_empty() && !operand.hasOneUse()) 115 continue; 116 if (auto *defInst = operand.getDefiningOp()) 117 addToWorklist(defInst); 118 } 119 } 120 121 /// The low-level pattern matcher. 122 RewritePatternMatcher matcher; 123 124 /// The worklist for this transformation keeps track of the operations that 125 /// need to be revisited, plus their index in the worklist. This allows us to 126 /// efficiently remove operations from the worklist when they are erased, even 127 /// if they aren't the root of a pattern. 128 std::vector<Operation *> worklist; 129 DenseMap<Operation *, unsigned> worklistMap; 130 131 /// Non-pattern based folder for operations. 132 OperationFolder folder; 133 }; 134 } // end anonymous namespace 135 136 /// Performs the rewrites while folding and erasing any dead ops. Returns true 137 /// if the rewrite converges in `maxIterations`. 138 bool GreedyPatternRewriteDriver::simplify(MutableArrayRef<Region> regions, 139 int maxIterations) { 140 // Add the given operation to the worklist. 141 auto collectOps = [this](Operation *op) { addToWorklist(op); }; 142 143 bool changed = false; 144 int i = 0; 145 do { 146 // Add all nested operations to the worklist. 147 for (auto ®ion : regions) 148 region.walk(collectOps); 149 150 // These are scratch vectors used in the folding loop below. 151 SmallVector<Value, 8> originalOperands, resultValues; 152 153 changed = false; 154 while (!worklist.empty()) { 155 auto *op = popFromWorklist(); 156 157 // Nulls get added to the worklist when operations are removed, ignore 158 // them. 159 if (op == nullptr) 160 continue; 161 162 // If the operation is trivially dead - remove it. 163 if (isOpTriviallyDead(op)) { 164 notifyOperationRemoved(op); 165 op->erase(); 166 changed = true; 167 continue; 168 } 169 170 // Collects all the operands and result uses of the given `op` into work 171 // list. Also remove `op` and nested ops from worklist. 172 originalOperands.assign(op->operand_begin(), op->operand_end()); 173 auto preReplaceAction = [&](Operation *op) { 174 // Add the operands to the worklist for visitation. 175 addToWorklist(originalOperands); 176 177 // Add all the users of the result to the worklist so we make sure 178 // to revisit them. 179 for (auto result : op->getResults()) 180 for (auto *userOp : result.getUsers()) 181 addToWorklist(userOp); 182 183 notifyOperationRemoved(op); 184 }; 185 186 // Try to fold this op. 187 bool inPlaceUpdate; 188 if ((succeeded(folder.tryToFold(op, collectOps, preReplaceAction, 189 &inPlaceUpdate)))) { 190 changed = true; 191 if (!inPlaceUpdate) 192 continue; 193 } 194 195 // Make sure that any new operations are inserted at this point. 196 setInsertionPoint(op); 197 198 // Try to match one of the patterns. The rewriter is automatically 199 // notified of any necessary changes, so there is nothing else to do here. 200 changed |= matcher.matchAndRewrite(op, *this); 201 } 202 203 // After applying patterns, make sure that the CFG of each of the regions is 204 // kept up to date. 205 if (succeeded(simplifyRegions(regions))) { 206 folder.clear(); 207 changed = true; 208 } 209 } while (changed && ++i < maxIterations); 210 // Whether the rewrite converges, i.e. wasn't changed in the last iteration. 211 return !changed; 212 } 213 214 /// Rewrite the regions of the specified operation, which must be isolated from 215 /// above, by repeatedly applying the highest benefit patterns in a greedy 216 /// work-list driven manner. Return true if no more patterns can be matched in 217 /// the result operation regions. 218 /// Note: This does not apply patterns to the top-level operation itself. 219 /// 220 bool mlir::applyPatternsAndFoldGreedily( 221 Operation *op, const OwningRewritePatternList &patterns) { 222 return applyPatternsAndFoldGreedily(op->getRegions(), patterns); 223 } 224 225 /// Rewrite the given regions, which must be isolated from above. 226 bool mlir::applyPatternsAndFoldGreedily( 227 MutableArrayRef<Region> regions, const OwningRewritePatternList &patterns) { 228 if (regions.empty()) 229 return true; 230 231 // The top-level operation must be known to be isolated from above to 232 // prevent performing canonicalizations on operations defined at or above 233 // the region containing 'op'. 234 auto regionIsIsolated = [](Region ®ion) { 235 return region.getParentOp()->isKnownIsolatedFromAbove(); 236 }; 237 (void)regionIsIsolated; 238 assert(llvm::all_of(regions, regionIsIsolated) && 239 "patterns can only be applied to operations IsolatedFromAbove"); 240 241 // Start the pattern driver. 242 GreedyPatternRewriteDriver driver(regions[0].getContext(), patterns); 243 bool converged = driver.simplify(regions, maxPatternMatchIterations); 244 LLVM_DEBUG(if (!converged) { 245 llvm::dbgs() << "The pattern rewrite doesn't converge after scanning " 246 << maxPatternMatchIterations << " times"; 247 }); 248 return converged; 249 } 250 251 //===----------------------------------------------------------------------===// 252 // OpPatternRewriteDriver 253 //===----------------------------------------------------------------------===// 254 255 namespace { 256 /// This is a simple driver for the PatternMatcher to apply patterns and perform 257 /// folding on a single op. It repeatedly applies locally optimal patterns. 258 class OpPatternRewriteDriver : public PatternRewriter { 259 public: 260 explicit OpPatternRewriteDriver(MLIRContext *ctx, 261 const OwningRewritePatternList &patterns) 262 : PatternRewriter(ctx), matcher(patterns), folder(ctx) {} 263 264 bool simplifyLocally(Operation *op, int maxIterations, bool &erased); 265 266 // These are hooks implemented for PatternRewriter. 267 protected: 268 /// If an operation is about to be removed, mark it so that we can let clients 269 /// know. 270 void notifyOperationRemoved(Operation *op) override { 271 opErasedViaPatternRewrites = true; 272 } 273 274 // When a root is going to be replaced, its removal will be notified as well. 275 // So there is nothing to do here. 276 void notifyRootReplaced(Operation *op) override {} 277 278 private: 279 /// The low-level pattern matcher. 280 RewritePatternMatcher matcher; 281 282 /// Non-pattern based folder for operations. 283 OperationFolder folder; 284 285 /// Set to true if the operation has been erased via pattern rewrites. 286 bool opErasedViaPatternRewrites = false; 287 }; 288 289 } // anonymous namespace 290 291 /// Performs the rewrites and folding only on `op`. The simplification converges 292 /// if the op is erased as a result of being folded, replaced, or dead, or no 293 /// more changes happen in an iteration. Returns true if the rewrite converges 294 /// in `maxIterations`. `erased` is set to true if `op` gets erased. 295 bool OpPatternRewriteDriver::simplifyLocally(Operation *op, int maxIterations, 296 bool &erased) { 297 bool changed = false; 298 erased = false; 299 opErasedViaPatternRewrites = false; 300 int i = 0; 301 // Iterate until convergence or until maxIterations. Deletion of the op as 302 // a result of being dead or folded is convergence. 303 do { 304 // If the operation is trivially dead - remove it. 305 if (isOpTriviallyDead(op)) { 306 op->erase(); 307 erased = true; 308 return true; 309 } 310 311 // Try to fold this op. 312 bool inPlaceUpdate; 313 if (succeeded(folder.tryToFold(op, /*processGeneratedConstants=*/nullptr, 314 /*preReplaceAction=*/nullptr, 315 &inPlaceUpdate))) { 316 changed = true; 317 if (!inPlaceUpdate) { 318 erased = true; 319 return true; 320 } 321 } 322 323 // Make sure that any new operations are inserted at this point. 324 setInsertionPoint(op); 325 326 // Try to match one of the patterns. The rewriter is automatically 327 // notified of any necessary changes, so there is nothing else to do here. 328 changed |= matcher.matchAndRewrite(op, *this); 329 if ((erased = opErasedViaPatternRewrites)) 330 return true; 331 } while (changed && ++i < maxIterations); 332 333 // Whether the rewrite converges, i.e. wasn't changed in the last iteration. 334 return !changed; 335 } 336 337 /// Rewrites only `op` using the supplied canonicalization patterns and 338 /// folding. `erased` is set to true if the op is erased as a result of being 339 /// folded, replaced, or dead. 340 bool mlir::applyOpPatternsAndFold(Operation *op, 341 const OwningRewritePatternList &patterns, 342 bool *erased) { 343 // Start the pattern driver. 344 OpPatternRewriteDriver driver(op->getContext(), patterns); 345 bool opErased; 346 bool converged = 347 driver.simplifyLocally(op, maxPatternMatchIterations, opErased); 348 if (erased) 349 *erased = opErased; 350 LLVM_DEBUG(if (!converged) { 351 llvm::dbgs() << "The pattern rewrite doesn't converge after scanning " 352 << maxPatternMatchIterations << " times"; 353 }); 354 return converged; 355 } 356