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