Skip to content

Commit eb63310

Browse files
[Clang] Only enable builtins on aux triple if supported by language
This patch makes it so that builtins for the aux triple only get enabled if they are marked as supported by the current language options. Otherwise we define them regardless of the language mode. This can cause issues in certain scenarios, like if someone has a definition that conflicts with a builtin gated behind __has_builtin, because __has_builtin will return false given the builtin is only enabled on the aux triple despite the builtin actually being enabled. This is best exemplified with the __cpuidex conflict test. Fixes #152558.
1 parent 4629291 commit eb63310

File tree

3 files changed

+3
-7
lines changed

3 files changed

+3
-7
lines changed

clang/lib/Basic/Builtins.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ void Builtin::Context::initializeBuiltins(IdentifierTable &Table,
225225
// Step #3: Register target-specific builtins for AuxTarget.
226226
for (const auto &Shard : AuxTargetShards)
227227
for (const auto &I : Shard.Infos) {
228-
Table.get(I.getName(Shard)).setBuiltinID(ID);
228+
if (builtinIsSupported(*Shard.Strings, I, LangOpts))
229+
Table.get(I.getName(Shard)).setBuiltinID(ID);
229230
++ID;
230231
}
231232
}

clang/lib/Headers/cpuid.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,10 @@ static __inline int __get_cpuid_count (unsigned int __leaf,
345345
// In some configurations, __cpuidex is defined as a builtin (primarily
346346
// -fms-extensions) which will conflict with the __cpuidex definition below.
347347
#if !(__has_builtin(__cpuidex))
348-
// In some cases, offloading will set the host as the aux triple and define the
349-
// builtin. Given __has_builtin does not detect builtins on aux triples, we need
350-
// to explicitly check for some offloading cases.
351-
#ifndef __NVPTX__
352348
static __inline void __cpuidex(int __cpu_info[4], int __leaf, int __subleaf) {
353349
__cpuid_count(__leaf, __subleaf, __cpu_info[0], __cpu_info[1], __cpu_info[2],
354350
__cpu_info[3]);
355351
}
356352
#endif
357-
#endif
358353

359354
#endif /* __CPUID_H */

clang/test/Headers/__cpuidex_conflict.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
// Ensure that we do not run into conflicts when offloading.
77
// RUN: %clang_cc1 %s -DIS_STATIC=static -ffreestanding -fopenmp -fopenmp-is-target-device -aux-triple x86_64-unknown-linux-gnu
8-
// RUN: %clang_cc1 -DIS_STATIC="" -triple nvptx64-nvidia-cuda -aux-triple x86_64-unknown-linux-gnu -aux-target-cpu x86-64 -fcuda-is-device -internal-isystem /home/gha/llvm-project/build/lib/clang/22/include -x cuda %s -o -
8+
// RUN: %clang_cc1 -DIS_STATIC=static -triple nvptx64-nvidia-cuda -aux-triple x86_64-unknown-linux-gnu -aux-target-cpu x86-64 -fcuda-is-device -x cuda %s -o -
99

1010
typedef __SIZE_TYPE__ size_t;
1111

0 commit comments

Comments
 (0)