From d66f282ff103f1aeed7a73aa6cba3b0d257c48d6 Mon Sep 17 00:00:00 2001 From: Snehasish Kumar Date: Thu, 24 Jul 2025 06:10:55 +0000 Subject: [PATCH 1/3] Write out raw profile bytes in little endian. Instead of writing out in native endian, write out the raw profile bytes in little endian. Also update the MIB data in little endian. Also clean up some lint and unused includes in rawprofile.cpp. --- .../lib/memprof/memprof_rawprofile.cpp | 17 +++++---- llvm/lib/ProfileData/MemProfReader.cpp | 35 +++++++++++++++++-- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/compiler-rt/lib/memprof/memprof_rawprofile.cpp b/compiler-rt/lib/memprof/memprof_rawprofile.cpp index f909d78f5f36a..fbcfee3d655eb 100644 --- a/compiler-rt/lib/memprof/memprof_rawprofile.cpp +++ b/compiler-rt/lib/memprof/memprof_rawprofile.cpp @@ -7,10 +7,7 @@ #include "sanitizer_common/sanitizer_allocator_internal.h" #include "sanitizer_common/sanitizer_array_ref.h" #include "sanitizer_common/sanitizer_common.h" -#include "sanitizer_common/sanitizer_linux.h" -#include "sanitizer_common/sanitizer_procmaps.h" #include "sanitizer_common/sanitizer_stackdepot.h" -#include "sanitizer_common/sanitizer_stackdepotbase.h" #include "sanitizer_common/sanitizer_stacktrace.h" #include "sanitizer_common/sanitizer_vector.h" @@ -23,7 +20,16 @@ using ::llvm::memprof::encodeHistogramCount; namespace { template char *WriteBytes(const T &Pod, char *Buffer) { - *(T *)Buffer = Pod; + static_assert(is_trivially_copyable::value, "T must be POD"); + const uint8_t *Src = reinterpret_cast(&Pod); + for (size_t I = 0; I < sizeof(T); ++I) { + Buffer[I] = Src[I]; + } +#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + for (size_t i = 0; i < sizeof(T) / 2; ++i) { + std::swap(buffer[i], buffer[sizeof(T) - 1 - i]); + } +#endif return Buffer + sizeof(T); } @@ -33,7 +39,6 @@ void RecordStackId(const uptr Key, UNUSED LockedMemInfoBlock *const &MIB, auto *StackIds = reinterpret_cast *>(Arg); StackIds->PushBack(Key); } -} // namespace u64 SegmentSizeBytes(ArrayRef Modules) { u64 NumSegmentsToRecord = 0; @@ -184,6 +189,7 @@ void SerializeMIBInfoToBuffer(MIBMapTy &MIBMap, const Vector &StackIds, CHECK(ExpectedNumBytes >= static_cast(Ptr - Buffer) && "Expected num bytes != actual bytes written"); } +} // namespace // Format // ---------- Header @@ -288,5 +294,4 @@ u64 SerializeToRawProfile(MIBMapTy &MIBMap, ArrayRef Modules, return TotalSizeBytes; } - } // namespace __memprof diff --git a/llvm/lib/ProfileData/MemProfReader.cpp b/llvm/lib/ProfileData/MemProfReader.cpp index 9db699712d6f3..3fc0dbfd8e69d 100644 --- a/llvm/lib/ProfileData/MemProfReader.cpp +++ b/llvm/lib/ProfileData/MemProfReader.cpp @@ -146,8 +146,39 @@ readMemInfoBlocksCommon(const char *Ptr, bool IsHistogramEncoded = false) { const uint64_t Id = endian::readNext(Ptr); - MemInfoBlock MIB = *reinterpret_cast(Ptr); - Ptr += sizeof(MemInfoBlock); + MemInfoBlock MIB; +#define READ_MIB_FIELD(FIELD) \ + MIB.FIELD = endian::readNext(Ptr) + + READ_MIB_FIELD(AllocCount); + READ_MIB_FIELD(TotalAccessCount); + READ_MIB_FIELD(MinAccessCount); + READ_MIB_FIELD(MaxAccessCount); + READ_MIB_FIELD(TotalSize); + READ_MIB_FIELD(MinSize); + READ_MIB_FIELD(MaxSize); + READ_MIB_FIELD(AllocTimestamp); + READ_MIB_FIELD(DeallocTimestamp); + READ_MIB_FIELD(TotalLifetime); + READ_MIB_FIELD(MinLifetime); + READ_MIB_FIELD(MaxLifetime); + READ_MIB_FIELD(AllocCpuId); + READ_MIB_FIELD(DeallocCpuId); + READ_MIB_FIELD(NumMigratedCpu); + READ_MIB_FIELD(NumLifetimeOverlaps); + READ_MIB_FIELD(NumSameAllocCpu); + READ_MIB_FIELD(NumSameDeallocCpu); + READ_MIB_FIELD(DataTypeId); + READ_MIB_FIELD(TotalAccessDensity); + READ_MIB_FIELD(MinAccessDensity); + READ_MIB_FIELD(MaxAccessDensity); + READ_MIB_FIELD(TotalLifetimeAccessDensity); + READ_MIB_FIELD(MinLifetimeAccessDensity); + READ_MIB_FIELD(MaxLifetimeAccessDensity); + READ_MIB_FIELD(AccessHistogramSize); + READ_MIB_FIELD(AccessHistogram); +#undef READ_MIB_FIELD if (MIB.AccessHistogramSize > 0) { // The in-memory representation uses uint64_t for histogram entries. From 69ccfd6931c806d86165c1cf3006c259c0c48066 Mon Sep 17 00:00:00 2001 From: Snehasish Kumar Date: Wed, 30 Jul 2025 00:35:20 +0000 Subject: [PATCH 2/3] Address comment --- compiler-rt/lib/memprof/memprof_rawprofile.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler-rt/lib/memprof/memprof_rawprofile.cpp b/compiler-rt/lib/memprof/memprof_rawprofile.cpp index fbcfee3d655eb..bf04afa679c9c 100644 --- a/compiler-rt/lib/memprof/memprof_rawprofile.cpp +++ b/compiler-rt/lib/memprof/memprof_rawprofile.cpp @@ -22,13 +22,13 @@ namespace { template char *WriteBytes(const T &Pod, char *Buffer) { static_assert(is_trivially_copyable::value, "T must be POD"); const uint8_t *Src = reinterpret_cast(&Pod); - for (size_t I = 0; I < sizeof(T); ++I) { - Buffer[I] = Src[I]; - } + + for (size_t I = 0; I < sizeof(T); ++I) #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - for (size_t i = 0; i < sizeof(T) / 2; ++i) { - std::swap(buffer[i], buffer[sizeof(T) - 1 - i]); - } + // Reverse byte order since reader is little-endian. + Buffer[I] = Src[sizeof(T) - 1 - I]; +#else + Buffer[I] = Src[I]; #endif return Buffer + sizeof(T); } From a4b7a903578a776ec6c6fa38baaa6a3eb7a16756 Mon Sep 17 00:00:00 2001 From: Snehasish Kumar Date: Wed, 30 Jul 2025 00:40:40 +0000 Subject: [PATCH 3/3] Format. --- compiler-rt/lib/memprof/memprof_rawprofile.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler-rt/lib/memprof/memprof_rawprofile.cpp b/compiler-rt/lib/memprof/memprof_rawprofile.cpp index bf04afa679c9c..f579e12b15d0a 100644 --- a/compiler-rt/lib/memprof/memprof_rawprofile.cpp +++ b/compiler-rt/lib/memprof/memprof_rawprofile.cpp @@ -25,7 +25,7 @@ template char *WriteBytes(const T &Pod, char *Buffer) { for (size_t I = 0; I < sizeof(T); ++I) #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - // Reverse byte order since reader is little-endian. + // Reverse byte order since reader is little-endian. Buffer[I] = Src[sizeof(T) - 1 - I]; #else Buffer[I] = Src[I];