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