22#include <unordered_set>
27#define DEBUG_TYPE "sample-profile-matcher"
30 "Number of functions matched by demangled basename");
36 cl::desc(
"Consider a profile matches a function if the similarity of their "
37 "callee sequences is above the specified percentile."));
41 cl::desc(
"The minimum number of basic blocks required for a function to "
42 "run stale profile call graph matching."));
46 cl::desc(
"The minimum number of call anchors required for a function to "
47 "run stale profile call graph matching."));
52 "Load top-level profiles that the sample reader initially skipped for "
53 "the call-graph matching (only meaningful for extended binary "
63 cl::desc(
"The maximum number of callsites in a function, above which stale "
64 "profile matching will be skipped."));
68void SampleProfileMatcher::findIRAnchors(
const Function &
F,
73 auto FindTopLevelInlinedCallsite = [](
const DILocation *DIL) {
74 assert((DIL && DIL->getInlinedAt()) &&
"No inlined callsite");
78 DIL = DIL->getInlinedAt();
79 }
while (DIL->getInlinedAt());
83 StringRef CalleeName = PrevDIL->getSubprogramLinkageName();
84 return std::make_pair(Callsite, FunctionId(CalleeName));
87 auto GetCanonicalCalleeName = [](
const CallBase *CB) {
88 StringRef CalleeName = UnknownIndirectCallee;
89 if (Function *Callee = CB->getCalledFunction())
97 DILocation *DIL =
I.getDebugLoc();
104 if (DIL->getInlinedAt()) {
105 IRAnchors.emplace(FindTopLevelInlinedCallsite(DIL));
108 StringRef CalleeName;
112 CalleeName = GetCanonicalCalleeName(CB);
114 LineLocation Loc = LineLocation(Probe->Id, 0);
115 IRAnchors.emplace(Loc, FunctionId(CalleeName));
125 if (DIL->getInlinedAt()) {
126 IRAnchors.emplace(FindTopLevelInlinedCallsite(DIL));
131 IRAnchors.emplace(Callsite, FunctionId(CalleeName));
138void SampleProfileMatcher::findProfileAnchors(
const FunctionSamples &FS,
140 auto isInvalidLineOffset = [](uint32_t LineOffset) {
141 return LineOffset & 0x8000;
144 auto InsertAnchor = [](
const LineLocation &Loc,
const FunctionId &CalleeName,
146 auto Ret = ProfileAnchors.try_emplace(Loc, CalleeName);
150 Ret.first->second = FunctionId(UnknownIndirectCallee);
154 for (
const auto &
I :
FS.getBodySamples()) {
155 const LineLocation &Loc =
I.first;
158 for (
const auto &
C :
I.second.getCallTargets())
159 InsertAnchor(Loc,
C.first, ProfileAnchors);
162 for (
const auto &
I :
FS.getCallsiteSamples()) {
163 const LineLocation &Loc =
I.first;
166 for (
const auto &
C :
I.second)
167 InsertAnchor(Loc,
C.first, ProfileAnchors);
171bool SampleProfileMatcher::functionHasProfile(
const FunctionId &IRFuncName,
173 FuncWithoutProfile =
nullptr;
174 auto R = FunctionsWithoutProfile.find(IRFuncName);
175 if (R != FunctionsWithoutProfile.end())
176 FuncWithoutProfile =
R->second;
177 return !FuncWithoutProfile;
180bool SampleProfileMatcher::isProfileUnused(
const FunctionId &ProfileFuncName) {
181 return SymbolMap->find(ProfileFuncName) == SymbolMap->
end();
184bool SampleProfileMatcher::functionMatchesProfile(
186 bool FindMatchedProfileOnly) {
187 if (IRFuncName == ProfileFuncName)
195 if (functionHasProfile(IRFuncName, IRFunc) ||
196 !isProfileUnused(ProfileFuncName))
200 "IR function should be different from profile function to match");
201 return functionMatchesProfile(*IRFunc, ProfileFuncName,
202 FindMatchedProfileOnly);
206SampleProfileMatcher::longestCommonSequence(
const AnchorList &AnchorList1,
208 bool MatchUnusedFunction) {
211 AnchorList1, AnchorList2,
212 [&](
const FunctionId &
A,
const FunctionId &
B) {
213 return functionMatchesProfile(
218 [&](LineLocation
A, LineLocation
B) {
219 MatchedAnchors.try_emplace(
A,
B);
221 return MatchedAnchors;
224void SampleProfileMatcher::matchNonCallsiteLocs(
227 auto InsertMatching = [&](
const LineLocation &From,
const LineLocation &To) {
230 IRToProfileLocationMap.insert({From, To});
234 int32_t LocationDelta = 0;
236 for (
const auto &
IR : IRAnchors) {
237 const auto &Loc =
IR.first;
238 bool IsMatchedAnchor =
false;
240 auto R = MatchedAnchors.find(Loc);
241 if (R != MatchedAnchors.end()) {
242 const auto &Candidate =
R->second;
243 InsertMatching(Loc, Candidate);
245 <<
" is matched from " << Loc <<
" to " << Candidate
247 LocationDelta = Candidate.LineOffset - Loc.
LineOffset;
253 for (
size_t I = (LastMatchedNonAnchors.
size() + 1) / 2;
254 I < LastMatchedNonAnchors.
size();
I++) {
255 const auto &
L = LastMatchedNonAnchors[
I];
256 uint32_t CandidateLineOffset =
L.LineOffset + LocationDelta;
257 LineLocation Candidate(CandidateLineOffset,
L.Discriminator);
258 InsertMatching(L, Candidate);
260 <<
" to " << Candidate <<
"\n");
263 IsMatchedAnchor =
true;
264 LastMatchedNonAnchors.
clear();
268 if (!IsMatchedAnchor) {
269 uint32_t CandidateLineOffset = Loc.
LineOffset + LocationDelta;
270 LineLocation Candidate(CandidateLineOffset, Loc.
Discriminator);
271 InsertMatching(Loc, Candidate);
273 << Candidate <<
"\n");
281void SampleProfileMatcher::getFilteredAnchorList(
284 for (
const auto &
I : IRAnchors) {
285 if (
I.second.stringRef().empty())
287 FilteredIRAnchorsList.emplace_back(
I);
290 for (
const auto &
I : ProfileAnchors)
291 FilteredProfileAnchorList.emplace_back(
I);
311void SampleProfileMatcher::runStaleProfileMatching(
314 bool RunCFGMatching,
bool RunCGMatching) {
315 if (!RunCFGMatching && !RunCGMatching)
319 assert(IRToProfileLocationMap.empty() &&
320 "Run stale profile matching only once per function");
324 getFilteredAnchorList(IRAnchors, ProfileAnchors, FilteredIRAnchorsList,
325 FilteredProfileAnchorList);
327 if (FilteredIRAnchorsList.empty() || FilteredProfileAnchorList.empty())
333 <<
" because the number of callsites in the IR is "
334 << FilteredIRAnchorsList.size()
335 <<
" and in the profile is "
336 << FilteredProfileAnchorList.size() <<
"\n");
351 longestCommonSequence(FilteredIRAnchorsList, FilteredProfileAnchorList,
359 matchNonCallsiteLocs(MatchedAnchors, IRAnchors, IRToProfileLocationMap);
362void SampleProfileMatcher::runOnFunction(
Function &
F) {
369 const auto *FSForMatching = getFlattenedSamplesFor(
F);
372 auto R = FuncToProfileNameMap.find(&
F);
373 if (R != FuncToProfileNameMap.end()) {
374 FSForMatching = getFlattenedSamplesFor(
R->second);
379 FSForMatching = Reader.getSamplesFor(
R->second.stringRef());
389 findIRAnchors(
F, IRAnchors);
393 findProfileAnchors(*FSForMatching, ProfileAnchors);
397 recordCallsiteMatchStates(
F, IRAnchors, ProfileAnchors,
nullptr);
404 !ProbeManager->profileIsValid(
F, *FSForMatching);
405 bool RunCFGMatching =
413 F.addFnAttr(
"profile-checksum-mismatch");
417 auto &IRToProfileLocationMap = getIRToProfileLocationMap(
F);
418 runStaleProfileMatching(
F, IRAnchors, ProfileAnchors, IRToProfileLocationMap,
419 RunCFGMatching, RunCGMatching);
422 recordCallsiteMatchStates(
F, IRAnchors, ProfileAnchors,
423 &IRToProfileLocationMap);
426void SampleProfileMatcher::recordCallsiteMatchStates(
430 bool IsPostMatch = IRToProfileLocationMap !=
nullptr;
431 auto &CallsiteMatchStates =
434 auto MapIRLocToProfileLoc = [&](
const LineLocation &IRLoc) {
436 if (!IRToProfileLocationMap)
438 const auto &ProfileLoc = IRToProfileLocationMap->find(IRLoc);
439 if (ProfileLoc != IRToProfileLocationMap->end())
440 return ProfileLoc->second;
445 for (
const auto &
I : IRAnchors) {
448 const auto &ProfileLoc = MapIRLocToProfileLoc(
I.first);
449 const auto &IRCalleeId =
I.second;
450 const auto &It = ProfileAnchors.find(ProfileLoc);
451 if (It == ProfileAnchors.end())
453 const auto &ProfCalleeId = It->second;
454 if (IRCalleeId == ProfCalleeId) {
455 auto It = CallsiteMatchStates.find(ProfileLoc);
456 if (It == CallsiteMatchStates.end())
457 CallsiteMatchStates.emplace(ProfileLoc, MatchState::InitialMatch);
458 else if (IsPostMatch) {
459 if (It->second == MatchState::InitialMatch)
460 It->second = MatchState::UnchangedMatch;
461 else if (It->second == MatchState::InitialMismatch)
462 It->second = MatchState::RecoveredMismatch;
469 for (
const auto &
I : ProfileAnchors) {
470 const auto &Loc =
I.first;
471 assert(!
I.second.stringRef().empty() &&
"Callees should not be empty");
472 auto It = CallsiteMatchStates.find(Loc);
473 if (It == CallsiteMatchStates.end())
474 CallsiteMatchStates.emplace(Loc, MatchState::InitialMismatch);
475 else if (IsPostMatch) {
478 if (It->second == MatchState::InitialMismatch)
479 It->second = MatchState::UnchangedMismatch;
480 else if (It->second == MatchState::InitialMatch)
481 It->second = MatchState::RemovedMatch;
486void SampleProfileMatcher::countMismatchedFuncSamples(
const FunctionSamples &FS,
488 const auto *FuncDesc = ProbeManager->getDesc(
FS.getGUID());
493 if (ProbeManager->profileIsHashMismatched(*FuncDesc, FS)) {
495 NumStaleProfileFunc++;
500 MismatchedFunctionSamples +=
FS.getTotalSamples();
509 for (
const auto &
I :
FS.getCallsiteSamples())
510 for (
const auto &CS :
I.second)
511 countMismatchedFuncSamples(CS.second,
false);
514void SampleProfileMatcher::countMismatchedCallsiteSamples(
516 auto It = FuncCallsiteMatchStates.find(
FS.getFuncName());
518 if (It == FuncCallsiteMatchStates.end() || It->second.empty())
520 const auto &CallsiteMatchStates = It->second;
522 auto findMatchState = [&](
const LineLocation &Loc) {
523 auto It = CallsiteMatchStates.find(Loc);
524 if (It == CallsiteMatchStates.end())
525 return MatchState::Unknown;
529 auto AttributeMismatchedSamples = [&](
const enum MatchState &State,
531 if (isMismatchState(State))
532 MismatchedCallsiteSamples += Samples;
533 else if (State == MatchState::RecoveredMismatch)
534 RecoveredCallsiteSamples += Samples;
539 for (
const auto &
I :
FS.getBodySamples())
540 AttributeMismatchedSamples(findMatchState(
I.first),
I.second.getSamples());
543 for (
const auto &
I :
FS.getCallsiteSamples()) {
544 auto State = findMatchState(
I.first);
545 uint64_t CallsiteSamples = 0;
546 for (
const auto &CS :
I.second)
547 CallsiteSamples += CS.second.getTotalSamples();
548 AttributeMismatchedSamples(State, CallsiteSamples);
550 if (isMismatchState(State))
556 for (
const auto &CS :
I.second)
557 countMismatchedCallsiteSamples(CS.second);
561void SampleProfileMatcher::countMismatchCallsites(
const FunctionSamples &FS) {
562 auto It = FuncCallsiteMatchStates.find(
FS.getFuncName());
564 if (It == FuncCallsiteMatchStates.end() || It->second.empty())
566 const auto &MatchStates = It->second;
567 [[maybe_unused]]
bool OnInitialState =
568 isInitialState(MatchStates.begin()->second);
569 for (
const auto &
I : MatchStates) {
570 TotalProfiledCallsites++;
572 (OnInitialState ? isInitialState(
I.second) : isFinalState(
I.second)) &&
573 "Profile matching state is inconsistent");
575 if (isMismatchState(
I.second))
576 NumMismatchedCallsites++;
577 else if (
I.second == MatchState::RecoveredMismatch)
578 NumRecoveredCallsites++;
582void SampleProfileMatcher::countCallGraphRecoveredSamples(
584 std::unordered_set<FunctionId> &CallGraphRecoveredProfiles) {
585 if (CallGraphRecoveredProfiles.count(
FS.getFunction())) {
586 NumCallGraphRecoveredFuncSamples +=
FS.getTotalSamples();
590 for (
const auto &CM :
FS.getCallsiteSamples()) {
591 for (
const auto &CS : CM.second) {
592 countCallGraphRecoveredSamples(CS.second, CallGraphRecoveredProfiles);
597void SampleProfileMatcher::computeAndReportProfileStaleness() {
601 std::unordered_set<FunctionId> CallGraphRecoveredProfiles;
603 for (
const auto &
I : FuncToProfileNameMap) {
604 CallGraphRecoveredProfiles.insert(
I.second);
607 NumCallGraphRecoveredProfiledFunc++;
612 for (
const auto &
F : M) {
619 const auto *
FS = Reader.getSamplesFor(
F);
623 TotalFunctionSamples +=
FS->getTotalSamples();
626 countCallGraphRecoveredSamples(*FS, CallGraphRecoveredProfiles);
630 countMismatchedFuncSamples(*FS,
true);
633 countMismatchCallsites(*FS);
634 countMismatchedCallsiteSamples(*FS);
639 errs() <<
"(" << NumStaleProfileFunc <<
"/" << TotalProfiledFunc
640 <<
") of functions' profile are invalid and ("
641 << MismatchedFunctionSamples <<
"/" << TotalFunctionSamples
642 <<
") of samples are discarded due to function hash mismatch.\n";
645 errs() <<
"(" << NumCallGraphRecoveredProfiledFunc <<
"/"
646 << TotalProfiledFunc <<
") of functions' profile are matched and ("
647 << NumCallGraphRecoveredFuncSamples <<
"/" << TotalFunctionSamples
648 <<
") of samples are reused by call graph matching.\n";
651 errs() <<
"(" << (NumMismatchedCallsites + NumRecoveredCallsites) <<
"/"
652 << TotalProfiledCallsites
653 <<
") of callsites' profile are invalid and ("
654 << (MismatchedCallsiteSamples + RecoveredCallsiteSamples) <<
"/"
655 << TotalFunctionSamples
656 <<
") of samples are discarded due to callsite location mismatch.\n";
657 errs() <<
"(" << NumRecoveredCallsites <<
"/"
658 << (NumRecoveredCallsites + NumMismatchedCallsites)
659 <<
") of callsites and (" << RecoveredCallsiteSamples <<
"/"
660 << (RecoveredCallsiteSamples + MismatchedCallsiteSamples)
661 <<
") of samples are recovered by stale profile matching.\n";
665 LLVMContext &Ctx = M.getContext();
670 ProfStatsVec.
emplace_back(
"NumStaleProfileFunc", NumStaleProfileFunc);
671 ProfStatsVec.
emplace_back(
"TotalProfiledFunc", TotalProfiledFunc);
673 MismatchedFunctionSamples);
674 ProfStatsVec.
emplace_back(
"TotalFunctionSamples", TotalFunctionSamples);
678 ProfStatsVec.
emplace_back(
"NumCallGraphRecoveredProfiledFunc",
679 NumCallGraphRecoveredProfiledFunc);
680 ProfStatsVec.
emplace_back(
"NumCallGraphRecoveredFuncSamples",
681 NumCallGraphRecoveredFuncSamples);
684 ProfStatsVec.
emplace_back(
"NumMismatchedCallsites", NumMismatchedCallsites);
685 ProfStatsVec.
emplace_back(
"NumRecoveredCallsites", NumRecoveredCallsites);
686 ProfStatsVec.
emplace_back(
"TotalProfiledCallsites", TotalProfiledCallsites);
688 MismatchedCallsiteSamples);
690 RecoveredCallsiteSamples);
692 auto *MD = MDB.createLLVMStats(ProfStatsVec);
693 auto *NMD = M.getOrInsertNamedMetadata(
"llvm.stats");
698void SampleProfileMatcher::findFunctionsWithoutProfile() {
702 StringSet<> NamesInProfile;
703 if (
auto NameTable = Reader.getNameTable()) {
704 for (
auto Name : *NameTable)
711 if (
F.isDeclaration())
715 const auto *
FS = getFlattenedSamplesFor(
F);
722 if (NamesInProfile.
count(CanonFName))
727 if (PSL && PSL->contains(CanonFName))
731 <<
" is not in profile or profile symbol list.\n");
732 FunctionsWithoutProfile[FunctionId(CanonFName)] = &
F;
740 auto FunctionName = FName.
str();
741 if (Demangler.partialDemangle(FunctionName.c_str()))
742 return std::string();
743 size_t BaseNameSize = 0;
747 char *BaseNamePtr = Demangler.getFunctionBaseName(
nullptr, &BaseNameSize);
748 std::string Result = (BaseNamePtr && BaseNameSize)
749 ? std::string(BaseNamePtr, BaseNameSize)
754 while (!Result.empty() && (Result.back() ==
' ' || Result.back() ==
'\0'))
759void SampleProfileMatcher::matchFunctionsWithoutProfileByBasename() {
762 auto *NameTable = Reader.getNameTable();
771 StringMap<Function *> OrphansByBaseName;
772 StringSet<> AmbiguousBaseNames;
773 for (
auto &[FuncId, Func] : FunctionsWithoutProfile) {
775 if (BaseName.empty() || AmbiguousBaseNames.
count(BaseName))
780 OrphansByBaseName.
erase(It);
781 AmbiguousBaseNames.
insert(BaseName);
784 if (OrphansByBaseName.
empty())
792 StringMap<FunctionId> CandidateByBaseName;
793 for (
auto &ProfileFuncId : *NameTable) {
794 StringRef ProfName = ProfileFuncId.stringRef();
795 if (ProfName.
empty())
797 for (
auto &[BaseName,
_] : OrphansByBaseName) {
798 if (AmbiguousBaseNames.
count(BaseName) || !ProfName.
contains(BaseName))
801 if (ProfBaseName != BaseName)
804 CandidateByBaseName.
try_emplace(BaseName, ProfileFuncId);
807 CandidateByBaseName.
erase(It);
808 AmbiguousBaseNames.
insert(BaseName);
813 if (CandidateByBaseName.
empty())
817 DenseSet<StringRef> ToLoad;
818 for (
auto &[BaseName, ProfId] : CandidateByBaseName)
819 ToLoad.
insert(ProfId.stringRef());
822 unsigned MatchCount = 0;
823 SampleProfileMap NewlyLoadedProfiles;
824 for (
auto &[BaseName, ProfId] : CandidateByBaseName) {
825 if (!isProfileUnused(ProfId))
827 Function *OrphanFunc = OrphansByBaseName.lookup(BaseName);
831 FuncToProfileNameMap[OrphanFunc] = ProfId;
832 if (
const auto *FS = Reader.getSamplesFor(ProfId.stringRef()))
836 <<
" (IR) -> " << ProfId <<
" (Profile)"
837 <<
" [basename: " << BaseName <<
"]\n");
842 if (!NewlyLoadedProfiles.empty())
846 NumDirectProfileMatch += MatchCount;
847 LLVM_DEBUG(
dbgs() <<
"Direct basename matching found " << MatchCount
851bool SampleProfileMatcher::functionMatchesProfileHelper(
855 float Similarity = 0.0;
862 if (!IRBaseName.empty() && IRBaseName == ProfBaseName) {
864 << ProfFunc <<
"(Profile) share the same base name: "
865 << IRBaseName <<
".\n");
869 const auto *FSForMatching = getFlattenedSamplesFor(ProfFunc);
876 DenseSet<StringRef> TopLevelFunc({ProfFunc.
stringRef()});
877 if (std::error_code EC = Reader.read(TopLevelFunc))
879 FSForMatching = Reader.getSamplesFor(ProfFunc.
stringRef());
884 SampleProfileMap TempProfiles;
885 TempProfiles.
create(FSForMatching->getFunction()).
merge(*FSForMatching);
888 FSForMatching = getFlattenedSamplesFor(ProfFunc);
892 dbgs() <<
"Read top-level function " << ProfFunc
893 <<
" for call-graph matching\n";
908 const auto *FuncDesc = ProbeManager->getDesc(IRFunc);
910 !ProbeManager->profileIsHashMismatched(*FuncDesc, *FSForMatching)) {
912 <<
"(IR) and " << ProfFunc <<
"(Profile) match.\n");
919 findIRAnchors(IRFunc, IRAnchors);
921 findProfileAnchors(*FSForMatching, ProfileAnchors);
925 getFilteredAnchorList(IRAnchors, ProfileAnchors, FilteredIRAnchorsList,
926 FilteredProfileAnchorList);
939 longestCommonSequence(FilteredIRAnchorsList, FilteredProfileAnchorList,
942 Similarity =
static_cast<float>(MatchedAnchors.size()) /
943 FilteredProfileAnchorList.size();
946 <<
"(IR) and " << ProfFunc <<
"(profile) is "
947 <<
format(
"%.2f", Similarity) <<
"\n");
948 assert((Similarity >= 0 && Similarity <= 1.0) &&
949 "Similarity value should be in [0, 1]");
955bool SampleProfileMatcher::functionMatchesProfile(
Function &IRFunc,
957 bool FindMatchedProfileOnly) {
958 auto R = FuncProfileMatchCache.find({&IRFunc, ProfFunc});
959 if (R != FuncProfileMatchCache.end())
962 if (FindMatchedProfileOnly)
965 bool Matched = functionMatchesProfileHelper(IRFunc, ProfFunc);
966 FuncProfileMatchCache[{&IRFunc, ProfFunc}] = Matched;
968 FuncToProfileNameMap[&IRFunc] = ProfFunc;
970 <<
" matches profile:" << ProfFunc <<
"\n");
976void SampleProfileMatcher::UpdateWithSalvagedProfiles() {
977 DenseSet<StringRef> ProfileSalvagedFuncs;
979 for (
auto &
I : FuncToProfileNameMap) {
980 assert(
I.first &&
"New function is null");
981 FunctionId FuncName(
I.first->getName());
982 ProfileSalvagedFuncs.
insert(
I.second.stringRef());
983 FuncNameToProfNameMap->emplace(FuncName,
I.second);
987 SymbolMap->erase(FuncName);
988 SymbolMap->emplace(
I.second,
I.first);
995 Reader.read(ProfileSalvagedFuncs);
996 Reader.setFuncNameToProfNameMap(*FuncNameToProfNameMap);
1003 findFunctionsWithoutProfile();
1004 matchFunctionsWithoutProfileByBasename();
1009 std::vector<Function *> TopDownFunctionList;
1010 TopDownFunctionList.reserve(M.size());
1012 for (
auto *
F : TopDownFunctionList) {
1019 UpdateWithSalvagedProfiles();
1022 distributeIRToProfileLocationMap();
1024 computeAndReportProfileStaleness();
1027void SampleProfileMatcher::distributeIRToProfileLocationMap(
1029 const auto ProfileMappings = FuncMappings.
find(FS.getFuncName());
1030 if (ProfileMappings != FuncMappings.
end()) {
1031 FS.setIRToProfileLocationMap(&(ProfileMappings->second));
1034 for (
auto &Callees :
1036 for (
auto &FS : Callees.second) {
1037 distributeIRToProfileLocationMap(FS.second);
1044void SampleProfileMatcher::distributeIRToProfileLocationMap() {
1045 for (
auto &
I : Reader.getProfiles()) {
1046 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)
static bool isAvailableExternallyLinkage(LinkageTypes Linkage)
reference emplace_back(ArgTypes &&... Args)
iterator find(StringRef Key)
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
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.
StringRef - Represent a constant reference to a string, i.e.
std::string str() const
str - Get the contents as an std::string.
constexpr bool empty() const
empty - Check if the string is empty.
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
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)
This class represents a function that is read from a sample profile.
StringRef stringRef() const
Convert to StringRef.
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
std::unordered_map< LineLocation, LineLocation, LineLocationHash > 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 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."))