[llvm] [SampleFDO] Stale profile call-graph matching (PR #95135)

Lei Wang via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 17 16:28:25 PDT 2024


================
@@ -279,36 +345,42 @@ void SampleProfileMatcher::matchNonCallsiteLocs(
 // The output mapping: [2->3, 3->4, 5->7, 6->8, 7->9].
 void SampleProfileMatcher::runStaleProfileMatching(
     const Function &F, const AnchorMap &IRAnchors,
-    const AnchorMap &ProfileAnchors, LocToLocMap &IRToProfileLocationMap) {
+    const AnchorMap &ProfileAnchors, LocToLocMap &IRToProfileLocationMap,
+    bool RunCFGMatching, bool RunCGMatching) {
+  if (!RunCFGMatching && !RunCGMatching)
+    return;
   LLVM_DEBUG(dbgs() << "Run stale profile matching for " << F.getName()
                     << "\n");
   assert(IRToProfileLocationMap.empty() &&
          "Run stale profile matching only once per function");
 
   AnchorList FilteredProfileAnchorList;
-  for (const auto &I : ProfileAnchors)
-    FilteredProfileAnchorList.emplace_back(I);
-
   AnchorList FilteredIRAnchorsList;
-  // Filter the non-callsite from IRAnchors.
-  for (const auto &I : IRAnchors) {
-    if (I.second.stringRef().empty())
-      continue;
-    FilteredIRAnchorsList.emplace_back(I);
-  }
+  getFilteredAnchorList(IRAnchors, ProfileAnchors, FilteredIRAnchorsList,
+                        FilteredProfileAnchorList);
 
   if (FilteredIRAnchorsList.empty() || FilteredProfileAnchorList.empty())
     return;
 
   // Match the callsite anchors by finding the longest common subsequence
-  // between IR and profile. Note that we need to use IR anchor as base(A side)
-  // to align with the order of IRToProfileLocationMap.
-  LocToLocMap MatchedAnchors =
-      longestCommonSequence(FilteredIRAnchorsList, FilteredProfileAnchorList);
+  // between IR and profile.
+  // Define a match between two anchors as follows:
+  // 1) The function names of anchors are the same.
+  // 2) The similarity between the anchor functions is above a threshold if
+  // RunCGMatching is set.
+  // For 2), we only consider the anchor functions from IR and profile don't
+  // appear on either side to reduce the matching scope. Note that we need to
+  // use IR anchor as base(A side) to align with the order of
+  // IRToProfileLocationMap.
+  LocToLocMap MatchedAnchors = longestCommonSequence(
+      FilteredIRAnchorsList, FilteredProfileAnchorList, RunCGMatching);
----------------
wlei-llvm wrote:

I see, the caller `RunCGMatching` is to be consistent with `RunCFGMatching`, added a comment along with the argument to clarify. 

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


More information about the llvm-commits mailing list