[clang] [Clang] Reland '__has_builtin should return false for aux triple builtins' (PR #126324)
Nick Sarnie via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 28 07:40:13 PDT 2025
https://github.com/sarnex updated https://github.com/llvm/llvm-project/pull/126324
>From 894307419c0e1f001f4369a1e4fc2a11cf2e00ef Mon Sep 17 00:00:00 2001
From: "Sarnie, Nick" <nick.sarnie at intel.com>
Date: Mon, 28 Jul 2025 07:37:01 -0700
Subject: [PATCH 1/2] [clang] Reland '__has_builtin should return false for aux
triple builtins'
Signed-off-by: Sarnie, Nick <nick.sarnie at intel.com>
---
clang/lib/Lex/PPMacroExpansion.cpp | 8 ++++++--
clang/test/Headers/__cpuidex_conflict.c | 5 ++++-
clang/test/Preprocessor/builtin_aux_info.cpp | 18 ++++++++++++++++++
3 files changed, 28 insertions(+), 3 deletions(-)
create mode 100644 clang/test/Preprocessor/builtin_aux_info.cpp
diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp
index 890567cfd3246..63c48e665ae32 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -1760,7 +1760,8 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
Tok, *this, diag::err_feature_check_malformed);
if (!II)
return false;
- else if (II->getBuiltinID() != 0) {
+ auto BuiltinID = II->getBuiltinID();
+ if (BuiltinID != 0) {
switch (II->getBuiltinID()) {
case Builtin::BI__builtin_cpu_is:
return getTargetInfo().supportsCpuIs();
@@ -1774,8 +1775,11 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
// usual allocation and deallocation functions. Required by libc++
return 201802;
default:
+ // __has_builtin should return false for aux builtins.
+ if (getBuiltinInfo().isAuxBuiltinID(BuiltinID))
+ return false;
return Builtin::evaluateRequiredTargetFeatures(
- getBuiltinInfo().getRequiredFeatures(II->getBuiltinID()),
+ getBuiltinInfo().getRequiredFeatures(BuiltinID),
getTargetInfo().getTargetOpts().FeatureMap);
}
return true;
diff --git a/clang/test/Headers/__cpuidex_conflict.c b/clang/test/Headers/__cpuidex_conflict.c
index 8687a6aa2f897..49795c447f6e0 100644
--- a/clang/test/Headers/__cpuidex_conflict.c
+++ b/clang/test/Headers/__cpuidex_conflict.c
@@ -3,7 +3,10 @@
// RUN: %clang_cc1 %s -ffreestanding -fms-extensions -fms-compatibility \
// RUN: -fms-compatibility-version=19.00 -triple x86_64-pc-windows-msvc -emit-llvm -o -
// %clang_cc1 %s -ffreestanding -triple x86_64-w64-windows-gnu -fms-extensions -emit-llvm -o -
-// RUN: %clang_cc1 %s -ffreestanding -fopenmp -fopenmp-is-target-device -aux-triple x86_64-unknown-linux-gnu
+//
+// FIXME: See https://github.com/llvm/llvm-project/pull/121839 and
+// FIXME: https://github.com/llvm/llvm-project/pull/126324
+// RUN: not %clang_cc1 %s -ffreestanding -fopenmp -fopenmp-is-target-device -aux-triple x86_64-unknown-linux-gnu
typedef __SIZE_TYPE__ size_t;
diff --git a/clang/test/Preprocessor/builtin_aux_info.cpp b/clang/test/Preprocessor/builtin_aux_info.cpp
new file mode 100644
index 0000000000000..60c8c6c492479
--- /dev/null
+++ b/clang/test/Preprocessor/builtin_aux_info.cpp
@@ -0,0 +1,18 @@
+// 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
+
+// RUN: %clang_cc1 -fopenmp -triple=nvptx64 -fopenmp-is-target-device \
+// RUN: -aux-triple x86_64-linux-unknown -E %s | FileCheck -implicit-check-not=BAD %s
+
+// RUN: %clang_cc1 -fopenmp -triple=amdgcn-amd-amdhsa -fopenmp-is-target-device \
+// RUN: -aux-triple x86_64-linux-unknown -E %s | FileCheck -implicit-check-not=BAD %s
+
+// RUN: %clang_cc1 -fopenmp -triple=aarch64 -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)
+ BAD
+#else
+ GOOD
+#endif
>From b1d1b20fe534993b361363a7faff4d1dfdc07dfb Mon Sep 17 00:00:00 2001
From: "Sarnie, Nick" <nick.sarnie at intel.com>
Date: Mon, 28 Jul 2025 07:39:51 -0700
Subject: [PATCH 2/2] missed feedback
Signed-off-by: Sarnie, Nick <nick.sarnie at intel.com>
---
clang/lib/Lex/PPMacroExpansion.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp
index 63c48e665ae32..6f12ac80d677e 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -1760,7 +1760,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
Tok, *this, diag::err_feature_check_malformed);
if (!II)
return false;
- auto BuiltinID = II->getBuiltinID();
+ unsigned BuiltinID = II->getBuiltinID();
if (BuiltinID != 0) {
switch (II->getBuiltinID()) {
case Builtin::BI__builtin_cpu_is:
More information about the cfe-commits
mailing list