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