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