LLVM  6.0.0svn
Go to the documentation of this file.
1 //===-- 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 //===----------------------------------------------------------------------===//
17 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Support/ErrorOr.h"
27 using namespace llvm;
30  function_ref<std::unique_ptr<TargetMachine>()> TMFactory,
32  std::unique_ptr<TargetMachine> TM = TMFactory();
33  legacy::PassManager CodeGenPasses;
34  if (TM->addPassesToEmitFile(CodeGenPasses, OS, FileType))
35  report_fatal_error("Failed to setup codegen");
36  CodeGenPasses.run(*M);
37 }
39 std::unique_ptr<Module> llvm::splitCodeGen(
40  std::unique_ptr<Module> M, ArrayRef<llvm::raw_pwrite_stream *> OSs,
42  const std::function<std::unique_ptr<TargetMachine>()> &TMFactory,
43  TargetMachine::CodeGenFileType FileType, bool PreserveLocals) {
44  assert(BCOSs.empty() || BCOSs.size() == OSs.size());
46  if (OSs.size() == 1) {
47  if (!BCOSs.empty())
48  WriteBitcodeToFile(M.get(), *BCOSs[0]);
49  codegen(M.get(), *OSs[0], TMFactory, FileType);
50  return M;
51  }
53  // Create ThreadPool in nested scope so that threads will be joined
54  // on destruction.
55  {
56  ThreadPool CodegenThreadPool(OSs.size());
57  int ThreadCount = 0;
60  std::move(M), OSs.size(),
61  [&](std::unique_ptr<Module> MPart) {
62  // We want to clone the module in a new context to multi-thread the
63  // codegen. We do it by serializing partition modules to bitcode
64  // (while still on the main thread, in order to avoid data races) and
65  // spinning up new threads which deserialize the partitions into
66  // separate contexts.
67  // FIXME: Provide a more direct way to do this in LLVM.
68  SmallString<0> BC;
69  raw_svector_ostream BCOS(BC);
70  WriteBitcodeToFile(MPart.get(), BCOS);
72  if (!BCOSs.empty()) {
73  BCOSs[ThreadCount]->write(BC.begin(), BC.size());
74  BCOSs[ThreadCount]->flush();
75  }
77  llvm::raw_pwrite_stream *ThreadOS = OSs[ThreadCount++];
78  // Enqueue the task
79  CodegenThreadPool.async(
80  [TMFactory, FileType, ThreadOS](const SmallString<0> &BC) {
81  LLVMContext Ctx;
83  MemoryBufferRef(StringRef(BC.data(), BC.size()),
84  "<split-module>"),
85  Ctx);
86  if (!MOrErr)
87  report_fatal_error("Failed to read bitcode");
88  std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
90  codegen(MPartInCtx.get(), *ThreadOS, TMFactory, FileType);
91  },
92  // Pass BC using std::move to ensure that it get moved rather than
93  // copied into the thread's context.
94  std::move(BC));
95  },
96  PreserveLocals);
97  }
99  return {};
100 }
static void codegen(Module *M, llvm::raw_pwrite_stream &OS, function_ref< std::unique_ptr< TargetMachine >()> TMFactory, TargetMachine::CodeGenFileType FileType)
Definition: ParallelCG.cpp:29
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:115
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:136
void SplitModule(std::unique_ptr< Module > M, unsigned N, function_ref< void(std::unique_ptr< Module > MPart)> ModuleCallback, bool PreserveLocals=false)
Splits the module M into N linkable partitions.
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:89
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:489
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
A ThreadPool for asynchronous parallel execution on a defined number of threads.
Definition: ThreadPool.h:36
PassManager manages ModulePassManagers.
void WriteBitcodeToFile(const Module *M, raw_ostream &Out, bool ShouldPreserveUseListOrder=false, const ModuleSummaryIndex *Index=nullptr, bool GenerateHash=false, ModuleHash *ModHash=nullptr)
Write the specified module to the specified raw output stream.
cl::opt< TargetMachine::CodeGenFileType > FileType("filetype", cl::init(TargetMachine::CGFT_AssemblyFile), cl::desc("Choose a file type (not all types are supported by all targets):"), cl::values(clEnumValN(TargetMachine::CGFT_AssemblyFile, "asm", "Emit an assembly ('.s') file"), clEnumValN(TargetMachine::CGFT_ObjectFile, "obj", "Emit a native object ('.o') file"), clEnumValN(TargetMachine::CGFT_Null, "null", "Emit nothing, for performance testing")))
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
Definition: SmallVector.h:116
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
Module.h This file contains the declarations for the Module class.
reference get()
Returns a reference to the stored T value.
Definition: Error.h:517
Expected< std::unique_ptr< Module > > parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context)
Read the specified bitcode file, returning the module.
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:143
Provides ErrorOr<T> smart pointer.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:337
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::unique_ptr< Module > splitCodeGen(std::unique_ptr< Module > M, ArrayRef< raw_pwrite_stream *> OSs, ArrayRef< llvm::raw_pwrite_stream *> BCOSs, const std::function< std::unique_ptr< TargetMachine >()> &TMFactory, TargetMachine::CodeGenFileType FT=TargetMachine::CGFT_ObjectFile, bool PreserveLocals=false)
Split M into OSs.size() partitions, and generate code for each.
Definition: ParallelCG.cpp:39
print Print MemDeps of function
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit...
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:144