1*9880d681SAndroid Build Coastguard Worker //===-- ParallelCG.cpp ----------------------------------------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file defines functions that can be used for parallel code generation.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker
14*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/ParallelCG.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/Bitcode/ReaderWriter.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LLVMContext.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LegacyPassManager.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorOr.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/MemoryBuffer.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/TargetRegistry.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ThreadPool.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetMachine.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/SplitModule.h"
25*9880d681SAndroid Build Coastguard Worker
26*9880d681SAndroid Build Coastguard Worker using namespace llvm;
27*9880d681SAndroid Build Coastguard Worker
codegen(Module * M,llvm::raw_pwrite_stream & OS,function_ref<std::unique_ptr<TargetMachine> ()> TMFactory,TargetMachine::CodeGenFileType FileType)28*9880d681SAndroid Build Coastguard Worker static void codegen(Module *M, llvm::raw_pwrite_stream &OS,
29*9880d681SAndroid Build Coastguard Worker function_ref<std::unique_ptr<TargetMachine>()> TMFactory,
30*9880d681SAndroid Build Coastguard Worker TargetMachine::CodeGenFileType FileType) {
31*9880d681SAndroid Build Coastguard Worker std::unique_ptr<TargetMachine> TM = TMFactory();
32*9880d681SAndroid Build Coastguard Worker legacy::PassManager CodeGenPasses;
33*9880d681SAndroid Build Coastguard Worker if (TM->addPassesToEmitFile(CodeGenPasses, OS, FileType))
34*9880d681SAndroid Build Coastguard Worker report_fatal_error("Failed to setup codegen");
35*9880d681SAndroid Build Coastguard Worker CodeGenPasses.run(*M);
36*9880d681SAndroid Build Coastguard Worker }
37*9880d681SAndroid Build Coastguard Worker
splitCodeGen(std::unique_ptr<Module> M,ArrayRef<llvm::raw_pwrite_stream * > OSs,ArrayRef<llvm::raw_pwrite_stream * > BCOSs,const std::function<std::unique_ptr<TargetMachine> ()> & TMFactory,TargetMachine::CodeGenFileType FileType,bool PreserveLocals)38*9880d681SAndroid Build Coastguard Worker std::unique_ptr<Module> llvm::splitCodeGen(
39*9880d681SAndroid Build Coastguard Worker std::unique_ptr<Module> M, ArrayRef<llvm::raw_pwrite_stream *> OSs,
40*9880d681SAndroid Build Coastguard Worker ArrayRef<llvm::raw_pwrite_stream *> BCOSs,
41*9880d681SAndroid Build Coastguard Worker const std::function<std::unique_ptr<TargetMachine>()> &TMFactory,
42*9880d681SAndroid Build Coastguard Worker TargetMachine::CodeGenFileType FileType, bool PreserveLocals) {
43*9880d681SAndroid Build Coastguard Worker assert(BCOSs.empty() || BCOSs.size() == OSs.size());
44*9880d681SAndroid Build Coastguard Worker
45*9880d681SAndroid Build Coastguard Worker if (OSs.size() == 1) {
46*9880d681SAndroid Build Coastguard Worker if (!BCOSs.empty())
47*9880d681SAndroid Build Coastguard Worker WriteBitcodeToFile(M.get(), *BCOSs[0]);
48*9880d681SAndroid Build Coastguard Worker codegen(M.get(), *OSs[0], TMFactory, FileType);
49*9880d681SAndroid Build Coastguard Worker return M;
50*9880d681SAndroid Build Coastguard Worker }
51*9880d681SAndroid Build Coastguard Worker
52*9880d681SAndroid Build Coastguard Worker // Create ThreadPool in nested scope so that threads will be joined
53*9880d681SAndroid Build Coastguard Worker // on destruction.
54*9880d681SAndroid Build Coastguard Worker {
55*9880d681SAndroid Build Coastguard Worker ThreadPool CodegenThreadPool(OSs.size());
56*9880d681SAndroid Build Coastguard Worker int ThreadCount = 0;
57*9880d681SAndroid Build Coastguard Worker
58*9880d681SAndroid Build Coastguard Worker SplitModule(
59*9880d681SAndroid Build Coastguard Worker std::move(M), OSs.size(),
60*9880d681SAndroid Build Coastguard Worker [&](std::unique_ptr<Module> MPart) {
61*9880d681SAndroid Build Coastguard Worker // We want to clone the module in a new context to multi-thread the
62*9880d681SAndroid Build Coastguard Worker // codegen. We do it by serializing partition modules to bitcode
63*9880d681SAndroid Build Coastguard Worker // (while still on the main thread, in order to avoid data races) and
64*9880d681SAndroid Build Coastguard Worker // spinning up new threads which deserialize the partitions into
65*9880d681SAndroid Build Coastguard Worker // separate contexts.
66*9880d681SAndroid Build Coastguard Worker // FIXME: Provide a more direct way to do this in LLVM.
67*9880d681SAndroid Build Coastguard Worker SmallString<0> BC;
68*9880d681SAndroid Build Coastguard Worker raw_svector_ostream BCOS(BC);
69*9880d681SAndroid Build Coastguard Worker WriteBitcodeToFile(MPart.get(), BCOS);
70*9880d681SAndroid Build Coastguard Worker
71*9880d681SAndroid Build Coastguard Worker if (!BCOSs.empty()) {
72*9880d681SAndroid Build Coastguard Worker BCOSs[ThreadCount]->write(BC.begin(), BC.size());
73*9880d681SAndroid Build Coastguard Worker BCOSs[ThreadCount]->flush();
74*9880d681SAndroid Build Coastguard Worker }
75*9880d681SAndroid Build Coastguard Worker
76*9880d681SAndroid Build Coastguard Worker llvm::raw_pwrite_stream *ThreadOS = OSs[ThreadCount++];
77*9880d681SAndroid Build Coastguard Worker // Enqueue the task
78*9880d681SAndroid Build Coastguard Worker CodegenThreadPool.async(
79*9880d681SAndroid Build Coastguard Worker [TMFactory, FileType, ThreadOS](const SmallString<0> &BC) {
80*9880d681SAndroid Build Coastguard Worker LLVMContext Ctx;
81*9880d681SAndroid Build Coastguard Worker ErrorOr<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
82*9880d681SAndroid Build Coastguard Worker MemoryBufferRef(StringRef(BC.data(), BC.size()),
83*9880d681SAndroid Build Coastguard Worker "<split-module>"),
84*9880d681SAndroid Build Coastguard Worker Ctx);
85*9880d681SAndroid Build Coastguard Worker if (!MOrErr)
86*9880d681SAndroid Build Coastguard Worker report_fatal_error("Failed to read bitcode");
87*9880d681SAndroid Build Coastguard Worker std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
88*9880d681SAndroid Build Coastguard Worker
89*9880d681SAndroid Build Coastguard Worker codegen(MPartInCtx.get(), *ThreadOS, TMFactory, FileType);
90*9880d681SAndroid Build Coastguard Worker },
91*9880d681SAndroid Build Coastguard Worker // Pass BC using std::move to ensure that it get moved rather than
92*9880d681SAndroid Build Coastguard Worker // copied into the thread's context.
93*9880d681SAndroid Build Coastguard Worker std::move(BC));
94*9880d681SAndroid Build Coastguard Worker },
95*9880d681SAndroid Build Coastguard Worker PreserveLocals);
96*9880d681SAndroid Build Coastguard Worker }
97*9880d681SAndroid Build Coastguard Worker
98*9880d681SAndroid Build Coastguard Worker return {};
99*9880d681SAndroid Build Coastguard Worker }
100