Skip to content

Commit 8987eb9

Browse files
committed
[BOLT][NFC] Keep FILE symbols in a vector
In discoverFileObjects, replace mapping from every local symbol to associated file name with a vector of symbol data for FILE symbols only. This cuts down on memory needed to resolve local file names. Test Plan: NFC
1 parent 2b86fb2 commit 8987eb9

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -813,14 +813,7 @@ void RewriteInstance::discoverFileObjects() {
813813

814814
// For local symbols we want to keep track of associated FILE symbol name for
815815
// disambiguation by combined name.
816-
StringRef FileSymbolName;
817-
bool SeenFileName = false;
818-
struct SymbolRefHash {
819-
size_t operator()(SymbolRef const &S) const {
820-
return std::hash<decltype(DataRefImpl::p)>{}(S.getRawDataRefImpl().p);
821-
}
822-
};
823-
std::unordered_map<SymbolRef, StringRef, SymbolRefHash> SymbolToFileName;
816+
std::vector<DataRefImpl> FileSymbols;
824817
for (const ELFSymbolRef &Symbol : InputFile->symbols()) {
825818
Expected<StringRef> NameOrError = Symbol.getName();
826819
if (NameOrError && NameOrError->starts_with("__asan_init")) {
@@ -846,13 +839,9 @@ void RewriteInstance::discoverFileObjects() {
846839
// and this uncertainty is causing havoc in function name matching.
847840
if (Name == "ld-temp.o")
848841
continue;
849-
FileSymbolName = Name;
850-
SeenFileName = true;
842+
FileSymbols.emplace_back(Symbol.getRawDataRefImpl());
851843
continue;
852844
}
853-
if (!FileSymbolName.empty() &&
854-
!(cantFail(Symbol.getFlags()) & SymbolRef::SF_Global))
855-
SymbolToFileName[Symbol] = FileSymbolName;
856845
}
857846

858847
// Sort symbols in the file by value. Ignore symbols from non-allocatable
@@ -1027,14 +1016,18 @@ void RewriteInstance::discoverFileObjects() {
10271016
// The <id> field is used for disambiguation of local symbols since there
10281017
// could be identical function names coming from identical file names
10291018
// (e.g. from different directories).
1030-
std::string AltPrefix;
1031-
auto SFI = SymbolToFileName.find(Symbol);
1032-
if (SymbolType == SymbolRef::ST_Function && SFI != SymbolToFileName.end())
1033-
AltPrefix = Name + "/" + std::string(SFI->second);
1019+
auto CompareSymsByIdx = [](const DataRefImpl &A, const DataRefImpl &B) {
1020+
return A.d.b < B.d.b;
1021+
};
1022+
DataRefImpl SymDataRef = Symbol.getRawDataRefImpl();
1023+
auto SFI = llvm::upper_bound(FileSymbols, SymDataRef, CompareSymsByIdx);
1024+
if (SymbolType == SymbolRef::ST_Function && SFI != FileSymbols.begin()) {
1025+
SymbolRef FileSymbol(SFI[-1], InputFile);
1026+
StringRef FileName = cantFail(FileSymbol.getName());
1027+
AlternativeName = NR.uniquify(Name + "/" + FileName.str());
1028+
}
10341029

10351030
UniqueName = NR.uniquify(Name);
1036-
if (!AltPrefix.empty())
1037-
AlternativeName = NR.uniquify(AltPrefix);
10381031
}
10391032

10401033
uint64_t SymbolSize = ELFSymbolRef(Symbol).getSize();
@@ -1285,7 +1278,7 @@ void RewriteInstance::discoverFileObjects() {
12851278
FDE->getAddressRange());
12861279
}
12871280

1288-
BC->setHasSymbolsWithFileName(SeenFileName);
1281+
BC->setHasSymbolsWithFileName(!FileSymbols.empty());
12891282

12901283
// Now that all the functions were created - adjust their boundaries.
12911284
adjustFunctionBoundaries();

0 commit comments

Comments
 (0)