Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:11

0001 //===-LTO.h - LLVM Link Time Optimizer ------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file declares functions and classes used to support LTO. It is intended
0010 // to be used both by LTO classes as well as by clients (gold-plugin) that
0011 // don't utilize the LTO code generator interfaces.
0012 //
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_LTO_LTO_H
0016 #define LLVM_LTO_LTO_H
0017 
0018 #include <memory>
0019 
0020 #include "llvm/ADT/DenseMap.h"
0021 #include "llvm/ADT/MapVector.h"
0022 #include "llvm/Bitcode/BitcodeReader.h"
0023 #include "llvm/IR/ModuleSummaryIndex.h"
0024 #include "llvm/LTO/Config.h"
0025 #include "llvm/Object/IRSymtab.h"
0026 #include "llvm/Support/Caching.h"
0027 #include "llvm/Support/Error.h"
0028 #include "llvm/Support/StringSaver.h"
0029 #include "llvm/Support/ThreadPool.h"
0030 #include "llvm/Support/thread.h"
0031 #include "llvm/Transforms/IPO/FunctionAttrs.h"
0032 #include "llvm/Transforms/IPO/FunctionImport.h"
0033 
0034 namespace llvm {
0035 
0036 class Error;
0037 class IRMover;
0038 class LLVMContext;
0039 class MemoryBufferRef;
0040 class Module;
0041 class raw_pwrite_stream;
0042 class ToolOutputFile;
0043 
0044 /// Resolve linkage for prevailing symbols in the \p Index. Linkage changes
0045 /// recorded in the index and the ThinLTO backends must apply the changes to
0046 /// the module via thinLTOFinalizeInModule.
0047 ///
0048 /// This is done for correctness (if value exported, ensure we always
0049 /// emit a copy), and compile-time optimization (allow drop of duplicates).
0050 void thinLTOResolvePrevailingInIndex(
0051     const lto::Config &C, ModuleSummaryIndex &Index,
0052     function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
0053         isPrevailing,
0054     function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
0055         recordNewLinkage,
0056     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols);
0057 
0058 /// Update the linkages in the given \p Index to mark exported values
0059 /// as external and non-exported values as internal. The ThinLTO backends
0060 /// must apply the changes to the Module via thinLTOInternalizeModule.
0061 void thinLTOInternalizeAndPromoteInIndex(
0062     ModuleSummaryIndex &Index,
0063     function_ref<bool(StringRef, ValueInfo)> isExported,
0064     function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
0065         isPrevailing);
0066 
0067 /// Computes a unique hash for the Module considering the current list of
0068 /// export/import and other global analysis results.
0069 std::string computeLTOCacheKey(
0070     const lto::Config &Conf, const ModuleSummaryIndex &Index,
0071     StringRef ModuleID, const FunctionImporter::ImportMapTy &ImportList,
0072     const FunctionImporter::ExportSetTy &ExportList,
0073     const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
0074     const GVSummaryMapTy &DefinedGlobals,
0075     const DenseSet<GlobalValue::GUID> &CfiFunctionDefs = {},
0076     const DenseSet<GlobalValue::GUID> &CfiFunctionDecls = {});
0077 
0078 /// Recomputes the LTO cache key for a given key with an extra identifier.
0079 std::string recomputeLTOCacheKey(const std::string &Key, StringRef ExtraID);
0080 
0081 namespace lto {
0082 
0083 StringLiteral getThinLTODefaultCPU(const Triple &TheTriple);
0084 
0085 /// Given the original \p Path to an output file, replace any path
0086 /// prefix matching \p OldPrefix with \p NewPrefix. Also, create the
0087 /// resulting directory if it does not yet exist.
0088 std::string getThinLTOOutputFile(StringRef Path, StringRef OldPrefix,
0089                                  StringRef NewPrefix);
0090 
0091 /// Setup optimization remarks.
0092 Expected<std::unique_ptr<ToolOutputFile>> setupLLVMOptimizationRemarks(
0093     LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses,
0094     StringRef RemarksFormat, bool RemarksWithHotness,
0095     std::optional<uint64_t> RemarksHotnessThreshold = 0, int Count = -1);
0096 
0097 /// Setups the output file for saving statistics.
0098 Expected<std::unique_ptr<ToolOutputFile>>
0099 setupStatsFile(StringRef StatsFilename);
0100 
0101 /// Produces a container ordering for optimal multi-threaded processing. Returns
0102 /// ordered indices to elements in the input array.
0103 std::vector<int> generateModulesOrdering(ArrayRef<BitcodeModule *> R);
0104 
0105 /// Updates MemProf attributes (and metadata) based on whether the index
0106 /// has recorded that we are linking with allocation libraries containing
0107 /// the necessary APIs for downstream transformations.
0108 void updateMemProfAttributes(Module &Mod, const ModuleSummaryIndex &Index);
0109 
0110 class LTO;
0111 struct SymbolResolution;
0112 
0113 /// An input file. This is a symbol table wrapper that only exposes the
0114 /// information that an LTO client should need in order to do symbol resolution.
0115 class InputFile {
0116 public:
0117   struct Symbol;
0118 
0119 private:
0120   // FIXME: Remove LTO class friendship once we have bitcode symbol tables.
0121   friend LTO;
0122   InputFile() = default;
0123 
0124   std::vector<BitcodeModule> Mods;
0125   SmallVector<char, 0> Strtab;
0126   std::vector<Symbol> Symbols;
0127 
0128   // [begin, end) for each module
0129   std::vector<std::pair<size_t, size_t>> ModuleSymIndices;
0130 
0131   StringRef TargetTriple, SourceFileName, COFFLinkerOpts;
0132   std::vector<StringRef> DependentLibraries;
0133   std::vector<std::pair<StringRef, Comdat::SelectionKind>> ComdatTable;
0134 
0135 public:
0136   ~InputFile();
0137 
0138   /// Create an InputFile.
0139   static Expected<std::unique_ptr<InputFile>> create(MemoryBufferRef Object);
0140 
0141   /// The purpose of this struct is to only expose the symbol information that
0142   /// an LTO client should need in order to do symbol resolution.
0143   struct Symbol : irsymtab::Symbol {
0144     friend LTO;
0145 
0146   public:
0147     Symbol(const irsymtab::Symbol &S) : irsymtab::Symbol(S) {}
0148 
0149     using irsymtab::Symbol::isUndefined;
0150     using irsymtab::Symbol::isCommon;
0151     using irsymtab::Symbol::isWeak;
0152     using irsymtab::Symbol::isIndirect;
0153     using irsymtab::Symbol::getName;
0154     using irsymtab::Symbol::getIRName;
0155     using irsymtab::Symbol::getVisibility;
0156     using irsymtab::Symbol::canBeOmittedFromSymbolTable;
0157     using irsymtab::Symbol::isTLS;
0158     using irsymtab::Symbol::getComdatIndex;
0159     using irsymtab::Symbol::getCommonSize;
0160     using irsymtab::Symbol::getCommonAlignment;
0161     using irsymtab::Symbol::getCOFFWeakExternalFallback;
0162     using irsymtab::Symbol::getSectionName;
0163     using irsymtab::Symbol::isExecutable;
0164     using irsymtab::Symbol::isUsed;
0165   };
0166 
0167   /// A range over the symbols in this InputFile.
0168   ArrayRef<Symbol> symbols() const { return Symbols; }
0169 
0170   /// Returns linker options specified in the input file.
0171   StringRef getCOFFLinkerOpts() const { return COFFLinkerOpts; }
0172 
0173   /// Returns dependent library specifiers from the input file.
0174   ArrayRef<StringRef> getDependentLibraries() const { return DependentLibraries; }
0175 
0176   /// Returns the path to the InputFile.
0177   StringRef getName() const;
0178 
0179   /// Returns the input file's target triple.
0180   StringRef getTargetTriple() const { return TargetTriple; }
0181 
0182   /// Returns the source file path specified at compile time.
0183   StringRef getSourceFileName() const { return SourceFileName; }
0184 
0185   // Returns a table with all the comdats used by this file.
0186   ArrayRef<std::pair<StringRef, Comdat::SelectionKind>> getComdatTable() const {
0187     return ComdatTable;
0188   }
0189 
0190   // Returns the only BitcodeModule from InputFile.
0191   BitcodeModule &getSingleBitcodeModule();
0192 
0193 private:
0194   ArrayRef<Symbol> module_symbols(unsigned I) const {
0195     const auto &Indices = ModuleSymIndices[I];
0196     return {Symbols.data() + Indices.first, Symbols.data() + Indices.second};
0197   }
0198 };
0199 
0200 using IndexWriteCallback = std::function<void(const std::string &)>;
0201 
0202 /// This class defines the interface to the ThinLTO backend.
0203 class ThinBackendProc {
0204 protected:
0205   const Config &Conf;
0206   ModuleSummaryIndex &CombinedIndex;
0207   const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries;
0208   IndexWriteCallback OnWrite;
0209   bool ShouldEmitImportsFiles;
0210   DefaultThreadPool BackendThreadPool;
0211   std::optional<Error> Err;
0212   std::mutex ErrMu;
0213 
0214 public:
0215   ThinBackendProc(
0216       const Config &Conf, ModuleSummaryIndex &CombinedIndex,
0217       const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
0218       lto::IndexWriteCallback OnWrite, bool ShouldEmitImportsFiles,
0219       ThreadPoolStrategy ThinLTOParallelism)
0220       : Conf(Conf), CombinedIndex(CombinedIndex),
0221         ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries),
0222         OnWrite(OnWrite), ShouldEmitImportsFiles(ShouldEmitImportsFiles),
0223         BackendThreadPool(ThinLTOParallelism) {}
0224 
0225   virtual ~ThinBackendProc() = default;
0226   virtual Error start(
0227       unsigned Task, BitcodeModule BM,
0228       const FunctionImporter::ImportMapTy &ImportList,
0229       const FunctionImporter::ExportSetTy &ExportList,
0230       const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
0231       MapVector<StringRef, BitcodeModule> &ModuleMap) = 0;
0232   Error wait() {
0233     BackendThreadPool.wait();
0234     if (Err)
0235       return std::move(*Err);
0236     return Error::success();
0237   }
0238   unsigned getThreadCount() { return BackendThreadPool.getMaxConcurrency(); }
0239   virtual bool isSensitiveToInputOrder() { return false; }
0240 
0241   // Write sharded indices and (optionally) imports to disk
0242   Error emitFiles(const FunctionImporter::ImportMapTy &ImportList,
0243                   llvm::StringRef ModulePath,
0244                   const std::string &NewModulePath) const;
0245 };
0246 
0247 /// This callable defines the behavior of a ThinLTO backend after the thin-link
0248 /// phase. It accepts a configuration \p C, a combined module summary index
0249 /// \p CombinedIndex, a map of module identifiers to global variable summaries
0250 /// \p ModuleToDefinedGVSummaries, a function to add output streams \p
0251 /// AddStream, and a file cache \p Cache. It returns a unique pointer to a
0252 /// ThinBackendProc, which can be used to launch backends in parallel.
0253 using ThinBackendFunction = std::function<std::unique_ptr<ThinBackendProc>(
0254     const Config &C, ModuleSummaryIndex &CombinedIndex,
0255     const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
0256     AddStreamFn AddStream, FileCache Cache)>;
0257 
0258 /// This type defines the behavior following the thin-link phase during ThinLTO.
0259 /// It encapsulates a backend function and a strategy for thread pool
0260 /// parallelism. Clients should use one of the provided create*ThinBackend()
0261 /// functions to instantiate a ThinBackend. Parallelism defines the thread pool
0262 /// strategy to be used for processing.
0263 struct ThinBackend {
0264   ThinBackend(ThinBackendFunction Func, ThreadPoolStrategy Parallelism)
0265       : Func(std::move(Func)), Parallelism(std::move(Parallelism)) {}
0266   ThinBackend() = default;
0267 
0268   std::unique_ptr<ThinBackendProc> operator()(
0269       const Config &Conf, ModuleSummaryIndex &CombinedIndex,
0270       const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
0271       AddStreamFn AddStream, FileCache Cache) {
0272     assert(isValid() && "Invalid backend function");
0273     return Func(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
0274                 std::move(AddStream), std::move(Cache));
0275   }
0276   ThreadPoolStrategy getParallelism() const { return Parallelism; }
0277   bool isValid() const { return static_cast<bool>(Func); }
0278 
0279 private:
0280   ThinBackendFunction Func = nullptr;
0281   ThreadPoolStrategy Parallelism;
0282 };
0283 
0284 /// This ThinBackend runs the individual backend jobs in-process.
0285 /// The default value means to use one job per hardware core (not hyper-thread).
0286 /// OnWrite is callback which receives module identifier and notifies LTO user
0287 /// that index file for the module (and optionally imports file) was created.
0288 /// ShouldEmitIndexFiles being true will write sharded ThinLTO index files
0289 /// to the same path as the input module, with suffix ".thinlto.bc"
0290 /// ShouldEmitImportsFiles is true it also writes a list of imported files to a
0291 /// similar path with ".imports" appended instead.
0292 ThinBackend createInProcessThinBackend(ThreadPoolStrategy Parallelism,
0293                                        IndexWriteCallback OnWrite = nullptr,
0294                                        bool ShouldEmitIndexFiles = false,
0295                                        bool ShouldEmitImportsFiles = false);
0296 
0297 /// This ThinBackend writes individual module indexes to files, instead of
0298 /// running the individual backend jobs. This backend is for distributed builds
0299 /// where separate processes will invoke the real backends.
0300 ///
0301 /// To find the path to write the index to, the backend checks if the path has a
0302 /// prefix of OldPrefix; if so, it replaces that prefix with NewPrefix. It then
0303 /// appends ".thinlto.bc" and writes the index to that path. If
0304 /// ShouldEmitImportsFiles is true it also writes a list of imported files to a
0305 /// similar path with ".imports" appended instead.
0306 /// LinkedObjectsFile is an output stream to write the list of object files for
0307 /// the final ThinLTO linking. Can be nullptr.  If LinkedObjectsFile is not
0308 /// nullptr and NativeObjectPrefix is not empty then it replaces the prefix of
0309 /// the objects with NativeObjectPrefix instead of NewPrefix. OnWrite is
0310 /// callback which receives module identifier and notifies LTO user that index
0311 /// file for the module (and optionally imports file) was created.
0312 ThinBackend createWriteIndexesThinBackend(ThreadPoolStrategy Parallelism,
0313                                           std::string OldPrefix,
0314                                           std::string NewPrefix,
0315                                           std::string NativeObjectPrefix,
0316                                           bool ShouldEmitImportsFiles,
0317                                           raw_fd_ostream *LinkedObjectsFile,
0318                                           IndexWriteCallback OnWrite);
0319 
0320 /// This class implements a resolution-based interface to LLVM's LTO
0321 /// functionality. It supports regular LTO, parallel LTO code generation and
0322 /// ThinLTO. You can use it from a linker in the following way:
0323 /// - Set hooks and code generation options (see lto::Config struct defined in
0324 ///   Config.h), and use the lto::Config object to create an lto::LTO object.
0325 /// - Create lto::InputFile objects using lto::InputFile::create(), then use
0326 ///   the symbols() function to enumerate its symbols and compute a resolution
0327 ///   for each symbol (see SymbolResolution below).
0328 /// - After the linker has visited each input file (and each regular object
0329 ///   file) and computed a resolution for each symbol, take each lto::InputFile
0330 ///   and pass it and an array of symbol resolutions to the add() function.
0331 /// - Call the getMaxTasks() function to get an upper bound on the number of
0332 ///   native object files that LTO may add to the link.
0333 /// - Call the run() function. This function will use the supplied AddStream
0334 ///   and Cache functions to add up to getMaxTasks() native object files to
0335 ///   the link.
0336 class LTO {
0337   friend InputFile;
0338 
0339 public:
0340   /// Unified LTO modes
0341   enum LTOKind {
0342     /// Any LTO mode without Unified LTO. The default mode.
0343     LTOK_Default,
0344 
0345     /// Regular LTO, with Unified LTO enabled.
0346     LTOK_UnifiedRegular,
0347 
0348     /// ThinLTO, with Unified LTO enabled.
0349     LTOK_UnifiedThin,
0350   };
0351 
0352   /// Create an LTO object. A default constructed LTO object has a reasonable
0353   /// production configuration, but you can customize it by passing arguments to
0354   /// this constructor.
0355   /// FIXME: We do currently require the DiagHandler field to be set in Conf.
0356   /// Until that is fixed, a Config argument is required.
0357   LTO(Config Conf, ThinBackend Backend = {},
0358       unsigned ParallelCodeGenParallelismLevel = 1,
0359       LTOKind LTOMode = LTOK_Default);
0360   ~LTO();
0361 
0362   /// Add an input file to the LTO link, using the provided symbol resolutions.
0363   /// The symbol resolutions must appear in the enumeration order given by
0364   /// InputFile::symbols().
0365   Error add(std::unique_ptr<InputFile> Obj, ArrayRef<SymbolResolution> Res);
0366 
0367   /// Returns an upper bound on the number of tasks that the client may expect.
0368   /// This may only be called after all IR object files have been added. For a
0369   /// full description of tasks see LTOBackend.h.
0370   unsigned getMaxTasks() const;
0371 
0372   /// Runs the LTO pipeline. This function calls the supplied AddStream
0373   /// function to add native object files to the link.
0374   ///
0375   /// The Cache parameter is optional. If supplied, it will be used to cache
0376   /// native object files and add them to the link.
0377   ///
0378   /// The client will receive at most one callback (via either AddStream or
0379   /// Cache) for each task identifier.
0380   Error run(AddStreamFn AddStream, FileCache Cache = {});
0381 
0382   /// Static method that returns a list of libcall symbols that can be generated
0383   /// by LTO but might not be visible from bitcode symbol table.
0384   static SmallVector<const char *> getRuntimeLibcallSymbols(const Triple &TT);
0385 
0386 private:
0387   Config Conf;
0388 
0389   struct RegularLTOState {
0390     RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
0391                     const Config &Conf);
0392     struct CommonResolution {
0393       uint64_t Size = 0;
0394       Align Alignment;
0395       /// Record if at least one instance of the common was marked as prevailing
0396       bool Prevailing = false;
0397     };
0398     std::map<std::string, CommonResolution> Commons;
0399 
0400     unsigned ParallelCodeGenParallelismLevel;
0401     LTOLLVMContext Ctx;
0402     std::unique_ptr<Module> CombinedModule;
0403     std::unique_ptr<IRMover> Mover;
0404 
0405     // This stores the information about a regular LTO module that we have added
0406     // to the link. It will either be linked immediately (for modules without
0407     // summaries) or after summary-based dead stripping (for modules with
0408     // summaries).
0409     struct AddedModule {
0410       std::unique_ptr<Module> M;
0411       std::vector<GlobalValue *> Keep;
0412     };
0413     std::vector<AddedModule> ModsWithSummaries;
0414     bool EmptyCombinedModule = true;
0415   } RegularLTO;
0416 
0417   using ModuleMapType = MapVector<StringRef, BitcodeModule>;
0418 
0419   struct ThinLTOState {
0420     ThinLTOState(ThinBackend Backend);
0421 
0422     ThinBackend Backend;
0423     ModuleSummaryIndex CombinedIndex;
0424     // The full set of bitcode modules in input order.
0425     ModuleMapType ModuleMap;
0426     // The bitcode modules to compile, if specified by the LTO Config.
0427     std::optional<ModuleMapType> ModulesToCompile;
0428     DenseMap<GlobalValue::GUID, StringRef> PrevailingModuleForGUID;
0429   } ThinLTO;
0430 
0431   // The global resolution for a particular (mangled) symbol name. This is in
0432   // particular necessary to track whether each symbol can be internalized.
0433   // Because any input file may introduce a new cross-partition reference, we
0434   // cannot make any final internalization decisions until all input files have
0435   // been added and the client has called run(). During run() we apply
0436   // internalization decisions either directly to the module (for regular LTO)
0437   // or to the combined index (for ThinLTO).
0438   struct GlobalResolution {
0439     /// The unmangled name of the global.
0440     std::string IRName;
0441 
0442     /// Keep track if the symbol is visible outside of a module with a summary
0443     /// (i.e. in either a regular object or a regular LTO module without a
0444     /// summary).
0445     bool VisibleOutsideSummary = false;
0446 
0447     /// The symbol was exported dynamically, and therefore could be referenced
0448     /// by a shared library not visible to the linker.
0449     bool ExportDynamic = false;
0450 
0451     bool UnnamedAddr = true;
0452 
0453     /// True if module contains the prevailing definition.
0454     bool Prevailing = false;
0455 
0456     /// Returns true if module contains the prevailing definition and symbol is
0457     /// an IR symbol. For example when module-level inline asm block is used,
0458     /// symbol can be prevailing in module but have no IR name.
0459     bool isPrevailingIRSymbol() const { return Prevailing && !IRName.empty(); }
0460 
0461     /// This field keeps track of the partition number of this global. The
0462     /// regular LTO object is partition 0, while each ThinLTO object has its own
0463     /// partition number from 1 onwards.
0464     ///
0465     /// Any global that is defined or used by more than one partition, or that
0466     /// is referenced externally, may not be internalized.
0467     ///
0468     /// Partitions generally have a one-to-one correspondence with tasks, except
0469     /// that we use partition 0 for all parallel LTO code generation partitions.
0470     /// Any partitioning of the combined LTO object is done internally by the
0471     /// LTO backend.
0472     unsigned Partition = Unknown;
0473 
0474     /// Special partition numbers.
0475     enum : unsigned {
0476       /// A partition number has not yet been assigned to this global.
0477       Unknown = -1u,
0478 
0479       /// This global is either used by more than one partition or has an
0480       /// external reference, and therefore cannot be internalized.
0481       External = -2u,
0482 
0483       /// The RegularLTO partition
0484       RegularLTO = 0,
0485     };
0486   };
0487 
0488   // GlobalResolutionSymbolSaver allocator.
0489   std::unique_ptr<llvm::BumpPtrAllocator> Alloc;
0490 
0491   // Symbol saver for global resolution map.
0492   std::unique_ptr<llvm::StringSaver> GlobalResolutionSymbolSaver;
0493 
0494   // Global mapping from mangled symbol names to resolutions.
0495   // Make this an unique_ptr to guard against accessing after it has been reset
0496   // (to reduce memory after we're done with it).
0497   std::unique_ptr<llvm::DenseMap<StringRef, GlobalResolution>>
0498       GlobalResolutions;
0499 
0500   void releaseGlobalResolutionsMemory();
0501 
0502   void addModuleToGlobalRes(ArrayRef<InputFile::Symbol> Syms,
0503                             ArrayRef<SymbolResolution> Res, unsigned Partition,
0504                             bool InSummary);
0505 
0506   // These functions take a range of symbol resolutions [ResI, ResE) and consume
0507   // the resolutions used by a single input module by incrementing ResI. After
0508   // these functions return, [ResI, ResE) will refer to the resolution range for
0509   // the remaining modules in the InputFile.
0510   Error addModule(InputFile &Input, unsigned ModI,
0511                   const SymbolResolution *&ResI, const SymbolResolution *ResE);
0512 
0513   Expected<RegularLTOState::AddedModule>
0514   addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
0515                 const SymbolResolution *&ResI, const SymbolResolution *ResE);
0516   Error linkRegularLTO(RegularLTOState::AddedModule Mod,
0517                        bool LivenessFromIndex);
0518 
0519   Error addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
0520                    const SymbolResolution *&ResI, const SymbolResolution *ResE);
0521 
0522   Error runRegularLTO(AddStreamFn AddStream);
0523   Error runThinLTO(AddStreamFn AddStream, FileCache Cache,
0524                    const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols);
0525 
0526   Error checkPartiallySplit();
0527 
0528   mutable bool CalledGetMaxTasks = false;
0529 
0530   // LTO mode when using Unified LTO.
0531   LTOKind LTOMode;
0532 
0533   // Use Optional to distinguish false from not yet initialized.
0534   std::optional<bool> EnableSplitLTOUnit;
0535 
0536   // Identify symbols exported dynamically, and that therefore could be
0537   // referenced by a shared library not visible to the linker.
0538   DenseSet<GlobalValue::GUID> DynamicExportSymbols;
0539 
0540   // Diagnostic optimization remarks file
0541   std::unique_ptr<ToolOutputFile> DiagnosticOutputFile;
0542 };
0543 
0544 /// The resolution for a symbol. The linker must provide a SymbolResolution for
0545 /// each global symbol based on its internal resolution of that symbol.
0546 struct SymbolResolution {
0547   SymbolResolution()
0548       : Prevailing(0), FinalDefinitionInLinkageUnit(0), VisibleToRegularObj(0),
0549         ExportDynamic(0), LinkerRedefined(0) {}
0550 
0551   /// The linker has chosen this definition of the symbol.
0552   unsigned Prevailing : 1;
0553 
0554   /// The definition of this symbol is unpreemptable at runtime and is known to
0555   /// be in this linkage unit.
0556   unsigned FinalDefinitionInLinkageUnit : 1;
0557 
0558   /// The definition of this symbol is visible outside of the LTO unit.
0559   unsigned VisibleToRegularObj : 1;
0560 
0561   /// The symbol was exported dynamically, and therefore could be referenced
0562   /// by a shared library not visible to the linker.
0563   unsigned ExportDynamic : 1;
0564 
0565   /// Linker redefined version of the symbol which appeared in -wrap or -defsym
0566   /// linker option.
0567   unsigned LinkerRedefined : 1;
0568 };
0569 
0570 } // namespace lto
0571 } // namespace llvm
0572 
0573 #endif