@@ -214,23 +214,13 @@ static Error writeMemProfV2(ProfOStream &OS,
214214 return Error::success ();
215215}
216216
217- // Write out MemProf Version3 as follows:
218- // uint64_t Version
219- // uint64_t CallStackPayloadOffset = Offset for the call stack payload
220- // uint64_t RecordPayloadOffset = Offset for the record payload
221- // uint64_t RecordTableOffset = RecordTableGenerator.Emit
222- // uint64_t Num schema entries
223- // uint64_t Schema entry 0
224- // uint64_t Schema entry 1
225- // ....
226- // uint64_t Schema entry N - 1
227- // Frames serialized one after another
228- // Call stacks encoded as a radix tree
229- // OnDiskChainedHashTable MemProfRecordData
230- static Error writeMemProfV3 (ProfOStream &OS,
231- memprof::IndexedMemProfData &MemProfData,
232- bool MemProfFullSchema) {
233- OS.write (memprof::Version3);
217+ static Error writeMemProfRadixTreeBased (
218+ ProfOStream &OS, memprof::IndexedMemProfData &MemProfData,
219+ memprof::IndexedVersion Version, bool MemProfFullSchema) {
220+ assert ((Version == memprof::Version3 || Version == memprof::Version4) &&
221+ " Unsupported version for radix tree format" );
222+
223+ OS.write (Version); // Write the specific version (V3 or V4)
234224 uint64_t HeaderUpdatePos = OS.tell ();
235225 OS.write (0ULL ); // Reserve space for the memprof call stack payload offset.
236226 OS.write (0ULL ); // Reserve space for the memprof record payload offset.
@@ -258,13 +248,11 @@ static Error writeMemProfV3(ProfOStream &OS,
258248 NumElements);
259249
260250 uint64_t RecordPayloadOffset = OS.tell ();
261- uint64_t RecordTableOffset =
262- writeMemProfRecords (OS, MemProfData.Records , &Schema, memprof::Version3,
263- &MemProfCallStackIndexes);
251+ uint64_t RecordTableOffset = writeMemProfRecords (
252+ OS, MemProfData.Records , &Schema, Version, &MemProfCallStackIndexes);
264253
265- // IndexedMemProfReader::deserializeV3 computes the number of elements in the
266- // call stack array from the difference between CallStackPayloadOffset and
267- // RecordPayloadOffset. Verify that the computation works.
254+ // Verify that the computation for the number of elements in the call stack
255+ // array works.
268256 assert (CallStackPayloadOffset +
269257 NumElements * sizeof (memprof::LinearFrameId) ==
270258 RecordPayloadOffset);
@@ -279,6 +267,22 @@ static Error writeMemProfV3(ProfOStream &OS,
279267 return Error::success ();
280268}
281269
270+ // Write out MemProf Version3
271+ static Error writeMemProfV3 (ProfOStream &OS,
272+ memprof::IndexedMemProfData &MemProfData,
273+ bool MemProfFullSchema) {
274+ return writeMemProfRadixTreeBased (OS, MemProfData, memprof::Version3,
275+ MemProfFullSchema);
276+ }
277+
278+ // Write out MemProf Version4
279+ static Error writeMemProfV4 (ProfOStream &OS,
280+ memprof::IndexedMemProfData &MemProfData,
281+ bool MemProfFullSchema) {
282+ return writeMemProfRadixTreeBased (OS, MemProfData, memprof::Version4,
283+ MemProfFullSchema);
284+ }
285+
282286// Write out the MemProf data in a requested version.
283287Error writeMemProf (ProfOStream &OS, memprof::IndexedMemProfData &MemProfData,
284288 memprof::IndexedVersion MemProfVersionRequested,
@@ -288,6 +292,8 @@ Error writeMemProf(ProfOStream &OS, memprof::IndexedMemProfData &MemProfData,
288292 return writeMemProfV2 (OS, MemProfData, MemProfFullSchema);
289293 case memprof::Version3:
290294 return writeMemProfV3 (OS, MemProfData, MemProfFullSchema);
295+ case memprof::Version4:
296+ return writeMemProfV4 (OS, MemProfData, MemProfFullSchema);
291297 }
292298
293299 return make_error<InstrProfError>(
@@ -350,8 +356,8 @@ Error IndexedMemProfReader::deserializeV2(const unsigned char *Start,
350356 return Error::success ();
351357}
352358
353- Error IndexedMemProfReader::deserializeV3 ( const unsigned char *Start,
354- const unsigned char *Ptr) {
359+ Error IndexedMemProfReader::deserializeRadixTreeBased (
360+ const unsigned char *Start, const unsigned char *Ptr) {
355361 // The offset in the stream right before invoking
356362 // CallStackTableGenerator.Emit.
357363 const uint64_t CallStackPayloadOffset =
@@ -382,7 +388,7 @@ Error IndexedMemProfReader::deserializeV3(const unsigned char *Start,
382388 MemProfRecordTable.reset (MemProfRecordHashTable::Create (
383389 /* Buckets=*/ Start + RecordTableOffset,
384390 /* Payload=*/ Start + RecordPayloadOffset,
385- /* Base=*/ Start, memprof::RecordLookupTrait (memprof::Version3 , Schema)));
391+ /* Base=*/ Start, memprof::RecordLookupTrait (Version , Schema)));
386392
387393 return Error::success ();
388394}
@@ -395,8 +401,10 @@ Error IndexedMemProfReader::deserialize(const unsigned char *Start,
395401 const uint64_t FirstWord =
396402 support::endian::readNext<uint64_t , llvm::endianness::little>(Ptr);
397403
398- if (FirstWord == memprof::Version2 || FirstWord == memprof::Version3) {
399- // Everything is good. We can proceed to deserialize the rest.
404+ // Check if the version is supported
405+ if (FirstWord >= memprof::MinimumSupportedVersion &&
406+ FirstWord <= memprof::MaximumSupportedVersion) {
407+ // Everything is good. We can proceed to deserialize the rest.
400408 Version = static_cast <memprof::IndexedVersion>(FirstWord);
401409 } else {
402410 return make_error<InstrProfError>(
@@ -413,12 +421,13 @@ Error IndexedMemProfReader::deserialize(const unsigned char *Start,
413421 return E;
414422 break ;
415423 case memprof::Version3:
416- if (Error E = deserializeV3 (Start, Ptr))
424+ case memprof::Version4:
425+ // V3 and V4 share the same high-level structure (radix tree, linear IDs).
426+ if (Error E = deserializeRadixTreeBased (Start, Ptr))
417427 return E;
418428 break ;
419429 }
420430
421431 return Error::success ();
422432}
423-
424433} // namespace llvm
0 commit comments