25#define DEBUG_TYPE "sample-profile-matcher"
28 "Number of functions matched by demangled basename");
34 cl::desc(
"Consider a profile matches a function if the similarity of their "
35 "callee sequences is above the specified percentile."));
39 cl::desc(
"The minimum number of basic blocks required for a function to "
40 "run stale profile call graph matching."));
44 cl::desc(
"The minimum number of call anchors required for a function to "
45 "run stale profile call graph matching."));
50 "Load top-level profiles that the sample reader initially skipped for "
51 "the call-graph matching (only meaningful for extended binary "
61 cl::desc(
"The maximum number of functions in a module, above which salvage "
62 "unused profile will be skipped."));
66 cl::desc(
"The maximum number of callsites in a function, above which stale "
67 "profile matching will be skipped."));
71void SampleProfileMatcher::findIRAnchors(
const Function &
F,
76 auto FindTopLevelInlinedCallsite = [](
const DILocation *DIL) {
77 assert((DIL && DIL->getInlinedAt()) &&
"No inlined callsite");
81 DIL = DIL->getInlinedAt();
82 }
while (DIL->getInlinedAt());
86 StringRef CalleeName = PrevDIL->getSubprogramLinkageName();
87 return std::make_pair(Callsite, FunctionId(CalleeName));
90 auto GetCanonicalCalleeName = [](
const CallBase *CB) {
91 StringRef CalleeName = UnknownIndirectCallee;
92 if (Function *Callee = CB->getCalledFunction())
100 DILocation *DIL =
I.getDebugLoc();
107 if (DIL->getInlinedAt()) {
108 IRAnchors.emplace(FindTopLevelInlinedCallsite(DIL));
111 StringRef CalleeName;
115 CalleeName = GetCanonicalCalleeName(CB);
117 LineLocation Loc = LineLocation(Probe->Id, 0);
118 IRAnchors.emplace(Loc, FunctionId(CalleeName));
128 if (DIL->getInlinedAt()) {
129 IRAnchors.emplace(FindTopLevelInlinedCallsite(DIL));
134 IRAnchors.emplace(Callsite, FunctionId(CalleeName));
141void SampleProfileMatcher::findProfileAnchors(
const FunctionSamples &FS,
143 auto isInvalidLineOffset = [](uint32_t LineOffset) {
144 return LineOffset & 0x8000;
147 auto InsertAnchor = [](
const LineLocation &Loc,
const FunctionId &CalleeName,
149 auto Ret = ProfileAnchors.try_emplace(Loc, CalleeName);
153 Ret.first->second = FunctionId(UnknownIndirectCallee);
157 for (
const auto &
I :
FS.getBodySamples()) {
158 const LineLocation &Loc =
I.first;
161 for (
const auto &
C :
I.second.getCallTargets())
162 InsertAnchor(Loc,
C.first, ProfileAnchors);
165 for (
const auto &
I :
FS.getCallsiteSamples()) {
166 const LineLocation &Loc =
I.first;
169 for (
const auto &
C :
I.second)
170 InsertAnchor(Loc,
C.first, ProfileAnchors);
174bool SampleProfileMatcher::functionHasProfile(
const FunctionId &IRFuncName,
176 FuncWithoutProfile =
nullptr;
177 auto R = FunctionsWithoutProfile.find(IRFuncName);
178 if (R != FunctionsWithoutProfile.end())
179 FuncWithoutProfile =
R->second;
180 return !FuncWithoutProfile;
183bool SampleProfileMatcher::isProfileUnused(
const FunctionId &ProfileFuncName) {
186 return (SymbolMap->find(ProfileFuncName) == SymbolMap->end()) &&
190 (ProbeManager->getDesc(ProfileFuncName.
stringRef()) ==
nullptr));
193bool SampleProfileMatcher::functionMatchesProfile(
195 bool FindMatchedProfileOnly) {
196 if (IRFuncName == ProfileFuncName)
204 if (functionHasProfile(IRFuncName, IRFunc) ||
205 !isProfileUnused(ProfileFuncName))
209 "IR function should be different from profile function to match");
210 return functionMatchesProfile(*IRFunc, ProfileFuncName,
211 FindMatchedProfileOnly);
215SampleProfileMatcher::longestCommonSequence(
const AnchorList &AnchorList1,
217 bool MatchUnusedFunction) {
220 AnchorList1, AnchorList2,
221 [&](
const FunctionId &
A,
const FunctionId &
B) {
222 return functionMatchesProfile(
227 [&](LineLocation
A, LineLocation
B) {
230 return MatchedAnchors;
233void SampleProfileMatcher::matchNonCallsiteLocs(
236 auto UpdateMatching = [&](
const LineLocation &From,
const LineLocation &To) {
241 IRToProfileLocationMap.
erase(From);
245 int32_t LocationDelta = 0;
247 for (
const auto &
IR : IRAnchors) {
248 const auto &Loc =
IR.first;
249 bool IsMatchedAnchor =
false;
251 auto R = MatchedAnchors.
find(Loc);
252 if (R != MatchedAnchors.
end()) {
253 const auto &Candidate =
R->second;
254 UpdateMatching(Loc, Candidate);
256 <<
" is matched from " << Loc <<
" to " << Candidate
258 LocationDelta = Candidate.LineOffset - Loc.
LineOffset;
264 for (
size_t I = (LastMatchedNonAnchors.
size() + 1) / 2;
265 I < LastMatchedNonAnchors.
size();
I++) {
266 const auto &
L = LastMatchedNonAnchors[
I];
267 uint32_t CandidateLineOffset =
L.LineOffset + LocationDelta;
268 LineLocation Candidate(CandidateLineOffset,
L.Discriminator);
269 UpdateMatching(L, Candidate);
271 <<
" to " << Candidate <<
"\n");
274 IsMatchedAnchor =
true;
275 LastMatchedNonAnchors.
clear();
279 if (!IsMatchedAnchor) {
280 uint32_t CandidateLineOffset = Loc.
LineOffset + LocationDelta;
281 LineLocation Candidate(CandidateLineOffset, Loc.
Discriminator);
282 UpdateMatching(Loc, Candidate);
284 << Candidate <<
"\n");
292void SampleProfileMatcher::getFilteredAnchorList(
295 for (
const auto &
I : IRAnchors) {
296 if (
I.second.stringRef().empty())
298 FilteredIRAnchorsList.emplace_back(
I);
301 for (
const auto &
I : ProfileAnchors)
302 FilteredProfileAnchorList.emplace_back(
I);
322void SampleProfileMatcher::runStaleProfileMatching(
325 bool RunCFGMatching,
bool RunCGMatching) {
326 if (!RunCFGMatching && !RunCGMatching)
331 "Run stale profile matching only once per function");
335 getFilteredAnchorList(IRAnchors, ProfileAnchors, FilteredIRAnchorsList,
336 FilteredProfileAnchorList);
338 if (FilteredIRAnchorsList.empty() || FilteredProfileAnchorList.empty())
344 <<
" because the number of callsites in the IR is "
345 << FilteredIRAnchorsList.size()
346 <<
" and in the profile is "
347 << FilteredProfileAnchorList.size() <<
"\n");
362 longestCommonSequence(FilteredIRAnchorsList, FilteredProfileAnchorList,
370 matchNonCallsiteLocs(MatchedAnchors, IRAnchors, IRToProfileLocationMap);
373void SampleProfileMatcher::runOnFunction(
Function &
F) {
380 const auto *FSForMatching = getFlattenedSamplesFor(
F);
383 auto R = FuncToProfileNameMap.find(&
F);
384 if (R != FuncToProfileNameMap.end()) {
385 FSForMatching = getFlattenedSamplesFor(
R->second);
390 FSForMatching = Reader.getSamplesFor(
R->second.stringRef());
400 findIRAnchors(
F, IRAnchors);
404 findProfileAnchors(*FSForMatching, ProfileAnchors);
408 recordCallsiteMatchStates(
F, IRAnchors, ProfileAnchors,
nullptr);
415 !ProbeManager->profileIsValid(
F, *FSForMatching);
416 bool RunCFGMatching =
424 F.addFnAttr(
"profile-checksum-mismatch");
428 auto &IRToProfileLocationMap = getIRToProfileLocationMap(*FSForMatching);
429 runStaleProfileMatching(
F, IRAnchors, ProfileAnchors, IRToProfileLocationMap,
430 RunCFGMatching, RunCGMatching);
433 recordCallsiteMatchStates(
F, IRAnchors, ProfileAnchors,
434 &IRToProfileLocationMap);
437void SampleProfileMatcher::recordCallsiteMatchStates(
441 bool IsPostMatch = IRToProfileLocationMap !=
nullptr;
442 auto &CallsiteMatchStates =
445 auto MapIRLocToProfileLoc = [&](
const LineLocation &IRLoc) {
447 if (!IRToProfileLocationMap)
449 const auto &ProfileLoc = IRToProfileLocationMap->
find(IRLoc);
450 if (ProfileLoc != IRToProfileLocationMap->
end())
451 return ProfileLoc->second;
456 for (
const auto &
I : IRAnchors) {
459 const auto &ProfileLoc = MapIRLocToProfileLoc(
I.first);
460 const auto &IRCalleeId =
I.second;
461 const auto &It = ProfileAnchors.find(ProfileLoc);
462 if (It == ProfileAnchors.end())
464 const auto &ProfCalleeId = It->second;
465 if (IRCalleeId == ProfCalleeId) {
466 auto It = CallsiteMatchStates.find(ProfileLoc);
467 if (It == CallsiteMatchStates.end())
468 CallsiteMatchStates.try_emplace(ProfileLoc, MatchState::InitialMatch);
469 else if (IsPostMatch) {
470 if (It->second == MatchState::InitialMatch)
471 It->second = MatchState::UnchangedMatch;
472 else if (It->second == MatchState::InitialMismatch)
473 It->second = MatchState::RecoveredMismatch;
480 for (
const auto &
I : ProfileAnchors) {
481 const auto &Loc =
I.first;
482 assert(!
I.second.stringRef().empty() &&
"Callees should not be empty");
483 auto It = CallsiteMatchStates.find(Loc);
484 if (It == CallsiteMatchStates.end())
485 CallsiteMatchStates.try_emplace(Loc, MatchState::InitialMismatch);
486 else if (IsPostMatch) {
489 if (It->second == MatchState::InitialMismatch)
490 It->second = MatchState::UnchangedMismatch;
491 else if (It->second == MatchState::InitialMatch)
492 It->second = MatchState::RemovedMatch;
497void SampleProfileMatcher::countMismatchedFuncSamples(
const FunctionSamples &FS,
499 const auto *FuncDesc = ProbeManager->getDesc(
FS.getGUID());
504 if (ProbeManager->profileIsHashMismatched(*FuncDesc, FS)) {
506 NumStaleProfileFunc++;
511 MismatchedFunctionSamples +=
FS.getTotalSamples();
520 for (
const auto &
I :
FS.getCallsiteSamples())
521 for (
const auto &CS :
I.second)
522 countMismatchedFuncSamples(CS.second,
false);
525void SampleProfileMatcher::countMismatchedCallsiteSamples(
527 auto It = FuncCallsiteMatchStates.find(
FS.getFuncName());
529 if (It == FuncCallsiteMatchStates.end() || It->second.empty())
531 const auto &CallsiteMatchStates = It->second;
533 auto findMatchState = [&](
const LineLocation &Loc) {
534 auto It = CallsiteMatchStates.find(Loc);
535 if (It == CallsiteMatchStates.end())
536 return MatchState::Unknown;
540 auto AttributeMismatchedSamples = [&](
const enum MatchState &State,
542 if (isMismatchState(State))
543 MismatchedCallsiteSamples += Samples;
544 else if (State == MatchState::RecoveredMismatch)
545 RecoveredCallsiteSamples += Samples;
550 for (
const auto &
I :
FS.getBodySamples())
551 AttributeMismatchedSamples(findMatchState(
I.first),
I.second.getSamples());
554 for (
const auto &
I :
FS.getCallsiteSamples()) {
555 auto State = findMatchState(
I.first);
556 uint64_t CallsiteSamples = 0;
557 for (
const auto &CS :
I.second)
558 CallsiteSamples += CS.second.getTotalSamples();
559 AttributeMismatchedSamples(State, CallsiteSamples);
561 if (isMismatchState(State))
567 for (
const auto &CS :
I.second)
568 countMismatchedCallsiteSamples(CS.second);
572void SampleProfileMatcher::countMismatchCallsites(
const FunctionSamples &FS) {
573 auto It = FuncCallsiteMatchStates.find(
FS.getFuncName());
575 if (It == FuncCallsiteMatchStates.end() || It->second.empty())
577 const auto &MatchStates = It->second;
578 [[maybe_unused]]
bool OnInitialState =
579 isInitialState(MatchStates.begin()->second);
580 for (
const auto &
I : MatchStates) {
581 TotalProfiledCallsites++;
583 (OnInitialState ? isInitialState(
I.second) : isFinalState(
I.second)) &&
584 "Profile matching state is inconsistent");
586 if (isMismatchState(
I.second))
587 NumMismatchedCallsites++;
588 else if (
I.second == MatchState::RecoveredMismatch)
589 NumRecoveredCallsites++;
593void SampleProfileMatcher::countCallGraphRecoveredSamples(
596 if (CallGraphRecoveredProfiles.
count(
FS.getFunction())) {
597 NumCallGraphRecoveredFuncSamples +=
FS.getTotalSamples();
601 for (
const auto &CM :
FS.getCallsiteSamples()) {
602 for (
const auto &CS : CM.second) {
603 countCallGraphRecoveredSamples(CS.second, CallGraphRecoveredProfiles);
608void SampleProfileMatcher::computeAndReportProfileStaleness() {
612 DenseSet<FunctionId> CallGraphRecoveredProfiles;
614 for (
const auto &
I : FuncToProfileNameMap) {
615 CallGraphRecoveredProfiles.
insert(
I.second);
618 NumCallGraphRecoveredProfiledFunc++;
623 for (
const auto &
F : M) {
630 const auto *
FS = Reader.getSamplesFor(
F);
634 TotalFunctionSamples +=
FS->getTotalSamples();
637 countCallGraphRecoveredSamples(*FS, CallGraphRecoveredProfiles);
641 countMismatchedFuncSamples(*FS,
true);
644 countMismatchCallsites(*FS);
645 countMismatchedCallsiteSamples(*FS);
650 errs() <<
"(" << NumStaleProfileFunc <<
"/" << TotalProfiledFunc
651 <<
") of functions' profile are invalid and ("
652 << MismatchedFunctionSamples <<
"/" << TotalFunctionSamples
653 <<
") of samples are discarded due to function hash mismatch.\n";
656 errs() <<
"(" << NumCallGraphRecoveredProfiledFunc <<
"/"
657 << TotalProfiledFunc <<
") of functions' profile are matched and ("
658 << NumCallGraphRecoveredFuncSamples <<
"/" << TotalFunctionSamples
659 <<
") of samples are reused by call graph matching.\n";
662 errs() <<
"(" << (NumMismatchedCallsites + NumRecoveredCallsites) <<
"/"
663 << TotalProfiledCallsites
664 <<
") of callsites' profile are invalid and ("
665 << (MismatchedCallsiteSamples + RecoveredCallsiteSamples) <<
"/"
666 << TotalFunctionSamples
667 <<
") of samples are discarded due to callsite location mismatch.\n";
668 errs() <<
"(" << NumRecoveredCallsites <<
"/"
669 << (NumRecoveredCallsites + NumMismatchedCallsites)
670 <<
") of callsites and (" << RecoveredCallsiteSamples <<
"/"
671 << (RecoveredCallsiteSamples + MismatchedCallsiteSamples)
672 <<
") of samples are recovered by stale profile matching.\n";
676 LLVMContext &Ctx = M.getContext();
681 ProfStatsVec.
emplace_back(
"NumStaleProfileFunc", NumStaleProfileFunc);
682 ProfStatsVec.
emplace_back(
"TotalProfiledFunc", TotalProfiledFunc);
684 MismatchedFunctionSamples);
685 ProfStatsVec.
emplace_back(
"TotalFunctionSamples", TotalFunctionSamples);
689 ProfStatsVec.
emplace_back(
"NumCallGraphRecoveredProfiledFunc",
690 NumCallGraphRecoveredProfiledFunc);
691 ProfStatsVec.
emplace_back(
"NumCallGraphRecoveredFuncSamples",
692 NumCallGraphRecoveredFuncSamples);
695 ProfStatsVec.
emplace_back(
"NumMismatchedCallsites", NumMismatchedCallsites);
696 ProfStatsVec.
emplace_back(
"NumRecoveredCallsites", NumRecoveredCallsites);
697 ProfStatsVec.
emplace_back(
"TotalProfiledCallsites", TotalProfiledCallsites);
699 MismatchedCallsiteSamples);
701 RecoveredCallsiteSamples);
703 auto *MD = MDB.createLLVMStats(ProfStatsVec);
704 auto *NMD = M.getOrInsertNamedMetadata(
"llvm.stats");
709void SampleProfileMatcher::findFunctionsWithoutProfile() {
713 StringSet<> NamesInProfile;
714 for (FunctionId Name : Reader.getNameTable())
720 if (
F.isDeclaration())
724 const auto *
FS = getFlattenedSamplesFor(
F);
731 if (NamesInProfile.
count(CanonFName))
736 if (PSL && PSL->contains(CanonFName))
740 <<
" is not in profile or profile symbol list.\n");
741 FunctionsWithoutProfile[FunctionId(CanonFName)] = &
F;
749 auto FunctionName = FName.
str();
750 if (Demangler.partialDemangle(FunctionName.c_str()))
751 return std::string();
752 size_t BaseNameSize = 0;
756 char *BaseNamePtr = Demangler.getFunctionBaseName(
nullptr, &BaseNameSize);
757 std::string Result = (BaseNamePtr && BaseNameSize)
758 ? std::string(BaseNamePtr, BaseNameSize)
763 while (!Result.empty() && (Result.back() ==
' ' || Result.back() ==
'\0'))
768void SampleProfileMatcher::matchFunctionsWithoutProfileByBasename() {
771 auto NameTable = Reader.getNameTable();
772 if (NameTable.empty())
780 StringMap<Function *> OrphansByBaseName;
781 StringSet<> AmbiguousBaseNames;
782 for (
auto &[FuncId, Func] : FunctionsWithoutProfile) {
784 if (BaseName.empty() || AmbiguousBaseNames.
count(BaseName))
789 OrphansByBaseName.
erase(It);
790 AmbiguousBaseNames.
insert(BaseName);
793 if (OrphansByBaseName.
empty())
798 StringMap<FunctionId> CandidateByBaseName;
799 for (FunctionId ProfileFuncId : NameTable) {
800 StringRef ProfName = ProfileFuncId.stringRef();
801 if (ProfName.
empty())
805 if (ProfBaseName.empty())
808 if (OrphansByBaseName.
count(ProfBaseName)) {
809 if (AmbiguousBaseNames.
count(ProfBaseName))
813 CandidateByBaseName.
try_emplace(ProfBaseName, ProfileFuncId);
816 CandidateByBaseName.
erase(It);
817 AmbiguousBaseNames.
insert(ProfBaseName);
822 if (CandidateByBaseName.
empty())
826 DenseSet<StringRef> ToLoad;
827 for (
auto &[BaseName, ProfId] : CandidateByBaseName)
828 ToLoad.
insert(ProfId.stringRef());
831 unsigned MatchCount = 0;
832 SampleProfileMap NewlyLoadedProfiles;
833 for (
auto &[BaseName, ProfId] : CandidateByBaseName) {
834 if (!isProfileUnused(ProfId))
840 FuncToProfileNameMap[OrphanFunc] = ProfId;
841 if (
const auto *FS = Reader.getSamplesFor(ProfId.stringRef()))
845 <<
" (IR) -> " << ProfId <<
" (Profile)"
846 <<
" [basename: " << BaseName <<
"]\n");
851 if (!NewlyLoadedProfiles.empty())
855 NumDirectProfileMatch += MatchCount;
856 LLVM_DEBUG(
dbgs() <<
"Direct basename matching found " << MatchCount
860bool SampleProfileMatcher::functionMatchesProfileHelper(
864 float Similarity = 0.0;
871 if (!IRBaseName.empty() && IRBaseName == ProfBaseName) {
873 << ProfFunc <<
"(Profile) share the same base name: "
874 << IRBaseName <<
".\n");
878 const auto *FSForMatching = getFlattenedSamplesFor(ProfFunc);
885 DenseSet<StringRef> TopLevelFunc({ProfFunc.
stringRef()});
886 if (std::error_code EC = Reader.read(TopLevelFunc))
888 FSForMatching = Reader.getSamplesFor(ProfFunc.
stringRef());
893 SampleProfileMap TempProfiles;
894 TempProfiles.
create(FSForMatching->getFunction()).
merge(*FSForMatching);
897 FSForMatching = getFlattenedSamplesFor(ProfFunc);
901 dbgs() <<
"Read top-level function " << ProfFunc
902 <<
" for call-graph matching\n";
917 const auto *FuncDesc = ProbeManager->getDesc(IRFunc);
919 !ProbeManager->profileIsHashMismatched(*FuncDesc, *FSForMatching)) {
921 <<
"(IR) and " << ProfFunc <<
"(Profile) match.\n");
928 findIRAnchors(IRFunc, IRAnchors);
930 findProfileAnchors(*FSForMatching, ProfileAnchors);
934 getFilteredAnchorList(IRAnchors, ProfileAnchors, FilteredIRAnchorsList,
935 FilteredProfileAnchorList);
948 longestCommonSequence(FilteredIRAnchorsList, FilteredProfileAnchorList,
951 Similarity =
static_cast<float>(MatchedAnchors.
size()) /
952 FilteredProfileAnchorList.size();
955 <<
"(IR) and " << ProfFunc <<
"(profile) is "
956 <<
format(
"%.2f", Similarity) <<
"\n");
957 assert((Similarity >= 0 && Similarity <= 1.0) &&
958 "Similarity value should be in [0, 1]");
964bool SampleProfileMatcher::functionMatchesProfile(
Function &IRFunc,
966 bool FindMatchedProfileOnly) {
967 auto R = FuncProfileMatchCache.find({&IRFunc, ProfFunc});
968 if (R != FuncProfileMatchCache.end())
971 if (FindMatchedProfileOnly)
974 bool Matched = functionMatchesProfileHelper(IRFunc, ProfFunc);
975 FuncProfileMatchCache[{&IRFunc, ProfFunc}] = Matched;
977 FuncToProfileNameMap[&IRFunc] = ProfFunc;
979 <<
" matches profile:" << ProfFunc <<
"\n");
985void SampleProfileMatcher::UpdateWithSalvagedProfiles() {
986 DenseSet<StringRef> ProfileSalvagedFuncs;
988 for (
auto &
I : FuncToProfileNameMap) {
989 assert(
I.first &&
"New function is null");
990 FunctionId FuncName(
I.first->getName());
991 ProfileSalvagedFuncs.
insert(
I.second.stringRef());
992 FuncNameToProfNameMap->emplace(FuncName,
I.second);
996 SymbolMap->erase(FuncName);
997 [[maybe_unused]]
auto Ret = SymbolMap->emplace(
I.second,
I.first);
1000 dbgs() <<
"Profile Function " <<
I.second
1001 <<
" has already been matched to another IR function.\n";
1009 Reader.read(ProfileSalvagedFuncs);
1010 Reader.setFuncNameToProfNameMap(*FuncNameToProfNameMap);
1022 findFunctionsWithoutProfile();
1023 matchFunctionsWithoutProfileByBasename();
1028 std::vector<Function *> TopDownFunctionList;
1029 TopDownFunctionList.reserve(M.size());
1031 for (
auto *
F : TopDownFunctionList) {
1038 UpdateWithSalvagedProfiles();
1041 distributeIRToProfileLocationMap();
1043 computeAndReportProfileStaleness();
1046void SampleProfileMatcher::distributeIRToProfileLocationMap(
1048 const auto ProfileMappings = FuncMappings.find(FS.getFuncName());
1049 if (ProfileMappings != FuncMappings.end()) {
1050 FS.setIRToProfileLocationMap(&(ProfileMappings->second));
1053 for (
auto &Callees :
1055 for (
auto &FS : Callees.second) {
1056 distributeIRToProfileLocationMap(FS.second);
1063void SampleProfileMatcher::distributeIRToProfileLocationMap() {
1064 for (
auto &
I : Reader.getProfiles()) {
1065 distributeIRToProfileLocationMap(
I.second);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
itanium_demangle::ManglingParser< DefaultAllocator > Demangler
Legalize the Machine IR a function s Machine IR
static std::string getDemangledBaseName(ItaniumPartialDemangler &Demangler, StringRef FName)
This file provides the interface for SampleProfileMatcher.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
bool erase(const KeyT &Val)
std::pair< iterator, bool > insert_or_assign(const KeyT &Key, V &&Val)
Implements a dense probed hash-table based set.
static bool isAvailableExternallyLinkage(LinkageTypes Linkage)
LLVM_ABI void runOnModule()
reference emplace_back(ArgTypes &&... Args)
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
ValueTy lookup(StringRef Key) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
std::pair< iterator, bool > try_emplace(StringRef Key, ArgsTy &&...Args)
Emplace a new element for the specified key into the map if the key isn't already in the map.
Represent a constant reference to a string, i.e.
std::string str() const
Get the contents as an std::string.
constexpr bool empty() const
Check if the string is empty.
std::pair< typename Base::iterator, bool > insert(StringRef key)
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
std::pair< iterator, bool > insert(const ValueT &V)
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
This class represents a function that is read from a sample profile.
StringRef stringRef() const
Convert to StringRef.
bool isStringRef() const
Check if this object represents a StringRef, or a hash code.
Representation of the samples collected for a function.
static LLVM_ABI bool ProfileIsCS
static LLVM_ABI bool ProfileIsProbeBased
static StringRef getCanonicalFnName(const Function &F)
Return the canonical name for a function, taking into account suffix elision policy attributes.
static LLVM_ABI bool ProfileIsFS
If this profile uses flow sensitive discriminators.
sampleprof_error merge(const FunctionSamples &Other, uint64_t Weight=1)
Merge the samples in Other into this one.
static LLVM_ABI LineLocation getCallSiteIdentifier(const DILocation *DIL, bool ProfileIsFS=false)
Returns a unique call site identifier for a given debug location of a call instruction.
static LLVM_ABI bool UseMD5
Whether the profile uses MD5 to represent string.
static void flattenProfile(SampleProfileMap &ProfileMap, bool ProfileIsCS=false)
mapped_type & create(const SampleContext &Ctx)
@ C
The default llvm calling convention, compatible with C.
initializer< Ty > init(const Ty &Val)
NodeAddr< FuncNode * > Func
std::map< LineLocation, FunctionSamplesMap > CallsiteSampleMap
DenseMap< LineLocation, LineLocation > LocToLocMap
This is an optimization pass for GlobalISel generic memory operations.
cl::opt< bool > ReportProfileStaleness("report-profile-staleness", cl::Hidden, cl::init(false), cl::desc("Compute and report stale profile statistical metrics."))
cl::opt< bool > PersistProfileStaleness("persist-profile-staleness", cl::Hidden, cl::init(false), cl::desc("Compute stale profile statistical metrics and write it into the " "native object file(.llvm_stats section)."))
std::map< LineLocation, FunctionId > AnchorMap
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
static cl::opt< bool > LoadFuncProfileforCGMatching("load-func-profile-for-cg-matching", cl::Hidden, cl::init(true), cl::desc("Load top-level profiles that the sample reader initially skipped for " "the call-graph matching (only meaningful for extended binary " "format)"))
static cl::opt< unsigned > SalvageUnusedProfileMaxFunctions("salvage-unused-profile-max-functions", cl::Hidden, cl::init(UINT_MAX), cl::desc("The maximum number of functions in a module, above which salvage " "unused profile will be skipped."))
static void buildTopDownFuncOrder(LazyCallGraph &CG, std::vector< Function * > &FunctionOrderList)
@ ThinLTOPreLink
ThinLTO prelink (summary) phase.
static cl::opt< unsigned > MinCallCountForCGMatching("min-call-count-for-cg-matching", cl::Hidden, cl::init(3), cl::desc("The minimum number of call anchors required for a function to " "run stale profile call graph matching."))
LLVM_ABI std::optional< PseudoProbe > extractProbe(const Instruction &Inst)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static cl::opt< unsigned > MinFuncCountForCGMatching("min-func-count-for-cg-matching", cl::Hidden, cl::init(5), cl::desc("The minimum number of basic blocks required for a function to " "run stale profile call graph matching."))
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
cl::opt< bool > SalvageStaleProfile("salvage-stale-profile", cl::Hidden, cl::init(false), cl::desc("Salvage stale profile by fuzzy matching and use the remapped " "location for sample profile query."))
void longestCommonSequence(AnchorList AnchorList1, AnchorList AnchorList2, llvm::function_ref< bool(const Function &, const Function &)> FunctionMatchesProfile, llvm::function_ref< void(Loc, Loc)> InsertMatching)
std::vector< std::pair< LineLocation, FunctionId > > AnchorList
static bool skipProfileForFunction(const Function &F)
cl::opt< bool > SalvageUnusedProfile("salvage-unused-profile", cl::Hidden, cl::init(false), cl::desc("Salvage unused profile by matching with new " "functions on call graph."))
static cl::opt< unsigned > SalvageStaleProfileMaxCallsites("salvage-stale-profile-max-callsites", cl::Hidden, cl::init(UINT_MAX), cl::desc("The maximum number of callsites in a function, above which stale " "profile matching will be skipped."))
static cl::opt< unsigned > FuncProfileSimilarityThreshold("func-profile-similarity-threshold", cl::Hidden, cl::init(80), cl::desc("Consider a profile matches a function if the similarity of their " "callee sequences is above the specified percentile."))