LLVM 22.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
19#include <memory>
20
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/MapVector.h"
25#include "llvm/LTO/Config.h"
28#include "llvm/Support/Error.h"
31#include "llvm/Support/thread.h"
34
35namespace llvm {
36
37class Error;
38class IRMover;
39class LLVMContext;
40class MemoryBufferRef;
41class Module;
42class raw_pwrite_stream;
43class 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).
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.
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.
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.
80LLVM_ABI std::string recomputeLTOCacheKey(const std::string &Key,
81 StringRef ExtraID);
82
83namespace lto {
84
85LLVM_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.
90LLVM_ABI std::string getThinLTOOutputFile(StringRef Path, StringRef OldPrefix,
91 StringRef NewPrefix);
92
93/// Setup optimization remarks.
94LLVM_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.
100LLVM_ABI Expected<std::unique_ptr<ToolOutputFile>>
101setupStatsFile(StringRef StatsFilename);
102
103/// Produces a container ordering for optimal multi-threaded processing. Returns
104/// ordered indices to elements in the input array.
105LLVM_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.
111 const ModuleSummaryIndex &Index);
112
113class LTO;
114struct 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.
119public:
120 struct Symbol;
121
122private:
123 // FIXME: Remove LTO class friendship once we have bitcode symbol tables.
124 friend LTO;
125 InputFile() = default;
126
127 std::vector<BitcodeModule> Mods;
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
138public:
140
141 /// Create an 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.
148 friend LTO;
149
150 public:
151 Symbol(const irsymtab::Symbol &S) : irsymtab::Symbol(S) {}
152
169 };
170
171 /// A range over the symbols in this InputFile.
172 ArrayRef<Symbol> symbols() const { return Symbols; }
173
174 /// Returns linker options specified in the input file.
175 StringRef getCOFFLinkerOpts() const { return COFFLinkerOpts; }
176
177 /// Returns dependent library specifiers from the input file.
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.
184 StringRef getTargetTriple() const { return TargetTriple; }
185
186 /// Returns the source file path specified at compile time.
187 StringRef getSourceFileName() const { return SourceFileName; }
188
189 // Returns a table with all the comdats used by this file.
191 return ComdatTable;
192 }
193
194 // Returns the only BitcodeModule from InputFile.
196
197private:
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
204using IndexWriteCallback = std::function<void(const std::string &)>;
205
207
208/// This class defines the interface to the ThinLTO backend.
210protected:
211 const Config &Conf;
217 std::optional<Error> Err;
218 std::mutex ErrMu;
219
220public:
225 ThreadPoolStrategy ThinLTOParallelism)
229 BackendThreadPool(ThinLTOParallelism) {}
230
231 virtual ~ThinBackendProc() = default;
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,
240 virtual Error wait() {
242 if (Err)
243 return std::move(*Err);
244 return Error::success();
245 }
247 virtual bool isSensitiveToInputOrder() { return false; }
248
249 // Write sharded indices and (optionally) imports to disk
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.
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.
269using 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.
281 : Func(std::move(Func)), Parallelism(std::move(Parallelism)) {}
282 ThinBackend() = default;
283
284 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 }
292 ThreadPoolStrategy getParallelism() const { return Parallelism; }
293 bool isValid() const { return static_cast<bool>(Func); }
294
295private:
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.
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.
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.
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.
373class LTO {
374 friend InputFile;
375
376public:
377 /// Unified LTO modes
378 enum LTOKind {
379 /// Any LTO mode without Unified LTO. The default mode.
381
382 /// Regular LTO, with Unified LTO enabled.
384
385 /// ThinLTO, with Unified LTO enabled.
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);
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,
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.
424
425private:
426 Config Conf;
427
428 struct RegularLTOState {
429 LLVM_ABI RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
430 const Config &Conf);
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
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.
498 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 and consume the
546 // resolutions used by a single input module. Functions return ranges refering
547 // to the resolutions for the remaining modules in the InputFile.
548 Expected<ArrayRef<SymbolResolution>>
549 addModule(InputFile &Input, ArrayRef<SymbolResolution> InputRes,
550 unsigned ModI, ArrayRef<SymbolResolution> Res);
551
552 Expected<std::pair<RegularLTOState::AddedModule, ArrayRef<SymbolResolution>>>
553 addRegularLTO(InputFile &Input, ArrayRef<SymbolResolution> InputRes,
554 BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
555 ArrayRef<SymbolResolution> Res);
556 Error linkRegularLTO(RegularLTOState::AddedModule Mod,
557 bool LivenessFromIndex);
558
559 Expected<ArrayRef<SymbolResolution>>
560 addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
561 ArrayRef<SymbolResolution> Res);
562
563 Error runRegularLTO(AddStreamFn AddStream);
564 Error runThinLTO(AddStreamFn AddStream, FileCache Cache,
565 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols);
566
567 Error checkPartiallySplit();
568
569 mutable bool CalledGetMaxTasks = false;
570
571 // LTO mode when using Unified LTO.
572 LTOKind LTOMode;
573
574 // Use Optional to distinguish false from not yet initialized.
575 std::optional<bool> EnableSplitLTOUnit;
576
577 // Identify symbols exported dynamically, and that therefore could be
578 // referenced by a shared library not visible to the linker.
579 DenseSet<GlobalValue::GUID> DynamicExportSymbols;
580
581 // Diagnostic optimization remarks file
582 std::unique_ptr<ToolOutputFile> DiagnosticOutputFile;
583};
584
585/// The resolution for a symbol. The linker must provide a SymbolResolution for
586/// each global symbol based on its internal resolution of that symbol.
591
592 /// The linker has chosen this definition of the symbol.
593 unsigned Prevailing : 1;
594
595 /// The definition of this symbol is unpreemptable at runtime and is known to
596 /// be in this linkage unit.
598
599 /// The definition of this symbol is visible outside of the LTO unit.
601
602 /// The symbol was exported dynamically, and therefore could be referenced
603 /// by a shared library not visible to the linker.
604 unsigned ExportDynamic : 1;
605
606 /// Linker redefined version of the symbol which appeared in -wrap or -defsym
607 /// linker option.
608 unsigned LinkerRedefined : 1;
609};
610
611} // namespace lto
612} // namespace llvm
613
614#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition: Compiler.h:213
This file defines the DenseMap class.
uint32_t Index
Provides passes for computing function attributes based on interprocedural analyses.
#define I(x, y, z)
Definition: MD5.cpp:58
Machine Check Debug Module
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...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Represents a module in a bitcode file.
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.
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Definition: GlobalValue.h:577
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:52
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:36
Class to hold module path string table and global value map, and encapsulate methods for operating on...
A non-threaded implementation.
Definition: ThreadPool.h:215
void wait() override
Blocking wait for all the tasks to execute first.
Definition: ThreadPool.cpp:200
unsigned getMaxConcurrency() const override
Returns always 1: there is no concurrency.
Definition: ThreadPool.h:230
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
This tells how a thread pool will be used.
Definition: Threading.h:115
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:47
An input file.
Definition: LTO.h:118
static LLVM_ABI Expected< std::unique_ptr< InputFile > > create(MemoryBufferRef Object)
Create an InputFile.
Definition: LTO.cpp:561
ArrayRef< Symbol > symbols() const
A range over the symbols in this InputFile.
Definition: LTO.h:172
StringRef getCOFFLinkerOpts() const
Returns linker options specified in the input file.
Definition: LTO.h:175
ArrayRef< StringRef > getDependentLibraries() const
Returns dependent library specifiers from the input file.
Definition: LTO.h:178
ArrayRef< std::pair< StringRef, Comdat::SelectionKind > > getComdatTable() const
Definition: LTO.h:190
StringRef getTargetTriple() const
Returns the input file's target triple.
Definition: LTO.h:184
LLVM_ABI StringRef getName() const
Returns the path to the InputFile.
Definition: LTO.cpp:590
LLVM_ABI BitcodeModule & getSingleBitcodeModule()
Definition: LTO.cpp:594
StringRef getSourceFileName() const
Returns the source file path specified at compile time.
Definition: LTO.h:187
This class implements a resolution-based interface to LLVM's LTO functionality.
Definition: LTO.h:373
LLVM_ABI 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:732
static LLVM_ABI SmallVector< const char * > getRuntimeLibcallSymbols(const Triple &TT)
Static method that returns a list of libcall symbols that can be generated by LTO but might not be vi...
Definition: LTO.cpp:1417
LTOKind
Unified LTO modes.
Definition: LTO.h:378
@ LTOK_UnifiedRegular
Regular LTO, with Unified LTO enabled.
Definition: LTO.h:383
@ LTOK_Default
Any LTO mode without Unified LTO. The default mode.
Definition: LTO.h:380
@ LTOK_UnifiedThin
ThinLTO, with Unified LTO enabled.
Definition: LTO.h:386
LLVM_ABI ~LTO()
LLVM_ABI unsigned getMaxTasks() const
Returns an upper bound on the number of tasks that the client may expect.
Definition: LTO.cpp:1144
LLVM_ABI Error run(AddStreamFn AddStream, FileCache Cache={})
Runs the LTO pipeline.
Definition: LTO.cpp:1195
This class defines the interface to the ThinLTO backend.
Definition: LTO.h:209
DefaultThreadPool BackendThreadPool
Definition: LTO.h:216
const Config & Conf
Definition: LTO.h:211
std::optional< Error > Err
Definition: LTO.h:217
virtual bool isSensitiveToInputOrder()
Definition: LTO.h:247
unsigned getThreadCount()
Definition: LTO.h:246
const DenseMap< StringRef, GVSummaryMapTy > & ModuleToDefinedGVSummaries
Definition: LTO.h:213
LLVM_ABI Error emitFiles(const FunctionImporter::ImportMapTy &ImportList, StringRef ModulePath, const std::string &NewModulePath) const
Definition: LTO.cpp:1431
std::mutex ErrMu
Definition: LTO.h:218
ThinBackendProc(const Config &Conf, ModuleSummaryIndex &CombinedIndex, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, lto::IndexWriteCallback OnWrite, bool ShouldEmitImportsFiles, ThreadPoolStrategy ThinLTOParallelism)
Definition: LTO.h:221
virtual Error wait()
Definition: LTO.h:240
ModuleSummaryIndex & CombinedIndex
Definition: LTO.h:212
virtual void setup(unsigned ThinLTONumTasks, unsigned ThinLTOTaskOffset, Triple Triple)
Definition: LTO.h:232
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
IndexWriteCallback OnWrite
Definition: LTO.h:214
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:461
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
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:1779
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:1813
std::function< std::unique_ptr< ThinBackendProc >(const Config &C, ModuleSummaryIndex &CombinedIndex, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, AddStreamFn AddStream, FileCache Cache)> ThinBackendFunction
This callable defines the behavior of a ThinLTO backend after the thin-link phase.
Definition: LTO.h:272
std::function< void(const std::string &)> IndexWriteCallback
Definition: LTO.h:204
LLVM_ABI StringLiteral getThinLTODefaultCPU(const Triple &TheTriple)
Definition: LTO.cpp:1795
LLVM_ABI Expected< std::unique_ptr< ToolOutputFile > > setupStatsFile(StringRef StatsFilename)
Setups the output file for saving statistics.
Definition: LTO.cpp:2192
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:1899
LLVM_ABI ThinBackend createOutOfProcessThinBackend(ThreadPoolStrategy Parallelism, IndexWriteCallback OnWrite, bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles, StringRef LinkerOutputFile, StringRef Distributor, ArrayRef< StringRef > DistributorArgs, StringRef RemoteCompiler, ArrayRef< StringRef > RemoteCompilerArgs, bool SaveTemps)
This ThinBackend generates the index shards and then runs the individual backend jobs via an external...
Definition: LTO.cpp:2529
LLVM_ABI std::vector< int > generateModulesOrdering(ArrayRef< BitcodeModule * > R)
Produces a container ordering for optimal multi-threaded processing.
Definition: LTO.cpp:2211
LLVM_ABI Expected< std::unique_ptr< ToolOutputFile > > setupLLVMOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses, StringRef RemarksFormat, bool RemarksWithHotness, std::optional< uint64_t > RemarksHotnessThreshold=0, int Count=-1)
Setup optimization remarks.
Definition: LTO.cpp:2167
LLVM_ABI void updateMemProfAttributes(Module &Mod, const ModuleSummaryIndex &Index)
Updates MemProf attributes (and metadata) based on whether the index has recorded that we are linking...
Definition: LTO.cpp:1253
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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"))
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"))
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:60
LLVM_ABI void thinLTOInternalizeAndPromoteInIndex(ModuleSummaryIndex &Index, function_ref< bool(StringRef, ValueInfo)> isExported, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing)
Update the linkages in the given Index to mark exported values as external and non-exported values as...
Definition: LTO.cpp:548
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:353
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:447
@ Mod
The access may modify the value stored in memory.
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:1886
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:103
DenseMap< GlobalValue::GUID, GlobalValueSummary * > GVSummaryMapTy
Map of global value GUID to its summary, used to identify values defined in a particular module,...
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
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:85
This represents a symbol that has been read from a storage::Symbol and possibly a storage::Uncommon.
Definition: IRSymtab.h:172
StringRef getName() const
Returns the mangled symbol name.
Definition: IRSymtab.h:185
bool canBeOmittedFromSymbolTable() const
Definition: IRSymtab.h:208
bool isUsed() const
Definition: IRSymtab.h:205
StringRef getSectionName() const
Definition: IRSymtab.h:234
bool isTLS() const
Definition: IRSymtab.h:206
bool isWeak() const
Definition: IRSymtab.h:202
bool isIndirect() const
Definition: IRSymtab.h:204
bool isCommon() const
Definition: IRSymtab.h:203
uint32_t getCommonAlignment() const
Definition: IRSymtab.h:222
bool isExecutable() const
Definition: IRSymtab.h:215
uint64_t getCommonSize() const
Definition: IRSymtab.h:217
int getComdatIndex() const
Returns the index into the comdat table (see Reader::getComdatTable()), or -1 if not a comdat member.
Definition: IRSymtab.h:193
GlobalValue::VisibilityTypes getVisibility() const
Definition: IRSymtab.h:197
bool isUndefined() const
Definition: IRSymtab.h:201
StringRef getIRName() const
Returns the unmangled symbol name, or the empty string if this is not an IR symbol.
Definition: IRSymtab.h:189
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:229
Contains the information needed by linkers for symbol resolution, as well as by the LTO implementatio...
Definition: IRSymtab.h:92
LTO configuration.
Definition: Config.h:42
The purpose of this struct is to only expose the symbol information that an LTO client should need in...
Definition: LTO.h:147
Symbol(const irsymtab::Symbol &S)
Definition: LTO.h:151
A derived class of LLVMContext that initializes itself according to a given Config object.
Definition: Config.h:300
std::vector< GlobalValue * > Keep
Definition: LTO.h:450
std::unique_ptr< Module > M
Definition: LTO.h:449
bool Prevailing
Record if at least one instance of the common was marked as prevailing.
Definition: LTO.h:435
The resolution for a symbol.
Definition: LTO.h:587
unsigned FinalDefinitionInLinkageUnit
The definition of this symbol is unpreemptable at runtime and is known to be in this linkage unit.
Definition: LTO.h:597
unsigned ExportDynamic
The symbol was exported dynamically, and therefore could be referenced by a shared library not visibl...
Definition: LTO.h:604
unsigned Prevailing
The linker has chosen this definition of the symbol.
Definition: LTO.h:593
unsigned LinkerRedefined
Linker redefined version of the symbol which appeared in -wrap or -defsym linker option.
Definition: LTO.h:608
unsigned VisibleToRegularObj
The definition of this symbol is visible outside of the LTO unit.
Definition: LTO.h:600
This type defines the behavior following the thin-link phase during ThinLTO.
Definition: LTO.h:279
std::unique_ptr< ThinBackendProc > operator()(const Config &Conf, ModuleSummaryIndex &CombinedIndex, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, AddStreamFn AddStream, FileCache Cache)
Definition: LTO.h:284
bool isValid() const
Definition: LTO.h:293
ThreadPoolStrategy getParallelism() const
Definition: LTO.h:292
ThinBackend(ThinBackendFunction Func, ThreadPoolStrategy Parallelism)
Definition: LTO.h:280