[libc-commits] [libc] 78cd158 - [libc][AArch64] Fix fullbuild when using G++/GCC

David Spickett via libc-commits libc-commits at lists.llvm.org
Mon Feb 20 00:40:15 PST 2023


Author: David Spickett
Date: 2023-02-20T08:40:10Z
New Revision: 78cd158cf92ce5abdf5e68230b68b06b57354a0f

URL: https://github.com/llvm/llvm-project/commit/78cd158cf92ce5abdf5e68230b68b06b57354a0f
DIFF: https://github.com/llvm/llvm-project/commit/78cd158cf92ce5abdf5e68230b68b06b57354a0f.diff

LOG: [libc][AArch64] Fix fullbuild when using G++/GCC

The libc uses some functions that GCC does not currently
implement, that come from Arm's ACLE header usually.

These are:
```
__arm_wsr64
__arm_rsr64
__arm_wsr
__arm_rsr
```

This issue was reported to us (https://github.com/llvm/llvm-project/issues/60473)
and I've then reported that back to GCC (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108642).

Even if these functions are added, clang has some non standard extensions
to them that gcc may not take. So we're looking at a fix in gcc 13 at best,
and that may not be enough for what we're doing with them.

So I've added ifdefs to use alternatives with gcc.

For handling the stack pointer, inline assembly is unfortunately the only option.
I have verified that the single mov is essentially what __arm_rsr64 generates.

For fpsr and fpcr the gcc devs suggested using
https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/AArch64-Built-in-Functions.html#AArch64-Built-in-Functions.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D143261

Added: 
    

Modified: 
    libc/src/__support/FPUtil/aarch64/FEnvImpl.h
    libc/src/__support/threads/linux/thread.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/FPUtil/aarch64/FEnvImpl.h b/libc/src/__support/FPUtil/aarch64/FEnvImpl.h
index e43c718e6ca5c..783d3f3f56eb4 100644
--- a/libc/src/__support/FPUtil/aarch64/FEnvImpl.h
+++ b/libc/src/__support/FPUtil/aarch64/FEnvImpl.h
@@ -67,13 +67,39 @@ struct FEnv {
            (status & INEXACT ? FE_INEXACT : 0);
   }
 
-  static uint32_t getControlWord() { return __arm_rsr("fpcr"); }
+  static uint32_t getControlWord() {
+#ifdef __clang__
+    // GCC does not currently support __arm_rsr.
+    return __arm_rsr("fpcr");
+#else
+    return __builtin_aarch64_get_fpcr();
+#endif
+  }
 
-  static void writeControlWord(uint32_t fpcr) { __arm_wsr("fpcr", fpcr); }
+  static void writeControlWord(uint32_t fpcr) {
+#ifdef __clang__
+    // GCC does not currently support __arm_wsr.
+    __arm_wsr("fpcr", fpcr);
+#else
+    __builtin_aarch64_set_fpcr(fpcr);
+#endif
+  }
 
-  static uint32_t getStatusWord() { return __arm_rsr("fpsr"); }
+  static uint32_t getStatusWord() {
+#ifdef __clang__
+    return __arm_rsr("fpsr");
+#else
+    return __builtin_aarch64_get_fpsr();
+#endif
+  }
 
-  static void writeStatusWord(uint32_t fpsr) { __arm_wsr("fpsr", fpsr); }
+  static void writeStatusWord(uint32_t fpsr) {
+#ifdef __clang__
+    __arm_wsr("fpsr", fpsr);
+#else
+    __builtin_aarch64_set_fpsr(fpsr);
+#endif
+  }
 };
 
 LIBC_INLINE int enable_except(int excepts) {

diff  --git a/libc/src/__support/threads/linux/thread.cpp b/libc/src/__support/threads/linux/thread.cpp
index a084a007ab754..90198fba327c4 100644
--- a/libc/src/__support/threads/linux/thread.cpp
+++ b/libc/src/__support/threads/linux/thread.cpp
@@ -214,7 +214,12 @@ int Thread::run(ThreadStyle style, ThreadRunner runner, void *arg, void *stack,
 #ifdef LIBC_TARGET_ARCH_IS_AARCH64
     // We set the frame pointer to be the same as the "sp" so that start args
     // can be sniffed out from start_thread.
+#ifdef __clang__
+    // GCC does not currently implement __arm_wsr64/__arm_rsr64.
     __arm_wsr64("x29", __arm_rsr64("sp"));
+#else
+    asm volatile("mov x29, sp");
+#endif
 #endif
     start_thread();
   } else if (clone_result < 0) {


        


More information about the libc-commits mailing list