Skip to content

Commit f97c610

Browse files
[memprof] Add MemProfReader::takeMemProfData (#116769)
This patch adds MemProfReader::takeMemProfData, a function to return the complete MemProf profile from the reader. We can directly pass its return value to InstrProfWriter::addMemProfData without having to deal with the indivual components of the MemProf profile. The new function is named "take", but it doesn't do std::move yet because of type differences (DenseMap v.s. MapVector). The end state I'm trying to get to is roughly as follows: - MemProfReader accepts IndexedMemProfData as a parameter as opposed to the three individual components (frames, call stacks, and records). - MemProfReader keeps IndexedMemProfData as a class member without decomposing it into its individual components. - MemProfReader returns IndexedMemProfData like: IndexedMemProfData takeMemProfData() { return std::move(MemProfData); }
1 parent a2e266b commit f97c610

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

llvm/include/llvm/ProfileData/MemProfReader.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,24 @@ class MemProfReader {
6363
return FunctionProfileData;
6464
}
6565

66+
// Take the complete profile data.
67+
IndexedMemProfData takeMemProfData() {
68+
// TODO: Once we replace the three member variables, namely IdToFrame,
69+
// CSIdToCallStack, and FunctionProfileData, with MemProfData, replace the
70+
// following code with just "return std::move(MemProfData);".
71+
IndexedMemProfData MemProfData;
72+
// Copy key-value pairs because IdToFrame uses DenseMap, whereas
73+
// IndexedMemProfData::Frames uses MapVector.
74+
for (const auto &[FrameId, F] : IdToFrame)
75+
MemProfData.Frames.try_emplace(FrameId, F);
76+
// Copy key-value pairs because CSIdToCallStack uses DenseMap, whereas
77+
// IndexedMemProfData::CallStacks uses MapVector.
78+
for (const auto &[CSId, CS] : CSIdToCallStack)
79+
MemProfData.CallStacks.try_emplace(CSId, CS);
80+
MemProfData.Records = FunctionProfileData;
81+
return MemProfData;
82+
}
83+
6684
virtual Error
6785
readNextRecord(GuidMemProfRecordPair &GuidRecord,
6886
std::function<const Frame(const FrameId)> Callback = nullptr) {

llvm/lib/ProfileData/InstrProfWriter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,8 @@ bool InstrProfWriter::addMemProfData(memprof::IndexedMemProfData Incoming,
370370
if (addMemProfCallStack(CSId, CS, Warn))
371371
return false;
372372

373-
if (MemProfData.Records.empty())
373+
// Add one record at a time if randomization is requested.
374+
if (MemProfData.Records.empty() && !MemprofGenerateRandomHotness)
374375
MemProfData.Records = std::move(Incoming.Records);
375376
else
376377
for (const auto &[GUID, Record] : Incoming.Records)

llvm/tools/llvm-profdata/llvm-profdata.cpp

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -720,33 +720,7 @@ loadInput(const WeightedFile &Input, SymbolRemapper *Remapper,
720720
Filename);
721721
};
722722

723-
// Add the frame mappings into the writer context.
724-
const auto &IdToFrame = Reader->getFrameMapping();
725-
for (const auto &I : IdToFrame) {
726-
bool Succeeded = WC->Writer.addMemProfFrame(
727-
/*Id=*/I.first, /*Frame=*/I.getSecond(), MemProfError);
728-
// If we weren't able to add the frame mappings then it doesn't make sense
729-
// to try to add the records from this profile.
730-
if (!Succeeded)
731-
return;
732-
}
733-
734-
// Add the call stacks into the writer context.
735-
const auto &CSIdToCallStacks = Reader->getCallStacks();
736-
for (const auto &I : CSIdToCallStacks) {
737-
bool Succeeded = WC->Writer.addMemProfCallStack(
738-
/*Id=*/I.first, /*Frame=*/I.getSecond(), MemProfError);
739-
// If we weren't able to add the call stacks then it doesn't make sense
740-
// to try to add the records from this profile.
741-
if (!Succeeded)
742-
return;
743-
}
744-
745-
const auto &FunctionProfileData = Reader->getProfileData();
746-
// Add the memprof records into the writer context.
747-
for (const auto &[GUID, Record] : FunctionProfileData) {
748-
WC->Writer.addMemProfRecord(GUID, Record);
749-
}
723+
WC->Writer.addMemProfData(Reader->takeMemProfData(), MemProfError);
750724
return;
751725
}
752726

0 commit comments

Comments
 (0)