Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 1 | //===-ThinLTOCodeGenerator.cpp - LLVM Link Time Optimizer -----------------===// |
| 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 Thin Link Time Optimization library. This library is |
| 11 | // intended to be used by linker to optimize code at link time. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/LTO/ThinLTOCodeGenerator.h" |
| 16 | |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/ModuleSummaryAnalysis.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 21 | #include "llvm/Analysis/TargetTransformInfo.h" |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 22 | #include "llvm/Bitcode/BitcodeWriterPass.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 23 | #include "llvm/Bitcode/ReaderWriter.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 24 | #include "llvm/ExecutionEngine/ObjectMemoryBuffer.h" |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/DiagnosticPrinter.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 26 | #include "llvm/IR/LLVMContext.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 27 | #include "llvm/IR/LegacyPassManager.h" |
| 28 | #include "llvm/IR/Mangler.h" |
| 29 | #include "llvm/IRReader/IRReader.h" |
| 30 | #include "llvm/Linker/Linker.h" |
| 31 | #include "llvm/MC/SubtargetFeature.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 32 | #include "llvm/Object/ModuleSummaryIndexObjectFile.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 33 | #include "llvm/Support/SourceMgr.h" |
| 34 | #include "llvm/Support/TargetRegistry.h" |
| 35 | #include "llvm/Support/ThreadPool.h" |
| 36 | #include "llvm/Target/TargetMachine.h" |
| 37 | #include "llvm/Transforms/IPO.h" |
| 38 | #include "llvm/Transforms/IPO/FunctionImport.h" |
| 39 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
| 40 | #include "llvm/Transforms/ObjCARC.h" |
| 41 | #include "llvm/Transforms/Utils/FunctionImportUtils.h" |
| 42 | |
| 43 | using namespace llvm; |
| 44 | |
Mehdi Amini | 09b4a8d | 2016-03-10 01:28:54 +0000 | [diff] [blame] | 45 | namespace llvm { |
| 46 | // Flags -discard-value-names, defined in LTOCodeGenerator.cpp |
| 47 | extern cl::opt<bool> LTODiscardValueNames; |
| 48 | } |
| 49 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 50 | namespace { |
| 51 | |
| 52 | static cl::opt<int> ThreadCount("threads", |
| 53 | cl::init(std::thread::hardware_concurrency())); |
| 54 | |
| 55 | static void diagnosticHandler(const DiagnosticInfo &DI) { |
| 56 | DiagnosticPrinterRawOStream DP(errs()); |
| 57 | DI.print(DP); |
| 58 | errs() << '\n'; |
| 59 | } |
| 60 | |
| 61 | // Simple helper to load a module from bitcode |
| 62 | static std::unique_ptr<Module> |
| 63 | loadModuleFromBuffer(const MemoryBufferRef &Buffer, LLVMContext &Context, |
| 64 | bool Lazy) { |
| 65 | SMDiagnostic Err; |
| 66 | ErrorOr<std::unique_ptr<Module>> ModuleOrErr(nullptr); |
| 67 | if (Lazy) { |
| 68 | ModuleOrErr = |
| 69 | getLazyBitcodeModule(MemoryBuffer::getMemBuffer(Buffer, false), Context, |
| 70 | /* ShouldLazyLoadMetadata */ Lazy); |
| 71 | } else { |
| 72 | ModuleOrErr = parseBitcodeFile(Buffer, Context); |
| 73 | } |
| 74 | if (std::error_code EC = ModuleOrErr.getError()) { |
| 75 | Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error, |
| 76 | EC.message()); |
| 77 | Err.print("ThinLTO", errs()); |
| 78 | report_fatal_error("Can't load module, abort."); |
| 79 | } |
| 80 | return std::move(ModuleOrErr.get()); |
| 81 | } |
| 82 | |
| 83 | // Simple helper to save temporary files for debug. |
| 84 | static void saveTempBitcode(const Module &TheModule, StringRef TempDir, |
| 85 | unsigned count, StringRef Suffix) { |
| 86 | if (TempDir.empty()) |
| 87 | return; |
| 88 | // User asked to save temps, let dump the bitcode file after import. |
| 89 | auto SaveTempPath = TempDir + llvm::utostr(count) + Suffix; |
| 90 | std::error_code EC; |
| 91 | raw_fd_ostream OS(SaveTempPath.str(), EC, sys::fs::F_None); |
| 92 | if (EC) |
| 93 | report_fatal_error(Twine("Failed to open ") + SaveTempPath + |
| 94 | " to save optimized bitcode\n"); |
Teresa Johnson | 3c35e09 | 2016-04-04 21:19:31 +0000 | [diff] [blame] | 95 | WriteBitcodeToFile(&TheModule, OS, /* ShouldPreserveUseListOrder */ true); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 98 | bool IsFirstDefinitionForLinker(const GlobalValueInfoList &GVInfo, |
| 99 | const ModuleSummaryIndex &Index, |
| 100 | StringRef ModulePath) { |
| 101 | // Get the first *linker visible* definition for this global in the summary |
| 102 | // list. |
| 103 | auto FirstDefForLinker = llvm::find_if( |
| 104 | GVInfo, [](const std::unique_ptr<GlobalValueInfo> &FuncInfo) { |
| 105 | auto Linkage = FuncInfo->summary()->linkage(); |
| 106 | return !GlobalValue::isAvailableExternallyLinkage(Linkage); |
| 107 | }); |
| 108 | // If \p GV is not the first definition, give up... |
| 109 | if ((*FirstDefForLinker)->summary()->modulePath() != ModulePath) |
| 110 | return false; |
| 111 | // If there is any strong definition anywhere, do not bother emitting this. |
| 112 | if (llvm::any_of( |
| 113 | GVInfo, [](const std::unique_ptr<GlobalValueInfo> &FuncInfo) { |
| 114 | auto Linkage = FuncInfo->summary()->linkage(); |
| 115 | return !GlobalValue::isAvailableExternallyLinkage(Linkage) && |
| 116 | !GlobalValue::isWeakForLinker(Linkage); |
| 117 | })) |
| 118 | return false; |
| 119 | return true; |
Hans Wennborg | fa6e414 | 2016-04-02 01:03:41 +0000 | [diff] [blame] | 120 | } |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 121 | |
| 122 | static void ResolveODR(GlobalValue &GV, const ModuleSummaryIndex &Index, |
| 123 | StringRef ModulePath) { |
| 124 | if (GV.isDeclaration()) |
| 125 | return; |
| 126 | |
| 127 | auto HasMultipleCopies = |
| 128 | [&](const GlobalValueInfoList &GVInfo) { return GVInfo.size() > 1; }; |
| 129 | |
| 130 | auto getGVInfo = [&](GlobalValue &GV) -> const GlobalValueInfoList *{ |
| 131 | auto GUID = Function::getGlobalIdentifier(GV.getName(), GV.getLinkage(), |
| 132 | ModulePath); |
| 133 | auto It = Index.findGlobalValueInfoList(GV.getName()); |
| 134 | if (It == Index.end()) |
| 135 | return nullptr; |
| 136 | return &It->second; |
| 137 | }; |
| 138 | |
| 139 | switch (GV.getLinkage()) { |
| 140 | case GlobalValue::ExternalLinkage: |
| 141 | case GlobalValue::AvailableExternallyLinkage: |
| 142 | case GlobalValue::AppendingLinkage: |
| 143 | case GlobalValue::InternalLinkage: |
| 144 | case GlobalValue::PrivateLinkage: |
| 145 | case GlobalValue::ExternalWeakLinkage: |
| 146 | case GlobalValue::CommonLinkage: |
| 147 | case GlobalValue::LinkOnceAnyLinkage: |
| 148 | case GlobalValue::WeakAnyLinkage: |
| 149 | break; |
| 150 | case GlobalValue::LinkOnceODRLinkage: |
| 151 | case GlobalValue::WeakODRLinkage: { |
| 152 | auto *GVInfo = getGVInfo(GV); |
| 153 | if (!GVInfo) |
| 154 | break; |
| 155 | // We need to emit only one of these, the first module will keep |
| 156 | // it, but turned into a weak while the others will drop it. |
| 157 | if (!HasMultipleCopies(*GVInfo)) |
| 158 | break; |
| 159 | if (IsFirstDefinitionForLinker(*GVInfo, Index, ModulePath)) |
| 160 | GV.setLinkage(GlobalValue::WeakODRLinkage); |
| 161 | else |
| 162 | GV.setLinkage(GlobalValue::AvailableExternallyLinkage); |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /// Resolve LinkOnceODR and WeakODR. |
| 169 | /// |
| 170 | /// We'd like to drop these function if they are no longer referenced in the |
| 171 | /// current module. However there is a chance that another module is still |
| 172 | /// referencing them because of the import. We make sure we always emit at least |
| 173 | /// one copy. |
| 174 | static void ResolveODR(Module &TheModule, |
| 175 | const ModuleSummaryIndex &Index) { |
Mehdi Amini | 8dcc808 | 2016-04-14 08:46:22 +0000 | [diff] [blame^] | 176 | if (Index.modulePaths().size() == 1) |
| 177 | // Nothing to do if we don't have multiple modules |
| 178 | return; |
| 179 | |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 180 | // We won't optimize the globals that are referenced by an alias for now |
| 181 | // Ideally we should turn the alias into a global and duplicate the definition |
| 182 | // when needed. |
| 183 | DenseSet<GlobalValue *> GlobalInvolvedWithAlias; |
| 184 | for (auto &GA : TheModule.aliases()) { |
| 185 | auto *GO = GA.getBaseObject(); |
| 186 | if (auto *GV = dyn_cast<GlobalValue>(GO)) |
| 187 | GlobalInvolvedWithAlias.insert(GV); |
| 188 | } |
| 189 | // Process functions and global now |
| 190 | for (auto &GV : TheModule) { |
| 191 | if (!GlobalInvolvedWithAlias.count(&GV)) |
| 192 | ResolveODR(GV, Index, TheModule.getModuleIdentifier()); |
| 193 | } |
| 194 | for (auto &GV : TheModule.globals()) { |
| 195 | if (!GlobalInvolvedWithAlias.count(&GV)) |
| 196 | ResolveODR(GV, Index, TheModule.getModuleIdentifier()); |
| 197 | } |
| 198 | } |
| 199 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 200 | static StringMap<MemoryBufferRef> |
| 201 | generateModuleMap(const std::vector<MemoryBufferRef> &Modules) { |
| 202 | StringMap<MemoryBufferRef> ModuleMap; |
| 203 | for (auto &ModuleBuffer : Modules) { |
| 204 | assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) == |
| 205 | ModuleMap.end() && |
| 206 | "Expect unique Buffer Identifier"); |
| 207 | ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer; |
| 208 | } |
| 209 | return ModuleMap; |
| 210 | } |
| 211 | |
| 212 | /// Provide a "loader" for the FunctionImporter to access function from other |
| 213 | /// modules. |
| 214 | class ModuleLoader { |
| 215 | /// The context that will be used for importing. |
| 216 | LLVMContext &Context; |
| 217 | |
| 218 | /// Map from Module identifier to MemoryBuffer. Used by clients like the |
| 219 | /// FunctionImported to request loading a Module. |
| 220 | StringMap<MemoryBufferRef> &ModuleMap; |
| 221 | |
| 222 | public: |
| 223 | ModuleLoader(LLVMContext &Context, StringMap<MemoryBufferRef> &ModuleMap) |
| 224 | : Context(Context), ModuleMap(ModuleMap) {} |
| 225 | |
| 226 | /// Load a module on demand. |
| 227 | std::unique_ptr<Module> operator()(StringRef Identifier) { |
| 228 | return loadModuleFromBuffer(ModuleMap[Identifier], Context, /*Lazy*/ true); |
| 229 | } |
| 230 | }; |
| 231 | |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 232 | static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 233 | if (renameModuleForThinLTO(TheModule, Index)) |
| 234 | report_fatal_error("renameModuleForThinLTO failed"); |
| 235 | } |
| 236 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 237 | static void |
| 238 | crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index, |
| 239 | StringMap<MemoryBufferRef> &ModuleMap, |
| 240 | const FunctionImporter::ImportMapTy &ImportList) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 241 | ModuleLoader Loader(TheModule.getContext(), ModuleMap); |
| 242 | FunctionImporter Importer(Index, Loader); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 243 | Importer.importFunctions(TheModule, ImportList); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static void optimizeModule(Module &TheModule, TargetMachine &TM) { |
| 247 | // Populate the PassManager |
| 248 | PassManagerBuilder PMB; |
| 249 | PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple()); |
| 250 | PMB.Inliner = createFunctionInliningPass(); |
| 251 | // FIXME: should get it from the bitcode? |
| 252 | PMB.OptLevel = 3; |
| 253 | PMB.LoopVectorize = true; |
| 254 | PMB.SLPVectorize = true; |
| 255 | PMB.VerifyInput = true; |
| 256 | PMB.VerifyOutput = false; |
| 257 | |
| 258 | legacy::PassManager PM; |
| 259 | |
| 260 | // Add the TTI (required to inform the vectorizer about register size for |
| 261 | // instance) |
| 262 | PM.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis())); |
| 263 | |
| 264 | // Add optimizations |
| 265 | PMB.populateThinLTOPassManager(PM); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 266 | |
| 267 | PM.run(TheModule); |
| 268 | } |
| 269 | |
| 270 | std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule, |
| 271 | TargetMachine &TM) { |
| 272 | SmallVector<char, 128> OutputBuffer; |
| 273 | |
| 274 | // CodeGen |
| 275 | { |
| 276 | raw_svector_ostream OS(OutputBuffer); |
| 277 | legacy::PassManager PM; |
Mehdi Amini | 215d59e | 2016-04-01 08:22:59 +0000 | [diff] [blame] | 278 | |
| 279 | // If the bitcode files contain ARC code and were compiled with optimization, |
| 280 | // the ObjCARCContractPass must be run, so do it unconditionally here. |
| 281 | PM.add(createObjCARCContractPass()); |
| 282 | |
| 283 | // Setup the codegen now. |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 284 | if (TM.addPassesToEmitFile(PM, OS, TargetMachine::CGFT_ObjectFile, |
| 285 | /* DisableVerify */ true)) |
| 286 | report_fatal_error("Failed to setup codegen"); |
| 287 | |
| 288 | // Run codegen now. resulting binary is in OutputBuffer. |
| 289 | PM.run(TheModule); |
| 290 | } |
| 291 | return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer)); |
| 292 | } |
| 293 | |
| 294 | static std::unique_ptr<MemoryBuffer> |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 295 | ProcessThinLTOModule(Module &TheModule, const ModuleSummaryIndex &Index, |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 296 | StringMap<MemoryBufferRef> &ModuleMap, TargetMachine &TM, |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 297 | const FunctionImporter::ImportMapTy &ImportList, |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 298 | ThinLTOCodeGenerator::CachingOptions CacheOptions, |
Mehdi Amini | 43b657b | 2016-04-01 06:47:02 +0000 | [diff] [blame] | 299 | bool DisableCodeGen, StringRef SaveTempsDir, |
| 300 | unsigned count) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 301 | |
| 302 | // Save temps: after IPO. |
| 303 | saveTempBitcode(TheModule, SaveTempsDir, count, ".1.IPO.bc"); |
| 304 | |
| 305 | // "Benchmark"-like optimization: single-source case |
| 306 | bool SingleModule = (ModuleMap.size() == 1); |
| 307 | |
| 308 | if (!SingleModule) { |
| 309 | promoteModule(TheModule, Index); |
| 310 | |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 311 | // Resolve the LinkOnce/Weak ODR, trying to turn them into |
| 312 | // "available_externally" when possible. |
| 313 | // This is a compile-time optimization. |
| 314 | ResolveODR(TheModule, Index); |
| 315 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 316 | // Save temps: after promotion. |
| 317 | saveTempBitcode(TheModule, SaveTempsDir, count, ".2.promoted.bc"); |
| 318 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 319 | crossImportIntoModule(TheModule, Index, ModuleMap, ImportList); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 320 | |
| 321 | // Save temps: after cross-module import. |
| 322 | saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc"); |
| 323 | } |
| 324 | |
| 325 | optimizeModule(TheModule, TM); |
| 326 | |
| 327 | saveTempBitcode(TheModule, SaveTempsDir, count, ".3.opt.bc"); |
| 328 | |
Mehdi Amini | 43b657b | 2016-04-01 06:47:02 +0000 | [diff] [blame] | 329 | if (DisableCodeGen) { |
| 330 | // Configured to stop before CodeGen, serialize the bitcode and return. |
| 331 | SmallVector<char, 128> OutputBuffer; |
| 332 | { |
| 333 | raw_svector_ostream OS(OutputBuffer); |
Teresa Johnson | 2d5487c | 2016-04-11 13:58:45 +0000 | [diff] [blame] | 334 | ModuleSummaryIndexBuilder IndexBuilder(&TheModule); |
| 335 | WriteBitcodeToFile(&TheModule, OS, true, &IndexBuilder.getIndex()); |
Mehdi Amini | 43b657b | 2016-04-01 06:47:02 +0000 | [diff] [blame] | 336 | } |
| 337 | return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer)); |
| 338 | } |
| 339 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 340 | return codegenModule(TheModule, TM); |
| 341 | } |
| 342 | |
| 343 | // Initialize the TargetMachine builder for a given Triple |
| 344 | static void initTMBuilder(TargetMachineBuilder &TMBuilder, |
| 345 | const Triple &TheTriple) { |
| 346 | // Set a default CPU for Darwin triples (copied from LTOCodeGenerator). |
| 347 | // FIXME this looks pretty terrible... |
| 348 | if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) { |
| 349 | if (TheTriple.getArch() == llvm::Triple::x86_64) |
| 350 | TMBuilder.MCpu = "core2"; |
| 351 | else if (TheTriple.getArch() == llvm::Triple::x86) |
| 352 | TMBuilder.MCpu = "yonah"; |
| 353 | else if (TheTriple.getArch() == llvm::Triple::aarch64) |
| 354 | TMBuilder.MCpu = "cyclone"; |
| 355 | } |
| 356 | TMBuilder.TheTriple = std::move(TheTriple); |
| 357 | } |
| 358 | |
| 359 | } // end anonymous namespace |
| 360 | |
| 361 | void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) { |
| 362 | MemoryBufferRef Buffer(Data, Identifier); |
| 363 | if (Modules.empty()) { |
| 364 | // First module added, so initialize the triple and some options |
| 365 | LLVMContext Context; |
| 366 | Triple TheTriple(getBitcodeTargetTriple(Buffer, Context)); |
| 367 | initTMBuilder(TMBuilder, Triple(TheTriple)); |
| 368 | } |
| 369 | #ifndef NDEBUG |
| 370 | else { |
| 371 | LLVMContext Context; |
| 372 | assert(TMBuilder.TheTriple.str() == |
| 373 | getBitcodeTargetTriple(Buffer, Context) && |
| 374 | "ThinLTO modules with different triple not supported"); |
| 375 | } |
| 376 | #endif |
| 377 | Modules.push_back(Buffer); |
| 378 | } |
| 379 | |
| 380 | void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) { |
| 381 | PreservedSymbols.insert(Name); |
| 382 | } |
| 383 | |
| 384 | void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) { |
| 385 | CrossReferencedSymbols.insert(Name); |
| 386 | } |
| 387 | |
| 388 | // TargetMachine factory |
| 389 | std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const { |
| 390 | std::string ErrMsg; |
| 391 | const Target *TheTarget = |
| 392 | TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg); |
| 393 | if (!TheTarget) { |
| 394 | report_fatal_error("Can't load target for this Triple: " + ErrMsg); |
| 395 | } |
| 396 | |
| 397 | // Use MAttr as the default set of features. |
| 398 | SubtargetFeatures Features(MAttr); |
| 399 | Features.getDefaultSubtargetFeatures(TheTriple); |
| 400 | std::string FeatureStr = Features.getString(); |
| 401 | return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine( |
| 402 | TheTriple.str(), MCpu, FeatureStr, Options, RelocModel, |
| 403 | CodeModel::Default, CGOptLevel)); |
| 404 | } |
| 405 | |
| 406 | /** |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 407 | * Produce the combined summary index from all the bitcode files: |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 408 | * "thin-link". |
| 409 | */ |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 410 | std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() { |
| 411 | std::unique_ptr<ModuleSummaryIndex> CombinedIndex; |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 412 | uint64_t NextModuleId = 0; |
| 413 | for (auto &ModuleBuffer : Modules) { |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 414 | ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr = |
| 415 | object::ModuleSummaryIndexObjectFile::create(ModuleBuffer, |
| 416 | diagnosticHandler, false); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 417 | if (std::error_code EC = ObjOrErr.getError()) { |
| 418 | // FIXME diagnose |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 419 | errs() << "error: can't create ModuleSummaryIndexObjectFile for buffer: " |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 420 | << EC.message() << "\n"; |
| 421 | return nullptr; |
| 422 | } |
| 423 | auto Index = (*ObjOrErr)->takeIndex(); |
| 424 | if (CombinedIndex) { |
| 425 | CombinedIndex->mergeFrom(std::move(Index), ++NextModuleId); |
| 426 | } else { |
| 427 | CombinedIndex = std::move(Index); |
| 428 | } |
| 429 | } |
| 430 | return CombinedIndex; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Perform promotion and renaming of exported internal functions. |
| 435 | */ |
| 436 | void ThinLTOCodeGenerator::promote(Module &TheModule, |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 437 | ModuleSummaryIndex &Index) { |
Mehdi Amini | 5a2e5d3 | 2016-04-01 21:53:50 +0000 | [diff] [blame] | 438 | |
| 439 | // Resolve the LinkOnceODR, trying to turn them into "available_externally" |
| 440 | // where possible. |
| 441 | ResolveODR(TheModule, Index); |
| 442 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 443 | promoteModule(TheModule, Index); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Perform cross-module importing for the module identified by ModuleIdentifier. |
| 448 | */ |
| 449 | void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule, |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 450 | ModuleSummaryIndex &Index) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 451 | auto ModuleMap = generateModuleMap(Modules); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 452 | |
| 453 | // Generate import/export list |
| 454 | auto ModuleCount = Index.modulePaths().size(); |
| 455 | StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); |
| 456 | StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); |
| 457 | ComputeCrossModuleImport(Index, ImportLists, ExportLists); |
| 458 | auto &ImportList = ImportLists[TheModule.getModuleIdentifier()]; |
| 459 | |
| 460 | crossImportIntoModule(TheModule, Index, ModuleMap, ImportList); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Perform post-importing ThinLTO optimizations. |
| 465 | */ |
| 466 | void ThinLTOCodeGenerator::optimize(Module &TheModule) { |
| 467 | initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple())); |
| 468 | optimizeModule(TheModule, *TMBuilder.create()); |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Perform ThinLTO CodeGen. |
| 473 | */ |
| 474 | std::unique_ptr<MemoryBuffer> ThinLTOCodeGenerator::codegen(Module &TheModule) { |
| 475 | initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple())); |
| 476 | return codegenModule(TheModule, *TMBuilder.create()); |
| 477 | } |
| 478 | |
| 479 | // Main entry point for the ThinLTO processing |
| 480 | void ThinLTOCodeGenerator::run() { |
Mehdi Amini | 43b657b | 2016-04-01 06:47:02 +0000 | [diff] [blame] | 481 | if (CodeGenOnly) { |
| 482 | // Perform only parallel codegen and return. |
| 483 | ThreadPool Pool; |
| 484 | assert(ProducedBinaries.empty() && "The generator should not be reused"); |
| 485 | ProducedBinaries.resize(Modules.size()); |
| 486 | int count = 0; |
| 487 | for (auto &ModuleBuffer : Modules) { |
| 488 | Pool.async([&](int count) { |
| 489 | LLVMContext Context; |
| 490 | Context.setDiscardValueNames(LTODiscardValueNames); |
| 491 | |
| 492 | // Parse module now |
| 493 | auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false); |
| 494 | |
| 495 | // CodeGen |
| 496 | ProducedBinaries[count] = codegen(*TheModule); |
| 497 | }, count++); |
| 498 | } |
| 499 | |
| 500 | return; |
| 501 | } |
| 502 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 503 | // Sequential linking phase |
| 504 | auto Index = linkCombinedIndex(); |
| 505 | |
| 506 | // Save temps: index. |
| 507 | if (!SaveTempsDir.empty()) { |
| 508 | auto SaveTempPath = SaveTempsDir + "index.bc"; |
| 509 | std::error_code EC; |
| 510 | raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None); |
| 511 | if (EC) |
| 512 | report_fatal_error(Twine("Failed to open ") + SaveTempPath + |
| 513 | " to save optimized bitcode\n"); |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 514 | WriteIndexToFile(*Index, OS); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | // Prepare the resulting object vector |
| 518 | assert(ProducedBinaries.empty() && "The generator should not be reused"); |
| 519 | ProducedBinaries.resize(Modules.size()); |
| 520 | |
| 521 | // Prepare the module map. |
| 522 | auto ModuleMap = generateModuleMap(Modules); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 523 | auto ModuleCount = Modules.size(); |
| 524 | |
| 525 | // Collect the import/export lists for all modules from the call-graph in the |
| 526 | // combined index. |
| 527 | StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); |
| 528 | StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); |
| 529 | ComputeCrossModuleImport(*Index, ImportLists, ExportLists); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 530 | |
| 531 | // Parallel optimizer + codegen |
| 532 | { |
| 533 | ThreadPool Pool(ThreadCount); |
| 534 | int count = 0; |
| 535 | for (auto &ModuleBuffer : Modules) { |
| 536 | Pool.async([&](int count) { |
| 537 | LLVMContext Context; |
Mehdi Amini | 09b4a8d | 2016-03-10 01:28:54 +0000 | [diff] [blame] | 538 | Context.setDiscardValueNames(LTODiscardValueNames); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 539 | |
| 540 | // Parse module now |
| 541 | auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false); |
| 542 | |
| 543 | // Save temps: original file. |
| 544 | if (!SaveTempsDir.empty()) { |
| 545 | saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc"); |
| 546 | } |
| 547 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 548 | auto &ImportList = ImportLists[TheModule->getModuleIdentifier()]; |
Mehdi Amini | 024a79f | 2016-04-02 05:08:18 +0000 | [diff] [blame] | 549 | ProducedBinaries[count] = ProcessThinLTOModule( |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 550 | *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList, |
Mehdi Amini | 43b657b | 2016-04-01 06:47:02 +0000 | [diff] [blame] | 551 | CacheOptions, DisableCodeGen, SaveTempsDir, count); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 552 | }, count); |
| 553 | count++; |
| 554 | } |
| 555 | } |
| 556 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 557 | // If statistics were requested, print them out now. |
| 558 | if (llvm::AreStatisticsEnabled()) |
| 559 | llvm::PrintStatistics(); |
| 560 | } |