Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 1 | //===-LTOBackend.cpp - LLVM Link Time Optimizer Backend -------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the "backend" phase of LTO, i.e. it performs |
| 10 | // optimization and code generation on a loaded module. It is generally used |
| 11 | // internally by the LTO class but can also be used independently, for example |
| 12 | // to implement a standalone ThinLTO backend. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "llvm/LTO/LTOBackend.h" |
Davide Italiano | ec9612d | 2016-09-07 17:46:16 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/AliasAnalysis.h" |
| 18 | #include "llvm/Analysis/CGSCCPassManager.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 20 | #include "llvm/Analysis/TargetTransformInfo.h" |
Teresa Johnson | ad17679 | 2016-11-11 05:34:58 +0000 | [diff] [blame] | 21 | #include "llvm/Bitcode/BitcodeReader.h" |
| 22 | #include "llvm/Bitcode/BitcodeWriter.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 23 | #include "llvm/IR/LegacyPassManager.h" |
Davide Italiano | ec9612d | 2016-09-07 17:46:16 +0000 | [diff] [blame] | 24 | #include "llvm/IR/PassManager.h" |
| 25 | #include "llvm/IR/Verifier.h" |
Mehdi Amini | 970800e | 2016-08-17 06:23:09 +0000 | [diff] [blame] | 26 | #include "llvm/LTO/LTO.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 27 | #include "llvm/MC/SubtargetFeature.h" |
Peter Collingbourne | 192d852 | 2017-03-28 23:35:34 +0000 | [diff] [blame] | 28 | #include "llvm/Object/ModuleSymbolTable.h" |
Davide Italiano | ec9612d | 2016-09-07 17:46:16 +0000 | [diff] [blame] | 29 | #include "llvm/Passes/PassBuilder.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Error.h" |
| 31 | #include "llvm/Support/FileSystem.h" |
Yunlian Jiang | bd200b9 | 2018-04-13 05:03:28 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MemoryBuffer.h" |
| 33 | #include "llvm/Support/Path.h" |
| 34 | #include "llvm/Support/Program.h" |
| 35 | #include "llvm/Support/raw_ostream.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 36 | #include "llvm/Support/TargetRegistry.h" |
| 37 | #include "llvm/Support/ThreadPool.h" |
| 38 | #include "llvm/Target/TargetMachine.h" |
| 39 | #include "llvm/Transforms/IPO.h" |
| 40 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
Chandler Carruth | 3bab7e1 | 2017-01-11 09:43:56 +0000 | [diff] [blame] | 41 | #include "llvm/Transforms/Scalar/LoopPassManager.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 42 | #include "llvm/Transforms/Utils/FunctionImportUtils.h" |
| 43 | #include "llvm/Transforms/Utils/SplitModule.h" |
| 44 | |
| 45 | using namespace llvm; |
| 46 | using namespace lto; |
| 47 | |
Benjamin Kramer | 4c2582a | 2016-10-18 19:39:31 +0000 | [diff] [blame] | 48 | LLVM_ATTRIBUTE_NORETURN static void reportOpenError(StringRef Path, Twine Msg) { |
Davide Italiano | a416d11 | 2016-09-17 22:32:42 +0000 | [diff] [blame] | 49 | errs() << "failed to open " << Path << ": " << Msg << '\n'; |
| 50 | errs().flush(); |
| 51 | exit(1); |
| 52 | } |
| 53 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 54 | Error Config::addSaveTemps(std::string OutputFileName, |
| 55 | bool UseInputModulePath) { |
| 56 | ShouldDiscardValueNames = false; |
| 57 | |
| 58 | std::error_code EC; |
| 59 | ResolutionFile = llvm::make_unique<raw_fd_ostream>( |
Mehdi Amini | eccffad | 2016-08-18 00:12:33 +0000 | [diff] [blame] | 60 | OutputFileName + "resolution.txt", EC, sys::fs::OpenFlags::F_Text); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 61 | if (EC) |
| 62 | return errorCodeToError(EC); |
| 63 | |
| 64 | auto setHook = [&](std::string PathSuffix, ModuleHookFn &Hook) { |
| 65 | // Keep track of the hook provided by the linker, which also needs to run. |
| 66 | ModuleHookFn LinkerHook = Hook; |
Mehdi Amini | f8c2f08 | 2016-08-22 16:17:40 +0000 | [diff] [blame] | 67 | Hook = [=](unsigned Task, const Module &M) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 68 | // If the linker's hook returned false, we need to pass that result |
| 69 | // through. |
| 70 | if (LinkerHook && !LinkerHook(Task, M)) |
| 71 | return false; |
| 72 | |
| 73 | std::string PathPrefix; |
| 74 | // If this is the combined module (not a ThinLTO backend compile) or the |
| 75 | // user hasn't requested using the input module's path, emit to a file |
| 76 | // named from the provided OutputFileName with the Task ID appended. |
| 77 | if (M.getModuleIdentifier() == "ld-temp.o" || !UseInputModulePath) { |
Teresa Johnson | 81d9207 | 2018-05-05 14:37:20 +0000 | [diff] [blame] | 78 | PathPrefix = OutputFileName; |
| 79 | if (Task != (unsigned)-1) |
| 80 | PathPrefix += utostr(Task) + "."; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 81 | } else |
Teresa Johnson | 81d9207 | 2018-05-05 14:37:20 +0000 | [diff] [blame] | 82 | PathPrefix = M.getModuleIdentifier() + "."; |
| 83 | std::string Path = PathPrefix + PathSuffix + ".bc"; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 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()); |
Rafael Espindola | 6a86e25 | 2018-02-14 19:11:32 +0000 | [diff] [blame] | 90 | WriteBitcodeToFile(M, OS, /*ShouldPreserveUseListOrder=*/false); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 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); |
Eugene Leviant | 28d8a49 | 2018-01-22 13:35:40 +0000 | [diff] [blame] | 111 | |
| 112 | Path = OutputFileName + "index.dot"; |
| 113 | raw_fd_ostream OSDot(Path, EC, sys::fs::OpenFlags::F_None); |
| 114 | if (EC) |
| 115 | reportOpenError(Path, EC.message()); |
| 116 | Index.exportToDot(OSDot); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 117 | return true; |
| 118 | }; |
| 119 | |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 120 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | namespace { |
| 124 | |
| 125 | std::unique_ptr<TargetMachine> |
Evgeniy Stepanov | b9f1b01 | 2017-05-22 21:11:35 +0000 | [diff] [blame] | 126 | createTargetMachine(Config &Conf, const Target *TheTarget, Module &M) { |
| 127 | StringRef TheTriple = M.getTargetTriple(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 128 | SubtargetFeatures Features; |
| 129 | Features.getDefaultSubtargetFeatures(Triple(TheTriple)); |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 130 | for (const std::string &A : Conf.MAttrs) |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 131 | Features.AddFeature(A); |
| 132 | |
Evgeniy Stepanov | b9f1b01 | 2017-05-22 21:11:35 +0000 | [diff] [blame] | 133 | Reloc::Model RelocModel; |
| 134 | if (Conf.RelocModel) |
| 135 | RelocModel = *Conf.RelocModel; |
| 136 | else |
| 137 | RelocModel = |
| 138 | M.getPICLevel() == PICLevel::NotPIC ? Reloc::Static : Reloc::PIC_; |
| 139 | |
Caroline Tice | 3dea3f9 | 2018-09-21 18:41:31 +0000 | [diff] [blame] | 140 | Optional<CodeModel::Model> CodeModel; |
| 141 | if (Conf.CodeModel) |
| 142 | CodeModel = *Conf.CodeModel; |
| 143 | else |
| 144 | CodeModel = M.getCodeModel(); |
| 145 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 146 | return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine( |
Evgeniy Stepanov | b9f1b01 | 2017-05-22 21:11:35 +0000 | [diff] [blame] | 147 | TheTriple, Conf.CPU, Features.getString(), Conf.Options, RelocModel, |
Caroline Tice | 3dea3f9 | 2018-09-21 18:41:31 +0000 | [diff] [blame] | 148 | CodeModel, Conf.CGOptLevel)); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Dehao Chen | 89d3226 | 2017-08-02 01:28:31 +0000 | [diff] [blame] | 151 | static void runNewPMPasses(Config &Conf, Module &Mod, TargetMachine *TM, |
Teresa Johnson | 28023db | 2018-07-19 14:51:32 +0000 | [diff] [blame] | 152 | unsigned OptLevel, bool IsThinLTO, |
| 153 | ModuleSummaryIndex *ExportSummary, |
| 154 | const ModuleSummaryIndex *ImportSummary) { |
Dehao Chen | 89d3226 | 2017-08-02 01:28:31 +0000 | [diff] [blame] | 155 | Optional<PGOOptions> PGOOpt; |
| 156 | if (!Conf.SampleProfile.empty()) |
Rong Xu | db29a3a | 2019-03-04 20:21:27 +0000 | [diff] [blame] | 157 | PGOOpt = PGOOptions(Conf.SampleProfile, "", Conf.ProfileRemapping, |
| 158 | PGOOptions::SampleUse, PGOOptions::NoCSAction, true); |
| 159 | else if (Conf.RunCSIRInstr) { |
| 160 | PGOOpt = PGOOptions("", Conf.CSIRProfile, Conf.ProfileRemapping, |
| 161 | PGOOptions::IRUse, PGOOptions::CSIRInstr); |
| 162 | } else if (!Conf.CSIRProfile.empty()) { |
| 163 | PGOOpt = PGOOptions(Conf.CSIRProfile, "", Conf.ProfileRemapping, |
| 164 | PGOOptions::IRUse, PGOOptions::CSIRUse); |
| 165 | } |
Dehao Chen | 89d3226 | 2017-08-02 01:28:31 +0000 | [diff] [blame] | 166 | |
Alina Sbirlea | 0499a2f | 2019-04-19 16:11:59 +0000 | [diff] [blame^] | 167 | PassBuilder PB(TM, PipelineTuningOptions(), PGOOpt); |
Davide Italiano | 0dd200e | 2017-01-24 00:58:24 +0000 | [diff] [blame] | 168 | AAManager AA; |
| 169 | |
| 170 | // Parse a custom AA pipeline if asked to. |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 171 | if (auto Err = PB.parseAAPipeline(AA, "default")) |
Dehao Chen | 3246dc3 | 2017-08-02 03:03:19 +0000 | [diff] [blame] | 172 | report_fatal_error("Error parsing default AA pipeline"); |
Davide Italiano | 0dd200e | 2017-01-24 00:58:24 +0000 | [diff] [blame] | 173 | |
Dehao Chen | 3246dc3 | 2017-08-02 03:03:19 +0000 | [diff] [blame] | 174 | LoopAnalysisManager LAM(Conf.DebugPassManager); |
| 175 | FunctionAnalysisManager FAM(Conf.DebugPassManager); |
| 176 | CGSCCAnalysisManager CGAM(Conf.DebugPassManager); |
| 177 | ModuleAnalysisManager MAM(Conf.DebugPassManager); |
Davide Italiano | 0dd200e | 2017-01-24 00:58:24 +0000 | [diff] [blame] | 178 | |
| 179 | // Register the AA manager first so that our version is the one used. |
| 180 | FAM.registerPass([&] { return std::move(AA); }); |
| 181 | |
| 182 | // Register all the basic analyses with the managers. |
| 183 | PB.registerModuleAnalyses(MAM); |
| 184 | PB.registerCGSCCAnalyses(CGAM); |
| 185 | PB.registerFunctionAnalyses(FAM); |
| 186 | PB.registerLoopAnalyses(LAM); |
| 187 | PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); |
| 188 | |
Dehao Chen | 3246dc3 | 2017-08-02 03:03:19 +0000 | [diff] [blame] | 189 | ModulePassManager MPM(Conf.DebugPassManager); |
Davide Italiano | 0dd200e | 2017-01-24 00:58:24 +0000 | [diff] [blame] | 190 | // FIXME (davide): verify the input. |
| 191 | |
| 192 | PassBuilder::OptimizationLevel OL; |
| 193 | |
| 194 | switch (OptLevel) { |
| 195 | default: |
| 196 | llvm_unreachable("Invalid optimization level"); |
| 197 | case 0: |
| 198 | OL = PassBuilder::O0; |
| 199 | break; |
| 200 | case 1: |
| 201 | OL = PassBuilder::O1; |
| 202 | break; |
| 203 | case 2: |
| 204 | OL = PassBuilder::O2; |
| 205 | break; |
| 206 | case 3: |
| 207 | OL = PassBuilder::O3; |
| 208 | break; |
| 209 | } |
| 210 | |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 211 | if (IsThinLTO) |
Teresa Johnson | 28023db | 2018-07-19 14:51:32 +0000 | [diff] [blame] | 212 | MPM = PB.buildThinLTODefaultPipeline(OL, Conf.DebugPassManager, |
| 213 | ImportSummary); |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 214 | else |
Teresa Johnson | 28023db | 2018-07-19 14:51:32 +0000 | [diff] [blame] | 215 | MPM = PB.buildLTODefaultPipeline(OL, Conf.DebugPassManager, ExportSummary); |
Davide Italiano | 0dd200e | 2017-01-24 00:58:24 +0000 | [diff] [blame] | 216 | MPM.run(Mod, MAM); |
| 217 | |
| 218 | // FIXME (davide): verify the output. |
| 219 | } |
| 220 | |
Davide Italiano | ec9612d | 2016-09-07 17:46:16 +0000 | [diff] [blame] | 221 | static void runNewPMCustomPasses(Module &Mod, TargetMachine *TM, |
| 222 | std::string PipelineDesc, |
Davide Italiano | 14e9e8a | 2016-09-16 21:03:21 +0000 | [diff] [blame] | 223 | std::string AAPipelineDesc, |
Davide Italiano | ec9612d | 2016-09-07 17:46:16 +0000 | [diff] [blame] | 224 | bool DisableVerify) { |
| 225 | PassBuilder PB(TM); |
| 226 | AAManager AA; |
Davide Italiano | 14e9e8a | 2016-09-16 21:03:21 +0000 | [diff] [blame] | 227 | |
| 228 | // Parse a custom AA pipeline if asked to. |
| 229 | if (!AAPipelineDesc.empty()) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 230 | if (auto Err = PB.parseAAPipeline(AA, AAPipelineDesc)) |
| 231 | report_fatal_error("unable to parse AA pipeline description '" + |
| 232 | AAPipelineDesc + "': " + toString(std::move(Err))); |
Davide Italiano | 14e9e8a | 2016-09-16 21:03:21 +0000 | [diff] [blame] | 233 | |
Davide Italiano | ec9612d | 2016-09-07 17:46:16 +0000 | [diff] [blame] | 234 | LoopAnalysisManager LAM; |
| 235 | FunctionAnalysisManager FAM; |
| 236 | CGSCCAnalysisManager CGAM; |
| 237 | ModuleAnalysisManager MAM; |
| 238 | |
| 239 | // Register the AA manager first so that our version is the one used. |
| 240 | FAM.registerPass([&] { return std::move(AA); }); |
| 241 | |
| 242 | // Register all the basic analyses with the managers. |
| 243 | PB.registerModuleAnalyses(MAM); |
| 244 | PB.registerCGSCCAnalyses(CGAM); |
| 245 | PB.registerFunctionAnalyses(FAM); |
| 246 | PB.registerLoopAnalyses(LAM); |
| 247 | PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); |
| 248 | |
| 249 | ModulePassManager MPM; |
| 250 | |
| 251 | // Always verify the input. |
| 252 | MPM.addPass(VerifierPass()); |
| 253 | |
| 254 | // Now, add all the passes we've been requested to. |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 255 | if (auto Err = PB.parsePassPipeline(MPM, PipelineDesc)) |
| 256 | report_fatal_error("unable to parse pass pipeline description '" + |
| 257 | PipelineDesc + "': " + toString(std::move(Err))); |
Davide Italiano | ec9612d | 2016-09-07 17:46:16 +0000 | [diff] [blame] | 258 | |
| 259 | if (!DisableVerify) |
| 260 | MPM.addPass(VerifierPass()); |
| 261 | MPM.run(Mod, MAM); |
| 262 | } |
| 263 | |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 264 | static void runOldPMPasses(Config &Conf, Module &Mod, TargetMachine *TM, |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 265 | bool IsThinLTO, ModuleSummaryIndex *ExportSummary, |
| 266 | const ModuleSummaryIndex *ImportSummary) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 267 | legacy::PassManager passes; |
| 268 | passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis())); |
| 269 | |
| 270 | PassManagerBuilder PMB; |
| 271 | PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM->getTargetTriple())); |
| 272 | PMB.Inliner = createFunctionInliningPass(); |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 273 | PMB.ExportSummary = ExportSummary; |
| 274 | PMB.ImportSummary = ImportSummary; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 275 | // Unconditionally verify input since it is not verified before this |
| 276 | // point and has unknown origin. |
| 277 | PMB.VerifyInput = true; |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 278 | PMB.VerifyOutput = !Conf.DisableVerify; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 279 | PMB.LoopVectorize = true; |
| 280 | PMB.SLPVectorize = true; |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 281 | PMB.OptLevel = Conf.OptLevel; |
Dehao Chen | 2797800 | 2016-12-16 16:48:46 +0000 | [diff] [blame] | 282 | PMB.PGOSampleUse = Conf.SampleProfile; |
Rong Xu | db29a3a | 2019-03-04 20:21:27 +0000 | [diff] [blame] | 283 | PMB.EnablePGOCSInstrGen = Conf.RunCSIRInstr; |
| 284 | if (!Conf.RunCSIRInstr && !Conf.CSIRProfile.empty()) { |
| 285 | PMB.EnablePGOCSInstrUse = true; |
| 286 | PMB.PGOInstrUse = Conf.CSIRProfile; |
| 287 | } |
Davide Italiano | 8812f28 | 2016-11-24 00:23:09 +0000 | [diff] [blame] | 288 | if (IsThinLTO) |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 289 | PMB.populateThinLTOPassManager(passes); |
| 290 | else |
| 291 | PMB.populateLTOPassManager(passes); |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 292 | passes.run(Mod); |
Davide Italiano | 1e9d3d3 | 2016-08-31 17:02:44 +0000 | [diff] [blame] | 293 | } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 294 | |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 295 | bool opt(Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod, |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 296 | bool IsThinLTO, ModuleSummaryIndex *ExportSummary, |
| 297 | const ModuleSummaryIndex *ImportSummary) { |
Davide Italiano | 0dd200e | 2017-01-24 00:58:24 +0000 | [diff] [blame] | 298 | // FIXME: Plumb the combined index into the new pass manager. |
| 299 | if (!Conf.OptPipeline.empty()) |
Davide Italiano | 14e9e8a | 2016-09-16 21:03:21 +0000 | [diff] [blame] | 300 | runNewPMCustomPasses(Mod, TM, Conf.OptPipeline, Conf.AAPipeline, |
| 301 | Conf.DisableVerify); |
Tim Shen | 4e912aa | 2017-06-01 23:13:44 +0000 | [diff] [blame] | 302 | else if (Conf.UseNewPM) |
Teresa Johnson | 28023db | 2018-07-19 14:51:32 +0000 | [diff] [blame] | 303 | runNewPMPasses(Conf, Mod, TM, Conf.OptLevel, IsThinLTO, ExportSummary, |
| 304 | ImportSummary); |
Davide Italiano | 0dd200e | 2017-01-24 00:58:24 +0000 | [diff] [blame] | 305 | else |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 306 | runOldPMPasses(Conf, Mod, TM, IsThinLTO, ExportSummary, ImportSummary); |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 307 | return !Conf.PostOptModuleHook || Conf.PostOptModuleHook(Task, Mod); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 310 | void codegen(Config &Conf, TargetMachine *TM, AddStreamFn AddStream, |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 311 | unsigned Task, Module &Mod) { |
| 312 | if (Conf.PreCodeGenModuleHook && !Conf.PreCodeGenModuleHook(Task, Mod)) |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 313 | return; |
| 314 | |
Peter Collingbourne | c5a9765 | 2018-05-21 20:26:49 +0000 | [diff] [blame] | 315 | std::unique_ptr<ToolOutputFile> DwoOut; |
Peter Collingbourne | 3aa30e8 | 2018-05-31 18:25:59 +0000 | [diff] [blame] | 316 | SmallString<1024> DwoFile(Conf.DwoPath); |
Yunlian Jiang | bd200b9 | 2018-04-13 05:03:28 +0000 | [diff] [blame] | 317 | if (!Conf.DwoDir.empty()) { |
Peter Collingbourne | c5a9765 | 2018-05-21 20:26:49 +0000 | [diff] [blame] | 318 | std::error_code EC; |
| 319 | if (auto EC = llvm::sys::fs::create_directories(Conf.DwoDir)) |
| 320 | report_fatal_error("Failed to create directory " + Conf.DwoDir + ": " + |
| 321 | EC.message()); |
| 322 | |
Peter Collingbourne | 3aa30e8 | 2018-05-31 18:25:59 +0000 | [diff] [blame] | 323 | DwoFile = Conf.DwoDir; |
Peter Collingbourne | c5a9765 | 2018-05-21 20:26:49 +0000 | [diff] [blame] | 324 | sys::path::append(DwoFile, std::to_string(Task) + ".dwo"); |
Peter Collingbourne | 3aa30e8 | 2018-05-31 18:25:59 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | if (!DwoFile.empty()) { |
| 328 | std::error_code EC; |
Peter Collingbourne | c5a9765 | 2018-05-21 20:26:49 +0000 | [diff] [blame] | 329 | TM->Options.MCOptions.SplitDwarfFile = DwoFile.str().str(); |
Peter Collingbourne | 274c4f7 | 2018-05-21 20:56:28 +0000 | [diff] [blame] | 330 | DwoOut = llvm::make_unique<ToolOutputFile>(DwoFile, EC, sys::fs::F_None); |
Peter Collingbourne | c5a9765 | 2018-05-21 20:26:49 +0000 | [diff] [blame] | 331 | if (EC) |
| 332 | report_fatal_error("Failed to open " + DwoFile + ": " + EC.message()); |
Yunlian Jiang | bd200b9 | 2018-04-13 05:03:28 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 335 | auto Stream = AddStream(Task); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 336 | legacy::PassManager CodeGenPasses; |
Peter Collingbourne | c5a9765 | 2018-05-21 20:26:49 +0000 | [diff] [blame] | 337 | if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS, |
| 338 | DwoOut ? &DwoOut->os() : nullptr, |
Peter Collingbourne | 9a45114 | 2018-05-21 20:16:41 +0000 | [diff] [blame] | 339 | Conf.CGFileType)) |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 340 | report_fatal_error("Failed to setup codegen"); |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 341 | CodeGenPasses.run(Mod); |
Peter Collingbourne | c5a9765 | 2018-05-21 20:26:49 +0000 | [diff] [blame] | 342 | |
| 343 | if (DwoOut) |
| 344 | DwoOut->keep(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 347 | void splitCodeGen(Config &C, TargetMachine *TM, AddStreamFn AddStream, |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 348 | unsigned ParallelCodeGenParallelismLevel, |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 349 | std::unique_ptr<Module> Mod) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 350 | ThreadPool CodegenThreadPool(ParallelCodeGenParallelismLevel); |
| 351 | unsigned ThreadCount = 0; |
| 352 | const Target *T = &TM->getTarget(); |
| 353 | |
| 354 | SplitModule( |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 355 | std::move(Mod), ParallelCodeGenParallelismLevel, |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 356 | [&](std::unique_ptr<Module> MPart) { |
| 357 | // We want to clone the module in a new context to multi-thread the |
| 358 | // codegen. We do it by serializing partition modules to bitcode |
| 359 | // (while still on the main thread, in order to avoid data races) and |
| 360 | // spinning up new threads which deserialize the partitions into |
| 361 | // separate contexts. |
| 362 | // FIXME: Provide a more direct way to do this in LLVM. |
| 363 | SmallString<0> BC; |
| 364 | raw_svector_ostream BCOS(BC); |
Rafael Espindola | 6a86e25 | 2018-02-14 19:11:32 +0000 | [diff] [blame] | 365 | WriteBitcodeToFile(*MPart, BCOS); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 366 | |
| 367 | // Enqueue the task |
| 368 | CodegenThreadPool.async( |
| 369 | [&](const SmallString<0> &BC, unsigned ThreadId) { |
| 370 | LTOLLVMContext Ctx(C); |
Peter Collingbourne | d9445c4 | 2016-11-13 07:00:17 +0000 | [diff] [blame] | 371 | Expected<std::unique_ptr<Module>> MOrErr = parseBitcodeFile( |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 372 | MemoryBufferRef(StringRef(BC.data(), BC.size()), "ld-temp.o"), |
| 373 | Ctx); |
| 374 | if (!MOrErr) |
| 375 | report_fatal_error("Failed to read bitcode"); |
| 376 | std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get()); |
| 377 | |
| 378 | std::unique_ptr<TargetMachine> TM = |
Evgeniy Stepanov | b9f1b01 | 2017-05-22 21:11:35 +0000 | [diff] [blame] | 379 | createTargetMachine(C, T, *MPartInCtx); |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 380 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 381 | codegen(C, TM.get(), AddStream, ThreadId, *MPartInCtx); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 382 | }, |
| 383 | // Pass BC using std::move to ensure that it get moved rather than |
| 384 | // copied into the thread's context. |
| 385 | std::move(BC), ThreadCount++); |
| 386 | }, |
| 387 | false); |
Peter Collingbourne | f75609e | 2016-09-29 03:29:28 +0000 | [diff] [blame] | 388 | |
| 389 | // Because the inner lambda (which runs in a worker thread) captures our local |
| 390 | // variables, we need to wait for the worker threads to terminate before we |
| 391 | // can leave the function scope. |
Peter Collingbourne | 0d5636e | 2016-09-29 01:28:36 +0000 | [diff] [blame] | 392 | CodegenThreadPool.wait(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 395 | Expected<const Target *> initAndLookupTarget(Config &C, Module &Mod) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 396 | if (!C.OverrideTriple.empty()) |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 397 | Mod.setTargetTriple(C.OverrideTriple); |
| 398 | else if (Mod.getTargetTriple().empty()) |
| 399 | Mod.setTargetTriple(C.DefaultTriple); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 400 | |
| 401 | std::string Msg; |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 402 | const Target *T = TargetRegistry::lookupTarget(Mod.getTargetTriple(), Msg); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 403 | if (!T) |
| 404 | return make_error<StringError>(Msg, inconvertibleErrorCode()); |
| 405 | return T; |
| 406 | } |
| 407 | |
| 408 | } |
| 409 | |
Teresa Johnson | 85cc298 | 2018-05-03 20:24:12 +0000 | [diff] [blame] | 410 | static Error |
Reid Kleckner | 3fc649c | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 411 | finalizeOptimizationRemarks(std::unique_ptr<ToolOutputFile> DiagOutputFile) { |
Davide Italiano | 20a895c | 2017-02-13 14:39:51 +0000 | [diff] [blame] | 412 | // Make sure we flush the diagnostic remarks file in case the linker doesn't |
| 413 | // call the global destructors before exiting. |
| 414 | if (!DiagOutputFile) |
Teresa Johnson | 85cc298 | 2018-05-03 20:24:12 +0000 | [diff] [blame] | 415 | return Error::success(); |
Davide Italiano | 20a895c | 2017-02-13 14:39:51 +0000 | [diff] [blame] | 416 | DiagOutputFile->keep(); |
| 417 | DiagOutputFile->os().flush(); |
Teresa Johnson | 85cc298 | 2018-05-03 20:24:12 +0000 | [diff] [blame] | 418 | return Error::success(); |
Davide Italiano | 20a895c | 2017-02-13 14:39:51 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 421 | Error lto::backend(Config &C, AddStreamFn AddStream, |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 422 | unsigned ParallelCodeGenParallelismLevel, |
Peter Collingbourne | e02b74e | 2017-01-20 22:18:52 +0000 | [diff] [blame] | 423 | std::unique_ptr<Module> Mod, |
| 424 | ModuleSummaryIndex &CombinedIndex) { |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 425 | Expected<const Target *> TOrErr = initAndLookupTarget(C, *Mod); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 426 | if (!TOrErr) |
| 427 | return TOrErr.takeError(); |
| 428 | |
Evgeniy Stepanov | b9f1b01 | 2017-05-22 21:11:35 +0000 | [diff] [blame] | 429 | std::unique_ptr<TargetMachine> TM = createTargetMachine(C, *TOrErr, *Mod); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 430 | |
Davide Italiano | ebd4719 | 2017-02-12 03:31:30 +0000 | [diff] [blame] | 431 | // Setup optimization remarks. |
Francis Visoiu Mistrih | dd42236 | 2019-03-12 21:22:27 +0000 | [diff] [blame] | 432 | auto DiagFileOrErr = |
| 433 | lto::setupOptimizationRemarks(Mod->getContext(), C.RemarksFilename, |
| 434 | C.RemarksPasses, C.RemarksWithHotness); |
Davide Italiano | ebd4719 | 2017-02-12 03:31:30 +0000 | [diff] [blame] | 435 | if (!DiagFileOrErr) |
| 436 | return DiagFileOrErr.takeError(); |
Davide Italiano | 20a895c | 2017-02-13 14:39:51 +0000 | [diff] [blame] | 437 | auto DiagnosticOutputFile = std::move(*DiagFileOrErr); |
Davide Italiano | ebd4719 | 2017-02-12 03:31:30 +0000 | [diff] [blame] | 438 | |
Davide Italiano | 20a895c | 2017-02-13 14:39:51 +0000 | [diff] [blame] | 439 | if (!C.CodeGenOnly) { |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 440 | if (!opt(C, TM.get(), 0, *Mod, /*IsThinLTO=*/false, |
Teresa Johnson | 85cc298 | 2018-05-03 20:24:12 +0000 | [diff] [blame] | 441 | /*ExportSummary=*/&CombinedIndex, /*ImportSummary=*/nullptr)) |
| 442 | return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile)); |
Davide Italiano | 20a895c | 2017-02-13 14:39:51 +0000 | [diff] [blame] | 443 | } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 444 | |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 445 | if (ParallelCodeGenParallelismLevel == 1) { |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 446 | codegen(C, TM.get(), AddStream, 0, *Mod); |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 447 | } else { |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 448 | splitCodeGen(C, TM.get(), AddStream, ParallelCodeGenParallelismLevel, |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 449 | std::move(Mod)); |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 450 | } |
Teresa Johnson | 85cc298 | 2018-05-03 20:24:12 +0000 | [diff] [blame] | 451 | return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile)); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 452 | } |
| 453 | |
George Rimar | eaf5172 | 2018-01-29 08:03:30 +0000 | [diff] [blame] | 454 | static void dropDeadSymbols(Module &Mod, const GVSummaryMapTy &DefinedGlobals, |
| 455 | const ModuleSummaryIndex &Index) { |
Teresa Johnson | 791c98e | 2018-02-06 00:43:39 +0000 | [diff] [blame] | 456 | std::vector<GlobalValue*> DeadGVs; |
Teresa Johnson | 5a95c47 | 2018-02-05 15:44:27 +0000 | [diff] [blame] | 457 | for (auto &GV : Mod.global_values()) |
George Rimar | f5de271 | 2018-02-02 12:21:26 +0000 | [diff] [blame] | 458 | if (GlobalValueSummary *GVS = DefinedGlobals.lookup(GV.getGUID())) |
Teresa Johnson | 791c98e | 2018-02-06 00:43:39 +0000 | [diff] [blame] | 459 | if (!Index.isGlobalValueLive(GVS)) { |
| 460 | DeadGVs.push_back(&GV); |
| 461 | convertToDeclaration(GV); |
| 462 | } |
George Rimar | 76c5fae | 2018-02-02 12:17:33 +0000 | [diff] [blame] | 463 | |
Teresa Johnson | 791c98e | 2018-02-06 00:43:39 +0000 | [diff] [blame] | 464 | // Now that all dead bodies have been dropped, delete the actual objects |
| 465 | // themselves when possible. |
| 466 | for (GlobalValue *GV : DeadGVs) { |
| 467 | GV->removeDeadConstantUsers(); |
| 468 | // Might reference something defined in native object (i.e. dropped a |
| 469 | // non-prevailing IR def, but we need to keep the declaration). |
| 470 | if (GV->use_empty()) |
| 471 | GV->eraseFromParent(); |
| 472 | } |
George Rimar | eaf5172 | 2018-01-29 08:03:30 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 475 | Error lto::thinBackend(Config &Conf, unsigned Task, AddStreamFn AddStream, |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 476 | Module &Mod, const ModuleSummaryIndex &CombinedIndex, |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 477 | const FunctionImporter::ImportMapTy &ImportList, |
| 478 | const GVSummaryMapTy &DefinedGlobals, |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 479 | MapVector<StringRef, BitcodeModule> &ModuleMap) { |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 480 | Expected<const Target *> TOrErr = initAndLookupTarget(Conf, Mod); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 481 | if (!TOrErr) |
| 482 | return TOrErr.takeError(); |
| 483 | |
Evgeniy Stepanov | b9f1b01 | 2017-05-22 21:11:35 +0000 | [diff] [blame] | 484 | std::unique_ptr<TargetMachine> TM = createTargetMachine(Conf, *TOrErr, Mod); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 485 | |
Teresa Johnson | 85cc298 | 2018-05-03 20:24:12 +0000 | [diff] [blame] | 486 | // Setup optimization remarks. |
| 487 | auto DiagFileOrErr = lto::setupOptimizationRemarks( |
Francis Visoiu Mistrih | dd42236 | 2019-03-12 21:22:27 +0000 | [diff] [blame] | 488 | Mod.getContext(), Conf.RemarksFilename, Conf.RemarksPasses, |
| 489 | Conf.RemarksWithHotness, Task); |
Teresa Johnson | 85cc298 | 2018-05-03 20:24:12 +0000 | [diff] [blame] | 490 | if (!DiagFileOrErr) |
| 491 | return DiagFileOrErr.takeError(); |
| 492 | auto DiagnosticOutputFile = std::move(*DiagFileOrErr); |
| 493 | |
Mehdi Amini | d310b47 | 2016-08-22 06:25:41 +0000 | [diff] [blame] | 494 | if (Conf.CodeGenOnly) { |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 495 | codegen(Conf, TM.get(), AddStream, Task, Mod); |
Teresa Johnson | 85cc298 | 2018-05-03 20:24:12 +0000 | [diff] [blame] | 496 | return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile)); |
Mehdi Amini | d310b47 | 2016-08-22 06:25:41 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 499 | if (Conf.PreOptModuleHook && !Conf.PreOptModuleHook(Task, Mod)) |
Teresa Johnson | 85cc298 | 2018-05-03 20:24:12 +0000 | [diff] [blame] | 500 | return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile)); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 501 | |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 502 | renameModuleForThinLTO(Mod, CombinedIndex); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 503 | |
George Rimar | eaf5172 | 2018-01-29 08:03:30 +0000 | [diff] [blame] | 504 | dropDeadSymbols(Mod, DefinedGlobals, CombinedIndex); |
| 505 | |
Pirama Arumuga Nainar | e61652a | 2018-11-08 20:10:07 +0000 | [diff] [blame] | 506 | thinLTOResolvePrevailingInModule(Mod, DefinedGlobals); |
Mehdi Amini | 8ac7b32 | 2016-08-18 00:59:24 +0000 | [diff] [blame] | 507 | |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 508 | if (Conf.PostPromoteModuleHook && !Conf.PostPromoteModuleHook(Task, Mod)) |
Teresa Johnson | 85cc298 | 2018-05-03 20:24:12 +0000 | [diff] [blame] | 509 | return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile)); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 510 | |
| 511 | if (!DefinedGlobals.empty()) |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 512 | thinLTOInternalizeModule(Mod, DefinedGlobals); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 513 | |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 514 | if (Conf.PostInternalizeModuleHook && |
| 515 | !Conf.PostInternalizeModuleHook(Task, Mod)) |
Teresa Johnson | 85cc298 | 2018-05-03 20:24:12 +0000 | [diff] [blame] | 516 | return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile)); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 517 | |
| 518 | auto ModuleLoader = [&](StringRef Identifier) { |
Mehdi Amini | 9ec5a61 | 2016-08-23 16:53:34 +0000 | [diff] [blame] | 519 | assert(Mod.getContext().isODRUniquingDebugTypes() && |
Davide Italiano | 63e8f44 | 2016-09-14 18:48:43 +0000 | [diff] [blame] | 520 | "ODR Type uniquing should be enabled on the context"); |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 521 | auto I = ModuleMap.find(Identifier); |
| 522 | assert(I != ModuleMap.end()); |
| 523 | return I->second.getLazyModule(Mod.getContext(), |
Teresa Johnson | a61f5e3 | 2016-12-16 21:25:01 +0000 | [diff] [blame] | 524 | /*ShouldLazyLoadMetadata=*/true, |
| 525 | /*IsImporting*/ true); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 526 | }; |
| 527 | |
| 528 | FunctionImporter Importer(CombinedIndex, ModuleLoader); |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 529 | if (Error Err = Importer.importFunctions(Mod, ImportList).takeError()) |
| 530 | return Err; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 531 | |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 532 | if (Conf.PostImportModuleHook && !Conf.PostImportModuleHook(Task, Mod)) |
Teresa Johnson | 85cc298 | 2018-05-03 20:24:12 +0000 | [diff] [blame] | 533 | return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile)); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 534 | |
Peter Collingbourne | f7691d8 | 2017-03-22 18:22:59 +0000 | [diff] [blame] | 535 | if (!opt(Conf, TM.get(), Task, Mod, /*IsThinLTO=*/true, |
| 536 | /*ExportSummary=*/nullptr, /*ImportSummary=*/&CombinedIndex)) |
Teresa Johnson | 85cc298 | 2018-05-03 20:24:12 +0000 | [diff] [blame] | 537 | return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile)); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 538 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 539 | codegen(Conf, TM.get(), AddStream, Task, Mod); |
Teresa Johnson | 85cc298 | 2018-05-03 20:24:12 +0000 | [diff] [blame] | 540 | return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile)); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 541 | } |