blob: fea1c7fb8fa981c69c8f7d924fdf308df50c2c5e [file] [log] [blame]
Mehdi Amini7c4a1a82016-03-09 01:37:22 +00001//===-ThinLTOCodeGenerator.cpp - LLVM Link Time Optimizer -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Thin Link Time Optimization library. This library is
11// intended to be used by linker to optimize code at link time.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/LTO/ThinLTOCodeGenerator.h"
16
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000017#include "llvm/ADT/Statistic.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000018#include "llvm/ADT/StringExtras.h"
Teresa Johnson2d5487c2016-04-11 13:58:45 +000019#include "llvm/Analysis/ModuleSummaryAnalysis.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000020#include "llvm/Analysis/TargetLibraryInfo.h"
21#include "llvm/Analysis/TargetTransformInfo.h"
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000022#include "llvm/Bitcode/BitcodeWriterPass.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000023#include "llvm/Bitcode/ReaderWriter.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000024#include "llvm/ExecutionEngine/ObjectMemoryBuffer.h"
Teresa Johnsoncec0cae2016-03-14 21:18:10 +000025#include "llvm/IR/DiagnosticPrinter.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000026#include "llvm/IR/LLVMContext.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000027#include "llvm/IR/LegacyPassManager.h"
28#include "llvm/IR/Mangler.h"
29#include "llvm/IRReader/IRReader.h"
30#include "llvm/Linker/Linker.h"
31#include "llvm/MC/SubtargetFeature.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000032#include "llvm/Object/ModuleSummaryIndexObjectFile.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000033#include "llvm/Support/SourceMgr.h"
34#include "llvm/Support/TargetRegistry.h"
35#include "llvm/Support/ThreadPool.h"
36#include "llvm/Target/TargetMachine.h"
37#include "llvm/Transforms/IPO.h"
38#include "llvm/Transforms/IPO/FunctionImport.h"
39#include "llvm/Transforms/IPO/PassManagerBuilder.h"
40#include "llvm/Transforms/ObjCARC.h"
41#include "llvm/Transforms/Utils/FunctionImportUtils.h"
42
43using namespace llvm;
44
Mehdi Amini09b4a8d2016-03-10 01:28:54 +000045namespace llvm {
46// Flags -discard-value-names, defined in LTOCodeGenerator.cpp
47extern cl::opt<bool> LTODiscardValueNames;
48}
49
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000050namespace {
51
52static cl::opt<int> ThreadCount("threads",
53 cl::init(std::thread::hardware_concurrency()));
54
55static void diagnosticHandler(const DiagnosticInfo &DI) {
56 DiagnosticPrinterRawOStream DP(errs());
57 DI.print(DP);
58 errs() << '\n';
59}
60
61// Simple helper to load a module from bitcode
62static std::unique_ptr<Module>
63loadModuleFromBuffer(const MemoryBufferRef &Buffer, LLVMContext &Context,
64 bool Lazy) {
65 SMDiagnostic Err;
66 ErrorOr<std::unique_ptr<Module>> ModuleOrErr(nullptr);
67 if (Lazy) {
68 ModuleOrErr =
69 getLazyBitcodeModule(MemoryBuffer::getMemBuffer(Buffer, false), Context,
70 /* ShouldLazyLoadMetadata */ Lazy);
71 } else {
72 ModuleOrErr = parseBitcodeFile(Buffer, Context);
73 }
74 if (std::error_code EC = ModuleOrErr.getError()) {
75 Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
76 EC.message());
77 Err.print("ThinLTO", errs());
78 report_fatal_error("Can't load module, abort.");
79 }
80 return std::move(ModuleOrErr.get());
81}
82
83// Simple helper to save temporary files for debug.
84static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
85 unsigned count, StringRef Suffix) {
86 if (TempDir.empty())
87 return;
88 // User asked to save temps, let dump the bitcode file after import.
89 auto SaveTempPath = TempDir + llvm::utostr(count) + Suffix;
90 std::error_code EC;
91 raw_fd_ostream OS(SaveTempPath.str(), EC, sys::fs::F_None);
92 if (EC)
93 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
94 " to save optimized bitcode\n");
Teresa Johnson3c35e092016-04-04 21:19:31 +000095 WriteBitcodeToFile(&TheModule, OS, /* ShouldPreserveUseListOrder */ true);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000096}
97
Mehdi Amini5a2e5d32016-04-01 21:53:50 +000098bool IsFirstDefinitionForLinker(const GlobalValueInfoList &GVInfo,
99 const ModuleSummaryIndex &Index,
100 StringRef ModulePath) {
101 // Get the first *linker visible* definition for this global in the summary
102 // list.
103 auto FirstDefForLinker = llvm::find_if(
104 GVInfo, [](const std::unique_ptr<GlobalValueInfo> &FuncInfo) {
105 auto Linkage = FuncInfo->summary()->linkage();
106 return !GlobalValue::isAvailableExternallyLinkage(Linkage);
107 });
108 // If \p GV is not the first definition, give up...
109 if ((*FirstDefForLinker)->summary()->modulePath() != ModulePath)
110 return false;
111 // If there is any strong definition anywhere, do not bother emitting this.
112 if (llvm::any_of(
113 GVInfo, [](const std::unique_ptr<GlobalValueInfo> &FuncInfo) {
114 auto Linkage = FuncInfo->summary()->linkage();
115 return !GlobalValue::isAvailableExternallyLinkage(Linkage) &&
116 !GlobalValue::isWeakForLinker(Linkage);
117 }))
118 return false;
119 return true;
Hans Wennborgfa6e4142016-04-02 01:03:41 +0000120}
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000121
122static void ResolveODR(GlobalValue &GV, const ModuleSummaryIndex &Index,
123 StringRef ModulePath) {
124 if (GV.isDeclaration())
125 return;
126
127 auto HasMultipleCopies =
128 [&](const GlobalValueInfoList &GVInfo) { return GVInfo.size() > 1; };
129
130 auto getGVInfo = [&](GlobalValue &GV) -> const GlobalValueInfoList *{
131 auto GUID = Function::getGlobalIdentifier(GV.getName(), GV.getLinkage(),
132 ModulePath);
133 auto It = Index.findGlobalValueInfoList(GV.getName());
134 if (It == Index.end())
135 return nullptr;
136 return &It->second;
137 };
138
139 switch (GV.getLinkage()) {
140 case GlobalValue::ExternalLinkage:
141 case GlobalValue::AvailableExternallyLinkage:
142 case GlobalValue::AppendingLinkage:
143 case GlobalValue::InternalLinkage:
144 case GlobalValue::PrivateLinkage:
145 case GlobalValue::ExternalWeakLinkage:
146 case GlobalValue::CommonLinkage:
147 case GlobalValue::LinkOnceAnyLinkage:
148 case GlobalValue::WeakAnyLinkage:
149 break;
150 case GlobalValue::LinkOnceODRLinkage:
151 case GlobalValue::WeakODRLinkage: {
152 auto *GVInfo = getGVInfo(GV);
153 if (!GVInfo)
154 break;
155 // We need to emit only one of these, the first module will keep
156 // it, but turned into a weak while the others will drop it.
157 if (!HasMultipleCopies(*GVInfo))
158 break;
159 if (IsFirstDefinitionForLinker(*GVInfo, Index, ModulePath))
160 GV.setLinkage(GlobalValue::WeakODRLinkage);
161 else
162 GV.setLinkage(GlobalValue::AvailableExternallyLinkage);
163 break;
164 }
165 }
166}
167
168/// Resolve LinkOnceODR and WeakODR.
169///
170/// We'd like to drop these function if they are no longer referenced in the
171/// current module. However there is a chance that another module is still
172/// referencing them because of the import. We make sure we always emit at least
173/// one copy.
174static void ResolveODR(Module &TheModule,
175 const ModuleSummaryIndex &Index) {
176 // We won't optimize the globals that are referenced by an alias for now
177 // Ideally we should turn the alias into a global and duplicate the definition
178 // when needed.
179 DenseSet<GlobalValue *> GlobalInvolvedWithAlias;
180 for (auto &GA : TheModule.aliases()) {
181 auto *GO = GA.getBaseObject();
182 if (auto *GV = dyn_cast<GlobalValue>(GO))
183 GlobalInvolvedWithAlias.insert(GV);
184 }
185 // Process functions and global now
186 for (auto &GV : TheModule) {
187 if (!GlobalInvolvedWithAlias.count(&GV))
188 ResolveODR(GV, Index, TheModule.getModuleIdentifier());
189 }
190 for (auto &GV : TheModule.globals()) {
191 if (!GlobalInvolvedWithAlias.count(&GV))
192 ResolveODR(GV, Index, TheModule.getModuleIdentifier());
193 }
194}
195
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000196static StringMap<MemoryBufferRef>
197generateModuleMap(const std::vector<MemoryBufferRef> &Modules) {
198 StringMap<MemoryBufferRef> ModuleMap;
199 for (auto &ModuleBuffer : Modules) {
200 assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) ==
201 ModuleMap.end() &&
202 "Expect unique Buffer Identifier");
203 ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer;
204 }
205 return ModuleMap;
206}
207
208/// Provide a "loader" for the FunctionImporter to access function from other
209/// modules.
210class ModuleLoader {
211 /// The context that will be used for importing.
212 LLVMContext &Context;
213
214 /// Map from Module identifier to MemoryBuffer. Used by clients like the
215 /// FunctionImported to request loading a Module.
216 StringMap<MemoryBufferRef> &ModuleMap;
217
218public:
219 ModuleLoader(LLVMContext &Context, StringMap<MemoryBufferRef> &ModuleMap)
220 : Context(Context), ModuleMap(ModuleMap) {}
221
222 /// Load a module on demand.
223 std::unique_ptr<Module> operator()(StringRef Identifier) {
224 return loadModuleFromBuffer(ModuleMap[Identifier], Context, /*Lazy*/ true);
225 }
226};
227
Teresa Johnson26ab5772016-03-15 00:04:37 +0000228static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000229 if (renameModuleForThinLTO(TheModule, Index))
230 report_fatal_error("renameModuleForThinLTO failed");
231}
232
Mehdi Amini01e32132016-03-26 05:40:34 +0000233static void
234crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
235 StringMap<MemoryBufferRef> &ModuleMap,
236 const FunctionImporter::ImportMapTy &ImportList) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000237 ModuleLoader Loader(TheModule.getContext(), ModuleMap);
238 FunctionImporter Importer(Index, Loader);
Mehdi Amini01e32132016-03-26 05:40:34 +0000239 Importer.importFunctions(TheModule, ImportList);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000240}
241
242static void optimizeModule(Module &TheModule, TargetMachine &TM) {
243 // Populate the PassManager
244 PassManagerBuilder PMB;
245 PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple());
246 PMB.Inliner = createFunctionInliningPass();
247 // FIXME: should get it from the bitcode?
248 PMB.OptLevel = 3;
249 PMB.LoopVectorize = true;
250 PMB.SLPVectorize = true;
251 PMB.VerifyInput = true;
252 PMB.VerifyOutput = false;
253
254 legacy::PassManager PM;
255
256 // Add the TTI (required to inform the vectorizer about register size for
257 // instance)
258 PM.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
259
260 // Add optimizations
261 PMB.populateThinLTOPassManager(PM);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000262
263 PM.run(TheModule);
264}
265
266std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
267 TargetMachine &TM) {
268 SmallVector<char, 128> OutputBuffer;
269
270 // CodeGen
271 {
272 raw_svector_ostream OS(OutputBuffer);
273 legacy::PassManager PM;
Mehdi Amini215d59e2016-04-01 08:22:59 +0000274
275 // If the bitcode files contain ARC code and were compiled with optimization,
276 // the ObjCARCContractPass must be run, so do it unconditionally here.
277 PM.add(createObjCARCContractPass());
278
279 // Setup the codegen now.
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000280 if (TM.addPassesToEmitFile(PM, OS, TargetMachine::CGFT_ObjectFile,
281 /* DisableVerify */ true))
282 report_fatal_error("Failed to setup codegen");
283
284 // Run codegen now. resulting binary is in OutputBuffer.
285 PM.run(TheModule);
286 }
287 return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
288}
289
290static std::unique_ptr<MemoryBuffer>
Teresa Johnson26ab5772016-03-15 00:04:37 +0000291ProcessThinLTOModule(Module &TheModule, const ModuleSummaryIndex &Index,
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000292 StringMap<MemoryBufferRef> &ModuleMap, TargetMachine &TM,
Mehdi Amini01e32132016-03-26 05:40:34 +0000293 const FunctionImporter::ImportMapTy &ImportList,
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000294 ThinLTOCodeGenerator::CachingOptions CacheOptions,
Mehdi Amini43b657b2016-04-01 06:47:02 +0000295 bool DisableCodeGen, StringRef SaveTempsDir,
296 unsigned count) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000297
298 // Save temps: after IPO.
299 saveTempBitcode(TheModule, SaveTempsDir, count, ".1.IPO.bc");
300
301 // "Benchmark"-like optimization: single-source case
302 bool SingleModule = (ModuleMap.size() == 1);
303
304 if (!SingleModule) {
305 promoteModule(TheModule, Index);
306
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000307 // Resolve the LinkOnce/Weak ODR, trying to turn them into
308 // "available_externally" when possible.
309 // This is a compile-time optimization.
310 ResolveODR(TheModule, Index);
311
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000312 // Save temps: after promotion.
313 saveTempBitcode(TheModule, SaveTempsDir, count, ".2.promoted.bc");
314
Mehdi Amini01e32132016-03-26 05:40:34 +0000315 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000316
317 // Save temps: after cross-module import.
318 saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
319 }
320
321 optimizeModule(TheModule, TM);
322
323 saveTempBitcode(TheModule, SaveTempsDir, count, ".3.opt.bc");
324
Mehdi Amini43b657b2016-04-01 06:47:02 +0000325 if (DisableCodeGen) {
326 // Configured to stop before CodeGen, serialize the bitcode and return.
327 SmallVector<char, 128> OutputBuffer;
328 {
329 raw_svector_ostream OS(OutputBuffer);
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000330 ModuleSummaryIndexBuilder IndexBuilder(&TheModule);
331 WriteBitcodeToFile(&TheModule, OS, true, &IndexBuilder.getIndex());
Mehdi Amini43b657b2016-04-01 06:47:02 +0000332 }
333 return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
334 }
335
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000336 return codegenModule(TheModule, TM);
337}
338
339// Initialize the TargetMachine builder for a given Triple
340static void initTMBuilder(TargetMachineBuilder &TMBuilder,
341 const Triple &TheTriple) {
342 // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
343 // FIXME this looks pretty terrible...
344 if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
345 if (TheTriple.getArch() == llvm::Triple::x86_64)
346 TMBuilder.MCpu = "core2";
347 else if (TheTriple.getArch() == llvm::Triple::x86)
348 TMBuilder.MCpu = "yonah";
349 else if (TheTriple.getArch() == llvm::Triple::aarch64)
350 TMBuilder.MCpu = "cyclone";
351 }
352 TMBuilder.TheTriple = std::move(TheTriple);
353}
354
355} // end anonymous namespace
356
357void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
358 MemoryBufferRef Buffer(Data, Identifier);
359 if (Modules.empty()) {
360 // First module added, so initialize the triple and some options
361 LLVMContext Context;
362 Triple TheTriple(getBitcodeTargetTriple(Buffer, Context));
363 initTMBuilder(TMBuilder, Triple(TheTriple));
364 }
365#ifndef NDEBUG
366 else {
367 LLVMContext Context;
368 assert(TMBuilder.TheTriple.str() ==
369 getBitcodeTargetTriple(Buffer, Context) &&
370 "ThinLTO modules with different triple not supported");
371 }
372#endif
373 Modules.push_back(Buffer);
374}
375
376void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) {
377 PreservedSymbols.insert(Name);
378}
379
380void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
381 CrossReferencedSymbols.insert(Name);
382}
383
384// TargetMachine factory
385std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
386 std::string ErrMsg;
387 const Target *TheTarget =
388 TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
389 if (!TheTarget) {
390 report_fatal_error("Can't load target for this Triple: " + ErrMsg);
391 }
392
393 // Use MAttr as the default set of features.
394 SubtargetFeatures Features(MAttr);
395 Features.getDefaultSubtargetFeatures(TheTriple);
396 std::string FeatureStr = Features.getString();
397 return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
398 TheTriple.str(), MCpu, FeatureStr, Options, RelocModel,
399 CodeModel::Default, CGOptLevel));
400}
401
402/**
Teresa Johnson26ab5772016-03-15 00:04:37 +0000403 * Produce the combined summary index from all the bitcode files:
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000404 * "thin-link".
405 */
Teresa Johnson26ab5772016-03-15 00:04:37 +0000406std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
407 std::unique_ptr<ModuleSummaryIndex> CombinedIndex;
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000408 uint64_t NextModuleId = 0;
409 for (auto &ModuleBuffer : Modules) {
Teresa Johnson26ab5772016-03-15 00:04:37 +0000410 ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
411 object::ModuleSummaryIndexObjectFile::create(ModuleBuffer,
412 diagnosticHandler, false);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000413 if (std::error_code EC = ObjOrErr.getError()) {
414 // FIXME diagnose
Teresa Johnson26ab5772016-03-15 00:04:37 +0000415 errs() << "error: can't create ModuleSummaryIndexObjectFile for buffer: "
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000416 << EC.message() << "\n";
417 return nullptr;
418 }
419 auto Index = (*ObjOrErr)->takeIndex();
420 if (CombinedIndex) {
421 CombinedIndex->mergeFrom(std::move(Index), ++NextModuleId);
422 } else {
423 CombinedIndex = std::move(Index);
424 }
425 }
426 return CombinedIndex;
427}
428
429/**
430 * Perform promotion and renaming of exported internal functions.
431 */
432void ThinLTOCodeGenerator::promote(Module &TheModule,
Teresa Johnson26ab5772016-03-15 00:04:37 +0000433 ModuleSummaryIndex &Index) {
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000434
435 // Resolve the LinkOnceODR, trying to turn them into "available_externally"
436 // where possible.
437 ResolveODR(TheModule, Index);
438
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000439 promoteModule(TheModule, Index);
440}
441
442/**
443 * Perform cross-module importing for the module identified by ModuleIdentifier.
444 */
445void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
Teresa Johnson26ab5772016-03-15 00:04:37 +0000446 ModuleSummaryIndex &Index) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000447 auto ModuleMap = generateModuleMap(Modules);
Mehdi Amini01e32132016-03-26 05:40:34 +0000448
449 // Generate import/export list
450 auto ModuleCount = Index.modulePaths().size();
451 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
452 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
453 ComputeCrossModuleImport(Index, ImportLists, ExportLists);
454 auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
455
456 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000457}
458
459/**
460 * Perform post-importing ThinLTO optimizations.
461 */
462void ThinLTOCodeGenerator::optimize(Module &TheModule) {
463 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
464 optimizeModule(TheModule, *TMBuilder.create());
465}
466
467/**
468 * Perform ThinLTO CodeGen.
469 */
470std::unique_ptr<MemoryBuffer> ThinLTOCodeGenerator::codegen(Module &TheModule) {
471 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
472 return codegenModule(TheModule, *TMBuilder.create());
473}
474
475// Main entry point for the ThinLTO processing
476void ThinLTOCodeGenerator::run() {
Mehdi Amini43b657b2016-04-01 06:47:02 +0000477 if (CodeGenOnly) {
478 // Perform only parallel codegen and return.
479 ThreadPool Pool;
480 assert(ProducedBinaries.empty() && "The generator should not be reused");
481 ProducedBinaries.resize(Modules.size());
482 int count = 0;
483 for (auto &ModuleBuffer : Modules) {
484 Pool.async([&](int count) {
485 LLVMContext Context;
486 Context.setDiscardValueNames(LTODiscardValueNames);
487
488 // Parse module now
489 auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
490
491 // CodeGen
492 ProducedBinaries[count] = codegen(*TheModule);
493 }, count++);
494 }
495
496 return;
497 }
498
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000499 // Sequential linking phase
500 auto Index = linkCombinedIndex();
501
502 // Save temps: index.
503 if (!SaveTempsDir.empty()) {
504 auto SaveTempPath = SaveTempsDir + "index.bc";
505 std::error_code EC;
506 raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None);
507 if (EC)
508 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
509 " to save optimized bitcode\n");
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000510 WriteIndexToFile(*Index, OS);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000511 }
512
513 // Prepare the resulting object vector
514 assert(ProducedBinaries.empty() && "The generator should not be reused");
515 ProducedBinaries.resize(Modules.size());
516
517 // Prepare the module map.
518 auto ModuleMap = generateModuleMap(Modules);
Mehdi Amini01e32132016-03-26 05:40:34 +0000519 auto ModuleCount = Modules.size();
520
521 // Collect the import/export lists for all modules from the call-graph in the
522 // combined index.
523 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
524 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
525 ComputeCrossModuleImport(*Index, ImportLists, ExportLists);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000526
527 // Parallel optimizer + codegen
528 {
529 ThreadPool Pool(ThreadCount);
530 int count = 0;
531 for (auto &ModuleBuffer : Modules) {
532 Pool.async([&](int count) {
533 LLVMContext Context;
Mehdi Amini09b4a8d2016-03-10 01:28:54 +0000534 Context.setDiscardValueNames(LTODiscardValueNames);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000535
536 // Parse module now
537 auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
538
539 // Save temps: original file.
540 if (!SaveTempsDir.empty()) {
541 saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
542 }
543
Mehdi Amini01e32132016-03-26 05:40:34 +0000544 auto &ImportList = ImportLists[TheModule->getModuleIdentifier()];
Mehdi Amini024a79f2016-04-02 05:08:18 +0000545 ProducedBinaries[count] = ProcessThinLTOModule(
Mehdi Amini01e32132016-03-26 05:40:34 +0000546 *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
Mehdi Amini43b657b2016-04-01 06:47:02 +0000547 CacheOptions, DisableCodeGen, SaveTempsDir, count);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000548 }, count);
549 count++;
550 }
551 }
552
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000553 // If statistics were requested, print them out now.
554 if (llvm::AreStatisticsEnabled())
555 llvm::PrintStatistics();
556}