[clang] [Clang] Add __builtin_is_debugging_enabled (PR #248440)

Alexander Hück via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 3 05:47:08 PST 2026


https://github.com/ahueck created https://github.com/llvm/llvm-project/pull/248440

Add `__builtin_is_debugging_enabled()` as a target dependent Clang builtin. Like `__builtin_cpu_supports`, it uses a `TargetInfo` hook so  `__has_builtin` and Sema share the same capability check. 
A follow-up enables it on AMDGPU and lowers calls to `llvm.is.debugging.enabled`.

AI assisted development

>From 322f12b4cb9274cffb0b25e8bcf8181e780b322c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alexander=20H=C3=BCck?= <alexander.huck at amd.com>
Date: Tue, 3 Nov 2026 10:17:08 +0000
Subject: [PATCH] [Clang] Add __builtin_is_debugging_enabled

---
 clang/docs/LanguageExtensions.md              | 34 +++++++++++++++++++
 clang/docs/ReleaseNotes.md                    |  3 ++
 clang/include/clang/Basic/Builtins.td         |  6 ++++
 clang/include/clang/Basic/TargetInfo.h        |  3 ++
 clang/lib/Lex/PPMacroExpansion.cpp            |  2 ++
 clang/lib/Sema/SemaChecking.cpp               |  5 +++
 .../has_builtin_is_debugging_enabled.c        | 13 +++++++
 .../test/Sema/builtin-is-debugging-enabled.c  |  6 ++++
 8 files changed, 72 insertions(+)
 create mode 100644 clang/test/Preprocessor/has_builtin_is_debugging_enabled.c
 create mode 100644 clang/test/Sema/builtin-is-debugging-enabled.c

diff --git a/clang/docs/LanguageExtensions.md b/clang/docs/LanguageExtensions.md
index 59c4a9a66ce07f..b3b0569f2ccf7d 100644
--- a/clang/docs/LanguageExtensions.md
+++ b/clang/docs/LanguageExtensions.md
@@ -3525,6 +3525,40 @@ counter's true frequency will need to be provided by the user.
 
 Query for this feature with `__has_builtin(__builtin_readsteadycounter)`.
 
+### `__builtin_is_debugging_enabled`
+
+`__builtin_is_debugging_enabled` reports whether debugging is enabled for the
+current execution context, as defined by the target.
+
+**Syntax**:
+
+```c++
+bool __builtin_is_debugging_enabled();
+```
+
+**Example of Use**:
+
+```c++
+bool debugging() {
+#if __has_builtin(__builtin_is_debugging_enabled)
+  return __builtin_is_debugging_enabled();
+#else
+  return library_provided_fallback();
+#endif
+}
+```
+
+**Description**:
+
+The builtin makes a distinct observation at run time on each call, even if its
+result is unused. Sequenced calls to this builtin retain their relative order. See
+[`llvm.is.debugging.enabled`](https://llvm.org/docs/LangRef.html#llvm-is-debugging-enabled).
+The query runs without calling the operating system or a runtime library.
+
+Check whether the builtin is available for the current compilation target with
+`__has_builtin(__builtin_is_debugging_enabled)`. Calling it on an unsupported
+target is a compile-time error.
+
 ### `__builtin_cpu_supports`
 
 **Syntax**:
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index e87f00c687ab6b..67e76a9847215c 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -403,6 +403,9 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
 - Clang now allows GNU computed `goto` extension in `constexpr` functions, matching the relaxed
   `constexpr` function body rules introduced in C++23.
 
+- Added `__builtin_is_debugging_enabled`, a runtime query of whether debugging
+  is enabled for the current execution context.
+
 - Added support for the `__builtin_strlcat` and `__builtin_strlcpy` builtins.
 
 - Added `__builtin_sort_pack` to sort a pack of types using the same
diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td
index b4713317a80be7..873f724f48e204 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -1485,6 +1485,12 @@ def ReadSteadyCounter : Builtin {
   let Prototype = "unsigned long long int()";
 }
 
+def IsDebuggingEnabled : Builtin {
+  let Spellings = ["__builtin_is_debugging_enabled"];
+  let Attributes = [NoThrow];
+  let Prototype = "bool()";
+}
+
 def Trap : Builtin {
   let Spellings = ["__builtin_trap"];
   let Attributes = [NoThrow, NoReturn];
diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index 7c5c35a5cfb7c5..72a02879e76e6b 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -1627,6 +1627,9 @@ class TargetInfo : public TransferrableTargetInfo,
   virtual bool supportsCpuIs() const { return false; }
   virtual bool supportsCpuInit() const { return false; }
 
+  // Identify whether this target supports __builtin_is_debugging_enabled.
+  virtual bool supportsIsDebuggingEnabled() const { return false; }
+
   // Validate the contents of the __builtin_cpu_supports(const char*)
   // argument.
   virtual bool validateCpuSupports(StringRef Name) const { return false; }
diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp
index 6048937677c0db..1c1995fe8db469 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -1780,6 +1780,8 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
               return getTargetInfo().supportsCpuInit();
             case Builtin::BI__builtin_cpu_supports:
               return getTargetInfo().supportsCpuSupports();
+            case Builtin::BI__builtin_is_debugging_enabled:
+              return getTargetInfo().supportsIsDebuggingEnabled();
             case Builtin::BI__builtin_operator_new:
             case Builtin::BI__builtin_operator_delete:
               // denotes date of behavior change to support calling arbitrary
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index c91ed4508b1fdb..cbcdaea80b9569 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -3305,6 +3305,11 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
       return ExprError();
     }
     break;
+  case Builtin::BI__builtin_is_debugging_enabled:
+    if (!Context.getTargetInfo().supportsIsDebuggingEnabled())
+      targetDiag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported)
+          << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc());
+    break;
   case Builtin::BI__builtin___CFStringMakeConstantString:
     // CFStringMakeConstantString is currently not implemented for GOFF (i.e.,
     // on z/OS) and for XCOFF (i.e., on AIX). Emit unsupported
diff --git a/clang/test/Preprocessor/has_builtin_is_debugging_enabled.c b/clang/test/Preprocessor/has_builtin_is_debugging_enabled.c
new file mode 100644
index 00000000000000..0d0330f499ff56
--- /dev/null
+++ b/clang/test/Preprocessor/has_builtin_is_debugging_enabled.c
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -E -triple x86_64-unknown-linux-gnu %s -o - \
+// RUN:   | FileCheck %s --check-prefix=NOBUILTIN
+// RUN: %clang_cc1 -E -x hip -triple x86_64-unknown-linux-gnu \
+// RUN:   -aux-triple amdgpu12.00-amd-amdhsa %s -o - \
+// RUN:   | FileCheck %s --check-prefix=NOBUILTIN
+
+#if __has_builtin(__builtin_is_debugging_enabled)
+// BUILTIN: has_builtin_yes
+has_builtin_yes
+#else
+// NOBUILTIN: has_builtin_no
+has_builtin_no
+#endif
diff --git a/clang/test/Sema/builtin-is-debugging-enabled.c b/clang/test/Sema/builtin-is-debugging-enabled.c
new file mode 100644
index 00000000000000..38b8ad0284158a
--- /dev/null
+++ b/clang/test/Sema/builtin-is-debugging-enabled.c
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fsyntax-only -verify=unsupported %s
+// RUN: %clang_cc1 -triple amdgpu12.00-amd-amdhsa -fsyntax-only -verify=unsupported %s
+
+_Bool f(void) {
+  return __builtin_is_debugging_enabled(); // unsupported-error {{builtin is not supported on this target}}
+}



More information about the cfe-commits mailing list