[clang] [Clang] Add __has_target_builtin macro (PR #126324)

Joseph Huber via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 10 12:04:10 PST 2025


================
@@ -96,6 +101,37 @@ the ``<cmath>`` header file to conditionally make a function constexpr whenever
 the constant evaluation of the corresponding builtin (for example,
 ``std::fmax`` calls ``__builtin_fmax``) is supported in Clang.
 
+``__has_target_builtin``
+------------------------
+
+This function-like macro takes a single identifier argument that is the name of
+a builtin function, a builtin pseudo-function (taking one or more type
+arguments), or a builtin template.
+It evaluates to 1 if the builtin is supported on the current target or 0 if not.
+The behavior is different than ``__has_builtin`` when there is an auxiliary target,
+such when offloading to a target device.
+It can be used like this:
+
+.. code-block:: c++
+
+  #ifndef __has_target_builtin         // Optional of course.
+    #define __has_target_builtin(x) 0  // Compatibility with non-clang compilers.
+  #endif
+
+  ...
+  #if __has_target_builtin(__builtin_trap)
+    __builtin_trap();
+  #else
+    abort();
+  #endif
+  ...
+
+.. note::
+  ``__has_target_builtin`` should not be used to detect support for a builtin macro;
+  use ``#ifdef`` instead.
+
+  ``__has_target_built`` is only defined for offloading targets.
----------------
jhuber6 wrote:

```suggestion
  ``__has_target_builtin`` is only defined for offloading targets.
```

https://github.com/llvm/llvm-project/pull/126324


More information about the cfe-commits mailing list