1 //===-- ParallelCG.cpp ----------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines functions that can be used for parallel code generation.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/CodeGen/ParallelCG.h"
14 #include "llvm/Bitcode/BitcodeReader.h"
15 #include "llvm/Bitcode/BitcodeWriter.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/ThreadPool.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Transforms/Utils/SplitModule.h"
27 static void codegen(Module
*M
, llvm::raw_pwrite_stream
&OS
,
28 function_ref
<std::unique_ptr
<TargetMachine
>()> TMFactory
,
29 TargetMachine::CodeGenFileType FileType
) {
30 std::unique_ptr
<TargetMachine
> TM
= TMFactory();
31 legacy::PassManager CodeGenPasses
;
32 if (TM
->addPassesToEmitFile(CodeGenPasses
, OS
, nullptr, FileType
))
33 report_fatal_error("Failed to setup codegen");
34 CodeGenPasses
.run(*M
);
37 std::unique_ptr
<Module
> llvm::splitCodeGen(
38 std::unique_ptr
<Module
> M
, ArrayRef
<llvm::raw_pwrite_stream
*> OSs
,
39 ArrayRef
<llvm::raw_pwrite_stream
*> BCOSs
,
40 const std::function
<std::unique_ptr
<TargetMachine
>()> &TMFactory
,
41 TargetMachine::CodeGenFileType FileType
, bool PreserveLocals
) {
42 assert(BCOSs
.empty() || BCOSs
.size() == OSs
.size());
44 if (OSs
.size() == 1) {
46 WriteBitcodeToFile(*M
, *BCOSs
[0]);
47 codegen(M
.get(), *OSs
[0], TMFactory
, FileType
);
51 // Create ThreadPool in nested scope so that threads will be joined
54 ThreadPool
CodegenThreadPool(OSs
.size());
58 std::move(M
), OSs
.size(),
59 [&](std::unique_ptr
<Module
> MPart
) {
60 // We want to clone the module in a new context to multi-thread the
61 // codegen. We do it by serializing partition modules to bitcode
62 // (while still on the main thread, in order to avoid data races) and
63 // spinning up new threads which deserialize the partitions into
65 // FIXME: Provide a more direct way to do this in LLVM.
67 raw_svector_ostream
BCOS(BC
);
68 WriteBitcodeToFile(*MPart
, BCOS
);
71 BCOSs
[ThreadCount
]->write(BC
.begin(), BC
.size());
72 BCOSs
[ThreadCount
]->flush();
75 llvm::raw_pwrite_stream
*ThreadOS
= OSs
[ThreadCount
++];
77 CodegenThreadPool
.async(
78 [TMFactory
, FileType
, ThreadOS
](const SmallString
<0> &BC
) {
80 Expected
<std::unique_ptr
<Module
>> MOrErr
= parseBitcodeFile(
81 MemoryBufferRef(StringRef(BC
.data(), BC
.size()),
85 report_fatal_error("Failed to read bitcode");
86 std::unique_ptr
<Module
> MPartInCtx
= std::move(MOrErr
.get());
88 codegen(MPartInCtx
.get(), *ThreadOS
, TMFactory
, FileType
);
90 // Pass BC using std::move to ensure that it get moved rather than
91 // copied into the thread's context.