[PATCH] D76203: [mlir] Add a hook to PatternRewriter to allow for patterns to notify why a match failed.

River Riddle via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 16 13:40:24 PDT 2020


rriddle updated this revision to Diff 250623.
rriddle added a comment.

Resolve comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D76203/new/

https://reviews.llvm.org/D76203

Files:
  mlir/include/mlir/IR/PatternMatch.h
  mlir/include/mlir/Transforms/DialectConversion.h
  mlir/lib/Transforms/DialectConversion.cpp
  mlir/test/lib/TestDialect/TestPatterns.cpp


Index: mlir/test/lib/TestDialect/TestPatterns.cpp
===================================================================
--- mlir/test/lib/TestDialect/TestPatterns.cpp
+++ mlir/test/lib/TestDialect/TestPatterns.cpp
@@ -272,7 +272,7 @@
                   ConversionPatternRewriter &rewriter) const final {
     // If the type is F32, change the type to F64.
     if (!Type(*op->result_type_begin()).isF32())
-      return matchFailure();
+      return rewriter.notifyMatchFailure(op, "expected single f32 operand");
     rewriter.replaceOpWithNewOp<TestTypeProducerOp>(op, rewriter.getF64Type());
     return matchSuccess();
   }
Index: mlir/lib/Transforms/DialectConversion.cpp
===================================================================
--- mlir/lib/Transforms/DialectConversion.cpp
+++ mlir/lib/Transforms/DialectConversion.cpp
@@ -989,6 +989,17 @@
   rootUpdates.erase(rootUpdates.begin() + (rootUpdates.rend() - it));
 }
 
+/// PatternRewriter hook for notifying match failure reasons.
+LogicalResult ConversionPatternRewriter::notifyMatchFailure(
+    Operation *op, function_ref<void(Diagnostic &)> reasonCallback) {
+  LLVM_DEBUG({
+    Diagnostic diag(op->getLoc(), DiagnosticSeverity::Remark);
+    reasonCallback(diag);
+    impl->logger.startLine() << "** Failure : " << diag.str() << "\n";
+  });
+  return failure();
+}
+
 /// Return a reference to the internal implementation.
 detail::ConversionPatternRewriterImpl &ConversionPatternRewriter::getImpl() {
   return *impl;
Index: mlir/include/mlir/Transforms/DialectConversion.h
===================================================================
--- mlir/include/mlir/Transforms/DialectConversion.h
+++ mlir/include/mlir/Transforms/DialectConversion.h
@@ -379,6 +379,12 @@
   /// PatternRewriter hook for updating the root operation in-place.
   void cancelRootUpdate(Operation *op) override;
 
+  /// PatternRewriter hook for notifying match failure reasons.
+  LogicalResult
+  notifyMatchFailure(Operation *op,
+                     function_ref<void(Diagnostic &)> reasonCallback) override;
+  using PatternRewriter::notifyMatchFailure;
+
   /// Return a reference to the internal implementation.
   detail::ConversionPatternRewriterImpl &getImpl();
 
Index: mlir/include/mlir/IR/PatternMatch.h
===================================================================
--- mlir/include/mlir/IR/PatternMatch.h
+++ mlir/include/mlir/IR/PatternMatch.h
@@ -334,6 +334,23 @@
     finalizeRootUpdate(root);
   }
 
+  /// Notify the pattern rewriter that the pattern is failing to match the given
+  /// operation, and provide a callback to populate a diagnostic with the reason
+  /// why the failure occurred. This method allows for derived rewriters to
+  /// optionally hook into the reason why a pattern failed, and display it to
+  /// users.
+  virtual LogicalResult
+  notifyMatchFailure(Operation *op,
+                     function_ref<void(Diagnostic &)> reasonCallback) {
+    return failure();
+  }
+  LogicalResult notifyMatchFailure(Operation *op, const Twine &msg) {
+    return notifyMatchFailure(op, [&](Diagnostic &diag) { diag << msg; });
+  }
+  LogicalResult notifyMatchFailure(Operation *op, const char *msg) {
+    return notifyMatchFailure(op, Twine(msg));
+  }
+
 protected:
   explicit PatternRewriter(MLIRContext *ctx) : OpBuilder(ctx) {}
   virtual ~PatternRewriter();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76203.250623.patch
Type: text/x-patch
Size: 3359 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200316/77bdaf62/attachment.bin>


More information about the llvm-commits mailing list