Skip to content

Commit c736369

Browse files
matthias-springerkuhar
authored andcommitted
Automerge: [mlir][IR] Deprecate match and rewrite functions (#130031)
Deprecate the `match` and `rewrite` functions. They mainly exist for historic reasons. This PR also updates all remaining uses of in the MLIR codebase. This is addressing a [comment](llvm/llvm-project#129861 (review)) on an earlier PR. Note for LLVM integration: `SplitMatchAndRewrite` will be deleted soon, update your patterns to use `matchAndRewrite` instead of separate `match` / `rewrite`. --------- Co-authored-by: Jakub Kuderski <[email protected]>
2 parents 18d05ba + a21cfca commit c736369

File tree

11 files changed

+158
-171
lines changed

11 files changed

+158
-171
lines changed

mlir/docs/DialectConversion.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,13 @@ updated/remapped operands of an operation, such as when the types of results
179179
defined by an operation have changed. The general Rewrite Patterns can no longer
180180
be used in these situations, as the types of the operands of the operation being
181181
matched will not correspond with those expected by the user. This pattern
182-
provides, as an additional argument to the `matchAndRewrite` and `rewrite`
183-
methods, the list of operands that the operation should use after conversion. If
184-
an operand was the result of a non-converted operation, for example if it was
185-
already legal, the original operand is used. This means that the operands
186-
provided always have a 1-1 non-null correspondence with the operands on the
187-
operation. The original operands of the operation are still intact and may be
188-
inspected as normal. These patterns also utilize a special `PatternRewriter`,
182+
provides, as an additional argument to the `matchAndRewrite` method, the list
183+
of operands that the operation should use after conversion. If an operand was
184+
the result of a non-converted operation, for example if it was already legal,
185+
the original operand is used. This means that the operands provided always have
186+
a 1-1 non-null correspondence with the operands on the operation. The original
187+
operands of the operation are still intact and may be inspected as normal.
188+
These patterns also utilize a special `PatternRewriter`,
189189
`ConversionPatternRewriter`, that provides special hooks for use with the
190190
conversion infrastructure.
191191

mlir/docs/PatternRewriter.md

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,9 @@ operation type, a special tag must be provided to make the intent explicit:
4848
### `matchAndRewrite` implementation
4949

5050
This is the chunk of code that matches a given root `Operation` and performs a
51-
rewrite of the IR. A `RewritePattern` can specify this implementation either via
52-
the `matchAndRewrite` method or via separate `match` and `rewrite` methods when
53-
deriving from `RewritePattern::SplitMatchAndRewrite`. When using the combined
54-
`matchAndRewrite` method, no IR mutation should take place before the match is
55-
deemed successful. The combined `matchAndRewrite` is useful when non-trivially
56-
recomputable information is required by the matching and rewriting phase. See
57-
below for examples:
51+
rewrite of the IR. A `RewritePattern` can specify this implementation via the
52+
`matchAndRewrite` method. No IR mutation should take place before the match is
53+
deemed successful. See below for examples:
5854

5955
```c++
6056
class MyPattern : public RewritePattern {
@@ -67,21 +63,6 @@ public:
6763
MyPattern(PatternBenefit benefit)
6864
: RewritePattern(benefit, MatchAnyOpTypeTag()) {}
6965

70-
/// In this section, the `match` and `rewrite` implementation is specified
71-
/// using the separate hooks.
72-
LogicalResult match(Operation *op) const override {
73-
// The `match` method returns `success()` if the pattern is a match, failure
74-
// otherwise.
75-
// ...
76-
}
77-
void rewrite(Operation *op, PatternRewriter &rewriter) const override {
78-
// The `rewrite` method performs mutations on the IR rooted at `op` using
79-
// the provided rewriter. All mutations must go through the provided
80-
// rewriter.
81-
}
82-
83-
/// In this section, the `match` and `rewrite` implementation is specified
84-
/// using a single hook.
8566
LogicalResult matchAndRewrite(Operation *op, PatternRewriter &rewriter) const override {
8667
// The `matchAndRewrite` method performs both the matching and the mutation.
8768
// Note that the match must reach a successful point before IR mutation may
@@ -92,12 +73,6 @@ public:
9273
9374
#### Restrictions
9475
95-
Within the `match` section of a pattern, the following constraints apply:
96-
97-
* No mutation of the IR is allowed.
98-
99-
Within the `rewrite` section of a pattern, the following constraints apply:
100-
10176
* All IR mutations, including creation, *must* be performed by the given
10277
`PatternRewriter`. This class provides hooks for performing all of the
10378
possible mutations that may take place within a pattern. For example, this
@@ -107,8 +82,6 @@ Within the `rewrite` section of a pattern, the following constraints apply:
10782
* The root operation is required to either be: updated in-place, replaced, or
10883
erased.
10984
* `matchAndRewrite` must return "success" if and only if the IR was modified.
110-
`match` must return "success" if and only if the IR is going to be modified
111-
during `rewrite`.
11285
11386
11487
### Application Recursion

mlir/docs/Tutorials/QuickstartRewrites.md

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -216,25 +216,6 @@ In case ODS patterns and `matchAndRewrite`-style functions are not sufficient
216216
you can also specify rewrites as a general set of `RewritePattern`s:
217217

218218
```c++
219-
/// Multi-step rewrite using "match" and "rewrite". This allows for separating
220-
/// the concerns of matching and rewriting.
221-
struct ConvertTFLeakyRelu : public RewritePattern {
222-
ConvertTFLeakyRelu(MLIRContext *context)
223-
: RewritePattern("tf.LeakyRelu", 1, context) {}
224-
225-
LogicalResult match(Operation *op) const override {
226-
return success();
227-
}
228-
229-
void rewrite(Operation *op, PatternRewriter &rewriter) const override {
230-
rewriter.replaceOpWithNewOp<TFL::LeakyReluOp>(
231-
op, op->getResult(0).getType(), op->getOperand(0),
232-
/*alpha=*/op->getAttrOfType<FloatAttr>("alpha"));
233-
}
234-
};
235-
236-
/// Single-step rewrite with "matchAndRewrite". This allows for performing the
237-
/// rewrite immediately upon a successful match.
238219
struct ConvertTFLeakyRelu : public RewritePattern {
239220
ConvertTFLeakyRelu(MLIRContext *context)
240221
: RewritePattern("tf.LeakyRelu", 1, context) {}

mlir/include/mlir/Conversion/LLVMCommon/Pattern.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ LogicalResult oneToOneRewrite(
4040
/// during the entire pattern lifetime.
4141
class ConvertToLLVMPattern : public ConversionPattern {
4242
public:
43+
/// `SplitMatchAndRewrite` is deprecated. Use `matchAndRewrite` instead of
44+
/// separate `match` and `rewrite`.
4345
using SplitMatchAndRewrite =
4446
detail::ConversionSplitMatchAndRewriteImpl<ConvertToLLVMPattern>;
4547

@@ -149,6 +151,9 @@ class ConvertOpToLLVMPattern : public ConvertToLLVMPattern {
149151
using OpAdaptor = typename SourceOp::Adaptor;
150152
using OneToNOpAdaptor =
151153
typename SourceOp::template GenericAdaptor<ArrayRef<ValueRange>>;
154+
155+
/// `SplitMatchAndRewrite` is deprecated. Use `matchAndRewrite` instead of
156+
/// separate `match` and `rewrite`.
152157
using SplitMatchAndRewrite = detail::ConversionSplitMatchAndRewriteImpl<
153158
ConvertOpToLLVMPattern<SourceOp>>;
154159

mlir/include/mlir/IR/PatternMatch.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ class Pattern {
237237
namespace detail {
238238
/// Helper class that derives from a RewritePattern class and provides separate
239239
/// `match` and `rewrite` entry points instead of a combined `matchAndRewrite`.
240+
///
241+
/// This class is deprecated. Use `matchAndRewrite` instead of separate `match`
242+
/// and `rewrite`.
240243
template <typename PatternT>
241244
class SplitMatchAndRewriteImpl : public PatternT {
242245
using PatternT::PatternT;
@@ -268,6 +271,9 @@ class SplitMatchAndRewriteImpl : public PatternT {
268271
class RewritePattern : public Pattern {
269272
public:
270273
using OperationT = Operation *;
274+
275+
/// `SplitMatchAndRewrite` is deprecated. Use `matchAndRewrite` instead of
276+
/// separate `match` and `rewrite`.
271277
using SplitMatchAndRewrite = detail::SplitMatchAndRewriteImpl<RewritePattern>;
272278

273279
virtual ~RewritePattern() = default;
@@ -350,6 +356,9 @@ struct OpOrInterfaceRewritePatternBase : public RewritePattern {
350356
template <typename SourceOp>
351357
struct OpRewritePattern
352358
: public detail::OpOrInterfaceRewritePatternBase<SourceOp> {
359+
360+
/// `SplitMatchAndRewrite` is deprecated. Use `matchAndRewrite` instead of
361+
/// separate `match` and `rewrite`.
353362
using SplitMatchAndRewrite =
354363
detail::SplitMatchAndRewriteImpl<OpRewritePattern<SourceOp>>;
355364

@@ -368,6 +377,9 @@ struct OpRewritePattern
368377
template <typename SourceOp>
369378
struct OpInterfaceRewritePattern
370379
: public detail::OpOrInterfaceRewritePatternBase<SourceOp> {
380+
381+
/// `SplitMatchAndRewrite` is deprecated. Use `matchAndRewrite` instead of
382+
/// separate `match` and `rewrite`.
371383
using SplitMatchAndRewrite =
372384
detail::SplitMatchAndRewriteImpl<OpInterfaceRewritePattern<SourceOp>>;
373385

mlir/include/mlir/Transforms/DialectConversion.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,9 @@ class ConversionPattern : public RewritePattern {
598598
using OperationT = Operation *;
599599
using OpAdaptor = ArrayRef<Value>;
600600
using OneToNOpAdaptor = ArrayRef<ValueRange>;
601+
602+
/// `SplitMatchAndRewrite` is deprecated. Use `matchAndRewrite` instead of
603+
/// separate `match` and `rewrite`.
601604
using SplitMatchAndRewrite =
602605
detail::ConversionSplitMatchAndRewriteImpl<ConversionPattern>;
603606

@@ -669,6 +672,9 @@ class OpConversionPattern : public ConversionPattern {
669672
using OpAdaptor = typename SourceOp::Adaptor;
670673
using OneToNOpAdaptor =
671674
typename SourceOp::template GenericAdaptor<ArrayRef<ValueRange>>;
675+
676+
/// `SplitMatchAndRewrite` is deprecated. Use `matchAndRewrite` instead of
677+
/// separate `match` and `rewrite`.
672678
using SplitMatchAndRewrite =
673679
detail::ConversionSplitMatchAndRewriteImpl<OpConversionPattern<SourceOp>>;
674680

0 commit comments

Comments
 (0)