================
@@ -342,6 +350,107 @@ bool YAMLProfileReader::mayHaveProfileData(const 
BinaryFunction &BF) {
   return false;
 }
 
+uint64_t YAMLProfileReader::matchWithNameSimilarity(BinaryContext &BC) {
+  uint64_t MatchedWithNameSimilarity = 0;
+  ItaniumPartialDemangler Demangler;
+
+  // 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 (Demangler.partialDemangle(DemangledName.c_str()))
+      return std::string("");
+    std::vector<char> Buffer(DemangledName.begin(), DemangledName.end());
+    size_t BufferSize = Buffer.size();
+    char *NameSpace =
+        Demangler.getFunctionDeclContextName(&Buffer[0], &BufferSize);
+    return std::string(NameSpace, BufferSize);
+  };
+
+  // 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 derivation.
+  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;
+
+  // Maps namespaces to BFs excluding binary functions with no equal sized
+  // profiled functions belonging to the same namespace.
+  for (BinaryFunction *BF : BC.getAllBinaryFunctions()) {
+    std::string DemangledName = BF->getDemangledName();
+    std::string Namespace = DeriveNameSpace(DemangledName);
+
+    auto NamespaceToProfiledBFSizesIt =
+        NamespaceToProfiledBFSizes.find(Namespace);
+    if (NamespaceToProfiledBFSizesIt == NamespaceToProfiledBFSizes.end())
+      continue;
+    if (NamespaceToProfiledBFSizesIt->second.count(BF->size()) == 0)
+      continue;
+    auto NamespaceToBFsIt = NamespaceToBFs.find(Namespace);
+    if (NamespaceToBFsIt == NamespaceToBFs.end())
+      NamespaceToBFs[Namespace] = {BF};
+    else
+      NamespaceToBFsIt->second.push_back(BF);
+  }
+
+  // Iterates through all profiled functions and binary functions belonging to
+  // the same namespace and matches based on edit distance thresehold.
+  assert(YamlBP.Functions.size() == ProfiledBFNamespaces.size() &&
+         ProfiledBFNamespaces.size() == ProfileBFDemangledNames.size());
+  for (size_t I = 0; I < YamlBP.Functions.size(); ++I) {
+    yaml::bolt::BinaryFunctionProfile &YamlBF = YamlBP.Functions[I];
+    std::string &YamlBFNamespace = ProfiledBFNamespaces[I];
+    if (YamlBF.Used)
+      continue;
+    auto It = NamespaceToBFs.find(YamlBFNamespace);
----------------
aaupov wrote:

```suggestion
    // Skip if there are no BFs in a given \p Namespace.
    auto It = NamespaceToBFs.find(YamlBFNamespace);
```

https://github.com/llvm/llvm-project/pull/95884
_______________________________________________
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to