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/ADT/MapVector.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/Bitcode/BitcodeReader.h"
21 #include "llvm/IR/ModuleSummaryIndex.h"
22 #include "llvm/LTO/Config.h"
23 #include "llvm/Object/IRSymtab.h"
24 #include "llvm/Support/Caching.h"
25 #include "llvm/Support/Error.h"
26 #include "llvm/Support/thread.h"
27 #include "llvm/Transforms/IPO/FunctionAttrs.h"
28 #include "llvm/Transforms/IPO/FunctionImport.h"
29 
30 namespace llvm {
31 
32 class Error;
33 class IRMover;
34 class LLVMContext;
35 class MemoryBufferRef;
36 class Module;
37 class raw_pwrite_stream;
38 class ToolOutputFile;
39 
40 /// Resolve linkage for prevailing symbols in the \p Index. Linkage changes
41 /// recorded in the index and the ThinLTO backends must apply the changes to
42 /// the module via thinLTOFinalizeInModule.
43 ///
44 /// This is done for correctness (if value exported, ensure we always
45 /// emit a copy), and compile-time optimization (allow drop of duplicates).
46 void thinLTOResolvePrevailingInIndex(
47     const lto::Config &C, ModuleSummaryIndex &Index,
48     function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
49         isPrevailing,
50     function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
51         recordNewLinkage,
52     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols);
53 
54 /// Update the linkages in the given \p Index to mark exported values
55 /// as external and non-exported values as internal. The ThinLTO backends
56 /// must apply the changes to the Module via thinLTOInternalizeModule.
57 void thinLTOInternalizeAndPromoteInIndex(
58     ModuleSummaryIndex &Index,
59     function_ref<bool(StringRef, ValueInfo)> isExported,
60     function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
61         isPrevailing);
62 
63 /// Computes a unique hash for the Module considering the current list of
64 /// export/import and other global analysis results.
65 /// The hash is produced in \p Key.
66 void computeLTOCacheKey(
67     SmallString<40> &Key, const lto::Config &Conf,
68     const ModuleSummaryIndex &Index, StringRef ModuleID,
69     const FunctionImporter::ImportMapTy &ImportList,
70     const FunctionImporter::ExportSetTy &ExportList,
71     const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
72     const GVSummaryMapTy &DefinedGlobals,
73     const std::set<GlobalValue::GUID> &CfiFunctionDefs = {},
74     const std::set<GlobalValue::GUID> &CfiFunctionDecls = {});
75 
76 namespace lto {
77 
78 /// Given the original \p Path to an output file, replace any path
79 /// prefix matching \p OldPrefix with \p NewPrefix. Also, create the
80 /// resulting directory if it does not yet exist.
81 std::string getThinLTOOutputFile(const std::string &Path,
82                                  const std::string &OldPrefix,
83                                  const std::string &NewPrefix);
84 
85 /// Setup optimization remarks.
86 Expected<std::unique_ptr<ToolOutputFile>> setupLLVMOptimizationRemarks(
87     LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses,
88     StringRef RemarksFormat, bool RemarksWithHotness,
89     Optional<uint64_t> RemarksHotnessThreshold = 0, int Count = -1);
90 
91 /// Setups the output file for saving statistics.
92 Expected<std::unique_ptr<ToolOutputFile>>
93 setupStatsFile(StringRef StatsFilename);
94 
95 /// Produces a container ordering for optimal multi-threaded processing. Returns
96 /// ordered indices to elements in the input array.
97 std::vector<int> generateModulesOrdering(ArrayRef<BitcodeModule *> R);
98 
99 class LTO;
100 struct SymbolResolution;
101 class ThinBackendProc;
102 
103 /// An input file. This is a symbol table wrapper that only exposes the
104 /// information that an LTO client should need in order to do symbol resolution.
105 class InputFile {
106 public:
107   class Symbol;
108 
109 private:
110   // FIXME: Remove LTO class friendship once we have bitcode symbol tables.
111   friend LTO;
112   InputFile() = default;
113 
114   std::vector<BitcodeModule> Mods;
115   SmallVector<char, 0> Strtab;
116   std::vector<Symbol> Symbols;
117 
118   // [begin, end) for each module
119   std::vector<std::pair<size_t, size_t>> ModuleSymIndices;
120 
121   StringRef TargetTriple, SourceFileName, COFFLinkerOpts;
122   std::vector<StringRef> DependentLibraries;
123   std::vector<std::pair<StringRef, Comdat::SelectionKind>> ComdatTable;
124 
125 public:
126   ~InputFile();
127 
128   /// Create an InputFile.
129   static Expected<std::unique_ptr<InputFile>> create(MemoryBufferRef Object);
130 
131   /// The purpose of this class is to only expose the symbol information that an
132   /// LTO client should need in order to do symbol resolution.
133   class Symbol : irsymtab::Symbol {
134     friend LTO;
135 
136   public:
137     Symbol(const irsymtab::Symbol &S) : irsymtab::Symbol(S) {}
138 
139     using irsymtab::Symbol::isUndefined;
140     using irsymtab::Symbol::isCommon;
141     using irsymtab::Symbol::isWeak;
142     using irsymtab::Symbol::isIndirect;
143     using irsymtab::Symbol::getName;
144     using irsymtab::Symbol::getIRName;
145     using irsymtab::Symbol::getVisibility;
146     using irsymtab::Symbol::canBeOmittedFromSymbolTable;
147     using irsymtab::Symbol::isTLS;
148     using irsymtab::Symbol::getComdatIndex;
149     using irsymtab::Symbol::getCommonSize;
150     using irsymtab::Symbol::getCommonAlignment;
151     using irsymtab::Symbol::getCOFFWeakExternalFallback;
152     using irsymtab::Symbol::getSectionName;
153     using irsymtab::Symbol::isExecutable;
154     using irsymtab::Symbol::isUsed;
155   };
156 
157   /// A range over the symbols in this InputFile.
158   ArrayRef<Symbol> symbols() const { return Symbols; }
159 
160   /// Returns linker options specified in the input file.
161   StringRef getCOFFLinkerOpts() const { return COFFLinkerOpts; }
162 
163   /// Returns dependent library specifiers from the input file.
164   ArrayRef<StringRef> getDependentLibraries() const { return DependentLibraries; }
165 
166   /// Returns the path to the InputFile.
167   StringRef getName() const;
168 
169   /// Returns the input file's target triple.
170   StringRef getTargetTriple() const { return TargetTriple; }
171 
172   /// Returns the source file path specified at compile time.
173   StringRef getSourceFileName() const { return SourceFileName; }
174 
175   // Returns a table with all the comdats used by this file.
176   ArrayRef<std::pair<StringRef, Comdat::SelectionKind>> getComdatTable() const {
177     return ComdatTable;
178   }
179 
180   // Returns the only BitcodeModule from InputFile.
181   BitcodeModule &getSingleBitcodeModule();
182 
183 private:
184   ArrayRef<Symbol> module_symbols(unsigned I) const {
185     const auto &Indices = ModuleSymIndices[I];
186     return {Symbols.data() + Indices.first, Symbols.data() + Indices.second};
187   }
188 };
189 
190 /// A ThinBackend defines what happens after the thin-link phase during ThinLTO.
191 /// The details of this type definition aren't important; clients can only
192 /// create a ThinBackend using one of the create*ThinBackend() functions below.
193 using ThinBackend = std::function<std::unique_ptr<ThinBackendProc>(
194     const Config &C, ModuleSummaryIndex &CombinedIndex,
195     StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
196     AddStreamFn AddStream, FileCache Cache)>;
197 
198 /// This ThinBackend runs the individual backend jobs in-process.
199 /// The default value means to use one job per hardware core (not hyper-thread).
200 /// OnWrite is callback which receives module identifier and notifies LTO user
201 /// that index file for the module (and optionally imports file) was created.
202 /// ShouldEmitIndexFiles being true will write sharded ThinLTO index files
203 /// to the same path as the input module, with suffix ".thinlto.bc"
204 /// ShouldEmitImportsFiles is true it also writes a list of imported files to a
205 /// similar path with ".imports" appended instead.
206 using IndexWriteCallback = std::function<void(const std::string &)>;
207 ThinBackend createInProcessThinBackend(ThreadPoolStrategy Parallelism,
208                                        IndexWriteCallback OnWrite = nullptr,
209                                        bool ShouldEmitIndexFiles = false,
210                                        bool ShouldEmitImportsFiles = false);
211 
212 /// This ThinBackend writes individual module indexes to files, instead of
213 /// running the individual backend jobs. This backend is for distributed builds
214 /// where separate processes will invoke the real backends.
215 ///
216 /// To find the path to write the index to, the backend checks if the path has a
217 /// prefix of OldPrefix; if so, it replaces that prefix with NewPrefix. It then
218 /// appends ".thinlto.bc" and writes the index to that path. If
219 /// ShouldEmitImportsFiles is true it also writes a list of imported files to a
220 /// similar path with ".imports" appended instead.
221 /// LinkedObjectsFile is an output stream to write the list of object files for
222 /// the final ThinLTO linking. Can be nullptr.
223 /// OnWrite is callback which receives module identifier and notifies LTO user
224 /// that index file for the module (and optionally imports file) was created.
225 ThinBackend createWriteIndexesThinBackend(std::string OldPrefix,
226                                           std::string NewPrefix,
227                                           bool ShouldEmitImportsFiles,
228                                           raw_fd_ostream *LinkedObjectsFile,
229                                           IndexWriteCallback OnWrite);
230 
231 /// This class implements a resolution-based interface to LLVM's LTO
232 /// functionality. It supports regular LTO, parallel LTO code generation and
233 /// ThinLTO. You can use it from a linker in the following way:
234 /// - Set hooks and code generation options (see lto::Config struct defined in
235 ///   Config.h), and use the lto::Config object to create an lto::LTO object.
236 /// - Create lto::InputFile objects using lto::InputFile::create(), then use
237 ///   the symbols() function to enumerate its symbols and compute a resolution
238 ///   for each symbol (see SymbolResolution below).
239 /// - After the linker has visited each input file (and each regular object
240 ///   file) and computed a resolution for each symbol, take each lto::InputFile
241 ///   and pass it and an array of symbol resolutions to the add() function.
242 /// - Call the getMaxTasks() function to get an upper bound on the number of
243 ///   native object files that LTO may add to the link.
244 /// - Call the run() function. This function will use the supplied AddStream
245 ///   and Cache functions to add up to getMaxTasks() native object files to
246 ///   the link.
247 class LTO {
248   friend InputFile;
249 
250 public:
251   /// Create an LTO object. A default constructed LTO object has a reasonable
252   /// production configuration, but you can customize it by passing arguments to
253   /// this constructor.
254   /// FIXME: We do currently require the DiagHandler field to be set in Conf.
255   /// Until that is fixed, a Config argument is required.
256   LTO(Config Conf, ThinBackend Backend = nullptr,
257       unsigned ParallelCodeGenParallelismLevel = 1);
258   ~LTO();
259 
260   /// Add an input file to the LTO link, using the provided symbol resolutions.
261   /// The symbol resolutions must appear in the enumeration order given by
262   /// InputFile::symbols().
263   Error add(std::unique_ptr<InputFile> Obj, ArrayRef<SymbolResolution> Res);
264 
265   /// Returns an upper bound on the number of tasks that the client may expect.
266   /// This may only be called after all IR object files have been added. For a
267   /// full description of tasks see LTOBackend.h.
268   unsigned getMaxTasks() const;
269 
270   /// Runs the LTO pipeline. This function calls the supplied AddStream
271   /// function to add native object files to the link.
272   ///
273   /// The Cache parameter is optional. If supplied, it will be used to cache
274   /// native object files and add them to the link.
275   ///
276   /// The client will receive at most one callback (via either AddStream or
277   /// Cache) for each task identifier.
278   Error run(AddStreamFn AddStream, FileCache Cache = nullptr);
279 
280   /// Static method that returns a list of libcall symbols that can be generated
281   /// by LTO but might not be visible from bitcode symbol table.
282   static ArrayRef<const char*> getRuntimeLibcallSymbols();
283 
284 private:
285   Config Conf;
286 
287   struct RegularLTOState {
288     RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
289                     const Config &Conf);
290     struct CommonResolution {
291       uint64_t Size = 0;
292       MaybeAlign Align;
293       /// Record if at least one instance of the common was marked as prevailing
294       bool Prevailing = false;
295     };
296     std::map<std::string, CommonResolution> Commons;
297 
298     unsigned ParallelCodeGenParallelismLevel;
299     LTOLLVMContext Ctx;
300     std::unique_ptr<Module> CombinedModule;
301     std::unique_ptr<IRMover> Mover;
302 
303     // This stores the information about a regular LTO module that we have added
304     // to the link. It will either be linked immediately (for modules without
305     // summaries) or after summary-based dead stripping (for modules with
306     // summaries).
307     struct AddedModule {
308       std::unique_ptr<Module> M;
309       std::vector<GlobalValue *> Keep;
310     };
311     std::vector<AddedModule> ModsWithSummaries;
312     bool EmptyCombinedModule = true;
313   } RegularLTO;
314 
315   using ModuleMapType = MapVector<StringRef, BitcodeModule>;
316 
317   struct ThinLTOState {
318     ThinLTOState(ThinBackend Backend);
319 
320     ThinBackend Backend;
321     ModuleSummaryIndex CombinedIndex;
322     // The full set of bitcode modules in input order.
323     ModuleMapType ModuleMap;
324     // The bitcode modules to compile, if specified by the LTO Config.
325     Optional<ModuleMapType> ModulesToCompile;
326     DenseMap<GlobalValue::GUID, StringRef> PrevailingModuleForGUID;
327   } ThinLTO;
328 
329   // The global resolution for a particular (mangled) symbol name. This is in
330   // particular necessary to track whether each symbol can be internalized.
331   // Because any input file may introduce a new cross-partition reference, we
332   // cannot make any final internalization decisions until all input files have
333   // been added and the client has called run(). During run() we apply
334   // internalization decisions either directly to the module (for regular LTO)
335   // or to the combined index (for ThinLTO).
336   struct GlobalResolution {
337     /// The unmangled name of the global.
338     std::string IRName;
339 
340     /// Keep track if the symbol is visible outside of a module with a summary
341     /// (i.e. in either a regular object or a regular LTO module without a
342     /// summary).
343     bool VisibleOutsideSummary = false;
344 
345     /// The symbol was exported dynamically, and therefore could be referenced
346     /// by a shared library not visible to the linker.
347     bool ExportDynamic = false;
348 
349     bool UnnamedAddr = true;
350 
351     /// True if module contains the prevailing definition.
352     bool Prevailing = false;
353 
354     /// Returns true if module contains the prevailing definition and symbol is
355     /// an IR symbol. For example when module-level inline asm block is used,
356     /// symbol can be prevailing in module but have no IR name.
357     bool isPrevailingIRSymbol() const { return Prevailing && !IRName.empty(); }
358 
359     /// This field keeps track of the partition number of this global. The
360     /// regular LTO object is partition 0, while each ThinLTO object has its own
361     /// partition number from 1 onwards.
362     ///
363     /// Any global that is defined or used by more than one partition, or that
364     /// is referenced externally, may not be internalized.
365     ///
366     /// Partitions generally have a one-to-one correspondence with tasks, except
367     /// that we use partition 0 for all parallel LTO code generation partitions.
368     /// Any partitioning of the combined LTO object is done internally by the
369     /// LTO backend.
370     unsigned Partition = Unknown;
371 
372     /// Special partition numbers.
373     enum : unsigned {
374       /// A partition number has not yet been assigned to this global.
375       Unknown = -1u,
376 
377       /// This global is either used by more than one partition or has an
378       /// external reference, and therefore cannot be internalized.
379       External = -2u,
380 
381       /// The RegularLTO partition
382       RegularLTO = 0,
383     };
384   };
385 
386   // Global mapping from mangled symbol names to resolutions.
387   StringMap<GlobalResolution> GlobalResolutions;
388 
389   void addModuleToGlobalRes(ArrayRef<InputFile::Symbol> Syms,
390                             ArrayRef<SymbolResolution> Res, unsigned Partition,
391                             bool InSummary);
392 
393   // These functions take a range of symbol resolutions [ResI, ResE) and consume
394   // the resolutions used by a single input module by incrementing ResI. After
395   // these functions return, [ResI, ResE) will refer to the resolution range for
396   // the remaining modules in the InputFile.
397   Error addModule(InputFile &Input, unsigned ModI,
398                   const SymbolResolution *&ResI, const SymbolResolution *ResE);
399 
400   Expected<RegularLTOState::AddedModule>
401   addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
402                 const SymbolResolution *&ResI, const SymbolResolution *ResE);
403   Error linkRegularLTO(RegularLTOState::AddedModule Mod,
404                        bool LivenessFromIndex);
405 
406   Error addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
407                    const SymbolResolution *&ResI, const SymbolResolution *ResE);
408 
409   Error runRegularLTO(AddStreamFn AddStream);
410   Error runThinLTO(AddStreamFn AddStream, FileCache Cache,
411                    const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols);
412 
413   Error checkPartiallySplit();
414 
415   mutable bool CalledGetMaxTasks = false;
416 
417   // Use Optional to distinguish false from not yet initialized.
418   Optional<bool> EnableSplitLTOUnit;
419 
420   // Identify symbols exported dynamically, and that therefore could be
421   // referenced by a shared library not visible to the linker.
422   DenseSet<GlobalValue::GUID> DynamicExportSymbols;
423 
424   // Diagnostic optimization remarks file
425   std::unique_ptr<ToolOutputFile> DiagnosticOutputFile;
426 };
427 
428 /// The resolution for a symbol. The linker must provide a SymbolResolution for
429 /// each global symbol based on its internal resolution of that symbol.
430 struct SymbolResolution {
431   SymbolResolution()
432       : Prevailing(0), FinalDefinitionInLinkageUnit(0), VisibleToRegularObj(0),
433         ExportDynamic(0), LinkerRedefined(0) {}
434 
435   /// The linker has chosen this definition of the symbol.
436   unsigned Prevailing : 1;
437 
438   /// The definition of this symbol is unpreemptable at runtime and is known to
439   /// be in this linkage unit.
440   unsigned FinalDefinitionInLinkageUnit : 1;
441 
442   /// The definition of this symbol is visible outside of the LTO unit.
443   unsigned VisibleToRegularObj : 1;
444 
445   /// The symbol was exported dynamically, and therefore could be referenced
446   /// by a shared library not visible to the linker.
447   unsigned ExportDynamic : 1;
448 
449   /// Linker redefined version of the symbol which appeared in -wrap or -defsym
450   /// linker option.
451   unsigned LinkerRedefined : 1;
452 };
453 
454 } // namespace lto
455 } // namespace llvm
456 
457 #endif
458