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