https://github.com/boomanaiden154 created 
https://github.com/llvm/llvm-project/pull/154217

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.

>From eb63310aa9495583e8343ff5b17f8ca1ef48afd3 Mon Sep 17 00:00:00 2001
From: Aiden Grossman <aidengross...@google.com>
Date: Mon, 18 Aug 2025 22:48:26 +0000
Subject: [PATCH] [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.
---
 clang/lib/Basic/Builtins.cpp            | 3 ++-
 clang/lib/Headers/cpuid.h               | 5 -----
 clang/test/Headers/__cpuidex_conflict.c | 2 +-
 3 files changed, 3 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Basic/Builtins.cpp b/clang/lib/Basic/Builtins.cpp
index 885abdc152e3a..8e38ec98277e7 100644
--- a/clang/lib/Basic/Builtins.cpp
+++ b/clang/lib/Basic/Builtins.cpp
@@ -225,7 +225,8 @@ void Builtin::Context::initializeBuiltins(IdentifierTable 
&Table,
     // Step #3: Register target-specific builtins for AuxTarget.
     for (const auto &Shard : AuxTargetShards)
       for (const auto &I : Shard.Infos) {
-        Table.get(I.getName(Shard)).setBuiltinID(ID);
+        if (builtinIsSupported(*Shard.Strings, I, LangOpts))
+          Table.get(I.getName(Shard)).setBuiltinID(ID);
         ++ID;
       }
   }
diff --git a/clang/lib/Headers/cpuid.h b/clang/lib/Headers/cpuid.h
index ce8c79e77dc18..52addb7bfa856 100644
--- a/clang/lib/Headers/cpuid.h
+++ b/clang/lib/Headers/cpuid.h
@@ -345,15 +345,10 @@ static __inline int __get_cpuid_count (unsigned int 
__leaf,
 // In some configurations, __cpuidex is defined as a builtin (primarily
 // -fms-extensions) which will conflict with the __cpuidex definition below.
 #if !(__has_builtin(__cpuidex))
-// In some cases, offloading will set the host as the aux triple and define the
-// builtin. Given __has_builtin does not detect builtins on aux triples, we 
need
-// to explicitly check for some offloading cases.
-#ifndef __NVPTX__
 static __inline void __cpuidex(int __cpu_info[4], int __leaf, int __subleaf) {
   __cpuid_count(__leaf, __subleaf, __cpu_info[0], __cpu_info[1], __cpu_info[2],
                 __cpu_info[3]);
 }
 #endif
-#endif
 
 #endif /* __CPUID_H */
diff --git a/clang/test/Headers/__cpuidex_conflict.c 
b/clang/test/Headers/__cpuidex_conflict.c
index d14ef293e586d..e911036a1d953 100644
--- a/clang/test/Headers/__cpuidex_conflict.c
+++ b/clang/test/Headers/__cpuidex_conflict.c
@@ -5,7 +5,7 @@
 
 // Ensure that we do not run into conflicts when offloading.
 // RUN: %clang_cc1 %s -DIS_STATIC=static -ffreestanding -fopenmp 
-fopenmp-is-target-device -aux-triple x86_64-unknown-linux-gnu
-// 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 -
+// 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 -
 
 typedef __SIZE_TYPE__ size_t;
 

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to