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