[libc-commits] [libc] 0aea170 - [libc] Add more robust compile time architecture detection

Guillaume Chatelet via libc-commits libc-commits at lists.llvm.org
Tue Nov 2 04:01:05 PDT 2021


Author: Guillaume Chatelet
Date: 2021-11-02T11:00:33Z
New Revision: 0aea170b97f6523b94409f3facfa4549cbfbe6b0

URL: https://github.com/llvm/llvm-project/commit/0aea170b97f6523b94409f3facfa4549cbfbe6b0
DIFF: https://github.com/llvm/llvm-project/commit/0aea170b97f6523b94409f3facfa4549cbfbe6b0.diff

LOG: [libc] Add more robust compile time architecture detection

We may want to restrict the detected platforms to only `x86_64` and `aarch64`.
There are still custom detection in api.td but I don't think we can handle these:
 - config/linux/api.td:205
 - config/linux/api.td:199

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

Added: 
    libc/src/__support/architectures.h

Modified: 
    libc/src/__support/CMakeLists.txt
    libc/src/__support/FPUtil/FEnvUtils.h
    libc/src/__support/FPUtil/FMA.h
    libc/src/__support/FPUtil/PlatformDefs.h
    libc/src/__support/FPUtil/PolyEval.h
    libc/src/string/memcmp.cpp
    libc/src/string/memory_utils/elements_aarch64.h
    libc/src/string/memory_utils/elements_x86.h
    libc/src/string/memory_utils/memset_utils.h
    libc/src/string/memory_utils/utils.h
    libc/test/src/fenv/enabled_exceptions_test.cpp
    libc/test/src/fenv/feenableexcept_test.cpp
    libc/test/src/fenv/feholdexcept_test.cpp
    libc/utils/MPFRWrapper/MPFRUtils.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index 871c3a447b80a..5d2667dcb7fa4 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -3,6 +3,7 @@ add_subdirectory(CPP)
 add_header_library(
   common
   HDRS
+    architectures.h
     common.h
     endian.h
     sanitizer.h

diff  --git a/libc/src/__support/FPUtil/FEnvUtils.h b/libc/src/__support/FPUtil/FEnvUtils.h
index a9d11ceb6535d..2cb62e558c80f 100644
--- a/libc/src/__support/FPUtil/FEnvUtils.h
+++ b/libc/src/__support/FPUtil/FEnvUtils.h
@@ -9,9 +9,11 @@
 #ifndef LLVM_LIBC_SRC_SUPPORT_FPUTIL_FENVUTILS_H
 #define LLVM_LIBC_SRC_SUPPORT_FPUTIL_FENVUTILS_H
 
-#ifdef __x86_64__
+#include "src/__support/architectures.h"
+
+#if defined(LLVM_LIBC_ARCH_X86_64)
 #include "x86_64/FEnvImpl.h"
-#elif defined(__aarch64__)
+#elif defined(LLVM_LIBC_ARCH_AARCH64)
 #include "aarch64/FEnvImpl.h"
 #else
 #include "DummyFEnvImpl.h"

diff  --git a/libc/src/__support/FPUtil/FMA.h b/libc/src/__support/FPUtil/FMA.h
index c109b3470bd3e..5f35bec644ebb 100644
--- a/libc/src/__support/FPUtil/FMA.h
+++ b/libc/src/__support/FPUtil/FMA.h
@@ -10,10 +10,11 @@
 #define LLVM_LIBC_SRC_SUPPORT_FPUTIL_FMA_H
 
 #include "src/__support/CPP/TypeTraits.h"
+#include "src/__support/architectures.h"
 
-#ifdef __x86_64__
+#if defined(LLVM_LIBC_ARCH_X86_64)
 #include "x86_64/FMA.h"
-#elif defined(__aarch64__)
+#elif defined(LLVM_LIBC_ARCH_AARCH64)
 #include "aarch64/FMA.h"
 #else
 #include "generic/FMA.h"

diff  --git a/libc/src/__support/FPUtil/PlatformDefs.h b/libc/src/__support/FPUtil/PlatformDefs.h
index d9964d11106c4..61af0dae49723 100644
--- a/libc/src/__support/FPUtil/PlatformDefs.h
+++ b/libc/src/__support/FPUtil/PlatformDefs.h
@@ -9,7 +9,9 @@
 #ifndef LLVM_LIBC_SRC_SUPPORT_FPUTIL_PLATFORM_DEFS_H
 #define LLVM_LIBC_SRC_SUPPORT_FPUTIL_PLATFORM_DEFS_H
 
-#if defined(__x86_64__) || defined(__i386__)
+#include "src/__support/architectures.h"
+
+#if defined(LLVM_LIBC_ARCH_X86)
 #define X87_FPU
 #endif
 

diff  --git a/libc/src/__support/FPUtil/PolyEval.h b/libc/src/__support/FPUtil/PolyEval.h
index ccf4d60c1043a..10d406d05262b 100644
--- a/libc/src/__support/FPUtil/PolyEval.h
+++ b/libc/src/__support/FPUtil/PolyEval.h
@@ -10,6 +10,7 @@
 #define LLVM_LIBC_SRC_SUPPORT_FPUTIL_POLYEVAL_H
 
 #include "src/__support/CPP/TypeTraits.h"
+#include "src/__support/architectures.h"
 
 // Evaluate polynomial using Horner's Scheme:
 // With polyeval(x, a_0, a_1, ..., a_n) = a_n * x^n + ... + a_1 * x + a_0, we
@@ -18,7 +19,7 @@
 // Example: to evaluate x^3 + 2*x^2 + 3*x + 4, call
 //   polyeval( x, 4.0, 3.0, 2.0, 1.0 )
 
-#if defined(__x86_64__) || defined(__aarch64__)
+#if defined(LLVM_LIBC_ARCH_X86_64) || defined(LLVM_LIBC_ARCH_AARCH64)
 #include "FMA.h"
 
 namespace __llvm_libc {

diff  --git a/libc/src/__support/architectures.h b/libc/src/__support/architectures.h
new file mode 100644
index 0000000000000..485faae47c7e3
--- /dev/null
+++ b/libc/src/__support/architectures.h
@@ -0,0 +1,35 @@
+//===-- Compile time architecture detection -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#if defined(__pnacl__) || defined(__CLR_VER)
+#define LLVM_LIBC_ARCH_VM
+#endif
+
+#if (defined(_M_IX86) || defined(__i386__)) && !defined(LLVM_LIBC_ARCH_VM)
+#define LLVM_LIBC_ARCH_X86_32
+#endif
+
+#if (defined(_M_X64) || defined(__x86_64__)) && !defined(LLVM_LIBC_ARCH_VM)
+#define LLVM_LIBC_ARCH_X86_64
+#endif
+
+#if defined(LLVM_LIBC_ARCH_X86_32) || defined(LLVM_LIBC_ARCH_X86_64)
+#define LLVM_LIBC_ARCH_X86
+#endif
+
+#if (defined(__arm__) || defined(_M_ARM))
+#define LLVM_LIBC_ARCH_ARM
+#endif
+
+#if defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64)
+#define LLVM_LIBC_ARCH_AARCH64
+#endif
+
+#if (defined(LLVM_LIBC_ARCH_AARCH64) || defined(LLVM_LIBC_ARCH_ARM))
+#define LLVM_LIBC_ARCH_ANY_ARM
+#endif

diff  --git a/libc/src/string/memcmp.cpp b/libc/src/string/memcmp.cpp
index bb2b5e2f37791..0f2dae27546d5 100644
--- a/libc/src/string/memcmp.cpp
+++ b/libc/src/string/memcmp.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/string/memcmp.h"
+#include "src/__support/architectures.h"
 #include "src/__support/common.h"
 #include "src/string/memory_utils/elements.h"
 
@@ -15,7 +16,7 @@
 namespace __llvm_libc {
 
 static int memcmp_impl(const char *lhs, const char *rhs, size_t count) {
-#if defined(__i386__) || defined(__x86_64__)
+#if defined(LLVM_LIBC_ARCH_X86)
   using namespace ::__llvm_libc::x86;
 #else
   using namespace ::__llvm_libc::scalar;

diff  --git a/libc/src/string/memory_utils/elements_aarch64.h b/libc/src/string/memory_utils/elements_aarch64.h
index 36d3074bac5bf..0c8990cd6f054 100644
--- a/libc/src/string/memory_utils/elements_aarch64.h
+++ b/libc/src/string/memory_utils/elements_aarch64.h
@@ -9,7 +9,9 @@
 #ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ELEMENTS_AARCH64_H
 #define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ELEMENTS_AARCH64_H
 
-#if defined(__arm__) || defined(__aarch64__)
+#include "src/__support/architectures.h"
+
+#if defined(LLVM_LIBC_ARCH_AARCH64)
 
 #include <src/string/memory_utils/elements.h>
 #include <stddef.h> // size_t
@@ -115,6 +117,6 @@ using _32 = __llvm_libc::scalar::_32;
 } // namespace aarch64
 } // namespace __llvm_libc
 
-#endif // defined(__arm__) || defined(__aarch64__)
+#endif // defined(LLVM_LIBC_ARCH_AARCH64)
 
 #endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ELEMENTS_AARCH64_H

diff  --git a/libc/src/string/memory_utils/elements_x86.h b/libc/src/string/memory_utils/elements_x86.h
index 9b32b427f76e4..e8be55b510e20 100644
--- a/libc/src/string/memory_utils/elements_x86.h
+++ b/libc/src/string/memory_utils/elements_x86.h
@@ -9,8 +9,9 @@
 #ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ELEMENTS_X86_H
 #define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ELEMENTS_X86_H
 
-#if defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) ||            \
-    defined(_M_X64)
+#include "src/__support/architectures.h"
+
+#if defined(LLVM_LIBC_ARCH_X86)
 
 #include <stddef.h> // size_t
 #include <stdint.h> // uint8_t, uint16_t, uint32_t, uint64_t
@@ -172,7 +173,6 @@ struct Accelerator {
 } // namespace x86
 } // namespace __llvm_libc
 
-#endif // defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) ||
-       // defined(_M_X64)
+#endif // defined(LLVM_LIBC_ARCH_X86)
 
 #endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ELEMENTS_X86_H

diff  --git a/libc/src/string/memory_utils/memset_utils.h b/libc/src/string/memory_utils/memset_utils.h
index 5b955a3e30b1e..666d649434795 100644
--- a/libc/src/string/memory_utils/memset_utils.h
+++ b/libc/src/string/memory_utils/memset_utils.h
@@ -9,6 +9,7 @@
 #ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_MEMSET_UTILS_H
 #define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_MEMSET_UTILS_H
 
+#include "src/__support/architectures.h"
 #include "src/string/memory_utils/elements.h"
 #include "src/string/memory_utils/utils.h"
 
@@ -49,7 +50,7 @@ namespace __llvm_libc {
 // superior for sizes that mattered.
 inline static void GeneralPurposeMemset(char *dst, unsigned char value,
                                         size_t count) {
-#if defined(__i386__) || defined(__x86_64__)
+#if defined(LLVM_LIBC_ARCH_X86)
   using namespace ::__llvm_libc::x86;
 #else
   using namespace ::__llvm_libc::scalar;

diff  --git a/libc/src/string/memory_utils/utils.h b/libc/src/string/memory_utils/utils.h
index d6047e1c482fe..f23a3240fde72 100644
--- a/libc/src/string/memory_utils/utils.h
+++ b/libc/src/string/memory_utils/utils.h
@@ -9,19 +9,13 @@
 #ifndef LLVM_LIBC_SRC_MEMORY_UTILS_UTILS_H
 #define LLVM_LIBC_SRC_MEMORY_UTILS_UTILS_H
 
+#include "src/__support/architectures.h"
+
 // Cache line sizes for ARM: These values are not strictly correct since
 // cache line sizes depend on implementations, not architectures.  There
 // are even implementations with cache line sizes configurable at boot
 // time.
-#if defined(__aarch64__)
-#define LLVM_LIBC_CACHELINE_SIZE 64
-#elif defined(__ARM_ARCH_5T__)
-#define LLVM_LIBC_CACHELINE_SIZE 32
-#elif defined(__ARM_ARCH_7A__)
-#define LLVM_LIBC_CACHELINE_SIZE 64
-#elif defined(__PPC64__)
-#define LLVM_LIBC_CACHELINE_SIZE 128
-#elif defined(__i386__) || defined(__x86_64__)
+#if defined(LLVM_LIBC_ARCH_AARCH64) || defined(LLVM_LIBC_ARCH_X86)
 #define LLVM_LIBC_CACHELINE_SIZE 64
 #else
 #error "Unsupported platform for memory functions."

diff  --git a/libc/test/src/fenv/enabled_exceptions_test.cpp b/libc/test/src/fenv/enabled_exceptions_test.cpp
index 7b91e98b4e94d..bf04b6418eb45 100644
--- a/libc/test/src/fenv/enabled_exceptions_test.cpp
+++ b/libc/test/src/fenv/enabled_exceptions_test.cpp
@@ -11,6 +11,7 @@
 #include "src/fenv/fetestexcept.h"
 
 #include "src/__support/FPUtil/FEnvUtils.h"
+#include "src/__support/architectures.h"
 #include "utils/UnitTest/FPExceptMatcher.h"
 #include "utils/UnitTest/Test.h"
 
@@ -20,7 +21,7 @@
 // This test enables an exception and verifies that raising that exception
 // triggers SIGFPE.
 TEST(LlvmLibcExceptionStatusTest, RaiseAndCrash) {
-#ifdef __aarch64__
+#if defined(LLVM_LIBC_ARCH_AARCH64)
   // Few aarch64 HW implementations do not trap exceptions. We skip this test
   // completely on such HW.
   //
@@ -32,7 +33,7 @@ TEST(LlvmLibcExceptionStatusTest, RaiseAndCrash) {
   __llvm_libc::fputil::enableExcept(FE_DIVBYZERO);
   if (__llvm_libc::fputil::getExcept() == 0)
     return;
-#endif
+#endif // defined(LLVM_LIBC_ARCH_AARCH64)
 
   // TODO: Install a floating point exception handler and verify that the
   // the expected exception was raised. One will have to longjmp back from

diff  --git a/libc/test/src/fenv/feenableexcept_test.cpp b/libc/test/src/fenv/feenableexcept_test.cpp
index 2158f954bcd2b..f1cb8a32ea677 100644
--- a/libc/test/src/fenv/feenableexcept_test.cpp
+++ b/libc/test/src/fenv/feenableexcept_test.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "src/__support/architectures.h"
 #include "src/fenv/fedisableexcept.h"
 #include "src/fenv/feenableexcept.h"
 #include "src/fenv/fegetexcept.h"
@@ -15,7 +16,7 @@
 #include <fenv.h>
 
 TEST(LlvmLibcFEnvTest, EnableTest) {
-#ifdef __aarch64__
+#if defined(LLVM_LIBC_ARCH_AARCH64)
   // Few aarch64 HW implementations do not trap exceptions. We skip this test
   // completely on such HW.
   //
@@ -27,7 +28,7 @@ TEST(LlvmLibcFEnvTest, EnableTest) {
   __llvm_libc::feenableexcept(FE_DIVBYZERO);
   if (__llvm_libc::fegetexcept() == 0)
     return;
-#endif
+#endif // defined(LLVM_LIBC_ARCH_AARCH64)
 
   int excepts[] = {FE_DIVBYZERO, FE_INVALID, FE_INEXACT, FE_OVERFLOW,
                    FE_UNDERFLOW};

diff  --git a/libc/test/src/fenv/feholdexcept_test.cpp b/libc/test/src/fenv/feholdexcept_test.cpp
index be836a4c23ce6..6bdea80f40373 100644
--- a/libc/test/src/fenv/feholdexcept_test.cpp
+++ b/libc/test/src/fenv/feholdexcept_test.cpp
@@ -9,13 +9,14 @@
 #include "src/fenv/feholdexcept.h"
 
 #include "src/__support/FPUtil/FEnvUtils.h"
+#include "src/__support/architectures.h"
 #include "utils/UnitTest/FPExceptMatcher.h"
 #include "utils/UnitTest/Test.h"
 
 #include <fenv.h>
 
 TEST(LlvmLibcFEnvTest, RaiseAndCrash) {
-#ifdef __aarch64__
+#if defined(LLVM_LIBC_ARCH_AARCH64)
   // Few aarch64 HW implementations do not trap exceptions. We skip this test
   // completely on such HW.
   //
@@ -27,7 +28,7 @@ TEST(LlvmLibcFEnvTest, RaiseAndCrash) {
   __llvm_libc::fputil::enableExcept(FE_DIVBYZERO);
   if (__llvm_libc::fputil::getExcept() == 0)
     return;
-#endif
+#endif // defined(LLVM_LIBC_ARCH_AARCH64)
 
   int excepts[] = {FE_DIVBYZERO, FE_INVALID, FE_INEXACT, FE_OVERFLOW,
                    FE_UNDERFLOW};

diff  --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index 9a2a125d301b7..9ae0ce6ff8097 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -10,6 +10,7 @@
 
 #include "src/__support/CPP/StringView.h"
 #include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/architectures.h"
 #include "utils/UnitTest/FPMatcher.h"
 
 #include <cmath>
@@ -44,7 +45,7 @@ template <> struct Precision<double> {
   static constexpr unsigned int value = 53;
 };
 
-#if !(defined(__x86_64__) || defined(__i386__))
+#if !(defined(LLVM_LIBC_ARCH_X86))
 template <> struct Precision<long double> {
   static constexpr unsigned int value = 64;
 };
@@ -100,9 +101,7 @@ class MPFRNumber {
     mpfr_set(value, other.value, MPFR_RNDN);
   }
 
-  ~MPFRNumber() {
-    mpfr_clear(value);
-  }
+  ~MPFRNumber() { mpfr_clear(value); }
 
   MPFRNumber &operator=(const MPFRNumber &rhs) {
     mpfrPrecision = rhs.mpfrPrecision;


        


More information about the libc-commits mailing list