blob: e4c73658cb4f7e8c2dd2bdf50f63f2cec29dfe3e [file] [log] [blame]
Peter Collingbournec269ed52015-08-27 23:37:36 +00001//===-- ParallelCG.cpp ----------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Peter Collingbournec269ed52015-08-27 23:37:36 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines functions that can be used for parallel code generation.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/CodeGen/ParallelCG.h"
Teresa Johnsonad176792016-11-11 05:34:58 +000014#include "llvm/Bitcode/BitcodeReader.h"
15#include "llvm/Bitcode/BitcodeWriter.h"
Peter Collingbournec269ed52015-08-27 23:37:36 +000016#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"
Teresa Johnsond84c7de2016-03-04 15:39:13 +000021#include "llvm/Support/ThreadPool.h"
Peter Collingbournec269ed52015-08-27 23:37:36 +000022#include "llvm/Target/TargetMachine.h"
23#include "llvm/Transforms/Utils/SplitModule.h"
24
25using namespace llvm;
26
Benjamin Kramer1afc1de2016-06-17 20:41:14 +000027static void codegen(Module *M, llvm::raw_pwrite_stream &OS,
28 function_ref<std::unique_ptr<TargetMachine>()> TMFactory,
29 TargetMachine::CodeGenFileType FileType) {
Davide Italiano7950b122016-04-15 17:34:32 +000030 std::unique_ptr<TargetMachine> TM = TMFactory();
Peter Collingbournec269ed52015-08-27 23:37:36 +000031 legacy::PassManager CodeGenPasses;
Peter Collingbourne9a451142018-05-21 20:16:41 +000032 if (TM->addPassesToEmitFile(CodeGenPasses, OS, nullptr, FileType))
Peter Collingbournec269ed52015-08-27 23:37:36 +000033 report_fatal_error("Failed to setup codegen");
34 CodeGenPasses.run(*M);
35}
36
Davide Italiano7950b122016-04-15 17:34:32 +000037std::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) {
Evgeniy Stepanov268826a2016-04-06 18:32:13 +000042 assert(BCOSs.empty() || BCOSs.size() == OSs.size());
43
Peter Collingbournec269ed52015-08-27 23:37:36 +000044 if (OSs.size() == 1) {
Evgeniy Stepanov268826a2016-04-06 18:32:13 +000045 if (!BCOSs.empty())
Rafael Espindola6a86e252018-02-14 19:11:32 +000046 WriteBitcodeToFile(*M, *BCOSs[0]);
Davide Italiano7950b122016-04-15 17:34:32 +000047 codegen(M.get(), *OSs[0], TMFactory, FileType);
Peter Collingbournec269ed52015-08-27 23:37:36 +000048 return M;
49 }
50
Teresa Johnsond84c7de2016-03-04 15:39:13 +000051 // Create ThreadPool in nested scope so that threads will be joined
52 // on destruction.
53 {
54 ThreadPool CodegenThreadPool(OSs.size());
55 int ThreadCount = 0;
Peter Collingbournec269ed52015-08-27 23:37:36 +000056
Teresa Johnsond84c7de2016-03-04 15:39:13 +000057 SplitModule(
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
64 // separate contexts.
65 // FIXME: Provide a more direct way to do this in LLVM.
Davide Italianocaa11692016-04-17 19:38:57 +000066 SmallString<0> BC;
Teresa Johnsond84c7de2016-03-04 15:39:13 +000067 raw_svector_ostream BCOS(BC);
Rafael Espindola6a86e252018-02-14 19:11:32 +000068 WriteBitcodeToFile(*MPart, BCOS);
Peter Collingbournec269ed52015-08-27 23:37:36 +000069
Evgeniy Stepanov268826a2016-04-06 18:32:13 +000070 if (!BCOSs.empty()) {
71 BCOSs[ThreadCount]->write(BC.begin(), BC.size());
72 BCOSs[ThreadCount]->flush();
73 }
74
Teresa Johnsond84c7de2016-03-04 15:39:13 +000075 llvm::raw_pwrite_stream *ThreadOS = OSs[ThreadCount++];
76 // Enqueue the task
77 CodegenThreadPool.async(
Davide Italianocaa11692016-04-17 19:38:57 +000078 [TMFactory, FileType, ThreadOS](const SmallString<0> &BC) {
Teresa Johnsond84c7de2016-03-04 15:39:13 +000079 LLVMContext Ctx;
Peter Collingbourned9445c42016-11-13 07:00:17 +000080 Expected<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
Teresa Johnsond84c7de2016-03-04 15:39:13 +000081 MemoryBufferRef(StringRef(BC.data(), BC.size()),
82 "<split-module>"),
83 Ctx);
84 if (!MOrErr)
85 report_fatal_error("Failed to read bitcode");
86 std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
87
Davide Italiano7950b122016-04-15 17:34:32 +000088 codegen(MPartInCtx.get(), *ThreadOS, TMFactory, FileType);
Teresa Johnsond84c7de2016-03-04 15:39:13 +000089 },
90 // Pass BC using std::move to ensure that it get moved rather than
91 // copied into the thread's context.
92 std::move(BC));
Peter Collingbournec269ed52015-08-27 23:37:36 +000093 },
Teresa Johnsond84c7de2016-03-04 15:39:13 +000094 PreserveLocals);
95 }
Peter Collingbournec269ed52015-08-27 23:37:36 +000096
97 return {};
98}