Skip to content

Commit 5e025f8

Browse files
address comments
1 parent 68f0685 commit 5e025f8

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

mlir/include/mlir/IR/PatternMatch.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,18 @@ template <typename PatternT>
241241
class SplitMatchAndRewriteImpl : public PatternT {
242242
using PatternT::PatternT;
243243

244+
/// Attempt to match against IR rooted at the specified operation, which is
245+
/// the same operation kind as getRootKind().
246+
///
247+
/// Note: This function must not modify the IR.
248+
virtual LogicalResult match(typename PatternT::OperationT op) const = 0;
249+
244250
/// Rewrite the IR rooted at the specified operation with the result of
245251
/// this pattern, generating any new operations with the specified
246252
/// rewriter.
247253
virtual void rewrite(typename PatternT::OperationT op,
248254
PatternRewriter &rewriter) const = 0;
249255

250-
/// Attempt to match against code rooted at the specified operation,
251-
/// which is the same operation code as getRootKind().
252-
virtual LogicalResult match(typename PatternT::OperationT op) const = 0;
253-
254256
LogicalResult matchAndRewrite(typename PatternT::OperationT op,
255257
PatternRewriter &rewriter) const final {
256258
if (succeeded(match(op))) {
@@ -263,9 +265,7 @@ class SplitMatchAndRewriteImpl : public PatternT {
263265
} // namespace detail
264266

265267
/// RewritePattern is the common base class for all DAG to DAG replacements.
266-
/// By overloading the "matchAndRewrite" function, the user can perform the
267-
/// rewrite in the same call as the match.
268-
///
268+
269269
class RewritePattern : public Pattern {
270270
public:
271271
using OperationT = Operation *;
@@ -274,8 +274,11 @@ class RewritePattern : public Pattern {
274274
virtual ~RewritePattern() = default;
275275

276276
/// Attempt to match against code rooted at the specified operation,
277-
/// which is the same operation code as getRootKind(). If successful, this
278-
/// function will automatically perform the rewrite.
277+
/// which is the same operation code as getRootKind(). If successful, perform
278+
/// the rewrite.
279+
///
280+
/// Note: Implementations must modify the IR if and only if the function
281+
/// returns "success".
279282
virtual LogicalResult matchAndRewrite(Operation *op,
280283
PatternRewriter &rewriter) const = 0;
281284

mlir/include/mlir/Transforms/DialectConversion.h

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,12 @@ template <typename PatternT>
536536
class ConversionSplitMatchAndRewriteImpl : public PatternT {
537537
using PatternT::PatternT;
538538

539+
/// Attempt to match against IR rooted at the specified operation, which is
540+
/// the same operation kind as getRootKind().
541+
///
542+
/// Note: This function must not modify the IR.
543+
virtual LogicalResult match(typename PatternT::OperationT op) const = 0;
544+
539545
/// Rewrite the IR rooted at the specified operation with the result of
540546
/// this pattern, generating any new operations with the specified
541547
/// rewriter.
@@ -560,17 +566,6 @@ class ConversionSplitMatchAndRewriteImpl : public PatternT {
560566
}
561567
}
562568

563-
/// Attempt to match against code rooted at the specified operation,
564-
/// which is the same operation code as getRootKind().
565-
virtual LogicalResult match(typename PatternT::OperationT op) const = 0;
566-
567-
LogicalResult
568-
matchAndRewrite(typename PatternT::OperationT op,
569-
typename PatternT::OpAdaptor adaptor,
570-
ConversionPatternRewriter &rewriter) const final {
571-
llvm_unreachable("1:1 matchAndRewrite entry point is never used");
572-
}
573-
574569
LogicalResult
575570
matchAndRewrite(typename PatternT::OperationT op,
576571
typename PatternT::OneToNOpAdaptor adaptor,
@@ -581,6 +576,17 @@ class ConversionSplitMatchAndRewriteImpl : public PatternT {
581576
}
582577
return failure();
583578
}
579+
580+
LogicalResult
581+
matchAndRewrite(typename PatternT::OperationT op,
582+
typename PatternT::OpAdaptor adaptor,
583+
ConversionPatternRewriter &rewriter) const final {
584+
// Users would normally override this function in conversion patterns to
585+
// implement a 1:1 pattern. Patterns that are derived from this class have
586+
// separate `match` and `rewrite` functions, so this `matchAndRewrite`
587+
// overload is obsolete.
588+
llvm_unreachable("this function is unreachable");
589+
}
584590
};
585591
} // namespace detail
586592

0 commit comments

Comments
 (0)