Skip to content

Commit f88c913

Browse files
[memprof] Add a new constructor to MemProfReader (NFC) (#116918)
This patch adds a new constructor to MemProfReader that takes IndexedMemProfData, a complete package of MemProf profile. To showcase its usage, I'm updating one of the unit tests to use the new constructor. Because of type mismatches between DenseMap and MapVector, I'm copying Frames and CallStacks for now. Once we remove the methods and old constructors that take or return individual components (frames, call stacks, and records), we will drop the copying, and the new constructor will collapse down to: MemProfReader(IndexedMemProfData MemProfData) : MemProfData(std::move(MemProfData)) {} Since nobody in the LLVM codebase uses the constructor that takes the three indivdual components, I'm deprecating the old constructor.
1 parent 5bf017c commit f88c913

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

llvm/include/llvm/ProfileData/MemProfReader.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,24 @@ class MemProfReader {
120120

121121
// Initialize the MemProfReader with the frame mappings, call stack mappings,
122122
// and profile contents.
123+
LLVM_DEPRECATED("Construct MemProfReader with IndexedMemProfData",
124+
"MemProfReader")
123125
MemProfReader(
124126
llvm::DenseMap<FrameId, Frame> FrameIdMap,
125127
llvm::DenseMap<CallStackId, llvm::SmallVector<FrameId>> CSIdMap,
126128
llvm::MapVector<GlobalValue::GUID, IndexedMemProfRecord> ProfData)
127129
: IdToFrame(std::move(FrameIdMap)), CSIdToCallStack(std::move(CSIdMap)),
128130
FunctionProfileData(std::move(ProfData)) {}
129131

132+
// Initialize the MemProfReader with the given MemProf profile.
133+
MemProfReader(IndexedMemProfData MemProfData) {
134+
for (const auto &[FrameId, F] : MemProfData.Frames)
135+
IdToFrame.try_emplace(FrameId, F);
136+
for (const auto &[CSId, CS] : MemProfData.CallStacks)
137+
CSIdToCallStack.try_emplace(CSId, CS);
138+
FunctionProfileData = std::move(MemProfData.Records);
139+
}
140+
130141
protected:
131142
// A helper method to extract the frame from the IdToFrame map.
132143
const Frame &idToFrame(const FrameId Id) const {

llvm/unittests/ProfileData/MemProfTest.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -490,30 +490,28 @@ TEST(MemProf, BaseMemProfReader) {
490490
}
491491

492492
TEST(MemProf, BaseMemProfReaderWithCSIdMap) {
493-
llvm::DenseMap<FrameId, Frame> FrameIdMap;
493+
llvm::memprof::IndexedMemProfData MemProfData;
494494
Frame F1(/*Hash=*/IndexedMemProfRecord::getGUID("foo"), /*LineOffset=*/20,
495495
/*Column=*/5, /*IsInlineFrame=*/true);
496496
Frame F2(/*Hash=*/IndexedMemProfRecord::getGUID("bar"), /*LineOffset=*/10,
497497
/*Column=*/2, /*IsInlineFrame=*/false);
498-
FrameIdMap.insert({F1.hash(), F1});
499-
FrameIdMap.insert({F2.hash(), F2});
498+
MemProfData.Frames.insert({F1.hash(), F1});
499+
MemProfData.Frames.insert({F2.hash(), F2});
500500

501-
llvm::DenseMap<CallStackId, llvm::SmallVector<FrameId>> CSIdMap;
502501
llvm::SmallVector<FrameId> CallStack = {F1.hash(), F2.hash()};
503502
CallStackId CSId = llvm::memprof::hashCallStack(CallStack);
504-
CSIdMap.insert({CSId, CallStack});
503+
MemProfData.CallStacks.insert({CSId, CallStack});
505504

506-
llvm::MapVector<llvm::GlobalValue::GUID, IndexedMemProfRecord> ProfData;
507505
IndexedMemProfRecord FakeRecord;
508506
MemInfoBlock Block;
509507
Block.AllocCount = 1U, Block.TotalAccessDensity = 4,
510508
Block.TotalLifetime = 200001;
511509
FakeRecord.AllocSites.emplace_back(
512510
/*CSId=*/llvm::memprof::hashCallStack(CallStack),
513511
/*MB=*/Block);
514-
ProfData.insert({F1.hash(), FakeRecord});
512+
MemProfData.Records.insert({F1.hash(), FakeRecord});
515513

516-
MemProfReader Reader(FrameIdMap, CSIdMap, ProfData);
514+
MemProfReader Reader(MemProfData);
517515

518516
llvm::SmallVector<MemProfRecord, 1> Records;
519517
for (const auto &KeyRecordPair : Reader) {

0 commit comments

Comments
 (0)