-
Notifications
You must be signed in to change notification settings - Fork 15k
[llvm][support] Add LDBG macro. #143704
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
[llvm][support] Add LDBG macro. #143704
Conversation
|
@llvm/pr-subscribers-llvm-support Author: Jacques Pienaar (jpienaar) ChangesAdd macro that mirror a common usage of logging to output (e.g., one I invariably end up creating locally often). This makes it easy to have streaming log like behavior while still using the base debug logging. I also wanted to avoid inventing a full logging library here while enabling others to change the sink without too much pain, so put it in its own header (this also avoids making Debug depend on raw_ostream beyond forward reference). The should allow a consistent dev experience without fixing the sink too much. Full diff: https://github.com/llvm/llvm-project/pull/143704.diff 2 Files Affected:
diff --git a/llvm/include/llvm/Support/DebugLog.h b/llvm/include/llvm/Support/DebugLog.h
new file mode 100644
index 0000000000000..d83c951bd23e2
--- /dev/null
+++ b/llvm/include/llvm/Support/DebugLog.h
@@ -0,0 +1,69 @@
+//===- llvm/Support/DebugLog.h - Logging like debug output ------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+// This file contains macros for logging like debug output. It builds upon the
+// support in Debug.h but provides a utility function for common debug output
+// style.
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_SUPPORT_DEBUGLOG_H
+#define LLVM_SUPPORT_DEBUGLOG_H
+
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/raw_ostream.h"
+
+namespace llvm {
+#ifndef NDEBUG
+
+// Output with given inputs and trailing newline. E.g.,
+// LLVM_DLOG() << "Bitset contains: " << Bitset;
+// is equivalent to
+// LLVM_DEBUG(dbgs() << DEBUG_TYPE << " " << __FILE__ << ":" << __LINE__
+// << "] " << "Bitset contains: " << Bitset << "\n");
+#define LLVM_DLOG(...) \
+ DEBUGLOG_WITH_STREAM_AND_TYPE(llvm::dbgs(), DEBUG_TYPE, __VA_ARGS__)
+
+#define DEBUGLOG_WITH_STREAM_AND_TYPE(STREAM, TYPE, ...) \
+ for (bool _c = (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE)); _c; \
+ _c = false) \
+ ::llvm::impl::LogWithNewline(TYPE, __FILE__, __LINE__, (STREAM))
+
+namespace impl {
+class LogWithNewline {
+public:
+ LogWithNewline(const char *debug_type, const char *file, int line,
+ raw_ostream &os)
+ : os(os) {
+ if (debug_type)
+ os << debug_type << " ";
+ os << file << ":" << line << "] ";
+ }
+ ~LogWithNewline() { os << '\n'; }
+ template <typename T> raw_ostream &operator<<(const T &t) && {
+ return os << t;
+ }
+
+ // Prevent copying, as this class manages newline responsibility and is
+ // intended for use as a temporary.
+ LogWithNewline(const LogWithNewline &) = delete;
+ LogWithNewline &operator=(const LogWithNewline &) = delete;
+ LogWithNewline &operator=(LogWithNewline &&) = delete;
+
+private:
+ raw_ostream &os;
+};
+} // end namespace impl
+#else
+// As others in Debug, When compiling without assertions, the -debug-* options
+// and all inputs too LLVM_DLOG() are ignored.
+#define LLVM_DLOG(...) \
+ for (bool _c = false; _c; _c = false) \
+ ::llvm::nulls()
+#endif
+} // end namespace llvm
+
+#endif // LLVM_SUPPORT_DEBUGLOG_H
diff --git a/llvm/unittests/Support/DebugLogTest.cpp b/llvm/unittests/Support/DebugLogTest.cpp
new file mode 100644
index 0000000000000..84208f5dc7e40
--- /dev/null
+++ b/llvm/unittests/Support/DebugLogTest.cpp
@@ -0,0 +1,39 @@
+//===- llvm/unittest/Support/DebugLogTest.cpp ------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/DebugLog.h"
+#include "llvm/Support/raw_ostream.h"
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+#include <string>
+using namespace llvm;
+using testing::HasSubstr;
+
+#ifndef NDEBUG
+TEST(DebugLogTest, Basic) {
+ std::string s1, s2;
+ raw_string_ostream os1(s1), os2(s2);
+ static const char *DT[] = {"A", "B"};
+
+ llvm::DebugFlag = true;
+ setCurrentDebugTypes(DT, 2);
+ DEBUGLOG_WITH_STREAM_AND_TYPE(os1, "A") << "A";
+ DEBUGLOG_WITH_STREAM_AND_TYPE(os1, "B") << "B";
+ EXPECT_THAT(os1.str(), AllOf(HasSubstr("A\n"), HasSubstr("B\n")));
+
+ setCurrentDebugType("A");
+ volatile int x = 0;
+ if (x == 0)
+ DEBUGLOG_WITH_STREAM_AND_TYPE(os2, "A") << "A";
+ else
+ DEBUGLOG_WITH_STREAM_AND_TYPE(os2, "A") << "B";
+ DEBUGLOG_WITH_STREAM_AND_TYPE(os2, "B") << "B";
+ EXPECT_THAT(os2.str(), AllOf(HasSubstr("A\n"), Not(HasSubstr("B\n"))));
+}
+#endif
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
Add macro that mirror a common usage of logging to output (e.g., one I invariably end up creating locally often). This makes it easy to have streaming log like behavior while still using the base debug logging. I also wanted to avoid inventing a full logging library here while enabling others to change the sink without too much pain, so put it in its own header (this also avoids making Debug depend on raw_ostream beyond forward reference). The should allow a consistent dev experience without fixing the sink too much.
|
This looks really nice! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, I think it can really help improving our debug outputs!
| #ifndef LLVM_SUPPORT_DEBUGLOG_H | ||
| #define LLVM_SUPPORT_DEBUGLOG_H | ||
|
|
||
| #include "llvm/Support/Debug.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you considered making this part of the Debug.h header directly?
Co-authored-by: Mehdi Amini <[email protected]>
… upstream uses. And test without debug type given.
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/2/builds/29743 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/123/builds/24051 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/63/builds/8163 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/202/builds/2474 Here is the relevant piece of the build log for the reference |
|
Working on fix |
| // is equivalent to | ||
| // LLVM_DEBUG(dbgs() << DEBUG_TYPE << " [" << __FILE__ << ":" << __LINE__ | ||
| // << "] " << "Bitset contains: " << Bitset << "\n"); | ||
| #define LDBG() DEBUGLOG_WITH_STREAM_AND_TYPE(llvm::dbgs(), DEBUG_TYPE) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we prefix this with LLVM_? In the past, we used DEBUG instead of LLVM_DEBUG which caused naming collisions downstream: https://discourse.llvm.org/t/rfc-change-debug-macro-to-llvm-debug/48092 . I wonder if LDBG may be terse enough to run into similar issues.
|
@jpienaar this is great! Consider sending a PSA to discourse to advertise this. |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/127/builds/4191 Here is the relevant piece of the build log for the reference |
Add macro that mirror a common usage of logging to output .This makes it easy to have streaming log like behavior while still using the base debug logging. I also wanted to avoid inventing a full logging library here while enabling others to change the sink without too much pain, so put it in its own header (this also avoids making Debug depend on raw_ostream beyond forward reference). The should allow a consistent dev experience without fixing the sink too much. --------- Co-authored-by: Mehdi Amini <[email protected]>
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/49/builds/1938 Here is the relevant piece of the build log for the reference |
This change removes IREE’s local LDBG macro definition in favor of the LLVM-provided version introduced in llvm/llvm-project#143704. This update is also in preparation for llvm/llvm-project#165429, which would otherwise cause a macro redefinition error due to the existing local LDBG definition. Signed-off-by: Yu-Zhewen <[email protected]>
…22456) This change removes IREE’s local LDBG macro definition in favor of the LLVM-provided version introduced in llvm/llvm-project#143704. This update is also in preparation for llvm/llvm-project#165429, which would otherwise cause a macro redefinition error due to the existing local LDBG definition. Signed-off-by: Yu-Zhewen <[email protected]>
Add macro that mirror a common usage of logging to output (e.g., one I invariably end up creating locally often). This makes it easy to have streaming log like behavior while still using the base debug logging.
I also wanted to avoid inventing a full logging library here while enabling others to change the sink without too much pain, so put it in its own header (this also avoids making Debug depend on raw_ostream beyond forward reference). The should allow a consistent dev experience without fixing the sink too much.