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