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