Skip to content

Commit 24f9482

Browse files
authored
Revert "[MemProf] Change histogram storage from uint64_t to uint16_t" (#151382)
Reverts #147854 Test failure when building with gcc. https://lab.llvm.org/buildbot/#/builders/174/builds/21989
1 parent 96b4425 commit 24f9482

30 files changed

+32
-296
lines changed

compiler-rt/include/profile/MemProfData.inc

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@
3333
(uint64_t)'o' << 24 | (uint64_t)'f' << 16 | (uint64_t)'r' << 8 | (uint64_t)129)
3434

3535
// The version number of the raw binary format.
36-
#define MEMPROF_RAW_VERSION 5ULL
36+
#define MEMPROF_RAW_VERSION 4ULL
3737

3838
// Currently supported versions.
39-
#define MEMPROF_RAW_SUPPORTED_VERSIONS {3ULL, 4ULL, 5ULL}
39+
#define MEMPROF_RAW_SUPPORTED_VERSIONS \
40+
{ 3ULL, 4ULL }
4041

4142
#define MEMPROF_V3_MIB_SIZE 132ULL;
4243

@@ -228,41 +229,6 @@ void Merge(const MemInfoBlock &newMIB) {
228229
} __attribute__((__packed__));
229230
#endif
230231

231-
constexpr int MantissaBits = 12;
232-
constexpr int ExponentBits = 4;
233-
constexpr uint16_t MaxMantissa = (1U << MantissaBits) - 1;
234-
constexpr uint16_t MaxExponent = (1U << ExponentBits) - 1;
235-
constexpr uint64_t MaxRepresentableValue = static_cast<uint64_t>(MaxMantissa)
236-
<< MaxExponent;
237-
238-
// Encodes a 64-bit unsigned integer into a 16-bit scaled integer format.
239-
inline uint16_t encodeHistogramCount(uint64_t Count) {
240-
if (Count == 0)
241-
return 0;
242-
243-
if (Count > MaxRepresentableValue)
244-
Count = MaxRepresentableValue;
245-
246-
if (Count <= MaxMantissa)
247-
return Count;
248-
249-
uint64_t M = Count;
250-
uint16_t E = 0;
251-
while (M > MaxMantissa) {
252-
M = (M + 1) >> 1;
253-
E++;
254-
}
255-
return (E << MantissaBits) | static_cast<uint16_t>(M);
256-
}
257-
258-
// Decodes a 16-bit scaled integer and returns the
259-
// decoded 64-bit unsigned integer.
260-
inline uint64_t decodeHistogramCount(uint16_t EncodedValue) {
261-
const uint16_t E = EncodedValue >> MantissaBits;
262-
const uint16_t M = EncodedValue & MaxMantissa;
263-
return static_cast<uint64_t>(M) << E;
264-
}
265-
266232
} // namespace memprof
267233
} // namespace llvm
268234

compiler-rt/lib/memprof/memprof_rawprofile.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ using ::__sanitizer::Vector;
1919
using ::llvm::memprof::MemInfoBlock;
2020
using SegmentEntry = ::llvm::memprof::SegmentEntry;
2121
using Header = ::llvm::memprof::Header;
22-
using ::llvm::memprof::encodeHistogramCount;
2322

