LLVM 23.0.0git
SampleProfileMatcher.h
Go to the documentation of this file.
1//===- Transforms/IPO/SampleProfileMatcher.h ----------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10/// This file provides the interface for SampleProfileMatcher.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TRANSFORMS_IPO_SAMPLEPROFILEMATCHER_H
15#define LLVM_TRANSFORMS_IPO_SAMPLEPROFILEMATCHER_H
16
17#include "llvm/ADT/MapVector.h"
18#include "llvm/ADT/StringSet.h"
20
21#include <unordered_set>
22
23namespace llvm {
24
25using AnchorList = std::vector<std::pair<LineLocation, FunctionId>>;
26using AnchorMap = std::map<LineLocation, FunctionId>;
27
28// Sample profile matching - fuzzy match.
30 Module &M;
31 SampleProfileReader &Reader;
32 LazyCallGraph &CG;
33 const PseudoProbeManager *ProbeManager;
34 const ThinOrFullLTOPhase LTOPhase;
35 SampleProfileMap FlattenedProfiles;
36 // For each function, the matcher generates a map, of which each entry is a
37 // mapping from the source location of current build to the source location
38 // in the profile.
39 StringMap<LocToLocMap> FuncMappings;
40
41 // Match state for an anchor/callsite.
42 enum class MatchState {
43 Unknown = 0,
44 // Initial match between input profile and current IR.
45 InitialMatch = 1,
46 // Initial mismatch between input profile and current IR.
47 InitialMismatch = 2,
48 // InitialMatch stays matched after fuzzy profile matching.
49 UnchangedMatch = 3,
50 // InitialMismatch stays mismatched after fuzzy profile matching.
51 UnchangedMismatch = 4,
52 // InitialMismatch is recovered after fuzzy profile matching.
53 RecoveredMismatch = 5,
54 // InitialMatch is removed and becomes mismatched after fuzzy profile
55 // matching.
56 RemovedMatch = 6,
57 };
58
59 // For each function, store every callsite and its matching state into this
60 // map, of which each entry is a pair of callsite location and MatchState.
61 // This is used for profile staleness computation and report.
63 FuncCallsiteMatchStates;
64
65 struct FuncToProfileNameMapHash {
67 operator()(const std::pair<const Function *, FunctionId> &P) const {
68 return hash_combine(P.first, P.second);
69 }
70 };
71 // A map from a pair of function and profile name to a boolean value
72 // indicating whether they are matched. This is used as a cache for the
73 // matching result.
74 std::unordered_map<std::pair<const Function *, FunctionId>, bool,
75 FuncToProfileNameMapHash>
76 FuncProfileMatchCache;
77 // The new functions found by the call graph matching. The map's key is the
78 // the new(renamed) function pointer and the value is old(unused) profile
79 // name.
80 MapVector<Function *, FunctionId> FuncToProfileNameMap;
81
82 // A map pointer to the FuncNameToProfNameMap in SampleProfileLoader,
83 // which maps the function name to the matched profile name. This is used
84 // for sample loader to look up profile using the new name.
86
87 // A map pointer to the SymbolMap in SampleProfileLoader, which stores all
88 // the original matched symbols before the matching. this is to determine if
89 // the profile is unused(to be matched) or not.
91
92 // The new functions from IR.
94 FunctionsWithoutProfile;
95
96 // Pointer to the Profile Symbol List in the reader.
97 std::shared_ptr<ProfileSymbolList> PSL;
98
99 // Profile mismatch statstics:
100 uint64_t TotalProfiledFunc = 0;
101 // Num of checksum-mismatched function.
102 uint64_t NumStaleProfileFunc = 0;
103 uint64_t TotalProfiledCallsites = 0;
104 uint64_t NumMismatchedCallsites = 0;
105 uint64_t NumRecoveredCallsites = 0;
106 // Total samples for all profiled functions.
107 uint64_t TotalFunctionSamples = 0;
108 // Total samples for all checksum-mismatched functions.
109 uint64_t MismatchedFunctionSamples = 0;
110 uint64_t MismatchedCallsiteSamples = 0;
111 uint64_t RecoveredCallsiteSamples = 0;
112
113 // Profile call-graph matching statstics:
114 uint64_t NumCallGraphRecoveredProfiledFunc = 0;
115 uint64_t NumCallGraphRecoveredFuncSamples = 0;
116
117 // A dummy name for unknown indirect callee, used to differentiate from a
118 // non-call instruction that also has an empty callee name.
119 static constexpr const char *UnknownIndirectCallee =
120 "unknown.indirect.callee";
121
122public:
125 const PseudoProbeManager *ProbeManager, ThinOrFullLTOPhase LTOPhase,
127 std::shared_ptr<ProfileSymbolList> PSL,
129 &FuncNameToProfNameMap)
130 : M(M), Reader(Reader), CG(CG), ProbeManager(ProbeManager),
131 LTOPhase(LTOPhase), FuncNameToProfNameMap(&FuncNameToProfNameMap),
132 SymbolMap(&SymMap), PSL(PSL) {};
133 void runOnModule();
135 // Do not clear FuncMappings, it stores IRLoc to ProfLoc remappings which
136 // will be used for sample loader.
137 // Do not clear FlattenedProfiles as it contains function names referenced
138 // by FuncNameToProfNameMap. Clearing this memory could lead to a
139 // use-after-free error.
140 freeContainer(FuncCallsiteMatchStates);
141 freeContainer(FunctionsWithoutProfile);
142 freeContainer(FuncToProfileNameMap);
143 }
144
145private:
146 FunctionSamples *getFlattenedSamplesFor(const FunctionId &Fname) {
147 auto It = FlattenedProfiles.find(Fname);
148 if (It != FlattenedProfiles.end())
149 return &It->second;
150 return nullptr;
151 }
152 FunctionSamples *getFlattenedSamplesFor(const Function &F) {
153 StringRef CanonFName = FunctionSamples::getCanonicalFnName(F);
154 return getFlattenedSamplesFor(FunctionId(CanonFName));
155 }
156 template <typename T> inline void freeContainer(T &C) {
157 T Empty;
158 std::swap(C, Empty);
159 }
160 void getFilteredAnchorList(const AnchorMap &IRAnchors,
161 const AnchorMap &ProfileAnchors,
162 AnchorList &FilteredIRAnchorsList,
163 AnchorList &FilteredProfileAnchorList);
164 void runOnFunction(Function &F);
165 void findIRAnchors(const Function &F, AnchorMap &IRAnchors) const;
166 void findProfileAnchors(const FunctionSamples &FS,
167 AnchorMap &ProfileAnchors) const;
168 // Record the callsite match states for profile staleness report, the result
169 // is saved in FuncCallsiteMatchStates.
170 void recordCallsiteMatchStates(const Function &F, const AnchorMap &IRAnchors,
171 const AnchorMap &ProfileAnchors,
172 const LocToLocMap *IRToProfileLocationMap);
173
174 bool isMismatchState(const enum MatchState &State) {
175 return State == MatchState::InitialMismatch ||
176 State == MatchState::UnchangedMismatch ||
177 State == MatchState::RemovedMatch;
178 };
179
180 bool isInitialState(const enum MatchState &State) {
181 return State == MatchState::InitialMatch ||
182 State == MatchState::InitialMismatch;
183 };
184
185 bool isFinalState(const enum MatchState &State) {
186 return State == MatchState::UnchangedMatch ||
187 State == MatchState::UnchangedMismatch ||
188 State == MatchState::RecoveredMismatch ||
189 State == MatchState::RemovedMatch;
190 };
191
192 void countCallGraphRecoveredSamples(
193 const FunctionSamples &FS,
194 std::unordered_set<FunctionId> &MatchedUnusedProfile);
195 // Count the samples of checksum mismatched function for the top-level
196 // function and all inlinees.
197 void countMismatchedFuncSamples(const FunctionSamples &FS, bool IsTopLevel);
198 // Count the number of mismatched or recovered callsites.
199 void countMismatchCallsites(const FunctionSamples &FS);
200 // Count the samples of mismatched or recovered callsites for top-level
201 // function and all inlinees.
202 void countMismatchedCallsiteSamples(const FunctionSamples &FS);
203 void computeAndReportProfileStaleness();
204 void UpdateWithSalvagedProfiles();
205
206 LocToLocMap &getIRToProfileLocationMap(const Function &F) {
207 return FuncMappings[FunctionSamples::getCanonicalFnName(F.getName())];
208 }
209 void distributeIRToProfileLocationMap();
210 void distributeIRToProfileLocationMap(FunctionSamples &FS);
211 LocToLocMap longestCommonSequence(const AnchorList &IRCallsiteAnchors,
212 const AnchorList &ProfileCallsiteAnchors,
213 bool MatchUnusedFunction);
214 void matchNonCallsiteLocs(const LocToLocMap &AnchorMatchings,
215 const AnchorMap &IRAnchors,
216 LocToLocMap &IRToProfileLocationMap);
217 void runStaleProfileMatching(const Function &F, const AnchorMap &IRAnchors,
218 const AnchorMap &ProfileAnchors,
219 LocToLocMap &IRToProfileLocationMap,
220 bool RunCFGMatching, bool RunCGMatching);
221 // If the function doesn't have profile, return the pointer to the function.
222 bool functionHasProfile(const FunctionId &IRFuncName,
223 Function *&FuncWithoutProfile);
224 bool isProfileUnused(const FunctionId &ProfileFuncName);
225 bool functionMatchesProfileHelper(const Function &IRFunc,
226 const FunctionId &ProfFunc);
227 // Determine if the function matches profile. If FindMatchedProfileOnly is
228 // set, only search the existing matched function. Otherwise, try matching the
229 // two functions.
230 bool functionMatchesProfile(const FunctionId &IRFuncName,
231 const FunctionId &ProfileFuncName,
232 bool FindMatchedProfileOnly);
233 // Determine if the function matches profile by computing a similarity ratio
234 // between two sequences of callsite anchors extracted from function and
235 // profile. If it's above the threshold, the function matches the profile.
236 bool functionMatchesProfile(Function &IRFunc, const FunctionId &ProfFunc,
237 bool FindMatchedProfileOnly);
238 // Find functions that don't show in the profile or profile symbol list,
239 // which are supposed to be new functions. We use them as the targets for
240 // call graph matching.
241 void findFunctionsWithoutProfile();
242 // Match orphan IR functions to unused top-level profile entries by demangled
243 // basename, without requiring a matched caller in the call graph.
244 void matchFunctionsWithoutProfileByBasename();
245 void reportOrPersistProfileStats();
246};
247} // end namespace llvm
248#endif // LLVM_TRANSFORMS_IPO_SAMPLEPROFILEMATCHER_H
#define F(x, y, z)
Definition MD5.cpp:54
This file implements a map that provides insertion order iteration.
#define T
#define P(N)
This file provides the interface for the sampled PGO profile loader base implementation.
StringSet - A set-like wrapper for the StringMap.
LocallyHashedType DenseMapInfo< LocallyHashedType >::Empty
A lazily constructed view of the call graph of a module.
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:36
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
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",...
Definition StringMap.h:133
This class represents a function that is read from a sample profile.
Definition FunctionId.h:36
Representation of the samples collected for a function.
Definition SampleProf.h:783
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...
Definition HashKeyMap.h:52
This class provides operator overloads to the map container using MD5 as the key type,...
iterator find(const SampleContext &Ctx)
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
std::unordered_map< LineLocation, LineLocation, LineLocationHash > LocToLocMap
Definition SampleProf.h:775
This is an optimization pass for GlobalISel generic memory operations.
std::map< LineLocation, FunctionId > AnchorMap
ThinOrFullLTOPhase
This enumerates the LLVM full LTO or ThinLTO optimization phases.
Definition Pass.h:77
std::vector< std::pair< LineLocation, FunctionId > > AnchorList
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition Hashing.h:592
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872