diff --git a/llvm/include/llvm/ProfileData/MemProfReader.h b/llvm/include/llvm/ProfileData/MemProfReader.h index de2167f97b0dc..ade0c3901562d 100644 --- a/llvm/include/llvm/ProfileData/MemProfReader.h +++ b/llvm/include/llvm/ProfileData/MemProfReader.h @@ -120,6 +120,8 @@ class MemProfReader { // Initialize the MemProfReader with the frame mappings, call stack mappings, // and profile contents. + LLVM_DEPRECATED("Construct MemProfReader with IndexedMemProfData", + "MemProfReader") MemProfReader( llvm::DenseMap FrameIdMap, llvm::DenseMap> CSIdMap, @@ -127,6 +129,15 @@ class MemProfReader { : IdToFrame(std::move(FrameIdMap)), CSIdToCallStack(std::move(CSIdMap)), FunctionProfileData(std::move(ProfData)) {} + // Initialize the MemProfReader with the given MemProf profile. + MemProfReader(IndexedMemProfData MemProfData) { + for (const auto &[FrameId, F] : MemProfData.Frames) + IdToFrame.try_emplace(FrameId, F); + for (const auto &[CSId, CS] : MemProfData.CallStacks) + CSIdToCallStack.try_emplace(CSId, CS); + FunctionProfileData = std::move(MemProfData.Records); + } + protected: // A helper method to extract the frame from the IdToFrame map. const Frame &idToFrame(const FrameId Id) const { diff --git a/llvm/unittests/ProfileData/MemProfTest.cpp b/llvm/unittests/ProfileData/MemProfTest.cpp index 5097dbdd6c391..e5d41db06dcc0 100644 --- a/llvm/unittests/ProfileData/MemProfTest.cpp +++ b/llvm/unittests/ProfileData/MemProfTest.cpp @@ -490,20 +490,18 @@ TEST(MemProf, BaseMemProfReader) { } TEST(MemProf, BaseMemProfReaderWithCSIdMap) { - llvm::DenseMap FrameIdMap; + llvm::memprof::IndexedMemProfData MemProfData; Frame F1(/*Hash=*/IndexedMemProfRecord::getGUID("foo"), /*LineOffset=*/20, /*Column=*/5, /*IsInlineFrame=*/true); Frame F2(/*Hash=*/IndexedMemProfRecord::getGUID("bar"), /*LineOffset=*/10, /*Column=*/2, /*IsInlineFrame=*/false); - FrameIdMap.insert({F1.hash(), F1}); - FrameIdMap.insert({F2.hash(), F2}); + MemProfData.Frames.insert({F1.hash(), F1}); + MemProfData.Frames.insert({F2.hash(), F2}); - llvm::DenseMap> CSIdMap; llvm::SmallVector CallStack = {F1.hash(), F2.hash()}; CallStackId CSId = llvm::memprof::hashCallStack(CallStack); - CSIdMap.insert({CSId, CallStack}); + MemProfData.CallStacks.insert({CSId, CallStack}); - llvm::MapVector ProfData; IndexedMemProfRecord FakeRecord; MemInfoBlock Block; Block.AllocCount = 1U, Block.TotalAccessDensity = 4, @@ -511,9 +509,9 @@ TEST(MemProf, BaseMemProfReaderWithCSIdMap) { FakeRecord.AllocSites.emplace_back( /*CSId=*/llvm::memprof::hashCallStack(CallStack), /*MB=*/Block); - ProfData.insert({F1.hash(), FakeRecord}); + MemProfData.Records.insert({F1.hash(), FakeRecord}); - MemProfReader Reader(FrameIdMap, CSIdMap, ProfData); + MemProfReader Reader(MemProfData); llvm::SmallVector Records; for (const auto &KeyRecordPair : Reader) {