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" |
| 20 | #include "llvm/Analysis/LoopPassManager.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 22 | #include "llvm/Analysis/TargetTransformInfo.h" |
| 23 | #include "llvm/Bitcode/ReaderWriter.h" |
| 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" |
| 38 | #include "llvm/Transforms/Utils/FunctionImportUtils.h" |
| 39 | #include "llvm/Transforms/Utils/SplitModule.h" |
| 40 | |
| 41 | using namespace llvm; |
| 42 | using namespace lto; |
| 43 | |
Davide Italiano | a416d11 | 2016-09-17 22:32:42 +0000 | [diff] [blame] | 44 | LLVM_ATTRIBUTE_NORETURN void reportOpenError(StringRef Path, Twine Msg) { |
| 45 | errs() << "failed to open " << Path << ": " << Msg << '\n'; |
| 46 | errs().flush(); |
| 47 | exit(1); |
| 48 | } |
| 49 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 50 | Error Config::addSaveTemps(std::string OutputFileName, |
| 51 | bool UseInputModulePath) { |
| 52 | ShouldDiscardValueNames = false; |
| 53 | |
| 54 | std::error_code EC; |
| 55 | ResolutionFile = llvm::make_unique<raw_fd_ostream>( |
Mehdi Amini | eccffad | 2016-08-18 00:12:33 +0000 | [diff] [blame] | 56 | OutputFileName + "resolution.txt", EC, sys::fs::OpenFlags::F_Text); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 57 | if (EC) |
| 58 | return errorCodeToError(EC); |
| 59 | |
| 60 | auto setHook = [&](std::string PathSuffix, ModuleHookFn &Hook) { |
| 61 | // Keep track of the hook provided by the linker, which also needs to run. |
| 62 | ModuleHookFn LinkerHook = Hook; |
Mehdi Amini | f8c2f08 | 2016-08-22 16:17:40 +0000 | [diff] [blame] | 63 | Hook = [=](unsigned Task, const Module &M) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 64 | // If the linker's hook returned false, we need to pass that result |
| 65 | // through. |
| 66 | if (LinkerHook && !LinkerHook(Task, M)) |
| 67 | return false; |
| 68 | |
| 69 | std::string PathPrefix; |
| 70 | // If this is the combined module (not a ThinLTO backend compile) or the |
| 71 | // user hasn't requested using the input module's path, emit to a file |
| 72 | // named from the provided OutputFileName with the Task ID appended. |
| 73 | if (M.getModuleIdentifier() == "ld-temp.o" || !UseInputModulePath) { |
Mehdi Amini | eccffad | 2016-08-18 00:12:33 +0000 | [diff] [blame] | 74 | PathPrefix = OutputFileName + utostr(Task); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 75 | } else |
| 76 | PathPrefix = M.getModuleIdentifier(); |
| 77 | std::string Path = PathPrefix + "." + PathSuffix + ".bc"; |
| 78 | std::error_code EC; |
| 79 | raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None); |
Davide Italiano | a416d11 | 2016-09-17 22:32:42 +0000 | [diff] [blame] | 80 | // Because -save-temps is a debugging feature, we report the error |
| 81 | // directly and exit. |
| 82 | if (EC) |
| 83 | reportOpenError(Path, EC.message()); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 84 | WriteBitcodeToFile(&M, OS, /*ShouldPreserveUseListOrder=*/false); |
| 85 | return true; |
| 86 | }; |
| 87 | }; |
| 88 | |
| 89 | setHook("0.preopt", PreOptModuleHook); |
| 90 | setHook("1.promote", PostPromoteModuleHook); |
| 91 | setHook("2.internalize", PostInternalizeModuleHook); |
| 92 | setHook("3.import", PostImportModuleHook); |
| 93 | setHook("4.opt", PostOptModuleHook); |
| 94 | setHook("5.precodegen", PreCodeGenModuleHook); |
| 95 | |
| 96 | CombinedIndexHook = [=](const ModuleSummaryIndex &Index) { |
Mehdi Amini | eccffad | 2016-08-18 00:12:33 +0000 | [diff] [blame] | 97 | std::string Path = OutputFileName + "index.bc"; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 98 | std::error_code EC; |
| 99 | raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None); |
Davide Italiano | a416d11 | 2016-09-17 22:32:42 +0000 | [diff] [blame] | 100 | // Because -save-temps is a debugging feature, we report the error |
| 101 | // directly and exit. |
| 102 | if (EC) |
| 103 | reportOpenError(Path, EC.message()); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 104 | WriteIndexToFile(Index, OS); |
| 105 | return true; |
| 106 | }; |
| 107 | |
| 108 | return Error(); |
| 109 | } |
| 110 | |
| 111 | namespace { |
| 112 | |
| 113 | std::unique_ptr<TargetMachine> |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 114 | createTargetMachine(Config &Conf, StringRef TheTriple, |
| 115 | const Target *TheTarget) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 116 | SubtargetFeatures Features; |
| 117 | Features.getDefaultSubtargetFeatures(Triple(TheTriple)); |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 118 | for (const std::string &A : Conf.MAttrs) |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 119 | Features.AddFeature(A); |
| 120 | |
| 121 | return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine( |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 122 | TheTriple, Conf.CPU, Features.getString(), Conf.Options, Conf.RelocModel, |
| 123 | Conf.CodeModel, Conf.CGOptLevel)); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Davide Italiano | ec9612d | 2016-09-07 17:46:16 +0000 | [diff] [blame] | 126 | static void runNewPMCustomPasses(Module &Mod, TargetMachine *TM, |
| 127 | std::string PipelineDesc, |
Davide Italiano | 14e9e8a | 2016-09-16 21:03:21 +0000 | [diff] [blame] | 128 | std::string AAPipelineDesc, |
Davide Italiano | ec9612d | 2016-09-07 17:46:16 +0000 | [diff] [blame] | 129 | bool DisableVerify) { |
| 130 | PassBuilder PB(TM); |
| 131 | AAManager AA; |
Davide Italiano | 14e9e8a | 2016-09-16 21:03:21 +0000 | [diff] [blame] | 132 | |
| 133 | // Parse a custom AA pipeline if asked to. |
| 134 | if (!AAPipelineDesc.empty()) |
| 135 | if (!PB.parseAAPipeline(AA, AAPipelineDesc)) |
| 136 | report_fatal_error("unable to parse AA pipeline description: " + |
| 137 | AAPipelineDesc); |
| 138 | |
Davide Italiano | ec9612d | 2016-09-07 17:46:16 +0000 | [diff] [blame] | 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 | |
| 156 | // Always verify the input. |
| 157 | MPM.addPass(VerifierPass()); |
| 158 | |
| 159 | // Now, add all the passes we've been requested to. |
| 160 | if (!PB.parsePassPipeline(MPM, PipelineDesc)) |
| 161 | report_fatal_error("unable to parse pass pipeline description: " + |
| 162 | PipelineDesc); |
| 163 | |
| 164 | if (!DisableVerify) |
| 165 | MPM.addPass(VerifierPass()); |
| 166 | MPM.run(Mod, MAM); |
| 167 | } |
| 168 | |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 169 | static void runOldPMPasses(Config &Conf, Module &Mod, TargetMachine *TM, |
Davide Italiano | 1e9d3d3 | 2016-08-31 17:02:44 +0000 | [diff] [blame] | 170 | bool IsThinLto) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 171 | legacy::PassManager passes; |
| 172 | passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis())); |
| 173 | |
| 174 | PassManagerBuilder PMB; |
| 175 | PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM->getTargetTriple())); |
| 176 | PMB.Inliner = createFunctionInliningPass(); |
| 177 | // Unconditionally verify input since it is not verified before this |
| 178 | // point and has unknown origin. |
| 179 | PMB.VerifyInput = true; |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 180 | PMB.VerifyOutput = !Conf.DisableVerify; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 181 | PMB.LoopVectorize = true; |
| 182 | PMB.SLPVectorize = true; |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 183 | PMB.OptLevel = Conf.OptLevel; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 184 | if (IsThinLto) |
| 185 | PMB.populateThinLTOPassManager(passes); |
| 186 | else |
| 187 | PMB.populateLTOPassManager(passes); |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 188 | passes.run(Mod); |
Davide Italiano | 1e9d3d3 | 2016-08-31 17:02:44 +0000 | [diff] [blame] | 189 | } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 190 | |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 191 | bool opt(Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod, |
Davide Italiano | 1e9d3d3 | 2016-08-31 17:02:44 +0000 | [diff] [blame] | 192 | bool IsThinLto) { |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 193 | Mod.setDataLayout(TM->createDataLayout()); |
Davide Italiano | ec9612d | 2016-09-07 17:46:16 +0000 | [diff] [blame] | 194 | if (Conf.OptPipeline.empty()) |
| 195 | runOldPMPasses(Conf, Mod, TM, IsThinLto); |
| 196 | else |
Davide Italiano | 14e9e8a | 2016-09-16 21:03:21 +0000 | [diff] [blame] | 197 | runNewPMCustomPasses(Mod, TM, Conf.OptPipeline, Conf.AAPipeline, |
| 198 | Conf.DisableVerify); |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 199 | return !Conf.PostOptModuleHook || Conf.PostOptModuleHook(Task, Mod); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 202 | void codegen(Config &Conf, TargetMachine *TM, AddStreamFn AddStream, |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 203 | unsigned Task, Module &Mod) { |
| 204 | if (Conf.PreCodeGenModuleHook && !Conf.PreCodeGenModuleHook(Task, Mod)) |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 205 | return; |
| 206 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 207 | auto Stream = AddStream(Task); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 208 | legacy::PassManager CodeGenPasses; |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 209 | if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS, |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 210 | TargetMachine::CGFT_ObjectFile)) |
| 211 | report_fatal_error("Failed to setup codegen"); |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 212 | CodeGenPasses.run(Mod); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 215 | void splitCodeGen(Config &C, TargetMachine *TM, AddStreamFn AddStream, |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 216 | unsigned ParallelCodeGenParallelismLevel, |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 217 | std::unique_ptr<Module> Mod) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 218 | ThreadPool CodegenThreadPool(ParallelCodeGenParallelismLevel); |
| 219 | unsigned ThreadCount = 0; |
| 220 | const Target *T = &TM->getTarget(); |
| 221 | |
| 222 | SplitModule( |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 223 | std::move(Mod), ParallelCodeGenParallelismLevel, |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 224 | [&](std::unique_ptr<Module> MPart) { |
| 225 | // We want to clone the module in a new context to multi-thread the |
| 226 | // codegen. We do it by serializing partition modules to bitcode |
| 227 | // (while still on the main thread, in order to avoid data races) and |
| 228 | // spinning up new threads which deserialize the partitions into |
| 229 | // separate contexts. |
| 230 | // FIXME: Provide a more direct way to do this in LLVM. |
| 231 | SmallString<0> BC; |
| 232 | raw_svector_ostream BCOS(BC); |
| 233 | WriteBitcodeToFile(MPart.get(), BCOS); |
| 234 | |
| 235 | // Enqueue the task |
| 236 | CodegenThreadPool.async( |
| 237 | [&](const SmallString<0> &BC, unsigned ThreadId) { |
| 238 | LTOLLVMContext Ctx(C); |
| 239 | ErrorOr<std::unique_ptr<Module>> MOrErr = parseBitcodeFile( |
| 240 | MemoryBufferRef(StringRef(BC.data(), BC.size()), "ld-temp.o"), |
| 241 | Ctx); |
| 242 | if (!MOrErr) |
| 243 | report_fatal_error("Failed to read bitcode"); |
| 244 | std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get()); |
| 245 | |
| 246 | std::unique_ptr<TargetMachine> TM = |
| 247 | createTargetMachine(C, MPartInCtx->getTargetTriple(), T); |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 248 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 249 | codegen(C, TM.get(), AddStream, ThreadId, *MPartInCtx); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 250 | }, |
| 251 | // Pass BC using std::move to ensure that it get moved rather than |
| 252 | // copied into the thread's context. |
| 253 | std::move(BC), ThreadCount++); |
| 254 | }, |
| 255 | false); |
Peter Collingbourne | f75609e | 2016-09-29 03:29:28 +0000 | [diff] [blame^] | 256 | |
| 257 | // Because the inner lambda (which runs in a worker thread) captures our local |
| 258 | // variables, we need to wait for the worker threads to terminate before we |
| 259 | // can leave the function scope. |
Peter Collingbourne | 0d5636e | 2016-09-29 01:28:36 +0000 | [diff] [blame] | 260 | CodegenThreadPool.wait(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 263 | Expected<const Target *> initAndLookupTarget(Config &C, Module &Mod) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 264 | if (!C.OverrideTriple.empty()) |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 265 | Mod.setTargetTriple(C.OverrideTriple); |
| 266 | else if (Mod.getTargetTriple().empty()) |
| 267 | Mod.setTargetTriple(C.DefaultTriple); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 268 | |
| 269 | std::string Msg; |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 270 | const Target *T = TargetRegistry::lookupTarget(Mod.getTargetTriple(), Msg); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 271 | if (!T) |
| 272 | return make_error<StringError>(Msg, inconvertibleErrorCode()); |
| 273 | return T; |
| 274 | } |
| 275 | |
| 276 | } |
| 277 | |
Davide Italiano | dc8e07b | 2016-09-16 16:05:25 +0000 | [diff] [blame] | 278 | static void handleAsmUndefinedRefs(Module &Mod, TargetMachine &TM) { |
| 279 | // Collect the list of undefined symbols used in asm and update |
| 280 | // llvm.compiler.used to prevent optimization to drop these from the output. |
| 281 | StringSet<> AsmUndefinedRefs; |
| 282 | object::IRObjectFile::CollectAsmUndefinedRefs( |
| 283 | Triple(Mod.getTargetTriple()), Mod.getModuleInlineAsm(), |
| 284 | [&AsmUndefinedRefs](StringRef Name, object::BasicSymbolRef::Flags Flags) { |
| 285 | if (Flags & object::BasicSymbolRef::SF_Undefined) |
| 286 | AsmUndefinedRefs.insert(Name); |
| 287 | }); |
| 288 | updateCompilerUsed(Mod, TM, AsmUndefinedRefs); |
| 289 | } |
| 290 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 291 | Error lto::backend(Config &C, AddStreamFn AddStream, |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 292 | unsigned ParallelCodeGenParallelismLevel, |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 293 | std::unique_ptr<Module> Mod) { |
| 294 | Expected<const Target *> TOrErr = initAndLookupTarget(C, *Mod); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 295 | if (!TOrErr) |
| 296 | return TOrErr.takeError(); |
| 297 | |
| 298 | std::unique_ptr<TargetMachine> TM = |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 299 | createTargetMachine(C, Mod->getTargetTriple(), *TOrErr); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 300 | |
Davide Italiano | dc8e07b | 2016-09-16 16:05:25 +0000 | [diff] [blame] | 301 | handleAsmUndefinedRefs(*Mod, *TM); |
| 302 | |
Mehdi Amini | d310b47 | 2016-08-22 06:25:41 +0000 | [diff] [blame] | 303 | if (!C.CodeGenOnly) |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 304 | if (!opt(C, TM.get(), 0, *Mod, /*IsThinLto=*/false)) |
Mehdi Amini | d310b47 | 2016-08-22 06:25:41 +0000 | [diff] [blame] | 305 | return Error(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 306 | |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 307 | if (ParallelCodeGenParallelismLevel == 1) { |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 308 | codegen(C, TM.get(), AddStream, 0, *Mod); |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 309 | } else { |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 310 | splitCodeGen(C, TM.get(), AddStream, ParallelCodeGenParallelismLevel, |
Davide Italiano | 24c29b1 | 2016-09-07 01:08:31 +0000 | [diff] [blame] | 311 | std::move(Mod)); |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 312 | } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 313 | return Error(); |
| 314 | } |
| 315 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 316 | Error lto::thinBackend(Config &Conf, unsigned Task, AddStreamFn AddStream, |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 317 | Module &Mod, ModuleSummaryIndex &CombinedIndex, |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 318 | const FunctionImporter::ImportMapTy &ImportList, |
| 319 | const GVSummaryMapTy &DefinedGlobals, |
| 320 | MapVector<StringRef, MemoryBufferRef> &ModuleMap) { |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 321 | Expected<const Target *> TOrErr = initAndLookupTarget(Conf, Mod); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 322 | if (!TOrErr) |
| 323 | return TOrErr.takeError(); |
| 324 | |
| 325 | std::unique_ptr<TargetMachine> TM = |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 326 | createTargetMachine(Conf, Mod.getTargetTriple(), *TOrErr); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 327 | |
Davide Italiano | dc8e07b | 2016-09-16 16:05:25 +0000 | [diff] [blame] | 328 | handleAsmUndefinedRefs(Mod, *TM); |
| 329 | |
Mehdi Amini | d310b47 | 2016-08-22 06:25:41 +0000 | [diff] [blame] | 330 | if (Conf.CodeGenOnly) { |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 331 | codegen(Conf, TM.get(), AddStream, Task, Mod); |
Mehdi Amini | d310b47 | 2016-08-22 06:25:41 +0000 | [diff] [blame] | 332 | return Error(); |
| 333 | } |
| 334 | |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 335 | if (Conf.PreOptModuleHook && !Conf.PreOptModuleHook(Task, Mod)) |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 336 | return Error(); |
| 337 | |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 338 | renameModuleForThinLTO(Mod, CombinedIndex); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 339 | |
Mehdi Amini | 8ac7b32 | 2016-08-18 00:59:24 +0000 | [diff] [blame] | 340 | thinLTOResolveWeakForLinkerModule(Mod, DefinedGlobals); |
| 341 | |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 342 | if (Conf.PostPromoteModuleHook && !Conf.PostPromoteModuleHook(Task, Mod)) |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 343 | return Error(); |
| 344 | |
| 345 | if (!DefinedGlobals.empty()) |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 346 | thinLTOInternalizeModule(Mod, DefinedGlobals); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 347 | |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 348 | if (Conf.PostInternalizeModuleHook && |
| 349 | !Conf.PostInternalizeModuleHook(Task, Mod)) |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 350 | return Error(); |
| 351 | |
| 352 | auto ModuleLoader = [&](StringRef Identifier) { |
Mehdi Amini | 9ec5a61 | 2016-08-23 16:53:34 +0000 | [diff] [blame] | 353 | assert(Mod.getContext().isODRUniquingDebugTypes() && |
Davide Italiano | 63e8f44 | 2016-09-14 18:48:43 +0000 | [diff] [blame] | 354 | "ODR Type uniquing should be enabled on the context"); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 355 | return std::move(getLazyBitcodeModule(MemoryBuffer::getMemBuffer( |
| 356 | ModuleMap[Identifier], false), |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 357 | Mod.getContext(), |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 358 | /*ShouldLazyLoadMetadata=*/true) |
| 359 | .get()); |
| 360 | }; |
| 361 | |
| 362 | FunctionImporter Importer(CombinedIndex, ModuleLoader); |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 363 | Importer.importFunctions(Mod, ImportList); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 364 | |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 365 | if (Conf.PostImportModuleHook && !Conf.PostImportModuleHook(Task, Mod)) |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 366 | return Error(); |
| 367 | |
Mehdi Amini | acc50c4 | 2016-08-16 00:44:46 +0000 | [diff] [blame] | 368 | if (!opt(Conf, TM.get(), Task, Mod, /*IsThinLto=*/true)) |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 369 | return Error(); |
| 370 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 371 | codegen(Conf, TM.get(), AddStream, Task, Mod); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 372 | return Error(); |
| 373 | } |