[clang] [llvm] [Support] Deprecate one form of support::endian::byte_swap (NFC) (PR #161045)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Sep 27 22:50:33 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-mc
Author: Kazu Hirata (kazutakahirata)
<details>
<summary>Changes</summary>
This is a follow-up to #<!-- -->156140 and #<!-- -->160979, which deprecated one form of
write and read, respectively.
We have two forms of byte_swap:
template <typename value_type>
[[nodiscard]] inline value_type byte_swap(value_type value, endianness endian)
template <typename value_type, endianness endian>
[[nodiscard]] inline value_type byte_swap(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 because the latter is just
forwarded to the former.
---
Full diff: https://github.com/llvm/llvm-project/pull/161045.diff
12 Files Affected:
- (modified) clang/lib/CodeGen/CodeGenPGO.cpp (+2-2)
- (modified) llvm/include/llvm/Bitstream/BitstreamWriter.h (+1-1)
- (modified) llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h (+10-10)
- (modified) llvm/include/llvm/Support/Endian.h (+9-7)
- (modified) llvm/lib/CGData/CodeGenDataWriter.cpp (+2-2)
- (modified) llvm/lib/MC/DXContainerRootSignature.cpp (+2-3)
- (modified) llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp (+6-6)
- (modified) llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp (+1-1)
- (modified) llvm/lib/ProfileData/InstrProf.cpp (+1-1)
- (modified) llvm/lib/ProfileData/InstrProfReader.cpp (+5-5)
- (modified) llvm/tools/llvm-jitlink/llvm-jitlink.cpp (+4-4)
- (modified) llvm/unittests/MC/StringTableBuilderTest.cpp (+2-2)
``````````diff
diff --git a/clang/lib/CodeGen/CodeGenPGO.cpp b/clang/lib/CodeGen/CodeGenPGO.cpp
index 98b30e084b18b..8f095649f87ce 100644
--- a/clang/lib/CodeGen/CodeGenPGO.cpp
+++ b/clang/lib/CodeGen/CodeGenPGO.cpp
@@ -972,7 +972,7 @@ void PGOHash::combine(HashType Type) {
if (Count && Count % NumTypesPerWord == 0) {
using namespace llvm::support;
uint64_t Swapped =
- endian::byte_swap<uint64_t, llvm::endianness::little>(Working);
+ endian::byte_swap<uint64_t>(Working, llvm::endianness::little);
MD5.update(llvm::ArrayRef((uint8_t *)&Swapped, sizeof(Swapped)));
Working = 0;
}
@@ -999,7 +999,7 @@ uint64_t PGOHash::finalize() {
} else {
using namespace llvm::support;
uint64_t Swapped =
- endian::byte_swap<uint64_t, llvm::endianness::little>(Working);
+ endian::byte_swap<uint64_t>(Working, llvm::endianness::little);
MD5.update(llvm::ArrayRef((uint8_t *)&Swapped, sizeof(Swapped)));
}
}
diff --git a/llvm/include/llvm/Bitstream/BitstreamWriter.h b/llvm/include/llvm/Bitstream/BitstreamWriter.h
index 5f53681320ce4..a2938642f824a 100644
--- a/llvm/include/llvm/Bitstream/BitstreamWriter.h
+++ b/llvm/include/llvm/Bitstream/BitstreamWriter.h
@@ -87,7 +87,7 @@ class BitstreamWriter {
void WriteWord(unsigned Value) {
Value =
- support::endian::byte_swap<uint32_t, llvm::endianness::little>(Value);
+ support::endian::byte_swap<uint32_t>(Value, llvm::endianness::little);
Buffer.append(reinterpret_cast<const char *>(&Value),
reinterpret_cast<const char *>(&Value + 1));
}
diff --git a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
index 7d1a85ba528fc..e09958160b9a0 100644
--- a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
+++ b/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
@@ -1215,19 +1215,19 @@ namespace accessors {
/// Return the structural hash associated with the function.
template <class FuncRecordTy, llvm::endianness Endian>
uint64_t getFuncHash(const FuncRecordTy *Record) {
- return support::endian::byte_swap<uint64_t, Endian>(Record->FuncHash);
+ return support::endian::byte_swap<uint64_t>(Record->FuncHash, Endian);
}
/// Return the coverage map data size for the function.
template <class FuncRecordTy, llvm::endianness Endian>
uint64_t getDataSize(const FuncRecordTy *Record) {
- return support::endian::byte_swap<uint32_t, Endian>(Record->DataSize);
+ return support::endian::byte_swap<uint32_t>(Record->DataSize, Endian);
}
/// Return the function lookup key. The value is considered opaque.
template <class FuncRecordTy, llvm::endianness Endian>
uint64_t getFuncNameRef(const FuncRecordTy *Record) {
- return support::endian::byte_swap<uint64_t, Endian>(Record->NameRef);
+ return support::endian::byte_swap<uint64_t>(Record->NameRef, Endian);
}
/// Return the PGO name of the function. Used for formats in which the name is
@@ -1280,14 +1280,14 @@ struct CovMapFunctionRecordV1 {
/// Return function lookup key. The value is consider opaque.
template <llvm::endianness Endian> IntPtrT getFuncNameRef() const {
- return support::endian::byte_swap<IntPtrT, Endian>(NamePtr);
+ return support::endian::byte_swap<IntPtrT>(NamePtr, Endian);
}
/// Return the PGO name of the function.
template <llvm::endianness Endian>
Error getFuncName(InstrProfSymtab &ProfileNames, StringRef &FuncName) const {
IntPtrT NameRef = getFuncNameRef<Endian>();
- uint32_t NameS = support::endian::byte_swap<uint32_t, Endian>(NameSize);
+ uint32_t NameS = support::endian::byte_swap<uint32_t>(NameSize, Endian);
FuncName = ProfileNames.getFuncName(NameRef, NameS);
if (NameS && FuncName.empty())
return make_error<CoverageMapError>(coveragemap_error::malformed,
@@ -1385,7 +1385,7 @@ struct CovMapFunctionRecordV3 {
/// Get the filename set reference.
template <llvm::endianness Endian> uint64_t getFilenamesRef() const {
- return support::endian::byte_swap<uint64_t, Endian>(FilenamesRef);
+ return support::endian::byte_swap<uint64_t>(FilenamesRef, Endian);
}
/// Read the inline coverage mapping. Ignore the buffer parameter, it is for
@@ -1416,19 +1416,19 @@ struct CovMapHeader {
#define COVMAP_HEADER(Type, LLVMType, Name, Init) Type Name;
#include "llvm/ProfileData/InstrProfData.inc"
template <llvm::endianness Endian> uint32_t getNRecords() const {
- return support::endian::byte_swap<uint32_t, Endian>(NRecords);
+ return support::endian::byte_swap<uint32_t>(NRecords, Endian);
}
template <llvm::endianness Endian> uint32_t getFilenamesSize() const {
- return support::endian::byte_swap<uint32_t, Endian>(FilenamesSize);
+ return support::endian::byte_swap<uint32_t>(FilenamesSize, Endian);
}
template <llvm::endianness Endian> uint32_t getCoverageSize() const {
- return support::endian::byte_swap<uint32_t, Endian>(CoverageSize);
+ return support::endian::byte_swap<uint32_t>(CoverageSize, Endian);
}
template <llvm::endianness Endian> uint32_t getVersion() const {
- return support::endian::byte_swap<uint32_t, Endian>(Version);
+ return support::endian::byte_swap<uint32_t>(Version, Endian);
}
};
diff --git a/llvm/include/llvm/Support/Endian.h b/llvm/include/llvm/Support/Endian.h
index 6c86feb78053c..51db225841dbe 100644
--- a/llvm/include/llvm/Support/Endian.h
+++ b/llvm/include/llvm/Support/Endian.h
@@ -49,7 +49,9 @@ template <typename value_type>
/// Swap the bytes of value to match the given endianness.
template <typename value_type, endianness endian>
-[[nodiscard]] inline value_type byte_swap(value_type value) {
+[[nodiscard]]
+LLVM_DEPRECATED("Pass endian as a function argument instead",
+ "byte_swap") inline value_type byte_swap(value_type value) {
return byte_swap(value, endian);
}
@@ -137,8 +139,8 @@ template <typename value_type, endianness endian, std::size_t alignment>
LLVM_ASSUME_ALIGNED(
memory, (detail::PickAlignment<value_type, alignment>::value)),
sizeof(value_type) * 2);
- val[0] = byte_swap<value_type, endian>(val[0]);
- val[1] = byte_swap<value_type, endian>(val[1]);
+ val[0] = byte_swap<value_type>(val[0], endian);
+ val[1] = byte_swap<value_type>(val[1], endian);
// Shift bits from the lower value into place.
make_unsigned_t<value_type> lowerVal = val[0] >> startBit;
@@ -172,8 +174,8 @@ inline void writeAtBitAlignment(void *memory, value_type value,
LLVM_ASSUME_ALIGNED(
memory, (detail::PickAlignment<value_type, alignment>::value)),
sizeof(value_type) * 2);
- val[0] = byte_swap<value_type, endian>(val[0]);
- val[1] = byte_swap<value_type, endian>(val[1]);
+ val[0] = byte_swap<value_type>(val[0], endian);
+ val[1] = byte_swap<value_type>(val[1], endian);
// Mask off any existing bits in the upper part of the lower value that
// we want to replace.
@@ -201,8 +203,8 @@ inline void writeAtBitAlignment(void *memory, value_type value,
val[1] |= upperVal;
// Finally, rewrite values.
- val[0] = byte_swap<value_type, endian>(val[0]);
- val[1] = byte_swap<value_type, endian>(val[1]);
+ val[0] = byte_swap<value_type>(val[0], endian);
+ val[1] = byte_swap<value_type>(val[1], endian);
memcpy(LLVM_ASSUME_ALIGNED(
memory, (detail::PickAlignment<value_type, alignment>::value)),
&val[0], sizeof(value_type) * 2);
diff --git a/llvm/lib/CGData/CodeGenDataWriter.cpp b/llvm/lib/CGData/CodeGenDataWriter.cpp
index 14a8558ba63b7..a2bbceebd0317 100644
--- a/llvm/lib/CGData/CodeGenDataWriter.cpp
+++ b/llvm/lib/CGData/CodeGenDataWriter.cpp
@@ -40,7 +40,7 @@ void CGDataOStream::patch(ArrayRef<CGDataPatchItem> P) {
for (const auto &K : P) {
for (size_t I = 0; I < K.D.size(); ++I) {
uint64_t Bytes =
- endian::byte_swap<uint64_t, llvm::endianness::little>(K.D[I]);
+ endian::byte_swap<uint64_t>(K.D[I], llvm::endianness::little);
Data.replace(K.Pos + I * sizeof(uint64_t), sizeof(uint64_t),
reinterpret_cast<const char *>(&Bytes), sizeof(uint64_t));
}
@@ -52,7 +52,7 @@ void CGDataOStream::patch(ArrayRef<CGDataPatchItem> P) {
for (const auto &K : P) {
for (size_t I = 0; I < K.D.size(); ++I) {
uint64_t Bytes =
- endian::byte_swap<uint64_t, llvm::endianness::little>(K.D[I]);
+ endian::byte_swap<uint64_t>(K.D[I], llvm::endianness::little);
VOStream.pwrite(reinterpret_cast<const char *>(&Bytes),
sizeof(uint64_t), K.Pos + I * sizeof(uint64_t));
}
diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index 2338370d84389..713aa3d8143e8 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -23,9 +23,8 @@ static uint32_t writePlaceholder(raw_svector_ostream &Stream) {
static uint32_t rewriteOffsetToCurrentByte(raw_svector_ostream &Stream,
uint32_t Offset) {
uint32_t ByteOffset = Stream.tell();
- uint32_t Value =
- support::endian::byte_swap<uint32_t, llvm::endianness::little>(
- ByteOffset);
+ uint32_t Value = support::endian::byte_swap<uint32_t>(
+ ByteOffset, llvm::endianness::little);
Stream.pwrite(reinterpret_cast<const char *>(&Value), sizeof(Value), Offset);
return ByteOffset;
}
diff --git a/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp b/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp
index fc2577e6ada5d..075ad8d7aec8b 100644
--- a/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp
+++ b/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp
@@ -949,9 +949,9 @@ loadTestingFormat(StringRef Data, StringRef CompilationDir) {
if (Data.size() < sizeof(uint64_t))
return make_error<CoverageMapError>(coveragemap_error::malformed,
"the size of data is too small");
- auto TestingVersion =
- support::endian::byte_swap<uint64_t, llvm::endianness::little>(
- *reinterpret_cast<const uint64_t *>(Data.data()));
+ auto TestingVersion = support::endian::byte_swap<uint64_t>(
+ *reinterpret_cast<const uint64_t *>(Data.data()),
+ llvm::endianness::little);
Data = Data.substr(sizeof(uint64_t));
// Read the ProfileNames data.
@@ -1274,9 +1274,9 @@ BinaryCoverageReader::create(
std::vector<std::unique_ptr<BinaryCoverageReader>> Readers;
if (ObjectBuffer.getBuffer().size() > sizeof(TestingFormatMagic)) {
- uint64_t Magic =
- support::endian::byte_swap<uint64_t, llvm::endianness::little>(
- *reinterpret_cast<const uint64_t *>(ObjectBuffer.getBufferStart()));
+ uint64_t Magic = support::endian::byte_swap<uint64_t>(
+ *reinterpret_cast<const uint64_t *>(ObjectBuffer.getBufferStart()),
+ llvm::endianness::little);
if (Magic == TestingFormatMagic) {
// This is a special format used for testing.
auto ReaderOrErr =
diff --git a/llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp b/llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp
index 12b1687af69db..3875f01c48528 100644
--- a/llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp
+++ b/llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp
@@ -292,7 +292,7 @@ void CoverageMappingWriter::write(raw_ostream &OS) {
void TestingFormatWriter::write(raw_ostream &OS, TestingFormatVersion Version) {
auto ByteSwap = [](uint64_t N) {
- return support::endian::byte_swap<uint64_t, llvm::endianness::little>(N);
+ return support::endian::byte_swap<uint64_t>(N, llvm::endianness::little);
};
// Output a 64bit magic number.
diff --git a/llvm/lib/ProfileData/InstrProf.cpp b/llvm/lib/ProfileData/InstrProf.cpp
index e1c6315853b3b..3c8e44a18f533 100644
--- a/llvm/lib/ProfileData/InstrProf.cpp
+++ b/llvm/lib/ProfileData/InstrProf.cpp
@@ -292,7 +292,7 @@ void ProfOStream::patch(ArrayRef<PatchItem> P) {
for (const auto &K : P) {
for (int I = 0, E = K.D.size(); I != E; I++) {
uint64_t Bytes =
- endian::byte_swap<uint64_t, llvm::endianness::little>(K.D[I]);
+ endian::byte_swap<uint64_t>(K.D[I], llvm::endianness::little);
Data.replace(K.Pos + I * sizeof(uint64_t), sizeof(uint64_t),
(const char *)&Bytes, sizeof(uint64_t));
}
diff --git a/llvm/lib/ProfileData/InstrProfReader.cpp b/llvm/lib/ProfileData/InstrProfReader.cpp
index 1da92eafa4b4a..d2ae4b5226ff6 100644
--- a/llvm/lib/ProfileData/InstrProfReader.cpp
+++ b/llvm/lib/ProfileData/InstrProfReader.cpp
@@ -1186,10 +1186,10 @@ IndexedInstrProfReader::readSummary(IndexedInstrProf::ProfVersion Version,
if (Version >= IndexedInstrProf::Version4) {
const IndexedInstrProf::Summary *SummaryInLE =
reinterpret_cast<const IndexedInstrProf::Summary *>(Cur);
- uint64_t NFields = endian::byte_swap<uint64_t, llvm::endianness::little>(
- SummaryInLE->NumSummaryFields);
- uint64_t NEntries = endian::byte_swap<uint64_t, llvm::endianness::little>(
- SummaryInLE->NumCutoffEntries);
+ uint64_t NFields = endian::byte_swap<uint64_t>(
+ SummaryInLE->NumSummaryFields, llvm::endianness::little);
+ uint64_t NEntries = endian::byte_swap<uint64_t>(
+ SummaryInLE->NumCutoffEntries, llvm::endianness::little);
uint32_t SummarySize =
IndexedInstrProf::Summary::getSize(NFields, NEntries);
std::unique_ptr<IndexedInstrProf::Summary> SummaryData =
@@ -1198,7 +1198,7 @@ IndexedInstrProfReader::readSummary(IndexedInstrProf::ProfVersion Version,
const uint64_t *Src = reinterpret_cast<const uint64_t *>(SummaryInLE);
uint64_t *Dst = reinterpret_cast<uint64_t *>(SummaryData.get());
for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++)
- Dst[I] = endian::byte_swap<uint64_t, llvm::endianness::little>(Src[I]);
+ Dst[I] = endian::byte_swap<uint64_t>(Src[I], llvm::endianness::little);
SummaryEntryVector DetailedSummary;
for (unsigned I = 0; I < SummaryData->NumCutoffEntries; I++) {
diff --git a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp
index 31bf6a9d2d9c8..e09ddb45da6e9 100644
--- a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp
+++ b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp
@@ -1519,10 +1519,10 @@ class MemoryMatcher {
static StringRef detectStubKind(const Session::MemoryRegionInfo &Stub) {
using namespace support::endian;
- auto Armv7MovWTle = byte_swap<uint32_t, endianness::little>(0xe300c000);
- auto Armv7BxR12le = byte_swap<uint32_t, endianness::little>(0xe12fff1c);
- auto Thumbv7MovWTle = byte_swap<uint32_t, endianness::little>(0x0c00f240);
- auto Thumbv7BxR12le = byte_swap<uint16_t, endianness::little>(0x4760);
+ auto Armv7MovWTle = byte_swap<uint32_t>(0xe300c000, endianness::little);
+ auto Armv7BxR12le = byte_swap<uint32_t>(0xe12fff1c, endianness::little);
+ auto Thumbv7MovWTle = byte_swap<uint32_t>(0x0c00f240, endianness::little);
+ auto Thumbv7BxR12le = byte_swap<uint16_t>(0x4760, endianness::little);
MemoryMatcher M(Stub.getContent());
if (M.matchMask(Thumbv7MovWTle)) {
diff --git a/llvm/unittests/MC/StringTableBuilderTest.cpp b/llvm/unittests/MC/StringTableBuilderTest.cpp
index 05f469a229bf9..44a985be6cfcb 100644
--- a/llvm/unittests/MC/StringTableBuilderTest.cpp
+++ b/llvm/unittests/MC/StringTableBuilderTest.cpp
@@ -58,8 +58,8 @@ TEST(StringTableBuilderTest, BasicWinCOFF) {
std::string Expected;
- ExpectedSize = support::endian::byte_swap<uint32_t, llvm::endianness::little>(
- ExpectedSize);
+ ExpectedSize = support::endian::byte_swap<uint32_t>(ExpectedSize,
+ llvm::endianness::little);
Expected.append((const char*)&ExpectedSize, 4);
Expected += "pygmy hippopotamus";
Expected += '\x00';
``````````
</details>
https://github.com/llvm/llvm-project/pull/161045
More information about the llvm-commits
mailing list