MLIR  18.0.0git
GreedyPatternRewriteDriver.h
Go to the documentation of this file.
1 //===- GreedyPatternRewriteDriver.h - Greedy Pattern Driver -----*- C++ -*-===//
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 declares methods for applying a set of patterns greedily, choosing
10 // the patterns with the highest local benefit, until a fixed point is reached.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_TRANSFORMS_GREEDYPATTERNREWRITEDRIVER_H_
15 #define MLIR_TRANSFORMS_GREEDYPATTERNREWRITEDRIVER_H_
16 
18 
19 namespace mlir {
20 
21 /// This enum controls which ops are put on the worklist during a greedy
22 /// pattern rewrite.
24  /// No restrictions wrt. which ops are processed.
25  AnyOp,
26  /// Only pre-existing and newly created ops are processed.
28  /// Only pre-existing ops are processed.
30 };
31 
32 /// This class allows control over how the GreedyPatternRewriteDriver works.
34 public:
35  /// This specifies the order of initial traversal that populates the rewriters
36  /// worklist. When set to true, it walks the operations top-down, which is
37  /// generally more efficient in compile time. When set to false, its initial
38  /// traversal of the region tree is bottom up on each block, which may match
39  /// larger patterns when given an ambiguous pattern set.
40  ///
41  /// Note: Only applicable when simplifying entire regions.
42  bool useTopDownTraversal = false;
43 
44  /// Perform control flow optimizations to the region tree after applying all
45  /// patterns.
46  ///
47  /// Note: Only applicable when simplifying entire regions.
49 
50  /// This specifies the maximum number of times the rewriter will iterate
51  /// between applying patterns and simplifying regions. Use `kNoLimit` to
52  /// disable this iteration limit.
53  ///
54  /// Note: Only applicable when simplifying entire regions.
55  int64_t maxIterations = 10;
56 
57  /// This specifies the maximum number of rewrites within an iteration. Use
58  /// `kNoLimit` to disable this limit.
60 
61  static constexpr int64_t kNoLimit = -1;
62 
63  /// Only ops within the scope are added to the worklist. If no scope is
64  /// specified, the closest enclosing region around the initial list of ops
65  /// is used as a scope.
66  Region *scope = nullptr;
67 
68  /// Strict mode can restrict the ops that are added to the worklist during
69  /// the rewrite.
70  ///
71  /// * GreedyRewriteStrictness::AnyOp: No ops are excluded.
72  /// * GreedyRewriteStrictness::ExistingAndNewOps: Only pre-existing ops (that
73  /// were on the worklist at the very beginning) and newly created ops are
74  /// enqueued. All other ops are excluded.
75  /// * GreedyRewriteStrictness::ExistingOps: Only pre-existing ops (that were
76  /// were on the worklist at the very beginning) enqueued. All other ops are
77  /// excluded.
79 
80  /// An optional listener that should be notified about IR modifications.
82 };
83 
84 //===----------------------------------------------------------------------===//
85 // applyPatternsGreedily
86 //===----------------------------------------------------------------------===//
87 
88 /// Rewrite ops in the given region, which must be isolated from above, by
89 /// repeatedly applying the highest benefit patterns in a greedy work-list
90 /// driven manner.
91 ///
92 /// This variant may stop after a predefined number of iterations, see the
93 /// alternative below to provide a specific number of iterations before stopping
94 /// in absence of convergence.
95 ///
96 /// Return success if the iterative process converged and no more patterns can
97 /// be matched in the result operation regions. `changed` is set to true if the
98 /// IR was modified at all.
99 ///
100 /// Note: This does not apply patterns to the top-level operation itself.
101 /// These methods also perform folding and simple dead-code elimination
102 /// before attempting to match any of the provided patterns.
103 ///
104 /// You may configure several aspects of this with GreedyRewriteConfig.
107  const FrozenRewritePatternSet &patterns,
109  bool *changed = nullptr);
110 
111 /// Rewrite ops in all regions of the given op, which must be isolated from
112 /// above.
113 inline LogicalResult
115  const FrozenRewritePatternSet &patterns,
117  bool *changed = nullptr) {
118  bool anyRegionChanged = false;
119  bool failed = false;
120  for (Region &region : op->getRegions()) {
121  bool regionChanged;
122  failed |=
123  applyPatternsAndFoldGreedily(region, patterns, config, &regionChanged)
124  .failed();
125  anyRegionChanged |= regionChanged;
126  }
127  if (changed)
128  *changed = anyRegionChanged;
129  return failure(failed);
130 }
131 
132 /// Applies the specified rewrite patterns on `ops` while also trying to fold
133 /// these ops.
134 ///
135 /// Newly created ops and other pre-existing ops that use results of rewritten
136 /// ops or supply operands to such ops are simplified, unless such ops are
137 /// excluded via `config.strictMode`. Any other ops remain unmodified (i.e.,
138 /// regardless of `strictMode`).
139 ///
140 /// In addition to strictness, a region scope can be specified. Only ops within
141 /// the scope are simplified. This is similar to `applyPatternsAndFoldGreedily`,
142 /// where only ops within the given regions are simplified. If no scope is
143 /// specified, it is assumed to be the first common enclosing region of the
144 /// given ops.
145 ///
146 /// Note that ops in `ops` could be erased as result of folding, becoming dead,
147 /// or via pattern rewrites. If more far reaching simplification is desired,
148 /// applyPatternsAndFoldGreedily should be used.
149 ///
150 /// Returns success if the iterative process converged and no more patterns can
151 /// be matched. `changed` is set to true if the IR was modified at all.
152 /// `allOpsErased` is set to true if all ops in `ops` were erased.
153 LogicalResult
154 applyOpPatternsAndFold(ArrayRef<Operation *> ops,
155  const FrozenRewritePatternSet &patterns,
156  GreedyRewriteConfig config = GreedyRewriteConfig(),
157  bool *changed = nullptr, bool *allErased = nullptr);
158 
159 } // namespace mlir
160 
161 #endif // MLIR_TRANSFORMS_GREEDYPATTERNREWRITEDRIVER_H_
This class represents a frozen set of patterns that can be processed by a pattern applicator.
This class allows control over how the GreedyPatternRewriteDriver works.
static constexpr int64_t kNoLimit
GreedyRewriteStrictness strictMode
Strict mode can restrict the ops that are added to the worklist during the rewrite.
int64_t maxIterations
This specifies the maximum number of times the rewriter will iterate between applying patterns and si...
Region * scope
Only ops within the scope are added to the worklist.
bool useTopDownTraversal
This specifies the order of initial traversal that populates the rewriters worklist.
RewriterBase::Listener * listener
An optional listener that should be notified about IR modifications.
int64_t maxNumRewrites
This specifies the maximum number of rewrites within an iteration.
bool enableRegionSimplification
Perform control flow optimizations to the region tree after applying all patterns.
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
Definition: Operation.h:655
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition: Region.h:26
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
LogicalResult applyOpPatternsAndFold(ArrayRef< Operation * > ops, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr, bool *allErased=nullptr)
Applies the specified rewrite patterns on ops while also trying to fold these ops.
LogicalResult applyPatternsAndFoldGreedily(Region &region, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr)
Rewrite ops in the given region, which must be isolated from above, by repeatedly applying the highes...
GreedyRewriteStrictness
This enum controls which ops are put on the worklist during a greedy pattern rewrite.
@ ExistingOps
Only pre-existing ops are processed.
@ ExistingAndNewOps
Only pre-existing and newly created ops are processed.
@ AnyOp
No restrictions wrt. which ops are processed.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
bool failed() const
Returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:44