1 //===-ThinLTOCodeGenerator.h - LLVM Link Time Optimizer -------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file declares the ThinLTOCodeGenerator class, similar to the
10 // LTOCodeGenerator but for the ThinLTO scheme. It provides an interface for
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LTO_THINLTOCODEGENERATOR_H
16 #define LLVM_LTO_THINLTOCODEGENERATOR_H
18 #include "llvm-c/lto.h"
19 #include "llvm/ADT/StringSet.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/IR/ModuleSummaryIndex.h"
22 #include "llvm/LTO/LTO.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 /// Helper to gather options relevant to the target machine creation
36 struct TargetMachineBuilder
{
40 TargetOptions Options
;
41 Optional
<Reloc::Model
> RelocModel
;
42 CodeGenOpt::Level CGOptLevel
= CodeGenOpt::Aggressive
;
44 std::unique_ptr
<TargetMachine
> create() const;
47 /// This class define an interface similar to the LTOCodeGenerator, but adapted
48 /// for ThinLTO processing.
49 /// The ThinLTOCodeGenerator is not intended to be reuse for multiple
50 /// compilation: the model is that the client adds modules to the generator and
51 /// ask to perform the ThinLTO optimizations / codegen, and finally destroys the
53 class ThinLTOCodeGenerator
{
55 /// Add given module to the code generator.
56 void addModule(StringRef Identifier
, StringRef Data
);
59 * Adds to a list of all global symbols that must exist in the final generated
60 * code. If a symbol is not listed there, it will be optimized away if it is
61 * inlined into every usage.
63 void preserveSymbol(StringRef Name
);
66 * Adds to a list of all global symbols that are cross-referenced between
67 * ThinLTO files. If the ThinLTO CodeGenerator can ensure that every
68 * references from a ThinLTO module to this symbol is optimized away, then
69 * the symbol can be discarded.
71 void crossReferenceSymbol(StringRef Name
);
74 * Process all the modules that were added to the code generator in parallel.
76 * Client can access the resulting object files using getProducedBinaries(),
77 * unless setGeneratedObjectsDirectory() has been called, in which case
78 * results are available through getProducedBinaryFiles().
83 * Return the "in memory" binaries produced by the code generator. This is
84 * filled after run() unless setGeneratedObjectsDirectory() has been
85 * called, in which case results are available through
86 * getProducedBinaryFiles().
88 std::vector
<std::unique_ptr
<MemoryBuffer
>> &getProducedBinaries() {
89 return ProducedBinaries
;
93 * Return the "on-disk" binaries produced by the code generator. This is
94 * filled after run() when setGeneratedObjectsDirectory() has been
95 * called, in which case results are available through getProducedBinaries().
97 std::vector
<std::string
> &getProducedBinaryFiles() {
98 return ProducedBinaryFiles
;
102 * \defgroup Options setters
107 * \defgroup Cache controlling options
109 * These entry points control the ThinLTO cache. The cache is intended to
110 * support incremental build, and thus needs to be persistent accross build.
111 * The client enabled the cache by supplying a path to an existing directory.
112 * The code generator will use this to store objects files that may be reused
113 * during a subsequent build.
114 * To avoid filling the disk space, a few knobs are provided:
115 * - The pruning interval limit the frequency at which the garbage collector
116 * will try to scan the cache directory to prune it from expired entries.
117 * Setting to -1 disable the pruning (default). Setting to 0 will force
119 * - The pruning expiration time indicates to the garbage collector how old
120 * an entry needs to be to be removed.
121 * - Finally, the garbage collector can be instructed to prune the cache till
122 * the occupied space goes below a threshold.
126 struct CachingOptions
{
127 std::string Path
; // Path to the cache, empty to disable.
128 CachePruningPolicy Policy
;
131 /// Provide a path to a directory where to store the cached files for
132 /// incremental build.
133 void setCacheDir(std::string Path
) { CacheOptions
.Path
= std::move(Path
); }
135 /// Cache policy: interval (seconds) between two prunes of the cache. Set to a
136 /// negative value to disable pruning. A value of 0 will force pruning to
138 void setCachePruningInterval(int Interval
) {
140 CacheOptions
.Policy
.Interval
.reset();
142 CacheOptions
.Policy
.Interval
= std::chrono::seconds(Interval
);
145 /// Cache policy: expiration (in seconds) for an entry.
146 /// A value of 0 will be ignored.
147 void setCacheEntryExpiration(unsigned Expiration
) {
149 CacheOptions
.Policy
.Expiration
= std::chrono::seconds(Expiration
);
153 * Sets the maximum cache size that can be persistent across build, in terms
154 * of percentage of the available space on the disk. Set to 100 to indicate
155 * no limit, 50 to indicate that the cache size will not be left over
156 * half the available space. A value over 100 will be reduced to 100, and a
157 * value of 0 will be ignored.
160 * The formula looks like:
161 * AvailableSpace = FreeSpace + ExistingCacheSize
162 * NewCacheSize = AvailableSpace * P/100
165 void setMaxCacheSizeRelativeToAvailableSpace(unsigned Percentage
) {
167 CacheOptions
.Policy
.MaxSizePercentageOfAvailableSpace
= Percentage
;
170 /// Cache policy: the maximum size for the cache directory in bytes. A value
171 /// over the amount of available space on the disk will be reduced to the
172 /// amount of available space. A value of 0 will be ignored.
173 void setCacheMaxSizeBytes(uint64_t MaxSizeBytes
) {
175 CacheOptions
.Policy
.MaxSizeBytes
= MaxSizeBytes
;
178 /// Cache policy: the maximum number of files in the cache directory. A value
179 /// of 0 will be ignored.
180 void setCacheMaxSizeFiles(unsigned MaxSizeFiles
) {
182 CacheOptions
.Policy
.MaxSizeFiles
= MaxSizeFiles
;
187 /// Set the path to a directory where to save temporaries at various stages of
189 void setSaveTempsDir(std::string Path
) { SaveTempsDir
= std::move(Path
); }
191 /// Set the path to a directory where to save generated object files. This
192 /// path can be used by a linker to request on-disk files instead of in-memory
193 /// buffers. When set, results are available through getProducedBinaryFiles()
194 /// instead of getProducedBinaries().
195 void setGeneratedObjectsDirectory(std::string Path
) {
196 SavedObjectsDirectoryPath
= std::move(Path
);
199 /// CPU to use to initialize the TargetMachine
200 void setCpu(std::string Cpu
) { TMBuilder
.MCpu
= std::move(Cpu
); }
202 /// Subtarget attributes
203 void setAttr(std::string MAttr
) { TMBuilder
.MAttr
= std::move(MAttr
); }
205 /// TargetMachine options
206 void setTargetOptions(TargetOptions Options
) {
207 TMBuilder
.Options
= std::move(Options
);
210 /// Enable the Freestanding mode: indicate that the optimizer should not
211 /// assume builtins are present on the target.
212 void setFreestanding(bool Enabled
) { Freestanding
= Enabled
; }
215 void setCodePICModel(Optional
<Reloc::Model
> Model
) {
216 TMBuilder
.RelocModel
= Model
;
219 /// CodeGen optimization level
220 void setCodeGenOptLevel(CodeGenOpt::Level CGOptLevel
) {
221 TMBuilder
.CGOptLevel
= CGOptLevel
;
224 /// IR optimization level: from 0 to 3.
225 void setOptLevel(unsigned NewOptLevel
) {
226 OptLevel
= (NewOptLevel
> 3) ? 3 : NewOptLevel
;
229 /// Disable CodeGen, only run the stages till codegen and stop. The output
231 void disableCodeGen(bool Disable
) { DisableCodeGen
= Disable
; }
233 /// Perform CodeGen only: disable all other stages.
234 void setCodeGenOnly(bool CGOnly
) { CodeGenOnly
= CGOnly
; }
239 * \defgroup Set of APIs to run individual stages in isolation.
244 * Produce the combined summary index from all the bitcode files:
247 std::unique_ptr
<ModuleSummaryIndex
> linkCombinedIndex();
250 * Perform promotion and renaming of exported internal functions,
251 * and additionally resolve weak and linkonce symbols.
252 * Index is updated to reflect linkage changes from weak resolution.
254 void promote(Module
&Module
, ModuleSummaryIndex
&Index
,
255 const lto::InputFile
&File
);
258 * Compute and emit the imported files for module at \p ModulePath.
260 void emitImports(Module
&Module
, StringRef OutputName
,
261 ModuleSummaryIndex
&Index
,
262 const lto::InputFile
&File
);
265 * Perform cross-module importing for the module identified by
268 void crossModuleImport(Module
&Module
, ModuleSummaryIndex
&Index
,
269 const lto::InputFile
&File
);
272 * Compute the list of summaries needed for importing into module.
274 void gatherImportedSummariesForModule(
275 Module
&Module
, ModuleSummaryIndex
&Index
,
276 std::map
<std::string
, GVSummaryMapTy
> &ModuleToSummariesForIndex
,
277 const lto::InputFile
&File
);
280 * Perform internalization. Index is updated to reflect linkage changes.
282 void internalize(Module
&Module
, ModuleSummaryIndex
&Index
,
283 const lto::InputFile
&File
);
286 * Perform post-importing ThinLTO optimizations.
288 void optimize(Module
&Module
);
291 * Write temporary object file to SavedObjectDirectoryPath, write symlink
292 * to Cache directory if needed. Returns the path to the generated file in
293 * SavedObjectsDirectoryPath.
295 std::string
writeGeneratedObject(int count
, StringRef CacheEntryPath
,
296 const MemoryBuffer
&OutputBuffer
);
300 /// Helper factory to build a TargetMachine
301 TargetMachineBuilder TMBuilder
;
303 /// Vector holding the in-memory buffer containing the produced binaries, when
304 /// SavedObjectsDirectoryPath isn't set.
305 std::vector
<std::unique_ptr
<MemoryBuffer
>> ProducedBinaries
;
307 /// Path to generated files in the supplied SavedObjectsDirectoryPath if any.
308 std::vector
<std::string
> ProducedBinaryFiles
;
310 /// Vector holding the input buffers containing the bitcode modules to
312 std::vector
<std::unique_ptr
<lto::InputFile
>> Modules
;
314 /// Set of symbols that need to be preserved outside of the set of bitcode
316 StringSet
<> PreservedSymbols
;
318 /// Set of symbols that are cross-referenced between bitcode files.
319 StringSet
<> CrossReferencedSymbols
;
321 /// Control the caching behavior.
322 CachingOptions CacheOptions
;
324 /// Path to a directory to save the temporary bitcode files.
325 std::string SaveTempsDir
;
327 /// Path to a directory to save the generated object files.
328 std::string SavedObjectsDirectoryPath
;
330 /// Flag to enable/disable CodeGen. When set to true, the process stops after
331 /// optimizations and a bitcode is produced.
332 bool DisableCodeGen
= false;
334 /// Flag to indicate that only the CodeGen will be performed, no cross-module
335 /// importing or optimization.
336 bool CodeGenOnly
= false;
338 /// Flag to indicate that the optimizer should not assume builtins are present
340 bool Freestanding
= false;
342 /// IR Optimization Level [0-3].
343 unsigned OptLevel
= 3;