13#include "llvm/Config/llvm-config.h"
23#define DEBUG_TYPE "jobserver"
34struct JobserverConfig {
50 if (
Input.consume_front(Prefix)) {
60static std::optional<FdPair> getFileDescriptorPair(
StringRef Input) {
62 if (
Input.consumeInteger(10, FDs.Read))
64 if (!
Input.consume_front(
","))
66 if (
Input.consumeInteger(10, FDs.Write))
68 if (!
Input.empty() || !FDs.isValid())
79 JobserverConfig Config;
80 if (MakeFlags.
empty())
89 if (!Args.empty() && !Args[0].starts_with(
"-") && Args[0].contains(
'n'))
96 if (getPrefixedValue(Arg,
"--jobserver-auth=",
Value)) {
98 if (
auto FDPair = getFileDescriptorPair(
Value)) {
99 Config.TheMode = JobserverConfig::PosixPipe;
100 Config.PipeFDs = *FDPair;
104 if (getPrefixedValue(
Value,
"fifo:", FifoPath)) {
105 Config.TheMode = JobserverConfig::PosixFifo;
106 Config.Path = FifoPath.
str();
109 Config.TheMode = JobserverConfig::Win32Semaphore;
110 Config.Path =
Value.str();
113 }
else if (getPrefixedValue(Arg,
"--jobserver-fds=",
Value)) {
115 if (
auto FDPair = getFileDescriptorPair(
Value)) {
116 Config.TheMode = JobserverConfig::PosixPipe;
117 Config.PipeFDs = *FDPair;
120 "Invalid file descriptor pair in MAKEFLAGS");
127 if (Config.TheMode == JobserverConfig::PosixFifo ||
128 Config.TheMode == JobserverConfig::PosixPipe)
131 "FIFO/Pipe-based jobserver is not supported on Windows");
133 if (Config.TheMode == JobserverConfig::Win32Semaphore)
136 "Semaphore-based jobserver is not supported on this platform");
141std::once_flag GJobserverOnceFlag;
148 bool IsInitialized =
false;
149 std::atomic<bool> HasImplicitSlot{
true};
150 unsigned NumJobs = 0;
160 bool isValid()
const {
return IsInitialized; }
163#if defined(LLVM_ON_UNIX)
166 std::string FifoPath;
168 void *Semaphore =
nullptr;
174#if defined(LLVM_ON_UNIX)
191 return static_cast<uint8_t>(Value);
201 std::call_once(GJobserverOnceFlag, []() {
204 <<
"JobserverClient::getInstance() called for the first time.\n");
205 const char *MakeFlagsEnv = getenv(
"MAKEFLAGS");
207 errs() <<
"Warning: failed to create jobserver client due to MAKEFLAGS "
208 "environment variable not found\n";
212 LLVM_DEBUG(
dbgs() <<
"Found MAKEFLAGS = \"" << MakeFlagsEnv <<
"\"\n");
214 auto ConfigOrErr = parseNativeMakeFlags(MakeFlagsEnv);
215 if (
Error Err = ConfigOrErr.takeError()) {
216 errs() <<
"Warning: failed to create jobserver client due to invalid "
217 "MAKEFLAGS environment variable: "
218 <<
toString(std::move(Err)) <<
"\n";
222 JobserverConfig Config = *ConfigOrErr;
223 if (Config.TheMode == JobserverConfig::None) {
224 errs() <<
"Warning: failed to create jobserver client due to jobserver "
225 "mode missing in MAKEFLAGS environment variable\n";
229 if (Config.TheMode == JobserverConfig::PosixPipe) {
230#if defined(LLVM_ON_UNIX)
231 if (!areFdsValid(Config.PipeFDs.Read, Config.PipeFDs.Write)) {
232 errs() <<
"Warning: failed to create jobserver client due to invalid "
233 "Pipe FDs in MAKEFLAGS environment variable\n";
239 auto Client = std::make_unique<JobserverClientImpl>(Config);
240 if (Client->isValid()) {
242 GJobserver = Client.release();
244 errs() <<
"Warning: jobserver client initialization failed.\n";
255 GJobserver =
nullptr;
257 new (&GJobserverOnceFlag) std::once_flag();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
Lightweight error class with error context and mandatory checking.
Tagged union holding either a T or a Error.
A JobSlot represents a single job slot that can be acquired from or released to a jobserver pool.
uint8_t getExplicitValue() const
JobserverClientImpl(const JobserverConfig &Config)
~JobserverClientImpl() override
void release(JobSlot Slot) override
Releases a job slot back to the pool.
JobSlot tryAcquire() override
Tries to acquire a job slot from the pool.
unsigned getNumJobs() const override
Returns the number of job slots available, as determined on first use.
The public interface for a jobserver client.
static JobserverClient * getInstance()
Returns the singleton instance of the JobserverClient.
static void resetForTesting()
Resets the singleton instance. For testing purposes only.
virtual ~JobserverClient()
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
std::string str() const
str - Get the contents as an std::string.
constexpr bool empty() const
empty - Check if the string is empty.
LLVM Value Representation.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
LLVM_ABI void SplitString(StringRef Source, SmallVectorImpl< StringRef > &OutFragments, StringRef Delimiters=" \t\n\v\f\r")
SplitString - Split up the specified string according to the specified delimiters,...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)