diff --git a/llvm/tools/llvm-profgen/PerfReader.cpp b/llvm/tools/llvm-profgen/PerfReader.cpp --- a/llvm/tools/llvm-profgen/PerfReader.cpp +++ b/llvm/tools/llvm-profgen/PerfReader.cpp @@ -95,6 +95,8 @@ std::shared_ptr KeyStr = std::make_shared(); KeyStr->Context = Binary->getExpandedContextStr(Stack); + if (KeyStr->Context.empty()) + return nullptr; KeyStr->genHashCode(); return KeyStr; } @@ -118,6 +120,8 @@ return; std::shared_ptr Key = Stack.getContextKey(); + if (Key == nullptr) + return; auto Ret = CtxCounterMap->emplace(Hashable(Key), SampleCounter()); SampleCounter &SCounter = Ret.first->second; for (auto &Item : Cur->RangeSamples) { 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 @@ -237,9 +237,11 @@ while (IP.Address <= RangeEnd) { uint64_t Offset = Binary->virtualAddrToOffset(IP.Address); - const FrameLocation &LeafLoc = Binary->getInlineLeafFrameLoc(Offset); - // Recording body sample for this specific context - updateBodySamplesforFunctionProfile(FunctionProfile, LeafLoc, Count); + auto LeafLoc = Binary->getInlineLeafFrameLoc(Offset); + if (LeafLoc.hasValue()) { + // Recording body sample for this specific context + updateBodySamplesforFunctionProfile(FunctionProfile, *LeafLoc, Count); + } // Move to next IP within the range IP.advance(); } @@ -261,9 +263,11 @@ continue; // Record called target sample and its count - const FrameLocation &LeafLoc = Binary->getInlineLeafFrameLoc(SourceOffset); - FunctionProfile.addCalledTargetSamples(LeafLoc.second.LineOffset, - LeafLoc.second.Discriminator, + auto LeafLoc = Binary->getInlineLeafFrameLoc(SourceOffset); + if (!LeafLoc.hasValue()) + continue; + FunctionProfile.addCalledTargetSamples(LeafLoc->second.LineOffset, + LeafLoc->second.Discriminator, CalleeName, Count); // Record head sample for called target(callee) @@ -272,7 +276,7 @@ OCalleeCtxStr << ContextId.rsplit(" @ ").first.str(); OCalleeCtxStr << " @ "; } - OCalleeCtxStr << getCallSite(LeafLoc) << " @ " << CalleeName.str(); + OCalleeCtxStr << getCallSite(*LeafLoc) << " @ " << CalleeName.str(); FunctionSamples &CalleeProfile = getFunctionProfileForContext(OCalleeCtxStr.str()); 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 @@ -11,6 +11,7 @@ #include "CallContext.h" #include "PseudoProbe.h" +#include "llvm/ADT/Optional.h" #include "llvm/ADT/StringRef.h" #include "llvm/DebugInfo/Symbolize/Symbolize.h" #include "llvm/MC/MCAsmInfo.h" @@ -232,9 +233,12 @@ return FuncStartAddrMap[Offset]; } - const FrameLocation &getInlineLeafFrameLoc(uint64_t Offset, - bool NameOnly = false) { - return getFrameLocationStack(Offset).back(); + Optional getInlineLeafFrameLoc(uint64_t Offset, + bool NameOnly = false) { + auto &Stack = getFrameLocationStack(Offset); + if (Stack.empty()) + return {}; + return Stack.back(); } // Compare two addresses' inline context 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 @@ -139,7 +139,8 @@ const FrameLocationStack &Context2 = getFrameLocationStack(Offset2); if (Context1.size() != Context2.size()) return false; - + if (Context1.empty()) + return false; // The leaf frame contains location within the leaf, and it // needs to be remove that as it's not part of the calling context return std::equal(Context1.begin(), Context1.begin() + Context1.size() - 1, @@ -154,6 +155,8 @@ for (auto Address : Stack) { uint64_t Offset = virtualAddrToOffset(Address); const FrameLocationStack &ExpandedContext = getFrameLocationStack(Offset); + if (ExpandedContext.empty()) + return ""; for (const auto &Loc : ExpandedContext) { ContextVec.push_back(getCallSite(Loc)); } @@ -262,9 +265,14 @@ const MCInstrDesc &MCDesc = MII->get(Inst.getOpcode()); // Populate a vector of the symbolized callsite at this location - InstructionPointer IP(this, Offset); - Offset2LocStackMap[Offset] = symbolize(IP, true); - + // We don't need symbolized info for probe-based profile, just use an empty + // stack as an entry to indicate a valid binary offset + FrameLocationStack SymbolizedCallStack; + if (!UsePseudoProbes) { + InstructionPointer IP(this, Offset); + SymbolizedCallStack = symbolize(IP, true); + } + Offset2LocStackMap[Offset] = SymbolizedCallStack; // Populate address maps. CodeAddrs.push_back(Offset); if (MCDesc.isCall())