Skip to content

Commit ea0dbee

Browse files
[memprof] Move IndexedMemProfReader::deserialize to IndexedemProfData.cpp (NFC) (#137089)
This patch moves IndexedMemProfReader::deserialize and its subroutines to IndexedMemProfData.cpp, building on: commit 9a8f90d Author: Kazu Hirata <kazu@google.com> Date: Wed Apr 23 15:39:45 2025 -0700 The intent is as follows: - Reduce the size of InstrProfReader.cpp. - Move the subroutines to a separate file because they don't interact with anything else in InstrProfReader.cpp.
1 parent 30fec12 commit ea0dbee

File tree

2 files changed

+124
-123
lines changed

2 files changed

+124
-123
lines changed

llvm/lib/ProfileData/IndexedMemProfData.cpp

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "llvm/ProfileData/InstrProf.h"
14+
#include "llvm/ProfileData/InstrProfReader.h"
1415
#include "llvm/ProfileData/MemProf.h"
1516
#include "llvm/Support/FormatVariadic.h"
1617
#include "llvm/Support/OnDiskHashTable.h"
@@ -297,4 +298,127 @@ Error writeMemProf(ProfOStream &OS, memprof::IndexedMemProfData &MemProfData,
297298
memprof::MaximumSupportedVersion));
298299
}
299300

301+
Error IndexedMemProfReader::deserializeV2(const unsigned char *Start,
302+
const unsigned char *Ptr) {
303+
// The value returned from RecordTableGenerator.Emit.
304+
const uint64_t RecordTableOffset =
305+
support::endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
306+
// The offset in the stream right before invoking
307+
// FrameTableGenerator.Emit.
308+
const uint64_t FramePayloadOffset =
309+
support::endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
310+
// The value returned from FrameTableGenerator.Emit.
311+
const uint64_t FrameTableOffset =
312+
support::endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
313+
314+
// The offset in the stream right before invoking
315+
// CallStackTableGenerator.Emit.
316+
uint64_t CallStackPayloadOffset = 0;
317+
// The value returned from CallStackTableGenerator.Emit.
318+
uint64_t CallStackTableOffset = 0;
319+
if (Version >= memprof::Version2) {
320+
CallStackPayloadOffset =
321+
support::endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
322+
CallStackTableOffset =
323+
support::endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
324+
}
325+
326+
// Read the schema.
327+
auto SchemaOr = memprof::readMemProfSchema(Ptr);
328+
if (!SchemaOr)
329+
return SchemaOr.takeError();
330+
Schema = SchemaOr.get();
331+
332+
// Now initialize the table reader with a pointer into data buffer.
333+
MemProfRecordTable.reset(MemProfRecordHashTable::Create(
334+
/*Buckets=*/Start + RecordTableOffset,
335+
/*Payload=*/Ptr,
336+
/*Base=*/Start, memprof::RecordLookupTrait(Version, Schema)));
337+
338+
// Initialize the frame table reader with the payload and bucket offsets.
339+
MemProfFrameTable.reset(MemProfFrameHashTable::Create(
340+
/*Buckets=*/Start + FrameTableOffset,
341+
/*Payload=*/Start + FramePayloadOffset,
342+
/*Base=*/Start));
343+
344+
if (Version >= memprof::Version2)
345+
MemProfCallStackTable.reset(MemProfCallStackHashTable::Create(
346+
/*Buckets=*/Start + CallStackTableOffset,
347+
/*Payload=*/Start + CallStackPayloadOffset,
348+
/*Base=*/Start));
349+
350+
return Error::success();
351+
}
352+
353+
Error IndexedMemProfReader::deserializeV3(const unsigned char *Start,
354+
const unsigned char *Ptr) {
355+
// The offset in the stream right before invoking
356+
// CallStackTableGenerator.Emit.
357+
const uint64_t CallStackPayloadOffset =
358+
support::endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
359+
// The offset in the stream right before invoking RecordTableGenerator.Emit.
360+
const uint64_t RecordPayloadOffset =
361+
support::endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
362+
// The value returned from RecordTableGenerator.Emit.
363+
const uint64_t RecordTableOffset =
364+
support::endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
365+
366+
// Read the schema.
367+
auto SchemaOr = memprof::readMemProfSchema(Ptr);
368+
if (!SchemaOr)
369+
return SchemaOr.takeError();
370+
Schema = SchemaOr.get();
371+
372+
FrameBase = Ptr;
373+
CallStackBase = Start + CallStackPayloadOffset;
374+
375+
// Compute the number of elements in the radix tree array. Since we use this
376+
// to reserve enough bits in a BitVector, it's totally OK if we overestimate
377+
// this number a little bit because of padding just before the next section.
378+
RadixTreeSize = (RecordPayloadOffset - CallStackPayloadOffset) /
379+
sizeof(memprof::LinearFrameId);
380+
381+
// Now initialize the table reader with a pointer into data buffer.
382+
MemProfRecordTable.reset(MemProfRecordHashTable::Create(
383+
/*Buckets=*/Start + RecordTableOffset,
384+
/*Payload=*/Start + RecordPayloadOffset,
385+
/*Base=*/Start, memprof::RecordLookupTrait(memprof::Version3, Schema)));
386+
387+
return Error::success();
388+
}
389+
390+
Error IndexedMemProfReader::deserialize(const unsigned char *Start,
391+
uint64_t MemProfOffset) {
392+
const unsigned char *Ptr = Start + MemProfOffset;
393+
394+
// Read the MemProf version number.
395+
const uint64_t FirstWord =
396+
support::endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
397+
398+
if (FirstWord == memprof::Version2 || FirstWord == memprof::Version3) {
399+
// Everything is good. We can proceed to deserialize the rest.
400+
Version = static_cast<memprof::IndexedVersion>(FirstWord);
401+
} else {
402+
return make_error<InstrProfError>(
403+
instrprof_error::unsupported_version,
404+
formatv("MemProf version {} not supported; "
405+
"requires version between {} and {}, inclusive",
406+
FirstWord, memprof::MinimumSupportedVersion,
407+
memprof::MaximumSupportedVersion));
408+
}
409+
410+
switch (Version) {
411+
case memprof::Version2:
412+
if (Error E = deserializeV2(Start, Ptr))
413+
return E;
414+
break;
415+
case memprof::Version3:
416+
if (Error E = deserializeV3(Start, Ptr))
417+
return E;
418+
break;
419+
}
420+
421+
return Error::success();
422+
}
423+
300424
} // namespace llvm

