2015-08-28 07:37:36 +08: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 13:34:58 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
2015-08-28 07:37:36 +08: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"
|
2016-03-04 23:39:13 +08:00
|
|
|
#include "llvm/Support/ThreadPool.h"
|
2015-08-28 07:37:36 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Transforms/Utils/SplitModule.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2016-06-18 04:41:14 +08:00
|
|
|
static void codegen(Module *M, llvm::raw_pwrite_stream &OS,
|
|
|
|
function_ref<std::unique_ptr<TargetMachine>()> TMFactory,
|
|
|
|
TargetMachine::CodeGenFileType FileType) {
|
2016-04-16 01:34:32 +08:00
|
|
|
std::unique_ptr<TargetMachine> TM = TMFactory();
|
2015-08-28 07:37:36 +08:00
|
|
|
legacy::PassManager CodeGenPasses;
|
2018-05-22 04:16:41 +08:00
|
|
|
if (TM->addPassesToEmitFile(CodeGenPasses, OS, nullptr, FileType))
|
2015-08-28 07:37:36 +08:00
|
|
|
report_fatal_error("Failed to setup codegen");
|
|
|
|
CodeGenPasses.run(*M);
|
|
|
|
}
|
|
|
|
|
2016-04-16 01:34:32 +08: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-07 02:32:13 +08:00
|
|
|
assert(BCOSs.empty() || BCOSs.size() == OSs.size());
|
|
|
|
|
2015-08-28 07:37:36 +08:00
|
|
|
if (OSs.size() == 1) {
|
2016-04-07 02:32:13 +08:00
|
|
|
if (!BCOSs.empty())
|
2018-02-15 03:11:32 +08:00
|
|
|
WriteBitcodeToFile(*M, *BCOSs[0]);
|
2016-04-16 01:34:32 +08:00
|
|
|
codegen(M.get(), *OSs[0], TMFactory, FileType);
|
2015-08-28 07:37:36 +08:00
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
2016-03-04 23:39:13 +08:00
|
|
|
// Create ThreadPool in nested scope so that threads will be joined
|
|
|
|
// on destruction.
|
|
|
|
{
|
|
|
|
ThreadPool CodegenThreadPool(OSs.size());
|
|
|
|
int ThreadCount = 0;
|
2015-08-28 07:37:36 +08:00
|
|
|
|
2016-03-04 23:39:13 +08: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-18 03:38:57 +08:00
|
|
|
SmallString<0> BC;
|
2016-03-04 23:39:13 +08:00
|
|
|
raw_svector_ostream BCOS(BC);
|
2018-02-15 03:11:32 +08:00
|
|
|
WriteBitcodeToFile(*MPart, BCOS);
|
2015-08-28 07:37:36 +08:00
|
|
|
|
2016-04-07 02:32:13 +08:00
|
|
|
if (!BCOSs.empty()) {
|
|
|
|
BCOSs[ThreadCount]->write(BC.begin(), BC.size());
|
|
|
|
BCOSs[ThreadCount]->flush();
|
|
|
|
}
|
|
|
|
|
2016-03-04 23:39:13 +08:00
|
|
|
llvm::raw_pwrite_stream *ThreadOS = OSs[ThreadCount++];
|
|
|
|
// Enqueue the task
|
|
|
|
CodegenThreadPool.async(
|
2016-04-18 03:38:57 +08:00
|
|
|
[TMFactory, FileType, ThreadOS](const SmallString<0> &BC) {
|
2016-03-04 23:39:13 +08:00
|
|
|
LLVMContext Ctx;
|
2016-11-13 15:00:17 +08:00
|
|
|
Expected<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
|
2016-03-04 23:39:13 +08: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-28 07:37:36 +08:00
|
|
|
|
2016-04-16 01:34:32 +08:00
|
|
|
codegen(MPartInCtx.get(), *ThreadOS, TMFactory, FileType);
|
2016-03-04 23:39:13 +08: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-28 07:37:36 +08:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|