[Clang] Add __has_target_builtin macro by sarnex · Pull Request #126324 · llvm/llvm-project (original) (raw)

@llvm/pr-subscribers-clang

Author: Nick Sarnie (sarnex)

Changes

As a follow-up to #121839, where we wanted to make __has_builtin return false for aux builtins, but that broke existing code.

Instead, introduce a new macro __has_target_builtin (name open to suggestions) that only considers builtins for the current target.


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

4 Files Affected:

diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst index 973cf8f9d091c30..057ad564f970bb4 100644 --- a/clang/docs/LanguageExtensions.rst +++ b/clang/docs/LanguageExtensions.rst @@ -67,6 +67,10 @@ It can be used like this: __has_builtin should not be used to detect support for a builtin macro; use #ifdef instead. + When using device offloading, a builtin is considered available if it is + available on either the host or the device targets. + Use __has_target_builtin to consider only the current target. + __has_constexpr_builtin

@@ -96,6 +100,35 @@ 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++ +

diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h index 2bf4d1a16699430..240fe28aba93e33 100644 --- a/clang/include/clang/Lex/Preprocessor.h +++ b/clang/include/clang/Lex/Preprocessor.h @@ -174,6 +174,7 @@ class Preprocessor { IdentifierInfo *Ident__has_extension; // __has_extension IdentifierInfo *Ident__has_builtin; // __has_builtin IdentifierInfo *Ident__has_constexpr_builtin; // __has_constexpr_builtin

@@ -1886,8 +1894,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {

     return false;
   });

@@ -1917,8 +1924,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { getLangOpts()) : 0; });