(original) (raw)

diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst index 973cf8f9d091c..be767af694519 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,55 @@ the ```` 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. + +For heterogeneous compilations that see source code intended for more than one target: + +``__has_builtin`` returns true if the builtin is known to the compiler +(i.e. it's available via one of the targets), but makes no promises whether it's available on the current target. +The compiler can parse it, but not necessarily generate code for it. + +``__has_target_builtin`` returns true if the builtin can actually be generated for the current target. + +As a motivating example, let's try to guard a builtin call using ``__has_builtin`` in a heterogeneous compilation, +such as OpenMP Offloading, with the host being x86-64 and the offloading device being AMDGPU. + +.. code-block:: c++ + + #if __has_builtin(__builtin_ia32_pause) + __builtin_ia32_pause(); + #else + abort(); + #endif + +Compilation of this code results in a compiler error because ``__builtin_ia32_pause`` is known to the compiler because +it is a builtin supported by the host x86-64 compilation so ``__has_builtin`` returns true. However, code cannot +be generated for ``__builtin_ia32_pause`` during the offload AMDGPU compilation as it is not supported on that target. + +To guard uses of builtins in heterogeneous compilations such as this, +``__has_target_builtin`` can be used as per the below example to verify code can be generated +for the referenced builtin on the current target being compiled. + +.. code-block:: c++ + + #if __has_target_builtin(__builtin_ia32_pause) + __builtin_ia32_pause(); + #else + abort(); + #endif + +.. note:: + ``__has_target_builtin`` should not be used to detect support for a builtin macro; + use ``#ifdef`` instead. + .. _langext-__has_feature-__has_extension: ``__has_feature`` and ``__has_extension`` diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 50d3bbbc97e91..d588c3727997e 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -104,6 +104,12 @@ Non-comprehensive list of changes in this release New Compiler Flags ------------------ +New Compiler Builtins +--------------------- + +- The new ``__has_target_builtin`` macro can be used to check if a builtin is available + on the current target. + Deprecated Compiler Flags ------------------------- diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h index 2bf4d1a166994..240fe28aba93e 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 + IdentifierInfo *Ident__has_target_builtin; // __has_target_builtin IdentifierInfo *Ident__has_attribute; // __has_attribute IdentifierInfo *Ident__has_embed; // __has_embed IdentifierInfo *Ident__has_include; // __has_include diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp index 944966a791add..7c8cd7b3354de 100644 --- a/clang/lib/Lex/PPMacroExpansion.cpp +++ b/clang/lib/Lex/PPMacroExpansion.cpp @@ -357,6 +357,7 @@ void Preprocessor::RegisterBuiltinMacros() { Ident__has_builtin = RegisterBuiltinMacro("__has_builtin"); Ident__has_constexpr_builtin = RegisterBuiltinMacro("__has_constexpr_builtin"); + Ident__has_target_builtin = RegisterBuiltinMacro("__has_target_builtin"); Ident__has_attribute = RegisterBuiltinMacro("__has_attribute"); if (!getLangOpts().CPlusPlus) Ident__has_c_attribute = RegisterBuiltinMacro("__has_c_attribute"); @@ -1797,16 +1798,18 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { diag::err_feature_check_malformed); return II && HasExtension(*this, II->getName()); }); - } else if (II == Ident__has_builtin) { + } else if (II == Ident__has_builtin || II == Ident__has_target_builtin) { + bool IsHasTargetBuiltin = II == Ident__has_target_builtin; EvaluateFeatureLikeBuiltinMacro( OS, Tok, II, *this, false, - [this](Token &Tok, bool &HasLexedNextToken) -> int { + [this, IsHasTargetBuiltin](Token &Tok, bool &HasLexedNextToken) -> int { IdentifierInfo *II = ExpectFeatureIdentifierInfo( Tok, *this, diag::err_feature_check_malformed); if (!II) return false; - else if (II->getBuiltinID() != 0) { - switch (II->getBuiltinID()) { + unsigned BuiltinID = II->getBuiltinID(); + if (BuiltinID != 0) { + switch (BuiltinID) { case Builtin::BI__builtin_cpu_is: return getTargetInfo().supportsCpuIs(); case Builtin::BI__builtin_cpu_init: @@ -1819,8 +1822,14 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { // usual allocation and deallocation functions. Required by libc++ return 201802; default: + // __has_target_builtin should return false for aux builtins. + // isAuxBuiltinID only returns true if the builtin is + // supported by the aux target alone. + if (IsHasTargetBuiltin && + getBuiltinInfo().isAuxBuiltinID(BuiltinID)) + return false; return Builtin::evaluateRequiredTargetFeatures( - getBuiltinInfo().getRequiredFeatures(II->getBuiltinID()), + getBuiltinInfo().getRequiredFeatures(BuiltinID), getTargetInfo().getTargetOpts().FeatureMap); } return true; diff --git a/clang/test/Preprocessor/has_target_builtin.cpp b/clang/test/Preprocessor/has_target_builtin.cpp new file mode 100644 index 0000000000000..f11f7ae0e28c0 --- /dev/null +++ b/clang/test/Preprocessor/has_target_builtin.cpp @@ -0,0 +1,24 @@ +// RUN: %clang_cc1 -fopenmp -triple=spirv64 -fopenmp-is-target-device \ +// RUN: -aux-triple x86_64-linux-unknown -E %s | FileCheck -implicit-check-not=HAS %s + +// RUN: %clang_cc1 -fopenmp -triple=nvptx64 -fopenmp-is-target-device \ +// RUN: -aux-triple x86_64-linux-unknown -E %s | FileCheck -implicit-check-not=HAS %s + +// RUN: %clang_cc1 -fopenmp -triple=amdgcn-amd-amdhsa -fopenmp-is-target-device \ +// RUN: -aux-triple x86_64-linux-unknown -E %s | FileCheck -implicit-check-not=HAS %s + +// RUN: %clang_cc1 -fopenmp -triple=aarch64 -fopenmp-is-target-device \ +// RUN: -aux-triple x86_64-linux-unknown -E %s | FileCheck -implicit-check-not=HAS %s + +// RUN: %clang_cc1 -triple=aarch64 -E %s | FileCheck -implicit-check-not=HAS %s + +// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -E %s | \ +// RUN: FileCheck -check-prefix=CHECK-NO-OFFLOAD-HAS-BUILTIN -implicit-check-not=DOESNT %s + +// CHECK: DOESNT +// CHECK-NO-OFFLOAD-HAS-BUILTIN: HAS +#if __has_target_builtin(__builtin_ia32_pause) + HAS +#else + DOESNT +#endif