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 18 #include "llvm/Support/Compiler.h" 19 #include <memory> 20 21 #include "llvm/ADT/DenseMap.h" 22 #include "llvm/ADT/MapVector.h" 23 #include "llvm/Bitcode/BitcodeReader.h" 24 #include "llvm/IR/ModuleSummaryIndex.h" 25 #include "llvm/LTO/Config.h" 26 #include "llvm/Object/IRSymtab.h" 27 #include "llvm/Support/Caching.h" 28 #include "llvm/Support/Error.h" 29 #include "llvm/Support/StringSaver.h" 30 #include "llvm/Support/ThreadPool.h" 31 #include "llvm/Support/thread.h" 32 #include "llvm/Transforms/IPO/FunctionAttrs.h" 33 #include "llvm/Transforms/IPO/FunctionImport.h" 34 35 namespace llvm { 36 37 class Error; 38 class IRMover; 39 class LLVMContext; 40 class MemoryBufferRef; 41 class Module; 42 class raw_pwrite_stream; 43 class ToolOutputFile; 44 45 /// Resolve linkage for prevailing symbols in the \p Index. Linkage changes 46 /// recorded in the index and the ThinLTO backends must apply the changes to 47 /// the module via thinLTOFinalizeInModule. 48 /// 49 /// This is done for correctness (if value exported, ensure we always 50 /// emit a copy), and compile-time optimization (allow drop of duplicates). 51 LLVM_ABI void thinLTOResolvePrevailingInIndex( 52 const lto::Config &C, ModuleSummaryIndex &Index, 53 function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)> 54 isPrevailing, 55 function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)> 56 recordNewLinkage, 57 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols); 58 59 /// Update the linkages in the given \p Index to mark exported values 60 /// as external and non-exported values as internal. The ThinLTO backends 61 /// must apply the changes to the Module via thinLTOInternalizeModule. 62 LLVM_ABI void thinLTOInternalizeAndPromoteInIndex( 63 ModuleSummaryIndex &Index, 64 function_ref<bool(StringRef, ValueInfo)> isExported, 65 function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)> 66 isPrevailing); 67 68 /// Computes a unique hash for the Module considering the current list of 69 /// export/import and other global analysis results. 70 LLVM_ABI std::string computeLTOCacheKey( 71 const lto::Config &Conf, const ModuleSummaryIndex &Index, 72 StringRef ModuleID, const FunctionImporter::ImportMapTy &ImportList, 73 const FunctionImporter::ExportSetTy &ExportList, 74 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR, 75 const GVSummaryMapTy &DefinedGlobals, 76 const DenseSet<GlobalValue::GUID> &CfiFunctionDefs = {}, 77 const DenseSet<GlobalValue::GUID> &CfiFunctionDecls = {}); 78 79 /// Recomputes the LTO cache key for a given key with an extra identifier. 80 LLVM_ABI std::string recomputeLTOCacheKey(const std::string &Key, 81 StringRef ExtraID); 82 83 namespace lto { 84 85 LLVM_ABI StringLiteral getThinLTODefaultCPU(const Triple &TheTriple); 86 87 /// Given the original \p Path to an output file, replace any path 88 /// prefix matching \p OldPrefix with \p NewPrefix. Also, create the 89 /// resulting directory if it does not yet exist. 90 LLVM_ABI std::string getThinLTOOutputFile(StringRef Path, StringRef OldPrefix, 91 StringRef NewPrefix); 92 93 /// Setup optimization remarks. 94 LLVM_ABI Expected<std::unique_ptr<ToolOutputFile>> setupLLVMOptimizationRemarks( 95 LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses, 96 StringRef RemarksFormat, bool RemarksWithHotness, 97 std::optional<uint64_t> RemarksHotnessThreshold = 0, int Count = -1); 98 99 /// Setups the output file for saving statistics. 100 LLVM_ABI Expected<std::unique_ptr<ToolOutputFile>> 101 setupStatsFile(StringRef StatsFilename); 102 103 /// Produces a container ordering for optimal multi-threaded processing. Returns 104 /// ordered indices to elements in the input array. 105 LLVM_ABI std::vector<int> generateModulesOrdering(ArrayRef<BitcodeModule *> R); 106 107 /// Updates MemProf attributes (and metadata) based on whether the index 108 /// has recorded that we are linking with allocation libraries containing 109 /// the necessary APIs for downstream transformations. 110 LLVM_ABI void updateMemProfAttributes(Module &Mod, 111 const ModuleSummaryIndex &Index); 112 113 class LTO; 114 struct SymbolResolution; 115 116 /// An input file. This is a symbol table wrapper that only exposes the 117 /// information that an LTO client should need in order to do symbol resolution. 118 class InputFile { 119 public: 120 struct Symbol; 121 122 private: 123 // FIXME: Remove LTO class friendship once we have bitcode symbol tables. 124 friend LTO; 125 InputFile() = default; 126 127 std::vector<BitcodeModule> Mods; 128 SmallVector<char, 0> Strtab; 129 std::vector<Symbol> Symbols; 130 131 // [begin, end) for each module 132 std::vector<std::pair<size_t, size_t>> ModuleSymIndices; 133 134 StringRef TargetTriple, SourceFileName, COFFLinkerOpts; 135 std::vector<StringRef> DependentLibraries; 136 std::vector<std::pair<StringRef, Comdat::SelectionKind>> ComdatTable; 137 138 public: 139 LLVM_ABI ~InputFile(); 140 141 /// Create an InputFile. 142 LLVM_ABI static Expected<std::unique_ptr<InputFile>> 143 create(MemoryBufferRef Object); 144 145 /// The purpose of this struct is to only expose the symbol information that 146 /// an LTO client should need in order to do symbol resolution. 147 struct Symbol : irsymtab::Symbol { 148 friend LTO; 149 150 public: SymbolSymbol151 Symbol(const irsymtab::Symbol &S) : irsymtab::Symbol(S) {} 152 153 using irsymtab::Symbol::isUndefined; 154 using irsymtab::Symbol::isCommon; 155 using irsymtab::Symbol::isWeak; 156 using irsymtab::Symbol::isIndirect; 157 using irsymtab::Symbol::getName; 158 using irsymtab::Symbol::getIRName; 159 using irsymtab::Symbol::getVisibility; 160 using irsymtab::Symbol::canBeOmittedFromSymbolTable; 161 using irsymtab::Symbol::isTLS; 162 using irsymtab::Symbol::getComdatIndex; 163 using irsymtab::Symbol::getCommonSize; 164 using irsymtab::Symbol::getCommonAlignment; 165 using irsymtab::Symbol::getCOFFWeakExternalFallback; 166 using irsymtab::Symbol::getSectionName; 167 using irsymtab::Symbol::isExecutable; 168 using irsymtab::Symbol::isUsed; 169 }; 170 171 /// A range over the symbols in this InputFile. symbols()172 ArrayRef<Symbol> symbols() const { return Symbols; } 173 174 /// Returns linker options specified in the input file. getCOFFLinkerOpts()175 StringRef getCOFFLinkerOpts() const { return COFFLinkerOpts; } 176 177 /// Returns dependent library specifiers from the input file. getDependentLibraries()178 ArrayRef<StringRef> getDependentLibraries() const { return DependentLibraries; } 179 180 /// Returns the path to the InputFile. 181 LLVM_ABI StringRef getName() const; 182 183 /// Returns the input file's target triple. getTargetTriple()184 StringRef getTargetTriple() const { return TargetTriple; } 185 186 /// Returns the source file path specified at compile time. getSourceFileName()187 StringRef getSourceFileName() const { return SourceFileName; } 188 189 // Returns a table with all the comdats used by this file. getComdatTable()190 ArrayRef<std::pair<StringRef, Comdat::SelectionKind>> getComdatTable() const { 191 return ComdatTable; 192 } 193 194 // Returns the only BitcodeModule from InputFile. 195 LLVM_ABI BitcodeModule &getSingleBitcodeModule(); 196 197 private: module_symbols(unsigned I)198 ArrayRef<Symbol> module_symbols(unsigned I) const { 199 const auto &Indices = ModuleSymIndices[I]; 200 return {Symbols.data() + Indices.first, Symbols.data() + Indices.second}; 201 } 202 }; 203 204 using IndexWriteCallback = std::function<void(const std::string &)>; 205 206 using ImportsFilesContainer = llvm::SmallVector<std::string>; 207 208 /// This class defines the interface to the ThinLTO backend. 209 class ThinBackendProc { 210 protected: 211 const Config &Conf; 212 ModuleSummaryIndex &CombinedIndex; 213 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries; 214 IndexWriteCallback OnWrite; 215 bool ShouldEmitImportsFiles; 216 DefaultThreadPool BackendThreadPool; 217 std::optional<Error> Err; 218 std::mutex ErrMu; 219 220 public: ThinBackendProc(const Config & Conf,ModuleSummaryIndex & CombinedIndex,const DenseMap<StringRef,GVSummaryMapTy> & ModuleToDefinedGVSummaries,lto::IndexWriteCallback OnWrite,bool ShouldEmitImportsFiles,ThreadPoolStrategy ThinLTOParallelism)221 ThinBackendProc( 222 const Config &Conf, ModuleSummaryIndex &CombinedIndex, 223 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries, 224 lto::IndexWriteCallback OnWrite, bool ShouldEmitImportsFiles, 225 ThreadPoolStrategy ThinLTOParallelism) 226 : Conf(Conf), CombinedIndex(CombinedIndex), 227 ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries), 228 OnWrite(OnWrite), ShouldEmitImportsFiles(ShouldEmitImportsFiles), 229 BackendThreadPool(ThinLTOParallelism) {} 230 231 virtual ~ThinBackendProc() = default; setup(unsigned ThinLTONumTasks,unsigned ThinLTOTaskOffset,Triple Triple)232 virtual void setup(unsigned ThinLTONumTasks, unsigned ThinLTOTaskOffset, 233 Triple Triple) {} 234 virtual Error start( 235 unsigned Task, BitcodeModule BM, 236 const FunctionImporter::ImportMapTy &ImportList, 237 const FunctionImporter::ExportSetTy &ExportList, 238 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR, 239 MapVector<StringRef, BitcodeModule> &ModuleMap) = 0; wait()240 virtual Error wait() { 241 BackendThreadPool.wait(); 242 if (Err) 243 return std::move(*Err); 244 return Error::success(); 245 } getThreadCount()246 unsigned getThreadCount() { return BackendThreadPool.getMaxConcurrency(); } isSensitiveToInputOrder()247 virtual bool isSensitiveToInputOrder() { return false; } 248 249 // Write sharded indices and (optionally) imports to disk 250 LLVM_ABI Error emitFiles(const FunctionImporter::ImportMapTy &ImportList, 251 StringRef ModulePath, 252 const std::string &NewModulePath) const; 253 254 // Write sharded indices to SummaryPath, (optionally) imports to disk, and 255 // (optionally) record imports in ImportsFiles. 256 LLVM_ABI Error emitFiles( 257 const FunctionImporter::ImportMapTy &ImportList, StringRef ModulePath, 258 const std::string &NewModulePath, StringRef SummaryPath, 259 std::optional<std::reference_wrapper<ImportsFilesContainer>> ImportsFiles) 260 const; 261 }; 262 263 /// This callable defines the behavior of a ThinLTO backend after the thin-link 264 /// phase. It accepts a configuration \p C, a combined module summary index 265 /// \p CombinedIndex, a map of module identifiers to global variable summaries 266 /// \p ModuleToDefinedGVSummaries, a function to add output streams \p 267 /// AddStream, and a file cache \p Cache. It returns a unique pointer to a 268 /// ThinBackendProc, which can be used to launch backends in parallel. 269 using ThinBackendFunction = std::function<std::unique_ptr<ThinBackendProc>( 270 const Config &C, ModuleSummaryIndex &CombinedIndex, 271 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries, 272 AddStreamFn AddStream, FileCache Cache)>; 273 274 /// This type defines the behavior following the thin-link phase during ThinLTO. 275 /// It encapsulates a backend function and a strategy for thread pool 276 /// parallelism. Clients should use one of the provided create*ThinBackend() 277 /// functions to instantiate a ThinBackend. Parallelism defines the thread pool 278 /// strategy to be used for processing. 279 struct ThinBackend { ThinBackendThinBackend280 ThinBackend(ThinBackendFunction Func, ThreadPoolStrategy Parallelism) 281 : Func(std::move(Func)), Parallelism(std::move(Parallelism)) {} 282 ThinBackend() = default; 283 operatorThinBackend284 std::unique_ptr<ThinBackendProc> operator()( 285 const Config &Conf, ModuleSummaryIndex &CombinedIndex, 286 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries, 287 AddStreamFn AddStream, FileCache Cache) { 288 assert(isValid() && "Invalid backend function"); 289 return Func(Conf, CombinedIndex, ModuleToDefinedGVSummaries, 290 std::move(AddStream), std::move(Cache)); 291 } getParallelismThinBackend292 ThreadPoolStrategy getParallelism() const { return Parallelism; } isValidThinBackend293 bool isValid() const { return static_cast<bool>(Func); } 294 295 private: 296 ThinBackendFunction Func = nullptr; 297 ThreadPoolStrategy Parallelism; 298 }; 299 300 /// This ThinBackend runs the individual backend jobs in-process. 301 /// The default value means to use one job per hardware core (not hyper-thread). 302 /// OnWrite is callback which receives module identifier and notifies LTO user 303 /// that index file for the module (and optionally imports file) was created. 304 /// ShouldEmitIndexFiles being true will write sharded ThinLTO index files 305 /// to the same path as the input module, with suffix ".thinlto.bc" 306 /// ShouldEmitImportsFiles is true it also writes a list of imported files to a 307 /// similar path with ".imports" appended instead. 308 LLVM_ABI ThinBackend createInProcessThinBackend( 309 ThreadPoolStrategy Parallelism, IndexWriteCallback OnWrite = nullptr, 310 bool ShouldEmitIndexFiles = false, bool ShouldEmitImportsFiles = false); 311 312 /// This ThinBackend generates the index shards and then runs the individual 313 /// backend jobs via an external process. It takes the same parameters as the 314 /// InProcessThinBackend; however, these parameters only control the behavior 315 /// when generating the index files for the modules. Additionally: 316 /// LinkerOutputFile is a string that should identify this LTO invocation in 317 /// the context of a wider build. It's used for naming to aid the user in 318 /// identifying activity related to a specific LTO invocation. 319 /// Distributor specifies the path to a process to invoke to manage the backend 320 /// job execution. 321 /// DistributorArgs specifies a list of arguments to be applied to the 322 /// distributor. 323 /// RemoteCompiler specifies the path to a Clang executable to be invoked for 324 /// the backend jobs. 325 /// RemoteCompilerArgs specifies a list of arguments to be applied to the 326 /// backend compilations. 327 /// SaveTemps is a debugging tool that prevents temporary files created by this 328 /// backend from being cleaned up. 329 LLVM_ABI ThinBackend createOutOfProcessThinBackend( 330 ThreadPoolStrategy Parallelism, IndexWriteCallback OnWrite, 331 bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles, 332 StringRef LinkerOutputFile, StringRef Distributor, 333 ArrayRef<StringRef> DistributorArgs, StringRef RemoteCompiler, 334 ArrayRef<StringRef> RemoteCompilerArgs, bool SaveTemps); 335 336 /// This ThinBackend writes individual module indexes to files, instead of 337 /// running the individual backend jobs. This backend is for distributed builds 338 /// where separate processes will invoke the real backends. 339 /// 340 /// To find the path to write the index to, the backend checks if the path has a 341 /// prefix of OldPrefix; if so, it replaces that prefix with NewPrefix. It then 342 /// appends ".thinlto.bc" and writes the index to that path. If 343 /// ShouldEmitImportsFiles is true it also writes a list of imported files to a 344 /// similar path with ".imports" appended instead. 345 /// LinkedObjectsFile is an output stream to write the list of object files for 346 /// the final ThinLTO linking. Can be nullptr. If LinkedObjectsFile is not 347 /// nullptr and NativeObjectPrefix is not empty then it replaces the prefix of 348 /// the objects with NativeObjectPrefix instead of NewPrefix. OnWrite is 349 /// callback which receives module identifier and notifies LTO user that index 350 /// file for the module (and optionally imports file) was created. 351 LLVM_ABI ThinBackend createWriteIndexesThinBackend( 352 ThreadPoolStrategy Parallelism, std::string OldPrefix, 353 std::string NewPrefix, std::string NativeObjectPrefix, 354 bool ShouldEmitImportsFiles, raw_fd_ostream *LinkedObjectsFile, 355 IndexWriteCallback OnWrite); 356 357 /// This class implements a resolution-based interface to LLVM's LTO 358 /// functionality. It supports regular LTO, parallel LTO code generation and 359 /// ThinLTO. You can use it from a linker in the following way: 360 /// - Set hooks and code generation options (see lto::Config struct defined in 361 /// Config.h), and use the lto::Config object to create an lto::LTO object. 362 /// - Create lto::InputFile objects using lto::InputFile::create(), then use 363 /// the symbols() function to enumerate its symbols and compute a resolution 364 /// for each symbol (see SymbolResolution below). 365 /// - After the linker has visited each input file (and each regular object 366 /// file) and computed a resolution for each symbol, take each lto::InputFile 367 /// and pass it and an array of symbol resolutions to the add() function. 368 /// - Call the getMaxTasks() function to get an upper bound on the number of 369 /// native object files that LTO may add to the link. 370 /// - Call the run() function. This function will use the supplied AddStream 371 /// and Cache functions to add up to getMaxTasks() native object files to 372 /// the link. 373 class LTO { 374 friend InputFile; 375 376 public: 377 /// Unified LTO modes 378 enum LTOKind { 379 /// Any LTO mode without Unified LTO. The default mode. 380 LTOK_Default, 381 382 /// Regular LTO, with Unified LTO enabled. 383 LTOK_UnifiedRegular, 384 385 /// ThinLTO, with Unified LTO enabled. 386 LTOK_UnifiedThin, 387 }; 388 389 /// Create an LTO object. A default constructed LTO object has a reasonable 390 /// production configuration, but you can customize it by passing arguments to 391 /// this constructor. 392 /// FIXME: We do currently require the DiagHandler field to be set in Conf. 393 /// Until that is fixed, a Config argument is required. 394 LLVM_ABI LTO(Config Conf, ThinBackend Backend = {}, 395 unsigned ParallelCodeGenParallelismLevel = 1, 396 LTOKind LTOMode = LTOK_Default); 397 LLVM_ABI ~LTO(); 398 399 /// Add an input file to the LTO link, using the provided symbol resolutions. 400 /// The symbol resolutions must appear in the enumeration order given by 401 /// InputFile::symbols(). 402 LLVM_ABI Error add(std::unique_ptr<InputFile> Obj, 403 ArrayRef<SymbolResolution> Res); 404 405 /// Returns an upper bound on the number of tasks that the client may expect. 406 /// This may only be called after all IR object files have been added. For a 407 /// full description of tasks see LTOBackend.h. 408 LLVM_ABI unsigned getMaxTasks() const; 409 410 /// Runs the LTO pipeline. This function calls the supplied AddStream 411 /// function to add native object files to the link. 412 /// 413 /// The Cache parameter is optional. If supplied, it will be used to cache 414 /// native object files and add them to the link. 415 /// 416 /// The client will receive at most one callback (via either AddStream or 417 /// Cache) for each task identifier. 418 LLVM_ABI Error run(AddStreamFn AddStream, FileCache Cache = {}); 419 420 /// Static method that returns a list of libcall symbols that can be generated 421 /// by LTO but might not be visible from bitcode symbol table. 422 LLVM_ABI static SmallVector<const char *> 423 getRuntimeLibcallSymbols(const Triple &TT); 424 425 private: 426 Config Conf; 427 428 struct RegularLTOState { 429 LLVM_ABI RegularLTOState(unsigned ParallelCodeGenParallelismLevel, 430 const Config &Conf); 431 struct CommonResolution { 432 uint64_t Size = 0; 433 Align Alignment; 434 /// Record if at least one instance of the common was marked as prevailing 435 bool Prevailing = false; 436 }; 437 std::map<std::string, CommonResolution> Commons; 438 439 unsigned ParallelCodeGenParallelismLevel; 440 LTOLLVMContext Ctx; 441 std::unique_ptr<Module> CombinedModule; 442 std::unique_ptr<IRMover> Mover; 443 444 // This stores the information about a regular LTO module that we have added 445 // to the link. It will either be linked immediately (for modules without 446 // summaries) or after summary-based dead stripping (for modules with 447 // summaries). 448 struct AddedModule { 449 std::unique_ptr<Module> M; 450 std::vector<GlobalValue *> Keep; 451 }; 452 std::vector<AddedModule> ModsWithSummaries; 453 bool EmptyCombinedModule = true; 454 } RegularLTO; 455 456 using ModuleMapType = MapVector<StringRef, BitcodeModule>; 457 458 struct ThinLTOState { 459 LLVM_ABI ThinLTOState(ThinBackend Backend); 460 461 ThinBackend Backend; 462 ModuleSummaryIndex CombinedIndex; 463 // The full set of bitcode modules in input order. 464 ModuleMapType ModuleMap; 465 // The bitcode modules to compile, if specified by the LTO Config. 466 std::optional<ModuleMapType> ModulesToCompile; 467 DenseMap<GlobalValue::GUID, StringRef> PrevailingModuleForGUID; 468 } ThinLTO; 469 470 // The global resolution for a particular (mangled) symbol name. This is in 471 // particular necessary to track whether each symbol can be internalized. 472 // Because any input file may introduce a new cross-partition reference, we 473 // cannot make any final internalization decisions until all input files have 474 // been added and the client has called run(). During run() we apply 475 // internalization decisions either directly to the module (for regular LTO) 476 // or to the combined index (for ThinLTO). 477 struct GlobalResolution { 478 /// The unmangled name of the global. 479 std::string IRName; 480 481 /// Keep track if the symbol is visible outside of a module with a summary 482 /// (i.e. in either a regular object or a regular LTO module without a 483 /// summary). 484 bool VisibleOutsideSummary = false; 485 486 /// The symbol was exported dynamically, and therefore could be referenced 487 /// by a shared library not visible to the linker. 488 bool ExportDynamic = false; 489 490 bool UnnamedAddr = true; 491 492 /// True if module contains the prevailing definition. 493 bool Prevailing = false; 494 495 /// Returns true if module contains the prevailing definition and symbol is 496 /// an IR symbol. For example when module-level inline asm block is used, 497 /// symbol can be prevailing in module but have no IR name. isPrevailingIRSymbolGlobalResolution498 bool isPrevailingIRSymbol() const { return Prevailing && !IRName.empty(); } 499 500 /// This field keeps track of the partition number of this global. The 501 /// regular LTO object is partition 0, while each ThinLTO object has its own 502 /// partition number from 1 onwards. 503 /// 504 /// Any global that is defined or used by more than one partition, or that 505 /// is referenced externally, may not be internalized. 506 /// 507 /// Partitions generally have a one-to-one correspondence with tasks, except 508 /// that we use partition 0 for all parallel LTO code generation partitions. 509 /// Any partitioning of the combined LTO object is done internally by the 510 /// LTO backend. 511 unsigned Partition = Unknown; 512 513 /// Special partition numbers. 514 enum : unsigned { 515 /// A partition number has not yet been assigned to this global. 516 Unknown = -1u, 517 518 /// This global is either used by more than one partition or has an 519 /// external reference, and therefore cannot be internalized. 520 External = -2u, 521 522 /// The RegularLTO partition 523 RegularLTO = 0, 524 }; 525 }; 526 527 // GlobalResolutionSymbolSaver allocator. 528 std::unique_ptr<llvm::BumpPtrAllocator> Alloc; 529 530 // Symbol saver for global resolution map. 531 std::unique_ptr<llvm::StringSaver> GlobalResolutionSymbolSaver; 532 533 // Global mapping from mangled symbol names to resolutions. 534 // Make this an unique_ptr to guard against accessing after it has been reset 535 // (to reduce memory after we're done with it). 536 std::unique_ptr<llvm::DenseMap<StringRef, GlobalResolution>> 537 GlobalResolutions; 538 539 void releaseGlobalResolutionsMemory(); 540 541 void addModuleToGlobalRes(ArrayRef<InputFile::Symbol> Syms, 542 ArrayRef<SymbolResolution> Res, unsigned Partition, 543 bool InSummary); 544 545 // These functions take a range of symbol resolutions [ResI, ResE) and consume 546 // the resolutions used by a single input module by incrementing ResI. After 547 // these functions return, [ResI, ResE) will refer to the resolution range for 548 // the remaining modules in the InputFile. 549 Error addModule(InputFile &Input, unsigned ModI, 550 const SymbolResolution *&ResI, const SymbolResolution *ResE); 551 552 Expected<RegularLTOState::AddedModule> 553 addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms, 554 const SymbolResolution *&ResI, const SymbolResolution *ResE); 555 Error linkRegularLTO(RegularLTOState::AddedModule Mod, 556 bool LivenessFromIndex); 557 558 Error addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms, 559 const SymbolResolution *&ResI, const SymbolResolution *ResE); 560 561 Error runRegularLTO(AddStreamFn AddStream); 562 Error runThinLTO(AddStreamFn AddStream, FileCache Cache, 563 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols); 564 565 Error checkPartiallySplit(); 566 567 mutable bool CalledGetMaxTasks = false; 568 569 // LTO mode when using Unified LTO. 570 LTOKind LTOMode; 571 572 // Use Optional to distinguish false from not yet initialized. 573 std::optional<bool> EnableSplitLTOUnit; 574 575 // Identify symbols exported dynamically, and that therefore could be 576 // referenced by a shared library not visible to the linker. 577 DenseSet<GlobalValue::GUID> DynamicExportSymbols; 578 579 // Diagnostic optimization remarks file 580 std::unique_ptr<ToolOutputFile> DiagnosticOutputFile; 581 }; 582 583 /// The resolution for a symbol. The linker must provide a SymbolResolution for 584 /// each global symbol based on its internal resolution of that symbol. 585 struct SymbolResolution { SymbolResolutionSymbolResolution586 SymbolResolution() 587 : Prevailing(0), FinalDefinitionInLinkageUnit(0), VisibleToRegularObj(0), 588 ExportDynamic(0), LinkerRedefined(0) {} 589 590 /// The linker has chosen this definition of the symbol. 591 unsigned Prevailing : 1; 592 593 /// The definition of this symbol is unpreemptable at runtime and is known to 594 /// be in this linkage unit. 595 unsigned FinalDefinitionInLinkageUnit : 1; 596 597 /// The definition of this symbol is visible outside of the LTO unit. 598 unsigned VisibleToRegularObj : 1; 599 600 /// The symbol was exported dynamically, and therefore could be referenced 601 /// by a shared library not visible to the linker. 602 unsigned ExportDynamic : 1; 603 604 /// Linker redefined version of the symbol which appeared in -wrap or -defsym 605 /// linker option. 606 unsigned LinkerRedefined : 1; 607 }; 608 609 } // namespace lto 610 } // namespace llvm 611 612 #endif 613