1c269ed51SPeter Collingbourne //===-- ParallelCG.cpp ----------------------------------------------------===//
2c269ed51SPeter Collingbourne //
3c269ed51SPeter Collingbourne //                     The LLVM Compiler Infrastructure
4c269ed51SPeter Collingbourne //
5c269ed51SPeter Collingbourne // This file is distributed under the University of Illinois Open Source
6c269ed51SPeter Collingbourne // License. See LICENSE.TXT for details.
7c269ed51SPeter Collingbourne //
8c269ed51SPeter Collingbourne //===----------------------------------------------------------------------===//
9c269ed51SPeter Collingbourne //
10c269ed51SPeter Collingbourne // This file defines functions that can be used for parallel code generation.
11c269ed51SPeter Collingbourne //
12c269ed51SPeter Collingbourne //===----------------------------------------------------------------------===//
13c269ed51SPeter Collingbourne 
14c269ed51SPeter Collingbourne #include "llvm/CodeGen/ParallelCG.h"
15ad17679aSTeresa Johnson #include "llvm/Bitcode/BitcodeReader.h"
16ad17679aSTeresa Johnson #include "llvm/Bitcode/BitcodeWriter.h"
17c269ed51SPeter Collingbourne #include "llvm/IR/LLVMContext.h"
18c269ed51SPeter Collingbourne #include "llvm/IR/LegacyPassManager.h"
19c269ed51SPeter Collingbourne #include "llvm/IR/Module.h"
20c269ed51SPeter Collingbourne #include "llvm/Support/ErrorOr.h"
21c269ed51SPeter Collingbourne #include "llvm/Support/MemoryBuffer.h"
22d84c7decSTeresa Johnson #include "llvm/Support/ThreadPool.h"
23c269ed51SPeter Collingbourne #include "llvm/Target/TargetMachine.h"
24c269ed51SPeter Collingbourne #include "llvm/Transforms/Utils/SplitModule.h"
25c269ed51SPeter Collingbourne 
26c269ed51SPeter Collingbourne using namespace llvm;
27c269ed51SPeter Collingbourne 
281afc1de4SBenjamin Kramer static void codegen(Module *M, llvm::raw_pwrite_stream &OS,
291afc1de4SBenjamin Kramer                     function_ref<std::unique_ptr<TargetMachine>()> TMFactory,
304d450906STobias Edler von Koch                     TargetMachine::CodeGenFileType FileType) {
317950b129SDavide Italiano   std::unique_ptr<TargetMachine> TM = TMFactory();
32c269ed51SPeter Collingbourne   legacy::PassManager CodeGenPasses;
334d450906STobias Edler von Koch   if (TM->addPassesToEmitFile(CodeGenPasses, OS, FileType))
34c269ed51SPeter Collingbourne     report_fatal_error("Failed to setup codegen");
35c269ed51SPeter Collingbourne   CodeGenPasses.run(*M);
36c269ed51SPeter Collingbourne }
37c269ed51SPeter Collingbourne 
387950b129SDavide Italiano std::unique_ptr<Module> llvm::splitCodeGen(
397950b129SDavide Italiano     std::unique_ptr<Module> M, ArrayRef<llvm::raw_pwrite_stream *> OSs,
407950b129SDavide Italiano     ArrayRef<llvm::raw_pwrite_stream *> BCOSs,
417950b129SDavide Italiano     const std::function<std::unique_ptr<TargetMachine>()> &TMFactory,
427950b129SDavide Italiano     TargetMachine::CodeGenFileType FileType, bool PreserveLocals) {
43268826a2SEvgeniy Stepanov   assert(BCOSs.empty() || BCOSs.size() == OSs.size());
44268826a2SEvgeniy Stepanov 
45c269ed51SPeter Collingbourne   if (OSs.size() == 1) {
46268826a2SEvgeniy Stepanov     if (!BCOSs.empty())
47*6a86e25dSRafael Espindola       WriteBitcodeToFile(*M, *BCOSs[0]);
487950b129SDavide Italiano     codegen(M.get(), *OSs[0], TMFactory, FileType);
49c269ed51SPeter Collingbourne     return M;
50c269ed51SPeter Collingbourne   }
51c269ed51SPeter Collingbourne 
52d84c7decSTeresa Johnson   // Create ThreadPool in nested scope so that threads will be joined
53d84c7decSTeresa Johnson   // on destruction.
54d84c7decSTeresa Johnson   {
55d84c7decSTeresa Johnson     ThreadPool CodegenThreadPool(OSs.size());
56d84c7decSTeresa Johnson     int ThreadCount = 0;
57d84c7decSTeresa Johnson 
58d84c7decSTeresa Johnson     SplitModule(
59d84c7decSTeresa Johnson         std::move(M), OSs.size(),
60d84c7decSTeresa Johnson         [&](std::unique_ptr<Module> MPart) {
61d84c7decSTeresa Johnson           // We want to clone the module in a new context to multi-thread the
62d84c7decSTeresa Johnson           // codegen. We do it by serializing partition modules to bitcode
63d84c7decSTeresa Johnson           // (while still on the main thread, in order to avoid data races) and
64d84c7decSTeresa Johnson           // spinning up new threads which deserialize the partitions into
65d84c7decSTeresa Johnson           // separate contexts.
66c269ed51SPeter Collingbourne           // FIXME: Provide a more direct way to do this in LLVM.
67caa11696SDavide Italiano           SmallString<0> BC;
68c269ed51SPeter Collingbourne           raw_svector_ostream BCOS(BC);
69*6a86e25dSRafael Espindola           WriteBitcodeToFile(*MPart, BCOS);
70c269ed51SPeter Collingbourne 
71268826a2SEvgeniy Stepanov           if (!BCOSs.empty()) {
72268826a2SEvgeniy Stepanov             BCOSs[ThreadCount]->write(BC.begin(), BC.size());
73268826a2SEvgeniy Stepanov             BCOSs[ThreadCount]->flush();
74268826a2SEvgeniy Stepanov           }
75268826a2SEvgeniy Stepanov 
76d84c7decSTeresa Johnson           llvm::raw_pwrite_stream *ThreadOS = OSs[ThreadCount++];
77d84c7decSTeresa Johnson           // Enqueue the task
78d84c7decSTeresa Johnson           CodegenThreadPool.async(
79caa11696SDavide Italiano               [TMFactory, FileType, ThreadOS](const SmallString<0> &BC) {
80c269ed51SPeter Collingbourne                 LLVMContext Ctx;
81d9445c49SPeter Collingbourne                 Expected<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
82d84c7decSTeresa Johnson                     MemoryBufferRef(StringRef(BC.data(), BC.size()),
83c269ed51SPeter Collingbourne                                     "<split-module>"),
84c269ed51SPeter Collingbourne                     Ctx);
85c269ed51SPeter Collingbourne                 if (!MOrErr)
86c269ed51SPeter Collingbourne                   report_fatal_error("Failed to read bitcode");
87c269ed51SPeter Collingbourne                 std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
88c269ed51SPeter Collingbourne 
897950b129SDavide Italiano                 codegen(MPartInCtx.get(), *ThreadOS, TMFactory, FileType);
90c269ed51SPeter Collingbourne               },
91c269ed51SPeter Collingbourne               // Pass BC using std::move to ensure that it get moved rather than
92c269ed51SPeter Collingbourne               // copied into the thread's context.
93c269ed51SPeter Collingbourne               std::move(BC));
94d84c7decSTeresa Johnson         },
95d84c7decSTeresa Johnson         PreserveLocals);
96d84c7decSTeresa Johnson   }
97c269ed51SPeter Collingbourne 
98c269ed51SPeter Collingbourne   return {};
99c269ed51SPeter Collingbourne }
100