llvm/lib/ProfileData/InstrProfReader.cpp

Lines changed: 0 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,129 +1230,6 @@ IndexedInstrProfReader::readSummary(IndexedInstrProf::ProfVersion Version,
12301230
}
12311231
}
12321232

1233-
Error IndexedMemProfReader::deserializeV2(const unsigned char *Start,
1234-
const unsigned char *Ptr) {
1235-
// The value returned from RecordTableGenerator.Emit.
1236-
const uint64_t RecordTableOffset =
1237-
support::endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
1238-
// The offset in the stream right before invoking
1239-
// FrameTableGenerator.Emit.
1240-
const uint64_t FramePayloadOffset =
1241-
support::endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
1242-
// The value returned from FrameTableGenerator.Emit.
1243-
const uint64_t FrameTableOffset =
1244-
support::endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
1245-
1246-
// The offset in the stream right before invoking
1247-
// CallStackTableGenerator.Emit.
1248-
uint64_t CallStackPayloadOffset = 0;
1249-
// The value returned from CallStackTableGenerator.Emit.
1250-
uint64_t CallStackTableOffset = 0;
1251-
if (Version >= memprof::Version2) {
1252-
CallStackPayloadOffset =
1253-
support::endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
1254-
CallStackTableOffset =
1255-
support::endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
1256-
}
1257-
1258-
// Read the schema.
1259-
auto SchemaOr = memprof::readMemProfSchema(Ptr);
1260-
if (!SchemaOr)
1261-
return SchemaOr.takeError();
1262-
Schema = SchemaOr.get();
1263-
1264-
// Now initialize the table reader with a pointer into data buffer.
1265-
MemProfRecordTable.reset(MemProfRecordHashTable::Create(
1266-
/*Buckets=*/Start + RecordTableOffset,
1267-
/*Payload=*/Ptr,
1268-
/*Base=*/Start, memprof::RecordLookupTrait(Version, Schema)));
1269-
1270-
// Initialize the frame table reader with the payload and bucket offsets.
1271-
MemProfFrameTable.reset(MemProfFrameHashTable::Create(
1272-
/*Buckets=*/Start + FrameTableOffset,
1273-
/*Payload=*/Start + FramePayloadOffset,
1274-
/*Base=*/Start));
1275-
1276-
if (Version >= memprof::Version2)
1277-
MemProfCallStackTable.reset(MemProfCallStackHashTable::Create(
1278-
/*Buckets=*/Start + CallStackTableOffset,
1279-
/*Payload=*/Start + CallStackPayloadOffset,
1280-
/*Base=*/Start));
1281-
1282-
return Error::success();
1283-
}
1284-
1285-
Error IndexedMemProfReader::deserializeV3(const unsigned char *Start,
1286-
const unsigned char *Ptr) {
1287-
// The offset in the stream right before invoking
1288-
// CallStackTableGenerator.Emit.
1289-
const uint64_t CallStackPayloadOffset =
1290-
support::endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
1291-
// The offset in the stream right before invoking RecordTableGenerator.Emit.
1292-
const uint64_t RecordPayloadOffset =
1293-
support::endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
1294-
// The value returned from RecordTableGenerator.Emit.
1295-
const uint64_t RecordTableOffset =
1296-
support::endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
1297-
1298-
// Read the schema.
1299-
auto SchemaOr = memprof::readMemProfSchema(Ptr);
1300-
if (!SchemaOr)
1301-
return SchemaOr.takeError();
1302-
Schema = SchemaOr.get();
1303-
1304-
FrameBase = Ptr;
1305-
CallStackBase = Start + CallStackPayloadOffset;
1306-
1307-
// Compute the number of elements in the radix tree array. Since we use this
1308-
// to reserve enough bits in a BitVector, it's totally OK if we overestimate
1309-
// this number a little bit because of padding just before the next section.
1310-
RadixTreeSize = (RecordPayloadOffset - CallStackPayloadOffset) /
1311-
sizeof(memprof::LinearFrameId);
1312-
1313-
// Now initialize the table reader with a pointer into data buffer.
1314-
MemProfRecordTable.reset(MemProfRecordHashTable::Create(
1315-
/*Buckets=*/Start + RecordTableOffset,
1316-
/*Payload=*/Start + RecordPayloadOffset,
1317-
/*Base=*/Start, memprof::RecordLookupTrait(memprof::Version3, Schema)));
1318-
1319-
return Error::success();
1320-
}
1321-
1322-
Error IndexedMemProfReader::deserialize(const unsigned char *Start,
1323-
uint64_t MemProfOffset) {
1324-
const unsigned char *Ptr = Start + MemProfOffset;
1325-
1326-
// Read the MemProf version number.
1327-
const uint64_t FirstWord =
1328-
support::endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
1329-
1330-
if (FirstWord == memprof::Version2 || FirstWord == memprof::Version3) {
1331-
// Everything is good. We can proceed to deserialize the rest.
1332-
Version = static_cast<memprof::IndexedVersion>(FirstWord);
1333-
} else {
1334-
return make_error<InstrProfError>(
1335-
instrprof_error::unsupported_version,
1336-
formatv("MemProf version {} not supported; "
1337-
"requires version between {} and {}, inclusive",
1338-
FirstWord, memprof::MinimumSupportedVersion,
1339-
memprof::MaximumSupportedVersion));
1340-
}
1341-
1342-
switch (Version) {
1343-
case memprof::Version2:
1344-
if (Error E = deserializeV2(Start, Ptr))
1345-
return E;
1346-
break;
1347-
case memprof::Version3:
1348-
if (Error E = deserializeV3(Start, Ptr))
1349-
return E;
1350-
break;
1351-
}
1352-
1353-
return Error::success();
1354-
}
1355-
13561233
Error IndexedInstrProfReader::readHeader() {
13571234
using namespace support;
13581235

0 commit comments

Comments
 (0)