Skip to content

Commit 0ff8833

Browse files
committed
Addres comments
1 parent b822ee3 commit 0ff8833

File tree

2 files changed

+23
-40
lines changed

2 files changed

+23
-40
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ TEST(MemProf, F16EncodeDecode) {
2121

2222
if (TestCase >= MaxRepresentable) {
2323
EXPECT_EQ(Decoded, MaxRepresentable);
24-
} else if (TestCase == 0) {
24+
} else if (TestCase <= MaxMantissa) {
2525
EXPECT_EQ(Decoded, TestCase);
2626
} else {
2727
// The decoded value should be close to the original value.

llvm/lib/ProfileData/MemProfReader.cpp

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

137137
llvm::SmallVector<std::pair<uint64_t, MemInfoBlock>>
138-
readMemInfoBlocksV4(const char *Ptr) {
139-
using namespace support;
140-
141-
const uint64_t NumItemsToRead =
142-
endian::readNext<uint64_t, llvm::endianness::little, unaligned>(Ptr);
143-
144-
llvm::SmallVector<std::pair<uint64_t, MemInfoBlock>> Items;
145-
for (uint64_t I = 0; I < NumItemsToRead; I++) {
146-
const uint64_t Id =
147-
endian::readNext<uint64_t, llvm::endianness::little, unaligned>(Ptr);
148-
// We cheat a bit here and remove the const from cast to set the
149-
// Histogram Pointer to newly allocated buffer.
150-
MemInfoBlock MIB = *reinterpret_cast<const MemInfoBlock *>(Ptr);
151-
152-
// Only increment by size of MIB since readNext implicitly increments.
153-
Ptr += sizeof(MemInfoBlock);
154-
155-
if (MIB.AccessHistogramSize > 0) {
156-
MIB.AccessHistogram =
157-
(uintptr_t)malloc(MIB.AccessHistogramSize * sizeof(uint64_t));
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);
163-
}
164-
Items.push_back({Id, MIB});
165-
}
166-
return Items;
167-
}
168-
169-
llvm::SmallVector<std::pair<uint64_t, MemInfoBlock>>
170-
readMemInfoBlocksV5(const char *Ptr) {
138+
readMemInfoBlocksCommon(const char *Ptr, bool IsHistogramEncoded = false) {
171139
using namespace support;
172140

173141
const uint64_t NumItemsToRead =
@@ -186,19 +154,34 @@ readMemInfoBlocksV5(const char *Ptr) {
186154
MIB.AccessHistogram =
187155
(uintptr_t)malloc(MIB.AccessHistogramSize * sizeof(uint64_t));
188156
for (uint64_t J = 0; J < MIB.AccessHistogramSize; J++) {
189-
// The on-disk format for V5 uses uint16_t which is then decoded to
190-
// uint64_t.
191-
const uint16_t Val =
192-
endian::readNext<uint16_t, llvm::endianness::little, unaligned>(
193-
Ptr);
194-
((uint64_t *)MIB.AccessHistogram)[J] = decodeHistogramCount(Val);
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+
}
195168
}
196169
}
197170
Items.push_back({Id, MIB});
198171
}
199172
return Items;
200173
}
201174

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+
202185
CallStackMap readStackInfo(const char *Ptr) {
203186
using namespace support;
204187

0 commit comments

Comments
 (0)