1 //===-ThinLTOCodeGenerator.h - LLVM Link Time Optimizer -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the ThinLTOCodeGenerator class, similar to the
11 // LTOCodeGenerator but for the ThinLTO scheme. It provides an interface for
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_LTO_THINLTOCODEGENERATOR_H
17 #define LLVM_LTO_THINLTOCODEGENERATOR_H
19 #include "llvm-c/lto.h"
20 #include "llvm/ADT/StringSet.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/IR/ModuleSummaryIndex.h"
23 #include "llvm/Support/CachePruning.h"
24 #include "llvm/Support/CodeGen.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Target/TargetOptions.h"
35 /// Wrapper around MemoryBufferRef, owning the identifier
37 std::string OwnedIdentifier;
41 ThinLTOBuffer(StringRef Buffer, StringRef Identifier)
42 : OwnedIdentifier(Identifier), Buffer(Buffer) {}
44 MemoryBufferRef getMemBuffer() const {
45 return MemoryBufferRef(Buffer,
46 {OwnedIdentifier.c_str(), OwnedIdentifier.size()});
48 StringRef getBuffer() const { return Buffer; }
49 StringRef getBufferIdentifier() const { return OwnedIdentifier; }
52 /// Helper to gather options relevant to the target machine creation
53 struct TargetMachineBuilder {
57 TargetOptions Options;
58 Optional<Reloc::Model> RelocModel;
59 CodeGenOpt::Level CGOptLevel = CodeGenOpt::Aggressive;
61 std::unique_ptr<TargetMachine> create() const;
64 /// This class define an interface similar to the LTOCodeGenerator, but adapted
65 /// for ThinLTO processing.
66 /// The ThinLTOCodeGenerator is not intended to be reuse for multiple
67 /// compilation: the model is that the client adds modules to the generator and
68 /// ask to perform the ThinLTO optimizations / codegen, and finally destroys the
70 class ThinLTOCodeGenerator {
72 /// Add given module to the code generator.
73 void addModule(StringRef Identifier, StringRef Data);
76 * Adds to a list of all global symbols that must exist in the final generated
77 * code. If a symbol is not listed there, it will be optimized away if it is
78 * inlined into every usage.
80 void preserveSymbol(StringRef Name);
83 * Adds to a list of all global symbols that are cross-referenced between
84 * ThinLTO files. If the ThinLTO CodeGenerator can ensure that every
85 * references from a ThinLTO module to this symbol is optimized away, then
86 * the symbol can be discarded.
88 void crossReferenceSymbol(StringRef Name);
91 * Process all the modules that were added to the code generator in parallel.
93 * Client can access the resulting object files using getProducedBinaries(),
94 * unless setGeneratedObjectsDirectory() has been called, in which case
95 * results are available through getProducedBinaryFiles().
100 * Return the "in memory" binaries produced by the code generator. This is
101 * filled after run() unless setGeneratedObjectsDirectory() has been
102 * called, in which case results are available through
103 * getProducedBinaryFiles().
105 std::vector<std::unique_ptr<MemoryBuffer>> &getProducedBinaries() {
106 return ProducedBinaries;
110 * Return the "on-disk" binaries produced by the code generator. This is
111 * filled after run() when setGeneratedObjectsDirectory() has been
112 * called, in which case results are available through getProducedBinaries().
114 std::vector<std::string> &getProducedBinaryFiles() {
115 return ProducedBinaryFiles;
119 * \defgroup Options setters
124 * \defgroup Cache controlling options
126 * These entry points control the ThinLTO cache. The cache is intended to
127 * support incremental build, and thus needs to be persistent accross build.
128 * The client enabled the cache by supplying a path to an existing directory.
129 * The code generator will use this to store objects files that may be reused
130 * during a subsequent build.
131 * To avoid filling the disk space, a few knobs are provided:
132 * - The pruning interval limit the frequency at which the garbage collector
133 * will try to scan the cache directory to prune it from expired entries.
134 * Setting to -1 disable the pruning (default).
135 * - The pruning expiration time indicates to the garbage collector how old
136 * an entry needs to be to be removed.
137 * - Finally, the garbage collector can be instructed to prune the cache till
138 * the occupied space goes below a threshold.
142 struct CachingOptions {
143 std::string Path; // Path to the cache, empty to disable.
144 CachePruningPolicy Policy;
147 /// Provide a path to a directory where to store the cached files for
148 /// incremental build.
149 void setCacheDir(std::string Path) { CacheOptions.Path = std::move(Path); }
151 /// Cache policy: interval (seconds) between two prunes of the cache. A
152 /// negative value sets the maximum possible pruning interval. A value
153 /// of 0 will be ignored.
154 void setCachePruningInterval(int Interval) {
155 static_assert(std::is_same<decltype(CacheOptions.Policy.Interval),
156 std::chrono::seconds>::value,
157 "ensure same types to avoid risk of overflow");
159 CacheOptions.Policy.Interval = Interval > 0 ? std::chrono::seconds(Interval)
160 : std::chrono::seconds::max();
163 /// Cache policy: expiration (in seconds) for an entry.
164 /// A value of 0 will be ignored.
165 void setCacheEntryExpiration(unsigned Expiration) {
167 CacheOptions.Policy.Expiration = std::chrono::seconds(Expiration);
171 * Sets the maximum cache size that can be persistent across build, in terms
172 * of percentage of the available space on the the disk. Set to 100 to
173 * indicate no limit, 50 to indicate that the cache size will not be left over
174 * half the available space. A value over 100 will be reduced to 100, and a
175 * value of 0 will be ignored.
178 * The formula looks like:
179 * AvailableSpace = FreeSpace + ExistingCacheSize
180 * NewCacheSize = AvailableSpace * P/100
183 void setMaxCacheSizeRelativeToAvailableSpace(unsigned Percentage) {
185 CacheOptions.Policy.MaxSizePercentageOfAvailableSpace = Percentage;
190 /// Set the path to a directory where to save temporaries at various stages of
192 void setSaveTempsDir(std::string Path) { SaveTempsDir = std::move(Path); }
194 /// Set the path to a directory where to save generated object files. This
195 /// path can be used by a linker to request on-disk files instead of in-memory
196 /// buffers. When set, results are available through getProducedBinaryFiles()
197 /// instead of getProducedBinaries().
198 void setGeneratedObjectsDirectory(std::string Path) {
199 SavedObjectsDirectoryPath = std::move(Path);
202 /// CPU to use to initialize the TargetMachine
203 void setCpu(std::string Cpu) { TMBuilder.MCpu = std::move(Cpu); }
205 /// Subtarget attributes
206 void setAttr(std::string MAttr) { TMBuilder.MAttr = std::move(MAttr); }
208 /// TargetMachine options
209 void setTargetOptions(TargetOptions Options) {
210 TMBuilder.Options = std::move(Options);
213 /// Enable the Freestanding mode: indicate that the optimizer should not
214 /// assume builtins are present on the target.
215 void setFreestanding(bool Enabled) { Freestanding = Enabled; }
218 void setCodePICModel(Optional<Reloc::Model> Model) {
219 TMBuilder.RelocModel = Model;
222 /// CodeGen optimization level
223 void setCodeGenOptLevel(CodeGenOpt::Level CGOptLevel) {
224 TMBuilder.CGOptLevel = CGOptLevel;
227 /// IR optimization level: from 0 to 3.
228 void setOptLevel(unsigned NewOptLevel) {
229 OptLevel = (NewOptLevel > 3) ? 3 : NewOptLevel;
232 /// Disable CodeGen, only run the stages till codegen and stop. The output
234 void disableCodeGen(bool Disable) { DisableCodeGen = Disable; }
236 /// Perform CodeGen only: disable all other stages.
237 void setCodeGenOnly(bool CGOnly) { CodeGenOnly = CGOnly; }
242 * \defgroup Set of APIs to run individual stages in isolation.
247 * Produce the combined summary index from all the bitcode files:
250 std::unique_ptr<ModuleSummaryIndex> linkCombinedIndex();
253 * Perform promotion and renaming of exported internal functions,
254 * and additionally resolve weak and linkonce symbols.
255 * Index is updated to reflect linkage changes from weak resolution.
257 void promote(Module &Module, ModuleSummaryIndex &Index);
260 * Compute and emit the imported files for module at \p ModulePath.
262 static void emitImports(StringRef ModulePath, StringRef OutputName,
263 ModuleSummaryIndex &Index);
266 * Perform cross-module importing for the module identified by
269 void crossModuleImport(Module &Module, ModuleSummaryIndex &Index);
272 * Compute the list of summaries needed for importing into module.
274 static void gatherImportedSummariesForModule(
275 StringRef ModulePath, ModuleSummaryIndex &Index,
276 std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex);
279 * Perform internalization. Index is updated to reflect linkage changes.
281 void internalize(Module &Module, ModuleSummaryIndex &Index);
284 * Perform post-importing ThinLTO optimizations.
286 void optimize(Module &Module);
289 * Perform ThinLTO CodeGen.
291 std::unique_ptr<MemoryBuffer> codegen(Module &Module);
296 /// Helper factory to build a TargetMachine
297 TargetMachineBuilder TMBuilder;
299 /// Vector holding the in-memory buffer containing the produced binaries, when
300 /// SavedObjectsDirectoryPath isn't set.
301 std::vector<std::unique_ptr<MemoryBuffer>> ProducedBinaries;
303 /// Path to generated files in the supplied SavedObjectsDirectoryPath if any.
304 std::vector<std::string> ProducedBinaryFiles;
306 /// Vector holding the input buffers containing the bitcode modules to
308 std::vector<ThinLTOBuffer> Modules;
310 /// Set of symbols that need to be preserved outside of the set of bitcode
312 StringSet<> PreservedSymbols;
314 /// Set of symbols that are cross-referenced between bitcode files.
315 StringSet<> CrossReferencedSymbols;
317 /// Control the caching behavior.
318 CachingOptions CacheOptions;
320 /// Path to a directory to save the temporary bitcode files.
321 std::string SaveTempsDir;
323 /// Path to a directory to save the generated object files.
324 std::string SavedObjectsDirectoryPath;
326 /// Flag to enable/disable CodeGen. When set to true, the process stops after
327 /// optimizations and a bitcode is produced.
328 bool DisableCodeGen = false;
330 /// Flag to indicate that only the CodeGen will be performed, no cross-module
331 /// importing or optimization.
332 bool CodeGenOnly = false;
334 /// Flag to indicate that the optimizer should not assume builtins are present
336 bool Freestanding = false;
338 /// IR Optimization Level [0-3].
339 unsigned OptLevel = 3;