[llvm] [MemProf][NFC] Clear each IndexedMemProfRecord after it is written (PR #75205)

Teresa Johnson via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 12 07:48:23 PST 2023


https://github.com/teresajohnson created https://github.com/llvm/llvm-project/pull/75205

The on-disk hash table for the memprof writer holds copies of all the
memprof records to be written. These hold a lot of memory in aggregate,
due to the lists of alloc sites (which each have a list of context
frames) and call sites. Clear each one after emitting it.

This drops the peak memory when writing a very large indexed memprof
profile by about 2.5G.


>From 75b7e50e3f40532ceb3d464498232aac26662caa Mon Sep 17 00:00:00 2001
From: Teresa Johnson <tejohnson at google.com>
Date: Tue, 12 Dec 2023 07:39:07 -0800
Subject: [PATCH] [MemProf][NFC] Clear each IndexedMemProfRecord after it is
 written

The on-disk hash table for the memprof writer holds copies of all the
memprof records to be written. These hold a lot of memory in aggregate,
due to the lists of alloc sites (which each have a list of context
frames) and call sites. Clear each one after emitting it.

This drops the peak memory when writing a very large indexed memprof
profile by about 2.5G.
---
 llvm/include/llvm/ProfileData/MemProf.h  | 5 +++++
 llvm/lib/ProfileData/InstrProfWriter.cpp | 3 +++
 2 files changed, 8 insertions(+)

diff --git a/llvm/include/llvm/ProfileData/MemProf.h b/llvm/include/llvm/ProfileData/MemProf.h
index 6557481870d8c..37c19094bc2a6 100644
--- a/llvm/include/llvm/ProfileData/MemProf.h
+++ b/llvm/include/llvm/ProfileData/MemProf.h
@@ -538,6 +538,11 @@ class RecordWriterTrait {
                 offset_type /*Unused*/) {
     assert(Schema != nullptr && "MemProf schema is not initialized!");
     V.serialize(*Schema, Out);
+    // Clear the IndexedMemProfRecord which results in clearing/freeing its
+    // vectors of allocs and callsites. This is owned by the associated on-disk
+    // hash table, but unused after this point. See also the comment added to
+    // the client which constructs the on-disk hash table for this trait.
+    V.clear();
   }
 };
 
diff --git a/llvm/lib/ProfileData/InstrProfWriter.cpp b/llvm/lib/ProfileData/InstrProfWriter.cpp
index 595c9aa1adc10..136e33ba04b2a 100644
--- a/llvm/lib/ProfileData/InstrProfWriter.cpp
+++ b/llvm/lib/ProfileData/InstrProfWriter.cpp
@@ -537,6 +537,9 @@ Error InstrProfWriter::writeImpl(ProfOStream &OS) {
       RecordTableGenerator.insert(I.first, I.second);
     }
 
+    // The call to Emit invokes RecordWriterTrait::EmitData which destructs
+    // the memprof record copies owned by the RecordTableGenerator. This works
+    // because the RecordTableGenerator is not used after this point.
     uint64_t RecordTableOffset =
         RecordTableGenerator.Emit(OS.OS, *RecordWriter);
 



More information about the llvm-commits mailing list