Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 1 | //===-LTOBackend.cpp - LLVM Link Time Optimizer Backend -------------------===// |
| 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 implements the "backend" phase of LTO, i.e. it performs |
| 11 | // optimization and code generation on a loaded module. It is generally used |
| 12 | // internally by the LTO class but can also be used independently, for example |
| 13 | // to implement a standalone ThinLTO backend. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "llvm/LTO/LTOBackend.h" |
Davide Italiano | ec9612d | 2016-09-07 17:46:16 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/AliasAnalysis.h" |
| 19 | #include "llvm/Analysis/CGSCCPassManager.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 21 | #include "llvm/Analysis/TargetTransformInfo.h" |
Teresa Johnson | ad17679 | 2016-11-11 05:34:58 +0000 | [diff] [blame] | 22 | #include "llvm/Bitcode/BitcodeReader.h" |
| 23 | #include "llvm/Bitcode/BitcodeWriter.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 24 | #include "llvm/IR/LegacyPassManager.h" |
Davide Italiano | ec9612d | 2016-09-07 17:46:16 +0000 | [diff] [blame] | 25 | #include "llvm/IR/PassManager.h" |
| 26 | #include "llvm/IR/Verifier.h" |
Mehdi Amini | 970800e | 2016-08-17 06:23:09 +0000 | [diff] [blame] | 27 | #include "llvm/LTO/LTO.h" |
Davide Italiano | dc8e07b | 2016-09-16 16:05:25 +0000 | [diff] [blame] | 28 | #include "llvm/LTO/legacy/UpdateCompilerUsed.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 29 | #include "llvm/MC/SubtargetFeature.h" |
Davide Italiano | ec9612d | 2016-09-07 17:46:16 +0000 | [diff] [blame] | 30 | #include "llvm/Passes/PassBuilder.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Error.h" |
| 32 | #include "llvm/Support/FileSystem.h" |
| 33 | #include "llvm/Support/TargetRegistry.h" |
| 34 | #include "llvm/Support/ThreadPool.h" |
| 35 | #include "llvm/Target/TargetMachine.h" |
| 36 | #include "llvm/Transforms/IPO.h" |
| 37 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
Chandler Carruth | 3bab7e1 | 2017-01-11 09:43:56 +0000 | [diff] [blame] | 38 | #include "llvm/Transforms/Scalar/LoopPassManager.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 39 | #include "llvm/Transforms/Utils/FunctionImportUtils.h" |
| 40 | #include "llvm/Transforms/Utils/SplitModule.h" |
| 41 | |
| 42 | using namespace llvm; |
| 43 | using namespace lto; |
| 44 | |
Davide Italiano | 0dd200e | 2017-01-24 00:58:24 +0000 | [diff] [blame^] | 45 | static cl::opt<bool> |
| 46 | LTOUseNewPM("lto-use-new-pm", |
| 47 | cl::desc("Run LTO passes using the new pass manager"), |
| 48 | cl::init(false), cl::Hidden); |
| 49 | |
Benjamin Kramer | 4c2582a | 2016-10-18 19:39:31 +0000 | [diff] [blame] | 50 | LLVM_ATTRIBUTE_NORETURN static void reportOpenError(StringRef Path, Twine Msg) { |
Davide Italiano | a416d11 | 2016-09-17 22:32:42 +0000 | [diff] [blame] | 51 | errs() << "failed to open " << Path << ": " << Msg << '\n'; |
| 52 | errs().flush(); |
| 53 | exit(1); |
| 54 | } |
| 55 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 56 | Error Config::addSaveTemps(std::string OutputFileName, |
| 57 | bool UseInputModulePath) { |
| 58 | ShouldDiscardValueNames = false; |
| 59 | |
| 60 | std::error_code EC; |
| 61 | ResolutionFile = llvm::make_unique<raw_fd_ostream>( |
Mehdi Amini | eccffad | 2016-08-18 00:12:33 +0000 | [diff] [blame] | 62 | OutputFileName + "resolution.txt", EC, sys::fs::OpenFlags::F_Text); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 63 | if (EC) |
| 64 | return errorCodeToError(EC); |
| 65 | |
| 66 | auto setHook = [&](std::string PathSuffix, ModuleHookFn &Hook) { |
| 67 | // Keep track of the hook provided by the linker, which also needs to run. |
| 68 | ModuleHookFn LinkerHook = Hook; |
Mehdi Amini | f8c2f08 | 2016-08-22 16:17:40 +0000 | [diff] [blame] | 69 | Hook = [=](unsigned Task, const Module &M) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 70 | // If the linker's hook returned false, we need to pass that result |
| 71 | // through. |
| 72 | if (LinkerHook && !LinkerHook(Task, M)) |
| 73 | return false; |
| 74 | |
| 75 | std::string PathPrefix; |
| 76 | // If this is the combined module (not a ThinLTO backend compile) or the |
| 77 | // user hasn't requested using the input module's path, emit to a file |
| 78 | // named from the provided OutputFileName with the Task ID appended. |
| 79 | if (M.getModuleIdentifier() == "ld-temp.o" || !UseInputModulePath) { |
Mehdi Amini | eccffad | 2016-08-18 00:12:33 +0000 | [diff] [blame] | 80 | PathPrefix = OutputFileName + utostr(Task); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 81 | } else |
| 82 | PathPrefix = M.getModuleIdentifier(); |
| 83 | std::string Path = PathPrefix + "." + PathSuffix + ".bc"; |
| 84 | std::error_code EC; |
| 85 | raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None); |
Davide Italiano | a416d11 | 2016-09-17 22:32:42 +0000 | [diff] [blame] | 86 | // Because -save-temps is a debugging feature, we report the error |
| 87 | // directly and exit. |
| 88 | if (EC) |
| 89 | reportOpenError(Path, EC.message()); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 90 | WriteBitcodeToFile(&M, OS, /*ShouldPreserveUseListOrder=*/false); |
| 91 | return true; |
| 92 | }; |
| 93 | }; |
| 94 | |
| 95 | setHook("0.preopt", PreOptModuleHook); |
| 96 | setHook("1.promote", PostPromoteModuleHook); |
| 97 | setHook("2.internalize", PostInternalizeModuleHook); |
| 98 | setHook("3.import", PostImportModuleHook); |
| 99 | setHook("4.opt", PostOptModuleHook); |
| 100 | setHook("5.precodegen", PreCodeGenModuleHook); |
| 101 | |
| 102 | CombinedIndexHook = [=](const ModuleSummaryIndex &Index) { |
Mehdi Amini | eccffad | 2016-08-18 00:12:33 +0000 | [diff] [blame] | 103 | std::string Path = OutputFileName + "index.bc"; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 104 | std::error_code EC; |
| 105 | raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None); |
Davide Italiano | a416d11 | 2016-09-17 22:32:42 +0000 | [diff] [blame] | 106 | // Because -save-temps is a debugging feature, we report the error |
| 107 | // directly and exit. |
| 108 | if (EC) |
| 109 | reportOpenError(Path, EC.message()); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 110 | WriteIndexToFile(Index, OS); |
| 111 | return true; |
| 112 | }; |
| 113 | |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 114 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | namespace { |
| 118 | |
| 119 | std::unique_ptr<TargetMachine> |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 120 | createTargetMachine(Config &Conf, StringRef TheTriple, |
| 121 | const Target *TheTarget) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 122 | SubtargetFeatures Features; |
| 123 | Features.getDefaultSubtargetFeatures(Triple(TheTriple)); |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 124 | for (const std::string &A : Conf.MAttrs) |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 125 | Features.AddFeature(A); |
| 126 | |
| 127 | return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine( |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 128 | TheTriple, Conf.CPU, Features.getString(), Conf.Options, Conf.RelocModel, |
| 129 | Conf.CodeModel, Conf.CGOptLevel)); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Davide Italiano | 0dd200e | 2017-01-24 00:58:24 +0000 | [diff] [blame^] | 132 | static void runNewPMPasses(Module &Mod, TargetMachine *TM, unsigned OptLevel) { |
| 133 | PassBuilder PB(TM); |
| 134 | AAManager AA; |
| 135 | |
| 136 | // Parse a custom AA pipeline if asked to. |
| 137 | assert(PB.parseAAPipeline(AA, "default")); |
| 138 | |
| 139 | LoopAnalysisManager LAM; |
| 140 | FunctionAnalysisManager FAM; |
| 141 | CGSCCAnalysisManager CGAM; |
| 142 | ModuleAnalysisManager MAM; |
| 143 | |
| 144 | // Register the AA manager first so that our version is the one used. |
| 145 | FAM.registerPass([&] { return std::move(AA); }); |
| 146 | |
| 147 | // Register all the basic analyses with the managers. |
| 148 | PB.registerModuleAnalyses(MAM); |
| 149 | PB.registerCGSCCAnalyses(CGAM); |
| 150 | PB.registerFunctionAnalyses(FAM); |
| 151 | PB.registerLoopAnalyses(LAM); |
| 152 | PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); |
| 153 | |
| 154 | ModulePassManager MPM; |
| 155 | // FIXME (davide): verify the input. |
| 156 | |
| 157 | PassBuilder::OptimizationLevel OL; |
| 158 | |
| 159 | switch (OptLevel) { |
| 160 | default: |
| 161 | llvm_unreachable("Invalid optimization level"); |
| 162 | case 0: |
| 163 | OL = PassBuilder::O0; |
| 164 | break; |
| 165 | case 1: |
| 166 | OL = PassBuilder::O1; |
| 167 | break; |
| 168 | case 2: |
| 169 | OL = PassBuilder::O2; |
| 170 | break; |
| 171 | case 3: |
| 172 | OL = PassBuilder::O3; |
| 173 | break; |
| 174 | } |
| 175 | |
| 176 | MPM = PB.buildLTODefaultPipeline(OL, false /* DebugLogging */); |
| 177 | MPM.run(Mod, MAM); |
| 178 | |
| 179 | // FIXME (davide): verify the output. |
| 180 | } |
| 181 | |
Davide Italiano | ec9612d | 2016-09-07 17:46:16 +0000 | [diff] [blame] | 182 | static void runNewPMCustomPasses(Module &Mod, TargetMachine *TM, |
| 183 | std::string PipelineDesc, |
Davide Italiano | 14e9e8a | 2016-09-16 21:03:21 +0000 | [diff] [blame] | 184 | std::string AAPipelineDesc, |
Davide Italiano | ec9612d | 2016-09-07 17:46:16 +0000 | [diff] [blame] | 185 | bool DisableVerify) { |
| 186 | PassBuilder PB(TM); |
| 187 | AAManager AA; |
Davide Italiano | 14e9e8a | 2016-09-16 21:03:21 +0000 | [diff] [blame] | 188 | |
| 189 | // Parse a custom AA pipeline if asked to. |
| 190 | if (!AAPipelineDesc.empty()) |
| 191 | if (!PB.parseAAPipeline(AA, AAPipelineDesc)) |
| 192 | report_fatal_error("unable to parse AA pipeline description: " + |
| 193 | AAPipelineDesc); |
| 194 | |
Davide Italiano | ec9612d | 2016-09-07 17:46:16 +0000 | [diff] [blame] | 195 | LoopAnalysisManager LAM; |
| 196 | FunctionAnalysisManager FAM; |
| 197 | CGSCCAnalysisManager CGAM; |
| 198 | ModuleAnalysisManager MAM; |
| 199 | |
| 200 | // Register the AA manager first so that our version is the one used. |
| 201 | FAM.registerPass([&] { return std::move(AA); }); |
| 202 | |
| 203 | // Register all the basic analyses with the managers. |
| 204 | PB.registerModuleAnalyses(MAM); |
| 205 | PB.registerCGSCCAnalyses(CGAM); |
| 206 | PB.registerFunctionAnalyses(FAM); |
| 207 | PB.registerLoopAnalyses(LAM); |
| 208 | PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); |
| 209 | |
| 210 | ModulePassManager MPM; |
| 211 | |
| 212 | // Always verify the input. |
| 213 | MPM.addPass(VerifierPass()); |
| 214 | |
| 215 | // Now, add all the passes we've been requested to. |
| 216 | if (!PB.parsePassPipeline(MPM, PipelineDesc)) |
| 217 | report_fatal_error("unable to parse pass pipeline description: " + |
| 218 | PipelineDesc); |
| 219 | |
| 220 | if (!DisableVerify) |
| 221 | MPM.addPass(VerifierPass()); |
| 222 | MPM.run(Mod, MAM); |
| 223 | } |
| 224 | |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 225 | static void runOldPMPasses(Config &Conf, Module &Mod, TargetMachine *TM, |
Peter Collingbourne | e02b74e | 2017-01-20 22:18:52 +0000 | [diff] [blame] | 226 | bool IsThinLTO, ModuleSummaryIndex &CombinedIndex) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 227 | legacy::PassManager passes; |
| 228 | passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis())); |
| 229 | |
| 230 | PassManagerBuilder PMB; |
| 231 | PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM->getTargetTriple())); |
| 232 | PMB.Inliner = createFunctionInliningPass(); |
Peter Collingbourne | e02b74e | 2017-01-20 22:18:52 +0000 | [diff] [blame] | 233 | PMB.Summary = &CombinedIndex; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 234 | // Unconditionally verify input since it is not verified before this |
| 235 | // point and has unknown origin. |
| 236 | PMB.VerifyInput = true; |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 237 | PMB.VerifyOutput = !Conf.DisableVerify; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 238 | PMB.LoopVectorize = true; |
| 239 | PMB.SLPVectorize = true; |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 240 | PMB.OptLevel = Conf.OptLevel; |
Dehao Chen | 2797800 | 2016-12-16 16:48:46 +0000 | [diff] [blame] | 241 | PMB.PGOSampleUse = Conf.SampleProfile; |
Davide Italiano | 8812f28 | 2016-11-24 00:23:09 +0000 | [diff] [blame] | 242 | if (IsThinLTO) |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 243 | PMB.populateThinLTOPassManager(passes); |
| 244 | else |
| 245 | PMB.populateLTOPassManager(passes); |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 246 | passes.run(Mod); |
Davide Italiano | 1e9d3d3 | 2016-08-31 17:02:44 +0000 | [diff] [blame] | 247 | } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 248 | |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 249 | bool opt(Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod, |
Peter Collingbourne | e02b74e | 2017-01-20 22:18:52 +0000 | [diff] [blame] | 250 | bool IsThinLTO, ModuleSummaryIndex &CombinedIndex) { |
Davide Italiano | 0dd200e | 2017-01-24 00:58:24 +0000 | [diff] [blame^] | 251 | // There's still no ThinLTO pipeline hooked up in the new pass manager, |
| 252 | // once there is one, we can just remove this. |
| 253 | if (LTOUseNewPM && IsThinLTO) |
| 254 | report_fatal_error("ThinLTO not supported with the new PM yet!"); |
| 255 | |
| 256 | // FIXME: Plumb the combined index into the new pass manager. |
| 257 | if (!Conf.OptPipeline.empty()) |
Davide Italiano | 14e9e8a | 2016-09-16 21:03:21 +0000 | [diff] [blame] | 258 | runNewPMCustomPasses(Mod, TM, Conf.OptPipeline, Conf.AAPipeline, |
| 259 | Conf.DisableVerify); |
Davide Italiano | 0dd200e | 2017-01-24 00:58:24 +0000 | [diff] [blame^] | 260 | else if (LTOUseNewPM) |
| 261 | runNewPMPasses(Mod, TM, Conf.OptLevel); |
| 262 | else |
| 263 | runOldPMPasses(Conf, Mod, TM, IsThinLTO, CombinedIndex); |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 264 | return !Conf.PostOptModuleHook || Conf.PostOptModuleHook(Task, Mod); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 267 | void codegen(Config &Conf, TargetMachine *TM, AddStreamFn AddStream, |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 268 | unsigned Task, Module &Mod) { |
| 269 | if (Conf.PreCodeGenModuleHook && !Conf.PreCodeGenModuleHook(Task, Mod)) |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 270 | return; |
| 271 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 272 | auto Stream = AddStream(Task); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 273 | legacy::PassManager CodeGenPasses; |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 274 | if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS, |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 275 | TargetMachine::CGFT_ObjectFile)) |
| 276 | report_fatal_error("Failed to setup codegen"); |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 277 | CodeGenPasses.run(Mod); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 280 | void splitCodeGen(Config &C, TargetMachine *TM, AddStreamFn AddStream, |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 281 | unsigned ParallelCodeGenParallelismLevel, |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 282 | std::unique_ptr<Module> Mod) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 283 | ThreadPool CodegenThreadPool(ParallelCodeGenParallelismLevel); |
| 284 | unsigned ThreadCount = 0; |
| 285 | const Target *T = &TM->getTarget(); |
| 286 | |
| 287 | SplitModule( |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 288 | std::move(Mod), ParallelCodeGenParallelismLevel, |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 289 | [&](std::unique_ptr<Module> MPart) { |
| 290 | // We want to clone the module in a new context to multi-thread the |
| 291 | // codegen. We do it by serializing partition modules to bitcode |
| 292 | // (while still on the main thread, in order to avoid data races) and |
| 293 | // spinning up new threads which deserialize the partitions into |
| 294 | // separate contexts. |
| 295 | // FIXME: Provide a more direct way to do this in LLVM. |
| 296 | SmallString<0> BC; |
| 297 | raw_svector_ostream BCOS(BC); |
| 298 | WriteBitcodeToFile(MPart.get(), BCOS); |
| 299 | |
| 300 | // Enqueue the task |
| 301 | CodegenThreadPool.async( |
| 302 | [&](const SmallString<0> &BC, unsigned ThreadId) { |
| 303 | LTOLLVMContext Ctx(C); |
Peter Collingbourne | d9445c4 | 2016-11-13 07:00:17 +0000 | [diff] [blame] | 304 | Expected<std::unique_ptr<Module>> MOrErr = parseBitcodeFile( |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 305 | MemoryBufferRef(StringRef(BC.data(), BC.size()), "ld-temp.o"), |
| 306 | Ctx); |
| 307 | if (!MOrErr) |
| 308 | report_fatal_error("Failed to read bitcode"); |
| 309 | std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get()); |
| 310 | |
| 311 | std::unique_ptr<TargetMachine> TM = |
| 312 | createTargetMachine(C, MPartInCtx->getTargetTriple(), T); |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 313 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 314 | codegen(C, TM.get(), AddStream, ThreadId, *MPartInCtx); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 315 | }, |
| 316 | // Pass BC using std::move to ensure that it get moved rather than |
| 317 | // copied into the thread's context. |
| 318 | std::move(BC), ThreadCount++); |
| 319 | }, |
| 320 | false); |
Peter Collingbourne | f75609e | 2016-09-29 03:29:28 +0000 | [diff] [blame] | 321 | |
| 322 | // Because the inner lambda (which runs in a worker thread) captures our local |
| 323 | // variables, we need to wait for the worker threads to terminate before we |
| 324 | // can leave the function scope. |
Peter Collingbourne | 0d5636e | 2016-09-29 01:28:36 +0000 | [diff] [blame] | 325 | CodegenThreadPool.wait(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 328 | Expected<const Target *> initAndLookupTarget(Config &C, Module &Mod) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 329 | if (!C.OverrideTriple.empty()) |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 330 | Mod.setTargetTriple(C.OverrideTriple); |
| 331 | else if (Mod.getTargetTriple().empty()) |
| 332 | Mod.setTargetTriple(C.DefaultTriple); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 333 | |
| 334 | std::string Msg; |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 335 | const Target *T = TargetRegistry::lookupTarget(Mod.getTargetTriple(), Msg); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 336 | if (!T) |
| 337 | return make_error<StringError>(Msg, inconvertibleErrorCode()); |
| 338 | return T; |
| 339 | } |
| 340 | |
| 341 | } |
| 342 | |
Davide Italiano | dc8e07b | 2016-09-16 16:05:25 +0000 | [diff] [blame] | 343 | static void handleAsmUndefinedRefs(Module &Mod, TargetMachine &TM) { |
| 344 | // Collect the list of undefined symbols used in asm and update |
| 345 | // llvm.compiler.used to prevent optimization to drop these from the output. |
| 346 | StringSet<> AsmUndefinedRefs; |
Peter Collingbourne | 863cbfb | 2016-12-01 06:51:47 +0000 | [diff] [blame] | 347 | ModuleSymbolTable::CollectAsmSymbols( |
Davide Italiano | dc8e07b | 2016-09-16 16:05:25 +0000 | [diff] [blame] | 348 | Triple(Mod.getTargetTriple()), Mod.getModuleInlineAsm(), |
| 349 | [&AsmUndefinedRefs](StringRef Name, object::BasicSymbolRef::Flags Flags) { |
| 350 | if (Flags & object::BasicSymbolRef::SF_Undefined) |
| 351 | AsmUndefinedRefs.insert(Name); |
| 352 | }); |
| 353 | updateCompilerUsed(Mod, TM, AsmUndefinedRefs); |
| 354 | } |
| 355 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 356 | Error lto::backend(Config &C, AddStreamFn AddStream, |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 357 | unsigned ParallelCodeGenParallelismLevel, |
Peter Collingbourne | e02b74e | 2017-01-20 22:18:52 +0000 | [diff] [blame] | 358 | std::unique_ptr<Module> Mod, |
| 359 | ModuleSummaryIndex &CombinedIndex) { |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 360 | Expected<const Target *> TOrErr = initAndLookupTarget(C, *Mod); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 361 | if (!TOrErr) |
| 362 | return TOrErr.takeError(); |
| 363 | |
| 364 | std::unique_ptr<TargetMachine> TM = |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 365 | createTargetMachine(C, Mod->getTargetTriple(), *TOrErr); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 366 | |
Davide Italiano | dc8e07b | 2016-09-16 16:05:25 +0000 | [diff] [blame] | 367 | handleAsmUndefinedRefs(*Mod, *TM); |
| 368 | |
Mehdi Amini | d310b47 | 2016-08-22 06:25:41 +0000 | [diff] [blame] | 369 | if (!C.CodeGenOnly) |
Peter Collingbourne | e02b74e | 2017-01-20 22:18:52 +0000 | [diff] [blame] | 370 | if (!opt(C, TM.get(), 0, *Mod, /*IsThinLTO=*/false, CombinedIndex)) |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 371 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 372 | |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 373 | if (ParallelCodeGenParallelismLevel == 1) { |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 374 | codegen(C, TM.get(), AddStream, 0, *Mod); |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 375 | } else { |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 376 | splitCodeGen(C, TM.get(), AddStream, ParallelCodeGenParallelismLevel, |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 377 | std::move(Mod)); |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 378 | } |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 379 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 382 | Error lto::thinBackend(Config &Conf, unsigned Task, AddStreamFn AddStream, |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 383 | Module &Mod, ModuleSummaryIndex &CombinedIndex, |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 384 | const FunctionImporter::ImportMapTy &ImportList, |
| 385 | const GVSummaryMapTy &DefinedGlobals, |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 386 | MapVector<StringRef, BitcodeModule> &ModuleMap) { |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 387 | Expected<const Target *> TOrErr = initAndLookupTarget(Conf, Mod); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 388 | if (!TOrErr) |
| 389 | return TOrErr.takeError(); |
| 390 | |
| 391 | std::unique_ptr<TargetMachine> TM = |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 392 | createTargetMachine(Conf, Mod.getTargetTriple(), *TOrErr); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 393 | |
Davide Italiano | dc8e07b | 2016-09-16 16:05:25 +0000 | [diff] [blame] | 394 | handleAsmUndefinedRefs(Mod, *TM); |
| 395 | |
Mehdi Amini | d310b47 | 2016-08-22 06:25:41 +0000 | [diff] [blame] | 396 | if (Conf.CodeGenOnly) { |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 397 | codegen(Conf, TM.get(), AddStream, Task, Mod); |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 398 | return Error::success(); |
Mehdi Amini | d310b47 | 2016-08-22 06:25:41 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 401 | if (Conf.PreOptModuleHook && !Conf.PreOptModuleHook(Task, Mod)) |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 402 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 403 | |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 404 | renameModuleForThinLTO(Mod, CombinedIndex); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 405 | |
Mehdi Amini | 8ac7b32 | 2016-08-18 00:59:24 +0000 | [diff] [blame] | 406 | thinLTOResolveWeakForLinkerModule(Mod, DefinedGlobals); |
| 407 | |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 408 | if (Conf.PostPromoteModuleHook && !Conf.PostPromoteModuleHook(Task, Mod)) |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 409 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 410 | |
| 411 | if (!DefinedGlobals.empty()) |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 412 | thinLTOInternalizeModule(Mod, DefinedGlobals); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 413 | |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 414 | if (Conf.PostInternalizeModuleHook && |
| 415 | !Conf.PostInternalizeModuleHook(Task, Mod)) |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 416 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 417 | |
| 418 | auto ModuleLoader = [&](StringRef Identifier) { |
Mehdi Amini | 9ec5a61 | 2016-08-23 16:53:34 +0000 | [diff] [blame] | 419 | assert(Mod.getContext().isODRUniquingDebugTypes() && |
Davide Italiano | 63e8f44 | 2016-09-14 18:48:43 +0000 | [diff] [blame] | 420 | "ODR Type uniquing should be enabled on the context"); |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 421 | auto I = ModuleMap.find(Identifier); |
| 422 | assert(I != ModuleMap.end()); |
| 423 | return I->second.getLazyModule(Mod.getContext(), |
Teresa Johnson | a61f5e3 | 2016-12-16 21:25:01 +0000 | [diff] [blame] | 424 | /*ShouldLazyLoadMetadata=*/true, |
| 425 | /*IsImporting*/ true); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 426 | }; |
| 427 | |
| 428 | FunctionImporter Importer(CombinedIndex, ModuleLoader); |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 429 | if (Error Err = Importer.importFunctions(Mod, ImportList).takeError()) |
| 430 | return Err; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 431 | |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 432 | if (Conf.PostImportModuleHook && !Conf.PostImportModuleHook(Task, Mod)) |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 433 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 434 | |
Peter Collingbourne | e02b74e | 2017-01-20 22:18:52 +0000 | [diff] [blame] | 435 | if (!opt(Conf, TM.get(), Task, Mod, /*IsThinLTO=*/true, CombinedIndex)) |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 436 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 437 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 438 | codegen(Conf, TM.get(), AddStream, Task, Mod); |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 439 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 440 | } |