From 152d0f5c0c0eaea369bf534b673d7625700ca7ef Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Fri, 12 Sep 2025 00:26:54 -0700 Subject: [PATCH] [Support] Deprecate one form of support::endian::write (NFC) (#156140) We have two forms of write: template inline void write(void *memory, value_type value, endianness endian) template inline void write(void *memory, value_type value) The difference is that endian is a function parameter in the former but a template parameter in the latter. This patch streamlines the code by migrating the use of the latter to the former while deprecating the latter. I'm planning to do the same for byte_swap and read in follow-up patches to keep this patch simple and small. --- llvm/include/llvm/Support/Endian.h | 13 ++++++------- llvm/lib/ObjectYAML/GOFFEmitter.cpp | 4 ++-- llvm/unittests/Support/EndianTest.cpp | 14 +++++++------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/llvm/include/llvm/Support/Endian.h b/llvm/include/llvm/Support/Endian.h index 02a3194e0978..7eb1d7e8dfe7 100644 --- a/llvm/include/llvm/Support/Endian.h +++ b/llvm/include/llvm/Support/Endian.h @@ -96,9 +96,8 @@ inline void write(void *memory, value_type value, endianness endian) { &value, sizeof(value_type)); } -template +template +LLVM_DEPRECATED("Pass endian as a function argument instead", "write") inline void write(void *memory, value_type value) { write(memory, value, endian); } @@ -163,7 +162,7 @@ inline void writeAtBitAlignment(void *memory, value_type value, uint64_t startBit) { assert(startBit < 8); if (startBit == 0) - write(memory, value); + write(memory, value, endian); else { // Read two values and shift the result into them. value_type val[2]; @@ -230,8 +229,8 @@ struct packed_endian_specific_integral { operator value_type() const { return value(); } void operator=(value_type newValue) { - endian::write( - (void*)Value.buffer, newValue); + endian::write((void *)Value.buffer, newValue, + endian); } packed_endian_specific_integral &operator+=(value_type newValue) { @@ -268,7 +267,7 @@ public: } void operator=(value_type NewValue) { - endian::write(Ptr, NewValue); + endian::write(Ptr, NewValue, endian); } private: diff --git a/llvm/lib/ObjectYAML/GOFFEmitter.cpp b/llvm/lib/ObjectYAML/GOFFEmitter.cpp index 7e94ac609a03..c26893cfaa72 100644 --- a/llvm/lib/ObjectYAML/GOFFEmitter.cpp +++ b/llvm/lib/ObjectYAML/GOFFEmitter.cpp @@ -38,8 +38,8 @@ template struct BinaryBeImpl { template raw_ostream &operator<<(raw_ostream &OS, const BinaryBeImpl &BBE) { char Buffer[sizeof(BBE.Value)]; - support::endian::write( - Buffer, BBE.Value); + support::endian::write(Buffer, BBE.Value, + llvm::endianness::big); OS.write(Buffer, sizeof(BBE.Value)); return OS; } diff --git a/llvm/unittests/Support/EndianTest.cpp b/llvm/unittests/Support/EndianTest.cpp index 59281c0ed544..c48b7707b775 100644 --- a/llvm/unittests/Support/EndianTest.cpp +++ b/llvm/unittests/Support/EndianTest.cpp @@ -201,26 +201,26 @@ TEST(Endian, WriteBitAligned) { TEST(Endian, Write) { unsigned char data[5]; - endian::write(data, -1362446643); + endian::write(data, -1362446643, llvm::endianness::big); EXPECT_EQ(data[0], 0xAE); EXPECT_EQ(data[1], 0xCA); EXPECT_EQ(data[2], 0xB6); EXPECT_EQ(data[3], 0xCD); - endian::write(data + 1, - -1362446643); + endian::write(data + 1, -1362446643, + llvm::endianness::big); EXPECT_EQ(data[1], 0xAE); EXPECT_EQ(data[2], 0xCA); EXPECT_EQ(data[3], 0xB6); EXPECT_EQ(data[4], 0xCD); - endian::write(data, - -1362446643); + endian::write(data, -1362446643, + llvm::endianness::little); EXPECT_EQ(data[0], 0xCD); EXPECT_EQ(data[1], 0xB6); EXPECT_EQ(data[2], 0xCA); EXPECT_EQ(data[3], 0xAE); - endian::write(data + 1, - -1362446643); + endian::write(data + 1, -1362446643, + llvm::endianness::little); EXPECT_EQ(data[1], 0xCD); EXPECT_EQ(data[2], 0xB6); EXPECT_EQ(data[3], 0xCA);