@@ -135,14 +135,13 @@ static void computePrevailingCopies(
135
135
}
136
136
}
137
137
138
- static StringMap<MemoryBufferRef>
139
- generateModuleMap (const std::vector<ThinLTOBuffer> &Modules) {
140
- StringMap<MemoryBufferRef> ModuleMap;
141
- for (auto &ModuleBuffer : Modules) {
142
- assert (ModuleMap.find (ModuleBuffer.getBufferIdentifier ()) ==
143
- ModuleMap.end () &&
138
+ static StringMap<lto::InputFile *>
139
+ generateModuleMap (std::vector<std::unique_ptr<lto::InputFile>> &Modules) {
140
+ StringMap<lto::InputFile *> ModuleMap;
141
+ for (auto &M : Modules) {
142
+ assert (ModuleMap.find (M->getName ()) == ModuleMap.end () &&
144
143
" Expect unique Buffer Identifier" );
145
- ModuleMap[ModuleBuffer. getBufferIdentifier ()] = ModuleBuffer. getMemBuffer ();
144
+ ModuleMap[M-> getName ()] = M. get ();
146
145
}
147
146
return ModuleMap;
148
147
}
@@ -175,35 +174,37 @@ static void verifyLoadedModule(Module &TheModule) {
175
174
}
176
175
}
177
176
178
- static std::unique_ptr<Module>
179
- loadModuleFromBuffer (const MemoryBufferRef &Buffer, LLVMContext &Context,
180
- bool Lazy, bool IsImporting) {
177
+ static std::unique_ptr<Module> loadModuleFromInput (lto::InputFile *Input,
178
+ LLVMContext &Context,
179
+ bool Lazy,
180
+ bool IsImporting) {
181
+ auto &Mod = Input->getSingleBitcodeModule ();
181
182
SMDiagnostic Err;
182
183
Expected<std::unique_ptr<Module>> ModuleOrErr =
183
- Lazy
184
- ? getLazyBitcodeModule (Buffer, Context,
185
- /* ShouldLazyLoadMetadata */ true , IsImporting)
186
- : parseBitcodeFile (Buffer, Context);
184
+ Lazy ? Mod.getLazyModule (Context,
185
+ /* ShouldLazyLoadMetadata */ true , IsImporting)
186
+ : Mod.parseModule (Context);
187
187
if (!ModuleOrErr) {
188
188
handleAllErrors (ModuleOrErr.takeError (), [&](ErrorInfoBase &EIB) {
189
- SMDiagnostic Err = SMDiagnostic (Buffer. getBufferIdentifier (),
189
+ SMDiagnostic Err = SMDiagnostic (Mod. getModuleIdentifier (),
190
190
SourceMgr::DK_Error, EIB.message ());
191
191
Err.print (" ThinLTO" , errs ());
192
192
});
193
193
report_fatal_error (" Can't load module, abort." );
194
194
}
195
195
if (!Lazy)
196
196
verifyLoadedModule (*ModuleOrErr.get ());
197
- return std::move (ModuleOrErr. get () );
197
+ return std::move (* ModuleOrErr);
198
198
}
199
199
200
200
static void
201
201
crossImportIntoModule (Module &TheModule, const ModuleSummaryIndex &Index,
202
- StringMap<MemoryBufferRef > &ModuleMap,
202
+ StringMap<lto::InputFile* > &ModuleMap,
203
203
const FunctionImporter::ImportMapTy &ImportList) {
204
204
auto Loader = [&](StringRef Identifier) {
205
- return loadModuleFromBuffer (ModuleMap[Identifier], TheModule.getContext (),
206
- /* Lazy=*/ true , /* IsImporting*/ true );
205
+ auto &Input = ModuleMap[Identifier];
206
+ return loadModuleFromInput (Input, TheModule.getContext (),
207
+ /* Lazy=*/ true , /* IsImporting*/ true );
207
208
};
208
209
209
210
FunctionImporter Importer (Index, Loader);
@@ -248,6 +249,15 @@ static void optimizeModule(Module &TheModule, TargetMachine &TM,
248
249
PM.run (TheModule);
249
250
}
250
251
252
+ static void
253
+ addUsedSymbolToPreservedGUID (const lto::InputFile &File,
254
+ DenseSet<GlobalValue::GUID> &PreservedGUID) {
255
+ for (const auto &Sym : File.symbols ()) {
256
+ if (Sym.isUsed ())
257
+ PreservedGUID.insert (GlobalValue::getGUID (Sym.getIRName ()));
258
+ }
259
+ }
260
+
251
261
// Convert the PreservedSymbols map from "Name" based to "GUID" based.
252
262
static DenseSet<GlobalValue::GUID>
253
263
computeGUIDPreservedSymbols (const StringSet<> &PreservedSymbols,
@@ -381,7 +391,7 @@ class ModuleCacheEntry {
381
391
382
392
static std::unique_ptr<MemoryBuffer>
383
393
ProcessThinLTOModule (Module &TheModule, ModuleSummaryIndex &Index,
384
- StringMap<MemoryBufferRef > &ModuleMap, TargetMachine &TM,
394
+ StringMap<lto::InputFile * > &ModuleMap, TargetMachine &TM,
385
395
const FunctionImporter::ImportMapTy &ImportList,
386
396
const FunctionImporter::ExportSetTy &ExportList,
387
397
const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
@@ -488,15 +498,13 @@ static void initTMBuilder(TargetMachineBuilder &TMBuilder,
488
498
} // end anonymous namespace
489
499
490
500
void ThinLTOCodeGenerator::addModule (StringRef Identifier, StringRef Data) {
491
- ThinLTOBuffer Buffer (Data, Identifier);
492
- LLVMContext Context;
493
- StringRef TripleStr;
494
- ErrorOr<std::string> TripleOrErr = expectedToErrorOrAndEmitErrors (
495
- Context, getBitcodeTargetTriple (Buffer.getMemBuffer ()));
501
+ MemoryBufferRef Buffer (Data, Identifier);
496
502
497
- if (TripleOrErr)
498
- TripleStr = *TripleOrErr;
503
+ auto InputOrError = lto::InputFile::create (Buffer);
504
+ if (!InputOrError)
505
+ report_fatal_error (" ThinLTO cannot create input file" );
499
506
507
+ auto TripleStr = (*InputOrError)->getTargetTriple ();
500
508
Triple TheTriple (TripleStr);
501
509
502
510
if (Modules.empty ())
@@ -508,7 +516,7 @@ void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
508
516
initTMBuilder (TMBuilder, Triple (TMBuilder.TheTriple .merge (TheTriple)));
509
517
}
510
518
511
- Modules.push_back (Buffer );
519
+ Modules.emplace_back ( std::move (*InputOrError) );
512
520
}
513
521
514
522
void ThinLTOCodeGenerator::preserveSymbol (StringRef Name) {
@@ -549,9 +557,10 @@ std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
549
557
std::unique_ptr<ModuleSummaryIndex> CombinedIndex =
550
558
llvm::make_unique<ModuleSummaryIndex>(/* HaveGVs=*/ false );
551
559
uint64_t NextModuleId = 0 ;
552
- for (auto &ModuleBuffer : Modules) {
553
- if (Error Err = readModuleSummaryIndex (ModuleBuffer.getMemBuffer (),
554
- *CombinedIndex, NextModuleId++)) {
560
+ for (auto &Mod : Modules) {
561
+ auto &M = Mod->getSingleBitcodeModule ();
562
+ if (Error Err =
563
+ M.readSummary (*CombinedIndex, Mod->getName (), NextModuleId++)) {
555
564
// FIXME diagnose
556
565
logAllUnhandledErrors (
557
566
std::move (Err), errs (),
@@ -593,8 +602,8 @@ static void computeDeadSymbolsInIndex(
593
602
* Perform promotion and renaming of exported internal functions.
594
603
* Index is updated to reflect linkage changes from weak resolution.
595
604
*/
596
- void ThinLTOCodeGenerator::promote (Module &TheModule,
597
- ModuleSummaryIndex &Index ) {
605
+ void ThinLTOCodeGenerator::promote (Module &TheModule, ModuleSummaryIndex &Index,
606
+ const lto::InputFile &File ) {
598
607
auto ModuleCount = Index.modulePaths ().size ();
599
608
auto ModuleIdentifier = TheModule.getModuleIdentifier ();
600
609
@@ -606,6 +615,9 @@ void ThinLTOCodeGenerator::promote(Module &TheModule,
606
615
auto GUIDPreservedSymbols = computeGUIDPreservedSymbols (
607
616
PreservedSymbols, Triple (TheModule.getTargetTriple ()));
608
617
618
+ // Add used symbol to the preserved symbols.
619
+ addUsedSymbolToPreservedGUID (File, GUIDPreservedSymbols);
620
+
609
621
// Compute "dead" symbols, we don't want to import/export these!
610
622
computeDeadSymbolsInIndex (Index, GUIDPreservedSymbols);
611
623
@@ -619,21 +631,22 @@ void ThinLTOCodeGenerator::promote(Module &TheModule,
619
631
StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
620
632
resolvePrevailingInIndex (Index, ResolvedODR);
621
633
622
- thinLTOResolvePrevailingInModule (
623
- TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
624
-
625
634
// Promote the exported values in the index, so that they are promoted
626
635
// in the module.
627
636
internalizeAndPromoteInIndex (ExportLists, GUIDPreservedSymbols, Index);
628
637
629
638
promoteModule (TheModule, Index);
639
+
640
+ thinLTOResolvePrevailingInModule (
641
+ TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
630
642
}
631
643
632
644
/* *
633
645
* Perform cross-module importing for the module identified by ModuleIdentifier.
634
646
*/
635
647
void ThinLTOCodeGenerator::crossModuleImport (Module &TheModule,
636
- ModuleSummaryIndex &Index) {
648
+ ModuleSummaryIndex &Index,
649
+ const lto::InputFile &File) {
637
650
auto ModuleMap = generateModuleMap (Modules);
638
651
auto ModuleCount = Index.modulePaths ().size ();
639
652
@@ -645,6 +658,8 @@ void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
645
658
auto GUIDPreservedSymbols = computeGUIDPreservedSymbols (
646
659
PreservedSymbols, Triple (TheModule.getTargetTriple ()));
647
660
661
+ addUsedSymbolToPreservedGUID (File, GUIDPreservedSymbols);
662
+
648
663
// Compute "dead" symbols, we don't want to import/export these!
649
664
computeDeadSymbolsInIndex (Index, GUIDPreservedSymbols);
650
665
@@ -663,7 +678,8 @@ void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
663
678
*/
664
679
void ThinLTOCodeGenerator::gatherImportedSummariesForModule (
665
680
Module &TheModule, ModuleSummaryIndex &Index,
666
- std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
681
+ std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex,
682
+ const lto::InputFile &File) {
667
683
auto ModuleCount = Index.modulePaths ().size ();
668
684
auto ModuleIdentifier = TheModule.getModuleIdentifier ();
669
685
@@ -675,6 +691,8 @@ void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
675
691
auto GUIDPreservedSymbols = computeGUIDPreservedSymbols (
676
692
PreservedSymbols, Triple (TheModule.getTargetTriple ()));
677
693
694
+ addUsedSymbolToPreservedGUID (File, GUIDPreservedSymbols);
695
+
678
696
// Compute "dead" symbols, we don't want to import/export these!
679
697
computeDeadSymbolsInIndex (Index, GUIDPreservedSymbols);
680
698
@@ -693,7 +711,8 @@ void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
693
711
* Emit the list of files needed for importing into module.
694
712
*/
695
713
void ThinLTOCodeGenerator::emitImports (Module &TheModule, StringRef OutputName,
696
- ModuleSummaryIndex &Index) {
714
+ ModuleSummaryIndex &Index,
715
+ const lto::InputFile &File) {
697
716
auto ModuleCount = Index.modulePaths ().size ();
698
717
auto ModuleIdentifier = TheModule.getModuleIdentifier ();
699
718
@@ -705,6 +724,8 @@ void ThinLTOCodeGenerator::emitImports(Module &TheModule, StringRef OutputName,
705
724
auto GUIDPreservedSymbols = computeGUIDPreservedSymbols (
706
725
PreservedSymbols, Triple (TheModule.getTargetTriple ()));
707
726
727
+ addUsedSymbolToPreservedGUID (File, GUIDPreservedSymbols);
728
+
708
729
// Compute "dead" symbols, we don't want to import/export these!
709
730
computeDeadSymbolsInIndex (Index, GUIDPreservedSymbols);
710
731
@@ -730,7 +751,8 @@ void ThinLTOCodeGenerator::emitImports(Module &TheModule, StringRef OutputName,
730
751
* Perform internalization. Index is updated to reflect linkage changes.
731
752
*/
732
753
void ThinLTOCodeGenerator::internalize (Module &TheModule,
733
- ModuleSummaryIndex &Index) {
754
+ ModuleSummaryIndex &Index,
755
+ const lto::InputFile &File) {
734
756
initTMBuilder (TMBuilder, Triple (TheModule.getTargetTriple ()));
735
757
auto ModuleCount = Index.modulePaths ().size ();
736
758
auto ModuleIdentifier = TheModule.getModuleIdentifier ();
@@ -739,6 +761,8 @@ void ThinLTOCodeGenerator::internalize(Module &TheModule,
739
761
auto GUIDPreservedSymbols =
740
762
computeGUIDPreservedSymbols (PreservedSymbols, TMBuilder.TheTriple );
741
763
764
+ addUsedSymbolToPreservedGUID (File, GUIDPreservedSymbols);
765
+
742
766
// Collect for each module the list of function it defines (GUID -> Summary).
743
767
StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries (ModuleCount);
744
768
Index.collectDefinedGVSummariesPerModule (ModuleToDefinedGVSummaries);
@@ -830,15 +854,14 @@ void ThinLTOCodeGenerator::run() {
830
854
// Perform only parallel codegen and return.
831
855
ThreadPool Pool;
832
856
int count = 0 ;
833
- for (auto &ModuleBuffer : Modules) {
857
+ for (auto &Mod : Modules) {
834
858
Pool.async ([&](int count) {
835
859
LLVMContext Context;
836
860
Context.setDiscardValueNames (LTODiscardValueNames);
837
861
838
862
// Parse module now
839
- auto TheModule =
840
- loadModuleFromBuffer (ModuleBuffer.getMemBuffer (), Context, false ,
841
- /* IsImporting*/ false );
863
+ auto TheModule = loadModuleFromInput (Mod.get (), Context, false ,
864
+ /* IsImporting*/ false );
842
865
843
866
// CodeGen
844
867
auto OutputBuffer = codegenModule (*TheModule, *TMBuilder.create ());
@@ -881,6 +904,10 @@ void ThinLTOCodeGenerator::run() {
881
904
auto GUIDPreservedSymbols =
882
905
computeGUIDPreservedSymbols (PreservedSymbols, TMBuilder.TheTriple );
883
906
907
+ // Add used symbol from inputs to the preserved symbols.
908
+ for (const auto &M : Modules)
909
+ addUsedSymbolToPreservedGUID (*M, GUIDPreservedSymbols);
910
+
884
911
// Compute "dead" symbols, we don't want to import/export these!
885
912
computeDeadSymbolsInIndex (*Index, GUIDPreservedSymbols);
886
913
@@ -913,7 +940,7 @@ void ThinLTOCodeGenerator::run() {
913
940
// GVSummary and ResolvedODR maps to enable threaded access to these maps
914
941
// below.
915
942
for (auto &Module : Modules) {
916
- auto ModuleIdentifier = Module. getBufferIdentifier ();
943
+ auto ModuleIdentifier = Module-> getName ();
917
944
ExportLists[ModuleIdentifier];
918
945
ImportLists[ModuleIdentifier];
919
946
ResolvedODR[ModuleIdentifier];
@@ -927,18 +954,20 @@ void ThinLTOCodeGenerator::run() {
927
954
ModulesOrdering.resize (Modules.size ());
928
955
std::iota (ModulesOrdering.begin (), ModulesOrdering.end (), 0 );
929
956
llvm::sort (ModulesOrdering, [&](int LeftIndex, int RightIndex) {
930
- auto LSize = Modules[LeftIndex].getBuffer ().size ();
931
- auto RSize = Modules[RightIndex].getBuffer ().size ();
957
+ auto LSize =
958
+ Modules[LeftIndex]->getSingleBitcodeModule ().getBuffer ().size ();
959
+ auto RSize =
960
+ Modules[RightIndex]->getSingleBitcodeModule ().getBuffer ().size ();
932
961
return LSize > RSize;
933
962
});
934
963
935
964
// Parallel optimizer + codegen
936
965
{
937
966
ThreadPool Pool (ThreadCount);
938
967
for (auto IndexCount : ModulesOrdering) {
939
- auto &ModuleBuffer = Modules[IndexCount];
968
+ auto &Mod = Modules[IndexCount];
940
969
Pool.async ([&](int count) {
941
- auto ModuleIdentifier = ModuleBuffer. getBufferIdentifier ();
970
+ auto ModuleIdentifier = Mod-> getName ();
942
971
auto &ExportList = ExportLists[ModuleIdentifier];
943
972
944
973
auto &DefinedGVSummaries = ModuleToDefinedGVSummaries[ModuleIdentifier];
@@ -982,9 +1011,8 @@ void ThinLTOCodeGenerator::run() {
982
1011
}
983
1012
984
1013
// Parse module now
985
- auto TheModule =
986
- loadModuleFromBuffer (ModuleBuffer.getMemBuffer (), Context, false ,
987
- /* IsImporting*/ false );
1014
+ auto TheModule = loadModuleFromInput (Mod.get (), Context, false ,
1015
+ /* IsImporting*/ false );
988
1016
989
1017
// Save temps: original file.
990
1018
saveTempBitcode (*TheModule, SaveTempsDir, count, " .0.original.bc" );
0 commit comments