Skip to content

[MLIR] Migrate pattern application / dialect conversion to the LDBG logging format #150991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions llvm/include/llvm/Support/DebugLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand All @@ -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;
bool HasPendingNewline;

/// 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.
Expand All @@ -109,22 +112,18 @@ class LLVM_ABI raw_ldbg_ostream final : public raw_ostream {
}
void emitPrefix() { Os.write(Prefix.c_str(), Prefix.size()); }
void writeWithPrefix(StringRef Str) {
if (HasPendingNewline) {
emitPrefix();
HasPendingNewline = false;
}
flushEol();
Os.write(Str.data(), Str.size());
}

public:
explicit raw_ldbg_ostream(std::string Prefix, raw_ostream &Os)
: Prefix(std::move(Prefix)), Os(Os) {
explicit raw_ldbg_ostream(std::string Prefix, raw_ostream &Os,
bool HasPendingNewline = true)
: Prefix(std::move(Prefix)), Os(Os),
HasPendingNewline(HasPendingNewline) {
SetUnbuffered();
}
~raw_ldbg_ostream() final {
flushEol();
Os << '\n';
}
~raw_ldbg_ostream() final { flushEol(); }
void flushEol() {
if (HasPendingNewline) {
emitPrefix();
Expand All @@ -135,10 +134,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) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/unittests/Support/DebugLogTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 6 additions & 9 deletions mlir/lib/Rewrite/PatternApplicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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);

Expand All @@ -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.
Expand Down
8 changes: 7 additions & 1 deletion mlir/lib/Transforms/Utils/DialectConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(), /*HasPendingNewline=*/false};

/// 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
Expand Down