diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp --- a/llvm/lib/Transforms/IPO/SampleProfile.cpp +++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp @@ -2111,20 +2111,30 @@ void SampleProfileMatcher::findIRAnchors( const Function &F, std::map &IRAnchors) { - auto FindTopLevelFrame = [](const DILocation *DIL) { - const DILocation *PrevDIL = DIL; - for (; DIL->getInlinedAt(); DIL = DIL->getInlinedAt()) - PrevDIL = DIL; - - LineLocation Callsite = FunctionSamples::getCallSiteIdentifier(DIL); - StringRef CalleeName = PrevDIL->getSubprogramLinkageName(); - return std::make_pair(Callsite, CalleeName); + // Flatten inlined IR for the matching. Recover the original callsite + // and call target by analyzing the inline frames from the debug info. + // CalleeDIL is null for non-inlined code. + auto FindTopTwoFrames = [](const DILocation *CallerDIL) { + const DILocation *CalleeDIL = nullptr; + for (; CallerDIL->getInlinedAt(); CallerDIL = CallerDIL->getInlinedAt()) + CalleeDIL = CallerDIL; + return std::make_pair(CallerDIL, CalleeDIL); }; - auto FindCalleeName = [](const CallBase *CB) { - StringRef CalleeName = UnknownIndirectCallee; - if (Function *Callee = CB->getCalledFunction()) - CalleeName = FunctionSamples::getCanonicalFnName(Callee->getName()); + auto FindCalleeName = [](const Instruction &I, const DILocation *CalleeDIL) { + // Use empty StringRef for non-call non-inlined instruction. + StringRef CalleeName; + if (CalleeDIL) { + // Use the call target of the top-level frame for inlined code. + CalleeName = CalleeDIL->getSubprogramLinkageName(); + } else if (const auto *CB = dyn_cast(&I)) { + if (isa(&I)) + return CalleeName; + if (Function *Callee = CB->getCalledFunction()) + CalleeName = FunctionSamples::getCanonicalFnName(Callee->getName()); + else + CalleeName = UnknownIndirectCallee; + } return CalleeName; }; @@ -2137,17 +2147,11 @@ if (FunctionSamples::ProfileIsProbeBased) { if (auto Probe = extractProbe(I)) { - // Flatten inlined IR for the matching. Recover the original callsite - // and call target by analyzing the inline frames from the debug info. - if (DIL->getInlinedAt()) { - IRAnchors.emplace(FindTopLevelFrame(DIL)); - } else { - // Use empty StringRef for basic block probe. - StringRef CalleeName; - if (const auto *CB = dyn_cast(&I)) - CalleeName = FindCalleeName(CB); - IRAnchors.emplace(LineLocation(Probe->Id, 0), CalleeName); - } + const auto &[CallerDIL, CalleeDIL] = FindTopTwoFrames(DIL); + LineLocation Callsite = + CalleeDIL ? FunctionSamples::getCallSiteIdentifier(CallerDIL) + : LineLocation(Probe->Id, 0); + IRAnchors.emplace(Callsite, FindCalleeName(I, CalleeDIL)); } } else { // TODO: For line-number based profile(AutoFDO), currently only support @@ -2155,14 +2159,14 @@ // instructions to extract the line locations for profile matching. if (!isa(&I) || isa(&I)) continue; - - if (DIL->getInlinedAt()) { - IRAnchors.emplace(FindTopLevelFrame(DIL)); - } else { - LineLocation IRCallsite = FunctionSamples::getCallSiteIdentifier(DIL); - StringRef CalleeName = FindCalleeName(dyn_cast(&I)); - IRAnchors.emplace(IRCallsite, CalleeName); - } + const auto &[CallerDIL, CalleeDIL] = FindTopTwoFrames(DIL); + LineLocation Callsite = + FunctionSamples::getCallSiteIdentifier(CallerDIL); + StringRef CalleeName = FindCalleeName(I, CalleeDIL); + assert( + !CalleeName.empty() && + "Callee name should not be empty for line-number based profile."); + IRAnchors.emplace(Callsite, CalleeName); } } }