Skip to content

Commit 0aea170

Browse files
committed
[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
1 parent 56513e3 commit 0aea170

File tree

15 files changed

+75
-33
lines changed

15 files changed

+75
-33
lines changed

libc/src/__support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ add_subdirectory(CPP)
33
add_header_library(
44
common
55
HDRS
6+
architectures.h
67
common.h
78
endian.h
89
sanitizer.h

libc/src/__support/FPUtil/FEnvUtils.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
#ifndef LLVM_LIBC_SRC_SUPPORT_FPUTIL_FENVUTILS_H
1010
#define LLVM_LIBC_SRC_SUPPORT_FPUTIL_FENVUTILS_H
1111

12-
#ifdef __x86_64__
12+
#include "src/__support/architectures.h"
13+
14+
#if defined(LLVM_LIBC_ARCH_X86_64)
1315
#include "x86_64/FEnvImpl.h"
14-
#elif defined(__aarch64__)
16+
#elif defined(LLVM_LIBC_ARCH_AARCH64)
1517
#include "aarch64/FEnvImpl.h"
1618
#else
1719
#include "DummyFEnvImpl.h"

libc/src/__support/FPUtil/FMA.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
#define LLVM_LIBC_SRC_SUPPORT_FPUTIL_FMA_H
1111

1212
#include "src/__support/CPP/TypeTraits.h"
13+
#include "src/__support/architectures.h"
1314

14-
#ifdef __x86_64__
15+
#if defined(LLVM_LIBC_ARCH_X86_64)
1516
#include "x86_64/FMA.h"
16-
#elif defined(__aarch64__)
17+
#elif defined(LLVM_LIBC_ARCH_AARCH64)
1718
#include "aarch64/FMA.h"
1819
#else
1920
#include "generic/FMA.h"

libc/src/__support/FPUtil/PlatformDefs.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#ifndef LLVM_LIBC_SRC_SUPPORT_FPUTIL_PLATFORM_DEFS_H
1010
#define LLVM_LIBC_SRC_SUPPORT_FPUTIL_PLATFORM_DEFS_H
1111

12-
#if defined(__x86_64__) || defined(__i386__)
12+
#include "src/__support/architectures.h"
13+
14+
#if defined(LLVM_LIBC_ARCH_X86)
1315
#define X87_FPU
1416
#endif
1517

libc/src/__support/FPUtil/PolyEval.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLVM_LIBC_SRC_SUPPORT_FPUTIL_POLYEVAL_H
1111

1212
#include "src/__support/CPP/TypeTraits.h"
13+
#include "src/__support/architectures.h"
1314

1415
// Evaluate polynomial using Horner's Scheme:
1516
// With polyeval(x, a_0, a_1, ..., a_n) = a_n * x^n + ... + a_1 * x + a_0, we
@@ -18,7 +19,7 @@
1819
// Example: to evaluate x^3 + 2*x^2 + 3*x + 4, call
1920
// polyeval( x, 4.0, 3.0, 2.0, 1.0 )
2021

21-
#if defined(__x86_64__) || defined(__aarch64__)
22+
#if defined(LLVM_LIBC_ARCH_X86_64) || defined(LLVM_LIBC_ARCH_AARCH64)
2223
#include "FMA.h"
2324

2425
namespace __llvm_libc {

libc/src/__support/architectures.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===-- Compile time architecture detection -------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#if defined(__pnacl__) || defined(__CLR_VER)
10+
#define LLVM_LIBC_ARCH_VM
11+
#endif
12+
13+
#if (defined(_M_IX86) || defined(__i386__)) && !defined(LLVM_LIBC_ARCH_VM)
14+
#define LLVM_LIBC_ARCH_X86_32
15+
#endif
16+
17+
#if (defined(_M_X64) || defined(__x86_64__)) && !defined(LLVM_LIBC_ARCH_VM)
18+
#define LLVM_LIBC_ARCH_X86_64
19+
#endif
20+
21+
#if defined(LLVM_LIBC_ARCH_X86_32) || defined(LLVM_LIBC_ARCH_X86_64)
22+
#define LLVM_LIBC_ARCH_X86
23+
#endif
24+
25+
#if (defined(__arm__) || defined(_M_ARM))
26+
#define LLVM_LIBC_ARCH_ARM
27+
#endif
28+
29+
#if defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64)
30+
#define LLVM_LIBC_ARCH_AARCH64
31+
#endif
32+
33+
#if (defined(LLVM_LIBC_ARCH_AARCH64) || defined(LLVM_LIBC_ARCH_ARM))
34+
#define LLVM_LIBC_ARCH_ANY_ARM
35+
#endif

libc/src/string/memcmp.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/string/memcmp.h"
10+
#include "src/__support/architectures.h"
1011
#include "src/__support/common.h"
1112
#include "src/string/memory_utils/elements.h"
1213

@@ -15,7 +16,7 @@
1516
namespace __llvm_libc {
1617

1718
static int memcmp_impl(const char *lhs, const char *rhs, size_t count) {
18-
#if defined(__i386__) || defined(__x86_64__)
19+
#if defined(LLVM_LIBC_ARCH_X86)
1920
using namespace ::__llvm_libc::x86;
2021
#else
2122
using namespace ::__llvm_libc::scalar;

libc/src/string/memory_utils/elements_aarch64.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ELEMENTS_AARCH64_H
1010
#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ELEMENTS_AARCH64_H
1111

12-
#if defined(__arm__) || defined(__aarch64__)
12+
#include "src/__support/architectures.h"
13+
14+
#if defined(LLVM_LIBC_ARCH_AARCH64)
1315

1416
#include <src/string/memory_utils/elements.h>
1517
#include <stddef.h> // size_t
@@ -115,6 +117,6 @@ using _32 = __llvm_libc::scalar::_32;
115117
} // namespace aarch64
116118
} // namespace __llvm_libc
117119

118-
#endif // defined(__arm__) || defined(__aarch64__)
120+
#endif // defined(LLVM_LIBC_ARCH_AARCH64)
119121

120122
#endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ELEMENTS_AARCH64_H

libc/src/string/memory_utils/elements_x86.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
#ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ELEMENTS_X86_H
1010
#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ELEMENTS_X86_H
1111

12-
#if defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || \
13-
defined(_M_X64)
12+
#include "src/__support/architectures.h"
13+
14+
#if defined(LLVM_LIBC_ARCH_X86)
1415

1516
#include <stddef.h> // size_t
1617
#include <stdint.h> // uint8_t, uint16_t, uint32_t, uint64_t
@@ -172,7 +173,6 @@ struct Accelerator {
172173
} // namespace x86
173174
} // namespace __llvm_libc
174175

175-
#endif // defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) ||
176-
// defined(_M_X64)
176+
#endif // defined(LLVM_LIBC_ARCH_X86)
177177

178178
#endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_ELEMENTS_X86_H

libc/src/string/memory_utils/memset_utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_MEMSET_UTILS_H
1010
#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_MEMSET_UTILS_H
1111

12+
#include "src/__support/architectures.h"
1213
#include "src/string/memory_utils/elements.h"
1314
#include "src/string/memory_utils/utils.h"
1415

@@ -49,7 +50,7 @@ namespace __llvm_libc {
4950
// superior for sizes that mattered.
5051
inline static void GeneralPurposeMemset(char *dst, unsigned char value,
5152
size_t count) {
52-
#if defined(__i386__) || defined(__x86_64__)
53+
#if defined(LLVM_LIBC_ARCH_X86)
5354
using namespace ::__llvm_libc::x86;
5455
#else
5556
using namespace ::__llvm_libc::scalar;

libc/src/string/memory_utils/utils.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,13 @@
99
#ifndef LLVM_LIBC_SRC_MEMORY_UTILS_UTILS_H
1010
#define LLVM_LIBC_SRC_MEMORY_UTILS_UTILS_H
1111

12+
#include "src/__support/architectures.h"
13+
1214
// Cache line sizes for ARM: These values are not strictly correct since
1315
// cache line sizes depend on implementations, not architectures. There
1416
// are even implementations with cache line sizes configurable at boot
1517
// time.
16-
#if defined(__aarch64__)
17-
#define LLVM_LIBC_CACHELINE_SIZE 64
18-
#elif defined(__ARM_ARCH_5T__)
19-
#define LLVM_LIBC_CACHELINE_SIZE 32
20-
#elif defined(__ARM_ARCH_7A__)
21-
#define LLVM_LIBC_CACHELINE_SIZE 64
22-
#elif defined(__PPC64__)
23-
#define LLVM_LIBC_CACHELINE_SIZE 128
24-
#elif defined(__i386__) || defined(__x86_64__)
18+
#if defined(LLVM_LIBC_ARCH_AARCH64) || defined(LLVM_LIBC_ARCH_X86)
2519
#define LLVM_LIBC_CACHELINE_SIZE 64
2620
#else
2721
#error "Unsupported platform for memory functions."

libc/test/src/fenv/enabled_exceptions_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "src/fenv/fetestexcept.h"
1212

1313
#include "src/__support/FPUtil/FEnvUtils.h"
14+
#include "src/__support/architectures.h"
1415
#include "utils/UnitTest/FPExceptMatcher.h"
1516
#include "utils/UnitTest/Test.h"
1617

@@ -20,7 +21,7 @@
2021
// This test enables an exception and verifies that raising that exception
2122
// triggers SIGFPE.
2223
TEST(LlvmLibcExceptionStatusTest, RaiseAndCrash) {
23-
#ifdef __aarch64__
24+
#if defined(LLVM_LIBC_ARCH_AARCH64)
2425
// Few aarch64 HW implementations do not trap exceptions. We skip this test
2526
// completely on such HW.
2627
//
@@ -32,7 +33,7 @@ TEST(LlvmLibcExceptionStatusTest, RaiseAndCrash) {
3233
__llvm_libc::fputil::enableExcept(FE_DIVBYZERO);
3334
if (__llvm_libc::fputil::getExcept() == 0)
3435
return;
35-
#endif
36+
#endif // defined(LLVM_LIBC_ARCH_AARCH64)
3637

3738
// TODO: Install a floating point exception handler and verify that the
3839
// the expected exception was raised. One will have to longjmp back from

libc/test/src/fenv/feenableexcept_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "src/__support/architectures.h"
910
#include "src/fenv/fedisableexcept.h"
1011
#include "src/fenv/feenableexcept.h"
1112
#include "src/fenv/fegetexcept.h"
@@ -15,7 +16,7 @@
1516
#include <fenv.h>
1617

1718
TEST(LlvmLibcFEnvTest, EnableTest) {
18-
#ifdef __aarch64__
19+
#if defined(LLVM_LIBC_ARCH_AARCH64)
1920
// Few aarch64 HW implementations do not trap exceptions. We skip this test
2021
// completely on such HW.
2122
//
@@ -27,7 +28,7 @@ TEST(LlvmLibcFEnvTest, EnableTest) {
2728
__llvm_libc::feenableexcept(FE_DIVBYZERO);
2829
if (__llvm_libc::fegetexcept() == 0)
2930
return;
30-
#endif
31+
#endif // defined(LLVM_LIBC_ARCH_AARCH64)
3132

3233
int excepts[] = {FE_DIVBYZERO, FE_INVALID, FE_INEXACT, FE_OVERFLOW,
3334
FE_UNDERFLOW};

libc/test/src/fenv/feholdexcept_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
#include "src/fenv/feholdexcept.h"
1010

1111
#include "src/__support/FPUtil/FEnvUtils.h"
12+
#include "src/__support/architectures.h"
1213
#include "utils/UnitTest/FPExceptMatcher.h"
1314
#include "utils/UnitTest/Test.h"
1415

1516
#include <fenv.h>
1617

1718
TEST(LlvmLibcFEnvTest, RaiseAndCrash) {
18-
#ifdef __aarch64__
19+
#if defined(LLVM_LIBC_ARCH_AARCH64)
1920
// Few aarch64 HW implementations do not trap exceptions. We skip this test
2021
// completely on such HW.
2122
//
@@ -27,7 +28,7 @@ TEST(LlvmLibcFEnvTest, RaiseAndCrash) {
2728
__llvm_libc::fputil::enableExcept(FE_DIVBYZERO);
2829
if (__llvm_libc::fputil::getExcept() == 0)
2930
return;
30-
#endif
31+
#endif // defined(LLVM_LIBC_ARCH_AARCH64)
3132

3233
int excepts[] = {FE_DIVBYZERO, FE_INVALID, FE_INEXACT, FE_OVERFLOW,
3334
FE_UNDERFLOW};

libc/utils/MPFRWrapper/MPFRUtils.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "src/__support/CPP/StringView.h"
1212
#include "src/__support/FPUtil/FPBits.h"
13+
#include "src/__support/architectures.h"
1314
#include "utils/UnitTest/FPMatcher.h"
1415

1516
#include <cmath>
@@ -44,7 +45,7 @@ template <> struct Precision<double> {
4445
static constexpr unsigned int value = 53;
4546
};
4647

47-
#if !(defined(__x86_64__) || defined(__i386__))
48+
#if !(defined(LLVM_LIBC_ARCH_X86))
4849
template <> struct Precision<long double> {
4950
static constexpr unsigned int value = 64;
5051
};
@@ -100,9 +101,7 @@ class MPFRNumber {
100101
mpfr_set(value, other.value, MPFR_RNDN);
101102
}
102103

103-
~MPFRNumber() {
104-
mpfr_clear(value);
105-
}
104+
~MPFRNumber() { mpfr_clear(value); }
106105

107106
MPFRNumber &operator=(const MPFRNumber &rhs) {
108107
mpfrPrecision = rhs.mpfrPrecision;

0 commit comments

Comments
 (0)