[llvm] [BOLT][NFC] Define getCommonName (PR #106243)

Amir Ayupov via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 28 19:54:10 PDT 2024


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

>From 1f2e9a33d80616e7acdc68f1956a37f82abecd9e Mon Sep 17 00:00:00 2001
From: Amir Ayupov <aaupov at fb.com>
Date: Tue, 27 Aug 2024 09:28:19 -0700
Subject: [PATCH 1/2] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?=
 =?UTF-8?q?itial=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.4
---
 bolt/include/bolt/Utils/Utils.h | 6 +++++-
 bolt/lib/Utils/Utils.cpp        | 7 ++++---
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/bolt/include/bolt/Utils/Utils.h b/bolt/include/bolt/Utils/Utils.h
index 3886c5f8757c08..63de3c5ee8f123 100644
--- a/bolt/include/bolt/Utils/Utils.h
+++ b/bolt/include/bolt/Utils/Utils.h
@@ -64,8 +64,12 @@ std::string getUnescapedName(const StringRef &Name);
 /// of functions. Later, out of all matching profiles we pick the one with the
 /// best match.
 ///
+static SmallVector<StringRef, 4> LTOSuffixes({".__uniq.", ".lto_priv.",
+                                              ".constprop.", ".llvm."});
 /// Return a common part of LTO name for a given \p Name.
-std::optional<StringRef> getLTOCommonName(const StringRef Name);
+std::optional<StringRef>
+getLTOCommonName(const StringRef Name,
+                 ArrayRef<StringRef> Suffixes = LTOSuffixes);
 
 // Determines which register a given DWARF expression is being assigned to.
 // If the expression is defining the CFA, return std::nullopt.
diff --git a/bolt/lib/Utils/Utils.cpp b/bolt/lib/Utils/Utils.cpp
index 718e97535fd22a..a4b6a6a317e2cb 100644
--- a/bolt/lib/Utils/Utils.cpp
+++ b/bolt/lib/Utils/Utils.cpp
@@ -66,11 +66,12 @@ std::string getUnescapedName(const StringRef &Name) {
   return Output;
 }
 
-std::optional<StringRef> getLTOCommonName(const StringRef Name) {
-  for (StringRef Suffix : {".__uniq.", ".lto_priv.", ".constprop.", ".llvm."}) {
+std::optional<StringRef> getLTOCommonName(const StringRef Name,
+                                          ArrayRef<StringRef> Suffixes) {
+  for (StringRef Suffix : Suffixes) {
     size_t LTOSuffixPos = Name.find(Suffix);
     if (LTOSuffixPos != StringRef::npos)
-      return Name.substr(0, LTOSuffixPos + Suffix.size());
+      return Name.substr(0, LTOSuffixPos);
   }
   return std::nullopt;
 }

>From 6eff5fe988ce505903a3836e86f274c4a1616fb3 Mon Sep 17 00:00:00 2001
From: Amir Ayupov <aaupov at fb.com>
Date: Tue, 27 Aug 2024 15:35:35 -0700
Subject: [PATCH 2/2] define getCommonName

Created using spr 1.3.4
---
 bolt/include/bolt/Utils/Utils.h          | 11 ++++++-----
 bolt/lib/Rewrite/PseudoProbeRewriter.cpp | 11 +++++++++--
 bolt/lib/Utils/Utils.cpp                 | 11 ++++++++---
 3 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/bolt/include/bolt/Utils/Utils.h b/bolt/include/bolt/Utils/Utils.h
index 63de3c5ee8f123..9baee7d94066de 100644
--- a/bolt/include/bolt/Utils/Utils.h
+++ b/bolt/include/bolt/Utils/Utils.h
@@ -41,6 +41,11 @@ std::string getEscapedName(const StringRef &Name);
 /// Return the unescaped name
 std::string getUnescapedName(const StringRef &Name);
 
+/// Return a common part for a given \p Name wrt a given \p Suffixes list.
+/// Preserve the suffix if \p KeepSuffix is set, only dropping characters
+/// following it, otherwise drop the suffix as well.
+std::optional<StringRef> getCommonName(const StringRef Name, bool KeepSuffix,
+                                       ArrayRef<StringRef> Suffixes);
 /// LTO-generated function names take a form:
 ///
 ///   <function_name>.lto_priv.<decimal_number>/...
@@ -64,12 +69,8 @@ std::string getUnescapedName(const StringRef &Name);
 /// of functions. Later, out of all matching profiles we pick the one with the
 /// best match.
 ///
-static SmallVector<StringRef, 4> LTOSuffixes({".__uniq.", ".lto_priv.",
-                                              ".constprop.", ".llvm."});
 /// Return a common part of LTO name for a given \p Name.
-std::optional<StringRef>
-getLTOCommonName(const StringRef Name,
-                 ArrayRef<StringRef> Suffixes = LTOSuffixes);
+std::optional<StringRef> getLTOCommonName(const StringRef Name);
 
 // Determines which register a given DWARF expression is being assigned to.
 // If the expression is defining the CFA, return std::nullopt.
diff --git a/bolt/lib/Rewrite/PseudoProbeRewriter.cpp b/bolt/lib/Rewrite/PseudoProbeRewriter.cpp
index 4925b4b385d9b1..6e80d9b0014b7b 100644
--- a/bolt/lib/Rewrite/PseudoProbeRewriter.cpp
+++ b/bolt/lib/Rewrite/PseudoProbeRewriter.cpp
@@ -14,6 +14,7 @@
 #include "bolt/Rewrite/MetadataRewriter.h"
 #include "bolt/Rewrite/MetadataRewriters.h"
 #include "bolt/Utils/CommandLineOpts.h"
+#include "bolt/Utils/Utils.h"
 #include "llvm/IR/Function.h"
 #include "llvm/MC/MCPseudoProbe.h"
 #include "llvm/Support/CommandLine.h"
@@ -133,10 +134,16 @@ void PseudoProbeRewriter::parsePseudoProbe() {
 
   MCPseudoProbeDecoder::Uint64Set GuidFilter;
   MCPseudoProbeDecoder::Uint64Map FuncStartAddrs;
+  SmallVector<StringRef, 3> Suffixes({".llvm.", ".destroy", ".resume"});
   for (const BinaryFunction *F : BC.getAllBinaryFunctions()) {
     for (const MCSymbol *Sym : F->getSymbols()) {
-      FuncStartAddrs[Function::getGUID(NameResolver::restore(Sym->getName()))] =
-          F->getAddress();
+      StringRef SymName = NameResolver::restore(Sym->getName());
+      if (std::optional<StringRef> CommonName =
+              getCommonName(SymName, false, Suffixes)) {
+        SymName = *CommonName;
+      }
+      uint64_t GUID = Function::getGUID(SymName);
+      FuncStartAddrs[GUID] = F->getAddress();
     }
   }
   Contents = PseudoProbeSection->getContents();
diff --git a/bolt/lib/Utils/Utils.cpp b/bolt/lib/Utils/Utils.cpp
index a4b6a6a317e2cb..ecc2f1010a9858 100644
--- a/bolt/lib/Utils/Utils.cpp
+++ b/bolt/lib/Utils/Utils.cpp
@@ -66,16 +66,21 @@ std::string getUnescapedName(const StringRef &Name) {
   return Output;
 }
 
-std::optional<StringRef> getLTOCommonName(const StringRef Name,
-                                          ArrayRef<StringRef> Suffixes) {
+std::optional<StringRef> getCommonName(const StringRef Name, bool KeepSuffix,
+                                       ArrayRef<StringRef> Suffixes) {
   for (StringRef Suffix : Suffixes) {
     size_t LTOSuffixPos = Name.find(Suffix);
     if (LTOSuffixPos != StringRef::npos)
-      return Name.substr(0, LTOSuffixPos);
+      return Name.substr(0, LTOSuffixPos + (KeepSuffix ? Suffix.size() : 0));
   }
   return std::nullopt;
 }
 
+std::optional<StringRef> getLTOCommonName(const StringRef Name) {
+  return getCommonName(Name, true,
+                       {".__uniq.", ".lto_priv.", ".constprop.", ".llvm."});
+}
+
 std::optional<uint8_t> readDWARFExpressionTargetReg(StringRef ExprBytes) {
   uint8_t Opcode = ExprBytes[0];
   if (Opcode == dwarf::DW_CFA_def_cfa_expression)



More information about the llvm-commits mailing list