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