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" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 20 | #include "llvm/Analysis/TargetTransformInfo.h" |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 21 | #include "llvm/Bitcode/BitcodeWriterPass.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 22 | #include "llvm/Bitcode/ReaderWriter.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 23 | #include "llvm/ExecutionEngine/ObjectMemoryBuffer.h" |
Teresa Johnson | cec0cae | 2016-03-14 21:18:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/DiagnosticPrinter.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 25 | #include "llvm/IR/LLVMContext.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 26 | #include "llvm/IR/LegacyPassManager.h" |
| 27 | #include "llvm/IR/Mangler.h" |
| 28 | #include "llvm/IRReader/IRReader.h" |
| 29 | #include "llvm/Linker/Linker.h" |
| 30 | #include "llvm/MC/SubtargetFeature.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 31 | #include "llvm/Object/ModuleSummaryIndexObjectFile.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 32 | #include "llvm/Support/SourceMgr.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/FunctionImport.h" |
| 38 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
| 39 | #include "llvm/Transforms/ObjCARC.h" |
| 40 | #include "llvm/Transforms/Utils/FunctionImportUtils.h" |
| 41 | |
| 42 | using namespace llvm; |
| 43 | |
Mehdi Amini | 09b4a8d | 2016-03-10 01:28:54 +0000 | [diff] [blame] | 44 | namespace llvm { |
| 45 | // Flags -discard-value-names, defined in LTOCodeGenerator.cpp |
| 46 | extern cl::opt<bool> LTODiscardValueNames; |
| 47 | } |
| 48 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 49 | namespace { |
| 50 | |
| 51 | static cl::opt<int> ThreadCount("threads", |
| 52 | cl::init(std::thread::hardware_concurrency())); |
| 53 | |
| 54 | static void diagnosticHandler(const DiagnosticInfo &DI) { |
| 55 | DiagnosticPrinterRawOStream DP(errs()); |
| 56 | DI.print(DP); |
| 57 | errs() << '\n'; |
| 58 | } |
| 59 | |
| 60 | // Simple helper to load a module from bitcode |
| 61 | static std::unique_ptr<Module> |
| 62 | loadModuleFromBuffer(const MemoryBufferRef &Buffer, LLVMContext &Context, |
| 63 | bool Lazy) { |
| 64 | SMDiagnostic Err; |
| 65 | ErrorOr<std::unique_ptr<Module>> ModuleOrErr(nullptr); |
| 66 | if (Lazy) { |
| 67 | ModuleOrErr = |
| 68 | getLazyBitcodeModule(MemoryBuffer::getMemBuffer(Buffer, false), Context, |
| 69 | /* ShouldLazyLoadMetadata */ Lazy); |
| 70 | } else { |
| 71 | ModuleOrErr = parseBitcodeFile(Buffer, Context); |
| 72 | } |
| 73 | if (std::error_code EC = ModuleOrErr.getError()) { |
| 74 | Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error, |
| 75 | EC.message()); |
| 76 | Err.print("ThinLTO", errs()); |
| 77 | report_fatal_error("Can't load module, abort."); |
| 78 | } |
| 79 | return std::move(ModuleOrErr.get()); |
| 80 | } |
| 81 | |
| 82 | // Simple helper to save temporary files for debug. |
| 83 | static void saveTempBitcode(const Module &TheModule, StringRef TempDir, |
| 84 | unsigned count, StringRef Suffix) { |
| 85 | if (TempDir.empty()) |
| 86 | return; |
| 87 | // User asked to save temps, let dump the bitcode file after import. |
| 88 | auto SaveTempPath = TempDir + llvm::utostr(count) + Suffix; |
| 89 | std::error_code EC; |
| 90 | raw_fd_ostream OS(SaveTempPath.str(), EC, sys::fs::F_None); |
| 91 | if (EC) |
| 92 | report_fatal_error(Twine("Failed to open ") + SaveTempPath + |
| 93 | " to save optimized bitcode\n"); |
| 94 | WriteBitcodeToFile(&TheModule, OS, true, false); |
| 95 | } |
| 96 | |
| 97 | static StringMap<MemoryBufferRef> |
| 98 | generateModuleMap(const std::vector<MemoryBufferRef> &Modules) { |
| 99 | StringMap<MemoryBufferRef> ModuleMap; |
| 100 | for (auto &ModuleBuffer : Modules) { |
| 101 | assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) == |
| 102 | ModuleMap.end() && |
| 103 | "Expect unique Buffer Identifier"); |
| 104 | ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer; |
| 105 | } |
| 106 | return ModuleMap; |
| 107 | } |
| 108 | |
| 109 | /// Provide a "loader" for the FunctionImporter to access function from other |
| 110 | /// modules. |
| 111 | class ModuleLoader { |
| 112 | /// The context that will be used for importing. |
| 113 | LLVMContext &Context; |
| 114 | |
| 115 | /// Map from Module identifier to MemoryBuffer. Used by clients like the |
| 116 | /// FunctionImported to request loading a Module. |
| 117 | StringMap<MemoryBufferRef> &ModuleMap; |
| 118 | |
| 119 | public: |
| 120 | ModuleLoader(LLVMContext &Context, StringMap<MemoryBufferRef> &ModuleMap) |
| 121 | : Context(Context), ModuleMap(ModuleMap) {} |
| 122 | |
| 123 | /// Load a module on demand. |
| 124 | std::unique_ptr<Module> operator()(StringRef Identifier) { |
| 125 | return loadModuleFromBuffer(ModuleMap[Identifier], Context, /*Lazy*/ true); |
| 126 | } |
| 127 | }; |
| 128 | |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 129 | static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 130 | if (renameModuleForThinLTO(TheModule, Index)) |
| 131 | report_fatal_error("renameModuleForThinLTO failed"); |
| 132 | } |
| 133 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 134 | static void |
| 135 | crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index, |
| 136 | StringMap<MemoryBufferRef> &ModuleMap, |
| 137 | const FunctionImporter::ImportMapTy &ImportList) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 138 | ModuleLoader Loader(TheModule.getContext(), ModuleMap); |
| 139 | FunctionImporter Importer(Index, Loader); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 140 | Importer.importFunctions(TheModule, ImportList); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static void optimizeModule(Module &TheModule, TargetMachine &TM) { |
| 144 | // Populate the PassManager |
| 145 | PassManagerBuilder PMB; |
| 146 | PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple()); |
| 147 | PMB.Inliner = createFunctionInliningPass(); |
| 148 | // FIXME: should get it from the bitcode? |
| 149 | PMB.OptLevel = 3; |
| 150 | PMB.LoopVectorize = true; |
| 151 | PMB.SLPVectorize = true; |
| 152 | PMB.VerifyInput = true; |
| 153 | PMB.VerifyOutput = false; |
| 154 | |
| 155 | legacy::PassManager PM; |
| 156 | |
| 157 | // Add the TTI (required to inform the vectorizer about register size for |
| 158 | // instance) |
| 159 | PM.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis())); |
| 160 | |
| 161 | // Add optimizations |
| 162 | PMB.populateThinLTOPassManager(PM); |
| 163 | PM.add(createObjCARCContractPass()); |
| 164 | |
| 165 | PM.run(TheModule); |
| 166 | } |
| 167 | |
| 168 | std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule, |
| 169 | TargetMachine &TM) { |
| 170 | SmallVector<char, 128> OutputBuffer; |
| 171 | |
| 172 | // CodeGen |
| 173 | { |
| 174 | raw_svector_ostream OS(OutputBuffer); |
| 175 | legacy::PassManager PM; |
| 176 | if (TM.addPassesToEmitFile(PM, OS, TargetMachine::CGFT_ObjectFile, |
| 177 | /* DisableVerify */ true)) |
| 178 | report_fatal_error("Failed to setup codegen"); |
| 179 | |
| 180 | // Run codegen now. resulting binary is in OutputBuffer. |
| 181 | PM.run(TheModule); |
| 182 | } |
| 183 | return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer)); |
| 184 | } |
| 185 | |
| 186 | static std::unique_ptr<MemoryBuffer> |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 187 | ProcessThinLTOModule(Module &TheModule, const ModuleSummaryIndex &Index, |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 188 | StringMap<MemoryBufferRef> &ModuleMap, TargetMachine &TM, |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 189 | const FunctionImporter::ImportMapTy &ImportList, |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 190 | ThinLTOCodeGenerator::CachingOptions CacheOptions, |
Mehdi Amini | 43b657b | 2016-04-01 06:47:02 +0000 | [diff] [blame^] | 191 | bool DisableCodeGen, StringRef SaveTempsDir, |
| 192 | unsigned count) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 193 | |
| 194 | // Save temps: after IPO. |
| 195 | saveTempBitcode(TheModule, SaveTempsDir, count, ".1.IPO.bc"); |
| 196 | |
| 197 | // "Benchmark"-like optimization: single-source case |
| 198 | bool SingleModule = (ModuleMap.size() == 1); |
| 199 | |
| 200 | if (!SingleModule) { |
| 201 | promoteModule(TheModule, Index); |
| 202 | |
| 203 | // Save temps: after promotion. |
| 204 | saveTempBitcode(TheModule, SaveTempsDir, count, ".2.promoted.bc"); |
| 205 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 206 | crossImportIntoModule(TheModule, Index, ModuleMap, ImportList); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 207 | |
| 208 | // Save temps: after cross-module import. |
| 209 | saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc"); |
| 210 | } |
| 211 | |
| 212 | optimizeModule(TheModule, TM); |
| 213 | |
| 214 | saveTempBitcode(TheModule, SaveTempsDir, count, ".3.opt.bc"); |
| 215 | |
Mehdi Amini | 43b657b | 2016-04-01 06:47:02 +0000 | [diff] [blame^] | 216 | if (DisableCodeGen) { |
| 217 | // Configured to stop before CodeGen, serialize the bitcode and return. |
| 218 | SmallVector<char, 128> OutputBuffer; |
| 219 | { |
| 220 | raw_svector_ostream OS(OutputBuffer); |
| 221 | WriteBitcodeToFile(&TheModule, OS, true, true); |
| 222 | } |
| 223 | return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer)); |
| 224 | } |
| 225 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 226 | return codegenModule(TheModule, TM); |
| 227 | } |
| 228 | |
| 229 | // Initialize the TargetMachine builder for a given Triple |
| 230 | static void initTMBuilder(TargetMachineBuilder &TMBuilder, |
| 231 | const Triple &TheTriple) { |
| 232 | // Set a default CPU for Darwin triples (copied from LTOCodeGenerator). |
| 233 | // FIXME this looks pretty terrible... |
| 234 | if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) { |
| 235 | if (TheTriple.getArch() == llvm::Triple::x86_64) |
| 236 | TMBuilder.MCpu = "core2"; |
| 237 | else if (TheTriple.getArch() == llvm::Triple::x86) |
| 238 | TMBuilder.MCpu = "yonah"; |
| 239 | else if (TheTriple.getArch() == llvm::Triple::aarch64) |
| 240 | TMBuilder.MCpu = "cyclone"; |
| 241 | } |
| 242 | TMBuilder.TheTriple = std::move(TheTriple); |
| 243 | } |
| 244 | |
| 245 | } // end anonymous namespace |
| 246 | |
| 247 | void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) { |
| 248 | MemoryBufferRef Buffer(Data, Identifier); |
| 249 | if (Modules.empty()) { |
| 250 | // First module added, so initialize the triple and some options |
| 251 | LLVMContext Context; |
| 252 | Triple TheTriple(getBitcodeTargetTriple(Buffer, Context)); |
| 253 | initTMBuilder(TMBuilder, Triple(TheTriple)); |
| 254 | } |
| 255 | #ifndef NDEBUG |
| 256 | else { |
| 257 | LLVMContext Context; |
| 258 | assert(TMBuilder.TheTriple.str() == |
| 259 | getBitcodeTargetTriple(Buffer, Context) && |
| 260 | "ThinLTO modules with different triple not supported"); |
| 261 | } |
| 262 | #endif |
| 263 | Modules.push_back(Buffer); |
| 264 | } |
| 265 | |
| 266 | void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) { |
| 267 | PreservedSymbols.insert(Name); |
| 268 | } |
| 269 | |
| 270 | void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) { |
| 271 | CrossReferencedSymbols.insert(Name); |
| 272 | } |
| 273 | |
| 274 | // TargetMachine factory |
| 275 | std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const { |
| 276 | std::string ErrMsg; |
| 277 | const Target *TheTarget = |
| 278 | TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg); |
| 279 | if (!TheTarget) { |
| 280 | report_fatal_error("Can't load target for this Triple: " + ErrMsg); |
| 281 | } |
| 282 | |
| 283 | // Use MAttr as the default set of features. |
| 284 | SubtargetFeatures Features(MAttr); |
| 285 | Features.getDefaultSubtargetFeatures(TheTriple); |
| 286 | std::string FeatureStr = Features.getString(); |
| 287 | return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine( |
| 288 | TheTriple.str(), MCpu, FeatureStr, Options, RelocModel, |
| 289 | CodeModel::Default, CGOptLevel)); |
| 290 | } |
| 291 | |
| 292 | /** |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 293 | * Produce the combined summary index from all the bitcode files: |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 294 | * "thin-link". |
| 295 | */ |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 296 | std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() { |
| 297 | std::unique_ptr<ModuleSummaryIndex> CombinedIndex; |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 298 | uint64_t NextModuleId = 0; |
| 299 | for (auto &ModuleBuffer : Modules) { |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 300 | ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr = |
| 301 | object::ModuleSummaryIndexObjectFile::create(ModuleBuffer, |
| 302 | diagnosticHandler, false); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 303 | if (std::error_code EC = ObjOrErr.getError()) { |
| 304 | // FIXME diagnose |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 305 | errs() << "error: can't create ModuleSummaryIndexObjectFile for buffer: " |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 306 | << EC.message() << "\n"; |
| 307 | return nullptr; |
| 308 | } |
| 309 | auto Index = (*ObjOrErr)->takeIndex(); |
| 310 | if (CombinedIndex) { |
| 311 | CombinedIndex->mergeFrom(std::move(Index), ++NextModuleId); |
| 312 | } else { |
| 313 | CombinedIndex = std::move(Index); |
| 314 | } |
| 315 | } |
| 316 | return CombinedIndex; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Perform promotion and renaming of exported internal functions. |
| 321 | */ |
| 322 | void ThinLTOCodeGenerator::promote(Module &TheModule, |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 323 | ModuleSummaryIndex &Index) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 324 | promoteModule(TheModule, Index); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Perform cross-module importing for the module identified by ModuleIdentifier. |
| 329 | */ |
| 330 | void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule, |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 331 | ModuleSummaryIndex &Index) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 332 | auto ModuleMap = generateModuleMap(Modules); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 333 | |
| 334 | // Generate import/export list |
| 335 | auto ModuleCount = Index.modulePaths().size(); |
| 336 | StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); |
| 337 | StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); |
| 338 | ComputeCrossModuleImport(Index, ImportLists, ExportLists); |
| 339 | auto &ImportList = ImportLists[TheModule.getModuleIdentifier()]; |
| 340 | |
| 341 | crossImportIntoModule(TheModule, Index, ModuleMap, ImportList); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Perform post-importing ThinLTO optimizations. |
| 346 | */ |
| 347 | void ThinLTOCodeGenerator::optimize(Module &TheModule) { |
| 348 | initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple())); |
| 349 | optimizeModule(TheModule, *TMBuilder.create()); |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Perform ThinLTO CodeGen. |
| 354 | */ |
| 355 | std::unique_ptr<MemoryBuffer> ThinLTOCodeGenerator::codegen(Module &TheModule) { |
| 356 | initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple())); |
| 357 | return codegenModule(TheModule, *TMBuilder.create()); |
| 358 | } |
| 359 | |
| 360 | // Main entry point for the ThinLTO processing |
| 361 | void ThinLTOCodeGenerator::run() { |
Mehdi Amini | 43b657b | 2016-04-01 06:47:02 +0000 | [diff] [blame^] | 362 | if (CodeGenOnly) { |
| 363 | // Perform only parallel codegen and return. |
| 364 | ThreadPool Pool; |
| 365 | assert(ProducedBinaries.empty() && "The generator should not be reused"); |
| 366 | ProducedBinaries.resize(Modules.size()); |
| 367 | int count = 0; |
| 368 | for (auto &ModuleBuffer : Modules) { |
| 369 | Pool.async([&](int count) { |
| 370 | LLVMContext Context; |
| 371 | Context.setDiscardValueNames(LTODiscardValueNames); |
| 372 | |
| 373 | // Parse module now |
| 374 | auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false); |
| 375 | |
| 376 | // CodeGen |
| 377 | ProducedBinaries[count] = codegen(*TheModule); |
| 378 | }, count++); |
| 379 | } |
| 380 | |
| 381 | return; |
| 382 | } |
| 383 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 384 | // Sequential linking phase |
| 385 | auto Index = linkCombinedIndex(); |
| 386 | |
| 387 | // Save temps: index. |
| 388 | if (!SaveTempsDir.empty()) { |
| 389 | auto SaveTempPath = SaveTempsDir + "index.bc"; |
| 390 | std::error_code EC; |
| 391 | raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None); |
| 392 | if (EC) |
| 393 | report_fatal_error(Twine("Failed to open ") + SaveTempPath + |
| 394 | " to save optimized bitcode\n"); |
Teresa Johnson | 76a1c1d | 2016-03-11 18:52:24 +0000 | [diff] [blame] | 395 | WriteIndexToFile(*Index, OS); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | // Prepare the resulting object vector |
| 399 | assert(ProducedBinaries.empty() && "The generator should not be reused"); |
| 400 | ProducedBinaries.resize(Modules.size()); |
| 401 | |
| 402 | // Prepare the module map. |
| 403 | auto ModuleMap = generateModuleMap(Modules); |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 404 | auto ModuleCount = Modules.size(); |
| 405 | |
| 406 | // Collect the import/export lists for all modules from the call-graph in the |
| 407 | // combined index. |
| 408 | StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount); |
| 409 | StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount); |
| 410 | ComputeCrossModuleImport(*Index, ImportLists, ExportLists); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 411 | |
| 412 | // Parallel optimizer + codegen |
| 413 | { |
| 414 | ThreadPool Pool(ThreadCount); |
| 415 | int count = 0; |
| 416 | for (auto &ModuleBuffer : Modules) { |
| 417 | Pool.async([&](int count) { |
| 418 | LLVMContext Context; |
Mehdi Amini | 09b4a8d | 2016-03-10 01:28:54 +0000 | [diff] [blame] | 419 | Context.setDiscardValueNames(LTODiscardValueNames); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 420 | |
| 421 | // Parse module now |
| 422 | auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false); |
| 423 | |
| 424 | // Save temps: original file. |
| 425 | if (!SaveTempsDir.empty()) { |
| 426 | saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc"); |
| 427 | } |
| 428 | |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 429 | auto &ImportList = ImportLists[TheModule->getModuleIdentifier()]; |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 430 | ProducedBinaries[count] = ProcessThinLTOModule( |
Mehdi Amini | 01e3213 | 2016-03-26 05:40:34 +0000 | [diff] [blame] | 431 | *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList, |
Mehdi Amini | 43b657b | 2016-04-01 06:47:02 +0000 | [diff] [blame^] | 432 | CacheOptions, DisableCodeGen, SaveTempsDir, count); |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 433 | }, count); |
| 434 | count++; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | // If statistics were requested, print them out now. |
| 439 | if (llvm::AreStatisticsEnabled()) |
| 440 | llvm::PrintStatistics(); |
| 441 | } |