[libc-commits] [libc] [libc] fix aarch64 GCC build (PR #97932)

Schrodinger ZHU Yifan via libc-commits libc-commits at lists.llvm.org
Sat Jul 6 19:04:01 PDT 2024


https://github.com/SchrodingerZhu created https://github.com/llvm/llvm-project/pull/97932

None

>From 8c20e25022472a7d2f769ec465bd17fd0f2c0aba Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <i at zhuyi.fan>
Date: Sat, 6 Jul 2024 19:00:37 -0700
Subject: [PATCH] [libc] fix aarch64 GCC build

---
 libc/hdr/math_macros.h                       |  3 +++
 libc/src/__support/File/linux/CMakeLists.txt |  2 ++
 libc/src/__support/threads/sleep.h           |  4 +++-
 libc/src/math/generic/cos.cpp                |  2 +-
 libc/src/math/generic/sin.cpp                |  2 +-
 libc/src/math/generic/sincos.cpp             |  2 +-
 libc/src/search/hsearch.cpp                  |  2 +-
 libc/startup/linux/aarch64/tls.cpp           | 13 ++++++++++++-
 8 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/libc/hdr/math_macros.h b/libc/hdr/math_macros.h
index d13c5ff7647ad..ae42dbe2bc4e5 100644
--- a/libc/hdr/math_macros.h
+++ b/libc/hdr/math_macros.h
@@ -15,6 +15,9 @@
 
 #else // Overlay mode
 
+// GCC will include CXX headers when __cplusplus is defined. This behavior
+// can be suppressed by defining _GLIBCXX_INCLUDE_NEXT_C_HEADERS.
+#define _GLIBCXX_INCLUDE_NEXT_C_HEADERS
 #include <math.h>
 
 // Some older math.h header does not have FP_INT_* constants yet.
diff --git a/libc/src/__support/File/linux/CMakeLists.txt b/libc/src/__support/File/linux/CMakeLists.txt
index ccf27f73d6aa8..8436a687116bd 100644
--- a/libc/src/__support/File/linux/CMakeLists.txt
+++ b/libc/src/__support/File/linux/CMakeLists.txt
@@ -1,3 +1,4 @@
+# TODO: migrate to proxy headers
 add_object_library(
   file
   SRCS
@@ -8,6 +9,7 @@ add_object_library(
     libc.include.fcntl
     libc.include.stdio
     libc.include.sys_syscall
+    libc.include.sys_stat
     libc.src.__support.CPP.new
     libc.src.__support.OSUtil.osutil
     libc.src.errno.errno
diff --git a/libc/src/__support/threads/sleep.h b/libc/src/__support/threads/sleep.h
index 9a2dff598ece8..6433bc3badd50 100644
--- a/libc/src/__support/threads/sleep.h
+++ b/libc/src/__support/threads/sleep.h
@@ -22,8 +22,10 @@ LIBC_INLINE void sleep_briefly() {
   __builtin_amdgcn_s_sleep(2);
 #elif defined(LIBC_TARGET_ARCH_IS_X86)
   __builtin_ia32_pause();
-#elif defined(LIBC_TARGET_ARCH_IS_AARCH64)
+#elif defined(LIBC_TARGET_ARCH_IS_AARCH64) && __has_builtin(__builtin_arm_isb)
   __builtin_arm_isb(0xf);
+#elif defined(LIBC_TARGET_ARCH_IS_AARCH64)
+  asm volatile("isb\n" ::: "memory");
 #else
   // Simply do nothing if sleeping isn't supported on this platform.
 #endif
diff --git a/libc/src/math/generic/cos.cpp b/libc/src/math/generic/cos.cpp
index a2cfe758fe4cc..0eb6d9d6a6de8 100644
--- a/libc/src/math/generic/cos.cpp
+++ b/libc/src/math/generic/cos.cpp
@@ -61,7 +61,7 @@ LLVM_LIBC_FUNCTION(double, cos, (double x)) {
 
   DoubleDouble y;
   unsigned k;
-  generic::LargeRangeReduction<NO_FMA> range_reduction_large;
+  generic::LargeRangeReduction<NO_FMA> range_reduction_large{};
 
   // |x| < 2^32 (with FMA) or |x| < 2^23 (w/o FMA)
   if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT)) {
diff --git a/libc/src/math/generic/sin.cpp b/libc/src/math/generic/sin.cpp
index 207435d4385ae..e7a43245408bf 100644
--- a/libc/src/math/generic/sin.cpp
+++ b/libc/src/math/generic/sin.cpp
@@ -62,7 +62,7 @@ LLVM_LIBC_FUNCTION(double, sin, (double x)) {
 
   DoubleDouble y;
   unsigned k;
-  generic::LargeRangeReduction<NO_FMA> range_reduction_large;
+  generic::LargeRangeReduction<NO_FMA> range_reduction_large{};
 
   // |x| < 2^32 (with FMA) or |x| < 2^23 (w/o FMA)
   if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT)) {
diff --git a/libc/src/math/generic/sincos.cpp b/libc/src/math/generic/sincos.cpp
index a0dd3a018af59..ed70e380b72e8 100644
--- a/libc/src/math/generic/sincos.cpp
+++ b/libc/src/math/generic/sincos.cpp
@@ -63,7 +63,7 @@ LLVM_LIBC_FUNCTION(void, sincos, (double x, double *sin_x, double *cos_x)) {
 
   DoubleDouble y;
   unsigned k;
-  generic::LargeRangeReduction<NO_FMA> range_reduction_large;
+  generic::LargeRangeReduction<NO_FMA> range_reduction_large{};
 
   // |x| < 2^32 (with FMA) or |x| < 2^23 (w/o FMA)
   if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT)) {
diff --git a/libc/src/search/hsearch.cpp b/libc/src/search/hsearch.cpp
index 5aeb5c29449e1..a30803c5a0de7 100644
--- a/libc/src/search/hsearch.cpp
+++ b/libc/src/search/hsearch.cpp
@@ -14,7 +14,7 @@
 
 namespace LIBC_NAMESPACE {
 LLVM_LIBC_FUNCTION(ENTRY *, hsearch, (ENTRY item, ACTION action)) {
-  ENTRY *result;
+  ENTRY *result = nullptr;
   if (internal::global_hash_table == nullptr) {
     // If global_hash_table is null, we create a new hash table with a minimal
     // capacity. Such hashtable will be expanded as needed.
diff --git a/libc/startup/linux/aarch64/tls.cpp b/libc/startup/linux/aarch64/tls.cpp
index f2579e821b1bf..9f143f962892d 100644
--- a/libc/startup/linux/aarch64/tls.cpp
+++ b/libc/startup/linux/aarch64/tls.cpp
@@ -80,7 +80,18 @@ void cleanup_tls(uintptr_t addr, uintptr_t size) {
 }
 
 bool set_thread_ptr(uintptr_t val) {
-  __arm_wsr64("tpidr_el0", val);
+// The PR for __arm_wsr64 support in GCC was merged on Dec 6, 2023, and it is
+// not yet usable in 13.3.0
+// https://github.com/gcc-mirror/gcc/commit/fc42900d21abd5eacb7537c3c8ffc5278d510195
+#if __has_builtin(__builtin_arm_wsr64)
+  __builtin_arm_wsr64("tpidr_el0", val);
+#elif __has_builtin(__builtin_aarch64_wsr)
+  __builtin_aarch64_wsr("tpidr_el0", val);
+#elif defined(__GNUC__)
+  asm volatile("msr tpidr_el0, %0" ::"r"(val));
+#else
+#error "Unsupported compiler"
+#endif
   return true;
 }
 } // namespace LIBC_NAMESPACE



More information about the libc-commits mailing list