2423
namespace {
2524
template <class T> char *WriteBytes(const T &Pod, char *Buffer) {
@@ -170,15 +169,13 @@ void SerializeMIBInfoToBuffer(MIBMapTy &MIBMap, const Vector<u64> &StackIds,
170169
// FIXME: We unnecessarily serialize the AccessHistogram pointer. Adding a
171170
// serialization schema will fix this issue. See also FIXME in
172171
// deserialization.
173-
auto &MIB = (*h)->mib;
174-
Ptr = WriteBytes(MIB, Ptr);
175-
for (u64 j = 0; j < MIB.AccessHistogramSize; ++j) {
176-
u16 HistogramEntry =
177-
encodeHistogramCount(((u64 *)(MIB.AccessHistogram))[j]);
172+
Ptr = WriteBytes((*h)->mib, Ptr);
173+
for (u64 j = 0; j < (*h)->mib.AccessHistogramSize; ++j) {
174+
u64 HistogramEntry = ((u64 *)((*h)->mib.AccessHistogram))[j];
178175
Ptr = WriteBytes(HistogramEntry, Ptr);
179176
}
180-
if (MIB.AccessHistogramSize > 0) {
181-
InternalFree((void *)MIB.AccessHistogram);
177+
if ((*h)->mib.AccessHistogramSize > 0) {
178+
InternalFree((void *)((*h)->mib.AccessHistogram));
182179
}
183180
}
184181
CHECK(ExpectedNumBytes >= static_cast<u64>(Ptr - Buffer) &&
@@ -252,7 +249,7 @@ u64 SerializeToRawProfile(MIBMapTy &MIBMap, ArrayRef<LoadedModule> Modules,
252249
},
253250
reinterpret_cast<void *>(&TotalAccessHistogramEntries));
254251
const u64 NumHistogramBytes =
255-
RoundUpTo(TotalAccessHistogramEntries * sizeof(uint16_t), 8);
252+
RoundUpTo(TotalAccessHistogramEntries * sizeof(uint64_t), 8);
256253

257254
const u64 NumStackBytes = RoundUpTo(StackSizeBytes(StackIds), 8);
258255

compiler-rt/lib/memprof/tests/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ set(MEMPROF_SOURCES
2626
../memprof_rawprofile.cpp)
2727

2828
set(MEMPROF_UNITTESTS
29-
histogram_encoding.cpp
3029
rawprofile.cpp
3130
driver.cpp)
3231

compiler-rt/lib/memprof/tests/histogram_encoding.cpp

Lines changed: 0 additions & 35 deletions
This file was deleted.

compiler-rt/test/memprof/TestCases/memprof_histogram_uint8.cpp

Lines changed: 0 additions & 37 deletions
This file was deleted.

llvm/include/llvm/ProfileData/MemProfData.inc

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@
3333
(uint64_t)'o' << 24 | (uint64_t)'f' << 16 | (uint64_t)'r' << 8 | (uint64_t)129)
3434

3535
// The version number of the raw binary format.
36-
#define MEMPROF_RAW_VERSION 5ULL
36+
#define MEMPROF_RAW_VERSION 4ULL
3737

3838
// Currently supported versions.
39-
#define MEMPROF_RAW_SUPPORTED_VERSIONS {3ULL, 4ULL, 5ULL}
39+
#define MEMPROF_RAW_SUPPORTED_VERSIONS \
40+
{ 3ULL, 4ULL }
4041

4142
#define MEMPROF_V3_MIB_SIZE 132ULL;
4243

@@ -228,41 +229,6 @@ void Merge(const MemInfoBlock &newMIB) {
228229
} __attribute__((__packed__));
229230
#endif
230231

231-
constexpr int MantissaBits = 12;
232-
constexpr int ExponentBits = 4;
233-
constexpr uint16_t MaxMantissa = (1U << MantissaBits) - 1;
234-
constexpr uint16_t MaxExponent = (1U << ExponentBits) - 1;
235-
constexpr uint64_t MaxRepresentableValue = static_cast<uint64_t>(MaxMantissa)
236-
<< MaxExponent;
237-
238-
// Encodes a 64-bit unsigned integer into a 16-bit scaled integer format.
239-
inline uint16_t encodeHistogramCount(uint64_t Count) {
240-
if (Count == 0)
241-
return 0;
242-
243-
if (Count > MaxRepresentableValue)
244-
Count = MaxRepresentableValue;
245-
246-
if (Count <= MaxMantissa)
247-
return Count;
248-
249-
uint64_t M = Count;
250-
uint16_t E = 0;
251-
while (M > MaxMantissa) {
252-
M = (M + 1) >> 1;
253-
E++;
254-
}
255-
return (E << MantissaBits) | static_cast<uint16_t>(M);
256-
}
257-
258-
// Decodes a 16-bit scaled integer and returns the
259-
// decoded 64-bit unsigned integer.
260-
inline uint64_t decodeHistogramCount(uint16_t EncodedValue) {
261-
const uint16_t E = EncodedValue >> MantissaBits;
262-
const uint16_t M = EncodedValue & MaxMantissa;
263-
return static_cast<uint64_t>(M) << E;
264-
}
265-
266232
} // namespace memprof
267233
} // namespace llvm
268234

llvm/lib/ProfileData/MemProfReader.cpp

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ readMemInfoBlocksV3(const char *Ptr) {
135135
}
136136

137137
llvm::SmallVector<std::pair<uint64_t, MemInfoBlock>>
138-
readMemInfoBlocksCommon(const char *Ptr, bool IsHistogramEncoded = false) {
138+
readMemInfoBlocksV4(const char *Ptr) {
139139
using namespace support;
140140

141141
const uint64_t NumItemsToRead =
@@ -145,43 +145,27 @@ readMemInfoBlocksCommon(const char *Ptr, bool IsHistogramEncoded = false) {
145145
for (uint64_t I = 0; I < NumItemsToRead; I++) {
146146
const uint64_t Id =
147147
endian::readNext<uint64_t, llvm::endianness::little, unaligned>(Ptr);
148-
148+
// We cheat a bit here and remove the const from cast to set the
149+
// Histogram Pointer to newly allocated buffer.
149150
MemInfoBlock MIB = *reinterpret_cast<const MemInfoBlock *>(Ptr);
151+
152+
// Only increment by size of MIB since readNext implicitly increments.
150153
Ptr += sizeof(MemInfoBlock);
151154

152155
if (MIB.AccessHistogramSize > 0) {
153-
// The in-memory representation uses uint64_t for histogram entries.
154156
MIB.AccessHistogram =
155157
(uintptr_t)malloc(MIB.AccessHistogramSize * sizeof(uint64_t));
156-
for (uint64_t J = 0; J < MIB.AccessHistogramSize; J++) {
157-
if (!IsHistogramEncoded) {
158-
((uint64_t *)MIB.AccessHistogram)[J] =
159-
endian::readNext<uint64_t, llvm::endianness::little, unaligned>(
160-
Ptr);
161-
} else {
162-
// The encoded on-disk format (V5 onwards) uses uint16_t.
163-
const uint16_t Val =
164-
endian::readNext<uint16_t, llvm::endianness::little, unaligned>(
165-
Ptr);
166-
((uint64_t *)MIB.AccessHistogram)[J] = decodeHistogramCount(Val);
167-
}
168-
}
158+
}
159+
160+
for (uint64_t J = 0; J < MIB.AccessHistogramSize; J++) {
161+
((uint64_t *)MIB.AccessHistogram)[J] =
162+
endian::readNext<uint64_t, llvm::endianness::little, unaligned>(Ptr);
169163
}
170164
Items.push_back({Id, MIB});
171165
}
172166
return Items;
173167
}
174168

175-
llvm::SmallVector<std::pair<uint64_t, MemInfoBlock>>
176-
readMemInfoBlocksV4(const char *Ptr) {
177-
return readMemInfoBlocksCommon(Ptr);
178-
}
179-
180-
llvm::SmallVector<std::pair<uint64_t, MemInfoBlock>>
181-
readMemInfoBlocksV5(const char *Ptr) {
182-
return readMemInfoBlocksCommon(Ptr, /*IsHistogramEncoded=*/true);
183-
}
184-
185169
CallStackMap readStackInfo(const char *Ptr) {
186170
using namespace support;
187171

@@ -674,8 +658,6 @@ RawMemProfReader::readMemInfoBlocks(const char *Ptr) {
674658
return readMemInfoBlocksV3(Ptr);
675659
if (MemprofRawVersion == 4ULL)
676660
return readMemInfoBlocksV4(Ptr);
677-
if (MemprofRawVersion == 5ULL)
678-
return readMemInfoBlocksV5(Ptr);
679661
llvm_unreachable(
680662
"Panic: Unsupported version number when reading MemInfoBlocks");
681663
}
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)