[llvm] [BOLT] Unify two symbol lookup interation into the one (PR #90724)

via llvm-commits llvm-commits at lists.llvm.org
Wed May 1 04:26:41 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-bolt

Author: Donghee Na (corona10)

<details>
<summary>Changes</summary>

Simple refactoring which was suggested from https://github.com/llvm/llvm-project/issues/90661#issue-2272382883

---
Full diff: https://github.com/llvm/llvm-project/pull/90724.diff


1 Files Affected:

- (modified) bolt/lib/Rewrite/RewriteInstance.cpp (+21-21) 


``````````diff
diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index 23f79e3c135a78..2e91856c0ebfe6 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -820,7 +820,26 @@ void RewriteInstance::discoverFileObjects() {
       return std::hash<decltype(DataRefImpl::p)>{}(S.getRawDataRefImpl().p);
     }
   };
+
+  // Sort symbols in the file by value. Ignore symbols from non-allocatable
+  // sections. We memoize getAddress(), as it has rather high overhead.
+  struct SymbolInfo {
+    uint64_t Address;
+    SymbolRef Symbol;
+  };
+  auto isSymbolInMemory = [this](const SymbolRef &Sym) {
+    if (cantFail(Sym.getType()) == SymbolRef::ST_File)
+      return false;
+    if (cantFail(Sym.getFlags()) & SymbolRef::SF_Absolute)
+      return true;
+    if (cantFail(Sym.getFlags()) & SymbolRef::SF_Undefined)
+      return false;
+    BinarySection Section(*BC, *cantFail(Sym.getSection()));
+    return Section.isAllocatable();
+  };
+  std::vector<SymbolInfo> SortedSymbols;
   std::unordered_map<SymbolRef, StringRef, SymbolRefHash> SymbolToFileName;
+
   for (const ELFSymbolRef &Symbol : InputFile->symbols()) {
     Expected<StringRef> NameOrError = Symbol.getName();
     if (NameOrError && NameOrError->starts_with("__asan_init")) {
@@ -835,6 +854,8 @@ void RewriteInstance::discoverFileObjects() {
              "support. Cannot optimize.\n";
       exit(1);
     }
+    if (isSymbolInMemory(Symbol))
+      SortedSymbols.push_back({cantFail(Symbol.getAddress()), Symbol});
 
     if (cantFail(Symbol.getFlags()) & SymbolRef::SF_Undefined)
       continue;
@@ -856,27 +877,6 @@ void RewriteInstance::discoverFileObjects() {
       SymbolToFileName[Symbol] = FileSymbolName;
   }
 
-  // Sort symbols in the file by value. Ignore symbols from non-allocatable
-  // sections. We memoize getAddress(), as it has rather high overhead.
-  struct SymbolInfo {
-    uint64_t Address;
-    SymbolRef Symbol;
-  };
-  std::vector<SymbolInfo> SortedSymbols;
-  auto isSymbolInMemory = [this](const SymbolRef &Sym) {
-    if (cantFail(Sym.getType()) == SymbolRef::ST_File)
-      return false;
-    if (cantFail(Sym.getFlags()) & SymbolRef::SF_Absolute)
-      return true;
-    if (cantFail(Sym.getFlags()) & SymbolRef::SF_Undefined)
-      return false;
-    BinarySection Section(*BC, *cantFail(Sym.getSection()));
-    return Section.isAllocatable();
-  };
-  for (const SymbolRef &Symbol : InputFile->symbols())
-    if (isSymbolInMemory(Symbol))
-      SortedSymbols.push_back({cantFail(Symbol.getAddress()), Symbol});
-
   auto CompareSymbols = [this](const SymbolInfo &A, const SymbolInfo &B) {
     if (A.Address != B.Address)
       return A.Address < B.Address;

``````````

</details>


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


More information about the llvm-commits mailing list