[llvm] [nfc][InstrProfReader]Store header fields in native endianness (PR #92947)

Mingming Liu via llvm-commits llvm-commits at lists.llvm.org
Tue May 21 20:56:14 PDT 2024


================
@@ -1620,13 +1620,12 @@ inline size_t constexpr offsetOf(T1 T2::*Member) {
   return size_t(&(Object.*Member)) - size_t(&Object);
 }
 
+// Read a uint64_t from the specified buffer offset, and swap the bytes in
+// native endianness if necessary.
 static inline uint64_t read(const unsigned char *Buffer, size_t Offset) {
-  return *reinterpret_cast<const uint64_t *>(Buffer + Offset);
-}
-
-uint64_t Header::formatVersion() const {
-  using namespace support;
-  return endian::byte_swap<uint64_t, llvm::endianness::little>(Version);
+  using namespace ::support;
+  uint64_t Data = *reinterpret_cast<const uint64_t *>(Buffer + Offset);
+  return endian::byte_swap<uint64_t, llvm::endianness::little>(Data);
----------------
minglotus-6 wrote:

> May I suggest combining the read access and `byte_swap`?

thanks! While it doesn't matter for reading a couple of `uint64_t`, it's pretty interesting that `memcpy` (called by `endian::read`) does a good job as `reinterpret_cast` (https://gcc.godbolt.org/z/1qWvT7GEG)

https://github.com/llvm/llvm-project/pull/92947


More information about the llvm-commits mailing list