[Flang] remove whole-archive option for AIX linker (#76039)
[llvm-project.git] / clang / lib / Driver / Driver.cpp
blobff95c899c5f3d4e91770f8203001a62eafddb199
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 //===----------------------------------------------------------------------===//
9 #include "clang/Driver/Driver.h"
10 #include "ToolChains/AIX.h"
11 #include "ToolChains/AMDGPU.h"
12 #include "ToolChains/AMDGPUOpenMP.h"
13 #include "ToolChains/AVR.h"
14 #include "ToolChains/Arch/RISCV.h"
15 #include "ToolChains/BareMetal.h"
16 #include "ToolChains/CSKYToolChain.h"
17 #include "ToolChains/Clang.h"
18 #include "ToolChains/CrossWindows.h"
19 #include "ToolChains/Cuda.h"
20 #include "ToolChains/Darwin.h"
21 #include "ToolChains/DragonFly.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"
36 #include "ToolChains/MipsLinux.h"
37 #include "ToolChains/NaCl.h"
38 #include "ToolChains/NetBSD.h"
39 #include "ToolChains/OHOS.h"
40 #include "ToolChains/OpenBSD.h"
41 #include "ToolChains/PPCFreeBSD.h"
42 #include "ToolChains/PPCLinux.h"
43 #include "ToolChains/PS4CPU.h"
44 #include "ToolChains/RISCVToolchain.h"
45 #include "ToolChains/SPIRV.h"
46 #include "ToolChains/Solaris.h"
47 #include "ToolChains/TCE.h"
48 #include "ToolChains/VEToolchain.h"
49 #include "ToolChains/WebAssembly.h"
50 #include "ToolChains/XCore.h"
51 #include "ToolChains/ZOS.h"
52 #include "clang/Basic/TargetID.h"
53 #include "clang/Basic/Version.h"
54 #include "clang/Config/config.h"
55 #include "clang/Driver/Action.h"
56 #include "clang/Driver/Compilation.h"
57 #include "clang/Driver/DriverDiagnostic.h"
58 #include "clang/Driver/InputInfo.h"
59 #include "clang/Driver/Job.h"
60 #include "clang/Driver/Options.h"
61 #include "clang/Driver/Phases.h"
62 #include "clang/Driver/SanitizerArgs.h"
63 #include "clang/Driver/Tool.h"
64 #include "clang/Driver/ToolChain.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
103 using namespace clang::driver;
104 using namespace clang;
105 using namespace llvm::opt;
107 static 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;
123 return llvm::Triple(OffloadTargets[0]);
126 static std::optional<llvm::Triple>
127 getNVIDIAOffloadTargetTriple(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");
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;
141 D.Diag(diag::err_drv_invalid_or_unsupported_offload_target) << TT->str();
142 return std::nullopt;
144 static std::optional<llvm::Triple>
145 getHIPOffloadTargetTriple(const Driver &D, const ArgList &Args) {
146 if (!Args.hasArg(options::OPT_offload_EQ)) {
147 return llvm::Triple("amdgcn-amd-amdhsa"); // Default HIP triple.
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;
162 // static
163 std::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).
169 // Dir is bin/ or lib/, depending on where BinaryPath is.
170 std::string Dir = std::string(llvm::sys::path::parent_path(BinaryPath));
172 SmallString<128> P(Dir);
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);
188 return std::string(P.str());
191 Driver::Driver(StringRef ClangExecutable, StringRef TargetTriple,
192 DiagnosticsEngine &Diags, std::string Title,
193 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS)
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();
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.
213 if ((!SysRoot.empty()) && llvm::sys::path::is_relative(SysRoot)) {
214 // Prepend InstalledDir if SysRoot is relative
215 SmallString<128> P(InstalledDir);
216 llvm::sys::path::append(P, SysRoot);
217 SysRoot = std::string(P);
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)
225 SmallString<128> P;
226 llvm::sys::fs::expand_tilde(CLANG_CONFIG_FILE_USER_DIR, P);
227 UserConfigDir = static_cast<std::string>(P);
229 #endif
231 // Compute the path to the resource directory.
232 ResourceDir = GetResourcesPath(ClangExecutable, CLANG_RESOURCE_DIR);
235 void 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;
251 InputArgList Driver::ParseArgStrings(ArrayRef<const char *> ArgStrings,
252 bool UseDriverMode, bool &ContainsError) {
253 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
254 ContainsError = false;
256 llvm::opt::Visibility VisibilityMask = getOptionVisibilityMask(UseDriverMode);
257 unsigned MissingArgIndex, MissingArgCount;
258 InputArgList Args = getOpts().ParseArgs(ArgStrings, MissingArgIndex,
259 MissingArgCount, VisibilityMask);
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,
267 SourceLocation()) > DiagnosticsEngine::Warning;
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()) >
276 DiagnosticsEngine::Warning;
277 continue;
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,
285 SourceLocation()) > DiagnosticsEngine::Warning;
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;
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;
310 ContainsError |= Diags.getDiagnosticLevel(DiagID, SourceLocation()) >
311 DiagnosticsEngine::Warning;
314 for (const Arg *A : Args.filtered(options::OPT_o)) {
315 if (ArgStrings[A->getIndex()] == A->getSpelling())
316 continue;
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;
326 return Args;
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.
332 phases::ID Driver::getFinalPhase(const DerivedArgList &DAL,
333 Arg **FinalPhaseArg) const {
334 Arg *PhaseArg = nullptr;
335 phases::ID FinalPhase;
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)) ||
342 CCGenDiagnostics) {
343 FinalPhase = phases::Preprocess;
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;
365 // -S only runs up to the backend.
366 } else if ((PhaseArg = DAL.getLastArg(options::OPT_S))) {
367 FinalPhase = phases::Backend;
369 // -c compilation only runs up to the assembler.
370 } else if ((PhaseArg = DAL.getLastArg(options::OPT_c))) {
371 FinalPhase = phases::Assemble;
373 } else if ((PhaseArg = DAL.getLastArg(options::OPT_emit_interface_stubs))) {
374 FinalPhase = phases::IfsMerge;
376 // Otherwise do everything.
377 } else
378 FinalPhase = phases::Link;
380 if (FinalPhaseArg)
381 *FinalPhaseArg = PhaseArg;
383 return FinalPhase;
386 static 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;
396 DerivedArgList *Driver::TranslateInputArgs(const InputArgList &Args) const {
397 const llvm::opt::OptTable &Opts = getOpts();
398 DerivedArgList *DAL = new DerivedArgList(Args);
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();
408 if (A->getOption().matches(options::OPT_start_no_unused_arguments)) {
409 IgnoreUnused = true;
410 continue;
412 if (A->getOption().matches(options::OPT_end_no_unused_arguments)) {
413 IgnoreUnused = false;
414 continue;
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').
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));
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);
434 continue;
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;
453 // Rewrite reserved library names.
454 if (A->getOption().matches(options::OPT_l)) {
455 StringRef Value = A->getValue();
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;
464 // Rewrite unconditionally.
465 if (Value == "cc_kext") {
466 DAL->AddFlagArg(A, Opts.getOption(options::OPT_Z_reserved_lib_cckext));
467 continue;
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;
479 DAL->append(A);
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));
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));
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();
499 #endif
501 return DAL;
504 /// Compute target triple from args.
506 /// This routine provides the logic to compute a target triple from various
507 /// args passed to the driver and the default triple string.
508 static 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();
516 llvm::Triple Target(llvm::Triple::normalize(TargetTriple));
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");
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()) {
528 tools::darwin::setTripleTypeForMachOArchName(Target, DarwinArchName,
529 Args);
530 return Target;
533 // Handle the Darwin '-arch' flag.
534 if (Arg *A = Args.getLastArg(options::OPT_arch)) {
535 StringRef ArchName = A->getValue();
536 tools::darwin::setTripleTypeForMachOArchName(Target, ArchName, Args);
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);
553 // Skip further flag support on OSes which don't support '-m32' or '-m64'.
554 if (Target.getArch() == llvm::Triple::tce)
555 return Target;
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;
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;
572 if (AT != llvm::Triple::UnknownArch && AT != Target.getArch())
573 Target.setArch(AT);
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();
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;
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);
617 if (AT != llvm::Triple::UnknownArch && AT != Target.getArch()) {
618 Target.setArch(AT);
619 if (Target.isWindowsGNUEnvironment())
620 toolchains::MinGW::fixTripleArch(D, Target, Args);
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();
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);
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");
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);
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);
680 return Target;
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.
686 static 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;
691 const Arg *A = Args.getLastArg(OptEq);
692 StringRef LTOName = A->getValue();
694 driver::LTOKind LTOMode = llvm::StringSwitch<LTOKind>(LTOName)
695 .Case("full", LTOK_Full)
696 .Case("thin", LTOK_Thin)
697 .Default(LTOK_Unknown);
699 if (LTOMode == LTOK_Unknown) {
700 D.Diag(diag::err_drv_unsupported_option_argument)
701 << A->getSpelling() << A->getValue();
702 return LTOK_None;
704 return LTOMode;
707 // Parse the LTO options.
708 void Driver::setLTOMode(const llvm::opt::ArgList &Args) {
709 LTOMode =
710 parseLTOMode(*this, Args, options::OPT_flto_EQ, options::OPT_fno_lto);
712 OffloadLTOMode = parseLTOMode(*this, Args, options::OPT_foffload_lto_EQ,
713 options::OPT_fno_offload_lto);
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;
727 /// Compute the desired OpenMP runtime from the flags provided.
728 Driver::OpenMPRuntimeKind Driver::getOpenMPRuntime(const ArgList &Args) const {
729 StringRef RuntimeName(CLANG_DEFAULT_OPENMP_RUNTIME);
731 const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ);
732 if (A)
733 RuntimeName = A->getValue();
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);
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";
750 return RT;
753 void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
754 InputList &Inputs) {
757 // CUDA/HIP
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);
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;
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());
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();
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;
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);
817 // OpenMP
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;
836 llvm::StringMap<llvm::DenseSet<StringRef>> DerivedArchs;
837 llvm::StringMap<StringRef> FoundNormalizedTriples;
838 std::multiset<StringRef> OpenMPTriples;
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;
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());
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.
863 llvm::DenseSet<StringRef> Archs;
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);
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);
878 if (!AMDTriple && !NVPTXTriple) {
879 for (StringRef Arch :
880 getOffloadArchs(C, C.getArgs(), Action::OFK_OpenMP, nullptr, true))
881 Archs.insert(Arch);
884 for (StringRef Arch : Archs) {
885 if (NVPTXTriple && IsNVIDIAGpuArch(StringToCudaArch(
886 getProcessorFromTargetID(*NVPTXTriple, Arch)))) {
887 DerivedArchs[NVPTXTriple->getTriple()].insert(Arch);
888 } else if (AMDTriple &&
889 IsAMDGpuArch(StringToCudaArch(
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;
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;
905 for (const auto &TripleAndArchs : DerivedArchs)
906 OpenMPTriples.insert(TripleAndArchs.first());
909 for (StringRef Val : OpenMPTriples) {
910 llvm::Triple TT(ToolChain::getOpenMPTriple(Val));
911 std::string NormalizedName = TT.normalize();
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;
921 // Store the current triple so that we can check for duplicates in the
922 // following iterations.
923 FoundNormalizedTriples[NormalizedName] = Val;
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.");
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()];
957 } else if (C.getInputArgs().hasArg(options::OPT_fopenmp_targets_EQ)) {
958 Diag(clang::diag::err_drv_expecting_fopenmp_with_fopenmp_targets);
959 return;
963 // TODO: Add support for other offloading programming models here.
967 static 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);
983 bool 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;
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;
998 // Try reading the given file.
999 SmallVector<const char *, 32> NewCfgArgs;
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;
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;
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();
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);
1031 ConfigFiles.push_back(std::string(CfgFileName));
1032 return false;
1035 bool Driver::loadConfigFiles() {
1036 llvm::cl::ExpansionContext ExpCtx(Saver.getAllocator(),
1037 llvm::cl::tokenizeConfigFile);
1038 ExpCtx.setVFS(&getVFS());
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);
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);
1062 // Prepare list of directories where config file is searched for.
1063 StringRef CfgFileSearchDirs[] = {UserConfigDir, SystemConfigDir, Dir};
1064 ExpCtx.setSearchDirs(CfgFileSearchDirs);
1066 // First try to load configuration from the default files, return on error.
1067 if (loadDefaultConfigFiles(ExpCtx))
1068 return true;
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;
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;
1094 // Try to read the config file, return on error.
1095 if (readConfigFile(CfgFilePath, ExpCtx))
1096 return true;
1100 // No error occurred.
1101 return false;
1104 bool 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;
1111 if (CLOptions && CLOptions->hasArg(options::OPT_no_default_config))
1112 return false;
1114 std::string RealMode = getExecutableForDriverMode(Mode);
1115 std::string Triple;
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();
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());
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++).
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);
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);
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;
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);
1179 // If we were unable to find a config file deduced from executable name,
1180 // that is not an error.
1181 return false;
1184 Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
1185 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
1187 // FIXME: Handle environment options which affect driver behavior, somewhere
1188 // (client?). GCC_EXEC_PREFIX, LPATH, CC_PRINT_OPTIONS.
1190 // We look for the driver mode option early, because the mode can affect
1191 // how other options are parsed.
1193 auto DriverMode = getDriverMode(ClangExecutable, ArgList.slice(1));
1194 if (!DriverMode.empty())
1195 setDriverMode(DriverMode);
1197 // FIXME: What are we going to do with -V and -b?
1199 // Arguments specified in command line.
1200 bool ContainsError;
1201 CLOptions = std::make_unique<InputArgList>(
1202 ParseArgStrings(ArgList.slice(1), /*UseDriverMode=*/true, ContainsError));
1204 // Try parsing configuration file.
1205 if (!ContainsError)
1206 ContainsError = loadConfigFiles();
1207 bool HasConfigFile = !ContainsError && (CfgOptions.get() != nullptr);
1209 // All arguments, from both config file and command line.
1210 InputArgList Args = std::move(HasConfigFile ? std::move(*CfgOptions)
1211 : std::move(*CLOptions));
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);
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());
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));
1238 if (!ContainsError)
1239 for (auto *Opt : *CLModePassThroughOptions) {
1240 appendOneArg(Args, Opt, nullptr);
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();
1250 // FIXME: This stuff needs to go into the Compilation, not the driver.
1251 bool CCCPrintPhases;
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);
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);
1261 // Ignore -pipe.
1262 Args.ClaimAllArgs(options::OPT_pipe);
1264 // Extract -ccc args.
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();
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();
1280 if (Args.hasArg(options::OPT_fproc_stat_report))
1281 CCPrintProcessStats = true;
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 =
1300 toolchains::HLSLToolChain::parseTargetProfile(TargetProfile))
1301 TargetTriple = *Triple;
1302 else
1303 Diag(diag::err_drv_invalid_directx_shader_module) << TargetProfile;
1305 A->claim();
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();
1313 } else {
1314 Diag(diag::err_drv_dxc_missing_target_profile);
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));
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;
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();
1341 if (const Arg *A = Args.getLastArg(options::OPT_resource_dir))
1342 ResourceDir = A->getValue();
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);
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;
1362 setLTOMode(Args);
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);
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());
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"));
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);
1417 std::unique_ptr<llvm::opt::InputArgList> UArgs =
1418 std::make_unique<InputArgList>(std::move(Args));
1420 // Perform the default argument translations.
1421 DerivedArgList *TranslatedArgs = TranslateInputArgs(*UArgs);
1423 // Owned by the host.
1424 const ToolChain &TC = getToolChain(
1425 *UArgs, computeTargetTriple(*this, TargetTriple, *UArgs));
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();
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");
1450 break;
1451 case llvm::Triple::aarch64:
1452 case llvm::Triple::aarch64_be:
1453 case llvm::Triple::aarch64_32:
1454 if (TC.getTriple().getEnvironmentName().starts_with("eabi")) {
1455 Diag(diag::warn_target_unrecognized_env)
1456 << TargetTriple
1457 << (TC.getTriple().getArchName().str() + "-none-elf");
1459 break;
1460 default:
1461 break;
1465 // The compilation takes ownership of Args.
1466 Compilation *C = new Compilation(*this, TC, UArgs.release(), TranslatedArgs,
1467 ContainsError);
1469 if (!HandleImmediateArgs(*C))
1470 return C;
1472 // Construct the list of inputs.
1473 InputList Inputs;
1474 BuildInputs(C->getDefaultToolChain(), *TranslatedArgs, Inputs);
1476 // Populate the tool chains for the offloading devices, if any.
1477 CreateOffloadingDeviceToolChains(*C, Inputs);
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());
1486 if (CCCPrintPhases) {
1487 PrintActions(*C);
1488 return C;
1491 BuildJobs(*C);
1493 return C;
1496 static 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);
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);
1512 OS << '\n';
1515 bool 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");
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.starts_with("/var/root"))
1526 CrashDiagDir = "/";
1527 path::append(CrashDiagDir, "Library/Logs/DiagnosticReports");
1528 int PID =
1529 #if LLVM_ON_UNIX
1530 getpid();
1531 #else
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.starts_with(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.starts_with("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;
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;
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;
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;
1601 return false;
1604 static 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:";
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.
1612 void Driver::generateCompilationDiagnostics(
1613 Compilation &C, const Command &FailingCommand,
1614 StringRef AdditionalInformation, CompilationDiagnosticReport *Report) {
1615 if (C.getArgs().hasArg(options::OPT_fno_crash_diagnostics))
1616 return;
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);
1626 if (!Level)
1627 return;
1629 // Don't try to generate diagnostics for dsymutil jobs.
1630 if (FailingCommand.getCreator().isDsymutilJob())
1631 return;
1633 bool IsLLD = false;
1634 ArgStringList SavedTemps;
1635 if (FailingCommand.getCreator().isLinkJob()) {
1636 C.getDefaultToolChain().GetLinkerPath(&IsLLD);
1637 if (!IsLLD || Level < 2)
1638 return;
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());
1648 // Print the version of the compiler.
1649 PrintVersion(C, llvm::errs());
1651 // Suppress driver output and emit preprocessor output to temp file.
1652 CCGenDiagnostics = true;
1654 // Save the original job command(s).
1655 Command Cmd = FailingCommand;
1657 // Keep track of whether we produce any errors while trying to produce
1658 // preprocessed sources.
1659 DiagnosticErrorTrap Trap(Diags);
1661 // Suppress tool output.
1662 C.initCompilationForDiagnostics();
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));
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;
1687 // Construct the list of inputs.
1688 InputList Inputs;
1689 BuildInputs(C.getDefaultToolChain(), C.getArgs(), Inputs);
1691 for (InputList::iterator it = Inputs.begin(), ie = Inputs.end(); it != ie;) {
1692 bool IgnoreInput = false;
1694 // Ignore input from stdin or any inputs that cannot be preprocessed.
1695 // Check type first as not all linker inputs have a value.
1696 if (types::getPreprocessedType(it->first) == types::TY_INVALID) {
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;
1705 if (IgnoreInput) {
1706 it = Inputs.erase(it);
1707 ie = Inputs.end();
1708 } else {
1709 ++it;
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;
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);
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;
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());
1744 BuildJobs(C);
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;
1753 // Generate preprocessed output.
1754 SmallVector<std::pair<int, const Command *>, 4> FailingCommands;
1755 C.ExecuteJobs(C.getJobs(), FailingCommands);
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;
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;
1771 Diag(clang::diag::note_drv_command_failed_diag_msg) << BugReporMsg;
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");
1783 if (StringRef(TempFile).ends_with(".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");
1791 for (const char *TempFile : SavedTemps)
1792 C.addTempFile(TempFile);
1794 // Assume associated files are based off of the first temporary file.
1795 CrashReportInfo CrashInfo(TempFiles[0], VFS);
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;
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)";
1839 Diag(clang::diag::note_drv_command_failed_diag_msg)
1840 << "\n\n********************";
1843 void 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 ==
1849 ResponseFileSupport::RF_None ||
1850 llvm::sys::commandLineFitsWithinSystemLimits(Cmd.getExecutable(),
1851 Cmd.getArguments()))
1852 return;
1854 std::string TmpName = GetTemporaryPath("response", "txt");
1855 Cmd.setResponseFile(C.addTempFile(C.getArgs().MakeArgString(TmpName)));
1858 int Driver::ExecuteCompilation(
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);
1865 C.ExecuteJobs(C.getJobs(), FailingCommands, /*LogOnly=*/true);
1867 // If there were errors building the compilation, quit now.
1868 if (!FailingCommands.empty() || Diags.hasErrorOccurred())
1869 return 1;
1871 return 0;
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;
1880 // If there were errors building the compilation, quit now.
1881 if (Diags.hasErrorOccurred())
1882 return 1;
1884 // Set up response file names for each command, if necessary.
1885 for (auto &Job : C.getJobs())
1886 setUpResponseFiles(C, Job);
1888 C.ExecuteJobs(C.getJobs(), FailingCommands);
1890 // If the command succeeded, we are done.
1891 if (FailingCommands.empty())
1892 return 0;
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;
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);
1906 // Failure result files are valid unless we crashed.
1907 if (CommandRes < 0)
1908 C.CleanupFileMap(C.getFailureResultFiles(), JA, true);
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;
1918 // Print extra information about abnormal failures, if possible.
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();
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;
1937 return Res;
1940 void Driver::PrintHelp(bool ShowHidden) const {
1941 llvm::opt::Visibility VisibilityMask = getOptionVisibilityMask();
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);
1949 void 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';
1957 const ToolChain &TC = C.getDefaultToolChain();
1958 OS << "Target: " << TC.getTripleString() << '\n';
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';
1969 // Print out the install directory.
1970 OS << "InstalledDir: " << InstalledDir << '\n';
1972 // If configuration files were used, print their paths.
1973 for (auto ConfigFile : ConfigFiles)
1974 OS << "Configuration file: " << ConfigFile << '\n';
1977 /// PrintDiagnosticCategories - Implement the --print-diagnostic-categories
1978 /// option.
1979 static 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';
1986 void 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;
1994 llvm::opt::Visibility VisibilityMask(options::ClangOption);
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);
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.ends_with(",");
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));
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);
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);
2029 if (SuggestedCompletions.empty())
2030 SuggestedCompletions = Opts.suggestValueCompletions(Cur, "");
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;
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.ends_with("=")) {
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);
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.starts_with(Cur))
2057 SuggestedCompletions.push_back(std::string(S));
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;
2070 llvm::outs() << llvm::join(SuggestedCompletions, "\n") << '\n';
2073 bool Driver::HandleImmediateArgs(const Compilation &C) {
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.
2077 if (C.getArgs().hasArg(options::OPT_dumpmachine)) {
2078 llvm::outs() << C.getDefaultToolChain().getTripleString() << '\n';
2079 return false;
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;
2089 if (C.getArgs().hasArg(options::OPT__print_diagnostic_categories)) {
2090 PrintDiagnosticCategories(llvm::outs());
2091 return false;
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;
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;
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;
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";
2123 const ToolChain &TC = C.getDefaultToolChain();
2125 if (C.getArgs().hasArg(options::OPT_v))
2126 TC.printVerboseInfo(llvm::errs());
2128 if (C.getArgs().hasArg(options::OPT_print_resource_dir)) {
2129 llvm::outs() << ResourceDir << '\n';
2130 return false;
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;
2143 for (const std::string &Path : TC.getProgramPaths()) {
2144 if (separator)
2145 llvm::outs() << llvm::sys::EnvPathSeparator;
2146 llvm::outs() << Path;
2147 separator = true;
2149 llvm::outs() << "\n";
2150 llvm::outs() << "libraries: =" << ResourceDir;
2152 StringRef sysroot = C.getSysRoot();
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;
2163 llvm::outs() << "\n";
2164 return false;
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;
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;
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;
2189 if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
2190 StringRef ProgName = A->getValue();
2192 // Null program name cannot have a path.
2193 if (! ProgName.empty())
2194 llvm::outs() << GetProgramPath(ProgName, TC);
2196 llvm::outs() << "\n";
2197 return false;
2200 if (Arg *A = C.getArgs().getLastArg(options::OPT_autocomplete)) {
2201 StringRef PassedFlags = A->getValue();
2202 HandleAutocompletions(PassedFlags);
2203 return false;
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) {
2211 case ToolChain::RLT_CompilerRT:
2212 llvm::outs() << TC.getCompilerRT(C.getArgs(), "builtins") << "\n";
2213 break;
2214 case ToolChain::RLT_Libgcc:
2215 llvm::outs() << GetFilePath("libgcc.a", TC) << "\n";
2216 break;
2218 return false;
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;
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;
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";
2248 return false;
2251 if (C.getArgs().hasArg(options::OPT_print_target_triple)) {
2252 llvm::outs() << TC.getTripleString() << "\n";
2253 return false;
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;
2262 if (C.getArgs().hasArg(options::OPT_print_targets)) {
2263 llvm::TargetRegistry::printRegisteredTargetsForVersion(llvm::outs());
2264 return false;
2267 return true;
2270 enum {
2271 TopLevelAction = 0,
2272 HeadSibAction = 1,
2273 OtherSibAction = 2,
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.
2279 static 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];
2285 std::string str;
2286 llvm::raw_string_ostream os(str);
2288 auto getSibIndent = [](int K) -> Twine {
2289 return (K == HeadSibAction) ? " " : (K == OtherSibAction) ? "| " : "";
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;
2323 } else {
2324 const ActionList *AL = &A->getInputs();
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;
2333 os << "}";
2334 } else
2335 os << "{}";
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 << ")";
2352 auto getSelfIndent = [](int K) -> Twine {
2353 return (K == HeadSibAction) ? "+- " : (K == OtherSibAction) ? "|- " : "";
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";
2361 return Id;
2364 // Print the action graphs in a compilation C.
2365 // For example "clang -c file1.c file2.c" is composed of two subgraphs.
2366 void Driver::PrintActions(const Compilation &C) const {
2367 std::map<Action *, unsigned> Ids;
2368 for (Action *A : C.getActions())
2369 PrintActions1(C, A, Ids);
2372 /// Check whether the given input tree contains any compilation or
2373 /// assembly actions.
2374 static bool ContainsCompileOrAssembleAction(const Action *A) {
2375 if (isa<CompileJobAction>(A) || isa<BackendJobAction>(A) ||
2376 isa<AssembleJobAction>(A))
2377 return true;
2379 return llvm::any_of(A->inputs(), ContainsCompileOrAssembleAction);
2382 void Driver::BuildUniversalActions(Compilation &C, const ToolChain &TC,
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;
2390 SmallVector<const char *, 4> Archs;
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 =
2396 tools::darwin::getArchTypeForMachOArchName(A->getValue());
2397 if (Arch == llvm::Triple::UnknownArch) {
2398 Diag(clang::diag::err_drv_invalid_arch_name) << A->getAsString(Args);
2399 continue;
2402 A->claim();
2403 if (ArchNames.insert(A->getValue()).second)
2404 Archs.push_back(A->getValue());
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()));
2413 ActionList SingleActions;
2414 BuildActions(C, Args, BAInputs, SingleActions);
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.
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());
2429 ActionList Inputs;
2430 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
2431 Inputs.push_back(C.MakeAction<BindArchAction>(Act, Archs[i]));
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()));
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())) {
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));
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));
2470 bool Driver::DiagnoseInputExistence(const DerivedArgList &Args, StringRef Value,
2471 types::ID Ty, bool TypoCorrect) const {
2472 if (!getCheckInputsExist())
2473 return true;
2475 // stdin always exists.
2476 if (Value == "-")
2477 return true;
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;
2487 if (getVFS().exists(Value))
2488 return true;
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;
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.)
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.)
2526 // Don't do this for inputs that start with a '/', else we'd pass options
2527 // like /libpath: through to the linker silently.
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.starts_with("/"))
2539 return true;
2541 Diag(clang::diag::err_drv_no_such_file) << Value;
2542 return false;
2545 // Get the C++20 Header Unit type corresponding to the input type.
2546 static types::ID CXXHeaderUnitType(ModuleHeaderMode HM) {
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");
2557 return types::TY_CXXHUHeader;
2560 // Construct a the list of inputs and their types.
2561 void 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;
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;
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;
2587 Previous = A;
2589 if (ShowNote)
2590 Diag(clang::diag::note_drv_t_option_is_global);
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";
2611 for (Arg *A : Args) {
2612 if (A->getOption().getKind() == Option::InputClass) {
2613 const char *Value = A->getValue();
2614 types::ID Ty = types::TY_INVALID;
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();
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.
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;
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);
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;
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;
2663 Ty = types::lookupCXXTypeForCType(Ty);
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);
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;
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.
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;
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;
2707 if (Ty == types::TY_INVALID) {
2708 Ty = InputType;
2709 InputTypeArg->claim();
2713 if ((Ty == types::TY_C || Ty == types::TY_CXX) &&
2714 Args.hasArgNoClaim(options::OPT_hipstdpar))
2715 Ty = types::TY_HIP;
2717 if (DiagnoseInputExistence(Args, Value, Ty, /*TypoCorrect=*/true))
2718 Inputs.push_back(std::make_pair(Ty, A));
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));
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));
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));
2741 } else if (A->getOption().matches(options::OPT_x)) {
2742 InputTypeArg = A;
2743 InputType = types::lookupTypeForTypeSpecifier(A->getValue());
2744 A->claim();
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;
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);
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));
2776 namespace {
2777 /// Provides a convenient interface for different programming models to generate
2778 /// the required device actions.
2779 class OffloadingActionBuilder final {
2780 /// Flag used to trace errors in the builder.
2781 bool IsValid = false;
2783 /// The compilation that is using this builder.
2784 Compilation &C;
2786 /// Map between an input argument and the offload kinds used to process it.
2787 std::map<const Arg *, unsigned> InputArgToOffloadKindMap;
2789 /// Map between a host action and its originating input argument.
2790 std::map<Action *, const Arg *> HostActionToInputArgMap;
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;
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,
2807 protected:
2808 /// Compilation associated with this builder.
2809 Compilation &C;
2811 /// Tool chains associated with this builder. The same programming
2812 /// model may have associated one or more tool chains.
2813 SmallVector<const ToolChain *, 2> ToolChains;
2815 /// The derived arguments associated with this builder.
2816 DerivedArgList &Args;
2818 /// The inputs associated with this builder.
2819 const Driver::InputList &Inputs;
2821 /// The associated offload kind.
2822 Action::OffloadKind AssociatedOffloadKind = Action::OFK_None;
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() {}
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;
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;
2848 /// Append top level actions generated by the builder.
2849 virtual void appendTopLevelActions(ActionList &AL) {}
2851 /// Append linker device actions generated by the builder.
2852 virtual void appendLinkDeviceActions(ActionList &AL) {}
2854 /// Append linker host action generated by the builder.
2855 virtual Action* appendLinkHostActions(ActionList &AL) { return nullptr; }
2857 /// Append linker actions generated by the builder.
2858 virtual void appendLinkDependences(OffloadAction::DeviceDependences &DA) {}
2860 /// Initialize the builder. Return true if any initialization errors are
2861 /// found.
2862 virtual bool initialize() { return false; }
2864 /// Return true if the builder can use bundling/unbundling.
2865 virtual bool canUseBundlerUnbundler() const { return false; }
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(); }
2871 /// Return the associated offload kind.
2872 Action::OffloadKind getAssociatedOffloadKind() {
2873 return AssociatedOffloadKind;
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;
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); }
2900 /// List of GPU architectures to use in this compilation.
2901 SmallVector<TargetID, 4> GpuArchList;
2903 /// The CUDA actions for the current input.
2904 ActionList CudaDeviceActions;
2906 /// The CUDA fat binary if it was generated for the current input.
2907 Action *CudaFatBinary = nullptr;
2909 /// Flag that is set to true if this builder acted on the current input.
2910 bool IsActive = false;
2912 /// Flag for -fgpu-rdc.
2913 bool Relocatable = false;
2915 /// Default GPU architecture if there's no one specified.
2916 CudaArch DefaultCudaArch = CudaArch::UNKNOWN;
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;
2923 /// Compilation unit ID specified by option '-cuid='.
2924 StringRef FixedCUID;
2926 public:
2927 CudaActionBuilderBase(Compilation &C, DerivedArgList &Args,
2928 const Driver::InputList &Inputs,
2929 Action::OffloadKind OFKind)
2930 : DeviceActionBuilder(C, Args, Inputs, OFKind) {
2932 CompileDeviceOnly = C.getDriver().offloadDeviceOnly();
2933 Relocatable = Args.hasFlag(options::OPT_fgpu_rdc,
2934 options::OPT_fno_gpu_rdc, /*Default=*/false);
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.
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.");
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;
2958 // Set the flag to true, so that the builder acts on the current input.
2959 IsActive = true;
2961 if (CompileHostOnly)
2962 return ABRT_Success;
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));
2984 Hasher.final(Hash);
2985 CUID = llvm::utohexstr(Hash.low(), /*LowerCase=*/true);
2988 IA->setId(CUID);
2990 for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) {
2991 CudaDeviceActions.push_back(
2992 C.MakeAction<InputAction>(IA->getInputArg(), Ty, IA->getId()));
2995 return ABRT_Success;
2998 // If this is an unbundling action use it as is for each CUDA toolchain.
2999 if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(HostAction)) {
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;
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) ||
3017 types::lookupTypeForExtension(
3018 llvm::sys::path::extension(FileName).drop_front()) !=
3019 types::TY_Object ||
3020 llvm::sys::path::extension(FileName) == LibFileExt))
3021 return ABRT_Inactive;
3023 for (auto Arch : GpuArchList) {
3024 CudaDeviceActions.push_back(UA);
3025 UA->registerDependentActionInfo(ToolChains[0], Arch,
3026 AssociatedOffloadKind);
3028 IsActive = true;
3029 return ABRT_Success;
3032 return IsActive ? ABRT_Success : ABRT_Inactive;
3035 void appendTopLevelActions(ActionList &AL) override {
3036 // Utility to append actions to the top level list.
3037 auto AddTopLevel = [&](Action *A, TargetID TargetID) {
3038 OffloadAction::DeviceDependences Dep;
3039 Dep.add(*A, *ToolChains.front(), TargetID, AssociatedOffloadKind);
3040 AL.push_back(C.MakeAction<OffloadAction>(Dep, A->getType()));
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;
3051 if (CudaDeviceActions.empty())
3052 return;
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]);
3064 CudaDeviceActions.clear();
3067 /// Get canonicalized offload arch option. \returns empty StringRef if the
3068 /// option is invalid.
3069 virtual StringRef getCanonicalOffloadArch(StringRef Arch) = 0;
3071 virtual std::optional<std::pair<llvm::StringRef, llvm::StringRef>>
3072 getConflictOffloadArchCombination(const std::set<StringRef> &GpuArchs) = 0;
3074 bool initialize() override {
3075 assert(AssociatedOffloadKind == Action::OFK_Cuda ||
3076 AssociatedOffloadKind == Action::OFK_HIP);
3078 // We don't need to support CUDA.
3079 if (AssociatedOffloadKind == Action::OFK_Cuda &&
3080 !C.hasOffloadToolChain<Action::OFK_Cuda>())
3081 return false;
3083 // We don't need to support HIP.
3084 if (AssociatedOffloadKind == Action::OFK_HIP &&
3085 !C.hasOffloadToolChain<Action::OFK_HIP>())
3086 return false;
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;
3100 ToolChains.push_back(
3101 AssociatedOffloadKind == Action::OFK_Cuda
3102 ? C.getSingleOffloadToolChain<Action::OFK_Cuda>()
3103 : C.getSingleOffloadToolChain<Action::OFK_HIP>());
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;
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";
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();
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;
3155 for (auto GPU : *GPUsOrErr) {
3156 GpuArchs.insert(Args.MakeArgString(GPU));
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.");
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;
3180 // Collect list of GPUs remaining in the set.
3181 for (auto Arch : GpuArchs)
3182 GpuArchList.push_back(Arch.data());
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);
3194 return Error;
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;
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();
3214 return CudaArchToString(Arch);
3217 std::optional<std::pair<llvm::StringRef, llvm::StringRef>>
3218 getConflictOffloadArchCombination(
3219 const std::set<StringRef> &GpuArchs) override {
3220 return std::nullopt;
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;
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;
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.");
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;
3259 CudaDeviceActions[I] = C.getDriver().ConstructPhaseAction(
3260 C, Args, Ph, CudaDeviceActions[I], Action::OFK_Cuda);
3262 if (Ph == phases::Assemble)
3263 break;
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;
3273 Action *AssembleAction = CudaDeviceActions[I];
3274 assert(AssembleAction->getType() == types::TY_Object);
3275 assert(AssembleAction->getInputs().size() == 1);
3277 Action *BackendAction = AssembleAction->getInputs()[0];
3278 assert(BackendAction->getType() == types::TY_PP_Asm);
3280 for (auto &A : {AssembleAction, BackendAction}) {
3281 OffloadAction::DeviceDependences DDep;
3282 DDep.add(*A, *ToolChains.front(), GpuArchList[I], Action::OFK_Cuda);
3283 DeviceActions.push_back(
3284 C.MakeAction<OffloadAction>(DDep, A->getType()));
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);
3293 if (!CompileDeviceOnly) {
3294 DA.add(*CudaFatBinary, *ToolChains.front(), /*BoundArch=*/nullptr,
3295 Action::OFK_Cuda);
3296 // Clear the fat binary, it is already a dependence to an host
3297 // action.
3298 CudaFatBinary = nullptr;
3301 // Remove the CUDA actions as they are already connected to an host
3302 // action or fat binary.
3303 CudaDeviceActions.clear();
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;
3315 assert(CurPhase < phases::Backend && "Generating single CUDA "
3316 "instructions should only occur "
3317 "before the backend phase!");
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);
3323 return ABRT_Success;
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;
3339 public:
3340 HIPActionBuilder(Compilation &C, DerivedArgList &Args,
3341 const Driver::InputList &Inputs)
3342 : CudaActionBuilderBase(C, Args, Inputs, Action::OFK_HIP) {
3344 DefaultCudaArch = CudaArch::GFX906;
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);
3351 if (*EmitReloc) {
3352 if (Relocatable) {
3353 C.getDriver().Diag(diag::err_opt_not_valid_with_opt)
3354 << "-fhip-emit-relocatable"
3355 << "-fgpu-rdc";
3358 if (!CompileDeviceOnly) {
3359 C.getDriver().Diag(diag::err_opt_not_valid_without_opt)
3360 << "-fhip-emit-relocatable"
3361 << "--cuda-device-only";
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);
3373 bool canUseBundlerUnbundler() const override { return true; }
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();
3387 auto CanId = getCanonicalTargetID(*ArchStr, Features);
3388 return Args.MakeArgStringRef(CanId);
3391 std::optional<std::pair<llvm::StringRef, llvm::StringRef>>
3392 getConflictOffloadArchCombination(
3393 const std::set<StringRef> &GpuArchs) override {
3394 return getConflictTargetIDCombination(GpuArchs);
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;
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;
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.");
3417 bool ShouldLink = !EmitReloc || !*EmitReloc;
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;
3447 BackendAction =
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(
3454 C, Args, phases::Assemble, BackendAction,
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);
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.
3469 OffloadAction::DeviceDependences DDep;
3470 DDep.add(*CudaDeviceActions[I], *ToolChains.front(), GpuArchList[I],
3471 AssociatedOffloadKind);
3472 CudaDeviceActions[I] = C.MakeAction<OffloadAction>(
3473 DDep, CudaDeviceActions[I]->getType());
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);
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;
3489 // Remove the CUDA actions as they are already connected to an host
3490 // action or fat binary.
3491 CudaDeviceActions.clear();
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;
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;
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);
3521 if (CompileDeviceOnly && CurPhase == FinalPhase && BundleOutput &&
3522 *BundleOutput) {
3523 for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) {
3524 OffloadAction::DeviceDependences DDep;
3525 DDep.add(*CudaDeviceActions[I], *ToolChains.front(), GpuArchList[I],
3526 AssociatedOffloadKind);
3527 CudaDeviceActions[I] = C.MakeAction<OffloadAction>(
3528 DDep, CudaDeviceActions[I]->getType());
3530 CudaFatBinary =
3531 C.MakeAction<OffloadBundlingJobAction>(CudaDeviceActions);
3532 CudaDeviceActions.clear();
3535 return (CompileDeviceOnly &&
3536 (CurPhase == FinalPhase ||
3537 (!ShouldLink && CurPhase == phases::Assemble)))
3538 ? ABRT_Ignore_Host
3539 : ABRT_Success;
3542 void appendLinkDeviceActions(ActionList &AL) override {
3543 if (DeviceLinkerInputs.size() == 0)
3544 return;
3546 assert(DeviceLinkerInputs.size() == GpuArchList.size() &&
3547 "Linker inputs and GPU arch list sizes do not match.");
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) {
3555 types::ID Output = Args.hasArg(options::OPT_emit_llvm)
3556 ? types::TY_LLVM_BC
3557 : types::TY_Image;
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;
3569 DeviceLinkerInputs.clear();
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;
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.
3580 OffloadAction::DeviceDependences DDeps;
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);
3595 Action* appendLinkHostActions(ActionList &AL) override { return AL.back(); }
3597 void appendLinkDependences(OffloadAction::DeviceDependences &DA) override {}
3601 /// TODO: Add the implementation for other specialized builders here.
3604 /// Specialized builders being used by this offloading action builder.
3605 SmallVector<DeviceActionBuilder *, 4> SpecializedBuilders;
3607 /// Flag set to true if all valid builders allow file bundling/unbundling.
3608 bool CanUseBundler;
3610 public:
3611 OffloadingActionBuilder(Compilation &C, DerivedArgList &Args,
3612 const Driver::InputList &Inputs)
3613 : C(C) {
3614 // Create a specialized builder for each device toolchain.
3616 IsValid = true;
3618 // Create a specialized builder for CUDA.
3619 SpecializedBuilders.push_back(new CudaActionBuilder(C, Args, Inputs));
3621 // Create a specialized builder for HIP.
3622 SpecializedBuilders.push_back(new HIPActionBuilder(C, Args, Inputs));
3625 // TODO: Build other specialized builders here.
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();
3635 // Update the counters if the builder is valid.
3636 if (SB->isValid()) {
3637 ++ValidBuilders;
3638 if (SB->canUseBundlerUnbundler())
3639 ++ValidBuildersSupportingBundling;
3642 CanUseBundler =
3643 ValidBuilders && ValidBuilders == ValidBuildersSupportingBundling;
3646 ~OffloadingActionBuilder() {
3647 for (auto *SB : SpecializedBuilders)
3648 delete SB;
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");
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;
3673 if (SpecializedBuilders.empty())
3674 return HostAction;
3676 assert(HostAction && "Invalid host action!");
3677 recordHostAction(HostAction, InputArg);
3679 OffloadAction::DeviceDependences DDeps;
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;
3690 auto RetCode =
3691 SB->getDeviceDependences(DDeps, CurPhase, FinalPhase, Phases);
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;
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();
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;
3711 if (DDeps.getActions().empty())
3712 return HostAction;
3714 // We have dependences we need to bundle together. We use an offload action
3715 // for that.
3716 OffloadAction::HostDependence HDep(
3717 *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(),
3718 /*BoundArch=*/nullptr, DDeps);
3719 return C.MakeAction<OffloadAction>(HDep, DDeps);
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;
3730 recordHostAction(HostAction, InputArg);
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);
3751 assert(HostAction && "Invalid host action!");
3753 // Register the offload kinds that are used.
3754 auto &OffloadKind = InputArgToOffloadKindMap[InputArg];
3755 for (auto *SB : SpecializedBuilders) {
3756 if (!SB->isValid())
3757 continue;
3759 auto RetCode = SB->addDeviceDependences(HostAction);
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.!");
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();
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();
3777 return false;
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);
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);
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);
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());
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;
3822 void appendDeviceLinkActions(ActionList &AL) {
3823 for (DeviceActionBuilder *SB : SpecializedBuilders) {
3824 if (!SB->isValid())
3825 continue;
3826 SB->appendLinkDeviceActions(AL);
3830 Action *makeHostLinkAction() {
3831 // Build a list of device linking actions.
3832 ActionList DeviceAL;
3833 appendDeviceLinkActions(DeviceAL);
3834 if (DeviceAL.empty())
3835 return nullptr;
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);
3849 return HA;
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.
3858 OffloadAction::DeviceDependences DDeps;
3859 for (auto *SB : SpecializedBuilders) {
3860 if (!SB->isValid())
3861 continue;
3863 SB->appendLinkDependences(DDeps);
3866 // Calculate all the offload kinds used in the current compilation.
3867 unsigned ActiveOffloadKinds = 0u;
3868 for (auto &I : InputArgToOffloadKindMap)
3869 ActiveOffloadKinds |= I.second;
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);
3890 return HostAction;
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.
3896 OffloadAction::HostDependence HDep(
3897 *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(),
3898 /*BoundArch*/ nullptr, ActiveOffloadKinds);
3899 return C.MakeAction<OffloadAction>(HDep, DDeps);
3902 } // anonymous namespace.
3904 void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
3905 const InputList &Inputs,
3906 ActionList &Actions) const {
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;
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;
3923 Arg *FinalPhaseArg;
3924 phases::ID FinalPhase = getFinalPhase(Args, &FinalPhaseArg);
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));
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);
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);
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;
3967 unsigned LastPLSize = 0;
3968 for (auto &I : Inputs) {
3969 types::ID InputType = I.first;
3970 const Arg *InputArg = I.second;
3972 auto PL = types::getCompilationPhases(InputType);
3973 LastPLSize = PL.size();
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;
3982 // Claim here to avoid the more general unused warning.
3983 InputArg->claim();
3985 // Suppress all unused style warnings with -Qunused-arguments
3986 if (Args.hasArg(options::OPT_Qunused_arguments))
3987 continue;
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)) &&
4001 getPreprocessedType(InputType) == types::TY_INVALID)
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;
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.
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);
4041 void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
4042 const InputList &Inputs, ActionList &Actions) const {
4043 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
4045 if (!SuppressMissingInputWarning && Inputs.empty()) {
4046 Diag(clang::diag::err_drv_no_input_files);
4047 return;
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);
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);
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);
4083 handleArguments(C, Args, Inputs, Actions);
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);
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;
4096 // Construct the actions to perform.
4097 ExtractAPIJobAction *ExtractAPIAction = nullptr;
4098 ActionList LinkerInputs;
4099 ActionList MergerInputs;
4101 for (auto &I : Inputs) {
4102 types::ID InputType = I.first;
4103 const Arg *InputArg = I.second;
4105 auto PL = types::getCompilationPhases(*this, Args, InputType);
4106 if (PL.empty())
4107 continue;
4109 auto FullPL = types::getCompilationPhases(InputType);
4111 // Build the pipeline for this file.
4112 Action *Current = C.MakeAction<InputAction>(*InputArg, InputType);
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;
4120 for (phases::ID Phase : PL) {
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;
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))) &&
4136 !offloadDeviceOnly())
4137 LinkerInputs.push_back(Current);
4138 Current = nullptr;
4139 break;
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;
4153 if (Phase == phases::Precompile && ExtractAPIAction) {
4154 ExtractAPIAction->addHeaderInput(Current);
4155 Current = nullptr;
4156 break;
4159 // FIXME: Should we include any prior module file outputs as inputs of
4160 // later actions in the same command line?
4162 // Otherwise construct the appropriate action.
4163 Action *NewCurrent = ConstructPhaseAction(C, Args, Phase, Current);
4165 // We didn't create a new action, so we will just move to the next phase.
4166 if (NewCurrent == Current)
4167 continue;
4169 if (auto *EAA = dyn_cast<ExtractAPIJobAction>(NewCurrent))
4170 ExtractAPIAction = EAA;
4172 Current = NewCurrent;
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;
4184 if (Current->getType() == types::TY_Nothing)
4185 break;
4188 // If we ended with something, add to the output list.
4189 if (Current)
4190 Actions.push_back(Current);
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);
4200 // Add a link action if necessary.
4202 if (LinkerInputs.empty()) {
4203 Arg *FinalPhaseArg;
4204 if (getFinalPhase(Args, &FinalPhaseArg) == phases::Link)
4205 if (!UseNewOffloadingDriver)
4206 OffloadBuilder->appendDeviceLinkActions(Actions);
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);
4225 if (!UseNewOffloadingDriver)
4226 LA = OffloadBuilder->processHostLinkAction(LA);
4227 Actions.push_back(LA);
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));
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);
4240 ActionList MergerInputs;
4242 for (auto &I : Inputs) {
4243 types::ID InputType = I.first;
4244 const Arg *InputArg = I.second;
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;
4253 Action *Current = C.MakeAction<InputAction>(*InputArg, InputType);
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;
4267 Current = C.MakeAction<CompileJobAction>(Current, types::TY_IFS_CPP);
4268 break;
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;
4280 // If we ended with something, add to the output list.
4281 if (Current)
4282 Actions.push_back(Current);
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));
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.
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;
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();
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));
4331 // Claim ignored clang-cl options.
4332 Args.ClaimAllArgs(options::OPT_cl_ignored_Group);
4335 /// Returns the canonical name for the offloading architecture when using a HIP
4336 /// or CUDA architecture.
4337 static StringRef getCanonicalArchString(Compilation &C,
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();
4357 if (IsNVIDIAGpuArch(Arch))
4358 return Args.MakeArgStringRef(CudaArchToString(Arch));
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();
4371 return Args.MakeArgStringRef(getCanonicalTargetID(*Arch, Features));
4374 // If the input isn't CUDA or HIP just return the architecture.
4375 return ArchStr;
4378 /// Checks if the set offloading architectures does not conflict. Returns the
4379 /// incompatible pair if a conflict occurs.
4380 static std::optional<std::pair<llvm::StringRef, llvm::StringRef>>
4381 getConflictOffloadArchCombination(const llvm::DenseSet<StringRef> &Archs,
4382 llvm::Triple Triple) {
4383 if (!Triple.isAMDGPU())
4384 return std::nullopt;
4386 std::set<StringRef> ArchSet;
4387 llvm::copy(Archs, std::inserter(ArchSet, ArchSet.begin()));
4388 return getConflictTargetIDCombination(ArchSet);
4391 llvm::DenseSet<StringRef>
4392 Driver::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();
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");
4409 if (KnownArchs.contains(TC))
4410 return KnownArchs.lookup(TC);
4412 llvm::DenseSet<StringRef> Archs;
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();
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;
4440 for (auto ArchStr : *GPUsOrErr) {
4441 Archs.insert(
4442 getCanonicalArchString(C, Args, Args.MakeArgString(ArchStr),
4443 TC->getTriple(), SuppressError));
4445 } else {
4446 StringRef ArchStr = getCanonicalArchString(
4447 C, Args, Arch, TC->getTriple(), SuppressError);
4448 if (ArchStr.empty())
4449 return Archs;
4450 Archs.insert(ArchStr);
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);
4468 if (auto ConflictingArchs =
4469 getConflictOffloadArchCombination(Archs, TC->getTriple())) {
4470 C.getDriver().Diag(clang::diag::err_drv_bad_offload_arch_combo)
4471 << ConflictingArchs->first << ConflictingArchs->second;
4472 C.setContainsError();
4475 // Skip filling defaults if we're just querying what is availible.
4476 if (SuppressError)
4477 return Archs;
4479 if (Archs.empty()) {
4480 if (Kind == Action::OFK_Cuda)
4481 Archs.insert(CudaArchToString(CudaArch::CudaDefault));
4482 else if (Kind == Action::OFK_HIP)
4483 Archs.insert(CudaArchToString(CudaArch::HIPDefault));
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);
4491 return Archs;
4494 Action *Driver::BuildOffloadingActions(Compilation &C,
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) ||
4503 getFinalPhase(Args) == phases::Preprocess))
4504 return HostAction;
4506 ActionList OffloadActions;
4507 OffloadAction::DeviceDependences DDeps;
4509 const Action::OffloadKind OffloadKinds[] = {
4510 Action::OFK_OpenMP, Action::OFK_Cuda, Action::OFK_HIP};
4512 for (Action::OffloadKind Kind : OffloadKinds) {
4513 SmallVector<const ToolChain *, 2> ToolChains;
4514 ActionList DeviceActions;
4516 auto TCRange = C.getOffloadToolChains(Kind);
4517 for (auto TI = TCRange.first, TE = TCRange.second; TI != TE; ++TI)
4518 ToolChains.push_back(TI->second);
4520 if (ToolChains.empty())
4521 continue;
4523 types::ID InputType = Input.first;
4524 const Arg *InputArg = Input.second;
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;
4531 // Get the product of all bound architectures and toolchains.
4532 SmallVector<std::pair<const ToolChain *, StringRef>> TCAndArchs;
4533 for (const ToolChain *TC : ToolChains)
4534 for (StringRef Arch : getOffloadArchs(C, Args, Kind, TC))
4535 TCAndArchs.push_back(std::make_pair(TC, Arch));
4537 for (unsigned I = 0, E = TCAndArchs.size(); I != E; ++I)
4538 DeviceActions.push_back(C.MakeAction<InputAction>(*InputArg, InputType));
4540 if (DeviceActions.empty())
4541 return HostAction;
4543 auto PL = types::getCompilationPhases(*this, Args, InputType);
4545 for (phases::ID Phase : PL) {
4546 if (Phase == phases::Link) {
4547 assert(Phase == PL.back() && "linking must be final compilation step.");
4548 break;
4551 auto TCAndArch = TCAndArchs.begin();
4552 for (Action *&A : DeviceActions) {
4553 if (A->getType() == types::TY_Nothing)
4554 continue;
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);
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.
4567 HostAction->setCannotBeCollapsedWithNextDependentAction();
4568 OffloadAction::HostDependence HDep(
4569 *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(),
4570 TCAndArch->second.data(), Kind);
4571 OffloadAction::DeviceDependences DDep;
4572 DDep.add(*A, *TCAndArch->first, TCAndArch->second.data(), Kind);
4573 A = C.MakeAction<OffloadAction>(HDep, DDep);
4576 ++TCAndArch;
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);
4591 auto TCAndArch = TCAndArchs.begin();
4592 for (Action *A : DeviceActions) {
4593 DDeps.add(*A, *TCAndArch->first, TCAndArch->second.data(), Kind);
4594 OffloadAction::DeviceDependences DDep;
4595 DDep.add(*A, *TCAndArch->first, TCAndArch->second.data(), Kind);
4596 OffloadActions.push_back(C.MakeAction<OffloadAction>(DDep, A->getType()));
4597 ++TCAndArch;
4601 if (offloadDeviceOnly())
4602 return C.MakeAction<OffloadAction>(DDeps, types::TY_Nothing);
4604 if (OffloadActions.empty())
4605 return HostAction;
4607 OffloadAction::DeviceDependences DDep;
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());
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);
4639 OffloadAction::HostDependence HDep(
4640 *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(),
4641 /*BoundArch=*/nullptr, SingleDeviceOutput ? DDep : DDeps);
4642 return C.MakeAction<OffloadAction>(HDep, SingleDeviceOutput ? DDep : DDeps);
4645 Action *Driver::ConstructPhaseAction(
4646 Compilation &C, const ArgList &Args, phases::ID Phase, Action *Input,
4647 Action::OffloadKind TargetDeviceOffloadKind) const {
4648 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
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;
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) &&
4679 !CCGenDiagnostics)
4680 OutputTy = types::getPreprocessedType(OutputTy);
4681 assert(OutputTy != types::TY_INVALID &&
4682 "Cannot preprocess this input type!");
4684 return C.MakeAction<PreprocessJobAction>(Input, OutputTy);
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);
4691 types::ID OutputTy = getPrecompiledType(Input->getType());
4692 assert(OutputTy != types::TY_INVALID &&
4693 "Cannot precompile this input type!");
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;
4705 if (Args.hasArg(options::OPT_fsyntax_only)) {
4706 // Syntax checks should not emit a PCH file
4707 OutputTy = types::TY_Nothing;
4710 return C.MakeAction<PrecompileJobAction>(Input, OutputTy);
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);
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);
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);
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 ||
4761 offloadDeviceOnly() ||
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);
4769 return C.MakeAction<BackendJobAction>(Input, types::TY_PP_Asm);
4771 case phases::Assemble:
4772 return C.MakeAction<AssembleJobAction>(std::move(Input), types::TY_Object);
4775 llvm_unreachable("invalid phase in ConstructPhaseAction");
4778 void Driver::BuildJobs(Compilation &C) const {
4779 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
4781 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
4783 // It is an error to provide a -o option if we are making multiple output
4784 // files. There are exceptions:
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.
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.
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 &&
4806 !(A->getKind() == Action::IfsMergeJobClass ||
4807 (A->getType() == clang::driver::types::TY_IFS_CPP &&
4808 A->getKind() == clang::driver::Action::CompileJobClass &&
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();
4819 if (NumOutputs > 1) {
4820 Diag(clang::diag::err_drv_output_argument_with_multiple_files);
4821 FinalOutput = nullptr;
4825 const llvm::Triple &RawTriple = C.getDefaultToolChain().getTriple();
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());
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.
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();
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);
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;
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;
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();
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;
4915 OS << Buffer;
4916 OS.flush();
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;
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);
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);
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");
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;
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;
4956 for (const Arg *AA : C.getArgs().filtered(&Opt)) {
4957 if (AA->isClaimed()) {
4958 DuplicateClaimed = true;
4959 break;
4963 if (DuplicateClaimed)
4964 continue;
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());
4988 namespace {
4989 /// Utility class to control the collapse of dependent actions and select the
4990 /// tools accordingly.
4991 class ToolSelector final {
4992 /// The tool chain this selector refers to.
4993 const ToolChain &TC;
4995 /// The compilation this selector refers to.
4996 const Compilation &C;
4998 /// The base action this selector refers to.
4999 const JobAction *BaseAction;
5001 /// Set to true if the current toolchain refers to host actions.
5002 bool IsHostSelector;
5004 /// Set to true if save-temps and embed-bitcode functionalities are active.
5005 bool SaveTemps;
5006 bool EmbedBitcode;
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;
5018 Action *CurAction = *Inputs.begin();
5019 if (CanBeCollapsed &&
5020 !CurAction->isCollapsingWithNextDependentActionLegal())
5021 return nullptr;
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 &&
5034 !CurAction->isCollapsingWithNextDependentActionLegal())
5035 return nullptr;
5036 SavedOffloadAction.push_back(OA);
5037 return dyn_cast<JobAction>(CurAction);
5039 } else if (OA->hasHostDependence()) {
5040 CurAction = OA->getHostDependence();
5041 if (CanBeCollapsed &&
5042 !CurAction->isCollapsingWithNextDependentActionLegal())
5043 return nullptr;
5044 SavedOffloadAction.push_back(OA);
5045 return dyn_cast<JobAction>(CurAction);
5047 return nullptr;
5050 return dyn_cast<JobAction>(CurAction);
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);
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);
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;
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());
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;
5111 // Get compiler tool.
5112 const Tool *T = TC.SelectTool(*CJ);
5113 if (!T)
5114 return nullptr;
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;
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;
5130 if (!T->hasIntegratedAssembler())
5131 return nullptr;
5133 Inputs = CJ->getInputs();
5134 AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo,
5135 /*NumElements=*/3);
5136 return T;
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;
5148 // Get backend tool.
5149 const Tool *T = TC.SelectTool(*BJ);
5150 if (!T)
5151 return nullptr;
5153 if (!T->hasIntegratedAssembler())
5154 return nullptr;
5156 Inputs = BJ->getInputs();
5157 AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo,
5158 /*NumElements=*/2);
5159 return T;
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;
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;
5183 if (!InputIsBitcode && !canCollapsePreprocessorAction())
5184 return nullptr;
5186 // Get compiler tool.
5187 const Tool *T = TC.SelectTool(*CJ);
5188 if (!T)
5189 return nullptr;
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;
5197 if (T->canEmitIR() && ((SaveTemps && !InputIsBitcode) || EmbedBitcode))
5198 return nullptr;
5200 Inputs = CJ->getInputs();
5201 AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo,
5202 /*NumElements=*/2);
5203 return T;
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;
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;
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());
5231 Inputs = NewInputs;
5234 public:
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),
5238 EmbedBitcode(EmbedBitcode) {
5239 assert(BaseAction && "Invalid base action.");
5240 IsHostSelector = BaseAction->getOffloadingDeviceKind() == Action::OFK_None;
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) {
5252 // Get the largest chain of actions that we could combine.
5255 SmallVector<JobActionInfo, 5> ActionChain(1);
5256 ActionChain.back().JA = BaseAction;
5257 while (ActionChain.back().JA) {
5258 const Action *CurAction = ActionChain.back().JA;
5260 // Grow the chain by one element.
5261 ActionChain.resize(ActionChain.size() + 1);
5262 JobActionInfo &AI = ActionChain.back();
5264 // Attempt to fill it with the
5265 AI.JA =
5266 getPrevDependentAction(CurAction->getInputs(), AI.SavedOffloadAction);
5269 // Pop the last action info as it could not be filled.
5270 ActionChain.pop_back();
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.
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);
5289 combineWithPreprocessor(T, Inputs, CollapsedOffloadAction);
5290 return T;
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.
5300 static 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;
5308 TriplePlusArch += "-";
5309 TriplePlusArch += Action::GetOffloadKindName(OffloadKind);
5310 return TriplePlusArch;
5313 InputInfoList Driver::BuildJobsForAction(
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;
5325 InputInfoList Result = BuildJobsForActionNoCache(
5326 C, A, TC, BoundArch, AtTopLevel, MultipleArchs, LinkingOutput,
5327 CachedResults, TargetDeviceOffloadKind);
5328 CachedResults[ActionTC] = Result;
5329 return Result;
5332 static 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));
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();
5356 llvm::sys::path::replace_extension(Path, "json");
5358 const char *ResultFile = C.getArgs().MakeArgString(Path);
5359 C.addTimeTraceFile(ResultFile, JA);
5360 C.addResultFile(ResultFile, JA);
5363 InputInfoList 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");
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);
5380 // The offload action is expected to be used in four different situations.
5382 // a) Set a toolchain/architecture/kind for a host action:
5383 // Host Action 1 -> OffloadAction -> Host Action 2
5385 // b) Set a toolchain/architecture/kind for a device action;
5386 // Device Action 1 -> OffloadAction -> Device Action 2
5388 // c) Specify a device dependence to a host action;
5389 // Device Action 1 _
5390 // \
5391 // Host Action 1 ---> OffloadAction -> Host Action 2
5393 // d) Specify a host dependence to a device action.
5394 // Host Action 1 _
5395 // \
5396 // Device Action 1 ---> OffloadAction -> Device Action 2
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).
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()));
5414 return DevA;
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()));
5430 A = BuildingForOffloadDevice
5431 ? OA->getSingleDeviceDependence(/*DoNotConsiderHostActions=*/true)
5432 : OA->getHostDependence();
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;
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)};
5455 return {InputInfo(A, &Input, /* _BaseInput = */ "")};
5458 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
5459 const ToolChain *TC;
5460 StringRef ArchName = BAA->getArchName();
5462 if (!ArchName.empty())
5463 TC = &getToolChain(C.getArgs(),
5464 computeTargetTriple(*this, TargetTriple,
5465 C.getArgs(), ArchName));
5466 else
5467 TC = &C.getDefaultToolChain();
5469 return BuildJobsForAction(C, *BAA->input_begin(), TC, ArchName, AtTopLevel,
5470 MultipleArchs, LinkingOutput, CachedResults,
5471 TargetDeviceOffloadKind);
5475 ActionList Inputs = A->getInputs();
5477 const JobAction *JA = cast<JobAction>(A);
5478 ActionList CollapsedOffloadActions;
5480 ToolSelector TS(JA, *TC, C, isSaveTempsEnabled(),
5481 embedBitcodeInObject() && !isUsingLTO());
5482 const Tool *T = TS.getTool(Inputs, CollapsedOffloadActions);
5484 if (!T)
5485 return {InputInfo()};
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()));
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()));
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;
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();
5526 // Append outputs of offload device jobs to the input list
5527 if (!OffloadDependencesInputInfo.empty())
5528 InputInfos.append(OffloadDependencesInputInfo.begin(),
5529 OffloadDependencesInputInfo.end());
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()));
5543 RegisterEffectiveTriple TripleRAII(ToolTC, EffectiveTriple);
5545 // Determine the place to write output to, if any.
5546 InputInfo Result;
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 also update the results cache so that other actions using
5551 // this unbundling action can get the right results.
5552 for (auto &UI : UA->getDependentActionsInfo()) {
5553 assert(UI.DependentOffloadKind != Action::OFK_None &&
5554 "Unbundling with no offloading??");
5556 // Unbundling actions are never at the top level. When we generate the
5557 // offloading prefix, we also do that for the host file because the
5558 // unbundling action does not change the type of the output which can
5559 // cause a overwrite.
5560 std::string OffloadingPrefix = Action::GetOffloadingFileNamePrefix(
5561 UI.DependentOffloadKind,
5562 UI.DependentToolChain->getTriple().normalize(),
5563 /*CreatePrefixForHost=*/true);
5564 auto CurI = InputInfo(
5566 GetNamedOutputPath(C, *UA, BaseInput, UI.DependentBoundArch,
5567 /*AtTopLevel=*/false,
5568 MultipleArchs ||
5569 UI.DependentOffloadKind == Action::OFK_HIP,
5570 OffloadingPrefix),
5571 BaseInput);
5572 // Save the unbundling result.
5573 UnbundlingResults.push_back(CurI);
5575 // Get the unique string identifier for this dependence and cache the
5576 // result.
5577 StringRef Arch;
5578 if (TargetDeviceOffloadKind == Action::OFK_HIP) {
5579 if (UI.DependentOffloadKind == Action::OFK_Host)
5580 Arch = StringRef();
5581 else
5582 Arch = UI.DependentBoundArch;
5583 } else
5584 Arch = BoundArch;
5586 CachedResults[{A, GetTriplePlusArchString(UI.DependentToolChain, Arch,
5587 UI.DependentOffloadKind)}] = {
5588 CurI};
5591 // Now that we have all the results generated, select the one that should be
5592 // returned for the current depending action.
5593 std::pair<const Action *, std::string> ActionTC = {
5594 A, GetTriplePlusArchString(TC, BoundArch, TargetDeviceOffloadKind)};
5595 assert(CachedResults.find(ActionTC) != CachedResults.end() &&
5596 "Result does not exist??");
5597 Result = CachedResults[ActionTC].front();
5598 } else if (JA->getType() == types::TY_Nothing)
5599 Result = {InputInfo(A, BaseInput)};
5600 else {
5601 // We only have to generate a prefix for the host if this is not a top-level
5602 // action.
5603 std::string OffloadingPrefix = Action::GetOffloadingFileNamePrefix(
5604 A->getOffloadingDeviceKind(), TC->getTriple().normalize(),
5605 /*CreatePrefixForHost=*/isa<OffloadPackagerJobAction>(A) ||
5606 !(A->getOffloadingHostActiveKinds() == Action::OFK_None ||
5607 AtTopLevel));
5608 Result = InputInfo(A, GetNamedOutputPath(C, *JA, BaseInput, BoundArch,
5609 AtTopLevel, MultipleArchs,
5610 OffloadingPrefix),
5611 BaseInput);
5612 if (T->canEmitIR() && OffloadingPrefix.empty())
5613 handleTimeTrace(C, Args, JA, BaseInput, Result);
5616 if (CCCPrintBindings && !CCGenDiagnostics) {
5617 llvm::errs() << "# \"" << T->getToolChain().getTripleString() << '"'
5618 << " - \"" << T->getName() << "\", inputs: [";
5619 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
5620 llvm::errs() << InputInfos[i].getAsString();
5621 if (i + 1 != e)
5622 llvm::errs() << ", ";
5624 if (UnbundlingResults.empty())
5625 llvm::errs() << "], output: " << Result.getAsString() << "\n";
5626 else {
5627 llvm::errs() << "], outputs: [";
5628 for (unsigned i = 0, e = UnbundlingResults.size(); i != e; ++i) {
5629 llvm::errs() << UnbundlingResults[i].getAsString();
5630 if (i + 1 != e)
5631 llvm::errs() << ", ";
5633 llvm::errs() << "] \n";
5635 } else {
5636 if (UnbundlingResults.empty())
5637 T->ConstructJob(
5638 C, *JA, Result, InputInfos,
5639 C.getArgsForToolChain(TC, BoundArch, JA->getOffloadingDeviceKind()),
5640 LinkingOutput);
5641 else
5642 T->ConstructJobMultipleOutputs(
5643 C, *JA, UnbundlingResults, InputInfos,
5644 C.getArgsForToolChain(TC, BoundArch, JA->getOffloadingDeviceKind()),
5645 LinkingOutput);
5647 return {Result};
5650 const char *Driver::getDefaultImageName() const {
5651 llvm::Triple Target(llvm::Triple::normalize(TargetTriple));
5652 return Target.isOSWindows() ? "a.exe" : "a.out";
5655 /// Create output filename based on ArgValue, which could either be a
5656 /// full filename, filename without extension, or a directory. If ArgValue
5657 /// does not provide a filename, then use BaseName, and use the extension
5658 /// suitable for FileType.
5659 static const char *MakeCLOutputFilename(const ArgList &Args, StringRef ArgValue,
5660 StringRef BaseName,
5661 types::ID FileType) {
5662 SmallString<128> Filename = ArgValue;
5664 if (ArgValue.empty()) {
5665 // If the argument is empty, output to BaseName in the current dir.
5666 Filename = BaseName;
5667 } else if (llvm::sys::path::is_separator(Filename.back())) {
5668 // If the argument is a directory, output to BaseName in that dir.
5669 llvm::sys::path::append(Filename, BaseName);
5672 if (!llvm::sys::path::has_extension(ArgValue)) {
5673 // If the argument didn't provide an extension, then set it.
5674 const char *Extension = types::getTypeTempSuffix(FileType, true);
5676 if (FileType == types::TY_Image &&
5677 Args.hasArg(options::OPT__SLASH_LD, options::OPT__SLASH_LDd)) {
5678 // The output file is a dll.
5679 Extension = "dll";
5682 llvm::sys::path::replace_extension(Filename, Extension);
5685 return Args.MakeArgString(Filename.c_str());
5688 static bool HasPreprocessOutput(const Action &JA) {
5689 if (isa<PreprocessJobAction>(JA))
5690 return true;
5691 if (isa<OffloadAction>(JA) && isa<PreprocessJobAction>(JA.getInputs()[0]))
5692 return true;
5693 if (isa<OffloadBundlingJobAction>(JA) &&
5694 HasPreprocessOutput(*(JA.getInputs()[0])))
5695 return true;
5696 return false;
5699 const char *Driver::CreateTempFile(Compilation &C, StringRef Prefix,
5700 StringRef Suffix, bool MultipleArchs,
5701 StringRef BoundArch,
5702 bool NeedUniqueDirectory) const {
5703 SmallString<128> TmpName;
5704 Arg *A = C.getArgs().getLastArg(options::OPT_fcrash_diagnostics_dir);
5705 std::optional<std::string> CrashDirectory =
5706 CCGenDiagnostics && A
5707 ? std::string(A->getValue())
5708 : llvm::sys::Process::GetEnv("CLANG_CRASH_DIAGNOSTICS_DIR");
5709 if (CrashDirectory) {
5710 if (!getVFS().exists(*CrashDirectory))
5711 llvm::sys::fs::create_directories(*CrashDirectory);
5712 SmallString<128> Path(*CrashDirectory);
5713 llvm::sys::path::append(Path, Prefix);
5714 const char *Middle = !Suffix.empty() ? "-%%%%%%." : "-%%%%%%";
5715 if (std::error_code EC =
5716 llvm::sys::fs::createUniqueFile(Path + Middle + Suffix, TmpName)) {
5717 Diag(clang::diag::err_unable_to_make_temp) << EC.message();
5718 return "";
5720 } else {
5721 if (MultipleArchs && !BoundArch.empty()) {
5722 if (NeedUniqueDirectory) {
5723 TmpName = GetTemporaryDirectory(Prefix);
5724 llvm::sys::path::append(TmpName,
5725 Twine(Prefix) + "-" + BoundArch + "." + Suffix);
5726 } else {
5727 TmpName =
5728 GetTemporaryPath((Twine(Prefix) + "-" + BoundArch).str(), Suffix);
5731 } else {
5732 TmpName = GetTemporaryPath(Prefix, Suffix);
5735 return C.addTempFile(C.getArgs().MakeArgString(TmpName));
5738 // Calculate the output path of the module file when compiling a module unit
5739 // with the `-fmodule-output` option or `-fmodule-output=` option specified.
5740 // The behavior is:
5741 // - If `-fmodule-output=` is specfied, then the module file is
5742 // writing to the value.
5743 // - Otherwise if the output object file of the module unit is specified, the
5744 // output path
5745 // of the module file should be the same with the output object file except
5746 // the corresponding suffix. This requires both `-o` and `-c` are specified.
5747 // - Otherwise, the output path of the module file will be the same with the
5748 // input with the corresponding suffix.
5749 static const char *GetModuleOutputPath(Compilation &C, const JobAction &JA,
5750 const char *BaseInput) {
5751 assert(isa<PrecompileJobAction>(JA) && JA.getType() == types::TY_ModuleFile &&
5752 (C.getArgs().hasArg(options::OPT_fmodule_output) ||
5753 C.getArgs().hasArg(options::OPT_fmodule_output_EQ)));
5755 if (Arg *ModuleOutputEQ =
5756 C.getArgs().getLastArg(options::OPT_fmodule_output_EQ))
5757 return C.addResultFile(ModuleOutputEQ->getValue(), &JA);
5759 SmallString<64> OutputPath;
5760 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
5761 if (FinalOutput && C.getArgs().hasArg(options::OPT_c))
5762 OutputPath = FinalOutput->getValue();
5763 else
5764 OutputPath = BaseInput;
5766 const char *Extension = types::getTypeTempSuffix(JA.getType());
5767 llvm::sys::path::replace_extension(OutputPath, Extension);
5768 return C.addResultFile(C.getArgs().MakeArgString(OutputPath.c_str()), &JA);
5771 const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA,
5772 const char *BaseInput,
5773 StringRef OrigBoundArch, bool AtTopLevel,
5774 bool MultipleArchs,
5775 StringRef OffloadingPrefix) const {
5776 std::string BoundArch = OrigBoundArch.str();
5777 if (is_style_windows(llvm::sys::path::Style::native)) {
5778 // BoundArch may contains ':', which is invalid in file names on Windows,
5779 // therefore replace it with '%'.
5780 std::replace(BoundArch.begin(), BoundArch.end(), ':', '@');
5783 llvm::PrettyStackTraceString CrashInfo("Computing output path");
5784 // Output to a user requested destination?
5785 if (AtTopLevel && !isa<DsymutilJobAction>(JA) && !isa<VerifyJobAction>(JA)) {
5786 if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
5787 return C.addResultFile(FinalOutput->getValue(), &JA);
5790 // For /P, preprocess to file named after BaseInput.
5791 if (C.getArgs().hasArg(options::OPT__SLASH_P)) {
5792 assert(AtTopLevel && isa<PreprocessJobAction>(JA));
5793 StringRef BaseName = llvm::sys::path::filename(BaseInput);
5794 StringRef NameArg;
5795 if (Arg *A = C.getArgs().getLastArg(options::OPT__SLASH_Fi))
5796 NameArg = A->getValue();
5797 return C.addResultFile(
5798 MakeCLOutputFilename(C.getArgs(), NameArg, BaseName, types::TY_PP_C),
5799 &JA);
5802 // Default to writing to stdout?
5803 if (AtTopLevel && !CCGenDiagnostics && HasPreprocessOutput(JA)) {
5804 return "-";
5807 if (JA.getType() == types::TY_ModuleFile &&
5808 C.getArgs().getLastArg(options::OPT_module_file_info)) {
5809 return "-";
5812 if (JA.getType() == types::TY_PP_Asm &&
5813 C.getArgs().hasArg(options::OPT_dxc_Fc)) {
5814 StringRef FcValue = C.getArgs().getLastArgValue(options::OPT_dxc_Fc);
5815 // TODO: Should we use `MakeCLOutputFilename` here? If so, we can probably
5816 // handle this as part of the SLASH_Fa handling below.
5817 return C.addResultFile(C.getArgs().MakeArgString(FcValue.str()), &JA);
5820 if (JA.getType() == types::TY_Object &&
5821 C.getArgs().hasArg(options::OPT_dxc_Fo)) {
5822 StringRef FoValue = C.getArgs().getLastArgValue(options::OPT_dxc_Fo);
5823 // TODO: Should we use `MakeCLOutputFilename` here? If so, we can probably
5824 // handle this as part of the SLASH_Fo handling below.
5825 return C.addResultFile(C.getArgs().MakeArgString(FoValue.str()), &JA);
5828 // Is this the assembly listing for /FA?
5829 if (JA.getType() == types::TY_PP_Asm &&
5830 (C.getArgs().hasArg(options::OPT__SLASH_FA) ||
5831 C.getArgs().hasArg(options::OPT__SLASH_Fa))) {
5832 // Use /Fa and the input filename to determine the asm file name.
5833 StringRef BaseName = llvm::sys::path::filename(BaseInput);
5834 StringRef FaValue = C.getArgs().getLastArgValue(options::OPT__SLASH_Fa);
5835 return C.addResultFile(
5836 MakeCLOutputFilename(C.getArgs(), FaValue, BaseName, JA.getType()),
5837 &JA);
5840 // DXC defaults to standard out when generating assembly. We check this after
5841 // any DXC flags that might specify a file.
5842 if (AtTopLevel && JA.getType() == types::TY_PP_Asm && IsDXCMode())
5843 return "-";
5845 bool SpecifiedModuleOutput =
5846 C.getArgs().hasArg(options::OPT_fmodule_output) ||
5847 C.getArgs().hasArg(options::OPT_fmodule_output_EQ);
5848 if (MultipleArchs && SpecifiedModuleOutput)
5849 Diag(clang::diag::err_drv_module_output_with_multiple_arch);
5851 // If we're emitting a module output with the specified option
5852 // `-fmodule-output`.
5853 if (!AtTopLevel && isa<PrecompileJobAction>(JA) &&
5854 JA.getType() == types::TY_ModuleFile && SpecifiedModuleOutput)
5855 return GetModuleOutputPath(C, JA, BaseInput);
5857 // Output to a temporary file?
5858 if ((!AtTopLevel && !isSaveTempsEnabled() &&
5859 !C.getArgs().hasArg(options::OPT__SLASH_Fo)) ||
5860 CCGenDiagnostics) {
5861 StringRef Name = llvm::sys::path::filename(BaseInput);
5862 std::pair<StringRef, StringRef> Split = Name.split('.');
5863 const char *Suffix =
5864 types::getTypeTempSuffix(JA.getType(), IsCLMode() || IsDXCMode());
5865 // The non-offloading toolchain on Darwin requires deterministic input
5866 // file name for binaries to be deterministic, therefore it needs unique
5867 // directory.
5868 llvm::Triple Triple(C.getDriver().getTargetTriple());
5869 bool NeedUniqueDirectory =
5870 (JA.getOffloadingDeviceKind() == Action::OFK_None ||
5871 JA.getOffloadingDeviceKind() == Action::OFK_Host) &&
5872 Triple.isOSDarwin();
5873 return CreateTempFile(C, Split.first, Suffix, MultipleArchs, BoundArch,
5874 NeedUniqueDirectory);
5877 SmallString<128> BasePath(BaseInput);
5878 SmallString<128> ExternalPath("");
5879 StringRef BaseName;
5881 // Dsymutil actions should use the full path.
5882 if (isa<DsymutilJobAction>(JA) && C.getArgs().hasArg(options::OPT_dsym_dir)) {
5883 ExternalPath += C.getArgs().getLastArg(options::OPT_dsym_dir)->getValue();
5884 // We use posix style here because the tests (specifically
5885 // darwin-dsymutil.c) demonstrate that posix style paths are acceptable
5886 // even on Windows and if we don't then the similar test covering this
5887 // fails.
5888 llvm::sys::path::append(ExternalPath, llvm::sys::path::Style::posix,
5889 llvm::sys::path::filename(BasePath));
5890 BaseName = ExternalPath;
5891 } else if (isa<DsymutilJobAction>(JA) || isa<VerifyJobAction>(JA))
5892 BaseName = BasePath;
5893 else
5894 BaseName = llvm::sys::path::filename(BasePath);
5896 // Determine what the derived output name should be.
5897 const char *NamedOutput;
5899 if ((JA.getType() == types::TY_Object || JA.getType() == types::TY_LTO_BC) &&
5900 C.getArgs().hasArg(options::OPT__SLASH_Fo, options::OPT__SLASH_o)) {
5901 // The /Fo or /o flag decides the object filename.
5902 StringRef Val =
5903 C.getArgs()
5904 .getLastArg(options::OPT__SLASH_Fo, options::OPT__SLASH_o)
5905 ->getValue();
5906 NamedOutput =
5907 MakeCLOutputFilename(C.getArgs(), Val, BaseName, types::TY_Object);
5908 } else if (JA.getType() == types::TY_Image &&
5909 C.getArgs().hasArg(options::OPT__SLASH_Fe,
5910 options::OPT__SLASH_o)) {
5911 // The /Fe or /o flag names the linked file.
5912 StringRef Val =
5913 C.getArgs()
5914 .getLastArg(options::OPT__SLASH_Fe, options::OPT__SLASH_o)
5915 ->getValue();
5916 NamedOutput =
5917 MakeCLOutputFilename(C.getArgs(), Val, BaseName, types::TY_Image);
5918 } else if (JA.getType() == types::TY_Image) {
5919 if (IsCLMode()) {
5920 // clang-cl uses BaseName for the executable name.
5921 NamedOutput =
5922 MakeCLOutputFilename(C.getArgs(), "", BaseName, types::TY_Image);
5923 } else {
5924 SmallString<128> Output(getDefaultImageName());
5925 // HIP image for device compilation with -fno-gpu-rdc is per compilation
5926 // unit.
5927 bool IsHIPNoRDC = JA.getOffloadingDeviceKind() == Action::OFK_HIP &&
5928 !C.getArgs().hasFlag(options::OPT_fgpu_rdc,
5929 options::OPT_fno_gpu_rdc, false);
5930 bool UseOutExtension = IsHIPNoRDC || isa<OffloadPackagerJobAction>(JA);
5931 if (UseOutExtension) {
5932 Output = BaseName;
5933 llvm::sys::path::replace_extension(Output, "");
5935 Output += OffloadingPrefix;
5936 if (MultipleArchs && !BoundArch.empty()) {
5937 Output += "-";
5938 Output.append(BoundArch);
5940 if (UseOutExtension)
5941 Output += ".out";
5942 NamedOutput = C.getArgs().MakeArgString(Output.c_str());
5944 } else if (JA.getType() == types::TY_PCH && IsCLMode()) {
5945 NamedOutput = C.getArgs().MakeArgString(GetClPchPath(C, BaseName));
5946 } else if ((JA.getType() == types::TY_Plist || JA.getType() == types::TY_AST) &&
5947 C.getArgs().hasArg(options::OPT__SLASH_o)) {
5948 StringRef Val =
5949 C.getArgs()
5950 .getLastArg(options::OPT__SLASH_o)
5951 ->getValue();
5952 NamedOutput =
5953 MakeCLOutputFilename(C.getArgs(), Val, BaseName, types::TY_Object);
5954 } else {
5955 const char *Suffix =
5956 types::getTypeTempSuffix(JA.getType(), IsCLMode() || IsDXCMode());
5957 assert(Suffix && "All types used for output should have a suffix.");
5959 std::string::size_type End = std::string::npos;
5960 if (!types::appendSuffixForType(JA.getType()))
5961 End = BaseName.rfind('.');
5962 SmallString<128> Suffixed(BaseName.substr(0, End));
5963 Suffixed += OffloadingPrefix;
5964 if (MultipleArchs && !BoundArch.empty()) {
5965 Suffixed += "-";
5966 Suffixed.append(BoundArch);
5968 // When using both -save-temps and -emit-llvm, use a ".tmp.bc" suffix for
5969 // the unoptimized bitcode so that it does not get overwritten by the ".bc"
5970 // optimized bitcode output.
5971 auto IsAMDRDCInCompilePhase = [](const JobAction &JA,
5972 const llvm::opt::DerivedArgList &Args) {
5973 // The relocatable compilation in HIP and OpenMP implies -emit-llvm.
5974 // Similarly, use a ".tmp.bc" suffix for the unoptimized bitcode
5975 // (generated in the compile phase.)
5976 const ToolChain *TC = JA.getOffloadingToolChain();
5977 return isa<CompileJobAction>(JA) &&
5978 ((JA.getOffloadingDeviceKind() == Action::OFK_HIP &&
5979 Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
5980 false)) ||
5981 (JA.getOffloadingDeviceKind() == Action::OFK_OpenMP && TC &&
5982 TC->getTriple().isAMDGPU()));
5984 if (!AtTopLevel && JA.getType() == types::TY_LLVM_BC &&
5985 (C.getArgs().hasArg(options::OPT_emit_llvm) ||
5986 IsAMDRDCInCompilePhase(JA, C.getArgs())))
5987 Suffixed += ".tmp";
5988 Suffixed += '.';
5989 Suffixed += Suffix;
5990 NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
5993 // Prepend object file path if -save-temps=obj
5994 if (!AtTopLevel && isSaveTempsObj() && C.getArgs().hasArg(options::OPT_o) &&
5995 JA.getType() != types::TY_PCH) {
5996 Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
5997 SmallString<128> TempPath(FinalOutput->getValue());
5998 llvm::sys::path::remove_filename(TempPath);
5999 StringRef OutputFileName = llvm::sys::path::filename(NamedOutput);
6000 llvm::sys::path::append(TempPath, OutputFileName);
6001 NamedOutput = C.getArgs().MakeArgString(TempPath.c_str());
6004 // If we're saving temps and the temp file conflicts with the input file,
6005 // then avoid overwriting input file.
6006 if (!AtTopLevel && isSaveTempsEnabled() && NamedOutput == BaseName) {
6007 bool SameFile = false;
6008 SmallString<256> Result;
6009 llvm::sys::fs::current_path(Result);
6010 llvm::sys::path::append(Result, BaseName);
6011 llvm::sys::fs::equivalent(BaseInput, Result.c_str(), SameFile);
6012 // Must share the same path to conflict.
6013 if (SameFile) {
6014 StringRef Name = llvm::sys::path::filename(BaseInput);
6015 std::pair<StringRef, StringRef> Split = Name.split('.');
6016 std::string TmpName = GetTemporaryPath(
6017 Split.first,
6018 types::getTypeTempSuffix(JA.getType(), IsCLMode() || IsDXCMode()));
6019 return C.addTempFile(C.getArgs().MakeArgString(TmpName));
6023 // As an annoying special case, PCH generation doesn't strip the pathname.
6024 if (JA.getType() == types::TY_PCH && !IsCLMode()) {
6025 llvm::sys::path::remove_filename(BasePath);
6026 if (BasePath.empty())
6027 BasePath = NamedOutput;
6028 else
6029 llvm::sys::path::append(BasePath, NamedOutput);
6030 return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()), &JA);
6033 return C.addResultFile(NamedOutput, &JA);
6036 std::string Driver::GetFilePath(StringRef Name, const ToolChain &TC) const {
6037 // Search for Name in a list of paths.
6038 auto SearchPaths = [&](const llvm::SmallVectorImpl<std::string> &P)
6039 -> std::optional<std::string> {
6040 // Respect a limited subset of the '-Bprefix' functionality in GCC by
6041 // attempting to use this prefix when looking for file paths.
6042 for (const auto &Dir : P) {
6043 if (Dir.empty())
6044 continue;
6045 SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir);
6046 llvm::sys::path::append(P, Name);
6047 if (llvm::sys::fs::exists(Twine(P)))
6048 return std::string(P);
6050 return std::nullopt;
6053 if (auto P = SearchPaths(PrefixDirs))
6054 return *P;
6056 SmallString<128> R(ResourceDir);
6057 llvm::sys::path::append(R, Name);
6058 if (llvm::sys::fs::exists(Twine(R)))
6059 return std::string(R.str());
6061 SmallString<128> P(TC.getCompilerRTPath());
6062 llvm::sys::path::append(P, Name);
6063 if (llvm::sys::fs::exists(Twine(P)))
6064 return std::string(P.str());
6066 SmallString<128> D(Dir);
6067 llvm::sys::path::append(D, "..", Name);
6068 if (llvm::sys::fs::exists(Twine(D)))
6069 return std::string(D.str());
6071 if (auto P = SearchPaths(TC.getLibraryPaths()))
6072 return *P;
6074 if (auto P = SearchPaths(TC.getFilePaths()))
6075 return *P;
6077 return std::string(Name);
6080 void Driver::generatePrefixedToolNames(
6081 StringRef Tool, const ToolChain &TC,
6082 SmallVectorImpl<std::string> &Names) const {
6083 // FIXME: Needs a better variable than TargetTriple
6084 Names.emplace_back((TargetTriple + "-" + Tool).str());
6085 Names.emplace_back(Tool);
6088 static bool ScanDirForExecutable(SmallString<128> &Dir, StringRef Name) {
6089 llvm::sys::path::append(Dir, Name);
6090 if (llvm::sys::fs::can_execute(Twine(Dir)))
6091 return true;
6092 llvm::sys::path::remove_filename(Dir);
6093 return false;
6096 std::string Driver::GetProgramPath(StringRef Name, const ToolChain &TC) const {
6097 SmallVector<std::string, 2> TargetSpecificExecutables;
6098 generatePrefixedToolNames(Name, TC, TargetSpecificExecutables);
6100 // Respect a limited subset of the '-Bprefix' functionality in GCC by
6101 // attempting to use this prefix when looking for program paths.
6102 for (const auto &PrefixDir : PrefixDirs) {
6103 if (llvm::sys::fs::is_directory(PrefixDir)) {
6104 SmallString<128> P(PrefixDir);
6105 if (ScanDirForExecutable(P, Name))
6106 return std::string(P.str());
6107 } else {
6108 SmallString<128> P((PrefixDir + Name).str());
6109 if (llvm::sys::fs::can_execute(Twine(P)))
6110 return std::string(P.str());
6114 const ToolChain::path_list &List = TC.getProgramPaths();
6115 for (const auto &TargetSpecificExecutable : TargetSpecificExecutables) {
6116 // For each possible name of the tool look for it in
6117 // program paths first, then the path.
6118 // Higher priority names will be first, meaning that
6119 // a higher priority name in the path will be found
6120 // instead of a lower priority name in the program path.
6121 // E.g. <triple>-gcc on the path will be found instead
6122 // of gcc in the program path
6123 for (const auto &Path : List) {
6124 SmallString<128> P(Path);
6125 if (ScanDirForExecutable(P, TargetSpecificExecutable))
6126 return std::string(P.str());
6129 // Fall back to the path
6130 if (llvm::ErrorOr<std::string> P =
6131 llvm::sys::findProgramByName(TargetSpecificExecutable))
6132 return *P;
6135 return std::string(Name);
6138 std::string Driver::GetTemporaryPath(StringRef Prefix, StringRef Suffix) const {
6139 SmallString<128> Path;
6140 std::error_code EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, Path);
6141 if (EC) {
6142 Diag(clang::diag::err_unable_to_make_temp) << EC.message();
6143 return "";
6146 return std::string(Path.str());
6149 std::string Driver::GetTemporaryDirectory(StringRef Prefix) const {
6150 SmallString<128> Path;
6151 std::error_code EC = llvm::sys::fs::createUniqueDirectory(Prefix, Path);
6152 if (EC) {
6153 Diag(clang::diag::err_unable_to_make_temp) << EC.message();
6154 return "";
6157 return std::string(Path.str());
6160 std::string Driver::GetClPchPath(Compilation &C, StringRef BaseName) const {
6161 SmallString<128> Output;
6162 if (Arg *FpArg = C.getArgs().getLastArg(options::OPT__SLASH_Fp)) {
6163 // FIXME: If anybody needs it, implement this obscure rule:
6164 // "If you specify a directory without a file name, the default file name
6165 // is VCx0.pch., where x is the major version of Visual C++ in use."
6166 Output = FpArg->getValue();
6168 // "If you do not specify an extension as part of the path name, an
6169 // extension of .pch is assumed. "
6170 if (!llvm::sys::path::has_extension(Output))
6171 Output += ".pch";
6172 } else {
6173 if (Arg *YcArg = C.getArgs().getLastArg(options::OPT__SLASH_Yc))
6174 Output = YcArg->getValue();
6175 if (Output.empty())
6176 Output = BaseName;
6177 llvm::sys::path::replace_extension(Output, ".pch");
6179 return std::string(Output.str());
6182 const ToolChain &Driver::getToolChain(const ArgList &Args,
6183 const llvm::Triple &Target) const {
6185 auto &TC = ToolChains[Target.str()];
6186 if (!TC) {
6187 switch (Target.getOS()) {
6188 case llvm::Triple::AIX:
6189 TC = std::make_unique<toolchains::AIX>(*this, Target, Args);
6190 break;
6191 case llvm::Triple::Haiku:
6192 TC = std::make_unique<toolchains::Haiku>(*this, Target, Args);
6193 break;
6194 case llvm::Triple::Darwin:
6195 case llvm::Triple::MacOSX:
6196 case llvm::Triple::IOS:
6197 case llvm::Triple::TvOS:
6198 case llvm::Triple::WatchOS:
6199 case llvm::Triple::DriverKit:
6200 TC = std::make_unique<toolchains::DarwinClang>(*this, Target, Args);
6201 break;
6202 case llvm::Triple::DragonFly:
6203 TC = std::make_unique<toolchains::DragonFly>(*this, Target, Args);
6204 break;
6205 case llvm::Triple::OpenBSD:
6206 TC = std::make_unique<toolchains::OpenBSD>(*this, Target, Args);
6207 break;
6208 case llvm::Triple::NetBSD:
6209 TC = std::make_unique<toolchains::NetBSD>(*this, Target, Args);
6210 break;
6211 case llvm::Triple::FreeBSD:
6212 if (Target.isPPC())
6213 TC = std::make_unique<toolchains::PPCFreeBSDToolChain>(*this, Target,
6214 Args);
6215 else
6216 TC = std::make_unique<toolchains::FreeBSD>(*this, Target, Args);
6217 break;
6218 case llvm::Triple::Linux:
6219 case llvm::Triple::ELFIAMCU:
6220 if (Target.getArch() == llvm::Triple::hexagon)
6221 TC = std::make_unique<toolchains::HexagonToolChain>(*this, Target,
6222 Args);
6223 else if ((Target.getVendor() == llvm::Triple::MipsTechnologies) &&
6224 !Target.hasEnvironment())
6225 TC = std::make_unique<toolchains::MipsLLVMToolChain>(*this, Target,
6226 Args);
6227 else if (Target.isPPC())
6228 TC = std::make_unique<toolchains::PPCLinuxToolChain>(*this, Target,
6229 Args);
6230 else if (Target.getArch() == llvm::Triple::ve)
6231 TC = std::make_unique<toolchains::VEToolChain>(*this, Target, Args);
6232 else if (Target.isOHOSFamily())
6233 TC = std::make_unique<toolchains::OHOS>(*this, Target, Args);
6234 else
6235 TC = std::make_unique<toolchains::Linux>(*this, Target, Args);
6236 break;
6237 case llvm::Triple::NaCl:
6238 TC = std::make_unique<toolchains::NaClToolChain>(*this, Target, Args);
6239 break;
6240 case llvm::Triple::Fuchsia:
6241 TC = std::make_unique<toolchains::Fuchsia>(*this, Target, Args);
6242 break;
6243 case llvm::Triple::Solaris:
6244 TC = std::make_unique<toolchains::Solaris>(*this, Target, Args);
6245 break;
6246 case llvm::Triple::CUDA:
6247 TC = std::make_unique<toolchains::NVPTXToolChain>(*this, Target, Args);
6248 break;
6249 case llvm::Triple::AMDHSA:
6250 TC = std::make_unique<toolchains::ROCMToolChain>(*this, Target, Args);
6251 break;
6252 case llvm::Triple::AMDPAL:
6253 case llvm::Triple::Mesa3D:
6254 TC = std::make_unique<toolchains::AMDGPUToolChain>(*this, Target, Args);
6255 break;
6256 case llvm::Triple::Win32:
6257 switch (Target.getEnvironment()) {
6258 default:
6259 if (Target.isOSBinFormatELF())
6260 TC = std::make_unique<toolchains::Generic_ELF>(*this, Target, Args);
6261 else if (Target.isOSBinFormatMachO())
6262 TC = std::make_unique<toolchains::MachO>(*this, Target, Args);
6263 else
6264 TC = std::make_unique<toolchains::Generic_GCC>(*this, Target, Args);
6265 break;
6266 case llvm::Triple::GNU:
6267 TC = std::make_unique<toolchains::MinGW>(*this, Target, Args);
6268 break;
6269 case llvm::Triple::Itanium:
6270 TC = std::make_unique<toolchains::CrossWindowsToolChain>(*this, Target,
6271 Args);
6272 break;
6273 case llvm::Triple::MSVC:
6274 case llvm::Triple::UnknownEnvironment:
6275 if (Args.getLastArgValue(options::OPT_fuse_ld_EQ)
6276 .starts_with_insensitive("bfd"))
6277 TC = std::make_unique<toolchains::CrossWindowsToolChain>(
6278 *this, Target, Args);
6279 else
6280 TC =
6281 std::make_unique<toolchains::MSVCToolChain>(*this, Target, Args);
6282 break;
6284 break;
6285 case llvm::Triple::PS4:
6286 TC = std::make_unique<toolchains::PS4CPU>(*this, Target, Args);
6287 break;
6288 case llvm::Triple::PS5:
6289 TC = std::make_unique<toolchains::PS5CPU>(*this, Target, Args);
6290 break;
6291 case llvm::Triple::Hurd:
6292 TC = std::make_unique<toolchains::Hurd>(*this, Target, Args);
6293 break;
6294 case llvm::Triple::LiteOS:
6295 TC = std::make_unique<toolchains::OHOS>(*this, Target, Args);
6296 break;
6297 case llvm::Triple::ZOS:
6298 TC = std::make_unique<toolchains::ZOS>(*this, Target, Args);
6299 break;
6300 case llvm::Triple::ShaderModel:
6301 TC = std::make_unique<toolchains::HLSLToolChain>(*this, Target, Args);
6302 break;
6303 default:
6304 // Of these targets, Hexagon is the only one that might have
6305 // an OS of Linux, in which case it got handled above already.
6306 switch (Target.getArch()) {
6307 case llvm::Triple::tce:
6308 TC = std::make_unique<toolchains::TCEToolChain>(*this, Target, Args);
6309 break;
6310 case llvm::Triple::tcele:
6311 TC = std::make_unique<toolchains::TCELEToolChain>(*this, Target, Args);
6312 break;
6313 case llvm::Triple::hexagon:
6314 TC = std::make_unique<toolchains::HexagonToolChain>(*this, Target,
6315 Args);
6316 break;
6317 case llvm::Triple::lanai:
6318 TC = std::make_unique<toolchains::LanaiToolChain>(*this, Target, Args);
6319 break;
6320 case llvm::Triple::xcore:
6321 TC = std::make_unique<toolchains::XCoreToolChain>(*this, Target, Args);
6322 break;
6323 case llvm::Triple::wasm32:
6324 case llvm::Triple::wasm64:
6325 TC = std::make_unique<toolchains::WebAssembly>(*this, Target, Args);
6326 break;
6327 case llvm::Triple::avr:
6328 TC = std::make_unique<toolchains::AVRToolChain>(*this, Target, Args);
6329 break;
6330 case llvm::Triple::msp430:
6331 TC =
6332 std::make_unique<toolchains::MSP430ToolChain>(*this, Target, Args);
6333 break;
6334 case llvm::Triple::riscv32:
6335 case llvm::Triple::riscv64:
6336 if (toolchains::RISCVToolChain::hasGCCToolchain(*this, Args))
6337 TC =
6338 std::make_unique<toolchains::RISCVToolChain>(*this, Target, Args);
6339 else
6340 TC = std::make_unique<toolchains::BareMetal>(*this, Target, Args);
6341 break;
6342 case llvm::Triple::ve:
6343 TC = std::make_unique<toolchains::VEToolChain>(*this, Target, Args);
6344 break;
6345 case llvm::Triple::spirv32:
6346 case llvm::Triple::spirv64:
6347 TC = std::make_unique<toolchains::SPIRVToolChain>(*this, Target, Args);
6348 break;
6349 case llvm::Triple::csky:
6350 TC = std::make_unique<toolchains::CSKYToolChain>(*this, Target, Args);
6351 break;
6352 default:
6353 if (toolchains::BareMetal::handlesTarget(Target))
6354 TC = std::make_unique<toolchains::BareMetal>(*this, Target, Args);
6355 else if (Target.isOSBinFormatELF())
6356 TC = std::make_unique<toolchains::Generic_ELF>(*this, Target, Args);
6357 else if (Target.isOSBinFormatMachO())
6358 TC = std::make_unique<toolchains::MachO>(*this, Target, Args);
6359 else
6360 TC = std::make_unique<toolchains::Generic_GCC>(*this, Target, Args);
6365 return *TC;
6368 const ToolChain &Driver::getOffloadingDeviceToolChain(
6369 const ArgList &Args, const llvm::Triple &Target, const ToolChain &HostTC,
6370 const Action::OffloadKind &TargetDeviceOffloadKind) const {
6371 // Use device / host triples as the key into the ToolChains map because the
6372 // device ToolChain we create depends on both.
6373 auto &TC = ToolChains[Target.str() + "/" + HostTC.getTriple().str()];
6374 if (!TC) {
6375 // Categorized by offload kind > arch rather than OS > arch like
6376 // the normal getToolChain call, as it seems a reasonable way to categorize
6377 // things.
6378 switch (TargetDeviceOffloadKind) {
6379 case Action::OFK_HIP: {
6380 if (Target.getArch() == llvm::Triple::amdgcn &&
6381 Target.getVendor() == llvm::Triple::AMD &&
6382 Target.getOS() == llvm::Triple::AMDHSA)
6383 TC = std::make_unique<toolchains::HIPAMDToolChain>(*this, Target,
6384 HostTC, Args);
6385 else if (Target.getArch() == llvm::Triple::spirv64 &&
6386 Target.getVendor() == llvm::Triple::UnknownVendor &&
6387 Target.getOS() == llvm::Triple::UnknownOS)
6388 TC = std::make_unique<toolchains::HIPSPVToolChain>(*this, Target,
6389 HostTC, Args);
6390 break;
6392 default:
6393 break;
6397 return *TC;
6400 bool Driver::ShouldUseClangCompiler(const JobAction &JA) const {
6401 // Say "no" if there is not exactly one input of a type clang understands.
6402 if (JA.size() != 1 ||
6403 !types::isAcceptedByClang((*JA.input_begin())->getType()))
6404 return false;
6406 // And say "no" if this is not a kind of action clang understands.
6407 if (!isa<PreprocessJobAction>(JA) && !isa<PrecompileJobAction>(JA) &&
6408 !isa<CompileJobAction>(JA) && !isa<BackendJobAction>(JA) &&
6409 !isa<ExtractAPIJobAction>(JA))
6410 return false;
6412 return true;
6415 bool Driver::ShouldUseFlangCompiler(const JobAction &JA) const {
6416 // Say "no" if there is not exactly one input of a type flang understands.
6417 if (JA.size() != 1 ||
6418 !types::isAcceptedByFlang((*JA.input_begin())->getType()))
6419 return false;
6421 // And say "no" if this is not a kind of action flang understands.
6422 if (!isa<PreprocessJobAction>(JA) && !isa<CompileJobAction>(JA) &&
6423 !isa<BackendJobAction>(JA))
6424 return false;
6426 return true;
6429 bool Driver::ShouldEmitStaticLibrary(const ArgList &Args) const {
6430 // Only emit static library if the flag is set explicitly.
6431 if (Args.hasArg(options::OPT_emit_static_lib))
6432 return true;
6433 return false;
6436 /// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the
6437 /// grouped values as integers. Numbers which are not provided are set to 0.
6439 /// \return True if the entire string was parsed (9.2), or all groups were
6440 /// parsed (10.3.5extrastuff).
6441 bool Driver::GetReleaseVersion(StringRef Str, unsigned &Major, unsigned &Minor,
6442 unsigned &Micro, bool &HadExtra) {
6443 HadExtra = false;
6445 Major = Minor = Micro = 0;
6446 if (Str.empty())
6447 return false;
6449 if (Str.consumeInteger(10, Major))
6450 return false;
6451 if (Str.empty())
6452 return true;
6453 if (Str[0] != '.')
6454 return false;
6456 Str = Str.drop_front(1);
6458 if (Str.consumeInteger(10, Minor))
6459 return false;
6460 if (Str.empty())
6461 return true;
6462 if (Str[0] != '.')
6463 return false;
6464 Str = Str.drop_front(1);
6466 if (Str.consumeInteger(10, Micro))
6467 return false;
6468 if (!Str.empty())
6469 HadExtra = true;
6470 return true;
6473 /// Parse digits from a string \p Str and fulfill \p Digits with
6474 /// the parsed numbers. This method assumes that the max number of
6475 /// digits to look for is equal to Digits.size().
6477 /// \return True if the entire string was parsed and there are
6478 /// no extra characters remaining at the end.
6479 bool Driver::GetReleaseVersion(StringRef Str,
6480 MutableArrayRef<unsigned> Digits) {
6481 if (Str.empty())
6482 return false;
6484 unsigned CurDigit = 0;
6485 while (CurDigit < Digits.size()) {
6486 unsigned Digit;
6487 if (Str.consumeInteger(10, Digit))
6488 return false;
6489 Digits[CurDigit] = Digit;
6490 if (Str.empty())
6491 return true;
6492 if (Str[0] != '.')
6493 return false;
6494 Str = Str.drop_front(1);
6495 CurDigit++;
6498 // More digits than requested, bail out...
6499 return false;
6502 llvm::opt::Visibility
6503 Driver::getOptionVisibilityMask(bool UseDriverMode) const {
6504 if (!UseDriverMode)
6505 return llvm::opt::Visibility(options::ClangOption);
6506 if (IsCLMode())
6507 return llvm::opt::Visibility(options::CLOption);
6508 if (IsDXCMode())
6509 return llvm::opt::Visibility(options::DXCOption);
6510 if (IsFlangMode()) {
6511 return llvm::opt::Visibility(options::FlangOption);
6513 return llvm::opt::Visibility(options::ClangOption);
6516 const char *Driver::getExecutableForDriverMode(DriverMode Mode) {
6517 switch (Mode) {
6518 case GCCMode:
6519 return "clang";
6520 case GXXMode:
6521 return "clang++";
6522 case CPPMode:
6523 return "clang-cpp";
6524 case CLMode:
6525 return "clang-cl";
6526 case FlangMode:
6527 return "flang";
6528 case DXCMode:
6529 return "clang-dxc";
6532 llvm_unreachable("Unhandled Mode");
6535 bool clang::driver::isOptimizationLevelFast(const ArgList &Args) {
6536 return Args.hasFlag(options::OPT_Ofast, options::OPT_O_Group, false);
6539 bool clang::driver::willEmitRemarks(const ArgList &Args) {
6540 // -fsave-optimization-record enables it.
6541 if (Args.hasFlag(options::OPT_fsave_optimization_record,
6542 options::OPT_fno_save_optimization_record, false))
6543 return true;
6545 // -fsave-optimization-record=<format> enables it as well.
6546 if (Args.hasFlag(options::OPT_fsave_optimization_record_EQ,
6547 options::OPT_fno_save_optimization_record, false))
6548 return true;
6550 // -foptimization-record-file alone enables it too.
6551 if (Args.hasFlag(options::OPT_foptimization_record_file_EQ,
6552 options::OPT_fno_save_optimization_record, false))
6553 return true;
6555 // -foptimization-record-passes alone enables it too.
6556 if (Args.hasFlag(options::OPT_foptimization_record_passes_EQ,
6557 options::OPT_fno_save_optimization_record, false))
6558 return true;
6559 return false;
6562 llvm::StringRef clang::driver::getDriverMode(StringRef ProgName,
6563 ArrayRef<const char *> Args) {
6564 static StringRef OptName =
6565 getDriverOptTable().getOption(options::OPT_driver_mode).getPrefixedName();
6566 llvm::StringRef Opt;
6567 for (StringRef Arg : Args) {
6568 if (!Arg.starts_with(OptName))
6569 continue;
6570 Opt = Arg;
6572 if (Opt.empty())
6573 Opt = ToolChain::getTargetAndModeFromProgramName(ProgName).DriverMode;
6574 return Opt.consume_front(OptName) ? Opt : "";
6577 bool driver::IsClangCL(StringRef DriverMode) { return DriverMode.equals("cl"); }
6579 llvm::Error driver::expandResponseFiles(SmallVectorImpl<const char *> &Args,
6580 bool ClangCLMode,
6581 llvm::BumpPtrAllocator &Alloc,
6582 llvm::vfs::FileSystem *FS) {
6583 // Parse response files using the GNU syntax, unless we're in CL mode. There
6584 // are two ways to put clang in CL compatibility mode: ProgName is either
6585 // clang-cl or cl, or --driver-mode=cl is on the command line. The normal
6586 // command line parsing can't happen until after response file parsing, so we
6587 // have to manually search for a --driver-mode=cl argument the hard way.
6588 // Finally, our -cc1 tools don't care which tokenization mode we use because
6589 // response files written by clang will tokenize the same way in either mode.
6590 enum { Default, POSIX, Windows } RSPQuoting = Default;
6591 for (const char *F : Args) {
6592 if (strcmp(F, "--rsp-quoting=posix") == 0)
6593 RSPQuoting = POSIX;
6594 else if (strcmp(F, "--rsp-quoting=windows") == 0)
6595 RSPQuoting = Windows;
6598 // Determines whether we want nullptr markers in Args to indicate response
6599 // files end-of-lines. We only use this for the /LINK driver argument with
6600 // clang-cl.exe on Windows.
6601 bool MarkEOLs = ClangCLMode;
6603 llvm::cl::TokenizerCallback Tokenizer;
6604 if (RSPQuoting == Windows || (RSPQuoting == Default && ClangCLMode))
6605 Tokenizer = &llvm::cl::TokenizeWindowsCommandLine;
6606 else
6607 Tokenizer = &llvm::cl::TokenizeGNUCommandLine;
6609 if (MarkEOLs && Args.size() > 1 && StringRef(Args[1]).starts_with("-cc1"))
6610 MarkEOLs = false;
6612 llvm::cl::ExpansionContext ECtx(Alloc, Tokenizer);
6613 ECtx.setMarkEOLs(MarkEOLs);
6614 if (FS)
6615 ECtx.setVFS(FS);
6617 if (llvm::Error Err = ECtx.expandResponseFiles(Args))
6618 return Err;
6620 // If -cc1 came from a response file, remove the EOL sentinels.
6621 auto FirstArg = llvm::find_if(llvm::drop_begin(Args),
6622 [](const char *A) { return A != nullptr; });
6623 if (FirstArg != Args.end() && StringRef(*FirstArg).starts_with("-cc1")) {
6624 // If -cc1 came from a response file, remove the EOL sentinels.
6625 if (MarkEOLs) {
6626 auto newEnd = std::remove(Args.begin(), Args.end(), nullptr);
6627 Args.resize(newEnd - Args.begin());
6631 return llvm::Error::success();