Skip to content

Commit f0d3257

Browse files
authored
[BOLT][NFCI] Use FileSymbols for local symbol disambiguation (#89088)
Remove SymbolToFileName mapping from every local symbol to its containing FILE symbol name, and reuse FileSymbols to disambiguate local symbols instead. Also removes the check for `ld-temp.o` file symbol which was added to prevent LTO build mode from affecting the disambiguated name. This may cause incompatibility when using the profile collected on a binary built in a different mode than the input binary. Addresses #90661. Speeds up discover file objects by 5-10% for large binaries: - binary with ~1.2M symbols: 12.6422s -> 12.0297s - binary with ~4.5M symbols: 48.8851s -> 43.7315s
1 parent 5886f0a commit f0d3257

File tree

1 file changed

+8
-29
lines changed

1 file changed

+8
-29
lines changed

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -780,14 +780,6 @@ void RewriteInstance::discoverFileObjects() {
780780

781781
// For local symbols we want to keep track of associated FILE symbol name for
782782
// disambiguation by combined name.
783-
StringRef FileSymbolName;
784-
bool SeenFileName = false;
785-
struct SymbolRefHash {
786-
size_t operator()(SymbolRef const &S) const {
787-
return std::hash<decltype(DataRefImpl::p)>{}(S.getRawDataRefImpl().p);
788-
}
789-
};
790-
std::unordered_map<SymbolRef, StringRef, SymbolRefHash> SymbolToFileName;
791783
for (const ELFSymbolRef &Symbol : InputFile->symbols()) {
792784
Expected<StringRef> NameOrError = Symbol.getName();
793785
if (NameOrError && NameOrError->starts_with("__asan_init")) {
@@ -806,21 +798,8 @@ void RewriteInstance::discoverFileObjects() {
806798
if (cantFail(Symbol.getFlags()) & SymbolRef::SF_Undefined)
807799
continue;
808800

809-
if (cantFail(Symbol.getType()) == SymbolRef::ST_File) {
801+
if (cantFail(Symbol.getType()) == SymbolRef::ST_File)
810802
FileSymbols.emplace_back(Symbol);
811-
StringRef Name =
812-
cantFail(std::move(NameOrError), "cannot get symbol name for file");
813-
// Ignore Clang LTO artificial FILE symbol as it is not always generated,
814-
// and this uncertainty is causing havoc in function name matching.
815-
if (Name == "ld-temp.o")
816-
continue;
817-
FileSymbolName = Name;
818-
SeenFileName = true;
819-
continue;
820-
}
821-
if (!FileSymbolName.empty() &&
822-
!(cantFail(Symbol.getFlags()) & SymbolRef::SF_Global))
823-
SymbolToFileName[Symbol] = FileSymbolName;
824803
}
825804

826805
// Sort symbols in the file by value. Ignore symbols from non-allocatable
@@ -1028,14 +1007,14 @@ void RewriteInstance::discoverFileObjects() {
10281007
// The <id> field is used for disambiguation of local symbols since there
10291008
// could be identical function names coming from identical file names
10301009
// (e.g. from different directories).
1031-
std::string AltPrefix;
1032-
auto SFI = SymbolToFileName.find(Symbol);
1033-
if (SymbolType == SymbolRef::ST_Function && SFI != SymbolToFileName.end())
1034-
AltPrefix = Name + "/" + std::string(SFI->second);
1010+
auto SFI = llvm::upper_bound(FileSymbols, ELFSymbolRef(Symbol));
1011+
if (SymbolType == SymbolRef::ST_Function && SFI != FileSymbols.begin()) {
1012+
StringRef FileSymbolName = cantFail(SFI[-1].getName());
1013+
if (!FileSymbolName.empty())
1014+
AlternativeName = NR.uniquify(Name + "/" + FileSymbolName.str());
1015+
}
10351016

10361017
UniqueName = NR.uniquify(Name);
1037-
if (!AltPrefix.empty())
1038-
AlternativeName = NR.uniquify(AltPrefix);
10391018
}
10401019

10411020
uint64_t SymbolSize = ELFSymbolRef(Symbol).getSize();
@@ -1294,7 +1273,7 @@ void RewriteInstance::discoverFileObjects() {
12941273
FDE->getAddressRange());
12951274
}
12961275

1297-
BC->setHasSymbolsWithFileName(SeenFileName);
1276+
BC->setHasSymbolsWithFileName(FileSymbols.size());
12981277

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

0 commit comments

Comments
 (0)