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