1 //===-- ParallelCG.cpp ----------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines functions that can be used for parallel code generation.
12 //===----------------------------------------------------------------------===//
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"
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));
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);
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();
49 const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
51 report_fatal_error(Twine("Target not found: ") + ErrMsg);
53 if (OSs.size() == 1) {
54 codegen(M.get(), *OSs[0], TheTarget, CPU, Features, Options, RM, CM,
59 std::vector<thread> Threads;
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);
70 llvm::raw_pwrite_stream *ThreadOS = OSs[Threads.size()];
72 [TheTarget, CPU, Features, Options, RM, CM, OL,
73 ThreadOS](const SmallVector<char, 0> &BC) {
75 ErrorOr<std::unique_ptr<Module>> MOrErr =
76 parseBitcodeFile(MemoryBufferRef(StringRef(BC.data(), BC.size()),
80 report_fatal_error("Failed to read bitcode");
81 std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
83 codegen(MPartInCtx.get(), *ThreadOS, TheTarget, CPU, Features,
86 // Pass BC using std::move to ensure that it get moved rather than
87 // copied into the thread's context.
91 for (thread &T : Threads)