Skip to content

Conversation

@adurang
Copy link
Contributor

@adurang adurang commented Dec 16, 2025

This PR adds a new set of debug macros that allow a certain code to be only executed when certain debug conditions are met. This is useful to guard things that are not strictly messages but compute and store things that are related to those messages.

Strictly speaking the existing ODBG_OS could be used as well but that requires a stream object to be created which is unnecessary in some cases.

Example of how it works:

ODBG_IF("Counters", [&](uint32_t Level) {
    someCounter++;
    if (Level == 2) moreDetailedCounter += f();
});
ODBG("Counters") << "Counter" =  someCounter 
                 << ODBG_IF(2) << "DetailedCounter" << moreDetailedCounter;

@llvmbot
Copy link
Member

llvmbot commented Dec 16, 2025

@llvm/pr-subscribers-offload

Author: Alex Duran (adurang)

Changes

This PR adds a new set of debug macros that allow a certain code to be only executed when certain debug conditions are met. This is useful to guard things that are not strictly messages but compute and store things that are related to those messages.

Strictly speaking the existing ODBG_OS could be used as well but that requires a stream object to be created which is unnecessary in some cases.

Example of how it works:

ODBG_IF("Counters", [&](uint32_t Level) {
someCounter++;
if (Level == 2) moreDetailedCounter += f();
});
ODBG("Counters") << "Counter" = someCounter
<< ODBG_IF(2) << "DetailedCounter" << moreDetailedCounter;


Full diff: https://github.com/llvm/llvm-project/pull/172573.diff

1 Files Affected:

  • (modified) offload/include/Shared/Debug.h (+46-9)
diff --git a/offload/include/Shared/Debug.h b/offload/include/Shared/Debug.h
index d5cf719f1ebf3..1b04c252f775f 100644
--- a/offload/include/Shared/Debug.h
+++ b/offload/include/Shared/Debug.h
@@ -432,15 +432,22 @@ static inline raw_ostream &operator<<(raw_ostream &Os,
 
 // helper templates to support lambdas with different number of arguments
 template <typename LambdaTy> struct LambdaHelper {
-  template <typename T, typename = std::void_t<>>
-  struct has_two_args : std::false_type {};
-  template <typename T>
-  struct has_two_args<T,
-                      std::void_t<decltype(std::declval<T>().operator()(1, 2))>>
-      : std::true_type {};
+  template <typename FuncTy, typename RetTy, typename... Args>
+  static constexpr size_t CountArgs(RetTy (FuncTy::*)(Args...)) {
+    return sizeof...(Args);
+  }
+  template <typename FuncTy, typename RetTy, typename... Args>
+  static constexpr size_t CountArgs(RetTy (FuncTy::*)(Args...) const) {
+    return sizeof...(Args);
+  }
 
+  static constexpr size_t NArgs = CountArgs(&LambdaTy::operator());
+};
+
+template <typename LambdaTy>
+struct LambdaOs : public LambdaHelper<LambdaTy> {
   static void dispatch(LambdaTy func, llvm::raw_ostream &Os, uint32_t Level) {
-    if constexpr (has_two_args<LambdaTy>::value)
+    if constexpr (LambdaHelper<LambdaTy>::NArgs == 2)
       func(Os, Level);
     else
       func(Os);
@@ -457,8 +464,8 @@ template <typename LambdaTy> struct LambdaHelper {
           RealLevel, /*ShouldPrefixNextString=*/true,                          \
           /*ShouldEmitNewLineOnDestruction=*/true};                            \
       auto F = Callback;                                                       \
-      ::llvm::offload::debug::LambdaHelper<decltype(F)>::dispatch(F, OS,       \
-                                                                  RealLevel);  \
+      ::llvm::offload::debug::LambdaOs<decltype(F)>::dispatch(F, OS,           \
+                                                              RealLevel);      \
     }                                                                          \
   }
 
@@ -476,6 +483,33 @@ template <typename LambdaTy> struct LambdaHelper {
 #define ODBG_OS(...)                                                           \
   ODBG_OS_SELECT(__VA_ARGS__ __VA_OPT__(, ) 3, 2, 1)(__VA_ARGS__)
 
+// helper templates to support lambdas with different number of arguments
+template <typename LambdaTy> struct LambdaIf : public LambdaHelper<LambdaTy> {
+  static void dispatch(LambdaTy func, uint32_t Level) {
+    if constexpr (LambdaHelper<LambdaTy>::NArgs == 1)
+      func(Level);
+    else
+      func();
+  }
+};
+
+#define ODBG_IF_BASE(Type, Level, Callback)                                    \
+  if (::llvm::offload::debug::isDebugEnabled()) {                              \
+    uint32_t RealLevel = (Level);                                              \
+    if (::llvm::offload::debug::shouldPrintDebug(GETNAME(TARGET_NAME), (Type), \
+                                                 RealLevel)) {                 \
+      auto F = Callback;                                                       \
+      ::llvm::offload::debug::LambdaIf<decltype(F)>::dispatch(F, RealLevel);   \
+    }                                                                          \
+  }
+
+#define ODBG_IF_3(Type, Level, Callback) ODBG_IF_BASE(Type, Level, Callback)
+#define ODBG_IF_2(Type, Callback) ODBG_IF_3(Type, 1, Callback)
+#define ODBG_IF_1(Callback) ODBG_IF_2("default", Callback)
+#define ODBG_IF_SELECT(Type, Level, Callback, NArgs, ...) ODBG_IF_##NArgs
+#define ODBG_IF(...)                                                           \
+  ODBG_IF_SELECT(__VA_ARGS__ __VA_OPT__(, ) 3, 2, 1)(__VA_ARGS__)
+
 #else
 
 inline bool isDebugEnabled() { return false; }
@@ -496,6 +530,9 @@ inline bool isDebugEnabled() { return false; }
 #define ODBG_OS_STREAM(Stream, Type, Level, Callback)
 #define ODBG_OS(...)
 
+#define ODBG_IF_BASE(Type, Level, Callback)
+#define ODBG_IF(...)
+
 #endif
 
 } // namespace llvm::offload::debug

@github-actions
Copy link

github-actions bot commented Dec 16, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@abhinavgaba abhinavgaba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fixes, Alex!

static void dispatch(LambdaTy func, llvm::raw_ostream &Os, uint32_t Level) {
if constexpr (has_two_args<LambdaTy>::value)
if constexpr (LambdaHelper<LambdaTy>::NArgs == 2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and line 488 are the only new changes vs #172107, and they fix the build failure I saw locally.

@adurang adurang merged commit d502ff0 into llvm:main Dec 17, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants