LLVM 19.0.0git
Go to the documentation of this file.
1//===-- ParallelCG.cpp ----------------------------------------------------===//
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
9// This file defines functions that can be used for parallel code generation.
16#include "llvm/IR/LLVMContext.h"
18#include "llvm/IR/Module.h"
24using namespace llvm;
27 function_ref<std::unique_ptr<TargetMachine>()> TMFactory,
28 CodeGenFileType FileType) {
29 std::unique_ptr<TargetMachine> TM = TMFactory();
30 assert(TM && "Failed to create target machine!");
32 legacy::PassManager CodeGenPasses;
33 if (TM->addPassesToEmitFile(CodeGenPasses, OS, nullptr, FileType))
34 report_fatal_error("Failed to setup codegen");
35 CodeGenPasses.run(*M);
41 const std::function<std::unique_ptr<TargetMachine>()> &TMFactory,
42 CodeGenFileType FileType, bool PreserveLocals) {
43 assert(BCOSs.empty() || BCOSs.size() == OSs.size());
45 if (OSs.size() == 1) {
46 if (!BCOSs.empty())
47 WriteBitcodeToFile(M, *BCOSs[0]);
48 codegen(&M, *OSs[0], TMFactory, FileType);
49 return;
50 }
52 // Create ThreadPool in nested scope so that threads will be joined
53 // on destruction.
54 {
55 DefaultThreadPool CodegenThreadPool(hardware_concurrency(OSs.size()));
56 int ThreadCount = 0;
59 M, OSs.size(),
60 [&](std::unique_ptr<Module> MPart) {
61 // We want to clone the module in a new context to multi-thread the
62 // codegen. We do it by serializing partition modules to bitcode
63 // (while still on the main thread, in order to avoid data races) and
64 // spinning up new threads which deserialize the partitions into
65 // separate contexts.
66 // FIXME: Provide a more direct way to do this in LLVM.
67 SmallString<0> BC;
68 raw_svector_ostream BCOS(BC);
69 WriteBitcodeToFile(*MPart, BCOS);
71 if (!BCOSs.empty()) {
72 BCOSs[ThreadCount]->write(BC.begin(), BC.size());
73 BCOSs[ThreadCount]->flush();
74 }
76 llvm::raw_pwrite_stream *ThreadOS = OSs[ThreadCount++];
77 // Enqueue the task
78 CodegenThreadPool.async(
79 [TMFactory, FileType, ThreadOS](const SmallString<0> &BC) {
80 LLVMContext Ctx;
81 Expected<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
82 MemoryBufferRef(BC.str(), "<split-module>"), Ctx);
83 if (!MOrErr)
84 report_fatal_error("Failed to read bitcode");
85 std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
87 codegen(MPartInCtx.get(), *ThreadOS, TMFactory, FileType);
88 },
89 // Pass BC using std::move to ensure that it get moved rather than
90 // copied into the thread's context.
91 std::move(BC));
92 },
93 PreserveLocals);
94 }
Module.h This file contains the declarations for the Module class.
static void codegen(Module *M, llvm::raw_pwrite_stream &OS, function_ref< std::unique_ptr< TargetMachine >()> TMFactory, CodeGenFileType FileType)
Definition: ParallelCG.cpp:26
const char LLVMTargetMachineRef TM
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A non-threaded implementation.
Definition: ThreadPool.h:218
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
auto async(Function &&F, Args &&...ArgList)
Asynchronous submission of a task to the pool.
Definition: ThreadPool.h:78
An efficient, type-erasing, non-owning reference to a callable.
PassManager manages ModulePassManagers.
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:445
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
ThreadPoolStrategy hardware_concurrency(unsigned ThreadCount=0)
Returns a default thread strategy where all available hardware resources are to be used,...
Definition: Threading.h:185
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.
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit,...
Definition: CodeGen.h:83
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:159
void SplitModule(Module &M, unsigned N, function_ref< void(std::unique_ptr< Module > MPart)> ModuleCallback, bool PreserveLocals=false)
Splits the module M into N linkable partitions.
void splitCodeGen(Module &M, ArrayRef< raw_pwrite_stream * > OSs, ArrayRef< llvm::raw_pwrite_stream * > BCOSs, const std::function< std::unique_ptr< TargetMachine >()> &TMFactory, CodeGenFileType FileType=CodeGenFileType::ObjectFile, bool PreserveLocals=false)
Split M into OSs.size() partitions, and generate code for each.
Definition: ParallelCG.cpp:38