diff --git a/llvm/tools/llvm-profgen/ProfileGenerator.cpp b/llvm/tools/llvm-profgen/ProfileGenerator.cpp --- a/llvm/tools/llvm-profgen/ProfileGenerator.cpp +++ b/llvm/tools/llvm-profgen/ProfileGenerator.cpp @@ -670,7 +670,7 @@ do { uint64_t Offset = Binary->virtualAddrToOffset(IP.Address); - const SampleContextFrameVector &FrameVec = + const SampleContextFrameVector FrameVec = Binary->getFrameLocationStack(Offset); if (!FrameVec.empty()) { // FIXME: As accumulating total count per instruction caused some @@ -711,7 +711,7 @@ continue; // Record called target sample and its count. const SampleContextFrameVector &FrameVec = - Binary->getFrameLocationStack(SourceOffset); + Binary->getCachedFrameLocationStack(SourceOffset); if (!FrameVec.empty()) { FunctionSamples &FunctionProfile = getLeafProfileAndAddTotalSamples(FrameVec, 0); diff --git a/llvm/tools/llvm-profgen/ProfiledBinary.h b/llvm/tools/llvm-profgen/ProfiledBinary.h --- a/llvm/tools/llvm-profgen/ProfiledBinary.h +++ b/llvm/tools/llvm-profgen/ProfiledBinary.h @@ -492,18 +492,23 @@ // Load the symbols from debug table and populate into symbol list. void populateSymbolListFromDWARF(ProfileSymbolList &SymbolList); - const SampleContextFrameVector & + SampleContextFrameVector getFrameLocationStack(uint64_t Offset, bool UseProbeDiscriminator = false) { + InstructionPointer IP(this, Offset); + return symbolize(IP, true, UseProbeDiscriminator); + } + + const SampleContextFrameVector & + getCachedFrameLocationStack(uint64_t Offset, + bool UseProbeDiscriminator = false) { auto I = Offset2LocStackMap.emplace(Offset, SampleContextFrameVector()); - if (I.second) { - InstructionPointer IP(this, Offset); - I.first->second = symbolize(IP, true, UseProbeDiscriminator); - } + if (I.second) + I.first->second = getFrameLocationStack(Offset, UseProbeDiscriminator); return I.first->second; } Optional getInlineLeafFrameLoc(uint64_t Offset) { - const auto &Stack = getFrameLocationStack(Offset); + const auto &Stack = getCachedFrameLocationStack(Offset); if (Stack.empty()) return {}; return Stack.back(); diff --git a/llvm/tools/llvm-profgen/ProfiledBinary.cpp b/llvm/tools/llvm-profgen/ProfiledBinary.cpp --- a/llvm/tools/llvm-profgen/ProfiledBinary.cpp +++ b/llvm/tools/llvm-profgen/ProfiledBinary.cpp @@ -235,8 +235,10 @@ bool ProfiledBinary::inlineContextEqual(uint64_t Address1, uint64_t Address2) { uint64_t Offset1 = virtualAddrToOffset(Address1); uint64_t Offset2 = virtualAddrToOffset(Address2); - const SampleContextFrameVector &Context1 = getFrameLocationStack(Offset1); - const SampleContextFrameVector &Context2 = getFrameLocationStack(Offset2); + const SampleContextFrameVector &Context1 = + getCachedFrameLocationStack(Offset1); + const SampleContextFrameVector &Context2 = + getCachedFrameLocationStack(Offset2); if (Context1.size() != Context2.size()) return false; if (Context1.empty()) @@ -257,7 +259,7 @@ for (auto Address : Stack) { uint64_t Offset = virtualAddrToOffset(Address); const SampleContextFrameVector &ExpandedContext = - getFrameLocationStack(Offset); + getCachedFrameLocationStack(Offset); // An instruction without a valid debug line will be ignored by sample // processing if (ExpandedContext.empty()) @@ -817,7 +819,7 @@ do { uint64_t Offset = virtualAddrToOffset(IP.Address); - const SampleContextFrameVector &SymbolizedCallStack = + const SampleContextFrameVector SymbolizedCallStack = getFrameLocationStack(Offset, UsePseudoProbes); uint64_t Size = Offset2InstSizeMap[Offset];