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