[Mlir-commits] [llvm] [mlir] [MLIR] Migrate pattern application / dialect conversion to the LDBG logging format (PR #150991)

Mehdi Amini llvmlistbot at llvm.org
Thu Jul 31 09:21:05 PDT 2025


https://github.com/joker-eph updated https://github.com/llvm/llvm-project/pull/150991

>From 3a854118fcf06861f1ea3dabf110781040246ac5 Mon Sep 17 00:00:00 2001
From: Mehdi Amini <joker.eph at gmail.com>
Date: Sat, 26 Jul 2025 07:30:30 -0700
Subject: [PATCH] [MLIR] Migrate pattern application / dialect conversion to
 the LDBG logging format

This prefix the output with the DEBUG_TYPE.
Dialect conversion is using a ScopedPrinter, we insert the raw_ldbg_ostream to
consistently prefix each new line.
---
 llvm/include/llvm/Support/DebugLog.h          | 30 +++++++++++++------
 llvm/unittests/Support/DebugLogTest.cpp       |  2 +-
 mlir/lib/Rewrite/PatternApplicator.cpp        | 15 ++++------
 .../Transforms/Utils/DialectConversion.cpp    |  8 ++++-
 4 files changed, 35 insertions(+), 20 deletions(-)

diff --git a/llvm/include/llvm/Support/DebugLog.h b/llvm/include/llvm/Support/DebugLog.h
index 8fca2d5e2816b..d62072e21a1b1 100644
--- a/llvm/include/llvm/Support/DebugLog.h
+++ b/llvm/include/llvm/Support/DebugLog.h
@@ -61,8 +61,10 @@ namespace llvm {
   for (bool _c =                                                               \
            (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE, LEVEL));     \
        _c; _c = false)                                                         \
+    for (::llvm::impl::RAIINewLineStream NewLineStream{(STREAM)}; _c;          \
+         _c = false)                                                           \
   ::llvm::impl::raw_ldbg_ostream{                                              \
-      ::llvm::impl::computePrefix(TYPE, FILE, LINE, LEVEL), (STREAM)}          \
+      ::llvm::impl::computePrefix(TYPE, FILE, LINE, LEVEL), NewLineStream}     \
       .asLvalue()
 
 #define DEBUGLOG_WITH_STREAM_TYPE_AND_FILE(STREAM, LEVEL, TYPE, FILE)          \
