Skip to content

Commit 21aacb0

Browse files
[mlir] Improve GreedyPatternRewriteDriver and pass documentation (#77614)
Clarify what kind of IR modifications are allowed. Also improve the documentation of the greedy rewrite driver entry points. Addressing comments in #76219.
1 parent a02c0d9 commit 21aacb0

File tree

2 files changed

+75
-39
lines changed

2 files changed

+75
-39
lines changed

mlir/docs/PassManagement.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,26 @@ this is a great place to start.
1717

1818
In MLIR, the main unit of abstraction and transformation is an
1919
[operation](LangRef.md/#operations). As such, the pass manager is designed to
20-
work on instances of operations at different levels of nesting. The structure of
21-
the [pass manager](#pass-manager), and the concept of nesting, is detailed
22-
further below. All passes in MLIR derive from `OperationPass` and adhere to the
23-
following restrictions; any noncompliance will lead to problematic behavior in
24-
multithreaded and other advanced scenarios:
25-
26-
* Must not modify any state referenced or relied upon outside the current
27-
operation being operated on. This includes adding or removing operations
28-
from the parent block, changing the attributes(depending on the contract
29-
of the current operation)/operands/results/successors of the current operation.
30-
* Must not modify the state of another operation not nested within the current
31-
operation being operated on.
32-
* Other threads may be operating on these operations simultaneously.
33-
* Must not inspect the state of sibling operations.
20+
work on instances of operations at different levels of nesting. In the following
21+
paragraphs, we refer to the operation that a pass operates on as the "current
22+
operation".
23+
24+
The structure of the [pass manager](#pass-manager), and the concept of nesting,
25+
is detailed further below. All passes in MLIR derive from `OperationPass` and
26+
adhere to the following restrictions; any noncompliance will lead to problematic
27+
behavior in multithreaded and other advanced scenarios:
28+
29+
* Must not inspect the state of operations that are siblings of the current
30+
operation. Must neither access operations nested under those siblings.
3431
* Other threads may be modifying these operations in parallel.
3532
* Inspecting the state of ancestor/parent operations is permitted.
33+
* Must not modify the state of operations other than the operations that are
34+
nested under the current operation. This includes adding, modifying or
35+
removing other operations from an ancestor/parent block.
36+
* Other threads may be operating on these operations simultaneously.
37+
* As an exception, the attributes of the current operation may be modified
38+
freely. This is the only way that the current operation may be modified.
39+
(I.e., modifying operands, etc. is not allowed.)
3640
* Must not maintain mutable pass state across invocations of `runOnOperation`.
3741
A pass may be run on many different operations with no guarantee of
3842
execution order.

mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ class GreedyRewriteConfig {
6262

6363
/// Only ops within the scope are added to the worklist. If no scope is
6464
/// specified, the closest enclosing region around the initial list of ops
65-
/// is used as a scope.
65+
/// (or the specified region, depending on which greedy rewrite entry point
66+
/// is used) is used as a scope.
6667
Region *scope = nullptr;
6768

6869
/// Strict mode can restrict the ops that are added to the worklist during
@@ -86,30 +87,54 @@ class GreedyRewriteConfig {
8687
//===----------------------------------------------------------------------===//
8788

8889
/// 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.
90+
/// repeatedly applying the highest benefit patterns in a greedy worklist
91+
/// driven manner until a fixpoint is reached.
9192
///
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.
93+
/// The greedy rewrite may prematurely stop after a maximum number of
94+
/// iterations, which can be configured in the configuration parameter.
9595
///
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.
96+
/// Also performs folding and simple dead-code elimination before attempting to
97+
/// match any of the provided patterns.
9998
///
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.
99+
/// A region scope can be set in the configuration parameter. By default, the
100+
/// scope is set to the specified region. Only in-scope ops are added to the
101+
/// worklist and only in-scope ops are allowed to be modified by the patterns.
103102
///
104-
/// You may configure several aspects of this with GreedyRewriteConfig.
103+
/// Returns "success" if the iterative process converged (i.e., fixpoint was
104+
/// reached) and no more patterns can be matched within the region. `changed`
105+
/// is set to "true" if the IR was modified at all.
106+
///
107+
/// Note: This method does not apply patterns to the region's parent operation.
105108
LogicalResult
106109
applyPatternsAndFoldGreedily(Region &region,
107110
const FrozenRewritePatternSet &patterns,
108111
GreedyRewriteConfig config = GreedyRewriteConfig(),
109112
bool *changed = nullptr);
110113

111-
/// Rewrite ops in all regions of the given op, which must be isolated from
112-
/// above.
114+
/// Rewrite ops nested under the given operation, which must be isolated from
115+
/// above, by repeatedly applying the highest benefit patterns in a greedy
116+
/// worklist driven manner until a fixpoint is reached.
117+
///
118+
/// The greedy rewrite may prematurely stop after a maximum number of
119+
/// iterations, which can be configured in the configuration parameter.
120+
///
121+
/// Also performs folding and simple dead-code elimination before attempting to
122+
/// match any of the provided patterns.
123+
///
124+
/// This overload runs a separate greedy rewrite for each region of the
125+
/// specified op. A region scope can be set in the configuration parameter. By
126+
/// default, the scope is set to the region of the current greedy rewrite. Only
127+
/// in-scope ops are added to the worklist and only in-scope ops and the
128+
/// specified op itself are allowed to be modified by the patterns.
129+
///
130+
/// Note: The specified op may be modified, but it may not be removed by the
131+
/// patterns.
132+
///
133+
/// Returns "success" if the iterative process converged (i.e., fixpoint was
134+
/// reached) and no more patterns can be matched within the region. `changed`
135+
/// is set to "true" if the IR was modified at all.
136+
///
137+
/// Note: This method does not apply patterns to the given operation itself.
113138
inline LogicalResult
114139
applyPatternsAndFoldGreedily(Operation *op,
115140
const FrozenRewritePatternSet &patterns,
@@ -129,27 +154,34 @@ applyPatternsAndFoldGreedily(Operation *op,
129154
return failure(failed);
130155
}
131156

132-
/// Applies the specified rewrite patterns on `ops` while also trying to fold
133-
/// these ops.
157+
/// Rewrite the specified ops by repeatedly applying the highest benefit
158+
/// patterns in a greedy worklist driven manner until a fixpoint is reached.
159+
///
160+
/// The greedy rewrite may prematurely stop after a maximum number of
161+
/// iterations, which can be configured in the configuration parameter.
162+
///
163+
/// Also performs folding and simple dead-code elimination before attempting to
164+
/// match any of the provided patterns.
134165
///
135166
/// 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
167+
/// ops or supply operands to such ops are also processed, unless such ops are
137168
/// excluded via `config.strictMode`. Any other ops remain unmodified (i.e.,
138169
/// regardless of `strictMode`).
139170
///
140171
/// In addition to strictness, a region scope can be specified. Only ops within
141172
/// 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.
173+
/// where only ops within the given region/op are simplified by default. If no
174+
/// scope is specified, it is assumed to be the first common enclosing region of
175+
/// the given ops.
145176
///
146177
/// Note that ops in `ops` could be erased as result of folding, becoming dead,
147178
/// or via pattern rewrites. If more far reaching simplification is desired,
148-
/// applyPatternsAndFoldGreedily should be used.
179+
/// `applyPatternsAndFoldGreedily` should be used.
149180
///
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.
181+
/// Returns "success" if the iterative process converged (i.e., fixpoint was
182+
/// reached) and no more patterns can be matched. `changed` is set to "true" if
183+
/// the IR was modified at all. `allOpsErased` is set to "true" if all ops in
184+
/// `ops` were erased.
153185
LogicalResult
154186
applyOpPatternsAndFold(ArrayRef<Operation *> ops,
155187
const FrozenRewritePatternSet &patterns,

0 commit comments

Comments
 (0)