2015-08-27 23:37:36 +00:00
|
|
|
//===-- ParallelCG.cpp ----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines functions that can be used for parallel code generation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/ParallelCG.h"
|
2016-11-11 05:34:58 +00:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
2015-08-27 23:37:36 +00:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/Support/ErrorOr.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2016-03-04 15:39:13 +00:00
|
|
|
#include "llvm/Support/ThreadPool.h"
|
2015-08-27 23:37:36 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Transforms/Utils/SplitModule.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2016-06-17 20:41:14 +00:00
|
|
|
static void codegen(Module *M, llvm::raw_pwrite_stream &OS,
|
|
|
|
function_ref<std::unique_ptr<TargetMachine>()> TMFactory,
|
|
|
|
TargetMachine::CodeGenFileType FileType) {
|
2016-04-15 17:34:32 +00:00
|
|
|
std::unique_ptr<TargetMachine> TM = TMFactory();
|
2015-08-27 23:37:36 +00:00
|
|
|
legacy::PassManager CodeGenPasses;
|
2015-11-19 23:59:24 +00:00
|
|
|
if (TM->addPassesToEmitFile(CodeGenPasses, OS, FileType))
|
2015-08-27 23:37:36 +00:00
|
|
|
report_fatal_error("Failed to setup codegen");
|
|
|
|
CodeGenPasses.run(*M);
|
|
|
|
}
|
|
|
|
|
2016-04-15 17:34:32 +00:00
|
|
|
std::unique_ptr<Module> llvm::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) {
|
2016-04-06 18:32:13 +00:00
|
|
|
assert(BCOSs.empty() || BCOSs.size() == OSs.size());
|
|
|
|
|
2015-08-27 23:37:36 +00:00
|
|
|
if (OSs.size() == 1) {
|
2016-04-06 18:32:13 +00:00
|
|
|
if (!BCOSs.empty())
|
|
|
|
WriteBitcodeToFile(M.get(), *BCOSs[0]);
|
2016-04-15 17:34:32 +00:00
|
|
|
codegen(M.get(), *OSs[0], TMFactory, FileType);
|
2015-08-27 23:37:36 +00:00
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
2016-03-04 15:39:13 +00:00
|
|
|
// Create ThreadPool in nested scope so that threads will be joined
|
|
|
|
// on destruction.
|
|
|
|
{
|
|
|
|
ThreadPool CodegenThreadPool(OSs.size());
|
|
|
|
int ThreadCount = 0;
|
2015-08-27 23:37:36 +00:00
|
|
|
|
2016-03-04 15:39:13 +00:00
|
|
|
SplitModule(
|
|
|
|
std::move(M), OSs.size(),
|
|
|
|
[&](std::unique_ptr<Module> MPart) {
|
|
|
|
// We want to clone the module in a new context to multi-thread the
|
|
|
|
// codegen. We do it by serializing partition modules to bitcode
|
|
|
|
// (while still on the main thread, in order to avoid data races) and
|
|
|
|
// spinning up new threads which deserialize the partitions into
|
|
|
|
// separate contexts.
|
|
|
|
// FIXME: Provide a more direct way to do this in LLVM.
|
2016-04-17 19:38:57 +00:00
|
|
|
SmallString<0> BC;
|
2016-03-04 15:39:13 +00:00
|
|
|
raw_svector_ostream BCOS(BC);
|
|
|
|
WriteBitcodeToFile(MPart.get(), BCOS);
|
2015-08-27 23:37:36 +00:00
|
|
|
|
2016-04-06 18:32:13 +00:00
|
|
|
if (!BCOSs.empty()) {
|
|
|
|
BCOSs[ThreadCount]->write(BC.begin(), BC.size());
|
|
|
|
BCOSs[ThreadCount]->flush();
|
|
|
|
}
|
|
|
|
|
2016-03-04 15:39:13 +00:00
|
|
|
llvm::raw_pwrite_stream *ThreadOS = OSs[ThreadCount++];
|
|
|
|
// Enqueue the task
|
|
|
|
CodegenThreadPool.async(
|
2016-04-17 19:38:57 +00:00
|
|
|
[TMFactory, FileType, ThreadOS](const SmallString<0> &BC) {
|
2016-03-04 15:39:13 +00:00
|
|
|
LLVMContext Ctx;
|
2016-11-13 07:00:17 +00:00
|
|
|
Expected<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
|
2016-03-04 15:39:13 +00:00
|
|
|
MemoryBufferRef(StringRef(BC.data(), BC.size()),
|
|
|
|
"<split-module>"),
|
|
|
|
Ctx);
|
|
|
|
if (!MOrErr)
|
|
|
|
report_fatal_error("Failed to read bitcode");
|
|
|
|
std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
|
2015-08-27 23:37:36 +00:00
|
|
|
|
2016-04-15 17:34:32 +00:00
|
|
|
codegen(MPartInCtx.get(), *ThreadOS, TMFactory, FileType);
|
2016-03-04 15:39:13 +00:00
|
|
|
},
|
|
|
|
// Pass BC using std::move to ensure that it get moved rather than
|
|
|
|
// copied into the thread's context.
|
|
|
|
std::move(BC));
|
|
|
|
},
|
|
|
|
PreserveLocals);
|
|
|
|
}
|
2015-08-27 23:37:36 +00:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|