[llvm-branch-commits] [llvm] [BOLT] Match blocks with calls as anchors (PR #96596)

Maksim Panchenko via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Jul 8 12:25:45 PDT 2024


================
@@ -155,5 +155,52 @@ std::string hashBlockLoose(BinaryContext &BC, const BinaryBasicBlock &BB) {
   return HashString;
 }
 
+/// An even looser hash level relative to $ hashBlockLoose to use with stale
+/// profile matching, composed of the names of a block's called functions in
+/// lexicographic order.
+std::string hashBlockCalls(BinaryContext &BC, const BinaryBasicBlock &BB) {
+  // The hash is computed by creating a string of all lexicographically ordered
+  // called function names.
+  std::multiset<std::string> FunctionNames;
+  for (const MCInst &Instr : BB) {
+    // Skip non-call instructions.
+    if (!BC.MIB->isCall(Instr))
+      continue;
+    const MCSymbol *CallSymbol = BC.MIB->getTargetSymbol(Instr);
+    if (!CallSymbol)
+      continue;
+    FunctionNames.insert(std::string(CallSymbol->getName()));
+  }
+
+  std::string HashString;
+  for (const std::string &FunctionName : FunctionNames)
+    HashString.append(FunctionName);
+
+  return HashString;
+}
+
+/// The same as the $hashBlockCalls function, but for profiled functions.
+std::string
+hashBlockCalls(const DenseMap<uint32_t, StringRef> &IdToFunctionName,
+               const yaml::bolt::BinaryBasicBlockProfile &YamlBB) {
+  std::multiset<std::string> FunctionNames;
+  for (const yaml::bolt::CallSiteInfo &CallSiteInfo : YamlBB.CallSites) {
+    auto It = IdToFunctionName.find(CallSiteInfo.DestId);
+    if (It == IdToFunctionName.end())
+      continue;
+    StringRef Name = It->second;
+    const size_t Pos = Name.find("(*");
----------------
maksfb wrote:

Let's expand `NameResolver` interface to restore the name of the symbol and use it here.

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


More information about the llvm-branch-commits mailing list