@@ -81,14 +83,15 @@ namespace llvm {
 
 namespace impl {
 
-/// A raw_ostream that tracks `\n` and print the prefix.
+/// A raw_ostream that tracks `\n` and print the prefix after each
+/// newline.
 class LLVM_ABI raw_ldbg_ostream final : public raw_ostream {
   std::string Prefix;
   raw_ostream &Os;
   bool HasPendingNewline = true;
 
-  /// Split the line on newlines and insert the prefix before each newline.
-  /// Forward everything to the underlying stream.
+  /// Split the line on newlines and insert the prefix before each
+  /// newline. Forward everything to the underlying stream.
   void write_impl(const char *Ptr, size_t Size) final {
     auto Str = StringRef(Ptr, Size);
     // Handle the initial prefix.
@@ -121,10 +124,7 @@ class LLVM_ABI raw_ldbg_ostream final : public raw_ostream {
       : Prefix(std::move(Prefix)), Os(Os) {
     SetUnbuffered();
   }
-  ~raw_ldbg_ostream() final {
-    flushEol();
-    Os << '\n';
-  }
+  ~raw_ldbg_ostream() final { flushEol(); }
   void flushEol() {
     if (HasPendingNewline) {
       emitPrefix();
@@ -135,10 +135,22 @@ class LLVM_ABI raw_ldbg_ostream final : public raw_ostream {
   /// Forward the current_pos method to the underlying stream.
   uint64_t current_pos() const final { return Os.tell(); }
 
-  /// Some of the `<<` operators expect an lvalue, so we trick the type system.
+  /// Some of the `<<` operators expect an lvalue, so we trick the type
+  /// system.
   raw_ldbg_ostream &asLvalue() { return *this; }
 };
 
+/// A raw_ostream that prints a newline on destruction, useful for LDBG()
+class RAIINewLineStream final : public raw_ostream {
+  raw_ostream &Os;
+
+public:
+  RAIINewLineStream(raw_ostream &Os) : Os(Os) { SetUnbuffered(); }
+  ~RAIINewLineStream() { Os << '\n'; }
+  void write_impl(const char *Ptr, size_t Size) final { Os.write(Ptr, Size); }
+  uint64_t current_pos() const final { return Os.tell(); }
+};
+
 /// Remove the path prefix from the file name.
 static LLVM_ATTRIBUTE_UNUSED constexpr const char *
 getShortFileName(const char *path) {
diff --git a/llvm/unittests/Support/DebugLogTest.cpp b/llvm/unittests/Support/DebugLogTest.cpp
index 0c464c16cf269..c24d1a5693169 100644
--- a/llvm/unittests/Support/DebugLogTest.cpp
+++ b/llvm/unittests/Support/DebugLogTest.cpp
@@ -121,7 +121,7 @@ TEST(DebugLogTest, StreamPrefix) {
     EXPECT_EQ(os.str(), expected);
   }
   // After destructors, there was a pending newline for stream B.
-  EXPECT_EQ(os.str(), expected + "\nPrefixB \n");
+  EXPECT_EQ(os.str(), expected + "PrefixB ");
 }
 #else
 TEST(DebugLogTest, Basic) {
diff --git a/mlir/lib/Rewrite/PatternApplicator.cpp b/mlir/lib/Rewrite/PatternApplicator.cpp
index b2b372b7b1249..e13bcff2767ec 100644
--- a/mlir/lib/Rewrite/PatternApplicator.cpp
+++ b/mlir/lib/Rewrite/PatternApplicator.cpp
@@ -13,7 +13,7 @@
 
 #include "mlir/Rewrite/PatternApplicator.h"
 #include "ByteCode.h"
-#include "llvm/Support/Debug.h"
+#include "llvm/Support/DebugLog.h"
 
 #ifndef NDEBUG
 #include "llvm/ADT/ScopeExit.h"
@@ -51,9 +51,7 @@ static Operation *getDumpRootOp(Operation *op) {
   return op;
 }
 static void logSucessfulPatternApplication(Operation *op) {
-  llvm::dbgs() << "// *** IR Dump After Pattern Application ***\n";
-  op->dump();
-  llvm::dbgs() << "\n\n";
+  LDBG(2) << "// *** IR Dump After Pattern Application ***\n" << *op << "\n";
 }
 #endif
 
@@ -208,8 +206,8 @@ LogicalResult PatternApplicator::matchAndRewrite(
             result =
                 bytecode->rewrite(rewriter, *pdlMatch, *mutableByteCodeState);
           } else {
-            LLVM_DEBUG(llvm::dbgs() << "Trying to match \""
-                                    << bestPattern->getDebugName() << "\"\n");
+            LDBG() << "Trying to match \"" << bestPattern->getDebugName()
+                   << "\"";
             const auto *pattern =
                 static_cast<const RewritePattern *>(bestPattern);
 
@@ -223,9 +221,8 @@ LogicalResult PatternApplicator::matchAndRewrite(
                 [&] { rewriter.setListener(oldListener); });
 #endif
             result = pattern->matchAndRewrite(op, rewriter);
-            LLVM_DEBUG(llvm::dbgs()
-                       << "\"" << bestPattern->getDebugName() << "\" result "
-                       << succeeded(result) << "\n");
+            LDBG() << " -> matchAndRewrite "
+                   << (succeeded(result) ? "successful" : "failed");
           }
 
           // Process the result of the pattern application.
diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index 08803e082b057..d8151eef729fc 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -20,6 +20,7 @@
 #include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/Support/Debug.h"
+#include "llvm/Support/DebugLog.h"
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/Support/SaveAndRestore.h"
 #include "llvm/Support/ScopedPrinter.h"
@@ -1129,8 +1130,13 @@ struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
   /// verification.
   SmallPtrSet<Operation *, 1> pendingRootUpdates;
 
+  /// A raw output stream used to prefix the debug log.
+  llvm::impl::raw_ldbg_ostream os{(Twine("[") + DEBUG_TYPE + "] ").str(),
+                                  llvm::dbgs()};
+
   /// A logger used to emit diagnostics during the conversion process.
-  llvm::ScopedPrinter logger{llvm::dbgs()};
+  llvm::ScopedPrinter logger{os};
+  std::string logPrefix;
 #endif
 };
 } // namespace detail



More information about the Mlir-commits mailing list