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 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
141 StringRef ModuleIdentifier, GlobalValue::GUID GUID,
142 const GlobalValueSummary &GV) {
143 auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) {
144 return GVSummaryList.size() > 1;
147 auto OriginalLinkage = GV.linkage();
148 switch (OriginalLinkage) {
149 case GlobalValue::ExternalLinkage:
150 case GlobalValue::AvailableExternallyLinkage:
151 case GlobalValue::AppendingLinkage:
152 case GlobalValue::InternalLinkage:
153 case GlobalValue::PrivateLinkage:
154 case GlobalValue::ExternalWeakLinkage:
155 case GlobalValue::CommonLinkage:
156 case GlobalValue::LinkOnceAnyLinkage:
157 case GlobalValue::WeakAnyLinkage:
159 case GlobalValue::LinkOnceODRLinkage:
160 case GlobalValue::WeakODRLinkage: {
161 auto &GVSummaryList = Index.findGlobalValueSummaryList(GUID)->second;
162 // We need to emit only one of these, the first module will keep
163 // it, but turned into a weak while the others will drop it.
164 if (!HasMultipleCopies(GVSummaryList)) {
165 // Exported LinkonceODR needs to be promoted to not be discarded
166 if (GlobalValue::isDiscardableIfUnused(OriginalLinkage) &&
167 (ExportList.count(GUID) || GUIDPreservedSymbols.count(GUID)))
168 return GlobalValue::WeakODRLinkage;
171 if (IsFirstDefinitionForLinker(GVSummaryList, Index, ModuleIdentifier))
172 return GlobalValue::WeakODRLinkage;
173 else if (isa<AliasSummary>(&GV))
174 // Alias can't be turned into available_externally.
175 return OriginalLinkage;
176 return GlobalValue::AvailableExternallyLinkage;
179 return OriginalLinkage;
182 /// Resolve LinkOnceODR and WeakODR.
184 /// We'd like to drop these function if they are no longer referenced in the
185 /// current module. However there is a chance that another module is still
186 /// referencing them because of the import. We make sure we always emit at least
188 static void ResolveODR(
189 const ModuleSummaryIndex &Index,
190 const FunctionImporter::ExportSetTy &ExportList,
191 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
192 const GVSummaryMapTy &DefinedGlobals, StringRef ModuleIdentifier,
193 std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR) {
194 if (Index.modulePaths().size() == 1)
195 // Nothing to do if we don't have multiple modules
198 // We won't optimize the globals that are referenced by an alias for now
199 // Ideally we should turn the alias into a global and duplicate the definition
201 DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias;
202 for (auto &GA : DefinedGlobals) {
203 if (auto AS = dyn_cast<AliasSummary>(GA.second))
204 GlobalInvolvedWithAlias.insert(&AS->getAliasee());
207 for (auto &GV : DefinedGlobals) {
208 if (GlobalInvolvedWithAlias.count(GV.second))
211 ResolveODR(Index, ExportList, GUIDPreservedSymbols, ModuleIdentifier, GV.first, *GV.second);
212 if (NewLinkage != GV.second->linkage()) {
213 ResolvedODR[GV.first] = NewLinkage;
218 /// Fixup linkage, see ResolveODR() above.
221 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR) {
222 // Process functions and global now
223 for (auto &GV : TheModule) {
224 auto NewLinkage = ResolvedODR.find(GV.getGUID());
225 if (NewLinkage == ResolvedODR.end())
227 DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName() << "` from "
228 << GV.getLinkage() << " to " << NewLinkage->second << "\n");
229 GV.setLinkage(NewLinkage->second);
231 for (auto &GV : TheModule.globals()) {
232 auto NewLinkage = ResolvedODR.find(GV.getGUID());
233 if (NewLinkage == ResolvedODR.end())
235 DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName() << "` from "
236 << GV.getLinkage() << " to " << NewLinkage->second << "\n");
237 GV.setLinkage(NewLinkage->second);
239 for (auto &GV : TheModule.aliases()) {
240 auto NewLinkage = ResolvedODR.find(GV.getGUID());
241 if (NewLinkage == ResolvedODR.end())
243 DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName() << "` from "
244 << GV.getLinkage() << " to " << NewLinkage->second << "\n");
245 GV.setLinkage(NewLinkage->second);
249 static StringMap<MemoryBufferRef>
250 generateModuleMap(const std::vector<MemoryBufferRef> &Modules) {
251 StringMap<MemoryBufferRef> ModuleMap;
252 for (auto &ModuleBuffer : Modules) {
253 assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) ==
255 "Expect unique Buffer Identifier");
256 ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer;
261 /// Provide a "loader" for the FunctionImporter to access function from other
264 /// The context that will be used for importing.
265 LLVMContext &Context;
267 /// Map from Module identifier to MemoryBuffer. Used by clients like the
268 /// FunctionImported to request loading a Module.
269 StringMap<MemoryBufferRef> &ModuleMap;
272 ModuleLoader(LLVMContext &Context, StringMap<MemoryBufferRef> &ModuleMap)
273 : Context(Context), ModuleMap(ModuleMap) {}
275 /// Load a module on demand.
276 std::unique_ptr<Module> operator()(StringRef Identifier) {
277 return loadModuleFromBuffer(ModuleMap[Identifier], Context, /*Lazy*/ true);
281 static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) {
282 if (renameModuleForThinLTO(TheModule, Index))
283 report_fatal_error("renameModuleForThinLTO failed");
287 crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
288 StringMap<MemoryBufferRef> &ModuleMap,
289 const FunctionImporter::ImportMapTy &ImportList) {
290 ModuleLoader Loader(TheModule.getContext(), ModuleMap);
291 FunctionImporter Importer(Index, Loader);
292 Importer.importFunctions(TheModule, ImportList);
295 static void optimizeModule(Module &TheModule, TargetMachine &TM) {
296 // Populate the PassManager
297 PassManagerBuilder PMB;
298 PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple());
299 PMB.Inliner = createFunctionInliningPass();
300 // FIXME: should get it from the bitcode?
302 PMB.LoopVectorize = true;
303 PMB.SLPVectorize = true;
304 PMB.VerifyInput = true;
305 PMB.VerifyOutput = false;
307 legacy::PassManager PM;
309 // Add the TTI (required to inform the vectorizer about register size for
311 PM.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
314 PMB.populateThinLTOPassManager(PM);
319 // Create a DenseSet of GlobalValue to be used with the Internalizer.
320 static DenseSet<const GlobalValue *> computePreservedSymbolsForModule(
321 Module &TheModule, const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
322 const FunctionImporter::ExportSetTy &ExportList) {
323 DenseSet<const GlobalValue *> PreservedGV;
324 if (GUIDPreservedSymbols.empty())
325 // Early exit: internalize is disabled when there is nothing to preserve.
328 auto AddPreserveGV = [&](const GlobalValue &GV) {
329 auto GUID = GV.getGUID();
330 if (GUIDPreservedSymbols.count(GUID) || ExportList.count(GUID))
331 PreservedGV.insert(&GV);
334 for (auto &GV : TheModule)
336 for (auto &GV : TheModule.globals())
338 for (auto &GV : TheModule.aliases())
344 // Run internalization on \p TheModule
346 doInternalizeModule(Module &TheModule, const TargetMachine &TM,
347 const DenseSet<const GlobalValue *> &PreservedGV) {
348 if (PreservedGV.empty()) {
349 // Be friendly and don't nuke totally the module when the client didn't
350 // supply anything to preserve.
354 // Parse inline ASM and collect the list of symbols that are not defined in
355 // the current module.
356 StringSet<> AsmUndefinedRefs;
357 object::IRObjectFile::CollectAsmUndefinedRefs(
358 Triple(TheModule.getTargetTriple()), TheModule.getModuleInlineAsm(),
359 [&AsmUndefinedRefs](StringRef Name, object::BasicSymbolRef::Flags Flags) {
360 if (Flags & object::BasicSymbolRef::SF_Undefined)
361 AsmUndefinedRefs.insert(Name);
364 // Update the llvm.compiler_used globals to force preserving libcalls and
365 // symbols referenced from asm
366 UpdateCompilerUsed(TheModule, TM, AsmUndefinedRefs);
368 // Declare a callback for the internalize pass that will ask for every
369 // candidate GlobalValue if it can be internalized or not.
370 auto MustPreserveGV =
371 [&](const GlobalValue &GV) -> bool { return PreservedGV.count(&GV); };
373 llvm::internalizeModule(TheModule, MustPreserveGV);
376 // Convert the PreservedSymbols map from "Name" based to "GUID" based.
377 static DenseSet<GlobalValue::GUID>
378 computeGUIDPreservedSymbols(const StringSet<> &PreservedSymbols,
379 const Triple &TheTriple) {
380 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size());
381 for (auto &Entry : PreservedSymbols) {
382 StringRef Name = Entry.first();
383 if (TheTriple.isOSBinFormatMachO() && Name.size() > 0 && Name[0] == '_')
384 Name = Name.drop_front();
385 GUIDPreservedSymbols.insert(GlobalValue::getGUID(Name));
387 return GUIDPreservedSymbols;
390 std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
392 SmallVector<char, 128> OutputBuffer;
396 raw_svector_ostream OS(OutputBuffer);
397 legacy::PassManager PM;
399 // If the bitcode files contain ARC code and were compiled with optimization,
400 // the ObjCARCContractPass must be run, so do it unconditionally here.
401 PM.add(createObjCARCContractPass());
403 // Setup the codegen now.
404 if (TM.addPassesToEmitFile(PM, OS, TargetMachine::CGFT_ObjectFile,
405 /* DisableVerify */ true))
406 report_fatal_error("Failed to setup codegen");
408 // Run codegen now. resulting binary is in OutputBuffer.
411 return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
414 /// Manage caching for a single Module.
415 class ModuleCacheEntry {
416 SmallString<128> EntryPath;
419 // Create a cache entry. This compute a unique hash for the Module considering
420 // the current list of export/import, and offer an interface to query to
421 // access the content in the cache.
423 StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID,
424 const FunctionImporter::ImportMapTy &ImportList,
425 const FunctionImporter::ExportSetTy &ExportList,
426 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
427 const GVSummaryMapTy &DefinedFunctions,
428 const DenseSet<GlobalValue::GUID> &PreservedSymbols) {
429 if (CachePath.empty())
432 // Compute the unique hash for this entry
433 // This is based on the current compiler version, the module itself, the
434 // export list, the hash for every single module in the import list, the
435 // list of ResolvedODR for the module, and the list of preserved symbols.
439 // Start with the compiler revision
440 Hasher.update(LLVM_VERSION_STRING);
441 #ifdef HAVE_LLVM_REVISION
442 Hasher.update(LLVM_REVISION);
445 // Include the hash for the current module
446 auto ModHash = Index.getModuleHash(ModuleID);
447 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
448 for (auto F : ExportList)
449 // The export list can impact the internalization, be conservative here
450 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&F, sizeof(F)));
452 // Include the hash for every module we import functions from
453 for (auto &Entry : ImportList) {
454 auto ModHash = Index.getModuleHash(Entry.first());
455 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
458 // Include the hash for the resolved ODR.
459 for (auto &Entry : ResolvedODR) {
460 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
461 sizeof(GlobalValue::GUID)));
462 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
463 sizeof(GlobalValue::LinkageTypes)));
466 // Include the hash for the preserved symbols.
467 for (auto &Entry : PreservedSymbols) {
468 if (DefinedFunctions.count(Entry))
470 ArrayRef<uint8_t>((const uint8_t *)&Entry, sizeof(GlobalValue::GUID)));
473 sys::path::append(EntryPath, CachePath, toHex(Hasher.result()));
476 // Access the path to this entry in the cache.
477 StringRef getEntryPath() { return EntryPath; }
479 // Try loading the buffer for this cache entry.
480 ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() {
481 if (EntryPath.empty())
482 return std::error_code();
483 return MemoryBuffer::getFile(EntryPath);
486 // Cache the Produced object file
487 void write(MemoryBufferRef OutputBuffer) {
488 if (EntryPath.empty())
491 // Write to a temporary to avoid race condition
492 SmallString<128> TempFilename;
495 sys::fs::createTemporaryFile("Thin", "tmp.o", TempFD, TempFilename);
497 errs() << "Error: " << EC.message() << "\n";
498 report_fatal_error("ThinLTO: Can't get a temporary file");
501 raw_fd_ostream OS(TempFD, /* ShouldClose */ true);
502 OS << OutputBuffer.getBuffer();
504 // Rename to final destination (hopefully race condition won't matter here)
505 sys::fs::rename(TempFilename, EntryPath);
509 static std::unique_ptr<MemoryBuffer> ProcessThinLTOModule(
510 Module &TheModule, const ModuleSummaryIndex &Index,
511 StringMap<MemoryBufferRef> &ModuleMap, TargetMachine &TM,
512 const FunctionImporter::ImportMapTy &ImportList,
513 const FunctionImporter::ExportSetTy &ExportList,
514 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
515 std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
516 ThinLTOCodeGenerator::CachingOptions CacheOptions, bool DisableCodeGen,
517 StringRef SaveTempsDir, unsigned count) {
519 // Prepare for internalization by computing the set of symbols to preserve.
520 // We need to compute the list of symbols to preserve during internalization
521 // before doing any promotion because after renaming we won't (easily) match
522 // to the original name.
523 auto PreservedGV = computePreservedSymbolsForModule(
524 TheModule, GUIDPreservedSymbols, ExportList);
526 // "Benchmark"-like optimization: single-source case
527 bool SingleModule = (ModuleMap.size() == 1);
530 promoteModule(TheModule, Index);
532 // Resolve the LinkOnce/Weak ODR, trying to turn them into
533 // "available_externally" when possible.
534 // This is a compile-time optimization.
535 fixupODR(TheModule, ResolvedODR);
537 // Save temps: after promotion.
538 saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
542 doInternalizeModule(TheModule, TM, PreservedGV);
544 // Save internalized bitcode
545 saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
548 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
550 // Save temps: after cross-module import.
551 saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
554 optimizeModule(TheModule, TM);
556 saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
558 if (DisableCodeGen) {
559 // Configured to stop before CodeGen, serialize the bitcode and return.
560 SmallVector<char, 128> OutputBuffer;
562 raw_svector_ostream OS(OutputBuffer);
563 ModuleSummaryIndexBuilder IndexBuilder(&TheModule);
564 WriteBitcodeToFile(&TheModule, OS, true, &IndexBuilder.getIndex());
566 return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
569 return codegenModule(TheModule, TM);
572 // Initialize the TargetMachine builder for a given Triple
573 static void initTMBuilder(TargetMachineBuilder &TMBuilder,
574 const Triple &TheTriple) {
575 // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
576 // FIXME this looks pretty terrible...
577 if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
578 if (TheTriple.getArch() == llvm::Triple::x86_64)
579 TMBuilder.MCpu = "core2";
580 else if (TheTriple.getArch() == llvm::Triple::x86)
581 TMBuilder.MCpu = "yonah";
582 else if (TheTriple.getArch() == llvm::Triple::aarch64)
583 TMBuilder.MCpu = "cyclone";
585 TMBuilder.TheTriple = std::move(TheTriple);
588 } // end anonymous namespace
590 void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
591 MemoryBufferRef Buffer(Data, Identifier);
592 if (Modules.empty()) {
593 // First module added, so initialize the triple and some options
595 Triple TheTriple(getBitcodeTargetTriple(Buffer, Context));
596 initTMBuilder(TMBuilder, Triple(TheTriple));
601 assert(TMBuilder.TheTriple.str() ==
602 getBitcodeTargetTriple(Buffer, Context) &&
603 "ThinLTO modules with different triple not supported");
606 Modules.push_back(Buffer);
609 void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) {
610 PreservedSymbols.insert(Name);
613 void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
614 // FIXME: At the moment, we don't take advantage of this extra information,
615 // we're conservatively considering cross-references as preserved.
616 // CrossReferencedSymbols.insert(Name);
617 PreservedSymbols.insert(Name);
620 // TargetMachine factory
621 std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
623 const Target *TheTarget =
624 TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
626 report_fatal_error("Can't load target for this Triple: " + ErrMsg);
629 // Use MAttr as the default set of features.
630 SubtargetFeatures Features(MAttr);
631 Features.getDefaultSubtargetFeatures(TheTriple);
632 std::string FeatureStr = Features.getString();
633 return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
634 TheTriple.str(), MCpu, FeatureStr, Options, RelocModel,
635 CodeModel::Default, CGOptLevel));
639 * Produce the combined summary index from all the bitcode files:
642 std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
643 std::unique_ptr<ModuleSummaryIndex> CombinedIndex;
644 uint64_t NextModuleId = 0;
645 for (auto &ModuleBuffer : Modules) {
646 ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
647 object::ModuleSummaryIndexObjectFile::create(ModuleBuffer,
649 if (std::error_code EC = ObjOrErr.getError()) {
651 errs() << "error: can't create ModuleSummaryIndexObjectFile for buffer: "
652 << EC.message() << "\n";
655 auto Index = (*ObjOrErr)->takeIndex();
657 CombinedIndex->mergeFrom(std::move(Index), ++NextModuleId);
659 CombinedIndex = std::move(Index);
662 return CombinedIndex;
666 * Perform promotion and renaming of exported internal functions.
668 void ThinLTOCodeGenerator::promote(Module &TheModule,
669 ModuleSummaryIndex &Index) {
670 auto ModuleCount = Index.modulePaths().size();
671 auto ModuleIdentifier = TheModule.getModuleIdentifier();
672 // Collect for each module the list of function it defines (GUID -> Summary).
673 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
674 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
676 // Generate import/export list
677 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
678 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
679 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
681 auto &ExportList = ExportLists[ModuleIdentifier];
683 // Convert the preserved symbols set from string to GUID
684 auto GUIDPreservedSymbols =
685 computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
687 // Resolve the LinkOnceODR, trying to turn them into "available_externally"
689 // This is a compile-time optimization.
690 // We use a std::map here to be able to have a defined ordering when
691 // producing a hash for the cache entry.
692 std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> ResolvedODR;
693 ResolveODR(Index, ExportList, GUIDPreservedSymbols, ModuleToDefinedGVSummaries[ModuleIdentifier],
694 ModuleIdentifier, ResolvedODR);
695 fixupODR(TheModule, ResolvedODR);
697 promoteModule(TheModule, Index);
701 * Perform cross-module importing for the module identified by ModuleIdentifier.
703 void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
704 ModuleSummaryIndex &Index) {
705 auto ModuleMap = generateModuleMap(Modules);
706 auto ModuleCount = Index.modulePaths().size();
708 // Collect for each module the list of function it defines (GUID -> Summary).
709 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
710 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
712 // Generate import/export list
713 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
714 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
715 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
717 auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
719 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
723 * Compute the list of summaries needed for importing into module.
725 void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
726 StringRef ModulePath, ModuleSummaryIndex &Index,
727 std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
728 auto ModuleCount = Index.modulePaths().size();
730 // Collect for each module the list of function it defines (GUID -> Summary).
731 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
732 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
734 // Generate import/export list
735 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
736 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
737 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
740 llvm::gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
742 ModuleToSummariesForIndex);
746 * Perform internalization.
748 void ThinLTOCodeGenerator::internalize(Module &TheModule,
749 ModuleSummaryIndex &Index) {
750 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
751 auto ModuleCount = Index.modulePaths().size();
752 auto ModuleIdentifier = TheModule.getModuleIdentifier();
754 // Convert the preserved symbols set from string to GUID
755 auto GUIDPreservedSymbols =
756 computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
758 // Collect for each module the list of function it defines (GUID -> Summary).
759 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
760 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
762 // Generate import/export list
763 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
764 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
765 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
767 auto &ExportList = ExportLists[ModuleIdentifier];
770 auto PreservedGV = computePreservedSymbolsForModule(
771 TheModule, GUIDPreservedSymbols, ExportList);
772 doInternalizeModule(TheModule, *TMBuilder.create(), PreservedGV);
776 * Perform post-importing ThinLTO optimizations.
778 void ThinLTOCodeGenerator::optimize(Module &TheModule) {
779 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
782 optimizeModule(TheModule, *TMBuilder.create());
786 * Perform ThinLTO CodeGen.
788 std::unique_ptr<MemoryBuffer> ThinLTOCodeGenerator::codegen(Module &TheModule) {
789 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
790 return codegenModule(TheModule, *TMBuilder.create());
793 // Main entry point for the ThinLTO processing
794 void ThinLTOCodeGenerator::run() {
796 // Perform only parallel codegen and return.
798 assert(ProducedBinaries.empty() && "The generator should not be reused");
799 ProducedBinaries.resize(Modules.size());
801 for (auto &ModuleBuffer : Modules) {
802 Pool.async([&](int count) {
804 Context.setDiscardValueNames(LTODiscardValueNames);
807 auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
810 ProducedBinaries[count] = codegen(*TheModule);
817 // Sequential linking phase
818 auto Index = linkCombinedIndex();
820 // Save temps: index.
821 if (!SaveTempsDir.empty()) {
822 auto SaveTempPath = SaveTempsDir + "index.bc";
824 raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None);
826 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
827 " to save optimized bitcode\n");
828 WriteIndexToFile(*Index, OS);
831 // Prepare the resulting object vector
832 assert(ProducedBinaries.empty() && "The generator should not be reused");
833 ProducedBinaries.resize(Modules.size());
835 // Prepare the module map.
836 auto ModuleMap = generateModuleMap(Modules);
837 auto ModuleCount = Modules.size();
839 // Collect for each module the list of function it defines (GUID -> Summary).
840 StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
841 Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
843 // Collect the import/export lists for all modules from the call-graph in the
845 StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
846 StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
847 ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
850 // Convert the preserved symbols set from string to GUID, this is needed for
851 // computing the caching hash and the internalization.
852 auto GUIDPreservedSymbols =
853 computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
855 // Parallel optimizer + codegen
857 ThreadPool Pool(ThreadCount);
859 for (auto &ModuleBuffer : Modules) {
860 Pool.async([&](int count) {
861 auto ModuleIdentifier = ModuleBuffer.getBufferIdentifier();
862 auto &ExportList = ExportLists[ModuleIdentifier];
864 auto &DefinedFunctions = ModuleToDefinedGVSummaries[ModuleIdentifier];
866 // Resolve ODR, this has to be done early because it impacts the caching
867 // We use a std::map here to be able to have a defined ordering when
868 // producing a hash for the cache entry.
869 std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> ResolvedODR;
870 ResolveODR(*Index, ExportList, GUIDPreservedSymbols, DefinedFunctions, ModuleIdentifier,
873 // The module may be cached, this helps handling it.
874 ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
875 ImportLists[ModuleIdentifier], ExportList,
876 ResolvedODR, DefinedFunctions,
877 GUIDPreservedSymbols);
880 auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
881 DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss") << " '"
882 << CacheEntry.getEntryPath() << "' for buffer " << count
883 << " " << ModuleIdentifier << "\n");
887 ProducedBinaries[count] = std::move(ErrOrBuffer.get());
893 Context.setDiscardValueNames(LTODiscardValueNames);
894 Context.enableDebugTypeODRUniquing();
897 auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
899 // Save temps: original file.
900 saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
902 auto &ImportList = ImportLists[ModuleIdentifier];
903 // Run the main process now, and generates a binary
904 auto OutputBuffer = ProcessThinLTOModule(
905 *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
906 ExportList, GUIDPreservedSymbols, ResolvedODR, CacheOptions,
907 DisableCodeGen, SaveTempsDir, count);
909 CacheEntry.write(*OutputBuffer);
910 ProducedBinaries[count] = std::move(OutputBuffer);
916 CachePruning(CacheOptions.Path)
917 .setPruningInterval(CacheOptions.PruningInterval)
918 .setEntryExpiration(CacheOptions.Expiration)
919 .setMaxSize(CacheOptions.MaxPercentageOfAvailableSpace)
922 // If statistics were requested, print them out now.
923 if (llvm::AreStatisticsEnabled())
924 llvm::PrintStatistics();