diff --git a/llvm/include/llvm/Support/DebugLog.h b/llvm/include/llvm/Support/DebugLog.h index 8fca2d5e2816b..a3312950da94e 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; + 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. @@ -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(); @@ -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) { 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(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..f23c6197accd5 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 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