Skip to content

Commit f511445

Browse files
vitalybukakrishna2803
authored andcommitted
[LTO][NFC] Switch LTO API from output parameter to return value (llvm#151272)
1 parent d77d5f7 commit f511445

File tree

2 files changed

+55
-50
lines changed

2 files changed

+55
-50
lines changed

llvm/include/llvm/LTO/LTO.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -542,21 +542,21 @@ class LTO {
542542
ArrayRef<SymbolResolution> Res, unsigned Partition,
543543
bool InSummary);
544544

545-
// These functions take a range of symbol resolutions [ResI, ResE) and consume
546-
// the resolutions used by a single input module by incrementing ResI. After
547-
// these functions return, [ResI, ResE) will refer to the resolution range for
548-
// the remaining modules in the InputFile.
549-
Error addModule(InputFile &Input, unsigned ModI,
550-
const SymbolResolution *&ResI, const SymbolResolution *ResE);
551-
552-
Expected<RegularLTOState::AddedModule>
545+
// These functions take a range of symbol resolutions and consume the
546+
// resolutions used by a single input module. Functions return ranges refering
547+
// to the resolutions for the remaining modules in the InputFile.
548+
Expected<ArrayRef<SymbolResolution>>
549+
addModule(InputFile &Input, unsigned ModI, ArrayRef<SymbolResolution> Res);
550+
551+
Expected<std::pair<RegularLTOState::AddedModule, ArrayRef<SymbolResolution>>>
553552
addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
554-
const SymbolResolution *&ResI, const SymbolResolution *ResE);
553+
ArrayRef<SymbolResolution> Res);
555554
Error linkRegularLTO(RegularLTOState::AddedModule Mod,
556555
bool LivenessFromIndex);
557556

558-
Error addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
559-
const SymbolResolution *&ResI, const SymbolResolution *ResE);
557+
Expected<ArrayRef<SymbolResolution>>
558+
addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
559+
ArrayRef<SymbolResolution> Res);
560560

561561
Error runRegularLTO(AddStreamFn AddStream);
562562
Error runThinLTO(AddStreamFn AddStream, FileCache Cache,

llvm/lib/LTO/LTO.cpp

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "llvm/LTO/LTO.h"
14+
#include "llvm/ADT/ArrayRef.h"
1415
#include "llvm/ADT/ScopeExit.h"
1516
#include "llvm/ADT/SmallSet.h"
1617
#include "llvm/ADT/StableHashing.h"
@@ -742,18 +743,18 @@ Error LTO::add(std::unique_ptr<InputFile> Input,
742743
Conf.VisibilityScheme = Config::ELF;
743744
}
744745

745-
const SymbolResolution *ResI = Res.begin();
746-
for (unsigned I = 0; I != Input->Mods.size(); ++I)
747-
if (Error Err = addModule(*Input, I, ResI, Res.end()))
746+
for (unsigned I = 0; I != Input->Mods.size(); ++I) {
747+
if (auto Err = addModule(*Input, I, Res).moveInto(Res))
748748
return Err;
749+
}
749750

750-
assert(ResI == Res.end());
751+
assert(Res.empty());
751752
return Error::success();
752753
}
753754

