Skip to content

Commit 9370271

Browse files
[Support] Redefine endianness::native (#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.
1 parent d08fcc8 commit 9370271

File tree

3 files changed

+7
-15
lines changed

3 files changed

+7
-15
lines changed

llvm/include/llvm/Support/Endian.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
namespace llvm {
2525
namespace support {
2626

27-
enum endianness {big, little, native};
27+
enum endianness {
28+
big,
29+
little,
30+
native = llvm::sys::IsBigEndianHost ? big : little
31+
};
2832

2933
// These are named values for common alignments.
3034
enum {aligned = 0, unaligned = 1};
@@ -47,7 +51,7 @@ constexpr endianness system_endianness() {
4751

4852
template <typename value_type>
4953
[[nodiscard]] inline value_type byte_swap(value_type value, endianness endian) {
50-
if ((endian != native) && (endian != system_endianness()))
54+
if (endian != native)
5155
sys::swapByteOrder(value);
5256
return value;
5357
}

llvm/include/llvm/Support/HashBuilder.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,8 @@ template <typename HasherT> class HashBuilderBase {
8686
};
8787

8888
/// Implementation of the `HashBuilder` interface.
89-
///
90-
/// `support::endianness::native` is not supported. `HashBuilder` is
91-
/// expected to canonicalize `support::endianness::native` to one of
92-
/// `support::endianness::big` or `support::endianness::little`.
9389
template <typename HasherT, support::endianness Endianness>
9490
class HashBuilderImpl : public HashBuilderBase<HasherT> {
95-
static_assert(Endianness != support::endianness::native,
96-
"HashBuilder should canonicalize endianness");
97-
9891
public:
9992
explicit HashBuilderImpl(HasherT &Hasher)
10093
: HashBuilderBase<HasherT>(Hasher) {}
@@ -395,10 +388,7 @@ class HashBuilderImpl : public HashBuilderBase<HasherT> {
395388
/// Specifiying a non-`native` `Endianness` template parameter allows to compute
396389
/// stable hash across platforms with different endianness.
397390
template <class HasherT, support::endianness Endianness>
398-
using HashBuilder =
399-
HashBuilderImpl<HasherT, (Endianness == support::endianness::native
400-
? support::endian::system_endianness()
401-
: Endianness)>;
391+
using HashBuilder = HashBuilderImpl<HasherT, Endianness>;
402392

403393
namespace hashbuilder_detail {
404394
class HashCodeHasher {

llvm/lib/ExecutionEngine/JITLink/aarch32.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,6 @@ void writeImmediate(WritableArmRelocation &R, uint32_t Imm) {
306306

307307
Expected<int64_t> readAddendData(LinkGraph &G, Block &B, const Edge &E) {
308308
support::endianness Endian = G.getEndianness();
309-
assert(Endian != support::native && "Declare as little or big explicitly");
310309

311310
Edge::Kind Kind = E.getKind();
312311
const char *BlockWorkingMem = B.getContent().data();
@@ -404,7 +403,6 @@ Error applyFixupData(LinkGraph &G, Block &B, const Edge &E) {
404403
char *FixupPtr = BlockWorkingMem + E.getOffset();
405404

406405
auto Write32 = [FixupPtr, Endian = G.getEndianness()](int64_t Value) {
407-
assert(Endian != native && "Must be explicit: little or big");
408406
assert(isInt<32>(Value) && "Must be in signed 32-bit range");
409407
uint32_t Imm = static_cast<int32_t>(Value);
410408
if (LLVM_LIKELY(Endian == little))

0 commit comments

Comments
 (0)