[clang] Add test for CWG190 "Layout-compatible POD-struct types" (#121668)
[llvm-project.git] / llvm / lib / LTO / ThinLTOCodeGenerator.cpp
blob4522f4adcebe685bda362a9da314fd7711771c86
1 //===-ThinLTOCodeGenerator.cpp - LLVM Link Time Optimizer -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the Thin Link Time Optimization library. This library is
10 // intended to be used by linker to optimize code at link time.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/LTO/legacy/ThinLTOCodeGenerator.h"
15 #include "llvm/Support/CommandLine.h"
17 #include "llvm/ADT/ScopeExit.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/Analysis/AliasAnalysis.h"
21 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
22 #include "llvm/Analysis/ProfileSummaryInfo.h"
23 #include "llvm/Analysis/TargetLibraryInfo.h"
24 #include "llvm/Bitcode/BitcodeReader.h"
25 #include "llvm/Bitcode/BitcodeWriter.h"
26 #include "llvm/Bitcode/BitcodeWriterPass.h"
27 #include "llvm/Config/llvm-config.h"
28 #include "llvm/IR/DebugInfo.h"
29 #include "llvm/IR/DiagnosticPrinter.h"
30 #include "llvm/IR/LLVMContext.h"
31 #include "llvm/IR/LLVMRemarkStreamer.h"
32 #include "llvm/IR/LegacyPassManager.h"
33 #include "llvm/IR/Mangler.h"
34 #include "llvm/IR/PassTimingInfo.h"
35 #include "llvm/IR/Verifier.h"
36 #include "llvm/IRReader/IRReader.h"
37 #include "llvm/LTO/LTO.h"
38 #include "llvm/MC/TargetRegistry.h"
39 #include "llvm/Object/IRObjectFile.h"
40 #include "llvm/Passes/PassBuilder.h"
41 #include "llvm/Passes/StandardInstrumentations.h"
42 #include "llvm/Remarks/HotnessThresholdParser.h"
43 #include "llvm/Support/CachePruning.h"
44 #include "llvm/Support/Debug.h"
45 #include "llvm/Support/Error.h"
46 #include "llvm/Support/FileSystem.h"
47 #include "llvm/Support/FormatVariadic.h"
48 #include "llvm/Support/Path.h"
49 #include "llvm/Support/SHA1.h"
50 #include "llvm/Support/SmallVectorMemoryBuffer.h"
51 #include "llvm/Support/ThreadPool.h"
52 #include "llvm/Support/Threading.h"
53 #include "llvm/Support/ToolOutputFile.h"
54 #include "llvm/Support/raw_ostream.h"
55 #include "llvm/Target/TargetMachine.h"
56 #include "llvm/TargetParser/SubtargetFeature.h"
57 #include "llvm/Transforms/IPO/FunctionAttrs.h"
58 #include "llvm/Transforms/IPO/FunctionImport.h"
59 #include "llvm/Transforms/IPO/Internalize.h"
60 #include "llvm/Transforms/IPO/WholeProgramDevirt.h"
61 #include "llvm/Transforms/ObjCARC.h"
62 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
64 #include <numeric>
66 #if !defined(_MSC_VER) && !defined(__MINGW32__)
67 #include <unistd.h>
68 #else
69 #include <io.h>
70 #endif
72 using namespace llvm;
73 using namespace ThinLTOCodeGeneratorImpl;
75 #define DEBUG_TYPE "thinlto"
77 namespace llvm {
78 // Flags -discard-value-names, defined in LTOCodeGenerator.cpp
79 extern cl::opt<bool> LTODiscardValueNames;
80 extern cl::opt<std::string> RemarksFilename;
81 extern cl::opt<std::string> RemarksPasses;
82 extern cl::opt<bool> RemarksWithHotness;
83 extern cl::opt<std::optional<uint64_t>, false, remarks::HotnessThresholdParser>
84 RemarksHotnessThreshold;
85 extern cl::opt<std::string> RemarksFormat;
88 // Default to using all available threads in the system, but using only one
89 // thred per core, as indicated by the usage of
90 // heavyweight_hardware_concurrency() below.
91 static cl::opt<int> ThreadCount("threads", cl::init(0));
93 // Simple helper to save temporary files for debug.
94 static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
95 unsigned count, StringRef Suffix) {
96 if (TempDir.empty())
97 return;
98 // User asked to save temps, let dump the bitcode file after import.
99 std::string SaveTempPath = (TempDir + llvm::Twine(count) + Suffix).str();
100 std::error_code EC;
101 raw_fd_ostream OS(SaveTempPath, EC, sys::fs::OF_None);
102 if (EC)
103 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
104 " to save optimized bitcode\n");
105 WriteBitcodeToFile(TheModule, OS, /* ShouldPreserveUseListOrder */ true);
108 static const GlobalValueSummary *
109 getFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList) {
110 // If there is any strong definition anywhere, get it.
111 auto StrongDefForLinker = llvm::find_if(
112 GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
113 auto Linkage = Summary->linkage();
114 return !GlobalValue::isAvailableExternallyLinkage(Linkage) &&
115 !GlobalValue::isWeakForLinker(Linkage);
117 if (StrongDefForLinker != GVSummaryList.end())
118 return StrongDefForLinker->get();
119 // Get the first *linker visible* definition for this global in the summary
120 // list.
121 auto FirstDefForLinker = llvm::find_if(
122 GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
123 auto Linkage = Summary->linkage();
124 return !GlobalValue::isAvailableExternallyLinkage(Linkage);
126 // Extern templates can be emitted as available_externally.
127 if (FirstDefForLinker == GVSummaryList.end())
128 return nullptr;
129 return FirstDefForLinker->get();
132 // Populate map of GUID to the prevailing copy for any multiply defined
133 // symbols. Currently assume first copy is prevailing, or any strong
134 // definition. Can be refined with Linker information in the future.
135 static void computePrevailingCopies(
136 const ModuleSummaryIndex &Index,
137 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> &PrevailingCopy) {
138 auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) {
139 return GVSummaryList.size() > 1;
142 for (auto &I : Index) {
143 if (HasMultipleCopies(I.second.SummaryList))
144 PrevailingCopy[I.first] =
145 getFirstDefinitionForLinker(I.second.SummaryList);
149 static StringMap<lto::InputFile *>
150 generateModuleMap(std::vector<std::unique_ptr<lto::InputFile>> &Modules) {
151 StringMap<lto::InputFile *> ModuleMap;
152 for (auto &M : Modules) {
153 LLVM_DEBUG(dbgs() << "Adding module " << M->getName() << " to ModuleMap\n");
154 assert(!ModuleMap.contains(M->getName()) &&
155 "Expect unique Buffer Identifier");
156 ModuleMap[M->getName()] = M.get();
158 return ModuleMap;
161 static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index,
162 bool ClearDSOLocalOnDeclarations) {
163 if (renameModuleForThinLTO(TheModule, Index, ClearDSOLocalOnDeclarations))
164 report_fatal_error("renameModuleForThinLTO failed");
167 namespace {
168 class ThinLTODiagnosticInfo : public DiagnosticInfo {
169 const Twine &Msg;
170 public:
171 ThinLTODiagnosticInfo(const Twine &DiagMsg,
172 DiagnosticSeverity Severity = DS_Error)
173 : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
174 void print(DiagnosticPrinter &DP) const override { DP << Msg; }
178 /// Verify the module and strip broken debug info.
179 static void verifyLoadedModule(Module &TheModule) {
180 bool BrokenDebugInfo = false;
181 if (verifyModule(TheModule, &dbgs(), &BrokenDebugInfo))
182 report_fatal_error("Broken module found, compilation aborted!");
183 if (BrokenDebugInfo) {
184 TheModule.getContext().diagnose(ThinLTODiagnosticInfo(
185 "Invalid debug info found, debug info will be stripped", DS_Warning));
186 StripDebugInfo(TheModule);
190 static std::unique_ptr<Module> loadModuleFromInput(lto::InputFile *Input,
191 LLVMContext &Context,
192 bool Lazy,
193 bool IsImporting) {
194 auto &Mod = Input->getSingleBitcodeModule();
195 SMDiagnostic Err;
196 Expected<std::unique_ptr<Module>> ModuleOrErr =
197 Lazy ? Mod.getLazyModule(Context,
198 /* ShouldLazyLoadMetadata */ true, IsImporting)
199 : Mod.parseModule(Context);
200 if (!ModuleOrErr) {
201 handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
202 SMDiagnostic Err = SMDiagnostic(Mod.getModuleIdentifier(),
203 SourceMgr::DK_Error, EIB.message());
204 Err.print("ThinLTO", errs());
206 report_fatal_error("Can't load module, abort.");
208 if (!Lazy)
209 verifyLoadedModule(*ModuleOrErr.get());
210 return std::move(*ModuleOrErr);
213 static void
214 crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
215 StringMap<lto::InputFile *> &ModuleMap,
216 const FunctionImporter::ImportMapTy &ImportList,
217 bool ClearDSOLocalOnDeclarations) {
218 auto Loader = [&](StringRef Identifier) {
219 auto &Input = ModuleMap[Identifier];
220 return loadModuleFromInput(Input, TheModule.getContext(),
221 /*Lazy=*/true, /*IsImporting*/ true);
224 FunctionImporter Importer(Index, Loader, ClearDSOLocalOnDeclarations);
225 Expected<bool> Result = Importer.importFunctions(TheModule, ImportList);
226 if (!Result) {
227 handleAllErrors(Result.takeError(), [&](ErrorInfoBase &EIB) {
228 SMDiagnostic Err = SMDiagnostic(TheModule.getModuleIdentifier(),
229 SourceMgr::DK_Error, EIB.message());
230 Err.print("ThinLTO", errs());
232 report_fatal_error("importFunctions failed");
234 // Verify again after cross-importing.
235 verifyLoadedModule(TheModule);
238 static void optimizeModule(Module &TheModule, TargetMachine &TM,
239 unsigned OptLevel, bool Freestanding,
240 bool DebugPassManager, ModuleSummaryIndex *Index) {
241 std::optional<PGOOptions> PGOOpt;
242 LoopAnalysisManager LAM;
243 FunctionAnalysisManager FAM;
244 CGSCCAnalysisManager CGAM;
245 ModuleAnalysisManager MAM;
247 PassInstrumentationCallbacks PIC;
248 StandardInstrumentations SI(TheModule.getContext(), DebugPassManager);
249 SI.registerCallbacks(PIC, &MAM);
250 PipelineTuningOptions PTO;
251 PTO.LoopVectorization = true;
252 PTO.SLPVectorization = true;
253 PassBuilder PB(&TM, PTO, PGOOpt, &PIC);
255 std::unique_ptr<TargetLibraryInfoImpl> TLII(
256 new TargetLibraryInfoImpl(Triple(TM.getTargetTriple())));
257 if (Freestanding)
258 TLII->disableAllFunctions();
259 FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
261 // Register all the basic analyses with the managers.
262 PB.registerModuleAnalyses(MAM);
263 PB.registerCGSCCAnalyses(CGAM);
264 PB.registerFunctionAnalyses(FAM);
265 PB.registerLoopAnalyses(LAM);
266 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
268 ModulePassManager MPM;
270 OptimizationLevel OL;
272 switch (OptLevel) {
273 default:
274 llvm_unreachable("Invalid optimization level");
275 case 0:
276 OL = OptimizationLevel::O0;
277 break;
278 case 1:
279 OL = OptimizationLevel::O1;
280 break;
281 case 2:
282 OL = OptimizationLevel::O2;
283 break;
284 case 3:
285 OL = OptimizationLevel::O3;
286 break;
289 MPM.addPass(PB.buildThinLTODefaultPipeline(OL, Index));
291 MPM.run(TheModule, MAM);
294 static void
295 addUsedSymbolToPreservedGUID(const lto::InputFile &File,
296 DenseSet<GlobalValue::GUID> &PreservedGUID) {
297 for (const auto &Sym : File.symbols()) {
298 if (Sym.isUsed())
299 PreservedGUID.insert(GlobalValue::getGUID(Sym.getIRName()));
303 // Convert the PreservedSymbols map from "Name" based to "GUID" based.
304 static void computeGUIDPreservedSymbols(const lto::InputFile &File,
305 const StringSet<> &PreservedSymbols,
306 const Triple &TheTriple,
307 DenseSet<GlobalValue::GUID> &GUIDs) {
308 // Iterate the symbols in the input file and if the input has preserved symbol
309 // compute the GUID for the symbol.
310 for (const auto &Sym : File.symbols()) {
311 if (PreservedSymbols.count(Sym.getName()) && !Sym.getIRName().empty())
312 GUIDs.insert(GlobalValue::getGUID(GlobalValue::getGlobalIdentifier(
313 Sym.getIRName(), GlobalValue::ExternalLinkage, "")));
317 static DenseSet<GlobalValue::GUID>
318 computeGUIDPreservedSymbols(const lto::InputFile &File,
319 const StringSet<> &PreservedSymbols,
320 const Triple &TheTriple) {
321 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size());
322 computeGUIDPreservedSymbols(File, PreservedSymbols, TheTriple,
323 GUIDPreservedSymbols);
324 return GUIDPreservedSymbols;
327 static std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
328 TargetMachine &TM) {
329 SmallVector<char, 128> OutputBuffer;
331 // CodeGen
333 raw_svector_ostream OS(OutputBuffer);
334 legacy::PassManager PM;
336 // Setup the codegen now.
337 if (TM.addPassesToEmitFile(PM, OS, nullptr, CodeGenFileType::ObjectFile,
338 /* DisableVerify */ true))
339 report_fatal_error("Failed to setup codegen");
341 // Run codegen now. resulting binary is in OutputBuffer.
342 PM.run(TheModule);
344 return std::make_unique<SmallVectorMemoryBuffer>(
345 std::move(OutputBuffer), /*RequiresNullTerminator=*/false);
348 namespace {
349 /// Manage caching for a single Module.
350 class ModuleCacheEntry {
351 SmallString<128> EntryPath;
353 public:
354 // Create a cache entry. This compute a unique hash for the Module considering
355 // the current list of export/import, and offer an interface to query to
356 // access the content in the cache.
357 ModuleCacheEntry(
358 StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID,
359 const FunctionImporter::ImportMapTy &ImportList,
360 const FunctionImporter::ExportSetTy &ExportList,
361 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
362 const GVSummaryMapTy &DefinedGVSummaries, unsigned OptLevel,
363 bool Freestanding, const TargetMachineBuilder &TMBuilder) {
364 if (CachePath.empty())
365 return;
367 if (!Index.modulePaths().count(ModuleID))
368 // The module does not have an entry, it can't have a hash at all
369 return;
371 if (all_of(Index.getModuleHash(ModuleID),
372 [](uint32_t V) { return V == 0; }))
373 // No hash entry, no caching!
374 return;
376 llvm::lto::Config Conf;
377 Conf.OptLevel = OptLevel;
378 Conf.Options = TMBuilder.Options;
379 Conf.CPU = TMBuilder.MCpu;
380 Conf.MAttrs.push_back(TMBuilder.MAttr);
381 Conf.RelocModel = TMBuilder.RelocModel;
382 Conf.CGOptLevel = TMBuilder.CGOptLevel;
383 Conf.Freestanding = Freestanding;
384 std::string Key =
385 computeLTOCacheKey(Conf, Index, ModuleID, ImportList, ExportList,
386 ResolvedODR, DefinedGVSummaries);
388 // This choice of file name allows the cache to be pruned (see pruneCache()
389 // in include/llvm/Support/CachePruning.h).
390 sys::path::append(EntryPath, CachePath, Twine("llvmcache-", Key));
393 // Access the path to this entry in the cache.
394 StringRef getEntryPath() { return EntryPath; }
396 // Try loading the buffer for this cache entry.
397 ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() {
398 if (EntryPath.empty())
399 return std::error_code();
400 SmallString<64> ResultPath;
401 Expected<sys::fs::file_t> FDOrErr = sys::fs::openNativeFileForRead(
402 Twine(EntryPath), sys::fs::OF_UpdateAtime, &ResultPath);
403 if (!FDOrErr)
404 return errorToErrorCode(FDOrErr.takeError());
405 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getOpenFile(
406 *FDOrErr, EntryPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false);
407 sys::fs::closeFile(*FDOrErr);
408 return MBOrErr;
411 // Cache the Produced object file
412 void write(const MemoryBuffer &OutputBuffer) {
413 if (EntryPath.empty())
414 return;
416 if (auto Err = llvm::writeToOutput(
417 EntryPath, [&OutputBuffer](llvm::raw_ostream &OS) -> llvm::Error {
418 OS << OutputBuffer.getBuffer();
419 return llvm::Error::success();
421 report_fatal_error(llvm::formatv("ThinLTO: Can't write file {0}: {1}",
422 EntryPath,
423 toString(std::move(Err)).c_str()));
426 } // end anonymous namespace
428 static std::unique_ptr<MemoryBuffer>
429 ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
430 StringMap<lto::InputFile *> &ModuleMap, TargetMachine &TM,
431 const FunctionImporter::ImportMapTy &ImportList,
432 const FunctionImporter::ExportSetTy &ExportList,
433 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
434 const GVSummaryMapTy &DefinedGlobals,
435 const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
436 bool DisableCodeGen, StringRef SaveTempsDir,
437 bool Freestanding, unsigned OptLevel, unsigned count,
438 bool DebugPassManager) {
439 // "Benchmark"-like optimization: single-source case
440 bool SingleModule = (ModuleMap.size() == 1);
442 // When linking an ELF shared object, dso_local should be dropped. We
443 // conservatively do this for -fpic.
444 bool ClearDSOLocalOnDeclarations =
445 TM.getTargetTriple().isOSBinFormatELF() &&
446 TM.getRelocationModel() != Reloc::Static &&
447 TheModule.getPIELevel() == PIELevel::Default;
449 if (!SingleModule) {
450 promoteModule(TheModule, Index, ClearDSOLocalOnDeclarations);
452 // Apply summary-based prevailing-symbol resolution decisions.
453 thinLTOFinalizeInModule(TheModule, DefinedGlobals, /*PropagateAttrs=*/true);
455 // Save temps: after promotion.
456 saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
459 // Be friendly and don't nuke totally the module when the client didn't
460 // supply anything to preserve.
461 if (!ExportList.empty() || !GUIDPreservedSymbols.empty()) {
462 // Apply summary-based internalization decisions.
463 thinLTOInternalizeModule(TheModule, DefinedGlobals);
466 // Save internalized bitcode
467 saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
469 if (!SingleModule)
470 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList,
471 ClearDSOLocalOnDeclarations);
473 // Do this after any importing so that imported code is updated.
474 // See comment at call to updateVCallVisibilityInIndex() for why
475 // WholeProgramVisibilityEnabledInLTO is false.
476 updatePublicTypeTestCalls(TheModule,
477 /* WholeProgramVisibilityEnabledInLTO */ false);
479 // Save temps: after cross-module import.
480 saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
482 optimizeModule(TheModule, TM, OptLevel, Freestanding, DebugPassManager,
483 &Index);
485 saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
487 if (DisableCodeGen) {
488 // Configured to stop before CodeGen, serialize the bitcode and return.
489 SmallVector<char, 128> OutputBuffer;
491 raw_svector_ostream OS(OutputBuffer);
492 ProfileSummaryInfo PSI(TheModule);
493 auto Index = buildModuleSummaryIndex(TheModule, nullptr, &PSI);
494 WriteBitcodeToFile(TheModule, OS, true, &Index);
496 return std::make_unique<SmallVectorMemoryBuffer>(
497 std::move(OutputBuffer), /*RequiresNullTerminator=*/false);
500 return codegenModule(TheModule, TM);
503 /// Resolve prevailing symbols. Record resolutions in the \p ResolvedODR map
504 /// for caching, and in the \p Index for application during the ThinLTO
505 /// backends. This is needed for correctness for exported symbols (ensure
506 /// at least one copy kept) and a compile-time optimization (to drop duplicate
507 /// copies when possible).
508 static void resolvePrevailingInIndex(
509 ModuleSummaryIndex &Index,
510 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>>
511 &ResolvedODR,
512 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
513 const DenseMap<GlobalValue::GUID, const GlobalValueSummary *>
514 &PrevailingCopy) {
516 auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
517 const auto &Prevailing = PrevailingCopy.find(GUID);
518 // Not in map means that there was only one copy, which must be prevailing.
519 if (Prevailing == PrevailingCopy.end())
520 return true;
521 return Prevailing->second == S;
524 auto recordNewLinkage = [&](StringRef ModuleIdentifier,
525 GlobalValue::GUID GUID,
526 GlobalValue::LinkageTypes NewLinkage) {
527 ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
530 // TODO Conf.VisibilityScheme can be lto::Config::ELF for ELF.
531 lto::Config Conf;
532 thinLTOResolvePrevailingInIndex(Conf, Index, isPrevailing, recordNewLinkage,
533 GUIDPreservedSymbols);
536 // Initialize the TargetMachine builder for a given Triple
537 static void initTMBuilder(TargetMachineBuilder &TMBuilder,
538 const Triple &TheTriple) {
539 if (TMBuilder.MCpu.empty())
540 TMBuilder.MCpu = lto::getThinLTODefaultCPU(TheTriple);
541 TMBuilder.TheTriple = std::move(TheTriple);
544 void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
545 MemoryBufferRef Buffer(Data, Identifier);
547 auto InputOrError = lto::InputFile::create(Buffer);
548 if (!InputOrError)
549 report_fatal_error(Twine("ThinLTO cannot create input file: ") +
550 toString(InputOrError.takeError()));
552 auto TripleStr = (*InputOrError)->getTargetTriple();
553 Triple TheTriple(TripleStr);
555 if (Modules.empty())
556 initTMBuilder(TMBuilder, Triple(TheTriple));
557 else if (TMBuilder.TheTriple != TheTriple) {
558 if (!TMBuilder.TheTriple.isCompatibleWith(TheTriple))
559 report_fatal_error("ThinLTO modules with incompatible triples not "
560 "supported");
561 initTMBuilder(TMBuilder, Triple(TMBuilder.TheTriple.merge(TheTriple)));
564 Modules.emplace_back(std::move(*InputOrError));
567 void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) {
568 PreservedSymbols.insert(Name);
571 void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
572 // FIXME: At the moment, we don't take advantage of this extra information,
573 // we're conservatively considering cross-references as preserved.
574 // CrossReferencedSymbols.insert(Name);
575 PreservedSymbols.insert(Name);
578 // TargetMachine factory
579 std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
580 std::string ErrMsg;
581 const Target *TheTarget =
582 TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
583 if (!TheTarget) {
584 report_fatal_error(Twine("Can't load target for this Triple: ") + ErrMsg);
587 // Use MAttr as the default set of features.
588 SubtargetFeatures Features(MAttr);
589 Features.getDefaultSubtargetFeatures(TheTriple);
590 std::string FeatureStr = Features.getString();
592 std::unique_ptr<TargetMachine> TM(
593 TheTarget->createTargetMachine(TheTriple.str(), MCpu, FeatureStr, Options,
594 RelocModel, std::nullopt, CGOptLevel));
595 assert(TM && "Cannot create target machine");
597 return TM;
601 * Produce the combined summary index from all the bitcode files:
602 * "thin-link".
604 std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
605 std::unique_ptr<ModuleSummaryIndex> CombinedIndex =
606 std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
607 for (auto &Mod : Modules) {
608 auto &M = Mod->getSingleBitcodeModule();
609 if (Error Err = M.readSummary(*CombinedIndex, Mod->getName())) {
610 // FIXME diagnose
611 logAllUnhandledErrors(
612 std::move(Err), errs(),
613 "error: can't create module summary index for buffer: ");
614 return nullptr;
617 return CombinedIndex;
620 namespace {
621 struct IsExported {
622 const DenseMap<StringRef, FunctionImporter::ExportSetTy> &ExportLists;
623 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols;
625 IsExported(
626 const DenseMap<StringRef, FunctionImporter::ExportSetTy> &ExportLists,
627 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols)
628 : ExportLists(ExportLists), GUIDPreservedSymbols(GUIDPreservedSymbols) {}
630 bool operator()(StringRef ModuleIdentifier, ValueInfo VI) const {
631 const auto &ExportList = ExportLists.find(ModuleIdentifier);
632 return (ExportList != ExportLists.end() && ExportList->second.count(VI)) ||
633 GUIDPreservedSymbols.count(VI.getGUID());
637 struct IsPrevailing {
638 const DenseMap<GlobalValue::GUID, const GlobalValueSummary *> &PrevailingCopy;
639 IsPrevailing(const DenseMap<GlobalValue::GUID, const GlobalValueSummary *>
640 &PrevailingCopy)
641 : PrevailingCopy(PrevailingCopy) {}
643 bool operator()(GlobalValue::GUID GUID, const GlobalValueSummary *S) const {
644 const auto &Prevailing = PrevailingCopy.find(GUID);
645 // Not in map means that there was only one copy, which must be prevailing.
646 if (Prevailing == PrevailingCopy.end())
647 return true;
648 return Prevailing->second == S;
651 } // namespace
653 static void computeDeadSymbolsInIndex(
654 ModuleSummaryIndex &Index,
655 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
656 // We have no symbols resolution available. And can't do any better now in the
657 // case where the prevailing symbol is in a native object. It can be refined
658 // with linker information in the future.
659 auto isPrevailing = [&](GlobalValue::GUID G) {
660 return PrevailingType::Unknown;
662 computeDeadSymbolsWithConstProp(Index, GUIDPreservedSymbols, isPrevailing,
663 /* ImportEnabled = */ true);
667 * Perform promotion and renaming of exported internal functions.
668 * Index is updated to reflect linkage changes from weak resolution.
670 void ThinLTOCodeGenerator::promote(Module &TheModule, ModuleSummaryIndex &Index,
671 const lto::InputFile &File) {
672 auto ModuleCount = Index.modulePaths().size();
673 auto ModuleIdentifier = TheModule.getModuleIdentifier();
675 // Collect for each module the list of function it defines (GUID -> Summary).
676 DenseMap<StringRef, GVSummaryMapTy> ModuleToDefinedGVSummaries;
677 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
679 // Convert the preserved symbols set from string to GUID
680 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
681 File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
683 // Add used symbol to the preserved symbols.
684 addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
686 // Compute "dead" symbols, we don't want to import/export these!
687 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
689 // Compute prevailing symbols
690 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
691 computePrevailingCopies(Index, PrevailingCopy);
693 // Generate import/export list
694 FunctionImporter::ImportListsTy ImportLists(ModuleCount);
695 DenseMap<StringRef, FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
696 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries,
697 IsPrevailing(PrevailingCopy), ImportLists,
698 ExportLists);
700 // Resolve prevailing symbols
701 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
702 resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols,
703 PrevailingCopy);
705 thinLTOFinalizeInModule(TheModule,
706 ModuleToDefinedGVSummaries[ModuleIdentifier],
707 /*PropagateAttrs=*/false);
709 // Promote the exported values in the index, so that they are promoted
710 // in the module.
711 thinLTOInternalizeAndPromoteInIndex(
712 Index, IsExported(ExportLists, GUIDPreservedSymbols),
713 IsPrevailing(PrevailingCopy));
715 // FIXME Set ClearDSOLocalOnDeclarations.
716 promoteModule(TheModule, Index, /*ClearDSOLocalOnDeclarations=*/false);
720 * Perform cross-module importing for the module identified by ModuleIdentifier.
722 void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
723 ModuleSummaryIndex &Index,
724 const lto::InputFile &File) {
725 auto ModuleMap = generateModuleMap(Modules);
726 auto ModuleCount = Index.modulePaths().size();
728 // Collect for each module the list of function it defines (GUID -> Summary).
729 DenseMap<StringRef, GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
730 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
732 // Convert the preserved symbols set from string to GUID
733 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
734 File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
736 addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
738 // Compute "dead" symbols, we don't want to import/export these!
739 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
741 // Compute prevailing symbols
742 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
743 computePrevailingCopies(Index, PrevailingCopy);
745 // Generate import/export list
746 FunctionImporter::ImportListsTy ImportLists(ModuleCount);
747 DenseMap<StringRef, FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
748 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries,
749 IsPrevailing(PrevailingCopy), ImportLists,
750 ExportLists);
751 auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
753 // FIXME Set ClearDSOLocalOnDeclarations.
754 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList,
755 /*ClearDSOLocalOnDeclarations=*/false);
759 * Compute the list of summaries needed for importing into module.
761 void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
762 Module &TheModule, ModuleSummaryIndex &Index,
763 ModuleToSummariesForIndexTy &ModuleToSummariesForIndex,
764 GVSummaryPtrSet &DecSummaries, const lto::InputFile &File) {
765 auto ModuleCount = Index.modulePaths().size();
766 auto ModuleIdentifier = TheModule.getModuleIdentifier();
768 // Collect for each module the list of function it defines (GUID -> Summary).
769 DenseMap<StringRef, GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
770 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
772 // Convert the preserved symbols set from string to GUID
773 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
774 File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
776 addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
778 // Compute "dead" symbols, we don't want to import/export these!
779 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
781 // Compute prevailing symbols
782 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
783 computePrevailingCopies(Index, PrevailingCopy);
785 // Generate import/export list
786 FunctionImporter::ImportListsTy ImportLists(ModuleCount);
787 DenseMap<StringRef, FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
788 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries,
789 IsPrevailing(PrevailingCopy), ImportLists,
790 ExportLists);
792 llvm::gatherImportedSummariesForModule(
793 ModuleIdentifier, ModuleToDefinedGVSummaries,
794 ImportLists[ModuleIdentifier], ModuleToSummariesForIndex, DecSummaries);
798 * Emit the list of files needed for importing into module.
800 void ThinLTOCodeGenerator::emitImports(Module &TheModule, StringRef OutputName,
801 ModuleSummaryIndex &Index,
802 const lto::InputFile &File) {
803 auto ModuleCount = Index.modulePaths().size();
804 auto ModuleIdentifier = TheModule.getModuleIdentifier();
806 // Collect for each module the list of function it defines (GUID -> Summary).
807 DenseMap<StringRef, GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
808 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
810 // Convert the preserved symbols set from string to GUID
811 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
812 File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
814 addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
816 // Compute "dead" symbols, we don't want to import/export these!
817 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
819 // Compute prevailing symbols
820 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
821 computePrevailingCopies(Index, PrevailingCopy);
823 // Generate import/export list
824 FunctionImporter::ImportListsTy ImportLists(ModuleCount);
825 DenseMap<StringRef, FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
826 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries,
827 IsPrevailing(PrevailingCopy), ImportLists,
828 ExportLists);
830 // 'EmitImportsFiles' emits the list of modules from which to import from, and
831 // the set of keys in `ModuleToSummariesForIndex` should be a superset of keys
832 // in `DecSummaries`, so no need to use `DecSummaries` in `EmitImportFiles`.
833 GVSummaryPtrSet DecSummaries;
834 ModuleToSummariesForIndexTy ModuleToSummariesForIndex;
835 llvm::gatherImportedSummariesForModule(
836 ModuleIdentifier, ModuleToDefinedGVSummaries,
837 ImportLists[ModuleIdentifier], ModuleToSummariesForIndex, DecSummaries);
839 if (Error EC = EmitImportsFiles(ModuleIdentifier, OutputName,
840 ModuleToSummariesForIndex))
841 report_fatal_error(Twine("Failed to open ") + OutputName +
842 " to save imports lists\n");
846 * Perform internalization. Runs promote and internalization together.
847 * Index is updated to reflect linkage changes.
849 void ThinLTOCodeGenerator::internalize(Module &TheModule,
850 ModuleSummaryIndex &Index,
851 const lto::InputFile &File) {
852 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
853 auto ModuleCount = Index.modulePaths().size();
854 auto ModuleIdentifier = TheModule.getModuleIdentifier();
856 // Convert the preserved symbols set from string to GUID
857 auto GUIDPreservedSymbols =
858 computeGUIDPreservedSymbols(File, PreservedSymbols, TMBuilder.TheTriple);
860 addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
862 // Collect for each module the list of function it defines (GUID -> Summary).
863 DenseMap<StringRef, GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
864 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
866 // Compute "dead" symbols, we don't want to import/export these!
867 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
869 // Compute prevailing symbols
870 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
871 computePrevailingCopies(Index, PrevailingCopy);
873 // Generate import/export list
874 FunctionImporter::ImportListsTy ImportLists(ModuleCount);
875 DenseMap<StringRef, FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
876 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries,
877 IsPrevailing(PrevailingCopy), ImportLists,
878 ExportLists);
879 auto &ExportList = ExportLists[ModuleIdentifier];
881 // Be friendly and don't nuke totally the module when the client didn't
882 // supply anything to preserve.
883 if (ExportList.empty() && GUIDPreservedSymbols.empty())
884 return;
886 // Resolve prevailing symbols
887 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
888 resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols,
889 PrevailingCopy);
891 // Promote the exported values in the index, so that they are promoted
892 // in the module.
893 thinLTOInternalizeAndPromoteInIndex(
894 Index, IsExported(ExportLists, GUIDPreservedSymbols),
895 IsPrevailing(PrevailingCopy));
897 // FIXME Set ClearDSOLocalOnDeclarations.
898 promoteModule(TheModule, Index, /*ClearDSOLocalOnDeclarations=*/false);
900 // Internalization
901 thinLTOFinalizeInModule(TheModule,
902 ModuleToDefinedGVSummaries[ModuleIdentifier],
903 /*PropagateAttrs=*/false);
905 thinLTOInternalizeModule(TheModule,
906 ModuleToDefinedGVSummaries[ModuleIdentifier]);
910 * Perform post-importing ThinLTO optimizations.
912 void ThinLTOCodeGenerator::optimize(Module &TheModule) {
913 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
915 // Optimize now
916 optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding,
917 DebugPassManager, nullptr);
920 /// Write out the generated object file, either from CacheEntryPath or from
921 /// OutputBuffer, preferring hard-link when possible.
922 /// Returns the path to the generated file in SavedObjectsDirectoryPath.
923 std::string
924 ThinLTOCodeGenerator::writeGeneratedObject(int count, StringRef CacheEntryPath,
925 const MemoryBuffer &OutputBuffer) {
926 auto ArchName = TMBuilder.TheTriple.getArchName();
927 SmallString<128> OutputPath(SavedObjectsDirectoryPath);
928 llvm::sys::path::append(OutputPath,
929 Twine(count) + "." + ArchName + ".thinlto.o");
930 OutputPath.c_str(); // Ensure the string is null terminated.
931 if (sys::fs::exists(OutputPath))
932 sys::fs::remove(OutputPath);
934 // We don't return a memory buffer to the linker, just a list of files.
935 if (!CacheEntryPath.empty()) {
936 // Cache is enabled, hard-link the entry (or copy if hard-link fails).
937 auto Err = sys::fs::create_hard_link(CacheEntryPath, OutputPath);
938 if (!Err)
939 return std::string(OutputPath);
940 // Hard linking failed, try to copy.
941 Err = sys::fs::copy_file(CacheEntryPath, OutputPath);
942 if (!Err)
943 return std::string(OutputPath);
944 // Copy failed (could be because the CacheEntry was removed from the cache
945 // in the meantime by another process), fall back and try to write down the
946 // buffer to the output.
947 errs() << "remark: can't link or copy from cached entry '" << CacheEntryPath
948 << "' to '" << OutputPath << "'\n";
950 // No cache entry, just write out the buffer.
951 std::error_code Err;
952 raw_fd_ostream OS(OutputPath, Err, sys::fs::OF_None);
953 if (Err)
954 report_fatal_error(Twine("Can't open output '") + OutputPath + "'\n");
955 OS << OutputBuffer.getBuffer();
956 return std::string(OutputPath);
959 // Main entry point for the ThinLTO processing
960 void ThinLTOCodeGenerator::run() {
961 timeTraceProfilerBegin("ThinLink", StringRef(""));
962 auto TimeTraceScopeExit = llvm::make_scope_exit([]() {
963 if (llvm::timeTraceProfilerEnabled())
964 llvm::timeTraceProfilerEnd();
966 // Prepare the resulting object vector
967 assert(ProducedBinaries.empty() && "The generator should not be reused");
968 if (SavedObjectsDirectoryPath.empty())
969 ProducedBinaries.resize(Modules.size());
970 else {
971 sys::fs::create_directories(SavedObjectsDirectoryPath);
972 bool IsDir;
973 sys::fs::is_directory(SavedObjectsDirectoryPath, IsDir);
974 if (!IsDir)
975 report_fatal_error(Twine("Unexistent dir: '") + SavedObjectsDirectoryPath + "'");
976 ProducedBinaryFiles.resize(Modules.size());
979 if (CodeGenOnly) {
980 // Perform only parallel codegen and return.
981 DefaultThreadPool Pool;
982 int count = 0;
983 for (auto &Mod : Modules) {
984 Pool.async([&](int count) {
985 LLVMContext Context;
986 Context.setDiscardValueNames(LTODiscardValueNames);
988 // Parse module now
989 auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
990 /*IsImporting*/ false);
992 // CodeGen
993 auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
994 if (SavedObjectsDirectoryPath.empty())
995 ProducedBinaries[count] = std::move(OutputBuffer);
996 else
997 ProducedBinaryFiles[count] =
998 writeGeneratedObject(count, "", *OutputBuffer);
999 }, count++);
1002 return;
1005 // Sequential linking phase
1006 auto Index = linkCombinedIndex();
1008 // Save temps: index.
1009 if (!SaveTempsDir.empty()) {
1010 auto SaveTempPath = SaveTempsDir + "index.bc";
1011 std::error_code EC;
1012 raw_fd_ostream OS(SaveTempPath, EC, sys::fs::OF_None);
1013 if (EC)
1014 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
1015 " to save optimized bitcode\n");
1016 writeIndexToFile(*Index, OS);
1020 // Prepare the module map.
1021 auto ModuleMap = generateModuleMap(Modules);
1022 auto ModuleCount = Modules.size();
1024 // Collect for each module the list of function it defines (GUID -> Summary).
1025 DenseMap<StringRef, GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
1026 Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
1028 // Convert the preserved symbols set from string to GUID, this is needed for
1029 // computing the caching hash and the internalization.
1030 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
1031 for (const auto &M : Modules)
1032 computeGUIDPreservedSymbols(*M, PreservedSymbols, TMBuilder.TheTriple,
1033 GUIDPreservedSymbols);
1035 // Add used symbol from inputs to the preserved symbols.
1036 for (const auto &M : Modules)
1037 addUsedSymbolToPreservedGUID(*M, GUIDPreservedSymbols);
1039 // Compute "dead" symbols, we don't want to import/export these!
1040 computeDeadSymbolsInIndex(*Index, GUIDPreservedSymbols);
1042 // Currently there is no support for enabling whole program visibility via a
1043 // linker option in the old LTO API, but this call allows it to be specified
1044 // via the internal option. Must be done before WPD below.
1045 if (hasWholeProgramVisibility(/* WholeProgramVisibilityEnabledInLTO */ false))
1046 Index->setWithWholeProgramVisibility();
1048 // FIXME: This needs linker information via a TBD new interface
1049 updateVCallVisibilityInIndex(*Index,
1050 /*WholeProgramVisibilityEnabledInLTO=*/false,
1051 // FIXME: These need linker information via a
1052 // TBD new interface.
1053 /*DynamicExportSymbols=*/{},
1054 /*VisibleToRegularObjSymbols=*/{});
1056 // Perform index-based WPD. This will return immediately if there are
1057 // no index entries in the typeIdMetadata map (e.g. if we are instead
1058 // performing IR-based WPD in hybrid regular/thin LTO mode).
1059 std::map<ValueInfo, std::vector<VTableSlotSummary>> LocalWPDTargetsMap;
1060 std::set<GlobalValue::GUID> ExportedGUIDs;
1061 runWholeProgramDevirtOnIndex(*Index, ExportedGUIDs, LocalWPDTargetsMap);
1062 for (auto GUID : ExportedGUIDs)
1063 GUIDPreservedSymbols.insert(GUID);
1065 // Compute prevailing symbols
1066 DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
1067 computePrevailingCopies(*Index, PrevailingCopy);
1069 // Collect the import/export lists for all modules from the call-graph in the
1070 // combined index.
1071 FunctionImporter::ImportListsTy ImportLists(ModuleCount);
1072 DenseMap<StringRef, FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
1073 ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries,
1074 IsPrevailing(PrevailingCopy), ImportLists,
1075 ExportLists);
1077 // We use a std::map here to be able to have a defined ordering when
1078 // producing a hash for the cache entry.
1079 // FIXME: we should be able to compute the caching hash for the entry based
1080 // on the index, and nuke this map.
1081 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
1083 // Resolve prevailing symbols, this has to be computed early because it
1084 // impacts the caching.
1085 resolvePrevailingInIndex(*Index, ResolvedODR, GUIDPreservedSymbols,
1086 PrevailingCopy);
1088 // Use global summary-based analysis to identify symbols that can be
1089 // internalized (because they aren't exported or preserved as per callback).
1090 // Changes are made in the index, consumed in the ThinLTO backends.
1091 updateIndexWPDForExports(*Index,
1092 IsExported(ExportLists, GUIDPreservedSymbols),
1093 LocalWPDTargetsMap);
1094 thinLTOInternalizeAndPromoteInIndex(
1095 *Index, IsExported(ExportLists, GUIDPreservedSymbols),
1096 IsPrevailing(PrevailingCopy));
1098 thinLTOPropagateFunctionAttrs(*Index, IsPrevailing(PrevailingCopy));
1100 // Make sure that every module has an entry in the ExportLists, ImportList,
1101 // GVSummary and ResolvedODR maps to enable threaded access to these maps
1102 // below.
1103 for (auto &Module : Modules) {
1104 auto ModuleIdentifier = Module->getName();
1105 ExportLists[ModuleIdentifier];
1106 ImportLists[ModuleIdentifier];
1107 ResolvedODR[ModuleIdentifier];
1108 ModuleToDefinedGVSummaries[ModuleIdentifier];
1111 std::vector<BitcodeModule *> ModulesVec;
1112 ModulesVec.reserve(Modules.size());
1113 for (auto &Mod : Modules)
1114 ModulesVec.push_back(&Mod->getSingleBitcodeModule());
1115 std::vector<int> ModulesOrdering = lto::generateModulesOrdering(ModulesVec);
1117 if (llvm::timeTraceProfilerEnabled())
1118 llvm::timeTraceProfilerEnd();
1120 TimeTraceScopeExit.release();
1122 // Parallel optimizer + codegen
1124 DefaultThreadPool Pool(heavyweight_hardware_concurrency(ThreadCount));
1125 for (auto IndexCount : ModulesOrdering) {
1126 auto &Mod = Modules[IndexCount];
1127 Pool.async([&](int count) {
1128 auto ModuleIdentifier = Mod->getName();
1129 auto &ExportList = ExportLists[ModuleIdentifier];
1131 auto &DefinedGVSummaries = ModuleToDefinedGVSummaries[ModuleIdentifier];
1133 // The module may be cached, this helps handling it.
1134 ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
1135 ImportLists[ModuleIdentifier], ExportList,
1136 ResolvedODR[ModuleIdentifier],
1137 DefinedGVSummaries, OptLevel, Freestanding,
1138 TMBuilder);
1139 auto CacheEntryPath = CacheEntry.getEntryPath();
1142 auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
1143 LLVM_DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss")
1144 << " '" << CacheEntryPath << "' for buffer "
1145 << count << " " << ModuleIdentifier << "\n");
1147 if (ErrOrBuffer) {
1148 // Cache Hit!
1149 if (SavedObjectsDirectoryPath.empty())
1150 ProducedBinaries[count] = std::move(ErrOrBuffer.get());
1151 else
1152 ProducedBinaryFiles[count] = writeGeneratedObject(
1153 count, CacheEntryPath, *ErrOrBuffer.get());
1154 return;
1158 LLVMContext Context;
1159 Context.setDiscardValueNames(LTODiscardValueNames);
1160 Context.enableDebugTypeODRUniquing();
1161 auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
1162 Context, RemarksFilename, RemarksPasses, RemarksFormat,
1163 RemarksWithHotness, RemarksHotnessThreshold, count);
1164 if (!DiagFileOrErr) {
1165 errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
1166 report_fatal_error("ThinLTO: Can't get an output file for the "
1167 "remarks");
1170 // Parse module now
1171 auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
1172 /*IsImporting*/ false);
1174 // Save temps: original file.
1175 saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
1177 auto &ImportList = ImportLists[ModuleIdentifier];
1178 // Run the main process now, and generates a binary
1179 auto OutputBuffer = ProcessThinLTOModule(
1180 *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
1181 ExportList, GUIDPreservedSymbols,
1182 ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
1183 DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count,
1184 DebugPassManager);
1186 // Commit to the cache (if enabled)
1187 CacheEntry.write(*OutputBuffer);
1189 if (SavedObjectsDirectoryPath.empty()) {
1190 // We need to generated a memory buffer for the linker.
1191 if (!CacheEntryPath.empty()) {
1192 // When cache is enabled, reload from the cache if possible.
1193 // Releasing the buffer from the heap and reloading it from the
1194 // cache file with mmap helps us to lower memory pressure.
1195 // The freed memory can be used for the next input file.
1196 // The final binary link will read from the VFS cache (hopefully!)
1197 // or from disk (if the memory pressure was too high).
1198 auto ReloadedBufferOrErr = CacheEntry.tryLoadingBuffer();
1199 if (auto EC = ReloadedBufferOrErr.getError()) {
1200 // On error, keep the preexisting buffer and print a diagnostic.
1201 errs() << "remark: can't reload cached file '" << CacheEntryPath
1202 << "': " << EC.message() << "\n";
1203 } else {
1204 OutputBuffer = std::move(*ReloadedBufferOrErr);
1207 ProducedBinaries[count] = std::move(OutputBuffer);
1208 return;
1210 ProducedBinaryFiles[count] = writeGeneratedObject(
1211 count, CacheEntryPath, *OutputBuffer);
1212 }, IndexCount);
1216 pruneCache(CacheOptions.Path, CacheOptions.Policy, ProducedBinaries);
1218 // If statistics were requested, print them out now.
1219 if (llvm::AreStatisticsEnabled())
1220 llvm::PrintStatistics();
1221 reportAndResetTimings();