diff --git a/llvm/include/llvm/ProfileData/MemProf.h b/llvm/include/llvm/ProfileData/MemProf.h index fe017913f6de2..9aaa2af335a23 100644 --- a/llvm/include/llvm/ProfileData/MemProf.h +++ b/llvm/include/llvm/ProfileData/MemProf.h @@ -1023,6 +1023,12 @@ struct IndexedMemProfData { // A map to hold call stack id to call stacks. llvm::MapVector> CallStacks; + + FrameId addFrame(const Frame &F) { + const FrameId Id = F.hash(); + Frames.try_emplace(Id, F); + return Id; + } }; struct FrameStat { diff --git a/llvm/lib/ProfileData/MemProfReader.cpp b/llvm/lib/ProfileData/MemProfReader.cpp index fdb8e596b0dd3..9dacf29898593 100644 --- a/llvm/lib/ProfileData/MemProfReader.cpp +++ b/llvm/lib/ProfileData/MemProfReader.cpp @@ -584,9 +584,7 @@ Error RawMemProfReader::symbolizeAndFilterStackFrames( GuidToSymbolName.insert({Guid, CanonicalName.str()}); } - const FrameId Hash = F.hash(); - MemProfData.Frames.insert({Hash, F}); - SymbolizedFrame[VAddr].push_back(Hash); + SymbolizedFrame[VAddr].push_back(MemProfData.addFrame(F)); } } @@ -769,11 +767,8 @@ void YAMLMemProfReader::parse(StringRef YAMLData) { auto AddCallStack = [&](ArrayRef CallStack) -> CallStackId { SmallVector IndexedCallStack; IndexedCallStack.reserve(CallStack.size()); - for (const Frame &F : CallStack) { - FrameId Id = F.hash(); - MemProfData.Frames.try_emplace(Id, F); - IndexedCallStack.push_back(Id); - } + for (const Frame &F : CallStack) + IndexedCallStack.push_back(MemProfData.addFrame(F)); CallStackId CSId = hashCallStack(IndexedCallStack); MemProfData.CallStacks.try_emplace(CSId, std::move(IndexedCallStack)); return CSId; diff --git a/llvm/unittests/ProfileData/MemProfTest.cpp b/llvm/unittests/ProfileData/MemProfTest.cpp index 2f8589bbfbb96..3ac64dcac0abe 100644 --- a/llvm/unittests/ProfileData/MemProfTest.cpp +++ b/llvm/unittests/ProfileData/MemProfTest.cpp @@ -430,8 +430,8 @@ TEST(MemProf, BaseMemProfReader) { /*Column=*/5, /*IsInlineFrame=*/true); Frame F2(/*Hash=*/IndexedMemProfRecord::getGUID("bar"), /*LineOffset=*/10, /*Column=*/2, /*IsInlineFrame=*/false); - MemProfData.Frames.insert({F1.hash(), F1}); - MemProfData.Frames.insert({F2.hash(), F2}); + MemProfData.addFrame(F1); + MemProfData.addFrame(F2); llvm::SmallVector CallStack{F1.hash(), F2.hash()}; CallStackId CSId = hashCallStack(CallStack); @@ -466,8 +466,8 @@ TEST(MemProf, BaseMemProfReaderWithCSIdMap) { /*Column=*/5, /*IsInlineFrame=*/true); Frame F2(/*Hash=*/IndexedMemProfRecord::getGUID("bar"), /*LineOffset=*/10, /*Column=*/2, /*IsInlineFrame=*/false); - MemProfData.Frames.insert({F1.hash(), F1}); - MemProfData.Frames.insert({F2.hash(), F2}); + MemProfData.addFrame(F1); + MemProfData.addFrame(F2); llvm::SmallVector CallStack = {F1.hash(), F2.hash()}; CallStackId CSId = hashCallStack(CallStack);