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"
15c269ed51SPeter Collingbourne #include "llvm/Bitcode/ReaderWriter.h"
16c269ed51SPeter Collingbourne #include "llvm/IR/LLVMContext.h"
17c269ed51SPeter Collingbourne #include "llvm/IR/LegacyPassManager.h"
18c269ed51SPeter Collingbourne #include "llvm/IR/Module.h"
19c269ed51SPeter Collingbourne #include "llvm/Support/ErrorOr.h"
20c269ed51SPeter Collingbourne #include "llvm/Support/MemoryBuffer.h"
21c269ed51SPeter Collingbourne #include "llvm/Support/TargetRegistry.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 
287950b129SDavide Italiano static void
297950b129SDavide Italiano codegen(Module *M, llvm::raw_pwrite_stream &OS,
307950b129SDavide Italiano         std::function<std::unique_ptr<TargetMachine>()> TMFactory,
314d450906STobias Edler von Koch         TargetMachine::CodeGenFileType FileType) {
327950b129SDavide Italiano   std::unique_ptr<TargetMachine> TM = TMFactory();
33c269ed51SPeter Collingbourne   legacy::PassManager CodeGenPasses;
344d450906STobias Edler von Koch   if (TM->addPassesToEmitFile(CodeGenPasses, OS, FileType))
35c269ed51SPeter Collingbourne     report_fatal_error("Failed to setup codegen");
36c269ed51SPeter Collingbourne   CodeGenPasses.run(*M);
37c269ed51SPeter Collingbourne }
38c269ed51SPeter Collingbourne 
397950b129SDavide Italiano std::unique_ptr<Module> llvm::splitCodeGen(
407950b129SDavide Italiano     std::unique_ptr<Module> M, ArrayRef<llvm::raw_pwrite_stream *> OSs,
417950b129SDavide Italiano     ArrayRef<llvm::raw_pwrite_stream *> BCOSs,
427950b129SDavide Italiano     const std::function<std::unique_ptr<TargetMachine>()> &TMFactory,
437950b129SDavide Italiano     TargetMachine::CodeGenFileType FileType, bool PreserveLocals) {
44268826a2SEvgeniy Stepanov   assert(BCOSs.empty() || BCOSs.size() == OSs.size());
45268826a2SEvgeniy Stepanov 
46c269ed51SPeter Collingbourne   if (OSs.size() == 1) {
47268826a2SEvgeniy Stepanov     if (!BCOSs.empty())
48268826a2SEvgeniy Stepanov       WriteBitcodeToFile(M.get(), *BCOSs[0]);
497950b129SDavide Italiano     codegen(M.get(), *OSs[0], TMFactory, FileType);
50c269ed51SPeter Collingbourne     return M;
51c269ed51SPeter Collingbourne   }
52c269ed51SPeter Collingbourne 
53d84c7decSTeresa Johnson   // Create ThreadPool in nested scope so that threads will be joined
54d84c7decSTeresa Johnson   // on destruction.
55d84c7decSTeresa Johnson   {
56d84c7decSTeresa Johnson     ThreadPool CodegenThreadPool(OSs.size());
57d84c7decSTeresa Johnson     int ThreadCount = 0;
58d84c7decSTeresa Johnson 
59d84c7decSTeresa Johnson     SplitModule(
60d84c7decSTeresa Johnson         std::move(M), OSs.size(),
61d84c7decSTeresa Johnson         [&](std::unique_ptr<Module> MPart) {
62d84c7decSTeresa Johnson           // We want to clone the module in a new context to multi-thread the
63d84c7decSTeresa Johnson           // codegen. We do it by serializing partition modules to bitcode
64d84c7decSTeresa Johnson           // (while still on the main thread, in order to avoid data races) and
65d84c7decSTeresa Johnson           // spinning up new threads which deserialize the partitions into
66d84c7decSTeresa Johnson           // separate contexts.
67c269ed51SPeter Collingbourne           // FIXME: Provide a more direct way to do this in LLVM.
68*caa11696SDavide Italiano           SmallString<0> BC;
69c269ed51SPeter Collingbourne           raw_svector_ostream BCOS(BC);
70c269ed51SPeter Collingbourne           WriteBitcodeToFile(MPart.get(), BCOS);
71c269ed51SPeter Collingbourne 
72268826a2SEvgeniy Stepanov           if (!BCOSs.empty()) {
73268826a2SEvgeniy Stepanov             BCOSs[ThreadCount]->write(BC.begin(), BC.size());
74268826a2SEvgeniy Stepanov             BCOSs[ThreadCount]->flush();
75268826a2SEvgeniy Stepanov           }
76268826a2SEvgeniy Stepanov 
77d84c7decSTeresa Johnson           llvm::raw_pwrite_stream *ThreadOS = OSs[ThreadCount++];
78d84c7decSTeresa Johnson           // Enqueue the task
79d84c7decSTeresa Johnson           CodegenThreadPool.async(
80*caa11696SDavide Italiano               [TMFactory, FileType, ThreadOS](const SmallString<0> &BC) {
81c269ed51SPeter Collingbourne                 LLVMContext Ctx;
82d84c7decSTeresa Johnson                 ErrorOr<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
83d84c7decSTeresa Johnson                     MemoryBufferRef(StringRef(BC.data(), BC.size()),
84c269ed51SPeter Collingbourne                                     "<split-module>"),
85c269ed51SPeter Collingbourne                     Ctx);
86c269ed51SPeter Collingbourne                 if (!MOrErr)
87c269ed51SPeter Collingbourne                   report_fatal_error("Failed to read bitcode");
88c269ed51SPeter Collingbourne                 std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
89c269ed51SPeter Collingbourne 
907950b129SDavide Italiano                 codegen(MPartInCtx.get(), *ThreadOS, TMFactory, FileType);
91c269ed51SPeter Collingbourne               },
92c269ed51SPeter Collingbourne               // Pass BC using std::move to ensure that it get moved rather than
93c269ed51SPeter Collingbourne               // copied into the thread's context.
94c269ed51SPeter Collingbourne               std::move(BC));
95d84c7decSTeresa Johnson         },
96d84c7decSTeresa Johnson         PreserveLocals);
97d84c7decSTeresa Johnson   }
98c269ed51SPeter Collingbourne 
99c269ed51SPeter Collingbourne   return {};
100c269ed51SPeter Collingbourne }
101