Skip to content

Commit 89abccc

Browse files
authored
[MLIR] Update GreedyRewriter to use the LDBG() debug log mechanism (NFC) (#153961)
Also improve a bit the LDBG() implementation
1 parent 8c605bd commit 89abccc

File tree

4 files changed

+48
-38
lines changed

4 files changed

+48
-38
lines changed

llvm/include/llvm/Support/DebugLog.h

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,10 @@ namespace llvm {
7171
for (bool _c = \
7272
(::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE, LEVEL)); \
7373
_c; _c = false) \
74-
for (::llvm::impl::RAIINewLineStream NewLineStream{(STREAM)}; _c; \
75-
_c = false) \
76-
::llvm::impl::raw_ldbg_ostream{ \
77-
::llvm::impl::computePrefix(TYPE, FILE, LINE, LEVEL), NewLineStream} \
78-
.asLvalue()
74+
for (::llvm::impl::raw_ldbg_ostream LdbgOS{ \
75+
::llvm::impl::computePrefix(TYPE, FILE, LINE, LEVEL), (STREAM)}; \
76+
_c; _c = false) \
77+
::llvm::impl::RAIINewLineStream{LdbgOS}.asLvalue()
7978

8079
#define DEBUGLOG_WITH_STREAM_TYPE_AND_FILE(STREAM, LEVEL, TYPE, FILE) \
8180
DEBUGLOG_WITH_STREAM_TYPE_FILE_AND_LINE(STREAM, LEVEL, TYPE, FILE, __LINE__)
@@ -89,22 +88,22 @@ namespace impl {
8988
class LLVM_ABI raw_ldbg_ostream final : public raw_ostream {
9089
std::string Prefix;
9190
raw_ostream &Os;
92-
bool HasPendingNewline;
91+
bool ShouldPrefixNextString;
9392

9493
/// Split the line on newlines and insert the prefix before each
9594
/// newline. Forward everything to the underlying stream.
9695
void write_impl(const char *Ptr, size_t Size) final {
9796
auto Str = StringRef(Ptr, Size);
98-
// Handle the initial prefix.
99-
if (!Str.empty())
100-
writeWithPrefix(StringRef());
101-
10297
auto Eol = Str.find('\n');
98+
// Handle `\n` occurring in the string, ensure to print the prefix at the
99+
// beginning of each line.
103100
while (Eol != StringRef::npos) {
101+
// Take the line up to the newline (including the newline).
104102
StringRef Line = Str.take_front(Eol + 1);
105103
if (!Line.empty())
106104
writeWithPrefix(Line);
107-
HasPendingNewline = true;
105+
// We printed a newline, record here to print a prefix.
106+
ShouldPrefixNextString = true;
108107
Str = Str.drop_front(Eol + 1);
109108
Eol = Str.find('\n');
110109
}
@@ -113,24 +112,21 @@ class LLVM_ABI raw_ldbg_ostream final : public raw_ostream {
113112
}
114113
void emitPrefix() { Os.write(Prefix.c_str(), Prefix.size()); }
115114
void writeWithPrefix(StringRef Str) {
116-
flushEol();
115+
if (ShouldPrefixNextString) {
116+
emitPrefix();
117+
ShouldPrefixNextString = false;
118+
}
117119
Os.write(Str.data(), Str.size());
118120
}
119121

120122
public:
121123
explicit raw_ldbg_ostream(std::string Prefix, raw_ostream &Os,
122-
bool HasPendingNewline = true)
124+
bool ShouldPrefixNextString = true)
123125
: Prefix(std::move(Prefix)), Os(Os),
124-
HasPendingNewline(HasPendingNewline) {
126+
ShouldPrefixNextString(ShouldPrefixNextString) {
125127
SetUnbuffered();
126128
}
127-
~raw_ldbg_ostream() final { flushEol(); }
128-
void flushEol() {
129-
if (HasPendingNewline) {
130-
emitPrefix();
131-
HasPendingNewline = false;
132-
}
133-
}
129+
~raw_ldbg_ostream() final {}
134130

135131
/// Forward the current_pos method to the underlying stream.
136132
uint64_t current_pos() const final { return Os.tell(); }
@@ -149,6 +145,7 @@ class RAIINewLineStream final : public raw_ostream {
149145
~RAIINewLineStream() { Os << '\n'; }
150146
void write_impl(const char *Ptr, size_t Size) final { Os.write(Ptr, Size); }
151147
uint64_t current_pos() const final { return Os.tell(); }
148+
RAIINewLineStream &asLvalue() { return *this; }
152149
};
153150

154151
/// Remove the path prefix from the file name.

llvm/unittests/Support/DebugLogTest.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,18 @@ TEST(DebugLogTest, StreamPrefix) {
115115
ldbg_osA << "5";
116116
EXPECT_EQ(os.str(), expected);
117117
}
118-
// After destructors, there was a pending newline for stream B.
119-
EXPECT_EQ(os.str(), expected + "PrefixB ");
118+
EXPECT_EQ(os.str(), expected);
119+
}
120+
121+
TEST(DebugLogTest, DestructorPrefix) {
122+
llvm::DebugFlag = true;
123+
std::string str;
124+
raw_string_ostream os(str);
125+
{
126+
llvm::impl::raw_ldbg_ostream ldbg_osB("PrefixB ", os);
127+
}
128+
// After destructors, nothing should have been printed.
129+
EXPECT_EQ(os.str(), "");
120130
}
121131
#else
122132
TEST(DebugLogTest, Basic) {

mlir/lib/Transforms/Utils/DialectConversion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,8 +1138,8 @@ struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
11381138
SmallPtrSet<Operation *, 1> pendingRootUpdates;
11391139

11401140
/// A raw output stream used to prefix the debug log.
1141-
llvm::impl::raw_ldbg_ostream os{(Twine("[") + DEBUG_TYPE + "] ").str(),
1142-
llvm::dbgs(), /*HasPendingNewline=*/false};
1141+
llvm::impl::raw_ldbg_ostream os{(Twine("[") + DEBUG_TYPE + ":1] ").str(),
1142+
llvm::dbgs()};
11431143

11441144
/// A logger used to emit diagnostics during the conversion process.
11451145
llvm::ScopedPrinter logger{os};

mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "mlir/Config/mlir-config.h"
1616
#include "mlir/IR/Action.h"
1717
#include "mlir/IR/Matchers.h"
18+
#include "mlir/IR/Operation.h"
19+
#include "mlir/IR/OperationSupport.h"
1820
#include "mlir/IR/Verifier.h"
1921
#include "mlir/Interfaces/SideEffectInterfaces.h"
2022
#include "mlir/Rewrite/PatternApplicator.h"
@@ -23,7 +25,7 @@
2325
#include "llvm/ADT/BitVector.h"
2426
#include "llvm/ADT/DenseMap.h"
2527
#include "llvm/ADT/ScopeExit.h"
26-
#include "llvm/Support/Debug.h"
28+
#include "llvm/Support/DebugLog.h"
2729
#include "llvm/Support/ScopedPrinter.h"
2830
#include "llvm/Support/raw_ostream.h"
2931

@@ -178,9 +180,8 @@ static Operation *getDumpRootOp(Operation *op) {
178180
return op;
179181
}
180182
static void logSuccessfulFolding(Operation *op) {
181-
llvm::dbgs() << "// *** IR Dump After Successful Folding ***\n";
182-
op->dump();
183-
llvm::dbgs() << "\n\n";
183+
LDBG() << "// *** IR Dump After Successful Folding ***\n"
184+
<< OpWithFlags(op, OpPrintingFlags().elideLargeElementsAttrs());
184185
}
185186
#endif // NDEBUG
186187

@@ -394,8 +395,12 @@ class GreedyPatternRewriteDriver : public RewriterBase::Listener {
394395
function_ref<void(Diagnostic &)> reasonCallback) override;
395396

396397
#ifndef NDEBUG
398+
/// A raw output stream used to prefix the debug log.
399+
400+
llvm::impl::raw_ldbg_ostream os{(Twine("[") + DEBUG_TYPE + ":1] ").str(),
401+
llvm::dbgs()};
397402
/// A logger used to emit information during the application process.
398-
llvm::ScopedPrinter logger{llvm::dbgs()};
403+
llvm::ScopedPrinter logger{os};
399404
#endif
400405

401406
/// The low-level pattern applicator.
@@ -928,10 +933,9 @@ mlir::applyPatternsGreedily(Region &region,
928933
RegionPatternRewriteDriver driver(region.getContext(), patterns, config,
929934
region);
930935
LogicalResult converged = std::move(driver).simplify(changed);
931-
LLVM_DEBUG(if (failed(converged)) {
932-
llvm::dbgs() << "The pattern rewrite did not converge after scanning "
933-
<< config.getMaxIterations() << " times\n";
934-
});
936+
if (failed(converged))
937+
LDBG() << "The pattern rewrite did not converge after scanning "
938+
<< config.getMaxIterations() << " times";
935939
return converged;
936940
}
937941

@@ -1063,9 +1067,8 @@ LogicalResult mlir::applyOpPatternsGreedily(
10631067
LogicalResult converged = std::move(driver).simplify(ops, changed);
10641068
if (allErased)
10651069
*allErased = surviving.empty();
1066-
LLVM_DEBUG(if (failed(converged)) {
1067-
llvm::dbgs() << "The pattern rewrite did not converge after "
1068-
<< config.getMaxNumRewrites() << " rewrites";
1069-
});
1070+
if (failed(converged))
1071+
LDBG() << "The pattern rewrite did not converge after "
1072+
<< config.getMaxNumRewrites() << " rewrites";
10701073
return converged;
10711074
}

0 commit comments

Comments
 (0)