[Clang] __has_builtin should return false for aux triple builtins by sarnex · Pull Request #121839 · llvm/llvm-project (original) (raw)

@llvm/pr-subscribers-clang

Author: Nick Sarnie (sarnex)

Changes

Currently, __has_builtin will return true when passed a builtin that is only supported on the aux target. I found this when __has_builtin was called with an X86 builtin but the current target was SPIR-V.

If we know for sure the builtin can't be supported on the current target, the function should return false.

We can't simply check if it's an aux builtin, see the test mentioned in the comment.


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

4 Files Affected:

diff --git a/clang/include/clang/Basic/Builtins.h b/clang/include/clang/Basic/Builtins.h index 63559d977ce6b6..0939f95b0922c1 100644 --- a/clang/include/clang/Basic/Builtins.h +++ b/clang/include/clang/Basic/Builtins.h @@ -74,6 +74,7 @@ struct Info { const char *Features; HeaderDesc Header; LanguageID Langs;

/// Holds information about both target-independent and @@ -268,6 +269,10 @@ class Context { /// for AuxTarget). unsigned getAuxBuiltinID(unsigned ID) const { return ID - TSRecords.size(); }

+bool Builtin::Info::operator==(const Builtin::Info &Other) const {

+} + const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const { if (ID < Builtin::FirstTSBuiltin) return BuiltinInfo[ID]; @@ -183,6 +191,17 @@ unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const { return Width; }

+bool Builtin::Context::isAuxBuiltinIDAlwaysUnsupportedOnDefaultTarget(

@@ -1818,8 +1819,21 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { // usual allocation and deallocation functions. Required by libc++ return 201802; default:

diff --git a/clang/test/Preprocessor/builtin_aux_info.cpp b/clang/test/Preprocessor/builtin_aux_info.cpp new file mode 100644 index 00000000000000..041c7edfdcadac --- /dev/null +++ b/clang/test/Preprocessor/builtin_aux_info.cpp @@ -0,0 +1,12 @@ +// REQUIRES: spirv-registered-target +// REQUIRES: x86-registered-target + +// RUN: %clang_cc1 -fopenmp -triple=spirv64 -fopenmp-is-target-device
+// RUN: -aux-triple x86_64-linux-unknown -E %s | FileCheck -implicit-check-not=BAD %s + +// CHECK: GOOD +#if __has_builtin(__builtin_ia32_pause)