Skip to content

Commit 9973733

Browse files
committed
[Clang] __has_builtin should return false for aux triple builtins
Signed-off-by: Sarnie, Nick <[email protected]>
1 parent f3bc8c3 commit 9973733

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

clang/include/clang/Basic/Builtins.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ struct Info {
7474
const char *Features;
7575
HeaderDesc Header;
7676
LanguageID Langs;
77+
bool operator==(const Info &Other) const;
7778
};
7879

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

272+
// Return true if the AuxBuiltin ID represents a target-specific builtin that
273+
// is always unsupported on the default target.
274+
bool isAuxBuiltinIDAlwaysUnsupportedOnDefaultTarget(unsigned ID) const;
275+
271276
/// Returns true if this is a libc/libm function without the '__builtin_'
272277
/// prefix.
273278
static bool isBuiltinFunc(llvm::StringRef Name);

clang/lib/Basic/Builtins.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ static constexpr Builtin::Info BuiltinInfo[] = {
4141
#include "clang/Basic/Builtins.inc"
4242
};
4343

44+
bool Builtin::Info::operator==(const Builtin::Info &Other) const {
45+
auto StrCompare = [](StringRef A, StringRef B) { return A == B; };
46+
return Name == Other.Name && StrCompare(Type, Other.Type) &&
47+
StrCompare(Attributes, Other.Attributes) &&
48+
StrCompare(Features, Other.Features) && Header.ID == Other.Header.ID &&
49+
Langs == Other.Langs;
50+
}
51+
4452
const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const {
4553
if (ID < Builtin::FirstTSBuiltin)
4654
return BuiltinInfo[ID];
@@ -183,6 +191,17 @@ unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const {
183191
return Width;
184192
}
185193

194+
bool Builtin::Context::isAuxBuiltinIDAlwaysUnsupportedOnDefaultTarget(
195+
unsigned ID) const {
196+
assert(isAuxTargetBuiltinID(ID) && "Expected aux target builtin ID");
197+
const auto &Record = getRecord(ID);
198+
for (const auto &MainTargetBuiltin : TSRecords)
199+
if (Record == MainTargetBuiltin)
200+
return false;
201+
202+
return true;
203+
}
204+
186205
bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
187206
bool &HasVAListArg, const char *Fmt) const {
188207
assert(Fmt && "Not passed a format string");

clang/lib/Lex/PPMacroExpansion.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1804,8 +1804,9 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
18041804
diag::err_feature_check_malformed);
18051805
if (!II)
18061806
return false;
1807-
else if (II->getBuiltinID() != 0) {
1808-
switch (II->getBuiltinID()) {
1807+
auto BuiltinID = II->getBuiltinID();
1808+
if (BuiltinID != 0) {
1809+
switch (BuiltinID) {
18091810
case Builtin::BI__builtin_cpu_is:
18101811
return getTargetInfo().supportsCpuIs();
18111812
case Builtin::BI__builtin_cpu_init:
@@ -1818,8 +1819,21 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
18181819
// usual allocation and deallocation functions. Required by libc++
18191820
return 201802;
18201821
default:
1822+
// We may get here because of aux builtins which may not be
1823+
// supported on the default target, for example if we have an X86
1824+
// specific builtin and the current target is SPIR-V. Sometimes we
1825+
// rely on __has_builtin returning true when passed a builtin that
1826+
// is not supported on the default target due to LangOpts but is
1827+
// supported on the aux target. See
1828+
// test/Headers/__cpuidex_conflict.c for an example. If the builtin
1829+
// is an aux builtin and it can never be supported on the default
1830+
// target, __has_builtin should return false.
1831+
if (getBuiltinInfo().isAuxBuiltinID(BuiltinID) &&
1832+
getBuiltinInfo().isAuxBuiltinIDAlwaysUnsupportedOnDefaultTarget(
1833+
BuiltinID))
1834+
return false;
18211835
return Builtin::evaluateRequiredTargetFeatures(
1822-
getBuiltinInfo().getRequiredFeatures(II->getBuiltinID()),
1836+
getBuiltinInfo().getRequiredFeatures(BuiltinID),
18231837
getTargetInfo().getTargetOpts().FeatureMap);
18241838
}
18251839
return true;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// REQUIRES: spirv-registered-target
2+
// REQUIRES: x86-registered-target
3+
4+
// RUN: %clang_cc1 -fopenmp -triple=spirv64 -fopenmp-is-target-device \
5+
// RUN: -aux-triple x86_64-linux-unknown -E %s | FileCheck -implicit-check-not=BAD %s
6+
7+
// CHECK: GOOD
8+
#if __has_builtin(__builtin_ia32_pause)
9+
BAD
10+
#else
11+
GOOD
12+
#endif

0 commit comments

Comments
 (0)