19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/SmallPtrSet.h"
21#include "llvm/Option/ArgList.h"
22#include "llvm/Option/OptSpecifier.h"
23#include "llvm/Option/Option.h"
24#include "llvm/Support/FileSystem.h"
25#include "llvm/Support/ThreadPool.h"
26#include "llvm/Support/Threading.h"
27#include "llvm/Support/raw_ostream.h"
28#include "llvm/TargetParser/Triple.h"
33#include <system_error>
41 InputArgList *_Args, DerivedArgList *_TranslatedArgs,
43 : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
44 TranslatedArgs(_TranslatedArgs), ContainsError(ContainsError) {
46 OrderedOffloadingToolchains.insert(
53 if (!TheDriver.isSaveTempsEnabled() && !ForceKeepTempFiles)
56 delete TranslatedArgs;
60 for (
auto Arg : TCArgs)
61 if (Arg.second != TranslatedArgs)
69 TC = &DefaultToolChain;
71 DerivedArgList *&Entry = TCArgs[{TC, BA, DeviceOffloadKind}];
74 DerivedArgList *OpenMPArgs =
nullptr;
79 *TranslatedArgs, HostTC->
getTriple(), AllocatedArgs);
82 DerivedArgList *NewDAL =
nullptr;
95 if (NewDAL && TheDriver.IsCLMode()) {
99 *NewDAL, DefaultToolChain.getTriple(), *TranslatedArgs);
105 Entry = TC->
TranslateArgs(*TranslatedArgs, BA, DeviceOffloadKind);
107 Entry = TranslatedArgs;
117 for (
auto *ArgPtr : AllocatedArgs)
118 Entry->AddSynthesizedArg(ArgPtr);
127 auto It = llvm::lower_bound(Devices,
Device);
128 if (It == Devices.end() || *It !=
Device)
129 Devices.insert(It,
Device);
134 auto It = IGCADevices.find({TC,
Target});
135 if (It == IGCADevices.end())
154 if (!llvm::sys::fs::can_write(
File) || !llvm::sys::fs::is_regular_file(
File))
157 if (std::error_code EC = llvm::sys::fs::remove(
File)) {
171 bool IssueErrors)
const {
173 for (
const auto &
File: Files)
180 bool IssueErrors)
const {
182 for (
const auto &
File : Files) {
185 if (JA &&
File.first != JA)
193 const Command *&FailingCommand,
194 bool LogOnly)
const {
197 raw_ostream *OS = &llvm::errs();
198 std::unique_ptr<llvm::raw_fd_ostream> OwnedStream;
203 !
getDriver().CCPrintOptionsFilename.empty()) {
205 OwnedStream.reset(
new llvm::raw_fd_ostream(
207 llvm::sys::fs::OF_Append | llvm::sys::fs::OF_TextWithCRLF));
214 OS = OwnedStream.get();
218 *OS <<
"[Logging clang options]\n";
220 C.Print(*OS,
"\n",
getDriver().CCPrintOptions);
227 bool ExecutionFailed;
228 int Res =
C.Execute(Redirects, &
Error, &ExecutionFailed);
230 PostCallback(
C, Res);
231 if (!
Error.empty()) {
232 assert(Res &&
"Error string set with 0 result code!");
239 return ExecutionFailed ? 1 : Res;
246 if (FailingCommands.empty())
256 for (
const auto &CI : FailingCommands)
257 if (A == &(CI.second->getSource()))
260 for (
const auto *AI : A->
inputs())
269 return ActionDependsOn(Input, Other);
287 return llvm::none_of(Jobs, [&](
const Command *Job) {
292static std::optional<llvm::ThreadPoolStrategy>
298 if (!OffloadJobs.isValid())
302 return llvm::jobserver_concurrency();
304 if (OffloadJobs.NumThreads < 2)
307 return llvm::hardware_concurrency(std::min(OffloadJobs.NumThreads, NumJobs));
319 ArrayRef<std::optional<StringRef>> Redirects,
322 llvm::SmallPtrSetImpl<const Command *> &Done,
324 const Command &Job = *JobStorage[StartIndex];
331 for (
size_t I = StartIndex + 1; I < JobStorage.size(); ++I) {
332 const Command &Candidate = *JobStorage[I];
333 if (Done.contains(&Candidate) ||
342 auto EarlierJobs = llvm::make_range(JobStorage.begin() + StartIndex,
343 JobStorage.begin() + I);
344 if (!llvm::all_of(EarlierJobs, [&](
const auto &
Other) {
345 return Done.contains(
Other.get()) ||
350 ParallelJobs.push_back(&Candidate);
353 std::optional<llvm::ThreadPoolStrategy> Strategy =
359 llvm::DefaultThreadPool Pool(*Strategy);
360 for (
auto IndexedJob : llvm::enumerate(ParallelJobs)) {
361 size_t Index = IndexedJob.index();
362 const Command *ParallelJob = IndexedJob.value();
363 Pool.async([&, Index, ParallelJob] {
364 Results[Index].Res = ParallelJob->
Execute(
365 Redirects, &Results[Index].
Error, &Results[Index].ExecutionFailed);
370 for (
auto [Index, ParallelJob] : llvm::enumerate(ParallelJobs)) {
372 if (!
Result.Error.empty()) {
373 assert(
Result.Res &&
"Error string set with 0 result code!");
374 D.
Diag(diag::err_drv_command_failure) <<
Result.Error;
378 FailingCommands.push_back(
379 std::make_pair(
Result.ExecutionFailed ? 1 :
Result.Res, ParallelJob));
383 for (
const Command *ParallelJob : ParallelJobs)
384 Done.insert(ParallelJob);
390 bool LogOnly)
const {
395 bool CanRunJobsInParallel =
399 Redirects.empty() && !PostCallback;
405 const auto &JobStorage = Jobs.getJobs();
406 for (
size_t I = 0; I < JobStorage.size(); ++I) {
407 const auto &Job = *JobStorage[I];
408 if (Done.contains(&Job))
413 if (CanRunJobsInParallel &&
415 JobStorage, I, Done, FailingCommands))
418 const Command *FailingCommand =
nullptr;
420 FailingCommands.push_back(std::make_pair(Res, FailingCommand));
422 if (TheDriver.IsCLMode())
429 ForDiagnostics =
true;
437 if (!TheDriver.isSaveTempsEnabled() && !ForceKeepTempFiles)
443 FailureResultFiles.clear();
447 OptSpecifier OutputOpts[] = {
448 options::OPT_o, options::OPT_MD, options::OPT_MMD, options::OPT_M,
449 options::OPT_MM, options::OPT_MF, options::OPT_MG, options::OPT_MJ,
450 options::OPT_MQ, options::OPT_MT, options::OPT_MV};
451 for (
const auto &Opt : OutputOpts) {
452 if (TranslatedArgs->hasArg(Opt))
453 TranslatedArgs->eraseArg(Opt);
455 TranslatedArgs->ClaimAllArgs();
459 for (
auto Arg : TCArgs)
460 if (Arg.second != TranslatedArgs)
465 Redirects = {std::nullopt, {
""}, {
""}};
468 ForceKeepTempFiles =
true;
476 this->Redirects = Redirects;
static bool ActionFailed(const Action *A, const FailingCommandList &FailingCommands)
static bool tryExecuteParallelOffloadJobGroup(const Driver &D, const ArgList &Args, ArrayRef< std::optional< StringRef > > Redirects, const JobList::list_type &JobStorage, size_t StartIndex, llvm::SmallPtrSetImpl< const Command * > &Done, FailingCommandList &FailingCommands)
static bool HasDistinctBoundArch(const Command &Candidate, ArrayRef< const Command * > Jobs)
static bool ActionDependsOn(const Action *A, const Action *Other)
static bool SameParallelOffloadJobGroup(const Command &A, const Command &B)
SmallVectorImpl< std::pair< int, const Command * > > FailingCommandList
static bool CanRunInParallelOffloadJobGroup(const Command &Job)
static std::optional< llvm::ThreadPoolStrategy > getParallelOffloadJobsStrategy(const ArgList &Args, unsigned NumJobs)
Result
Implement __builtin_bit_cast and related operations.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Action - Represent an abstract compilation step to perform.
bool isOffloading(OffloadKind OKind) const
static llvm::opt::DerivedArgList * translateArgs(const llvm::opt::DerivedArgList &Args, const llvm::Triple &HostTriple, const llvm::opt::DerivedArgList &Owner)
Translate a filtered list that may contain new clang-cl options, replacing earlier /O expansions.
Command - An executable path/name and argument vector to execute.
const Action & getSource() const
getSource - Return the Action which caused the creation of this job.
StringRef getOffloadDeviceParallelJobGroup() const
bool PrintInputFilenames
Whether to print the input filenames when executing.
bool InProcess
Whether the command will be executed in this process or not.
BoundArch getBoundArch() const
Return the bound architecture for this command, if any.
virtual int Execute(ArrayRef< std::optional< StringRef > > Redirects, std::string *ErrMsg, bool *ExecutionFailed) const
int ExecuteCommand(const Command &C, const Command *&FailingCommand, bool LogOnly=false) const
ExecuteCommand - Execute an actual command.
bool CleanupFileMap(const ArgStringMap &Files, const JobAction *JA, bool IssueErrors=false) const
CleanupFileMap - Remove the files in the given map.
bool CleanupFile(const char *File, bool IssueErrors=false) const
CleanupFile - Delete a given file.
Compilation(const Driver &D, const ToolChain &DefaultToolChain, llvm::opt::InputArgList *Args, llvm::opt::DerivedArgList *TranslatedArgs, bool ContainsError)
bool CleanupFileList(const llvm::opt::ArgStringList &Files, bool IssueErrors=false) const
CleanupFileList - Remove the files in the given list.
const llvm::opt::DerivedArgList & getArgsForToolChain(const ToolChain *TC, BoundArch BA, Action::OffloadKind DeviceOffloadKind)
getArgsForToolChain - Return the derived argument list for the tool chain TC (or the default tool cha...
StringRef getSysRoot() const
Returns the sysroot path.
void Redirect(ArrayRef< std::optional< StringRef > > Redirects)
Redirect - Redirect output of this compilation.
void initCompilationForDiagnostics()
initCompilationForDiagnostics - Remove stale state and suppress output so compilation can be reexecut...
ArrayRef< StringRef > getIGCADevices(const ToolChain *TC, StringRef Target) const
const llvm::opt::DerivedArgList & getArgs() const
void ExecuteJobs(const JobList &Jobs, SmallVectorImpl< std::pair< int, const Command * > > &FailingCommands, bool LogOnly=false) const
ExecuteJob - Execute a single job.
const ToolChain * getSingleOffloadToolChain() const
Return an offload toolchain of the provided kind.
const Driver & getDriver() const
void addIGCADevice(const ToolChain *TC, StringRef Target, StringRef Device)
Add the Intel GPU Device to the GPUs of the IGCA target Target for the device toolchain TC.
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
std::string SysRoot
sysroot, if present
unsigned CCPrintProcessStats
Set CC_PRINT_PROC_STAT mode, which causes the driver to dump performance report to CC_PRINT_PROC_STAT...
bool IsCLMode() const
Whether the driver should follow cl.exe like behavior.
unsigned CCGenDiagnostics
Whether the driver is generating diagnostics for debugging purposes.
DiagnosticBuilder Diag(unsigned DiagID) const
unsigned CCPrintOptions
Set CC_PRINT_OPTIONS mode, which is like -v but logs the commands to CCPrintOptionsFilename or to std...
JobList - A sequence of jobs to perform.
SmallVector< std::unique_ptr< Command >, 4 > list_type
llvm::DenseMap< const JobAction *, const char * > ArgStringMap
ArgStringMap - Type used to map a JobAction to its result file.
Top level wrappers for InstallAPI frontend operations.
@ Success
Annotation was successful.
Represents a bound architecture for offload / multiple architecture compilation.