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 functions in a module, above which salvage "
64 "unused profile will be skipped."));
68 cl::desc(
"The maximum number of callsites in a function, above which stale "
69 "profile matching will be skipped."));
73void SampleProfileMatcher::findIRAnchors(
const Function &
F,
78 auto FindTopLevelInlinedCallsite = [](
const DILocation *DIL) {
79 assert((DIL && DIL->getInlinedAt()) &&
"No inlined callsite");
83 DIL = DIL->getInlinedAt();
84 }
while (DIL->getInlinedAt());
88 StringRef CalleeName = PrevDIL->getSubprogramLinkageName();
89 return std::make_pair(Callsite, FunctionId(CalleeName));
92 auto GetCanonicalCalleeName = [](
const CallBase *CB) {
93 StringRef CalleeName = UnknownIndirectCallee;
94 if (Function *Callee = CB->getCalledFunction())
102 DILocation *DIL =
I.getDebugLoc();
109 if (DIL->getInlinedAt()) {
110 IRAnchors.emplace(FindTopLevelInlinedCallsite(DIL));
113 StringRef CalleeName;
117 CalleeName = GetCanonicalCalleeName(CB);
119 LineLocation Loc = LineLocation(Probe->Id, 0);
120 IRAnchors.emplace(Loc, FunctionId(CalleeName));
130 if (DIL->getInlinedAt()) {
131 IRAnchors.emplace(FindTopLevelInlinedCallsite(DIL));
136 IRAnchors.emplace(Callsite, FunctionId(CalleeName));
143void SampleProfileMatcher::findProfileAnchors(
const FunctionSamples &FS,
145 auto isInvalidLineOffset = [](uint32_t LineOffset) {
146 return LineOffset & 0x8000;
149 auto InsertAnchor = [](
const LineLocation &Loc,
const FunctionId &CalleeName,
151 auto Ret = ProfileAnchors.try_emplace(Loc, CalleeName);
155 Ret.first->second = FunctionId(UnknownIndirectCallee);
159 for (
const auto &
I :
FS.getBodySamples()) {
160 const LineLocation &Loc =
I.first;
163 for (
const auto &
C :
I.second.getCallTargets())
164 InsertAnchor(Loc,
C.first, ProfileAnchors);
167 for (
const auto &
I :
FS.getCallsiteSamples()) {
168 const LineLocation &Loc =
I.first;
171 for (
const auto &
C :
I.second)
172 InsertAnchor(Loc,
C.first, ProfileAnchors);
176bool SampleProfileMatcher::functionHasProfile(
const FunctionId &IRFuncName,
178 FuncWithoutProfile =
nullptr;
179 auto R = FunctionsWithoutProfile.find(IRFuncName);
180 if (R != FunctionsWithoutProfile.end())
181 FuncWithoutProfile =
R->second;
182 return !FuncWithoutProfile;
185bool SampleProfileMatcher::isProfileUnused(
const FunctionId &ProfileFuncName) {
186 return SymbolMap->find(ProfileFuncName) == SymbolMap->
end();
189bool SampleProfileMatcher::functionMatchesProfile(
191 bool FindMatchedProfileOnly) {
192 if (IRFuncName == ProfileFuncName)
200 if (functionHasProfile(IRFuncName, IRFunc) ||
201 !isProfileUnused(ProfileFuncName))
205 "IR function should be different from profile function to match");
206 return functionMatchesProfile(*IRFunc, ProfileFuncName,
207 FindMatchedProfileOnly);
211SampleProfileMatcher::longestCommonSequence(
const AnchorList &AnchorList1,
213 bool MatchUnusedFunction) {
216 AnchorList1, AnchorList2,
217 [&](
const FunctionId &
A,
const FunctionId &
B) {
218 return functionMatchesProfile(
223 [&](LineLocation
A, LineLocation
B) {
224 MatchedAnchors.try_emplace(
A,
B);
226 return MatchedAnchors;
229void SampleProfileMatcher::matchNonCallsiteLocs(
232 auto InsertMatching = [&](
const LineLocation &From,
const LineLocation &To) {
235 IRToProfileLocationMap.insert({From, To});
239 int32_t LocationDelta = 0;
241 for (
const auto &
IR : IRAnchors) {
242 const auto &Loc =
IR.first;
243 bool IsMatchedAnchor =
false;
245 auto R = MatchedAnchors.find(Loc);
246 if (R != MatchedAnchors.end()) {
247 const auto &Candidate =
R->second;
248 InsertMatching(Loc, Candidate);
250 <<
" is matched from " << Loc <<
" to " << Candidate
252 LocationDelta = Candidate.LineOffset - Loc.
LineOffset;
258 for (
size_t I = (LastMatchedNonAnchors.
size() + 1) / 2;
259 I < LastMatchedNonAnchors.
size();
I++) {
260 const auto &
L = LastMatchedNonAnchors[
I];
261 uint32_t CandidateLineOffset =
L.LineOffset + LocationDelta;
262 LineLocation Candidate(CandidateLineOffset,
L.Discriminator);
263 InsertMatching(L, Candidate);
265 <<
" to " << Candidate <<
"\n");
268 IsMatchedAnchor =
true;
269 LastMatchedNonAnchors.
clear();
273 if (!IsMatchedAnchor) {
274 uint32_t CandidateLineOffset = Loc.
LineOffset + LocationDelta;
275 LineLocation Candidate(CandidateLineOffset, Loc.
Discriminator);
276 InsertMatching(Loc, Candidate);
278 << Candidate <<
"\n");
286void SampleProfileMatcher::getFilteredAnchorList(
289 for (
const auto &
I : IRAnchors) {
290 if (
I.second.stringRef().empty())
292 FilteredIRAnchorsList.emplace_back(
I);
295 for (
const auto &
I : ProfileAnchors)
296 FilteredProfileAnchorList.emplace_back(
I);
316void SampleProfileMatcher::runStaleProfileMatching(
319 bool RunCFGMatching,
bool RunCGMatching) {
320 if (!RunCFGMatching && !RunCGMatching)
324 assert(IRToProfileLocationMap.empty() &&
325 "Run stale profile matching only once per function");
329 getFilteredAnchorList(IRAnchors, ProfileAnchors, FilteredIRAnchorsList,
330 FilteredProfileAnchorList);
332 if (FilteredIRAnchorsList.empty() || FilteredProfileAnchorList.empty())
338 <<
" because the number of callsites in the IR is "
339 << FilteredIRAnchorsList.size()
340 <<
" and in the profile is "
341 << FilteredProfileAnchorList.size() <<
"\n");
356 longestCommonSequence(FilteredIRAnchorsList, FilteredProfileAnchorList,
364 matchNonCallsiteLocs(MatchedAnchors, IRAnchors, IRToProfileLocationMap);
367void SampleProfileMatcher::runOnFunction(
Function &
F) {
374 const auto *FSForMatching = getFlattenedSamplesFor(
F);
377 auto R = FuncToProfileNameMap.find(&
F);
378 if (R != FuncToProfileNameMap.end()) {
379 FSForMatching = getFlattenedSamplesFor(
R->second);
384 FSForMatching = Reader.getSamplesFor(
R->second.stringRef());
394 findIRAnchors(
F, IRAnchors);
398 findProfileAnchors(*FSForMatching, ProfileAnchors);
402 recordCallsiteMatchStates(
F, IRAnchors, ProfileAnchors,
nullptr);
409 !ProbeManager->profileIsValid(
F, *FSForMatching);
410 bool RunCFGMatching =
418 F.addFnAttr(
"profile-checksum-mismatch");
422 auto &IRToProfileLocationMap = getIRToProfileLocationMap(
F);
423 runStaleProfileMatching(
F, IRAnchors, ProfileAnchors, IRToProfileLocationMap,
424 RunCFGMatching, RunCGMatching);
427 recordCallsiteMatchStates(
F, IRAnchors, ProfileAnchors,
428 &IRToProfileLocationMap);
431void SampleProfileMatcher::recordCallsiteMatchStates(
435 bool IsPostMatch = IRToProfileLocationMap !=
nullptr;
436 auto &CallsiteMatchStates =
439 auto MapIRLocToProfileLoc = [&](
const LineLocation &IRLoc) {
441 if (!IRToProfileLocationMap)
443 const auto &ProfileLoc = IRToProfileLocationMap->find(IRLoc);
444 if (ProfileLoc != IRToProfileLocationMap->end())
445 return ProfileLoc->second;
450 for (
const auto &
I : IRAnchors) {
453 const auto &ProfileLoc = MapIRLocToProfileLoc(
I.first);
454 const auto &IRCalleeId =
I.second;
455 const auto &It = ProfileAnchors.find(ProfileLoc);
456 if (It == ProfileAnchors.end())
458 const auto &ProfCalleeId = It->second;
459 if (IRCalleeId == ProfCalleeId) {
460 auto It = CallsiteMatchStates.find(ProfileLoc);
461 if (It == CallsiteMatchStates.end())
462 CallsiteMatchStates.emplace(ProfileLoc, MatchState::InitialMatch);
463 else if (IsPostMatch) {
464 if (It->second == MatchState::InitialMatch)
465 It->second = MatchState::UnchangedMatch;
466 else if (It->second == MatchState::InitialMismatch)
467 It->second = MatchState::RecoveredMismatch;
474 for (
const auto &
I : ProfileAnchors) {
475 const auto &Loc =
I.first;
476 assert(!
I.second.stringRef().empty() &&
"Callees should not be empty");
477 auto It = CallsiteMatchStates.find(Loc);
478 if (It == CallsiteMatchStates.end())
479 CallsiteMatchStates.emplace(Loc, MatchState::InitialMismatch);
480 else if (IsPostMatch) {
483 if (It->second == MatchState::InitialMismatch)
484 It->second = MatchState::UnchangedMismatch;
485 else if (It->second == MatchState::InitialMatch)
486 It->second = MatchState::RemovedMatch;
491void SampleProfileMatcher::countMismatchedFuncSamples(
const FunctionSamples &FS,
493 const auto *FuncDesc = ProbeManager->getDesc(
FS.getGUID());
498 if (ProbeManager->profileIsHashMismatched(*FuncDesc, FS)) {
500 NumStaleProfileFunc++;
505 MismatchedFunctionSamples +=
FS.getTotalSamples();
514 for (
const auto &
I :
FS.getCallsiteSamples())
515 for (
const auto &CS :
I.second)
516 countMismatchedFuncSamples(CS.second,
false);
519void SampleProfileMatcher::countMismatchedCallsiteSamples(
521 auto It = FuncCallsiteMatchStates.find(
FS.getFuncName());
523 if (It == FuncCallsiteMatchStates.end() || It->second.empty())
525 const auto &CallsiteMatchStates = It->second;
527 auto findMatchState = [&](
const LineLocation &Loc) {
528 auto It = CallsiteMatchStates.find(Loc);
529 if (It == CallsiteMatchStates.end())
530 return MatchState::Unknown;
534 auto AttributeMismatchedSamples = [&](
const enum MatchState &State,
536 if (isMismatchState(State))
537 MismatchedCallsiteSamples += Samples;
538 else if (State == MatchState::RecoveredMismatch)
539 RecoveredCallsiteSamples += Samples;
544 for (
const auto &
I :
FS.getBodySamples())
545 AttributeMismatchedSamples(findMatchState(
I.first),
I.second.getSamples());
548 for (
const auto &
I :
FS.getCallsiteSamples()) {
549 auto State = findMatchState(
I.first);
550 uint64_t CallsiteSamples = 0;
551 for (
const auto &CS :
I.second)
552 CallsiteSamples += CS.second.getTotalSamples();
553 AttributeMismatchedSamples(State, CallsiteSamples);
555 if (isMismatchState(State))
561 for (
const auto &CS :
I.second)
562 countMismatchedCallsiteSamples(CS.second);
566void SampleProfileMatcher::countMismatchCallsites(
const FunctionSamples &FS) {
567 auto It = FuncCallsiteMatchStates.find(
FS.getFuncName());
569 if (It == FuncCallsiteMatchStates.end() || It->second.empty())
571 const auto &MatchStates = It->second;
572 [[maybe_unused]]
bool OnInitialState =
573 isInitialState(MatchStates.begin()->second);
574 for (
const auto &
I : MatchStates) {
575 TotalProfiledCallsites++;
577 (OnInitialState ? isInitialState(
I.second) : isFinalState(
I.second)) &&
578 "Profile matching state is inconsistent");
580 if (isMismatchState(
I.second))
581 NumMismatchedCallsites++;
582 else if (
I.second == MatchState::RecoveredMismatch)
583 NumRecoveredCallsites++;
587void SampleProfileMatcher::countCallGraphRecoveredSamples(
589 std::unordered_set<FunctionId> &CallGraphRecoveredProfiles) {
590 if (CallGraphRecoveredProfiles.count(
FS.getFunction())) {
591 NumCallGraphRecoveredFuncSamples +=
FS.getTotalSamples();
595 for (
const auto &CM :
FS.getCallsiteSamples()) {
596 for (
const auto &CS : CM.second) {
597 countCallGraphRecoveredSamples(CS.second, CallGraphRecoveredProfiles);
602void SampleProfileMatcher::computeAndReportProfileStaleness() {
606 std::unordered_set<FunctionId> CallGraphRecoveredProfiles;
608 for (
const auto &
I : FuncToProfileNameMap) {
609 CallGraphRecoveredProfiles.insert(
I.second);
612 NumCallGraphRecoveredProfiledFunc++;
617 for (
const auto &
F : M) {
624 const auto *
FS = Reader.getSamplesFor(
F);
628 TotalFunctionSamples +=
FS->getTotalSamples();
631 countCallGraphRecoveredSamples(*FS, CallGraphRecoveredProfiles);
635 countMismatchedFuncSamples(*FS,
true);
638 countMismatchCallsites(*FS);
639 countMismatchedCallsiteSamples(*FS);
644 errs() <<
"(" << NumStaleProfileFunc <<
"/" << TotalProfiledFunc
645 <<
") of functions' profile are invalid and ("
646 << MismatchedFunctionSamples <<
"/" << TotalFunctionSamples
647 <<
") of samples are discarded due to function hash mismatch.\n";
650 errs() <<
"(" << NumCallGraphRecoveredProfiledFunc <<
"/"
651 << TotalProfiledFunc <<
") of functions' profile are matched and ("
652 << NumCallGraphRecoveredFuncSamples <<
"/" << TotalFunctionSamples
653 <<
") of samples are reused by call graph matching.\n";
656 errs() <<
"(" << (NumMismatchedCallsites + NumRecoveredCallsites) <<
"/"
657 << TotalProfiledCallsites
658 <<
") of callsites' profile are invalid and ("
659 << (MismatchedCallsiteSamples + RecoveredCallsiteSamples) <<
"/"
660 << TotalFunctionSamples
661 <<
") of samples are discarded due to callsite location mismatch.\n";
662 errs() <<
"(" << NumRecoveredCallsites <<
"/"
663 << (NumRecoveredCallsites + NumMismatchedCallsites)
664 <<
") of callsites and (" << RecoveredCallsiteSamples <<
"/"
665 << (RecoveredCallsiteSamples + MismatchedCallsiteSamples)
666 <<
") of samples are recovered by stale profile matching.\n";
670 LLVMContext &Ctx = M.getContext();
675 ProfStatsVec.
emplace_back(
"NumStaleProfileFunc", NumStaleProfileFunc);
676 ProfStatsVec.
emplace_back(
"TotalProfiledFunc", TotalProfiledFunc);
678 MismatchedFunctionSamples);
679 ProfStatsVec.
emplace_back(
"TotalFunctionSamples", TotalFunctionSamples);
683 ProfStatsVec.
emplace_back(
"NumCallGraphRecoveredProfiledFunc",
684 NumCallGraphRecoveredProfiledFunc);
685 ProfStatsVec.
emplace_back(
"NumCallGraphRecoveredFuncSamples",
686 NumCallGraphRecoveredFuncSamples);
689 ProfStatsVec.
emplace_back(
"NumMismatchedCallsites", NumMismatchedCallsites);
690 ProfStatsVec.
emplace_back(
"NumRecoveredCallsites", NumRecoveredCallsites);
691 ProfStatsVec.
emplace_back(
"TotalProfiledCallsites", TotalProfiledCallsites);
693 MismatchedCallsiteSamples);
695 RecoveredCallsiteSamples);
697 auto *MD = MDB.createLLVMStats(ProfStatsVec);
698 auto *NMD = M.getOrInsertNamedMetadata(
"llvm.stats");
703void SampleProfileMatcher::findFunctionsWithoutProfile() {
707 StringSet<> NamesInProfile;
708 if (
auto NameTable = Reader.getNameTable()) {
709 for (
auto Name : *NameTable)
716 if (
F.isDeclaration())
720 const auto *
FS = getFlattenedSamplesFor(
F);
727 if (NamesInProfile.
count(CanonFName))
732 if (PSL && PSL->contains(CanonFName))
736 <<
" is not in profile or profile symbol list.\n");
737 FunctionsWithoutProfile[FunctionId(CanonFName)] = &
F;
745 auto FunctionName = FName.
str();
746 if (Demangler.partialDemangle(FunctionName.c_str()))
747 return std::string();
748 size_t BaseNameSize = 0;
752 char *BaseNamePtr = Demangler.getFunctionBaseName(
nullptr, &BaseNameSize);
753 std::string Result = (BaseNamePtr && BaseNameSize)
754 ? std::string(BaseNamePtr, BaseNameSize)
759 while (!Result.empty() && (Result.back() ==
' ' || Result.back() ==
'\0'))
764void SampleProfileMatcher::matchFunctionsWithoutProfileByBasename() {
767 auto *NameTable = Reader.getNameTable();
776 StringMap<Function *> OrphansByBaseName;
777 StringSet<> AmbiguousBaseNames;
778 for (
auto &[FuncId, Func] : FunctionsWithoutProfile) {
780 if (BaseName.empty() || AmbiguousBaseNames.
count(BaseName))
785 OrphansByBaseName.
erase(It);
786 AmbiguousBaseNames.
insert(BaseName);
789 if (OrphansByBaseName.
empty())
797 StringMap<FunctionId> CandidateByBaseName;
798 for (
auto &ProfileFuncId : *NameTable) {
799 StringRef ProfName = ProfileFuncId.stringRef();
800 if (ProfName.
empty())
802 for (
auto &[BaseName,
_] : OrphansByBaseName) {
803 if (AmbiguousBaseNames.
count(BaseName) || !ProfName.
contains(BaseName))
806 if (ProfBaseName != BaseName)
809 CandidateByBaseName.
try_emplace(BaseName, ProfileFuncId);
812 CandidateByBaseName.
erase(It);
813 AmbiguousBaseNames.
insert(BaseName);
818 if (CandidateByBaseName.
empty())
822 DenseSet<StringRef> ToLoad;
823 for (
auto &[BaseName, ProfId] : CandidateByBaseName)
824 ToLoad.
insert(ProfId.stringRef());
827 unsigned MatchCount = 0;
828 SampleProfileMap NewlyLoadedProfiles;
829 for (
auto &[BaseName, ProfId] : CandidateByBaseName) {
830 if (!isProfileUnused(ProfId))
832 Function *OrphanFunc = OrphansByBaseName.lookup(BaseName);
836 FuncToProfileNameMap[OrphanFunc] = ProfId;
837 if (
const auto *FS = Reader.getSamplesFor(ProfId.stringRef()))
841 <<
" (IR) -> " << ProfId <<
" (Profile)"
842 <<
" [basename: " << BaseName <<
"]\n");
847 if (!NewlyLoadedProfiles.empty())
851 NumDirectProfileMatch += MatchCount;
852 LLVM_DEBUG(
dbgs() <<
"Direct basename matching found " << MatchCount
856bool SampleProfileMatcher::functionMatchesProfileHelper(
860 float Similarity = 0.0;
867 if (!IRBaseName.empty() && IRBaseName == ProfBaseName) {
869 << ProfFunc <<
"(Profile) share the same base name: "
870 << IRBaseName <<
".\n");
874 const auto *FSForMatching = getFlattenedSamplesFor(ProfFunc);
881 DenseSet<StringRef> TopLevelFunc({ProfFunc.
stringRef()});
882 if (std::error_code EC = Reader.read(TopLevelFunc))
884 FSForMatching = Reader.getSamplesFor(ProfFunc.
stringRef());
889 SampleProfileMap TempProfiles;
890 TempProfiles.
create(FSForMatching->getFunction()).
merge(*FSForMatching);
893 FSForMatching = getFlattenedSamplesFor(ProfFunc);
897 dbgs() <<
"Read top-level function " << ProfFunc
898 <<
" for call-graph matching\n";
913 const auto *FuncDesc = ProbeManager->getDesc(IRFunc);
915 !ProbeManager->profileIsHashMismatched(*FuncDesc, *FSForMatching)) {
917 <<
"(IR) and " << ProfFunc <<
"(Profile) match.\n");
924 findIRAnchors(IRFunc, IRAnchors);
926 findProfileAnchors(*FSForMatching, ProfileAnchors);
930 getFilteredAnchorList(IRAnchors, ProfileAnchors, FilteredIRAnchorsList,
931 FilteredProfileAnchorList);
944 longestCommonSequence(FilteredIRAnchorsList, FilteredProfileAnchorList,
947 Similarity =
static_cast<float>(MatchedAnchors.size()) /
948 FilteredProfileAnchorList.size();
951 <<
"(IR) and " << ProfFunc <<
"(profile) is "
952 <<
format(
"%.2f", Similarity) <<
"\n");
953 assert((Similarity >= 0 && Similarity <= 1.0) &&
954 "Similarity value should be in [0, 1]");
960bool SampleProfileMatcher::functionMatchesProfile(
Function &IRFunc,
962 bool FindMatchedProfileOnly) {
963 auto R = FuncProfileMatchCache.find({&IRFunc, ProfFunc});
964 if (R != FuncProfileMatchCache.end())
967 if (FindMatchedProfileOnly)
970 bool Matched = functionMatchesProfileHelper(IRFunc, ProfFunc);
971 FuncProfileMatchCache[{&IRFunc, ProfFunc}] = Matched;
973 FuncToProfileNameMap[&IRFunc] = ProfFunc;
975 <<
" matches profile:" << ProfFunc <<
"\n");
981void SampleProfileMatcher::UpdateWithSalvagedProfiles() {
982 DenseSet<StringRef> ProfileSalvagedFuncs;
984 for (
auto &
I : FuncToProfileNameMap) {
985 assert(
I.first &&
"New function is null");
986 FunctionId FuncName(
I.first->getName());
987 ProfileSalvagedFuncs.
insert(
I.second.stringRef());
988 FuncNameToProfNameMap->emplace(FuncName,
I.second);
992 SymbolMap->erase(FuncName);
993 SymbolMap->emplace(
I.second,
I.first);
1000 Reader.read(ProfileSalvagedFuncs);
1001 Reader.setFuncNameToProfNameMap(*FuncNameToProfNameMap);
1013 findFunctionsWithoutProfile();
1014 matchFunctionsWithoutProfileByBasename();
1019 std::vector<Function *> TopDownFunctionList;
1020 TopDownFunctionList.reserve(M.size());
1022 for (
auto *
F : TopDownFunctionList) {
1029 UpdateWithSalvagedProfiles();
1032 distributeIRToProfileLocationMap();
1034 computeAndReportProfileStaleness();
1037void SampleProfileMatcher::distributeIRToProfileLocationMap(
1039 const auto ProfileMappings = FuncMappings.
find(FS.getFuncName());
1040 if (ProfileMappings != FuncMappings.
end()) {
1041 FS.setIRToProfileLocationMap(&(ProfileMappings->second));
1044 for (
auto &Callees :
1046 for (
auto &FS : Callees.second) {
1047 distributeIRToProfileLocationMap(FS.second);
1054void SampleProfileMatcher::distributeIRToProfileLocationMap() {
1055 for (
auto &
I : Reader.getProfiles()) {
1056 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 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."))