Skip to content

Commit c569c1f

Browse files
authored
[MLIR] Migrate pattern application / dialect conversion to the LDBG logging format (#150991)
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.
1 parent 803a50a commit c569c1f

File tree

4 files changed

+41
-27
lines changed

4 files changed

+41
-27
lines changed

llvm/include/llvm/Support/DebugLog.h

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ namespace llvm {
6161
for (bool _c = \
6262
(::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE, LEVEL)); \
6363
_c; _c = false) \
64+
for (::llvm::impl::RAIINewLineStream NewLineStream{(STREAM)}; _c; \
65+
_c = false) \
6466
::llvm::impl::raw_ldbg_ostream{ \
65-
::llvm::impl::computePrefix(TYPE, FILE, LINE, LEVEL), (STREAM)} \
67+
::llvm::impl::computePrefix(TYPE, FILE, LINE, LEVEL), NewLineStream} \
6668
.asLvalue()
6769

6870
#define DEBUGLOG_WITH_STREAM_TYPE_AND_FILE(STREAM, LEVEL, TYPE, FILE) \
@@ -81,14 +83,15 @@ namespace llvm {
8183

8284
namespace impl {
8385

84-
/// A raw_ostream that tracks `\n` and print the prefix.
86+
/// A raw_ostream that tracks `\n` and print the prefix after each
87+
/// newline.
8588
class LLVM_ABI raw_ldbg_ostream final : public raw_ostream {
8689
std::string Prefix;
8790
raw_ostream &Os;
88-
bool HasPendingNewline = true;
91+
bool HasPendingNewline;
8992

90-
/// Split the line on newlines and insert the prefix before each newline.
91-
/// Forward everything to the underlying stream.
93+
/// Split the line on newlines and insert the prefix before each
94+
/// newline. Forward everything to the underlying stream.
9295
void write_impl(const char *Ptr, size_t Size) final {
9396
auto Str = StringRef(Ptr, Size);
9497
// Handle the initial prefix.
@@ -109,22 +112,18 @@ class LLVM_ABI raw_ldbg_ostream final : public raw_ostream {
109112
}
110113
void emitPrefix() { Os.write(Prefix.c_str(), Prefix.size()); }
111114
void writeWithPrefix(StringRef Str) {
112-
if (HasPendingNewline) {
113-
emitPrefix();
114-
HasPendingNewline = false;
115-
}
115+
flushEol();
116116
Os.write(Str.data(), Str.size());
117117
}
118118

119119
public:
120-
explicit raw_ldbg_ostream(std::string Prefix, raw_ostream &Os)
121-
: Prefix(std::move(Prefix)), Os(Os) {
120+
explicit raw_ldbg_ostream(std::string Prefix, raw_ostream &Os,
121+
bool HasPendingNewline = true)
122+
: Prefix(std::move(Prefix)), Os(Os),
123+
HasPendingNewline(HasPendingNewline) {
122124
SetUnbuffered();
123125
}
124-
~raw_ldbg_ostream() final {
125-
flushEol();
126-
Os << '\n';
127-
}
126+
~raw_ldbg_ostream() final { flushEol(); }
128127
void flushEol() {
129128
if (HasPendingNewline) {
130129
emitPrefix();
@@ -135,10 +134,22 @@ class LLVM_ABI raw_ldbg_ostream final : public raw_ostream {
135134
/// Forward the current_pos method to the underlying stream.
136135
uint64_t current_pos() const final { return Os.tell(); }
137136

138-
/// Some of the `<<` operators expect an lvalue, so we trick the type system.
137+
/// Some of the `<<` operators expect an lvalue, so we trick the type
138+
/// system.
139139
raw_ldbg_ostream &asLvalue() { return *this; }
140140
};
141141

142+
/// A raw_ostream that prints a newline on destruction, useful for LDBG()
143+
class RAIINewLineStream final : public raw_ostream {
144+
raw_ostream &Os;
145+
146+
public:
147+
RAIINewLineStream(raw_ostream &Os) : Os(Os) { SetUnbuffered(); }
148+
~RAIINewLineStream() { Os << '\n'; }
149+
void write_impl(const char *Ptr, size_t Size) final { Os.write(Ptr, Size); }
150+
uint64_t current_pos() const final { return Os.tell(); }
151+
};
152+
142153
/// Remove the path prefix from the file name.
143154
static LLVM_ATTRIBUTE_UNUSED constexpr const char *
144155
getShortFileName(const char *path) {

llvm/unittests/Support/DebugLogTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ TEST(DebugLogTest, StreamPrefix) {
121121
EXPECT_EQ(os.str(), expected);
122122
}
123123
// After destructors, there was a pending newline for stream B.
124-
EXPECT_EQ(os.str(), expected + "\nPrefixB \n");
124+
EXPECT_EQ(os.str(), expected + "PrefixB ");
125125
}
126126
#else
127127
TEST(DebugLogTest, Basic) {

mlir/lib/Rewrite/PatternApplicator.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
#include "mlir/Rewrite/PatternApplicator.h"
1515
#include "ByteCode.h"
16-
#include "llvm/Support/Debug.h"
16+
#include "llvm/Support/DebugLog.h"
1717

1818
#ifndef NDEBUG
1919
#include "llvm/ADT/ScopeExit.h"
@@ -51,9 +51,7 @@ static Operation *getDumpRootOp(Operation *op) {
5151
return op;
5252
}
5353
static void logSucessfulPatternApplication(Operation *op) {
54-
llvm::dbgs() << "// *** IR Dump After Pattern Application ***\n";
55-
op->dump();
56-
llvm::dbgs() << "\n\n";
54+
LDBG(2) << "// *** IR Dump After Pattern Application ***\n" << *op << "\n";
5755
}
5856
#endif
5957

@@ -208,8 +206,8 @@ LogicalResult PatternApplicator::matchAndRewrite(
208206
result =
209207
bytecode->rewrite(rewriter, *pdlMatch, *mutableByteCodeState);
210208
} else {
211-
LLVM_DEBUG(llvm::dbgs() << "Trying to match \""
212-
<< bestPattern->getDebugName() << "\"\n");
209+
LDBG() << "Trying to match \"" << bestPattern->getDebugName()
210+
<< "\"";
213211
const auto *pattern =
214212
static_cast<const RewritePattern *>(bestPattern);
215213

@@ -223,9 +221,8 @@ LogicalResult PatternApplicator::matchAndRewrite(
223221
[&] { rewriter.setListener(oldListener); });
224222
#endif
225223
result = pattern->matchAndRewrite(op, rewriter);
226-
LLVM_DEBUG(llvm::dbgs()
227-
<< "\"" << bestPattern->getDebugName() << "\" result "
228-
<< succeeded(result) << "\n");
224+
LDBG() << " -> matchAndRewrite "
225+
<< (succeeded(result) ? "successful" : "failed");
229226
}
230227

231228
// Process the result of the pattern application.

mlir/lib/Transforms/Utils/DialectConversion.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/ADT/ScopeExit.h"
2121
#include "llvm/ADT/SmallPtrSet.h"
2222
#include "llvm/Support/Debug.h"
23+
#include "llvm/Support/DebugLog.h"
2324
#include "llvm/Support/FormatVariadic.h"
2425
#include "llvm/Support/SaveAndRestore.h"
2526
#include "llvm/Support/ScopedPrinter.h"
@@ -1129,8 +1130,13 @@ struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
11291130
/// verification.
11301131
SmallPtrSet<Operation *, 1> pendingRootUpdates;
11311132

1133+
/// A raw output stream used to prefix the debug log.
1134+
llvm::impl::raw_ldbg_ostream os{(Twine("[") + DEBUG_TYPE + "] ").str(),
1135+
llvm::dbgs(), /*HasPendingNewline=*/false};
1136+
11321137
/// A logger used to emit diagnostics during the conversion process.
1133-
llvm::ScopedPrinter logger{llvm::dbgs()};
1138+
llvm::ScopedPrinter logger{os};
1139+
std::string logPrefix;
11341140
#endif
11351141
};
11361142
} // namespace detail

0 commit comments

Comments
 (0)