Skip to content

Commit

Permalink
[SampleFDO] Stale profile renaming matching
Browse files Browse the repository at this point in the history
  • Loading branch information
wlei-llvm committed May 16, 2024
1 parent 6bf1859 commit e6b7077
Show file tree
Hide file tree
Showing 5 changed files with 687 additions and 26 deletions.
54 changes: 45 additions & 9 deletions llvm/include/llvm/Transforms/IPO/SampleProfileMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
#define LLVM_TRANSFORMS_IPO_SAMPLEPROFILEMATCHER_H

#include "llvm/ADT/StringSet.h"
#include "llvm/Analysis/ProfileSummaryInfo.h"
#include "llvm/Transforms/Utils/SampleProfileLoaderBaseImpl.h"

namespace llvm {

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

// Sample profile matching - fuzzy match.
class SampleProfileMatcher {
Expand Down Expand Up @@ -58,6 +60,20 @@ class SampleProfileMatcher {
StringMap<std::unordered_map<LineLocation, MatchState, LineLocationHash>>
FuncCallsiteMatchStates;

struct RenameDecisionCacheHash {
uint64_t
operator()(const std::pair<const Function *, FunctionId> &P) const {
return hash_combine(P.first, P.second);
}
};
std::unordered_map<std::pair<const Function *, FunctionId>, bool,
RenameDecisionCacheHash>
RenameDecisionCache;

FunctionMap *SymbolMap;

std::shared_ptr<ProfileSymbolList> PSL;

// Profile mismatch statstics:
uint64_t TotalProfiledFunc = 0;
// Num of checksum-mismatched function.
Expand All @@ -80,26 +96,32 @@ class SampleProfileMatcher {
public:
SampleProfileMatcher(Module &M, SampleProfileReader &Reader,
const PseudoProbeManager *ProbeManager,
ThinOrFullLTOPhase LTOPhase)
: M(M), Reader(Reader), ProbeManager(ProbeManager), LTOPhase(LTOPhase){};
void runOnModule();
ThinOrFullLTOPhase LTOPhase,
std::shared_ptr<ProfileSymbolList> PSL)
: M(M), Reader(Reader), ProbeManager(ProbeManager), LTOPhase(LTOPhase),
PSL(PSL){};
void runOnModule(FunctionMap &SymbolMap);
void clearMatchingData() {
// Do not clear FuncMappings, it stores IRLoc to ProfLoc remappings which
// will be used for sample loader.
FuncCallsiteMatchStates.clear();
}

private:
FunctionSamples *getFlattenedSamplesFor(const Function &F) {
StringRef CanonFName = FunctionSamples::getCanonicalFnName(F);
auto It = FlattenedProfiles.find(FunctionId(CanonFName));
FunctionSamples *getFlattenedSamplesFor(const FunctionId &Fname) {
auto It = FlattenedProfiles.find(Fname);
if (It != FlattenedProfiles.end())
return &It->second;
return nullptr;
}
void runOnFunction(Function &F);
void findIRAnchors(const Function &F, AnchorMap &IRAnchors);
void findProfileAnchors(const FunctionSamples &FS, AnchorMap &ProfileAnchors);
FunctionSamples *getFlattenedSamplesFor(const Function &F) {
StringRef CanonFName = FunctionSamples::getCanonicalFnName(F);
return getFlattenedSamplesFor(FunctionId(CanonFName));
}
void runBlockLevelMatching(Function &F);
void findIRAnchors(const Function &F, AnchorMap &IRAnchors) const;
void findProfileAnchors(const FunctionSamples &FS,
AnchorMap &ProfileAnchors) const;
// Record the callsite match states for profile staleness report, the result
// is saved in FuncCallsiteMatchStates.
void recordCallsiteMatchStates(const Function &F, const AnchorMap &IRAnchors,
Expand Down Expand Up @@ -160,6 +182,20 @@ class SampleProfileMatcher {
void runStaleProfileMatching(const Function &F, const AnchorMap &IRAnchors,
const AnchorMap &ProfileAnchors,
LocToLocMap &IRToProfileLocationMap);
void findIRNewCallees(Function &Caller,
const StringMap<Function *> &IRNewFunctions,
std::vector<Function *> &IRNewCallees);
float checkFunctionSimilarity(const Function &IRFunc,
const FunctionId &ProfFunc);
bool functionIsRenamedImpl(const Function &IRFunc,
const FunctionId &ProfFunc);
bool functionIsRenamed(const Function &IRFunc, const FunctionId &ProfFunc);
void
runFuncRenamingMatchingOnProfile(const StringMap<Function *> &IRNewFunctions,
FunctionSamples &FS,
FunctionMap &OldProfToNewSymbolMap);
void findIRNewFunctions(StringMap<Function *> &IRNewFunctions);
void runFuncLevelMatching();
void reportOrPersistProfileStats();
};
} // end namespace llvm
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Transforms/IPO/SampleProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ class SampleProfileLoader final : public SampleProfileLoaderBaseImpl<Function> {

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

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

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

if (ReportProfileStaleness || PersistProfileStaleness ||
SalvageStaleProfile) {
MatchingManager->runOnModule();
MatchingManager->runOnModule(SymbolMap);
MatchingManager->clearMatchingData();
}

Expand Down
Loading

0 comments on commit e6b7077

Please sign in to comment.