11
11
// ===----------------------------------------------------------------------===//
12
12
13
13
#include " llvm/LTO/LTO.h"
14
+ #include " llvm/ADT/ArrayRef.h"
14
15
#include " llvm/ADT/ScopeExit.h"
15
16
#include " llvm/ADT/SmallSet.h"
16
17
#include " llvm/ADT/StableHashing.h"
@@ -742,18 +743,18 @@ Error LTO::add(std::unique_ptr<InputFile> Input,
742
743
Conf.VisibilityScheme = Config::ELF;
743
744
}
744
745
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))
748
748
return Err;
749
+ }
749
750
750
- assert (ResI == Res.end ());
751
+ assert (Res.empty ());
751
752
return Error::success ();
752
753
}
753
754
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 ) {
757
758
Expected<BitcodeLTOInfo> LTOInfo = Input.Mods [ModI].getLTOInfo ();
758
759
if (!LTOInfo)
759
760
return LTOInfo.takeError ();
@@ -782,28 +783,32 @@ Error LTO::addModule(InputFile &Input, unsigned ModI,
782
783
bool IsThinLTO = LTOInfo->IsThinLTO && (LTOMode != LTOK_UnifiedRegular);
783
784
784
785
auto ModSyms = Input.module_symbols (ModI);
785
- addModuleToGlobalRes (ModSyms, {ResI, ResE} ,
786
+ addModuleToGlobalRes (ModSyms, Res ,
786
787
IsThinLTO ? ThinLTO.ModuleMap .size () + 1 : 0 ,
787
788
LTOInfo->HasSummary );
788
789
789
790
if (IsThinLTO)
790
- return addThinLTO (BM, ModSyms, ResI, ResE );
791
+ return addThinLTO (BM, ModSyms, Res );
791
792
792
793
RegularLTO.EmptyCombinedModule = false ;
793
- Expected<RegularLTOState::AddedModule> ModOrErr =
794
- addRegularLTO (BM, ModSyms, ResI, ResE);
794
+ auto ModOrErr = addRegularLTO (BM, ModSyms, Res);
795
795
if (!ModOrErr)
796
796
return ModOrErr.takeError ();
797
+ Res = ModOrErr->second ;
797
798
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
+ }
800
805
801
806
// Regular LTO module summaries are added to a dummy module that represents
802
807
// the combined regular LTO module.
803
808
if (Error Err = BM.readSummary (ThinLTO.CombinedIndex , " " ))
804
809
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 ;
807
812
}
808
813
809
814
// Checks whether the given global value is in a non-prevailing comdat
@@ -839,10 +844,10 @@ handleNonPrevailingComdat(GlobalValue &GV,
839
844
// Add a regular LTO object to the link.
840
845
// The resulting module needs to be linked into the combined LTO module with
841
846
// linkRegularLTO.
842
- Expected<LTO::RegularLTOState::AddedModule>
847
+ Expected<
848
+ std::pair<LTO::RegularLTOState::AddedModule, ArrayRef<SymbolResolution>>>
843
849
LTO::addRegularLTO (BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
844
- const SymbolResolution *&ResI,
845
- const SymbolResolution *ResE) {
850
+ ArrayRef<SymbolResolution> Res) {
846
851
RegularLTOState::AddedModule Mod;
847
852
Expected<std::unique_ptr<Module>> MOrErr =
848
853
BM.getLazyModule (RegularLTO.Ctx , /* ShouldLazyLoadMetadata*/ true ,
@@ -899,22 +904,22 @@ LTO::addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
899
904
std::set<const Comdat *> NonPrevailingComdats;
900
905
SmallSet<StringRef, 2 > NonPrevailingAsmSymbols;
901
906
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 () ;
904
909
905
910
assert (MsymI != MsymE);
906
911
ModuleSymbolTable::Symbol Msym = *MsymI++;
907
912
Skip ();
908
913
909
914
if (GlobalValue *GV = dyn_cast_if_present<GlobalValue *>(Msym)) {
910
- if (Res .Prevailing ) {
915
+ if (R .Prevailing ) {
911
916
if (Sym.isUndefined ())
912
917
continue ;
913
918
Mod.Keep .push_back (GV);
914
919
// For symbols re-defined with linker -wrap and -defsym options,
915
920
// set the linkage to weak to inhibit IPO. The linkage will be
916
921
// restored by the linker.
917
- if (Res .LinkerRedefined )
922
+ if (R .LinkerRedefined )
918
923
GV->setLinkage (GlobalValue::WeakAnyLinkage);
919
924
920
925
GlobalValue::LinkageTypes OriginalLinkage = GV->getLinkage ();
@@ -938,7 +943,7 @@ LTO::addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
938
943
}
939
944
940
945
// Set the 'local' flag based on the linker resolution for this symbol.
941
- if (Res .FinalDefinitionInLinkageUnit ) {
946
+ if (R .FinalDefinitionInLinkageUnit ) {
942
947
GV->setDSOLocal (true );
943
948
if (GV->hasDLLImportStorageClass ())
944
949
GV->setDLLStorageClass (GlobalValue::DLLStorageClassTypes::
@@ -947,7 +952,7 @@ LTO::addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
947
952
} else if (auto *AS =
948
953
dyn_cast_if_present<ModuleSymbolTable::AsmSymbol *>(Msym)) {
949
954
// Collect non-prevailing symbols.
950
- if (!Res .Prevailing )
955
+ if (!R .Prevailing )
951
956
NonPrevailingAsmSymbols.insert (AS->first );
952
957
} else {
953
958
llvm_unreachable (" unknown symbol type" );
@@ -965,7 +970,7 @@ LTO::addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
965
970
CommonRes.Alignment =
966
971
std::max (Align (SymAlignValue), CommonRes.Alignment );
967
972
}
968
- CommonRes.Prevailing |= Res .Prevailing ;
973
+ CommonRes.Prevailing |= R .Prevailing ;
969
974
}
970
975
}
971
976
@@ -991,7 +996,7 @@ LTO::addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
991
996
}
992
997
993
998
assert (MsymI == MsymE);
994
- return std::move (Mod);
999
+ return std::make_pair ( std:: move (Mod), Res );
995
1000
}
996
1001
997
1002
Error LTO::linkRegularLTO (RegularLTOState::AddedModule Mod,
@@ -1032,19 +1037,19 @@ Error LTO::linkRegularLTO(RegularLTOState::AddedModule Mod,
1032
1037
}
1033
1038
1034
1039
// 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 ;
1039
1044
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 () ;
1042
1047
1043
1048
if (!Sym.getIRName ().empty ()) {
1044
1049
auto GUID = GlobalValue::getGUIDAssumingExternalLinkage (
1045
1050
GlobalValue::getGlobalIdentifier (Sym.getIRName (),
1046
1051
GlobalValue::ExternalLinkage, " " ));
1047
- if (Res .Prevailing )
1052
+ if (R .Prevailing )
1048
1053
ThinLTO.PrevailingModuleForGUID [GUID] = BM.getModuleIdentifier ();
1049
1054
}
1050
1055
}
@@ -1059,30 +1064,30 @@ Error LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
1059
1064
LLVM_DEBUG (dbgs () << " Module " << BM.getModuleIdentifier () << " \n " );
1060
1065
1061
1066
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 () ;
1064
1069
1065
1070
if (!Sym.getIRName ().empty ()) {
1066
1071
auto GUID = GlobalValue::getGUIDAssumingExternalLinkage (
1067
1072
GlobalValue::getGlobalIdentifier (Sym.getIRName (),
1068
1073
GlobalValue::ExternalLinkage, " " ));
1069
- if (Res .Prevailing ) {
1074
+ if (R .Prevailing ) {
1070
1075
assert (ThinLTO.PrevailingModuleForGUID [GUID] ==
1071
1076
BM.getModuleIdentifier ());
1072
1077
1073
1078
// For linker redefined symbols (via --wrap or --defsym) we want to
1074
1079
// switch the linkage to `weak` to prevent IPOs from happening.
1075
1080
// Find the summary in the module for this very GV and record the new
1076
1081
// linkage so that we can switch it when we import the GV.
1077
- if (Res .LinkerRedefined )
1082
+ if (R .LinkerRedefined )
1078
1083
if (auto S = ThinLTO.CombinedIndex .findSummaryInModule (
1079
1084
GUID, BM.getModuleIdentifier ()))
1080
1085
S->setLinkage (GlobalValue::WeakAnyLinkage);
1081
1086
}
1082
1087
1083
1088
// If the linker resolved the symbol to a local definition then mark it
1084
1089
// as local in the summary for the module we are adding.
1085
- if (Res .FinalDefinitionInLinkageUnit ) {
1090
+ if (R .FinalDefinitionInLinkageUnit ) {
1086
1091
if (auto S = ThinLTO.CombinedIndex .findSummaryInModule (
1087
1092
GUID, BM.getModuleIdentifier ())) {
1088
1093
S->setDSOLocal (true );
@@ -1110,7 +1115,7 @@ Error LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
1110
1115
}
1111
1116
}
1112
1117
1113
- return Error::success () ;
1118
+ return Res ;
1114
1119
}
1115
1120
1116
1121
unsigned LTO::getMaxTasks () const {
0 commit comments