[llvm-branch-commits] [llvm] [BOLT] Name similarity function matching (PR #95884)

Maksim Panchenko via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Jun 28 14:10:31 PDT 2024


================
@@ -342,6 +350,108 @@ bool YAMLProfileReader::mayHaveProfileData(const BinaryFunction &BF) {
   return false;
 }
 
+uint64_t YAMLProfileReader::matchWithNameSimilarity(BinaryContext &BC) {
+  uint64_t MatchedWithNameSimilarity = 0;
+  ItaniumPartialDemangler ItaniumPartialDemangler;
+
+  // Demangle and derive namespace from function name.
+  auto DemangleName = [&](std::string &FunctionName) {
+    StringRef RestoredName = NameResolver::restore(FunctionName);
+    return demangle(RestoredName);
+  };
+  auto DeriveNameSpace = [&](std::string &DemangledName) {
+    if (ItaniumPartialDemangler.partialDemangle(DemangledName.c_str()))
+      return std::string("");
+    std::vector<char> Buffer(DemangledName.begin(), DemangledName.end());
+    size_t BufferSize = Buffer.size();
+    char *NameSpace = ItaniumPartialDemangler.getFunctionDeclContextName(
+        &Buffer[0], &BufferSize);
+    return NameSpace ? std::string(NameSpace) : std::string("");
+  };
+
+  // Maps namespaces to associated function block counts and gets profile
+  // function names and namespaces to minimize the number of BFs to process and
+  // avoid repeated name demangling/namespace derivision.
+  StringMap<std::set<uint32_t>>
+    NamespaceToProfiledBFSizes;
+  std::vector<std::string> ProfileBFDemangledNames;
+  ProfileBFDemangledNames.reserve(YamlBP.Functions.size());
+  std::vector<std::string> ProfiledBFNamespaces;
+  ProfiledBFNamespaces.reserve(YamlBP.Functions.size());
+
+  for (auto &YamlBF : YamlBP.Functions) {
+    std::string YamlBFDemangledName = DemangleName(YamlBF.Name);
+    ProfileBFDemangledNames.push_back(YamlBFDemangledName);
+    std::string YamlBFNamespace = DeriveNameSpace(YamlBFDemangledName);
+    ProfiledBFNamespaces.push_back(YamlBFNamespace);
+    NamespaceToProfiledBFSizes[YamlBFNamespace].insert(YamlBF.NumBasicBlocks);
+  }
+
+  StringMap<std::vector<BinaryFunction *>>
+      NamespaceToBFs;
----------------
maksfb wrote:

nit: formatting looks off.

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


More information about the llvm-branch-commits mailing list