LLVM  4.0.0
Config.h
Go to the documentation of this file.
1 //===-Config.h - LLVM Link Time Optimizer Configuration -------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the lto::Config data structure, which allows clients to
11 // configure LTO.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_LTO_CONFIG_H
16 #define LLVM_LTO_CONFIG_H
17 
18 #include "llvm/IR/DiagnosticInfo.h"
19 #include "llvm/Support/CodeGen.h"
21 
22 #include <functional>
23 
24 namespace llvm {
25 
26 class Error;
27 class Module;
28 class ModuleSummaryIndex;
29 class raw_pwrite_stream;
30 
31 namespace lto {
32 
33 /// LTO configuration. A linker can configure LTO by setting fields in this data
34 /// structure and passing it to the lto::LTO constructor.
35 struct Config {
36  // Note: when adding fields here, consider whether they need to be added to
37  // computeCacheKey in LTO.cpp.
38  std::string CPU;
40  std::vector<std::string> MAttrs;
44  unsigned OptLevel = 2;
45  bool DisableVerify = false;
46 
47  /// Disable entirely the optimizer, including importing for ThinLTO
48  bool CodeGenOnly = false;
49 
50  /// If this field is set, the set of passes run in the middle-end optimizer
51  /// will be the one specified by the string. Only works with the new pass
52  /// manager as the old one doesn't have this ability.
53  std::string OptPipeline;
54 
55  // If this field is set, it has the same effect of specifying an AA pipeline
56  // identified by the string. Only works with the new pass manager, in
57  // conjunction OptPipeline.
58  std::string AAPipeline;
59 
60  /// Setting this field will replace target triples in input files with this
61  /// triple.
62  std::string OverrideTriple;
63 
64  /// Setting this field will replace unspecified target triples in input files
65  /// with this triple.
66  std::string DefaultTriple;
67 
68  /// Sample PGO profile path.
69  std::string SampleProfile;
70 
73 
74  /// If this field is set, LTO will write input file paths and symbol
75  /// resolutions here in llvm-lto2 command line flag format. This can be
76  /// used for testing and for running the LTO pipeline outside of the linker
77  /// with llvm-lto2.
78  std::unique_ptr<raw_ostream> ResolutionFile;
79 
80  /// The following callbacks deal with tasks, which normally represent the
81  /// entire optimization and code generation pipeline for what will become a
82  /// single native object file. Each task has a unique identifier between 0 and
83  /// getMaxTasks()-1, which is supplied to the callback via the Task parameter.
84  /// A task represents the entire pipeline for ThinLTO and regular
85  /// (non-parallel) LTO, but a parallel code generation task will be split into
86  /// N tasks before code generation, where N is the parallelism level.
87  ///
88  /// LTO may decide to stop processing a task at any time, for example if the
89  /// module is empty or if a module hook (see below) returns false. For this
90  /// reason, the client should not expect to receive exactly getMaxTasks()
91  /// native object files.
92 
93  /// A module hook may be used by a linker to perform actions during the LTO
94  /// pipeline. For example, a linker may use this function to implement
95  /// -save-temps. If this function returns false, any further processing for
96  /// that task is aborted.
97  ///
98  /// Module hooks must be thread safe with respect to the linker's internal
99  /// data structures. A module hook will never be called concurrently from
100  /// multiple threads with the same task ID, or the same module.
101  ///
102  /// Note that in out-of-process backend scenarios, none of the hooks will be
103  /// called for ThinLTO tasks.
104  typedef std::function<bool(unsigned Task, const Module &)> ModuleHookFn;
105 
106  /// This module hook is called after linking (regular LTO) or loading
107  /// (ThinLTO) the module, before modifying it.
109 
110  /// This hook is called after promoting any internal functions
111  /// (ThinLTO-specific).
113 
114  /// This hook is called after internalizing the module.
116 
117  /// This hook is called after importing from other modules (ThinLTO-specific).
119 
120  /// This module hook is called after optimization is complete.
122 
123  /// This module hook is called before code generation. It is similar to the
124  /// PostOptModuleHook, but for parallel code generation it is called after
125  /// splitting the module.
127 
128  /// A combined index hook is called after all per-module indexes have been
129  /// combined (ThinLTO-specific). It can be used to implement -save-temps for
130  /// the combined index.
131  ///
132  /// If this function returns false, any further processing for ThinLTO tasks
133  /// is aborted.
134  ///
135  /// It is called regardless of whether the backend is in-process, although it
136  /// is not called from individual backend processes.
137  typedef std::function<bool(const ModuleSummaryIndex &Index)>
140 
141  /// This is a convenience function that configures this Config object to write
142  /// temporary files named after the given OutputFileName for each of the LTO
143  /// phases to disk. A client can use this function to implement -save-temps.
144  ///
145  /// FIXME: Temporary files derived from ThinLTO backends are currently named
146  /// after the input file name, rather than the output file name, when
147  /// UseInputModulePath is set to true.
148  ///
149  /// Specifically, it (1) sets each of the above module hooks and the combined
150  /// index hook to a function that calls the hook function (if any) that was
151  /// present in the appropriate field when the addSaveTemps function was
152  /// called, and writes the module to a bitcode file with a name prefixed by
153  /// the given output file name, and (2) creates a resolution file whose name
154  /// is prefixed by the given output file name and sets ResolutionFile to its
155  /// file handle.
156  Error addSaveTemps(std::string OutputFileName,
157  bool UseInputModulePath = false);
158 };
159 
160 /// A derived class of LLVMContext that initializes itself according to a given
161 /// Config object. The purpose of this class is to tie ownership of the
162 /// diagnostic handler to the context, as opposed to the Config object (which
163 /// may be ephemeral).
165  static void funcDiagHandler(const DiagnosticInfo &DI, void *Context) {
166  auto *Fn = static_cast<DiagnosticHandlerFunction *>(Context);
167  (*Fn)(DI);
168  }
169 
174  }
176 };
177 
178 }
179 }
180 
181 #endif
std::string CPU
Definition: Config.h:38
std::string AAPipeline
Definition: Config.h:58
LLVMContext & Context
CodeGenOpt::Level CGOptLevel
Definition: Config.h:43
bool ShouldDiscardValueNames
Definition: Config.h:71
std::string OverrideTriple
Setting this field will replace target triples in input files with this triple.
Definition: Config.h:62
bool CodeGenOnly
Disable entirely the optimizer, including importing for ThinLTO.
Definition: Config.h:48
void enableDebugTypeODRUniquing()
std::unique_ptr< raw_ostream > ResolutionFile
If this field is set, LTO will write input file paths and symbol resolutions here in llvm-lto2 comman...
Definition: Config.h:78
ModuleHookFn PreCodeGenModuleHook
This module hook is called before code generation.
Definition: Config.h:126
void setDiscardValueNames(bool Discard)
Set the Context runtime configuration to discard all value name (but GlobalValue).
CombinedIndexHookFn CombinedIndexHook
Definition: Config.h:139
static void funcDiagHandler(const DiagnosticInfo &DI, void *Context)
Definition: Config.h:165
std::vector< std::string > MAttrs
Definition: Config.h:40
std::function< bool(unsigned Task, const Module &)> ModuleHookFn
The following callbacks deal with tasks, which normally represent the entire optimization and code ge...
Definition: Config.h:104
bool DisableVerify
Definition: Config.h:45
std::function< bool(const ModuleSummaryIndex &Index)> CombinedIndexHookFn
A combined index hook is called after all per-module indexes have been combined (ThinLTO-specific).
Definition: Config.h:138
std::string SampleProfile
Sample PGO profile path.
Definition: Config.h:69
TargetOptions Options
Definition: Config.h:39
ModuleHookFn PreOptModuleHook
This module hook is called after linking (regular LTO) or loading (ThinLTO) the module, before modifying it.
Definition: Config.h:108
This is the base abstract class for diagnostic reporting in the backend.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:48
unsigned OptLevel
Definition: Config.h:44
LTO configuration.
Definition: Config.h:35
std::string DefaultTriple
Setting this field will replace unspecified target triples in input files with this triple...
Definition: Config.h:66
DiagnosticHandlerFunction DiagHandler
Definition: Config.h:175
ModuleHookFn PostInternalizeModuleHook
This hook is called after internalizing the module.
Definition: Config.h:115
std::function< void(const DiagnosticInfo &)> DiagnosticHandlerFunction
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
std::string OptPipeline
If this field is set, the set of passes run in the middle-end optimizer will be the one specified by ...
Definition: Config.h:53
DiagnosticHandlerFunction DiagHandler
Definition: Config.h:72
LTOLLVMContext(const Config &C)
Definition: Config.h:170
Reloc::Model RelocModel
Definition: Config.h:41
CodeModel::Model CodeModel
Definition: Config.h:42
void setDiagnosticHandler(DiagnosticHandlerTy DiagHandler, void *DiagContext=nullptr, bool RespectFilters=false)
setDiagnosticHandler - This method sets a handler that is invoked when the backend needs to report an...
ModuleHookFn PostImportModuleHook
This hook is called after importing from other modules (ThinLTO-specific).
Definition: Config.h:118
Lightweight error class with error context and mandatory checking.
ModuleHookFn PostOptModuleHook
This module hook is called after optimization is complete.
Definition: Config.h:121
Error addSaveTemps(std::string OutputFileName, bool UseInputModulePath=false)
This is a convenience function that configures this Config object to write temporary files named afte...
Definition: LTOBackend.cpp:51
A derived class of LLVMContext that initializes itself according to a given Config object...
Definition: Config.h:164
ModuleHookFn PostPromoteModuleHook
This hook is called after promoting any internal functions (ThinLTO-specific).
Definition: Config.h:112