Skip to content

Commit 89bdc6e

Browse files
committed
[SampleFDO] Stale profile renaming matching
1 parent 6bf1859 commit 89bdc6e

File tree

5 files changed

+687
-26
lines changed

5 files changed

+687
-26
lines changed

llvm/include/llvm/Transforms/IPO/SampleProfileMatcher.h

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
#define LLVM_TRANSFORMS_IPO_SAMPLEPROFILEMATCHER_H
1616

1717
#include "llvm/ADT/StringSet.h"
18+
#include "llvm/Analysis/ProfileSummaryInfo.h"
1819
#include "llvm/Transforms/Utils/SampleProfileLoaderBaseImpl.h"
1920

2021
namespace llvm {
2122

2223
using AnchorList = std::vector<std::pair<LineLocation, FunctionId>>;
2324
using AnchorMap = std::map<LineLocation, FunctionId>;
25+
using FunctionMap = HashKeyMap<std::unordered_map, FunctionId, Function *>;
2426

2527
// Sample profile matching - fuzzy match.
2628
class SampleProfileMatcher {
@@ -58,6 +60,20 @@ class SampleProfileMatcher {
5860
StringMap<std::unordered_map<LineLocation, MatchState, LineLocationHash>>
5961
FuncCallsiteMatchStates;
6062

63+
struct RenameDecisionCacheHash {
64+
uint64_t
65+
operator()(const std::pair<const Function *, FunctionId> &P) const {
66+
return hash_combine(P.first, P.second);
67+
}
68+
};
69+
std::unordered_map<std::pair<const Function *, FunctionId>, bool,
70+
RenameDecisionCacheHash>
71+
RenameDecisionCache;
72+
73+
FunctionMap *SymbolMap;
74+
75+
std::shared_ptr<ProfileSymbolList> PSL;
76+
6177
// Profile mismatch statstics:
6278
uint64_t TotalProfiledFunc = 0;
6379
// Num of checksum-mismatched function.
@@ -80,26 +96,32 @@ class SampleProfileMatcher {
8096
public:
8197
SampleProfileMatcher(Module &M, SampleProfileReader &Reader,
8298
const PseudoProbeManager *ProbeManager,
83-
ThinOrFullLTOPhase LTOPhase)
84-
: M(M), Reader(Reader), ProbeManager(ProbeManager), LTOPhase(LTOPhase){};
85-
void runOnModule();
99+
ThinOrFullLTOPhase LTOPhase,
100+
std::shared_ptr<ProfileSymbolList> PSL)
101+
: M(M), Reader(Reader), ProbeManager(ProbeManager), LTOPhase(LTOPhase),
102+
PSL(PSL) {};
103+
void runOnModule(FunctionMap &SymbolMap);
86104
void clearMatchingData() {
87105
// Do not clear FuncMappings, it stores IRLoc to ProfLoc remappings which
88106
// will be used for sample loader.
89107
FuncCallsiteMatchStates.clear();
90108
}
91109

92110
private:
93-
FunctionSamples *getFlattenedSamplesFor(const Function &F) {
94-
StringRef CanonFName = FunctionSamples::getCanonicalFnName(F);
95-
auto It = FlattenedProfiles.find(FunctionId(CanonFName));
111+
FunctionSamples *getFlattenedSamplesFor(const FunctionId &Fname) {
112+
auto It = FlattenedProfiles.find(Fname);
96113
if (It != FlattenedProfiles.end())
97114
return &It->second;
98115
return nullptr;
99116
}
100-
void runOnFunction(Function &F);
101-
void findIRAnchors(const Function &F, AnchorMap &IRAnchors);
102-
void findProfileAnchors(const FunctionSamples &FS, AnchorMap &ProfileAnchors);
117+
FunctionSamples *getFlattenedSamplesFor(const Function &F) {
118+
StringRef CanonFName = FunctionSamples::getCanonicalFnName(F);
119+
return getFlattenedSamplesFor(FunctionId(CanonFName));
120+
}
121+
void runBlockLevelMatching(Function &F);
122+
void findIRAnchors(const Function &F, AnchorMap &IRAnchors) const;
123+
void findProfileAnchors(const FunctionSamples &FS,
124+
AnchorMap &ProfileAnchors) const;
103125
// Record the callsite match states for profile staleness report, the result
104126
// is saved in FuncCallsiteMatchStates.
105127
void recordCallsiteMatchStates(const Function &F, const AnchorMap &IRAnchors,
@@ -160,6 +182,20 @@ class SampleProfileMatcher {
160182
void runStaleProfileMatching(const Function &F, const AnchorMap &IRAnchors,
161183
const AnchorMap &ProfileAnchors,
162184
LocToLocMap &IRToProfileLocationMap);
185+
void findIRNewCallees(Function &Caller,
186+
const StringMap<Function *> &IRNewFunctions,
187+
std::vector<Function *> &IRNewCallees);
188+
float checkFunctionSimilarity(const Function &IRFunc,
189+
const FunctionId &ProfFunc);
190+
bool functionIsRenamedImpl(const Function &IRFunc,
191+
const FunctionId &ProfFunc);
192+
bool functionIsRenamed(const Function &IRFunc, const FunctionId &ProfFunc);
193+
void
194+
runFuncRenamingMatchingOnProfile(const StringMap<Function *> &IRNewFunctions,
195+
FunctionSamples &FS,
196+
FunctionMap &OldProfToNewSymbolMap);
197+
void findIRNewFunctions(StringMap<Function *> &IRNewFunctions);
198+
void runFuncLevelMatching();
163199
void reportOrPersistProfileStats();
164200
};
165201
} // end namespace llvm

llvm/lib/Transforms/IPO/SampleProfile.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ class SampleProfileLoader final : public SampleProfileLoaderBaseImpl<Function> {
544544

545545
/// Profle Symbol list tells whether a function name appears in the binary
546546
/// used to generate the current profile.
547-
std::unique_ptr<ProfileSymbolList> PSL;
547+
std::shared_ptr<ProfileSymbolList> PSL;
548548

549549
/// Total number of samples collected in this profile.
550550
///
@@ -2076,7 +2076,7 @@ bool SampleProfileLoader::doInitialization(Module &M,
20762076
if (ReportProfileStaleness || PersistProfileStaleness ||
20772077
SalvageStaleProfile) {
20782078
MatchingManager = std::make_unique<SampleProfileMatcher>(
2079-
M, *Reader, ProbeManager.get(), LTOPhase);
2079+
M, *Reader, ProbeManager.get(), LTOPhase, PSL);
20802080
}
20812081

20822082
return true;
@@ -2197,7 +2197,7 @@ bool SampleProfileLoader::runOnModule(Module &M, ModuleAnalysisManager *AM,
21972197

21982198
if (ReportProfileStaleness || PersistProfileStaleness ||
21992199
SalvageStaleProfile) {
2200-
MatchingManager->runOnModule();
2200+
MatchingManager->runOnModule(SymbolMap);
22012201
MatchingManager->clearMatchingData();
22022202
}
22032203

0 commit comments

Comments
 (0)