Skip to content

Commit 115ae08

Browse files
committed
[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. (cherry picked from commit 564c3de)
1 parent 0b5b2ad commit 115ae08

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
@@ -1035,63 +1035,59 @@ Error LTO::linkRegularLTO(RegularLTOState::AddedModule Mod,
10351035
Error LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
10361036
const SymbolResolution *&ResI,
10371037
const SymbolResolution *ResE) {
1038+
const auto BMID = BM.getModuleIdentifier();
10381039
const SymbolResolution *ResITmp = ResI;
10391040
for (const InputFile::Symbol &Sym : Syms) {
10401041
assert(ResITmp != ResE);
10411042
SymbolResolution Res = *ResITmp++;
10421043

1043-
if (!Sym.getIRName().empty()) {
1044+
if (!Sym.getIRName().empty() && Res.Prevailing) {
10441045
auto GUID = GlobalValue::getGUIDAssumingExternalLinkage(
10451046
GlobalValue::getGlobalIdentifier(Sym.getIRName(),
10461047
GlobalValue::ExternalLinkage, ""));
1047-
if (Res.Prevailing)
1048-
ThinLTO.setPrevailingModuleForGUID(GUID, BM.getModuleIdentifier());
1048+
ThinLTO.setPrevailingModuleForGUID(GUID, BMID);
10491049
}
10501050
}
10511051

1052-
if (Error Err =
1053-
BM.readSummary(ThinLTO.CombinedIndex, BM.getModuleIdentifier(),
1054-
[&](GlobalValue::GUID GUID) {
1055-
return ThinLTO.isPrevailingModuleForGUID(
1056-
GUID, BM.getModuleIdentifier());
1057-
}))
1052+
if (Error Err = BM.readSummary(
1053+
ThinLTO.CombinedIndex, BMID, [&](GlobalValue::GUID GUID) {
1054+
return ThinLTO.isPrevailingModuleForGUID(GUID, BMID);
1055+
}))
10581056
return Err;
1059-
LLVM_DEBUG(dbgs() << "Module " << BM.getModuleIdentifier() << "\n");
1057+
LLVM_DEBUG(dbgs() << "Module " << BMID << "\n");
10601058

10611059
for (const InputFile::Symbol &Sym : Syms) {
10621060
assert(ResI != ResE);
10631061
SymbolResolution Res = *ResI++;
10641062

1065-
if (!Sym.getIRName().empty()) {
1063+
if (!Sym.getIRName().empty() &&
1064+
(Res.Prevailing || Res.FinalDefinitionInLinkageUnit)) {
10661065
auto GUID = GlobalValue::getGUIDAssumingExternalLinkage(
10671066
GlobalValue::getGlobalIdentifier(Sym.getIRName(),
10681067
GlobalValue::ExternalLinkage, ""));
10691068
if (Res.Prevailing) {
1070-
assert(
1071-
ThinLTO.isPrevailingModuleForGUID(GUID, BM.getModuleIdentifier()));
1069+
assert(ThinLTO.isPrevailingModuleForGUID(GUID, BMID));
10721070

10731071
// For linker redefined symbols (via --wrap or --defsym) we want to
10741072
// switch the linkage to `weak` to prevent IPOs from happening.
10751073
// Find the summary in the module for this very GV and record the new
10761074
// linkage so that we can switch it when we import the GV.
10771075
if (Res.LinkerRedefined)
1078-
if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
1079-
GUID, BM.getModuleIdentifier()))
1076+
if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(GUID, BMID))
10801077
S->setLinkage(GlobalValue::WeakAnyLinkage);
10811078
}
10821079

10831080
// If the linker resolved the symbol to a local definition then mark it
10841081
// as local in the summary for the module we are adding.
10851082
if (Res.FinalDefinitionInLinkageUnit) {
1086-
if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
1087-
GUID, BM.getModuleIdentifier())) {
1083+
if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(GUID, BMID)) {
10881084
S->setDSOLocal(true);
10891085
}
10901086
}
10911087
}
10921088
}
10931089

1094-
if (!ThinLTO.ModuleMap.insert({BM.getModuleIdentifier(), BM}).second)
1090+
if (!ThinLTO.ModuleMap.insert({BMID, BM}).second)
10951091
return make_error<StringError>(
10961092
"Expected at most one ThinLTO module per bitcode file",
10971093
inconvertibleErrorCode());
@@ -1102,10 +1098,10 @@ Error LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
11021098
// This is a fuzzy name matching where only modules with name containing the
11031099
// specified switch values are going to be compiled.
11041100
for (const std::string &Name : Conf.ThinLTOModulesToCompile) {
1105-
if (BM.getModuleIdentifier().contains(Name)) {
1106-
ThinLTO.ModulesToCompile->insert({BM.getModuleIdentifier(), BM});
1107-
LLVM_DEBUG(dbgs() << "[ThinLTO] Selecting " << BM.getModuleIdentifier()
1108-
<< " to compile\n");
1101+
if (BMID.contains(Name)) {
1102+
ThinLTO.ModulesToCompile->insert({BMID, BM});
1103+
LLVM_DEBUG(dbgs() << "[ThinLTO] Selecting " << BMID << " to compile\n");
1104+
break;
11091105
}
11101106
}
11111107
}

0 commit comments

Comments
 (0)