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