| 1 | //===- Compilation.cpp - Compilation Task Implementation ------------------===// |
| 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 | #include "clang/Driver/Compilation.h" |
| 10 | #include "ClangCLArgs.h" |
| 11 | #include "clang/Basic/LLVM.h" |
| 12 | #include "clang/Driver/Action.h" |
| 13 | #include "clang/Driver/CommonArgs.h" |
| 14 | #include "clang/Driver/Driver.h" |
| 15 | #include "clang/Driver/Job.h" |
| 16 | #include "clang/Driver/ToolChain.h" |
| 17 | #include "clang/Driver/Util.h" |
| 18 | #include "clang/Options/Options.h" |
| 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" |
| 29 | #include <algorithm> |
| 30 | #include <cassert> |
| 31 | #include <optional> |
| 32 | #include <string> |
| 33 | #include <system_error> |
| 34 | #include <utility> |
| 35 | |
| 36 | using namespace clang; |
| 37 | using namespace driver; |
| 38 | using namespace llvm::opt; |
| 39 | |
| 40 | Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain, |
| 41 | InputArgList *_Args, DerivedArgList *_TranslatedArgs, |
| 42 | bool ContainsError) |
| 43 | : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args), |
| 44 | TranslatedArgs(_TranslatedArgs), ContainsError(ContainsError) { |
| 45 | // The offloading host toolchain is the default toolchain. |
| 46 | OrderedOffloadingToolchains.insert( |
| 47 | x: std::make_pair(x: Action::OFK_Host, y: &DefaultToolChain)); |
| 48 | } |
| 49 | |
| 50 | Compilation::~Compilation() { |
| 51 | // Remove temporary files. This must be done before arguments are freed, as |
| 52 | // the file names might be derived from the input arguments. |
| 53 | if (!TheDriver.isSaveTempsEnabled() && !ForceKeepTempFiles) |
| 54 | CleanupFileList(Files: TempFiles); |
| 55 | |
| 56 | delete TranslatedArgs; |
| 57 | delete Args; |
| 58 | |
| 59 | // Free any derived arg lists. |
| 60 | for (auto Arg : TCArgs) |
| 61 | if (Arg.second != TranslatedArgs) |
| 62 | delete Arg.second; |
| 63 | } |
| 64 | |
| 65 | const DerivedArgList & |
| 66 | Compilation::getArgsForToolChain(const ToolChain *TC, BoundArch BA, |
| 67 | Action::OffloadKind DeviceOffloadKind) { |
| 68 | if (!TC) |
| 69 | TC = &DefaultToolChain; |
| 70 | |
| 71 | DerivedArgList *&Entry = TCArgs[{TC, BA, DeviceOffloadKind}]; |
| 72 | if (!Entry) { |
| 73 | SmallVector<Arg *, 4> AllocatedArgs; |
| 74 | DerivedArgList *OpenMPArgs = nullptr; |
| 75 | // Translate OpenMP toolchain arguments provided via the -Xopenmp-target flags. |
| 76 | if (DeviceOffloadKind == Action::OFK_OpenMP) { |
| 77 | const ToolChain *HostTC = getSingleOffloadToolChain<Action::OFK_Host>(); |
| 78 | OpenMPArgs = TC->TranslateOpenMPTargetArgs( |
| 79 | Args: *TranslatedArgs, HostTriple: HostTC->getTriple(), AllocatedArgs); |
| 80 | } |
| 81 | |
| 82 | DerivedArgList *NewDAL = nullptr; |
| 83 | if (!OpenMPArgs) { |
| 84 | NewDAL = TC->TranslateXarchArgs(Args: *TranslatedArgs, BA, DeviceOffloadKind, |
| 85 | AllocatedArgs: &AllocatedArgs); |
| 86 | } else { |
| 87 | NewDAL = TC->TranslateXarchArgs(Args: *OpenMPArgs, BA, DeviceOffloadKind, |
| 88 | AllocatedArgs: &AllocatedArgs); |
| 89 | if (!NewDAL) |
| 90 | NewDAL = OpenMPArgs; |
| 91 | else |
| 92 | delete OpenMPArgs; |
| 93 | } |
| 94 | |
| 95 | if (NewDAL && TheDriver.IsCLMode()) { |
| 96 | // Forwarding can introduce clang-cl options after the shared |
| 97 | // translation. Keep synthesized arguments in the compilation-owned list. |
| 98 | auto *DAL = ClangCLArgs::translateArgs( |
| 99 | Args: *NewDAL, HostTriple: DefaultToolChain.getTriple(), Owner: *TranslatedArgs); |
| 100 | delete NewDAL; |
| 101 | NewDAL = DAL; |
| 102 | } |
| 103 | |
| 104 | if (!NewDAL) { |
| 105 | Entry = TC->TranslateArgs(Args: *TranslatedArgs, BA, DeviceOffloadKind); |
| 106 | if (!Entry) |
| 107 | Entry = TranslatedArgs; |
| 108 | } else { |
| 109 | Entry = TC->TranslateArgs(Args: *NewDAL, BA, DeviceOffloadKind); |
| 110 | if (!Entry) |
| 111 | Entry = NewDAL; |
| 112 | else |
| 113 | delete NewDAL; |
| 114 | } |
| 115 | |
| 116 | // Add allocated arguments to the final DAL. |
| 117 | for (auto *ArgPtr : AllocatedArgs) |
| 118 | Entry->AddSynthesizedArg(A: ArgPtr); |
| 119 | } |
| 120 | |
| 121 | return *Entry; |
| 122 | } |
| 123 | |
| 124 | bool Compilation::CleanupFile(const char *File, bool IssueErrors) const { |
| 125 | // FIXME: Why are we trying to remove files that we have not created? For |
| 126 | // example we should only try to remove a temporary assembly file if |
| 127 | // "clang -cc1" succeed in writing it. Was this a workaround for when |
| 128 | // clang was writing directly to a .s file and sometimes leaving it behind |
| 129 | // during a failure? |
| 130 | |
| 131 | // FIXME: If this is necessary, we can still try to split |
| 132 | // llvm::sys::fs::remove into a removeFile and a removeDir and avoid the |
| 133 | // duplicated stat from is_regular_file. |
| 134 | |
| 135 | // Don't try to remove files which we don't have write access to (but may be |
| 136 | // able to remove), or non-regular files. Underlying tools may have |
| 137 | // intentionally not overwritten them. |
| 138 | if (!llvm::sys::fs::can_write(Path: File) || !llvm::sys::fs::is_regular_file(Path: File)) |
| 139 | return true; |
| 140 | |
| 141 | if (std::error_code EC = llvm::sys::fs::remove(path: File)) { |
| 142 | // Failure is only failure if the file exists and is "regular". We checked |
| 143 | // for it being regular before, and llvm::sys::fs::remove ignores ENOENT, |
| 144 | // so we don't need to check again. |
| 145 | |
| 146 | if (IssueErrors) |
| 147 | getDriver().Diag(DiagID: diag::err_drv_unable_to_remove_file) |
| 148 | << EC.message(); |
| 149 | return false; |
| 150 | } |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | bool Compilation::CleanupFileList(const llvm::opt::ArgStringList &Files, |
| 155 | bool IssueErrors) const { |
| 156 | bool Success = true; |
| 157 | for (const auto &File: Files) |
| 158 | Success &= CleanupFile(File, IssueErrors); |
| 159 | return Success; |
| 160 | } |
| 161 | |
| 162 | bool Compilation::CleanupFileMap(const ArgStringMap &Files, |
| 163 | const JobAction *JA, |
| 164 | bool IssueErrors) const { |
| 165 | bool Success = true; |
| 166 | for (const auto &File : Files) { |
| 167 | // If specified, only delete the files associated with the JobAction. |
| 168 | // Otherwise, delete all files in the map. |
| 169 | if (JA && File.first != JA) |
| 170 | continue; |
| 171 | Success &= CleanupFile(File: File.second, IssueErrors); |
| 172 | } |
| 173 | return Success; |
| 174 | } |
| 175 | |
| 176 | int Compilation::ExecuteCommand(const Command &C, |
| 177 | const Command *&FailingCommand, |
| 178 | bool LogOnly) const { |
| 179 | if ((getDriver().CCPrintOptions || |
| 180 | getArgs().hasArg(Ids: options::OPT_v)) && !getDriver().CCGenDiagnostics) { |
| 181 | raw_ostream *OS = &llvm::errs(); |
| 182 | std::unique_ptr<llvm::raw_fd_ostream> OwnedStream; |
| 183 | |
| 184 | // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the |
| 185 | // output stream. |
| 186 | if (getDriver().CCPrintOptions && |
| 187 | !getDriver().CCPrintOptionsFilename.empty()) { |
| 188 | std::error_code EC; |
| 189 | OwnedStream.reset(p: new llvm::raw_fd_ostream( |
| 190 | getDriver().CCPrintOptionsFilename, EC, |
| 191 | llvm::sys::fs::OF_Append | llvm::sys::fs::OF_TextWithCRLF)); |
| 192 | if (EC) { |
| 193 | getDriver().Diag(DiagID: diag::err_drv_cc_print_options_failure) |
| 194 | << EC.message(); |
| 195 | FailingCommand = &C; |
| 196 | return 1; |
| 197 | } |
| 198 | OS = OwnedStream.get(); |
| 199 | } |
| 200 | |
| 201 | if (getDriver().CCPrintOptions) |
| 202 | *OS << "[Logging clang options]\n" ; |
| 203 | |
| 204 | C.Print(OS&: *OS, Terminator: "\n" , /*Quote=*/getDriver().CCPrintOptions); |
| 205 | } |
| 206 | |
| 207 | if (LogOnly) |
| 208 | return 0; |
| 209 | |
| 210 | std::string Error; |
| 211 | bool ExecutionFailed; |
| 212 | int Res = C.Execute(Redirects, ErrMsg: &Error, ExecutionFailed: &ExecutionFailed); |
| 213 | if (PostCallback) |
| 214 | PostCallback(C, Res); |
| 215 | if (!Error.empty()) { |
| 216 | assert(Res && "Error string set with 0 result code!" ); |
| 217 | getDriver().Diag(DiagID: diag::err_drv_command_failure) << Error; |
| 218 | } |
| 219 | |
| 220 | if (Res) |
| 221 | FailingCommand = &C; |
| 222 | |
| 223 | return ExecutionFailed ? 1 : Res; |
| 224 | } |
| 225 | |
| 226 | using FailingCommandList = SmallVectorImpl<std::pair<int, const Command *>>; |
| 227 | |
| 228 | static bool ActionFailed(const Action *A, |
| 229 | const FailingCommandList &FailingCommands) { |
| 230 | if (FailingCommands.empty()) |
| 231 | return false; |
| 232 | |
| 233 | // CUDA/HIP/SYCL can have the same input source code compiled multiple times |
| 234 | // so do not compile again if there are already failures. It is OK to abort |
| 235 | // the CUDA/HIP/SYCL pipeline on errors. |
| 236 | if (A->isOffloading(OKind: Action::OFK_Cuda) || A->isOffloading(OKind: Action::OFK_HIP) || |
| 237 | A->isOffloading(OKind: Action::OFK_SYCL)) |
| 238 | return true; |
| 239 | |
| 240 | for (const auto &CI : FailingCommands) |
| 241 | if (A == &(CI.second->getSource())) |
| 242 | return true; |
| 243 | |
| 244 | for (const auto *AI : A->inputs()) |
| 245 | if (ActionFailed(A: AI, FailingCommands)) |
| 246 | return true; |
| 247 | |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | static bool ActionDependsOn(const Action *A, const Action *Other) { |
| 252 | return A == Other || llvm::any_of(Range: A->inputs(), P: [&](const Action *Input) { |
| 253 | return ActionDependsOn(A: Input, Other); |
| 254 | }); |
| 255 | } |
| 256 | |
| 257 | static bool CanRunInParallelOffloadJobGroup(const Command &Job) { |
| 258 | return !Job.InProcess && !Job.PrintInputFilenames && |
| 259 | !Job.getBoundArch().empty() && |
| 260 | !Job.getOffloadDeviceParallelJobGroup().empty(); |
| 261 | } |
| 262 | |
| 263 | static bool SameParallelOffloadJobGroup(const Command &A, const Command &B) { |
| 264 | return A.getOffloadDeviceParallelJobGroup() == |
| 265 | B.getOffloadDeviceParallelJobGroup(); |
| 266 | } |
| 267 | |
| 268 | static bool HasDistinctBoundArch(const Command &Candidate, |
| 269 | ArrayRef<const Command *> Jobs) { |
| 270 | BoundArch CandidateArch = Candidate.getBoundArch(); |
| 271 | return llvm::none_of(Range&: Jobs, P: [&](const Command *Job) { |
| 272 | return Job->getBoundArch() == CandidateArch; |
| 273 | }); |
| 274 | } |
| 275 | |
| 276 | static std::optional<llvm::ThreadPoolStrategy> |
| 277 | getParallelOffloadJobsStrategy(const ArgList &Args, unsigned NumJobs) { |
| 278 | if (NumJobs < 2) |
| 279 | return std::nullopt; |
| 280 | |
| 281 | auto OffloadJobs = tools::parseOffloadJobs(Args); |
| 282 | if (!OffloadJobs.isValid()) |
| 283 | return std::nullopt; |
| 284 | |
| 285 | if (OffloadJobs.K == tools::OffloadJobsOpt::Kind::Jobserver) |
| 286 | return llvm::jobserver_concurrency(); |
| 287 | |
| 288 | if (OffloadJobs.NumThreads < 2) |
| 289 | return std::nullopt; |
| 290 | |
| 291 | return llvm::hardware_concurrency(ThreadCount: std::min(a: OffloadJobs.NumThreads, b: NumJobs)); |
| 292 | } |
| 293 | |
| 294 | struct ParallelJobResult { |
| 295 | int Res = 0; |
| 296 | bool ExecutionFailed = false; |
| 297 | std::string Error; |
| 298 | }; |
| 299 | |
| 300 | // Device-only compiles in one group can be separated by device links. |
| 301 | static bool |
| 302 | tryExecuteParallelOffloadJobGroup(const Driver &D, const ArgList &Args, |
| 303 | ArrayRef<std::optional<StringRef>> Redirects, |
| 304 | const JobList::list_type &JobStorage, |
| 305 | size_t StartIndex, |
| 306 | llvm::SmallPtrSetImpl<const Command *> &Done, |
| 307 | FailingCommandList &FailingCommands) { |
| 308 | const Command &Job = *JobStorage[StartIndex]; |
| 309 | if (!CanRunInParallelOffloadJobGroup(Job)) |
| 310 | return false; |
| 311 | |
| 312 | // The start job's dependencies precede it in the topologically ordered job |
| 313 | // list and have already run, so it is always safe to run here. |
| 314 | SmallVector<const Command *, 4> ParallelJobs{&Job}; |
| 315 | for (size_t I = StartIndex + 1; I < JobStorage.size(); ++I) { |
| 316 | const Command &Candidate = *JobStorage[I]; |
| 317 | if (Done.contains(Ptr: &Candidate) || |
| 318 | ActionFailed(A: &Candidate.getSource(), FailingCommands) || |
| 319 | !CanRunInParallelOffloadJobGroup(Job: Candidate) || |
| 320 | !SameParallelOffloadJobGroup(A: Job, B: Candidate) || |
| 321 | !HasDistinctBoundArch(Candidate, Jobs: ParallelJobs)) |
| 322 | continue; |
| 323 | |
| 324 | // In this topologically ordered list, only the candidate can depend on |
| 325 | // an earlier job, such as preprocessing with -save-temps. |
| 326 | auto EarlierJobs = llvm::make_range(x: JobStorage.begin() + StartIndex, |
| 327 | y: JobStorage.begin() + I); |
| 328 | if (!llvm::all_of(Range&: EarlierJobs, P: [&](const auto &Other) { |
| 329 | return Done.contains(Ptr: Other.get()) || |
| 330 | !ActionDependsOn(&Candidate.getSource(), &Other->getSource()); |
| 331 | })) |
| 332 | continue; |
| 333 | |
| 334 | ParallelJobs.push_back(Elt: &Candidate); |
| 335 | } |
| 336 | |
| 337 | std::optional<llvm::ThreadPoolStrategy> Strategy = |
| 338 | getParallelOffloadJobsStrategy(Args, NumJobs: ParallelJobs.size()); |
| 339 | if (!Strategy) |
| 340 | return false; |
| 341 | |
| 342 | SmallVector<ParallelJobResult, 4> Results(ParallelJobs.size()); |
| 343 | llvm::DefaultThreadPool Pool(*Strategy); |
| 344 | for (auto IndexedJob : llvm::enumerate(First&: ParallelJobs)) { |
| 345 | size_t Index = IndexedJob.index(); |
| 346 | const Command *ParallelJob = IndexedJob.value(); |
| 347 | Pool.async(F: [&, Index, ParallelJob] { |
| 348 | Results[Index].Res = ParallelJob->Execute( |
| 349 | Redirects, ErrMsg: &Results[Index].Error, ExecutionFailed: &Results[Index].ExecutionFailed); |
| 350 | }); |
| 351 | } |
| 352 | Pool.wait(); |
| 353 | |
| 354 | for (auto [Index, ParallelJob] : llvm::enumerate(First&: ParallelJobs)) { |
| 355 | ParallelJobResult &Result = Results[Index]; |
| 356 | if (!Result.Error.empty()) { |
| 357 | assert(Result.Res && "Error string set with 0 result code!" ); |
| 358 | D.Diag(DiagID: diag::err_drv_command_failure) << Result.Error; |
| 359 | } |
| 360 | |
| 361 | if (Result.Res) { |
| 362 | FailingCommands.push_back( |
| 363 | Elt: std::make_pair(x: Result.ExecutionFailed ? 1 : Result.Res, y&: ParallelJob)); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | for (const Command *ParallelJob : ParallelJobs) |
| 368 | Done.insert(Ptr: ParallelJob); |
| 369 | return true; |
| 370 | } |
| 371 | |
| 372 | void Compilation::ExecuteJobs(const JobList &Jobs, |
| 373 | FailingCommandList &FailingCommands, |
| 374 | bool LogOnly) const { |
| 375 | // According to UNIX standard, driver need to continue compiling all the |
| 376 | // inputs on the command line even one of them failed. |
| 377 | // In all but CLMode, execute all the jobs unless the necessary inputs for the |
| 378 | // job is missing due to previous failures. |
| 379 | bool CanRunJobsInParallel = |
| 380 | !LogOnly && !getDriver().CCPrintOptions && |
| 381 | !getDriver().CCPrintProcessStats && !getDriver().CCGenDiagnostics && |
| 382 | !getDriver().IsCLMode() && !getArgs().hasArg(Ids: options::OPT_v) && |
| 383 | Redirects.empty() && !PostCallback; |
| 384 | |
| 385 | // Jobs run as part of a parallel offload group. Group members are not |
| 386 | // necessarily adjacent, so track them and skip them when the loop reaches |
| 387 | // them. |
| 388 | llvm::SmallPtrSet<const Command *, 16> Done; |
| 389 | const auto &JobStorage = Jobs.getJobs(); |
| 390 | for (size_t I = 0; I < JobStorage.size(); ++I) { |
| 391 | const auto &Job = *JobStorage[I]; |
| 392 | if (Done.contains(Ptr: &Job)) |
| 393 | continue; |
| 394 | if (ActionFailed(A: &Job.getSource(), FailingCommands)) |
| 395 | continue; |
| 396 | |
| 397 | if (CanRunJobsInParallel && |
| 398 | tryExecuteParallelOffloadJobGroup(D: getDriver(), Args: getArgs(), Redirects, |
| 399 | JobStorage, StartIndex: I, Done, FailingCommands)) |
| 400 | continue; |
| 401 | |
| 402 | const Command *FailingCommand = nullptr; |
| 403 | if (int Res = ExecuteCommand(C: Job, FailingCommand, LogOnly)) { |
| 404 | FailingCommands.push_back(Elt: std::make_pair(x&: Res, y&: FailingCommand)); |
| 405 | // Bail as soon as one command fails in cl driver mode. |
| 406 | if (TheDriver.IsCLMode()) |
| 407 | return; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | void Compilation::initCompilationForDiagnostics() { |
| 413 | ForDiagnostics = true; |
| 414 | |
| 415 | // Free actions and jobs. |
| 416 | Actions.clear(); |
| 417 | AllActions.clear(); |
| 418 | Jobs.clear(); |
| 419 | |
| 420 | // Remove temporary files. |
| 421 | if (!TheDriver.isSaveTempsEnabled() && !ForceKeepTempFiles) |
| 422 | CleanupFileList(Files: TempFiles); |
| 423 | |
| 424 | // Clear temporary/results file lists. |
| 425 | TempFiles.clear(); |
| 426 | ResultFiles.clear(); |
| 427 | FailureResultFiles.clear(); |
| 428 | |
| 429 | // Remove any user specified output. Claim any unclaimed arguments, so as |
| 430 | // to avoid emitting warnings about unused args. |
| 431 | OptSpecifier OutputOpts[] = { |
| 432 | options::OPT_o, options::OPT_MD, options::OPT_MMD, options::OPT_M, |
| 433 | options::OPT_MM, options::OPT_MF, options::OPT_MG, options::OPT_MJ, |
| 434 | options::OPT_MQ, options::OPT_MT, options::OPT_MV}; |
| 435 | for (const auto &Opt : OutputOpts) { |
| 436 | if (TranslatedArgs->hasArg(Ids: Opt)) |
| 437 | TranslatedArgs->eraseArg(Id: Opt); |
| 438 | } |
| 439 | TranslatedArgs->ClaimAllArgs(); |
| 440 | |
| 441 | // Force re-creation of the toolchain Args, otherwise our modifications just |
| 442 | // above will have no effect. |
| 443 | for (auto Arg : TCArgs) |
| 444 | if (Arg.second != TranslatedArgs) |
| 445 | delete Arg.second; |
| 446 | TCArgs.clear(); |
| 447 | |
| 448 | // Redirect stdout/stderr to /dev/null. |
| 449 | Redirects = {std::nullopt, {"" }, {"" }}; |
| 450 | |
| 451 | // Temporary files added by diagnostics should be kept. |
| 452 | ForceKeepTempFiles = true; |
| 453 | } |
| 454 | |
| 455 | StringRef Compilation::getSysRoot() const { |
| 456 | return getDriver().SysRoot; |
| 457 | } |
| 458 | |
| 459 | void Compilation::Redirect(ArrayRef<std::optional<StringRef>> Redirects) { |
| 460 | this->Redirects = Redirects; |
| 461 | } |
| 462 | |