Config.h revision 314564
1//===-Config.h - LLVM Link Time Optimizer Configuration -------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the lto::Config data structure, which allows clients to 11// configure LTO. 12// 13//===----------------------------------------------------------------------===// 14 15#ifndef LLVM_LTO_CONFIG_H 16#define LLVM_LTO_CONFIG_H 17 18#include "llvm/IR/DiagnosticInfo.h" 19#include "llvm/Support/CodeGen.h" 20#include "llvm/Target/TargetOptions.h" 21 22#include <functional> 23 24namespace llvm { 25 26class Error; 27class Module; 28class ModuleSummaryIndex; 29class raw_pwrite_stream; 30 31namespace 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. 35struct 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 Reloc::Model RelocModel = Reloc::PIC_; 42 CodeModel::Model CodeModel = CodeModel::Default; 43 CodeGenOpt::Level CGOptLevel = CodeGenOpt::Default; 44 unsigned OptLevel = 2; 45 bool DisableVerify = false; 46 47 /// Disable entirely the optimizer, including importing for ThinLTO 48 bool CodeGenOnly = false; 49 50 /// If this field is set, the set of passes run in the middle-end optimizer 51 /// will be the one specified by the string. Only works with the new pass 52 /// manager as the old one doesn't have this ability. 53 std::string OptPipeline; 54 55 // If this field is set, it has the same effect of specifying an AA pipeline 56 // identified by the string. Only works with the new pass manager, in 57 // conjunction OptPipeline. 58 std::string AAPipeline; 59 60 /// Setting this field will replace target triples in input files with this 61 /// triple. 62 std::string OverrideTriple; 63 64 /// Setting this field will replace unspecified target triples in input files 65 /// with this triple. 66 std::string DefaultTriple; 67 68 /// Sample PGO profile path. 69 std::string SampleProfile; 70 71 bool ShouldDiscardValueNames = true; 72 DiagnosticHandlerFunction DiagHandler; 73 74 /// If this field is set, LTO will write input file paths and symbol 75 /// resolutions here in llvm-lto2 command line flag format. This can be 76 /// used for testing and for running the LTO pipeline outside of the linker 77 /// with llvm-lto2. 78 std::unique_ptr<raw_ostream> ResolutionFile; 79 80 /// The following callbacks deal with tasks, which normally represent the 81 /// entire optimization and code generation pipeline for what will become a 82 /// single native object file. Each task has a unique identifier between 0 and 83 /// getMaxTasks()-1, which is supplied to the callback via the Task parameter. 84 /// A task represents the entire pipeline for ThinLTO and regular 85 /// (non-parallel) LTO, but a parallel code generation task will be split into 86 /// N tasks before code generation, where N is the parallelism level. 87 /// 88 /// LTO may decide to stop processing a task at any time, for example if the 89 /// module is empty or if a module hook (see below) returns false. For this 90 /// reason, the client should not expect to receive exactly getMaxTasks() 91 /// native object files. 92 93 /// A module hook may be used by a linker to perform actions during the LTO 94 /// pipeline. For example, a linker may use this function to implement 95 /// -save-temps. If this function returns false, any further processing for 96 /// that task is aborted. 97 /// 98 /// Module hooks must be thread safe with respect to the linker's internal 99 /// data structures. A module hook will never be called concurrently from 100 /// multiple threads with the same task ID, or the same module. 101 /// 102 /// Note that in out-of-process backend scenarios, none of the hooks will be 103 /// called for ThinLTO tasks. 104 typedef std::function<bool(unsigned Task, const Module &)> ModuleHookFn; 105 106 /// This module hook is called after linking (regular LTO) or loading 107 /// (ThinLTO) the module, before modifying it. 108 ModuleHookFn PreOptModuleHook; 109 110 /// This hook is called after promoting any internal functions 111 /// (ThinLTO-specific). 112 ModuleHookFn PostPromoteModuleHook; 113 114 /// This hook is called after internalizing the module. 115 ModuleHookFn PostInternalizeModuleHook; 116 117 /// This hook is called after importing from other modules (ThinLTO-specific). 118 ModuleHookFn PostImportModuleHook; 119 120 /// This module hook is called after optimization is complete. 121 ModuleHookFn PostOptModuleHook; 122 123 /// This module hook is called before code generation. It is similar to the 124 /// PostOptModuleHook, but for parallel code generation it is called after 125 /// splitting the module. 126 ModuleHookFn PreCodeGenModuleHook; 127 128 /// A combined index hook is called after all per-module indexes have been 129 /// combined (ThinLTO-specific). It can be used to implement -save-temps for 130 /// the combined index. 131 /// 132 /// If this function returns false, any further processing for ThinLTO tasks 133 /// is aborted. 134 /// 135 /// It is called regardless of whether the backend is in-process, although it 136 /// is not called from individual backend processes. 137 typedef std::function<bool(const ModuleSummaryIndex &Index)> 138 CombinedIndexHookFn; 139 CombinedIndexHookFn CombinedIndexHook; 140 141 /// This is a convenience function that configures this Config object to write 142 /// temporary files named after the given OutputFileName for each of the LTO 143 /// phases to disk. A client can use this function to implement -save-temps. 144 /// 145 /// FIXME: Temporary files derived from ThinLTO backends are currently named 146 /// after the input file name, rather than the output file name, when 147 /// UseInputModulePath is set to true. 148 /// 149 /// Specifically, it (1) sets each of the above module hooks and the combined 150 /// index hook to a function that calls the hook function (if any) that was 151 /// present in the appropriate field when the addSaveTemps function was 152 /// called, and writes the module to a bitcode file with a name prefixed by 153 /// the given output file name, and (2) creates a resolution file whose name 154 /// is prefixed by the given output file name and sets ResolutionFile to its 155 /// file handle. 156 Error addSaveTemps(std::string OutputFileName, 157 bool UseInputModulePath = false); 158}; 159 160/// A derived class of LLVMContext that initializes itself according to a given 161/// Config object. The purpose of this class is to tie ownership of the 162/// diagnostic handler to the context, as opposed to the Config object (which 163/// may be ephemeral). 164struct LTOLLVMContext : LLVMContext { 165 static void funcDiagHandler(const DiagnosticInfo &DI, void *Context) { 166 auto *Fn = static_cast<DiagnosticHandlerFunction *>(Context); 167 (*Fn)(DI); 168 } 169 170 LTOLLVMContext(const Config &C) : DiagHandler(C.DiagHandler) { 171 setDiscardValueNames(C.ShouldDiscardValueNames); 172 enableDebugTypeODRUniquing(); 173 setDiagnosticHandler(funcDiagHandler, &DiagHandler, true); 174 } 175 DiagnosticHandlerFunction DiagHandler; 176}; 177 178} 179} 180 181#endif 182