[mlir] Print some message for op-printing verification
authorChia-hung Duan <chiahungduan@google.com>
Tue, 10 May 2022 22:48:46 +0000 (22:48 +0000)
committerChia-hung Duan <chiahungduan@google.com>
Tue, 10 May 2022 22:48:47 +0000 (22:48 +0000)
Before dump, Insetad of switching to generic form silently after
verification failure. Print some debug logs to help identify why an op
may be printed in a different way.

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D125136

mlir/lib/IR/AsmPrinter.cpp

index 013bf16..c9c7d4b 100644 (file)
@@ -38,6 +38,7 @@
 #include "llvm/ADT/StringSet.h"
 #include "llvm/ADT/TypeSwitch.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Debug.h"
 #include "llvm/Support/Endian.h"
 #include "llvm/Support/Regex.h"
 #include "llvm/Support/SaveAndRestore.h"
@@ -48,6 +49,8 @@
 using namespace mlir;
 using namespace mlir::detail;
 
+#define DEBUG_TYPE "mlir-asm-printer"
+
 void OperationName::print(raw_ostream &os) const { os << getStringRef(); }
 
 void OperationName::dump() const { print(llvm::errs()); }
@@ -1313,14 +1316,28 @@ static OpPrintingFlags verifyOpAndAdjustFlags(Operation *op,
       printerFlags.shouldAssumeVerified())
     return printerFlags;
 
+  LLVM_DEBUG(llvm::dbgs() << DEBUG_TYPE << ": Verifying operation: "
+                          << op->getName() << "\n");
+
   // Ignore errors emitted by the verifier. We check the thread id to avoid
   // consuming other threads' errors.
   auto parentThreadId = llvm::get_threadid();
-  ScopedDiagnosticHandler diagHandler(op->getContext(), [&](Diagnostic &) {
-    return success(parentThreadId == llvm::get_threadid());
+  ScopedDiagnosticHandler diagHandler(op->getContext(), [&](Diagnostic &diag) {
+    if (parentThreadId == llvm::get_threadid()) {
+      LLVM_DEBUG({
+        diag.print(llvm::dbgs());
+        llvm::dbgs() << "\n";
+      });
+      return success();
+    }
+    return failure();
   });
-  if (failed(verify(op)))
+  if (failed(verify(op))) {
+    LLVM_DEBUG(llvm::dbgs()
+               << DEBUG_TYPE << ": '" << op->getName()
+               << "' failed to verify and will be printed in generic form\n");
     printerFlags.printGenericOpForm();
+  }
 
   return printerFlags;
 }