llvm.org GIT mirror llvm / ef2acb5
Move legacy LTO interface headers to legacy/ directory. Differential Revision: https://reviews.llvm.org/D22173 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@275476 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 3 years ago
14 changed file(s) with 768 addition(s) and 768 deletion(s). Raw diff Collapse all Expand all
+0
-236
include/llvm/LTO/LTOCodeGenerator.h less more
None //===-LTOCodeGenerator.h - LLVM Link Time Optimizer -----------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the LTOCodeGenerator class.
10 //
11 // LTO compilation consists of three phases: Pre-IPO, IPO and Post-IPO.
12 //
13 // The Pre-IPO phase compiles source code into bitcode file. The resulting
14 // bitcode files, along with object files and libraries, will be fed to the
15 // linker to through the IPO and Post-IPO phases. By using obj-file extension,
16 // the resulting bitcode file disguises itself as an object file, and therefore
17 // obviates the need of writing a special set of the make-rules only for LTO
18 // compilation.
19 //
20 // The IPO phase perform inter-procedural analyses and optimizations, and
21 // the Post-IPO consists two sub-phases: intra-procedural scalar optimizations
22 // (SOPT), and intra-procedural target-dependent code generator (CG).
23 //
24 // As of this writing, we don't separate IPO and the Post-IPO SOPT. They
25 // are intermingled together, and are driven by a single pass manager (see
26 // PassManagerBuilder::populateLTOPassManager()).
27 //
28 // The "LTOCodeGenerator" is the driver for the IPO and Post-IPO stages.
29 // The "CodeGenerator" here is bit confusing. Don't confuse the "CodeGenerator"
30 // with the machine specific code generator.
31 //
32 //===----------------------------------------------------------------------===//
33
34 #ifndef LLVM_LTO_LTOCODEGENERATOR_H
35 #define LLVM_LTO_LTOCODEGENERATOR_H
36
37 #include "llvm-c/lto.h"
38 #include "llvm/ADT/SmallPtrSet.h"
39 #include "llvm/ADT/StringMap.h"
40 #include "llvm/ADT/StringSet.h"
41 #include "llvm/IR/GlobalValue.h"
42 #include "llvm/IR/Module.h"
43 #include "llvm/Target/TargetMachine.h"
44 #include "llvm/Target/TargetOptions.h"
45 #include
46 #include
47
48 namespace llvm {
49 template class ArrayRef;
50 class LLVMContext;
51 class DiagnosticInfo;
52 class Linker;
53 class Mangler;
54 class MemoryBuffer;
55 class TargetLibraryInfo;
56 class TargetMachine;
57 class raw_ostream;
58 class raw_pwrite_stream;
59
60 //===----------------------------------------------------------------------===//
61 /// C++ class which implements the opaque lto_code_gen_t type.
62 ///
63 struct LTOCodeGenerator {
64 static const char *getVersionString();
65
66 LTOCodeGenerator(LLVMContext &Context);
67 ~LTOCodeGenerator();
68
69 /// Merge given module. Return true on success.
70 ///
71 /// Resets \a HasVerifiedInput.
72 bool addModule(struct LTOModule *);
73
74 /// Set the destination module.
75 ///
76 /// Resets \a HasVerifiedInput.
77 void setModule(std::unique_ptr M);
78
79 void setTargetOptions(const TargetOptions &Options);
80 void setDebugInfo(lto_debug_model);
81 void setCodePICModel(Optional Model) { RelocModel = Model; }
82
83 /// Set the file type to be emitted (assembly or object code).
84 /// The default is TargetMachine::CGFT_ObjectFile.
85 void setFileType(TargetMachine::CodeGenFileType FT) { FileType = FT; }
86
87 void setCpu(const char *MCpu) { this->MCpu = MCpu; }
88 void setAttr(const char *MAttr) { this->MAttr = MAttr; }
89 void setOptLevel(unsigned OptLevel);
90
91 void setShouldInternalize(bool Value) { ShouldInternalize = Value; }
92 void setShouldEmbedUselists(bool Value) { ShouldEmbedUselists = Value; }
93
94 /// Restore linkage of globals
95 ///
96 /// When set, the linkage of globals will be restored prior to code
97 /// generation. That is, a global symbol that had external linkage prior to
98 /// LTO will be emitted with external linkage again; and a local will remain
99 /// local. Note that this option only affects the end result - globals may
100 /// still be internalized in the process of LTO and may be modified and/or
101 /// deleted where legal.
102 ///
103 /// The default behavior will internalize globals (unless on the preserve
104 /// list) and, if parallel code generation is enabled, will externalize
105 /// all locals.
106 void setShouldRestoreGlobalsLinkage(bool Value) {
107 ShouldRestoreGlobalsLinkage = Value;
108 }
109
110 void addMustPreserveSymbol(StringRef Sym) { MustPreserveSymbols[Sym] = 1; }
111
112 /// Pass options to the driver and optimization passes.
113 ///
114 /// These options are not necessarily for debugging purpose (the function
115 /// name is misleading). This function should be called before
116 /// LTOCodeGenerator::compilexxx(), and
117 /// LTOCodeGenerator::writeMergedModules().
118 void setCodeGenDebugOptions(const char *Opts);
119
120 /// Parse the options set in setCodeGenDebugOptions.
121 ///
122 /// Like \a setCodeGenDebugOptions(), this must be called before
123 /// LTOCodeGenerator::compilexxx() and
124 /// LTOCodeGenerator::writeMergedModules().
125 void parseCodeGenDebugOptions();
126
127 /// Write the merged module to the file specified by the given path. Return
128 /// true on success.
129 ///
130 /// Calls \a verifyMergedModuleOnce().
131 bool writeMergedModules(const char *Path);
132
133 /// Compile the merged module into a *single* output file; the path to output
134 /// file is returned to the caller via argument "name". Return true on
135 /// success.
136 ///
137 /// \note It is up to the linker to remove the intermediate output file. Do
138 /// not try to remove the object file in LTOCodeGenerator's destructor as we
139 /// don't who (LTOCodeGenerator or the output file) will last longer.
140 bool compile_to_file(const char **Name, bool DisableVerify,
141 bool DisableInline, bool DisableGVNLoadPRE,
142 bool DisableVectorization);
143
144 /// As with compile_to_file(), this function compiles the merged module into
145 /// single output file. Instead of returning the output file path to the
146 /// caller (linker), it brings the output to a buffer, and returns the buffer
147 /// to the caller. This function should delete the intermediate file once
148 /// its content is brought to memory. Return NULL if the compilation was not
149 /// successful.
150 std::unique_ptr compile(bool DisableVerify, bool DisableInline,
151 bool DisableGVNLoadPRE,
152 bool DisableVectorization);
153
154 /// Optimizes the merged module. Returns true on success.
155 ///
156 /// Calls \a verifyMergedModuleOnce().
157 bool optimize(bool DisableVerify, bool DisableInline, bool DisableGVNLoadPRE,
158 bool DisableVectorization);
159
160 /// Compiles the merged optimized module into a single output file. It brings
161 /// the output to a buffer, and returns the buffer to the caller. Return NULL
162 /// if the compilation was not successful.
163 std::unique_ptr compileOptimized();
164
165 /// Compile the merged optimized module into out.size() output files each
166 /// representing a linkable partition of the module. If out contains more
167 /// than one element, code generation is done in parallel with out.size()
168 /// threads. Output files will be written to members of out. Returns true on
169 /// success.
170 ///
171 /// Calls \a verifyMergedModuleOnce().
172 bool compileOptimized(ArrayRef Out);
173
174 void setDiagnosticHandler(lto_diagnostic_handler_t, void *);
175
176 LLVMContext &getContext() { return Context; }
177
178 void resetMergedModule() { MergedModule.reset(); }
179
180 private:
181 void initializeLTOPasses();
182
183 /// Verify the merged module on first call.
184 ///
185 /// Sets \a HasVerifiedInput on first call and doesn't run again on the same
186 /// input.
187 void verifyMergedModuleOnce();
188
189 bool compileOptimizedToFile(const char **Name);
190 void restoreLinkageForExternals();
191 void applyScopeRestrictions();
192 void preserveDiscardableGVs(
193 Module &TheModule,
194 llvm::function_ref mustPreserveGV);
195
196 bool determineTarget();
197 std::unique_ptr createTargetMachine();
198
199 static void DiagnosticHandler(const DiagnosticInfo &DI, void *Context);
200
201 void DiagnosticHandler2(const DiagnosticInfo &DI);
202
203 void emitError(const std::string &ErrMsg);
204 void emitWarning(const std::string &ErrMsg);
205
206 LLVMContext &Context;
207 std::unique_ptr MergedModule;
208 std::unique_ptr TheLinker;
209 std::unique_ptr TargetMach;
210 bool EmitDwarfDebugInfo = false;
211 bool ScopeRestrictionsDone = false;
212 bool HasVerifiedInput = false;
213 Optional RelocModel;
214 StringSet<> MustPreserveSymbols;
215 StringSet<> AsmUndefinedRefs;
216 StringMap ExternalSymbols;
217 std::vector CodegenOptions;
218 std::string FeatureStr;
219 std::string MCpu;
220 std::string MAttr;
221 std::string NativeObjectPath;
222 TargetOptions Options;
223 CodeGenOpt::Level CGOptLevel = CodeGenOpt::Default;
224 const Target *MArch = nullptr;
225 std::string TripleStr;
226 unsigned OptLevel = 2;
227 lto_diagnostic_handler_t DiagHandler = nullptr;
228 void *DiagContext = nullptr;
229 bool ShouldInternalize = true;
230 bool ShouldEmbedUselists = false;
231 bool ShouldRestoreGlobalsLinkage = false;
232 TargetMachine::CodeGenFileType FileType = TargetMachine::CGFT_ObjectFile;
233 };
234 }
235 #endif
+0
-212
include/llvm/LTO/LTOModule.h less more
None //===-LTOModule.h - LLVM Link Time Optimizer ------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the LTOModule class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_LTO_LTOMODULE_H
14 #define LLVM_LTO_LTOMODULE_H
15
16 #include "llvm-c/lto.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringSet.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Object/IRObjectFile.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include
23 #include
24
25 // Forward references to llvm classes.
26 namespace llvm {
27 class Function;
28 class GlobalValue;
29 class MemoryBuffer;
30 class TargetOptions;
31 class Value;
32
33 //===----------------------------------------------------------------------===//
34 /// C++ class which implements the opaque lto_module_t type.
35 ///
36 struct LTOModule {
37 private:
38 struct NameAndAttributes {
39 const char *name;
40 uint32_t attributes;
41 bool isFunction;
42 const GlobalValue *symbol;
43 };
44
45 std::unique_ptr OwnedContext;
46
47 std::string LinkerOpts;
48
49 std::unique_ptr IRFile;
50 std::unique_ptr _target;
51 std::vector _symbols;
52
53 // _defines and _undefines only needed to disambiguate tentative definitions
54 StringSet<> _defines;
55 StringMap _undefines;
56 std::vector _asm_undefines;
57
58 LTOModule(std::unique_ptr Obj, TargetMachine *TM);
59
60 public:
61 ~LTOModule();
62
63 /// Returns 'true' if the file or memory contents is LLVM bitcode.
64 static bool isBitcodeFile(const void *mem, size_t length);
65 static bool isBitcodeFile(const char *path);
66
67 /// Returns 'true' if the Module is produced for ThinLTO.
68 bool isThinLTO();
69
70 /// Returns 'true' if the memory buffer is LLVM bitcode for the specified
71 /// triple.
72 static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
73 StringRef triplePrefix);
74
75 /// Returns a string representing the producer identification stored in the
76 /// bitcode, or "" if the bitcode does not contains any.
77 ///
78 static std::string getProducerString(MemoryBuffer *Buffer);
79
80 /// Create a MemoryBuffer from a memory range with an optional name.
81 static std::unique_ptr
82 makeBuffer(const void *mem, size_t length, StringRef name = "");
83
84 /// Create an LTOModule. N.B. These methods take ownership of the buffer. The
85 /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters,
86 /// and the AsmParsers by calling:
87 ///
88 /// InitializeAllTargets();
89 /// InitializeAllTargetMCs();
90 /// InitializeAllAsmPrinters();
91 /// InitializeAllAsmParsers();
92 static ErrorOr>
93 createFromFile(LLVMContext &Context, const char *path,
94 const TargetOptions &options);
95 static ErrorOr>
96 createFromOpenFile(LLVMContext &Context, int fd, const char *path,
97 size_t size, const TargetOptions &options);
98 static ErrorOr>
99 createFromOpenFileSlice(LLVMContext &Context, int fd, const char *path,
100 size_t map_size, off_t offset,
101 const TargetOptions &options);
102 static ErrorOr>
103 createFromBuffer(LLVMContext &Context, const void *mem, size_t length,
104 const TargetOptions &options, StringRef path = "");
105 static ErrorOr>
106 createInLocalContext(std::unique_ptr Context, const void *mem,
107 size_t length, const TargetOptions &options,
108 StringRef path);
109
110 const Module &getModule() const {
111 return const_cast(this)->getModule();
112 }
113 Module &getModule() {
114 return IRFile->getModule();
115 }
116
117 std::unique_ptr takeModule() { return IRFile->takeModule(); }
118
119 /// Return the Module's target triple.
120 const std::string &getTargetTriple() {
121 return getModule().getTargetTriple();
122 }
123
124 /// Set the Module's target triple.
125 void setTargetTriple(StringRef Triple) {
126 getModule().setTargetTriple(Triple);
127 }
128
129 /// Get the number of symbols
130 uint32_t getSymbolCount() {
131 return _symbols.size();
132 }
133
134 /// Get the attributes for a symbol at the specified index.
135 lto_symbol_attributes getSymbolAttributes(uint32_t index) {
136 if (index < _symbols.size())
137 return lto_symbol_attributes(_symbols[index].attributes);
138 return lto_symbol_attributes(0);
139 }
140
141 /// Get the name of the symbol at the specified index.
142 const char *getSymbolName(uint32_t index) {
143 if (index < _symbols.size())
144 return _symbols[index].name;
145 return nullptr;
146 }
147
148 const GlobalValue *getSymbolGV(uint32_t index) {
149 if (index < _symbols.size())
150 return _symbols[index].symbol;
151 return nullptr;
152 }
153
154 const char *getLinkerOpts() {
155 return LinkerOpts.c_str();
156 }
157
158 const std::vector &getAsmUndefinedRefs() {
159 return _asm_undefines;
160 }
161
162 private:
163 /// Parse metadata from the module
164 // FIXME: it only parses "Linker Options" metadata at the moment
165 void parseMetadata();
166
167 /// Parse the symbols from the module and model-level ASM and add them to
168 /// either the defined or undefined lists.
169 void parseSymbols();
170
171 /// Add a symbol which isn't defined just yet to a list to be resolved later.
172 void addPotentialUndefinedSymbol(const object::BasicSymbolRef &Sym,
173 bool isFunc);
174
175 /// Add a defined symbol to the list.
176 void addDefinedSymbol(const char *Name, const GlobalValue *def,
177 bool isFunction);
178
179 /// Add a data symbol as defined to the list.
180 void addDefinedDataSymbol(const object::BasicSymbolRef &Sym);
181 void addDefinedDataSymbol(const char*Name, const GlobalValue *v);
182
183 /// Add a function symbol as defined to the list.
184 void addDefinedFunctionSymbol(const object::BasicSymbolRef &Sym);
185 void addDefinedFunctionSymbol(const char *Name, const Function *F);
186
187 /// Add a global symbol from module-level ASM to the defined list.
188 void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
189
190 /// Add a global symbol from module-level ASM to the undefined list.
191 void addAsmGlobalSymbolUndef(const char *);
192
193 /// Parse i386/ppc ObjC class data structure.
194 void addObjCClass(const GlobalVariable *clgv);
195
196 /// Parse i386/ppc ObjC category data structure.
197 void addObjCCategory(const GlobalVariable *clgv);
198
199 /// Parse i386/ppc ObjC class list data structure.
200 void addObjCClassRef(const GlobalVariable *clgv);
201
202 /// Get string that the data pointer points to.
203 bool objcClassNameFromExpression(const Constant *c, std::string &name);
204
205 /// Create an LTOModule (private version).
206 static ErrorOr>
207 makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
208 LLVMContext &Context, bool ShouldBeLazy);
209 };
210 }
211 #endif
+0
-276
include/llvm/LTO/ThinLTOCodeGenerator.h less more
None //===-ThinLTOCodeGenerator.h - LLVM Link Time Optimizer -------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the ThinLTOCodeGenerator class, similar to the
10 // LTOCodeGenerator but for the ThinLTO scheme. It provides an interface for
11 // linker plugin.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_LTO_THINLTOCODEGENERATOR_H
16 #define LLVM_LTO_THINLTOCODEGENERATOR_H
17
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/Support/CodeGen.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Target/TargetOptions.h"
25
26 #include
27
28 namespace llvm {
29 class StringRef;
30 class LLVMContext;
31 class TargetMachine;
32
33 /// Helper to gather options relevant to the target machine creation
34 struct TargetMachineBuilder {
35 Triple TheTriple;
36 std::string MCpu;
37 std::string MAttr;
38 TargetOptions Options;
39 Optional RelocModel;
40 CodeGenOpt::Level CGOptLevel = CodeGenOpt::Default;
41
42 std::unique_ptr create() const;
43 };
44
45 /// This class define an interface similar to the LTOCodeGenerator, but adapted
46 /// for ThinLTO processing.
47 /// The ThinLTOCodeGenerator is not intended to be reuse for multiple
48 /// compilation: the model is that the client adds modules to the generator and
49 /// ask to perform the ThinLTO optimizations / codegen, and finally destroys the
50 /// codegenerator.
51 class ThinLTOCodeGenerator {
52 public:
53 /// Add given module to the code generator.
54 void addModule(StringRef Identifier, StringRef Data);
55
56 /**
57 * Adds to a list of all global symbols that must exist in the final generated
58 * code. If a symbol is not listed there, it will be optimized away if it is
59 * inlined into every usage.
60 */
61 void preserveSymbol(StringRef Name);
62
63 /**
64 * Adds to a list of all global symbols that are cross-referenced between
65 * ThinLTO files. If the ThinLTO CodeGenerator can ensure that every
66 * references from a ThinLTO module to this symbol is optimized away, then
67 * the symbol can be discarded.
68 */
69 void crossReferenceSymbol(StringRef Name);
70
71 /**
72 * Process all the modules that were added to the code generator in parallel.
73 *
74 * Client can access the resulting object files using getProducedBinaries()
75 */
76 void run();
77
78 /**
79 * Return the "in memory" binaries produced by the code generator.
80 */
81 std::vector> &getProducedBinaries() {
82 return ProducedBinaries;
83 }
84
85 /**
86 * \defgroup Options setters
87 * @{
88 */
89
90 /**
91 * \defgroup Cache controlling options
92 *
93 * These entry points control the ThinLTO cache. The cache is intended to
94 * support incremental build, and thus needs to be persistent accross build.
95 * The client enabled the cache by supplying a path to an existing directory.
96 * The code generator will use this to store objects files that may be reused
97 * during a subsequent build.
98 * To avoid filling the disk space, a few knobs are provided:
99 * - The pruning interval limit the frequency at which the garbage collector
100 * will try to scan the cache directory to prune it from expired entries.
101 * Setting to -1 disable the pruning (default).
102 * - The pruning expiration time indicates to the garbage collector how old
103 * an entry needs to be to be removed.
104 * - Finally, the garbage collector can be instructed to prune the cache till
105 * the occupied space goes below a threshold.
106 * @{
107 */
108
109 struct CachingOptions {
110 std::string Path; // Path to the cache, empty to disable.
111 int PruningInterval = 1200; // seconds, -1 to disable pruning.
112 unsigned int Expiration = 7 * 24 * 3600; // seconds (1w default).
113 unsigned MaxPercentageOfAvailableSpace = 75; // percentage.
114 };
115
116 /// Provide a path to a directory where to store the cached files for
117 /// incremental build.
118 void setCacheDir(std::string Path) { CacheOptions.Path = std::move(Path); }
119
120 /// Cache policy: interval (seconds) between two prune of the cache. Set to a
121 /// negative value (default) to disable pruning. A value of 0 will be ignored.
122 void setCachePruningInterval(int Interval) {
123 if (Interval)
124 CacheOptions.PruningInterval = Interval;
125 }
126
127 /// Cache policy: expiration (in seconds) for an entry.
128 /// A value of 0 will be ignored.
129 void setCacheEntryExpiration(unsigned Expiration) {
130 if (Expiration)
131 CacheOptions.Expiration = Expiration;
132 }
133
134 /**
135 * Sets the maximum cache size that can be persistent across build, in terms
136 * of percentage of the available space on the the disk. Set to 100 to
137 * indicate no limit, 50 to indicate that the cache size will not be left over
138 * half the available space. A value over 100 will be reduced to 100, and a
139 * value of 0 will be ignored.
140 *
141 *
142 * The formula looks like:
143 * AvailableSpace = FreeSpace + ExistingCacheSize
144 * NewCacheSize = AvailableSpace * P/100
145 *
146 */
147 void setMaxCacheSizeRelativeToAvailableSpace(unsigned Percentage) {
148 if (Percentage)
149 CacheOptions.MaxPercentageOfAvailableSpace = Percentage;
150 }
151
152 /**@}*/
153
154 /// Set the path to a directory where to save temporaries at various stages of
155 /// the processing.
156 void setSaveTempsDir(std::string Path) { SaveTempsDir = std::move(Path); }
157
158 /// CPU to use to initialize the TargetMachine
159 void setCpu(std::string Cpu) { TMBuilder.MCpu = std::move(Cpu); }
160
161 /// Subtarget attributes
162 void setAttr(std::string MAttr) { TMBuilder.MAttr = std::move(MAttr); }
163
164 /// TargetMachine options
165 void setTargetOptions(TargetOptions Options) {
166 TMBuilder.Options = std::move(Options);
167 }
168
169 /// CodeModel
170 void setCodePICModel(Optional Model) {
171 TMBuilder.RelocModel = Model;
172 }
173
174 /// CodeGen optimization level
175 void setCodeGenOptLevel(CodeGenOpt::Level CGOptLevel) {
176 TMBuilder.CGOptLevel = CGOptLevel;
177 }
178
179 /// Disable CodeGen, only run the stages till codegen and stop. The output
180 /// will be bitcode.
181 void disableCodeGen(bool Disable) { DisableCodeGen = Disable; }
182
183 /// Perform CodeGen only: disable all other stages.
184 void setCodeGenOnly(bool CGOnly) { CodeGenOnly = CGOnly; }
185
186 /**@}*/
187
188 /**
189 * \defgroup Set of APIs to run individual stages in isolation.
190 * @{
191 */
192
193 /**
194 * Produce the combined summary index from all the bitcode files:
195 * "thin-link".
196 */
197 std::unique_ptr linkCombinedIndex();
198
199 /**
200 * Perform promotion and renaming of exported internal functions,
201 * and additionally resolve weak and linkonce symbols.
202 * Index is updated to reflect linkage changes from weak resolution.
203 */
204 void promote(Module &Module, ModuleSummaryIndex &Index);
205
206 /**
207 * Compute and emit the imported files for module at \p ModulePath.
208 */
209 static void emitImports(StringRef ModulePath, StringRef OutputName,
210 ModuleSummaryIndex &Index);
211
212 /**
213 * Perform cross-module importing for the module identified by
214 * ModuleIdentifier.
215 */
216 void crossModuleImport(Module &Module, ModuleSummaryIndex &Index);
217
218 /**
219 * Compute the list of summaries needed for importing into module.
220 */
221 static void gatherImportedSummariesForModule(
222 StringRef ModulePath, ModuleSummaryIndex &Index,
223 std::map &ModuleToSummariesForIndex);
224
225 /**
226 * Perform internalization. Index is updated to reflect linkage changes.
227 */
228 void internalize(Module &Module, ModuleSummaryIndex &Index);
229
230 /**
231 * Perform post-importing ThinLTO optimizations.
232 */
233 void optimize(Module &Module);
234
235 /**
236 * Perform ThinLTO CodeGen.
237 */
238 std::unique_ptr codegen(Module &Module);
239
240 /**@}*/
241
242 private:
243 /// Helper factory to build a TargetMachine
244 TargetMachineBuilder TMBuilder;
245
246 /// Vector holding the in-memory buffer containing the produced binaries.
247 std::vector> ProducedBinaries;
248
249 /// Vector holding the input buffers containing the bitcode modules to
250 /// process.
251 std::vector Modules;
252
253 /// Set of symbols that need to be preserved outside of the set of bitcode
254 /// files.
255 StringSet<> PreservedSymbols;
256
257 /// Set of symbols that are cross-referenced between bitcode files.
258 StringSet<> CrossReferencedSymbols;
259
260 /// Control the caching behavior.
261 CachingOptions CacheOptions;
262
263 /// Path to a directory to save the temporary bitcode files.
264 std::string SaveTempsDir;
265
266 /// Flag to enable/disable CodeGen. When set to true, the process stops after
267 /// optimizations and a bitcode is produced.
268 bool DisableCodeGen = false;
269
270 /// Flag to indicate that only the CodeGen will be performed, no cross-module
271 /// importing or optimization.
272 bool CodeGenOnly = false;
273 };
274 }
275 #endif
+0
-32
include/llvm/LTO/UpdateCompilerUsed.h less more
None //==------ UpdateCompilerUsed.h - LLVM Link Time Optimizer Utility --------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares a helper class to update llvm.compiler_used metadata.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_LTO_UPDATE_COMPILER_USED_H
14 #define LLVM_LTO_UPDATE_COMPILER_USED_H
15
16 #include "llvm/ADT/StringSet.h"
17 #include "llvm/IR/GlobalValue.h"
18
19 namespace llvm {
20 class Module;
21 class TargetMachine;
22
23 /// Find all globals in \p TheModule that are referenced in
24 /// \p AsmUndefinedRefs, as well as the user-supplied functions definitions that
25 /// are also libcalls, and create or update the magic "llvm.compiler_used"
26 /// global in \p TheModule.
27 void updateCompilerUsed(Module &TheModule, const TargetMachine &TM,
28 const StringSet<> &AsmUndefinedRefs);
29 }
30
31 #endif // LLVM_LTO_UPDATE_COMPILER_USED_H
0 //===-LTOCodeGenerator.h - LLVM Link Time Optimizer -----------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the LTOCodeGenerator class.
10 //
11 // LTO compilation consists of three phases: Pre-IPO, IPO and Post-IPO.
12 //
13 // The Pre-IPO phase compiles source code into bitcode file. The resulting
14 // bitcode files, along with object files and libraries, will be fed to the
15 // linker to through the IPO and Post-IPO phases. By using obj-file extension,
16 // the resulting bitcode file disguises itself as an object file, and therefore
17 // obviates the need of writing a special set of the make-rules only for LTO
18 // compilation.
19 //
20 // The IPO phase perform inter-procedural analyses and optimizations, and
21 // the Post-IPO consists two sub-phases: intra-procedural scalar optimizations
22 // (SOPT), and intra-procedural target-dependent code generator (CG).
23 //
24 // As of this writing, we don't separate IPO and the Post-IPO SOPT. They
25 // are intermingled together, and are driven by a single pass manager (see
26 // PassManagerBuilder::populateLTOPassManager()).
27 //
28 // The "LTOCodeGenerator" is the driver for the IPO and Post-IPO stages.
29 // The "CodeGenerator" here is bit confusing. Don't confuse the "CodeGenerator"
30 // with the machine specific code generator.
31 //
32 //===----------------------------------------------------------------------===//
33
34 #ifndef LLVM_LTO_LTOCODEGENERATOR_H
35 #define LLVM_LTO_LTOCODEGENERATOR_H
36
37 #include "llvm-c/lto.h"
38 #include "llvm/ADT/SmallPtrSet.h"
39 #include "llvm/ADT/StringMap.h"
40 #include "llvm/ADT/StringSet.h"
41 #include "llvm/IR/GlobalValue.h"
42 #include "llvm/IR/Module.h"
43 #include "llvm/Target/TargetMachine.h"
44 #include "llvm/Target/TargetOptions.h"
45 #include
46 #include
47
48 namespace llvm {
49 template class ArrayRef;
50 class LLVMContext;
51 class DiagnosticInfo;
52 class Linker;
53 class Mangler;
54 class MemoryBuffer;
55 class TargetLibraryInfo;
56 class TargetMachine;
57 class raw_ostream;
58 class raw_pwrite_stream;
59
60 //===----------------------------------------------------------------------===//
61 /// C++ class which implements the opaque lto_code_gen_t type.
62 ///
63 struct LTOCodeGenerator {
64 static const char *getVersionString();
65
66 LTOCodeGenerator(LLVMContext &Context);
67 ~LTOCodeGenerator();
68
69 /// Merge given module. Return true on success.
70 ///
71 /// Resets \a HasVerifiedInput.
72 bool addModule(struct LTOModule *);
73
74 /// Set the destination module.
75 ///
76 /// Resets \a HasVerifiedInput.
77 void setModule(std::unique_ptr M);
78
79 void setTargetOptions(const TargetOptions &Options);
80 void setDebugInfo(lto_debug_model);
81 void setCodePICModel(Optional Model) { RelocModel = Model; }
82
83 /// Set the file type to be emitted (assembly or object code).
84 /// The default is TargetMachine::CGFT_ObjectFile.
85 void setFileType(TargetMachine::CodeGenFileType FT) { FileType = FT; }
86
87 void setCpu(const char *MCpu) { this->MCpu = MCpu; }
88 void setAttr(const char *MAttr) { this->MAttr = MAttr; }
89 void setOptLevel(unsigned OptLevel);
90
91 void setShouldInternalize(bool Value) { ShouldInternalize = Value; }
92 void setShouldEmbedUselists(bool Value) { ShouldEmbedUselists = Value; }
93
94 /// Restore linkage of globals
95 ///
96 /// When set, the linkage of globals will be restored prior to code
97 /// generation. That is, a global symbol that had external linkage prior to
98 /// LTO will be emitted with external linkage again; and a local will remain
99 /// local. Note that this option only affects the end result - globals may
100 /// still be internalized in the process of LTO and may be modified and/or
101 /// deleted where legal.
102 ///
103 /// The default behavior will internalize globals (unless on the preserve
104 /// list) and, if parallel code generation is enabled, will externalize
105 /// all locals.
106 void setShouldRestoreGlobalsLinkage(bool Value) {
107 ShouldRestoreGlobalsLinkage = Value;
108 }
109
110 void addMustPreserveSymbol(StringRef Sym) { MustPreserveSymbols[Sym] = 1; }
111
112 /// Pass options to the driver and optimization passes.
113 ///
114 /// These options are not necessarily for debugging purpose (the function
115 /// name is misleading). This function should be called before
116 /// LTOCodeGenerator::compilexxx(), and
117 /// LTOCodeGenerator::writeMergedModules().
118 void setCodeGenDebugOptions(const char *Opts);
119
120 /// Parse the options set in setCodeGenDebugOptions.
121 ///
122 /// Like \a setCodeGenDebugOptions(), this must be called before
123 /// LTOCodeGenerator::compilexxx() and
124 /// LTOCodeGenerator::writeMergedModules().
125 void parseCodeGenDebugOptions();
126
127 /// Write the merged module to the file specified by the given path. Return
128 /// true on success.
129 ///
130 /// Calls \a verifyMergedModuleOnce().
131 bool writeMergedModules(const char *Path);
132
133 /// Compile the merged module into a *single* output file; the path to output
134 /// file is returned to the caller via argument "name". Return true on
135 /// success.
136 ///
137 /// \note It is up to the linker to remove the intermediate output file. Do
138 /// not try to remove the object file in LTOCodeGenerator's destructor as we
139 /// don't who (LTOCodeGenerator or the output file) will last longer.
140 bool compile_to_file(const char **Name, bool DisableVerify,
141 bool DisableInline, bool DisableGVNLoadPRE,
142 bool DisableVectorization);
143
144 /// As with compile_to_file(), this function compiles the merged module into
145 /// single output file. Instead of returning the output file path to the
146 /// caller (linker), it brings the output to a buffer, and returns the buffer
147 /// to the caller. This function should delete the intermediate file once
148 /// its content is brought to memory. Return NULL if the compilation was not
149 /// successful.
150 std::unique_ptr compile(bool DisableVerify, bool DisableInline,
151 bool DisableGVNLoadPRE,
152 bool DisableVectorization);
153
154 /// Optimizes the merged module. Returns true on success.
155 ///
156 /// Calls \a verifyMergedModuleOnce().
157 bool optimize(bool DisableVerify, bool DisableInline, bool DisableGVNLoadPRE,
158 bool DisableVectorization);
159
160 /// Compiles the merged optimized module into a single output file. It brings
161 /// the output to a buffer, and returns the buffer to the caller. Return NULL
162 /// if the compilation was not successful.
163 std::unique_ptr compileOptimized();
164
165 /// Compile the merged optimized module into out.size() output files each
166 /// representing a linkable partition of the module. If out contains more
167 /// than one element, code generation is done in parallel with out.size()
168 /// threads. Output files will be written to members of out. Returns true on
169 /// success.
170 ///
171 /// Calls \a verifyMergedModuleOnce().
172 bool compileOptimized(ArrayRef Out);
173
174 void setDiagnosticHandler(lto_diagnostic_handler_t, void *);
175
176 LLVMContext &getContext() { return Context; }
177
178 void resetMergedModule() { MergedModule.reset(); }
179
180 private:
181 void initializeLTOPasses();
182
183 /// Verify the merged module on first call.
184 ///
185 /// Sets \a HasVerifiedInput on first call and doesn't run again on the same
186 /// input.
187 void verifyMergedModuleOnce();
188
189 bool compileOptimizedToFile(const char **Name);
190 void restoreLinkageForExternals();
191 void applyScopeRestrictions();
192 void preserveDiscardableGVs(
193 Module &TheModule,
194 llvm::function_ref mustPreserveGV);
195
196 bool determineTarget();
197 std::unique_ptr createTargetMachine();
198
199 static void DiagnosticHandler(const DiagnosticInfo &DI, void *Context);
200
201 void DiagnosticHandler2(const DiagnosticInfo &DI);
202
203 void emitError(const std::string &ErrMsg);
204 void emitWarning(const std::string &ErrMsg);
205
206 LLVMContext &Context;
207 std::unique_ptr MergedModule;
208 std::unique_ptr TheLinker;
209 std::unique_ptr TargetMach;
210 bool EmitDwarfDebugInfo = false;
211 bool ScopeRestrictionsDone = false;
212 bool HasVerifiedInput = false;
213 Optional RelocModel;
214 StringSet<> MustPreserveSymbols;
215 StringSet<> AsmUndefinedRefs;
216 StringMap ExternalSymbols;
217 std::vector CodegenOptions;
218 std::string FeatureStr;
219 std::string MCpu;
220 std::string MAttr;
221 std::string NativeObjectPath;
222 TargetOptions Options;
223 CodeGenOpt::Level CGOptLevel = CodeGenOpt::Default;
224 const Target *MArch = nullptr;
225 std::string TripleStr;
226 unsigned OptLevel = 2;
227 lto_diagnostic_handler_t DiagHandler = nullptr;
228 void *DiagContext = nullptr;
229 bool ShouldInternalize = true;
230 bool ShouldEmbedUselists = false;
231 bool ShouldRestoreGlobalsLinkage = false;
232 TargetMachine::CodeGenFileType FileType = TargetMachine::CGFT_ObjectFile;
233 };
234 }
235 #endif
0 //===-LTOModule.h - LLVM Link Time Optimizer ------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the LTOModule class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_LTO_LTOMODULE_H
14 #define LLVM_LTO_LTOMODULE_H
15
16 #include "llvm-c/lto.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringSet.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Object/IRObjectFile.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include
23 #include
24
25 // Forward references to llvm classes.
26 namespace llvm {
27 class Function;
28 class GlobalValue;
29 class MemoryBuffer;
30 class TargetOptions;
31 class Value;
32
33 //===----------------------------------------------------------------------===//
34 /// C++ class which implements the opaque lto_module_t type.
35 ///
36 struct LTOModule {
37 private:
38 struct NameAndAttributes {
39 const char *name;
40 uint32_t attributes;
41 bool isFunction;
42 const GlobalValue *symbol;
43 };
44
45 std::unique_ptr OwnedContext;
46
47 std::string LinkerOpts;
48
49 std::unique_ptr IRFile;
50 std::unique_ptr _target;
51 std::vector _symbols;
52
53 // _defines and _undefines only needed to disambiguate tentative definitions
54 StringSet<> _defines;
55 StringMap _undefines;
56 std::vector _asm_undefines;
57
58 LTOModule(std::unique_ptr Obj, TargetMachine *TM);
59
60 public:
61 ~LTOModule();
62
63 /// Returns 'true' if the file or memory contents is LLVM bitcode.
64 static bool isBitcodeFile(const void *mem, size_t length);
65 static bool isBitcodeFile(const char *path);
66
67 /// Returns 'true' if the Module is produced for ThinLTO.
68 bool isThinLTO();
69
70 /// Returns 'true' if the memory buffer is LLVM bitcode for the specified
71 /// triple.
72 static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
73 StringRef triplePrefix);
74
75 /// Returns a string representing the producer identification stored in the
76 /// bitcode, or "" if the bitcode does not contains any.
77 ///
78 static std::string getProducerString(MemoryBuffer *Buffer);
79
80 /// Create a MemoryBuffer from a memory range with an optional name.
81 static std::unique_ptr
82 makeBuffer(const void *mem, size_t length, StringRef name = "");
83
84 /// Create an LTOModule. N.B. These methods take ownership of the buffer. The
85 /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters,
86 /// and the AsmParsers by calling:
87 ///
88 /// InitializeAllTargets();
89 /// InitializeAllTargetMCs();
90 /// InitializeAllAsmPrinters();
91 /// InitializeAllAsmParsers();
92 static ErrorOr>
93 createFromFile(LLVMContext &Context, const char *path,
94 const TargetOptions &options);
95 static ErrorOr>
96 createFromOpenFile(LLVMContext &Context, int fd, const char *path,
97 size_t size, const TargetOptions &options);
98 static ErrorOr>
99 createFromOpenFileSlice(LLVMContext &Context, int fd, const char *path,
100 size_t map_size, off_t offset,
101 const TargetOptions &options);
102 static ErrorOr>
103 createFromBuffer(LLVMContext &Context, const void *mem, size_t length,
104 const TargetOptions &options, StringRef path = "");
105 static ErrorOr>
106 createInLocalContext(std::unique_ptr Context, const void *mem,
107 size_t length, const TargetOptions &options,
108 StringRef path);
109
110 const Module &getModule() const {
111 return const_cast(this)->getModule();
112 }
113 Module &getModule() {
114 return IRFile->getModule();
115 }
116
117 std::unique_ptr takeModule() { return IRFile->takeModule(); }
118
119 /// Return the Module's target triple.
120 const std::string &getTargetTriple() {
121 return getModule().getTargetTriple();
122 }
123
124 /// Set the Module's target triple.
125 void setTargetTriple(StringRef Triple) {
126 getModule().setTargetTriple(Triple);
127 }
128
129 /// Get the number of symbols
130 uint32_t getSymbolCount() {
131 return _symbols.size();
132 }
133
134 /// Get the attributes for a symbol at the specified index.
135 lto_symbol_attributes getSymbolAttributes(uint32_t index) {
136 if (index < _symbols.size())
137 return lto_symbol_attributes(_symbols[index].attributes);
138 return lto_symbol_attributes(0);
139 }
140
141 /// Get the name of the symbol at the specified index.
142 const char *getSymbolName(uint32_t index) {
143 if (index < _symbols.size())
144 return _symbols[index].name;
145 return nullptr;
146 }
147
148 const GlobalValue *getSymbolGV(uint32_t index) {
149 if (index < _symbols.size())
150 return _symbols[index].symbol;
151 return nullptr;
152 }
153
154 const char *getLinkerOpts() {
155 return LinkerOpts.c_str();
156 }
157
158 const std::vector &getAsmUndefinedRefs() {
159 return _asm_undefines;
160 }
161
162 private:
163 /// Parse metadata from the module
164 // FIXME: it only parses "Linker Options" metadata at the moment
165 void parseMetadata();
166
167 /// Parse the symbols from the module and model-level ASM and add them to
168 /// either the defined or undefined lists.
169 void parseSymbols();
170
171 /// Add a symbol which isn't defined just yet to a list to be resolved later.
172 void addPotentialUndefinedSymbol(const object::BasicSymbolRef &Sym,
173 bool isFunc);
174
175 /// Add a defined symbol to the list.
176 void addDefinedSymbol(const char *Name, const GlobalValue *def,
177 bool isFunction);
178
179 /// Add a data symbol as defined to the list.
180 void addDefinedDataSymbol(const object::BasicSymbolRef &Sym);
181 void addDefinedDataSymbol(const char*Name, const GlobalValue *v);
182
183 /// Add a function symbol as defined to the list.
184 void addDefinedFunctionSymbol(const object::BasicSymbolRef &Sym);
185 void addDefinedFunctionSymbol(const char *Name, const Function *F);
186
187 /// Add a global symbol from module-level ASM to the defined list.
188 void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
189
190 /// Add a global symbol from module-level ASM to the undefined list.
191 void addAsmGlobalSymbolUndef(const char *);
192
193 /// Parse i386/ppc ObjC class data structure.
194 void addObjCClass(const GlobalVariable *clgv);
195
196 /// Parse i386/ppc ObjC category data structure.
197 void addObjCCategory(const GlobalVariable *clgv);
198
199 /// Parse i386/ppc ObjC class list data structure.
200 void addObjCClassRef(const GlobalVariable *clgv);
201
202 /// Get string that the data pointer points to.
203 bool objcClassNameFromExpression(const Constant *c, std::string &name);
204
205 /// Create an LTOModule (private version).
206 static ErrorOr>
207 makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
208 LLVMContext &Context, bool ShouldBeLazy);
209 };
210 }
211 #endif
0 //===-ThinLTOCodeGenerator.h - LLVM Link Time Optimizer -------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the ThinLTOCodeGenerator class, similar to the
10 // LTOCodeGenerator but for the ThinLTO scheme. It provides an interface for
11 // linker plugin.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_LTO_THINLTOCODEGENERATOR_H
16 #define LLVM_LTO_THINLTOCODEGENERATOR_H
17
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/Support/CodeGen.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Target/TargetOptions.h"
25
26 #include
27
28 namespace llvm {
29 class StringRef;
30 class LLVMContext;
31 class TargetMachine;
32
33 /// Helper to gather options relevant to the target machine creation
34 struct TargetMachineBuilder {
35 Triple TheTriple;
36 std::string MCpu;
37 std::string MAttr;
38 TargetOptions Options;
39 Optional RelocModel;
40 CodeGenOpt::Level CGOptLevel = CodeGenOpt::Default;
41
42 std::unique_ptr create() const;
43 };
44
45 /// This class define an interface similar to the LTOCodeGenerator, but adapted
46 /// for ThinLTO processing.
47 /// The ThinLTOCodeGenerator is not intended to be reuse for multiple
48 /// compilation: the model is that the client adds modules to the generator and
49 /// ask to perform the ThinLTO optimizations / codegen, and finally destroys the
50 /// codegenerator.
51 class ThinLTOCodeGenerator {
52 public:
53 /// Add given module to the code generator.
54 void addModule(StringRef Identifier, StringRef Data);
55
56 /**
57 * Adds to a list of all global symbols that must exist in the final generated
58 * code. If a symbol is not listed there, it will be optimized away if it is
59 * inlined into every usage.
60 */
61 void preserveSymbol(StringRef Name);
62
63 /**
64 * Adds to a list of all global symbols that are cross-referenced between
65 * ThinLTO files. If the ThinLTO CodeGenerator can ensure that every
66 * references from a ThinLTO module to this symbol is optimized away, then
67 * the symbol can be discarded.
68 */
69 void crossReferenceSymbol(StringRef Name);
70
71 /**
72 * Process all the modules that were added to the code generator in parallel.
73 *
74 * Client can access the resulting object files using getProducedBinaries()
75 */
76 void run();
77
78 /**
79 * Return the "in memory" binaries produced by the code generator.
80 */
81 std::vector> &getProducedBinaries() {
82 return ProducedBinaries;
83 }
84
85 /**
86 * \defgroup Options setters
87 * @{
88 */
89
90 /**
91 * \defgroup Cache controlling options
92 *
93 * These entry points control the ThinLTO cache. The cache is intended to
94 * support incremental build, and thus needs to be persistent accross build.
95 * The client enabled the cache by supplying a path to an existing directory.
96 * The code generator will use this to store objects files that may be reused
97 * during a subsequent build.
98 * To avoid filling the disk space, a few knobs are provided:
99 * - The pruning interval limit the frequency at which the garbage collector
100 * will try to scan the cache directory to prune it from expired entries.
101 * Setting to -1 disable the pruning (default).
102 * - The pruning expiration time indicates to the garbage collector how old
103 * an entry needs to be to be removed.
104 * - Finally, the garbage collector can be instructed to prune the cache till
105 * the occupied space goes below a threshold.
106 * @{
107 */
108
109 struct CachingOptions {
110 std::string Path; // Path to the cache, empty to disable.
111 int PruningInterval = 1200; // seconds, -1 to disable pruning.
112 unsigned int Expiration = 7 * 24 * 3600; // seconds (1w default).
113 unsigned MaxPercentageOfAvailableSpace = 75; // percentage.
114 };
115
116 /// Provide a path to a directory where to store the cached files for
117 /// incremental build.
118 void setCacheDir(std::string Path) { CacheOptions.Path = std::move(Path); }
119
120 /// Cache policy: interval (seconds) between two prune of the cache. Set to a
121 /// negative value (default) to disable pruning. A value of 0 will be ignored.
122 void setCachePruningInterval(int Interval) {
123 if (Interval)
124 CacheOptions.PruningInterval = Interval;
125 }
126
127 /// Cache policy: expiration (in seconds) for an entry.
128 /// A value of 0 will be ignored.
129 void setCacheEntryExpiration(unsigned Expiration) {
130 if (Expiration)
131 CacheOptions.Expiration = Expiration;
132 }
133
134 /**
135 * Sets the maximum cache size that can be persistent across build, in terms
136 * of percentage of the available space on the the disk. Set to 100 to
137 * indicate no limit, 50 to indicate that the cache size will not be left over
138 * half the available space. A value over 100 will be reduced to 100, and a
139 * value of 0 will be ignored.
140 *
141 *
142 * The formula looks like:
143 * AvailableSpace = FreeSpace + ExistingCacheSize
144 * NewCacheSize = AvailableSpace * P/100
145 *
146 */
147 void setMaxCacheSizeRelativeToAvailableSpace(unsigned Percentage) {
148 if (Percentage)
149 CacheOptions.MaxPercentageOfAvailableSpace = Percentage;
150 }
151
152 /**@}*/
153
154 /// Set the path to a directory where to save temporaries at various stages of
155 /// the processing.
156 void setSaveTempsDir(std::string Path) { SaveTempsDir = std::move(Path); }
157
158 /// CPU to use to initialize the TargetMachine
159 void setCpu(std::string Cpu) { TMBuilder.MCpu = std::move(Cpu); }
160
161 /// Subtarget attributes
162 void setAttr(std::string MAttr) { TMBuilder.MAttr = std::move(MAttr); }
163
164 /// TargetMachine options
165 void setTargetOptions(TargetOptions Options) {
166 TMBuilder.Options = std::move(Options);
167 }
168
169 /// CodeModel
170 void setCodePICModel(Optional Model) {
171 TMBuilder.RelocModel = Model;
172 }
173
174 /// CodeGen optimization level
175 void setCodeGenOptLevel(CodeGenOpt::Level CGOptLevel) {
176 TMBuilder.CGOptLevel = CGOptLevel;
177 }
178
179 /// Disable CodeGen, only run the stages till codegen and stop. The output
180 /// will be bitcode.
181 void disableCodeGen(bool Disable) { DisableCodeGen = Disable; }
182
183 /// Perform CodeGen only: disable all other stages.
184 void setCodeGenOnly(bool CGOnly) { CodeGenOnly = CGOnly; }
185
186 /**@}*/
187
188 /**
189 * \defgroup Set of APIs to run individual stages in isolation.
190 * @{
191 */
192
193 /**
194 * Produce the combined summary index from all the bitcode files:
195 * "thin-link".
196 */
197 std::unique_ptr linkCombinedIndex();
198
199 /**
200 * Perform promotion and renaming of exported internal functions,
201 * and additionally resolve weak and linkonce symbols.
202 * Index is updated to reflect linkage changes from weak resolution.
203 */
204 void promote(Module &Module, ModuleSummaryIndex &Index);
205
206 /**
207 * Compute and emit the imported files for module at \p ModulePath.
208 */
209 static void emitImports(StringRef ModulePath, StringRef OutputName,
210 ModuleSummaryIndex &Index);
211
212 /**
213 * Perform cross-module importing for the module identified by
214 * ModuleIdentifier.
215 */
216 void crossModuleImport(Module &Module, ModuleSummaryIndex &Index);
217
218 /**
219 * Compute the list of summaries needed for importing into module.
220 */
221 static void gatherImportedSummariesForModule(
222 StringRef ModulePath, ModuleSummaryIndex &Index,
223 std::map &ModuleToSummariesForIndex);
224
225 /**
226 * Perform internalization. Index is updated to reflect linkage changes.
227 */
228 void internalize(Module &Module, ModuleSummaryIndex &Index);
229
230 /**
231 * Perform post-importing ThinLTO optimizations.
232 */
233 void optimize(Module &Module);
234
235 /**
236 * Perform ThinLTO CodeGen.
237 */
238 std::unique_ptr codegen(Module &Module);
239
240 /**@}*/
241
242 private:
243 /// Helper factory to build a TargetMachine
244 TargetMachineBuilder TMBuilder;
245
246 /// Vector holding the in-memory buffer containing the produced binaries.
247 std::vector> ProducedBinaries;
248
249 /// Vector holding the input buffers containing the bitcode modules to
250 /// process.
251 std::vector Modules;
252
253 /// Set of symbols that need to be preserved outside of the set of bitcode
254 /// files.
255 StringSet<> PreservedSymbols;
256
257 /// Set of symbols that are cross-referenced between bitcode files.
258 StringSet<> CrossReferencedSymbols;
259
260 /// Control the caching behavior.
261 CachingOptions CacheOptions;
262
263 /// Path to a directory to save the temporary bitcode files.
264 std::string SaveTempsDir;
265
266 /// Flag to enable/disable CodeGen. When set to true, the process stops after
267 /// optimizations and a bitcode is produced.
268 bool DisableCodeGen = false;
269
270 /// Flag to indicate that only the CodeGen will be performed, no cross-module
271 /// importing or optimization.
272 bool CodeGenOnly = false;
273 };
274 }
275 #endif
0 //==------ UpdateCompilerUsed.h - LLVM Link Time Optimizer Utility --------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares a helper class to update llvm.compiler_used metadata.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_LTO_UPDATE_COMPILER_USED_H
14 #define LLVM_LTO_UPDATE_COMPILER_USED_H
15
16 #include "llvm/ADT/StringSet.h"
17 #include "llvm/IR/GlobalValue.h"
18
19 namespace llvm {
20 class Module;
21 class TargetMachine;
22
23 /// Find all globals in \p TheModule that are referenced in
24 /// \p AsmUndefinedRefs, as well as the user-supplied functions definitions that
25 /// are also libcalls, and create or update the magic "llvm.compiler_used"
26 /// global in \p TheModule.
27 void updateCompilerUsed(Module &TheModule, const TargetMachine &TM,
28 const StringSet<> &AsmUndefinedRefs);
29 }
30
31 #endif // LLVM_LTO_UPDATE_COMPILER_USED_H
1111 //
1212 //===----------------------------------------------------------------------===//
1313
14 #include "llvm/LTO/LTOCodeGenerator.h"
15 #include "llvm/LTO/UpdateCompilerUsed.h"
14 #include "llvm/LTO/legacy/LTOCodeGenerator.h"
1615
1716 #include "llvm/ADT/Statistic.h"
1817 #include "llvm/ADT/StringExtras.h"
3534 #include "llvm/IR/Module.h"
3635 #include "llvm/IR/Verifier.h"
3736 #include "llvm/InitializePasses.h"
38 #include "llvm/LTO/LTOModule.h"
37 #include "llvm/LTO/legacy/LTOModule.h"
38 #include "llvm/LTO/legacy/UpdateCompilerUsed.h"
3939 #include "llvm/Linker/Linker.h"
4040 #include "llvm/MC/MCAsmInfo.h"
4141 #include "llvm/MC/MCContext.h"
1111 //
1212 //===----------------------------------------------------------------------===//
1313
14 #include "llvm/LTO/LTOModule.h"
14 #include "llvm/LTO/legacy/LTOModule.h"
1515 #include "llvm/ADT/Triple.h"
1616 #include "llvm/Bitcode/ReaderWriter.h"
1717 #include "llvm/CodeGen/Analysis.h"
1111 //
1212 //===----------------------------------------------------------------------===//
1313
14 #include "llvm/LTO/ThinLTOCodeGenerator.h"
14 #include "llvm/LTO/legacy/ThinLTOCodeGenerator.h"
1515
1616 #ifdef HAVE_LLVM_REVISION
1717 #include "LLVMLTORevision.h"
1010 //
1111 //===----------------------------------------------------------------------===//
1212
13 #include "llvm/LTO/legacy/UpdateCompilerUsed.h"
1314 #include "llvm/Analysis/TargetLibraryInfo.h"
1415 #include "llvm/IR/LegacyPassManager.h"
1516 #include "llvm/IR/Mangler.h"
16 #include "llvm/LTO/UpdateCompilerUsed.h"
1717 #include "llvm/Target/TargetLowering.h"
1818 #include "llvm/Target/TargetSubtargetInfo.h"
1919 #include "llvm/Transforms/IPO/Internalize.h"
1818 #include "llvm/IR/LLVMContext.h"
1919 #include "llvm/IR/Verifier.h"
2020 #include "llvm/IRReader/IRReader.h"
21 #include "llvm/LTO/LTOCodeGenerator.h"
22 #include "llvm/LTO/LTOModule.h"
23 #include "llvm/LTO/ThinLTOCodeGenerator.h"
21 #include "llvm/LTO/legacy/LTOCodeGenerator.h"
22 #include "llvm/LTO/legacy/LTOModule.h"
23 #include "llvm/LTO/legacy/ThinLTOCodeGenerator.h"
2424 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
2525 #include "llvm/Support/CommandLine.h"
2626 #include "llvm/Support/FileSystem.h"
1818 #include "llvm/IR/DiagnosticInfo.h"
1919 #include "llvm/IR/DiagnosticPrinter.h"
2020 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/LTO/LTOCodeGenerator.h"
22 #include "llvm/LTO/LTOModule.h"
23 #include "llvm/LTO/ThinLTOCodeGenerator.h"
21 #include "llvm/LTO/legacy/LTOCodeGenerator.h"
22 #include "llvm/LTO/legacy/LTOModule.h"
23 #include "llvm/LTO/legacy/ThinLTOCodeGenerator.h"
2424 #include "llvm/Support/MemoryBuffer.h"
2525 #include "llvm/Support/Signals.h"
2626 #include "llvm/Support/TargetSelect.h"