LLVM 19.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/StringSet.h"
19
20namespace llvm {
21
22using AnchorList = std::vector<std::pair<LineLocation, FunctionId>>;
23using AnchorMap = std::map<LineLocation, FunctionId>;
24
25// Sample profile matching - fuzzy match.
27 Module &M;
28 SampleProfileReader &Reader;
29 const PseudoProbeManager *ProbeManager;
30 const ThinOrFullLTOPhase LTOPhase;
31 SampleProfileMap FlattenedProfiles;
32 // For each function, the matcher generates a map, of which each entry is a
33 // mapping from the source location of current build to the source location
34 // in the profile.
35 StringMap<LocToLocMap> FuncMappings;
36
37 // Match state for an anchor/callsite.
38 enum class MatchState {
39 Unknown = 0,
40 // Initial match between input profile and current IR.
41 InitialMatch = 1,
42 // Initial mismatch between input profile and current IR.
43 InitialMismatch = 2,
44 // InitialMatch stays matched after fuzzy profile matching.
45 UnchangedMatch = 3,
46 // InitialMismatch stays mismatched after fuzzy profile matching.
47 UnchangedMismatch = 4,
48 // InitialMismatch is recovered after fuzzy profile matching.
49 RecoveredMismatch = 5,
50 // InitialMatch is removed and becomes mismatched after fuzzy profile
51 // matching.
52 RemovedMatch = 6,
53 };
54
55 // For each function, store every callsite and its matching state into this
56 // map, of which each entry is a pair of callsite location and MatchState.
57 // This is used for profile staleness computation and report.
59 FuncCallsiteMatchStates;
60
61 // Profile mismatch statstics:
62 uint64_t TotalProfiledFunc = 0;
63 // Num of checksum-mismatched function.
64 uint64_t NumStaleProfileFunc = 0;
65 uint64_t TotalProfiledCallsites = 0;
66 uint64_t NumMismatchedCallsites = 0;
67 uint64_t NumRecoveredCallsites = 0;
68 // Total samples for all profiled functions.
69 uint64_t TotalFunctionSamples = 0;
70 // Total samples for all checksum-mismatched functions.
71 uint64_t MismatchedFunctionSamples = 0;
72 uint64_t MismatchedCallsiteSamples = 0;
73 uint64_t RecoveredCallsiteSamples = 0;
74
75 // A dummy name for unknown indirect callee, used to differentiate from a
76 // non-call instruction that also has an empty callee name.
77 static constexpr const char *UnknownIndirectCallee =
78 "unknown.indirect.callee";
79
80public:
82 const PseudoProbeManager *ProbeManager,
83 ThinOrFullLTOPhase LTOPhase)
84 : M(M), Reader(Reader), ProbeManager(ProbeManager), LTOPhase(LTOPhase){};
85 void runOnModule();
87 // Do not clear FuncMappings, it stores IRLoc to ProfLoc remappings which
88 // will be used for sample loader.
89 FuncCallsiteMatchStates.clear();
90 }
91
92private:
93 FunctionSamples *getFlattenedSamplesFor(const Function &F) {
95 auto It = FlattenedProfiles.find(FunctionId(CanonFName));
96 if (It != FlattenedProfiles.end())
97 return &It->second;
98 return nullptr;
99 }
100 void runOnFunction(Function &F);
101 void findIRAnchors(const Function &F, AnchorMap &IRAnchors);
102 void findProfileAnchors(const FunctionSamples &FS, AnchorMap &ProfileAnchors);
103 // Record the callsite match states for profile staleness report, the result
104 // is saved in FuncCallsiteMatchStates.
105 void recordCallsiteMatchStates(const Function &F, const AnchorMap &IRAnchors,
106 const AnchorMap &ProfileAnchors,
107 const LocToLocMap *IRToProfileLocationMap);
108
109 bool isMismatchState(const enum MatchState &State) {
110 return State == MatchState::InitialMismatch ||
111 State == MatchState::UnchangedMismatch ||
112 State == MatchState::RemovedMatch;
113 };
114
115 bool isInitialState(const enum MatchState &State) {
116 return State == MatchState::InitialMatch ||
117 State == MatchState::InitialMismatch;
118 };
119
120 bool isFinalState(const enum MatchState &State) {
121 return State == MatchState::UnchangedMatch ||
122 State == MatchState::UnchangedMismatch ||
123 State == MatchState::RecoveredMismatch ||
124 State == MatchState::RemovedMatch;
125 };
126
127 // Count the samples of checksum mismatched function for the top-level
128 // function and all inlinees.
129 void countMismatchedFuncSamples(const FunctionSamples &FS, bool IsTopLevel);
130 // Count the number of mismatched or recovered callsites.
131 void countMismatchCallsites(const FunctionSamples &FS);
132 // Count the samples of mismatched or recovered callsites for top-level
133 // function and all inlinees.
134 void countMismatchedCallsiteSamples(const FunctionSamples &FS);
135 void computeAndReportProfileStaleness();
136
137 LocToLocMap &getIRToProfileLocationMap(const Function &F) {
138 auto Ret = FuncMappings.try_emplace(
140 return Ret.first->second;
141 }
142 void distributeIRToProfileLocationMap();
143 void distributeIRToProfileLocationMap(FunctionSamples &FS);
144 // This function implements the Myers diff algorithm used for stale profile
145 // matching. The algorithm provides a simple and efficient way to find the
146 // Longest Common Subsequence(LCS) or the Shortest Edit Script(SES) of two
147 // sequences. For more details, refer to the paper 'An O(ND) Difference
148 // Algorithm and Its Variations' by Eugene W. Myers.
149 // In the scenario of profile fuzzy matching, the two sequences are the IR
150 // callsite anchors and profile callsite anchors. The subsequence equivalent
151 // parts from the resulting SES are used to remap the IR locations to the
152 // profile locations. As the number of function callsite is usually not big,
153 // we currently just implements the basic greedy version(page 6 of the paper).
155 longestCommonSequence(const AnchorList &IRCallsiteAnchors,
156 const AnchorList &ProfileCallsiteAnchors) const;
157 void matchNonCallsiteLocs(const LocToLocMap &AnchorMatchings,
158 const AnchorMap &IRAnchors,
159 LocToLocMap &IRToProfileLocationMap);
160 void runStaleProfileMatching(const Function &F, const AnchorMap &IRAnchors,
161 const AnchorMap &ProfileAnchors,
162 LocToLocMap &IRToProfileLocationMap);
163 void reportOrPersistProfileStats();
164};
165} // end namespace llvm
166#endif // LLVM_TRANSFORMS_IPO_SAMPLEPROFILEMATCHER_H
#define F(x, y, z)
Definition: MD5.cpp:55
This file provides the interface for the sampled PGO profile loader base implementation.
StringSet - A set-like wrapper for the StringMap.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
SampleProfileMatcher(Module &M, SampleProfileReader &Reader, const PseudoProbeManager *ProbeManager, ThinOrFullLTOPhase LTOPhase)
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:128
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.
Definition: StringMap.h:368
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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:744
static StringRef getCanonicalFnName(const Function &F)
Return the canonical name for a function, taking into account suffix elision policy attributes.
Definition: SampleProf.h:1085
This class provides operator overloads to the map container using MD5 as the key type,...
Definition: SampleProf.h:1306
iterator find(const SampleContext &Ctx)
Definition: SampleProf.h:1317
Sample-based profile reader.
std::unordered_map< LineLocation, LineLocation, LineLocationHash > LocToLocMap
Definition: SampleProf.h:737
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::vector< std::pair< LineLocation, FunctionId > > AnchorList
std::map< LineLocation, FunctionId > AnchorMap
ThinOrFullLTOPhase
This enumerates the LLVM full LTO or ThinLTO optimization phases.
Definition: Pass.h:76