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