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

via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 30 12:46:02 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Nemanja Ivanovic (nemanjai)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/80058.diff


2 Files Affected:

- (modified) clang/lib/Lex/PPMacroExpansion.cpp (+6) 
- (added) clang/test/Preprocessor/has_builtin_cpuid.c (+20) 


``````````diff
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

``````````

</details>


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


More information about the cfe-commits mailing list