Config.h revision 327952
1255376Sdes//===-Config.h - LLVM Link Time Optimizer Configuration -------------------===//
2255376Sdes//
3271947Sdes//                     The LLVM Compiler Infrastructure
491094Sdes//
591094Sdes// This file is distributed under the University of Illinois Open Source
691094Sdes// License. See LICENSE.TXT for details.
791094Sdes//
891094Sdes//===----------------------------------------------------------------------===//
991094Sdes//
1091094Sdes// This file defines the lto::Config data structure, which allows clients to
1191094Sdes// configure LTO.
12108794Sdes//
1391094Sdes//===----------------------------------------------------------------------===//
14117610Sdes
15174832Sdes#ifndef LLVM_LTO_CONFIG_H
1691094Sdes#define LLVM_LTO_CONFIG_H
1791094Sdes
18236109Sdes#include "llvm/IR/DiagnosticInfo.h"
1991100Sdes#include "llvm/Support/CodeGen.h"
20141098Sdes#include "llvm/Target/TargetMachine.h"
2191100Sdes#include "llvm/Target/TargetOptions.h"
22141098Sdes
2391100Sdes#include <functional>
2491100Sdes
2591100Sdesnamespace llvm {
2691100Sdes
2791100Sdesclass Error;
2891100Sdesclass Module;
29141098Sdesclass ModuleSummaryIndex;
3091100Sdesclass raw_pwrite_stream;
3191100Sdes
3291100Sdesnamespace lto {
3391100Sdes
3491100Sdes/// LTO configuration. A linker can configure LTO by setting fields in this data
3591100Sdes/// structure and passing it to the lto::LTO constructor.
3691100Sdesstruct Config {
3791100Sdes  // Note: when adding fields here, consider whether they need to be added to
3891100Sdes  // computeCacheKey in LTO.cpp.
3991100Sdes  std::string CPU;
4091100Sdes  TargetOptions Options;
4191100Sdes  std::vector<std::string> MAttrs;
4291100Sdes  Optional<Reloc::Model> RelocModel = Reloc::PIC_;
4391100Sdes  Optional<CodeModel::Model> CodeModel = None;
4491100Sdes  CodeGenOpt::Level CGOptLevel = CodeGenOpt::Default;
4599158Sdes  TargetMachine::CodeGenFileType CGFileType = TargetMachine::CGFT_ObjectFile;
4691100Sdes  unsigned OptLevel = 2;
4791100Sdes  bool DisableVerify = false;
4891100Sdes
4991100Sdes  /// Use the new pass manager
50141098Sdes  bool UseNewPM = false;
5191100Sdes
5291100Sdes  /// Disable entirely the optimizer, including importing for ThinLTO
5391100Sdes  bool CodeGenOnly = false;
5491100Sdes
5591100Sdes  /// If this field is set, the set of passes run in the middle-end optimizer
5691100Sdes  /// will be the one specified by the string. Only works with the new pass
5791100Sdes  /// manager as the old one doesn't have this ability.
5891100Sdes  std::string OptPipeline;
5991100Sdes
6091094Sdes  // If this field is set, it has the same effect of specifying an AA pipeline
6191094Sdes  // identified by the string. Only works with the new pass manager, in
62236109Sdes  // conjunction OptPipeline.
6391094Sdes  std::string AAPipeline;
6491100Sdes
6591100Sdes  /// Setting this field will replace target triples in input files with this
6691100Sdes  /// triple.
6791100Sdes  std::string OverrideTriple;
6891100Sdes
6991100Sdes  /// Setting this field will replace unspecified target triples in input files
7091100Sdes  /// with this triple.
7191094Sdes  std::string DefaultTriple;
7291094Sdes
7391100Sdes  /// Sample PGO profile path.
7491100Sdes  std::string SampleProfile;
7591100Sdes
7691100Sdes  /// Optimization remarks file path.
7791094Sdes  std::string RemarksFilename = "";
7891100Sdes
7991100Sdes  /// Whether to emit optimization remarks with hotness informations.
8091100Sdes  bool RemarksWithHotness = false;
8191094Sdes
8291100Sdes  /// Whether to emit the pass manager debuggging informations.
83236109Sdes  bool DebugPassManager = false;
8491100Sdes
8591094Sdes  bool ShouldDiscardValueNames = true;
8691094Sdes  DiagnosticHandlerFunction DiagHandler;
87236109Sdes
88236109Sdes  /// If this field is set, LTO will write input file paths and symbol
89236109Sdes  /// resolutions here in llvm-lto2 command line flag format. This can be
90117610Sdes  /// used for testing and for running the LTO pipeline outside of the linker
91236109Sdes  /// with llvm-lto2.
92147466Sdes  std::unique_ptr<raw_ostream> ResolutionFile;
93117610Sdes
9491094Sdes  /// The following callbacks deal with tasks, which normally represent the
9591094Sdes  /// entire optimization and code generation pipeline for what will become a
96255376Sdes  /// single native object file. Each task has a unique identifier between 0 and
97255376Sdes  /// getMaxTasks()-1, which is supplied to the callback via the Task parameter.
98255376Sdes  /// A task represents the entire pipeline for ThinLTO and regular
99  /// (non-parallel) LTO, but a parallel code generation task will be split into
100  /// N tasks before code generation, where N is the parallelism level.
101  ///
102  /// LTO may decide to stop processing a task at any time, for example if the
103  /// module is empty or if a module hook (see below) returns false. For this
104  /// reason, the client should not expect to receive exactly getMaxTasks()
105  /// native object files.
106
107  /// A module hook may be used by a linker to perform actions during the LTO
108  /// pipeline. For example, a linker may use this function to implement
109  /// -save-temps. If this function returns false, any further processing for
110  /// that task is aborted.
111  ///
112  /// Module hooks must be thread safe with respect to the linker's internal
113  /// data structures. A module hook will never be called concurrently from
114  /// multiple threads with the same task ID, or the same module.
115  ///
116  /// Note that in out-of-process backend scenarios, none of the hooks will be
117  /// called for ThinLTO tasks.
118  typedef std::function<bool(unsigned Task, const Module &)> ModuleHookFn;
119
120  /// This module hook is called after linking (regular LTO) or loading
121  /// (ThinLTO) the module, before modifying it.
122  ModuleHookFn PreOptModuleHook;
123
124  /// This hook is called after promoting any internal functions
125  /// (ThinLTO-specific).
126  ModuleHookFn PostPromoteModuleHook;
127
128  /// This hook is called after internalizing the module.
129  ModuleHookFn PostInternalizeModuleHook;
130
131  /// This hook is called after importing from other modules (ThinLTO-specific).
132  ModuleHookFn PostImportModuleHook;
133
134  /// This module hook is called after optimization is complete.
135  ModuleHookFn PostOptModuleHook;
136
137  /// This module hook is called before code generation. It is similar to the
138  /// PostOptModuleHook, but for parallel code generation it is called after
139  /// splitting the module.
140  ModuleHookFn PreCodeGenModuleHook;
141
142  /// A combined index hook is called after all per-module indexes have been
143  /// combined (ThinLTO-specific). It can be used to implement -save-temps for
144  /// the combined index.
145  ///
146  /// If this function returns false, any further processing for ThinLTO tasks
147  /// is aborted.
148  ///
149  /// It is called regardless of whether the backend is in-process, although it
150  /// is not called from individual backend processes.
151  typedef std::function<bool(const ModuleSummaryIndex &Index)>
152      CombinedIndexHookFn;
153  CombinedIndexHookFn CombinedIndexHook;
154
155  /// This is a convenience function that configures this Config object to write
156  /// temporary files named after the given OutputFileName for each of the LTO
157  /// phases to disk. A client can use this function to implement -save-temps.
158  ///
159  /// FIXME: Temporary files derived from ThinLTO backends are currently named
160  /// after the input file name, rather than the output file name, when
161  /// UseInputModulePath is set to true.
162  ///
163  /// Specifically, it (1) sets each of the above module hooks and the combined
164  /// index hook to a function that calls the hook function (if any) that was
165  /// present in the appropriate field when the addSaveTemps function was
166  /// called, and writes the module to a bitcode file with a name prefixed by
167  /// the given output file name, and (2) creates a resolution file whose name
168  /// is prefixed by the given output file name and sets ResolutionFile to its
169  /// file handle.
170  Error addSaveTemps(std::string OutputFileName,
171                     bool UseInputModulePath = false);
172};
173
174struct LTOLLVMDiagnosticHandler : public DiagnosticHandler {
175  DiagnosticHandlerFunction *Fn;
176  LTOLLVMDiagnosticHandler(DiagnosticHandlerFunction *DiagHandlerFn)
177      : Fn(DiagHandlerFn) {}
178  bool handleDiagnostics(const DiagnosticInfo &DI) override {
179    (*Fn)(DI);
180    return true;
181  }
182};
183/// A derived class of LLVMContext that initializes itself according to a given
184/// Config object. The purpose of this class is to tie ownership of the
185/// diagnostic handler to the context, as opposed to the Config object (which
186/// may be ephemeral).
187// FIXME: This should not be required as diagnostic handler is not callback.
188struct LTOLLVMContext : LLVMContext {
189
190  LTOLLVMContext(const Config &C) : DiagHandler(C.DiagHandler) {
191    setDiscardValueNames(C.ShouldDiscardValueNames);
192    enableDebugTypeODRUniquing();
193    setDiagnosticHandler(
194        llvm::make_unique<LTOLLVMDiagnosticHandler>(&DiagHandler), true);
195  }
196  DiagnosticHandlerFunction DiagHandler;
197};
198
199}
200}
201
202#endif
203