LLVM  8.0.1
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"
22 
23 #include <functional>
24 
25 namespace llvm {
26 
27 class Error;
28 class Module;
29 class ModuleSummaryIndex;
30 class raw_pwrite_stream;
31 
32 namespace lto {
33 
34 /// LTO configuration. A linker can configure LTO by setting fields in this data
35 /// structure and passing it to the lto::LTO constructor.
36 struct Config {
37  // Note: when adding fields here, consider whether they need to be added to
38  // computeCacheKey in LTO.cpp.
39  std::string CPU;
41  std::vector<std::string> MAttrs;
46  unsigned OptLevel = 2;
47  bool DisableVerify = false;
48 
49  /// Use the new pass manager
50  bool UseNewPM = false;
51 
52  /// Flag to indicate that the optimizer should not assume builtins are present
53  /// on the target.
54  bool Freestanding = false;
55 
56  /// Disable entirely the optimizer, including importing for ThinLTO
57  bool CodeGenOnly = false;
58 
59  /// If this field is set, the set of passes run in the middle-end optimizer
60  /// will be the one specified by the string. Only works with the new pass
61  /// manager as the old one doesn't have this ability.
62  std::string OptPipeline;
63 
64  // If this field is set, it has the same effect of specifying an AA pipeline
65  // identified by the string. Only works with the new pass manager, in
66  // conjunction OptPipeline.
67  std::string AAPipeline;
68 
69  /// Setting this field will replace target triples in input files with this
70  /// triple.
71  std::string OverrideTriple;
72 
73  /// Setting this field will replace unspecified target triples in input files
74  /// with this triple.
75  std::string DefaultTriple;
76 
77  /// Sample PGO profile path.
78  std::string SampleProfile;
79 
80  /// Name remapping file for profile data.
81  std::string ProfileRemapping;
82 
83  /// The directory to store .dwo files.
84  std::string DwoDir;
85 
86  /// The path to write a .dwo file to. This should generally only be used when
87  /// running an individual backend directly via thinBackend(), as otherwise
88  /// all .dwo files will be written to the same path.
89  std::string DwoPath;
90 
91  /// Optimization remarks file path.
92  std::string RemarksFilename = "";
93 
94  /// Whether to emit optimization remarks with hotness informations.
95  bool RemarksWithHotness = false;
96 
97  /// Whether to emit the pass manager debuggging informations.
98  bool DebugPassManager = false;
99 
100  /// Statistics output file path.
101  std::string StatsFile;
102 
105 
106  /// If this field is set, LTO will write input file paths and symbol
107  /// resolutions here in llvm-lto2 command line flag format. This can be
108  /// used for testing and for running the LTO pipeline outside of the linker
109  /// with llvm-lto2.
110  std::unique_ptr<raw_ostream> ResolutionFile;
111 
112  /// The following callbacks deal with tasks, which normally represent the
113  /// entire optimization and code generation pipeline for what will become a
114  /// single native object file. Each task has a unique identifier between 0 and
115  /// getMaxTasks()-1, which is supplied to the callback via the Task parameter.
116  /// A task represents the entire pipeline for ThinLTO and regular
117  /// (non-parallel) LTO, but a parallel code generation task will be split into
118  /// N tasks before code generation, where N is the parallelism level.
119  ///
120  /// LTO may decide to stop processing a task at any time, for example if the
121  /// module is empty or if a module hook (see below) returns false. For this
122  /// reason, the client should not expect to receive exactly getMaxTasks()
123  /// native object files.
124 
125  /// A module hook may be used by a linker to perform actions during the LTO
126  /// pipeline. For example, a linker may use this function to implement
127  /// -save-temps. If this function returns false, any further processing for
128  /// that task is aborted.
129  ///
130  /// Module hooks must be thread safe with respect to the linker's internal
131  /// data structures. A module hook will never be called concurrently from
132  /// multiple threads with the same task ID, or the same module.
133  ///
134  /// Note that in out-of-process backend scenarios, none of the hooks will be
135  /// called for ThinLTO tasks.
136  typedef std::function<bool(unsigned Task, const Module &)> ModuleHookFn;
137 
138  /// This module hook is called after linking (regular LTO) or loading
139  /// (ThinLTO) the module, before modifying it.
140  ModuleHookFn PreOptModuleHook;
141 
142  /// This hook is called after promoting any internal functions
143  /// (ThinLTO-specific).
144  ModuleHookFn PostPromoteModuleHook;
145 
146  /// This hook is called after internalizing the module.
148 
149  /// This hook is called after importing from other modules (ThinLTO-specific).
150  ModuleHookFn PostImportModuleHook;
151 
152  /// This module hook is called after optimization is complete.
153  ModuleHookFn PostOptModuleHook;
154 
155  /// This module hook is called before code generation. It is similar to the
156  /// PostOptModuleHook, but for parallel code generation it is called after
157  /// splitting the module.
158  ModuleHookFn PreCodeGenModuleHook;
159 
160  /// A combined index hook is called after all per-module indexes have been
161  /// combined (ThinLTO-specific). It can be used to implement -save-temps for
162  /// the combined index.
163  ///
164  /// If this function returns false, any further processing for ThinLTO tasks
165  /// is aborted.
166  ///
167  /// It is called regardless of whether the backend is in-process, although it
168  /// is not called from individual backend processes.
169  typedef std::function<bool(const ModuleSummaryIndex &Index)>
172 
173  /// This is a convenience function that configures this Config object to write
174  /// temporary files named after the given OutputFileName for each of the LTO
175  /// phases to disk. A client can use this function to implement -save-temps.
176  ///
177  /// FIXME: Temporary files derived from ThinLTO backends are currently named
178  /// after the input file name, rather than the output file name, when
179  /// UseInputModulePath is set to true.
180  ///
181  /// Specifically, it (1) sets each of the above module hooks and the combined
182  /// index hook to a function that calls the hook function (if any) that was
183  /// present in the appropriate field when the addSaveTemps function was
184  /// called, and writes the module to a bitcode file with a name prefixed by
185  /// the given output file name, and (2) creates a resolution file whose name
186  /// is prefixed by the given output file name and sets ResolutionFile to its
187  /// file handle.
188  Error addSaveTemps(std::string OutputFileName,
189  bool UseInputModulePath = false);
190 };
191 
195  : Fn(DiagHandlerFn) {}
196  bool handleDiagnostics(const DiagnosticInfo &DI) override {
197  (*Fn)(DI);
198  return true;
199  }
200 };
201 /// A derived class of LLVMContext that initializes itself according to a given
202 /// Config object. The purpose of this class is to tie ownership of the
203 /// diagnostic handler to the context, as opposed to the Config object (which
204 /// may be ephemeral).
205 // FIXME: This should not be required as diagnostic handler is not callback.
207 
209  setDiscardValueNames(C.ShouldDiscardValueNames);
210  enableDebugTypeODRUniquing();
211  setDiagnosticHandler(
212  llvm::make_unique<LTOLLVMDiagnosticHandler>(&DiagHandler), true);
213  }
215 };
216 
217 }
218 }
219 
220 #endif
const NoneType None
Definition: None.h:24
uint64_t CallInst * C
This is the base class for diagnostic handling in LLVM.
std::string CPU
Definition: Config.h:39
std::string AAPipeline
Definition: Config.h:67
CodeGenOpt::Level CGOptLevel
Definition: Config.h:44
This class represents lattice values for constants.
Definition: AllocatorList.h:24
DiagnosticHandlerFunction * Fn
Definition: Config.h:193
bool ShouldDiscardValueNames
Definition: Config.h:103
std::string OverrideTriple
Setting this field will replace target triples in input files with this triple.
Definition: Config.h:71
bool CodeGenOnly
Disable entirely the optimizer, including importing for ThinLTO.
Definition: Config.h:57
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:110
bool DebugPassManager
Whether to emit the pass manager debuggging informations.
Definition: Config.h:98
ModuleHookFn PreCodeGenModuleHook
This module hook is called before code generation.
Definition: Config.h:158
CombinedIndexHookFn CombinedIndexHook
Definition: Config.h:171
bool RemarksWithHotness
Whether to emit optimization remarks with hotness informations.
Definition: Config.h:95
std::string DwoDir
The directory to store .dwo files.
Definition: Config.h:84
std::vector< std::string > MAttrs
Definition: Config.h:41
std::string ProfileRemapping
Name remapping file for profile data.
Definition: Config.h:81
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:136
bool DisableVerify
Definition: Config.h:47
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:170
std::string SampleProfile
Sample PGO profile path.
Definition: Config.h:78
TargetOptions Options
Definition: Config.h:40
Optional< CodeModel::Model > CodeModel
Definition: Config.h:43
ModuleHookFn PreOptModuleHook
This module hook is called after linking (regular LTO) or loading (ThinLTO) the module, before modifying it.
Definition: Config.h:140
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:69
bool UseNewPM
Use the new pass manager.
Definition: Config.h:50
unsigned OptLevel
Definition: Config.h:46
LTO configuration.
Definition: Config.h:36
std::string DefaultTriple
Setting this field will replace unspecified target triples in input files with this triple...
Definition: Config.h:75
DiagnosticHandlerFunction DiagHandler
Definition: Config.h:214
LTOLLVMDiagnosticHandler(DiagnosticHandlerFunction *DiagHandlerFn)
Definition: Config.h:194
std::string StatsFile
Statistics output file path.
Definition: Config.h:101
ModuleHookFn PostInternalizeModuleHook
This hook is called after internalizing the module.
Definition: Config.h:147
TargetMachine::CodeGenFileType CGFileType
Definition: Config.h:45
std::string RemarksFilename
Optimization remarks file path.
Definition: Config.h:92
Optional< Reloc::Model > RelocModel
Definition: Config.h:42
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:62
DiagnosticHandlerFunction DiagHandler
Definition: Config.h:104
bool handleDiagnostics(const DiagnosticInfo &DI) override
Override handleDiagnostics to provide custom implementation.
Definition: Config.h:196
LTOLLVMContext(const Config &C)
Definition: Config.h:208
std::string DwoPath
The path to write a .dwo file to.
Definition: Config.h:89
ModuleHookFn PostImportModuleHook
This hook is called after importing from other modules (ThinLTO-specific).
Definition: Config.h:150
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
ModuleHookFn PostOptModuleHook
This module hook is called after optimization is complete.
Definition: Config.h:153
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:55
A derived class of LLVMContext that initializes itself according to a given Config object...
Definition: Config.h:206
bool Freestanding
Flag to indicate that the optimizer should not assume builtins are present on the target...
Definition: Config.h:54
CodeGenFileType
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit...
ModuleHookFn PostPromoteModuleHook
This hook is called after promoting any internal functions (ThinLTO-specific).
Definition: Config.h:144
std::function< void(const DiagnosticInfo &)> DiagnosticHandlerFunction