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