[llvm] [BOLT] Unify two symbol lookup interation into the one (PR #90724)
Donghee Na via llvm-commits
llvm-commits at lists.llvm.org
Wed May 1 04:26:13 PDT 2024
https://github.com/corona10 created https://github.com/llvm/llvm-project/pull/90724
Simple refactoring which was suggested from https://github.com/llvm/llvm-project/issues/90661#issue-2272382883
>From 33d77bda3525692aaca81ae390ee56f746e94df0 Mon Sep 17 00:00:00 2001
From: Donghee Na <donghee.na92 at gmail.com>
Date: Wed, 1 May 2024 20:12:32 +0900
Subject: [PATCH] [BOLT] Unify two symbol lookup interation into the one
---
bolt/lib/Rewrite/RewriteInstance.cpp | 42 ++++++++++++++--------------
1 file changed, 21 insertions(+), 21 deletions(-)
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;
More information about the llvm-commits
mailing list