Skip to content

Commit b32bab8

Browse files
committed
addressing comments
1 parent 6db01bc commit b32bab8

File tree

2 files changed

+48
-38
lines changed

2 files changed

+48
-38
lines changed

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,22 +186,42 @@ class SampleProfileMatcher {
186186
void runStaleProfileMatching(const Function &F, const AnchorMap &IRAnchors,
187187
const AnchorMap &ProfileAnchors,
188188
LocToLocMap &IRToProfileLocationMap);
189+
/// Find the function using the profile name. If the function is not found but
190+
/// the \p NewIRCallees is provided, try to match the function profile with
191+
/// all functions in \p NewIRCallees and return the matched function.
192+
///
193+
/// \param ProfCallee The profile name of the callee.
194+
/// \param OldProfToNewSymbolMap The map from old profile name to new symbol.
195+
/// \param NewIRCallees The new candidate callees in the same scope to match.
196+
///
197+
/// \returns The matched function and a bool value indicating whether the
198+
/// function is new(matched).
189199
std::pair<Function *, bool>
190200
findOrMatchFunction(const FunctionId &ProfCallee,
191201
FunctionMap &OldProfToNewSymbolMap,
192202
const std::vector<Function *> &NewIRCallees);
193203
std::vector<FunctionSamples *> sortFuncProfiles(SampleProfileMap &ProfileMap);
194204
void findNewIRCallees(Function &Caller,
195-
const StringMap<Function *> &newIRFunctions,
205+
const StringMap<Function *> &NewIRFunctions,
196206
std::vector<Function *> &NewIRCallees);
197207
bool functionMatchesProfileHelper(const Function &IRFunc,
198208
const FunctionId &ProfFunc);
209+
/// Determine if the function matches profile by computing a similarity ratio
210+
/// between two callsite anchors extracted from function and profile. If it's
211+
/// above the threshold, the function matches the profile.
212+
///
213+
/// \returns True if the function matches profile.
199214
bool functionMatchesProfile(const Function &IRFunc,
200215
const FunctionId &ProfFunc);
201-
void matchProfileForNewFunctions(const StringMap<Function *> &newIRFunctions,
216+
void matchProfileForNewFunctions(const StringMap<Function *> &NewIRFunctions,
202217
FunctionSamples &FS,
203218
FunctionMap &OldProfToNewSymbolMap);
204-
void findNewIRFunctions(StringMap<Function *> &newIRFunctions);
219+
/// Find functions that don't show in the profile or profile symbol list,
220+
/// which are supposed to be new functions. We use them as the targets for
221+
/// renaming matching.
222+
///
223+
/// \param NewIRFunctions The map from function name to the IR function.
224+
void findNewIRFunctions(StringMap<Function *> &NewIRFunctions);
205225
void runCallGraphMatching();
206226
void reportOrPersistProfileStats();
207227
};

llvm/lib/Transforms/IPO/SampleProfileMatcher.cpp

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -617,11 +617,8 @@ void SampleProfileMatcher::computeAndReportProfileStaleness() {
617617
}
618618
}
619619

620-
// Find functions that don't show in the profile or profile symbol list, which
621-
// are supposed to be new functions. We use them as the targets for renaming
622-
// matching.
623620
void SampleProfileMatcher::findNewIRFunctions(
624-
StringMap<Function *> &newIRFunctions) {
621+
StringMap<Function *> &NewIRFunctions) {
625622
// TODO: Support MD5 profile.
626623
if (FunctionSamples::UseMD5)
627624
return;
@@ -655,12 +652,12 @@ void SampleProfileMatcher::findNewIRFunctions(
655652

656653
LLVM_DEBUG(dbgs() << "Function " << CanonFName
657654
<< " is not in profile or symbol list table.\n");
658-
newIRFunctions[CanonFName] = &F;
655+
NewIRFunctions[CanonFName] = &F;
659656
}
660657
}
661658

