1//===--- Driver.cpp - Clang GCC Compatible Driver -------------------------===//
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/Driver.h"
10#include "ToolChains/AIX.h"
11#include "ToolChains/AMDGPU.h"
12#include "ToolChains/AMDGPUOpenMP.h"
13#include "ToolChains/AVR.h"
14#include "ToolChains/Arch/RISCV.h"
15#include "ToolChains/BareMetal.h"
16#include "ToolChains/CSKYToolChain.h"
17#include "ToolChains/Clang.h"
18#include "ToolChains/CrossWindows.h"
19#include "ToolChains/Cuda.h"
20#include "ToolChains/Darwin.h"
21#include "ToolChains/DragonFly.h"
22#include "ToolChains/FreeBSD.h"
23#include "ToolChains/Fuchsia.h"
24#include "ToolChains/Gnu.h"
25#include "ToolChains/HIPAMD.h"
26#include "ToolChains/HIPSPV.h"
27#include "ToolChains/HLSL.h"
28#include "ToolChains/Haiku.h"
29#include "ToolChains/Hexagon.h"
30#include "ToolChains/Hurd.h"
31#include "ToolChains/Lanai.h"
32#include "ToolChains/Linux.h"
33#include "ToolChains/MSP430.h"
34#include "ToolChains/MSVC.h"
35#include "ToolChains/MinGW.h"
36#include "ToolChains/MipsLinux.h"
37#include "ToolChains/NaCl.h"
38#include "ToolChains/NetBSD.h"
39#include "ToolChains/OHOS.h"
40#include "ToolChains/OpenBSD.h"
41#include "ToolChains/PPCFreeBSD.h"
42#include "ToolChains/PPCLinux.h"
43#include "ToolChains/PS4CPU.h"
44#include "ToolChains/RISCVToolchain.h"
45#include "ToolChains/SPIRV.h"
46#include "ToolChains/Solaris.h"
47#include "ToolChains/TCE.h"
48#include "ToolChains/VEToolchain.h"
49#include "ToolChains/WebAssembly.h"
50#include "ToolChains/XCore.h"
51#include "ToolChains/ZOS.h"
52#include "clang/Basic/DiagnosticDriver.h"
53#include "clang/Basic/TargetID.h"
54#include "clang/Basic/Version.h"
55#include "clang/Config/config.h"
56#include "clang/Driver/Action.h"
57#include "clang/Driver/Compilation.h"
58#include "clang/Driver/DriverDiagnostic.h"
59#include "clang/Driver/InputInfo.h"
60#include "clang/Driver/Job.h"
61#include "clang/Driver/Options.h"
62#include "clang/Driver/Phases.h"
63#include "clang/Driver/SanitizerArgs.h"
64#include "clang/Driver/Tool.h"
65#include "clang/Driver/ToolChain.h"
66#include "clang/Driver/Types.h"
67#include "llvm/ADT/ArrayRef.h"
68#include "llvm/ADT/STLExtras.h"
69#include "llvm/ADT/StringExtras.h"
70#include "llvm/ADT/StringRef.h"
71#include "llvm/ADT/StringSet.h"
72#include "llvm/ADT/StringSwitch.h"
73#include "llvm/Config/llvm-config.h"
74#include "llvm/MC/TargetRegistry.h"
75#include "llvm/Option/Arg.h"
76#include "llvm/Option/ArgList.h"
77#include "llvm/Option/OptSpecifier.h"
78#include "llvm/Option/OptTable.h"
79#include "llvm/Option/Option.h"
80#include "llvm/Support/CommandLine.h"
81#include "llvm/Support/ErrorHandling.h"
82#include "llvm/Support/ExitCodes.h"
83#include "llvm/Support/FileSystem.h"
84#include "llvm/Support/FormatVariadic.h"
85#include "llvm/Support/MD5.h"
86#include "llvm/Support/Path.h"
87#include "llvm/Support/PrettyStackTrace.h"
88#include "llvm/Support/Process.h"
89#include "llvm/Support/Program.h"
90#include "llvm/Support/Regex.h"
91#include "llvm/Support/StringSaver.h"
92#include "llvm/Support/VirtualFileSystem.h"
93#include "llvm/Support/raw_ostream.h"
94#include "llvm/TargetParser/Host.h"
95#include "llvm/TargetParser/RISCVISAInfo.h"
96#include <cstdlib> // ::getenv
97#include <map>
98#include <memory>
99#include <optional>
100#include <set>
101#include <utility>
102#if LLVM_ON_UNIX
103#include <unistd.h> // getpid
104#endif
105
106using namespace clang::driver;
107using namespace clang;
108using namespace llvm::opt;
109
110static std::optional<llvm::Triple> getOffloadTargetTriple(const Driver &D,
111 const ArgList &Args) {
112 auto OffloadTargets = Args.getAllArgValues(options::OPT_offload_EQ);
113 // Offload compilation flow does not support multiple targets for now. We
114 // need the HIPActionBuilder (and possibly the CudaActionBuilder{,Base}too)
115 // to support multiple tool chains first.
116 switch (OffloadTargets.size()) {
117 default:
118 D.Diag(diag::DiagID: err_drv_only_one_offload_target_supported);
119 return std::nullopt;
120 case 0:
121 D.Diag(diag::DiagID: err_drv_invalid_or_unsupported_offload_target) << "";
122 return std::nullopt;
123 case 1:
124 break;
125 }
126 return llvm::Triple(OffloadTargets[0]);
127}
128
129static std::optional<llvm::Triple>
130getNVIDIAOffloadTargetTriple(const Driver &D, const ArgList &Args,
131 const llvm::Triple &HostTriple) {
132 if (!Args.hasArg(options::OPT_offload_EQ)) {
133 return llvm::Triple(HostTriple.isArch64Bit() ? "nvptx64-nvidia-cuda"
134 : "nvptx-nvidia-cuda");
135 }
136 auto TT = getOffloadTargetTriple(D, Args);
137 if (TT && (TT->getArch() == llvm::Triple::spirv32 ||
138 TT->getArch() == llvm::Triple::spirv64)) {
139 if (Args.hasArg(options::OPT_emit_llvm))
140 return TT;
141 D.Diag(diag::DiagID: err_drv_cuda_offload_only_emit_bc);
142 return std::nullopt;
143 }
144 D.Diag(diag::DiagID: err_drv_invalid_or_unsupported_offload_target) << TT->str();
145 return std::nullopt;
146}
147static std::optional<llvm::Triple>
148getHIPOffloadTargetTriple(const Driver &D, const ArgList &Args) {
149 if (!Args.hasArg(options::OPT_offload_EQ)) {
150 return llvm::Triple("amdgcn-amd-amdhsa"); // Default HIP triple.
151 }
152 auto TT = getOffloadTargetTriple(D, Args);
153 if (!TT)
154 return std::nullopt;
155 if (TT->getArch() == llvm::Triple::amdgcn &&
156 TT->getVendor() == llvm::Triple::AMD &&
157 TT->getOS() == llvm::Triple::AMDHSA)
158 return TT;
159 if (TT->getArch() == llvm::Triple::spirv64)
160 return TT;
161 D.Diag(diag::DiagID: err_drv_invalid_or_unsupported_offload_target) << TT->str();
162 return std::nullopt;
163}
164
165// static
166std::string Driver::GetResourcesPath(StringRef BinaryPath,
167 StringRef CustomResourceDir) {
168 // Since the resource directory is embedded in the module hash, it's important
169 // that all places that need it call this function, so that they get the
170 // exact same string ("a/../b/" and "b/" get different hashes, for example).
171
172 // Dir is bin/ or lib/, depending on where BinaryPath is.
173 std::string Dir = std::string(llvm::sys::path::parent_path(path: BinaryPath));
174
175 SmallString<128> P(Dir);
176 if (CustomResourceDir != "") {
177 llvm::sys::path::append(path&: P, a: CustomResourceDir);
178 } else {
179 // On Windows, libclang.dll is in bin/.
180 // On non-Windows, libclang.so/.dylib is in lib/.
181 // With a static-library build of libclang, LibClangPath will contain the
182 // path of the embedding binary, which for LLVM binaries will be in bin/.
183 // ../lib gets us to lib/ in both cases.
184 P = llvm::sys::path::parent_path(path: Dir);
185 // This search path is also created in the COFF driver of lld, so any
186 // changes here also needs to happen in lld/COFF/Driver.cpp
187 llvm::sys::path::append(path&: P, CLANG_INSTALL_LIBDIR_BASENAME, b: "clang",
188 CLANG_VERSION_MAJOR_STRING);
189 }
190
191 return std::string(P);
192}
193
194Driver::Driver(StringRef ClangExecutable, StringRef TargetTriple,
195 DiagnosticsEngine &Diags, std::string Title,
196 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS)
197 : Diags(Diags), VFS(std::move(VFS)), Mode(GCCMode),
198 SaveTemps(SaveTempsNone), BitcodeEmbed(EmbedNone),
199 Offload(OffloadHostDevice), CXX20HeaderType(HeaderMode_None),
200 ModulesModeCXX20(false), LTOMode(LTOK_None),
201 ClangExecutable(ClangExecutable), SysRoot(DEFAULT_SYSROOT),
202 DriverTitle(Title), CCCPrintBindings(false), CCPrintOptions(false),
203 CCLogDiagnostics(false), CCGenDiagnostics(false),
204 CCPrintProcessStats(false), CCPrintInternalStats(false),
205 TargetTriple(TargetTriple), Saver(Alloc), PrependArg(nullptr),
206 CheckInputsExist(true), ProbePrecompiled(true),
207 SuppressMissingInputWarning(false) {
208 // Provide a sane fallback if no VFS is specified.
209 if (!this->VFS)
210 this->VFS = llvm::vfs::getRealFileSystem();
211
212 Name = std::string(llvm::sys::path::filename(path: ClangExecutable));
213 Dir = std::string(llvm::sys::path::parent_path(path: ClangExecutable));
214
215 if ((!SysRoot.empty()) && llvm::sys::path::is_relative(path: SysRoot)) {
216 // Prepend InstalledDir if SysRoot is relative
217 SmallString<128> P(Dir);
218 llvm::sys::path::append(path&: P, a: SysRoot);
219 SysRoot = std::string(P);
220 }
221
222#if defined(CLANG_CONFIG_FILE_SYSTEM_DIR)
223 SystemConfigDir = CLANG_CONFIG_FILE_SYSTEM_DIR;
224#endif
225#if defined(CLANG_CONFIG_FILE_USER_DIR)
226 {
227 SmallString<128> P;
228 llvm::sys::fs::expand_tilde(CLANG_CONFIG_FILE_USER_DIR, P);
229 UserConfigDir = static_cast<std::string>(P);
230 }
231#endif
232
233 // Compute the path to the resource directory.
234 ResourceDir = GetResourcesPath(BinaryPath: ClangExecutable, CLANG_RESOURCE_DIR);
235}
236
237void Driver::setDriverMode(StringRef Value) {
238 static StringRef OptName =
239 getOpts().getOption(options::Opt: OPT_driver_mode).getPrefixedName();
240 if (auto M = llvm::StringSwitch<std::optional<DriverMode>>(Value)
241 .Case(S: "gcc", Value: GCCMode)
242 .Case(S: "g++", Value: GXXMode)
243 .Case(S: "cpp", Value: CPPMode)
244 .Case(S: "cl", Value: CLMode)
245 .Case(S: "flang", Value: FlangMode)
246 .Case(S: "dxc", Value: DXCMode)
247 .Default(Value: std::nullopt))
248 Mode = *M;
249 else
250 Diag(diag::DiagID: err_drv_unsupported_option_argument) << OptName << Value;
251}
252
253InputArgList Driver::ParseArgStrings(ArrayRef<const char *> ArgStrings,
254 bool UseDriverMode, bool &ContainsError) {
255 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
256 ContainsError = false;
257
258 llvm::opt::Visibility VisibilityMask = getOptionVisibilityMask(UseDriverMode);
259 unsigned MissingArgIndex, MissingArgCount;
260 InputArgList Args = getOpts().ParseArgs(Args: ArgStrings, MissingArgIndex,
261 MissingArgCount, VisibilityMask);
262
263 // Check for missing argument error.
264 if (MissingArgCount) {
265 Diag(diag::DiagID: err_drv_missing_argument)
266 << Args.getArgString(Index: MissingArgIndex) << MissingArgCount;
267 ContainsError |=
268 Diags.getDiagnosticLevel(diag::DiagID: err_drv_missing_argument,
269 Loc: SourceLocation()) > DiagnosticsEngine::Warning;
270 }
271
272 // Check for unsupported options.
273 for (const Arg *A : Args) {
274 if (A->getOption().hasFlag(Val: options::Unsupported)) {
275 Diag(diag::DiagID: err_drv_unsupported_opt) << A->getAsString(Args);
276 ContainsError |= Diags.getDiagnosticLevel(diag::DiagID: err_drv_unsupported_opt,
277 Loc: SourceLocation()) >
278 DiagnosticsEngine::Warning;
279 continue;
280 }
281
282 // Warn about -mcpu= without an argument.
283 if (A->getOption().matches(options::ID: OPT_mcpu_EQ) && A->containsValue(Value: "")) {
284 Diag(diag::DiagID: warn_drv_empty_joined_argument) << A->getAsString(Args);
285 ContainsError |= Diags.getDiagnosticLevel(
286 diag::DiagID: warn_drv_empty_joined_argument,
287 Loc: SourceLocation()) > DiagnosticsEngine::Warning;
288 }
289 }
290
291 for (const Arg *A : Args.filtered(options::OPT_UNKNOWN)) {
292 unsigned DiagID;
293 auto ArgString = A->getAsString(Args);
294 std::string Nearest;
295 if (getOpts().findNearest(ArgString, Nearest, VisibilityMask) > 1) {
296 if (!IsCLMode() &&
297 getOpts().findExact(ArgString, Nearest,
298 llvm::opt::Visibility(options::CC1Option))) {
299 DiagID = diag::err_drv_unknown_argument_with_suggestion;
300 Diags.Report(DiagID) << ArgString << "-Xclang " + Nearest;
301 } else {
302 DiagID = IsCLMode() ? diag::warn_drv_unknown_argument_clang_cl
303 : diag::err_drv_unknown_argument;
304 Diags.Report(DiagID) << ArgString;
305 }
306 } else {
307 DiagID = IsCLMode()
308 ? diag::warn_drv_unknown_argument_clang_cl_with_suggestion
309 : diag::err_drv_unknown_argument_with_suggestion;
310 Diags.Report(DiagID) << ArgString << Nearest;
311 }
312 ContainsError |= Diags.getDiagnosticLevel(DiagID, SourceLocation()) >
313 DiagnosticsEngine::Warning;
314 }
315
316 for (const Arg *A : Args.filtered(options::OPT_o)) {
317 if (ArgStrings[A->getIndex()] == A->getSpelling())
318 continue;
319
320 // Warn on joined arguments that are similar to a long argument.
321 std::string ArgString = ArgStrings[A->getIndex()];
322 std::string Nearest;
323 if (getOpts().findExact("-" + ArgString, Nearest, VisibilityMask))
324 Diags.Report(diag::warn_drv_potentially_misspelled_joined_argument)
325 << A->getAsString(Args) << Nearest;
326 }
327
328 return Args;
329}
330
331// Determine which compilation mode we are in. We look for options which
332// affect the phase, starting with the earliest phases, and record which
333// option we used to determine the final phase.
334phases::ID Driver::getFinalPhase(const DerivedArgList &DAL,
335 Arg **FinalPhaseArg) const {
336 Arg *PhaseArg = nullptr;
337 phases::ID FinalPhase;
338
339 // -{E,EP,P,M,MM} only run the preprocessor.
340 if (CCCIsCPP() || (PhaseArg = DAL.getLastArg(options::OPT_E)) ||
341 (PhaseArg = DAL.getLastArg(options::OPT__SLASH_EP)) ||
342 (PhaseArg = DAL.getLastArg(options::OPT_M, options::OPT_MM)) ||
343 (PhaseArg = DAL.getLastArg(options::OPT__SLASH_P)) ||
344 CCGenDiagnostics) {
345 FinalPhase = phases::Preprocess;
346
347 // --precompile only runs up to precompilation.
348 // Options that cause the output of C++20 compiled module interfaces or
349 // header units have the same effect.
350 } else if ((PhaseArg = DAL.getLastArg(options::OPT__precompile)) ||
351 (PhaseArg = DAL.getLastArg(options::OPT_extract_api)) ||
352 (PhaseArg = DAL.getLastArg(options::OPT_fmodule_header,
353 options::OPT_fmodule_header_EQ))) {
354 FinalPhase = phases::Precompile;
355 // -{fsyntax-only,-analyze,emit-ast} only run up to the compiler.
356 } else if ((PhaseArg = DAL.getLastArg(options::OPT_fsyntax_only)) ||
357 (PhaseArg = DAL.getLastArg(options::OPT_print_supported_cpus)) ||
358 (PhaseArg = DAL.getLastArg(options::OPT_module_file_info)) ||
359 (PhaseArg = DAL.getLastArg(options::OPT_verify_pch)) ||
360 (PhaseArg = DAL.getLastArg(options::OPT_rewrite_objc)) ||
361 (PhaseArg = DAL.getLastArg(options::OPT_rewrite_legacy_objc)) ||
362 (PhaseArg = DAL.getLastArg(options::OPT__migrate)) ||
363 (PhaseArg = DAL.getLastArg(options::OPT__analyze)) ||
364 (PhaseArg = DAL.getLastArg(options::OPT_emit_ast))) {
365 FinalPhase = phases::Compile;
366
367 // -S only runs up to the backend.
368 } else if ((PhaseArg = DAL.getLastArg(options::OPT_S))) {
369 FinalPhase = phases::Backend;
370
371 // -c compilation only runs up to the assembler.
372 } else if ((PhaseArg = DAL.getLastArg(options::OPT_c))) {
373 FinalPhase = phases::Assemble;
374
375 } else if ((PhaseArg = DAL.getLastArg(options::OPT_emit_interface_stubs))) {
376 FinalPhase = phases::IfsMerge;
377
378 // Otherwise do everything.
379 } else
380 FinalPhase = phases::Link;
381
382 if (FinalPhaseArg)
383 *FinalPhaseArg = PhaseArg;
384
385 return FinalPhase;
386}
387
388static Arg *MakeInputArg(DerivedArgList &Args, const OptTable &Opts,
389 StringRef Value, bool Claim = true) {
390 Arg *A = new Arg(Opts.getOption(options::Opt: OPT_INPUT), Value,
391 Args.getBaseArgs().MakeIndex(String0: Value), Value.data());
392 Args.AddSynthesizedArg(A);
393 if (Claim)
394 A->claim();
395 return A;
396}
397
398DerivedArgList *Driver::TranslateInputArgs(const InputArgList &Args) const {
399 const llvm::opt::OptTable &Opts = getOpts();
400 DerivedArgList *DAL = new DerivedArgList(Args);
401
402 bool HasNostdlib = Args.hasArg(options::OPT_nostdlib);
403 bool HasNostdlibxx = Args.hasArg(options::OPT_nostdlibxx);
404 bool HasNodefaultlib = Args.hasArg(options::OPT_nodefaultlibs);
405 bool IgnoreUnused = false;
406 for (Arg *A : Args) {
407 if (IgnoreUnused)
408 A->claim();
409
410 if (A->getOption().matches(options::OPT_start_no_unused_arguments)) {
411 IgnoreUnused = true;
412 continue;
413 }
414 if (A->getOption().matches(options::OPT_end_no_unused_arguments)) {
415 IgnoreUnused = false;
416 continue;
417 }
418
419 // Unfortunately, we have to parse some forwarding options (-Xassembler,
420 // -Xlinker, -Xpreprocessor) because we either integrate their functionality
421 // (assembler and preprocessor), or bypass a previous driver ('collect2').
422
423 // Rewrite linker options, to replace --no-demangle with a custom internal
424 // option.
425 if ((A->getOption().matches(options::OPT_Wl_COMMA) ||
426 A->getOption().matches(options::OPT_Xlinker)) &&
427 A->containsValue("--no-demangle")) {
428 // Add the rewritten no-demangle argument.
429 DAL->AddFlagArg(A, Opts.getOption(options::OPT_Z_Xlinker__no_demangle));
430
431 // Add the remaining values as Xlinker arguments.
432 for (StringRef Val : A->getValues())
433 if (Val != "--no-demangle")
434 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_Xlinker), Val);
435
436 continue;
437 }
438
439 // Rewrite preprocessor options, to replace -Wp,-MD,FOO which is used by
440 // some build systems. We don't try to be complete here because we don't
441 // care to encourage this usage model.
442 if (A->getOption().matches(options::OPT_Wp_COMMA) &&
443 (A->getValue(0) == StringRef("-MD") ||
444 A->getValue(0) == StringRef("-MMD"))) {
445 // Rewrite to -MD/-MMD along with -MF.
446 if (A->getValue(0) == StringRef("-MD"))
447 DAL->AddFlagArg(A, Opts.getOption(options::OPT_MD));
448 else
449 DAL->AddFlagArg(A, Opts.getOption(options::OPT_MMD));
450 if (A->getNumValues() == 2)
451 DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF), A->getValue(1));
452 continue;
453 }
454
455 // Rewrite reserved library names.
456 if (A->getOption().matches(options::OPT_l)) {
457 StringRef Value = A->getValue();
458
459 // Rewrite unless -nostdlib is present.
460 if (!HasNostdlib && !HasNodefaultlib && !HasNostdlibxx &&
461 Value == "stdc++") {
462 DAL->AddFlagArg(A, Opts.getOption(options::OPT_Z_reserved_lib_stdcxx));
463 continue;
464 }
465
466 // Rewrite unconditionally.
467 if (Value == "cc_kext") {
468 DAL->AddFlagArg(A, Opts.getOption(options::OPT_Z_reserved_lib_cckext));
469 continue;
470 }
471 }
472
473 // Pick up inputs via the -- option.
474 if (A->getOption().matches(options::OPT__DASH_DASH)) {
475 A->claim();
476 for (StringRef Val : A->getValues())
477 DAL->append(A: MakeInputArg(Args&: *DAL, Opts, Value: Val, Claim: false));
478 continue;
479 }
480
481 DAL->append(A);
482 }
483
484 // DXC mode quits before assembly if an output object file isn't specified.
485 if (IsDXCMode() && !Args.hasArg(options::OPT_dxc_Fo))
486 DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_S));
487
488 // Enforce -static if -miamcu is present.
489 if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false))
490 DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_static));
491
492// Add a default value of -mlinker-version=, if one was given and the user
493// didn't specify one.
494#if defined(HOST_LINK_VERSION)
495 if (!Args.hasArg(options::OPT_mlinker_version_EQ) &&
496 strlen(HOST_LINK_VERSION) > 0) {
497 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mlinker_version_EQ),
498 HOST_LINK_VERSION);
499 DAL->getLastArg(options::OPT_mlinker_version_EQ)->claim();
500 }
501#endif
502
503 return DAL;
504}
505
506/// Compute target triple from args.
507///
508/// This routine provides the logic to compute a target triple from various
509/// args passed to the driver and the default triple string.
510static llvm::Triple computeTargetTriple(const Driver &D,
511 StringRef TargetTriple,
512 const ArgList &Args,
513 StringRef DarwinArchName = "") {
514 // FIXME: Already done in Compilation *Driver::BuildCompilation
515 if (const Arg *A = Args.getLastArg(options::OPT_target))
516 TargetTriple = A->getValue();
517
518 llvm::Triple Target(llvm::Triple::normalize(Str: TargetTriple));
519
520 // GNU/Hurd's triples should have been -hurd-gnu*, but were historically made
521 // -gnu* only, and we can not change this, so we have to detect that case as
522 // being the Hurd OS.
523 if (TargetTriple.contains(Other: "-unknown-gnu") || TargetTriple.contains(Other: "-pc-gnu"))
524 Target.setOSName("hurd");
525
526 // Handle Apple-specific options available here.
527 if (Target.isOSBinFormatMachO()) {
528 // If an explicit Darwin arch name is given, that trumps all.
529 if (!DarwinArchName.empty()) {
530 tools::darwin::setTripleTypeForMachOArchName(T&: Target, Str: DarwinArchName,
531 Args);
532 return Target;
533 }
534
535 // Handle the Darwin '-arch' flag.
536 if (Arg *A = Args.getLastArg(options::OPT_arch)) {
537 StringRef ArchName = A->getValue();
538 tools::darwin::setTripleTypeForMachOArchName(T&: Target, Str: ArchName, Args);
539 }
540 }
541
542 // Handle pseudo-target flags '-mlittle-endian'/'-EL' and
543 // '-mbig-endian'/'-EB'.
544 if (Arg *A = Args.getLastArgNoClaim(options::OPT_mlittle_endian,
545 options::OPT_mbig_endian)) {
546 llvm::Triple T = A->getOption().matches(options::OPT_mlittle_endian)
547 ? Target.getLittleEndianArchVariant()
548 : Target.getBigEndianArchVariant();
549 if (T.getArch() != llvm::Triple::UnknownArch) {
550 Target = std::move(T);
551 Args.claimAllArgs(options::OPT_mlittle_endian, options::OPT_mbig_endian);
552 }
553 }
554
555 // Skip further flag support on OSes which don't support '-m32' or '-m64'.
556 if (Target.getArch() == llvm::Triple::tce)
557 return Target;
558
559 // On AIX, the env OBJECT_MODE may affect the resulting arch variant.
560 if (Target.isOSAIX()) {
561 if (std::optional<std::string> ObjectModeValue =
562 llvm::sys::Process::GetEnv(name: "OBJECT_MODE")) {
563 StringRef ObjectMode = *ObjectModeValue;
564 llvm::Triple::ArchType AT = llvm::Triple::UnknownArch;
565
566 if (ObjectMode.equals(RHS: "64")) {
567 AT = Target.get64BitArchVariant().getArch();
568 } else if (ObjectMode.equals(RHS: "32")) {
569 AT = Target.get32BitArchVariant().getArch();
570 } else {
571 D.Diag(diag::err_drv_invalid_object_mode) << ObjectMode;
572 }
573
574 if (AT != llvm::Triple::UnknownArch && AT != Target.getArch())
575 Target.setArch(Kind: AT);
576 }
577 }
578
579 // The `-maix[32|64]` flags are only valid for AIX targets.
580 if (Arg *A = Args.getLastArgNoClaim(options::OPT_maix32, options::OPT_maix64);
581 A && !Target.isOSAIX())
582 D.Diag(diag::err_drv_unsupported_opt_for_target)
583 << A->getAsString(Args) << Target.str();
584
585 // Handle pseudo-target flags '-m64', '-mx32', '-m32' and '-m16'.
586 Arg *A = Args.getLastArg(options::OPT_m64, options::OPT_mx32,
587 options::OPT_m32, options::OPT_m16,
588 options::OPT_maix32, options::OPT_maix64);
589 if (A) {
590 llvm::Triple::ArchType AT = llvm::Triple::UnknownArch;
591
592 if (A->getOption().matches(options::OPT_m64) ||
593 A->getOption().matches(options::OPT_maix64)) {
594 AT = Target.get64BitArchVariant().getArch();
595 if (Target.getEnvironment() == llvm::Triple::GNUX32)
596 Target.setEnvironment(llvm::Triple::GNU);
597 else if (Target.getEnvironment() == llvm::Triple::MuslX32)
598 Target.setEnvironment(llvm::Triple::Musl);
599 } else if (A->getOption().matches(options::OPT_mx32) &&
600 Target.get64BitArchVariant().getArch() == llvm::Triple::x86_64) {
601 AT = llvm::Triple::x86_64;
602 if (Target.getEnvironment() == llvm::Triple::Musl)
603 Target.setEnvironment(llvm::Triple::MuslX32);
604 else
605 Target.setEnvironment(llvm::Triple::GNUX32);
606 } else if (A->getOption().matches(options::OPT_m32) ||
607 A->getOption().matches(options::OPT_maix32)) {
608 AT = Target.get32BitArchVariant().getArch();
609 if (Target.getEnvironment() == llvm::Triple::GNUX32)
610 Target.setEnvironment(llvm::Triple::GNU);
611 else if (Target.getEnvironment() == llvm::Triple::MuslX32)
612 Target.setEnvironment(llvm::Triple::Musl);
613 } else if (A->getOption().matches(options::OPT_m16) &&
614 Target.get32BitArchVariant().getArch() == llvm::Triple::x86) {
615 AT = llvm::Triple::x86;
616 Target.setEnvironment(llvm::Triple::CODE16);
617 }
618
619 if (AT != llvm::Triple::UnknownArch && AT != Target.getArch()) {
620 Target.setArch(Kind: AT);
621 if (Target.isWindowsGNUEnvironment())
622 toolchains::MinGW::fixTripleArch(D, Triple&: Target, Args);
623 }
624 }
625
626 // Handle -miamcu flag.
627 if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) {
628 if (Target.get32BitArchVariant().getArch() != llvm::Triple::x86)
629 D.Diag(diag::err_drv_unsupported_opt_for_target) << "-miamcu"
630 << Target.str();
631
632 if (A && !A->getOption().matches(options::OPT_m32))
633 D.Diag(diag::err_drv_argument_not_allowed_with)
634 << "-miamcu" << A->getBaseArg().getAsString(Args);
635
636 Target.setArch(Kind: llvm::Triple::x86);
637 Target.setArchName("i586");
638 Target.setEnvironment(llvm::Triple::UnknownEnvironment);
639 Target.setEnvironmentName("");
640 Target.setOS(llvm::Triple::ELFIAMCU);
641 Target.setVendor(llvm::Triple::UnknownVendor);
642 Target.setVendorName("intel");
643 }
644
645 // If target is MIPS adjust the target triple
646 // accordingly to provided ABI name.
647 if (Target.isMIPS()) {
648 if ((A = Args.getLastArg(options::OPT_mabi_EQ))) {
649 StringRef ABIName = A->getValue();
650 if (ABIName == "32") {
651 Target = Target.get32BitArchVariant();
652 if (Target.getEnvironment() == llvm::Triple::GNUABI64 ||
653 Target.getEnvironment() == llvm::Triple::GNUABIN32)
654 Target.setEnvironment(llvm::Triple::GNU);
655 } else if (ABIName == "n32") {
656 Target = Target.get64BitArchVariant();
657 if (Target.getEnvironment() == llvm::Triple::GNU ||
658 Target.getEnvironment() == llvm::Triple::GNUABI64)
659 Target.setEnvironment(llvm::Triple::GNUABIN32);
660 } else if (ABIName == "64") {
661 Target = Target.get64BitArchVariant();
662 if (Target.getEnvironment() == llvm::Triple::GNU ||
663 Target.getEnvironment() == llvm::Triple::GNUABIN32)
664 Target.setEnvironment(llvm::Triple::GNUABI64);
665 }
666 }
667 }
668
669 // If target is RISC-V adjust the target triple according to
670 // provided architecture name
671 if (Target.isRISCV()) {
672 if (Args.hasArg(options::OPT_march_EQ) ||
673 Args.hasArg(options::OPT_mcpu_EQ)) {
674 StringRef ArchName = tools::riscv::getRISCVArch(Args, Triple: Target);
675 auto ISAInfo = llvm::RISCVISAInfo::parseArchString(
676 Arch: ArchName, /*EnableExperimentalExtensions=*/EnableExperimentalExtension: true);
677 if (!llvm::errorToBool(Err: ISAInfo.takeError())) {
678 unsigned XLen = (*ISAInfo)->getXLen();
679 if (XLen == 32)
680 Target.setArch(Kind: llvm::Triple::riscv32);
681 else if (XLen == 64)
682 Target.setArch(Kind: llvm::Triple::riscv64);
683 }
684 }
685 }
686
687 return Target;
688}
689
690// Parse the LTO options and record the type of LTO compilation
691// based on which -f(no-)?lto(=.*)? or -f(no-)?offload-lto(=.*)?
692// option occurs last.
693static driver::LTOKind parseLTOMode(Driver &D, const llvm::opt::ArgList &Args,
694 OptSpecifier OptEq, OptSpecifier OptNeg) {
695 if (!Args.hasFlag(Pos: OptEq, Neg: OptNeg, Default: false))
696 return LTOK_None;
697
698 const Arg *A = Args.getLastArg(Ids: OptEq);
699 StringRef LTOName = A->getValue();
700
701 driver::LTOKind LTOMode = llvm::StringSwitch<LTOKind>(LTOName)
702 .Case(S: "full", Value: LTOK_Full)
703 .Case(S: "thin", Value: LTOK_Thin)
704 .Default(Value: LTOK_Unknown);
705
706 if (LTOMode == LTOK_Unknown) {
707 D.Diag(diag::err_drv_unsupported_option_argument)
708 << A->getSpelling() << A->getValue();
709 return LTOK_None;
710 }
711 return LTOMode;
712}
713
714// Parse the LTO options.
715void Driver::setLTOMode(const llvm::opt::ArgList &Args) {
716 LTOMode =
717 parseLTOMode(*this, Args, options::OPT_flto_EQ, options::OPT_fno_lto);
718
719 OffloadLTOMode = parseLTOMode(*this, Args, options::OPT_foffload_lto_EQ,
720 options::OPT_fno_offload_lto);
721
722 // Try to enable `-foffload-lto=full` if `-fopenmp-target-jit` is on.
723 if (Args.hasFlag(options::OPT_fopenmp_target_jit,
724 options::OPT_fno_openmp_target_jit, false)) {
725 if (Arg *A = Args.getLastArg(options::OPT_foffload_lto_EQ,
726 options::OPT_fno_offload_lto))
727 if (OffloadLTOMode != LTOK_Full)
728 Diag(diag::err_drv_incompatible_options)
729 << A->getSpelling() << "-fopenmp-target-jit";
730 OffloadLTOMode = LTOK_Full;
731 }
732}
733
734/// Compute the desired OpenMP runtime from the flags provided.
735Driver::OpenMPRuntimeKind Driver::getOpenMPRuntime(const ArgList &Args) const {
736 StringRef RuntimeName(CLANG_DEFAULT_OPENMP_RUNTIME);
737
738 const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ);
739 if (A)
740 RuntimeName = A->getValue();
741
742 auto RT = llvm::StringSwitch<OpenMPRuntimeKind>(RuntimeName)
743 .Case(S: "libomp", Value: OMPRT_OMP)
744 .Case(S: "libgomp", Value: OMPRT_GOMP)
745 .Case(S: "libiomp5", Value: OMPRT_IOMP5)
746 .Default(Value: OMPRT_Unknown);
747
748 if (RT == OMPRT_Unknown) {
749 if (A)
750 Diag(diag::err_drv_unsupported_option_argument)
751 << A->getSpelling() << A->getValue();
752 else
753 // FIXME: We could use a nicer diagnostic here.
754 Diag(diag::err_drv_unsupported_opt) << "-fopenmp";
755 }
756
757 return RT;
758}
759
760void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
761 InputList &Inputs) {
762
763 //
764 // CUDA/HIP
765 //
766 // We need to generate a CUDA/HIP toolchain if any of the inputs has a CUDA
767 // or HIP type. However, mixed CUDA/HIP compilation is not supported.
768 bool IsCuda =
769 llvm::any_of(Range&: Inputs, P: [](std::pair<types::ID, const llvm::opt::Arg *> &I) {
770 return types::isCuda(Id: I.first);
771 });
772 bool IsHIP =
773 llvm::any_of(Inputs,
774 [](std::pair<types::ID, const llvm::opt::Arg *> &I) {
775 return types::isHIP(I.first);
776 }) ||
777 C.getInputArgs().hasArg(options::OPT_hip_link) ||
778 C.getInputArgs().hasArg(options::OPT_hipstdpar);
779 if (IsCuda && IsHIP) {
780 Diag(clang::diag::err_drv_mix_cuda_hip);
781 return;
782 }
783 if (IsCuda) {
784 const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>();
785 const llvm::Triple &HostTriple = HostTC->getTriple();
786 auto OFK = Action::OFK_Cuda;
787 auto CudaTriple =
788 getNVIDIAOffloadTargetTriple(D: *this, Args: C.getInputArgs(), HostTriple);
789 if (!CudaTriple)
790 return;
791 // Use the CUDA and host triples as the key into the ToolChains map,
792 // because the device toolchain we create depends on both.
793 auto &CudaTC = ToolChains[CudaTriple->str() + "/" + HostTriple.str()];
794 if (!CudaTC) {
795 CudaTC = std::make_unique<toolchains::CudaToolChain>(
796 args&: *this, args&: *CudaTriple, args: *HostTC, args: C.getInputArgs());
797
798 // Emit a warning if the detected CUDA version is too new.
799 CudaInstallationDetector &CudaInstallation =
800 static_cast<toolchains::CudaToolChain &>(*CudaTC).CudaInstallation;
801 if (CudaInstallation.isValid())
802 CudaInstallation.WarnIfUnsupportedVersion();
803 }
804 C.addOffloadDeviceToolChain(DeviceToolChain: CudaTC.get(), OffloadKind: OFK);
805 } else if (IsHIP) {
806 if (auto *OMPTargetArg =
807 C.getInputArgs().getLastArg(options::OPT_fopenmp_targets_EQ)) {
808 Diag(clang::diag::err_drv_unsupported_opt_for_language_mode)
809 << OMPTargetArg->getSpelling() << "HIP";
810 return;
811 }
812 const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>();
813 auto OFK = Action::OFK_HIP;
814 auto HIPTriple = getHIPOffloadTargetTriple(D: *this, Args: C.getInputArgs());
815 if (!HIPTriple)
816 return;
817 auto *HIPTC = &getOffloadingDeviceToolChain(Args: C.getInputArgs(), Target: *HIPTriple,
818 HostTC: *HostTC, TargetDeviceOffloadKind: OFK);
819 assert(HIPTC && "Could not create offloading device tool chain.");
820 C.addOffloadDeviceToolChain(DeviceToolChain: HIPTC, OffloadKind: OFK);
821 }
822
823 //
824 // OpenMP
825 //
826 // We need to generate an OpenMP toolchain if the user specified targets with
827 // the -fopenmp-targets option or used --offload-arch with OpenMP enabled.
828 bool IsOpenMPOffloading =
829 C.getInputArgs().hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
830 options::OPT_fno_openmp, false) &&
831 (C.getInputArgs().hasArg(options::OPT_fopenmp_targets_EQ) ||
832 C.getInputArgs().hasArg(options::OPT_offload_arch_EQ));
833 if (IsOpenMPOffloading) {
834 // We expect that -fopenmp-targets is always used in conjunction with the
835 // option -fopenmp specifying a valid runtime with offloading support, i.e.
836 // libomp or libiomp.
837 OpenMPRuntimeKind RuntimeKind = getOpenMPRuntime(Args: C.getInputArgs());
838 if (RuntimeKind != OMPRT_OMP && RuntimeKind != OMPRT_IOMP5) {
839 Diag(clang::diag::err_drv_expecting_fopenmp_with_fopenmp_targets);
840 return;
841 }
842
843 llvm::StringMap<llvm::DenseSet<StringRef>> DerivedArchs;
844 llvm::StringMap<StringRef> FoundNormalizedTriples;
845 std::multiset<StringRef> OpenMPTriples;
846
847 // If the user specified -fopenmp-targets= we create a toolchain for each
848 // valid triple. Otherwise, if only --offload-arch= was specified we instead
849 // attempt to derive the appropriate toolchains from the arguments.
850 if (Arg *OpenMPTargets =
851 C.getInputArgs().getLastArg(options::OPT_fopenmp_targets_EQ)) {
852 if (OpenMPTargets && !OpenMPTargets->getNumValues()) {
853 Diag(clang::diag::warn_drv_empty_joined_argument)
854 << OpenMPTargets->getAsString(C.getInputArgs());
855 return;
856 }
857 for (StringRef T : OpenMPTargets->getValues())
858 OpenMPTriples.insert(x: T);
859 } else if (C.getInputArgs().hasArg(options::OPT_offload_arch_EQ) &&
860 !IsHIP && !IsCuda) {
861 const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>();
862 auto AMDTriple = getHIPOffloadTargetTriple(D: *this, Args: C.getInputArgs());
863 auto NVPTXTriple = getNVIDIAOffloadTargetTriple(D: *this, Args: C.getInputArgs(),
864 HostTriple: HostTC->getTriple());
865
866 // Attempt to deduce the offloading triple from the set of architectures.
867 // We can only correctly deduce NVPTX / AMDGPU triples currently. We need
868 // to temporarily create these toolchains so that we can access tools for
869 // inferring architectures.
870 llvm::DenseSet<StringRef> Archs;
871 if (NVPTXTriple) {
872 auto TempTC = std::make_unique<toolchains::CudaToolChain>(
873 args&: *this, args&: *NVPTXTriple, args: *HostTC, args: C.getInputArgs());
874 for (StringRef Arch : getOffloadArchs(
875 C, Args: C.getArgs(), Kind: Action::OFK_OpenMP, TC: &*TempTC, SuppressError: true))
876 Archs.insert(V: Arch);
877 }
878 if (AMDTriple) {
879 auto TempTC = std::make_unique<toolchains::AMDGPUOpenMPToolChain>(
880 args&: *this, args&: *AMDTriple, args: *HostTC, args: C.getInputArgs());
881 for (StringRef Arch : getOffloadArchs(
882 C, Args: C.getArgs(), Kind: Action::OFK_OpenMP, TC: &*TempTC, SuppressError: true))
883 Archs.insert(V: Arch);
884 }
885 if (!AMDTriple && !NVPTXTriple) {
886 for (StringRef Arch :
887 getOffloadArchs(C, Args: C.getArgs(), Kind: Action::OFK_OpenMP, TC: nullptr, SuppressError: true))
888 Archs.insert(V: Arch);
889 }
890
891 for (StringRef Arch : Archs) {
892 if (NVPTXTriple && IsNVIDIAGpuArch(A: StringToCudaArch(
893 S: getProcessorFromTargetID(T: *NVPTXTriple, OffloadArch: Arch)))) {
894 DerivedArchs[NVPTXTriple->getTriple()].insert(V: Arch);
895 } else if (AMDTriple &&
896 IsAMDGpuArch(A: StringToCudaArch(
897 S: getProcessorFromTargetID(T: *AMDTriple, OffloadArch: Arch)))) {
898 DerivedArchs[AMDTriple->getTriple()].insert(V: Arch);
899 } else {
900 Diag(clang::diag::err_drv_failed_to_deduce_target_from_arch) << Arch;
901 return;
902 }
903 }
904
905 // If the set is empty then we failed to find a native architecture.
906 if (Archs.empty()) {
907 Diag(clang::diag::err_drv_failed_to_deduce_target_from_arch)
908 << "native";
909 return;
910 }
911
912 for (const auto &TripleAndArchs : DerivedArchs)
913 OpenMPTriples.insert(x: TripleAndArchs.first());
914 }
915
916 for (StringRef Val : OpenMPTriples) {
917 llvm::Triple TT(ToolChain::getOpenMPTriple(TripleStr: Val));
918 std::string NormalizedName = TT.normalize();
919
920 // Make sure we don't have a duplicate triple.
921 auto Duplicate = FoundNormalizedTriples.find(Key: NormalizedName);
922 if (Duplicate != FoundNormalizedTriples.end()) {
923 Diag(clang::diag::warn_drv_omp_offload_target_duplicate)
924 << Val << Duplicate->second;
925 continue;
926 }
927
928 // Store the current triple so that we can check for duplicates in the
929 // following iterations.
930 FoundNormalizedTriples[NormalizedName] = Val;
931
932 // If the specified target is invalid, emit a diagnostic.
933 if (TT.getArch() == llvm::Triple::UnknownArch)
934 Diag(clang::diag::err_drv_invalid_omp_target) << Val;
935 else {
936 const ToolChain *TC;
937 // Device toolchains have to be selected differently. They pair host
938 // and device in their implementation.
939 if (TT.isNVPTX() || TT.isAMDGCN()) {
940 const ToolChain *HostTC =
941 C.getSingleOffloadToolChain<Action::OFK_Host>();
942 assert(HostTC && "Host toolchain should be always defined.");
943 auto &DeviceTC =
944 ToolChains[TT.str() + "/" + HostTC->getTriple().normalize()];
945 if (!DeviceTC) {
946 if (TT.isNVPTX())
947 DeviceTC = std::make_unique<toolchains::CudaToolChain>(
948 args&: *this, args&: TT, args: *HostTC, args: C.getInputArgs());
949 else if (TT.isAMDGCN())
950 DeviceTC = std::make_unique<toolchains::AMDGPUOpenMPToolChain>(
951 args&: *this, args&: TT, args: *HostTC, args: C.getInputArgs());
952 else
953 assert(DeviceTC && "Device toolchain not defined.");
954 }
955
956 TC = DeviceTC.get();
957 } else
958 TC = &getToolChain(Args: C.getInputArgs(), Target: TT);
959 C.addOffloadDeviceToolChain(DeviceToolChain: TC, OffloadKind: Action::OFK_OpenMP);
960 if (DerivedArchs.contains(Key: TT.getTriple()))
961 KnownArchs[TC] = DerivedArchs[TT.getTriple()];
962 }
963 }
964 } else if (C.getInputArgs().hasArg(options::OPT_fopenmp_targets_EQ)) {
965 Diag(clang::diag::err_drv_expecting_fopenmp_with_fopenmp_targets);
966 return;
967 }
968
969 //
970 // TODO: Add support for other offloading programming models here.
971 //
972}
973
974static void appendOneArg(InputArgList &Args, const Arg *Opt,
975 const Arg *BaseArg) {
976 // The args for config files or /clang: flags belong to different InputArgList
977 // objects than Args. This copies an Arg from one of those other InputArgLists
978 // to the ownership of Args.
979 unsigned Index = Args.MakeIndex(String0: Opt->getSpelling());
980 Arg *Copy = new llvm::opt::Arg(Opt->getOption(), Args.getArgString(Index),
981 Index, BaseArg);
982 Copy->getValues() = Opt->getValues();
983 if (Opt->isClaimed())
984 Copy->claim();
985 Copy->setOwnsValues(Opt->getOwnsValues());
986 Opt->setOwnsValues(false);
987 Args.append(A: Copy);
988}
989
990bool Driver::readConfigFile(StringRef FileName,
991 llvm::cl::ExpansionContext &ExpCtx) {
992 // Try opening the given file.
993 auto Status = getVFS().status(Path: FileName);
994 if (!Status) {
995 Diag(diag::err_drv_cannot_open_config_file)
996 << FileName << Status.getError().message();
997 return true;
998 }
999 if (Status->getType() != llvm::sys::fs::file_type::regular_file) {
1000 Diag(diag::err_drv_cannot_open_config_file)
1001 << FileName << "not a regular file";
1002 return true;
1003 }
1004
1005 // Try reading the given file.
1006 SmallVector<const char *, 32> NewCfgArgs;
1007 if (llvm::Error Err = ExpCtx.readConfigFile(CfgFile: FileName, Argv&: NewCfgArgs)) {
1008 Diag(diag::err_drv_cannot_read_config_file)
1009 << FileName << toString(std::move(Err));
1010 return true;
1011 }
1012
1013 // Read options from config file.
1014 llvm::SmallString<128> CfgFileName(FileName);
1015 llvm::sys::path::native(path&: CfgFileName);
1016 bool ContainErrors;
1017 std::unique_ptr<InputArgList> NewOptions = std::make_unique<InputArgList>(
1018 args: ParseArgStrings(ArgStrings: NewCfgArgs, /*UseDriverMode=*/true, ContainsError&: ContainErrors));
1019 if (ContainErrors)
1020 return true;
1021
1022 // Claim all arguments that come from a configuration file so that the driver
1023 // does not warn on any that is unused.
1024 for (Arg *A : *NewOptions)
1025 A->claim();
1026
1027 if (!CfgOptions)
1028 CfgOptions = std::move(NewOptions);
1029 else {
1030 // If this is a subsequent config file, append options to the previous one.
1031 for (auto *Opt : *NewOptions) {
1032 const Arg *BaseArg = &Opt->getBaseArg();
1033 if (BaseArg == Opt)
1034 BaseArg = nullptr;
1035 appendOneArg(Args&: *CfgOptions, Opt, BaseArg);
1036 }
1037 }
1038 ConfigFiles.push_back(x: std::string(CfgFileName));
1039 return false;
1040}
1041
1042bool Driver::loadConfigFiles() {
1043 llvm::cl::ExpansionContext ExpCtx(Saver.getAllocator(),
1044 llvm::cl::tokenizeConfigFile);
1045 ExpCtx.setVFS(&getVFS());
1046
1047 // Process options that change search path for config files.
1048 if (CLOptions) {
1049 if (CLOptions->hasArg(options::OPT_config_system_dir_EQ)) {
1050 SmallString<128> CfgDir;
1051 CfgDir.append(
1052 CLOptions->getLastArgValue(options::OPT_config_system_dir_EQ));
1053 if (CfgDir.empty() || getVFS().makeAbsolute(Path&: CfgDir))
1054 SystemConfigDir.clear();
1055 else
1056 SystemConfigDir = static_cast<std::string>(CfgDir);
1057 }
1058 if (CLOptions->hasArg(options::OPT_config_user_dir_EQ)) {
1059 SmallString<128> CfgDir;
1060 llvm::sys::fs::expand_tilde(
1061 CLOptions->getLastArgValue(options::OPT_config_user_dir_EQ), CfgDir);
1062 if (CfgDir.empty() || getVFS().makeAbsolute(Path&: CfgDir))
1063 UserConfigDir.clear();
1064 else
1065 UserConfigDir = static_cast<std::string>(CfgDir);
1066 }
1067 }
1068
1069 // Prepare list of directories where config file is searched for.
1070 StringRef CfgFileSearchDirs[] = {UserConfigDir, SystemConfigDir, Dir};
1071 ExpCtx.setSearchDirs(CfgFileSearchDirs);
1072
1073 // First try to load configuration from the default files, return on error.
1074 if (loadDefaultConfigFiles(ExpCtx))
1075 return true;
1076
1077 // Then load configuration files specified explicitly.
1078 SmallString<128> CfgFilePath;
1079 if (CLOptions) {
1080 for (auto CfgFileName : CLOptions->getAllArgValues(options::OPT_config)) {
1081 // If argument contains directory separator, treat it as a path to
1082 // configuration file.
1083 if (llvm::sys::path::has_parent_path(CfgFileName)) {
1084 CfgFilePath.assign(CfgFileName);
1085 if (llvm::sys::path::is_relative(CfgFilePath)) {
1086 if (getVFS().makeAbsolute(CfgFilePath)) {
1087 Diag(diag::err_drv_cannot_open_config_file)
1088 << CfgFilePath << "cannot get absolute path";
1089 return true;
1090 }
1091 }
1092 } else if (!ExpCtx.findConfigFile(CfgFileName, CfgFilePath)) {
1093 // Report an error that the config file could not be found.
1094 Diag(diag::err_drv_config_file_not_found) << CfgFileName;
1095 for (const StringRef &SearchDir : CfgFileSearchDirs)
1096 if (!SearchDir.empty())
1097 Diag(diag::note_drv_config_file_searched_in) << SearchDir;
1098 return true;
1099 }
1100
1101 // Try to read the config file, return on error.
1102 if (readConfigFile(CfgFilePath, ExpCtx))
1103 return true;
1104 }
1105 }
1106
1107 // No error occurred.
1108 return false;
1109}
1110
1111bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
1112 // Disable default config if CLANG_NO_DEFAULT_CONFIG is set to a non-empty
1113 // value.
1114 if (const char *NoConfigEnv = ::getenv(name: "CLANG_NO_DEFAULT_CONFIG")) {
1115 if (*NoConfigEnv)
1116 return false;
1117 }
1118 if (CLOptions && CLOptions->hasArg(options::OPT_no_default_config))
1119 return false;
1120
1121 std::string RealMode = getExecutableForDriverMode(Mode);
1122 std::string Triple;
1123
1124 // If name prefix is present, no --target= override was passed via CLOptions
1125 // and the name prefix is not a valid triple, force it for backwards
1126 // compatibility.
1127 if (!ClangNameParts.TargetPrefix.empty() &&
1128 computeTargetTriple(D: *this, TargetTriple: "/invalid/", Args: *CLOptions).str() ==
1129 "/invalid/") {
1130 llvm::Triple PrefixTriple{ClangNameParts.TargetPrefix};
1131 if (PrefixTriple.getArch() == llvm::Triple::UnknownArch ||
1132 PrefixTriple.isOSUnknown())
1133 Triple = PrefixTriple.str();
1134 }
1135
1136 // Otherwise, use the real triple as used by the driver.
1137 if (Triple.empty()) {
1138 llvm::Triple RealTriple =
1139 computeTargetTriple(D: *this, TargetTriple, Args: *CLOptions);
1140 Triple = RealTriple.str();
1141 assert(!Triple.empty());
1142 }
1143
1144 // Search for config files in the following order:
1145 // 1. <triple>-<mode>.cfg using real driver mode
1146 // (e.g. i386-pc-linux-gnu-clang++.cfg).
1147 // 2. <triple>-<mode>.cfg using executable suffix
1148 // (e.g. i386-pc-linux-gnu-clang-g++.cfg for *clang-g++).
1149 // 3. <triple>.cfg + <mode>.cfg using real driver mode
1150 // (e.g. i386-pc-linux-gnu.cfg + clang++.cfg).
1151 // 4. <triple>.cfg + <mode>.cfg using executable suffix
1152 // (e.g. i386-pc-linux-gnu.cfg + clang-g++.cfg for *clang-g++).
1153
1154 // Try loading <triple>-<mode>.cfg, and return if we find a match.
1155 SmallString<128> CfgFilePath;
1156 std::string CfgFileName = Triple + '-' + RealMode + ".cfg";
1157 if (ExpCtx.findConfigFile(FileName: CfgFileName, FilePath&: CfgFilePath))
1158 return readConfigFile(FileName: CfgFilePath, ExpCtx);
1159
1160 bool TryModeSuffix = !ClangNameParts.ModeSuffix.empty() &&
1161 ClangNameParts.ModeSuffix != RealMode;
1162 if (TryModeSuffix) {
1163 CfgFileName = Triple + '-' + ClangNameParts.ModeSuffix + ".cfg";
1164 if (ExpCtx.findConfigFile(FileName: CfgFileName, FilePath&: CfgFilePath))
1165 return readConfigFile(FileName: CfgFilePath, ExpCtx);
1166 }
1167
1168 // Try loading <mode>.cfg, and return if loading failed. If a matching file
1169 // was not found, still proceed on to try <triple>.cfg.
1170 CfgFileName = RealMode + ".cfg";
1171 if (ExpCtx.findConfigFile(FileName: CfgFileName, FilePath&: CfgFilePath)) {
1172 if (readConfigFile(FileName: CfgFilePath, ExpCtx))
1173 return true;
1174 } else if (TryModeSuffix) {
1175 CfgFileName = ClangNameParts.ModeSuffix + ".cfg";
1176 if (ExpCtx.findConfigFile(FileName: CfgFileName, FilePath&: CfgFilePath) &&
1177 readConfigFile(FileName: CfgFilePath, ExpCtx))
1178 return true;
1179 }
1180
1181 // Try loading <triple>.cfg and return if we find a match.
1182 CfgFileName = Triple + ".cfg";
1183 if (ExpCtx.findConfigFile(FileName: CfgFileName, FilePath&: CfgFilePath))
1184 return readConfigFile(FileName: CfgFilePath, ExpCtx);
1185
1186 // If we were unable to find a config file deduced from executable name,
1187 // that is not an error.
1188 return false;
1189}
1190
1191Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
1192 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
1193
1194 // FIXME: Handle environment options which affect driver behavior, somewhere
1195 // (client?). GCC_EXEC_PREFIX, LPATH, CC_PRINT_OPTIONS.
1196
1197 // We look for the driver mode option early, because the mode can affect
1198 // how other options are parsed.
1199
1200 auto DriverMode = getDriverMode(ProgName: ClangExecutable, Args: ArgList.slice(N: 1));
1201 if (!DriverMode.empty())
1202 setDriverMode(DriverMode);
1203
1204 // FIXME: What are we going to do with -V and -b?
1205
1206 // Arguments specified in command line.
1207 bool ContainsError;
1208 CLOptions = std::make_unique<InputArgList>(
1209 args: ParseArgStrings(ArgStrings: ArgList.slice(N: 1), /*UseDriverMode=*/true, ContainsError));
1210
1211 // Try parsing configuration file.
1212 if (!ContainsError)
1213 ContainsError = loadConfigFiles();
1214 bool HasConfigFile = !ContainsError && (CfgOptions.get() != nullptr);
1215
1216 // All arguments, from both config file and command line.
1217 InputArgList Args = std::move(HasConfigFile ? std::move(*CfgOptions)
1218 : std::move(*CLOptions));
1219
1220 if (HasConfigFile)
1221 for (auto *Opt : *CLOptions) {
1222 if (Opt->getOption().matches(options::OPT_config))
1223 continue;
1224 const Arg *BaseArg = &Opt->getBaseArg();
1225 if (BaseArg == Opt)
1226 BaseArg = nullptr;
1227 appendOneArg(Args, Opt, BaseArg);
1228 }
1229
1230 // In CL mode, look for any pass-through arguments
1231 if (IsCLMode() && !ContainsError) {
1232 SmallVector<const char *, 16> CLModePassThroughArgList;
1233 for (const auto *A : Args.filtered(options::OPT__SLASH_clang)) {
1234 A->claim();
1235 CLModePassThroughArgList.push_back(A->getValue());
1236 }
1237
1238 if (!CLModePassThroughArgList.empty()) {
1239 // Parse any pass through args using default clang processing rather
1240 // than clang-cl processing.
1241 auto CLModePassThroughOptions = std::make_unique<InputArgList>(
1242 args: ParseArgStrings(ArgStrings: CLModePassThroughArgList, /*UseDriverMode=*/false,
1243 ContainsError));
1244
1245 if (!ContainsError)
1246 for (auto *Opt : *CLModePassThroughOptions) {
1247 appendOneArg(Args, Opt, BaseArg: nullptr);
1248 }
1249 }
1250 }
1251
1252 // Check for working directory option before accessing any files
1253 if (Arg *WD = Args.getLastArg(options::OPT_working_directory))
1254 if (VFS->setCurrentWorkingDirectory(WD->getValue()))
1255 Diag(diag::err_drv_unable_to_set_working_directory) << WD->getValue();
1256
1257 // FIXME: This stuff needs to go into the Compilation, not the driver.
1258 bool CCCPrintPhases;
1259
1260 // -canonical-prefixes, -no-canonical-prefixes are used very early in main.
1261 Args.ClaimAllArgs(options::OPT_canonical_prefixes);
1262 Args.ClaimAllArgs(options::OPT_no_canonical_prefixes);
1263
1264 // f(no-)integated-cc1 is also used very early in main.
1265 Args.ClaimAllArgs(options::OPT_fintegrated_cc1);
1266 Args.ClaimAllArgs(options::OPT_fno_integrated_cc1);
1267
1268 // Ignore -pipe.
1269 Args.ClaimAllArgs(options::OPT_pipe);
1270
1271 // Extract -ccc args.
1272 //
1273 // FIXME: We need to figure out where this behavior should live. Most of it
1274 // should be outside in the client; the parts that aren't should have proper
1275 // options, either by introducing new ones or by overloading gcc ones like -V
1276 // or -b.
1277 CCCPrintPhases = Args.hasArg(options::OPT_ccc_print_phases);
1278 CCCPrintBindings = Args.hasArg(options::OPT_ccc_print_bindings);
1279 if (const Arg *A = Args.getLastArg(options::OPT_ccc_gcc_name))
1280 CCCGenericGCCName = A->getValue();
1281
1282 // Process -fproc-stat-report options.
1283 if (const Arg *A = Args.getLastArg(options::OPT_fproc_stat_report_EQ)) {
1284 CCPrintProcessStats = true;
1285 CCPrintStatReportFilename = A->getValue();
1286 }
1287 if (Args.hasArg(options::OPT_fproc_stat_report))
1288 CCPrintProcessStats = true;
1289
1290 // FIXME: TargetTriple is used by the target-prefixed calls to as/ld
1291 // and getToolChain is const.
1292 if (IsCLMode()) {
1293 // clang-cl targets MSVC-style Win32.
1294 llvm::Triple T(TargetTriple);
1295 T.setOS(llvm::Triple::Win32);
1296 T.setVendor(llvm::Triple::PC);
1297 T.setEnvironment(llvm::Triple::MSVC);
1298 T.setObjectFormat(llvm::Triple::COFF);
1299 if (Args.hasArg(options::OPT__SLASH_arm64EC))
1300 T.setArch(Kind: llvm::Triple::aarch64, SubArch: llvm::Triple::AArch64SubArch_arm64ec);
1301 TargetTriple = T.str();
1302 } else if (IsDXCMode()) {
1303 // Build TargetTriple from target_profile option for clang-dxc.
1304 if (const Arg *A = Args.getLastArg(options::OPT_target_profile)) {
1305 StringRef TargetProfile = A->getValue();
1306 if (auto Triple =
1307 toolchains::HLSLToolChain::parseTargetProfile(TargetProfile))
1308 TargetTriple = *Triple;
1309 else
1310 Diag(diag::err_drv_invalid_directx_shader_module) << TargetProfile;
1311
1312 A->claim();
1313
1314 if (Args.hasArg(options::OPT_spirv)) {
1315 llvm::Triple T(TargetTriple);
1316 T.setArch(Kind: llvm::Triple::spirv);
1317 T.setOS(llvm::Triple::Vulkan);
1318
1319 // Set specific Vulkan version if applicable.
1320 if (const Arg *A = Args.getLastArg(options::OPT_fspv_target_env_EQ)) {
1321 const llvm::StringSet<> ValidValues = {"vulkan1.2", "vulkan1.3"};
1322 if (ValidValues.contains(key: A->getValue())) {
1323 T.setOSName(A->getValue());
1324 } else {
1325 Diag(diag::err_drv_invalid_value)
1326 << A->getAsString(Args) << A->getValue();
1327 }
1328 A->claim();
1329 }
1330
1331 TargetTriple = T.str();
1332 }
1333 } else {
1334 Diag(diag::err_drv_dxc_missing_target_profile);
1335 }
1336 }
1337
1338 if (const Arg *A = Args.getLastArg(options::OPT_target))
1339 TargetTriple = A->getValue();
1340 if (const Arg *A = Args.getLastArg(options::OPT_ccc_install_dir))
1341 Dir = Dir = A->getValue();
1342 for (const Arg *A : Args.filtered(options::OPT_B)) {
1343 A->claim();
1344 PrefixDirs.push_back(A->getValue(0));
1345 }
1346 if (std::optional<std::string> CompilerPathValue =
1347 llvm::sys::Process::GetEnv(name: "COMPILER_PATH")) {
1348 StringRef CompilerPath = *CompilerPathValue;
1349 while (!CompilerPath.empty()) {
1350 std::pair<StringRef, StringRef> Split =
1351 CompilerPath.split(Separator: llvm::sys::EnvPathSeparator);
1352 PrefixDirs.push_back(Elt: std::string(Split.first));
1353 CompilerPath = Split.second;
1354 }
1355 }
1356 if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ))
1357 SysRoot = A->getValue();
1358 if (const Arg *A = Args.getLastArg(options::OPT__dyld_prefix_EQ))
1359 DyldPrefix = A->getValue();
1360
1361 if (const Arg *A = Args.getLastArg(options::OPT_resource_dir))
1362 ResourceDir = A->getValue();
1363
1364 if (const Arg *A = Args.getLastArg(options::OPT_save_temps_EQ)) {
1365 SaveTemps = llvm::StringSwitch<SaveTempsMode>(A->getValue())
1366 .Case(S: "cwd", Value: SaveTempsCwd)
1367 .Case(S: "obj", Value: SaveTempsObj)
1368 .Default(Value: SaveTempsCwd);
1369 }
1370
1371 if (const Arg *A = Args.getLastArg(options::OPT_offload_host_only,
1372 options::OPT_offload_device_only,
1373 options::OPT_offload_host_device)) {
1374 if (A->getOption().matches(options::OPT_offload_host_only))
1375 Offload = OffloadHost;
1376 else if (A->getOption().matches(options::OPT_offload_device_only))
1377 Offload = OffloadDevice;
1378 else
1379 Offload = OffloadHostDevice;
1380 }
1381
1382 setLTOMode(Args);
1383
1384 // Process -fembed-bitcode= flags.
1385 if (Arg *A = Args.getLastArg(options::OPT_fembed_bitcode_EQ)) {
1386 StringRef Name = A->getValue();
1387 unsigned Model = llvm::StringSwitch<unsigned>(Name)
1388 .Case(S: "off", Value: EmbedNone)
1389 .Case(S: "all", Value: EmbedBitcode)
1390 .Case(S: "bitcode", Value: EmbedBitcode)
1391 .Case(S: "marker", Value: EmbedMarker)
1392 .Default(Value: ~0U);
1393 if (Model == ~0U) {
1394 Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args)
1395 << Name;
1396 } else
1397 BitcodeEmbed = static_cast<BitcodeEmbedMode>(Model);
1398 }
1399
1400 // Remove existing compilation database so that each job can append to it.
1401 if (Arg *A = Args.getLastArg(options::OPT_MJ))
1402 llvm::sys::fs::remove(path: A->getValue());
1403
1404 // Setting up the jobs for some precompile cases depends on whether we are
1405 // treating them as PCH, implicit modules or C++20 ones.
1406 // TODO: inferring the mode like this seems fragile (it meets the objective
1407 // of not requiring anything new for operation, however).
1408 const Arg *Std = Args.getLastArg(options::OPT_std_EQ);
1409 ModulesModeCXX20 =
1410 !Args.hasArg(options::OPT_fmodules) && Std &&
1411 (Std->containsValue("c++20") || Std->containsValue("c++2a") ||
1412 Std->containsValue("c++23") || Std->containsValue("c++2b") ||
1413 Std->containsValue("c++26") || Std->containsValue("c++2c") ||
1414 Std->containsValue("c++latest"));
1415
1416 // Process -fmodule-header{=} flags.
1417 if (Arg *A = Args.getLastArg(options::OPT_fmodule_header_EQ,
1418 options::OPT_fmodule_header)) {
1419 // These flags force C++20 handling of headers.
1420 ModulesModeCXX20 = true;
1421 if (A->getOption().matches(options::OPT_fmodule_header))
1422 CXX20HeaderType = HeaderMode_Default;
1423 else {
1424 StringRef ArgName = A->getValue();
1425 unsigned Kind = llvm::StringSwitch<unsigned>(ArgName)
1426 .Case(S: "user", Value: HeaderMode_User)
1427 .Case(S: "system", Value: HeaderMode_System)
1428 .Default(Value: ~0U);
1429 if (Kind == ~0U) {
1430 Diags.Report(diag::err_drv_invalid_value)
1431 << A->getAsString(Args) << ArgName;
1432 } else
1433 CXX20HeaderType = static_cast<ModuleHeaderMode>(Kind);
1434 }
1435 }
1436
1437 std::unique_ptr<llvm::opt::InputArgList> UArgs =
1438 std::make_unique<InputArgList>(args: std::move(Args));
1439
1440 // Perform the default argument translations.
1441 DerivedArgList *TranslatedArgs = TranslateInputArgs(Args: *UArgs);
1442
1443 // Owned by the host.
1444 const ToolChain &TC = getToolChain(
1445 Args: *UArgs, Target: computeTargetTriple(D: *this, TargetTriple, Args: *UArgs));
1446
1447 // Check if the environment version is valid except wasm case.
1448 llvm::Triple Triple = TC.getTriple();
1449 if (!Triple.isWasm()) {
1450 StringRef TripleVersionName = Triple.getEnvironmentVersionString();
1451 StringRef TripleObjectFormat =
1452 Triple.getObjectFormatTypeName(ObjectFormat: Triple.getObjectFormat());
1453 if (Triple.getEnvironmentVersion().empty() && TripleVersionName != "" &&
1454 TripleVersionName != TripleObjectFormat) {
1455 Diags.Report(diag::err_drv_triple_version_invalid)
1456 << TripleVersionName << TC.getTripleString();
1457 ContainsError = true;
1458 }
1459 }
1460
1461 // Report warning when arm64EC option is overridden by specified target
1462 if ((TC.getTriple().getArch() != llvm::Triple::aarch64 ||
1463 TC.getTriple().getSubArch() != llvm::Triple::AArch64SubArch_arm64ec) &&
1464 UArgs->hasArg(options::OPT__SLASH_arm64EC)) {
1465 getDiags().Report(clang::diag::warn_target_override_arm64ec)
1466 << TC.getTriple().str();
1467 }
1468
1469 // A common user mistake is specifying a target of aarch64-none-eabi or
1470 // arm-none-elf whereas the correct names are aarch64-none-elf &
1471 // arm-none-eabi. Detect these cases and issue a warning.
1472 if (TC.getTriple().getOS() == llvm::Triple::UnknownOS &&
1473 TC.getTriple().getVendor() == llvm::Triple::UnknownVendor) {
1474 switch (TC.getTriple().getArch()) {
1475 case llvm::Triple::arm:
1476 case llvm::Triple::armeb:
1477 case llvm::Triple::thumb:
1478 case llvm::Triple::thumbeb:
1479 if (TC.getTriple().getEnvironmentName() == "elf") {
1480 Diag(diag::warn_target_unrecognized_env)
1481 << TargetTriple
1482 << (TC.getTriple().getArchName().str() + "-none-eabi");
1483 }
1484 break;
1485 case llvm::Triple::aarch64:
1486 case llvm::Triple::aarch64_be:
1487 case llvm::Triple::aarch64_32:
1488 if (TC.getTriple().getEnvironmentName().starts_with(Prefix: "eabi")) {
1489 Diag(diag::warn_target_unrecognized_env)
1490 << TargetTriple
1491 << (TC.getTriple().getArchName().str() + "-none-elf");
1492 }
1493 break;
1494 default:
1495 break;
1496 }
1497 }
1498
1499 // The compilation takes ownership of Args.
1500 Compilation *C = new Compilation(*this, TC, UArgs.release(), TranslatedArgs,
1501 ContainsError);
1502
1503 if (!HandleImmediateArgs(C: *C))
1504 return C;
1505
1506 // Construct the list of inputs.
1507 InputList Inputs;
1508 BuildInputs(TC: C->getDefaultToolChain(), Args&: *TranslatedArgs, Inputs);
1509
1510 // Populate the tool chains for the offloading devices, if any.
1511 CreateOffloadingDeviceToolChains(C&: *C, Inputs);
1512
1513 // Construct the list of abstract actions to perform for this compilation. On
1514 // MachO targets this uses the driver-driver and universal actions.
1515 if (TC.getTriple().isOSBinFormatMachO())
1516 BuildUniversalActions(C&: *C, TC: C->getDefaultToolChain(), BAInputs: Inputs);
1517 else
1518 BuildActions(C&: *C, Args&: C->getArgs(), Inputs, Actions&: C->getActions());
1519
1520 if (CCCPrintPhases) {
1521 PrintActions(C: *C);
1522 return C;
1523 }
1524
1525 BuildJobs(C&: *C);
1526
1527 return C;
1528}
1529
1530static void printArgList(raw_ostream &OS, const llvm::opt::ArgList &Args) {
1531 llvm::opt::ArgStringList ASL;
1532 for (const auto *A : Args) {
1533 // Use user's original spelling of flags. For example, use
1534 // `/source-charset:utf-8` instead of `-finput-charset=utf-8` if the user
1535 // wrote the former.
1536 while (A->getAlias())
1537 A = A->getAlias();
1538 A->render(Args, Output&: ASL);
1539 }
1540
1541 for (auto I = ASL.begin(), E = ASL.end(); I != E; ++I) {
1542 if (I != ASL.begin())
1543 OS << ' ';
1544 llvm::sys::printArg(OS, Arg: *I, Quote: true);
1545 }
1546 OS << '\n';
1547}
1548
1549bool Driver::getCrashDiagnosticFile(StringRef ReproCrashFilename,
1550 SmallString<128> &CrashDiagDir) {
1551 using namespace llvm::sys;
1552 assert(llvm::Triple(llvm::sys::getProcessTriple()).isOSDarwin() &&
1553 "Only knows about .crash files on Darwin");
1554
1555 // The .crash file can be found on at ~/Library/Logs/DiagnosticReports/
1556 // (or /Library/Logs/DiagnosticReports for root) and has the filename pattern
1557 // clang-<VERSION>_<YYYY-MM-DD-HHMMSS>_<hostname>.crash.
1558 path::home_directory(result&: CrashDiagDir);
1559 if (CrashDiagDir.starts_with(Prefix: "/var/root"))
1560 CrashDiagDir = "/";
1561 path::append(path&: CrashDiagDir, a: "Library/Logs/DiagnosticReports");
1562 int PID =
1563#if LLVM_ON_UNIX
1564 getpid();
1565#else
1566 0;
1567#endif
1568 std::error_code EC;
1569 fs::file_status FileStatus;
1570 TimePoint<> LastAccessTime;
1571 SmallString<128> CrashFilePath;
1572 // Lookup the .crash files and get the one generated by a subprocess spawned
1573 // by this driver invocation.
1574 for (fs::directory_iterator File(CrashDiagDir, EC), FileEnd;
1575 File != FileEnd && !EC; File.increment(ec&: EC)) {
1576 StringRef FileName = path::filename(path: File->path());
1577 if (!FileName.starts_with(Prefix: Name))
1578 continue;
1579 if (fs::status(path: File->path(), result&: FileStatus))
1580 continue;
1581 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> CrashFile =
1582 llvm::MemoryBuffer::getFile(Filename: File->path());
1583 if (!CrashFile)
1584 continue;
1585 // The first line should start with "Process:", otherwise this isn't a real
1586 // .crash file.
1587 StringRef Data = CrashFile.get()->getBuffer();
1588 if (!Data.starts_with(Prefix: "Process:"))
1589 continue;
1590 // Parse parent process pid line, e.g: "Parent Process: clang-4.0 [79141]"
1591 size_t ParentProcPos = Data.find(Str: "Parent Process:");
1592 if (ParentProcPos == StringRef::npos)
1593 continue;
1594 size_t LineEnd = Data.find_first_of(Chars: "\n", From: ParentProcPos);
1595 if (LineEnd == StringRef::npos)
1596 continue;
1597 StringRef ParentProcess = Data.slice(Start: ParentProcPos+15, End: LineEnd).trim();
1598 int OpenBracket = -1, CloseBracket = -1;
1599 for (size_t i = 0, e = ParentProcess.size(); i < e; ++i) {
1600 if (ParentProcess[i] == '[')
1601 OpenBracket = i;
1602 if (ParentProcess[i] == ']')
1603 CloseBracket = i;
1604 }
1605 // Extract the parent process PID from the .crash file and check whether
1606 // it matches this driver invocation pid.
1607 int CrashPID;
1608 if (OpenBracket < 0 || CloseBracket < 0 ||
1609 ParentProcess.slice(Start: OpenBracket + 1, End: CloseBracket)
1610 .getAsInteger(Radix: 10, Result&: CrashPID) || CrashPID != PID) {
1611 continue;
1612 }
1613
1614 // Found a .crash file matching the driver pid. To avoid getting an older
1615 // and misleading crash file, continue looking for the most recent.
1616 // FIXME: the driver can dispatch multiple cc1 invocations, leading to
1617 // multiple crashes poiting to the same parent process. Since the driver
1618 // does not collect pid information for the dispatched invocation there's
1619 // currently no way to distinguish among them.
1620 const auto FileAccessTime = FileStatus.getLastModificationTime();
1621 if (FileAccessTime > LastAccessTime) {
1622 CrashFilePath.assign(RHS: File->path());
1623 LastAccessTime = FileAccessTime;
1624 }
1625 }
1626
1627 // If found, copy it over to the location of other reproducer files.
1628 if (!CrashFilePath.empty()) {
1629 EC = fs::copy_file(From: CrashFilePath, To: ReproCrashFilename);
1630 if (EC)
1631 return false;
1632 return true;
1633 }
1634
1635 return false;
1636}
1637
1638static const char BugReporMsg[] =
1639 "\n********************\n\n"
1640 "PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:\n"
1641 "Preprocessed source(s) and associated run script(s) are located at:";
1642
1643// When clang crashes, produce diagnostic information including the fully
1644// preprocessed source file(s). Request that the developer attach the
1645// diagnostic information to a bug report.
1646void Driver::generateCompilationDiagnostics(
1647 Compilation &C, const Command &FailingCommand,
1648 StringRef AdditionalInformation, CompilationDiagnosticReport *Report) {
1649 if (C.getArgs().hasArg(options::OPT_fno_crash_diagnostics))
1650 return;
1651
1652 unsigned Level = 1;
1653 if (Arg *A = C.getArgs().getLastArg(options::OPT_fcrash_diagnostics_EQ)) {
1654 Level = llvm::StringSwitch<unsigned>(A->getValue())
1655 .Case(S: "off", Value: 0)
1656 .Case(S: "compiler", Value: 1)
1657 .Case(S: "all", Value: 2)
1658 .Default(Value: 1);
1659 }
1660 if (!Level)
1661 return;
1662
1663 // Don't try to generate diagnostics for dsymutil jobs.
1664 if (FailingCommand.getCreator().isDsymutilJob())
1665 return;
1666
1667 bool IsLLD = false;
1668 ArgStringList SavedTemps;
1669 if (FailingCommand.getCreator().isLinkJob()) {
1670 C.getDefaultToolChain().GetLinkerPath(LinkerIsLLD: &IsLLD);
1671 if (!IsLLD || Level < 2)
1672 return;
1673
1674 // If lld crashed, we will re-run the same command with the input it used
1675 // to have. In that case we should not remove temp files in
1676 // initCompilationForDiagnostics yet. They will be added back and removed
1677 // later.
1678 SavedTemps = std::move(C.getTempFiles());
1679 assert(!C.getTempFiles().size());
1680 }
1681
1682 // Print the version of the compiler.
1683 PrintVersion(C, OS&: llvm::errs());
1684
1685 // Suppress driver output and emit preprocessor output to temp file.
1686 CCGenDiagnostics = true;
1687
1688 // Save the original job command(s).
1689 Command Cmd = FailingCommand;
1690
1691 // Keep track of whether we produce any errors while trying to produce
1692 // preprocessed sources.
1693 DiagnosticErrorTrap Trap(Diags);
1694
1695 // Suppress tool output.
1696 C.initCompilationForDiagnostics();
1697
1698 // If lld failed, rerun it again with --reproduce.
1699 if (IsLLD) {
1700 const char *TmpName = CreateTempFile(C, Prefix: "linker-crash", Suffix: "tar");
1701 Command NewLLDInvocation = Cmd;
1702 llvm::opt::ArgStringList ArgList = NewLLDInvocation.getArguments();
1703 StringRef ReproduceOption =
1704 C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment()
1705 ? "/reproduce:"
1706 : "--reproduce=";
1707 ArgList.push_back(Elt: Saver.save(S: Twine(ReproduceOption) + TmpName).data());
1708 NewLLDInvocation.replaceArguments(List: std::move(ArgList));
1709
1710 // Redirect stdout/stderr to /dev/null.
1711 NewLLDInvocation.Execute(Redirects: {std::nullopt, {""}, {""}}, ErrMsg: nullptr, ExecutionFailed: nullptr);
1712 Diag(clang::diag::note_drv_command_failed_diag_msg) << BugReporMsg;
1713 Diag(clang::diag::note_drv_command_failed_diag_msg) << TmpName;
1714 Diag(clang::diag::note_drv_command_failed_diag_msg)
1715 << "\n\n********************";
1716 if (Report)
1717 Report->TemporaryFiles.push_back(Elt: TmpName);
1718 return;
1719 }
1720
1721 // Construct the list of inputs.
1722 InputList Inputs;
1723 BuildInputs(TC: C.getDefaultToolChain(), Args&: C.getArgs(), Inputs);
1724
1725 for (InputList::iterator it = Inputs.begin(), ie = Inputs.end(); it != ie;) {
1726 bool IgnoreInput = false;
1727
1728 // Ignore input from stdin or any inputs that cannot be preprocessed.
1729 // Check type first as not all linker inputs have a value.
1730 if (types::getPreprocessedType(Id: it->first) == types::TY_INVALID) {
1731 IgnoreInput = true;
1732 } else if (!strcmp(s1: it->second->getValue(), s2: "-")) {
1733 Diag(clang::diag::note_drv_command_failed_diag_msg)
1734 << "Error generating preprocessed source(s) - "
1735 "ignoring input from stdin.";
1736 IgnoreInput = true;
1737 }
1738
1739 if (IgnoreInput) {
1740 it = Inputs.erase(CI: it);
1741 ie = Inputs.end();
1742 } else {
1743 ++it;
1744 }
1745 }
1746
1747 if (Inputs.empty()) {
1748 Diag(clang::diag::note_drv_command_failed_diag_msg)
1749 << "Error generating preprocessed source(s) - "
1750 "no preprocessable inputs.";
1751 return;
1752 }
1753
1754 // Don't attempt to generate preprocessed files if multiple -arch options are
1755 // used, unless they're all duplicates.
1756 llvm::StringSet<> ArchNames;
1757 for (const Arg *A : C.getArgs()) {
1758 if (A->getOption().matches(options::OPT_arch)) {
1759 StringRef ArchName = A->getValue();
1760 ArchNames.insert(key: ArchName);
1761 }
1762 }
1763 if (ArchNames.size() > 1) {
1764 Diag(clang::diag::note_drv_command_failed_diag_msg)
1765 << "Error generating preprocessed source(s) - cannot generate "
1766 "preprocessed source with multiple -arch options.";
1767 return;
1768 }
1769
1770 // Construct the list of abstract actions to perform for this compilation. On
1771 // Darwin OSes this uses the driver-driver and builds universal actions.
1772 const ToolChain &TC = C.getDefaultToolChain();
1773 if (TC.getTriple().isOSBinFormatMachO())
1774 BuildUniversalActions(C, TC, BAInputs: Inputs);
1775 else
1776 BuildActions(C, Args&: C.getArgs(), Inputs, Actions&: C.getActions());
1777
1778 BuildJobs(C);
1779
1780 // If there were errors building the compilation, quit now.
1781 if (Trap.hasErrorOccurred()) {
1782 Diag(clang::diag::note_drv_command_failed_diag_msg)
1783 << "Error generating preprocessed source(s).";
1784 return;
1785 }
1786
1787 // Generate preprocessed output.
1788 SmallVector<std::pair<int, const Command *>, 4> FailingCommands;
1789 C.ExecuteJobs(Jobs: C.getJobs(), FailingCommands);
1790
1791 // If any of the preprocessing commands failed, clean up and exit.
1792 if (!FailingCommands.empty()) {
1793 Diag(clang::diag::note_drv_command_failed_diag_msg)
1794 << "Error generating preprocessed source(s).";
1795 return;
1796 }
1797
1798 const ArgStringList &TempFiles = C.getTempFiles();
1799 if (TempFiles.empty()) {
1800 Diag(clang::diag::note_drv_command_failed_diag_msg)
1801 << "Error generating preprocessed source(s).";
1802 return;
1803 }
1804
1805 Diag(clang::diag::note_drv_command_failed_diag_msg) << BugReporMsg;
1806
1807 SmallString<128> VFS;
1808 SmallString<128> ReproCrashFilename;
1809 for (const char *TempFile : TempFiles) {
1810 Diag(clang::diag::note_drv_command_failed_diag_msg) << TempFile;
1811 if (Report)
1812 Report->TemporaryFiles.push_back(Elt: TempFile);
1813 if (ReproCrashFilename.empty()) {
1814 ReproCrashFilename = TempFile;
1815 llvm::sys::path::replace_extension(path&: ReproCrashFilename, extension: ".crash");
1816 }
1817 if (StringRef(TempFile).ends_with(Suffix: ".cache")) {
1818 // In some cases (modules) we'll dump extra data to help with reproducing
1819 // the crash into a directory next to the output.
1820 VFS = llvm::sys::path::filename(path: TempFile);
1821 llvm::sys::path::append(path&: VFS, a: "vfs", b: "vfs.yaml");
1822 }
1823 }
1824
1825 for (const char *TempFile : SavedTemps)
1826 C.addTempFile(Name: TempFile);
1827
1828 // Assume associated files are based off of the first temporary file.
1829 CrashReportInfo CrashInfo(TempFiles[0], VFS);
1830
1831 llvm::SmallString<128> Script(CrashInfo.Filename);
1832 llvm::sys::path::replace_extension(path&: Script, extension: "sh");
1833 std::error_code EC;
1834 llvm::raw_fd_ostream ScriptOS(Script, EC, llvm::sys::fs::CD_CreateNew,
1835 llvm::sys::fs::FA_Write,
1836 llvm::sys::fs::OF_Text);
1837 if (EC) {
1838 Diag(clang::diag::note_drv_command_failed_diag_msg)
1839 << "Error generating run script: " << Script << " " << EC.message();
1840 } else {
1841 ScriptOS << "# Crash reproducer for " << getClangFullVersion() << "\n"
1842 << "# Driver args: ";
1843 printArgList(OS&: ScriptOS, Args: C.getInputArgs());
1844 ScriptOS << "# Original command: ";
1845 Cmd.Print(OS&: ScriptOS, Terminator: "\n", /*Quote=*/true);
1846 Cmd.Print(OS&: ScriptOS, Terminator: "\n", /*Quote=*/true, CrashInfo: &CrashInfo);
1847 if (!AdditionalInformation.empty())
1848 ScriptOS << "\n# Additional information: " << AdditionalInformation
1849 << "\n";
1850 if (Report)
1851 Report->TemporaryFiles.push_back(Elt: std::string(Script));
1852 Diag(clang::diag::note_drv_command_failed_diag_msg) << Script;
1853 }
1854
1855 // On darwin, provide information about the .crash diagnostic report.
1856 if (llvm::Triple(llvm::sys::getProcessTriple()).isOSDarwin()) {
1857 SmallString<128> CrashDiagDir;
1858 if (getCrashDiagnosticFile(ReproCrashFilename, CrashDiagDir)) {
1859 Diag(clang::diag::note_drv_command_failed_diag_msg)
1860 << ReproCrashFilename.str();
1861 } else { // Suggest a directory for the user to look for .crash files.
1862 llvm::sys::path::append(path&: CrashDiagDir, a: Name);
1863 CrashDiagDir += "_<YYYY-MM-DD-HHMMSS>_<hostname>.crash";
1864 Diag(clang::diag::note_drv_command_failed_diag_msg)
1865 << "Crash backtrace is located in";
1866 Diag(clang::diag::note_drv_command_failed_diag_msg)
1867 << CrashDiagDir.str();
1868 Diag(clang::diag::note_drv_command_failed_diag_msg)
1869 << "(choose the .crash file that corresponds to your crash)";
1870 }
1871 }
1872
1873 Diag(clang::diag::note_drv_command_failed_diag_msg)
1874 << "\n\n********************";
1875}
1876
1877void Driver::setUpResponseFiles(Compilation &C, Command &Cmd) {
1878 // Since commandLineFitsWithinSystemLimits() may underestimate system's
1879 // capacity if the tool does not support response files, there is a chance/
1880 // that things will just work without a response file, so we silently just
1881 // skip it.
1882 if (Cmd.getResponseFileSupport().ResponseKind ==
1883 ResponseFileSupport::RF_None ||
1884 llvm::sys::commandLineFitsWithinSystemLimits(Program: Cmd.getExecutable(),
1885 Args: Cmd.getArguments()))
1886 return;
1887
1888 std::string TmpName = GetTemporaryPath(Prefix: "response", Suffix: "txt");
1889 Cmd.setResponseFile(C.addTempFile(Name: C.getArgs().MakeArgString(Str: TmpName)));
1890}
1891
1892int Driver::ExecuteCompilation(
1893 Compilation &C,
1894 SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) {
1895 if (C.getArgs().hasArg(options::OPT_fdriver_only)) {
1896 if (C.getArgs().hasArg(options::OPT_v))
1897 C.getJobs().Print(OS&: llvm::errs(), Terminator: "\n", Quote: true);
1898
1899 C.ExecuteJobs(Jobs: C.getJobs(), FailingCommands, /*LogOnly=*/true);
1900
1901 // If there were errors building the compilation, quit now.
1902 if (!FailingCommands.empty() || Diags.hasErrorOccurred())
1903 return 1;
1904
1905 return 0;
1906 }
1907
1908 // Just print if -### was present.
1909 if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
1910 C.getJobs().Print(OS&: llvm::errs(), Terminator: "\n", Quote: true);
1911 return Diags.hasErrorOccurred() ? 1 : 0;
1912 }
1913
1914 // If there were errors building the compilation, quit now.
1915 if (Diags.hasErrorOccurred())
1916 return 1;
1917
1918 // Set up response file names for each command, if necessary.
1919 for (auto &Job : C.getJobs())
1920 setUpResponseFiles(C, Cmd&: Job);
1921
1922 C.ExecuteJobs(Jobs: C.getJobs(), FailingCommands);
1923
1924 // If the command succeeded, we are done.
1925 if (FailingCommands.empty())
1926 return 0;
1927
1928 // Otherwise, remove result files and print extra information about abnormal
1929 // failures.
1930 int Res = 0;
1931 for (const auto &CmdPair : FailingCommands) {
1932 int CommandRes = CmdPair.first;
1933 const Command *FailingCommand = CmdPair.second;
1934
1935 // Remove result files if we're not saving temps.
1936 if (!isSaveTempsEnabled()) {
1937 const JobAction *JA = cast<JobAction>(Val: &FailingCommand->getSource());
1938 C.CleanupFileMap(Files: C.getResultFiles(), JA, IssueErrors: true);
1939
1940 // Failure result files are valid unless we crashed.
1941 if (CommandRes < 0)
1942 C.CleanupFileMap(Files: C.getFailureResultFiles(), JA, IssueErrors: true);
1943 }
1944
1945 // llvm/lib/Support/*/Signals.inc will exit with a special return code
1946 // for SIGPIPE. Do not print diagnostics for this case.
1947 if (CommandRes == EX_IOERR) {
1948 Res = CommandRes;
1949 continue;
1950 }
1951
1952 // Print extra information about abnormal failures, if possible.
1953 //
1954 // This is ad-hoc, but we don't want to be excessively noisy. If the result
1955 // status was 1, assume the command failed normally. In particular, if it
1956 // was the compiler then assume it gave a reasonable error code. Failures
1957 // in other tools are less common, and they generally have worse
1958 // diagnostics, so always print the diagnostic there.
1959 const Tool &FailingTool = FailingCommand->getCreator();
1960
1961 if (!FailingCommand->getCreator().hasGoodDiagnostics() || CommandRes != 1) {
1962 // FIXME: See FIXME above regarding result code interpretation.
1963 if (CommandRes < 0)
1964 Diag(clang::diag::err_drv_command_signalled)
1965 << FailingTool.getShortName();
1966 else
1967 Diag(clang::diag::err_drv_command_failed)
1968 << FailingTool.getShortName() << CommandRes;
1969 }
1970 }
1971 return Res;
1972}
1973
1974void Driver::PrintHelp(bool ShowHidden) const {
1975 llvm::opt::Visibility VisibilityMask = getOptionVisibilityMask();
1976
1977 std::string Usage = llvm::formatv(Fmt: "{0} [options] file...", Vals: Name).str();
1978 getOpts().printHelp(OS&: llvm::outs(), Usage: Usage.c_str(), Title: DriverTitle.c_str(),
1979 ShowHidden, /*ShowAllAliases=*/false,
1980 VisibilityMask);
1981}
1982
1983void Driver::PrintVersion(const Compilation &C, raw_ostream &OS) const {
1984 if (IsFlangMode()) {
1985 OS << getClangToolFullVersion(ToolName: "flang-new") << '\n';
1986 } else {
1987 // FIXME: The following handlers should use a callback mechanism, we don't
1988 // know what the client would like to do.
1989 OS << getClangFullVersion() << '\n';
1990 }
1991 const ToolChain &TC = C.getDefaultToolChain();
1992 OS << "Target: " << TC.getTripleString() << '\n';
1993
1994 // Print the threading model.
1995 if (Arg *A = C.getArgs().getLastArg(options::OPT_mthread_model)) {
1996 // Don't print if the ToolChain would have barfed on it already
1997 if (TC.isThreadModelSupported(Model: A->getValue()))
1998 OS << "Thread model: " << A->getValue();
1999 } else
2000 OS << "Thread model: " << TC.getThreadModel();
2001 OS << '\n';
2002
2003 // Print out the install directory.
2004 OS << "InstalledDir: " << Dir << '\n';
2005
2006 // Print the build config if it's non-default.
2007 // Intended to help LLVM developers understand the configs of compilers
2008 // they're investigating.
2009 if (!llvm::cl::getCompilerBuildConfig().empty())
2010 llvm::cl::printBuildConfig(OS);
2011
2012 // If configuration files were used, print their paths.
2013 for (auto ConfigFile : ConfigFiles)
2014 OS << "Configuration file: " << ConfigFile << '\n';
2015}
2016
2017/// PrintDiagnosticCategories - Implement the --print-diagnostic-categories
2018/// option.
2019static void PrintDiagnosticCategories(raw_ostream &OS) {
2020 // Skip the empty category.
2021 for (unsigned i = 1, max = DiagnosticIDs::getNumberOfCategories(); i != max;
2022 ++i)
2023 OS << i << ',' << DiagnosticIDs::getCategoryNameFromID(CategoryID: i) << '\n';
2024}
2025
2026void Driver::HandleAutocompletions(StringRef PassedFlags) const {
2027 if (PassedFlags == "")
2028 return;
2029 // Print out all options that start with a given argument. This is used for
2030 // shell autocompletion.
2031 std::vector<std::string> SuggestedCompletions;
2032 std::vector<std::string> Flags;
2033
2034 llvm::opt::Visibility VisibilityMask(options::ClangOption);
2035
2036 // Make sure that Flang-only options don't pollute the Clang output
2037 // TODO: Make sure that Clang-only options don't pollute Flang output
2038 if (IsFlangMode())
2039 VisibilityMask = llvm::opt::Visibility(options::FlangOption);
2040
2041 // Distinguish "--autocomplete=-someflag" and "--autocomplete=-someflag,"
2042 // because the latter indicates that the user put space before pushing tab
2043 // which should end up in a file completion.
2044 const bool HasSpace = PassedFlags.ends_with(Suffix: ",");
2045
2046 // Parse PassedFlags by "," as all the command-line flags are passed to this
2047 // function separated by ","
2048 StringRef TargetFlags = PassedFlags;
2049 while (TargetFlags != "") {
2050 StringRef CurFlag;
2051 std::tie(args&: CurFlag, args&: TargetFlags) = TargetFlags.split(Separator: ",");
2052 Flags.push_back(x: std::string(CurFlag));
2053 }
2054
2055 // We want to show cc1-only options only when clang is invoked with -cc1 or
2056 // -Xclang.
2057 if (llvm::is_contained(Range&: Flags, Element: "-Xclang") || llvm::is_contained(Range&: Flags, Element: "-cc1"))
2058 VisibilityMask = llvm::opt::Visibility(options::CC1Option);
2059
2060 const llvm::opt::OptTable &Opts = getOpts();
2061 StringRef Cur;
2062 Cur = Flags.at(n: Flags.size() - 1);
2063 StringRef Prev;
2064 if (Flags.size() >= 2) {
2065 Prev = Flags.at(n: Flags.size() - 2);
2066 SuggestedCompletions = Opts.suggestValueCompletions(Option: Prev, Arg: Cur);
2067 }
2068
2069 if (SuggestedCompletions.empty())
2070 SuggestedCompletions = Opts.suggestValueCompletions(Option: Cur, Arg: "");
2071
2072 // If Flags were empty, it means the user typed `clang [tab]` where we should
2073 // list all possible flags. If there was no value completion and the user
2074 // pressed tab after a space, we should fall back to a file completion.
2075 // We're printing a newline to be consistent with what we print at the end of
2076 // this function.
2077 if (SuggestedCompletions.empty() && HasSpace && !Flags.empty()) {
2078 llvm::outs() << '\n';
2079 return;
2080 }
2081
2082 // When flag ends with '=' and there was no value completion, return empty
2083 // string and fall back to the file autocompletion.
2084 if (SuggestedCompletions.empty() && !Cur.ends_with(Suffix: "=")) {
2085 // If the flag is in the form of "--autocomplete=-foo",
2086 // we were requested to print out all option names that start with "-foo".
2087 // For example, "--autocomplete=-fsyn" is expanded to "-fsyntax-only".
2088 SuggestedCompletions = Opts.findByPrefix(
2089 Cur, VisibilityMask,
2090 /*DisableFlags=*/options::Unsupported | options::Ignored);
2091
2092 // We have to query the -W flags manually as they're not in the OptTable.
2093 // TODO: Find a good way to add them to OptTable instead and them remove
2094 // this code.
2095 for (StringRef S : DiagnosticIDs::getDiagnosticFlags())
2096 if (S.starts_with(Cur))
2097 SuggestedCompletions.push_back(std::string(S));
2098 }
2099
2100 // Sort the autocomplete candidates so that shells print them out in a
2101 // deterministic order. We could sort in any way, but we chose
2102 // case-insensitive sorting for consistency with the -help option
2103 // which prints out options in the case-insensitive alphabetical order.
2104 llvm::sort(C&: SuggestedCompletions, Comp: [](StringRef A, StringRef B) {
2105 if (int X = A.compare_insensitive(RHS: B))
2106 return X < 0;
2107 return A.compare(RHS: B) > 0;
2108 });
2109
2110 llvm::outs() << llvm::join(R&: SuggestedCompletions, Separator: "\n") << '\n';
2111}
2112
2113bool Driver::HandleImmediateArgs(const Compilation &C) {
2114 // The order these options are handled in gcc is all over the place, but we
2115 // don't expect inconsistencies w.r.t. that to matter in practice.
2116
2117 if (C.getArgs().hasArg(options::OPT_dumpmachine)) {
2118 llvm::outs() << C.getDefaultToolChain().getTripleString() << '\n';
2119 return false;
2120 }
2121
2122 if (C.getArgs().hasArg(options::OPT_dumpversion)) {
2123 // Since -dumpversion is only implemented for pedantic GCC compatibility, we
2124 // return an answer which matches our definition of __VERSION__.
2125 llvm::outs() << CLANG_VERSION_STRING << "\n";
2126 return false;
2127 }
2128
2129 if (C.getArgs().hasArg(options::OPT__print_diagnostic_categories)) {
2130 PrintDiagnosticCategories(OS&: llvm::outs());
2131 return false;
2132 }
2133
2134 if (C.getArgs().hasArg(options::OPT_help) ||
2135 C.getArgs().hasArg(options::OPT__help_hidden)) {
2136 PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden));
2137 return false;
2138 }
2139
2140 if (C.getArgs().hasArg(options::OPT__version)) {
2141 // Follow gcc behavior and use stdout for --version and stderr for -v.
2142 PrintVersion(C, OS&: llvm::outs());
2143 return false;
2144 }
2145
2146 if (C.getArgs().hasArg(options::OPT_v) ||
2147 C.getArgs().hasArg(options::OPT__HASH_HASH_HASH) ||
2148 C.getArgs().hasArg(options::OPT_print_supported_cpus) ||
2149 C.getArgs().hasArg(options::OPT_print_supported_extensions)) {
2150 PrintVersion(C, OS&: llvm::errs());
2151 SuppressMissingInputWarning = true;
2152 }
2153
2154 if (C.getArgs().hasArg(options::OPT_v)) {
2155 if (!SystemConfigDir.empty())
2156 llvm::errs() << "System configuration file directory: "
2157 << SystemConfigDir << "\n";
2158 if (!UserConfigDir.empty())
2159 llvm::errs() << "User configuration file directory: "
2160 << UserConfigDir << "\n";
2161 }
2162
2163 const ToolChain &TC = C.getDefaultToolChain();
2164
2165 if (C.getArgs().hasArg(options::OPT_v))
2166 TC.printVerboseInfo(OS&: llvm::errs());
2167
2168 if (C.getArgs().hasArg(options::OPT_print_resource_dir)) {
2169 llvm::outs() << ResourceDir << '\n';
2170 return false;
2171 }
2172
2173 if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
2174 llvm::outs() << "programs: =";
2175 bool separator = false;
2176 // Print -B and COMPILER_PATH.
2177 for (const std::string &Path : PrefixDirs) {
2178 if (separator)
2179 llvm::outs() << llvm::sys::EnvPathSeparator;
2180 llvm::outs() << Path;
2181 separator = true;
2182 }
2183 for (const std::string &Path : TC.getProgramPaths()) {
2184 if (separator)
2185 llvm::outs() << llvm::sys::EnvPathSeparator;
2186 llvm::outs() << Path;
2187 separator = true;
2188 }
2189 llvm::outs() << "\n";
2190 llvm::outs() << "libraries: =" << ResourceDir;
2191
2192 StringRef sysroot = C.getSysRoot();
2193
2194 for (const std::string &Path : TC.getFilePaths()) {
2195 // Always print a separator. ResourceDir was the first item shown.
2196 llvm::outs() << llvm::sys::EnvPathSeparator;
2197 // Interpretation of leading '=' is needed only for NetBSD.
2198 if (Path[0] == '=')
2199 llvm::outs() << sysroot << Path.substr(pos: 1);
2200 else
2201 llvm::outs() << Path;
2202 }
2203 llvm::outs() << "\n";
2204 return false;
2205 }
2206
2207 if (C.getArgs().hasArg(options::OPT_print_std_module_manifest_path)) {
2208 llvm::outs() << GetStdModuleManifestPath(C, TC: C.getDefaultToolChain())
2209 << '\n';
2210 return false;
2211 }
2212
2213 if (C.getArgs().hasArg(options::OPT_print_runtime_dir)) {
2214 if (std::optional<std::string> RuntimePath = TC.getRuntimePath())
2215 llvm::outs() << *RuntimePath << '\n';
2216 else
2217 llvm::outs() << TC.getCompilerRTPath() << '\n';
2218 return false;
2219 }
2220
2221 if (C.getArgs().hasArg(options::OPT_print_diagnostic_options)) {
2222 std::vector<std::string> Flags = DiagnosticIDs::getDiagnosticFlags();
2223 for (std::size_t I = 0; I != Flags.size(); I += 2)
2224 llvm::outs() << " " << Flags[I] << "\n " << Flags[I + 1] << "\n\n";
2225 return false;
2226 }
2227
2228 // FIXME: The following handlers should use a callback mechanism, we don't
2229 // know what the client would like to do.
2230 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
2231 llvm::outs() << GetFilePath(Name: A->getValue(), TC) << "\n";
2232 return false;
2233 }
2234
2235 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
2236 StringRef ProgName = A->getValue();
2237
2238 // Null program name cannot have a path.
2239 if (! ProgName.empty())
2240 llvm::outs() << GetProgramPath(Name: ProgName, TC);
2241
2242 llvm::outs() << "\n";
2243 return false;
2244 }
2245
2246 if (Arg *A = C.getArgs().getLastArg(options::OPT_autocomplete)) {
2247 StringRef PassedFlags = A->getValue();
2248 HandleAutocompletions(PassedFlags);
2249 return false;
2250 }
2251
2252 if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
2253 ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(Args: C.getArgs());
2254 const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(Args: C.getArgs()));
2255 RegisterEffectiveTriple TripleRAII(TC, Triple);
2256 switch (RLT) {
2257 case ToolChain::RLT_CompilerRT:
2258 llvm::outs() << TC.getCompilerRT(Args: C.getArgs(), Component: "builtins") << "\n";
2259 break;
2260 case ToolChain::RLT_Libgcc:
2261 llvm::outs() << GetFilePath(Name: "libgcc.a", TC) << "\n";
2262 break;
2263 }
2264 return false;
2265 }
2266
2267 if (C.getArgs().hasArg(options::OPT_print_multi_lib)) {
2268 for (const Multilib &Multilib : TC.getMultilibs())
2269 llvm::outs() << Multilib << "\n";
2270 return false;
2271 }
2272
2273 if (C.getArgs().hasArg(options::OPT_print_multi_flags)) {
2274 Multilib::flags_list ArgFlags = TC.getMultilibFlags(C.getArgs());
2275 llvm::StringSet<> ExpandedFlags = TC.getMultilibs().expandFlags(ArgFlags);
2276 std::set<llvm::StringRef> SortedFlags;
2277 for (const auto &FlagEntry : ExpandedFlags)
2278 SortedFlags.insert(x: FlagEntry.getKey());
2279 for (auto Flag : SortedFlags)
2280 llvm::outs() << Flag << '\n';
2281 return false;
2282 }
2283
2284 if (C.getArgs().hasArg(options::OPT_print_multi_directory)) {
2285 for (const Multilib &Multilib : TC.getSelectedMultilibs()) {
2286 if (Multilib.gccSuffix().empty())
2287 llvm::outs() << ".\n";
2288 else {
2289 StringRef Suffix(Multilib.gccSuffix());
2290 assert(Suffix.front() == '/');
2291 llvm::outs() << Suffix.substr(Start: 1) << "\n";
2292 }
2293 }
2294 return false;
2295 }
2296
2297 if (C.getArgs().hasArg(options::OPT_print_target_triple)) {
2298 llvm::outs() << TC.getTripleString() << "\n";
2299 return false;
2300 }
2301
2302 if (C.getArgs().hasArg(options::OPT_print_effective_triple)) {
2303 const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(Args: C.getArgs()));
2304 llvm::outs() << Triple.getTriple() << "\n";
2305 return false;
2306 }
2307
2308 if (C.getArgs().hasArg(options::OPT_print_targets)) {
2309 llvm::TargetRegistry::printRegisteredTargetsForVersion(OS&: llvm::outs());
2310 return false;
2311 }
2312
2313 return true;
2314}
2315
2316enum {
2317 TopLevelAction = 0,
2318 HeadSibAction = 1,
2319 OtherSibAction = 2,
2320};
2321
2322// Display an action graph human-readably. Action A is the "sink" node
2323// and latest-occuring action. Traversal is in pre-order, visiting the
2324// inputs to each action before printing the action itself.
2325static unsigned PrintActions1(const Compilation &C, Action *A,
2326 std::map<Action *, unsigned> &Ids,
2327 Twine Indent = {}, int Kind = TopLevelAction) {
2328 if (Ids.count(x: A)) // A was already visited.
2329 return Ids[A];
2330
2331 std::string str;
2332 llvm::raw_string_ostream os(str);
2333
2334 auto getSibIndent = [](int K) -> Twine {
2335 return (K == HeadSibAction) ? " " : (K == OtherSibAction) ? "| " : "";
2336 };
2337
2338 Twine SibIndent = Indent + getSibIndent(Kind);
2339 int SibKind = HeadSibAction;
2340 os << Action::getClassName(AC: A->getKind()) << ", ";
2341 if (InputAction *IA = dyn_cast<InputAction>(Val: A)) {
2342 os << "\"" << IA->getInputArg().getValue() << "\"";
2343 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(Val: A)) {
2344 os << '"' << BIA->getArchName() << '"' << ", {"
2345 << PrintActions1(C, A: *BIA->input_begin(), Ids, Indent: SibIndent, Kind: SibKind) << "}";
2346 } else if (OffloadAction *OA = dyn_cast<OffloadAction>(Val: A)) {
2347 bool IsFirst = true;
2348 OA->doOnEachDependence(
2349 Work: [&](Action *A, const ToolChain *TC, const char *BoundArch) {
2350 assert(TC && "Unknown host toolchain");
2351 // E.g. for two CUDA device dependences whose bound arch is sm_20 and
2352 // sm_35 this will generate:
2353 // "cuda-device" (nvptx64-nvidia-cuda:sm_20) {#ID}, "cuda-device"
2354 // (nvptx64-nvidia-cuda:sm_35) {#ID}
2355 if (!IsFirst)
2356 os << ", ";
2357 os << '"';
2358 os << A->getOffloadingKindPrefix();
2359 os << " (";
2360 os << TC->getTriple().normalize();
2361 if (BoundArch)
2362 os << ":" << BoundArch;
2363 os << ")";
2364 os << '"';
2365 os << " {" << PrintActions1(C, A, Ids, Indent: SibIndent, Kind: SibKind) << "}";
2366 IsFirst = false;
2367 SibKind = OtherSibAction;
2368 });
2369 } else {
2370 const ActionList *AL = &A->getInputs();
2371
2372 if (AL->size()) {
2373 const char *Prefix = "{";
2374 for (Action *PreRequisite : *AL) {
2375 os << Prefix << PrintActions1(C, A: PreRequisite, Ids, Indent: SibIndent, Kind: SibKind);
2376 Prefix = ", ";
2377 SibKind = OtherSibAction;
2378 }
2379 os << "}";
2380 } else
2381 os << "{}";
2382 }
2383
2384 // Append offload info for all options other than the offloading action
2385 // itself (e.g. (cuda-device, sm_20) or (cuda-host)).
2386 std::string offload_str;
2387 llvm::raw_string_ostream offload_os(offload_str);
2388 if (!isa<OffloadAction>(Val: A)) {
2389 auto S = A->getOffloadingKindPrefix();
2390 if (!S.empty()) {
2391 offload_os << ", (" << S;
2392 if (A->getOffloadingArch())
2393 offload_os << ", " << A->getOffloadingArch();
2394 offload_os << ")";
2395 }
2396 }
2397
2398 auto getSelfIndent = [](int K) -> Twine {
2399 return (K == HeadSibAction) ? "+- " : (K == OtherSibAction) ? "|- " : "";
2400 };
2401
2402 unsigned Id = Ids.size();
2403 Ids[A] = Id;
2404 llvm::errs() << Indent + getSelfIndent(Kind) << Id << ": " << os.str() << ", "
2405 << types::getTypeName(Id: A->getType()) << offload_os.str() << "\n";
2406
2407 return Id;
2408}
2409
2410// Print the action graphs in a compilation C.
2411// For example "clang -c file1.c file2.c" is composed of two subgraphs.
2412void Driver::PrintActions(const Compilation &C) const {
2413 std::map<Action *, unsigned> Ids;
2414 for (Action *A : C.getActions())
2415 PrintActions1(C, A, Ids);
2416}
2417
2418/// Check whether the given input tree contains any compilation or
2419/// assembly actions.
2420static bool ContainsCompileOrAssembleAction(const Action *A) {
2421 if (isa<CompileJobAction>(Val: A) || isa<BackendJobAction>(Val: A) ||
2422 isa<AssembleJobAction>(Val: A))
2423 return true;
2424
2425 return llvm::any_of(Range: A->inputs(), P: ContainsCompileOrAssembleAction);
2426}
2427
2428void Driver::BuildUniversalActions(Compilation &C, const ToolChain &TC,
2429 const InputList &BAInputs) const {
2430 DerivedArgList &Args = C.getArgs();
2431 ActionList &Actions = C.getActions();
2432 llvm::PrettyStackTraceString CrashInfo("Building universal build actions");
2433 // Collect the list of architectures. Duplicates are allowed, but should only
2434 // be handled once (in the order seen).
2435 llvm::StringSet<> ArchNames;
2436 SmallVector<const char *, 4> Archs;
2437 for (Arg *A : Args) {
2438 if (A->getOption().matches(options::OPT_arch)) {
2439 // Validate the option here; we don't save the type here because its
2440 // particular spelling may participate in other driver choices.
2441 llvm::Triple::ArchType Arch =
2442 tools::darwin::getArchTypeForMachOArchName(Str: A->getValue());
2443 if (Arch == llvm::Triple::UnknownArch) {
2444 Diag(clang::diag::err_drv_invalid_arch_name) << A->getAsString(Args);
2445 continue;
2446 }
2447
2448 A->claim();
2449 if (ArchNames.insert(key: A->getValue()).second)
2450 Archs.push_back(Elt: A->getValue());
2451 }
2452 }
2453
2454 // When there is no explicit arch for this platform, make sure we still bind
2455 // the architecture (to the default) so that -Xarch_ is handled correctly.
2456 if (!Archs.size())
2457 Archs.push_back(Elt: Args.MakeArgString(Str: TC.getDefaultUniversalArchName()));
2458
2459 ActionList SingleActions;
2460 BuildActions(C, Args, Inputs: BAInputs, Actions&: SingleActions);
2461
2462 // Add in arch bindings for every top level action, as well as lipo and
2463 // dsymutil steps if needed.
2464 for (Action* Act : SingleActions) {
2465 // Make sure we can lipo this kind of output. If not (and it is an actual
2466 // output) then we disallow, since we can't create an output file with the
2467 // right name without overwriting it. We could remove this oddity by just
2468 // changing the output names to include the arch, which would also fix
2469 // -save-temps. Compatibility wins for now.
2470
2471 if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
2472 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
2473 << types::getTypeName(Act->getType());
2474
2475 ActionList Inputs;
2476 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
2477 Inputs.push_back(Elt: C.MakeAction<BindArchAction>(Arg&: Act, Arg&: Archs[i]));
2478
2479 // Lipo if necessary, we do it this way because we need to set the arch flag
2480 // so that -Xarch_ gets overwritten.
2481 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
2482 Actions.append(in_start: Inputs.begin(), in_end: Inputs.end());
2483 else
2484 Actions.push_back(Elt: C.MakeAction<LipoJobAction>(Arg&: Inputs, Arg: Act->getType()));
2485
2486 // Handle debug info queries.
2487 Arg *A = Args.getLastArg(options::OPT_g_Group);
2488 bool enablesDebugInfo = A && !A->getOption().matches(options::OPT_g0) &&
2489 !A->getOption().matches(options::OPT_gstabs);
2490 if ((enablesDebugInfo || willEmitRemarks(Args)) &&
2491 ContainsCompileOrAssembleAction(A: Actions.back())) {
2492
2493 // Add a 'dsymutil' step if necessary, when debug info is enabled and we
2494 // have a compile input. We need to run 'dsymutil' ourselves in such cases
2495 // because the debug info will refer to a temporary object file which
2496 // will be removed at the end of the compilation process.
2497 if (Act->getType() == types::TY_Image) {
2498 ActionList Inputs;
2499 Inputs.push_back(Elt: Actions.back());
2500 Actions.pop_back();
2501 Actions.push_back(
2502 Elt: C.MakeAction<DsymutilJobAction>(Arg&: Inputs, Arg: types::TY_dSYM));
2503 }
2504
2505 // Verify the debug info output.
2506 if (Args.hasArg(options::OPT_verify_debug_info)) {
2507 Action* LastAction = Actions.back();
2508 Actions.pop_back();
2509 Actions.push_back(Elt: C.MakeAction<VerifyDebugInfoJobAction>(
2510 Arg&: LastAction, Arg: types::TY_Nothing));
2511 }
2512 }
2513 }
2514}
2515
2516bool Driver::DiagnoseInputExistence(const DerivedArgList &Args, StringRef Value,
2517 types::ID Ty, bool TypoCorrect) const {
2518 if (!getCheckInputsExist())
2519 return true;
2520
2521 // stdin always exists.
2522 if (Value == "-")
2523 return true;
2524
2525 // If it's a header to be found in the system or user search path, then defer
2526 // complaints about its absence until those searches can be done. When we
2527 // are definitely processing headers for C++20 header units, extend this to
2528 // allow the user to put "-fmodule-header -xc++-header vector" for example.
2529 if (Ty == types::TY_CXXSHeader || Ty == types::TY_CXXUHeader ||
2530 (ModulesModeCXX20 && Ty == types::TY_CXXHeader))
2531 return true;
2532
2533 if (getVFS().exists(Path: Value))
2534 return true;
2535
2536 if (TypoCorrect) {
2537 // Check if the filename is a typo for an option flag. OptTable thinks
2538 // that all args that are not known options and that start with / are
2539 // filenames, but e.g. `/diagnostic:caret` is more likely a typo for
2540 // the option `/diagnostics:caret` than a reference to a file in the root
2541 // directory.
2542 std::string Nearest;
2543 if (getOpts().findNearest(Option: Value, NearestString&: Nearest, VisibilityMask: getOptionVisibilityMask()) <= 1) {
2544 Diag(clang::diag::err_drv_no_such_file_with_suggestion)
2545 << Value << Nearest;
2546 return false;
2547 }
2548 }
2549
2550 // In CL mode, don't error on apparently non-existent linker inputs, because
2551 // they can be influenced by linker flags the clang driver might not
2552 // understand.
2553 // Examples:
2554 // - `clang-cl main.cc ole32.lib` in a non-MSVC shell will make the driver
2555 // module look for an MSVC installation in the registry. (We could ask
2556 // the MSVCToolChain object if it can find `ole32.lib`, but the logic to
2557 // look in the registry might move into lld-link in the future so that
2558 // lld-link invocations in non-MSVC shells just work too.)
2559 // - `clang-cl ... /link ...` can pass arbitrary flags to the linker,
2560 // including /libpath:, which is used to find .lib and .obj files.
2561 // So do not diagnose this on the driver level. Rely on the linker diagnosing
2562 // it. (If we don't end up invoking the linker, this means we'll emit a
2563 // "'linker' input unused [-Wunused-command-line-argument]" warning instead
2564 // of an error.)
2565 //
2566 // Only do this skip after the typo correction step above. `/Brepo` is treated
2567 // as TY_Object, but it's clearly a typo for `/Brepro`. It seems fine to emit
2568 // an error if we have a flag that's within an edit distance of 1 from a
2569 // flag. (Users can use `-Wl,` or `/linker` to launder the flag past the
2570 // driver in the unlikely case they run into this.)
2571 //
2572 // Don't do this for inputs that start with a '/', else we'd pass options
2573 // like /libpath: through to the linker silently.
2574 //
2575 // Emitting an error for linker inputs can also cause incorrect diagnostics
2576 // with the gcc driver. The command
2577 // clang -fuse-ld=lld -Wl,--chroot,some/dir /file.o
2578 // will make lld look for some/dir/file.o, while we will diagnose here that
2579 // `/file.o` does not exist. However, configure scripts check if
2580 // `clang /GR-` compiles without error to see if the compiler is cl.exe,
2581 // so we can't downgrade diagnostics for `/GR-` from an error to a warning
2582 // in cc mode. (We can in cl mode because cl.exe itself only warns on
2583 // unknown flags.)
2584 if (IsCLMode() && Ty == types::TY_Object && !Value.starts_with(Prefix: "/"))
2585 return true;
2586
2587 Diag(clang::diag::err_drv_no_such_file) << Value;
2588 return false;
2589}
2590
2591// Get the C++20 Header Unit type corresponding to the input type.
2592static types::ID CXXHeaderUnitType(ModuleHeaderMode HM) {
2593 switch (HM) {
2594 case HeaderMode_User:
2595 return types::TY_CXXUHeader;
2596 case HeaderMode_System:
2597 return types::TY_CXXSHeader;
2598 case HeaderMode_Default:
2599 break;
2600 case HeaderMode_None:
2601 llvm_unreachable("should not be called in this case");
2602 }
2603 return types::TY_CXXHUHeader;
2604}
2605
2606// Construct a the list of inputs and their types.
2607void Driver::BuildInputs(const ToolChain &TC, DerivedArgList &Args,
2608 InputList &Inputs) const {
2609 const llvm::opt::OptTable &Opts = getOpts();
2610 // Track the current user specified (-x) input. We also explicitly track the
2611 // argument used to set the type; we only want to claim the type when we
2612 // actually use it, so we warn about unused -x arguments.
2613 types::ID InputType = types::TY_Nothing;
2614 Arg *InputTypeArg = nullptr;
2615
2616 // The last /TC or /TP option sets the input type to C or C++ globally.
2617 if (Arg *TCTP = Args.getLastArgNoClaim(options::OPT__SLASH_TC,
2618 options::OPT__SLASH_TP)) {
2619 InputTypeArg = TCTP;
2620 InputType = TCTP->getOption().matches(options::OPT__SLASH_TC)
2621 ? types::TY_C
2622 : types::TY_CXX;
2623
2624 Arg *Previous = nullptr;
2625 bool ShowNote = false;
2626 for (Arg *A :
2627 Args.filtered(options::OPT__SLASH_TC, options::OPT__SLASH_TP)) {
2628 if (Previous) {
2629 Diag(clang::diag::warn_drv_overriding_option)
2630 << Previous->getSpelling() << A->getSpelling();
2631 ShowNote = true;
2632 }
2633 Previous = A;
2634 }
2635 if (ShowNote)
2636 Diag(clang::diag::note_drv_t_option_is_global);
2637 }
2638
2639 // CUDA/HIP and their preprocessor expansions can be accepted by CL mode.
2640 // Warn -x after last input file has no effect
2641 auto LastXArg = Args.getLastArgValue(options::OPT_x);
2642 const llvm::StringSet<> ValidXArgs = {"cuda", "hip", "cui", "hipi"};
2643 if (!IsCLMode() || ValidXArgs.contains(key: LastXArg)) {
2644 Arg *LastXArg = Args.getLastArgNoClaim(options::OPT_x);
2645 Arg *LastInputArg = Args.getLastArgNoClaim(options::OPT_INPUT);
2646 if (LastXArg && LastInputArg &&
2647 LastInputArg->getIndex() < LastXArg->getIndex())
2648 Diag(clang::diag::warn_drv_unused_x) << LastXArg->getValue();
2649 } else {
2650 // In CL mode suggest /TC or /TP since -x doesn't make sense if passed via
2651 // /clang:.
2652 if (auto *A = Args.getLastArg(options::OPT_x))
2653 Diag(diag::err_drv_unsupported_opt_with_suggestion)
2654 << A->getAsString(Args) << "/TC' or '/TP";
2655 }
2656
2657 for (Arg *A : Args) {
2658 if (A->getOption().getKind() == Option::InputClass) {
2659 const char *Value = A->getValue();
2660 types::ID Ty = types::TY_INVALID;
2661
2662 // Infer the input type if necessary.
2663 if (InputType == types::TY_Nothing) {
2664 // If there was an explicit arg for this, claim it.
2665 if (InputTypeArg)
2666 InputTypeArg->claim();
2667
2668 // stdin must be handled specially.
2669 if (memcmp(s1: Value, s2: "-", n: 2) == 0) {
2670 if (IsFlangMode()) {
2671 Ty = types::TY_Fortran;
2672 } else if (IsDXCMode()) {
2673 Ty = types::TY_HLSL;
2674 } else {
2675 // If running with -E, treat as a C input (this changes the
2676 // builtin macros, for example). This may be overridden by -ObjC
2677 // below.
2678 //
2679 // Otherwise emit an error but still use a valid type to avoid
2680 // spurious errors (e.g., no inputs).
2681 assert(!CCGenDiagnostics && "stdin produces no crash reproducer");
2682 if (!Args.hasArgNoClaim(options::OPT_E) && !CCCIsCPP())
2683 Diag(IsCLMode() ? clang::diag::err_drv_unknown_stdin_type_clang_cl
2684 : clang::diag::err_drv_unknown_stdin_type);
2685 Ty = types::TY_C;
2686 }
2687 } else {
2688 // Otherwise lookup by extension.
2689 // Fallback is C if invoked as C preprocessor, C++ if invoked with
2690 // clang-cl /E, or Object otherwise.
2691 // We use a host hook here because Darwin at least has its own
2692 // idea of what .s is.
2693 if (const char *Ext = strrchr(s: Value, c: '.'))
2694 Ty = TC.LookupTypeForExtension(Ext: Ext + 1);
2695
2696 if (Ty == types::TY_INVALID) {
2697 if (IsCLMode() && (Args.hasArgNoClaim(options::OPT_E) || CCGenDiagnostics))
2698 Ty = types::TY_CXX;
2699 else if (CCCIsCPP() || CCGenDiagnostics)
2700 Ty = types::TY_C;
2701 else
2702 Ty = types::TY_Object;
2703 }
2704
2705 // If the driver is invoked as C++ compiler (like clang++ or c++) it
2706 // should autodetect some input files as C++ for g++ compatibility.
2707 if (CCCIsCXX()) {
2708 types::ID OldTy = Ty;
2709 Ty = types::lookupCXXTypeForCType(Id: Ty);
2710
2711 // Do not complain about foo.h, when we are known to be processing
2712 // it as a C++20 header unit.
2713 if (Ty != OldTy && !(OldTy == types::TY_CHeader && hasHeaderMode()))
2714 Diag(clang::diag::warn_drv_treating_input_as_cxx)
2715 << getTypeName(OldTy) << getTypeName(Ty);
2716 }
2717
2718 // If running with -fthinlto-index=, extensions that normally identify
2719 // native object files actually identify LLVM bitcode files.
2720 if (Args.hasArgNoClaim(options::OPT_fthinlto_index_EQ) &&
2721 Ty == types::TY_Object)
2722 Ty = types::TY_LLVM_BC;
2723 }
2724
2725 // -ObjC and -ObjC++ override the default language, but only for "source
2726 // files". We just treat everything that isn't a linker input as a
2727 // source file.
2728 //
2729 // FIXME: Clean this up if we move the phase sequence into the type.
2730 if (Ty != types::TY_Object) {
2731 if (Args.hasArg(options::OPT_ObjC))
2732 Ty = types::TY_ObjC;
2733 else if (Args.hasArg(options::OPT_ObjCXX))
2734 Ty = types::TY_ObjCXX;
2735 }
2736
2737 // Disambiguate headers that are meant to be header units from those
2738 // intended to be PCH. Avoid missing '.h' cases that are counted as
2739 // C headers by default - we know we are in C++ mode and we do not
2740 // want to issue a complaint about compiling things in the wrong mode.
2741 if ((Ty == types::TY_CXXHeader || Ty == types::TY_CHeader) &&
2742 hasHeaderMode())
2743 Ty = CXXHeaderUnitType(HM: CXX20HeaderType);
2744 } else {
2745 assert(InputTypeArg && "InputType set w/o InputTypeArg");
2746 if (!InputTypeArg->getOption().matches(options::OPT_x)) {
2747 // If emulating cl.exe, make sure that /TC and /TP don't affect input
2748 // object files.
2749 const char *Ext = strrchr(s: Value, c: '.');
2750 if (Ext && TC.LookupTypeForExtension(Ext: Ext + 1) == types::TY_Object)
2751 Ty = types::TY_Object;
2752 }
2753 if (Ty == types::TY_INVALID) {
2754 Ty = InputType;
2755 InputTypeArg->claim();
2756 }
2757 }
2758
2759 if ((Ty == types::TY_C || Ty == types::TY_CXX) &&
2760 Args.hasArgNoClaim(options::OPT_hipstdpar))
2761 Ty = types::TY_HIP;
2762
2763 if (DiagnoseInputExistence(Args, Value, Ty, /*TypoCorrect=*/true))
2764 Inputs.push_back(Elt: std::make_pair(x&: Ty, y&: A));
2765
2766 } else if (A->getOption().matches(options::OPT__SLASH_Tc)) {
2767 StringRef Value = A->getValue();
2768 if (DiagnoseInputExistence(Args, Value, Ty: types::TY_C,
2769 /*TypoCorrect=*/false)) {
2770 Arg *InputArg = MakeInputArg(Args, Opts, Value: A->getValue());
2771 Inputs.push_back(Elt: std::make_pair(x: types::TY_C, y&: InputArg));
2772 }
2773 A->claim();
2774 } else if (A->getOption().matches(options::OPT__SLASH_Tp)) {
2775 StringRef Value = A->getValue();
2776 if (DiagnoseInputExistence(Args, Value, Ty: types::TY_CXX,
2777 /*TypoCorrect=*/false)) {
2778 Arg *InputArg = MakeInputArg(Args, Opts, Value: A->getValue());
2779 Inputs.push_back(Elt: std::make_pair(x: types::TY_CXX, y&: InputArg));
2780 }
2781 A->claim();
2782 } else if (A->getOption().hasFlag(Val: options::LinkerInput)) {
2783 // Just treat as object type, we could make a special type for this if
2784 // necessary.
2785 Inputs.push_back(Elt: std::make_pair(x: types::TY_Object, y&: A));
2786
2787 } else if (A->getOption().matches(options::OPT_x)) {
2788 InputTypeArg = A;
2789 InputType = types::lookupTypeForTypeSpecifier(Name: A->getValue());
2790 A->claim();
2791
2792 // Follow gcc behavior and treat as linker input for invalid -x
2793 // options. Its not clear why we shouldn't just revert to unknown; but
2794 // this isn't very important, we might as well be bug compatible.
2795 if (!InputType) {
2796 Diag(clang::diag::err_drv_unknown_language) << A->getValue();
2797 InputType = types::TY_Object;
2798 }
2799
2800 // If the user has put -fmodule-header{,=} then we treat C++ headers as
2801 // header unit inputs. So we 'promote' -xc++-header appropriately.
2802 if (InputType == types::TY_CXXHeader && hasHeaderMode())
2803 InputType = CXXHeaderUnitType(HM: CXX20HeaderType);
2804 } else if (A->getOption().getID() == options::OPT_U) {
2805 assert(A->getNumValues() == 1 && "The /U option has one value.");
2806 StringRef Val = A->getValue(N: 0);
2807 if (Val.find_first_of(Chars: "/\\") != StringRef::npos) {
2808 // Warn about e.g. "/Users/me/myfile.c".
2809 Diag(diag::warn_slash_u_filename) << Val;
2810 Diag(diag::note_use_dashdash);
2811 }
2812 }
2813 }
2814 if (CCCIsCPP() && Inputs.empty()) {
2815 // If called as standalone preprocessor, stdin is processed
2816 // if no other input is present.
2817 Arg *A = MakeInputArg(Args, Opts, Value: "-");
2818 Inputs.push_back(Elt: std::make_pair(x: types::TY_C, y&: A));
2819 }
2820}
2821
2822namespace {
2823/// Provides a convenient interface for different programming models to generate
2824/// the required device actions.
2825class OffloadingActionBuilder final {
2826 /// Flag used to trace errors in the builder.
2827 bool IsValid = false;
2828
2829 /// The compilation that is using this builder.
2830 Compilation &C;
2831
2832 /// Map between an input argument and the offload kinds used to process it.
2833 std::map<const Arg *, unsigned> InputArgToOffloadKindMap;
2834
2835 /// Map between a host action and its originating input argument.
2836 std::map<Action *, const Arg *> HostActionToInputArgMap;
2837
2838 /// Builder interface. It doesn't build anything or keep any state.
2839 class DeviceActionBuilder {
2840 public:
2841 typedef const llvm::SmallVectorImpl<phases::ID> PhasesTy;
2842
2843 enum ActionBuilderReturnCode {
2844 // The builder acted successfully on the current action.
2845 ABRT_Success,
2846 // The builder didn't have to act on the current action.
2847 ABRT_Inactive,
2848 // The builder was successful and requested the host action to not be
2849 // generated.
2850 ABRT_Ignore_Host,
2851 };
2852
2853 protected:
2854 /// Compilation associated with this builder.
2855 Compilation &C;
2856
2857 /// Tool chains associated with this builder. The same programming
2858 /// model may have associated one or more tool chains.
2859 SmallVector<const ToolChain *, 2> ToolChains;
2860
2861 /// The derived arguments associated with this builder.
2862 DerivedArgList &Args;
2863
2864 /// The inputs associated with this builder.
2865 const Driver::InputList &Inputs;
2866
2867 /// The associated offload kind.
2868 Action::OffloadKind AssociatedOffloadKind = Action::OFK_None;
2869
2870 public:
2871 DeviceActionBuilder(Compilation &C, DerivedArgList &Args,
2872 const Driver::InputList &Inputs,
2873 Action::OffloadKind AssociatedOffloadKind)
2874 : C(C), Args(Args), Inputs(Inputs),
2875 AssociatedOffloadKind(AssociatedOffloadKind) {}
2876 virtual ~DeviceActionBuilder() {}
2877
2878 /// Fill up the array \a DA with all the device dependences that should be
2879 /// added to the provided host action \a HostAction. By default it is
2880 /// inactive.
2881 virtual ActionBuilderReturnCode
2882 getDeviceDependences(OffloadAction::DeviceDependences &DA,
2883 phases::ID CurPhase, phases::ID FinalPhase,
2884 PhasesTy &Phases) {
2885 return ABRT_Inactive;
2886 }
2887
2888 /// Update the state to include the provided host action \a HostAction as a
2889 /// dependency of the current device action. By default it is inactive.
2890 virtual ActionBuilderReturnCode addDeviceDependences(Action *HostAction) {
2891 return ABRT_Inactive;
2892 }
2893
2894 /// Append top level actions generated by the builder.
2895 virtual void appendTopLevelActions(ActionList &AL) {}
2896
2897 /// Append linker device actions generated by the builder.
2898 virtual void appendLinkDeviceActions(ActionList &AL) {}
2899
2900 /// Append linker host action generated by the builder.
2901 virtual Action* appendLinkHostActions(ActionList &AL) { return nullptr; }
2902
2903 /// Append linker actions generated by the builder.
2904 virtual void appendLinkDependences(OffloadAction::DeviceDependences &DA) {}
2905
2906 /// Initialize the builder. Return true if any initialization errors are
2907 /// found.
2908 virtual bool initialize() { return false; }
2909
2910 /// Return true if the builder can use bundling/unbundling.
2911 virtual bool canUseBundlerUnbundler() const { return false; }
2912
2913 /// Return true if this builder is valid. We have a valid builder if we have
2914 /// associated device tool chains.
2915 bool isValid() { return !ToolChains.empty(); }
2916
2917 /// Return the associated offload kind.
2918 Action::OffloadKind getAssociatedOffloadKind() {
2919 return AssociatedOffloadKind;
2920 }
2921 };
2922
2923 /// Base class for CUDA/HIP action builder. It injects device code in
2924 /// the host backend action.
2925 class CudaActionBuilderBase : public DeviceActionBuilder {
2926 protected:
2927 /// Flags to signal if the user requested host-only or device-only
2928 /// compilation.
2929 bool CompileHostOnly = false;
2930 bool CompileDeviceOnly = false;
2931 bool EmitLLVM = false;
2932 bool EmitAsm = false;
2933
2934 /// ID to identify each device compilation. For CUDA it is simply the
2935 /// GPU arch string. For HIP it is either the GPU arch string or GPU
2936 /// arch string plus feature strings delimited by a plus sign, e.g.
2937 /// gfx906+xnack.
2938 struct TargetID {
2939 /// Target ID string which is persistent throughout the compilation.
2940 const char *ID;
2941 TargetID(CudaArch Arch) { ID = CudaArchToString(A: Arch); }
2942 TargetID(const char *ID) : ID(ID) {}
2943 operator const char *() { return ID; }
2944 operator StringRef() { return StringRef(ID); }
2945 };
2946 /// List of GPU architectures to use in this compilation.
2947 SmallVector<TargetID, 4> GpuArchList;
2948
2949 /// The CUDA actions for the current input.
2950 ActionList CudaDeviceActions;
2951
2952 /// The CUDA fat binary if it was generated for the current input.
2953 Action *CudaFatBinary = nullptr;
2954
2955 /// Flag that is set to true if this builder acted on the current input.
2956 bool IsActive = false;
2957
2958 /// Flag for -fgpu-rdc.
2959 bool Relocatable = false;
2960
2961 /// Default GPU architecture if there's no one specified.
2962 CudaArch DefaultCudaArch = CudaArch::UNKNOWN;
2963
2964 /// Method to generate compilation unit ID specified by option
2965 /// '-fuse-cuid='.
2966 enum UseCUIDKind { CUID_Hash, CUID_Random, CUID_None, CUID_Invalid };
2967 UseCUIDKind UseCUID = CUID_Hash;
2968
2969 /// Compilation unit ID specified by option '-cuid='.
2970 StringRef FixedCUID;
2971
2972 public:
2973 CudaActionBuilderBase(Compilation &C, DerivedArgList &Args,
2974 const Driver::InputList &Inputs,
2975 Action::OffloadKind OFKind)
2976 : DeviceActionBuilder(C, Args, Inputs, OFKind) {
2977
2978 CompileDeviceOnly = C.getDriver().offloadDeviceOnly();
2979 Relocatable = Args.hasFlag(options::OPT_fgpu_rdc,
2980 options::OPT_fno_gpu_rdc, /*Default=*/false);
2981 }
2982
2983 ActionBuilderReturnCode addDeviceDependences(Action *HostAction) override {
2984 // While generating code for CUDA, we only depend on the host input action
2985 // to trigger the creation of all the CUDA device actions.
2986
2987 // If we are dealing with an input action, replicate it for each GPU
2988 // architecture. If we are in host-only mode we return 'success' so that
2989 // the host uses the CUDA offload kind.
2990 if (auto *IA = dyn_cast<InputAction>(Val: HostAction)) {
2991 assert(!GpuArchList.empty() &&
2992 "We should have at least one GPU architecture.");
2993
2994 // If the host input is not CUDA or HIP, we don't need to bother about
2995 // this input.
2996 if (!(IA->getType() == types::TY_CUDA ||
2997 IA->getType() == types::TY_HIP ||
2998 IA->getType() == types::TY_PP_HIP)) {
2999 // The builder will ignore this input.
3000 IsActive = false;
3001 return ABRT_Inactive;
3002 }
3003
3004 // Set the flag to true, so that the builder acts on the current input.
3005 IsActive = true;
3006
3007 if (CompileHostOnly)
3008 return ABRT_Success;
3009
3010 // Replicate inputs for each GPU architecture.
3011 auto Ty = IA->getType() == types::TY_HIP ? types::TY_HIP_DEVICE
3012 : types::TY_CUDA_DEVICE;
3013 std::string CUID = FixedCUID.str();
3014 if (CUID.empty()) {
3015 if (UseCUID == CUID_Random)
3016 CUID = llvm::utohexstr(X: llvm::sys::Process::GetRandomNumber(),
3017 /*LowerCase=*/true);
3018 else if (UseCUID == CUID_Hash) {
3019 llvm::MD5 Hasher;
3020 llvm::MD5::MD5Result Hash;
3021 SmallString<256> RealPath;
3022 llvm::sys::fs::real_path(path: IA->getInputArg().getValue(), output&: RealPath,
3023 /*expand_tilde=*/true);
3024 Hasher.update(Str: RealPath);
3025 for (auto *A : Args) {
3026 if (A->getOption().matches(options::OPT_INPUT))
3027 continue;
3028 Hasher.update(Str: A->getAsString(Args));
3029 }
3030 Hasher.final(Result&: Hash);
3031 CUID = llvm::utohexstr(X: Hash.low(), /*LowerCase=*/true);
3032 }
3033 }
3034 IA->setId(CUID);
3035
3036 for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) {
3037 CudaDeviceActions.push_back(
3038 Elt: C.MakeAction<InputAction>(Arg: IA->getInputArg(), Arg&: Ty, Arg: IA->getId()));
3039 }
3040
3041 return ABRT_Success;
3042 }
3043
3044 // If this is an unbundling action use it as is for each CUDA toolchain.
3045 if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(Val: HostAction)) {
3046
3047 // If -fgpu-rdc is disabled, should not unbundle since there is no
3048 // device code to link.
3049 if (UA->getType() == types::TY_Object && !Relocatable)
3050 return ABRT_Inactive;
3051
3052 CudaDeviceActions.clear();
3053 auto *IA = cast<InputAction>(Val: UA->getInputs().back());
3054 std::string FileName = IA->getInputArg().getAsString(Args);
3055 // Check if the type of the file is the same as the action. Do not
3056 // unbundle it if it is not. Do not unbundle .so files, for example,
3057 // which are not object files. Files with extension ".lib" is classified
3058 // as TY_Object but they are actually archives, therefore should not be
3059 // unbundled here as objects. They will be handled at other places.
3060 const StringRef LibFileExt = ".lib";
3061 if (IA->getType() == types::TY_Object &&
3062 (!llvm::sys::path::has_extension(path: FileName) ||
3063 types::lookupTypeForExtension(
3064 Ext: llvm::sys::path::extension(path: FileName).drop_front()) !=
3065 types::TY_Object ||
3066 llvm::sys::path::extension(path: FileName) == LibFileExt))
3067 return ABRT_Inactive;
3068
3069 for (auto Arch : GpuArchList) {
3070 CudaDeviceActions.push_back(Elt: UA);
3071 UA->registerDependentActionInfo(TC: ToolChains[0], BoundArch: Arch,
3072 Kind: AssociatedOffloadKind);
3073 }
3074 IsActive = true;
3075 return ABRT_Success;
3076 }
3077
3078 return IsActive ? ABRT_Success : ABRT_Inactive;
3079 }
3080
3081 void appendTopLevelActions(ActionList &AL) override {
3082 // Utility to append actions to the top level list.
3083 auto AddTopLevel = [&](Action *A, TargetID TargetID) {
3084 OffloadAction::DeviceDependences Dep;
3085 Dep.add(A&: *A, TC: *ToolChains.front(), BoundArch: TargetID, OKind: AssociatedOffloadKind);
3086 AL.push_back(Elt: C.MakeAction<OffloadAction>(Arg&: Dep, Arg: A->getType()));
3087 };
3088
3089 // If we have a fat binary, add it to the list.
3090 if (CudaFatBinary) {
3091 AddTopLevel(CudaFatBinary, CudaArch::UNUSED);
3092 CudaDeviceActions.clear();
3093 CudaFatBinary = nullptr;
3094 return;
3095 }
3096
3097 if (CudaDeviceActions.empty())
3098 return;
3099
3100 // If we have CUDA actions at this point, that's because we have a have
3101 // partial compilation, so we should have an action for each GPU
3102 // architecture.
3103 assert(CudaDeviceActions.size() == GpuArchList.size() &&
3104 "Expecting one action per GPU architecture.");
3105 assert(ToolChains.size() == 1 &&
3106 "Expecting to have a single CUDA toolchain.");
3107 for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I)
3108 AddTopLevel(CudaDeviceActions[I], GpuArchList[I]);
3109
3110 CudaDeviceActions.clear();
3111 }
3112
3113 /// Get canonicalized offload arch option. \returns empty StringRef if the
3114 /// option is invalid.
3115 virtual StringRef getCanonicalOffloadArch(StringRef Arch) = 0;
3116
3117 virtual std::optional<std::pair<llvm::StringRef, llvm::StringRef>>
3118 getConflictOffloadArchCombination(const std::set<StringRef> &GpuArchs) = 0;
3119
3120 bool initialize() override {
3121 assert(AssociatedOffloadKind == Action::OFK_Cuda ||
3122 AssociatedOffloadKind == Action::OFK_HIP);
3123
3124 // We don't need to support CUDA.
3125 if (AssociatedOffloadKind == Action::OFK_Cuda &&
3126 !C.hasOffloadToolChain<Action::OFK_Cuda>())
3127 return false;
3128
3129 // We don't need to support HIP.
3130 if (AssociatedOffloadKind == Action::OFK_HIP &&
3131 !C.hasOffloadToolChain<Action::OFK_HIP>())
3132 return false;
3133
3134 const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>();
3135 assert(HostTC && "No toolchain for host compilation.");
3136 if (HostTC->getTriple().isNVPTX() ||
3137 HostTC->getTriple().getArch() == llvm::Triple::amdgcn) {
3138 // We do not support targeting NVPTX/AMDGCN for host compilation. Throw
3139 // an error and abort pipeline construction early so we don't trip
3140 // asserts that assume device-side compilation.
3141 C.getDriver().Diag(diag::err_drv_cuda_host_arch)
3142 << HostTC->getTriple().getArchName();
3143 return true;
3144 }
3145
3146 ToolChains.push_back(
3147 Elt: AssociatedOffloadKind == Action::OFK_Cuda
3148 ? C.getSingleOffloadToolChain<Action::OFK_Cuda>()
3149 : C.getSingleOffloadToolChain<Action::OFK_HIP>());
3150
3151 CompileHostOnly = C.getDriver().offloadHostOnly();
3152 EmitLLVM = Args.getLastArg(options::OPT_emit_llvm);
3153 EmitAsm = Args.getLastArg(options::OPT_S);
3154 FixedCUID = Args.getLastArgValue(options::OPT_cuid_EQ);
3155 if (Arg *A = Args.getLastArg(options::OPT_fuse_cuid_EQ)) {
3156 StringRef UseCUIDStr = A->getValue();
3157 UseCUID = llvm::StringSwitch<UseCUIDKind>(UseCUIDStr)
3158 .Case(S: "hash", Value: CUID_Hash)
3159 .Case(S: "random", Value: CUID_Random)
3160 .Case(S: "none", Value: CUID_None)
3161 .Default(Value: CUID_Invalid);
3162 if (UseCUID == CUID_Invalid) {
3163 C.getDriver().Diag(diag::err_drv_invalid_value)
3164 << A->getAsString(Args) << UseCUIDStr;
3165 C.setContainsError();
3166 return true;
3167 }
3168 }
3169
3170 // --offload and --offload-arch options are mutually exclusive.
3171 if (Args.hasArgNoClaim(options::OPT_offload_EQ) &&
3172 Args.hasArgNoClaim(options::OPT_offload_arch_EQ,
3173 options::OPT_no_offload_arch_EQ)) {
3174 C.getDriver().Diag(diag::err_opt_not_valid_with_opt) << "--offload-arch"
3175 << "--offload";
3176 }
3177
3178 // Collect all offload arch parameters, removing duplicates.
3179 std::set<StringRef> GpuArchs;
3180 bool Error = false;
3181 for (Arg *A : Args) {
3182 if (!(A->getOption().matches(options::OPT_offload_arch_EQ) ||
3183 A->getOption().matches(options::OPT_no_offload_arch_EQ)))
3184 continue;
3185 A->claim();
3186
3187 for (StringRef ArchStr : llvm::split(Str: A->getValue(), Separator: ",")) {
3188 if (A->getOption().matches(options::OPT_no_offload_arch_EQ) &&
3189 ArchStr == "all") {
3190 GpuArchs.clear();
3191 } else if (ArchStr == "native") {
3192 const ToolChain &TC = *ToolChains.front();
3193 auto GPUsOrErr = ToolChains.front()->getSystemGPUArchs(Args);
3194 if (!GPUsOrErr) {
3195 TC.getDriver().Diag(diag::err_drv_undetermined_gpu_arch)
3196 << llvm::Triple::getArchTypeName(TC.getArch())
3197 << llvm::toString(GPUsOrErr.takeError()) << "--offload-arch";
3198 continue;
3199 }
3200
3201 for (auto GPU : *GPUsOrErr) {
3202 GpuArchs.insert(x: Args.MakeArgString(Str: GPU));
3203 }
3204 } else {
3205 ArchStr = getCanonicalOffloadArch(Arch: ArchStr);
3206 if (ArchStr.empty()) {
3207 Error = true;
3208 } else if (A->getOption().matches(options::OPT_offload_arch_EQ))
3209 GpuArchs.insert(x: ArchStr);
3210 else if (A->getOption().matches(options::OPT_no_offload_arch_EQ))
3211 GpuArchs.erase(x: ArchStr);
3212 else
3213 llvm_unreachable("Unexpected option.");
3214 }
3215 }
3216 }
3217
3218 auto &&ConflictingArchs = getConflictOffloadArchCombination(GpuArchs);
3219 if (ConflictingArchs) {
3220 C.getDriver().Diag(clang::diag::err_drv_bad_offload_arch_combo)
3221 << ConflictingArchs->first << ConflictingArchs->second;
3222 C.setContainsError();
3223 return true;
3224 }
3225
3226 // Collect list of GPUs remaining in the set.
3227 for (auto Arch : GpuArchs)
3228 GpuArchList.push_back(Elt: Arch.data());
3229
3230 // Default to sm_20 which is the lowest common denominator for
3231 // supported GPUs. sm_20 code should work correctly, if
3232 // suboptimally, on all newer GPUs.
3233 if (GpuArchList.empty()) {
3234 if (ToolChains.front()->getTriple().isSPIRV())
3235 GpuArchList.push_back(Elt: CudaArch::Generic);
3236 else
3237 GpuArchList.push_back(Elt: DefaultCudaArch);
3238 }
3239
3240 return Error;
3241 }
3242 };
3243
3244 /// \brief CUDA action builder. It injects device code in the host backend
3245 /// action.
3246 class CudaActionBuilder final : public CudaActionBuilderBase {
3247 public:
3248 CudaActionBuilder(Compilation &C, DerivedArgList &Args,
3249 const Driver::InputList &Inputs)
3250 : CudaActionBuilderBase(C, Args, Inputs, Action::OFK_Cuda) {
3251 DefaultCudaArch = CudaArch::CudaDefault;
3252 }
3253
3254 StringRef getCanonicalOffloadArch(StringRef ArchStr) override {
3255 CudaArch Arch = StringToCudaArch(S: ArchStr);
3256 if (Arch == CudaArch::UNKNOWN || !IsNVIDIAGpuArch(A: Arch)) {
3257 C.getDriver().Diag(clang::diag::err_drv_cuda_bad_gpu_arch) << ArchStr;
3258 return StringRef();
3259 }
3260 return CudaArchToString(A: Arch);
3261 }
3262
3263 std::optional<std::pair<llvm::StringRef, llvm::StringRef>>
3264 getConflictOffloadArchCombination(
3265 const std::set<StringRef> &GpuArchs) override {
3266 return std::nullopt;
3267 }
3268
3269 ActionBuilderReturnCode
3270 getDeviceDependences(OffloadAction::DeviceDependences &DA,
3271 phases::ID CurPhase, phases::ID FinalPhase,
3272 PhasesTy &Phases) override {
3273 if (!IsActive)
3274 return ABRT_Inactive;
3275
3276 // If we don't have more CUDA actions, we don't have any dependences to
3277 // create for the host.
3278 if (CudaDeviceActions.empty())
3279 return ABRT_Success;
3280
3281 assert(CudaDeviceActions.size() == GpuArchList.size() &&
3282 "Expecting one action per GPU architecture.");
3283 assert(!CompileHostOnly &&
3284 "Not expecting CUDA actions in host-only compilation.");
3285
3286 // If we are generating code for the device or we are in a backend phase,
3287 // we attempt to generate the fat binary. We compile each arch to ptx and
3288 // assemble to cubin, then feed the cubin *and* the ptx into a device
3289 // "link" action, which uses fatbinary to combine these cubins into one
3290 // fatbin. The fatbin is then an input to the host action if not in
3291 // device-only mode.
3292 if (CompileDeviceOnly || CurPhase == phases::Backend) {
3293 ActionList DeviceActions;
3294 for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) {
3295 // Produce the device action from the current phase up to the assemble
3296 // phase.
3297 for (auto Ph : Phases) {
3298 // Skip the phases that were already dealt with.
3299 if (Ph < CurPhase)
3300 continue;
3301 // We have to be consistent with the host final phase.
3302 if (Ph > FinalPhase)
3303 break;
3304
3305 CudaDeviceActions[I] = C.getDriver().ConstructPhaseAction(
3306 C, Args, Phase: Ph, Input: CudaDeviceActions[I], TargetDeviceOffloadKind: Action::OFK_Cuda);
3307
3308 if (Ph == phases::Assemble)
3309 break;
3310 }
3311
3312 // If we didn't reach the assemble phase, we can't generate the fat
3313 // binary. We don't need to generate the fat binary if we are not in
3314 // device-only mode.
3315 if (!isa<AssembleJobAction>(Val: CudaDeviceActions[I]) ||
3316 CompileDeviceOnly)
3317 continue;
3318
3319 Action *AssembleAction = CudaDeviceActions[I];
3320 assert(AssembleAction->getType() == types::TY_Object);
3321 assert(AssembleAction->getInputs().size() == 1);
3322
3323 Action *BackendAction = AssembleAction->getInputs()[0];
3324 assert(BackendAction->getType() == types::TY_PP_Asm);
3325
3326 for (auto &A : {AssembleAction, BackendAction}) {
3327 OffloadAction::DeviceDependences DDep;
3328 DDep.add(A&: *A, TC: *ToolChains.front(), BoundArch: GpuArchList[I], OKind: Action::OFK_Cuda);
3329 DeviceActions.push_back(
3330 Elt: C.MakeAction<OffloadAction>(Arg&: DDep, Arg: A->getType()));
3331 }
3332 }
3333
3334 // We generate the fat binary if we have device input actions.
3335 if (!DeviceActions.empty()) {
3336 CudaFatBinary =
3337 C.MakeAction<LinkJobAction>(Arg&: DeviceActions, Arg: types::TY_CUDA_FATBIN);
3338
3339 if (!CompileDeviceOnly) {
3340 DA.add(A&: *CudaFatBinary, TC: *ToolChains.front(), /*BoundArch=*/nullptr,
3341 OKind: Action::OFK_Cuda);
3342 // Clear the fat binary, it is already a dependence to an host
3343 // action.
3344 CudaFatBinary = nullptr;
3345 }
3346
3347 // Remove the CUDA actions as they are already connected to an host
3348 // action or fat binary.
3349 CudaDeviceActions.clear();
3350 }
3351
3352 // We avoid creating host action in device-only mode.
3353 return CompileDeviceOnly ? ABRT_Ignore_Host : ABRT_Success;
3354 } else if (CurPhase > phases::Backend) {
3355 // If we are past the backend phase and still have a device action, we
3356 // don't have to do anything as this action is already a device
3357 // top-level action.
3358 return ABRT_Success;
3359 }
3360
3361 assert(CurPhase < phases::Backend && "Generating single CUDA "
3362 "instructions should only occur "
3363 "before the backend phase!");
3364
3365 // By default, we produce an action for each device arch.
3366 for (Action *&A : CudaDeviceActions)
3367 A = C.getDriver().ConstructPhaseAction(C, Args, Phase: CurPhase, Input: A);
3368
3369 return ABRT_Success;
3370 }
3371 };
3372 /// \brief HIP action builder. It injects device code in the host backend
3373 /// action.
3374 class HIPActionBuilder final : public CudaActionBuilderBase {
3375 /// The linker inputs obtained for each device arch.
3376 SmallVector<ActionList, 8> DeviceLinkerInputs;
3377 // The default bundling behavior depends on the type of output, therefore
3378 // BundleOutput needs to be tri-value: None, true, or false.
3379 // Bundle code objects except --no-gpu-output is specified for device
3380 // only compilation. Bundle other type of output files only if
3381 // --gpu-bundle-output is specified for device only compilation.
3382 std::optional<bool> BundleOutput;
3383 std::optional<bool> EmitReloc;
3384
3385 public:
3386 HIPActionBuilder(Compilation &C, DerivedArgList &Args,
3387 const Driver::InputList &Inputs)
3388 : CudaActionBuilderBase(C, Args, Inputs, Action::OFK_HIP) {
3389
3390 DefaultCudaArch = CudaArch::HIPDefault;
3391
3392 if (Args.hasArg(options::OPT_fhip_emit_relocatable,
3393 options::OPT_fno_hip_emit_relocatable)) {
3394 EmitReloc = Args.hasFlag(options::OPT_fhip_emit_relocatable,
3395 options::OPT_fno_hip_emit_relocatable, false);
3396
3397 if (*EmitReloc) {
3398 if (Relocatable) {
3399 C.getDriver().Diag(diag::err_opt_not_valid_with_opt)
3400 << "-fhip-emit-relocatable"
3401 << "-fgpu-rdc";
3402 }
3403
3404 if (!CompileDeviceOnly) {
3405 C.getDriver().Diag(diag::err_opt_not_valid_without_opt)
3406 << "-fhip-emit-relocatable"
3407 << "--cuda-device-only";
3408 }
3409 }
3410 }
3411
3412 if (Args.hasArg(options::OPT_gpu_bundle_output,
3413 options::OPT_no_gpu_bundle_output))
3414 BundleOutput = Args.hasFlag(options::OPT_gpu_bundle_output,
3415 options::OPT_no_gpu_bundle_output, true) &&
3416 (!EmitReloc || !*EmitReloc);
3417 }
3418
3419 bool canUseBundlerUnbundler() const override { return true; }
3420
3421 StringRef getCanonicalOffloadArch(StringRef IdStr) override {
3422 llvm::StringMap<bool> Features;
3423 // getHIPOffloadTargetTriple() is known to return valid value as it has
3424 // been called successfully in the CreateOffloadingDeviceToolChains().
3425 auto ArchStr = parseTargetID(
3426 T: *getHIPOffloadTargetTriple(D: C.getDriver(), Args: C.getInputArgs()), OffloadArch: IdStr,
3427 FeatureMap: &Features);
3428 if (!ArchStr) {
3429 C.getDriver().Diag(clang::diag::err_drv_bad_target_id) << IdStr;
3430 C.setContainsError();
3431 return StringRef();
3432 }
3433 auto CanId = getCanonicalTargetID(Processor: *ArchStr, Features);
3434 return Args.MakeArgStringRef(Str: CanId);
3435 };
3436
3437 std::optional<std::pair<llvm::StringRef, llvm::StringRef>>
3438 getConflictOffloadArchCombination(
3439 const std::set<StringRef> &GpuArchs) override {
3440 return getConflictTargetIDCombination(TargetIDs: GpuArchs);
3441 }
3442
3443 ActionBuilderReturnCode
3444 getDeviceDependences(OffloadAction::DeviceDependences &DA,
3445 phases::ID CurPhase, phases::ID FinalPhase,
3446 PhasesTy &Phases) override {
3447 if (!IsActive)
3448 return ABRT_Inactive;
3449
3450 // amdgcn does not support linking of object files, therefore we skip
3451 // backend and assemble phases to output LLVM IR. Except for generating
3452 // non-relocatable device code, where we generate fat binary for device
3453 // code and pass to host in Backend phase.
3454 if (CudaDeviceActions.empty())
3455 return ABRT_Success;
3456
3457 assert(((CurPhase == phases::Link && Relocatable) ||
3458 CudaDeviceActions.size() == GpuArchList.size()) &&
3459 "Expecting one action per GPU architecture.");
3460 assert(!CompileHostOnly &&
3461 "Not expecting HIP actions in host-only compilation.");
3462
3463 bool ShouldLink = !EmitReloc || !*EmitReloc;
3464
3465 if (!Relocatable && CurPhase == phases::Backend && !EmitLLVM &&
3466 !EmitAsm && ShouldLink) {
3467 // If we are in backend phase, we attempt to generate the fat binary.
3468 // We compile each arch to IR and use a link action to generate code
3469 // object containing ISA. Then we use a special "link" action to create
3470 // a fat binary containing all the code objects for different GPU's.
3471 // The fat binary is then an input to the host action.
3472 for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) {
3473 if (C.getDriver().isUsingLTO(/*IsOffload=*/true)) {
3474 // When LTO is enabled, skip the backend and assemble phases and
3475 // use lld to link the bitcode.
3476 ActionList AL;
3477 AL.push_back(Elt: CudaDeviceActions[I]);
3478 // Create a link action to link device IR with device library
3479 // and generate ISA.
3480 CudaDeviceActions[I] =
3481 C.MakeAction<LinkJobAction>(Arg&: AL, Arg: types::TY_Image);
3482 } else {
3483 // When LTO is not enabled, we follow the conventional
3484 // compiler phases, including backend and assemble phases.
3485 ActionList AL;
3486 Action *BackendAction = nullptr;
3487 if (ToolChains.front()->getTriple().isSPIRV()) {
3488 // Emit LLVM bitcode for SPIR-V targets. SPIR-V device tool chain
3489 // (HIPSPVToolChain) runs post-link LLVM IR passes.
3490 types::ID Output = Args.hasArg(options::OPT_S)
3491 ? types::TY_LLVM_IR
3492 : types::TY_LLVM_BC;
3493 BackendAction =
3494 C.MakeAction<BackendJobAction>(Arg&: CudaDeviceActions[I], Arg&: Output);
3495 } else
3496 BackendAction = C.getDriver().ConstructPhaseAction(
3497 C, Args, Phase: phases::Backend, Input: CudaDeviceActions[I],
3498 TargetDeviceOffloadKind: AssociatedOffloadKind);
3499 auto AssembleAction = C.getDriver().ConstructPhaseAction(
3500 C, Args, Phase: phases::Assemble, Input: BackendAction,
3501 TargetDeviceOffloadKind: AssociatedOffloadKind);
3502 AL.push_back(Elt: AssembleAction);
3503 // Create a link action to link device IR with device library
3504 // and generate ISA.
3505 CudaDeviceActions[I] =
3506 C.MakeAction<LinkJobAction>(Arg&: AL, Arg: types::TY_Image);
3507 }
3508
3509 // OffloadingActionBuilder propagates device arch until an offload
3510 // action. Since the next action for creating fatbin does
3511 // not have device arch, whereas the above link action and its input
3512 // have device arch, an offload action is needed to stop the null
3513 // device arch of the next action being propagated to the above link
3514 // action.
3515 OffloadAction::DeviceDependences DDep;
3516 DDep.add(A&: *CudaDeviceActions[I], TC: *ToolChains.front(), BoundArch: GpuArchList[I],
3517 OKind: AssociatedOffloadKind);
3518 CudaDeviceActions[I] = C.MakeAction<OffloadAction>(
3519 Arg&: DDep, Arg: CudaDeviceActions[I]->getType());
3520 }
3521
3522 if (!CompileDeviceOnly || !BundleOutput || *BundleOutput) {
3523 // Create HIP fat binary with a special "link" action.
3524 CudaFatBinary = C.MakeAction<LinkJobAction>(Arg&: CudaDeviceActions,
3525 Arg: types::TY_HIP_FATBIN);
3526
3527 if (!CompileDeviceOnly) {
3528 DA.add(A&: *CudaFatBinary, TC: *ToolChains.front(), /*BoundArch=*/nullptr,
3529 OKind: AssociatedOffloadKind);
3530 // Clear the fat binary, it is already a dependence to an host
3531 // action.
3532 CudaFatBinary = nullptr;
3533 }
3534
3535 // Remove the CUDA actions as they are already connected to an host
3536 // action or fat binary.
3537 CudaDeviceActions.clear();
3538 }
3539
3540 return CompileDeviceOnly ? ABRT_Ignore_Host : ABRT_Success;
3541 } else if (CurPhase == phases::Link) {
3542 if (!ShouldLink)
3543 return ABRT_Success;
3544 // Save CudaDeviceActions to DeviceLinkerInputs for each GPU subarch.
3545 // This happens to each device action originated from each input file.
3546 // Later on, device actions in DeviceLinkerInputs are used to create
3547 // device link actions in appendLinkDependences and the created device
3548 // link actions are passed to the offload action as device dependence.
3549 DeviceLinkerInputs.resize(N: CudaDeviceActions.size());
3550 auto LI = DeviceLinkerInputs.begin();
3551 for (auto *A : CudaDeviceActions) {
3552 LI->push_back(Elt: A);
3553 ++LI;
3554 }
3555
3556 // We will pass the device action as a host dependence, so we don't
3557 // need to do anything else with them.
3558 CudaDeviceActions.clear();
3559 return CompileDeviceOnly ? ABRT_Ignore_Host : ABRT_Success;
3560 }
3561
3562 // By default, we produce an action for each device arch.
3563 for (Action *&A : CudaDeviceActions)
3564 A = C.getDriver().ConstructPhaseAction(C, Args, Phase: CurPhase, Input: A,
3565 TargetDeviceOffloadKind: AssociatedOffloadKind);
3566
3567 if (CompileDeviceOnly && CurPhase == FinalPhase && BundleOutput &&
3568 *BundleOutput) {
3569 for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) {
3570 OffloadAction::DeviceDependences DDep;
3571 DDep.add(A&: *CudaDeviceActions[I], TC: *ToolChains.front(), BoundArch: GpuArchList[I],
3572 OKind: AssociatedOffloadKind);
3573 CudaDeviceActions[I] = C.MakeAction<OffloadAction>(
3574 Arg&: DDep, Arg: CudaDeviceActions[I]->getType());
3575 }
3576 CudaFatBinary =
3577 C.MakeAction<OffloadBundlingJobAction>(Arg&: CudaDeviceActions);
3578 CudaDeviceActions.clear();
3579 }
3580
3581 return (CompileDeviceOnly &&
3582 (CurPhase == FinalPhase ||
3583 (!ShouldLink && CurPhase == phases::Assemble)))
3584 ? ABRT_Ignore_Host
3585 : ABRT_Success;
3586 }
3587
3588 void appendLinkDeviceActions(ActionList &AL) override {
3589 if (DeviceLinkerInputs.size() == 0)
3590 return;
3591
3592 assert(DeviceLinkerInputs.size() == GpuArchList.size() &&
3593 "Linker inputs and GPU arch list sizes do not match.");
3594
3595 ActionList Actions;
3596 unsigned I = 0;
3597 // Append a new link action for each device.
3598 // Each entry in DeviceLinkerInputs corresponds to a GPU arch.
3599 for (auto &LI : DeviceLinkerInputs) {
3600
3601 types::ID Output = Args.hasArg(options::OPT_emit_llvm)
3602 ? types::TY_LLVM_BC
3603 : types::TY_Image;
3604
3605 auto *DeviceLinkAction = C.MakeAction<LinkJobAction>(Arg&: LI, Arg&: Output);
3606 // Linking all inputs for the current GPU arch.
3607 // LI contains all the inputs for the linker.
3608 OffloadAction::DeviceDependences DeviceLinkDeps;
3609 DeviceLinkDeps.add(A&: *DeviceLinkAction, TC: *ToolChains[0],
3610 BoundArch: GpuArchList[I], OKind: AssociatedOffloadKind);
3611 Actions.push_back(Elt: C.MakeAction<OffloadAction>(
3612 Arg&: DeviceLinkDeps, Arg: DeviceLinkAction->getType()));
3613 ++I;
3614 }
3615 DeviceLinkerInputs.clear();
3616
3617 // If emitting LLVM, do not generate final host/device compilation action
3618 if (Args.hasArg(options::OPT_emit_llvm)) {
3619 AL.append(RHS: Actions);
3620 return;
3621 }
3622
3623 // Create a host object from all the device images by embedding them
3624 // in a fat binary for mixed host-device compilation. For device-only
3625 // compilation, creates a fat binary.
3626 OffloadAction::DeviceDependences DDeps;
3627 if (!CompileDeviceOnly || !BundleOutput || *BundleOutput) {
3628 auto *TopDeviceLinkAction = C.MakeAction<LinkJobAction>(
3629 Arg&: Actions,
3630 Arg: CompileDeviceOnly ? types::TY_HIP_FATBIN : types::TY_Object);
3631 DDeps.add(A&: *TopDeviceLinkAction, TC: *ToolChains[0], BoundArch: nullptr,
3632 OKind: AssociatedOffloadKind);
3633 // Offload the host object to the host linker.
3634 AL.push_back(
3635 Elt: C.MakeAction<OffloadAction>(Arg&: DDeps, Arg: TopDeviceLinkAction->getType()));
3636 } else {
3637 AL.append(RHS: Actions);
3638 }
3639 }
3640
3641 Action* appendLinkHostActions(ActionList &AL) override { return AL.back(); }
3642
3643 void appendLinkDependences(OffloadAction::DeviceDependences &DA) override {}
3644 };
3645
3646 ///
3647 /// TODO: Add the implementation for other specialized builders here.
3648 ///
3649
3650 /// Specialized builders being used by this offloading action builder.
3651 SmallVector<DeviceActionBuilder *, 4> SpecializedBuilders;
3652
3653 /// Flag set to true if all valid builders allow file bundling/unbundling.
3654 bool CanUseBundler;
3655
3656public:
3657 OffloadingActionBuilder(Compilation &C, DerivedArgList &Args,
3658 const Driver::InputList &Inputs)
3659 : C(C) {
3660 // Create a specialized builder for each device toolchain.
3661
3662 IsValid = true;
3663
3664 // Create a specialized builder for CUDA.
3665 SpecializedBuilders.push_back(Elt: new CudaActionBuilder(C, Args, Inputs));
3666
3667 // Create a specialized builder for HIP.
3668 SpecializedBuilders.push_back(Elt: new HIPActionBuilder(C, Args, Inputs));
3669
3670 //
3671 // TODO: Build other specialized builders here.
3672 //
3673
3674 // Initialize all the builders, keeping track of errors. If all valid
3675 // builders agree that we can use bundling, set the flag to true.
3676 unsigned ValidBuilders = 0u;
3677 unsigned ValidBuildersSupportingBundling = 0u;
3678 for (auto *SB : SpecializedBuilders) {
3679 IsValid = IsValid && !SB->initialize();
3680
3681 // Update the counters if the builder is valid.
3682 if (SB->isValid()) {
3683 ++ValidBuilders;
3684 if (SB->canUseBundlerUnbundler())
3685 ++ValidBuildersSupportingBundling;
3686 }
3687 }
3688 CanUseBundler =
3689 ValidBuilders && ValidBuilders == ValidBuildersSupportingBundling;
3690 }
3691
3692 ~OffloadingActionBuilder() {
3693 for (auto *SB : SpecializedBuilders)
3694 delete SB;
3695 }
3696
3697 /// Record a host action and its originating input argument.
3698 void recordHostAction(Action *HostAction, const Arg *InputArg) {
3699 assert(HostAction && "Invalid host action");
3700 assert(InputArg && "Invalid input argument");
3701 auto Loc = HostActionToInputArgMap.find(x: HostAction);
3702 if (Loc == HostActionToInputArgMap.end())
3703 HostActionToInputArgMap[HostAction] = InputArg;
3704 assert(HostActionToInputArgMap[HostAction] == InputArg &&
3705 "host action mapped to multiple input arguments");
3706 }
3707
3708 /// Generate an action that adds device dependences (if any) to a host action.
3709 /// If no device dependence actions exist, just return the host action \a
3710 /// HostAction. If an error is found or if no builder requires the host action
3711 /// to be generated, return nullptr.
3712 Action *
3713 addDeviceDependencesToHostAction(Action *HostAction, const Arg *InputArg,
3714 phases::ID CurPhase, phases::ID FinalPhase,
3715 DeviceActionBuilder::PhasesTy &Phases) {
3716 if (!IsValid)
3717 return nullptr;
3718
3719 if (SpecializedBuilders.empty())
3720 return HostAction;
3721
3722 assert(HostAction && "Invalid host action!");
3723 recordHostAction(HostAction, InputArg);
3724
3725 OffloadAction::DeviceDependences DDeps;
3726 // Check if all the programming models agree we should not emit the host
3727 // action. Also, keep track of the offloading kinds employed.
3728 auto &OffloadKind = InputArgToOffloadKindMap[InputArg];
3729 unsigned InactiveBuilders = 0u;
3730 unsigned IgnoringBuilders = 0u;
3731 for (auto *SB : SpecializedBuilders) {
3732 if (!SB->isValid()) {
3733 ++InactiveBuilders;
3734 continue;
3735 }
3736 auto RetCode =
3737 SB->getDeviceDependences(DA&: DDeps, CurPhase, FinalPhase, Phases);
3738
3739 // If the builder explicitly says the host action should be ignored,
3740 // we need to increment the variable that tracks the builders that request
3741 // the host object to be ignored.
3742 if (RetCode == DeviceActionBuilder::ABRT_Ignore_Host)
3743 ++IgnoringBuilders;
3744
3745 // Unless the builder was inactive for this action, we have to record the
3746 // offload kind because the host will have to use it.
3747 if (RetCode != DeviceActionBuilder::ABRT_Inactive)
3748 OffloadKind |= SB->getAssociatedOffloadKind();
3749 }
3750
3751 // If all builders agree that the host object should be ignored, just return
3752 // nullptr.
3753 if (IgnoringBuilders &&
3754 SpecializedBuilders.size() == (InactiveBuilders + IgnoringBuilders))
3755 return nullptr;
3756
3757 if (DDeps.getActions().empty())
3758 return HostAction;
3759
3760 // We have dependences we need to bundle together. We use an offload action
3761 // for that.
3762 OffloadAction::HostDependence HDep(
3763 *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(),
3764 /*BoundArch=*/nullptr, DDeps);
3765 return C.MakeAction<OffloadAction>(Arg&: HDep, Arg&: DDeps);
3766 }
3767
3768 /// Generate an action that adds a host dependence to a device action. The
3769 /// results will be kept in this action builder. Return true if an error was
3770 /// found.
3771 bool addHostDependenceToDeviceActions(Action *&HostAction,
3772 const Arg *InputArg) {
3773 if (!IsValid)
3774 return true;
3775
3776 recordHostAction(HostAction, InputArg);
3777
3778 // If we are supporting bundling/unbundling and the current action is an
3779 // input action of non-source file, we replace the host action by the
3780 // unbundling action. The bundler tool has the logic to detect if an input
3781 // is a bundle or not and if the input is not a bundle it assumes it is a
3782 // host file. Therefore it is safe to create an unbundling action even if
3783 // the input is not a bundle.
3784 if (CanUseBundler && isa<InputAction>(Val: HostAction) &&
3785 InputArg->getOption().getKind() == llvm::opt::Option::InputClass &&
3786 (!types::isSrcFile(Id: HostAction->getType()) ||
3787 HostAction->getType() == types::TY_PP_HIP)) {
3788 auto UnbundlingHostAction =
3789 C.MakeAction<OffloadUnbundlingJobAction>(Arg&: HostAction);
3790 UnbundlingHostAction->registerDependentActionInfo(
3791 TC: C.getSingleOffloadToolChain<Action::OFK_Host>(),
3792 /*BoundArch=*/StringRef(), Kind: Action::OFK_Host);
3793 HostAction = UnbundlingHostAction;
3794 recordHostAction(HostAction, InputArg);
3795 }
3796
3797 assert(HostAction && "Invalid host action!");
3798
3799 // Register the offload kinds that are used.
3800 auto &OffloadKind = InputArgToOffloadKindMap[InputArg];
3801 for (auto *SB : SpecializedBuilders) {
3802 if (!SB->isValid())
3803 continue;
3804
3805 auto RetCode = SB->addDeviceDependences(HostAction);
3806
3807 // Host dependences for device actions are not compatible with that same
3808 // action being ignored.
3809 assert(RetCode != DeviceActionBuilder::ABRT_Ignore_Host &&
3810 "Host dependence not expected to be ignored.!");
3811
3812 // Unless the builder was inactive for this action, we have to record the
3813 // offload kind because the host will have to use it.
3814 if (RetCode != DeviceActionBuilder::ABRT_Inactive)
3815 OffloadKind |= SB->getAssociatedOffloadKind();
3816 }
3817
3818 // Do not use unbundler if the Host does not depend on device action.
3819 if (OffloadKind == Action::OFK_None && CanUseBundler)
3820 if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(Val: HostAction))
3821 HostAction = UA->getInputs().back();
3822
3823 return false;
3824 }
3825
3826 /// Add the offloading top level actions to the provided action list. This
3827 /// function can replace the host action by a bundling action if the
3828 /// programming models allow it.
3829 bool appendTopLevelActions(ActionList &AL, Action *HostAction,
3830 const Arg *InputArg) {
3831 if (HostAction)
3832 recordHostAction(HostAction, InputArg);
3833
3834 // Get the device actions to be appended.
3835 ActionList OffloadAL;
3836 for (auto *SB : SpecializedBuilders) {
3837 if (!SB->isValid())
3838 continue;
3839 SB->appendTopLevelActions(AL&: OffloadAL);
3840 }
3841
3842 // If we can use the bundler, replace the host action by the bundling one in
3843 // the resulting list. Otherwise, just append the device actions. For
3844 // device only compilation, HostAction is a null pointer, therefore only do
3845 // this when HostAction is not a null pointer.
3846 if (CanUseBundler && HostAction &&
3847 HostAction->getType() != types::TY_Nothing && !OffloadAL.empty()) {
3848 // Add the host action to the list in order to create the bundling action.
3849 OffloadAL.push_back(Elt: HostAction);
3850
3851 // We expect that the host action was just appended to the action list
3852 // before this method was called.
3853 assert(HostAction == AL.back() && "Host action not in the list??");
3854 HostAction = C.MakeAction<OffloadBundlingJobAction>(Arg&: OffloadAL);
3855 recordHostAction(HostAction, InputArg);
3856 AL.back() = HostAction;
3857 } else
3858 AL.append(in_start: OffloadAL.begin(), in_end: OffloadAL.end());
3859
3860 // Propagate to the current host action (if any) the offload information
3861 // associated with the current input.
3862 if (HostAction)
3863 HostAction->propagateHostOffloadInfo(OKinds: InputArgToOffloadKindMap[InputArg],
3864 /*BoundArch=*/OArch: nullptr);
3865 return false;
3866 }
3867
3868 void appendDeviceLinkActions(ActionList &AL) {
3869 for (DeviceActionBuilder *SB : SpecializedBuilders) {
3870 if (!SB->isValid())
3871 continue;
3872 SB->appendLinkDeviceActions(AL);
3873 }
3874 }
3875
3876 Action *makeHostLinkAction() {
3877 // Build a list of device linking actions.
3878 ActionList DeviceAL;
3879 appendDeviceLinkActions(AL&: DeviceAL);
3880 if (DeviceAL.empty())
3881 return nullptr;
3882
3883 // Let builders add host linking actions.
3884 Action* HA = nullptr;
3885 for (DeviceActionBuilder *SB : SpecializedBuilders) {
3886 if (!SB->isValid())
3887 continue;
3888 HA = SB->appendLinkHostActions(AL&: DeviceAL);
3889 // This created host action has no originating input argument, therefore
3890 // needs to set its offloading kind directly.
3891 if (HA)
3892 HA->propagateHostOffloadInfo(OKinds: SB->getAssociatedOffloadKind(),
3893 /*BoundArch=*/OArch: nullptr);
3894 }
3895 return HA;
3896 }
3897
3898 /// Processes the host linker action. This currently consists of replacing it
3899 /// with an offload action if there are device link objects and propagate to
3900 /// the host action all the offload kinds used in the current compilation. The
3901 /// resulting action is returned.
3902 Action *processHostLinkAction(Action *HostAction) {
3903 // Add all the dependences from the device linking actions.
3904 OffloadAction::DeviceDependences DDeps;
3905 for (auto *SB : SpecializedBuilders) {
3906 if (!SB->isValid())
3907 continue;
3908
3909 SB->appendLinkDependences(DA&: DDeps);
3910 }
3911
3912 // Calculate all the offload kinds used in the current compilation.
3913 unsigned ActiveOffloadKinds = 0u;
3914 for (auto &I : InputArgToOffloadKindMap)
3915 ActiveOffloadKinds |= I.second;
3916
3917 // If we don't have device dependencies, we don't have to create an offload
3918 // action.
3919 if (DDeps.getActions().empty()) {
3920 // Set all the active offloading kinds to the link action. Given that it
3921 // is a link action it is assumed to depend on all actions generated so
3922 // far.
3923 HostAction->setHostOffloadInfo(OKinds: ActiveOffloadKinds,
3924 /*BoundArch=*/OArch: nullptr);
3925 // Propagate active offloading kinds for each input to the link action.
3926 // Each input may have different active offloading kind.
3927 for (auto *A : HostAction->inputs()) {
3928 auto ArgLoc = HostActionToInputArgMap.find(x: A);
3929 if (ArgLoc == HostActionToInputArgMap.end())
3930 continue;
3931 auto OFKLoc = InputArgToOffloadKindMap.find(x: ArgLoc->second);
3932 if (OFKLoc == InputArgToOffloadKindMap.end())
3933 continue;
3934 A->propagateHostOffloadInfo(OKinds: OFKLoc->second, /*BoundArch=*/OArch: nullptr);
3935 }
3936 return HostAction;
3937 }
3938
3939 // Create the offload action with all dependences. When an offload action
3940 // is created the kinds are propagated to the host action, so we don't have
3941 // to do that explicitly here.
3942 OffloadAction::HostDependence HDep(
3943 *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(),
3944 /*BoundArch*/ nullptr, ActiveOffloadKinds);
3945 return C.MakeAction<OffloadAction>(Arg&: HDep, Arg&: DDeps);
3946 }
3947};
3948} // anonymous namespace.
3949
3950void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
3951 const InputList &Inputs,
3952 ActionList &Actions) const {
3953
3954 // Ignore /Yc/Yu if both /Yc and /Yu passed but with different filenames.
3955 Arg *YcArg = Args.getLastArg(options::OPT__SLASH_Yc);
3956 Arg *YuArg = Args.getLastArg(options::OPT__SLASH_Yu);
3957 if (YcArg && YuArg && strcmp(s1: YcArg->getValue(), s2: YuArg->getValue()) != 0) {
3958 Diag(clang::diag::warn_drv_ycyu_different_arg_clang_cl);
3959 Args.eraseArg(options::OPT__SLASH_Yc);
3960 Args.eraseArg(options::OPT__SLASH_Yu);
3961 YcArg = YuArg = nullptr;
3962 }
3963 if (YcArg && Inputs.size() > 1) {
3964 Diag(clang::diag::warn_drv_yc_multiple_inputs_clang_cl);
3965 Args.eraseArg(options::OPT__SLASH_Yc);
3966 YcArg = nullptr;
3967 }
3968
3969 Arg *FinalPhaseArg;
3970 phases::ID FinalPhase = getFinalPhase(DAL: Args, FinalPhaseArg: &FinalPhaseArg);
3971
3972 if (FinalPhase == phases::Link) {
3973 if (Args.hasArgNoClaim(options::OPT_hipstdpar)) {
3974 Args.AddFlagArg(nullptr, getOpts().getOption(options::OPT_hip_link));
3975 Args.AddFlagArg(nullptr,
3976 getOpts().getOption(options::OPT_frtlib_add_rpath));
3977 }
3978 // Emitting LLVM while linking disabled except in HIPAMD Toolchain
3979 if (Args.hasArg(options::OPT_emit_llvm) && !Args.hasArg(options::OPT_hip_link))
3980 Diag(clang::diag::err_drv_emit_llvm_link);
3981 if (IsCLMode() && LTOMode != LTOK_None &&
3982 !Args.getLastArgValue(options::OPT_fuse_ld_EQ)
3983 .equals_insensitive("lld"))
3984 Diag(clang::diag::err_drv_lto_without_lld);
3985
3986 // If -dumpdir is not specified, give a default prefix derived from the link
3987 // output filename. For example, `clang -g -gsplit-dwarf a.c -o x` passes
3988 // `-dumpdir x-` to cc1. If -o is unspecified, use
3989 // stem(getDefaultImageName()) (usually stem("a.out") = "a").
3990 if (!Args.hasArg(options::OPT_dumpdir)) {
3991 Arg *FinalOutput = Args.getLastArg(options::OPT_o, options::OPT__SLASH_o);
3992 Arg *Arg = Args.MakeSeparateArg(
3993 nullptr, getOpts().getOption(options::OPT_dumpdir),
3994 Args.MakeArgString(
3995 (FinalOutput ? FinalOutput->getValue()
3996 : llvm::sys::path::stem(getDefaultImageName())) +
3997 "-"));
3998 Arg->claim();
3999 Args.append(A: Arg);
4000 }
4001 }
4002
4003 if (FinalPhase == phases::Preprocess || Args.hasArg(options::OPT__SLASH_Y_)) {
4004 // If only preprocessing or /Y- is used, all pch handling is disabled.
4005 // Rather than check for it everywhere, just remove clang-cl pch-related
4006 // flags here.
4007 Args.eraseArg(options::OPT__SLASH_Fp);
4008 Args.eraseArg(options::OPT__SLASH_Yc);
4009 Args.eraseArg(options::OPT__SLASH_Yu);
4010 YcArg = YuArg = nullptr;
4011 }
4012
4013 unsigned LastPLSize = 0;
4014 for (auto &I : Inputs) {
4015 types::ID InputType = I.first;
4016 const Arg *InputArg = I.second;
4017
4018 auto PL = types::getCompilationPhases(Id: InputType);
4019 LastPLSize = PL.size();
4020
4021 // If the first step comes after the final phase we are doing as part of
4022 // this compilation, warn the user about it.
4023 phases::ID InitialPhase = PL[0];
4024 if (InitialPhase > FinalPhase) {
4025 if (InputArg->isClaimed())
4026 continue;
4027
4028 // Claim here to avoid the more general unused warning.
4029 InputArg->claim();
4030
4031 // Suppress all unused style warnings with -Qunused-arguments
4032 if (Args.hasArg(options::OPT_Qunused_arguments))
4033 continue;
4034
4035 // Special case when final phase determined by binary name, rather than
4036 // by a command-line argument with a corresponding Arg.
4037 if (CCCIsCPP())
4038 Diag(clang::diag::warn_drv_input_file_unused_by_cpp)
4039 << InputArg->getAsString(Args) << getPhaseName(InitialPhase);
4040 // Special case '-E' warning on a previously preprocessed file to make
4041 // more sense.
4042 else if (InitialPhase == phases::Compile &&
4043 (Args.getLastArg(options::OPT__SLASH_EP,
4044 options::OPT__SLASH_P) ||
4045 Args.getLastArg(options::OPT_E) ||
4046 Args.getLastArg(options::OPT_M, options::OPT_MM)) &&
4047 getPreprocessedType(InputType) == types::TY_INVALID)
4048 Diag(clang::diag::warn_drv_preprocessed_input_file_unused)
4049 << InputArg->getAsString(Args) << !!FinalPhaseArg
4050 << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : "");
4051 else
4052 Diag(clang::diag::warn_drv_input_file_unused)
4053 << InputArg->getAsString(Args) << getPhaseName(InitialPhase)
4054 << !!FinalPhaseArg
4055 << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : "");
4056 continue;
4057 }
4058
4059 if (YcArg) {
4060 // Add a separate precompile phase for the compile phase.
4061 if (FinalPhase >= phases::Compile) {
4062 const types::ID HeaderType = lookupHeaderTypeForSourceType(Id: InputType);
4063 // Build the pipeline for the pch file.
4064 Action *ClangClPch = C.MakeAction<InputAction>(Arg: *InputArg, Arg: HeaderType);
4065 for (phases::ID Phase : types::getCompilationPhases(Id: HeaderType))
4066 ClangClPch = ConstructPhaseAction(C, Args, Phase, Input: ClangClPch);
4067 assert(ClangClPch);
4068 Actions.push_back(Elt: ClangClPch);
4069 // The driver currently exits after the first failed command. This
4070 // relies on that behavior, to make sure if the pch generation fails,
4071 // the main compilation won't run.
4072 // FIXME: If the main compilation fails, the PCH generation should
4073 // probably not be considered successful either.
4074 }
4075 }
4076 }
4077
4078 // If we are linking, claim any options which are obviously only used for
4079 // compilation.
4080 // FIXME: Understand why the last Phase List length is used here.
4081 if (FinalPhase == phases::Link && LastPLSize == 1) {
4082 Args.ClaimAllArgs(options::OPT_CompileOnly_Group);
4083 Args.ClaimAllArgs(options::OPT_cl_compile_Group);
4084 }
4085}
4086
4087void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
4088 const InputList &Inputs, ActionList &Actions) const {
4089 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
4090
4091 if (!SuppressMissingInputWarning && Inputs.empty()) {
4092 Diag(clang::diag::err_drv_no_input_files);
4093 return;
4094 }
4095
4096 // Diagnose misuse of /Fo.
4097 if (Arg *A = Args.getLastArg(options::OPT__SLASH_Fo)) {
4098 StringRef V = A->getValue();
4099 if (Inputs.size() > 1 && !V.empty() &&
4100 !llvm::sys::path::is_separator(value: V.back())) {
4101 // Check whether /Fo tries to name an output file for multiple inputs.
4102 Diag(clang::diag::err_drv_out_file_argument_with_multiple_sources)
4103 << A->getSpelling() << V;
4104 Args.eraseArg(options::OPT__SLASH_Fo);
4105 }
4106 }
4107
4108 // Diagnose misuse of /Fa.
4109 if (Arg *A = Args.getLastArg(options::OPT__SLASH_Fa)) {
4110 StringRef V = A->getValue();
4111 if (Inputs.size() > 1 && !V.empty() &&
4112 !llvm::sys::path::is_separator(value: V.back())) {
4113 // Check whether /Fa tries to name an asm file for multiple inputs.
4114 Diag(clang::diag::err_drv_out_file_argument_with_multiple_sources)
4115 << A->getSpelling() << V;
4116 Args.eraseArg(options::OPT__SLASH_Fa);
4117 }
4118 }
4119
4120 // Diagnose misuse of /o.
4121 if (Arg *A = Args.getLastArg(options::OPT__SLASH_o)) {
4122 if (A->getValue()[0] == '\0') {
4123 // It has to have a value.
4124 Diag(clang::diag::err_drv_missing_argument) << A->getSpelling() << 1;
4125 Args.eraseArg(options::OPT__SLASH_o);
4126 }
4127 }
4128
4129 handleArguments(C, Args, Inputs, Actions);
4130
4131 bool UseNewOffloadingDriver =
4132 C.isOffloadingHostKind(Action::OFK_OpenMP) ||
4133 Args.hasFlag(options::OPT_offload_new_driver,
4134 options::OPT_no_offload_new_driver, false);
4135
4136 // Builder to be used to build offloading actions.
4137 std::unique_ptr<OffloadingActionBuilder> OffloadBuilder =
4138 !UseNewOffloadingDriver
4139 ? std::make_unique<OffloadingActionBuilder>(args&: C, args&: Args, args: Inputs)
4140 : nullptr;
4141
4142 // Construct the actions to perform.
4143 ExtractAPIJobAction *ExtractAPIAction = nullptr;
4144 ActionList LinkerInputs;
4145 ActionList MergerInputs;
4146
4147 for (auto &I : Inputs) {
4148 types::ID InputType = I.first;
4149 const Arg *InputArg = I.second;
4150
4151 auto PL = types::getCompilationPhases(Driver: *this, DAL&: Args, Id: InputType);
4152 if (PL.empty())
4153 continue;
4154
4155 auto FullPL = types::getCompilationPhases(Id: InputType);
4156
4157 // Build the pipeline for this file.
4158 Action *Current = C.MakeAction<InputAction>(Arg: *InputArg, Arg&: InputType);
4159
4160 // Use the current host action in any of the offloading actions, if
4161 // required.
4162 if (!UseNewOffloadingDriver)
4163 if (OffloadBuilder->addHostDependenceToDeviceActions(HostAction&: Current, InputArg))
4164 break;
4165
4166 for (phases::ID Phase : PL) {
4167
4168 // Add any offload action the host action depends on.
4169 if (!UseNewOffloadingDriver)
4170 Current = OffloadBuilder->addDeviceDependencesToHostAction(
4171 HostAction: Current, InputArg, CurPhase: Phase, FinalPhase: PL.back(), Phases: FullPL);
4172 if (!Current)
4173 break;
4174
4175 // Queue linker inputs.
4176 if (Phase == phases::Link) {
4177 assert(Phase == PL.back() && "linking must be final compilation step.");
4178 // We don't need to generate additional link commands if emitting AMD
4179 // bitcode or compiling only for the offload device
4180 if (!(C.getInputArgs().hasArg(options::OPT_hip_link) &&
4181 (C.getInputArgs().hasArg(options::OPT_emit_llvm))) &&
4182 !offloadDeviceOnly())
4183 LinkerInputs.push_back(Elt: Current);
4184 Current = nullptr;
4185 break;
4186 }
4187
4188 // TODO: Consider removing this because the merged may not end up being
4189 // the final Phase in the pipeline. Perhaps the merged could just merge
4190 // and then pass an artifact of some sort to the Link Phase.
4191 // Queue merger inputs.
4192 if (Phase == phases::IfsMerge) {
4193 assert(Phase == PL.back() && "merging must be final compilation step.");
4194 MergerInputs.push_back(Elt: Current);
4195 Current = nullptr;
4196 break;
4197 }
4198
4199 if (Phase == phases::Precompile && ExtractAPIAction) {
4200 ExtractAPIAction->addHeaderInput(Input: Current);
4201 Current = nullptr;
4202 break;
4203 }
4204
4205 // FIXME: Should we include any prior module file outputs as inputs of
4206 // later actions in the same command line?
4207
4208 // Otherwise construct the appropriate action.
4209 Action *NewCurrent = ConstructPhaseAction(C, Args, Phase, Input: Current);
4210
4211 // We didn't create a new action, so we will just move to the next phase.
4212 if (NewCurrent == Current)
4213 continue;
4214
4215 if (auto *EAA = dyn_cast<ExtractAPIJobAction>(Val: NewCurrent))
4216 ExtractAPIAction = EAA;
4217
4218 Current = NewCurrent;
4219
4220 // Try to build the offloading actions and add the result as a dependency
4221 // to the host.
4222 if (UseNewOffloadingDriver)
4223 Current = BuildOffloadingActions(C, Args, Input: I, HostAction: Current);
4224 // Use the current host action in any of the offloading actions, if
4225 // required.
4226 else if (OffloadBuilder->addHostDependenceToDeviceActions(HostAction&: Current,
4227 InputArg))
4228 break;
4229
4230 if (Current->getType() == types::TY_Nothing)
4231 break;
4232 }
4233
4234 // If we ended with something, add to the output list.
4235 if (Current)
4236 Actions.push_back(Elt: Current);
4237
4238 // Add any top level actions generated for offloading.
4239 if (!UseNewOffloadingDriver)
4240 OffloadBuilder->appendTopLevelActions(AL&: Actions, HostAction: Current, InputArg);
4241 else if (Current)
4242 Current->propagateHostOffloadInfo(OKinds: C.getActiveOffloadKinds(),
4243 /*BoundArch=*/OArch: nullptr);
4244 }
4245
4246 // Add a link action if necessary.
4247
4248 if (LinkerInputs.empty()) {
4249 Arg *FinalPhaseArg;
4250 if (getFinalPhase(DAL: Args, FinalPhaseArg: &FinalPhaseArg) == phases::Link)
4251 if (!UseNewOffloadingDriver)
4252 OffloadBuilder->appendDeviceLinkActions(AL&: Actions);
4253 }
4254
4255 if (!LinkerInputs.empty()) {
4256 if (!UseNewOffloadingDriver)
4257 if (Action *Wrapper = OffloadBuilder->makeHostLinkAction())
4258 LinkerInputs.push_back(Elt: Wrapper);
4259 Action *LA;
4260 // Check if this Linker Job should emit a static library.
4261 if (ShouldEmitStaticLibrary(Args)) {
4262 LA = C.MakeAction<StaticLibJobAction>(Arg&: LinkerInputs, Arg: types::TY_Image);
4263 } else if (UseNewOffloadingDriver ||
4264 Args.hasArg(options::OPT_offload_link)) {
4265 LA = C.MakeAction<LinkerWrapperJobAction>(Arg&: LinkerInputs, Arg: types::TY_Image);
4266 LA->propagateHostOffloadInfo(OKinds: C.getActiveOffloadKinds(),
4267 /*BoundArch=*/OArch: nullptr);
4268 } else {
4269 LA = C.MakeAction<LinkJobAction>(Arg&: LinkerInputs, Arg: types::TY_Image);
4270 }
4271 if (!UseNewOffloadingDriver)
4272 LA = OffloadBuilder->processHostLinkAction(HostAction: LA);
4273 Actions.push_back(Elt: LA);
4274 }
4275
4276 // Add an interface stubs merge action if necessary.
4277 if (!MergerInputs.empty())
4278 Actions.push_back(
4279 Elt: C.MakeAction<IfsMergeJobAction>(Arg&: MergerInputs, Arg: types::TY_Image));
4280
4281 if (Args.hasArg(options::OPT_emit_interface_stubs)) {
4282 auto PhaseList = types::getCompilationPhases(
4283 types::TY_IFS_CPP,
4284 Args.hasArg(options::OPT_c) ? phases::Compile : phases::IfsMerge);
4285
4286 ActionList MergerInputs;
4287
4288 for (auto &I : Inputs) {
4289 types::ID InputType = I.first;
4290 const Arg *InputArg = I.second;
4291
4292 // Currently clang and the llvm assembler do not support generating symbol
4293 // stubs from assembly, so we skip the input on asm files. For ifs files
4294 // we rely on the normal pipeline setup in the pipeline setup code above.
4295 if (InputType == types::TY_IFS || InputType == types::TY_PP_Asm ||
4296 InputType == types::TY_Asm)
4297 continue;
4298
4299 Action *Current = C.MakeAction<InputAction>(Arg: *InputArg, Arg&: InputType);
4300
4301 for (auto Phase : PhaseList) {
4302 switch (Phase) {
4303 default:
4304 llvm_unreachable(
4305 "IFS Pipeline can only consist of Compile followed by IfsMerge.");
4306 case phases::Compile: {
4307 // Only IfsMerge (llvm-ifs) can handle .o files by looking for ifs
4308 // files where the .o file is located. The compile action can not
4309 // handle this.
4310 if (InputType == types::TY_Object)
4311 break;
4312
4313 Current = C.MakeAction<CompileJobAction>(Current, types::TY_IFS_CPP);
4314 break;
4315 }
4316 case phases::IfsMerge: {
4317 assert(Phase == PhaseList.back() &&
4318 "merging must be final compilation step.");
4319 MergerInputs.push_back(Current);
4320 Current = nullptr;
4321 break;
4322 }
4323 }
4324 }
4325
4326 // If we ended with something, add to the output list.
4327 if (Current)
4328 Actions.push_back(Elt: Current);
4329 }
4330
4331 // Add an interface stubs merge action if necessary.
4332 if (!MergerInputs.empty())
4333 Actions.push_back(
4334 Elt: C.MakeAction<IfsMergeJobAction>(Arg&: MergerInputs, Arg: types::TY_Image));
4335 }
4336
4337 for (auto Opt : {options::OPT_print_supported_cpus,
4338 options::OPT_print_supported_extensions}) {
4339 // If --print-supported-cpus, -mcpu=? or -mtune=? is specified, build a
4340 // custom Compile phase that prints out supported cpu models and quits.
4341 //
4342 // If --print-supported-extensions is specified, call the helper function
4343 // RISCVMarchHelp in RISCVISAInfo.cpp that prints out supported extensions
4344 // and quits.
4345 if (Arg *A = Args.getLastArg(Opt)) {
4346 if (Opt == options::OPT_print_supported_extensions &&
4347 !C.getDefaultToolChain().getTriple().isRISCV() &&
4348 !C.getDefaultToolChain().getTriple().isAArch64() &&
4349 !C.getDefaultToolChain().getTriple().isARM()) {
4350 C.getDriver().Diag(diag::err_opt_not_valid_on_target)
4351 << "--print-supported-extensions";
4352 return;
4353 }
4354
4355 // Use the -mcpu=? flag as the dummy input to cc1.
4356 Actions.clear();
4357 Action *InputAc = C.MakeAction<InputAction>(*A, types::TY_C);
4358 Actions.push_back(
4359 C.MakeAction<PrecompileJobAction>(InputAc, types::TY_Nothing));
4360 for (auto &I : Inputs)
4361 I.second->claim();
4362 }
4363 }
4364
4365 // Call validator for dxil when -Vd not in Args.
4366 if (C.getDefaultToolChain().getTriple().isDXIL()) {
4367 // Only add action when needValidation.
4368 const auto &TC =
4369 static_cast<const toolchains::HLSLToolChain &>(C.getDefaultToolChain());
4370 if (TC.requiresValidation(Args)) {
4371 Action *LastAction = Actions.back();
4372 Actions.push_back(Elt: C.MakeAction<BinaryAnalyzeJobAction>(
4373 Arg&: LastAction, Arg: types::TY_DX_CONTAINER));
4374 }
4375 }
4376
4377 // Claim ignored clang-cl options.
4378 Args.ClaimAllArgs(options::OPT_cl_ignored_Group);
4379}
4380
4381/// Returns the canonical name for the offloading architecture when using a HIP
4382/// or CUDA architecture.
4383static StringRef getCanonicalArchString(Compilation &C,
4384 const llvm::opt::DerivedArgList &Args,
4385 StringRef ArchStr,
4386 const llvm::Triple &Triple,
4387 bool SuppressError = false) {
4388 // Lookup the CUDA / HIP architecture string. Only report an error if we were
4389 // expecting the triple to be only NVPTX / AMDGPU.
4390 CudaArch Arch = StringToCudaArch(S: getProcessorFromTargetID(T: Triple, OffloadArch: ArchStr));
4391 if (!SuppressError && Triple.isNVPTX() &&
4392 (Arch == CudaArch::UNKNOWN || !IsNVIDIAGpuArch(A: Arch))) {
4393 C.getDriver().Diag(clang::diag::err_drv_offload_bad_gpu_arch)
4394 << "CUDA" << ArchStr;
4395 return StringRef();
4396 } else if (!SuppressError && Triple.isAMDGPU() &&
4397 (Arch == CudaArch::UNKNOWN || !IsAMDGpuArch(A: Arch))) {
4398 C.getDriver().Diag(clang::diag::err_drv_offload_bad_gpu_arch)
4399 << "HIP" << ArchStr;
4400 return StringRef();
4401 }
4402
4403 if (IsNVIDIAGpuArch(A: Arch))
4404 return Args.MakeArgStringRef(Str: CudaArchToString(A: Arch));
4405
4406 if (IsAMDGpuArch(A: Arch)) {
4407 llvm::StringMap<bool> Features;
4408 auto HIPTriple = getHIPOffloadTargetTriple(D: C.getDriver(), Args: C.getInputArgs());
4409 if (!HIPTriple)
4410 return StringRef();
4411 auto Arch = parseTargetID(T: *HIPTriple, OffloadArch: ArchStr, FeatureMap: &Features);
4412 if (!Arch) {
4413 C.getDriver().Diag(clang::diag::err_drv_bad_target_id) << ArchStr;
4414 C.setContainsError();
4415 return StringRef();
4416 }
4417 return Args.MakeArgStringRef(Str: getCanonicalTargetID(Processor: *Arch, Features));
4418 }
4419
4420 // If the input isn't CUDA or HIP just return the architecture.
4421 return ArchStr;
4422}
4423
4424/// Checks if the set offloading architectures does not conflict. Returns the
4425/// incompatible pair if a conflict occurs.
4426static std::optional<std::pair<llvm::StringRef, llvm::StringRef>>
4427getConflictOffloadArchCombination(const llvm::DenseSet<StringRef> &Archs,
4428 llvm::Triple Triple) {
4429 if (!Triple.isAMDGPU())
4430 return std::nullopt;
4431
4432 std::set<StringRef> ArchSet;
4433 llvm::copy(Range: Archs, Out: std::inserter(x&: ArchSet, i: ArchSet.begin()));
4434 return getConflictTargetIDCombination(TargetIDs: ArchSet);
4435}
4436
4437llvm::DenseSet<StringRef>
4438Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args,
4439 Action::OffloadKind Kind, const ToolChain *TC,
4440 bool SuppressError) const {
4441 if (!TC)
4442 TC = &C.getDefaultToolChain();
4443
4444 // --offload and --offload-arch options are mutually exclusive.
4445 if (Args.hasArgNoClaim(options::OPT_offload_EQ) &&
4446 Args.hasArgNoClaim(options::OPT_offload_arch_EQ,
4447 options::OPT_no_offload_arch_EQ)) {
4448 C.getDriver().Diag(diag::err_opt_not_valid_with_opt)
4449 << "--offload"
4450 << (Args.hasArgNoClaim(options::OPT_offload_arch_EQ)
4451 ? "--offload-arch"
4452 : "--no-offload-arch");
4453 }
4454
4455 if (KnownArchs.contains(Val: TC))
4456 return KnownArchs.lookup(Val: TC);
4457
4458 llvm::DenseSet<StringRef> Archs;
4459 for (auto *Arg : Args) {
4460 // Extract any '--[no-]offload-arch' arguments intended for this toolchain.
4461 std::unique_ptr<llvm::opt::Arg> ExtractedArg = nullptr;
4462 if (Arg->getOption().matches(options::OPT_Xopenmp_target_EQ) &&
4463 ToolChain::getOpenMPTriple(Arg->getValue(0)) == TC->getTriple()) {
4464 Arg->claim();
4465 unsigned Index = Args.getBaseArgs().MakeIndex(String0: Arg->getValue(N: 1));
4466 ExtractedArg = getOpts().ParseOneArg(Args, Index);
4467 Arg = ExtractedArg.get();
4468 }
4469
4470 // Add or remove the seen architectures in order of appearance. If an
4471 // invalid architecture is given we simply exit.
4472 if (Arg->getOption().matches(options::OPT_offload_arch_EQ)) {
4473 for (StringRef Arch : llvm::split(Str: Arg->getValue(), Separator: ",")) {
4474 if (Arch == "native" || Arch.empty()) {
4475 auto GPUsOrErr = TC->getSystemGPUArchs(Args);
4476 if (!GPUsOrErr) {
4477 if (SuppressError)
4478 llvm::consumeError(Err: GPUsOrErr.takeError());
4479 else
4480 TC->getDriver().Diag(diag::err_drv_undetermined_gpu_arch)
4481 << llvm::Triple::getArchTypeName(TC->getArch())
4482 << llvm::toString(GPUsOrErr.takeError()) << "--offload-arch";
4483 continue;
4484 }
4485
4486 for (auto ArchStr : *GPUsOrErr) {
4487 Archs.insert(
4488 V: getCanonicalArchString(C, Args, ArchStr: Args.MakeArgString(Str: ArchStr),
4489 Triple: TC->getTriple(), SuppressError));
4490 }
4491 } else {
4492 StringRef ArchStr = getCanonicalArchString(
4493 C, Args, ArchStr: Arch, Triple: TC->getTriple(), SuppressError);
4494 if (ArchStr.empty())
4495 return Archs;
4496 Archs.insert(V: ArchStr);
4497 }
4498 }
4499 } else if (Arg->getOption().matches(options::OPT_no_offload_arch_EQ)) {
4500 for (StringRef Arch : llvm::split(Str: Arg->getValue(), Separator: ",")) {
4501 if (Arch == "all") {
4502 Archs.clear();
4503 } else {
4504 StringRef ArchStr = getCanonicalArchString(
4505 C, Args, ArchStr: Arch, Triple: TC->getTriple(), SuppressError);
4506 if (ArchStr.empty())
4507 return Archs;
4508 Archs.erase(V: ArchStr);
4509 }
4510 }
4511 }
4512 }
4513
4514 if (auto ConflictingArchs =
4515 getConflictOffloadArchCombination(Archs, Triple: TC->getTriple())) {
4516 C.getDriver().Diag(clang::diag::err_drv_bad_offload_arch_combo)
4517 << ConflictingArchs->first << ConflictingArchs->second;
4518 C.setContainsError();
4519 }
4520
4521 // Skip filling defaults if we're just querying what is availible.
4522 if (SuppressError)
4523 return Archs;
4524
4525 if (Archs.empty()) {
4526 if (Kind == Action::OFK_Cuda)
4527 Archs.insert(V: CudaArchToString(A: CudaArch::CudaDefault));
4528 else if (Kind == Action::OFK_HIP)
4529 Archs.insert(V: CudaArchToString(A: CudaArch::HIPDefault));
4530 else if (Kind == Action::OFK_OpenMP)
4531 Archs.insert(V: StringRef());
4532 } else {
4533 Args.ClaimAllArgs(options::OPT_offload_arch_EQ);
4534 Args.ClaimAllArgs(options::OPT_no_offload_arch_EQ);
4535 }
4536
4537 return Archs;
4538}
4539
4540Action *Driver::BuildOffloadingActions(Compilation &C,
4541 llvm::opt::DerivedArgList &Args,
4542 const InputTy &Input,
4543 Action *HostAction) const {
4544 // Don't build offloading actions if explicitly disabled or we do not have a
4545 // valid source input and compile action to embed it in. If preprocessing only
4546 // ignore embedding.
4547 if (offloadHostOnly() || !types::isSrcFile(Id: Input.first) ||
4548 !(isa<CompileJobAction>(Val: HostAction) ||
4549 getFinalPhase(DAL: Args) == phases::Preprocess))
4550 return HostAction;
4551
4552 ActionList OffloadActions;
4553 OffloadAction::DeviceDependences DDeps;
4554
4555 const Action::OffloadKind OffloadKinds[] = {
4556 Action::OFK_OpenMP, Action::OFK_Cuda, Action::OFK_HIP};
4557
4558 for (Action::OffloadKind Kind : OffloadKinds) {
4559 SmallVector<const ToolChain *, 2> ToolChains;
4560 ActionList DeviceActions;
4561
4562 auto TCRange = C.getOffloadToolChains(Kind);
4563 for (auto TI = TCRange.first, TE = TCRange.second; TI != TE; ++TI)
4564 ToolChains.push_back(Elt: TI->second);
4565
4566 if (ToolChains.empty())
4567 continue;
4568
4569 types::ID InputType = Input.first;
4570 const Arg *InputArg = Input.second;
4571
4572 // The toolchain can be active for unsupported file types.
4573 if ((Kind == Action::OFK_Cuda && !types::isCuda(Id: InputType)) ||
4574 (Kind == Action::OFK_HIP && !types::isHIP(Id: InputType)))
4575 continue;
4576
4577 // Get the product of all bound architectures and toolchains.
4578 SmallVector<std::pair<const ToolChain *, StringRef>> TCAndArchs;
4579 for (const ToolChain *TC : ToolChains)
4580 for (StringRef Arch : getOffloadArchs(C, Args, Kind, TC))
4581 TCAndArchs.push_back(Elt: std::make_pair(x&: TC, y&: Arch));
4582
4583 for (unsigned I = 0, E = TCAndArchs.size(); I != E; ++I)
4584 DeviceActions.push_back(Elt: C.MakeAction<InputAction>(Arg: *InputArg, Arg&: InputType));
4585
4586 if (DeviceActions.empty())
4587 return HostAction;
4588
4589 auto PL = types::getCompilationPhases(Driver: *this, DAL&: Args, Id: InputType);
4590
4591 for (phases::ID Phase : PL) {
4592 if (Phase == phases::Link) {
4593 assert(Phase == PL.back() && "linking must be final compilation step.");
4594 break;
4595 }
4596
4597 auto TCAndArch = TCAndArchs.begin();
4598 for (Action *&A : DeviceActions) {
4599 if (A->getType() == types::TY_Nothing)
4600 continue;
4601
4602 // Propagate the ToolChain so we can use it in ConstructPhaseAction.
4603 A->propagateDeviceOffloadInfo(OKind: Kind, OArch: TCAndArch->second.data(),
4604 OToolChain: TCAndArch->first);
4605 A = ConstructPhaseAction(C, Args, Phase, Input: A, TargetDeviceOffloadKind: Kind);
4606
4607 if (isa<CompileJobAction>(Val: A) && isa<CompileJobAction>(Val: HostAction) &&
4608 Kind == Action::OFK_OpenMP &&
4609 HostAction->getType() != types::TY_Nothing) {
4610 // OpenMP offloading has a dependency on the host compile action to
4611 // identify which declarations need to be emitted. This shouldn't be
4612 // collapsed with any other actions so we can use it in the device.
4613 HostAction->setCannotBeCollapsedWithNextDependentAction();
4614 OffloadAction::HostDependence HDep(
4615 *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(),
4616 TCAndArch->second.data(), Kind);
4617 OffloadAction::DeviceDependences DDep;
4618 DDep.add(A&: *A, TC: *TCAndArch->first, BoundArch: TCAndArch->second.data(), OKind: Kind);
4619 A = C.MakeAction<OffloadAction>(Arg&: HDep, Arg&: DDep);
4620 }
4621
4622 ++TCAndArch;
4623 }
4624 }
4625
4626 // Compiling HIP in non-RDC mode requires linking each action individually.
4627 for (Action *&A : DeviceActions) {
4628 if ((A->getType() != types::TY_Object &&
4629 A->getType() != types::TY_LTO_BC) ||
4630 Kind != Action::OFK_HIP ||
4631 Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false))
4632 continue;
4633 ActionList LinkerInput = {A};
4634 A = C.MakeAction<LinkJobAction>(Arg&: LinkerInput, Arg: types::TY_Image);
4635 }
4636
4637 auto TCAndArch = TCAndArchs.begin();
4638 for (Action *A : DeviceActions) {
4639 DDeps.add(A&: *A, TC: *TCAndArch->first, BoundArch: TCAndArch->second.data(), OKind: Kind);
4640 OffloadAction::DeviceDependences DDep;
4641 DDep.add(A&: *A, TC: *TCAndArch->first, BoundArch: TCAndArch->second.data(), OKind: Kind);
4642
4643 // Compiling CUDA in non-RDC mode uses the PTX output if available.
4644 for (Action *Input : A->getInputs())
4645 if (Kind == Action::OFK_Cuda && A->getType() == types::TY_Object &&
4646 !Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
4647 false))
4648 DDep.add(A&: *Input, TC: *TCAndArch->first, BoundArch: TCAndArch->second.data(), OKind: Kind);
4649 OffloadActions.push_back(Elt: C.MakeAction<OffloadAction>(Arg&: DDep, Arg: A->getType()));
4650
4651 ++TCAndArch;
4652 }
4653 }
4654
4655 // HIP code in non-RDC mode will bundle the output if it invoked the linker.
4656 bool ShouldBundleHIP =
4657 C.isOffloadingHostKind(Action::OFK_HIP) &&
4658 Args.hasFlag(options::OPT_gpu_bundle_output,
4659 options::OPT_no_gpu_bundle_output, true) &&
4660 !Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false) &&
4661 !llvm::any_of(OffloadActions,
4662 [](Action *A) { return A->getType() != types::TY_Image; });
4663
4664 // All kinds exit now in device-only mode except for non-RDC mode HIP.
4665 if (offloadDeviceOnly() && !ShouldBundleHIP)
4666 return C.MakeAction<OffloadAction>(Arg&: DDeps, Arg: types::TY_Nothing);
4667
4668 if (OffloadActions.empty())
4669 return HostAction;
4670
4671 OffloadAction::DeviceDependences DDep;
4672 if (C.isOffloadingHostKind(Action::OFK_Cuda) &&
4673 !Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false)) {
4674 // If we are not in RDC-mode we just emit the final CUDA fatbinary for
4675 // each translation unit without requiring any linking.
4676 Action *FatbinAction =
4677 C.MakeAction<LinkJobAction>(Arg&: OffloadActions, Arg: types::TY_CUDA_FATBIN);
4678 DDep.add(A&: *FatbinAction, TC: *C.getSingleOffloadToolChain<Action::OFK_Cuda>(),
4679 BoundArch: nullptr, OKind: Action::OFK_Cuda);
4680 } else if (C.isOffloadingHostKind(Action::OFK_HIP) &&
4681 !Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
4682 false)) {
4683 // If we are not in RDC-mode we just emit the final HIP fatbinary for each
4684 // translation unit, linking each input individually.
4685 Action *FatbinAction =
4686 C.MakeAction<LinkJobAction>(Arg&: OffloadActions, Arg: types::TY_HIP_FATBIN);
4687 DDep.add(A&: *FatbinAction, TC: *C.getSingleOffloadToolChain<Action::OFK_HIP>(),
4688 BoundArch: nullptr, OKind: Action::OFK_HIP);
4689 } else {
4690 // Package all the offloading actions into a single output that can be
4691 // embedded in the host and linked.
4692 Action *PackagerAction =
4693 C.MakeAction<OffloadPackagerJobAction>(Arg&: OffloadActions, Arg: types::TY_Image);
4694 DDep.add(A&: *PackagerAction, TC: *C.getSingleOffloadToolChain<Action::OFK_Host>(),
4695 BoundArch: nullptr, OffloadKindMask: C.getActiveOffloadKinds());
4696 }
4697
4698 // HIP wants '--offload-device-only' to create a fatbinary by default.
4699 if (offloadDeviceOnly())
4700 return C.MakeAction<OffloadAction>(Arg&: DDep, Arg: types::TY_Nothing);
4701
4702 // If we are unable to embed a single device output into the host, we need to
4703 // add each device output as a host dependency to ensure they are still built.
4704 bool SingleDeviceOutput = !llvm::any_of(Range&: OffloadActions, P: [](Action *A) {
4705 return A->getType() == types::TY_Nothing;
4706 }) && isa<CompileJobAction>(Val: HostAction);
4707 OffloadAction::HostDependence HDep(
4708 *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(),
4709 /*BoundArch=*/nullptr, SingleDeviceOutput ? DDep : DDeps);
4710 return C.MakeAction<OffloadAction>(Arg&: HDep, Arg&: SingleDeviceOutput ? DDep : DDeps);
4711}
4712
4713Action *Driver::ConstructPhaseAction(
4714 Compilation &C, const ArgList &Args, phases::ID Phase, Action *Input,
4715 Action::OffloadKind TargetDeviceOffloadKind) const {
4716 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
4717
4718 // Some types skip the assembler phase (e.g., llvm-bc), but we can't
4719 // encode this in the steps because the intermediate type depends on
4720 // arguments. Just special case here.
4721 if (Phase == phases::Assemble && Input->getType() != types::TY_PP_Asm)
4722 return Input;
4723
4724 // Build the appropriate action.
4725 switch (Phase) {
4726 case phases::Link:
4727 llvm_unreachable("link action invalid here.");
4728 case phases::IfsMerge:
4729 llvm_unreachable("ifsmerge action invalid here.");
4730 case phases::Preprocess: {
4731 types::ID OutputTy;
4732 // -M and -MM specify the dependency file name by altering the output type,
4733 // -if -MD and -MMD are not specified.
4734 if (Args.hasArg(options::OPT_M, options::OPT_MM) &&
4735 !Args.hasArg(options::OPT_MD, options::OPT_MMD)) {
4736 OutputTy = types::TY_Dependencies;
4737 } else {
4738 OutputTy = Input->getType();
4739 // For these cases, the preprocessor is only translating forms, the Output
4740 // still needs preprocessing.
4741 if (!Args.hasFlag(options::OPT_frewrite_includes,
4742 options::OPT_fno_rewrite_includes, false) &&
4743 !Args.hasFlag(options::OPT_frewrite_imports,
4744 options::OPT_fno_rewrite_imports, false) &&
4745 !Args.hasFlag(options::OPT_fdirectives_only,
4746 options::OPT_fno_directives_only, false) &&
4747 !CCGenDiagnostics)
4748 OutputTy = types::getPreprocessedType(Id: OutputTy);
4749 assert(OutputTy != types::TY_INVALID &&
4750 "Cannot preprocess this input type!");
4751 }
4752 return C.MakeAction<PreprocessJobAction>(Arg&: Input, Arg&: OutputTy);
4753 }
4754 case phases::Precompile: {
4755 // API extraction should not generate an actual precompilation action.
4756 if (Args.hasArg(options::OPT_extract_api))
4757 return C.MakeAction<ExtractAPIJobAction>(Arg&: Input, Arg: types::TY_API_INFO);
4758
4759 // With 'fexperimental-modules-reduced-bmi', we don't want to run the
4760 // precompile phase unless the user specified '--precompile'. In the case
4761 // the '--precompile' flag is enabled, we will try to emit the reduced BMI
4762 // as a by product in GenerateModuleInterfaceAction.
4763 if (Args.hasArg(options::OPT_modules_reduced_bmi) &&
4764 !Args.getLastArg(options::OPT__precompile))
4765 return Input;
4766
4767 types::ID OutputTy = getPrecompiledType(Id: Input->getType());
4768 assert(OutputTy != types::TY_INVALID &&
4769 "Cannot precompile this input type!");
4770
4771 // If we're given a module name, precompile header file inputs as a
4772 // module, not as a precompiled header.
4773 const char *ModName = nullptr;
4774 if (OutputTy == types::TY_PCH) {
4775 if (Arg *A = Args.getLastArg(options::OPT_fmodule_name_EQ))
4776 ModName = A->getValue();
4777 if (ModName)
4778 OutputTy = types::TY_ModuleFile;
4779 }
4780
4781 if (Args.hasArg(options::OPT_fsyntax_only)) {
4782 // Syntax checks should not emit a PCH file
4783 OutputTy = types::TY_Nothing;
4784 }
4785
4786 return C.MakeAction<PrecompileJobAction>(Arg&: Input, Arg&: OutputTy);
4787 }
4788 case phases::Compile: {
4789 if (Args.hasArg(options::OPT_fsyntax_only))
4790 return C.MakeAction<CompileJobAction>(Arg&: Input, Arg: types::TY_Nothing);
4791 if (Args.hasArg(options::OPT_rewrite_objc))
4792 return C.MakeAction<CompileJobAction>(Arg&: Input, Arg: types::TY_RewrittenObjC);
4793 if (Args.hasArg(options::OPT_rewrite_legacy_objc))
4794 return C.MakeAction<CompileJobAction>(Arg&: Input,
4795 Arg: types::TY_RewrittenLegacyObjC);
4796 if (Args.hasArg(options::OPT__analyze))
4797 return C.MakeAction<AnalyzeJobAction>(Arg&: Input, Arg: types::TY_Plist);
4798 if (Args.hasArg(options::OPT__migrate))
4799 return C.MakeAction<MigrateJobAction>(Arg&: Input, Arg: types::TY_Remap);
4800 if (Args.hasArg(options::OPT_emit_ast))
4801 return C.MakeAction<CompileJobAction>(Arg&: Input, Arg: types::TY_AST);
4802 if (Args.hasArg(options::OPT_module_file_info))
4803 return C.MakeAction<CompileJobAction>(Arg&: Input, Arg: types::TY_ModuleFile);
4804 if (Args.hasArg(options::OPT_verify_pch))
4805 return C.MakeAction<VerifyPCHJobAction>(Arg&: Input, Arg: types::TY_Nothing);
4806 if (Args.hasArg(options::OPT_extract_api))
4807 return C.MakeAction<ExtractAPIJobAction>(Arg&: Input, Arg: types::TY_API_INFO);
4808 return C.MakeAction<CompileJobAction>(Arg&: Input, Arg: types::TY_LLVM_BC);
4809 }
4810 case phases::Backend: {
4811 if (isUsingLTO() && TargetDeviceOffloadKind == Action::OFK_None) {
4812 types::ID Output;
4813 if (Args.hasArg(options::OPT_ffat_lto_objects) &&
4814 !Args.hasArg(options::OPT_emit_llvm))
4815 Output = types::TY_PP_Asm;
4816 else if (Args.hasArg(options::OPT_S))
4817 Output = types::TY_LTO_IR;
4818 else
4819 Output = types::TY_LTO_BC;
4820 return C.MakeAction<BackendJobAction>(Arg&: Input, Arg&: Output);
4821 }
4822 if (isUsingLTO(/* IsOffload */ true) &&
4823 TargetDeviceOffloadKind != Action::OFK_None) {
4824 types::ID Output =
4825 Args.hasArg(options::OPT_S) ? types::TY_LTO_IR : types::TY_LTO_BC;
4826 return C.MakeAction<BackendJobAction>(Arg&: Input, Arg&: Output);
4827 }
4828 if (Args.hasArg(options::OPT_emit_llvm) ||
4829 (((Input->getOffloadingToolChain() &&
4830 Input->getOffloadingToolChain()->getTriple().isAMDGPU()) ||
4831 TargetDeviceOffloadKind == Action::OFK_HIP) &&
4832 (Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
4833 false) ||
4834 TargetDeviceOffloadKind == Action::OFK_OpenMP))) {
4835 types::ID Output =
4836 Args.hasArg(options::OPT_S) &&
4837 (TargetDeviceOffloadKind == Action::OFK_None ||
4838 offloadDeviceOnly() ||
4839 (TargetDeviceOffloadKind == Action::OFK_HIP &&
4840 !Args.hasFlag(options::OPT_offload_new_driver,
4841 options::OPT_no_offload_new_driver, false)))
4842 ? types::TY_LLVM_IR
4843 : types::TY_LLVM_BC;
4844 return C.MakeAction<BackendJobAction>(Arg&: Input, Arg&: Output);
4845 }
4846 return C.MakeAction<BackendJobAction>(Arg&: Input, Arg: types::TY_PP_Asm);
4847 }
4848 case phases::Assemble:
4849 return C.MakeAction<AssembleJobAction>(Arg: std::move(Input), Arg: types::TY_Object);
4850 }
4851
4852 llvm_unreachable("invalid phase in ConstructPhaseAction");
4853}
4854
4855void Driver::BuildJobs(Compilation &C) const {
4856 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
4857
4858 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
4859
4860 // It is an error to provide a -o option if we are making multiple output
4861 // files. There are exceptions:
4862 //
4863 // IfsMergeJob: when generating interface stubs enabled we want to be able to
4864 // generate the stub file at the same time that we generate the real
4865 // library/a.out. So when a .o, .so, etc are the output, with clang interface
4866 // stubs there will also be a .ifs and .ifso at the same location.
4867 //
4868 // CompileJob of type TY_IFS_CPP: when generating interface stubs is enabled
4869 // and -c is passed, we still want to be able to generate a .ifs file while
4870 // we are also generating .o files. So we allow more than one output file in
4871 // this case as well.
4872 //
4873 // OffloadClass of type TY_Nothing: device-only output will place many outputs
4874 // into a single offloading action. We should count all inputs to the action
4875 // as outputs. Also ignore device-only outputs if we're compiling with
4876 // -fsyntax-only.
4877 if (FinalOutput) {
4878 unsigned NumOutputs = 0;
4879 unsigned NumIfsOutputs = 0;
4880 for (const Action *A : C.getActions()) {
4881 if (A->getType() != types::TY_Nothing &&
4882 A->getType() != types::TY_DX_CONTAINER &&
4883 !(A->getKind() == Action::IfsMergeJobClass ||
4884 (A->getType() == clang::driver::types::TY_IFS_CPP &&
4885 A->getKind() == clang::driver::Action::CompileJobClass &&
4886 0 == NumIfsOutputs++) ||
4887 (A->getKind() == Action::BindArchClass && A->getInputs().size() &&
4888 A->getInputs().front()->getKind() == Action::IfsMergeJobClass)))
4889 ++NumOutputs;
4890 else if (A->getKind() == Action::OffloadClass &&
4891 A->getType() == types::TY_Nothing &&
4892 !C.getArgs().hasArg(options::OPT_fsyntax_only))
4893 NumOutputs += A->size();
4894 }
4895
4896 if (NumOutputs > 1) {
4897 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
4898 FinalOutput = nullptr;
4899 }
4900 }
4901
4902 const llvm::Triple &RawTriple = C.getDefaultToolChain().getTriple();
4903
4904 // Collect the list of architectures.
4905 llvm::StringSet<> ArchNames;
4906 if (RawTriple.isOSBinFormatMachO())
4907 for (const Arg *A : C.getArgs())
4908 if (A->getOption().matches(options::OPT_arch))
4909 ArchNames.insert(key: A->getValue());
4910
4911 // Set of (Action, canonical ToolChain triple) pairs we've built jobs for.
4912 std::map<std::pair<const Action *, std::string>, InputInfoList> CachedResults;
4913 for (Action *A : C.getActions()) {
4914 // If we are linking an image for multiple archs then the linker wants
4915 // -arch_multiple and -final_output <final image name>. Unfortunately, this
4916 // doesn't fit in cleanly because we have to pass this information down.
4917 //
4918 // FIXME: This is a hack; find a cleaner way to integrate this into the
4919 // process.
4920 const char *LinkingOutput = nullptr;
4921 if (isa<LipoJobAction>(Val: A)) {
4922 if (FinalOutput)
4923 LinkingOutput = FinalOutput->getValue();
4924 else
4925 LinkingOutput = getDefaultImageName();
4926 }
4927
4928 BuildJobsForAction(C, A, TC: &C.getDefaultToolChain(),
4929 /*BoundArch*/ StringRef(),
4930 /*AtTopLevel*/ true,
4931 /*MultipleArchs*/ ArchNames.size() > 1,
4932 /*LinkingOutput*/ LinkingOutput, CachedResults,
4933 /*TargetDeviceOffloadKind*/ Action::OFK_None);
4934 }
4935
4936 // If we have more than one job, then disable integrated-cc1 for now. Do this
4937 // also when we need to report process execution statistics.
4938 if (C.getJobs().size() > 1 || CCPrintProcessStats)
4939 for (auto &J : C.getJobs())
4940 J.InProcess = false;
4941
4942 if (CCPrintProcessStats) {
4943 C.setPostCallback([=](const Command &Cmd, int Res) {
4944 std::optional<llvm::sys::ProcessStatistics> ProcStat =
4945 Cmd.getProcessStatistics();
4946 if (!ProcStat)
4947 return;
4948
4949 const char *LinkingOutput = nullptr;
4950 if (FinalOutput)
4951 LinkingOutput = FinalOutput->getValue();
4952 else if (!Cmd.getOutputFilenames().empty())
4953 LinkingOutput = Cmd.getOutputFilenames().front().c_str();
4954 else
4955 LinkingOutput = getDefaultImageName();
4956
4957 if (CCPrintStatReportFilename.empty()) {
4958 using namespace llvm;
4959 // Human readable output.
4960 outs() << sys::path::filename(path: Cmd.getExecutable()) << ": "
4961 << "output=" << LinkingOutput;
4962 outs() << ", total="
4963 << format(Fmt: "%.3f", Vals: ProcStat->TotalTime.count() / 1000.) << " ms"
4964 << ", user="
4965 << format(Fmt: "%.3f", Vals: ProcStat->UserTime.count() / 1000.) << " ms"
4966 << ", mem=" << ProcStat->PeakMemory << " Kb\n";
4967 } else {
4968 // CSV format.
4969 std::string Buffer;
4970 llvm::raw_string_ostream Out(Buffer);
4971 llvm::sys::printArg(OS&: Out, Arg: llvm::sys::path::filename(path: Cmd.getExecutable()),
4972 /*Quote*/ true);
4973 Out << ',';
4974 llvm::sys::printArg(OS&: Out, Arg: LinkingOutput, Quote: true);
4975 Out << ',' << ProcStat->TotalTime.count() << ','
4976 << ProcStat->UserTime.count() << ',' << ProcStat->PeakMemory
4977 << '\n';
4978 Out.flush();
4979 std::error_code EC;
4980 llvm::raw_fd_ostream OS(CCPrintStatReportFilename, EC,
4981 llvm::sys::fs::OF_Append |
4982 llvm::sys::fs::OF_Text);
4983 if (EC)
4984 return;
4985 auto L = OS.lock();
4986 if (!L) {
4987 llvm::errs() << "ERROR: Cannot lock file "
4988 << CCPrintStatReportFilename << ": "
4989 << toString(E: L.takeError()) << "\n";
4990 return;
4991 }
4992 OS << Buffer;
4993 OS.flush();
4994 }
4995 });
4996 }
4997
4998 // If the user passed -Qunused-arguments or there were errors, don't warn
4999 // about any unused arguments.
5000 if (Diags.hasErrorOccurred() ||
5001 C.getArgs().hasArg(options::OPT_Qunused_arguments))
5002 return;
5003
5004 // Claim -fdriver-only here.
5005 (void)C.getArgs().hasArg(options::OPT_fdriver_only);
5006 // Claim -### here.
5007 (void)C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
5008
5009 // Claim --driver-mode, --rsp-quoting, it was handled earlier.
5010 (void)C.getArgs().hasArg(options::OPT_driver_mode);
5011 (void)C.getArgs().hasArg(options::OPT_rsp_quoting);
5012
5013 bool HasAssembleJob = llvm::any_of(Range&: C.getJobs(), P: [](auto &J) {
5014 // Match ClangAs and other derived assemblers of Tool. ClangAs uses a
5015 // longer ShortName "clang integrated assembler" while other assemblers just
5016 // use "assembler".
5017 return strstr(J.getCreator().getShortName(), "assembler");
5018 });
5019 for (Arg *A : C.getArgs()) {
5020 // FIXME: It would be nice to be able to send the argument to the
5021 // DiagnosticsEngine, so that extra values, position, and so on could be
5022 // printed.
5023 if (!A->isClaimed()) {
5024 if (A->getOption().hasFlag(Val: options::NoArgumentUnused))
5025 continue;
5026
5027 // Suppress the warning automatically if this is just a flag, and it is an
5028 // instance of an argument we already claimed.
5029 const Option &Opt = A->getOption();
5030 if (Opt.getKind() == Option::FlagClass) {
5031 bool DuplicateClaimed = false;
5032
5033 for (const Arg *AA : C.getArgs().filtered(Ids: &Opt)) {
5034 if (AA->isClaimed()) {
5035 DuplicateClaimed = true;
5036 break;
5037 }
5038 }
5039
5040 if (DuplicateClaimed)
5041 continue;
5042 }
5043
5044 // In clang-cl, don't mention unknown arguments here since they have
5045 // already been warned about.
5046 if (!IsCLMode() || !A->getOption().matches(options::OPT_UNKNOWN)) {
5047 if (A->getOption().hasFlag(Val: options::TargetSpecific) &&
5048 !A->isIgnoredTargetSpecific() && !HasAssembleJob &&
5049 // When for example -### or -v is used
5050 // without a file, target specific options are not
5051 // consumed/validated.
5052 // Instead emitting an error emit a warning instead.
5053 !C.getActions().empty()) {
5054 Diag(diag::err_drv_unsupported_opt_for_target)
5055 << A->getSpelling() << getTargetTriple();
5056 } else {
5057 Diag(clang::diag::warn_drv_unused_argument)
5058 << A->getAsString(C.getArgs());
5059 }
5060 }
5061 }
5062 }
5063}
5064
5065namespace {
5066/// Utility class to control the collapse of dependent actions and select the
5067/// tools accordingly.
5068class ToolSelector final {
5069 /// The tool chain this selector refers to.
5070 const ToolChain &TC;
5071
5072 /// The compilation this selector refers to.
5073 const Compilation &C;
5074
5075 /// The base action this selector refers to.
5076 const JobAction *BaseAction;
5077
5078 /// Set to true if the current toolchain refers to host actions.
5079 bool IsHostSelector;
5080
5081 /// Set to true if save-temps and embed-bitcode functionalities are active.
5082 bool SaveTemps;
5083 bool EmbedBitcode;
5084
5085 /// Get previous dependent action or null if that does not exist. If
5086 /// \a CanBeCollapsed is false, that action must be legal to collapse or
5087 /// null will be returned.
5088 const JobAction *getPrevDependentAction(const ActionList &Inputs,
5089 ActionList &SavedOffloadAction,
5090 bool CanBeCollapsed = true) {
5091 // An option can be collapsed only if it has a single input.
5092 if (Inputs.size() != 1)
5093 return nullptr;
5094
5095 Action *CurAction = *Inputs.begin();
5096 if (CanBeCollapsed &&
5097 !CurAction->isCollapsingWithNextDependentActionLegal())
5098 return nullptr;
5099
5100 // If the input action is an offload action. Look through it and save any
5101 // offload action that can be dropped in the event of a collapse.
5102 if (auto *OA = dyn_cast<OffloadAction>(Val: CurAction)) {
5103 // If the dependent action is a device action, we will attempt to collapse
5104 // only with other device actions. Otherwise, we would do the same but
5105 // with host actions only.
5106 if (!IsHostSelector) {
5107 if (OA->hasSingleDeviceDependence(/*DoNotConsiderHostActions=*/true)) {
5108 CurAction =
5109 OA->getSingleDeviceDependence(/*DoNotConsiderHostActions=*/true);
5110 if (CanBeCollapsed &&
5111 !CurAction->isCollapsingWithNextDependentActionLegal())
5112 return nullptr;
5113 SavedOffloadAction.push_back(Elt: OA);
5114 return dyn_cast<JobAction>(Val: CurAction);
5115 }
5116 } else if (OA->hasHostDependence()) {
5117 CurAction = OA->getHostDependence();
5118 if (CanBeCollapsed &&
5119 !CurAction->isCollapsingWithNextDependentActionLegal())
5120 return nullptr;
5121 SavedOffloadAction.push_back(Elt: OA);
5122 return dyn_cast<JobAction>(Val: CurAction);
5123 }
5124 return nullptr;
5125 }
5126
5127 return dyn_cast<JobAction>(Val: CurAction);
5128 }
5129
5130 /// Return true if an assemble action can be collapsed.
5131 bool canCollapseAssembleAction() const {
5132 return TC.useIntegratedAs() && !SaveTemps &&
5133 !C.getArgs().hasArg(options::OPT_via_file_asm) &&
5134 !C.getArgs().hasArg(options::OPT__SLASH_FA) &&
5135 !C.getArgs().hasArg(options::OPT__SLASH_Fa) &&
5136 !C.getArgs().hasArg(options::OPT_dxc_Fc);
5137 }
5138
5139 /// Return true if a preprocessor action can be collapsed.
5140 bool canCollapsePreprocessorAction() const {
5141 return !C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
5142 !C.getArgs().hasArg(options::OPT_traditional_cpp) && !SaveTemps &&
5143 !C.getArgs().hasArg(options::OPT_rewrite_objc);
5144 }
5145
5146 /// Struct that relates an action with the offload actions that would be
5147 /// collapsed with it.
5148 struct JobActionInfo final {
5149 /// The action this info refers to.
5150 const JobAction *JA = nullptr;
5151 /// The offload actions we need to take care off if this action is
5152 /// collapsed.
5153 ActionList SavedOffloadAction;
5154 };
5155
5156 /// Append collapsed offload actions from the give nnumber of elements in the
5157 /// action info array.
5158 static void AppendCollapsedOffloadAction(ActionList &CollapsedOffloadAction,
5159 ArrayRef<JobActionInfo> &ActionInfo,
5160 unsigned ElementNum) {
5161 assert(ElementNum <= ActionInfo.size() && "Invalid number of elements.");
5162 for (unsigned I = 0; I < ElementNum; ++I)
5163 CollapsedOffloadAction.append(in_start: ActionInfo[I].SavedOffloadAction.begin(),
5164 in_end: ActionInfo[I].SavedOffloadAction.end());
5165 }
5166
5167 /// Functions that attempt to perform the combining. They detect if that is
5168 /// legal, and if so they update the inputs \a Inputs and the offload action
5169 /// that were collapsed in \a CollapsedOffloadAction. A tool that deals with
5170 /// the combined action is returned. If the combining is not legal or if the
5171 /// tool does not exist, null is returned.
5172 /// Currently three kinds of collapsing are supported:
5173 /// - Assemble + Backend + Compile;
5174 /// - Assemble + Backend ;
5175 /// - Backend + Compile.
5176 const Tool *
5177 combineAssembleBackendCompile(ArrayRef<JobActionInfo> ActionInfo,
5178 ActionList &Inputs,
5179 ActionList &CollapsedOffloadAction) {
5180 if (ActionInfo.size() < 3 || !canCollapseAssembleAction())
5181 return nullptr;
5182 auto *AJ = dyn_cast<AssembleJobAction>(Val: ActionInfo[0].JA);
5183 auto *BJ = dyn_cast<BackendJobAction>(Val: ActionInfo[1].JA);
5184 auto *CJ = dyn_cast<CompileJobAction>(Val: ActionInfo[2].JA);
5185 if (!AJ || !BJ || !CJ)
5186 return nullptr;
5187
5188 // Get compiler tool.
5189 const Tool *T = TC.SelectTool(JA: *CJ);
5190 if (!T)
5191 return nullptr;
5192
5193 // Can't collapse if we don't have codegen support unless we are
5194 // emitting LLVM IR.
5195 bool OutputIsLLVM = types::isLLVMIR(Id: ActionInfo[0].JA->getType());
5196 if (!T->hasIntegratedBackend() && !(OutputIsLLVM && T->canEmitIR()))
5197 return nullptr;
5198
5199 // When using -fembed-bitcode, it is required to have the same tool (clang)
5200 // for both CompilerJA and BackendJA. Otherwise, combine two stages.
5201 if (EmbedBitcode) {
5202 const Tool *BT = TC.SelectTool(JA: *BJ);
5203 if (BT == T)
5204 return nullptr;
5205 }
5206
5207 if (!T->hasIntegratedAssembler())
5208 return nullptr;
5209
5210 Inputs = CJ->getInputs();
5211 AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo,
5212 /*NumElements=*/ElementNum: 3);
5213 return T;
5214 }
5215 const Tool *combineAssembleBackend(ArrayRef<JobActionInfo> ActionInfo,
5216 ActionList &Inputs,
5217 ActionList &CollapsedOffloadAction) {
5218 if (ActionInfo.size() < 2 || !canCollapseAssembleAction())
5219 return nullptr;
5220 auto *AJ = dyn_cast<AssembleJobAction>(Val: ActionInfo[0].JA);
5221 auto *BJ = dyn_cast<BackendJobAction>(Val: ActionInfo[1].JA);
5222 if (!AJ || !BJ)
5223 return nullptr;
5224
5225 // Get backend tool.
5226 const Tool *T = TC.SelectTool(JA: *BJ);
5227 if (!T)
5228 return nullptr;
5229
5230 if (!T->hasIntegratedAssembler())
5231 return nullptr;
5232
5233 Inputs = BJ->getInputs();
5234 AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo,
5235 /*NumElements=*/ElementNum: 2);
5236 return T;
5237 }
5238 const Tool *combineBackendCompile(ArrayRef<JobActionInfo> ActionInfo,
5239 ActionList &Inputs,
5240 ActionList &CollapsedOffloadAction) {
5241 if (ActionInfo.size() < 2)
5242 return nullptr;
5243 auto *BJ = dyn_cast<BackendJobAction>(Val: ActionInfo[0].JA);
5244 auto *CJ = dyn_cast<CompileJobAction>(Val: ActionInfo[1].JA);
5245 if (!BJ || !CJ)
5246 return nullptr;
5247
5248 // Check if the initial input (to the compile job or its predessor if one
5249 // exists) is LLVM bitcode. In that case, no preprocessor step is required
5250 // and we can still collapse the compile and backend jobs when we have
5251 // -save-temps. I.e. there is no need for a separate compile job just to
5252 // emit unoptimized bitcode.
5253 bool InputIsBitcode = true;
5254 for (size_t i = 1; i < ActionInfo.size(); i++)
5255 if (ActionInfo[i].JA->getType() != types::TY_LLVM_BC &&
5256 ActionInfo[i].JA->getType() != types::TY_LTO_BC) {
5257 InputIsBitcode = false;
5258 break;
5259 }
5260 if (!InputIsBitcode && !canCollapsePreprocessorAction())
5261 return nullptr;
5262
5263 // Get compiler tool.
5264 const Tool *T = TC.SelectTool(JA: *CJ);
5265 if (!T)
5266 return nullptr;
5267
5268 // Can't collapse if we don't have codegen support unless we are
5269 // emitting LLVM IR.
5270 bool OutputIsLLVM = types::isLLVMIR(Id: ActionInfo[0].JA->getType());
5271 if (!T->hasIntegratedBackend() && !(OutputIsLLVM && T->canEmitIR()))
5272 return nullptr;
5273
5274 if (T->canEmitIR() && ((SaveTemps && !InputIsBitcode) || EmbedBitcode))
5275 return nullptr;
5276
5277 Inputs = CJ->getInputs();
5278 AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo,
5279 /*NumElements=*/ElementNum: 2);
5280 return T;
5281 }
5282
5283 /// Updates the inputs if the obtained tool supports combining with
5284 /// preprocessor action, and the current input is indeed a preprocessor
5285 /// action. If combining results in the collapse of offloading actions, those
5286 /// are appended to \a CollapsedOffloadAction.
5287 void combineWithPreprocessor(const Tool *T, ActionList &Inputs,
5288 ActionList &CollapsedOffloadAction) {
5289 if (!T || !canCollapsePreprocessorAction() || !T->hasIntegratedCPP())
5290 return;
5291
5292 // Attempt to get a preprocessor action dependence.
5293 ActionList PreprocessJobOffloadActions;
5294 ActionList NewInputs;
5295 for (Action *A : Inputs) {
5296 auto *PJ = getPrevDependentAction(Inputs: {A}, SavedOffloadAction&: PreprocessJobOffloadActions);
5297 if (!PJ || !isa<PreprocessJobAction>(Val: PJ)) {
5298 NewInputs.push_back(Elt: A);
5299 continue;
5300 }
5301
5302 // This is legal to combine. Append any offload action we found and add the
5303 // current input to preprocessor inputs.
5304 CollapsedOffloadAction.append(in_start: PreprocessJobOffloadActions.begin(),
5305 in_end: PreprocessJobOffloadActions.end());
5306 NewInputs.append(in_start: PJ->input_begin(), in_end: PJ->input_end());
5307 }
5308 Inputs = NewInputs;
5309 }
5310
5311public:
5312 ToolSelector(const JobAction *BaseAction, const ToolChain &TC,
5313 const Compilation &C, bool SaveTemps, bool EmbedBitcode)
5314 : TC(TC), C(C), BaseAction(BaseAction), SaveTemps(SaveTemps),
5315 EmbedBitcode(EmbedBitcode) {
5316 assert(BaseAction && "Invalid base action.");
5317 IsHostSelector = BaseAction->getOffloadingDeviceKind() == Action::OFK_None;
5318 }
5319
5320 /// Check if a chain of actions can be combined and return the tool that can
5321 /// handle the combination of actions. The pointer to the current inputs \a
5322 /// Inputs and the list of offload actions \a CollapsedOffloadActions
5323 /// connected to collapsed actions are updated accordingly. The latter enables
5324 /// the caller of the selector to process them afterwards instead of just
5325 /// dropping them. If no suitable tool is found, null will be returned.
5326 const Tool *getTool(ActionList &Inputs,
5327 ActionList &CollapsedOffloadAction) {
5328 //
5329 // Get the largest chain of actions that we could combine.
5330 //
5331
5332 SmallVector<JobActionInfo, 5> ActionChain(1);
5333 ActionChain.back().JA = BaseAction;
5334 while (ActionChain.back().JA) {
5335 const Action *CurAction = ActionChain.back().JA;
5336
5337 // Grow the chain by one element.
5338 ActionChain.resize(N: ActionChain.size() + 1);
5339 JobActionInfo &AI = ActionChain.back();
5340
5341 // Attempt to fill it with the
5342 AI.JA =
5343 getPrevDependentAction(Inputs: CurAction->getInputs(), SavedOffloadAction&: AI.SavedOffloadAction);
5344 }
5345
5346 // Pop the last action info as it could not be filled.
5347 ActionChain.pop_back();
5348
5349 //
5350 // Attempt to combine actions. If all combining attempts failed, just return
5351 // the tool of the provided action. At the end we attempt to combine the
5352 // action with any preprocessor action it may depend on.
5353 //
5354
5355 const Tool *T = combineAssembleBackendCompile(ActionInfo: ActionChain, Inputs,
5356 CollapsedOffloadAction);
5357 if (!T)
5358 T = combineAssembleBackend(ActionInfo: ActionChain, Inputs, CollapsedOffloadAction);
5359 if (!T)
5360 T = combineBackendCompile(ActionInfo: ActionChain, Inputs, CollapsedOffloadAction);
5361 if (!T) {
5362 Inputs = BaseAction->getInputs();
5363 T = TC.SelectTool(JA: *BaseAction);
5364 }
5365
5366 combineWithPreprocessor(T, Inputs, CollapsedOffloadAction);
5367 return T;
5368 }
5369};
5370}
5371
5372/// Return a string that uniquely identifies the result of a job. The bound arch
5373/// is not necessarily represented in the toolchain's triple -- for example,
5374/// armv7 and armv7s both map to the same triple -- so we need both in our map.
5375/// Also, we need to add the offloading device kind, as the same tool chain can
5376/// be used for host and device for some programming models, e.g. OpenMP.
5377static std::string GetTriplePlusArchString(const ToolChain *TC,
5378 StringRef BoundArch,
5379 Action::OffloadKind OffloadKind) {
5380 std::string TriplePlusArch = TC->getTriple().normalize();
5381 if (!BoundArch.empty()) {
5382 TriplePlusArch += "-";
5383 TriplePlusArch += BoundArch;
5384 }
5385 TriplePlusArch += "-";
5386 TriplePlusArch += Action::GetOffloadKindName(Kind: OffloadKind);
5387 return TriplePlusArch;
5388}
5389
5390InputInfoList Driver::BuildJobsForAction(
5391 Compilation &C, const Action *A, const ToolChain *TC, StringRef BoundArch,
5392 bool AtTopLevel, bool MultipleArchs, const char *LinkingOutput,
5393 std::map<std::pair<const Action *, std::string>, InputInfoList>
5394 &CachedResults,
5395 Action::OffloadKind TargetDeviceOffloadKind) const {
5396 std::pair<const Action *, std::string> ActionTC = {
5397 A, GetTriplePlusArchString(TC, BoundArch, OffloadKind: TargetDeviceOffloadKind)};
5398 auto CachedResult = CachedResults.find(x: ActionTC);
5399 if (CachedResult != CachedResults.end()) {
5400 return CachedResult->second;
5401 }
5402 InputInfoList Result = BuildJobsForActionNoCache(
5403 C, A, TC, BoundArch, AtTopLevel, MultipleArchs, LinkingOutput,
5404 CachedResults, TargetDeviceOffloadKind);
5405 CachedResults[ActionTC] = Result;
5406 return Result;
5407}
5408
5409static void handleTimeTrace(Compilation &C, const ArgList &Args,
5410 const JobAction *JA, const char *BaseInput,
5411 const InputInfo &Result) {
5412 Arg *A =
5413 Args.getLastArg(options::OPT_ftime_trace, options::OPT_ftime_trace_EQ);
5414 if (!A)
5415 return;
5416 SmallString<128> Path;
5417 if (A->getOption().matches(options::OPT_ftime_trace_EQ)) {
5418 Path = A->getValue();
5419 if (llvm::sys::fs::is_directory(Path)) {
5420 SmallString<128> Tmp(Result.getFilename());
5421 llvm::sys::path::replace_extension(path&: Tmp, extension: "json");
5422 llvm::sys::path::append(path&: Path, a: llvm::sys::path::filename(path: Tmp));
5423 }
5424 } else {
5425 if (Arg *DumpDir = Args.getLastArgNoClaim(options::OPT_dumpdir)) {
5426 // The trace file is ${dumpdir}${basename}.json. Note that dumpdir may not
5427 // end with a path separator.
5428 Path = DumpDir->getValue();
5429 Path += llvm::sys::path::filename(path: BaseInput);
5430 } else {
5431 Path = Result.getFilename();
5432 }
5433 llvm::sys::path::replace_extension(path&: Path, extension: "json");
5434 }
5435 const char *ResultFile = C.getArgs().MakeArgString(Str: Path);
5436 C.addTimeTraceFile(Name: ResultFile, JA);
5437 C.addResultFile(Name: ResultFile, JA);
5438}
5439
5440InputInfoList Driver::BuildJobsForActionNoCache(
5441 Compilation &C, const Action *A, const ToolChain *TC, StringRef BoundArch,
5442 bool AtTopLevel, bool MultipleArchs, const char *LinkingOutput,
5443 std::map<std::pair<const Action *, std::string>, InputInfoList>
5444 &CachedResults,
5445 Action::OffloadKind TargetDeviceOffloadKind) const {
5446 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
5447
5448 InputInfoList OffloadDependencesInputInfo;
5449 bool BuildingForOffloadDevice = TargetDeviceOffloadKind != Action::OFK_None;
5450 if (const OffloadAction *OA = dyn_cast<OffloadAction>(Val: A)) {
5451 // The 'Darwin' toolchain is initialized only when its arguments are
5452 // computed. Get the default arguments for OFK_None to ensure that
5453 // initialization is performed before processing the offload action.
5454 // FIXME: Remove when darwin's toolchain is initialized during construction.
5455 C.getArgsForToolChain(TC, BoundArch, DeviceOffloadKind: Action::OFK_None);
5456
5457 // The offload action is expected to be used in four different situations.
5458 //
5459 // a) Set a toolchain/architecture/kind for a host action:
5460 // Host Action 1 -> OffloadAction -> Host Action 2
5461 //
5462 // b) Set a toolchain/architecture/kind for a device action;
5463 // Device Action 1 -> OffloadAction -> Device Action 2
5464 //
5465 // c) Specify a device dependence to a host action;
5466 // Device Action 1 _
5467 // \
5468 // Host Action 1 ---> OffloadAction -> Host Action 2
5469 //
5470 // d) Specify a host dependence to a device action.
5471 // Host Action 1 _
5472 // \
5473 // Device Action 1 ---> OffloadAction -> Device Action 2
5474 //
5475 // For a) and b), we just return the job generated for the dependences. For
5476 // c) and d) we override the current action with the host/device dependence
5477 // if the current toolchain is host/device and set the offload dependences
5478 // info with the jobs obtained from the device/host dependence(s).
5479
5480 // If there is a single device option or has no host action, just generate
5481 // the job for it.
5482 if (OA->hasSingleDeviceDependence() || !OA->hasHostDependence()) {
5483 InputInfoList DevA;
5484 OA->doOnEachDeviceDependence(Work: [&](Action *DepA, const ToolChain *DepTC,
5485 const char *DepBoundArch) {
5486 DevA.append(RHS: BuildJobsForAction(C, A: DepA, TC: DepTC, BoundArch: DepBoundArch, AtTopLevel,
5487 /*MultipleArchs*/ !!DepBoundArch,
5488 LinkingOutput, CachedResults,
5489 TargetDeviceOffloadKind: DepA->getOffloadingDeviceKind()));
5490 });
5491 return DevA;
5492 }
5493
5494 // If 'Action 2' is host, we generate jobs for the device dependences and
5495 // override the current action with the host dependence. Otherwise, we
5496 // generate the host dependences and override the action with the device
5497 // dependence. The dependences can't therefore be a top-level action.
5498 OA->doOnEachDependence(
5499 /*IsHostDependence=*/BuildingForOffloadDevice,
5500 Work: [&](Action *DepA, const ToolChain *DepTC, const char *DepBoundArch) {
5501 OffloadDependencesInputInfo.append(RHS: BuildJobsForAction(
5502 C, A: DepA, TC: DepTC, BoundArch: DepBoundArch, /*AtTopLevel=*/false,
5503 /*MultipleArchs*/ !!DepBoundArch, LinkingOutput, CachedResults,
5504 TargetDeviceOffloadKind: DepA->getOffloadingDeviceKind()));
5505 });
5506
5507 A = BuildingForOffloadDevice
5508 ? OA->getSingleDeviceDependence(/*DoNotConsiderHostActions=*/true)
5509 : OA->getHostDependence();
5510
5511 // We may have already built this action as a part of the offloading
5512 // toolchain, return the cached input if so.
5513 std::pair<const Action *, std::string> ActionTC = {
5514 OA->getHostDependence(),
5515 GetTriplePlusArchString(TC, BoundArch, OffloadKind: TargetDeviceOffloadKind)};
5516 if (CachedResults.find(x: ActionTC) != CachedResults.end()) {
5517 InputInfoList Inputs = CachedResults[ActionTC];
5518 Inputs.append(RHS: OffloadDependencesInputInfo);
5519 return Inputs;
5520 }
5521 }
5522
5523 if (const InputAction *IA = dyn_cast<InputAction>(Val: A)) {
5524 // FIXME: It would be nice to not claim this here; maybe the old scheme of
5525 // just using Args was better?
5526 const Arg &Input = IA->getInputArg();
5527 Input.claim();
5528 if (Input.getOption().matches(options::OPT_INPUT)) {
5529 const char *Name = Input.getValue();
5530 return {InputInfo(A, Name, /* _BaseInput = */ Name)};
5531 }
5532 return {InputInfo(A, &Input, /* _BaseInput = */ "")};
5533 }
5534
5535 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(Val: A)) {
5536 const ToolChain *TC;
5537 StringRef ArchName = BAA->getArchName();
5538
5539 if (!ArchName.empty())
5540 TC = &getToolChain(Args: C.getArgs(),
5541 Target: computeTargetTriple(D: *this, TargetTriple,
5542 Args: C.getArgs(), DarwinArchName: ArchName));
5543 else
5544 TC = &C.getDefaultToolChain();
5545
5546 return BuildJobsForAction(C, A: *BAA->input_begin(), TC, BoundArch: ArchName, AtTopLevel,
5547 MultipleArchs, LinkingOutput, CachedResults,
5548 TargetDeviceOffloadKind);
5549 }
5550
5551
5552 ActionList Inputs = A->getInputs();
5553
5554 const JobAction *JA = cast<JobAction>(Val: A);
5555 ActionList CollapsedOffloadActions;
5556
5557 ToolSelector TS(JA, *TC, C, isSaveTempsEnabled(),
5558 embedBitcodeInObject() && !isUsingLTO());
5559 const Tool *T = TS.getTool(Inputs, CollapsedOffloadAction&: CollapsedOffloadActions);
5560
5561 if (!T)
5562 return {InputInfo()};
5563
5564 // If we've collapsed action list that contained OffloadAction we
5565 // need to build jobs for host/device-side inputs it may have held.
5566 for (const auto *OA : CollapsedOffloadActions)
5567 cast<OffloadAction>(Val: OA)->doOnEachDependence(
5568 /*IsHostDependence=*/BuildingForOffloadDevice,
5569 Work: [&](Action *DepA, const ToolChain *DepTC, const char *DepBoundArch) {
5570 OffloadDependencesInputInfo.append(RHS: BuildJobsForAction(
5571 C, A: DepA, TC: DepTC, BoundArch: DepBoundArch, /* AtTopLevel */ false,
5572 /*MultipleArchs=*/!!DepBoundArch, LinkingOutput, CachedResults,
5573 TargetDeviceOffloadKind: DepA->getOffloadingDeviceKind()));
5574 });
5575
5576 // Only use pipes when there is exactly one input.
5577 InputInfoList InputInfos;
5578 for (const Action *Input : Inputs) {
5579 // Treat dsymutil and verify sub-jobs as being at the top-level too, they
5580 // shouldn't get temporary output names.
5581 // FIXME: Clean this up.
5582 bool SubJobAtTopLevel =
5583 AtTopLevel && (isa<DsymutilJobAction>(Val: A) || isa<VerifyJobAction>(Val: A));
5584 InputInfos.append(RHS: BuildJobsForAction(
5585 C, A: Input, TC, BoundArch, AtTopLevel: SubJobAtTopLevel, MultipleArchs, LinkingOutput,
5586 CachedResults, TargetDeviceOffloadKind: A->getOffloadingDeviceKind()));
5587 }
5588
5589 // Always use the first file input as the base input.
5590 const char *BaseInput = InputInfos[0].getBaseInput();
5591 for (auto &Info : InputInfos) {
5592 if (Info.isFilename()) {
5593 BaseInput = Info.getBaseInput();
5594 break;
5595 }
5596 }
5597
5598 // ... except dsymutil actions, which use their actual input as the base
5599 // input.
5600 if (JA->getType() == types::TY_dSYM)
5601 BaseInput = InputInfos[0].getFilename();
5602
5603 // Append outputs of offload device jobs to the input list
5604 if (!OffloadDependencesInputInfo.empty())
5605 InputInfos.append(in_start: OffloadDependencesInputInfo.begin(),
5606 in_end: OffloadDependencesInputInfo.end());
5607
5608 // Set the effective triple of the toolchain for the duration of this job.
5609 llvm::Triple EffectiveTriple;
5610 const ToolChain &ToolTC = T->getToolChain();
5611 const ArgList &Args =
5612 C.getArgsForToolChain(TC, BoundArch, DeviceOffloadKind: A->getOffloadingDeviceKind());
5613 if (InputInfos.size() != 1) {
5614 EffectiveTriple = llvm::Triple(ToolTC.ComputeEffectiveClangTriple(Args));
5615 } else {
5616 // Pass along the input type if it can be unambiguously determined.
5617 EffectiveTriple = llvm::Triple(
5618 ToolTC.ComputeEffectiveClangTriple(Args, InputType: InputInfos[0].getType()));
5619 }
5620 RegisterEffectiveTriple TripleRAII(ToolTC, EffectiveTriple);
5621
5622 // Determine the place to write output to, if any.
5623 InputInfo Result;
5624 InputInfoList UnbundlingResults;
5625 if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(Val: JA)) {
5626 // If we have an unbundling job, we need to create results for all the
5627 // outputs. We also update the results cache so that other actions using
5628 // this unbundling action can get the right results.
5629 for (auto &UI : UA->getDependentActionsInfo()) {
5630 assert(UI.DependentOffloadKind != Action::OFK_None &&
5631 "Unbundling with no offloading??");
5632
5633 // Unbundling actions are never at the top level. When we generate the
5634 // offloading prefix, we also do that for the host file because the
5635 // unbundling action does not change the type of the output which can
5636 // cause a overwrite.
5637 std::string OffloadingPrefix = Action::GetOffloadingFileNamePrefix(
5638 Kind: UI.DependentOffloadKind,
5639 NormalizedTriple: UI.DependentToolChain->getTriple().normalize(),
5640 /*CreatePrefixForHost=*/true);
5641 auto CurI = InputInfo(
5642 UA,
5643 GetNamedOutputPath(C, JA: *UA, BaseInput, BoundArch: UI.DependentBoundArch,
5644 /*AtTopLevel=*/false,
5645 MultipleArchs: MultipleArchs ||
5646 UI.DependentOffloadKind == Action::OFK_HIP,
5647 NormalizedTriple: OffloadingPrefix),
5648 BaseInput);
5649 // Save the unbundling result.
5650 UnbundlingResults.push_back(Elt: CurI);
5651
5652 // Get the unique string identifier for this dependence and cache the
5653 // result.
5654 StringRef Arch;
5655 if (TargetDeviceOffloadKind == Action::OFK_HIP) {
5656 if (UI.DependentOffloadKind == Action::OFK_Host)
5657 Arch = StringRef();
5658 else
5659 Arch = UI.DependentBoundArch;
5660 } else
5661 Arch = BoundArch;
5662
5663 CachedResults[{A, GetTriplePlusArchString(TC: UI.DependentToolChain, BoundArch: Arch,
5664 OffloadKind: UI.DependentOffloadKind)}] = {
5665 CurI};
5666 }
5667
5668 // Now that we have all the results generated, select the one that should be
5669 // returned for the current depending action.
5670 std::pair<const Action *, std::string> ActionTC = {
5671 A, GetTriplePlusArchString(TC, BoundArch, OffloadKind: TargetDeviceOffloadKind)};
5672 assert(CachedResults.find(ActionTC) != CachedResults.end() &&
5673 "Result does not exist??");
5674 Result = CachedResults[ActionTC].front();
5675 } else if (JA->getType() == types::TY_Nothing)
5676 Result = {InputInfo(A, BaseInput)};
5677 else {
5678 // We only have to generate a prefix for the host if this is not a top-level
5679 // action.
5680 std::string OffloadingPrefix = Action::GetOffloadingFileNamePrefix(
5681 Kind: A->getOffloadingDeviceKind(), NormalizedTriple: TC->getTriple().normalize(),
5682 /*CreatePrefixForHost=*/isa<OffloadPackagerJobAction>(Val: A) ||
5683 !(A->getOffloadingHostActiveKinds() == Action::OFK_None ||
5684 AtTopLevel));
5685 Result = InputInfo(A, GetNamedOutputPath(C, JA: *JA, BaseInput, BoundArch,
5686 AtTopLevel, MultipleArchs,
5687 NormalizedTriple: OffloadingPrefix),
5688 BaseInput);
5689 if (T->canEmitIR() && OffloadingPrefix.empty())
5690 handleTimeTrace(C, Args, JA, BaseInput, Result);
5691 }
5692
5693 if (CCCPrintBindings && !CCGenDiagnostics) {
5694 llvm::errs() << "# \"" << T->getToolChain().getTripleString() << '"'
5695 << " - \"" << T->getName() << "\", inputs: [";
5696 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
5697 llvm::errs() << InputInfos[i].getAsString();
5698 if (i + 1 != e)
5699 llvm::errs() << ", ";
5700 }
5701 if (UnbundlingResults.empty())
5702 llvm::errs() << "], output: " << Result.getAsString() << "\n";
5703 else {
5704 llvm::errs() << "], outputs: [";
5705 for (unsigned i = 0, e = UnbundlingResults.size(); i != e; ++i) {
5706 llvm::errs() << UnbundlingResults[i].getAsString();
5707 if (i + 1 != e)
5708 llvm::errs() << ", ";
5709 }
5710 llvm::errs() << "] \n";
5711 }
5712 } else {
5713 if (UnbundlingResults.empty())
5714 T->ConstructJob(
5715 C, JA: *JA, Output: Result, Inputs: InputInfos,
5716 TCArgs: C.getArgsForToolChain(TC, BoundArch, DeviceOffloadKind: JA->getOffloadingDeviceKind()),
5717 LinkingOutput);
5718 else
5719 T->ConstructJobMultipleOutputs(
5720 C, JA: *JA, Outputs: UnbundlingResults, Inputs: InputInfos,
5721 TCArgs: C.getArgsForToolChain(TC, BoundArch, DeviceOffloadKind: JA->getOffloadingDeviceKind()),
5722 LinkingOutput);
5723 }
5724 return {Result};
5725}
5726
5727const char *Driver::getDefaultImageName() const {
5728 llvm::Triple Target(llvm::Triple::normalize(Str: TargetTriple));
5729 return Target.isOSWindows() ? "a.exe" : "a.out";
5730}
5731
5732/// Create output filename based on ArgValue, which could either be a
5733/// full filename, filename without extension, or a directory. If ArgValue
5734/// does not provide a filename, then use BaseName, and use the extension
5735/// suitable for FileType.
5736static const char *MakeCLOutputFilename(const ArgList &Args, StringRef ArgValue,
5737 StringRef BaseName,
5738 types::ID FileType) {
5739 SmallString<128> Filename = ArgValue;
5740
5741 if (ArgValue.empty()) {
5742 // If the argument is empty, output to BaseName in the current dir.
5743 Filename = BaseName;
5744 } else if (llvm::sys::path::is_separator(value: Filename.back())) {
5745 // If the argument is a directory, output to BaseName in that dir.
5746 llvm::sys::path::append(path&: Filename, a: BaseName);
5747 }
5748
5749 if (!llvm::sys::path::has_extension(path: ArgValue)) {
5750 // If the argument didn't provide an extension, then set it.
5751 const char *Extension = types::getTypeTempSuffix(Id: FileType, CLStyle: true);
5752
5753 if (FileType == types::TY_Image &&
5754 Args.hasArg(options::OPT__SLASH_LD, options::OPT__SLASH_LDd)) {
5755 // The output file is a dll.
5756 Extension = "dll";
5757 }
5758
5759 llvm::sys::path::replace_extension(path&: Filename, extension: Extension);
5760 }
5761
5762 return Args.MakeArgString(Str: Filename.c_str());
5763}
5764
5765static bool HasPreprocessOutput(const Action &JA) {
5766 if (isa<PreprocessJobAction>(Val: JA))
5767 return true;
5768 if (isa<OffloadAction>(Val: JA) && isa<PreprocessJobAction>(Val: JA.getInputs()[0]))
5769 return true;
5770 if (isa<OffloadBundlingJobAction>(Val: JA) &&
5771 HasPreprocessOutput(JA: *(JA.getInputs()[0])))
5772 return true;
5773 return false;
5774}
5775
5776const char *Driver::CreateTempFile(Compilation &C, StringRef Prefix,
5777 StringRef Suffix, bool MultipleArchs,
5778 StringRef BoundArch,
5779 bool NeedUniqueDirectory) const {
5780 SmallString<128> TmpName;
5781 Arg *A = C.getArgs().getLastArg(options::OPT_fcrash_diagnostics_dir);
5782 std::optional<std::string> CrashDirectory =
5783 CCGenDiagnostics && A
5784 ? std::string(A->getValue())
5785 : llvm::sys::Process::GetEnv(name: "CLANG_CRASH_DIAGNOSTICS_DIR");
5786 if (CrashDirectory) {
5787 if (!getVFS().exists(Path: *CrashDirectory))
5788 llvm::sys::fs::create_directories(path: *CrashDirectory);
5789 SmallString<128> Path(*CrashDirectory);
5790 llvm::sys::path::append(path&: Path, a: Prefix);
5791 const char *Middle = !Suffix.empty() ? "-%%%%%%." : "-%%%%%%";
5792 if (std::error_code EC =
5793 llvm::sys::fs::createUniqueFile(Model: Path + Middle + Suffix, ResultPath&: TmpName)) {
5794 Diag(clang::diag::err_unable_to_make_temp) << EC.message();
5795 return "";
5796 }
5797 } else {
5798 if (MultipleArchs && !BoundArch.empty()) {
5799 if (NeedUniqueDirectory) {
5800 TmpName = GetTemporaryDirectory(Prefix);
5801 llvm::sys::path::append(path&: TmpName,
5802 a: Twine(Prefix) + "-" + BoundArch + "." + Suffix);
5803 } else {
5804 TmpName =
5805 GetTemporaryPath(Prefix: (Twine(Prefix) + "-" + BoundArch).str(), Suffix);
5806 }
5807
5808 } else {
5809 TmpName = GetTemporaryPath(Prefix, Suffix);
5810 }
5811 }
5812 return C.addTempFile(Name: C.getArgs().MakeArgString(Str: TmpName));
5813}
5814
5815// Calculate the output path of the module file when compiling a module unit
5816// with the `-fmodule-output` option or `-fmodule-output=` option specified.
5817// The behavior is:
5818// - If `-fmodule-output=` is specfied, then the module file is
5819// writing to the value.
5820// - Otherwise if the output object file of the module unit is specified, the
5821// output path
5822// of the module file should be the same with the output object file except
5823// the corresponding suffix. This requires both `-o` and `-c` are specified.
5824// - Otherwise, the output path of the module file will be the same with the
5825// input with the corresponding suffix.
5826static const char *GetModuleOutputPath(Compilation &C, const JobAction &JA,
5827 const char *BaseInput) {
5828 assert(isa<PrecompileJobAction>(JA) && JA.getType() == types::TY_ModuleFile &&
5829 (C.getArgs().hasArg(options::OPT_fmodule_output) ||
5830 C.getArgs().hasArg(options::OPT_fmodule_output_EQ)));
5831
5832 SmallString<256> OutputPath =
5833 tools::getCXX20NamedModuleOutputPath(Args: C.getArgs(), BaseInput);
5834
5835 return C.addResultFile(Name: C.getArgs().MakeArgString(Str: OutputPath.c_str()), JA: &JA);
5836}
5837
5838const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA,
5839 const char *BaseInput,
5840 StringRef OrigBoundArch, bool AtTopLevel,
5841 bool MultipleArchs,
5842 StringRef OffloadingPrefix) const {
5843 std::string BoundArch = OrigBoundArch.str();
5844 if (is_style_windows(S: llvm::sys::path::Style::native)) {
5845 // BoundArch may contains ':', which is invalid in file names on Windows,
5846 // therefore replace it with '%'.
5847 std::replace(first: BoundArch.begin(), last: BoundArch.end(), old_value: ':', new_value: '@');
5848 }
5849
5850 llvm::PrettyStackTraceString CrashInfo("Computing output path");
5851 // Output to a user requested destination?
5852 if (AtTopLevel && !isa<DsymutilJobAction>(Val: JA) && !isa<VerifyJobAction>(Val: JA)) {
5853 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
5854 return C.addResultFile(Name: FinalOutput->getValue(), JA: &JA);
5855 }
5856
5857 // For /P, preprocess to file named after BaseInput.
5858 if (C.getArgs().hasArg(options::OPT__SLASH_P)) {
5859 assert(AtTopLevel && isa<PreprocessJobAction>(JA));
5860 StringRef BaseName = llvm::sys::path::filename(path: BaseInput);
5861 StringRef NameArg;
5862 if (Arg *A = C.getArgs().getLastArg(options::OPT__SLASH_Fi))
5863 NameArg = A->getValue();
5864 return C.addResultFile(
5865 Name: MakeCLOutputFilename(Args: C.getArgs(), ArgValue: NameArg, BaseName, FileType: types::TY_PP_C),
5866 JA: &JA);
5867 }
5868
5869 // Default to writing to stdout?
5870 if (AtTopLevel && !CCGenDiagnostics && HasPreprocessOutput(JA)) {
5871 return "-";
5872 }
5873
5874 if (JA.getType() == types::TY_ModuleFile &&
5875 C.getArgs().getLastArg(options::OPT_module_file_info)) {
5876 return "-";
5877 }
5878
5879 if (JA.getType() == types::TY_PP_Asm &&
5880 C.getArgs().hasArg(options::OPT_dxc_Fc)) {
5881 StringRef FcValue = C.getArgs().getLastArgValue(options::OPT_dxc_Fc);
5882 // TODO: Should we use `MakeCLOutputFilename` here? If so, we can probably
5883 // handle this as part of the SLASH_Fa handling below.
5884 return C.addResultFile(Name: C.getArgs().MakeArgString(Str: FcValue.str()), JA: &JA);
5885 }
5886
5887 if (JA.getType() == types::TY_Object &&
5888 C.getArgs().hasArg(options::OPT_dxc_Fo)) {
5889 StringRef FoValue = C.getArgs().getLastArgValue(options::OPT_dxc_Fo);
5890 // TODO: Should we use `MakeCLOutputFilename` here? If so, we can probably
5891 // handle this as part of the SLASH_Fo handling below.
5892 return C.addResultFile(Name: C.getArgs().MakeArgString(Str: FoValue.str()), JA: &JA);
5893 }
5894
5895 // Is this the assembly listing for /FA?
5896 if (JA.getType() == types::TY_PP_Asm &&
5897 (C.getArgs().hasArg(options::OPT__SLASH_FA) ||
5898 C.getArgs().hasArg(options::OPT__SLASH_Fa))) {
5899 // Use /Fa and the input filename to determine the asm file name.
5900 StringRef BaseName = llvm::sys::path::filename(path: BaseInput);
5901 StringRef FaValue = C.getArgs().getLastArgValue(options::OPT__SLASH_Fa);
5902 return C.addResultFile(
5903 Name: MakeCLOutputFilename(Args: C.getArgs(), ArgValue: FaValue, BaseName, FileType: JA.getType()),
5904 JA: &JA);
5905 }
5906
5907 if (JA.getType() == types::TY_API_INFO &&
5908 C.getArgs().hasArg(options::OPT_emit_extension_symbol_graphs) &&
5909 C.getArgs().hasArg(options::OPT_o))
5910 Diag(clang::diag::err_drv_unexpected_symbol_graph_output)
5911 << C.getArgs().getLastArgValue(options::OPT_o);
5912
5913 // DXC defaults to standard out when generating assembly. We check this after
5914 // any DXC flags that might specify a file.
5915 if (AtTopLevel && JA.getType() == types::TY_PP_Asm && IsDXCMode())
5916 return "-";
5917
5918 bool SpecifiedModuleOutput =
5919 C.getArgs().hasArg(options::OPT_fmodule_output) ||
5920 C.getArgs().hasArg(options::OPT_fmodule_output_EQ);
5921 if (MultipleArchs && SpecifiedModuleOutput)
5922 Diag(clang::diag::err_drv_module_output_with_multiple_arch);
5923
5924 // If we're emitting a module output with the specified option
5925 // `-fmodule-output`.
5926 if (!AtTopLevel && isa<PrecompileJobAction>(Val: JA) &&
5927 JA.getType() == types::TY_ModuleFile && SpecifiedModuleOutput) {
5928 assert(!C.getArgs().hasArg(options::OPT_modules_reduced_bmi));
5929 return GetModuleOutputPath(C, JA, BaseInput);
5930 }
5931
5932 // Output to a temporary file?
5933 if ((!AtTopLevel && !isSaveTempsEnabled() &&
5934 !C.getArgs().hasArg(options::OPT__SLASH_Fo)) ||
5935 CCGenDiagnostics) {
5936 StringRef Name = llvm::sys::path::filename(path: BaseInput);
5937 std::pair<StringRef, StringRef> Split = Name.split(Separator: '.');
5938 const char *Suffix =
5939 types::getTypeTempSuffix(Id: JA.getType(), CLStyle: IsCLMode() || IsDXCMode());
5940 // The non-offloading toolchain on Darwin requires deterministic input
5941 // file name for binaries to be deterministic, therefore it needs unique
5942 // directory.
5943 llvm::Triple Triple(C.getDriver().getTargetTriple());
5944 bool NeedUniqueDirectory =
5945 (JA.getOffloadingDeviceKind() == Action::OFK_None ||
5946 JA.getOffloadingDeviceKind() == Action::OFK_Host) &&
5947 Triple.isOSDarwin();
5948 return CreateTempFile(C, Prefix: Split.first, Suffix, MultipleArchs, BoundArch,
5949 NeedUniqueDirectory);
5950 }
5951
5952 SmallString<128> BasePath(BaseInput);
5953 SmallString<128> ExternalPath("");
5954 StringRef BaseName;
5955
5956 // Dsymutil actions should use the full path.
5957 if (isa<DsymutilJobAction>(JA) && C.getArgs().hasArg(options::OPT_dsym_dir)) {
5958 ExternalPath += C.getArgs().getLastArg(options::OPT_dsym_dir)->getValue();
5959 // We use posix style here because the tests (specifically
5960 // darwin-dsymutil.c) demonstrate that posix style paths are acceptable
5961 // even on Windows and if we don't then the similar test covering this
5962 // fails.
5963 llvm::sys::path::append(path&: ExternalPath, style: llvm::sys::path::Style::posix,
5964 a: llvm::sys::path::filename(path: BasePath));
5965 BaseName = ExternalPath;
5966 } else if (isa<DsymutilJobAction>(Val: JA) || isa<VerifyJobAction>(Val: JA))
5967 BaseName = BasePath;
5968 else
5969 BaseName = llvm::sys::path::filename(path: BasePath);
5970
5971 // Determine what the derived output name should be.
5972 const char *NamedOutput;
5973
5974 if ((JA.getType() == types::TY_Object || JA.getType() == types::TY_LTO_BC) &&
5975 C.getArgs().hasArg(options::OPT__SLASH_Fo, options::OPT__SLASH_o)) {
5976 // The /Fo or /o flag decides the object filename.
5977 StringRef Val =
5978 C.getArgs()
5979 .getLastArg(options::OPT__SLASH_Fo, options::OPT__SLASH_o)
5980 ->getValue();
5981 NamedOutput =
5982 MakeCLOutputFilename(Args: C.getArgs(), ArgValue: Val, BaseName, FileType: types::TY_Object);
5983 } else if (JA.getType() == types::TY_Image &&
5984 C.getArgs().hasArg(options::OPT__SLASH_Fe,
5985 options::OPT__SLASH_o)) {
5986 // The /Fe or /o flag names the linked file.
5987 StringRef Val =
5988 C.getArgs()
5989 .getLastArg(options::OPT__SLASH_Fe, options::OPT__SLASH_o)
5990 ->getValue();
5991 NamedOutput =
5992 MakeCLOutputFilename(Args: C.getArgs(), ArgValue: Val, BaseName, FileType: types::TY_Image);
5993 } else if (JA.getType() == types::TY_Image) {
5994 if (IsCLMode()) {
5995 // clang-cl uses BaseName for the executable name.
5996 NamedOutput =
5997 MakeCLOutputFilename(Args: C.getArgs(), ArgValue: "", BaseName, FileType: types::TY_Image);
5998 } else {
5999 SmallString<128> Output(getDefaultImageName());
6000 // HIP image for device compilation with -fno-gpu-rdc is per compilation
6001 // unit.
6002 bool IsHIPNoRDC = JA.getOffloadingDeviceKind() == Action::OFK_HIP &&
6003 !C.getArgs().hasFlag(options::OPT_fgpu_rdc,
6004 options::OPT_fno_gpu_rdc, false);
6005 bool UseOutExtension = IsHIPNoRDC || isa<OffloadPackagerJobAction>(Val: JA);
6006 if (UseOutExtension) {
6007 Output = BaseName;
6008 llvm::sys::path::replace_extension(path&: Output, extension: "");
6009 }
6010 Output += OffloadingPrefix;
6011 if (MultipleArchs && !BoundArch.empty()) {
6012 Output += "-";
6013 Output.append(RHS: BoundArch);
6014 }
6015 if (UseOutExtension)
6016 Output += ".out";
6017 NamedOutput = C.getArgs().MakeArgString(Str: Output.c_str());
6018 }
6019 } else if (JA.getType() == types::TY_PCH && IsCLMode()) {
6020 NamedOutput = C.getArgs().MakeArgString(Str: GetClPchPath(C, BaseName));
6021 } else if ((JA.getType() == types::TY_Plist || JA.getType() == types::TY_AST) &&
6022 C.getArgs().hasArg(options::OPT__SLASH_o)) {
6023 StringRef Val =
6024 C.getArgs()
6025 .getLastArg(options::OPT__SLASH_o)
6026 ->getValue();
6027 NamedOutput =
6028 MakeCLOutputFilename(Args: C.getArgs(), ArgValue: Val, BaseName, FileType: types::TY_Object);
6029 } else {
6030 const char *Suffix =
6031 types::getTypeTempSuffix(Id: JA.getType(), CLStyle: IsCLMode() || IsDXCMode());
6032 assert(Suffix && "All types used for output should have a suffix.");
6033
6034 std::string::size_type End = std::string::npos;
6035 if (!types::appendSuffixForType(Id: JA.getType()))
6036 End = BaseName.rfind(C: '.');
6037 SmallString<128> Suffixed(BaseName.substr(Start: 0, N: End));
6038 Suffixed += OffloadingPrefix;
6039 if (MultipleArchs && !BoundArch.empty()) {
6040 Suffixed += "-";
6041 Suffixed.append(RHS: BoundArch);
6042 }
6043 // When using both -save-temps and -emit-llvm, use a ".tmp.bc" suffix for
6044 // the unoptimized bitcode so that it does not get overwritten by the ".bc"
6045 // optimized bitcode output.
6046 auto IsAMDRDCInCompilePhase = [](const JobAction &JA,
6047 const llvm::opt::DerivedArgList &Args) {
6048 // The relocatable compilation in HIP and OpenMP implies -emit-llvm.
6049 // Similarly, use a ".tmp.bc" suffix for the unoptimized bitcode
6050 // (generated in the compile phase.)
6051 const ToolChain *TC = JA.getOffloadingToolChain();
6052 return isa<CompileJobAction>(JA) &&
6053 ((JA.getOffloadingDeviceKind() == Action::OFK_HIP &&
6054 Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
6055 false)) ||
6056 (JA.getOffloadingDeviceKind() == Action::OFK_OpenMP && TC &&
6057 TC->getTriple().isAMDGPU()));
6058 };
6059 if (!AtTopLevel && JA.getType() == types::TY_LLVM_BC &&
6060 (C.getArgs().hasArg(options::OPT_emit_llvm) ||
6061 IsAMDRDCInCompilePhase(JA, C.getArgs())))
6062 Suffixed += ".tmp";
6063 Suffixed += '.';
6064 Suffixed += Suffix;
6065 NamedOutput = C.getArgs().MakeArgString(Str: Suffixed.c_str());
6066 }
6067
6068 // Prepend object file path if -save-temps=obj
6069 if (!AtTopLevel && isSaveTempsObj() && C.getArgs().hasArg(options::OPT_o) &&
6070 JA.getType() != types::TY_PCH) {
6071 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
6072 SmallString<128> TempPath(FinalOutput->getValue());
6073 llvm::sys::path::remove_filename(path&: TempPath);
6074 StringRef OutputFileName = llvm::sys::path::filename(path: NamedOutput);
6075 llvm::sys::path::append(path&: TempPath, a: OutputFileName);
6076 NamedOutput = C.getArgs().MakeArgString(Str: TempPath.c_str());
6077 }
6078
6079 // If we're saving temps and the temp file conflicts with the input file,
6080 // then avoid overwriting input file.
6081 if (!AtTopLevel && isSaveTempsEnabled() && NamedOutput == BaseName) {
6082 bool SameFile = false;
6083 SmallString<256> Result;
6084 llvm::sys::fs::current_path(result&: Result);
6085 llvm::sys::path::append(path&: Result, a: BaseName);
6086 llvm::sys::fs::equivalent(A: BaseInput, B: Result.c_str(), result&: SameFile);
6087 // Must share the same path to conflict.
6088 if (SameFile) {
6089 StringRef Name = llvm::sys::path::filename(path: BaseInput);
6090 std::pair<StringRef, StringRef> Split = Name.split(Separator: '.');
6091 std::string TmpName = GetTemporaryPath(
6092 Prefix: Split.first,
6093 Suffix: types::getTypeTempSuffix(Id: JA.getType(), CLStyle: IsCLMode() || IsDXCMode()));
6094 return C.addTempFile(Name: C.getArgs().MakeArgString(Str: TmpName));
6095 }
6096 }
6097
6098 // As an annoying special case, PCH generation doesn't strip the pathname.
6099 if (JA.getType() == types::TY_PCH && !IsCLMode()) {
6100 llvm::sys::path::remove_filename(path&: BasePath);
6101 if (BasePath.empty())
6102 BasePath = NamedOutput;
6103 else
6104 llvm::sys::path::append(path&: BasePath, a: NamedOutput);
6105 return C.addResultFile(Name: C.getArgs().MakeArgString(Str: BasePath.c_str()), JA: &JA);
6106 }
6107
6108 return C.addResultFile(Name: NamedOutput, JA: &JA);
6109}
6110
6111std::string Driver::GetFilePath(StringRef Name, const ToolChain &TC) const {
6112 // Search for Name in a list of paths.
6113 auto SearchPaths = [&](const llvm::SmallVectorImpl<std::string> &P)
6114 -> std::optional<std::string> {
6115 // Respect a limited subset of the '-Bprefix' functionality in GCC by
6116 // attempting to use this prefix when looking for file paths.
6117 for (const auto &Dir : P) {
6118 if (Dir.empty())
6119 continue;
6120 SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(pos: 1) : Dir);
6121 llvm::sys::path::append(path&: P, a: Name);
6122 if (llvm::sys::fs::exists(Path: Twine(P)))
6123 return std::string(P);
6124 }
6125 return std::nullopt;
6126 };
6127
6128 if (auto P = SearchPaths(PrefixDirs))
6129 return *P;
6130
6131 SmallString<128> R(ResourceDir);
6132 llvm::sys::path::append(path&: R, a: Name);
6133 if (llvm::sys::fs::exists(Path: Twine(R)))
6134 return std::string(R);
6135
6136 SmallString<128> P(TC.getCompilerRTPath());
6137 llvm::sys::path::append(path&: P, a: Name);
6138 if (llvm::sys::fs::exists(Path: Twine(P)))
6139 return std::string(P);
6140
6141 SmallString<128> D(Dir);
6142 llvm::sys::path::append(path&: D, a: "..", b: Name);
6143 if (llvm::sys::fs::exists(Path: Twine(D)))
6144 return std::string(D);
6145
6146 if (auto P = SearchPaths(TC.getLibraryPaths()))
6147 return *P;
6148
6149 if (auto P = SearchPaths(TC.getFilePaths()))
6150 return *P;
6151
6152 return std::string(Name);
6153}
6154
6155void Driver::generatePrefixedToolNames(
6156 StringRef Tool, const ToolChain &TC,
6157 SmallVectorImpl<std::string> &Names) const {
6158 // FIXME: Needs a better variable than TargetTriple
6159 Names.emplace_back(Args: (TargetTriple + "-" + Tool).str());
6160 Names.emplace_back(Args&: Tool);
6161}
6162
6163static bool ScanDirForExecutable(SmallString<128> &Dir, StringRef Name) {
6164 llvm::sys::path::append(path&: Dir, a: Name);
6165 if (llvm::sys::fs::can_execute(Path: Twine(Dir)))
6166 return true;
6167 llvm::sys::path::remove_filename(path&: Dir);
6168 return false;
6169}
6170
6171std::string Driver::GetProgramPath(StringRef Name, const ToolChain &TC) const {
6172 SmallVector<std::string, 2> TargetSpecificExecutables;
6173 generatePrefixedToolNames(Tool: Name, TC, Names&: TargetSpecificExecutables);
6174
6175 // Respect a limited subset of the '-Bprefix' functionality in GCC by
6176 // attempting to use this prefix when looking for program paths.
6177 for (const auto &PrefixDir : PrefixDirs) {
6178 if (llvm::sys::fs::is_directory(Path: PrefixDir)) {
6179 SmallString<128> P(PrefixDir);
6180 if (ScanDirForExecutable(Dir&: P, Name))
6181 return std::string(P);
6182 } else {
6183 SmallString<128> P((PrefixDir + Name).str());
6184 if (llvm::sys::fs::can_execute(Path: Twine(P)))
6185 return std::string(P);
6186 }
6187 }
6188
6189 const ToolChain::path_list &List = TC.getProgramPaths();
6190 for (const auto &TargetSpecificExecutable : TargetSpecificExecutables) {
6191 // For each possible name of the tool look for it in
6192 // program paths first, then the path.
6193 // Higher priority names will be first, meaning that
6194 // a higher priority name in the path will be found
6195 // instead of a lower priority name in the program path.
6196 // E.g. <triple>-gcc on the path will be found instead
6197 // of gcc in the program path
6198 for (const auto &Path : List) {
6199 SmallString<128> P(Path);
6200 if (ScanDirForExecutable(Dir&: P, Name: TargetSpecificExecutable))
6201 return std::string(P);
6202 }
6203
6204 // Fall back to the path
6205 if (llvm::ErrorOr<std::string> P =
6206 llvm::sys::findProgramByName(Name: TargetSpecificExecutable))
6207 return *P;
6208 }
6209
6210 return std::string(Name);
6211}
6212
6213std::string Driver::GetStdModuleManifestPath(const Compilation &C,
6214 const ToolChain &TC) const {
6215 std::string error = "<NOT PRESENT>";
6216
6217 switch (TC.GetCXXStdlibType(Args: C.getArgs())) {
6218 case ToolChain::CST_Libcxx: {
6219 auto evaluate = [&](const char *library) -> std::optional<std::string> {
6220 std::string lib = GetFilePath(Name: library, TC);
6221
6222 // Note when there are multiple flavours of libc++ the module json needs
6223 // to look at the command-line arguments for the proper json. These
6224 // flavours do not exist at the moment, but there are plans to provide a
6225 // variant that is built with sanitizer instrumentation enabled.
6226
6227 // For example
6228 // StringRef modules = [&] {
6229 // const SanitizerArgs &Sanitize = TC.getSanitizerArgs(C.getArgs());
6230 // if (Sanitize.needsAsanRt())
6231 // return "libc++.modules-asan.json";
6232 // return "libc++.modules.json";
6233 // }();
6234
6235 SmallString<128> path(lib.begin(), lib.end());
6236 llvm::sys::path::remove_filename(path);
6237 llvm::sys::path::append(path, a: "libc++.modules.json");
6238 if (TC.getVFS().exists(Path: path))
6239 return static_cast<std::string>(path);
6240
6241 return {};
6242 };
6243
6244 if (std::optional<std::string> result = evaluate("libc++.so"); result)
6245 return *result;
6246
6247 return evaluate("libc++.a").value_or(u&: error);
6248 }
6249
6250 case ToolChain::CST_Libstdcxx:
6251 // libstdc++ does not provide Standard library modules yet.
6252 return error;
6253 }
6254
6255 return error;
6256}
6257
6258std::string Driver::GetTemporaryPath(StringRef Prefix, StringRef Suffix) const {
6259 SmallString<128> Path;
6260 std::error_code EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, ResultPath&: Path);
6261 if (EC) {
6262 Diag(clang::diag::err_unable_to_make_temp) << EC.message();
6263 return "";
6264 }
6265
6266 return std::string(Path);
6267}
6268
6269std::string Driver::GetTemporaryDirectory(StringRef Prefix) const {
6270 SmallString<128> Path;
6271 std::error_code EC = llvm::sys::fs::createUniqueDirectory(Prefix, ResultPath&: Path);
6272 if (EC) {
6273 Diag(clang::diag::err_unable_to_make_temp) << EC.message();
6274 return "";
6275 }
6276
6277 return std::string(Path);
6278}
6279
6280std::string Driver::GetClPchPath(Compilation &C, StringRef BaseName) const {
6281 SmallString<128> Output;
6282 if (Arg *FpArg = C.getArgs().getLastArg(options::OPT__SLASH_Fp)) {
6283 // FIXME: If anybody needs it, implement this obscure rule:
6284 // "If you specify a directory without a file name, the default file name
6285 // is VCx0.pch., where x is the major version of Visual C++ in use."
6286 Output = FpArg->getValue();
6287
6288 // "If you do not specify an extension as part of the path name, an
6289 // extension of .pch is assumed. "
6290 if (!llvm::sys::path::has_extension(path: Output))
6291 Output += ".pch";
6292 } else {
6293 if (Arg *YcArg = C.getArgs().getLastArg(options::OPT__SLASH_Yc))
6294 Output = YcArg->getValue();
6295 if (Output.empty())
6296 Output = BaseName;
6297 llvm::sys::path::replace_extension(path&: Output, extension: ".pch");
6298 }
6299 return std::string(Output);
6300}
6301
6302const ToolChain &Driver::getToolChain(const ArgList &Args,
6303 const llvm::Triple &Target) const {
6304
6305 auto &TC = ToolChains[Target.str()];
6306 if (!TC) {
6307 switch (Target.getOS()) {
6308 case llvm::Triple::AIX:
6309 TC = std::make_unique<toolchains::AIX>(args: *this, args: Target, args: Args);
6310 break;
6311 case llvm::Triple::Haiku:
6312 TC = std::make_unique<toolchains::Haiku>(args: *this, args: Target, args: Args);
6313 break;
6314 case llvm::Triple::Darwin:
6315 case llvm::Triple::MacOSX:
6316 case llvm::Triple::IOS:
6317 case llvm::Triple::TvOS:
6318 case llvm::Triple::WatchOS:
6319 case llvm::Triple::XROS:
6320 case llvm::Triple::DriverKit:
6321 TC = std::make_unique<toolchains::DarwinClang>(args: *this, args: Target, args: Args);
6322 break;
6323 case llvm::Triple::DragonFly:
6324 TC = std::make_unique<toolchains::DragonFly>(args: *this, args: Target, args: Args);
6325 break;
6326 case llvm::Triple::OpenBSD:
6327 TC = std::make_unique<toolchains::OpenBSD>(args: *this, args: Target, args: Args);
6328 break;
6329 case llvm::Triple::NetBSD:
6330 TC = std::make_unique<toolchains::NetBSD>(args: *this, args: Target, args: Args);
6331 break;
6332 case llvm::Triple::FreeBSD:
6333 if (Target.isPPC())
6334 TC = std::make_unique<toolchains::PPCFreeBSDToolChain>(args: *this, args: Target,
6335 args: Args);
6336 else
6337 TC = std::make_unique<toolchains::FreeBSD>(args: *this, args: Target, args: Args);
6338 break;
6339 case llvm::Triple::Linux:
6340 case llvm::Triple::ELFIAMCU:
6341 if (Target.getArch() == llvm::Triple::hexagon)
6342 TC = std::make_unique<toolchains::HexagonToolChain>(args: *this, args: Target,
6343 args: Args);
6344 else if ((Target.getVendor() == llvm::Triple::MipsTechnologies) &&
6345 !Target.hasEnvironment())
6346 TC = std::make_unique<toolchains::MipsLLVMToolChain>(args: *this, args: Target,
6347 args: Args);
6348 else if (Target.isPPC())
6349 TC = std::make_unique<toolchains::PPCLinuxToolChain>(args: *this, args: Target,
6350 args: Args);
6351 else if (Target.getArch() == llvm::Triple::ve)
6352 TC = std::make_unique<toolchains::VEToolChain>(args: *this, args: Target, args: Args);
6353 else if (Target.isOHOSFamily())
6354 TC = std::make_unique<toolchains::OHOS>(args: *this, args: Target, args: Args);
6355 else
6356 TC = std::make_unique<toolchains::Linux>(args: *this, args: Target, args: Args);
6357 break;
6358 case llvm::Triple::NaCl:
6359 TC = std::make_unique<toolchains::NaClToolChain>(args: *this, args: Target, args: Args);
6360 break;
6361 case llvm::Triple::Fuchsia:
6362 TC = std::make_unique<toolchains::Fuchsia>(args: *this, args: Target, args: Args);
6363 break;
6364 case llvm::Triple::Solaris:
6365 TC = std::make_unique<toolchains::Solaris>(args: *this, args: Target, args: Args);
6366 break;
6367 case llvm::Triple::CUDA:
6368 TC = std::make_unique<toolchains::NVPTXToolChain>(args: *this, args: Target, args: Args);
6369 break;
6370 case llvm::Triple::AMDHSA:
6371 TC = std::make_unique<toolchains::ROCMToolChain>(args: *this, args: Target, args: Args);
6372 break;
6373 case llvm::Triple::AMDPAL:
6374 case llvm::Triple::Mesa3D:
6375 TC = std::make_unique<toolchains::AMDGPUToolChain>(args: *this, args: Target, args: Args);
6376 break;
6377 case llvm::Triple::Win32:
6378 switch (Target.getEnvironment()) {
6379 default:
6380 if (Target.isOSBinFormatELF())
6381 TC = std::make_unique<toolchains::Generic_ELF>(args: *this, args: Target, args: Args);
6382 else if (Target.isOSBinFormatMachO())
6383 TC = std::make_unique<toolchains::MachO>(args: *this, args: Target, args: Args);
6384 else
6385 TC = std::make_unique<toolchains::Generic_GCC>(args: *this, args: Target, args: Args);
6386 break;
6387 case llvm::Triple::GNU:
6388 TC = std::make_unique<toolchains::MinGW>(args: *this, args: Target, args: Args);
6389 break;
6390 case llvm::Triple::Itanium:
6391 TC = std::make_unique<toolchains::CrossWindowsToolChain>(args: *this, args: Target,
6392 args: Args);
6393 break;
6394 case llvm::Triple::MSVC:
6395 case llvm::Triple::UnknownEnvironment:
6396 if (Args.getLastArgValue(options::OPT_fuse_ld_EQ)
6397 .starts_with_insensitive("bfd"))
6398 TC = std::make_unique<toolchains::CrossWindowsToolChain>(
6399 args: *this, args: Target, args: Args);
6400 else
6401 TC =
6402 std::make_unique<toolchains::MSVCToolChain>(args: *this, args: Target, args: Args);
6403 break;
6404 }
6405 break;
6406 case llvm::Triple::PS4:
6407 TC = std::make_unique<toolchains::PS4CPU>(args: *this, args: Target, args: Args);
6408 break;
6409 case llvm::Triple::PS5:
6410 TC = std::make_unique<toolchains::PS5CPU>(args: *this, args: Target, args: Args);
6411 break;
6412 case llvm::Triple::Hurd:
6413 TC = std::make_unique<toolchains::Hurd>(args: *this, args: Target, args: Args);
6414 break;
6415 case llvm::Triple::LiteOS:
6416 TC = std::make_unique<toolchains::OHOS>(args: *this, args: Target, args: Args);
6417 break;
6418 case llvm::Triple::ZOS:
6419 TC = std::make_unique<toolchains::ZOS>(args: *this, args: Target, args: Args);
6420 break;
6421 case llvm::Triple::ShaderModel:
6422 TC = std::make_unique<toolchains::HLSLToolChain>(args: *this, args: Target, args: Args);
6423 break;
6424 default:
6425 // Of these targets, Hexagon is the only one that might have
6426 // an OS of Linux, in which case it got handled above already.
6427 switch (Target.getArch()) {
6428 case llvm::Triple::tce:
6429 TC = std::make_unique<toolchains::TCEToolChain>(args: *this, args: Target, args: Args);
6430 break;
6431 case llvm::Triple::tcele:
6432 TC = std::make_unique<toolchains::TCELEToolChain>(args: *this, args: Target, args: Args);
6433 break;
6434 case llvm::Triple::hexagon:
6435 TC = std::make_unique<toolchains::HexagonToolChain>(args: *this, args: Target,
6436 args: Args);
6437 break;
6438 case llvm::Triple::lanai:
6439 TC = std::make_unique<toolchains::LanaiToolChain>(args: *this, args: Target, args: Args);
6440 break;
6441 case llvm::Triple::xcore:
6442 TC = std::make_unique<toolchains::XCoreToolChain>(args: *this, args: Target, args: Args);
6443 break;
6444 case llvm::Triple::wasm32:
6445 case llvm::Triple::wasm64:
6446 TC = std::make_unique<toolchains::WebAssembly>(args: *this, args: Target, args: Args);
6447 break;
6448 case llvm::Triple::avr:
6449 TC = std::make_unique<toolchains::AVRToolChain>(args: *this, args: Target, args: Args);
6450 break;
6451 case llvm::Triple::msp430:
6452 TC =
6453 std::make_unique<toolchains::MSP430ToolChain>(args: *this, args: Target, args: Args);
6454 break;
6455 case llvm::Triple::riscv32:
6456 case llvm::Triple::riscv64:
6457 if (toolchains::RISCVToolChain::hasGCCToolchain(D: *this, Args))
6458 TC =
6459 std::make_unique<toolchains::RISCVToolChain>(args: *this, args: Target, args: Args);
6460 else
6461 TC = std::make_unique<toolchains::BareMetal>(args: *this, args: Target, args: Args);
6462 break;
6463 case llvm::Triple::ve:
6464 TC = std::make_unique<toolchains::VEToolChain>(args: *this, args: Target, args: Args);
6465 break;
6466 case llvm::Triple::spirv32:
6467 case llvm::Triple::spirv64:
6468 TC = std::make_unique<toolchains::SPIRVToolChain>(args: *this, args: Target, args: Args);
6469 break;
6470 case llvm::Triple::csky:
6471 TC = std::make_unique<toolchains::CSKYToolChain>(args: *this, args: Target, args: Args);
6472 break;
6473 default:
6474 if (toolchains::BareMetal::handlesTarget(Triple: Target))
6475 TC = std::make_unique<toolchains::BareMetal>(args: *this, args: Target, args: Args);
6476 else if (Target.isOSBinFormatELF())
6477 TC = std::make_unique<toolchains::Generic_ELF>(args: *this, args: Target, args: Args);
6478 else if (Target.isOSBinFormatMachO())
6479 TC = std::make_unique<toolchains::MachO>(args: *this, args: Target, args: Args);
6480 else
6481 TC = std::make_unique<toolchains::Generic_GCC>(args: *this, args: Target, args: Args);
6482 }
6483 }
6484 }
6485
6486 return *TC;
6487}
6488
6489const ToolChain &Driver::getOffloadingDeviceToolChain(
6490 const ArgList &Args, const llvm::Triple &Target, const ToolChain &HostTC,
6491 const Action::OffloadKind &TargetDeviceOffloadKind) const {
6492 // Use device / host triples as the key into the ToolChains map because the
6493 // device ToolChain we create depends on both.
6494 auto &TC = ToolChains[Target.str() + "/" + HostTC.getTriple().str()];
6495 if (!TC) {
6496 // Categorized by offload kind > arch rather than OS > arch like
6497 // the normal getToolChain call, as it seems a reasonable way to categorize
6498 // things.
6499 switch (TargetDeviceOffloadKind) {
6500 case Action::OFK_HIP: {
6501 if (Target.getArch() == llvm::Triple::amdgcn &&
6502 Target.getVendor() == llvm::Triple::AMD &&
6503 Target.getOS() == llvm::Triple::AMDHSA)
6504 TC = std::make_unique<toolchains::HIPAMDToolChain>(args: *this, args: Target,
6505 args: HostTC, args: Args);
6506 else if (Target.getArch() == llvm::Triple::spirv64 &&
6507 Target.getVendor() == llvm::Triple::UnknownVendor &&
6508 Target.getOS() == llvm::Triple::UnknownOS)
6509 TC = std::make_unique<toolchains::HIPSPVToolChain>(args: *this, args: Target,
6510 args: HostTC, args: Args);
6511 break;
6512 }
6513 default:
6514 break;
6515 }
6516 }
6517
6518 return *TC;
6519}
6520
6521bool Driver::ShouldUseClangCompiler(const JobAction &JA) const {
6522 // Say "no" if there is not exactly one input of a type clang understands.
6523 if (JA.size() != 1 ||
6524 !types::isAcceptedByClang(Id: (*JA.input_begin())->getType()))
6525 return false;
6526
6527 // And say "no" if this is not a kind of action clang understands.
6528 if (!isa<PreprocessJobAction>(Val: JA) && !isa<PrecompileJobAction>(Val: JA) &&
6529 !isa<CompileJobAction>(Val: JA) && !isa<BackendJobAction>(Val: JA) &&
6530 !isa<ExtractAPIJobAction>(Val: JA))
6531 return false;
6532
6533 return true;
6534}
6535
6536bool Driver::ShouldUseFlangCompiler(const JobAction &JA) const {
6537 // Say "no" if there is not exactly one input of a type flang understands.
6538 if (JA.size() != 1 ||
6539 !types::isAcceptedByFlang(Id: (*JA.input_begin())->getType()))
6540 return false;
6541
6542 // And say "no" if this is not a kind of action flang understands.
6543 if (!isa<PreprocessJobAction>(Val: JA) && !isa<CompileJobAction>(Val: JA) &&
6544 !isa<BackendJobAction>(Val: JA))
6545 return false;
6546
6547 return true;
6548}
6549
6550bool Driver::ShouldEmitStaticLibrary(const ArgList &Args) const {
6551 // Only emit static library if the flag is set explicitly.
6552 if (Args.hasArg(options::OPT_emit_static_lib))
6553 return true;
6554 return false;
6555}
6556
6557/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the
6558/// grouped values as integers. Numbers which are not provided are set to 0.
6559///
6560/// \return True if the entire string was parsed (9.2), or all groups were
6561/// parsed (10.3.5extrastuff).
6562bool Driver::GetReleaseVersion(StringRef Str, unsigned &Major, unsigned &Minor,
6563 unsigned &Micro, bool &HadExtra) {
6564 HadExtra = false;
6565
6566 Major = Minor = Micro = 0;
6567 if (Str.empty())
6568 return false;
6569
6570 if (Str.consumeInteger(Radix: 10, Result&: Major))
6571 return false;
6572 if (Str.empty())
6573 return true;
6574 if (!Str.consume_front(Prefix: "."))
6575 return false;
6576
6577 if (Str.consumeInteger(Radix: 10, Result&: Minor))
6578 return false;
6579 if (Str.empty())
6580 return true;
6581 if (!Str.consume_front(Prefix: "."))
6582 return false;
6583
6584 if (Str.consumeInteger(Radix: 10, Result&: Micro))
6585 return false;
6586 if (!Str.empty())
6587 HadExtra = true;
6588 return true;
6589}
6590
6591/// Parse digits from a string \p Str and fulfill \p Digits with
6592/// the parsed numbers. This method assumes that the max number of
6593/// digits to look for is equal to Digits.size().
6594///
6595/// \return True if the entire string was parsed and there are
6596/// no extra characters remaining at the end.
6597bool Driver::GetReleaseVersion(StringRef Str,
6598 MutableArrayRef<unsigned> Digits) {
6599 if (Str.empty())
6600 return false;
6601
6602 unsigned CurDigit = 0;
6603 while (CurDigit < Digits.size()) {
6604 unsigned Digit;
6605 if (Str.consumeInteger(Radix: 10, Result&: Digit))
6606 return false;
6607 Digits[CurDigit] = Digit;
6608 if (Str.empty())
6609 return true;
6610 if (!Str.consume_front(Prefix: "."))
6611 return false;
6612 CurDigit++;
6613 }
6614
6615 // More digits than requested, bail out...
6616 return false;
6617}
6618
6619llvm::opt::Visibility
6620Driver::getOptionVisibilityMask(bool UseDriverMode) const {
6621 if (!UseDriverMode)
6622 return llvm::opt::Visibility(options::ClangOption);
6623 if (IsCLMode())
6624 return llvm::opt::Visibility(options::CLOption);
6625 if (IsDXCMode())
6626 return llvm::opt::Visibility(options::DXCOption);
6627 if (IsFlangMode()) {
6628 return llvm::opt::Visibility(options::FlangOption);
6629 }
6630 return llvm::opt::Visibility(options::ClangOption);
6631}
6632
6633const char *Driver::getExecutableForDriverMode(DriverMode Mode) {
6634 switch (Mode) {
6635 case GCCMode:
6636 return "clang";
6637 case GXXMode:
6638 return "clang++";
6639 case CPPMode:
6640 return "clang-cpp";
6641 case CLMode:
6642 return "clang-cl";
6643 case FlangMode:
6644 return "flang";
6645 case DXCMode:
6646 return "clang-dxc";
6647 }
6648
6649 llvm_unreachable("Unhandled Mode");
6650}
6651
6652bool clang::driver::isOptimizationLevelFast(const ArgList &Args) {
6653 return Args.hasFlag(options::OPT_Ofast, options::OPT_O_Group, false);
6654}
6655
6656bool clang::driver::willEmitRemarks(const ArgList &Args) {
6657 // -fsave-optimization-record enables it.
6658 if (Args.hasFlag(options::OPT_fsave_optimization_record,
6659 options::OPT_fno_save_optimization_record, false))
6660 return true;
6661
6662 // -fsave-optimization-record=<format> enables it as well.
6663 if (Args.hasFlag(options::OPT_fsave_optimization_record_EQ,
6664 options::OPT_fno_save_optimization_record, false))
6665 return true;
6666
6667 // -foptimization-record-file alone enables it too.
6668 if (Args.hasFlag(options::OPT_foptimization_record_file_EQ,
6669 options::OPT_fno_save_optimization_record, false))
6670 return true;
6671
6672 // -foptimization-record-passes alone enables it too.
6673 if (Args.hasFlag(options::OPT_foptimization_record_passes_EQ,
6674 options::OPT_fno_save_optimization_record, false))
6675 return true;
6676 return false;
6677}
6678
6679llvm::StringRef clang::driver::getDriverMode(StringRef ProgName,
6680 ArrayRef<const char *> Args) {
6681 static StringRef OptName =
6682 getDriverOptTable().getOption(options::OPT_driver_mode).getPrefixedName();
6683 llvm::StringRef Opt;
6684 for (StringRef Arg : Args) {
6685 if (!Arg.starts_with(Prefix: OptName))
6686 continue;
6687 Opt = Arg;
6688 }
6689 if (Opt.empty())
6690 Opt = ToolChain::getTargetAndModeFromProgramName(ProgName).DriverMode;
6691 return Opt.consume_front(Prefix: OptName) ? Opt : "";
6692}
6693
6694bool driver::IsClangCL(StringRef DriverMode) { return DriverMode.equals(RHS: "cl"); }
6695
6696llvm::Error driver::expandResponseFiles(SmallVectorImpl<const char *> &Args,
6697 bool ClangCLMode,
6698 llvm::BumpPtrAllocator &Alloc,
6699 llvm::vfs::FileSystem *FS) {
6700 // Parse response files using the GNU syntax, unless we're in CL mode. There
6701 // are two ways to put clang in CL compatibility mode: ProgName is either
6702 // clang-cl or cl, or --driver-mode=cl is on the command line. The normal
6703 // command line parsing can't happen until after response file parsing, so we
6704 // have to manually search for a --driver-mode=cl argument the hard way.
6705 // Finally, our -cc1 tools don't care which tokenization mode we use because
6706 // response files written by clang will tokenize the same way in either mode.
6707 enum { Default, POSIX, Windows } RSPQuoting = Default;
6708 for (const char *F : Args) {
6709 if (strcmp(s1: F, s2: "--rsp-quoting=posix") == 0)
6710 RSPQuoting = POSIX;
6711 else if (strcmp(s1: F, s2: "--rsp-quoting=windows") == 0)
6712 RSPQuoting = Windows;
6713 }
6714
6715 // Determines whether we want nullptr markers in Args to indicate response
6716 // files end-of-lines. We only use this for the /LINK driver argument with
6717 // clang-cl.exe on Windows.
6718 bool MarkEOLs = ClangCLMode;
6719
6720 llvm::cl::TokenizerCallback Tokenizer;
6721 if (RSPQuoting == Windows || (RSPQuoting == Default && ClangCLMode))
6722 Tokenizer = &llvm::cl::TokenizeWindowsCommandLine;
6723 else
6724 Tokenizer = &llvm::cl::TokenizeGNUCommandLine;
6725
6726 if (MarkEOLs && Args.size() > 1 && StringRef(Args[1]).starts_with(Prefix: "-cc1"))
6727 MarkEOLs = false;
6728
6729 llvm::cl::ExpansionContext ECtx(Alloc, Tokenizer);
6730 ECtx.setMarkEOLs(MarkEOLs);
6731 if (FS)
6732 ECtx.setVFS(FS);
6733
6734 if (llvm::Error Err = ECtx.expandResponseFiles(Argv&: Args))
6735 return Err;
6736
6737 // If -cc1 came from a response file, remove the EOL sentinels.
6738 auto FirstArg = llvm::find_if(Range: llvm::drop_begin(RangeOrContainer&: Args),
6739 P: [](const char *A) { return A != nullptr; });
6740 if (FirstArg != Args.end() && StringRef(*FirstArg).starts_with(Prefix: "-cc1")) {
6741 // If -cc1 came from a response file, remove the EOL sentinels.
6742 if (MarkEOLs) {
6743 auto newEnd = std::remove(first: Args.begin(), last: Args.end(), value: nullptr);
6744 Args.resize(N: newEnd - Args.begin());
6745 }
6746 }
6747
6748 return llvm::Error::success();
6749}
6750
6751static const char *GetStableCStr(llvm::StringSet<> &SavedStrings, StringRef S) {
6752 return SavedStrings.insert(key: S).first->getKeyData();
6753}
6754
6755/// Apply a list of edits to the input argument lists.
6756///
6757/// The input string is a space separated list of edits to perform,
6758/// they are applied in order to the input argument lists. Edits
6759/// should be one of the following forms:
6760///
6761/// '#': Silence information about the changes to the command line arguments.
6762///
6763/// '^': Add FOO as a new argument at the beginning of the command line.
6764///
6765/// '+': Add FOO as a new argument at the end of the command line.
6766///
6767/// 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command
6768/// line.
6769///
6770/// 'xOPTION': Removes all instances of the literal argument OPTION.
6771///
6772/// 'XOPTION': Removes all instances of the literal argument OPTION,
6773/// and the following argument.
6774///
6775/// 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox'
6776/// at the end of the command line.
6777///
6778/// \param OS - The stream to write edit information to.
6779/// \param Args - The vector of command line arguments.
6780/// \param Edit - The override command to perform.
6781/// \param SavedStrings - Set to use for storing string representations.
6782static void applyOneOverrideOption(raw_ostream &OS,
6783 SmallVectorImpl<const char *> &Args,
6784 StringRef Edit,
6785 llvm::StringSet<> &SavedStrings) {
6786 // This does not need to be efficient.
6787
6788 if (Edit[0] == '^') {
6789 const char *Str = GetStableCStr(SavedStrings, S: Edit.substr(Start: 1));
6790 OS << "### Adding argument " << Str << " at beginning\n";
6791 Args.insert(I: Args.begin() + 1, Elt: Str);
6792 } else if (Edit[0] == '+') {
6793 const char *Str = GetStableCStr(SavedStrings, S: Edit.substr(Start: 1));
6794 OS << "### Adding argument " << Str << " at end\n";
6795 Args.push_back(Elt: Str);
6796 } else if (Edit[0] == 's' && Edit[1] == '/' && Edit.ends_with(Suffix: "/") &&
6797 Edit.slice(Start: 2, End: Edit.size() - 1).contains(C: '/')) {
6798 StringRef MatchPattern = Edit.substr(Start: 2).split(Separator: '/').first;
6799 StringRef ReplPattern = Edit.substr(Start: 2).split(Separator: '/').second;
6800 ReplPattern = ReplPattern.slice(Start: 0, End: ReplPattern.size() - 1);
6801
6802 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
6803 // Ignore end-of-line response file markers
6804 if (Args[i] == nullptr)
6805 continue;
6806 std::string Repl = llvm::Regex(MatchPattern).sub(Repl: ReplPattern, String: Args[i]);
6807
6808 if (Repl != Args[i]) {
6809 OS << "### Replacing '" << Args[i] << "' with '" << Repl << "'\n";
6810 Args[i] = GetStableCStr(SavedStrings, S: Repl);
6811 }
6812 }
6813 } else if (Edit[0] == 'x' || Edit[0] == 'X') {
6814 auto Option = Edit.substr(Start: 1);
6815 for (unsigned i = 1; i < Args.size();) {
6816 if (Option == Args[i]) {
6817 OS << "### Deleting argument " << Args[i] << '\n';
6818 Args.erase(CI: Args.begin() + i);
6819 if (Edit[0] == 'X') {
6820 if (i < Args.size()) {
6821 OS << "### Deleting argument " << Args[i] << '\n';
6822 Args.erase(CI: Args.begin() + i);
6823 } else
6824 OS << "### Invalid X edit, end of command line!\n";
6825 }
6826 } else
6827 ++i;
6828 }
6829 } else if (Edit[0] == 'O') {
6830 for (unsigned i = 1; i < Args.size();) {
6831 const char *A = Args[i];
6832 // Ignore end-of-line response file markers
6833 if (A == nullptr)
6834 continue;
6835 if (A[0] == '-' && A[1] == 'O' &&
6836 (A[2] == '\0' || (A[3] == '\0' && (A[2] == 's' || A[2] == 'z' ||
6837 ('0' <= A[2] && A[2] <= '9'))))) {
6838 OS << "### Deleting argument " << Args[i] << '\n';
6839 Args.erase(CI: Args.begin() + i);
6840 } else
6841 ++i;
6842 }
6843 OS << "### Adding argument " << Edit << " at end\n";
6844 Args.push_back(Elt: GetStableCStr(SavedStrings, S: '-' + Edit.str()));
6845 } else {
6846 OS << "### Unrecognized edit: " << Edit << "\n";
6847 }
6848}
6849
6850void driver::applyOverrideOptions(SmallVectorImpl<const char *> &Args,
6851 const char *OverrideStr,
6852 llvm::StringSet<> &SavedStrings,
6853 raw_ostream *OS) {
6854 if (!OS)
6855 OS = &llvm::nulls();
6856
6857 if (OverrideStr[0] == '#') {
6858 ++OverrideStr;
6859 OS = &llvm::nulls();
6860 }
6861
6862 *OS << "### CCC_OVERRIDE_OPTIONS: " << OverrideStr << "\n";
6863
6864 // This does not need to be efficient.
6865
6866 const char *S = OverrideStr;
6867 while (*S) {
6868 const char *End = ::strchr(s: S, c: ' ');
6869 if (!End)
6870 End = S + strlen(s: S);
6871 if (End != S)
6872 applyOneOverrideOption(OS&: *OS, Args, Edit: std::string(S, End), SavedStrings);
6873 S = End;
6874 if (*S != '\0')
6875 ++S;
6876 }
6877}
6878

source code of clang/lib/Driver/Driver.cpp