Skip to content

Commit 1c28b92

Browse files
authored
[Clang] __has_builtin should return false for aux triple builtins (llvm#121839)
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. We should instead return false for aux builtins. --------- Signed-off-by: Sarnie, Nick <[email protected]>
1 parent 0d01908 commit 1c28b92

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

clang/lib/Lex/PPMacroExpansion.cpp

Lines changed: 7 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,11 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
18181819
// usual allocation and deallocation functions. Required by libc++
18191820
return 201802;
18201821
default:
1822+
// __has_builtin should return false for aux builtins.
1823+
if (getBuiltinInfo().isAuxBuiltinID(BuiltinID))
1824+
return false;
18211825
return Builtin::evaluateRequiredTargetFeatures(
1822-
getBuiltinInfo().getRequiredFeatures(II->getBuiltinID()),
1826+
getBuiltinInfo().getRequiredFeatures(BuiltinID),
18231827
getTargetInfo().getTargetOpts().FeatureMap);
18241828
}
18251829
return true;

clang/test/Headers/__cpuidex_conflict.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
// RUN: %clang_cc1 %s -ffreestanding -fms-extensions -fms-compatibility \
44
// RUN: -fms-compatibility-version=19.00 -triple x86_64-pc-windows-msvc -emit-llvm -o -
55
// %clang_cc1 %s -ffreestanding -triple x86_64-w64-windows-gnu -fms-extensions -emit-llvm -o -
6-
// RUN: %clang_cc1 %s -ffreestanding -fopenmp -fopenmp-is-target-device -aux-triple x86_64-unknown-linux-gnu
6+
//
7+
// FIXME: See https://github.com/llvm/llvm-project/pull/121839
8+
// RUN: not %clang_cc1 %s -ffreestanding -fopenmp -fopenmp-is-target-device -aux-triple x86_64-unknown-linux-gnu
79

810
typedef __SIZE_TYPE__ size_t;
911

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %clang_cc1 -fopenmp -triple=spirv64 -fopenmp-is-target-device \
2+
// RUN: -aux-triple x86_64-linux-unknown -E %s | FileCheck -implicit-check-not=BAD %s
3+
4+
// RUN: %clang_cc1 -fopenmp -triple=nvptx64 -fopenmp-is-target-device \
5+
// RUN: -aux-triple x86_64-linux-unknown -E %s | FileCheck -implicit-check-not=BAD %s
6+
7+
// RUN: %clang_cc1 -fopenmp -triple=amdgcn-amd-amdhsa -fopenmp-is-target-device \
8+
// RUN: -aux-triple x86_64-linux-unknown -E %s | FileCheck -implicit-check-not=BAD %s
9+
10+
// RUN: %clang_cc1 -fopenmp -triple=aarch64 -fopenmp-is-target-device \
11+
// RUN: -aux-triple x86_64-linux-unknown -E %s | FileCheck -implicit-check-not=BAD %s
12+
13+
// CHECK: GOOD
14+
#if __has_builtin(__builtin_ia32_pause)
15+
BAD
16+
#else
17+
GOOD
18+
#endif

0 commit comments

Comments
 (0)