[llvm] [BOLT][NFC] Store FILE symbols in a vector (PR #89088)

Amir Ayupov via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 17 08:24:29 PDT 2024


https://github.com/aaupov created https://github.com/llvm/llvm-project/pull/89088

Replace a map from local symbol to associated file name with a vector of
symbol index + StringRef for FILE symbols only. This cuts down on memory
needed to resolve local file name.

Test Plan: NFC

>From c5d5b9060e95712870bbdaf8c7f80aa3e0f40d22 Mon Sep 17 00:00:00 2001
From: Amir Ayupov <aaupov at fb.com>
Date: Wed, 17 Apr 2024 16:14:46 +0200
Subject: [PATCH] [BOLT][NFC] Store FILE symbols in a vector

Replace a map from local symbol to associated file name with a vector of
symbol index + StringRef for FILE symbols only. This cuts down on memory
needed to resolve local file name.

Test Plan: NFC
---
 bolt/lib/Rewrite/RewriteInstance.cpp | 29 +++++++++++-----------------
 1 file changed, 11 insertions(+), 18 deletions(-)

diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index 0c8ee0d417233b..ba86f1f5226674 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -813,14 +813,7 @@ void RewriteInstance::discoverFileObjects() {
 
   // For local symbols we want to keep track of associated FILE symbol name for
   // disambiguation by combined name.
-  StringRef FileSymbolName;
-  bool SeenFileName = false;
-  struct SymbolRefHash {
-    size_t operator()(SymbolRef const &S) const {
-      return std::hash<decltype(DataRefImpl::p)>{}(S.getRawDataRefImpl().p);
-    }
-  };
-  std::unordered_map<SymbolRef, StringRef, SymbolRefHash> SymbolToFileName;
+  std::vector<std::pair<uint32_t, StringRef>> FileSymbols;
   for (const ELFSymbolRef &Symbol : InputFile->symbols()) {
     Expected<StringRef> NameOrError = Symbol.getName();
     if (NameOrError && NameOrError->starts_with("__asan_init")) {
@@ -846,13 +839,9 @@ void RewriteInstance::discoverFileObjects() {
       // and this uncertainty is causing havoc in function name matching.
       if (Name == "ld-temp.o")
         continue;
-      FileSymbolName = Name;
-      SeenFileName = true;
-      continue;
+      uint32_t SymIdx = Symbol.getRawDataRefImpl().d.b;
+      FileSymbols.emplace_back(SymIdx, Name);
     }
-    if (!FileSymbolName.empty() &&
-        !(cantFail(Symbol.getFlags()) & SymbolRef::SF_Global))
-      SymbolToFileName[Symbol] = FileSymbolName;
   }
 
   // Sort symbols in the file by value. Ignore symbols from non-allocatable
@@ -1028,9 +1017,13 @@ void RewriteInstance::discoverFileObjects() {
       // could be identical function names coming from identical file names
       // (e.g. from different directories).
       std::string AltPrefix;
-      auto SFI = SymbolToFileName.find(Symbol);
-      if (SymbolType == SymbolRef::ST_Function && SFI != SymbolToFileName.end())
-        AltPrefix = Name + "/" + std::string(SFI->second);
+      if (SymbolType == SymbolRef::ST_Function) {
+        uint32_t SymIdx = Symbol.getRawDataRefImpl().d.b;
+        auto It =
+            llvm::upper_bound(FileSymbols, std::make_pair(SymIdx, StringRef()));
+        if (It != FileSymbols.begin())
+          AltPrefix = Name + "/" + It[-1].second.str();
+      }
 
       UniqueName = NR.uniquify(Name);
       if (!AltPrefix.empty())
@@ -1285,7 +1278,7 @@ void RewriteInstance::discoverFileObjects() {
                              FDE->getAddressRange());
   }
 
-  BC->setHasSymbolsWithFileName(SeenFileName);
+  BC->setHasSymbolsWithFileName(!FileSymbols.empty());
 
   // Now that all the functions were created - adjust their boundaries.
   adjustFunctionBoundaries();



More information about the llvm-commits mailing list