Skip to content

Commit b2c37c6

Browse files
committed
[MLIR] Add debug log to the pass manager (NFC)
1 parent 91dd13b commit b2c37c6

File tree

2 files changed

+302
-68
lines changed

2 files changed

+302
-68
lines changed

llvm/include/llvm/Support/DebugLog.h

Lines changed: 92 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,41 @@
1919
namespace llvm {
2020
#ifndef NDEBUG
2121

22-
// LDBG() is a macro that can be used as a raw_ostream for debugging.
23-
// It will stream the output to the dbgs() stream, with a prefix of the
24-
// debug type and the file and line number. A trailing newline is added to the
25-
// output automatically. If the streamed content contains a newline, the prefix
26-
// is added to each beginning of a new line. Nothing is printed if the debug
27-
// output is not enabled or the debug type does not match.
28-
//
29-
// E.g.,
30-
// LDBG() << "Bitset contains: " << Bitset;
31-
// is somehow equivalent to
32-
// LLVM_DEBUG(dbgs() << "[" << DEBUG_TYPE << "] " << __FILE__ << ":" <<
33-
// __LINE__ << " "
34-
// << "Bitset contains: " << Bitset << "\n");
35-
//
22+
/// LDBG() is a macro that can be used as a raw_ostream for debugging.
23+
/// It will stream the output to the dbgs() stream, with a prefix of the
24+
/// debug type and the file and line number. A trailing newline is added to the
25+
/// output automatically. If the streamed content contains a newline, the prefix
26+
/// is added to each beginning of a new line. Nothing is printed if the debug
27+
/// output is not enabled or the debug type does not match.
28+
///
29+
/// E.g.,
30+
/// LDBG() << "Bitset contains: " << Bitset;
31+
/// is somehow equivalent to
32+
/// LLVM_DEBUG(dbgs() << "[" << DEBUG_TYPE << "] " << __FILE__ << ":" <<
33+
/// __LINE__ << " "
34+
/// << "Bitset contains: " << Bitset << "\n");
35+
///
3636
// An optional `level` argument can be provided to control the verbosity of the
37-
// output. The default level is 1, and is in increasing level of verbosity.
38-
//
39-
// The `level` argument can be a literal integer, or a macro that evaluates to
40-
// an integer.
41-
//
42-
// An optional `type` argument can be provided to control the debug type. The
43-
// default type is DEBUG_TYPE. The `type` argument can be a literal string, or a
44-
// macro that evaluates to a string.
37+
/// output. The default level is 1, and is in increasing level of verbosity.
38+
///
39+
/// The `level` argument can be a literal integer, or a macro that evaluates to
40+
/// an integer.
41+
///
42+
/// An optional `type` argument can be provided to control the debug type. The
43+
/// default type is DEBUG_TYPE. The `type` argument can be a literal string, or
44+
/// a macro that evaluates to a string.
4545
#define LDBG(...) _GET_LDBG_MACRO(__VA_ARGS__)(__VA_ARGS__)
4646

47+
/// LDBG_OS() is a macro that can be used as a raw_ostream for debugging.
48+
/// It will stream the output to the dbgs() stream, with a prefix of the
49+
/// debug type and the file and line number. A trailing newline is added to the
50+
/// output automatically. If the streamed content contains a newline, the prefix
51+
/// is added to each beginning of a new line. Nothing is printed if the debug
52+
/// output is not enabled or the debug type does not match.
53+
/// Note: this macro isn't guarded by a debug flag so it will always print in
54+
/// assert builds.
55+
#define LDBG_OS(...) _GET_LDBG_OS_MACRO(__VA_ARGS__)(__VA_ARGS__)
56+
4757
// Helper macros to choose the correct macro based on the number of arguments.
4858
#define LDBG_FUNC_CHOOSER(_f1, _f2, _f3, ...) _f3
4959
#define LDBG_FUNC_RECOMPOSER(argsWithParentheses) \
@@ -55,17 +65,6 @@ namespace llvm {
5565
#define _GET_LDBG_MACRO(...) \
5666
LDBG_CHOOSE_FROM_ARG_COUNT(LDBG_NO_ARG_EXPANDER __VA_ARGS__())
5767

58-
// Dispatch macros to support the `level` argument or none (default to 1)
59-
#define LDBG_LOG_LEVEL(LEVEL) \
60-
DEBUGLOG_WITH_STREAM_AND_TYPE(llvm::dbgs(), LEVEL, DEBUG_TYPE)
61-
#define LDBG_LOG_LEVEL_1() LDBG_LOG_LEVEL(1)
62-
// This macro is a helper when LDBG() is called with 2 arguments.
63-
// In this case we want to allow the order of the arguments to be swapped.
64-
// We rely on the fact that the `level` argument is an integer, and the `type`
65-
// is a string and dispatch to a C++ API that is overloaded.
66-
#define LDBG_LOG_LEVEL_WITH_TYPE(LEVEL_OR_TYPE, TYPE_OR_LEVEL) \
67-
DEBUGLOG_WITH_STREAM_AND_TYPE(llvm::dbgs(), (LEVEL_OR_TYPE), (TYPE_OR_LEVEL))
68-
6968
// We want the filename without the full path. We are using the __FILE__ macro
7069
// and a constexpr function to strip the path prefix. We can avoid the frontend
7170
// repeated evaluation of __FILE__ by using the __FILE_NAME__ when defined
@@ -82,15 +81,63 @@ namespace llvm {
8281
(::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE, LEVEL)); \
8382
_c; _c = false) \
8483
for (::llvm::impl::raw_ldbg_ostream LdbgOS{ \
85-
::llvm::impl::computePrefix(TYPE, FILE, LINE, LEVEL), (STREAM)}; \
84+
::llvm::impl::computePrefix(TYPE, FILE, LINE, LEVEL), (STREAM), \
85+
/*ShouldPrefixNextString=*/true, \
86+
/*ShouldEmitNewLineOnDestruction=*/true}; \
8687
_c; _c = false) \
87-
::llvm::impl::RAIINewLineStream{LdbgOS}.asLvalue()
88+
LdbgOS
8889

