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