Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 1 | //===-- ParallelCG.cpp ----------------------------------------------------===// |
| 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 defines functions that can be used for parallel code generation. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/CodeGen/ParallelCG.h" |
| 15 | #include "llvm/Bitcode/ReaderWriter.h" |
| 16 | #include "llvm/IR/LLVMContext.h" |
| 17 | #include "llvm/IR/LegacyPassManager.h" |
| 18 | #include "llvm/IR/Module.h" |
| 19 | #include "llvm/Support/ErrorOr.h" |
| 20 | #include "llvm/Support/MemoryBuffer.h" |
| 21 | #include "llvm/Support/TargetRegistry.h" |
| 22 | #include "llvm/Support/thread.h" |
| 23 | #include "llvm/Target/TargetMachine.h" |
| 24 | #include "llvm/Transforms/Utils/SplitModule.h" |
| 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | static void codegen(Module *M, llvm::raw_pwrite_stream &OS, |
| 29 | const Target *TheTarget, StringRef CPU, StringRef Features, |
| 30 | const TargetOptions &Options, Reloc::Model RM, |
| 31 | CodeModel::Model CM, CodeGenOpt::Level OL) { |
| 32 | std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine( |
| 33 | M->getTargetTriple(), CPU, Features, Options, RM, CM, OL)); |
| 34 | |
| 35 | legacy::PassManager CodeGenPasses; |
| 36 | if (TM->addPassesToEmitFile(CodeGenPasses, OS, |
| 37 | TargetMachine::CGFT_ObjectFile)) |
| 38 | report_fatal_error("Failed to setup codegen"); |
| 39 | CodeGenPasses.run(*M); |
| 40 | } |
| 41 | |
| 42 | std::unique_ptr<Module> |
| 43 | llvm::splitCodeGen(std::unique_ptr<Module> M, |
| 44 | ArrayRef<llvm::raw_pwrite_stream *> OSs, StringRef CPU, |
| 45 | StringRef Features, const TargetOptions &Options, |
| 46 | Reloc::Model RM, CodeModel::Model CM, CodeGenOpt::Level OL) { |
| 47 | StringRef TripleStr = M->getTargetTriple(); |
| 48 | std::string ErrMsg; |
| 49 | const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg); |
| 50 | if (!TheTarget) |
| 51 | report_fatal_error(Twine("Target not found: ") + ErrMsg); |
| 52 | |
| 53 | if (OSs.size() == 1) { |
| 54 | codegen(M.get(), *OSs[0], TheTarget, CPU, Features, Options, RM, CM, |
| 55 | OL); |
| 56 | return M; |
| 57 | } |
| 58 | |
Peter Collingbourne | 592ee15 | 2015-08-31 00:09:01 +0000 | [diff] [blame] | 59 | std::vector<thread> Threads; |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 60 | SplitModule(std::move(M), OSs.size(), [&](std::unique_ptr<Module> MPart) { |
| 61 | // We want to clone the module in a new context to multi-thread the codegen. |
| 62 | // We do it by serializing partition modules to bitcode (while still on the |
| 63 | // main thread, in order to avoid data races) and spinning up new threads |
| 64 | // which deserialize the partitions into separate contexts. |
| 65 | // FIXME: Provide a more direct way to do this in LLVM. |
| 66 | SmallVector<char, 0> BC; |
| 67 | raw_svector_ostream BCOS(BC); |
| 68 | WriteBitcodeToFile(MPart.get(), BCOS); |
| 69 | |
| 70 | llvm::raw_pwrite_stream *ThreadOS = OSs[Threads.size()]; |
| 71 | Threads.emplace_back( |
| 72 | [TheTarget, CPU, Features, Options, RM, CM, OL, |
| 73 | ThreadOS](const SmallVector<char, 0> &BC) { |
| 74 | LLVMContext Ctx; |
| 75 | ErrorOr<std::unique_ptr<Module>> MOrErr = |
| 76 | parseBitcodeFile(MemoryBufferRef(StringRef(BC.data(), BC.size()), |
| 77 | "<split-module>"), |
| 78 | Ctx); |
| 79 | if (!MOrErr) |
| 80 | report_fatal_error("Failed to read bitcode"); |
| 81 | std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get()); |
| 82 | |
| 83 | codegen(MPartInCtx.get(), *ThreadOS, TheTarget, CPU, Features, |
| 84 | Options, RM, CM, OL); |
| 85 | }, |
| 86 | // Pass BC using std::move to ensure that it get moved rather than |
| 87 | // copied into the thread's context. |
| 88 | std::move(BC)); |
| 89 | }); |
| 90 | |
Peter Collingbourne | 592ee15 | 2015-08-31 00:09:01 +0000 | [diff] [blame] | 91 | for (thread &T : Threads) |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 92 | T.join(); |
| 93 | |
| 94 | return {}; |
| 95 | } |