[clang] [Clang] Add __has_target_builtin macro (PR #126324)
Yaxun Liu via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 11 10:11:19 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
----------------
yxsamliu wrote:
if we make it available to C++, we'd better document the following invalid usage which originally leads to this extension:
```
#if !__has_target_builtin(__wfi)
static __inline__ void __attribute__((__always_inline__, __nodebug__)) __wfi(void) {
__builtin_arm_wfi();
}
#endif
```
we should emphasize that a C++ header may be used by offloading languages, and in offloading language, the same source is compiled for host and device target separately. A builtin not available for the current target does not justify defining the builtin for both host and device targets. In this case, better to use `__has_builtin(__wfi)` since it makes sure the condition is true for both hosts and device targets so that the code won't break when used in offloading languages.
https://github.com/llvm/llvm-project/pull/126324
More information about the cfe-commits
mailing list