LLVM 24.0.0git
LTO.h
Go to the documentation of this file.
1//===-LTO.h - 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 declares functions and classes used to support LTO. It is intended
10// to be used both by LTO classes as well as by clients (gold-plugin) that
11// don't utilize the LTO code generator interfaces.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_LTO_LTO_H
16#define LLVM_LTO_LTO_H
17
21#include <memory>
22
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/MapVector.h"
27#include "llvm/LTO/Config.h"
30#include "llvm/Support/Error.h"
33#include "llvm/Support/thread.h"
36
37namespace llvm {
38
39class Error;
40class IRMover;
41class LLVMContext;
42class MemoryBufferRef;
43class Module;
44class PassPlugin;
46class ToolOutputFile;
47
48/// Resolve linkage for prevailing symbols in the \p Index. Linkage changes
49/// recorded in the index and the ThinLTO backends must apply the changes to
50/// the module via thinLTOFinalizeInModule.
51///
52/// This is done for correctness (if value exported, ensure we always
53/// emit a copy), and compile-time optimization (allow drop of duplicates).
55 const lto::Config &C, ModuleSummaryIndex &Index,
57 isPrevailing,
59 recordNewLinkage,
60 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols);
61
62/// Update the linkages in the given \p Index to mark exported values
63/// as external and non-exported values as internal. The ThinLTO backends
64/// must apply the changes to the Module via thinLTOInternalizeModule.
66 ModuleSummaryIndex &Index,
67 function_ref<bool(StringRef, ValueInfo)> isExported,
69 isPrevailing,
70 DenseSet<StringRef> *ExternallyVisibleSymbolNamesPtr = nullptr);
71
72/// Computes a unique hash for the Module considering the current list of
73/// export/import and other global analysis results.
75 const lto::Config &Conf, const ModuleSummaryIndex &Index,
76 StringRef ModuleID, const FunctionImporter::ImportMapTy &ImportList,
77 const FunctionImporter::ExportSetTy &ExportList,
78 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
79 const GVSummaryMapTy &DefinedGlobals,
80 const DenseSet<GlobalValue::GUID> &CfiFunctionDefs = {},
81 const DenseSet<GlobalValue::GUID> &CfiFunctionDecls = {});
82
83/// Recomputes the LTO cache key for a given key with an extra identifier.
84LLVM_ABI std::string recomputeLTOCacheKey(const std::string &Key,
85 StringRef ExtraID);
86
87namespace lto {
88
89LLVM_ABI StringLiteral getThinLTODefaultCPU(const Triple &TheTriple);
90
91/// Given the original \p Path to an output file, replace any path
92/// prefix matching \p OldPrefix with \p NewPrefix. Also, create the
93/// resulting directory if it does not yet exist.
94LLVM_ABI std::string getThinLTOOutputFile(StringRef Path, StringRef OldPrefix,
95 StringRef NewPrefix);
96
97/// Setup optimization remarks.
98LLVM_ABI Expected<LLVMRemarkFileHandle> setupLLVMOptimizationRemarks(
99 LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses,
100 StringRef RemarksFormat, bool RemarksWithHotness,
101 std::optional<uint64_t> RemarksHotnessThreshold = 0, int Count = -1);
102
103/// Setups the output file for saving statistics.
104LLVM_ABI Expected<std::unique_ptr<ToolOutputFile>>
105setupStatsFile(StringRef StatsFilename);
106
107/// Produces a container ordering for optimal multi-threaded processing. Returns
108/// ordered indices to elements in the input array.
110
111class LTO;
112struct SymbolResolution;
113
114/// An input file. This is a symbol table wrapper that only exposes the
115/// information that an LTO client should need in order to do symbol resolution.
116class InputFile {
117public:
118 struct Symbol;
119
120private:
121 // FIXME: Remove LTO class friendship once we have bitcode symbol tables.
122 friend LTO;
123 InputFile() = default;
124
125 std::vector<BitcodeModule> Mods;
127 std::vector<Symbol> Symbols;
128
129 // [begin, end) for each module
130 std::vector<std::pair<size_t, size_t>> ModuleSymIndices;
131
132 StringRef TargetTriple, SourceFileName, COFFLinkerOpts;
133 std::vector<StringRef> DependentLibraries;
134 std::vector<std::pair<StringRef, Comdat::SelectionKind>> ComdatTable;
135
136 MemoryBufferRef MbRef;
137 bool IsFatLTOObject = false;
138 // For distributed compilation, each input must exist as an individual
139 // bitcode file on disk identified by its ModuleID. For archive members and
140 // FatLTO objects, the input bitcode is a sub-section of a larger file. In
141 // these cases we flag that the bitcode must be written to a temporary
142 // standalone file. Effectively, extracted from its container.
143 bool ExtractForDistribution = false;
144 bool IsThinLTO = false;
145 StringRef ArchivePath;
146 StringRef MemberName;
147
148public:
150
151 /// Create an InputFile.
153 create(MemoryBufferRef Object);
154
155 /// The purpose of this struct is to only expose the symbol information that
156 /// an LTO client should need in order to do symbol resolution.
158 friend LTO;
159
160 public:
162
179
180 // Returns whether this symbol is a library call that LTO code generation
181 // may emit references to. Such symbols must be considered external, as
182 // removing them or modifying their interfaces would invalidate the code
183 // generator's knowledge about them.
185 const RTLIB::RuntimeLibcallsInfo &Libcalls) const;
186 };
187
188 /// A range over the symbols in this InputFile.
189 ArrayRef<Symbol> symbols() const { return Symbols; }
190
191 /// Returns linker options specified in the input file.
192 StringRef getCOFFLinkerOpts() const { return COFFLinkerOpts; }
193
194 /// Returns dependent library specifiers from the input file.
195 ArrayRef<StringRef> getDependentLibraries() const { return DependentLibraries; }
196
197 /// Returns the path to the InputFile.
198 LLVM_ABI StringRef getName() const;
199
200 /// Returns the input file's target triple.
201 StringRef getTargetTriple() const { return TargetTriple; }
202
203 /// Returns the source file path specified at compile time.
204 StringRef getSourceFileName() const { return SourceFileName; }
205
206 // Returns a table with all the comdats used by this file.
210
211 // Returns the only BitcodeModule from InputFile.
213 // Returns the primary BitcodeModule from InputFile.
215 // Returns the memory buffer reference for this input file.
216 MemoryBufferRef getFileBuffer() const { return MbRef; }
217 // Returns true if this input should be extracted to disk for distribution.
218 // See the comment on ExtractForDistribution for details.
219 bool getExtractForDistribution() const { return ExtractForDistribution; }
220 // Mark whether this input should be extracted to disk for distribution.
221 // See the comment on ExtractForDistribution for details.
222 void setExtractForDistribution(bool EFD) { ExtractForDistribution = EFD; }
223 // Returns true if this bitcode came from a FatLTO object.
224 bool isFatLTOObject() const { return IsFatLTOObject; }
225 // Mark this bitcode as coming from a FatLTO object.
226 void fatLTOObject(bool FO) { IsFatLTOObject = FO; }
227
228 // Returns true if bitcode is ThinLTO.
229 bool isThinLTO() const { return IsThinLTO; }
230
231 // Store an archive path and a member name.
233 ArchivePath = Path;
234 MemberName = Name;
235 }
236 StringRef getArchivePath() const { return ArchivePath; }
237 StringRef getMemberName() const { return MemberName; }
238
239private:
240 ArrayRef<Symbol> module_symbols(unsigned I) const {
241 const auto &Indices = ModuleSymIndices[I];
242 return {Symbols.data() + Indices.first, Symbols.data() + Indices.second};
243 }
244};
245
246using IndexWriteCallback = std::function<void(const std::string &)>;
247
249
250/// This class defines the interface to the ThinLTO backend.
252protected:
253 const Config &Conf;
259 std::optional<Error> Err;
260 std::mutex ErrMu;
261
262public:
272
273 virtual ~ThinBackendProc() = default;
274 virtual void setup(unsigned ThinLTONumTasks, unsigned ThinLTOTaskOffset,
275 Triple Triple) {}
276 virtual Error start(
277 unsigned Task, BitcodeModule BM,
278 const FunctionImporter::ImportMapTy &ImportList,
279 const FunctionImporter::ExportSetTy &ExportList,
280 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
282 virtual Error wait() {
283 BackendThreadPool.wait();
284 if (Err)
285 return std::move(*Err);
286 return Error::success();
287 }
288 unsigned getThreadCount() { return BackendThreadPool.getMaxConcurrency(); }
289 virtual bool isSensitiveToInputOrder() { return false; }
290
291 // Write sharded indices and (optionally) imports to disk
293 unsigned Task, StringRef ModulePath,
294 const std::string &NewModulePath) const;
295
296 // Write sharded indices to SummaryPath, (optionally) imports to disk, and
297 // (optionally) record imports in ImportsFilesList.
299 unsigned Task, StringRef ModulePath,
300 const std::string &NewModulePath,
301 StringRef SummaryPath) const;
302};
303
304/// This callable defines the behavior of a ThinLTO backend after the thin-link
305/// phase. It accepts a configuration \p C, a combined module summary index
306/// \p CombinedIndex, a map of module identifiers to global variable summaries
307/// \p ModuleToDefinedGVSummaries, a function to add output streams \p
308/// AddStream, and a file cache \p Cache. It returns a unique pointer to a
309/// ThinBackendProc, which can be used to launch backends in parallel.
310using ThinBackendFunction = std::function<std::unique_ptr<ThinBackendProc>(
311 const Config &C, ModuleSummaryIndex &CombinedIndex,
312 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
313 AddStreamFn AddStream, FileCache Cache,
314 ArrayRef<StringRef> BitcodeLibFuncs)>;
315
316/// This type defines the behavior following the thin-link phase during ThinLTO.
317/// It encapsulates a backend function and a strategy for thread pool
318/// parallelism. Clients should use one of the provided create*ThinBackend()
319/// functions to instantiate a ThinBackend. Parallelism defines the thread pool
320/// strategy to be used for processing.
323 : Func(std::move(Func)), Parallelism(std::move(Parallelism)) {}
324 ThinBackend() = default;
325
326 std::unique_ptr<ThinBackendProc> operator()(
327 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
328 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
329 AddStreamFn AddStream, FileCache Cache,
330 ArrayRef<StringRef> BitcodeLibFuncs) {
331 assert(isValid() && "Invalid backend function");
332 return Func(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
333 std::move(AddStream), std::move(Cache), BitcodeLibFuncs);
334 }
335 ThreadPoolStrategy getParallelism() const { return Parallelism; }
336 bool isValid() const { return static_cast<bool>(Func); }
337
338private:
339 ThinBackendFunction Func = nullptr;
340 ThreadPoolStrategy Parallelism;
341};
342
343/// This ThinBackend runs the individual backend jobs in-process.
344/// The default value means to use one job per hardware core (not hyper-thread).
345/// OnWrite is callback which receives module identifier and notifies LTO user
346/// that index file for the module (and optionally imports file) was created.
347/// ShouldEmitIndexFiles being true will write sharded ThinLTO index files
348/// to the same path as the input module, with suffix ".thinlto.bc"
349/// ShouldEmitImportsFiles is true it also writes a list of imported files to a
350/// similar path with ".imports" appended instead.
352 ThreadPoolStrategy Parallelism, IndexWriteCallback OnWrite = nullptr,
353 bool ShouldEmitIndexFiles = false, bool ShouldEmitImportsFiles = false);
354
355/// This ThinBackend writes individual module indexes to files, instead of
356/// running the individual backend jobs. This backend is for distributed builds
357/// where separate processes will invoke the real backends.
358///
359/// To find the path to write the index to, the backend checks if the path has a
360/// prefix of OldPrefix; if so, it replaces that prefix with NewPrefix. It then
361/// appends ".thinlto.bc" and writes the index to that path. If
362/// ShouldEmitImportsFiles is true it also writes a list of imported files to a
363/// similar path with ".imports" appended instead.
364/// LinkedObjectsFile is an output stream to write the list of object files for
365/// the final ThinLTO linking. Can be nullptr. If LinkedObjectsFile is not
366/// nullptr and NativeObjectPrefix is not empty then it replaces the prefix of
367/// the objects with NativeObjectPrefix instead of NewPrefix. OnWrite is
368/// callback which receives module identifier and notifies LTO user that index
369/// file for the module (and optionally imports file) was created.
371 ThreadPoolStrategy Parallelism, std::string OldPrefix,
372 std::string NewPrefix, std::string NativeObjectPrefix,
373 bool ShouldEmitImportsFiles, raw_fd_ostream *LinkedObjectsFile,
374 IndexWriteCallback OnWrite);
375
376/// This class implements a resolution-based interface to LLVM's LTO
377/// functionality. It supports regular LTO, parallel LTO code generation and
378/// ThinLTO. You can use it from a linker in the following way:
379/// - Set hooks and code generation options (see lto::Config struct defined in
380/// Config.h), and use the lto::Config object to create an lto::LTO object.
381/// - Create lto::InputFile objects using lto::InputFile::create(), then use
382/// the symbols() function to enumerate its symbols and compute a resolution
383/// for each symbol (see SymbolResolution below).
384/// - After the linker has visited each input file (and each regular object
385/// file) and computed a resolution for each symbol, take each lto::InputFile
386/// and pass it and an array of symbol resolutions to the add() function.
387/// - Call the getMaxTasks() function to get an upper bound on the number of
388/// native object files that LTO may add to the link.
389/// - Call the run() function. This function will use the supplied AddStream
390/// and Cache functions to add up to getMaxTasks() native object files to
391/// the link.
393 friend InputFile;
394
395public:
396 /// Unified LTO modes
397 enum LTOKind {
398 /// Any LTO mode without Unified LTO. The default mode.
400
401 /// Regular LTO, with Unified LTO enabled.
403
404 /// ThinLTO, with Unified LTO enabled.
406 };
407
408 /// Create an LTO object. A default constructed LTO object has a reasonable
409 /// production configuration, but you can customize it by passing arguments to
410 /// this constructor.
411 /// FIXME: We do currently require the DiagHandler field to be set in Conf.
412 /// Until that is fixed, a Config argument is required.
413 LTO(Config Conf, ThinBackend Backend = {},
414 unsigned ParallelCodeGenParallelismLevel = 1,
415 LTOKind LTOMode = LTOK_Default);
416 virtual ~LTO();
417
418 /// Add an input file to the LTO link, using the provided symbol resolutions.
419 /// The symbol resolutions must appear in the enumeration order given by
420 /// InputFile::symbols().
421 Error add(std::unique_ptr<InputFile> Obj, ArrayRef<SymbolResolution> Res);
422
423 /// Set the list of functions implemented in bitcode that were not extracted
424 /// from an archive. Such functions may not be referenced, as they have
425 /// lost their opportunity to be defined.
426 void setBitcodeLibFuncs(ArrayRef<StringRef> BitcodeLibFuncs);
427
428 /// Returns an upper bound on the number of tasks that the client may expect.
429 /// This may only be called after all IR object files have been added. For a
430 /// full description of tasks see LTOBackend.h.
431 unsigned getMaxTasks() const;
432
433 /// Runs the LTO pipeline. This function calls the supplied AddStream
434 /// function to add native object files to the link.
435 ///
436 /// The Cache parameter is optional. If supplied, it will be used to cache
437 /// native object files and add them to the link.
438 ///
439 /// The client will receive at most one callback (via either AddStream or
440 /// Cache) for each task identifier.
441 virtual Error run(AddStreamFn AddStream, FileCache Cache = {});
442
443 /// Wait for cleanup work started by run() to finish.
444 ///
445 /// A client may delay this call to overlap asynchronous cleanup with later
446 /// linking work, but must call it before finalizing time trace data because
447 /// cleanup may emit time trace events. Most LTO implementations have no
448 /// asynchronous cleanup.
449 virtual void waitForCleanup() {}
450
451 /// Static method that returns a list of libcall symbols that can be generated
452 /// by LTO but might not be visible from bitcode symbol table.
453 static SmallVector<const char *> getRuntimeLibcallSymbols(const Triple &TT);
454
455 /// Static method that returns a list of library function symbols that can be
456 /// generated by LTO but might not be visible from bitcode symbol table.
457 /// Unlike the runtime libcalls, the linker can report to the code generator
458 /// which of these are actually available in the link, and the code generator
459 /// can then only reference that set of symbols.
460 static SmallVector<StringRef> getLibFuncSymbols(const Triple &TT,
461 llvm::StringSaver &Saver);
462
463protected:
464 // Called before returning from run().
465 virtual void cleanup();
466
468
471 const Config &Conf);
475 /// Record if at least one instance of the common was marked as prevailing
476 bool Prevailing = false;
477 };
478 std::map<std::string, CommonResolution> Commons;
479
482 std::unique_ptr<Module> CombinedModule;
483 std::unique_ptr<IRMover> Mover;
484
485 // This stores the information about a regular LTO module that we have added
486 // to the link. It will either be linked immediately (for modules without
487 // summaries) or after summary-based dead stripping (for modules with
488 // summaries).
489 struct AddedModule {
490 std::unique_ptr<Module> M;
491 std::vector<GlobalValue *> Keep;
492 };
493 std::vector<AddedModule> ModsWithSummaries;
495 } RegularLTO;
496
498
501
504 // The full set of bitcode modules in input order.
506 // The bitcode modules to compile, if specified by the LTO Config.
507 std::optional<ModuleMapType> ModulesToCompile;
508
510 PrevailingModuleForGUID[GUID] = Module;
511 }
513 StringRef Module) const {
514 auto It = PrevailingModuleForGUID.find(GUID);
515 return It != PrevailingModuleForGUID.end() && It->second == Module;
516 }
517
518 private:
519 // Make this private so all accesses must go through above accessor methods
520 // to avoid inadvertently creating new entries on lookups.
521 DenseMap<GlobalValue::GUID, StringRef> PrevailingModuleForGUID;
522 } ThinLTO;
523
524private:
525 // The global resolution for a particular (mangled) symbol name. This is in
526 // particular necessary to track whether each symbol can be internalized.
527 // Because any input file may introduce a new cross-partition reference, we
528 // cannot make any final internalization decisions until all input files have
529 // been added and the client has called run(). During run() we apply
530 // internalization decisions either directly to the module (for regular LTO)
531 // or to the combined index (for ThinLTO).
532 // FIXME: Make this GlobalResolution a class, it has been becoming more than
533 // just a data bag.
534 struct GlobalResolution {
535 /// The unmangled name of the global.
536 std::string IRName;
537
538 /// Keep track if the symbol is visible outside of a module with a summary
539 /// (i.e. in either a regular object or a regular LTO module without a
540 /// summary).
541 bool VisibleOutsideSummary = false;
542
543 /// The symbol was exported dynamically, and therefore could be referenced
544 /// by a shared library not visible to the linker.
545 bool ExportDynamic = false;
546
547 bool UnnamedAddr = true;
548
549 /// True if module contains the prevailing definition.
550 bool Prevailing = false;
551
552 /// Returns true if module contains the prevailing definition and symbol is
553 /// an IR symbol. For example when module-level inline asm block is used,
554 /// symbol can be prevailing in module but have no IR name.
555 bool isPrevailingIRSymbol() const { return Prevailing && !IRName.empty(); }
556
557 /// This field keeps track of the partition number of this global. The
558 /// regular LTO object is partition 0, while each ThinLTO object has its own
559 /// partition number from 1 onwards.
560 ///
561 /// Any global that is defined or used by more than one partition, or that
562 /// is referenced externally, may not be internalized.
563 ///
564 /// Partitions generally have a one-to-one correspondence with tasks, except
565 /// that we use partition 0 for all parallel LTO code generation partitions.
566 /// Any partitioning of the combined LTO object is done internally by the
567 /// LTO backend.
568 unsigned Partition = Unknown;
569
570 private:
571 GlobalValue::GUID GUID = 0;
572
573 public:
574 void setGUID(GlobalValue::GUID G) {
575 assert(G);
576 assert(!GUID || GUID == G);
577 GUID = G;
578 }
579
580 GlobalValue::GUID getGUID() const {
581 return GUID ? GUID
582 : GlobalValue::getGUIDAssumingExternalLinkage(
583 GlobalValue::getGlobalIdentifier(
584 IRName, GlobalValue::LinkageTypes::ExternalLinkage,
585 ""));
586 }
587
588 /// Special partition numbers.
589 enum : unsigned {
590 /// A partition number has not yet been assigned to this global.
591 Unknown = -1u,
592
593 /// This global is either used by more than one partition or has an
594 /// external reference, and therefore cannot be internalized.
595 External = -2u,
596
597 /// The RegularLTO partition
598 RegularLTO = 0,
599 };
600 };
601
602 // GlobalResolutionSymbolSaver allocator.
603 std::unique_ptr<llvm::BumpPtrAllocator> Alloc;
604
605 // Symbol saver for global resolution map.
606 std::unique_ptr<llvm::StringSaver> GlobalResolutionSymbolSaver;
607
608 // Global mapping from mangled symbol names to resolutions.
609 // Make this an unique_ptr to guard against accessing after it has been reset
610 // (to reduce memory after we're done with it).
611 std::unique_ptr<llvm::DenseMap<StringRef, GlobalResolution>>
612 GlobalResolutions;
613
614 void releaseGlobalResolutionsMemory();
615
616 void addModuleToGlobalRes(ArrayRef<InputFile::Symbol> Syms,
617 ArrayRef<SymbolResolution> Res, unsigned Partition,
618 bool InSummary, const Triple &TT);
619
620 // These functions take a range of symbol resolutions and consume the
621 // resolutions used by a single input module. Functions return ranges refering
622 // to the resolutions for the remaining modules in the InputFile.
623 Expected<ArrayRef<SymbolResolution>>
624 addModule(InputFile &Input, ArrayRef<SymbolResolution> InputRes,
625 unsigned ModI, ArrayRef<SymbolResolution> Res);
626
627 Expected<std::pair<RegularLTOState::AddedModule, ArrayRef<SymbolResolution>>>
628 addRegularLTO(InputFile &Input, ArrayRef<SymbolResolution> InputRes,
629 BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
630 ArrayRef<SymbolResolution> Res);
631 Error linkRegularLTO(RegularLTOState::AddedModule Mod,
632 bool LivenessFromIndex);
633
634 Expected<ArrayRef<SymbolResolution>>
635 addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
636 ArrayRef<SymbolResolution> Res);
637
638 Error runRegularLTO(AddStreamFn AddStream);
639 Error runThinLTO(AddStreamFn AddStream, FileCache Cache,
640 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols);
641
642 Error checkPartiallySplit();
643
644 mutable bool CalledGetMaxTasks = false;
645
646protected:
647 // LTO mode when using Unified LTO.
649
650private:
651 // Use Optional to distinguish false from not yet initialized.
652 std::optional<bool> EnableSplitLTOUnit;
653
654 // Identify symbols exported dynamically, and that therefore could be
655 // referenced by a shared library not visible to the linker.
656 DenseSet<GlobalValue::GUID> DynamicExportSymbols;
657
658 // Diagnostic optimization remarks file
659 LLVMRemarkFileHandle DiagnosticOutputFile;
660
661 // A dummy module to host the dummy function.
662 std::unique_ptr<Module> DummyModule;
663
664 // A dummy function created in a private module to provide a context for
665 // LTO-link optimization remarks. This is needed for ThinLTO where we
666 // may not have any IR functions available, because the optimization remark
667 // handling requires a function.
668 Function *LinkerRemarkFunction = nullptr;
669
670 // Setup optimization remarks according to the provided configuration.
671 Error setupOptimizationRemarks();
672
673 // Pass-plugins that we loaded from the provided configuration.
675
676 // Load pass-plugins from the provided configuration.
677 Error setupPassPlugins();
678
679 // LibFuncs that were implemented in bitcode but were not extracted
680 // from their libraries. Such functions cannot safely be called, since
681 // they have lost their opportunity to be defined.
682 SmallVector<StringRef> BitcodeLibFuncs;
683
684public:
685 /// Helper to emit an optimization remark during the LTO link when outside of
686 /// the standard optimization pass pipeline.
688
690 addInput(std::unique_ptr<lto::InputFile> InputPtr) {
691 return std::shared_ptr<lto::InputFile>(InputPtr.release());
692 }
693};
694
695/// The resolution for a symbol. The linker must provide a SymbolResolution for
696/// each global symbol based on its internal resolution of that symbol.
701
702 /// The linker has chosen this definition of the symbol.
703 unsigned Prevailing : 1;
704
705 /// The definition of this symbol is unpreemptable at runtime and is known to
706 /// be in this linkage unit.
708
709 /// The definition of this symbol is visible outside of the LTO unit.
711
712 /// The symbol was exported dynamically, and therefore could be referenced
713 /// by a shared library not visible to the linker.
714 unsigned ExportDynamic : 1;
715
716 /// Linker redefined version of the symbol which appeared in -wrap or -defsym
717 /// linker option.
718 unsigned LinkerRedefined : 1;
719};
720
721} // namespace lto
722} // namespace llvm
723
724#endif
const TargetLowering & TLI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
static void cleanup(BlockFrequencyInfoImplBase &BFI)
Clear all memory not needed downstream.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_ABI
Definition Compiler.h:211
This file defines the DenseMap class.
Provides passes for computing function attributes based on interprocedural analyses.
static void emitRemark(const Function &F, OptimizationRemarkEmitter &ORE, bool Skip)
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
This file implements a map that provides insertion order iteration.
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
if(auto Err=PB.parsePassPipeline(MPM, Passes)) return wrap(std MPM run * Mod
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Represents a module in a bitcode file.
Implements a dense probed hash-table based set.
Definition DenseSet.h:283
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
The map maintains the list of imports.
DenseSet< ValueInfo > ExportSetTy
The set contains an entry for every global value that the module exports.
Function and variable summary information to aid decisions and implementation of importing.
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition GlobalValue.h:52
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
RAII handle that manages the lifetime of the ToolOutputFile used to output remarks.
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:39
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:68
Diagnostic information for applied optimization remarks.
A loaded pass plugin.
Definition PassPlugin.h:97
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.
Definition StringSaver.h:22
Provides information about what library functions are available for the current target.
This tells how a thread pool will be used.
Definition Threading.h:115
This class contains a raw_fd_ostream and adds a few extra features commonly needed for compiler-like ...
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
An efficient, type-erasing, non-owning reference to a callable.
LLVM_ABI BitcodeModule & getPrimaryBitcodeModule()
Definition LTO.cpp:676
MemoryBufferRef getFileBuffer() const
Definition LTO.h:216
static LLVM_ABI Expected< std::unique_ptr< InputFile > > create(MemoryBufferRef Object)
Create an InputFile.
Definition LTO.cpp:628
ArrayRef< Symbol > symbols() const
A range over the symbols in this InputFile.
Definition LTO.h:189
StringRef getCOFFLinkerOpts() const
Returns linker options specified in the input file.
Definition LTO.h:192
bool getExtractForDistribution() const
Definition LTO.h:219
bool isFatLTOObject() const
Definition LTO.h:224
ArrayRef< StringRef > getDependentLibraries() const
Returns dependent library specifiers from the input file.
Definition LTO.h:195
StringRef getArchivePath() const
Definition LTO.h:236
StringRef getMemberName() const
Definition LTO.h:237
ArrayRef< std::pair< StringRef, Comdat::SelectionKind > > getComdatTable() const
Definition LTO.h:207
void setExtractForDistribution(bool EFD)
Definition LTO.h:222
StringRef getTargetTriple() const
Returns the input file's target triple.
Definition LTO.h:201
LLVM_ABI StringRef getName() const
Returns the path to the InputFile.
Definition LTO.cpp:667
LLVM_ABI BitcodeModule & getSingleBitcodeModule()
Definition LTO.cpp:671
StringRef getSourceFileName() const
Returns the source file path specified at compile time.
Definition LTO.h:204
bool isThinLTO() const
Definition LTO.h:229
void fatLTOObject(bool FO)
Definition LTO.h:226
void setArchivePathAndName(StringRef Path, StringRef Name)
Definition LTO.h:232
This class implements a resolution-based interface to LLVM's LTO functionality.
Definition LTO.h:392
LTO(Config Conf, ThinBackend Backend={}, unsigned ParallelCodeGenParallelismLevel=1, LTOKind LTOMode=LTOK_Default)
Create an LTO object.
Definition LTO.cpp:691
Error add(std::unique_ptr< InputFile > Obj, ArrayRef< SymbolResolution > Res)
Add an input file to the LTO link, using the provided symbol resolutions.
Definition LTO.cpp:830
virtual Expected< std::shared_ptr< lto::InputFile > > addInput(std::unique_ptr< lto::InputFile > InputPtr)
Definition LTO.h:690
Config Conf
Definition LTO.h:467
void setBitcodeLibFuncs(ArrayRef< StringRef > BitcodeLibFuncs)
Set the list of functions implemented in bitcode that were not extracted from an archive.
Definition LTO.cpp:861
MapVector< StringRef, BitcodeModule > ModuleMapType
Definition LTO.h:497
LTOKind
Unified LTO modes.
Definition LTO.h:397
@ LTOK_UnifiedRegular
Regular LTO, with Unified LTO enabled.
Definition LTO.h:402
@ LTOK_Default
Any LTO mode without Unified LTO. The default mode.
Definition LTO.h:399
@ LTOK_UnifiedThin
ThinLTO, with Unified LTO enabled.
Definition LTO.h:405
virtual ~LTO()
LTOKind LTOMode
Definition LTO.h:648
unsigned getMaxTasks() const
Returns an upper bound on the number of tasks that the client may expect.
Definition LTO.cpp:1269
virtual Error run(AddStreamFn AddStream, FileCache Cache={})
Runs the LTO pipeline.
Definition LTO.cpp:1347
virtual void waitForCleanup()
Wait for cleanup work started by run() to finish.
Definition LTO.h:449
DefaultThreadPool BackendThreadPool
Definition LTO.h:258
const Config & Conf
Definition LTO.h:253
std::optional< Error > Err
Definition LTO.h:259
virtual bool isSensitiveToInputOrder()
Definition LTO.h:289
unsigned getThreadCount()
Definition LTO.h:288
const DenseMap< StringRef, GVSummaryMapTy > & ModuleToDefinedGVSummaries
Definition LTO.h:255
ThinBackendProc(const Config &Conf, ModuleSummaryIndex &CombinedIndex, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, lto::IndexWriteCallback OnWrite, bool ShouldEmitImportsFiles, ThreadPoolStrategy ThinLTOParallelism)
Definition LTO.h:263
virtual Error wait()
Definition LTO.h:282
ModuleSummaryIndex & CombinedIndex
Definition LTO.h:254
virtual void setup(unsigned ThinLTONumTasks, unsigned ThinLTOTaskOffset, Triple Triple)
Definition LTO.h:274
virtual ~ThinBackendProc()=default
virtual Error start(unsigned Task, BitcodeModule BM, const FunctionImporter::ImportMapTy &ImportList, const FunctionImporter::ExportSetTy &ExportList, const std::map< GlobalValue::GUID, GlobalValue::LinkageTypes > &ResolvedODR, MapVector< StringRef, BitcodeModule > &ModuleMap)=0
LLVM_ABI Error emitFiles(const FunctionImporter::ImportMapTy &ImportList, unsigned Task, StringRef ModulePath, const std::string &NewModulePath) const
Definition LTO.cpp:1574
IndexWriteCallback OnWrite
Definition LTO.h:256
A raw_ostream that writes to a file descriptor.
An abstract base class for streams implementations that also support a pwrite operation.
LLVM_ABI ThinBackend createInProcessThinBackend(ThreadPoolStrategy Parallelism, IndexWriteCallback OnWrite=nullptr, bool ShouldEmitIndexFiles=false, bool ShouldEmitImportsFiles=false)
This ThinBackend runs the individual backend jobs in-process.
Definition LTO.cpp:1935
LLVM_ABI std::string getThinLTOOutputFile(StringRef Path, StringRef OldPrefix, StringRef NewPrefix)
Given the original Path to an output file, replace any path prefix matching OldPrefix with NewPrefix.
Definition LTO.cpp:1970
std::function< std::unique_ptr< ThinBackendProc >( const Config &C, ModuleSummaryIndex &CombinedIndex, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, AddStreamFn AddStream, FileCache Cache, ArrayRef< StringRef > BitcodeLibFuncs)> ThinBackendFunction
This callable defines the behavior of a ThinLTO backend after the thin-link phase.
Definition LTO.h:310
LLVM_ABI StringLiteral getThinLTODefaultCPU(const Triple &TheTriple)
Definition LTO.cpp:1952
LLVM_ABI Expected< std::unique_ptr< ToolOutputFile > > setupStatsFile(StringRef StatsFilename)
Setups the output file for saving statistics.
Definition LTO.cpp:2408
std::function< void(const std::string &)> IndexWriteCallback
Definition LTO.h:246
LLVM_ABI ThinBackend createWriteIndexesThinBackend(ThreadPoolStrategy Parallelism, std::string OldPrefix, std::string NewPrefix, std::string NativeObjectPrefix, bool ShouldEmitImportsFiles, raw_fd_ostream *LinkedObjectsFile, IndexWriteCallback OnWrite)
This ThinBackend writes individual module indexes to files, instead of running the individual backend...
Definition LTO.cpp:2077
LLVM_ABI Expected< LLVMRemarkFileHandle > 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:2383
LLVM_ABI std::vector< int > generateModulesOrdering(ArrayRef< BitcodeModule * > R)
Produces a container ordering for optimal multi-threaded processing.
Definition LTO.cpp:2427
llvm::SmallVector< std::string > ImportsFilesContainer
Definition LTO.h:248
This is an optimization pass for GlobalISel generic memory operations.
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"))
@ Unknown
Not known to have no common set bits.
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"))
DenseMap< GlobalValue::GUID, GlobalValueSummary * > GVSummaryMapTy
Map of global value GUID to its summary, used to identify values defined in a particular module,...
LLVM_ABI std::string recomputeLTOCacheKey(const std::string &Key, StringRef ExtraID)
Recomputes the LTO cache key for a given key with an extra identifier.
Definition LTO.cpp:392
cl::opt< bool > RemarksWithHotness("lto-pass-remarks-with-hotness", cl::desc("With PGO, include profile count in optimization remarks"), cl::Hidden)
cl::opt< std::string > RemarksFilename("lto-pass-remarks-output", cl::desc("Output filename for pass remarks"), cl::value_desc("filename"))
LLVM_ABI 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:492
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
SingleThreadExecutor DefaultThreadPool
Definition ThreadPool.h:262
ArrayRef(const T &OneElt) -> ArrayRef< T >
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1933
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)
LLVM_ABI std::string computeLTOCacheKey(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 DenseSet< GlobalValue::GUID > &CfiFunctionDefs={}, const DenseSet< GlobalValue::GUID > &CfiFunctionDecls={})
Computes a unique hash for the Module considering the current list of export/import and other global ...
Definition LTO.cpp:137
std::function< Expected< std::unique_ptr< CachedFileStream > >( unsigned Task, const Twine &ModuleName)> AddStreamFn
This type defines the callback to add a file that is generated on the fly.
Definition Caching.h:79
LLVM_ABI void thinLTOInternalizeAndPromoteInIndex(ModuleSummaryIndex &Index, function_ref< bool(StringRef, ValueInfo)> isExported, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing, DenseSet< StringRef > *ExternallyVisibleSymbolNamesPtr=nullptr)
Update the linkages in the given Index to mark exported values as external and non-exported values as...
Definition LTO.cpp:610
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:879
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
This type represents a file cache system that manages caching of files.
Definition Caching.h:105
A simple container for information about the supported runtime calls.
Struct that holds a reference to a particular GUID in a global value summary.
This represents a symbol that has been read from a storage::Symbol and possibly a storage::Uncommon.
Definition IRSymtab.h:173
StringRef getName() const
Returns the mangled symbol name.
Definition IRSymtab.h:187
bool canBeOmittedFromSymbolTable() const
Definition IRSymtab.h:210
bool isUsed() const
Definition IRSymtab.h:207
StringRef getSectionName() const
Definition IRSymtab.h:236
bool isTLS() const
Definition IRSymtab.h:208
bool isWeak() const
Definition IRSymtab.h:204
bool isIndirect() const
Definition IRSymtab.h:206
bool isCommon() const
Definition IRSymtab.h:205
uint32_t getCommonAlignment() const
Definition IRSymtab.h:224
bool isExecutable() const
Definition IRSymtab.h:217
uint64_t getCommonSize() const
Definition IRSymtab.h:219
storage::Symbol S
Definition IRSymtab.h:197
int getComdatIndex() const
Returns the index into the comdat table (see Reader::getComdatTable()), or -1 if not a comdat member.
Definition IRSymtab.h:195
GlobalValue::VisibilityTypes getVisibility() const
Definition IRSymtab.h:199
bool isUndefined() const
Definition IRSymtab.h:203
StringRef getIRName() const
Returns the unmangled symbol name, or the empty string if this is not an IR symbol.
Definition IRSymtab.h:191
StringRef getCOFFWeakExternalFallback() const
COFF-specific: for weak externals, returns the name of the symbol that is used as a fallback if the w...
Definition IRSymtab.h:231
LTO configuration.
Definition Config.h:44
The purpose of this struct is to only expose the symbol information that an LTO client should need in...
Definition LTO.h:157
LLVM_ABI bool isLibcall(const TargetLibraryInfo &TLI, const RTLIB::RuntimeLibcallsInfo &Libcalls) const
Definition LTO.cpp:659
Symbol(const irsymtab::Symbol &S)
Definition LTO.h:161
A derived class of LLVMContext that initializes itself according to a given Config object.
Definition Config.h:339
std::vector< GlobalValue * > Keep
Definition LTO.h:491
std::unique_ptr< Module > M
Definition LTO.h:490
bool Prevailing
Record if at least one instance of the common was marked as prevailing.
Definition LTO.h:476
std::vector< AddedModule > ModsWithSummaries
Definition LTO.h:493
std::unique_ptr< IRMover > Mover
Definition LTO.h:483
unsigned ParallelCodeGenParallelismLevel
Definition LTO.h:480
std::map< std::string, CommonResolution > Commons
Definition LTO.h:478
std::unique_ptr< Module > CombinedModule
Definition LTO.h:482
LLVM_ABI RegularLTOState(unsigned ParallelCodeGenParallelismLevel, const Config &Conf)
Definition LTO.cpp:678
ModuleMapType ModuleMap
Definition LTO.h:505
LLVM_ABI ThinLTOState(ThinBackend Backend)
Definition LTO.cpp:684
bool isPrevailingModuleForGUID(GlobalValue::GUID GUID, StringRef Module) const
Definition LTO.h:512
void setPrevailingModuleForGUID(GlobalValue::GUID GUID, StringRef Module)
Definition LTO.h:509
std::optional< ModuleMapType > ModulesToCompile
Definition LTO.h:507
ModuleSummaryIndex CombinedIndex
Definition LTO.h:503
The resolution for a symbol.
Definition LTO.h:697
unsigned FinalDefinitionInLinkageUnit
The definition of this symbol is unpreemptable at runtime and is known to be in this linkage unit.
Definition LTO.h:707
unsigned ExportDynamic
The symbol was exported dynamically, and therefore could be referenced by a shared library not visibl...
Definition LTO.h:714
unsigned Prevailing
The linker has chosen this definition of the symbol.
Definition LTO.h:703
unsigned LinkerRedefined
Linker redefined version of the symbol which appeared in -wrap or -defsym linker option.
Definition LTO.h:718
unsigned VisibleToRegularObj
The definition of this symbol is visible outside of the LTO unit.
Definition LTO.h:710
This type defines the behavior following the thin-link phase during ThinLTO.
Definition LTO.h:321
bool isValid() const
Definition LTO.h:336
std::unique_ptr< ThinBackendProc > operator()(const Config &Conf, ModuleSummaryIndex &CombinedIndex, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, AddStreamFn AddStream, FileCache Cache, ArrayRef< StringRef > BitcodeLibFuncs)
Definition LTO.h:326
ThreadPoolStrategy getParallelism() const
Definition LTO.h:335
ThinBackend(ThinBackendFunction Func, ThreadPoolStrategy Parallelism)
Definition LTO.h:322