[clang] [Preprocessor] Fix __has_builtin for CPU ID functions (PR #80058)

Nemanja Ivanovic via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 30 12:45:32 PST 2024


https://github.com/nemanjai created https://github.com/llvm/llvm-project/pull/80058

My recent commit (67c1c1d) made the CPU ID builtins target-independent so they can be used on PPC as well. However, that had the unintended consequence of changing the behaviour of __has_builtin in that it reports these as supported at the pre-processor level. This makes it impossible to guard the use of these with this feature test macro which is clearly not ideal.
This patch restores the behaviour of __has_builtin for __builtin_cpu_is, __builtin_cpu_init,
__builtin_cpu_supports. Now the preprocessor queries the target to determine whether the target supports the builtin.

>From 85b246ea3882999c9b397e1e30c7656448e63bbf Mon Sep 17 00:00:00 2001
From: Nemanja Ivanovic <nemanja at synopsys.com>
Date: Tue, 30 Jan 2024 21:39:24 +0100
Subject: [PATCH] [Preprocessor] Fix __has_builtin for CPU ID functions

My recent commit (67c1c1d) made the CPU ID builtins
target-independent so they can be used on PPC as well.
However, that had the unintended consequence of changing
the behaviour of __has_builtin in that it reports these
as supported at the pre-processor level. This makes it
impossible to guard the use of these with this feature
test macro which is clearly not ideal.
This patch restores the behaviour of __has_builtin
for __builtin_cpu_is, __builtin_cpu_init,
__builtin_cpu_supports. Now the preprocessor queries
the target to determine whether the target supports
the builtin.
---
 clang/lib/Lex/PPMacroExpansion.cpp          |  6 ++++++
 clang/test/Preprocessor/has_builtin_cpuid.c | 20 ++++++++++++++++++++
 2 files changed, 26 insertions(+)
 create mode 100644 clang/test/Preprocessor/has_builtin_cpuid.c

diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp
index ad02f31209b0b..3017461dc66e8 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -1672,6 +1672,12 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
           return false;
         else if (II->getBuiltinID() != 0) {
           switch (II->getBuiltinID()) {
+          case Builtin::BI__builtin_cpu_is:
+            return getTargetInfo().supportsCpuIs();
+          case Builtin::BI__builtin_cpu_init:
+            return getTargetInfo().supportsCpuInit();
+          case Builtin::BI__builtin_cpu_supports:
+            return getTargetInfo().supportsCpuSupports();
           case Builtin::BI__builtin_operator_new:
           case Builtin::BI__builtin_operator_delete:
             // denotes date of behavior change to support calling arbitrary
diff --git a/clang/test/Preprocessor/has_builtin_cpuid.c b/clang/test/Preprocessor/has_builtin_cpuid.c
new file mode 100644
index 0000000000000..8de6331e62d6e
--- /dev/null
+++ b/clang/test/Preprocessor/has_builtin_cpuid.c
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -fsyntax-only -triple arm64-- -DARM -verify %s
+// RUN: %clang_cc1 -fsyntax-only -triple x86_64-- -DX86 -verify %s
+// RUN: %clang_cc1 -fsyntax-only -triple powerpc64-unknown-linux-gnu -DPPC \
+// RUN:   -verify %s
+// expected-no-diagnostics
+#if __has_builtin(__builtin_cpu_is)
+# ifdef ARM
+#   error "ARM shouldn't have __builtin_cpu_is"
+# endif
+#endif
+#if __has_builtin(__builtin_cpu_init)
+# if defined(ARM) || defined(PPC)
+#   error "ARM/PPC shouldn't have __builtin_cpu_init"
+# endif
+#endif
+#if __has_builtin(__builtin_cpu_supports)
+# ifdef ARM
+#   error "ARM shouldn't have __builtin_cpu_supports"
+# endif
+#endif



More information about the cfe-commits mailing list