662659
void SampleProfileMatcher::findNewIRCallees(
663-
Function &Caller, const StringMap<Function *> &newIRFunctions,
660+
Function &Caller, const StringMap<Function *> &NewIRFunctions,
664661
std::vector<Function *> &NewIRCallees) {
665662
for (auto &BB : Caller) {
666663
for (auto &I : BB) {
@@ -672,17 +669,12 @@ void SampleProfileMatcher::findNewIRCallees(
672669
continue;
673670
StringRef CalleeName =
674671
FunctionSamples::getCanonicalFnName(Callee->getName());
675-
if (newIRFunctions.count(CalleeName))
672+
if (NewIRFunctions.count(CalleeName))
676673
NewIRCallees.push_back(Callee);
677674
}
678675
}
679676
}
680677

681-
// Find the function using the profile name. If the function is not found but
682-
// the NewIRCallees is provided, try to match the function profile with all
683-
// functions in NewIRCallees and return the matched function.
684-
// The return pair includes the function pointer and a bool value indicating
685-
// whether the function is new(matched).
686678
std::pair<Function *, bool> SampleProfileMatcher::findOrMatchFunction(
687679
const FunctionId &ProfCallee, FunctionMap &OldProfToNewSymbolMap,
688680
const std::vector<Function *> &NewIRCallees = std::vector<Function *>()) {
@@ -703,8 +695,6 @@ std::pair<Function *, bool> SampleProfileMatcher::findOrMatchFunction(
703695
return {nullptr, false};
704696
}
705697

706-
// Determine if the function matches profile by computing a similarity ratio
707-
// between two callsite anchors sequences extracted from function and profile.
708698
bool SampleProfileMatcher::functionMatchesProfileHelper(
709699
const Function &IRFunc, const FunctionId &ProfFunc) {
710700
// The value is in the range [0, 1]. The bigger the value is, the more similar
@@ -713,7 +703,7 @@ bool SampleProfileMatcher::functionMatchesProfileHelper(
713703

714704
const auto *FSFlattened = getFlattenedSamplesFor(ProfFunc);
715705
assert(FSFlattened && "Flattened profile sample is null");
716-
// Similarity check may not be reiable if the function is tiny, we use the
706+
// Similarity check may not be reliable if the function is tiny, we use the
717707
// number of basic block as a proxy for the function complexity and skip the
718708
// matching if it's too small.
719709
if (IRFunc.size() < MinBBForCGMatching ||
@@ -778,7 +768,7 @@ bool SampleProfileMatcher::functionMatchesProfile(const Function &IRFunc,
778768
// Match profile for new function on the profiled call-graph edge to limit the
779769
// matching scope.
780770
void SampleProfileMatcher::matchProfileForNewFunctions(
781-
const StringMap<Function *> &newIRFunctions, FunctionSamples &CallerFS,
771+
const StringMap<Function *> &NewIRFunctions, FunctionSamples &CallerFS,
782772
FunctionMap &OldProfToNewSymbolMap) {
783773
// Find the new candidate callees from IR in the current caller scope.
784774
std::vector<Function *> NewIRCallees;
@@ -788,7 +778,7 @@ void SampleProfileMatcher::matchProfileForNewFunctions(
788778
// No callees for external function, skip the rename matching.
789779
if (IRCaller->isDeclaration())
790780
return;
791-
findNewIRCallees(*IRCaller, newIRFunctions, NewIRCallees);
781+
findNewIRCallees(*IRCaller, NewIRFunctions, NewIRCallees);
792782
}
793783

794784
// Match non-inline callees.
@@ -842,7 +832,7 @@ void SampleProfileMatcher::matchProfileForNewFunctions(
842832
// Note that even there is no renaming in the current scope, there could
843833
// be renaming in deeper callee scope, we need to traverse all the callee
844834
// profiles.
845-
matchProfileForNewFunctions(newIRFunctions, CalleeFS,
835+
matchProfileForNewFunctions(NewIRFunctions, CalleeFS,
846836
OldProfToNewSymbolMap);
847837
}
848838

@@ -880,9 +870,9 @@ void SampleProfileMatcher::runCallGraphMatching() {
880870
assert(FunctionProfileNameMap.empty() &&
881871
"FunctionProfileNameMap is not empty before the call graph matching");
882872

883-
StringMap<Function *> newIRFunctions;
884-
findNewIRFunctions(newIRFunctions);
885-
if (newIRFunctions.empty())
873+
StringMap<Function *> NewIRFunctions;
874+
findNewIRFunctions(NewIRFunctions);
875+
if (NewIRFunctions.empty())
886876
return;
887877

888878
// The new functions found by the renaming matching. Save them into a map
@@ -891,23 +881,23 @@ void SampleProfileMatcher::runCallGraphMatching() {
891881
FunctionMap OldProfToNewSymbolMap;
892882
// Sort the profiles to make the matching order deterministic.
893883
for (auto *P : sortFuncProfiles(Reader.getProfiles()))
894-
matchProfileForNewFunctions(newIRFunctions, *P, OldProfToNewSymbolMap);
884+
matchProfileForNewFunctions(NewIRFunctions, *P, OldProfToNewSymbolMap);
895885

886+
FunctionProfileNameMap.clear();
887+
if (OldProfToNewSymbolMap.empty())
888+
return;
896889
// Update all the data generated by the old profile.
897-
if (!OldProfToNewSymbolMap.empty()) {
898-
// Add the new function to the SymbolMap, which will be used in
899-
// SampleLoader.
900-
for (auto &I : OldProfToNewSymbolMap) {
901-
assert(I.second && "New function is null");
902-
SymbolMap->emplace(FunctionId(I.second->getName()), I.second);
903-
}
904-
905-
// Re-flatten the profiles after the renaming.
906-
FlattenedProfiles.clear();
907-
ProfileConverter::flattenProfile(Reader.getProfiles(), FlattenedProfiles,
908-
FunctionSamples::ProfileIsCS);
890+
// Add the new function to the SymbolMap, which will be used in
891+
// SampleLoader.
892+
for (auto &I : OldProfToNewSymbolMap) {
893+
assert(I.second && "New function is null");
894+
SymbolMap->emplace(FunctionId(I.second->getName()), I.second);
909895
}
910-
FunctionProfileNameMap.clear();
896+
897+
// Re-flatten the profiles after the renaming.
898+
FlattenedProfiles.clear();
899+
ProfileConverter::flattenProfile(Reader.getProfiles(), FlattenedProfiles,
900+
FunctionSamples::ProfileIsCS);
911901
}
912902

913903
void SampleProfileMatcher::runOnFunction(Function &F) {

0 commit comments

Comments
 (0)