LLVM 23.0.0git
Config.h
Go to the documentation of this file.
1//===-Config.h - LLVM Link Time Optimizer Configuration ---------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the lto::Config data structure, which allows clients to
10// configure LTO.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LTO_CONFIG_H
15#define LLVM_LTO_CONFIG_H
16
17#include "llvm/ADT/DenseSet.h"
18#include "llvm/Config/llvm-config.h"
20#include "llvm/IR/GlobalValue.h"
21#include "llvm/IR/LLVMContext.h"
27
28#include <functional>
29#include <optional>
30
31namespace llvm {
32
33class Error;
34class Module;
37class PassPlugin;
38
39namespace lto {
40
41/// LTO configuration. A linker can configure LTO by setting fields in this data
42/// structure and passing it to the lto::LTO constructor.
43struct Config {
48 // Note: when adding fields here, consider whether they need to be added to
49 // computeLTOCacheKey in LTO.cpp.
50 std::string CPU;
52 std::vector<std::string> MAttrs;
53 std::vector<std::string> MllvmArgs;
54 // LTO will register both lists of plugins, but
55 // if an LTO client has already loaded a set of plugins,
56 // they should register them via LoadedPassPlugins.
57 // LoadedPassPlugins is currently used by distributed thin-lto.
58 std::vector<llvm::PassPlugin *> LoadedPassPlugins;
59 std::vector<std::string> PassPluginFilenames;
60 /// For adding passes that run right before codegen.
62 std::optional<Reloc::Model> RelocModel = Reloc::PIC_;
63 std::optional<CodeModel::Model> CodeModel;
66 unsigned OptLevel = 2;
67 bool VerifyEach = false;
68 bool DisableVerify = false;
69
70 /// Flag to indicate that the optimizer should not assume builtins are present
71 /// on the target.
72 bool Freestanding = false;
73
74 /// Disable entirely the optimizer, including importing for ThinLTO
75 bool CodeGenOnly = false;
76
77 /// Run PGO context sensitive IR instrumentation.
78 bool RunCSIRInstr = false;
79
80 /// Turn on/off the warning about a hash mismatch in the PGO profile data.
81 bool PGOWarnMismatch = true;
82
83 /// Asserts whether we can assume whole program visibility during the LTO
84 /// link.
86
87 /// We're validating that all native vtables have corresponding type infos.
89 /// If all native vtables have corresponding type infos, allow
90 /// usage of RTTI to block devirtualization on types used in native files.
92
93 /// Always emit a Regular LTO object even when it is empty because no Regular
94 /// LTO modules were linked. This option is useful for some build system which
95 /// want to know a priori all possible output files.
97
98 /// If true, the LTO instance creates copies of the symbol names for LTO::run.
99 /// The lld linker uses string saver to keep symbol names alive and doesn't
100 /// need to create copies, so it can set this field to false.
102
103 /// This flag is used as one of parameters to calculate cache entries and to
104 /// ensure that in-process cache and out-of-process (DTLTO) cache are
105 /// distinguished.
106 mutable bool Dtlto = 0;
107
108 /// Allows non-imported definitions to get the potentially more constraining
109 /// visibility from the prevailing definition. FromPrevailing is the default
110 /// because it works for many binary formats. ELF can use the more optimized
111 /// 'ELF' scheme.
113
114 /// If this field is set, the set of passes run in the middle-end optimizer
115 /// will be the one specified by the string. Only works with the new pass
116 /// manager as the old one doesn't have this ability.
117 std::string OptPipeline;
118
119 // If this field is set, it has the same effect of specifying an AA pipeline
120 // identified by the string. Only works with the new pass manager, in
121 // conjunction OptPipeline.
122 std::string AAPipeline;
123
124 /// Setting this field will replace target triples in input files with this
125 /// triple.
126 std::string OverrideTriple;
127
128 /// Setting this field will replace unspecified target triples in input files
129 /// with this triple.
130 std::string DefaultTriple;
131
132 /// Context Sensitive PGO profile path.
133 std::string CSIRProfile;
134
135 /// Sample PGO profile path.
136 std::string SampleProfile;
137
138 /// Name remapping file for profile data.
139 std::string ProfileRemapping;
140
141 /// The directory to store .dwo files.
142 std::string DwoDir;
143
144 /// The name for the split debug info file used for the DW_AT_[GNU_]dwo_name
145 /// attribute in the skeleton CU. This should generally only be used when
146 /// running an individual backend directly via thinBackend(), as otherwise
147 /// all objects would use the same .dwo file. Not used as output path.
148 std::string SplitDwarfFile;
149
150 /// The path to write a .dwo file to. This should generally only be used when
151 /// running an individual backend directly via thinBackend(), as otherwise
152 /// all .dwo files will be written to the same path. Not used in skeleton CU.
153 std::string SplitDwarfOutput;
154
155 /// Optimization remarks file path.
156 std::string RemarksFilename;
157
158 /// Optimization remarks pass filter.
159 std::string RemarksPasses;
160
161 /// Whether to emit optimization remarks with hotness informations.
162 bool RemarksWithHotness = false;
163
164 /// The minimum hotness value a diagnostic needs in order to be included in
165 /// optimization diagnostics.
166 ///
167 /// The threshold is an Optional value, which maps to one of the 3 states:
168 /// 1. 0 => threshold disabled. All emarks will be printed.
169 /// 2. positive int => manual threshold by user. Remarks with hotness exceed
170 /// threshold will be printed.
171 /// 3. None => 'auto' threshold by user. The actual value is not
172 /// available at command line, but will be synced with
173 /// hotness threhold from profile summary during
174 /// compilation.
175 ///
176 /// If threshold option is not specified, it is disabled by default.
177 std::optional<uint64_t> RemarksHotnessThreshold = 0;
178
179 /// The format used for serializing remarks (default: YAML).
180 std::string RemarksFormat;
181
182 /// Whether to emit the pass manager debuggging informations.
183 bool DebugPassManager = false;
184
185 /// Statistics output file path.
186 std::string StatsFile;
187
188 /// Specific thinLTO modules to compile.
189 std::vector<std::string> ThinLTOModulesToCompile;
190
191 /// Time trace enabled.
192 bool TimeTraceEnabled = false;
193
194 /// Time trace granularity.
195 unsigned TimeTraceGranularity = 500;
196
199
200 /// Add FSAFDO discriminators.
201 bool AddFSDiscriminator = false;
202
203 /// If this field is set, LTO will write input file paths and symbol
204 /// resolutions here in llvm-lto2 command line flag format. This can be
205 /// used for testing and for running the LTO pipeline outside of the linker
206 /// with llvm-lto2.
207 std::unique_ptr<raw_ostream> ResolutionFile;
208
209 /// Tunable parameters for passes in the default pipelines.
211
212 /// The following callbacks deal with tasks, which normally represent the
213 /// entire optimization and code generation pipeline for what will become a
214 /// single native object file. Each task has a unique identifier between 0 and
215 /// getMaxTasks()-1, which is supplied to the callback via the Task parameter.
216 /// A task represents the entire pipeline for ThinLTO and regular
217 /// (non-parallel) LTO, but a parallel code generation task will be split into
218 /// N tasks before code generation, where N is the parallelism level.
219 ///
220 /// LTO may decide to stop processing a task at any time, for example if the
221 /// module is empty or if a module hook (see below) returns false. For this
222 /// reason, the client should not expect to receive exactly getMaxTasks()
223 /// native object files.
224
225 /// A module hook may be used by a linker to perform actions during the LTO
226 /// pipeline. For example, a linker may use this function to implement
227 /// -save-temps. If this function returns false, any further processing for
228 /// that task is aborted.
229 ///
230 /// Module hooks must be thread safe with respect to the linker's internal
231 /// data structures. A module hook will never be called concurrently from
232 /// multiple threads with the same task ID, or the same module.
233 ///
234 /// Note that in out-of-process backend scenarios, none of the hooks will be
235 /// called for ThinLTO tasks.
236 using ModuleHookFn = std::function<bool(unsigned Task, const Module &)>;
237
238 /// This module hook is called after linking (regular LTO) or loading
239 /// (ThinLTO) the module, before modifying it.
241
242 /// This hook is called after promoting any internal functions
243 /// (ThinLTO-specific).
245
246 /// This hook is called after internalizing the module.
248
249 /// This hook is called after importing from other modules (ThinLTO-specific).
251
252 /// This module hook is called after optimization is complete.
254
255 /// This module hook is called before code generation. It is similar to the
256 /// PostOptModuleHook, but for parallel code generation it is called after
257 /// splitting the module.
259
260 /// A combined index hook is called after all per-module indexes have been
261 /// combined (ThinLTO-specific). It can be used to implement -save-temps for
262 /// the combined index.
263 ///
264 /// If this function returns false, any further processing for ThinLTO tasks
265 /// is aborted.
266 ///
267 /// It is called regardless of whether the backend is in-process, although it
268 /// is not called from individual backend processes.
269 using CombinedIndexHookFn = std::function<bool(
270 const ModuleSummaryIndex &Index,
271 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols)>;
273
274 /// This is a convenience function that configures this Config object to write
275 /// temporary files named after the given OutputFileName for each of the LTO
276 /// phases to disk. A client can use this function to implement -save-temps.
277 ///
278 /// FIXME: Temporary files derived from ThinLTO backends are currently named
279 /// after the input file name, rather than the output file name, when
280 /// UseInputModulePath is set to true.
281 ///
282 /// Specifically, it (1) sets each of the above module hooks and the combined
283 /// index hook to a function that calls the hook function (if any) that was
284 /// present in the appropriate field when the addSaveTemps function was
285 /// called, and writes the module to a bitcode file with a name prefixed by
286 /// the given output file name, and (2) creates a resolution file whose name
287 /// is prefixed by the given output file name and sets ResolutionFile to its
288 /// file handle.
289 ///
290 /// SaveTempsArgs can be specified to select which temps to save.
291 /// If SaveTempsArgs is not provided, all temps are saved.
292 LLVM_ABI Error addSaveTemps(std::string OutputFileName,
293 bool UseInputModulePath = false,
294 const DenseSet<StringRef> &SaveTempsArgs = {});
295};
296
300 : Fn(DiagHandlerFn) {}
301 bool handleDiagnostics(const DiagnosticInfo &DI) override {
302 (*Fn)(DI);
303 return true;
304 }
305};
306/// A derived class of LLVMContext that initializes itself according to a given
307/// Config object. The purpose of this class is to tie ownership of the
308/// diagnostic handler to the context, as opposed to the Config object (which
309/// may be ephemeral).
310// FIXME: This should not be required as diagnostic handler is not callback.
312
314 setDiscardValueNames(C.ShouldDiscardValueNames);
317 std::make_unique<LTOLLVMDiagnosticHandler>(&DiagHandler), true);
318 }
320};
321
322}
323}
324
325#endif
#define LLVM_ABI
Definition Compiler.h:213
This file defines the DenseSet and SmallDenseSet classes.
Implements a dense probed hash-table based set.
Definition DenseSet.h:279
This is the base abstract class for diagnostic reporting in the backend.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
LLVM_ABI void enableDebugTypeODRUniquing()
LLVM_ABI LLVMContext()
LLVM_ABI void setDiscardValueNames(bool Discard)
Set the Context runtime configuration to discard all value name (but GlobalValue).
LLVM_ABI void setDiagnosticHandler(std::unique_ptr< DiagnosticHandler > &&DH, bool RespectFilters=false)
setDiagnosticHandler - This method sets unique_ptr to object of DiagnosticHandler to provide custom d...
Class to hold module path string table and global value map, and encapsulate methods for operating on...
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A loaded pass plugin.
Definition PassPlugin.h:71
Tunable parameters for passes in the default pipelines.
Definition PassBuilder.h:41
PassManager manages ModulePassManagers.
An abstract base class for streams implementations that also support a pwrite operation.
Interfaces for registering analysis passes, producing common pass manager configurations,...
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
std::function< void(const DiagnosticInfo &)> DiagnosticHandlerFunction
CodeGenFileType
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit,...
Definition CodeGen.h:111
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:82
@ Default
-O2, -Os, -Oz
Definition CodeGen.h:85
DiagnosticHandler(void *DiagContext=nullptr)
LTO configuration.
Definition Config.h:43
DiagnosticHandlerFunction DiagHandler
Definition Config.h:198
bool HasWholeProgramVisibility
Asserts whether we can assume whole program visibility during the LTO link.
Definition Config.h:85
bool ValidateAllVtablesHaveTypeInfos
We're validating that all native vtables have corresponding type infos.
Definition Config.h:88
bool KeepSymbolNameCopies
If true, the LTO instance creates copies of the symbol names for LTO::run.
Definition Config.h:101
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:236
bool DebugPassManager
Whether to emit the pass manager debuggging informations.
Definition Config.h:183
bool AddFSDiscriminator
Add FSAFDO discriminators.
Definition Config.h:201
std::optional< uint64_t > RemarksHotnessThreshold
The minimum hotness value a diagnostic needs in order to be included in optimization diagnostics.
Definition Config.h:177
LLVM_ABI Error addSaveTemps(std::string OutputFileName, bool UseInputModulePath=false, const DenseSet< StringRef > &SaveTempsArgs={})
This is a convenience function that configures this Config object to write temporary files named afte...
std::string StatsFile
Statistics output file path.
Definition Config.h:186
ModuleHookFn PreOptModuleHook
This module hook is called after linking (regular LTO) or loading (ThinLTO) the module,...
Definition Config.h:240
CombinedIndexHookFn CombinedIndexHook
Definition Config.h:272
std::optional< CodeModel::Model > CodeModel
Definition Config.h:63
std::string AAPipeline
Definition Config.h:122
std::function< void(legacy::PassManager &)> PreCodeGenPassesHook
For adding passes that run right before codegen.
Definition Config.h:61
bool CodeGenOnly
Disable entirely the optimizer, including importing for ThinLTO.
Definition Config.h:75
bool DisableVerify
Definition Config.h:68
std::vector< std::string > MAttrs
Definition Config.h:52
std::vector< std::string > MllvmArgs
Definition Config.h:53
std::vector< std::string > ThinLTOModulesToCompile
Specific thinLTO modules to compile.
Definition Config.h:189
CodeGenOptLevel CGOptLevel
Definition Config.h:64
PipelineTuningOptions PTO
Tunable parameters for passes in the default pipelines.
Definition Config.h:210
std::vector< llvm::PassPlugin * > LoadedPassPlugins
Definition Config.h:58
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:207
bool Dtlto
This flag is used as one of parameters to calculate cache entries and to ensure that in-process cache...
Definition Config.h:106
std::string DefaultTriple
Setting this field will replace unspecified target triples in input files with this triple.
Definition Config.h:130
bool AlwaysEmitRegularLTOObj
Always emit a Regular LTO object even when it is empty because no Regular LTO modules were linked.
Definition Config.h:96
std::string CPU
Definition Config.h:50
std::string DwoDir
The directory to store .dwo files.
Definition Config.h:142
std::string RemarksFilename
Optimization remarks file path.
Definition Config.h:156
VisScheme VisibilityScheme
Allows non-imported definitions to get the potentially more constraining visibility from the prevaili...
Definition Config.h:112
std::vector< std::string > PassPluginFilenames
Definition Config.h:59
ModuleHookFn PostPromoteModuleHook
This hook is called after promoting any internal functions (ThinLTO-specific).
Definition Config.h:244
std::string OverrideTriple
Setting this field will replace target triples in input files with this triple.
Definition Config.h:126
std::string ProfileRemapping
Name remapping file for profile data.
Definition Config.h:139
bool AllVtablesHaveTypeInfos
If all native vtables have corresponding type infos, allow usage of RTTI to block devirtualization on...
Definition Config.h:91
TargetOptions Options
Definition Config.h:51
std::string SplitDwarfFile
The name for the split debug info file used for the DW_AT_[GNU_]dwo_name attribute in the skeleton CU...
Definition Config.h:148
std::string SplitDwarfOutput
The path to write a .dwo file to.
Definition Config.h:153
bool TimeTraceEnabled
Time trace enabled.
Definition Config.h:192
ModuleHookFn PostOptModuleHook
This module hook is called after optimization is complete.
Definition Config.h:253
std::string RemarksPasses
Optimization remarks pass filter.
Definition Config.h:159
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:117
bool RunCSIRInstr
Run PGO context sensitive IR instrumentation.
Definition Config.h:78
ModuleHookFn PostInternalizeModuleHook
This hook is called after internalizing the module.
Definition Config.h:247
unsigned TimeTraceGranularity
Time trace granularity.
Definition Config.h:195
unsigned OptLevel
Definition Config.h:66
ModuleHookFn PostImportModuleHook
This hook is called after importing from other modules (ThinLTO-specific).
Definition Config.h:250
bool RemarksWithHotness
Whether to emit optimization remarks with hotness informations.
Definition Config.h:162
std::function< bool( const ModuleSummaryIndex &Index, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols)> CombinedIndexHookFn
A combined index hook is called after all per-module indexes have been combined (ThinLTO-specific).
Definition Config.h:269
std::string CSIRProfile
Context Sensitive PGO profile path.
Definition Config.h:133
ModuleHookFn PreCodeGenModuleHook
This module hook is called before code generation.
Definition Config.h:258
std::optional< Reloc::Model > RelocModel
Definition Config.h:62
bool ShouldDiscardValueNames
Definition Config.h:197
bool PGOWarnMismatch
Turn on/off the warning about a hash mismatch in the PGO profile data.
Definition Config.h:81
CodeGenFileType CGFileType
Definition Config.h:65
bool Freestanding
Flag to indicate that the optimizer should not assume builtins are present on the target.
Definition Config.h:72
std::string SampleProfile
Sample PGO profile path.
Definition Config.h:136
std::string RemarksFormat
The format used for serializing remarks (default: YAML).
Definition Config.h:180
DiagnosticHandlerFunction DiagHandler
Definition Config.h:319
LTOLLVMContext(const Config &C)
Definition Config.h:313
LTOLLVMDiagnosticHandler(DiagnosticHandlerFunction *DiagHandlerFn)
Definition Config.h:299
DiagnosticHandlerFunction * Fn
Definition Config.h:298
bool handleDiagnostics(const DiagnosticInfo &DI) override
Override handleDiagnostics to provide custom implementation.
Definition Config.h:301