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