754-
Error LTO::addModule(InputFile &Input, unsigned ModI,
755-
const SymbolResolution *&ResI,
756-
const SymbolResolution *ResE) {
755+
Expected<ArrayRef<SymbolResolution>>
756+
LTO::addModule(InputFile &Input, unsigned ModI,
757+
ArrayRef<SymbolResolution> Res) {
757758
Expected<BitcodeLTOInfo> LTOInfo = Input.Mods[ModI].getLTOInfo();
758759
if (!LTOInfo)
759760
return LTOInfo.takeError();
@@ -782,28 +783,32 @@ Error LTO::addModule(InputFile &Input, unsigned ModI,
782783
bool IsThinLTO = LTOInfo->IsThinLTO && (LTOMode != LTOK_UnifiedRegular);
783784

784785
auto ModSyms = Input.module_symbols(ModI);
785-
addModuleToGlobalRes(ModSyms, {ResI, ResE},
786+
addModuleToGlobalRes(ModSyms, Res,
786787
IsThinLTO ? ThinLTO.ModuleMap.size() + 1 : 0,
787788
LTOInfo->HasSummary);
788789

789790
if (IsThinLTO)
790-
return addThinLTO(BM, ModSyms, ResI, ResE);
791+
return addThinLTO(BM, ModSyms, Res);
791792

792793
RegularLTO.EmptyCombinedModule = false;
793-
Expected<RegularLTOState::AddedModule> ModOrErr =
794-
addRegularLTO(BM, ModSyms, ResI, ResE);
794+
auto ModOrErr = addRegularLTO(BM, ModSyms, Res);
795795
if (!ModOrErr)
796796
return ModOrErr.takeError();
797+
Res = ModOrErr->second;
797798

798-
if (!LTOInfo->HasSummary)
799-
return linkRegularLTO(std::move(*ModOrErr), /*LivenessFromIndex=*/false);
799+
if (!LTOInfo->HasSummary) {
800+
if (Error Err = linkRegularLTO(std::move(ModOrErr->first),
801+
/*LivenessFromIndex=*/false))
802+
return Err;
803+
return Res;
804+
}
800805

801806
// Regular LTO module summaries are added to a dummy module that represents
802807
// the combined regular LTO module.
803808
if (Error Err = BM.readSummary(ThinLTO.CombinedIndex, ""))
804809
return Err;
805-
RegularLTO.ModsWithSummaries.push_back(std::move(*ModOrErr));
806-
return Error::success();
810+
RegularLTO.ModsWithSummaries.push_back(std::move(ModOrErr->first));
811+
return Res;
807812
}
808813

809814
// Checks whether the given global value is in a non-prevailing comdat
@@ -839,10 +844,10 @@ handleNonPrevailingComdat(GlobalValue &GV,
839844
// Add a regular LTO object to the link.
840845
// The resulting module needs to be linked into the combined LTO module with
841846
// linkRegularLTO.
842-
Expected<LTO::RegularLTOState::AddedModule>
847+
Expected<
848+
std::pair<LTO::RegularLTOState::AddedModule, ArrayRef<SymbolResolution>>>
843849
LTO::addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
844-
const SymbolResolution *&ResI,
845-
const SymbolResolution *ResE) {
850+
ArrayRef<SymbolResolution> Res) {
846851
RegularLTOState::AddedModule Mod;
847852
Expected<std::unique_ptr<Module>> MOrErr =
848853
BM.getLazyModule(RegularLTO.Ctx, /*ShouldLazyLoadMetadata*/ true,
@@ -899,22 +904,22 @@ LTO::addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
899904
std::set<const Comdat *> NonPrevailingComdats;
900905
SmallSet<StringRef, 2> NonPrevailingAsmSymbols;
901906
for (const InputFile::Symbol &Sym : Syms) {
902-
assert(ResI != ResE);
903-
SymbolResolution Res = *ResI++;
907+
assert(!Res.empty());
908+
const SymbolResolution &R = Res.consume_front();
904909

905910
assert(MsymI != MsymE);
906911
ModuleSymbolTable::Symbol Msym = *MsymI++;
907912
Skip();
908913

909914
if (GlobalValue *GV = dyn_cast_if_present<GlobalValue *>(Msym)) {
910-
if (Res.Prevailing) {
915+
if (R.Prevailing) {
911916
if (Sym.isUndefined())
912917
continue;
913918
Mod.Keep.push_back(GV);
914919
// For symbols re-defined with linker -wrap and -defsym options,
915920
// set the linkage to weak to inhibit IPO. The linkage will be
916921
// restored by the linker.
917-
if (Res.LinkerRedefined)
922+
if (R.LinkerRedefined)
918923
GV->setLinkage(GlobalValue::WeakAnyLinkage);
919924

920925
GlobalValue::LinkageTypes OriginalLinkage = GV->getLinkage();
@@ -938,7 +943,7 @@ LTO::addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
938943
}
939944

940945
// Set the 'local' flag based on the linker resolution for this symbol.
941-
if (Res.FinalDefinitionInLinkageUnit) {
946+
if (R.FinalDefinitionInLinkageUnit) {
942947
GV->setDSOLocal(true);
943948
if (GV->hasDLLImportStorageClass())
944949
GV->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::
@@ -947,7 +952,7 @@ LTO::addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
947952
} else if (auto *AS =
948953
dyn_cast_if_present<ModuleSymbolTable::AsmSymbol *>(Msym)) {
949954
// Collect non-prevailing symbols.
950-
if (!Res.Prevailing)
955+
if (!R.Prevailing)
951956
NonPrevailingAsmSymbols.insert(AS->first);
952957
} else {
953958
llvm_unreachable("unknown symbol type");
@@ -965,7 +970,7 @@ LTO::addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
965970
CommonRes.Alignment =
966971
std::max(Align(SymAlignValue), CommonRes.Alignment);
967972
}
968-
CommonRes.Prevailing |= Res.Prevailing;
973+
CommonRes.Prevailing |= R.Prevailing;
969974
}
970975
}
971976

@@ -991,7 +996,7 @@ LTO::addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
991996
}
992997

993998
assert(MsymI == MsymE);
994-
return std::move(Mod);
999+
return std::make_pair(std::move(Mod), Res);
9951000
}
9961001

9971002
Error LTO::linkRegularLTO(RegularLTOState::AddedModule Mod,
@@ -1032,19 +1037,19 @@ Error LTO::linkRegularLTO(RegularLTOState::AddedModule Mod,
10321037
}
10331038

10341039
// Add a ThinLTO module to the link.
1035-
Error LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
1036-
const SymbolResolution *&ResI,
1037-
const SymbolResolution *ResE) {
1038-
const SymbolResolution *ResITmp = ResI;
1040+
Expected<ArrayRef<SymbolResolution>>
1041+
LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
1042+
ArrayRef<SymbolResolution> Res) {
1043+
ArrayRef<SymbolResolution> ResTmp = Res;
10391044
for (const InputFile::Symbol &Sym : Syms) {
1040-
assert(ResITmp != ResE);
1041-
SymbolResolution Res = *ResITmp++;
1045+
assert(!ResTmp.empty());
1046+
const SymbolResolution &R = ResTmp.consume_front();
10421047

10431048
if (!Sym.getIRName().empty()) {
10441049
auto GUID = GlobalValue::getGUIDAssumingExternalLinkage(
10451050
GlobalValue::getGlobalIdentifier(Sym.getIRName(),
10461051
GlobalValue::ExternalLinkage, ""));
1047-
if (Res.Prevailing)
1052+
if (R.Prevailing)
10481053
ThinLTO.PrevailingModuleForGUID[GUID] = BM.getModuleIdentifier();
10491054
}
10501055
}
@@ -1059,30 +1064,30 @@ Error LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
10591064
LLVM_DEBUG(dbgs() << "Module " << BM.getModuleIdentifier() << "\n");
10601065

10611066
for (const InputFile::Symbol &Sym : Syms) {
1062-
assert(ResI != ResE);
1063-
SymbolResolution Res = *ResI++;
1067+
assert(!Res.empty());
1068+
const SymbolResolution &R = Res.consume_front();
10641069

10651070
if (!Sym.getIRName().empty()) {
10661071
auto GUID = GlobalValue::getGUIDAssumingExternalLinkage(
10671072
GlobalValue::getGlobalIdentifier(Sym.getIRName(),
10681073
GlobalValue::ExternalLinkage, ""));
1069-
if (Res.Prevailing) {
1074+
if (R.Prevailing) {
10701075
assert(ThinLTO.PrevailingModuleForGUID[GUID] ==
10711076
BM.getModuleIdentifier());
10721077

10731078
// For linker redefined symbols (via --wrap or --defsym) we want to
10741079
// switch the linkage to `weak` to prevent IPOs from happening.
10751080
// Find the summary in the module for this very GV and record the new
10761081
// linkage so that we can switch it when we import the GV.
1077-
if (Res.LinkerRedefined)
1082+
if (R.LinkerRedefined)
10781083
if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
10791084
GUID, BM.getModuleIdentifier()))
10801085
S->setLinkage(GlobalValue::WeakAnyLinkage);
10811086
}
10821087

10831088
// If the linker resolved the symbol to a local definition then mark it
10841089
// as local in the summary for the module we are adding.
1085-
if (Res.FinalDefinitionInLinkageUnit) {
1090+
if (R.FinalDefinitionInLinkageUnit) {
10861091
if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
10871092
GUID, BM.getModuleIdentifier())) {
10881093
S->setDSOLocal(true);
@@ -1110,7 +1115,7 @@ Error LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
11101115
}
11111116
}
11121117

1113-
return Error::success();
1118+
return Res;
11141119
}
11151120

11161121
unsigned LTO::getMaxTasks() const {

0 commit comments

Comments
 (0)