blob: bc3f2a6e9b5a21ea61a9153fe1d44fcfb81435ae [file] [log] [blame]
Peter Collingbournedaf68852015-08-27 23:37:36 +00001//===-- ParallelCG.cpp ----------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines functions that can be used for parallel code generation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/ParallelCG.h"
Teresa Johnsona5479192016-11-11 05:34:58 +000015#include "llvm/Bitcode/BitcodeReader.h"
16#include "llvm/Bitcode/BitcodeWriter.h"
Peter Collingbournedaf68852015-08-27 23:37:36 +000017#include "llvm/IR/LLVMContext.h"
18#include "llvm/IR/LegacyPassManager.h"
19#include "llvm/IR/Module.h"
20#include "llvm/Support/ErrorOr.h"
21#include "llvm/Support/MemoryBuffer.h"
Teresa Johnson474ca562016-03-04 15:39:13 +000022#include "llvm/Support/ThreadPool.h"
Peter Collingbournedaf68852015-08-27 23:37:36 +000023#include "llvm/Target/TargetMachine.h"
24#include "llvm/Transforms/Utils/SplitModule.h"
25
26using namespace llvm;
27
Benjamin Kramere5eb6732016-06-17 20:41:14 +000028static void codegen(Module *M, llvm::raw_pwrite_stream &OS,
29 function_ref<std::unique_ptr<TargetMachine>()> TMFactory,
30 TargetMachine::CodeGenFileType FileType) {
Davide Italianodb52b362016-04-15 17:34:32 +000031 std::unique_ptr<TargetMachine> TM = TMFactory();
Peter Collingbournedaf68852015-08-27 23:37:36 +000032 legacy::PassManager CodeGenPasses;
Peter Collingbourne9ffe0732018-05-21 20:16:41 +000033 if (TM->addPassesToEmitFile(CodeGenPasses, OS, nullptr, FileType))
Peter Collingbournedaf68852015-08-27 23:37:36 +000034 report_fatal_error("Failed to setup codegen");
35 CodeGenPasses.run(*M);
36}
37
Davide Italianodb52b362016-04-15 17:34:32 +000038std::unique_ptr<Module> llvm::splitCodeGen(
39 std::unique_ptr<Module> M, ArrayRef<llvm::raw_pwrite_stream *> OSs,
40 ArrayRef<llvm::raw_pwrite_stream *> BCOSs,
41 const std::function<std::unique_ptr<TargetMachine>()> &TMFactory,
42 TargetMachine::CodeGenFileType FileType, bool PreserveLocals) {
Evgeniy Stepanovd8c5d5c2016-04-06 18:32:13 +000043 assert(BCOSs.empty() || BCOSs.size() == OSs.size());
44
Peter Collingbournedaf68852015-08-27 23:37:36 +000045 if (OSs.size() == 1) {
Evgeniy Stepanovd8c5d5c2016-04-06 18:32:13 +000046 if (!BCOSs.empty())
Rafael Espindola06d62072018-02-14 19:11:32 +000047 WriteBitcodeToFile(*M, *BCOSs[0]);
Davide Italianodb52b362016-04-15 17:34:32 +000048 codegen(M.get(), *OSs[0], TMFactory, FileType);
Peter Collingbournedaf68852015-08-27 23:37:36 +000049 return M;
50 }
51
Teresa Johnson474ca562016-03-04 15:39:13 +000052 // Create ThreadPool in nested scope so that threads will be joined
53 // on destruction.
54 {
55 ThreadPool CodegenThreadPool(OSs.size());
56 int ThreadCount = 0;
Peter Collingbournedaf68852015-08-27 23:37:36 +000057
Teresa Johnson474ca562016-03-04 15:39:13 +000058 SplitModule(
59 std::move(M), OSs.size(),
60 [&](std::unique_ptr<Module> MPart) {
61 // We want to clone the module in a new context to multi-thread the
62 // codegen. We do it by serializing partition modules to bitcode
63 // (while still on the main thread, in order to avoid data races) and
64 // spinning up new threads which deserialize the partitions into
65 // separate contexts.
66 // FIXME: Provide a more direct way to do this in LLVM.
Davide Italiano8bef6e22016-04-17 19:38:57 +000067 SmallString<0> BC;
Teresa Johnson474ca562016-03-04 15:39:13 +000068 raw_svector_ostream BCOS(BC);
Rafael Espindola06d62072018-02-14 19:11:32 +000069 WriteBitcodeToFile(*MPart, BCOS);
Peter Collingbournedaf68852015-08-27 23:37:36 +000070
Evgeniy Stepanovd8c5d5c2016-04-06 18:32:13 +000071 if (!BCOSs.empty()) {
72 BCOSs[ThreadCount]->write(BC.begin(), BC.size());
73 BCOSs[ThreadCount]->flush();
74 }
75
Teresa Johnson474ca562016-03-04 15:39:13 +000076 llvm::raw_pwrite_stream *ThreadOS = OSs[ThreadCount++];
77 // Enqueue the task
78 CodegenThreadPool.async(
Davide Italiano8bef6e22016-04-17 19:38:57 +000079 [TMFactory, FileType, ThreadOS](const SmallString<0> &BC) {
Teresa Johnson474ca562016-03-04 15:39:13 +000080 LLVMContext Ctx;
Peter Collingbournedead0812016-11-13 07:00:17 +000081 Expected<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
Teresa Johnson474ca562016-03-04 15:39:13 +000082 MemoryBufferRef(StringRef(BC.data(), BC.size()),
83 "<split-module>"),
84 Ctx);
85 if (!MOrErr)
86 report_fatal_error("Failed to read bitcode");
87 std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
88
Davide Italianodb52b362016-04-15 17:34:32 +000089 codegen(MPartInCtx.get(), *ThreadOS, TMFactory, FileType);
Teresa Johnson474ca562016-03-04 15:39:13 +000090 },
91 // Pass BC using std::move to ensure that it get moved rather than
92 // copied into the thread's context.
93 std::move(BC));
Peter Collingbournedaf68852015-08-27 23:37:36 +000094 },
Teresa Johnson474ca562016-03-04 15:39:13 +000095 PreserveLocals);
96 }
Peter Collingbournedaf68852015-08-27 23:37:36 +000097
98 return {};
99}