From f04ca41e0135669772b0608029982969c9f8b8a9 Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Fri, 25 Aug 2023 10:49:33 -0700 Subject: [PATCH 1/2] [Support, ADT] Move llvm::endianness to bit.h In C++20, std::endian comes from . Following the same spirit, this patch moves llvm::endianness and the endian detection logic to bit.h. Without this patch, llvm::endianness::native is defined in terms of llvm::sys::IsBigEndianHost. This patch reverses the dependency. llvm::sys::IsBigEndianHost is now defined in terms of llvm::endianness::native. --- llvm/include/llvm/ADT/bit.h | 35 +++++++++++++++++++++++ llvm/include/llvm/Support/Endian.h | 7 ----- llvm/include/llvm/Support/SwapByteOrder.h | 32 ++------------------- 3 files changed, 37 insertions(+), 37 deletions(-) diff --git a/llvm/include/llvm/ADT/bit.h b/llvm/include/llvm/ADT/bit.h index 2840c5f608d3ea8..0a228b2204d6fc2 100644 --- a/llvm/include/llvm/ADT/bit.h +++ b/llvm/include/llvm/ADT/bit.h @@ -27,6 +27,31 @@ #include // for _byteswap_{ushort,ulong,uint64} #endif +#if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__) || \ + defined(__Fuchsia__) || defined(__EMSCRIPTEN__) +#include +#elif defined(_AIX) +#include +#elif defined(__sun) +/* Solaris provides _BIG_ENDIAN/_LITTLE_ENDIAN selector in sys/types.h */ +#include +#define BIG_ENDIAN 4321 +#define LITTLE_ENDIAN 1234 +#if defined(_BIG_ENDIAN) +#define BYTE_ORDER BIG_ENDIAN +#else +#define BYTE_ORDER LITTLE_ENDIAN +#endif +#elif defined(__MVS__) +#define BIG_ENDIAN 4321 +#define LITTLE_ENDIAN 1234 +#define BYTE_ORDER BIG_ENDIAN +#else +#if !defined(BYTE_ORDER) && !defined(_WIN32) +#include +#endif +#endif + #ifdef _MSC_VER // Declare these intrinsics manually rather including intrin.h. It's very // expensive, and bit.h is popular via MathExtras.h. @@ -41,6 +66,16 @@ unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask); namespace llvm { +enum class endianness { + big, + little, +#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN + native = big +#else + native = little +#endif +}; + // This implementation of bit_cast is different from the C++20 one in two ways: // - It isn't constexpr because that requires compiler support. // - It requires trivially-constructible To, to avoid UB in the implementation. diff --git a/llvm/include/llvm/Support/Endian.h b/llvm/include/llvm/Support/Endian.h index e0afeabbcf34a0e..28a1d2da9bb5b15 100644 --- a/llvm/include/llvm/Support/Endian.h +++ b/llvm/include/llvm/Support/Endian.h @@ -22,13 +22,6 @@ #include namespace llvm { - -enum class endianness { - big, - little, - native = llvm::sys::IsBigEndianHost ? big : little -}; - namespace support { // TODO: Remove the following once we are done migrating to llvm::endianness, diff --git a/llvm/include/llvm/Support/SwapByteOrder.h b/llvm/include/llvm/Support/SwapByteOrder.h index 43fa5347d6fe851..8f26af6f68ac65f 100644 --- a/llvm/include/llvm/Support/SwapByteOrder.h +++ b/llvm/include/llvm/Support/SwapByteOrder.h @@ -19,40 +19,12 @@ #include #include -#if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__) || \ - defined(__Fuchsia__) || defined(__EMSCRIPTEN__) -#include -#elif defined(_AIX) -#include -#elif defined(__sun) -/* Solaris provides _BIG_ENDIAN/_LITTLE_ENDIAN selector in sys/types.h */ -#include -#define BIG_ENDIAN 4321 -#define LITTLE_ENDIAN 1234 -#if defined(_BIG_ENDIAN) -#define BYTE_ORDER BIG_ENDIAN -#else -#define BYTE_ORDER LITTLE_ENDIAN -#endif -#elif defined(__MVS__) -#define BIG_ENDIAN 4321 -#define LITTLE_ENDIAN 1234 -#define BYTE_ORDER BIG_ENDIAN -#else -#if !defined(BYTE_ORDER) && !defined(_WIN32) -#include -#endif -#endif - namespace llvm { namespace sys { -#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN -constexpr bool IsBigEndianHost = true; -#else -constexpr bool IsBigEndianHost = false; -#endif +constexpr bool IsBigEndianHost = + llvm::endianness::native == llvm::endianness::big; static const bool IsLittleEndianHost = !IsBigEndianHost; From 6e0e57830babbf3180f841eba9deb3be3f9d98a0 Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Thu, 5 Oct 2023 09:34:53 -0700 Subject: [PATCH 2/2] Include llvm/ADT/bit.h --- llvm/include/llvm/Support/Endian.h | 1 + 1 file changed, 1 insertion(+) diff --git a/llvm/include/llvm/Support/Endian.h b/llvm/include/llvm/Support/Endian.h index 28a1d2da9bb5b15..8b6f6efe2a37f22 100644 --- a/llvm/include/llvm/Support/Endian.h +++ b/llvm/include/llvm/Support/Endian.h @@ -13,6 +13,7 @@ #ifndef LLVM_SUPPORT_ENDIAN_H #define LLVM_SUPPORT_ENDIAN_H +#include "llvm/ADT/bit.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/SwapByteOrder.h" #include