8990
#define DEBUGLOG_WITH_STREAM_TYPE_AND_FILE(STREAM, LEVEL, TYPE, FILE) \
9091
DEBUGLOG_WITH_STREAM_TYPE_FILE_AND_LINE(STREAM, LEVEL, TYPE, FILE, __LINE__)
9192
#define DEBUGLOG_WITH_STREAM_AND_TYPE(STREAM, LEVEL, TYPE) \
9293
DEBUGLOG_WITH_STREAM_TYPE_AND_FILE(STREAM, LEVEL, TYPE, __LLVM_FILE_NAME__)
9394

95+
// Dispatch macros to support the `level` argument or none (default to 1)
96+
#define LDBG_LOG_LEVEL(LEVEL) \
97+
DEBUGLOG_WITH_STREAM_AND_TYPE(llvm::dbgs(), LEVEL, DEBUG_TYPE)
98+
#define LDBG_LOG_LEVEL_1() LDBG_LOG_LEVEL(1)
99+
// This macro is a helper when LDBG() is called with 2 arguments.
100+
// In this case we want to allow the order of the arguments to be swapped.
101+
// We rely on the fact that the `level` argument is an integer, and the `type`
102+
// is a string and dispatch to a C++ API that is overloaded.
103+
#define LDBG_LOG_LEVEL_WITH_TYPE(LEVEL_OR_TYPE, TYPE_OR_LEVEL) \
104+
DEBUGLOG_WITH_STREAM_AND_TYPE(llvm::dbgs(), (LEVEL_OR_TYPE), (TYPE_OR_LEVEL))
105+
106+
// Helper macros to choose the correct macro based on the number of arguments.
107+
// This is the same logic as for LDBG(), but for LDBG_OS().
108+
#define LDBG_OS_FUNC_CHOOSER(_f1, _f2, _f3, ...) _f3
109+
#define LDBG_OS_FUNC_RECOMPOSER(argsWithParentheses) \
110+
LDBG_OS_FUNC_CHOOSER argsWithParentheses
111+
#define LDBG_OS_CHOOSE_FROM_ARG_COUNT(...) \
112+
LDBG_OS_FUNC_RECOMPOSER( \
113+
(__VA_ARGS__, LDBG_OS_LOG_LEVEL_WITH_TYPE, LDBG_OS_LOG_LEVEL, ))
114+
#define LDBG_OS_NO_ARG_EXPANDER() , , LDBG_OS_LOG_LEVEL_1
115+
#define _GET_LDBG_OS_MACRO(...) \
116+
LDBG_OS_CHOOSE_FROM_ARG_COUNT(LDBG_OS_NO_ARG_EXPANDER __VA_ARGS__())
117+
118+
#define LDBG_OS_LOG_LEVEL(LEVEL) \
119+
LDBG_OS_WITH_STREAM_AND_TYPE(llvm::dbgs(), LEVEL, DEBUG_TYPE)
120+
#define LDBG_OS_LOG_LEVEL_1() LDBG_OS_LOG_LEVEL(1)
121+
// This macro is a helper when LDBG_OS() is called with 2 arguments.
122+
// In this case we want to allow the order of the arguments to be swapped.
123+
// We rely on the fact that the `level` argument is an integer, and the `type`
124+
// is a string and dispatch to a C++ API that is overloaded.
125+
#define LDBG_OS_LOG_LEVEL_WITH_TYPE(LEVEL_OR_TYPE, TYPE_OR_LEVEL) \
126+
LDBG_OS_WITH_STREAM_AND_TYPE(llvm::dbgs(), LEVEL_OR_TYPE, TYPE_OR_LEVEL)
127+
128+
#define LDBG_OS_WITH_STREAM_TYPE_FILE_AND_LINE(STREAM, LEVEL, TYPE, FILE, \
129+
LINE) \
130+
::llvm::impl::raw_ldbg_ostream { \
131+
::llvm::impl::computePrefix(TYPE, FILE, LINE, LEVEL), (STREAM), \
132+
/*ShouldPrefixNextString=*/true, \
133+
/*ShouldEmitNewLineOnDestruction=*/true \
134+
}
135+
136+
#define LDBG_OS_WITH_STREAM_TYPE_AND_FILE(STREAM, LEVEL, TYPE, FILE) \
137+
LDBG_OS_WITH_STREAM_TYPE_FILE_AND_LINE(STREAM, LEVEL, TYPE, FILE, __LINE__)
138+
#define LDBG_OS_WITH_STREAM_AND_TYPE(STREAM, LEVEL, TYPE) \
139+
LDBG_OS_WITH_STREAM_TYPE_AND_FILE(STREAM, LEVEL, TYPE, __LLVM_FILE_NAME__)
140+
94141
namespace impl {
95142

96143
/// A raw_ostream that tracks `\n` and print the prefix after each
@@ -99,6 +146,7 @@ class LLVM_ABI raw_ldbg_ostream final : public raw_ostream {
99146
std::string Prefix;
100147
raw_ostream &Os;
101148
bool ShouldPrefixNextString;
149+
bool ShouldEmitNewLineOnDestruction;
102150

103151
/// Split the line on newlines and insert the prefix before each
104152
/// newline. Forward everything to the underlying stream.
@@ -131,12 +179,17 @@ class LLVM_ABI raw_ldbg_ostream final : public raw_ostream {
131179

132180
public:
133181
explicit raw_ldbg_ostream(std::string Prefix, raw_ostream &Os,
134-
bool ShouldPrefixNextString = true)
182+
bool ShouldPrefixNextString = true,
183+
bool ShouldEmitNewLineOnDestruction = false)
135184
: Prefix(std::move(Prefix)), Os(Os),
136-
ShouldPrefixNextString(ShouldPrefixNextString) {
185+
ShouldPrefixNextString(ShouldPrefixNextString),
186+
ShouldEmitNewLineOnDestruction(ShouldEmitNewLineOnDestruction) {
137187
SetUnbuffered();
138188
}
139-
~raw_ldbg_ostream() final {}
189+
~raw_ldbg_ostream() final {
190+
if (ShouldEmitNewLineOnDestruction)
191+
Os << '\n';
192+
}
140193

141194
/// Forward the current_pos method to the underlying stream.
142195
uint64_t current_pos() const final { return Os.tell(); }
@@ -194,6 +247,7 @@ computePrefix(int Level, const char *File, int Line, const char *DebugType) {
194247
#define LDBG(...) \
195248
for (bool _c = false; _c; _c = false) \
196249
::llvm::nulls()
250+
#define LDBG_OS(...)
197251
#endif
198252
} // end namespace llvm
199253

0 commit comments

Comments
 (0)