[llvm] [SampleFDO] Read call-graph matching recovered top-level function profile (PR #101053)

Lei Wang via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 13 08:25:55 PDT 2024


================
@@ -863,6 +883,39 @@ bool SampleProfileMatcher::functionMatchesProfile(Function &IRFunc,
   return Matched;
 }
 
+void SampleProfileMatcher::UpdateSampleLoaderWithRecoveredProfiles() {
+  DenseSet<StringRef> RecoveredFuncs;
+  // Update FuncNameToProfNameMap and SymbolMap.
+  for (auto &I : FuncToProfileNameMap) {
+    assert(I.first && "New function is null");
+    FunctionId FuncName(I.first->getName());
+    RecoveredFuncs.insert(I.second.stringRef());
+    FuncNameToProfNameMap->emplace(FuncName, I.second);
+
+    // We need to remove the old entry to avoid duplicating the function
+    // processing.
+    SymbolMap->erase(FuncName);
+    SymbolMap->emplace(I.second, I.first);
+  }
+
+  // Read the top-level profiles for the recovered function profiles. This is
+  // because in extended binary format it only loads the top-level profile for
+  // the functions in the new build but not the recovered functions which is
+  // from the old build.
+  SampleProfileMap TopLevelRecoveredProfiles;
+  Reader.readOnDemand(RecoveredFuncs, TopLevelRecoveredProfiles);
+  auto &Profiles = Reader.getProfiles();
+  for (auto &I : TopLevelRecoveredProfiles) {
+    LLVM_DEBUG(dbgs() << "Top-level function " << I.second.getFunction()
+                      << " is recovered and re-read by the sample reader.\n");
+    auto &Ctx = I.second.getContext();
+    assert(Profiles.find(Ctx) == Profiles.end() &&
+           "Top level profile is found for the unused profile");
+    Profiles.create(Ctx) = std::move(I.second);
----------------
wlei-llvm wrote:

Good point! I was thinking using a different profile map so that we can debug check the reloaded profiles for the test, but I can test check it in a different place. 

https://github.com/llvm/llvm-project/pull/101053


More information about the llvm-commits mailing list