2015-08-28 07:37:36 +08:00
|
|
|
//===-- ParallelCG.cpp ----------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-08-28 07:37:36 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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,
|
2019-11-14 07:17:46 +08:00
|
|
|
CodeGenFileType FileType) {
|
2016-04-16 01:34:32 +08:00
|
|
|
std::unique_ptr<TargetMachine> TM = TMFactory();
|
2020-11-22 13:04:12 +08:00
|
|
|
assert(TM && "Failed to create target machine!");
|
|
|
|
|
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,
|
2019-11-14 07:17:46 +08:00
|
|
|
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.
|
|
|
|
{
|
[Support] On Windows, ensure hardware_concurrency() extends to all CPU sockets and all NUMA groups
The goal of this patch is to maximize CPU utilization on multi-socket or high core count systems, so that parallel computations such as LLD/ThinLTO can use all hardware threads in the system. Before this patch, on Windows, a maximum of 64 hardware threads could be used at most, in some cases dispatched only on one CPU socket.
== Background ==
Windows doesn't have a flat cpu_set_t like Linux. Instead, it projects hardware CPUs (or NUMA nodes) to applications through a concept of "processor groups". A "processor" is the smallest unit of execution on a CPU, that is, an hyper-thread if SMT is active; a core otherwise. There's a limit of 32-bit processors on older 32-bit versions of Windows, which later was raised to 64-processors with 64-bit versions of Windows. This limit comes from the affinity mask, which historically is represented by the sizeof(void*). Consequently, the concept of "processor groups" was introduced for dealing with systems with more than 64 hyper-threads.
By default, the Windows OS assigns only one "processor group" to each starting application, in a round-robin manner. If the application wants to use more processors, it needs to programmatically enable it, by assigning threads to other "processor groups". This also means that affinity cannot cross "processor group" boundaries; one can only specify a "preferred" group on start-up, but the application is free to allocate more groups if it wants to.
This creates a peculiar situation, where newer CPUs like the AMD EPYC 7702P (64-cores, 128-hyperthreads) are projected by the OS as two (2) "processor groups". This means that by default, an application can only use half of the cores. This situation could only get worse in the years to come, as dies with more cores will appear on the market.
== The problem ==
The heavyweight_hardware_concurrency() API was introduced so that only *one hardware thread per core* was used. Once that API returns, that original intention is lost, only the number of threads is retained. Consider a situation, on Windows, where the system has 2 CPU sockets, 18 cores each, each core having 2 hyper-threads, for a total of 72 hyper-threads. Both heavyweight_hardware_concurrency() and hardware_concurrency() currently return 36, because on Windows they are simply wrappers over std::thread::hardware_concurrency() -- which can only return processors from the current "processor group".
== The changes in this patch ==
To solve this situation, we capture (and retain) the initial intention until the point of usage, through a new ThreadPoolStrategy class. The number of threads to use is deferred as late as possible, until the moment where the std::threads are created (ThreadPool in the case of ThinLTO).
When using hardware_concurrency(), setting ThreadCount to 0 now means to use all the possible hardware CPU (SMT) threads. Providing a ThreadCount above to the maximum number of threads will have no effect, the maximum will be used instead.
The heavyweight_hardware_concurrency() is similar to hardware_concurrency(), except that only one thread per hardware *core* will be used.
When LLVM_ENABLE_THREADS is OFF, the threading APIs will always return 1, to ensure any caller loops will be exercised at least once.
Differential Revision: https://reviews.llvm.org/D71775
2020-02-14 11:49:57 +08:00
|
|
|
ThreadPool CodegenThreadPool(hardware_concurrency(OSs.size()));
|
2016-03-04 23:39:13 +08:00
|
|
|
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 {};
|
|
|
|
}
|