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