blob: c9e91eb2202fd9ecd27e4a2d5f9e2fc46b6c6681 [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) {
Mehdi Amini8dcc8082016-04-14 08:46:22 +0000176 if (Index.modulePaths().size() == 1)
177 // Nothing to do if we don't have multiple modules
178 return;
179
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000180 // We won't optimize the globals that are referenced by an alias for now
181 // Ideally we should turn the alias into a global and duplicate the definition
182 // when needed.
183 DenseSet<GlobalValue *> GlobalInvolvedWithAlias;
184 for (auto &GA : TheModule.aliases()) {
185 auto *GO = GA.getBaseObject();
186 if (auto *GV = dyn_cast<GlobalValue>(GO))
187 GlobalInvolvedWithAlias.insert(GV);
188 }
189 // Process functions and global now
190 for (auto &GV : TheModule) {
191 if (!GlobalInvolvedWithAlias.count(&GV))
192 ResolveODR(GV, Index, TheModule.getModuleIdentifier());
193 }
194 for (auto &GV : TheModule.globals()) {
195 if (!GlobalInvolvedWithAlias.count(&GV))
196 ResolveODR(GV, Index, TheModule.getModuleIdentifier());
197 }
198}
199
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000200static StringMap<MemoryBufferRef>
201generateModuleMap(const std::vector<MemoryBufferRef> &Modules) {
202 StringMap<MemoryBufferRef> ModuleMap;
203 for (auto &ModuleBuffer : Modules) {
204 assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) ==
205 ModuleMap.end() &&
206 "Expect unique Buffer Identifier");
207 ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer;
208 }
209 return ModuleMap;
210}
211
212/// Provide a "loader" for the FunctionImporter to access function from other
213/// modules.
214class ModuleLoader {
215 /// The context that will be used for importing.
216 LLVMContext &Context;
217
218 /// Map from Module identifier to MemoryBuffer. Used by clients like the
219 /// FunctionImported to request loading a Module.
220 StringMap<MemoryBufferRef> &ModuleMap;
221
222public:
223 ModuleLoader(LLVMContext &Context, StringMap<MemoryBufferRef> &ModuleMap)
224 : Context(Context), ModuleMap(ModuleMap) {}
225
226 /// Load a module on demand.
227 std::unique_ptr<Module> operator()(StringRef Identifier) {
228 return loadModuleFromBuffer(ModuleMap[Identifier], Context, /*Lazy*/ true);
229 }
230};
231
Teresa Johnson26ab5772016-03-15 00:04:37 +0000232static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000233 if (renameModuleForThinLTO(TheModule, Index))
234 report_fatal_error("renameModuleForThinLTO failed");
235}
236
Mehdi Amini01e32132016-03-26 05:40:34 +0000237static void
238crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
239 StringMap<MemoryBufferRef> &ModuleMap,
240 const FunctionImporter::ImportMapTy &ImportList) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000241 ModuleLoader Loader(TheModule.getContext(), ModuleMap);
242 FunctionImporter Importer(Index, Loader);
Mehdi Amini01e32132016-03-26 05:40:34 +0000243 Importer.importFunctions(TheModule, ImportList);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000244}
245
246static void optimizeModule(Module &TheModule, TargetMachine &TM) {
247 // Populate the PassManager
248 PassManagerBuilder PMB;
249 PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple());
250 PMB.Inliner = createFunctionInliningPass();
251 // FIXME: should get it from the bitcode?
252 PMB.OptLevel = 3;
253 PMB.LoopVectorize = true;
254 PMB.SLPVectorize = true;
255 PMB.VerifyInput = true;
256 PMB.VerifyOutput = false;
257
258 legacy::PassManager PM;
259
260 // Add the TTI (required to inform the vectorizer about register size for
261 // instance)
262 PM.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
263
264 // Add optimizations
265 PMB.populateThinLTOPassManager(PM);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000266
267 PM.run(TheModule);
268}
269
270std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
271 TargetMachine &TM) {
272 SmallVector<char, 128> OutputBuffer;
273
274 // CodeGen
275 {
276 raw_svector_ostream OS(OutputBuffer);
277 legacy::PassManager PM;
Mehdi Amini215d59e2016-04-01 08:22:59 +0000278
279 // If the bitcode files contain ARC code and were compiled with optimization,
280 // the ObjCARCContractPass must be run, so do it unconditionally here.
281 PM.add(createObjCARCContractPass());
282
283 // Setup the codegen now.
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000284 if (TM.addPassesToEmitFile(PM, OS, TargetMachine::CGFT_ObjectFile,
285 /* DisableVerify */ true))
286 report_fatal_error("Failed to setup codegen");
287
288 // Run codegen now. resulting binary is in OutputBuffer.
289 PM.run(TheModule);
290 }
291 return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
292}
293
294static std::unique_ptr<MemoryBuffer>
Teresa Johnson26ab5772016-03-15 00:04:37 +0000295ProcessThinLTOModule(Module &TheModule, const ModuleSummaryIndex &Index,
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000296 StringMap<MemoryBufferRef> &ModuleMap, TargetMachine &TM,
Mehdi Amini01e32132016-03-26 05:40:34 +0000297 const FunctionImporter::ImportMapTy &ImportList,
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000298 ThinLTOCodeGenerator::CachingOptions CacheOptions,
Mehdi Amini43b657b2016-04-01 06:47:02 +0000299 bool DisableCodeGen, StringRef SaveTempsDir,
300 unsigned count) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000301
302 // Save temps: after IPO.
303 saveTempBitcode(TheModule, SaveTempsDir, count, ".1.IPO.bc");
304
305 // "Benchmark"-like optimization: single-source case
306 bool SingleModule = (ModuleMap.size() == 1);
307
308 if (!SingleModule) {
309 promoteModule(TheModule, Index);
310
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000311 // Resolve the LinkOnce/Weak ODR, trying to turn them into
312 // "available_externally" when possible.
313 // This is a compile-time optimization.
314 ResolveODR(TheModule, Index);
315
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000316 // Save temps: after promotion.
317 saveTempBitcode(TheModule, SaveTempsDir, count, ".2.promoted.bc");
318
Mehdi Amini01e32132016-03-26 05:40:34 +0000319 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000320
321 // Save temps: after cross-module import.
322 saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
323 }
324
325 optimizeModule(TheModule, TM);
326
327 saveTempBitcode(TheModule, SaveTempsDir, count, ".3.opt.bc");
328
Mehdi Amini43b657b2016-04-01 06:47:02 +0000329 if (DisableCodeGen) {
330 // Configured to stop before CodeGen, serialize the bitcode and return.
331 SmallVector<char, 128> OutputBuffer;
332 {
333 raw_svector_ostream OS(OutputBuffer);
Teresa Johnson2d5487c2016-04-11 13:58:45 +0000334 ModuleSummaryIndexBuilder IndexBuilder(&TheModule);
335 WriteBitcodeToFile(&TheModule, OS, true, &IndexBuilder.getIndex());
Mehdi Amini43b657b2016-04-01 06:47:02 +0000336 }
337 return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
338 }
339
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000340 return codegenModule(TheModule, TM);
341}
342
343// Initialize the TargetMachine builder for a given Triple
344static void initTMBuilder(TargetMachineBuilder &TMBuilder,
345 const Triple &TheTriple) {
346 // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
347 // FIXME this looks pretty terrible...
348 if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
349 if (TheTriple.getArch() == llvm::Triple::x86_64)
350 TMBuilder.MCpu = "core2";
351 else if (TheTriple.getArch() == llvm::Triple::x86)
352 TMBuilder.MCpu = "yonah";
353 else if (TheTriple.getArch() == llvm::Triple::aarch64)
354 TMBuilder.MCpu = "cyclone";
355 }
356 TMBuilder.TheTriple = std::move(TheTriple);
357}
358
359} // end anonymous namespace
360
361void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
362 MemoryBufferRef Buffer(Data, Identifier);
363 if (Modules.empty()) {
364 // First module added, so initialize the triple and some options
365 LLVMContext Context;
366 Triple TheTriple(getBitcodeTargetTriple(Buffer, Context));
367 initTMBuilder(TMBuilder, Triple(TheTriple));
368 }
369#ifndef NDEBUG
370 else {
371 LLVMContext Context;
372 assert(TMBuilder.TheTriple.str() ==
373 getBitcodeTargetTriple(Buffer, Context) &&
374 "ThinLTO modules with different triple not supported");
375 }
376#endif
377 Modules.push_back(Buffer);
378}
379
380void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) {
381 PreservedSymbols.insert(Name);
382}
383
384void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
385 CrossReferencedSymbols.insert(Name);
386}
387
388// TargetMachine factory
389std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
390 std::string ErrMsg;
391 const Target *TheTarget =
392 TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
393 if (!TheTarget) {
394 report_fatal_error("Can't load target for this Triple: " + ErrMsg);
395 }
396
397 // Use MAttr as the default set of features.
398 SubtargetFeatures Features(MAttr);
399 Features.getDefaultSubtargetFeatures(TheTriple);
400 std::string FeatureStr = Features.getString();
401 return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
402 TheTriple.str(), MCpu, FeatureStr, Options, RelocModel,
403 CodeModel::Default, CGOptLevel));
404}
405
406/**
Teresa Johnson26ab5772016-03-15 00:04:37 +0000407 * Produce the combined summary index from all the bitcode files:
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000408 * "thin-link".
409 */
Teresa Johnson26ab5772016-03-15 00:04:37 +0000410std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
411 std::unique_ptr<ModuleSummaryIndex> CombinedIndex;
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000412 uint64_t NextModuleId = 0;
413 for (auto &ModuleBuffer : Modules) {
Teresa Johnson26ab5772016-03-15 00:04:37 +0000414 ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
415 object::ModuleSummaryIndexObjectFile::create(ModuleBuffer,
416 diagnosticHandler, false);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000417 if (std::error_code EC = ObjOrErr.getError()) {
418 // FIXME diagnose
Teresa Johnson26ab5772016-03-15 00:04:37 +0000419 errs() << "error: can't create ModuleSummaryIndexObjectFile for buffer: "
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000420 << EC.message() << "\n";
421 return nullptr;
422 }
423 auto Index = (*ObjOrErr)->takeIndex();
424 if (CombinedIndex) {
425 CombinedIndex->mergeFrom(std::move(Index), ++NextModuleId);
426 } else {
427 CombinedIndex = std::move(Index);
428 }
429 }
430 return CombinedIndex;
431}
432
433/**
434 * Perform promotion and renaming of exported internal functions.
435 */
436void ThinLTOCodeGenerator::promote(Module &TheModule,
Teresa Johnson26ab5772016-03-15 00:04:37 +0000437 ModuleSummaryIndex &Index) {
Mehdi Amini5a2e5d32016-04-01 21:53:50 +0000438
439 // Resolve the LinkOnceODR, trying to turn them into "available_externally"
440 // where possible.
441 ResolveODR(TheModule, Index);
442
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000443 promoteModule(TheModule, Index);
444}
445
446/**
447 * Perform cross-module importing for the module identified by ModuleIdentifier.
448 */
449void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
Teresa Johnson26ab5772016-03-15 00:04:37 +0000450 ModuleSummaryIndex &Index) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000451 auto ModuleMap = generateModuleMap(Modules);
Mehdi Amini01e32132016-03-26 05:40:34 +0000452
453 // Generate import/export list
454 auto ModuleCount = Index.modulePaths().size();
455 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
456 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
457 ComputeCrossModuleImport(Index, ImportLists, ExportLists);
458 auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
459
460 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000461}
462
463/**
464 * Perform post-importing ThinLTO optimizations.
465 */
466void ThinLTOCodeGenerator::optimize(Module &TheModule) {
467 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
468 optimizeModule(TheModule, *TMBuilder.create());
469}
470
471/**
472 * Perform ThinLTO CodeGen.
473 */
474std::unique_ptr<MemoryBuffer> ThinLTOCodeGenerator::codegen(Module &TheModule) {
475 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
476 return codegenModule(TheModule, *TMBuilder.create());
477}
478
479// Main entry point for the ThinLTO processing
480void ThinLTOCodeGenerator::run() {
Mehdi Amini43b657b2016-04-01 06:47:02 +0000481 if (CodeGenOnly) {
482 // Perform only parallel codegen and return.
483 ThreadPool Pool;
484 assert(ProducedBinaries.empty() && "The generator should not be reused");
485 ProducedBinaries.resize(Modules.size());
486 int count = 0;
487 for (auto &ModuleBuffer : Modules) {
488 Pool.async([&](int count) {
489 LLVMContext Context;
490 Context.setDiscardValueNames(LTODiscardValueNames);
491
492 // Parse module now
493 auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
494
495 // CodeGen
496 ProducedBinaries[count] = codegen(*TheModule);
497 }, count++);
498 }
499
500 return;
501 }
502
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000503 // Sequential linking phase
504 auto Index = linkCombinedIndex();
505
506 // Save temps: index.
507 if (!SaveTempsDir.empty()) {
508 auto SaveTempPath = SaveTempsDir + "index.bc";
509 std::error_code EC;
510 raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None);
511 if (EC)
512 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
513 " to save optimized bitcode\n");
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000514 WriteIndexToFile(*Index, OS);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000515 }
516
517 // Prepare the resulting object vector
518 assert(ProducedBinaries.empty() && "The generator should not be reused");
519 ProducedBinaries.resize(Modules.size());
520
521 // Prepare the module map.
522 auto ModuleMap = generateModuleMap(Modules);
Mehdi Amini01e32132016-03-26 05:40:34 +0000523 auto ModuleCount = Modules.size();
524
525 // Collect the import/export lists for all modules from the call-graph in the
526 // combined index.
527 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
528 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
529 ComputeCrossModuleImport(*Index, ImportLists, ExportLists);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000530
531 // Parallel optimizer + codegen
532 {
533 ThreadPool Pool(ThreadCount);
534 int count = 0;
535 for (auto &ModuleBuffer : Modules) {
536 Pool.async([&](int count) {
537 LLVMContext Context;
Mehdi Amini09b4a8d2016-03-10 01:28:54 +0000538 Context.setDiscardValueNames(LTODiscardValueNames);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000539
540 // Parse module now
541 auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
542
543 // Save temps: original file.
544 if (!SaveTempsDir.empty()) {
545 saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
546 }
547
Mehdi Amini01e32132016-03-26 05:40:34 +0000548 auto &ImportList = ImportLists[TheModule->getModuleIdentifier()];
Mehdi Amini024a79f2016-04-02 05:08:18 +0000549 ProducedBinaries[count] = ProcessThinLTOModule(
Mehdi Amini01e32132016-03-26 05:40:34 +0000550 *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
Mehdi Amini43b657b2016-04-01 06:47:02 +0000551 CacheOptions, DisableCodeGen, SaveTempsDir, count);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000552 }, count);
553 count++;
554 }
555 }
556
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000557 // If statistics were requested, print them out now.
558 if (llvm::AreStatisticsEnabled())
559 llvm::PrintStatistics();
560}