Skip to content

Commit 564c3de

Browse files
authored
[ThinLTO][NFC] Improve performance of addThinLTO (#166067)
Avoid the construction of `GUID` when not required. This improves the performance of a LLD `--thinlto-index-only` link of `clang` by ~4-5% on both Windows and Linux.
1 parent 0013b5f commit 564c3de

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

llvm/lib/LTO/LTO.cpp

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,63 +1076,59 @@ Expected<ArrayRef<SymbolResolution>>
10761076
LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
10771077
ArrayRef<SymbolResolution> Res) {
10781078
llvm::TimeTraceScope timeScope("LTO add thin LTO");
1079+
const auto BMID = BM.getModuleIdentifier();
10791080
ArrayRef<SymbolResolution> ResTmp = Res;
10801081
for (const InputFile::Symbol &Sym : Syms) {
10811082
assert(!ResTmp.empty());
10821083
const SymbolResolution &R = ResTmp.consume_front();
10831084

1084-
if (!Sym.getIRName().empty()) {
1085+
if (!Sym.getIRName().empty() && R.Prevailing) {
10851086
auto GUID = GlobalValue::getGUIDAssumingExternalLinkage(
10861087
GlobalValue::getGlobalIdentifier(Sym.getIRName(),
10871088
GlobalValue::ExternalLinkage, ""));
1088-
if (R.Prevailing)
1089-
ThinLTO.setPrevailingModuleForGUID(GUID, BM.getModuleIdentifier());
1089+
ThinLTO.setPrevailingModuleForGUID(GUID, BMID);
10901090
}
10911091
}
10921092

1093-
if (Error Err =
1094-
BM.readSummary(ThinLTO.CombinedIndex, BM.getModuleIdentifier(),
1095-
[&](GlobalValue::GUID GUID) {
1096-
return ThinLTO.isPrevailingModuleForGUID(
1097-
GUID, BM.getModuleIdentifier());
1098-
}))
1093+
if (Error Err = BM.readSummary(
1094+
ThinLTO.CombinedIndex, BMID, [&](GlobalValue::GUID GUID) {
1095+
return ThinLTO.isPrevailingModuleForGUID(GUID, BMID);
1096+
}))
10991097
return Err;
1100-
LLVM_DEBUG(dbgs() << "Module " << BM.getModuleIdentifier() << "\n");
1098+
LLVM_DEBUG(dbgs() << "Module " << BMID << "\n");
11011099

11021100
for (const InputFile::Symbol &Sym : Syms) {
11031101
assert(!Res.empty());
11041102
const SymbolResolution &R = Res.consume_front();
11051103

1106-
if (!Sym.getIRName().empty()) {
1104+
if (!Sym.getIRName().empty() &&
1105+
(R.Prevailing || R.FinalDefinitionInLinkageUnit)) {
11071106
auto GUID = GlobalValue::getGUIDAssumingExternalLinkage(
11081107
GlobalValue::getGlobalIdentifier(Sym.getIRName(),
11091108
GlobalValue::ExternalLinkage, ""));
11101109
if (R.Prevailing) {
1111-
assert(
1112-
ThinLTO.isPrevailingModuleForGUID(GUID, BM.getModuleIdentifier()));
1110+
assert(ThinLTO.isPrevailingModuleForGUID(GUID, BMID));
11131111

11141112
// For linker redefined symbols (via --wrap or --defsym) we want to
11151113
// switch the linkage to `weak` to prevent IPOs from happening.
11161114
// Find the summary in the module for this very GV and record the new
11171115
// linkage so that we can switch it when we import the GV.
11181116
if (R.LinkerRedefined)
1119-
if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
1120-
GUID, BM.getModuleIdentifier()))
1117+
if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(GUID, BMID))
11211118
S->setLinkage(GlobalValue::WeakAnyLinkage);
11221119
}
11231120

11241121
// If the linker resolved the symbol to a local definition then mark it
11251122
// as local in the summary for the module we are adding.
11261123
if (R.FinalDefinitionInLinkageUnit) {
1127-
if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
1128-
GUID, BM.getModuleIdentifier())) {
1124+
if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(GUID, BMID)) {
11291125
S->setDSOLocal(true);
11301126
}
11311127
}
11321128
}
11331129
}
11341130

1135-
if (!ThinLTO.ModuleMap.insert({BM.getModuleIdentifier(), BM}).second)
1131+
if (!ThinLTO.ModuleMap.insert({BMID, BM}).second)
11361132
return make_error<StringError>(
11371133
"Expected at most one ThinLTO module per bitcode file",
11381134
inconvertibleErrorCode());
@@ -1143,10 +1139,10 @@ LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
11431139
// This is a fuzzy name matching where only modules with name containing the
11441140
// specified switch values are going to be compiled.
11451141
for (const std::string &Name : Conf.ThinLTOModulesToCompile) {
1146-
if (BM.getModuleIdentifier().contains(Name)) {
1147-
ThinLTO.ModulesToCompile->insert({BM.getModuleIdentifier(), BM});
1148-
LLVM_DEBUG(dbgs() << "[ThinLTO] Selecting " << BM.getModuleIdentifier()
1149-
<< " to compile\n");
1142+
if (BMID.contains(Name)) {
1143+
ThinLTO.ModulesToCompile->insert({BMID, BM});
1144+
LLVM_DEBUG(dbgs() << "[ThinLTO] Selecting " << BMID << " to compile\n");
1145+
break;
11501146
}
11511147
}
11521148
}

0 commit comments

Comments
 (0)