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

Nick Sarnie via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 12 07:49:45 PST 2025


================
@@ -96,6 +100,47 @@ 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.
+
+``__has_builtin`` and ``__has_target_builtin`` behave identically for normal C++ compilations.
----------------
sarnex wrote:

yeah that's a fair point, so it sounds like you'd prefer an example of when you codegen doesn't work even though you are checking with `has_builtin`, and for that i could show the motiving example for this which is something like

```
void foo() {
#if __has_builtin(__builtin_ia32_pause)
__builtin_ia32_pause()
#else
abort()
#endif
}
```
and if the current target is an offloading target (amdgpu/nvptx/spirv) and the aux target is x86, we will get a error saying it can't be codegen'd

would that kind of example address your concern? thx


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


More information about the cfe-commits mailing list