14#ifndef LLVM_TRANSFORMS_IPO_SAMPLEPROFILEMATCHER_H
15#define LLVM_TRANSFORMS_IPO_SAMPLEPROFILEMATCHER_H
22using AnchorList = std::vector<std::pair<LineLocation, FunctionId>>;
23using AnchorMap = std::map<LineLocation, FunctionId>;
39 enum class MatchState {
48 UnchangedMismatch = 4,
50 RecoveredMismatch = 5,
60 FuncCallsiteMatchStates;
62 struct FuncToProfileNameMapHash {
64 operator()(
const std::pair<const Function *, FunctionId> &
P)
const {
71 std::unordered_map<std::pair<const Function *, FunctionId>,
bool,
72 FuncToProfileNameMapHash>
73 FuncProfileMatchCache;
77 std::unordered_map<Function *, FunctionId> FuncToProfileNameMap;
91 FunctionsWithoutProfile;
94 std::shared_ptr<ProfileSymbolList> PSL;
100 uint64_t TotalProfiledCallsites = 0;
101 uint64_t NumMismatchedCallsites = 0;
106 uint64_t MismatchedFunctionSamples = 0;
107 uint64_t MismatchedCallsiteSamples = 0;
108 uint64_t RecoveredCallsiteSamples = 0;
111 uint64_t NumCallGraphRecoveredProfiledFunc = 0;
112 uint64_t NumCallGraphRecoveredFuncSamples = 0;
116 static constexpr const char *UnknownIndirectCallee =
117 "unknown.indirect.callee";
124 std::shared_ptr<ProfileSymbolList> PSL,
126 &FuncNameToProfNameMap)
127 : M(M), Reader(Reader), CG(CG), ProbeManager(ProbeManager),
128 LTOPhase(LTOPhase), FuncNameToProfNameMap(&FuncNameToProfNameMap),
129 SymbolMap(&
SymMap), PSL(PSL) {};
137 freeContainer(FuncCallsiteMatchStates);
138 freeContainer(FunctionsWithoutProfile);
139 freeContainer(FuncToProfileNameMap);
144 auto It = FlattenedProfiles.
find(Fname);
145 if (It != FlattenedProfiles.end())
149 FunctionSamples *getFlattenedSamplesFor(
const Function &
F) {
151 return getFlattenedSamplesFor(FunctionId(CanonFName));
153 template <
typename T>
inline void freeContainer(
T &
C) {
157 void getFilteredAnchorList(
const AnchorMap &IRAnchors,
161 void runOnFunction(Function &
F);
162 void findIRAnchors(
const Function &
F,
AnchorMap &IRAnchors)
const;
163 void findProfileAnchors(
const FunctionSamples &FS,
167 void recordCallsiteMatchStates(
const Function &
F,
const AnchorMap &IRAnchors,
171 bool isMismatchState(
const enum MatchState &State) {
172 return State == MatchState::InitialMismatch ||
173 State == MatchState::UnchangedMismatch ||
174 State == MatchState::RemovedMatch;
177 bool isInitialState(
const enum MatchState &State) {
178 return State == MatchState::InitialMatch ||
179 State == MatchState::InitialMismatch;
182 bool isFinalState(
const enum MatchState &State) {
183 return State == MatchState::UnchangedMatch ||
184 State == MatchState::UnchangedMismatch ||
185 State == MatchState::RecoveredMismatch ||
186 State == MatchState::RemovedMatch;
189 void countCallGraphRecoveredSamples(
190 const FunctionSamples &FS,
191 std::unordered_set<FunctionId> &MatchedUnusedProfile);
194 void countMismatchedFuncSamples(
const FunctionSamples &FS,
bool IsTopLevel);
196 void countMismatchCallsites(
const FunctionSamples &FS);
199 void countMismatchedCallsiteSamples(
const FunctionSamples &FS);
200 void computeAndReportProfileStaleness();
202 LocToLocMap &getIRToProfileLocationMap(
const Function &
F) {
205 return Ret.first->second;
207 void distributeIRToProfileLocationMap();
208 void distributeIRToProfileLocationMap(FunctionSamples &FS);
221 bool MatchUnusedFunction);
222 void matchNonCallsiteLocs(
const LocToLocMap &AnchorMatchings,
225 void runStaleProfileMatching(
const Function &
F,
const AnchorMap &IRAnchors,
228 bool RunCFGMatching,
bool RunCGMatching);
230 bool functionHasProfile(
const FunctionId &IRFuncName,
231 Function *&FuncWithoutProfile);
232 bool isProfileUnused(
const FunctionId &ProfileFuncName);
233 bool functionMatchesProfileHelper(
const Function &IRFunc,
234 const FunctionId &ProfFunc);
238 bool functionMatchesProfile(
const FunctionId &IRFuncName,
239 const FunctionId &ProfileFuncName,
240 bool FindMatchedProfileOnly);
244 bool functionMatchesProfile(Function &IRFunc,
const FunctionId &ProfFunc,
245 bool FindMatchedProfileOnly);
249 void findFunctionsWithoutProfile();
250 void reportOrPersistProfileStats();
This file provides the interface for the sampled PGO profile loader base implementation.
StringSet - A set-like wrapper for the StringMap.
A lazily constructed view of the call graph of a module.
A Module instance is used to store all the information related to an LLVM module.
SampleProfileMatcher(Module &M, SampleProfileReader &Reader, LazyCallGraph &CG, const PseudoProbeManager *ProbeManager, ThinOrFullLTOPhase LTOPhase, HashKeyMap< std::unordered_map, FunctionId, Function * > &SymMap, std::shared_ptr< ProfileSymbolList > PSL, HashKeyMap< std::unordered_map, FunctionId, FunctionId > &FuncNameToProfNameMap)
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
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.
This class represents a function that is read from a sample profile.
Representation of the samples collected for a function.
static StringRef getCanonicalFnName(const Function &F)
Return the canonical name for a function, taking into account suffix elision policy attributes.
This class is a wrapper to associative container MapT<KeyT, ValueT> using the hash value of the origi...
This class provides operator overloads to the map container using MD5 as the key type,...
iterator find(const SampleContext &Ctx)
Sample-based profile reader.
@ C
The default llvm calling convention, compatible with C.
std::unordered_map< LineLocation, LineLocation, LineLocationHash > LocToLocMap
This is an optimization pass for GlobalISel generic memory operations.
std::vector< std::pair< LineLocation, FunctionId > > AnchorList
std::map< LineLocation, FunctionId > AnchorMap
ThinOrFullLTOPhase
This enumerates the LLVM full LTO or ThinLTO optimization phases.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.