blob: d93cb9192d4d15adb1d86b3f8137c1f9faeb1be1 [file] [log] [blame]
Peter Collingbournec269ed52015-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"
15#include "llvm/Bitcode/ReaderWriter.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/TargetRegistry.h"
22#include "llvm/Support/thread.h"
23#include "llvm/Target/TargetMachine.h"
24#include "llvm/Transforms/Utils/SplitModule.h"
25
26using namespace llvm;
27
28static void codegen(Module *M, llvm::raw_pwrite_stream &OS,
29 const Target *TheTarget, StringRef CPU, StringRef Features,
30 const TargetOptions &Options, Reloc::Model RM,
Tobias Edler von Koch4d450902015-11-19 23:59:24 +000031 CodeModel::Model CM, CodeGenOpt::Level OL,
32 TargetMachine::CodeGenFileType FileType) {
Peter Collingbournec269ed52015-08-27 23:37:36 +000033 std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
34 M->getTargetTriple(), CPU, Features, Options, RM, CM, OL));
35
36 legacy::PassManager CodeGenPasses;
Tobias Edler von Koch4d450902015-11-19 23:59:24 +000037 if (TM->addPassesToEmitFile(CodeGenPasses, OS, FileType))
Peter Collingbournec269ed52015-08-27 23:37:36 +000038 report_fatal_error("Failed to setup codegen");
39 CodeGenPasses.run(*M);
40}
41
42std::unique_ptr<Module>
43llvm::splitCodeGen(std::unique_ptr<Module> M,
44 ArrayRef<llvm::raw_pwrite_stream *> OSs, StringRef CPU,
45 StringRef Features, const TargetOptions &Options,
Tobias Edler von Koch4d450902015-11-19 23:59:24 +000046 Reloc::Model RM, CodeModel::Model CM, CodeGenOpt::Level OL,
Sergei Larind19d4d32016-01-18 21:07:13 +000047 TargetMachine::CodeGenFileType FileType,
48 bool PreserveLocals) {
Peter Collingbournec269ed52015-08-27 23:37:36 +000049 StringRef TripleStr = M->getTargetTriple();
50 std::string ErrMsg;
51 const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
52 if (!TheTarget)
53 report_fatal_error(Twine("Target not found: ") + ErrMsg);
54
55 if (OSs.size() == 1) {
56 codegen(M.get(), *OSs[0], TheTarget, CPU, Features, Options, RM, CM,
Tobias Edler von Koch4d450902015-11-19 23:59:24 +000057 OL, FileType);
Peter Collingbournec269ed52015-08-27 23:37:36 +000058 return M;
59 }
60
Peter Collingbourne592ee152015-08-31 00:09:01 +000061 std::vector<thread> Threads;
Peter Collingbournec269ed52015-08-27 23:37:36 +000062 SplitModule(std::move(M), OSs.size(), [&](std::unique_ptr<Module> MPart) {
63 // We want to clone the module in a new context to multi-thread the codegen.
64 // We do it by serializing partition modules to bitcode (while still on the
65 // main thread, in order to avoid data races) and spinning up new threads
66 // which deserialize the partitions into separate contexts.
67 // FIXME: Provide a more direct way to do this in LLVM.
68 SmallVector<char, 0> BC;
69 raw_svector_ostream BCOS(BC);
70 WriteBitcodeToFile(MPart.get(), BCOS);
71
72 llvm::raw_pwrite_stream *ThreadOS = OSs[Threads.size()];
73 Threads.emplace_back(
Tobias Edler von Koch4d450902015-11-19 23:59:24 +000074 [TheTarget, CPU, Features, Options, RM, CM, OL, FileType,
Peter Collingbournec269ed52015-08-27 23:37:36 +000075 ThreadOS](const SmallVector<char, 0> &BC) {
76 LLVMContext Ctx;
77 ErrorOr<std::unique_ptr<Module>> MOrErr =
78 parseBitcodeFile(MemoryBufferRef(StringRef(BC.data(), BC.size()),
79 "<split-module>"),
80 Ctx);
81 if (!MOrErr)
82 report_fatal_error("Failed to read bitcode");
83 std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
84
85 codegen(MPartInCtx.get(), *ThreadOS, TheTarget, CPU, Features,
Tobias Edler von Koch4d450902015-11-19 23:59:24 +000086 Options, RM, CM, OL, FileType);
Peter Collingbournec269ed52015-08-27 23:37:36 +000087 },
88 // Pass BC using std::move to ensure that it get moved rather than
89 // copied into the thread's context.
90 std::move(BC));
Sergei Larind19d4d32016-01-18 21:07:13 +000091 }, PreserveLocals);
Peter Collingbournec269ed52015-08-27 23:37:36 +000092
Peter Collingbourne592ee152015-08-31 00:09:01 +000093 for (thread &T : Threads)
Peter Collingbournec269ed52015-08-27 23:37:36 +000094 T.join();
95
96 return {};
97}