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