[llvm] [SampleFDO] Stale profile renaming matching (PR #92151)
David Li via llvm-commits
llvm-commits at lists.llvm.org
Tue May 28 21:30:40 PDT 2024
================
@@ -590,14 +617,314 @@ void SampleProfileMatcher::computeAndReportProfileStaleness() {
}
}
-void SampleProfileMatcher::runOnModule() {
- ProfileConverter::flattenProfile(Reader.getProfiles(), FlattenedProfiles,
- FunctionSamples::ProfileIsCS);
+// Find functions that don't show in the profile or profile symbol list, which
+// are supposed to be new functions. We use them as the targets for renaming
+// matching.
+void SampleProfileMatcher::findNewIRFunctions(
+ StringMap<Function *> &newIRFunctions) {
+ // TODO: Support MD5 profile.
+ if (FunctionSamples::UseMD5)
+ return;
+ StringSet<> NamesInProfile;
+ if (auto NameTable = Reader.getNameTable()) {
+ for (auto Name : *NameTable)
+ NamesInProfile.insert(Name.stringRef());
+ }
+
for (auto &F : M) {
- if (skipProfileForFunction(F))
+ // Skip declarations, as even if the function can be recognized renamed, we
+ // have nothing to do with it.
+ if (F.isDeclaration())
continue;
- runOnFunction(F);
+
+ StringRef CanonFName = FunctionSamples::getCanonicalFnName(F.getName());
+ const auto *FS = getFlattenedSamplesFor(F);
+ if (FS)
+ continue;
+
+ // For extended binary, functions are fully inlined may not be loaded in the
+ // top-level profile, so check the NameTable which has the all symbol names
+ // in profile.
+ if (NamesInProfile.count(CanonFName))
+ continue;
+
+ // For extended binary, non-profiled function symbols are in the profile
+ // symbol list table.
+ if (PSL && PSL->contains(CanonFName))
+ continue;
+
+ LLVM_DEBUG(dbgs() << "Function " << CanonFName
+ << " is not in profile or symbol list table.\n");
+ newIRFunctions[CanonFName] = &F;
+ }
+}
+
+void SampleProfileMatcher::findNewIRCallees(
+ Function &Caller, const StringMap<Function *> &newIRFunctions,
+ std::vector<Function *> &NewIRCallees) {
+ for (auto &BB : Caller) {
+ for (auto &I : BB) {
+ const auto *CB = dyn_cast<CallBase>(&I);
+ if (!CB || isa<IntrinsicInst>(&I))
+ continue;
+ Function *Callee = CB->getCalledFunction();
+ if (!Callee || Callee->isDeclaration())
+ continue;
+ StringRef CalleeName =
+ FunctionSamples::getCanonicalFnName(Callee->getName());
+ if (newIRFunctions.count(CalleeName))
+ NewIRCallees.push_back(Callee);
+ }
+ }
+}
+
+// Find the function using the profile name. If the function is not found but
+// the NewIRCallees is provided, try to match the function profile with all
+// functions in NewIRCallees and return the matched function.
+// The return pair includes the function pointer and a bool value indicating
+// whether the function is new(matched).
+std::pair<Function *, bool> SampleProfileMatcher::findOrMatchNewFunction(
+ const FunctionId &ProfCallee, FunctionMap &OldProfToNewSymbolMap,
+ const std::vector<Function *> &NewIRCallees = std::vector<Function *>()) {
+ auto F = SymbolMap->find(ProfCallee);
+ if (F != SymbolMap->end())
+ return {F->second, false};
+
+ // Existing matched function is found.
+ auto NewF = OldProfToNewSymbolMap.find(ProfCallee);
+ if (NewF != OldProfToNewSymbolMap.end())
+ return {NewF->second, true};
+
+ for (auto *IRCallee : NewIRCallees)
+ if (functionMatchesProfile(*IRCallee, ProfCallee)) {
+ OldProfToNewSymbolMap[ProfCallee] = IRCallee;
+ return {IRCallee, true};
+ }
+ return {nullptr, false};
+}
+
+// Determine if the function matches profile by computing a similarity ratio
+// between two callsite anchors sequences extracted from function and profile.
+bool SampleProfileMatcher::functionMatchesProfileHelper(
+ const Function &IRFunc, const FunctionId &ProfFunc) {
+ // The value is in the range [0, 1]. The bigger the value is, the more similar
+ // two sequences are.
+ float Similarity = 0.0;
+
+ const auto *FSFlattened = getFlattenedSamplesFor(ProfFunc);
+ assert(FSFlattened && "Flattened profile sample is null");
+ // Similarity check may not be reiable if the function is tiny, we use the
----------------
david-xl wrote:
s/reiable/reliable/
https://github.com/llvm/llvm-project/pull/92151
More information about the llvm-commits
mailing list