@@ -214,23 +214,13 @@ static Error writeMemProfV2(ProfOStream &OS,
214
214
return Error::success ();
215
215
}
216
216
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)
234
224
uint64_t HeaderUpdatePos = OS.tell ();
235
225
OS.write (0ULL ); // Reserve space for the memprof call stack payload offset.
236
226
OS.write (0ULL ); // Reserve space for the memprof record payload offset.
@@ -258,13 +248,11 @@ static Error writeMemProfV3(ProfOStream &OS,
258
248
NumElements);
259
249
260
250
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);
264
253
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.
268
256
assert (CallStackPayloadOffset +
269
257
NumElements * sizeof (memprof::LinearFrameId) ==
270
258
RecordPayloadOffset);
@@ -279,6 +267,22 @@ static Error writeMemProfV3(ProfOStream &OS,
279
267
return Error::success ();
280
268
}
281
269
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
+
282
286
// Write out the MemProf data in a requested version.
283
287
Error writeMemProf (ProfOStream &OS, memprof::IndexedMemProfData &MemProfData,
284
288
memprof::IndexedVersion MemProfVersionRequested,
@@ -288,6 +292,8 @@ Error writeMemProf(ProfOStream &OS, memprof::IndexedMemProfData &MemProfData,
288
292
return writeMemProfV2 (OS, MemProfData, MemProfFullSchema);
289
293
case memprof::Version3:
290
294
return writeMemProfV3 (OS, MemProfData, MemProfFullSchema);
295
+ case memprof::Version4:
296
+ return writeMemProfV4 (OS, MemProfData, MemProfFullSchema);
291
297
}
292
298
293
299
return make_error<InstrProfError>(
@@ -350,8 +356,8 @@ Error IndexedMemProfReader::deserializeV2(const unsigned char *Start,
350
356
return Error::success ();
351
357
}
352
358
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 ) {
355
361
// The offset in the stream right before invoking
356
362
// CallStackTableGenerator.Emit.
357
363
const uint64_t CallStackPayloadOffset =
@@ -382,7 +388,7 @@ Error IndexedMemProfReader::deserializeV3(const unsigned char *Start,
382
388
MemProfRecordTable.reset (MemProfRecordHashTable::Create (
383
389
/* Buckets=*/ Start + RecordTableOffset,
384
390
/* Payload=*/ Start + RecordPayloadOffset,
385
- /* Base=*/ Start, memprof::RecordLookupTrait (memprof::Version3 , Schema)));
391
+ /* Base=*/ Start, memprof::RecordLookupTrait (Version , Schema)));
386
392
387
393
return Error::success ();
388
394
}
@@ -395,8 +401,10 @@ Error IndexedMemProfReader::deserialize(const unsigned char *Start,
395
401
const uint64_t FirstWord =
396
402
support::endian::readNext<uint64_t , llvm::endianness::little>(Ptr );
397
403
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.
400
408
Version = static_cast <memprof::IndexedVersion>(FirstWord);
401
409
} else {
402
410
return make_error<InstrProfError>(
@@ -413,12 +421,13 @@ Error IndexedMemProfReader::deserialize(const unsigned char *Start,
413
421
return E;
414
422
break ;
415
423
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 ))
417
427
return E;
418
428
break ;
419
429
}
420
430
421
431
return Error::success ();
422
432
}
423
-
424
433
} // namespace llvm
0 commit comments