LLVM 19.0.0git
ThinLTOCodeGenerator.cpp
Go to the documentation of this file.
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.
11//
12//===----------------------------------------------------------------------===//
13
16
17#include "llvm/ADT/ScopeExit.h"
18#include "llvm/ADT/Statistic.h"
27#include "llvm/Config/llvm-config.h"
28#include "llvm/IR/DebugInfo.h"
31#include "llvm/IR/LLVMContext.h"
33#include "llvm/IR/Mangler.h"
35#include "llvm/IR/Verifier.h"
37#include "llvm/LTO/LTO.h"
45#include "llvm/Support/Debug.h"
46#include "llvm/Support/Error.h"
49#include "llvm/Support/Path.h"
50#include "llvm/Support/SHA1.h"
64
65#include <numeric>
66
67#if !defined(_MSC_VER) && !defined(__MINGW32__)
68#include <unistd.h>
69#else
70#include <io.h>
71#endif
72
73using namespace llvm;
74
75#define DEBUG_TYPE "thinlto"
76
77namespace llvm {
78// Flags -discard-value-names, defined in LTOCodeGenerator.cpp
86}
87
88namespace {
89
90// Default to using all available threads in the system, but using only one
91// thred per core, as indicated by the usage of
92// heavyweight_hardware_concurrency() below.
93static cl::opt<int> ThreadCount("threads", cl::init(0));
94
95// Simple helper to save temporary files for debug.
96static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
97 unsigned count, StringRef Suffix) {
98 if (TempDir.empty())
99 return;
100 // User asked to save temps, let dump the bitcode file after import.
101 std::string SaveTempPath = (TempDir + llvm::Twine(count) + Suffix).str();
102 std::error_code EC;
103 raw_fd_ostream OS(SaveTempPath, EC, sys::fs::OF_None);
104 if (EC)
105 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
106 " to save optimized bitcode\n");
107 WriteBitcodeToFile(TheModule, OS, /* ShouldPreserveUseListOrder */ true);
108}
109
110static const GlobalValueSummary *
111getFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList) {
112 // If there is any strong definition anywhere, get it.
113 auto StrongDefForLinker = llvm::find_if(
114 GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
115 auto Linkage = Summary->linkage();
118 });
119 if (StrongDefForLinker != GVSummaryList.end())
120 return StrongDefForLinker->get();
121 // Get the first *linker visible* definition for this global in the summary
122 // list.
123 auto FirstDefForLinker = llvm::find_if(
124 GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
125 auto Linkage = Summary->linkage();
127 });
128 // Extern templates can be emitted as available_externally.
129 if (FirstDefForLinker == GVSummaryList.end())
130 return nullptr;
131 return FirstDefForLinker->get();
132}
133
134// Populate map of GUID to the prevailing copy for any multiply defined
135// symbols. Currently assume first copy is prevailing, or any strong
136// definition. Can be refined with Linker information in the future.
137static void computePrevailingCopies(
140 auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) {
141 return GVSummaryList.size() > 1;
142 };
143
144 for (auto &I : Index) {
145 if (HasMultipleCopies(I.second.SummaryList))
146 PrevailingCopy[I.first] =
147 getFirstDefinitionForLinker(I.second.SummaryList);
148 }
149}
150
152generateModuleMap(std::vector<std::unique_ptr<lto::InputFile>> &Modules) {
154 for (auto &M : Modules) {
155 LLVM_DEBUG(dbgs() << "Adding module " << M->getName() << " to ModuleMap\n");
156 assert(!ModuleMap.contains(M->getName()) &&
157 "Expect unique Buffer Identifier");
158 ModuleMap[M->getName()] = M.get();
159 }
160 return ModuleMap;
161}
162
163static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index,
164 bool ClearDSOLocalOnDeclarations) {
165 if (renameModuleForThinLTO(TheModule, Index, ClearDSOLocalOnDeclarations))
166 report_fatal_error("renameModuleForThinLTO failed");
167}
168
169namespace {
170class ThinLTODiagnosticInfo : public DiagnosticInfo {
171 const Twine &Msg;
172public:
173 ThinLTODiagnosticInfo(const Twine &DiagMsg,
174 DiagnosticSeverity Severity = DS_Error)
175 : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
176 void print(DiagnosticPrinter &DP) const override { DP << Msg; }
177};
178}
179
180/// Verify the module and strip broken debug info.
181static void verifyLoadedModule(Module &TheModule) {
182 bool BrokenDebugInfo = false;
183 if (verifyModule(TheModule, &dbgs(), &BrokenDebugInfo))
184 report_fatal_error("Broken module found, compilation aborted!");
185 if (BrokenDebugInfo) {
186 TheModule.getContext().diagnose(ThinLTODiagnosticInfo(
187 "Invalid debug info found, debug info will be stripped", DS_Warning));
188 StripDebugInfo(TheModule);
189 }
190}
191
192static std::unique_ptr<Module> loadModuleFromInput(lto::InputFile *Input,
193 LLVMContext &Context,
194 bool Lazy,
195 bool IsImporting) {
196 auto &Mod = Input->getSingleBitcodeModule();
197 SMDiagnostic Err;
199 Lazy ? Mod.getLazyModule(Context,
200 /* ShouldLazyLoadMetadata */ true, IsImporting)
201 : Mod.parseModule(Context);
202 if (!ModuleOrErr) {
203 handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
204 SMDiagnostic Err = SMDiagnostic(Mod.getModuleIdentifier(),
205 SourceMgr::DK_Error, EIB.message());
206 Err.print("ThinLTO", errs());
207 });
208 report_fatal_error("Can't load module, abort.");
209 }
210 if (!Lazy)
211 verifyLoadedModule(*ModuleOrErr.get());
212 return std::move(*ModuleOrErr);
213}
214
215static void
216crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
218 const FunctionImporter::ImportMapTy &ImportList,
219 bool ClearDSOLocalOnDeclarations) {
220 auto Loader = [&](StringRef Identifier) {
221 auto &Input = ModuleMap[Identifier];
222 return loadModuleFromInput(Input, TheModule.getContext(),
223 /*Lazy=*/true, /*IsImporting*/ true);
224 };
225
226 FunctionImporter Importer(Index, Loader, ClearDSOLocalOnDeclarations);
227 Expected<bool> Result = Importer.importFunctions(TheModule, ImportList);
228 if (!Result) {
229 handleAllErrors(Result.takeError(), [&](ErrorInfoBase &EIB) {
230 SMDiagnostic Err = SMDiagnostic(TheModule.getModuleIdentifier(),
231 SourceMgr::DK_Error, EIB.message());
232 Err.print("ThinLTO", errs());
233 });
234 report_fatal_error("importFunctions failed");
235 }
236 // Verify again after cross-importing.
237 verifyLoadedModule(TheModule);
238}
239
240static void optimizeModule(Module &TheModule, TargetMachine &TM,
241 unsigned OptLevel, bool Freestanding,
242 bool DebugPassManager, ModuleSummaryIndex *Index) {
243 std::optional<PGOOptions> PGOOpt;
248
250 StandardInstrumentations SI(TheModule.getContext(), DebugPassManager);
251 SI.registerCallbacks(PIC, &MAM);
253 PTO.LoopVectorization = true;
254 PTO.SLPVectorization = true;
255 PassBuilder PB(&TM, PTO, PGOOpt, &PIC);
256
257 std::unique_ptr<TargetLibraryInfoImpl> TLII(
258 new TargetLibraryInfoImpl(Triple(TM.getTargetTriple())));
259 if (Freestanding)
260 TLII->disableAllFunctions();
261 FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
262
263 // Register all the basic analyses with the managers.
269
271
273
274 switch (OptLevel) {
275 default:
276 llvm_unreachable("Invalid optimization level");
277 case 0:
279 break;
280 case 1:
282 break;
283 case 2:
285 break;
286 case 3:
288 break;
289 }
290
292
293 MPM.run(TheModule, MAM);
294}
295
296static void
297addUsedSymbolToPreservedGUID(const lto::InputFile &File,
298 DenseSet<GlobalValue::GUID> &PreservedGUID) {
299 for (const auto &Sym : File.symbols()) {
300 if (Sym.isUsed())
301 PreservedGUID.insert(GlobalValue::getGUID(Sym.getIRName()));
302 }
303}
304
305// Convert the PreservedSymbols map from "Name" based to "GUID" based.
306static void computeGUIDPreservedSymbols(const lto::InputFile &File,
308 const Triple &TheTriple,
310 // Iterate the symbols in the input file and if the input has preserved symbol
311 // compute the GUID for the symbol.
312 for (const auto &Sym : File.symbols()) {
313 if (PreservedSymbols.count(Sym.getName()) && !Sym.getIRName().empty())
315 Sym.getIRName(), GlobalValue::ExternalLinkage, "")));
316 }
317}
318
320computeGUIDPreservedSymbols(const lto::InputFile &File,
322 const Triple &TheTriple) {
323 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size());
324 computeGUIDPreservedSymbols(File, PreservedSymbols, TheTriple,
325 GUIDPreservedSymbols);
326 return GUIDPreservedSymbols;
327}
328
329std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
330 TargetMachine &TM) {
332
333 // CodeGen
334 {
337
338 // If the bitcode files contain ARC code and were compiled with optimization,
339 // the ObjCARCContractPass must be run, so do it unconditionally here.
341
342 // Setup the codegen now.
343 if (TM.addPassesToEmitFile(PM, OS, nullptr, CodeGenFileType::ObjectFile,
344 /* DisableVerify */ true))
345 report_fatal_error("Failed to setup codegen");
346
347 // Run codegen now. resulting binary is in OutputBuffer.
348 PM.run(TheModule);
349 }
350 return std::make_unique<SmallVectorMemoryBuffer>(
351 std::move(OutputBuffer), /*RequiresNullTerminator=*/false);
352}
353
354/// Manage caching for a single Module.
355class ModuleCacheEntry {
356 SmallString<128> EntryPath;
357
358public:
359 // Create a cache entry. This compute a unique hash for the Module considering
360 // the current list of export/import, and offer an interface to query to
361 // access the content in the cache.
362 ModuleCacheEntry(
363 StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID,
364 const FunctionImporter::ImportMapTy &ImportList,
365 const FunctionImporter::ExportSetTy &ExportList,
366 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
367 const GVSummaryMapTy &DefinedGVSummaries, unsigned OptLevel,
368 bool Freestanding, const TargetMachineBuilder &TMBuilder) {
369 if (CachePath.empty())
370 return;
371
372 if (!Index.modulePaths().count(ModuleID))
373 // The module does not have an entry, it can't have a hash at all
374 return;
375
376 if (all_of(Index.getModuleHash(ModuleID),
377 [](uint32_t V) { return V == 0; }))
378 // No hash entry, no caching!
379 return;
380
382 Conf.OptLevel = OptLevel;
383 Conf.Options = TMBuilder.Options;
384 Conf.CPU = TMBuilder.MCpu;
385 Conf.MAttrs.push_back(TMBuilder.MAttr);
386 Conf.RelocModel = TMBuilder.RelocModel;
387 Conf.CGOptLevel = TMBuilder.CGOptLevel;
388 Conf.Freestanding = Freestanding;
390 computeLTOCacheKey(Key, Conf, Index, ModuleID, ImportList, ExportList,
391 ResolvedODR, DefinedGVSummaries);
392
393 // This choice of file name allows the cache to be pruned (see pruneCache()
394 // in include/llvm/Support/CachePruning.h).
395 sys::path::append(EntryPath, CachePath, "llvmcache-" + Key);
396 }
397
398 // Access the path to this entry in the cache.
399 StringRef getEntryPath() { return EntryPath; }
400
401 // Try loading the buffer for this cache entry.
402 ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() {
403 if (EntryPath.empty())
404 return std::error_code();
405 SmallString<64> ResultPath;
407 Twine(EntryPath), sys::fs::OF_UpdateAtime, &ResultPath);
408 if (!FDOrErr)
409 return errorToErrorCode(FDOrErr.takeError());
411 *FDOrErr, EntryPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false);
412 sys::fs::closeFile(*FDOrErr);
413 return MBOrErr;
414 }
415
416 // Cache the Produced object file
417 void write(const MemoryBuffer &OutputBuffer) {
418 if (EntryPath.empty())
419 return;
420
421 if (auto Err = llvm::writeToOutput(
422 EntryPath, [&OutputBuffer](llvm::raw_ostream &OS) -> llvm::Error {
424 return llvm::Error::success();
425 }))
426 report_fatal_error(llvm::formatv("ThinLTO: Can't write file {0}: {1}",
427 EntryPath,
428 toString(std::move(Err)).c_str()));
429 }
430};
431
432static std::unique_ptr<MemoryBuffer>
433ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
435 const FunctionImporter::ImportMapTy &ImportList,
436 const FunctionImporter::ExportSetTy &ExportList,
437 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
438 const GVSummaryMapTy &DefinedGlobals,
439 const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
440 bool DisableCodeGen, StringRef SaveTempsDir,
441 bool Freestanding, unsigned OptLevel, unsigned count,
442 bool DebugPassManager) {
443 // "Benchmark"-like optimization: single-source case
444 bool SingleModule = (ModuleMap.size() == 1);
445
446 // When linking an ELF shared object, dso_local should be dropped. We
447 // conservatively do this for -fpic.
448 bool ClearDSOLocalOnDeclarations =
449 TM.getTargetTriple().isOSBinFormatELF() &&
450 TM.getRelocationModel() != Reloc::Static &&
451 TheModule.getPIELevel() == PIELevel::Default;
452
453 if (!SingleModule) {
454 promoteModule(TheModule, Index, ClearDSOLocalOnDeclarations);
455
456 // Apply summary-based prevailing-symbol resolution decisions.
457 thinLTOFinalizeInModule(TheModule, DefinedGlobals, /*PropagateAttrs=*/true);
458
459 // Save temps: after promotion.
460 saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
461 }
462
463 // Be friendly and don't nuke totally the module when the client didn't
464 // supply anything to preserve.
465 if (!ExportList.empty() || !GUIDPreservedSymbols.empty()) {
466 // Apply summary-based internalization decisions.
467 thinLTOInternalizeModule(TheModule, DefinedGlobals);
468 }
469
470 // Save internalized bitcode
471 saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
472
473 if (!SingleModule)
474 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList,
475 ClearDSOLocalOnDeclarations);
476
477 // Do this after any importing so that imported code is updated.
478 // See comment at call to updateVCallVisibilityInIndex() for why
479 // WholeProgramVisibilityEnabledInLTO is false.
481 /* WholeProgramVisibilityEnabledInLTO */ false);
482
483 // Save temps: after cross-module import.
484 saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
485
486 optimizeModule(TheModule, TM, OptLevel, Freestanding, DebugPassManager,
487 &Index);
488
489 saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
490
491 if (DisableCodeGen) {
492 // Configured to stop before CodeGen, serialize the bitcode and return.
494 {
496 ProfileSummaryInfo PSI(TheModule);
497 auto Index = buildModuleSummaryIndex(TheModule, nullptr, &PSI);
498 WriteBitcodeToFile(TheModule, OS, true, &Index);
499 }
500 return std::make_unique<SmallVectorMemoryBuffer>(
501 std::move(OutputBuffer), /*RequiresNullTerminator=*/false);
502 }
503
504 return codegenModule(TheModule, TM);
505}
506
507/// Resolve prevailing symbols. Record resolutions in the \p ResolvedODR map
508/// for caching, and in the \p Index for application during the ThinLTO
509/// backends. This is needed for correctness for exported symbols (ensure
510/// at least one copy kept) and a compile-time optimization (to drop duplicate
511/// copies when possible).
512static void resolvePrevailingInIndex(
514 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>>
515 &ResolvedODR,
516 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
518 &PrevailingCopy) {
519
520 auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
521 const auto &Prevailing = PrevailingCopy.find(GUID);
522 // Not in map means that there was only one copy, which must be prevailing.
523 if (Prevailing == PrevailingCopy.end())
524 return true;
525 return Prevailing->second == S;
526 };
527
528 auto recordNewLinkage = [&](StringRef ModuleIdentifier,
530 GlobalValue::LinkageTypes NewLinkage) {
531 ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
532 };
533
534 // TODO Conf.VisibilityScheme can be lto::Config::ELF for ELF.
535 lto::Config Conf;
536 thinLTOResolvePrevailingInIndex(Conf, Index, isPrevailing, recordNewLinkage,
537 GUIDPreservedSymbols);
538}
539
540// Initialize the TargetMachine builder for a given Triple
541static void initTMBuilder(TargetMachineBuilder &TMBuilder,
542 const Triple &TheTriple) {
543 if (TMBuilder.MCpu.empty())
544 TMBuilder.MCpu = lto::getThinLTODefaultCPU(TheTriple);
545 TMBuilder.TheTriple = std::move(TheTriple);
546}
547
548} // end anonymous namespace
549
551 MemoryBufferRef Buffer(Data, Identifier);
552
553 auto InputOrError = lto::InputFile::create(Buffer);
554 if (!InputOrError)
555 report_fatal_error(Twine("ThinLTO cannot create input file: ") +
556 toString(InputOrError.takeError()));
557
558 auto TripleStr = (*InputOrError)->getTargetTriple();
559 Triple TheTriple(TripleStr);
560
561 if (Modules.empty())
562 initTMBuilder(TMBuilder, Triple(TheTriple));
563 else if (TMBuilder.TheTriple != TheTriple) {
564 if (!TMBuilder.TheTriple.isCompatibleWith(TheTriple))
565 report_fatal_error("ThinLTO modules with incompatible triples not "
566 "supported");
567 initTMBuilder(TMBuilder, Triple(TMBuilder.TheTriple.merge(TheTriple)));
568 }
569
570 Modules.emplace_back(std::move(*InputOrError));
571}
572
574 PreservedSymbols.insert(Name);
575}
576
578 // FIXME: At the moment, we don't take advantage of this extra information,
579 // we're conservatively considering cross-references as preserved.
580 // CrossReferencedSymbols.insert(Name);
581 PreservedSymbols.insert(Name);
582}
583
584// TargetMachine factory
585std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
586 std::string ErrMsg;
587 const Target *TheTarget =
589 if (!TheTarget) {
590 report_fatal_error(Twine("Can't load target for this Triple: ") + ErrMsg);
591 }
592
593 // Use MAttr as the default set of features.
594 SubtargetFeatures Features(MAttr);
596 std::string FeatureStr = Features.getString();
597
598 std::unique_ptr<TargetMachine> TM(
599 TheTarget->createTargetMachine(TheTriple.str(), MCpu, FeatureStr, Options,
600 RelocModel, std::nullopt, CGOptLevel));
601 assert(TM && "Cannot create target machine");
602
603 return TM;
604}
605
606/**
607 * Produce the combined summary index from all the bitcode files:
608 * "thin-link".
609 */
610std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
611 std::unique_ptr<ModuleSummaryIndex> CombinedIndex =
612 std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
613 for (auto &Mod : Modules) {
614 auto &M = Mod->getSingleBitcodeModule();
615 if (Error Err = M.readSummary(*CombinedIndex, Mod->getName())) {
616 // FIXME diagnose
618 std::move(Err), errs(),
619 "error: can't create module summary index for buffer: ");
620 return nullptr;
621 }
622 }
623 return CombinedIndex;
624}
625
626namespace {
627struct IsExported {
629 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols;
630
631 IsExported(
633 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols)
634 : ExportLists(ExportLists), GUIDPreservedSymbols(GUIDPreservedSymbols) {}
635
636 bool operator()(StringRef ModuleIdentifier, ValueInfo VI) const {
637 const auto &ExportList = ExportLists.find(ModuleIdentifier);
638 return (ExportList != ExportLists.end() && ExportList->second.count(VI)) ||
639 GUIDPreservedSymbols.count(VI.getGUID());
640 }
641};
642
643struct IsPrevailing {
646 &PrevailingCopy)
647 : PrevailingCopy(PrevailingCopy) {}
648
649 bool operator()(GlobalValue::GUID GUID, const GlobalValueSummary *S) const {
650 const auto &Prevailing = PrevailingCopy.find(GUID);
651 // Not in map means that there was only one copy, which must be prevailing.
652 if (Prevailing == PrevailingCopy.end())
653 return true;
654 return Prevailing->second == S;
655 };
656};
657} // namespace
658
661 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
662 // We have no symbols resolution available. And can't do any better now in the
663 // case where the prevailing symbol is in a native object. It can be refined
664 // with linker information in the future.
665 auto isPrevailing = [&](GlobalValue::GUID G) {
667 };
668 computeDeadSymbolsWithConstProp(Index, GUIDPreservedSymbols, isPrevailing,
669 /* ImportEnabled = */ true);
670}
671
672/**
673 * Perform promotion and renaming of exported internal functions.
674 * Index is updated to reflect linkage changes from weak resolution.
675 */
677 const lto::InputFile &File) {
678 auto ModuleCount = Index.modulePaths().size();
679 auto ModuleIdentifier = TheModule.getModuleIdentifier();
680
681 // Collect for each module the list of function it defines (GUID -> Summary).
682 DenseMap<StringRef, GVSummaryMapTy> ModuleToDefinedGVSummaries;
683 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
684
685 // Convert the preserved symbols set from string to GUID
686 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
687 File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
688
689 // Add used symbol to the preserved symbols.
690 addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
691
692 // Compute "dead" symbols, we don't want to import/export these!
693 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
694
695 // Compute prevailing symbols
697 computePrevailingCopies(Index, PrevailingCopy);
698
699 // Generate import/export list
702 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries,
703 IsPrevailing(PrevailingCopy), ImportLists,
704 ExportLists);
705
706 // Resolve prevailing symbols
708 resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols,
709 PrevailingCopy);
710
711 thinLTOFinalizeInModule(TheModule,
712 ModuleToDefinedGVSummaries[ModuleIdentifier],
713 /*PropagateAttrs=*/false);
714
715 // Promote the exported values in the index, so that they are promoted
716 // in the module.
718 Index, IsExported(ExportLists, GUIDPreservedSymbols),
719 IsPrevailing(PrevailingCopy));
720
721 // FIXME Set ClearDSOLocalOnDeclarations.
722 promoteModule(TheModule, Index, /*ClearDSOLocalOnDeclarations=*/false);
723}
724
725/**
726 * Perform cross-module importing for the module identified by ModuleIdentifier.
727 */
730 const lto::InputFile &File) {
731 auto ModuleMap = generateModuleMap(Modules);
732 auto ModuleCount = Index.modulePaths().size();
733
734 // Collect for each module the list of function it defines (GUID -> Summary).
735 DenseMap<StringRef, GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
736 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
737
738 // Convert the preserved symbols set from string to GUID
739 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
740 File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
741
742 addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
743
744 // Compute "dead" symbols, we don't want to import/export these!
745 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
746
747 // Compute prevailing symbols
749 computePrevailingCopies(Index, PrevailingCopy);
750
751 // Generate import/export list
754 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries,
755 IsPrevailing(PrevailingCopy), ImportLists,
756 ExportLists);
757 auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
758
759 // FIXME Set ClearDSOLocalOnDeclarations.
760 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList,
761 /*ClearDSOLocalOnDeclarations=*/false);
762}
763
764/**
765 * Compute the list of summaries needed for importing into module.
766 */
768 Module &TheModule, ModuleSummaryIndex &Index,
769 std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex,
770 const lto::InputFile &File) {
771 auto ModuleCount = Index.modulePaths().size();
772 auto ModuleIdentifier = TheModule.getModuleIdentifier();
773
774 // Collect for each module the list of function it defines (GUID -> Summary).
775 DenseMap<StringRef, GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
776 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
777
778 // Convert the preserved symbols set from string to GUID
779 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
780 File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
781
782 addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
783
784 // Compute "dead" symbols, we don't want to import/export these!
785 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
786
787 // Compute prevailing symbols
789 computePrevailingCopies(Index, PrevailingCopy);
790
791 // Generate import/export list
794 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries,
795 IsPrevailing(PrevailingCopy), ImportLists,
796 ExportLists);
797
799 ModuleIdentifier, ModuleToDefinedGVSummaries,
800 ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
801}
802
803/**
804 * Emit the list of files needed for importing into module.
805 */
808 const lto::InputFile &File) {
809 auto ModuleCount = Index.modulePaths().size();
810 auto ModuleIdentifier = TheModule.getModuleIdentifier();
811
812 // Collect for each module the list of function it defines (GUID -> Summary).
813 DenseMap<StringRef, GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
814 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
815
816 // Convert the preserved symbols set from string to GUID
817 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
818 File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
819
820 addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
821
822 // Compute "dead" symbols, we don't want to import/export these!
823 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
824
825 // Compute prevailing symbols
827 computePrevailingCopies(Index, PrevailingCopy);
828
829 // Generate import/export list
832 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries,
833 IsPrevailing(PrevailingCopy), ImportLists,
834 ExportLists);
835
836 std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
838 ModuleIdentifier, ModuleToDefinedGVSummaries,
839 ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
840
841 std::error_code EC;
842 if ((EC = EmitImportsFiles(ModuleIdentifier, OutputName,
843 ModuleToSummariesForIndex)))
844 report_fatal_error(Twine("Failed to open ") + OutputName +
845 " to save imports lists\n");
846}
847
848/**
849 * Perform internalization. Runs promote and internalization together.
850 * Index is updated to reflect linkage changes.
851 */
854 const lto::InputFile &File) {
855 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
856 auto ModuleCount = Index.modulePaths().size();
857 auto ModuleIdentifier = TheModule.getModuleIdentifier();
858
859 // Convert the preserved symbols set from string to GUID
860 auto GUIDPreservedSymbols =
861 computeGUIDPreservedSymbols(File, PreservedSymbols, TMBuilder.TheTriple);
862
863 addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
864
865 // Collect for each module the list of function it defines (GUID -> Summary).
866 DenseMap<StringRef, GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
867 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
868
869 // Compute "dead" symbols, we don't want to import/export these!
870 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
871
872 // Compute prevailing symbols
874 computePrevailingCopies(Index, PrevailingCopy);
875
876 // Generate import/export list
879 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries,
880 IsPrevailing(PrevailingCopy), ImportLists,
881 ExportLists);
882 auto &ExportList = ExportLists[ModuleIdentifier];
883
884 // Be friendly and don't nuke totally the module when the client didn't
885 // supply anything to preserve.
886 if (ExportList.empty() && GUIDPreservedSymbols.empty())
887 return;
888
889 // Resolve prevailing symbols
891 resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols,
892 PrevailingCopy);
893
894 // Promote the exported values in the index, so that they are promoted
895 // in the module.
897 Index, IsExported(ExportLists, GUIDPreservedSymbols),
898 IsPrevailing(PrevailingCopy));
899
900 // FIXME Set ClearDSOLocalOnDeclarations.
901 promoteModule(TheModule, Index, /*ClearDSOLocalOnDeclarations=*/false);
902
903 // Internalization
904 thinLTOFinalizeInModule(TheModule,
905 ModuleToDefinedGVSummaries[ModuleIdentifier],
906 /*PropagateAttrs=*/false);
907
908 thinLTOInternalizeModule(TheModule,
909 ModuleToDefinedGVSummaries[ModuleIdentifier]);
910}
911
912/**
913 * Perform post-importing ThinLTO optimizations.
914 */
916 initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
917
918 // Optimize now
919 optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding,
920 DebugPassManager, nullptr);
921}
922
923/// Write out the generated object file, either from CacheEntryPath or from
924/// OutputBuffer, preferring hard-link when possible.
925/// Returns the path to the generated file in SavedObjectsDirectoryPath.
926std::string
928 const MemoryBuffer &OutputBuffer) {
929 auto ArchName = TMBuilder.TheTriple.getArchName();
930 SmallString<128> OutputPath(SavedObjectsDirectoryPath);
931 llvm::sys::path::append(OutputPath,
932 Twine(count) + "." + ArchName + ".thinlto.o");
933 OutputPath.c_str(); // Ensure the string is null terminated.
934 if (sys::fs::exists(OutputPath))
935 sys::fs::remove(OutputPath);
936
937 // We don't return a memory buffer to the linker, just a list of files.
938 if (!CacheEntryPath.empty()) {
939 // Cache is enabled, hard-link the entry (or copy if hard-link fails).
940 auto Err = sys::fs::create_hard_link(CacheEntryPath, OutputPath);
941 if (!Err)
942 return std::string(OutputPath);
943 // Hard linking failed, try to copy.
944 Err = sys::fs::copy_file(CacheEntryPath, OutputPath);
945 if (!Err)
946 return std::string(OutputPath);
947 // Copy failed (could be because the CacheEntry was removed from the cache
948 // in the meantime by another process), fall back and try to write down the
949 // buffer to the output.
950 errs() << "remark: can't link or copy from cached entry '" << CacheEntryPath
951 << "' to '" << OutputPath << "'\n";
952 }
953 // No cache entry, just write out the buffer.
954 std::error_code Err;
955 raw_fd_ostream OS(OutputPath, Err, sys::fs::OF_None);
956 if (Err)
957 report_fatal_error(Twine("Can't open output '") + OutputPath + "'\n");
958 OS << OutputBuffer.getBuffer();
959 return std::string(OutputPath);
960}
961
962// Main entry point for the ThinLTO processing
964 timeTraceProfilerBegin("ThinLink", StringRef(""));
965 auto TimeTraceScopeExit = llvm::make_scope_exit([]() {
968 });
969 // Prepare the resulting object vector
970 assert(ProducedBinaries.empty() && "The generator should not be reused");
971 if (SavedObjectsDirectoryPath.empty())
972 ProducedBinaries.resize(Modules.size());
973 else {
974 sys::fs::create_directories(SavedObjectsDirectoryPath);
975 bool IsDir;
976 sys::fs::is_directory(SavedObjectsDirectoryPath, IsDir);
977 if (!IsDir)
978 report_fatal_error(Twine("Unexistent dir: '") + SavedObjectsDirectoryPath + "'");
979 ProducedBinaryFiles.resize(Modules.size());
980 }
981
982 if (CodeGenOnly) {
983 // Perform only parallel codegen and return.
985 int count = 0;
986 for (auto &Mod : Modules) {
987 Pool.async([&](int count) {
988 LLVMContext Context;
990
991 // Parse module now
992 auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
993 /*IsImporting*/ false);
994
995 // CodeGen
996 auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
997 if (SavedObjectsDirectoryPath.empty())
998 ProducedBinaries[count] = std::move(OutputBuffer);
999 else
1000 ProducedBinaryFiles[count] =
1001 writeGeneratedObject(count, "", *OutputBuffer);
1002 }, count++);
1003 }
1004
1005 return;
1006 }
1007
1008 // Sequential linking phase
1009 auto Index = linkCombinedIndex();
1010
1011 // Save temps: index.
1012 if (!SaveTempsDir.empty()) {
1013 auto SaveTempPath = SaveTempsDir + "index.bc";
1014 std::error_code EC;
1015 raw_fd_ostream OS(SaveTempPath, EC, sys::fs::OF_None);
1016 if (EC)
1017 report_fatal_error(Twine("Failed to open ") + SaveTempPath +
1018 " to save optimized bitcode\n");
1020 }
1021
1022
1023 // Prepare the module map.
1024 auto ModuleMap = generateModuleMap(Modules);
1025 auto ModuleCount = Modules.size();
1026
1027 // Collect for each module the list of function it defines (GUID -> Summary).
1028 DenseMap<StringRef, GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
1029 Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
1030
1031 // Convert the preserved symbols set from string to GUID, this is needed for
1032 // computing the caching hash and the internalization.
1033 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
1034 for (const auto &M : Modules)
1035 computeGUIDPreservedSymbols(*M, PreservedSymbols, TMBuilder.TheTriple,
1036 GUIDPreservedSymbols);
1037
1038 // Add used symbol from inputs to the preserved symbols.
1039 for (const auto &M : Modules)
1040 addUsedSymbolToPreservedGUID(*M, GUIDPreservedSymbols);
1041
1042 // Compute "dead" symbols, we don't want to import/export these!
1043 computeDeadSymbolsInIndex(*Index, GUIDPreservedSymbols);
1044
1045 // Synthesize entry counts for functions in the combined index.
1047
1048 // Currently there is no support for enabling whole program visibility via a
1049 // linker option in the old LTO API, but this call allows it to be specified
1050 // via the internal option. Must be done before WPD below.
1051 if (hasWholeProgramVisibility(/* WholeProgramVisibilityEnabledInLTO */ false))
1052 Index->setWithWholeProgramVisibility();
1053
1054 // FIXME: This needs linker information via a TBD new interface
1056 /*WholeProgramVisibilityEnabledInLTO=*/false,
1057 // FIXME: These need linker information via a
1058 // TBD new interface.
1059 /*DynamicExportSymbols=*/{},
1060 /*VisibleToRegularObjSymbols=*/{});
1061
1062 // Perform index-based WPD. This will return immediately if there are
1063 // no index entries in the typeIdMetadata map (e.g. if we are instead
1064 // performing IR-based WPD in hybrid regular/thin LTO mode).
1065 std::map<ValueInfo, std::vector<VTableSlotSummary>> LocalWPDTargetsMap;
1066 std::set<GlobalValue::GUID> ExportedGUIDs;
1067 runWholeProgramDevirtOnIndex(*Index, ExportedGUIDs, LocalWPDTargetsMap);
1068 for (auto GUID : ExportedGUIDs)
1069 GUIDPreservedSymbols.insert(GUID);
1070
1071 // Compute prevailing symbols
1073 computePrevailingCopies(*Index, PrevailingCopy);
1074
1075 // Collect the import/export lists for all modules from the call-graph in the
1076 // combined index.
1079 ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries,
1080 IsPrevailing(PrevailingCopy), ImportLists,
1081 ExportLists);
1082
1083 // We use a std::map here to be able to have a defined ordering when
1084 // producing a hash for the cache entry.
1085 // FIXME: we should be able to compute the caching hash for the entry based
1086 // on the index, and nuke this map.
1088
1089 // Resolve prevailing symbols, this has to be computed early because it
1090 // impacts the caching.
1091 resolvePrevailingInIndex(*Index, ResolvedODR, GUIDPreservedSymbols,
1092 PrevailingCopy);
1093
1094 // Use global summary-based analysis to identify symbols that can be
1095 // internalized (because they aren't exported or preserved as per callback).
1096 // Changes are made in the index, consumed in the ThinLTO backends.
1098 IsExported(ExportLists, GUIDPreservedSymbols),
1099 LocalWPDTargetsMap);
1101 *Index, IsExported(ExportLists, GUIDPreservedSymbols),
1102 IsPrevailing(PrevailingCopy));
1103
1104 thinLTOPropagateFunctionAttrs(*Index, IsPrevailing(PrevailingCopy));
1105
1106 // Make sure that every module has an entry in the ExportLists, ImportList,
1107 // GVSummary and ResolvedODR maps to enable threaded access to these maps
1108 // below.
1109 for (auto &Module : Modules) {
1110 auto ModuleIdentifier = Module->getName();
1111 ExportLists[ModuleIdentifier];
1112 ImportLists[ModuleIdentifier];
1113 ResolvedODR[ModuleIdentifier];
1114 ModuleToDefinedGVSummaries[ModuleIdentifier];
1115 }
1116
1117 std::vector<BitcodeModule *> ModulesVec;
1118 ModulesVec.reserve(Modules.size());
1119 for (auto &Mod : Modules)
1120 ModulesVec.push_back(&Mod->getSingleBitcodeModule());
1121 std::vector<int> ModulesOrdering = lto::generateModulesOrdering(ModulesVec);
1122
1125
1126 TimeTraceScopeExit.release();
1127
1128 // Parallel optimizer + codegen
1129 {
1131 for (auto IndexCount : ModulesOrdering) {
1132 auto &Mod = Modules[IndexCount];
1133 Pool.async([&](int count) {
1134 auto ModuleIdentifier = Mod->getName();
1135 auto &ExportList = ExportLists[ModuleIdentifier];
1136
1137 auto &DefinedGVSummaries = ModuleToDefinedGVSummaries[ModuleIdentifier];
1138
1139 // The module may be cached, this helps handling it.
1140 ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
1141 ImportLists[ModuleIdentifier], ExportList,
1142 ResolvedODR[ModuleIdentifier],
1143 DefinedGVSummaries, OptLevel, Freestanding,
1144 TMBuilder);
1145 auto CacheEntryPath = CacheEntry.getEntryPath();
1146
1147 {
1148 auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
1149 LLVM_DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss")
1150 << " '" << CacheEntryPath << "' for buffer "
1151 << count << " " << ModuleIdentifier << "\n");
1152
1153 if (ErrOrBuffer) {
1154 // Cache Hit!
1155 if (SavedObjectsDirectoryPath.empty())
1156 ProducedBinaries[count] = std::move(ErrOrBuffer.get());
1157 else
1158 ProducedBinaryFiles[count] = writeGeneratedObject(
1159 count, CacheEntryPath, *ErrOrBuffer.get());
1160 return;
1161 }
1162 }
1163
1164 LLVMContext Context;
1167 auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
1170 if (!DiagFileOrErr) {
1171 errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
1172 report_fatal_error("ThinLTO: Can't get an output file for the "
1173 "remarks");
1174 }
1175
1176 // Parse module now
1177 auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
1178 /*IsImporting*/ false);
1179
1180 // Save temps: original file.
1181 saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
1182
1183 auto &ImportList = ImportLists[ModuleIdentifier];
1184 // Run the main process now, and generates a binary
1185 auto OutputBuffer = ProcessThinLTOModule(
1186 *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
1187 ExportList, GUIDPreservedSymbols,
1188 ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
1189 DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count,
1190 DebugPassManager);
1191
1192 // Commit to the cache (if enabled)
1193 CacheEntry.write(*OutputBuffer);
1194
1195 if (SavedObjectsDirectoryPath.empty()) {
1196 // We need to generated a memory buffer for the linker.
1197 if (!CacheEntryPath.empty()) {
1198 // When cache is enabled, reload from the cache if possible.
1199 // Releasing the buffer from the heap and reloading it from the
1200 // cache file with mmap helps us to lower memory pressure.
1201 // The freed memory can be used for the next input file.
1202 // The final binary link will read from the VFS cache (hopefully!)
1203 // or from disk (if the memory pressure was too high).
1204 auto ReloadedBufferOrErr = CacheEntry.tryLoadingBuffer();
1205 if (auto EC = ReloadedBufferOrErr.getError()) {
1206 // On error, keep the preexisting buffer and print a diagnostic.
1207 errs() << "remark: can't reload cached file '" << CacheEntryPath
1208 << "': " << EC.message() << "\n";
1209 } else {
1210 OutputBuffer = std::move(*ReloadedBufferOrErr);
1211 }
1212 }
1213 ProducedBinaries[count] = std::move(OutputBuffer);
1214 return;
1215 }
1216 ProducedBinaryFiles[count] = writeGeneratedObject(
1217 count, CacheEntryPath, *OutputBuffer);
1218 }, IndexCount);
1219 }
1220 }
1221
1222 pruneCache(CacheOptions.Path, CacheOptions.Policy, ProducedBinaries);
1223
1224 // If statistics were requested, print them out now.
1228}
This file provides a bitcode writing pass.
#define LLVM_DEBUG(X)
Definition: Debug.h:101
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:479
Provides passes for computing function attributes based on interprocedural analyses.
This file implements a simple parser to decode commandline option for remarks hotness threshold that ...
static const char * PreservedSymbols[]
Definition: IRSymtab.cpp:48
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
This is the interface to build a ModuleSummaryIndex for a module.
CGSCCAnalysisManager CGAM
ModulePassManager MPM
Module * Mod
LoopAnalysisManager LAM
FunctionAnalysisManager FAM
ModuleAnalysisManager MAM
const char LLVMTargetMachineRef TM
PassInstrumentationCallbacks PIC
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
This header defines classes/functions to handle pass execution timing information with interfaces for...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This header defines a class that provides bookkeeping for all standard (i.e in-tree) pass instrumenta...
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
This file contains some functions that are useful when dealing with strings.
static void computeDeadSymbolsInIndex(ModuleSummaryIndex &Index, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols)
char * getBuffer()
Definition: Utility.h:180
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
bool registerPass(PassBuilderT &&PassBuilder)
Register an analysis pass with the manager.
Definition: PassManager.h:467
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
bool empty() const
Definition: DenseMap.h:98
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:151
iterator end()
Definition: DenseMap.h:84
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition: DenseMap.h:103
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
This is the base abstract class for diagnostic reporting in the backend.
virtual void print(DiagnosticPrinter &DP) const =0
Print using the given DP a user-friendly message.
Interface for custom diagnostic printing.
Base class for error info classes.
Definition: Error.h:45
Represents either an error or a value T.
Definition: ErrorOr.h:56
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
reference get()
Returns a reference to the stored T value.
Definition: Error.h:578
The function importer is automatically importing function from other modules based on the provided su...
Function and variable summary information to aid decisions and implementation of importing.
static bool isAvailableExternallyLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:379
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:595
bool isWeakForLinker() const
Definition: GlobalValue.h:552
std::string getGlobalIdentifier() const
Return the modified name for this global value suitable to be used as the key for a global lookup (e....
Definition: Globals.cpp:178
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:51
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:52
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
void enableDebugTypeODRUniquing()
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
void setDiscardValueNames(bool Discard)
Set the Context runtime configuration to discard all value name (but GlobalValue).
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:51
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
StringRef getBuffer() const
Definition: MemoryBuffer.h:70
Class to hold module path string table and global value map, and encapsulate methods for operating on...
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:301
StringRef getName() const
Get a short "name" for the module.
Definition: Module.h:284
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:297
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:267
PIELevel::Level getPIELevel() const
Returns the PIE level (small or large model)
Definition: Module.cpp:622
static const OptimizationLevel O3
Optimize for fast execution as much as possible.
static const OptimizationLevel O0
Disable as many optimizations as possible.
static const OptimizationLevel O2
Optimize for fast execution as much as possible without triggering significant incremental compile ti...
static const OptimizationLevel O1
Optimize quickly without destroying debuggability.
This class provides access to building LLVM's passes.
Definition: PassBuilder.h:106
void registerLoopAnalyses(LoopAnalysisManager &LAM)
Registers all available loop analysis passes.
void crossRegisterProxies(LoopAnalysisManager &LAM, FunctionAnalysisManager &FAM, CGSCCAnalysisManager &CGAM, ModuleAnalysisManager &MAM, MachineFunctionAnalysisManager *MFAM=nullptr)
Cross register the analysis managers through their proxies.
ModulePassManager buildThinLTODefaultPipeline(OptimizationLevel Level, const ModuleSummaryIndex *ImportSummary)
Build a ThinLTO default optimization pipeline to a pass manager.
void registerModuleAnalyses(ModuleAnalysisManager &MAM)
Registers all available module analysis passes.
void registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM)
Registers all available CGSCC analysis passes.
void registerFunctionAnalyses(FunctionAnalysisManager &FAM)
Registers all available function analysis passes.
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...
LLVM_ATTRIBUTE_MINSIZE std::enable_if_t<!std::is_same_v< PassT, PassManager > > addPass(PassT &&Pass)
Definition: PassManager.h:195
PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs... ExtraArgs)
Run all of the passes in this manager over the given unit of IR.
Tunable parameters for passes in the default pipelines.
Definition: PassBuilder.h:44
bool SLPVectorization
Tuning option to enable/disable slp loop vectorization, set based on opt level.
Definition: PassBuilder.h:59
bool LoopVectorization
Tuning option to enable/disable loop vectorization, set based on opt level.
Definition: PassBuilder.h:55
Analysis providing profile information.
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:281
A non-threaded implementation.
Definition: ThreadPool.h:218
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
const char * c_str()
Definition: SmallString.h:259
bool empty() const
Definition: SmallVector.h:94
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
This class provides an interface to register all the standard pass instrumentations and manages their...
unsigned size() const
Definition: StringMap.h:104
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:128
bool contains(StringRef Key) const
contains - Return true if the element is in the map, false otherwise.
Definition: StringMap.h:273
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:23
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition: StringSet.h:38
Manages the enabling and disabling of subtarget specific features.
void getDefaultSubtargetFeatures(const Triple &Triple)
Adds the default features for the specified target triple.
std::string getString() const
Returns features as a string.
Analysis pass providing the TargetLibraryInfo.
Implementation of the target library information.
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
Target - Wrapper for Target specific information.
TargetMachine * createTargetMachine(StringRef TT, StringRef CPU, StringRef Features, const TargetOptions &Options, std::optional< Reloc::Model > RM, std::optional< CodeModel::Model > CM=std::nullopt, CodeGenOptLevel OL=CodeGenOptLevel::Default, bool JIT=false) const
createTargetMachine - Create a target specific machine implementation for the specified Triple.
void preserveSymbol(StringRef Name)
Adds to a list of all global symbols that must exist in the final generated code.
void run()
Process all the modules that were added to the code generator in parallel.
void crossReferenceSymbol(StringRef Name)
Adds to a list of all global symbols that are cross-referenced between ThinLTO files.
void addModule(StringRef Identifier, StringRef Data)
Add given module to the code generator.
auto async(Function &&F, Args &&...ArgList)
Asynchronous submission of a task to the pool.
Definition: ThreadPool.h:78
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
const std::string & str() const
Definition: Triple.h:440
std::string merge(const Triple &Other) const
Merge target triples.
Definition: Triple.cpp:2018
StringRef getArchName() const
Get the architecture (first) component of the triple.
Definition: Triple.cpp:1299
bool isCompatibleWith(const Triple &Other) const
Test whether target triples are compatible.
Definition: Triple.cpp:1994
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:97
PassManager manages ModulePassManagers.
void add(Pass *P) override
Add a pass to the queue of passes to run.
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
An input file.
Definition: LTO.h:111
static Expected< std::unique_ptr< InputFile > > create(MemoryBufferRef Object)
Create an InputFile.
Definition: LTO.cpp:553
BitcodeModule & getSingleBitcodeModule()
Definition: LTO.cpp:586
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:471
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:691
void optimize(Module &Module)
Perform post-importing ThinLTO optimizations.
std::unique_ptr< ModuleSummaryIndex > linkCombinedIndex()
Produce the combined summary index from all the bitcode files: "thin-link".
void crossModuleImport(Module &Module, ModuleSummaryIndex &Index, const lto::InputFile &File)
Perform cross-module importing for the module identified by ModuleIdentifier.
void emitImports(Module &Module, StringRef OutputName, ModuleSummaryIndex &Index, const lto::InputFile &File)
Compute and emit the imported files for module at ModulePath.
void gatherImportedSummariesForModule(Module &Module, ModuleSummaryIndex &Index, std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex, const lto::InputFile &File)
Compute the list of summaries needed for importing into module.
void internalize(Module &Module, ModuleSummaryIndex &Index, const lto::InputFile &File)
Perform internalization.
void promote(Module &Module, ModuleSummaryIndex &Index, const lto::InputFile &File)
Perform promotion and renaming of exported internal functions, and additionally resolve weak and link...
std::string writeGeneratedObject(int count, StringRef CacheEntryPath, const MemoryBuffer &OutputBuffer)
Write temporary object file to SavedObjectDirectoryPath, write symlink to Cache directory if needed.
Interfaces for registering analysis passes, producing common pass manager configurations,...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Key
PAL metadata keys.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
StringLiteral getThinLTODefaultCPU(const Triple &TheTriple)
Definition: LTO.cpp:1578
std::vector< int > generateModulesOrdering(ArrayRef< BitcodeModule * > R)
Produces a container ordering for optimal multi-threaded processing.
Definition: LTO.cpp:1931
Expected< std::unique_ptr< ToolOutputFile > > setupLLVMOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses, StringRef RemarksFormat, bool RemarksWithHotness, std::optional< uint64_t > RemarksHotnessThreshold=0, int Count=-1)
Setup optimization remarks.
Definition: LTO.cpp:1887
std::error_code closeFile(file_t &F)
Close the file object.
bool exists(const basic_file_status &status)
Does file exist?
Definition: Path.cpp:1078
@ OF_UpdateAtime
Force files Atime to be updated on access.
Definition: FileSystem.h:782
std::error_code create_hard_link(const Twine &to, const Twine &from)
Create a hard link from from to to, or return an error.
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
std::error_code create_directories(const Twine &path, bool IgnoreExisting=true, perms Perms=owner_all|group_all)
Create all the non-existent directories in path.
Definition: Path.cpp:968
std::error_code copy_file(const Twine &From, const Twine &To)
Copy the contents of From to To.
Definition: Path.cpp:1017
Expected< file_t > openNativeFileForRead(const Twine &Name, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
bool is_directory(const basic_file_status &status)
Does status represent a directory?
Definition: Path.cpp:1093
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:457
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
ThreadPoolStrategy heavyweight_hardware_concurrency(unsigned ThreadCount=0)
Returns a thread strategy for tasks requiring significant memory or other resources.
Definition: Threading.h:162
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition: Error.cpp:65
cl::opt< std::string > RemarksFormat("lto-pass-remarks-format", cl::desc("The format used for serializing remarks (default: YAML)"), cl::value_desc("format"), cl::init("yaml"))
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1722
void ComputeCrossModuleImport(const ModuleSummaryIndex &Index, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing, DenseMap< StringRef, FunctionImporter::ImportMapTy > &ImportLists, DenseMap< StringRef, FunctionImporter::ExportSetTy > &ExportLists)
Compute all the imports and exports for every module in the Index.
cl::opt< bool > LTODiscardValueNames("lto-discard-value-names", cl::desc("Strip names from Value during LTO (other than GlobalValue)."), cl::init(false), cl::Hidden)
void WriteBitcodeToFile(const Module &M, raw_ostream &Out, bool ShouldPreserveUseListOrder=false, const ModuleSummaryIndex *Index=nullptr, bool GenerateHash=false, ModuleHash *ModHash=nullptr)
Write the specified module to the specified raw output stream.
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:59
cl::opt< std::string > RemarksPasses("lto-pass-remarks-filter", cl::desc("Only record optimization remarks from passes whose " "names match the given regular expression"), cl::value_desc("regex"))
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:977
void writeIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out, const std::map< std::string, GVSummaryMapTy > *ModuleToSummariesForIndex=nullptr)
Write the specified module summary index to the given raw output stream, where it will be written in ...
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
auto formatv(const char *Fmt, Ts &&...Vals) -> formatv_object< decltype(std::make_tuple(support::detail::build_format_adapter(std::forward< Ts >(Vals))...))>
std::error_code EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename, const std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex)
Emit into OutputFilename the files module ModulePath will import from.
bool thinLTOPropagateFunctionAttrs(ModuleSummaryIndex &Index, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing)
Propagate function attributes for function summaries along the index's callgraph during thinlink.
ModuleSummaryIndex buildModuleSummaryIndex(const Module &M, std::function< BlockFrequencyInfo *(const Function &F)> GetBFICallback, ProfileSummaryInfo *PSI, std::function< const StackSafetyInfo *(const Function &F)> GetSSICallback=[](const Function &F) -> const StackSafetyInfo *{ return nullptr;})
Direct function to compute a ModuleSummaryIndex from a given module.
void reportAndResetTimings(raw_ostream *OutStream=nullptr)
If -time-passes has been specified, report the timings immediately and then reset the timers to zero.
@ DK_Linker
bool hasWholeProgramVisibility(bool WholeProgramVisibilityEnabledInLTO)
bool renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index, bool ClearDSOLocalOnDeclarations, SetVector< GlobalValue * > *GlobalsToImport=nullptr)
Perform in-place global value handling on the given Module for exported local functions renamed and p...
void thinLTOInternalizeAndPromoteInIndex(ModuleSummaryIndex &Index, function_ref< bool(StringRef, ValueInfo)> isExported, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing)
Update the linkages in the given Index to mark exported values as external and non-exported values as...
Definition: LTO.cpp:540
void computeLTOCacheKey(SmallString< 40 > &Key, const lto::Config &Conf, const ModuleSummaryIndex &Index, StringRef ModuleID, const FunctionImporter::ImportMapTy &ImportList, const FunctionImporter::ExportSetTy &ExportList, const std::map< GlobalValue::GUID, GlobalValue::LinkageTypes > &ResolvedODR, const GVSummaryMapTy &DefinedGlobals, const std::set< GlobalValue::GUID > &CfiFunctionDefs={}, const std::set< GlobalValue::GUID > &CfiFunctionDecls={})
Computes a unique hash for the Module considering the current list of export/import and other global ...
Definition: LTO.cpp:91
void updatePublicTypeTestCalls(Module &M, bool WholeProgramVisibilityEnabledInLTO)
Error write(MCStreamer &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue)
Definition: DWP.cpp:625
bool timeTraceProfilerEnabled()
Is the time trace profiler enabled, i.e. initialized?
Definition: TimeProfiler.h:104
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
Error writeToOutput(StringRef OutputFileName, std::function< Error(raw_ostream &)> Write)
This helper creates an output stream and then passes it to Write.
bool AreStatisticsEnabled()
Check if statistics are enabled.
Definition: Statistic.cpp:139
cl::opt< bool > RemarksWithHotness("lto-pass-remarks-with-hotness", cl::desc("With PGO, include profile count in optimization remarks"), cl::Hidden)
void computeSyntheticCounts(ModuleSummaryIndex &Index)
Compute synthetic function entry counts.
void timeTraceProfilerEnd()
Manually end the last time section.
cl::opt< std::string > RemarksFilename("lto-pass-remarks-output", cl::desc("Output filename for pass remarks"), cl::value_desc("filename"))
void updateIndexWPDForExports(ModuleSummaryIndex &Summary, function_ref< bool(StringRef, ValueInfo)> isExported, std::map< ValueInfo, std::vector< VTableSlotSummary > > &LocalWPDTargetsMap)
Call after cross-module importing to update the recorded single impl devirt target names for any loca...
void thinLTOResolvePrevailingInIndex(const lto::Config &C, ModuleSummaryIndex &Index, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing, function_ref< void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)> recordNewLinkage, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols)
Resolve linkage for prevailing symbols in the Index.
Definition: LTO.cpp:439
std::vector< std::unique_ptr< GlobalValueSummary > > GlobalValueSummaryList
void gatherImportedSummariesForModule(StringRef ModulePath, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, const FunctionImporter::ImportMapTy &ImportList, std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex)
Compute the set of summaries needed for a ThinLTO backend compilation of ModulePath.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
bool StripDebugInfo(Module &M)
Strip debug info in the module if it exists.
Definition: DebugInfo.cpp:591
@ Mod
The access may modify the value stored in memory.
void PrintStatistics()
Print statistics to the file returned by CreateInfoOutputFile().
Definition: Statistic.cpp:229
void runWholeProgramDevirtOnIndex(ModuleSummaryIndex &Summary, std::set< GlobalValue::GUID > &ExportedGUIDs, std::map< ValueInfo, std::vector< VTableSlotSummary > > &LocalWPDTargetsMap)
Perform index-based whole program devirtualization on the Summary index.
bool pruneCache(StringRef Path, CachePruningPolicy Policy, const std::vector< std::unique_ptr< MemoryBuffer > > &Files={})
Peform pruning using the supplied policy, returns true if pruning occurred, i.e.
void thinLTOInternalizeModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals)
Internalize TheModule based on the information recorded in the summaries during global summary-based ...
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:1914
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
@ DS_Warning
@ DS_Error
cl::opt< std::optional< uint64_t >, false, remarks::HotnessThresholdParser > RemarksHotnessThreshold("lto-pass-remarks-hotness-threshold", cl::desc("Minimum profile count required for an " "optimization remark to be output." " Use 'auto' to apply the threshold from profile summary."), cl::value_desc("uint or 'auto'"), cl::init(0), cl::Hidden)
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1749
Pass * createObjCARCContractPass()
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition: Error.cpp:117
void computeDeadSymbolsWithConstProp(ModuleSummaryIndex &Index, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols, function_ref< PrevailingType(GlobalValue::GUID)> isPrevailing, bool ImportEnabled)
Compute dead symbols and run constant propagation in combined index after that.
bool verifyModule(const Module &M, raw_ostream *OS=nullptr, bool *BrokenDebugInfo=nullptr)
Check a module for errors.
Definition: Verifier.cpp:7106
TimeTraceProfilerEntry * timeTraceProfilerBegin(StringRef Name, StringRef Detail)
Manually begin a time section, with the given Name and Detail.
void updateVCallVisibilityInIndex(ModuleSummaryIndex &Index, bool WholeProgramVisibilityEnabledInLTO, const DenseSet< GlobalValue::GUID > &DynamicExportSymbols, const DenseSet< GlobalValue::GUID > &VisibleToRegularObjSymbols)
If whole program visibility asserted, then upgrade all public vcall visibility metadata on vtable def...
void thinLTOFinalizeInModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals, bool PropagateAttrs)
Based on the information recorded in the summaries during global summary-based analysis:
Helper to gather options relevant to the target machine creation.
std::unique_ptr< TargetMachine > create() const
std::optional< Reloc::Model > RelocModel
static const Target * lookupTarget(StringRef Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
Struct that holds a reference to a particular GUID in a global value summary.
LTO configuration.
Definition: Config.h:41
std::vector< std::string > MAttrs
Definition: Config.h:50
CodeGenOptLevel CGOptLevel
Definition: Config.h:57
std::string CPU
Definition: Config.h:48
TargetOptions Options
Definition: Config.h:49
unsigned OptLevel
Definition: Config.h:59
std::optional< Reloc::Model > RelocModel
Definition: Config.h:55
bool Freestanding
Flag to indicate that the optimizer should not assume builtins are present on the target.
Definition: Config.h:65