LLVM  12.0.0git
Go to the documentation of this file.
1 //===-- ParallelCG.cpp ----------------------------------------------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines functions that can be used for parallel code generation.
10 //
11 //===----------------------------------------------------------------------===//
16 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Support/ErrorOr.h"
25 using namespace llvm;
28  function_ref<std::unique_ptr<TargetMachine>()> TMFactory,
30  std::unique_ptr<TargetMachine> TM = TMFactory();
31  legacy::PassManager CodeGenPasses;
32  if (TM->addPassesToEmitFile(CodeGenPasses, OS, nullptr, FileType))
33  report_fatal_error("Failed to setup codegen");
34  CodeGenPasses.run(*M);
35 }
37 std::unique_ptr<Module> llvm::splitCodeGen(
38  std::unique_ptr<Module> M, ArrayRef<llvm::raw_pwrite_stream *> OSs,
40  const std::function<std::unique_ptr<TargetMachine>()> &TMFactory,
41  CodeGenFileType FileType, bool PreserveLocals) {
42  assert(BCOSs.empty() || BCOSs.size() == OSs.size());
44  if (OSs.size() == 1) {
45  if (!BCOSs.empty())
46  WriteBitcodeToFile(*M, *BCOSs[0]);
47  codegen(M.get(), *OSs[0], TMFactory, FileType);
48  return M;
49  }
51  // Create ThreadPool in nested scope so that threads will be joined
52  // on destruction.
53  {
54  ThreadPool CodegenThreadPool(hardware_concurrency(OSs.size()));
55  int ThreadCount = 0;
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.
66  SmallString<0> BC;
67  raw_svector_ostream BCOS(BC);
68  WriteBitcodeToFile(*MPart, BCOS);
70  if (!BCOSs.empty()) {
71  BCOSs[ThreadCount]->write(BC.begin(), BC.size());
72  BCOSs[ThreadCount]->flush();
73  }
75  llvm::raw_pwrite_stream *ThreadOS = OSs[ThreadCount++];
76  // Enqueue the task
77  CodegenThreadPool.async(
78  [TMFactory, FileType, ThreadOS](const SmallString<0> &BC) {
79  LLVMContext Ctx;
81  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());
88  codegen(MPartInCtx.get(), *ThreadOS, TMFactory, FileType);
89  },
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));
93  },
94  PreserveLocals);
95  }
97  return {};
98 }
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, CodeGenFileType FileType=CGFT_ObjectFile, bool PreserveLocals=false)
Split M into OSs.size() partitions, and generate code for each.
Definition: ParallelCG.cpp:37
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:140
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:67
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:176
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:550
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
std::shared_future< void > async(Function &&F, Args &&... ArgList)
Asynchronous submission of a task to the pool.
Definition: ThreadPool.h:54
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
A ThreadPool for asynchronous parallel execution on a defined number of threads.
Definition: ThreadPool.h:37
static void codegen(Module *M, llvm::raw_pwrite_stream &OS, function_ref< std::unique_ptr< TargetMachine >()> TMFactory, CodeGenFileType FileType)
Definition: ParallelCG.cpp:27
PassManager manages ModulePassManagers.
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit...
Definition: CodeGen.h:63
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:156
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.
Defines the file type this file represents.
Definition: InterfaceFile.h:57
Module.h This file contains the declarations for the Module class.
reference get()
Returns a reference to the stored T value.
Definition: Error.h:537
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:171
ThreadPoolStrategy hardware_concurrency(unsigned ThreadCount=0)
Returns a default thread strategy where all available hardware ressources are to be used...
Definition: Threading.h:217
size_t size() const
Definition: SmallVector.h:66
Provides ErrorOr<T> smart pointer.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:383
Expected< std::unique_ptr< Module > > parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, DataLayoutCallbackTy DataLayoutCallback=[](StringRef) { return None;})
Read the specified bitcode file, returning the module.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
print Print MemDeps of function
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:151