[llvm-branch-commits] [llvm] [BOLT] Match functions with pseudo probes (PR #100446)

Amir Ayupov via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Nov 8 13:16:04 PST 2024


https://github.com/aaupov updated https://github.com/llvm/llvm-project/pull/100446

>From 56b45b104a2ab2dbc4ab8e9643c90092894b579e Mon Sep 17 00:00:00 2001
From: shawbyoung <shawbyoung at gmail.com>
Date: Wed, 24 Jul 2024 11:29:22 -0700
Subject: [PATCH 1/6] Comment

Created using spr 1.3.4
---
 bolt/include/bolt/Profile/YAMLProfileReader.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bolt/include/bolt/Profile/YAMLProfileReader.h b/bolt/include/bolt/Profile/YAMLProfileReader.h
index 6c00f82302fb92..bc09751fcae75e 100644
--- a/bolt/include/bolt/Profile/YAMLProfileReader.h
+++ b/bolt/include/bolt/Profile/YAMLProfileReader.h
@@ -108,7 +108,7 @@ class YAMLProfileReader : public ProfileReaderBase {
   std::vector<BinaryFunction *> YamlProfileToFunction;
 
   using FunctionSet = std::unordered_set<const BinaryFunction *>;
-  /// To keep track of functions that have a matched profile before the profilez
+  /// To keep track of functions that have a matched profile before the profile
   /// is attributed.
   FunctionSet ProfiledFunctions;
 

>From b851ca65c2bf2a9569315d62722b60a04c8102ee Mon Sep 17 00:00:00 2001
From: shawbyoung <shawbyoung at gmail.com>
Date: Wed, 24 Jul 2024 11:39:48 -0700
Subject: [PATCH 2/6] Was accessing wrong YamlBF Hash, fixed

Created using spr 1.3.4
---
 bolt/lib/Profile/YAMLProfileReader.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bolt/lib/Profile/YAMLProfileReader.cpp b/bolt/lib/Profile/YAMLProfileReader.cpp
index 68af95a1cd043e..f5ac0b8e2c56a2 100644
--- a/bolt/lib/Profile/YAMLProfileReader.cpp
+++ b/bolt/lib/Profile/YAMLProfileReader.cpp
@@ -614,7 +614,7 @@ size_t YAMLProfileReader::matchWithPseudoProbes(BinaryContext &BC) {
 
   uint64_t MatchedWithPseudoProbes = 0;
   for (yaml::bolt::BinaryFunctionProfile &YamlBF : YamlBP.Functions) {
-    auto It = PseudoProbeDescHashToBF.find(YamlBF.Hash);
+    auto It = PseudoProbeDescHashToBF.find(YamlBF.PseudoProbeDescHash);
     if (It == PseudoProbeDescHashToBF.end())
       continue;
     BinaryFunction *BF = It->second;

>From 39ba7175c9224c3584db7f5f8ca8fbed14da41e5 Mon Sep 17 00:00:00 2001
From: shawbyoung <shawbyoung at gmail.com>
Date: Wed, 24 Jul 2024 11:49:54 -0700
Subject: [PATCH 3/6] Changed ordering of matching

Created using spr 1.3.4
---
 bolt/lib/Profile/YAMLProfileReader.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/bolt/lib/Profile/YAMLProfileReader.cpp b/bolt/lib/Profile/YAMLProfileReader.cpp
index f5ac0b8e2c56a2..75ec4465856a15 100644
--- a/bolt/lib/Profile/YAMLProfileReader.cpp
+++ b/bolt/lib/Profile/YAMLProfileReader.cpp
@@ -770,8 +770,8 @@ Error YAMLProfileReader::readProfile(BinaryContext &BC) {
   const size_t MatchedWithHash = matchWithHash(BC);
   const size_t MatchedWithLTOCommonName = matchWithLTOCommonName();
   const size_t MatchedWithCallGraph = matchWithCallGraph(BC);
-  const size_t MatchedWithNameSimilarity = matchWithNameSimilarity(BC);
   const size_t MatchedWithPseudoProbes = matchWithPseudoProbes(BC);
+  const size_t MatchedWithNameSimilarity = matchWithNameSimilarity(BC);
 
   for (auto [YamlBF, BF] : llvm::zip_equal(YamlBP.Functions, ProfileBFs))
     if (!YamlBF.Used && BF && !ProfiledFunctions.count(BF))
@@ -792,10 +792,10 @@ Error YAMLProfileReader::readProfile(BinaryContext &BC) {
            << " functions with matching LTO common names\n";
     outs() << "BOLT-INFO: matched " << MatchedWithCallGraph
            << " functions with call graph\n";
-    outs() << "BOLT-INFO: matched " << MatchedWithNameSimilarity
-           << " functions with similar names\n";
     outs() << "BOLT-INFO: matched " << MatchedWithPseudoProbes
            << " functions with pseudo probes\n";
+    outs() << "BOLT-INFO: matched " << MatchedWithNameSimilarity
+           << " functions with similar names\n";
   }
 
   // Set for parseFunctionProfile().

>From 11af7f19953da7c5ad4eb263be3d38a70b2518e0 Mon Sep 17 00:00:00 2001
From: shawbyoung <shawbyoung at gmail.com>
Date: Wed, 24 Jul 2024 12:56:56 -0700
Subject: [PATCH 4/6] Added check for YamlBF.Used in pseudo probe function
 matching

Created using spr 1.3.4
---
 bolt/lib/Profile/YAMLProfileReader.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/bolt/lib/Profile/YAMLProfileReader.cpp b/bolt/lib/Profile/YAMLProfileReader.cpp
index 75ec4465856a15..8dfdf1fb30eb36 100644
--- a/bolt/lib/Profile/YAMLProfileReader.cpp
+++ b/bolt/lib/Profile/YAMLProfileReader.cpp
@@ -614,6 +614,8 @@ size_t YAMLProfileReader::matchWithPseudoProbes(BinaryContext &BC) {
 
   uint64_t MatchedWithPseudoProbes = 0;
   for (yaml::bolt::BinaryFunctionProfile &YamlBF : YamlBP.Functions) {
+    if (YamlBF.Used)
+      continue;
     auto It = PseudoProbeDescHashToBF.find(YamlBF.PseudoProbeDescHash);
     if (It == PseudoProbeDescHashToBF.end())
       continue;

>From ad4d98fc4bf3f16d119ddbc5abad11b93641bf99 Mon Sep 17 00:00:00 2001
From: shawbyoung <shawbyoung at gmail.com>
Date: Wed, 11 Sep 2024 15:49:03 -0700
Subject: [PATCH 5/6] Debug logging

Created using spr 1.3.4
---
 bolt/lib/Profile/YAMLProfileReader.cpp | 31 +++++++++++++++++++-------
 1 file changed, 23 insertions(+), 8 deletions(-)

diff --git a/bolt/lib/Profile/YAMLProfileReader.cpp b/bolt/lib/Profile/YAMLProfileReader.cpp
index 9aa5eb8391ab47..ecb786f3679517 100644
--- a/bolt/lib/Profile/YAMLProfileReader.cpp
+++ b/bolt/lib/Profile/YAMLProfileReader.cpp
@@ -20,6 +20,9 @@
 #include "llvm/Support/CommandLine.h"
 #include <algorithm>
 
+#undef DEBUG_TYPE
+#define DEBUG_TYPE "bolt-prof"
+
 using namespace llvm;
 
 namespace opts {
@@ -666,24 +669,35 @@ size_t YAMLProfileReader::matchWithPseudoProbes(BinaryContext &BC) {
 
   // Get unused profile functions GUID.
   for (yaml::bolt::BinaryFunctionProfile &YamlBF : YamlBP.Functions) {
-    if (YamlBF.Used || YamlBF.InlineTree.empty())
+    if (YamlBF.Used)
+      continue;
+    LLVM_DEBUG(dbgs() << "Attempting to match " << YamlBF.Name
+                      << " using pseudo probes: ");
+    if (YamlBF.InlineTree.empty()) {
+      LLVM_DEBUG(dbgs() << "no probe information\n");
       continue;
+    }
     uint64_t GUIDIdx = YamlBF.InlineTree.front().GUIDIndex;
     assert(GUIDIdx - 1 < YamlPD.GUID.size());
     uint64_t YamlGUID = YamlPD.GUID[GUIDIdx - 1];
 
     // Look up corresponding GUID in binary.
     auto It = GUID2FuncDescMap.find(YamlGUID);
-    if (It == GUID2FuncDescMap.end())
+    if (It == GUID2FuncDescMap.end()) {
+      LLVM_DEBUG(dbgs() << "no function with GUID=" << YamlGUID
+                        << " in the binary\n");
       continue;
+    }
 
     // Check if checksums match between profile and binary.
     assert(GUIDIdx - 1 < YamlPD.GUIDHash.size());
     uint64_t HashIdx = YamlPD.GUIDHash[GUIDIdx - 1];
     assert(HashIdx < YamlPD.Hash.size());
     uint64_t YamlHash = YamlPD.Hash[HashIdx];
-    if (YamlHash != It->FuncHash)
+    if (YamlHash != It->FuncHash) {
+      LLVM_DEBUG(dbgs() << "hash mismatch\n");
       continue;
+    }
 
     // Look for binary inline trees with the best match to YAML inline tree.
     auto Range = llvm::make_range(std::equal_range(
@@ -707,6 +721,8 @@ size_t YAMLProfileReader::matchWithPseudoProbes(BinaryContext &BC) {
       BestNodes.push_back(Node);
     }
 
+    LLVM_DEBUG(dbgs() << BestNodes.size() << " candidates: ");
+
     // Find binary functions containing best matching binary inline tree nodes.
     DenseMap<BinaryFunction *, uint32_t> BFProbeCountMap;
     for (const MCDecodedPseudoProbeInlineTree *Node : BestNodes) {
@@ -714,10 +730,6 @@ size_t YAMLProfileReader::matchWithPseudoProbes(BinaryContext &BC) {
       const MCDecodedPseudoProbeInlineTree *Root = Node;
       while (!Root->Parent->isRoot())
         Root = (const MCDecodedPseudoProbeInlineTree *)Root->Parent;
-      // Get containing non-profiled binary function.
-      auto GUIDToBFIt = GUIDToBF.find(Root->Guid);
-      if (GUIDToBFIt == GUIDToBF.end())
-        continue;
       // Compute the number of probes belonging to functions.
       for (const MCDecodedPseudoProbe &Probe : Node->getProbes()) {
         uint64_t Address = Probe.getAddress();
@@ -732,10 +744,13 @@ size_t YAMLProfileReader::matchWithPseudoProbes(BinaryContext &BC) {
     llvm::sort(BFProbeCountVec);
     for (BinaryFunction *BF : llvm::make_second_range(BFProbeCountVec)) {
       // Check if BF already has profile attached, bail for now.
-      if (ProfiledFunctions.count(BF))
+      if (ProfiledFunctions.count(BF)) {
+        LLVM_DEBUG(dbgs() << *BF << " is already profiled, ");
         continue;
+      }
       matchProfileToFunction(YamlBF, *BF);
       ++MatchedWithPseudoProbes;
+      LLVM_DEBUG(dbgs() << "matched to " << *BF << '\n');
       break;
     }
   }

>From 15643b7da8102e313a28f0f14dd4e607f7a81323 Mon Sep 17 00:00:00 2001
From: shawbyoung <shawbyoung at gmail.com>
Date: Fri, 8 Nov 2024 13:15:53 -0800
Subject: [PATCH 6/6] clang-format

Created using spr 1.3.4
---
 bolt/lib/Profile/YAMLProfileReader.cpp | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/bolt/lib/Profile/YAMLProfileReader.cpp b/bolt/lib/Profile/YAMLProfileReader.cpp
index 07bc890926652e..2c0c3cc1376b78 100644
--- a/bolt/lib/Profile/YAMLProfileReader.cpp
+++ b/bolt/lib/Profile/YAMLProfileReader.cpp
@@ -51,9 +51,10 @@ llvm::cl::opt<bool>
                        cl::desc("Match functions with call graph"), cl::Hidden,
                        cl::cat(BoltOptCategory));
 
-llvm::cl::opt<bool> MatchWithPseudoProbes(
-    "match-with-pseudo-probes", cl::desc("Match functions with pseudo probes"),
-    cl::Hidden, cl::cat(BoltOptCategory));
+llvm::cl::opt<bool>
+    MatchWithPseudoProbes("match-with-pseudo-probes",
+                          cl::desc("Match functions with pseudo probes"),
+                          cl::Hidden, cl::cat(BoltOptCategory));
 
 llvm::cl::opt<bool> ProfileUseDFS("profile-use-dfs",
                                   cl::desc("use DFS order for YAML profile"),
@@ -809,12 +810,11 @@ size_t YAMLProfileReader::matchWithPseudoProbes(BinaryContext &BC) {
       const auto ProbeIt = Node->getProbes().begin();
       const auto *Probe =
           (ProbeIt != Node->getProbes().end()) ? &*ProbeIt : nullptr;
-      LLVM_DEBUG(dbgs()
-                 << MatchedNodes << "/" << YamlBF.InlineTree.size()
-                 << " match with " << *BF << " at "
-                 << (Probe ? Probe->getInlineContextStr(GUID2FuncDescMap)
-                           : "(none)")
-                 << '\n');
+      LLVM_DEBUG(dbgs() << MatchedNodes << "/" << YamlBF.InlineTree.size()
+                        << " match with " << *BF << " at "
+                        << (Probe ? Probe->getInlineContextStr(GUID2FuncDescMap)
+                                  : "(none)")
+                        << '\n');
     }
     MatchedWithPseudoProbes += !!Matched;
     YamlBF.Used |= !!Matched;



More information about the llvm-branch-commits mailing list