Skip to content

Commit 0efcb83

Browse files
authored
[Clang] Reland '__has_builtin should return false for aux triple builtins' (#126324)
Reland #121839 based on the results of the Discourse discussion [here](https://discourse.llvm.org/t/rfc-has-builtin-behavior-on-offloading-targets/84964). --------- Signed-off-by: Sarnie, Nick <[email protected]>
1 parent 35693da commit 0efcb83

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ It can be used like this:
6868
``__has_builtin`` should not be used to detect support for a builtin macro;
6969
use ``#ifdef`` instead.
7070

71+
When compiling with target offloading, ``__has_builtin`` only considers the
72+
currently active target.
73+
7174
``__has_constexpr_builtin``
7275
---------------------------
7376

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Potentially Breaking Changes
3737
C/C++ Language Potentially Breaking Changes
3838
-------------------------------------------
3939

40+
- The ``__has_builtin`` function now only considers the currently active target when being used with target offloading.
41+
4042
C++ Specific Potentially Breaking Changes
4143
-----------------------------------------
4244
- For C++20 modules, the Reduced BMI mode will be the default option. This may introduce

clang/lib/Lex/PPMacroExpansion.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,7 +1760,8 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
17601760
Tok, *this, diag::err_feature_check_malformed);
17611761
if (!II)
17621762
return false;
1763-
else if (II->getBuiltinID() != 0) {
1763+
unsigned BuiltinID = II->getBuiltinID();
1764+
if (BuiltinID != 0) {
17641765
switch (II->getBuiltinID()) {
17651766
case Builtin::BI__builtin_cpu_is:
17661767
return getTargetInfo().supportsCpuIs();
@@ -1774,8 +1775,11 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
17741775
// usual allocation and deallocation functions. Required by libc++
17751776
return 201802;
17761777
default:
1778+
// __has_builtin should return false for aux builtins.
1779+
if (getBuiltinInfo().isAuxBuiltinID(BuiltinID))
1780+
return false;
17771781
return Builtin::evaluateRequiredTargetFeatures(
1778-
getBuiltinInfo().getRequiredFeatures(II->getBuiltinID()),
1782+
getBuiltinInfo().getRequiredFeatures(BuiltinID),
17791783
getTargetInfo().getTargetOpts().FeatureMap);
17801784
}
17811785
return true;

clang/test/Headers/__cpuidex_conflict.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
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 and
8+
// FIXME: https://github.com/llvm/llvm-project/pull/126324
9+
// RUN: not %clang_cc1 %s -ffreestanding -fopenmp -fopenmp-is-target-device -aux-triple x86_64-unknown-linux-gnu
710

811
typedef __SIZE_TYPE__ size_t;
912

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)