[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
Sun Mar 15 14:27:41 PDT 2020


rriddle created this revision.
rriddle added reviewers: mehdi_amini, jpienaar.
Herald added subscribers: llvm-commits, Joonsoo, liufengdb, lucyrfox, mgester, arpith-jacob, nicolasvasilache, antiagainst, shauheen, burmako.
Herald added a project: LLVM.

This revision adds a new hook, `notifyMatchFailure`, that allows for notifying the rewriter that a match failure is coming with the provided reason. This hook takes as a parameter a callback that fills a `Diagnostic` instance with the reason why the match failed. This allows for the rewriter to decide how this information can be displayed to the end-user, and may completely ignore it if desired(opt mode). For now, DialectConversion is updated to include this information in the debug output.

Depends On D76202 <https://reviews.llvm.org/D76202>


Repository:
  rG LLVM Github Monorepo

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::Error);
+    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.250441.patch
Type: text/x-patch
Size: 3358 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200315/3aae1cb4/attachment.bin>


More information about the llvm-commits mailing list