Skip to content

Commit 5422eb0

Browse files
[memprof] Add another constructor to MemProfReader (#88952)
This patch enables users of MemProfReader to directly supply mappings from CallStackId to actual call stacks. Once the users of the current constructor without CSIdMap switch to the new constructor, we'll have fewer users of: - IndexedAllocationInfo::CallStack - IndexedMemProfRecord::CallSites bringing us one step closer to the removal of these fields in favor of: - IndexedAllocationInfo::CSId - IndexedMemProfRecord::CallSiteIds
1 parent 0665669 commit 5422eb0

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

llvm/include/llvm/ProfileData/MemProfReader.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ class MemProfReader {
9898
llvm::DenseMap<FrameId, Frame> FrameIdMap,
9999
llvm::MapVector<GlobalValue::GUID, IndexedMemProfRecord> ProfData);
100100

101+
// Initialize the MemProfReader with the frame mappings, call stack mappings,
102+
// and profile contents.
103+
MemProfReader(
104+
llvm::DenseMap<FrameId, Frame> FrameIdMap,
105+
llvm::DenseMap<CallStackId, llvm::SmallVector<FrameId>> CSIdMap,
106+
llvm::MapVector<GlobalValue::GUID, IndexedMemProfRecord> ProfData)
107+
: IdToFrame(std::move(FrameIdMap)), CSIdToCallStack(std::move(CSIdMap)),
108+
FunctionProfileData(std::move(ProfData)) {}
109+
101110
protected:
102111
// A helper method to extract the frame from the IdToFrame map.
103112
const Frame &idToFrame(const FrameId Id) const {

llvm/unittests/ProfileData/MemProfTest.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,47 @@ TEST(MemProf, BaseMemProfReader) {
436436
FrameContains("bar", 10U, 2U, false));
437437
}
438438

439+
TEST(MemProf, BaseMemProfReaderWithCSIdMap) {
440+
llvm::DenseMap<FrameId, Frame> FrameIdMap;
441+
Frame F1(/*Hash=*/IndexedMemProfRecord::getGUID("foo"), /*LineOffset=*/20,
442+
/*Column=*/5, /*IsInlineFrame=*/true);
443+
Frame F2(/*Hash=*/IndexedMemProfRecord::getGUID("bar"), /*LineOffset=*/10,
444+
/*Column=*/2, /*IsInlineFrame=*/false);
445+
FrameIdMap.insert({F1.hash(), F1});
446+
FrameIdMap.insert({F2.hash(), F2});
447+
448+
llvm::DenseMap<CallStackId, llvm::SmallVector<FrameId>> CSIdMap;
449+
llvm::SmallVector<FrameId> CallStack = {F1.hash(), F2.hash()};
450+
CallStackId CSId = llvm::memprof::hashCallStack(CallStack);
451+
CSIdMap.insert({CSId, CallStack});
452+
453+
llvm::MapVector<llvm::GlobalValue::GUID, IndexedMemProfRecord> ProfData;
454+
IndexedMemProfRecord FakeRecord;
455+
MemInfoBlock Block;
456+
Block.AllocCount = 1U, Block.TotalAccessDensity = 4,
457+
Block.TotalLifetime = 200001;
458+
FakeRecord.AllocSites.emplace_back(
459+
/*CS=*/llvm::SmallVector<FrameId>(),
460+
/*CSId=*/llvm::memprof::hashCallStack(CallStack),
461+
/*MB=*/Block);
462+
ProfData.insert({F1.hash(), FakeRecord});
463+
464+
MemProfReader Reader(FrameIdMap, CSIdMap, ProfData);
465+
466+
llvm::SmallVector<MemProfRecord, 1> Records;
467+
for (const auto &KeyRecordPair : Reader) {
468+
Records.push_back(KeyRecordPair.second);
469+
}
470+
471+
ASSERT_THAT(Records, SizeIs(1));
472+
ASSERT_THAT(Records[0].AllocSites, SizeIs(1));
473+
ASSERT_THAT(Records[0].AllocSites[0].CallStack, SizeIs(2));
474+
EXPECT_THAT(Records[0].AllocSites[0].CallStack[0],
475+
FrameContains("foo", 20U, 5U, true));
476+
EXPECT_THAT(Records[0].AllocSites[0].CallStack[1],
477+
FrameContains("bar", 10U, 2U, false));
478+
}
479+
439480
TEST(MemProf, IndexedMemProfRecordToMemProfRecord) {
440481
// Verify that MemProfRecord can be constructed from IndexedMemProfRecord with
441482
// CallStackIds only.

0 commit comments

Comments
 (0)