diff --git a/llvm/tools/llvm-profgen/ProfileGenerator.h b/llvm/tools/llvm-profgen/ProfileGenerator.h --- a/llvm/tools/llvm-profgen/ProfileGenerator.h +++ b/llvm/tools/llvm-profgen/ProfileGenerator.h @@ -203,6 +203,9 @@ uint64_t HotCountThreshold; uint64_t ColdCountThreshold; + // Underlying context table serves for sample profile writer. + std::unordered_set Contexts; + private: // Helper function for updating body sample for a leaf location in // FunctionProfile @@ -249,9 +252,6 @@ FunctionSamples & getFunctionProfileForLeafProbe(SampleContextFrames ContextStack, const MCDecodedPseudoProbe *LeafProbe); - - // Underlying context table serves for sample profile writer. - std::unordered_set Contexts; }; } // end namespace sampleprof 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 @@ -219,16 +219,20 @@ FunctionSamples & CSProfileGenerator::getFunctionProfileForContext(SampleContextFrames Context, bool WasLeafInlined) { - SampleContext FContext(Context); - auto Ret = ProfileMap.emplace(Context, FunctionSamples()); - if (Ret.second) { - SampleContext FContext(Context, RawContext); + auto I = ProfileMap.find(SampleContext(Context)); + if (I == ProfileMap.end()) { + // Save the new context for future references. + SampleContextFrames NewContext = + *Contexts.insert((const SampleContextFrameVector &)Context).first; + SampleContext FContext(NewContext, RawContext); + auto Ret = ProfileMap.emplace(FContext, FunctionSamples()); if (WasLeafInlined) FContext.setAttribute(ContextWasInlined); FunctionSamples &FProfile = Ret.first->second; FProfile.setContext(FContext); + return Ret.first->second; } - return Ret.first->second; + return I->second; } void CSProfileGenerator::generateProfile() { @@ -549,11 +553,8 @@ uint64_t CallerIndex = CallerLeafFrameLoc.Callsite.LineOffset; assert(CallerIndex && "Inferred caller's location index shouldn't be zero!"); - // Save the new context for future references. - SampleContextFrames CallerContext = - *Contexts.insert(CallerContextId).first; FunctionSamples &CallerProfile = - getFunctionProfileForContext(CallerContext); + getFunctionProfileForContext(CallerContextId); CallerProfile.setFunctionHash(InlinerDesc->FuncHash); CallerProfile.addBodySamples(CallerIndex, 0, Count); CallerProfile.addTotalSamples(Count); @@ -616,13 +617,11 @@ CSProfileGenerator::compressRecursionContext(NewContextStack); CSProfileGenerator::trimContext(NewContextStack); NewContextStack.push_back(LeafFrame); - // Save the new context for future references. - SampleContextFrames NewContext = *Contexts.insert(NewContextStack).first; const auto *FuncDesc = Binary->getFuncDescForGUID(LeafProbe->getGuid()); bool WasLeafInlined = LeafProbe->getInlineTreeNode()->hasInlineSite(); FunctionSamples &FunctionProile = - getFunctionProfileForContext(NewContext, WasLeafInlined); + getFunctionProfileForContext(NewContextStack, WasLeafInlined); FunctionProile.setFunctionHash(FuncDesc->FuncHash); return FunctionProile; }