[llvm] [Support] Redefine endianness::native (PR #67876)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 29 23:38:06 PDT 2023
https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/67876
We should eventually migrate llvm::support::endianness to std::endian
when C++20 is available for the codebase.
This patch prepares our codebase for that future by removing the
assumption that native is a unique value different from both big and
little. Note that in C++20, native is equal to either big or little
on typical machines, where the endianness is the same for all scalar
types.
>From 6fc5bb9ecae626a1fb70fa09439541dcbc344ae1 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Thu, 28 Sep 2023 13:42:41 -0700
Subject: [PATCH] [Support] Redefine endianness::native
We should eventually migrate llvm::support::endianness to std::endian
when C++20 is available for the codebase.
This patch prepares our codebase for that future by removing the
assumption that native is a unique value different from both big and
little. Note that in C++20, native is equal to either big or little
on typical machines, where the endianness is the same for all scalar
types.
---
llvm/include/llvm/Support/Endian.h | 8 ++++++--
llvm/include/llvm/Support/HashBuilder.h | 12 +-----------
llvm/lib/ExecutionEngine/JITLink/aarch32.cpp | 2 --
3 files changed, 7 insertions(+), 15 deletions(-)
diff --git a/llvm/include/llvm/Support/Endian.h b/llvm/include/llvm/Support/Endian.h
index d407ed112767115..9bfe46022859c9e 100644
--- a/llvm/include/llvm/Support/Endian.h
+++ b/llvm/include/llvm/Support/Endian.h
@@ -24,7 +24,11 @@
namespace llvm {
namespace support {
-enum endianness {big, little, native};
+enum endianness {
+ big,
+ little,
+ native = llvm::sys::IsBigEndianHost ? big : little
+};
// These are named values for common alignments.
enum {aligned = 0, unaligned = 1};
@@ -47,7 +51,7 @@ constexpr endianness system_endianness() {
template <typename value_type>
[[nodiscard]] inline value_type byte_swap(value_type value, endianness endian) {
- if ((endian != native) && (endian != system_endianness()))
+ if (endian != native)
sys::swapByteOrder(value);
return value;
}
diff --git a/llvm/include/llvm/Support/HashBuilder.h b/llvm/include/llvm/Support/HashBuilder.h
index 04a7b2e7dc8ab26..c3fa011b6aa7e9c 100644
--- a/llvm/include/llvm/Support/HashBuilder.h
+++ b/llvm/include/llvm/Support/HashBuilder.h
@@ -86,15 +86,8 @@ template <typename HasherT> class HashBuilderBase {
};
/// Implementation of the `HashBuilder` interface.
-///
-/// `support::endianness::native` is not supported. `HashBuilder` is
-/// expected to canonicalize `support::endianness::native` to one of
-/// `support::endianness::big` or `support::endianness::little`.
template <typename HasherT, support::endianness Endianness>
class HashBuilderImpl : public HashBuilderBase<HasherT> {
- static_assert(Endianness != support::endianness::native,
- "HashBuilder should canonicalize endianness");
-
public:
explicit HashBuilderImpl(HasherT &Hasher)
: HashBuilderBase<HasherT>(Hasher) {}
@@ -395,10 +388,7 @@ class HashBuilderImpl : public HashBuilderBase<HasherT> {
/// Specifiying a non-`native` `Endianness` template parameter allows to compute
/// stable hash across platforms with different endianness.
template <class HasherT, support::endianness Endianness>
-using HashBuilder =
- HashBuilderImpl<HasherT, (Endianness == support::endianness::native
- ? support::endian::system_endianness()
- : Endianness)>;
+using HashBuilder = HashBuilderImpl<HasherT, Endianness>;
namespace hashbuilder_detail {
class HashCodeHasher {
diff --git a/llvm/lib/ExecutionEngine/JITLink/aarch32.cpp b/llvm/lib/ExecutionEngine/JITLink/aarch32.cpp
index 37ced88169b0ac4..124955236cf96a9 100644
--- a/llvm/lib/ExecutionEngine/JITLink/aarch32.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/aarch32.cpp
@@ -306,7 +306,6 @@ void writeImmediate(WritableArmRelocation &R, uint32_t Imm) {
Expected<int64_t> readAddendData(LinkGraph &G, Block &B, const Edge &E) {
support::endianness Endian = G.getEndianness();
- assert(Endian != support::native && "Declare as little or big explicitly");
Edge::Kind Kind = E.getKind();
const char *BlockWorkingMem = B.getContent().data();
@@ -404,7 +403,6 @@ Error applyFixupData(LinkGraph &G, Block &B, const Edge &E) {
char *FixupPtr = BlockWorkingMem + E.getOffset();
auto Write32 = [FixupPtr, Endian = G.getEndianness()](int64_t Value) {
- assert(Endian != native && "Must be explicit: little or big");
assert(isInt<32>(Value) && "Must be in signed 32-bit range");
uint32_t Imm = static_cast<int32_t>(Value);
if (LLVM_LIKELY(Endian == little))
More information about the llvm-commits
mailing list