1 //===-ThinLTOCodeGenerator.cpp - LLVM Link Time Optimizer -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/LTO/ThinLTOCodeGenerator.h"
17 #ifdef HAVE_LLVM_REVISION
18 #include "LLVMLTORevision.h"
21 #include "UpdateCompilerUsed.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
25 #include "llvm/Analysis/TargetLibraryInfo.h"
26 #include "llvm/Analysis/TargetTransformInfo.h"
27 #include "llvm/Bitcode/BitcodeWriterPass.h"
28 #include "llvm/Bitcode/ReaderWriter.h"
29 #include "llvm/ExecutionEngine/ObjectMemoryBuffer.h"
30 #include "llvm/IR/DiagnosticPrinter.h"
31 #include "llvm/IR/LLVMContext.h"
32 #include "llvm/IR/LegacyPassManager.h"
33 #include "llvm/IR/Mangler.h"
34 #include "llvm/IRReader/IRReader.h"
35 #include "llvm/Linker/Linker.h"
36 #include "llvm/MC/SubtargetFeature.h"
37 #include "llvm/Object/IRObjectFile.h"
38 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Support/CachePruning.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/Path.h"
43 #include "llvm/Support/SHA1.h"
44 #include "llvm/Support/SourceMgr.h"
45 #include "llvm/Support/TargetRegistry.h"
46 #include "llvm/Support/ThreadPool.h"
47 #include "llvm/Target/TargetMachine.h"
48 #include "llvm/Transforms/IPO.h"
49 #include "llvm/Transforms/IPO/FunctionImport.h"
50 #include "llvm/Transforms/IPO/Internalize.h"
51 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
52 #include "llvm/Transforms/ObjCARC.h"
53 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
57 #define DEBUG_TYPE "thinlto"
60 // Flags -discard-value-names, defined in LTOCodeGenerator.cpp
61 extern cl::opt<bool> LTODiscardValueNames;
66 static cl::opt<int> ThreadCount("threads",
67 cl::init(std::thread::hardware_concurrency()));
69 static void diagnosticHandler(const DiagnosticInfo &DI) {
70 DiagnosticPrinterRawOStream DP(errs());
75 // Simple helper to load a module from bitcode
76 static std::unique_ptr<Module>
77 loadModuleFromBuffer(const MemoryBufferRef &Buffer, LLVMContext &Context,
80 ErrorOr<std::unique_ptr<Module>> ModuleOrErr(nullptr);
83 getLazyBitcodeModule(MemoryBuffer::getMemBuffer(Buffer, false), Context,
84 /* ShouldLazyLoadMetadata */ Lazy);
86 ModuleOrErr = parseBitcodeFile(Buffer, Context);
88 if (std::error_code EC = ModuleOrErr.getError()) {
89 Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
91 Err.print("ThinLTO", errs());
92 report_fatal_error("Can't load module, abort.");
94 return std::move(ModuleOrErr.get());
97 // Simple helper to save temporary files for debug.
98 static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
99 unsigned count, StringRef Suffix) {
102 // User asked to save temps, let dump the bitcode file after import.
103 auto SaveTempPath = TempDir + llvm::utostr(count) + Suffix;
105 raw_fd_ostream OS(SaveTempPath.str(), EC, sys::fs::F_None);
107 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
108 " to save optimized bitcode\n");
109 WriteBitcodeToFile(&TheModule, OS, /* ShouldPreserveUseListOrder */ true);
112 bool IsFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList,
113 const ModuleSummaryIndex &Index,
114 StringRef ModulePath) {
115 // Get the first *linker visible* definition for this global in the summary
117 auto FirstDefForLinker = llvm::find_if(
118 GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
119 auto Linkage = Summary->linkage();
120 return !GlobalValue::isAvailableExternallyLinkage(Linkage);
122 // If \p GV is not the first definition, give up...
123 if ((*FirstDefForLinker)->modulePath() != ModulePath)
125 // If there is any strong definition anywhere, do not bother emitting this.
128 [](const std::unique_ptr<GlobalValueSummary> &Summary) {
129 auto Linkage = Summary->linkage();
130 return !GlobalValue::isAvailableExternallyLinkage(Linkage) &&
131 !GlobalValue::isWeakForLinker(Linkage);
137 static GlobalValue::LinkageTypes
138 ResolveODR(const ModuleSummaryIndex &Index,
139 const FunctionImporter::ExportSetTy &ExportList,
140 StringRef ModuleIdentifier, GlobalValue::GUID GUID,
141 const GlobalValueSummary &GV) {
142 auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) {
143 return GVSummaryList.size() > 1;
146 auto OriginalLinkage = GV.linkage();
147 switch (OriginalLinkage) {
148 case GlobalValue::ExternalLinkage:
149 case GlobalValue::AvailableExternallyLinkage:
150 case GlobalValue::AppendingLinkage:
151 case GlobalValue::InternalLinkage:
152 case GlobalValue::PrivateLinkage:
153 case GlobalValue::ExternalWeakLinkage:
154 case GlobalValue::CommonLinkage:
155 case GlobalValue::LinkOnceAnyLinkage:
156 case GlobalValue::WeakAnyLinkage:
158 case GlobalValue::LinkOnceODRLinkage:
159 case GlobalValue::WeakODRLinkage: {
160 auto &GVSummaryList = Index.findGlobalValueSummaryList(GUID)->second;
161 // We need to emit only one of these, the first module will keep
162 // it, but turned into a weak while the others will drop it.
163 if (!HasMultipleCopies(GVSummaryList)) {
164 // Exported LinkonceODR needs to be promoted to not be discarded
165 if (GlobalValue::isDiscardableIfUnused(OriginalLinkage) &&
166 ExportList.count(GUID))
167 return GlobalValue::WeakODRLinkage;
170 if (IsFirstDefinitionForLinker(GVSummaryList, Index, ModuleIdentifier))
171 return GlobalValue::WeakODRLinkage;
172 else if (isa<AliasSummary>(&GV))
173 // Alias can't be turned into available_externally.
174 return OriginalLinkage;
175 return GlobalValue::AvailableExternallyLinkage;
178 return OriginalLinkage;
181 /// Resolve LinkOnceODR and WeakODR.
183 /// We'd like to drop these function if they are no longer referenced in the
184 /// current module. However there is a chance that another module is still
185 /// referencing them because of the import. We make sure we always emit at least
187 static void ResolveODR(
188 const ModuleSummaryIndex &Index,
189 const FunctionImporter::ExportSetTy &ExportList,
190 const GVSummaryMapTy &DefinedGlobals, StringRef ModuleIdentifier,
191 std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR) {
192 if (Index.modulePaths().size() == 1)
193 // Nothing to do if we don't have multiple modules
196 // We won't optimize the globals that are referenced by an alias for now
197 // Ideally we should turn the alias into a global and duplicate the definition
199 DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias;
200 for (auto &GA : DefinedGlobals) {
201 if (auto AS = dyn_cast<AliasSummary>(GA.second))
202 GlobalInvolvedWithAlias.insert(&AS->getAliasee());
205 for (auto &GV : DefinedGlobals) {
206 if (GlobalInvolvedWithAlias.count(GV.second))
209 ResolveODR(Index, ExportList, ModuleIdentifier, GV.first, *GV.second);
210 if (NewLinkage != GV.second->linkage()) {
211 ResolvedODR[GV.first] = NewLinkage;
216 /// Fixup linkage, see ResolveODR() above.
219 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR) {
220 // Process functions and global now
221 for (auto &GV : TheModule) {
222 auto NewLinkage = ResolvedODR.find(GV.getGUID());
223 if (NewLinkage == ResolvedODR.end())
225 DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName() << "` from "
226 << GV.getLinkage() << " to " << NewLinkage->second << "\n");
227 GV.setLinkage(NewLinkage->second);
229 for (auto &GV : TheModule.globals()) {
230 auto NewLinkage = ResolvedODR.find(GV.getGUID());
231 if (NewLinkage == ResolvedODR.end())
233 DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName() << "` from "
234 << GV.getLinkage() << " to " << NewLinkage->second << "\n");
235 GV.setLinkage(NewLinkage->second);
237 for (auto &GV : TheModule.aliases()) {
238 auto NewLinkage = ResolvedODR.find(GV.getGUID());
239 if (NewLinkage == ResolvedODR.end())
241 DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName() << "` from "
242 << GV.getLinkage() << " to " << NewLinkage->second << "\n");
243 GV.setLinkage(NewLinkage->second);
247 static StringMap<MemoryBufferRef>
248 generateModuleMap(const std::vector<MemoryBufferRef> &Modules) {
249 StringMap<MemoryBufferRef> ModuleMap;
250 for (auto &ModuleBuffer : Modules) {
251 assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) ==
253 "Expect unique Buffer Identifier");
254 ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer;
259 /// Provide a "loader" for the FunctionImporter to access function from other
262 /// The context that will be used for importing.
263 LLVMContext &Context;
265 /// Map from Module identifier to MemoryBuffer. Used by clients like the
266 /// FunctionImported to request loading a Module.
267 StringMap<MemoryBufferRef> &ModuleMap;
270 ModuleLoader(LLVMContext &Context, StringMap<MemoryBufferRef> &ModuleMap)
271 : Context(Context), ModuleMap(ModuleMap) {}
273 /// Load a module on demand.
274 std::unique_ptr<Module> operator()(StringRef Identifier) {
275 return loadModuleFromBuffer(ModuleMap[Identifier], Context, /*Lazy*/ true);
279 static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) {
280 if (renameModuleForThinLTO(TheModule, Index))
281 report_fatal_error("renameModuleForThinLTO failed");
285 crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
286 StringMap<MemoryBufferRef> &ModuleMap,
287 const FunctionImporter::ImportMapTy &ImportList) {
288 ModuleLoader Loader(TheModule.getContext(), ModuleMap);
289 FunctionImporter Importer(Index, Loader);
290 Importer.importFunctions(TheModule, ImportList);
293 static void optimizeModule(Module &TheModule, TargetMachine &TM) {
294 // Populate the PassManager
295 PassManagerBuilder PMB;
296 PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple());
297 PMB.Inliner = createFunctionInliningPass();
298 // FIXME: should get it from the bitcode?
300 PMB.LoopVectorize = true;
301 PMB.SLPVectorize = true;
302 PMB.VerifyInput = true;
303 PMB.VerifyOutput = false;
305 legacy::PassManager PM;
307 // Add the TTI (required to inform the vectorizer about register size for
309 PM.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
312 PMB.populateThinLTOPassManager(PM);
317 // Create a DenseSet of GlobalValue to be used with the Internalizer.
318 static DenseSet<const GlobalValue *> computePreservedSymbolsForModule(
319 Module &TheModule, const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
320 const FunctionImporter::ExportSetTy &ExportList) {
321 DenseSet<const GlobalValue *> PreservedGV;
322 if (GUIDPreservedSymbols.empty())
323 // Early exit: internalize is disabled when there is nothing to preserve.
326 auto AddPreserveGV = [&](const GlobalValue &GV) {
327 auto GUID = GV.getGUID();
328 if (GUIDPreservedSymbols.count(GUID) || ExportList.count(GUID))
329 PreservedGV.insert(&GV);
332 for (auto &GV : TheModule)
334 for (auto &GV : TheModule.globals())
336 for (auto &GV : TheModule.aliases())
342 // Run internalization on \p TheModule
344 doInternalizeModule(Module &TheModule, const TargetMachine &TM,
345 const DenseSet<const GlobalValue *> &PreservedGV) {
346 if (PreservedGV.empty()) {
347 // Be friendly and don't nuke totally the module when the client didn't
348 // supply anything to preserve.
352 // Parse inline ASM and collect the list of symbols that are not defined in
353 // the current module.
354 StringSet<> AsmUndefinedRefs;
355 object::IRObjectFile::CollectAsmUndefinedRefs(
356 Triple(TheModule.getTargetTriple()), TheModule.getModuleInlineAsm(),
357 [&AsmUndefinedRefs](StringRef Name, object::BasicSymbolRef::Flags Flags) {
358 if (Flags & object::BasicSymbolRef::SF_Undefined)
359 AsmUndefinedRefs.insert(Name);
362 // Update the llvm.compiler_used globals to force preserving libcalls and
363 // symbols referenced from asm
364 UpdateCompilerUsed(TheModule, TM, AsmUndefinedRefs);
366 // Declare a callback for the internalize pass that will ask for every
367 // candidate GlobalValue if it can be internalized or not.
368 auto MustPreserveGV =
369 [&](const GlobalValue &GV) -> bool { return PreservedGV.count(&GV); };
371 llvm::internalizeModule(TheModule, MustPreserveGV);
374 // Convert the PreservedSymbols map from "Name" based to "GUID" based.
375 static DenseSet<GlobalValue::GUID>
376 computeGUIDPreservedSymbols(const StringSet<> &PreservedSymbols,
377 const Triple &TheTriple) {
378 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size());
379 for (auto &Entry : PreservedSymbols) {
380 StringRef Name = Entry.first();
381 if (TheTriple.isOSBinFormatMachO() && Name.size() > 0 && Name[0] == '_')
382 Name = Name.drop_front();
383 GUIDPreservedSymbols.insert(GlobalValue::getGUID(Name));
385 return GUIDPreservedSymbols;
388 std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
390 SmallVector<char, 128> OutputBuffer;
394 raw_svector_ostream OS(OutputBuffer);
395 legacy::PassManager PM;
397 // If the bitcode files contain ARC code and were compiled with optimization,
398 // the ObjCARCContractPass must be run, so do it unconditionally here.
399 PM.add(createObjCARCContractPass());
401 // Setup the codegen now.
402 if (TM.addPassesToEmitFile(PM, OS, TargetMachine::CGFT_ObjectFile,
403 /* DisableVerify */ true))
404 report_fatal_error("Failed to setup codegen");
406 // Run codegen now. resulting binary is in OutputBuffer.
409 return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
412 /// Manage caching for a single Module.
413 class ModuleCacheEntry {
414 SmallString<128> EntryPath;
417 // Create a cache entry. This compute a unique hash for the Module considering
418 // the current list of export/import, and offer an interface to query to
419 // access the content in the cache.
421 StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID,
422 const FunctionImporter::ImportMapTy &ImportList,
423 const FunctionImporter::ExportSetTy &ExportList,
424 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
425 const GVSummaryMapTy &DefinedFunctions,
426 const DenseSet<GlobalValue::GUID> &PreservedSymbols) {
427 if (CachePath.empty())
430 // Compute the unique hash for this entry
431 // This is based on the current compiler version, the module itself, the
432 // export list, the hash for every single module in the import list, the
433 // list of ResolvedODR for the module, and the list of preserved symbols.
437 // Start with the compiler revision
438 Hasher.update(LLVM_VERSION_STRING);
439 #ifdef HAVE_LLVM_REVISION
440 Hasher.update(LLVM_REVISION);
443 // Include the hash for the current module
444 auto ModHash = Index.getModuleHash(ModuleID);
445 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
446 for (auto F : ExportList)
447 // The export list can impact the internalization, be conservative here
448 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&F, sizeof(F)));
450 // Include the hash for every module we import functions from
451 for (auto &Entry : ImportList) {
452 auto ModHash = Index.getModuleHash(Entry.first());
453 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
456 // Include the hash for the resolved ODR.
457 for (auto &Entry : ResolvedODR) {
458 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&Entry.first,
459 sizeof(GlobalValue::GUID)));
460 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&Entry.second,
461 sizeof(GlobalValue::LinkageTypes)));
464 // Include the hash for the preserved symbols.
465 for (auto &Entry : PreservedSymbols) {
466 if (DefinedFunctions.count(Entry))
468 ArrayRef<uint8_t>((uint8_t *)&Entry, sizeof(GlobalValue::GUID)));
471 sys::path::append(EntryPath, CachePath, toHex(Hasher.result()));
474 // Access the path to this entry in the cache.
475 StringRef getEntryPath() { return EntryPath; }
477 // Try loading the buffer for this cache entry.
478 ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() {
479 if (EntryPath.empty())
480 return std::error_code();
481 return MemoryBuffer::getFile(EntryPath);
484 // Cache the Produced object file
485 void write(MemoryBufferRef OutputBuffer) {
486 if (EntryPath.empty())
489 // Write to a temporary to avoid race condition
490 SmallString<128> TempFilename;
493 sys::fs::createTemporaryFile("Thin", "tmp.o", TempFD, TempFilename);
495 errs() << "Error: " << EC.message() << "\n";
496 report_fatal_error("ThinLTO: Can't get a temporary file");
499 raw_fd_ostream OS(TempFD, /* ShouldClose */ true);
500 OS << OutputBuffer.getBuffer();
502 // Rename to final destination (hopefully race condition won't matter here)
503 sys::fs::rename(TempFilename, EntryPath);
507 static std::unique_ptr<MemoryBuffer> ProcessThinLTOModule(
508 Module &TheModule, const ModuleSummaryIndex &Index,
509 StringMap<MemoryBufferRef> &ModuleMap, TargetMachine &TM,
510 const FunctionImporter::ImportMapTy &ImportList,
511 const FunctionImporter::ExportSetTy &ExportList,
512 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
513 std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
514 ThinLTOCodeGenerator::CachingOptions CacheOptions, bool DisableCodeGen,
515 StringRef SaveTempsDir, unsigned count) {
517 // Save temps: after IPO.
518 saveTempBitcode(TheModule, SaveTempsDir, count, ".1.IPO.bc");
520 // Prepare for internalization by computing the set of symbols to preserve.
521 // We need to compute the list of symbols to preserve during internalization
522 // before doing any promotion because after renaming we won't (easily) match
523 // to the original name.
524 auto PreservedGV = computePreservedSymbolsForModule(
525 TheModule, GUIDPreservedSymbols, ExportList);
527 // "Benchmark"-like optimization: single-source case
528 bool SingleModule = (ModuleMap.size() == 1);
531 promoteModule(TheModule, Index);
533 // Resolve the LinkOnce/Weak ODR, trying to turn them into
534 // "available_externally" when possible.
535 // This is a compile-time optimization.
536 fixupODR(TheModule, ResolvedODR);
538 // Save temps: after promotion.
539 saveTempBitcode(TheModule, SaveTempsDir, count, ".2.promoted.bc");
543 doInternalizeModule(TheModule, TM, PreservedGV);
545 // Save internalized bitcode
546 saveTempBitcode(TheModule, SaveTempsDir, count, ".3.internalized.bc");
549 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
551 // Save temps: after cross-module import.
552 saveTempBitcode(TheModule, SaveTempsDir, count, ".4.imported.bc");
555 optimizeModule(TheModule, TM);
557 saveTempBitcode(TheModule, SaveTempsDir, count, ".5.opt.bc");
559 if (DisableCodeGen) {
560 // Configured to stop before CodeGen, serialize the bitcode and return.
561 SmallVector<char, 128> OutputBuffer;
563 raw_svector_ostream OS(OutputBuffer);
564 ModuleSummaryIndexBuilder IndexBuilder(&TheModule);
565 WriteBitcodeToFile(&TheModule, OS, true, &IndexBuilder.getIndex());
567 return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
570 return codegenModule(TheModule, TM);
573 // Initialize the TargetMachine builder for a given Triple
574 static void initTMBuilder(TargetMachineBuilder &TMBuilder,
575 const Triple &TheTriple) {
576 // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
577 // FIXME this looks pretty terrible...
578 if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
579 if (TheTriple.getArch() == llvm::Triple::x86_64)
580 TMBuilder.MCpu = "core2";
581 else if (TheTriple.getArch() == llvm::Triple::x86)
582 TMBuilder.MCpu = "yonah";
583 else if (TheTriple.getArch() == llvm::Triple::aarch64)
584 TMBuilder.MCpu = "cyclone";
586 TMBuilder.TheTriple = std::move(TheTriple);
589 } // end anonymous namespace
591 void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
592 MemoryBufferRef Buffer(Data, Identifier);
593 if (Modules.empty()) {
594 // First module added, so initialize the triple and some options
596 Triple TheTriple(getBitcodeTargetTriple(Buffer, Context));
597 initTMBuilder(TMBuilder, Triple(TheTriple));
602 assert(TMBuilder.TheTriple.str() ==
603 getBitcodeTargetTriple(Buffer, Context) &&
604 "ThinLTO modules with different triple not supported");
607 Modules.push_back(Buffer);
610 void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) {
611 PreservedSymbols.insert(Name);
614 void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
615 // FIXME: At the moment, we don't take advantage of this extra information,
616 // we're conservatively considering cross-references as preserved.
617 // CrossReferencedSymbols.insert(Name);
618 PreservedSymbols.insert(Name);
621 // TargetMachine factory
622 std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
624 const Target *TheTarget =
625 TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
627 report_fatal_error("Can't load target for this Triple: " + ErrMsg);
630 // Use MAttr as the default set of features.
631 SubtargetFeatures Features(MAttr);
632 Features.getDefaultSubtargetFeatures(TheTriple);
633 std::string FeatureStr = Features.getString();
634 return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
635 TheTriple.str(), MCpu, FeatureStr, Options, RelocModel,
636 CodeModel::Default, CGOptLevel));
640 * Produce the combined summary index from all the bitcode files:
643 std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
644 std::unique_ptr<ModuleSummaryIndex> CombinedIndex;
645 uint64_t NextModuleId = 0;
646 for (auto &ModuleBuffer : Modules) {
647 ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
648 object::ModuleSummaryIndexObjectFile::create(ModuleBuffer,
650 if (std::error_code EC = ObjOrErr.getError()) {
652 errs() << "error: can't create ModuleSummaryIndexObjectFile for buffer: "
653 << EC.message() << "\n";
656 auto Index = (*ObjOrErr)->takeIndex();
658 CombinedIndex->mergeFrom(std::move(Index), ++NextModuleId);
660 CombinedIndex = std::move(Index);
663 return CombinedIndex;
667 * Perform promotion and renaming of exported internal functions.
669 void ThinLTOCodeGenerator::promote(Module &TheModule,
670 ModuleSummaryIndex &Index) {
671 auto ModuleCount = Index.modulePaths().size();
672 auto ModuleIdentifier = TheModule.getModuleIdentifier();
673 // Collect for each module the list of function it defines (GUID -> Summary).
674 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
675 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
677 // Generate import/export list
678 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
679 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
680 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
682 auto &ExportList = ExportLists[ModuleIdentifier];
684 // Resolve the LinkOnceODR, trying to turn them into "available_externally"
686 // This is a compile-time optimization.
687 // We use a std::map here to be able to have a defined ordering when
688 // producing a hash for the cache entry.
689 std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> ResolvedODR;
690 ResolveODR(Index, ExportList, ModuleToDefinedGVSummaries[ModuleIdentifier],
691 ModuleIdentifier, ResolvedODR);
692 fixupODR(TheModule, ResolvedODR);
694 promoteModule(TheModule, Index);
698 * Perform cross-module importing for the module identified by ModuleIdentifier.
700 void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
701 ModuleSummaryIndex &Index) {
702 auto ModuleMap = generateModuleMap(Modules);
703 auto ModuleCount = Index.modulePaths().size();
705 // Collect for each module the list of function it defines (GUID -> Summary).
706 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
707 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
709 // Generate import/export list
710 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
711 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
712 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
714 auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
716 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
720 * Perform internalization.
722 void ThinLTOCodeGenerator::internalize(Module &TheModule,
723 ModuleSummaryIndex &Index) {
724 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
725 auto ModuleCount = Index.modulePaths().size();
726 auto ModuleIdentifier = TheModule.getModuleIdentifier();
728 // Convert the preserved symbols set from string to GUID
729 auto GUIDPreservedSymbols =
730 computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
732 // Collect for each module the list of function it defines (GUID -> Summary).
733 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
734 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
736 // Generate import/export list
737 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
738 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
739 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
741 auto &ExportList = ExportLists[ModuleIdentifier];
744 auto PreservedGV = computePreservedSymbolsForModule(
745 TheModule, GUIDPreservedSymbols, ExportList);
746 doInternalizeModule(TheModule, *TMBuilder.create(), PreservedGV);
750 * Perform post-importing ThinLTO optimizations.
752 void ThinLTOCodeGenerator::optimize(Module &TheModule) {
753 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
756 optimizeModule(TheModule, *TMBuilder.create());
760 * Perform ThinLTO CodeGen.
762 std::unique_ptr<MemoryBuffer> ThinLTOCodeGenerator::codegen(Module &TheModule) {
763 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
764 return codegenModule(TheModule, *TMBuilder.create());
767 // Main entry point for the ThinLTO processing
768 void ThinLTOCodeGenerator::run() {
770 // Perform only parallel codegen and return.
772 assert(ProducedBinaries.empty() && "The generator should not be reused");
773 ProducedBinaries.resize(Modules.size());
775 for (auto &ModuleBuffer : Modules) {
776 Pool.async([&](int count) {
778 Context.setDiscardValueNames(LTODiscardValueNames);
781 auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
784 ProducedBinaries[count] = codegen(*TheModule);
791 // Sequential linking phase
792 auto Index = linkCombinedIndex();
794 // Save temps: index.
795 if (!SaveTempsDir.empty()) {
796 auto SaveTempPath = SaveTempsDir + "index.bc";
798 raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None);
800 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
801 " to save optimized bitcode\n");
802 WriteIndexToFile(*Index, OS);
805 // Prepare the resulting object vector
806 assert(ProducedBinaries.empty() && "The generator should not be reused");
807 ProducedBinaries.resize(Modules.size());
809 // Prepare the module map.
810 auto ModuleMap = generateModuleMap(Modules);
811 auto ModuleCount = Modules.size();
813 // Collect for each module the list of function it defines (GUID -> Summary).
814 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
815 Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
817 // Collect the import/export lists for all modules from the call-graph in the
819 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
820 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
821 ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
824 // Convert the preserved symbols set from string to GUID, this is needed for
825 // computing the caching hash and the internalization.
826 auto GUIDPreservedSymbols =
827 computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
829 // Parallel optimizer + codegen
831 ThreadPool Pool(ThreadCount);
833 for (auto &ModuleBuffer : Modules) {
834 Pool.async([&](int count) {
835 auto ModuleIdentifier = ModuleBuffer.getBufferIdentifier();
836 auto &ExportList = ExportLists[ModuleIdentifier];
838 auto &DefinedFunctions = ModuleToDefinedGVSummaries[ModuleIdentifier];
840 // Resolve ODR, this has to be done early because it impacts the caching
841 // We use a std::map here to be able to have a defined ordering when
842 // producing a hash for the cache entry.
843 std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> ResolvedODR;
844 ResolveODR(*Index, ExportList, DefinedFunctions, ModuleIdentifier,
847 // The module may be cached, this helps handling it.
848 ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
849 ImportLists[ModuleIdentifier], ExportList,
850 ResolvedODR, DefinedFunctions,
851 GUIDPreservedSymbols);
854 auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
855 DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss") << " '"
856 << CacheEntry.getEntryPath() << "' for buffer " << count
857 << " " << ModuleIdentifier << "\n");
861 ProducedBinaries[count] = std::move(ErrOrBuffer.get());
867 Context.setDiscardValueNames(LTODiscardValueNames);
868 Context.enableDebugTypeODRUniquing();
871 auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
873 // Save temps: original file.
874 saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
876 auto &ImportList = ImportLists[ModuleIdentifier];
877 // Run the main process now, and generates a binary
878 auto OutputBuffer = ProcessThinLTOModule(
879 *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
880 ExportList, GUIDPreservedSymbols, ResolvedODR, CacheOptions,
881 DisableCodeGen, SaveTempsDir, count);
883 CacheEntry.write(*OutputBuffer);
884 ProducedBinaries[count] = std::move(OutputBuffer);
890 CachePruning(CacheOptions.Path)
891 .setPruningInterval(CacheOptions.PruningInterval)
892 .setEntryExpiration(CacheOptions.Expiration)
893 .setMaxSize(CacheOptions.MaxPercentageOfAvailableSpace)
896 // If statistics were requested, print them out now.
897 if (llvm::AreStatisticsEnabled())
898 llvm::PrintStatistics();