Teresa Johnson | f99573b | 2016-08-11 12:56:40 +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" |
| 18 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 19 | #include "llvm/Analysis/TargetTransformInfo.h" |
| 20 | #include "llvm/Bitcode/ReaderWriter.h" |
| 21 | #include "llvm/IR/LegacyPassManager.h" |
| 22 | #include "llvm/MC/SubtargetFeature.h" |
| 23 | #include "llvm/Support/Error.h" |
| 24 | #include "llvm/Support/FileSystem.h" |
| 25 | #include "llvm/Support/TargetRegistry.h" |
| 26 | #include "llvm/Support/ThreadPool.h" |
| 27 | #include "llvm/Target/TargetMachine.h" |
| 28 | #include "llvm/Transforms/IPO.h" |
| 29 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
| 30 | #include "llvm/Transforms/Utils/FunctionImportUtils.h" |
| 31 | #include "llvm/Transforms/Utils/SplitModule.h" |
| 32 | |
| 33 | using namespace llvm; |
| 34 | using namespace lto; |
| 35 | |
| 36 | Error Config::addSaveTemps(std::string OutputFileName) { |
| 37 | ShouldDiscardValueNames = false; |
| 38 | |
| 39 | std::error_code EC; |
| 40 | ResolutionFile = make_unique<raw_fd_ostream>( |
| 41 | OutputFileName + ".resolution.txt", EC, sys::fs::OpenFlags::F_Text); |
| 42 | if (EC) |
| 43 | return errorCodeToError(EC); |
| 44 | |
| 45 | auto setHook = [&](std::string PathSuffix, ModuleHookFn &Hook) { |
| 46 | // Keep track of the hook provided by the linker, which also needs to run. |
| 47 | ModuleHookFn LinkerHook = Hook; |
| 48 | Hook = [=](size_t Task, Module &M) { |
| 49 | // If the linker's hook returned false, we need to pass that result |
| 50 | // through. |
| 51 | if (LinkerHook && !LinkerHook(Task, M)) |
| 52 | return false; |
| 53 | |
| 54 | std::string PathPrefix; |
| 55 | PathPrefix = OutputFileName; |
| 56 | if (Task != 0) |
| 57 | PathPrefix += "." + utostr(Task); |
| 58 | std::string Path = PathPrefix + "." + PathSuffix + ".bc"; |
| 59 | std::error_code EC; |
| 60 | raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None); |
| 61 | if (EC) { |
| 62 | // Because -save-temps is a debugging feature, we report the error |
| 63 | // directly and exit. |
| 64 | llvm::errs() << "failed to open " << Path << ": " << EC.message() |
| 65 | << '\n'; |
| 66 | exit(1); |
| 67 | } |
| 68 | WriteBitcodeToFile(&M, OS, /*ShouldPreserveUseListOrder=*/false); |
| 69 | return true; |
| 70 | }; |
| 71 | }; |
| 72 | |
| 73 | setHook("0.preopt", PreOptModuleHook); |
| 74 | setHook("1.promote", PostPromoteModuleHook); |
| 75 | setHook("2.internalize", PostInternalizeModuleHook); |
| 76 | setHook("3.import", PostImportModuleHook); |
| 77 | setHook("4.opt", PostOptModuleHook); |
| 78 | setHook("5.precodegen", PreCodeGenModuleHook); |
| 79 | |
| 80 | CombinedIndexHook = [=](const ModuleSummaryIndex &Index) { |
| 81 | std::string Path = OutputFileName + ".index.bc"; |
| 82 | std::error_code EC; |
| 83 | raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None); |
| 84 | if (EC) { |
| 85 | // Because -save-temps is a debugging feature, we report the error |
| 86 | // directly and exit. |
| 87 | llvm::errs() << "failed to open " << Path << ": " << EC.message() << '\n'; |
| 88 | exit(1); |
| 89 | } |
| 90 | WriteIndexToFile(Index, OS); |
| 91 | return true; |
| 92 | }; |
| 93 | |
| 94 | return Error(); |
| 95 | } |
| 96 | |
| 97 | namespace { |
| 98 | |
| 99 | std::unique_ptr<TargetMachine> |
| 100 | createTargetMachine(Config &C, StringRef TheTriple, const Target *TheTarget) { |
| 101 | SubtargetFeatures Features; |
| 102 | Features.getDefaultSubtargetFeatures(Triple(TheTriple)); |
| 103 | for (const std::string &A : C.MAttrs) |
| 104 | Features.AddFeature(A); |
| 105 | |
| 106 | return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine( |
| 107 | TheTriple, C.CPU, Features.getString(), C.Options, C.RelocModel, |
| 108 | C.CodeModel, C.CGOptLevel)); |
| 109 | } |
| 110 | |
| 111 | bool opt(Config &C, TargetMachine *TM, size_t Task, Module &M, bool IsThinLto) { |
| 112 | M.setDataLayout(TM->createDataLayout()); |
| 113 | |
| 114 | legacy::PassManager passes; |
| 115 | passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis())); |
| 116 | |
| 117 | PassManagerBuilder PMB; |
| 118 | PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM->getTargetTriple())); |
| 119 | PMB.Inliner = createFunctionInliningPass(); |
| 120 | // Unconditionally verify input since it is not verified before this |
| 121 | // point and has unknown origin. |
| 122 | PMB.VerifyInput = true; |
| 123 | PMB.VerifyOutput = !C.DisableVerify; |
| 124 | PMB.LoopVectorize = true; |
| 125 | PMB.SLPVectorize = true; |
| 126 | PMB.OptLevel = C.OptLevel; |
| 127 | if (IsThinLto) |
| 128 | PMB.populateThinLTOPassManager(passes); |
| 129 | else |
| 130 | PMB.populateLTOPassManager(passes); |
| 131 | passes.run(M); |
| 132 | |
| 133 | if (C.PostOptModuleHook && !C.PostOptModuleHook(Task, M)) |
| 134 | return false; |
| 135 | |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | void codegen(Config &C, TargetMachine *TM, AddStreamFn AddStream, size_t Task, |
| 140 | Module &M) { |
| 141 | if (C.PreCodeGenModuleHook && !C.PreCodeGenModuleHook(Task, M)) |
| 142 | return; |
| 143 | |
| 144 | std::unique_ptr<raw_pwrite_stream> OS = AddStream(Task); |
| 145 | legacy::PassManager CodeGenPasses; |
| 146 | if (TM->addPassesToEmitFile(CodeGenPasses, *OS, |
| 147 | TargetMachine::CGFT_ObjectFile)) |
| 148 | report_fatal_error("Failed to setup codegen"); |
| 149 | CodeGenPasses.run(M); |
| 150 | } |
| 151 | |
| 152 | void splitCodeGen(Config &C, TargetMachine *TM, AddStreamFn AddStream, |
| 153 | unsigned ParallelCodeGenParallelismLevel, |
| 154 | std::unique_ptr<Module> M) { |
| 155 | ThreadPool CodegenThreadPool(ParallelCodeGenParallelismLevel); |
| 156 | unsigned ThreadCount = 0; |
| 157 | const Target *T = &TM->getTarget(); |
| 158 | |
| 159 | SplitModule( |
| 160 | std::move(M), ParallelCodeGenParallelismLevel, |
| 161 | [&](std::unique_ptr<Module> MPart) { |
| 162 | // We want to clone the module in a new context to multi-thread the |
| 163 | // codegen. We do it by serializing partition modules to bitcode |
| 164 | // (while still on the main thread, in order to avoid data races) and |
| 165 | // spinning up new threads which deserialize the partitions into |
| 166 | // separate contexts. |
| 167 | // FIXME: Provide a more direct way to do this in LLVM. |
| 168 | SmallString<0> BC; |
| 169 | raw_svector_ostream BCOS(BC); |
| 170 | WriteBitcodeToFile(MPart.get(), BCOS); |
| 171 | |
| 172 | // Enqueue the task |
| 173 | CodegenThreadPool.async( |
| 174 | [&](const SmallString<0> &BC, unsigned ThreadId) { |
| 175 | LTOLLVMContext Ctx(C); |
| 176 | ErrorOr<std::unique_ptr<Module>> MOrErr = parseBitcodeFile( |
| 177 | MemoryBufferRef(StringRef(BC.data(), BC.size()), "ld-temp.o"), |
| 178 | Ctx); |
| 179 | if (!MOrErr) |
| 180 | report_fatal_error("Failed to read bitcode"); |
| 181 | std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get()); |
| 182 | |
| 183 | std::unique_ptr<TargetMachine> TM = |
| 184 | createTargetMachine(C, MPartInCtx->getTargetTriple(), T); |
| 185 | codegen(C, TM.get(), AddStream, ThreadId, *MPartInCtx); |
| 186 | }, |
| 187 | // Pass BC using std::move to ensure that it get moved rather than |
| 188 | // copied into the thread's context. |
| 189 | std::move(BC), ThreadCount++); |
| 190 | }, |
| 191 | false); |
| 192 | } |
| 193 | |
| 194 | Expected<const Target *> initAndLookupTarget(Config &C, Module &M) { |
| 195 | if (!C.OverrideTriple.empty()) |
| 196 | M.setTargetTriple(C.OverrideTriple); |
| 197 | else if (M.getTargetTriple().empty()) |
| 198 | M.setTargetTriple(C.DefaultTriple); |
| 199 | |
| 200 | std::string Msg; |
| 201 | const Target *T = TargetRegistry::lookupTarget(M.getTargetTriple(), Msg); |
| 202 | if (!T) |
| 203 | return make_error<StringError>(Msg, inconvertibleErrorCode()); |
| 204 | return T; |
| 205 | } |
| 206 | |
| 207 | } |
| 208 | |
| 209 | Error lto::backend(Config &C, AddStreamFn AddStream, |
| 210 | unsigned ParallelCodeGenParallelismLevel, |
| 211 | std::unique_ptr<Module> M) { |
| 212 | Expected<const Target *> TOrErr = initAndLookupTarget(C, *M); |
| 213 | if (!TOrErr) |
| 214 | return TOrErr.takeError(); |
| 215 | |
| 216 | std::unique_ptr<TargetMachine> TM = |
| 217 | createTargetMachine(C, M->getTargetTriple(), *TOrErr); |
| 218 | |
| 219 | if (!opt(C, TM.get(), 0, *M, /*IsThinLto=*/false)) |
| 220 | return Error(); |
| 221 | |
| 222 | if (ParallelCodeGenParallelismLevel == 1) |
| 223 | codegen(C, TM.get(), AddStream, 0, *M); |
| 224 | else |
| 225 | splitCodeGen(C, TM.get(), AddStream, ParallelCodeGenParallelismLevel, |
| 226 | std::move(M)); |
| 227 | return Error(); |
| 228 | } |
| 229 | |
| 230 | Error lto::thinBackend(Config &C, size_t Task, AddStreamFn AddStream, Module &M, |
| 231 | ModuleSummaryIndex &CombinedIndex, |
| 232 | const FunctionImporter::ImportMapTy &ImportList, |
| 233 | const GVSummaryMapTy &DefinedGlobals, |
| 234 | MapVector<StringRef, MemoryBufferRef> &ModuleMap) { |
| 235 | Expected<const Target *> TOrErr = initAndLookupTarget(C, M); |
| 236 | if (!TOrErr) |
| 237 | return TOrErr.takeError(); |
| 238 | |
| 239 | std::unique_ptr<TargetMachine> TM = |
| 240 | createTargetMachine(C, M.getTargetTriple(), *TOrErr); |
| 241 | |
| 242 | if (C.PreOptModuleHook && !C.PreOptModuleHook(Task, M)) |
| 243 | return Error(); |
| 244 | |
| 245 | thinLTOResolveWeakForLinkerModule(M, DefinedGlobals); |
| 246 | |
| 247 | renameModuleForThinLTO(M, CombinedIndex); |
| 248 | |
| 249 | if (C.PostPromoteModuleHook && !C.PostPromoteModuleHook(Task, M)) |
| 250 | return Error(); |
| 251 | |
| 252 | if (!DefinedGlobals.empty()) |
| 253 | thinLTOInternalizeModule(M, DefinedGlobals); |
| 254 | |
| 255 | if (C.PostInternalizeModuleHook && !C.PostInternalizeModuleHook(Task, M)) |
| 256 | return Error(); |
| 257 | |
| 258 | auto ModuleLoader = [&](StringRef Identifier) { |
| 259 | return std::move(getLazyBitcodeModule(MemoryBuffer::getMemBuffer( |
| 260 | ModuleMap[Identifier], false), |
| 261 | M.getContext(), |
| 262 | /*ShouldLazyLoadMetadata=*/true) |
| 263 | .get()); |
| 264 | }; |
| 265 | |
| 266 | FunctionImporter Importer(CombinedIndex, ModuleLoader); |
| 267 | Importer.importFunctions(M, ImportList); |
| 268 | |
| 269 | if (C.PostImportModuleHook && !C.PostImportModuleHook(Task, M)) |
| 270 | return Error(); |
| 271 | |
| 272 | if (!opt(C, TM.get(), Task, M, /*IsThinLto=*/true)) |
| 273 | return Error(); |
| 274 | |
| 275 | codegen(C, TM.get(), AddStream, Task, M); |
| 276 | return Error(); |
| 277 | } |