1 //===--- Driver.cpp - Clang GCC Compatible Driver -------------------------===//
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
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
100 #include <unistd.h> // getpid
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()) {
115 D
.Diag(diag::err_drv_only_one_offload_target_supported
);
118 D
.Diag(diag::err_drv_invalid_or_unsupported_offload_target
) << "";
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
))
138 D
.Diag(diag::err_drv_cuda_offload_only_emit_bc
);
141 D
.Diag(diag::err_drv_invalid_or_unsupported_offload_target
) << TT
->str();
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
);
152 if (TT
->getArch() == llvm::Triple::amdgcn
&&
153 TT
->getVendor() == llvm::Triple::AMD
&&
154 TT
->getOS() == llvm::Triple::AMDHSA
)
156 if (TT
->getArch() == llvm::Triple::spirv64
)
158 D
.Diag(diag::err_drv_invalid_or_unsupported_offload_target
) << TT
->str();
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
);
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.
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
;
223 #if defined(CLANG_CONFIG_FILE_USER_DIR)
226 llvm::sys::fs::expand_tilde(CLANG_CONFIG_FILE_USER_DIR
, P
);
227 UserConfigDir
= static_cast<std::string
>(P
);
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
)
243 .Case("flang", FlangMode
)
244 .Case("dxc", DXCMode
)
245 .Default(std::nullopt
))
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
;
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
,
276 DiagnosticsEngine::Warning
;
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
)) {
291 auto ArgString
= A
->getAsString(Args
);
293 if (getOpts().findNearest(ArgString
, Nearest
, VisibilityMask
) > 1) {
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
;
300 DiagID
= IsCLMode() ? diag::warn_drv_unknown_argument_clang_cl
301 : diag::err_drv_unknown_argument
;
302 Diags
.Report(DiagID
) << ArgString
;
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())
318 // Warn on joined arguments that are similar to a long argument.
319 std::string ArgString
= ArgStrings
[A
->getIndex()];
321 if (getOpts().findExact("-" + ArgString
, Nearest
, VisibilityMask
))
322 Diags
.Report(diag::warn_drv_potentially_misspelled_joined_argument
)
323 << A
->getAsString(Args
) << Nearest
;
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
)) ||
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.
378 FinalPhase
= phases::Link
;
381 *FinalPhaseArg
= PhaseArg
;
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
);
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
) {
408 if (A
->getOption().matches(options::OPT_start_no_unused_arguments
)) {
412 if (A
->getOption().matches(options::OPT_end_no_unused_arguments
)) {
413 IgnoreUnused
= false;
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
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
);
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
));
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));
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
&&
460 DAL
->AddFlagArg(A
, Opts
.getOption(options::OPT_Z_reserved_lib_stdcxx
));
464 // Rewrite unconditionally.
465 if (Value
== "cc_kext") {
466 DAL
->AddFlagArg(A
, Opts
.getOption(options::OPT_Z_reserved_lib_cckext
));
471 // Pick up inputs via the -- option.
472 if (A
->getOption().matches(options::OPT__DASH_DASH
)) {
474 for (StringRef Val
: A
->getValues())
475 DAL
->append(MakeInputArg(*DAL
, Opts
, Val
, false));
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
),
497 DAL
->getLastArg(options::OPT_mlinker_version_EQ
)->claim();
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
,
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
,
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
)
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();
569 D
.Diag(diag::err_drv_invalid_object_mode
) << ObjectMode
;
572 if (AT
!= llvm::Triple::UnknownArch
&& AT
!= Target
.getArch())
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
);
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
);
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()) {
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"
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
);
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))
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();
707 // Parse the LTO options.
708 void Driver::setLTOMode(const llvm::opt::ArgList
&Args
) {
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
);
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
) {
743 Diag(diag::err_drv_unsupported_option_argument
)
744 << A
->getSpelling() << A
->getValue();
746 // FIXME: We could use a nicer diagnostic here.
747 Diag(diag::err_drv_unsupported_opt
) << "-fopenmp";
753 void Driver::CreateOffloadingDeviceToolChains(Compilation
&C
,
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.
762 llvm::any_of(Inputs
, [](std::pair
<types::ID
, const llvm::opt::Arg
*> &I
) {
763 return types::isCuda(I
.first
);
767 [](std::pair
<types::ID
, const llvm::opt::Arg
*> &I
) {
768 return types::isHIP(I
.first
);
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
);
777 const ToolChain
*HostTC
= C
.getSingleOffloadToolChain
<Action::OFK_Host
>();
778 const llvm::Triple
&HostTriple
= HostTC
->getTriple();
779 auto OFK
= Action::OFK_Cuda
;
781 getNVIDIAOffloadTargetTriple(*this, C
.getInputArgs(), HostTriple
);
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()];
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
);
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";
805 const ToolChain
*HostTC
= C
.getSingleOffloadToolChain
<Action::OFK_Host
>();
806 auto OFK
= Action::OFK_HIP
;
807 auto HIPTriple
= getHIPOffloadTargetTriple(*this, C
.getInputArgs());
810 auto *HIPTC
= &getOffloadingDeviceToolChain(C
.getInputArgs(), *HIPTriple
,
812 assert(HIPTC
&& "Could not create offloading device tool chain.");
813 C
.addOffloadDeviceToolChain(HIPTC
, OFK
);
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
);
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());
850 for (StringRef T
: OpenMPTargets
->getValues())
851 OpenMPTriples
.insert(T
);
852 } else if (C
.getInputArgs().hasArg(options::OPT_offload_arch_EQ
) &&
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
;
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))
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))
878 if (!AMDTriple
&& !NVPTXTriple
) {
879 for (StringRef Arch
:
880 getOffloadArchs(C
, C
.getArgs(), Action::OFK_OpenMP
, nullptr, true))
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
);
893 Diag(clang::diag::err_drv_failed_to_deduce_target_from_arch
) << Arch
;
898 // If the set is empty then we failed to find a native architecture.
900 Diag(clang::diag::err_drv_failed_to_deduce_target_from_arch
)
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
;
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
;
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.");
937 ToolChains
[TT
.str() + "/" + HostTC
->getTriple().normalize()];
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());
946 assert(DeviceTC
&& "Device toolchain not defined.");
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
);
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
),
975 Copy
->getValues() = Opt
->getValues();
976 if (Opt
->isClaimed())
978 Copy
->setOwnsValues(Opt
->getOwnsValues());
979 Opt
->setOwnsValues(false);
983 bool Driver::readConfigFile(StringRef FileName
,
984 llvm::cl::ExpansionContext
&ExpCtx
) {
985 // Try opening the given file.
986 auto Status
= getVFS().status(FileName
);
988 Diag(diag::err_drv_cannot_open_config_file
)
989 << FileName
<< Status
.getError().message();
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";
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
));
1006 // Read options from config file.
1007 llvm::SmallString
<128> CfgFileName(FileName
);
1008 llvm::sys::path::native(CfgFileName
);
1010 std::unique_ptr
<InputArgList
> NewOptions
= std::make_unique
<InputArgList
>(
1011 ParseArgStrings(NewCfgArgs
, /*UseDriverMode=*/true, ContainErrors
));
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
)
1021 CfgOptions
= std::move(NewOptions
);
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();
1028 appendOneArg(*CfgOptions
, Opt
, BaseArg
);
1031 ConfigFiles
.push_back(std::string(CfgFileName
));
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.
1042 if (CLOptions
->hasArg(options::OPT_config_system_dir_EQ
)) {
1043 SmallString
<128> CfgDir
;
1045 CLOptions
->getLastArgValue(options::OPT_config_system_dir_EQ
));
1046 if (CfgDir
.empty() || getVFS().makeAbsolute(CfgDir
))
1047 SystemConfigDir
.clear();
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();
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
))
1070 // Then load configuration files specified explicitly.
1071 SmallString
<128> CfgFilePath
;
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";
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
;
1094 // Try to read the config file, return on error.
1095 if (readConfigFile(CfgFilePath
, ExpCtx
))
1100 // No error occurred.
1104 bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext
&ExpCtx
) {
1105 // Disable default config if CLANG_NO_DEFAULT_CONFIG is set to a non-empty
1107 if (const char *NoConfigEnv
= ::getenv("CLANG_NO_DEFAULT_CONFIG")) {
1111 if (CLOptions
&& CLOptions
->hasArg(options::OPT_no_default_config
))
1114 std::string RealMode
= getExecutableForDriverMode(Mode
);
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
1120 if (!ClangNameParts
.TargetPrefix
.empty() &&
1121 computeTargetTriple(*this, "/invalid/", *CLOptions
).str() ==
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
))
1167 } else if (TryModeSuffix
) {
1168 CfgFileName
= ClangNameParts
.ModeSuffix
+ ".cfg";
1169 if (ExpCtx
.findConfigFile(CfgFileName
, CfgFilePath
) &&
1170 readConfigFile(CfgFilePath
, ExpCtx
))
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.
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.
1201 CLOptions
= std::make_unique
<InputArgList
>(
1202 ParseArgStrings(ArgList
.slice(1), /*UseDriverMode=*/true, ContainsError
));
1204 // Try parsing configuration file.
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
));
1214 for (auto *Opt
: *CLOptions
) {
1215 if (Opt
->getOption().matches(options::OPT_config
))
1217 const Arg
*BaseArg
= &Opt
->getBaseArg();
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
)) {
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,
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
);
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
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.
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();
1300 toolchains::HLSLToolChain::parseTargetProfile(TargetProfile
))
1301 TargetTriple
= *Triple
;
1303 Diag(diag::err_drv_invalid_directx_shader_module
) << TargetProfile
;
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();
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
)) {
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
;
1359 Offload
= OffloadHostDevice
;
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
)
1374 Diags
.Report(diag::err_drv_invalid_value
) << A
->getAsString(Args
)
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
);
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
;
1404 StringRef ArgName
= A
->getValue();
1405 unsigned Kind
= llvm::StringSwitch
<unsigned>(ArgName
)
1406 .Case("user", HeaderMode_User
)
1407 .Case("system", HeaderMode_System
)
1410 Diags
.Report(diag::err_drv_invalid_value
)
1411 << A
->getAsString(Args
) << ArgName
;
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
)
1448 << (TC
.getTriple().getArchName().str() + "-none-eabi");
1451 case llvm::Triple::aarch64
:
1452 case llvm::Triple::aarch64_be
:
1453 case llvm::Triple::aarch64_32
:
1454 if (TC
.getTriple().getEnvironmentName().startswith("eabi")) {
1455 Diag(diag::warn_target_unrecognized_env
)
1457 << (TC
.getTriple().getArchName().str() + "-none-elf");
1465 // The compilation takes ownership of Args.
1466 Compilation
*C
= new Compilation(*this, TC
, UArgs
.release(), TranslatedArgs
,
1469 if (!HandleImmediateArgs(*C
))
1472 // Construct the list of 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
);
1484 BuildActions(*C
, C
->getArgs(), Inputs
, C
->getActions());
1486 if (CCCPrintPhases
) {
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())
1504 A
->render(Args
, ASL
);
1507 for (auto I
= ASL
.begin(), E
= ASL
.end(); I
!= E
; ++I
) {
1508 if (I
!= ASL
.begin())
1510 llvm::sys::printArg(OS
, *I
, true);
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
.startswith("/var/root"))
1527 path::append(CrashDiagDir
, "Library/Logs/DiagnosticReports");
1535 fs::file_status FileStatus
;
1536 TimePoint
<> LastAccessTime
;
1537 SmallString
<128> CrashFilePath
;
1538 // Lookup the .crash files and get the one generated by a subprocess spawned
1539 // by this driver invocation.
1540 for (fs::directory_iterator
File(CrashDiagDir
, EC
), FileEnd
;
1541 File
!= FileEnd
&& !EC
; File
.increment(EC
)) {
1542 StringRef FileName
= path::filename(File
->path());
1543 if (!FileName
.startswith(Name
))
1545 if (fs::status(File
->path(), FileStatus
))
1547 llvm::ErrorOr
<std::unique_ptr
<llvm::MemoryBuffer
>> CrashFile
=
1548 llvm::MemoryBuffer::getFile(File
->path());
1551 // The first line should start with "Process:", otherwise this isn't a real
1553 StringRef Data
= CrashFile
.get()->getBuffer();
1554 if (!Data
.startswith("Process:"))
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
)
1560 size_t LineEnd
= Data
.find_first_of("\n", ParentProcPos
);
1561 if (LineEnd
== StringRef::npos
)
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
] == '[')
1568 if (ParentProcess
[i
] == ']')
1571 // Extract the parent process PID from the .crash file and check whether
1572 // it matches this driver invocation pid.
1574 if (OpenBracket
< 0 || CloseBracket
< 0 ||
1575 ParentProcess
.slice(OpenBracket
+ 1, CloseBracket
)
1576 .getAsInteger(10, CrashPID
) || CrashPID
!= PID
) {
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
);
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
))
1619 if (Arg
*A
= C
.getArgs().getLastArg(options::OPT_fcrash_diagnostics_EQ
)) {
1620 Level
= llvm::StringSwitch
<unsigned>(A
->getValue())
1622 .Case("compiler", 1)
1629 // Don't try to generate diagnostics for dsymutil jobs.
1630 if (FailingCommand
.getCreator().isDsymutilJob())
1634 ArgStringList SavedTemps
;
1635 if (FailingCommand
.getCreator().isLinkJob()) {
1636 C
.getDefaultToolChain().GetLinkerPath(&IsLLD
);
1637 if (!IsLLD
|| Level
< 2)
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
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.
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()
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********************";
1683 Report
->TemporaryFiles
.push_back(TmpName
);
1687 // Construct the list of 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
) {
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.";
1706 it
= Inputs
.erase(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.";
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.";
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
);
1742 BuildActions(C
, C
.getArgs(), Inputs
, C
.getActions());
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).";
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).";
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).";
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
;
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
).endswith(".cache")) {
1784 // In some cases (modules) we'll dump extra data to help with reproducing
1785 // the crash into a directory next to the output.
1786 VFS
= llvm::sys::path::filename(TempFile
);
1787 llvm::sys::path::append(VFS
, "vfs", "vfs.yaml");
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");
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
);
1804 Diag(clang::diag::note_drv_command_failed_diag_msg
)
1805 << "Error generating run script: " << Script
<< " " << EC
.message();
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
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
1848 if (Cmd
.getResponseFileSupport().ResponseKind
==
1849 ResponseFileSupport::RF_None
||
1850 llvm::sys::commandLineFitsWithinSystemLimits(Cmd
.getExecutable(),
1851 Cmd
.getArguments()))
1854 std::string TmpName
= GetTemporaryPath("response", "txt");
1855 Cmd
.setResponseFile(C
.addTempFile(C
.getArgs().MakeArgString(TmpName
)));
1858 int Driver::ExecuteCompilation(
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())
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())
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())
1894 // Otherwise, remove result files and print extra information about abnormal
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.
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
) {
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.
1930 Diag(clang::diag::err_drv_command_signalled
)
1931 << FailingTool
.getShortName();
1933 Diag(clang::diag::err_drv_command_failed
)
1934 << FailingTool
.getShortName() << CommandRes
;
1940 void Driver::PrintHelp(bool ShowHidden
) const {
1941 llvm::opt::Visibility VisibilityMask
= getOptionVisibilityMask();
1943 // TODO: We're overriding the mask for flang here to keep this NFC for the
1944 // option refactoring, but what we really need to do is annotate the flags
1947 VisibilityMask
= llvm::opt::Visibility(options::FlangOption
);
1949 std::string Usage
= llvm::formatv("{0} [options] file...", Name
).str();
1950 getOpts().printHelp(llvm::outs(), Usage
.c_str(), DriverTitle
.c_str(),
1951 ShowHidden
, /*ShowAllAliases=*/false,
1955 void Driver::PrintVersion(const Compilation
&C
, raw_ostream
&OS
) const {
1956 if (IsFlangMode()) {
1957 OS
<< getClangToolFullVersion("flang-new") << '\n';
1959 // FIXME: The following handlers should use a callback mechanism, we don't
1960 // know what the client would like to do.
1961 OS
<< getClangFullVersion() << '\n';
1963 const ToolChain
&TC
= C
.getDefaultToolChain();
1964 OS
<< "Target: " << TC
.getTripleString() << '\n';
1966 // Print the threading model.
1967 if (Arg
*A
= C
.getArgs().getLastArg(options::OPT_mthread_model
)) {
1968 // Don't print if the ToolChain would have barfed on it already
1969 if (TC
.isThreadModelSupported(A
->getValue()))
1970 OS
<< "Thread model: " << A
->getValue();
1972 OS
<< "Thread model: " << TC
.getThreadModel();
1975 // Print out the install directory.
1976 OS
<< "InstalledDir: " << InstalledDir
<< '\n';
1978 // If configuration files were used, print their paths.
1979 for (auto ConfigFile
: ConfigFiles
)
1980 OS
<< "Configuration file: " << ConfigFile
<< '\n';
1983 /// PrintDiagnosticCategories - Implement the --print-diagnostic-categories
1985 static void PrintDiagnosticCategories(raw_ostream
&OS
) {
1986 // Skip the empty category.
1987 for (unsigned i
= 1, max
= DiagnosticIDs::getNumberOfCategories(); i
!= max
;
1989 OS
<< i
<< ',' << DiagnosticIDs::getCategoryNameFromID(i
) << '\n';
1992 void Driver::HandleAutocompletions(StringRef PassedFlags
) const {
1993 if (PassedFlags
== "")
1995 // Print out all options that start with a given argument. This is used for
1996 // shell autocompletion.
1997 std::vector
<std::string
> SuggestedCompletions
;
1998 std::vector
<std::string
> Flags
;
2000 llvm::opt::Visibility
VisibilityMask(options::ClangOption
);
2002 // Make sure that Flang-only options don't pollute the Clang output
2003 // TODO: Make sure that Clang-only options don't pollute Flang output
2005 VisibilityMask
= llvm::opt::Visibility(options::FlangOption
);
2007 // Distinguish "--autocomplete=-someflag" and "--autocomplete=-someflag,"
2008 // because the latter indicates that the user put space before pushing tab
2009 // which should end up in a file completion.
2010 const bool HasSpace
= PassedFlags
.endswith(",");
2012 // Parse PassedFlags by "," as all the command-line flags are passed to this
2013 // function separated by ","
2014 StringRef TargetFlags
= PassedFlags
;
2015 while (TargetFlags
!= "") {
2017 std::tie(CurFlag
, TargetFlags
) = TargetFlags
.split(",");
2018 Flags
.push_back(std::string(CurFlag
));
2021 // We want to show cc1-only options only when clang is invoked with -cc1 or
2023 if (llvm::is_contained(Flags
, "-Xclang") || llvm::is_contained(Flags
, "-cc1"))
2024 VisibilityMask
= llvm::opt::Visibility(options::CC1Option
);
2026 const llvm::opt::OptTable
&Opts
= getOpts();
2028 Cur
= Flags
.at(Flags
.size() - 1);
2030 if (Flags
.size() >= 2) {
2031 Prev
= Flags
.at(Flags
.size() - 2);
2032 SuggestedCompletions
= Opts
.suggestValueCompletions(Prev
, Cur
);
2035 if (SuggestedCompletions
.empty())
2036 SuggestedCompletions
= Opts
.suggestValueCompletions(Cur
, "");
2038 // If Flags were empty, it means the user typed `clang [tab]` where we should
2039 // list all possible flags. If there was no value completion and the user
2040 // pressed tab after a space, we should fall back to a file completion.
2041 // We're printing a newline to be consistent with what we print at the end of
2043 if (SuggestedCompletions
.empty() && HasSpace
&& !Flags
.empty()) {
2044 llvm::outs() << '\n';
2048 // When flag ends with '=' and there was no value completion, return empty
2049 // string and fall back to the file autocompletion.
2050 if (SuggestedCompletions
.empty() && !Cur
.endswith("=")) {
2051 // If the flag is in the form of "--autocomplete=-foo",
2052 // we were requested to print out all option names that start with "-foo".
2053 // For example, "--autocomplete=-fsyn" is expanded to "-fsyntax-only".
2054 SuggestedCompletions
= Opts
.findByPrefix(
2055 Cur
, VisibilityMask
,
2056 /*DisableFlags=*/options::Unsupported
| options::Ignored
);
2058 // We have to query the -W flags manually as they're not in the OptTable.
2059 // TODO: Find a good way to add them to OptTable instead and them remove
2061 for (StringRef S
: DiagnosticIDs::getDiagnosticFlags())
2062 if (S
.startswith(Cur
))
2063 SuggestedCompletions
.push_back(std::string(S
));
2066 // Sort the autocomplete candidates so that shells print them out in a
2067 // deterministic order. We could sort in any way, but we chose
2068 // case-insensitive sorting for consistency with the -help option
2069 // which prints out options in the case-insensitive alphabetical order.
2070 llvm::sort(SuggestedCompletions
, [](StringRef A
, StringRef B
) {
2071 if (int X
= A
.compare_insensitive(B
))
2073 return A
.compare(B
) > 0;
2076 llvm::outs() << llvm::join(SuggestedCompletions
, "\n") << '\n';
2079 bool Driver::HandleImmediateArgs(const Compilation
&C
) {
2080 // The order these options are handled in gcc is all over the place, but we
2081 // don't expect inconsistencies w.r.t. that to matter in practice.
2083 if (C
.getArgs().hasArg(options::OPT_dumpmachine
)) {
2084 llvm::outs() << C
.getDefaultToolChain().getTripleString() << '\n';
2088 if (C
.getArgs().hasArg(options::OPT_dumpversion
)) {
2089 // Since -dumpversion is only implemented for pedantic GCC compatibility, we
2090 // return an answer which matches our definition of __VERSION__.
2091 llvm::outs() << CLANG_VERSION_STRING
<< "\n";
2095 if (C
.getArgs().hasArg(options::OPT__print_diagnostic_categories
)) {
2096 PrintDiagnosticCategories(llvm::outs());
2100 if (C
.getArgs().hasArg(options::OPT_help
) ||
2101 C
.getArgs().hasArg(options::OPT__help_hidden
)) {
2102 PrintHelp(C
.getArgs().hasArg(options::OPT__help_hidden
));
2106 if (C
.getArgs().hasArg(options::OPT__version
)) {
2107 // Follow gcc behavior and use stdout for --version and stderr for -v.
2108 PrintVersion(C
, llvm::outs());
2112 if (C
.getArgs().hasArg(options::OPT_v
) ||
2113 C
.getArgs().hasArg(options::OPT__HASH_HASH_HASH
) ||
2114 C
.getArgs().hasArg(options::OPT_print_supported_cpus
) ||
2115 C
.getArgs().hasArg(options::OPT_print_supported_extensions
)) {
2116 PrintVersion(C
, llvm::errs());
2117 SuppressMissingInputWarning
= true;
2120 if (C
.getArgs().hasArg(options::OPT_v
)) {
2121 if (!SystemConfigDir
.empty())
2122 llvm::errs() << "System configuration file directory: "
2123 << SystemConfigDir
<< "\n";
2124 if (!UserConfigDir
.empty())
2125 llvm::errs() << "User configuration file directory: "
2126 << UserConfigDir
<< "\n";
2129 const ToolChain
&TC
= C
.getDefaultToolChain();
2131 if (C
.getArgs().hasArg(options::OPT_v
))
2132 TC
.printVerboseInfo(llvm::errs());
2134 if (C
.getArgs().hasArg(options::OPT_print_resource_dir
)) {
2135 llvm::outs() << ResourceDir
<< '\n';
2139 if (C
.getArgs().hasArg(options::OPT_print_search_dirs
)) {
2140 llvm::outs() << "programs: =";
2141 bool separator
= false;
2142 // Print -B and COMPILER_PATH.
2143 for (const std::string
&Path
: PrefixDirs
) {
2145 llvm::outs() << llvm::sys::EnvPathSeparator
;
2146 llvm::outs() << Path
;
2149 for (const std::string
&Path
: TC
.getProgramPaths()) {
2151 llvm::outs() << llvm::sys::EnvPathSeparator
;
2152 llvm::outs() << Path
;
2155 llvm::outs() << "\n";
2156 llvm::outs() << "libraries: =" << ResourceDir
;
2158 StringRef sysroot
= C
.getSysRoot();
2160 for (const std::string
&Path
: TC
.getFilePaths()) {
2161 // Always print a separator. ResourceDir was the first item shown.
2162 llvm::outs() << llvm::sys::EnvPathSeparator
;
2163 // Interpretation of leading '=' is needed only for NetBSD.
2165 llvm::outs() << sysroot
<< Path
.substr(1);
2167 llvm::outs() << Path
;
2169 llvm::outs() << "\n";
2173 if (C
.getArgs().hasArg(options::OPT_print_runtime_dir
)) {
2174 if (std::optional
<std::string
> RuntimePath
= TC
.getRuntimePath())
2175 llvm::outs() << *RuntimePath
<< '\n';
2177 llvm::outs() << TC
.getCompilerRTPath() << '\n';
2181 if (C
.getArgs().hasArg(options::OPT_print_diagnostic_options
)) {
2182 std::vector
<std::string
> Flags
= DiagnosticIDs::getDiagnosticFlags();
2183 for (std::size_t I
= 0; I
!= Flags
.size(); I
+= 2)
2184 llvm::outs() << " " << Flags
[I
] << "\n " << Flags
[I
+ 1] << "\n\n";
2188 // FIXME: The following handlers should use a callback mechanism, we don't
2189 // know what the client would like to do.
2190 if (Arg
*A
= C
.getArgs().getLastArg(options::OPT_print_file_name_EQ
)) {
2191 llvm::outs() << GetFilePath(A
->getValue(), TC
) << "\n";
2195 if (Arg
*A
= C
.getArgs().getLastArg(options::OPT_print_prog_name_EQ
)) {
2196 StringRef ProgName
= A
->getValue();
2198 // Null program name cannot have a path.
2199 if (! ProgName
.empty())
2200 llvm::outs() << GetProgramPath(ProgName
, TC
);
2202 llvm::outs() << "\n";
2206 if (Arg
*A
= C
.getArgs().getLastArg(options::OPT_autocomplete
)) {
2207 StringRef PassedFlags
= A
->getValue();
2208 HandleAutocompletions(PassedFlags
);
2212 if (C
.getArgs().hasArg(options::OPT_print_libgcc_file_name
)) {
2213 ToolChain::RuntimeLibType RLT
= TC
.GetRuntimeLibType(C
.getArgs());
2214 const llvm::Triple
Triple(TC
.ComputeEffectiveClangTriple(C
.getArgs()));
2215 RegisterEffectiveTriple
TripleRAII(TC
, Triple
);
2217 case ToolChain::RLT_CompilerRT
:
2218 llvm::outs() << TC
.getCompilerRT(C
.getArgs(), "builtins") << "\n";
2220 case ToolChain::RLT_Libgcc
:
2221 llvm::outs() << GetFilePath("libgcc.a", TC
) << "\n";
2227 if (C
.getArgs().hasArg(options::OPT_print_multi_lib
)) {
2228 for (const Multilib
&Multilib
: TC
.getMultilibs())
2229 llvm::outs() << Multilib
<< "\n";
2233 if (C
.getArgs().hasArg(options::OPT_print_multi_flags
)) {
2234 Multilib::flags_list ArgFlags
= TC
.getMultilibFlags(C
.getArgs());
2235 llvm::StringSet
<> ExpandedFlags
= TC
.getMultilibs().expandFlags(ArgFlags
);
2236 std::set
<llvm::StringRef
> SortedFlags
;
2237 for (const auto &FlagEntry
: ExpandedFlags
)
2238 SortedFlags
.insert(FlagEntry
.getKey());
2239 for (auto Flag
: SortedFlags
)
2240 llvm::outs() << Flag
<< '\n';
2244 if (C
.getArgs().hasArg(options::OPT_print_multi_directory
)) {
2245 for (const Multilib
&Multilib
: TC
.getSelectedMultilibs()) {
2246 if (Multilib
.gccSuffix().empty())
2247 llvm::outs() << ".\n";
2249 StringRef
Suffix(Multilib
.gccSuffix());
2250 assert(Suffix
.front() == '/');
2251 llvm::outs() << Suffix
.substr(1) << "\n";
2257 if (C
.getArgs().hasArg(options::OPT_print_target_triple
)) {
2258 llvm::outs() << TC
.getTripleString() << "\n";
2262 if (C
.getArgs().hasArg(options::OPT_print_effective_triple
)) {
2263 const llvm::Triple
Triple(TC
.ComputeEffectiveClangTriple(C
.getArgs()));
2264 llvm::outs() << Triple
.getTriple() << "\n";
2268 if (C
.getArgs().hasArg(options::OPT_print_targets
)) {
2269 llvm::TargetRegistry::printRegisteredTargetsForVersion(llvm::outs());
2282 // Display an action graph human-readably. Action A is the "sink" node
2283 // and latest-occuring action. Traversal is in pre-order, visiting the
2284 // inputs to each action before printing the action itself.
2285 static unsigned PrintActions1(const Compilation
&C
, Action
*A
,
2286 std::map
<Action
*, unsigned> &Ids
,
2287 Twine Indent
= {}, int Kind
= TopLevelAction
) {
2288 if (Ids
.count(A
)) // A was already visited.
2292 llvm::raw_string_ostream
os(str
);
2294 auto getSibIndent
= [](int K
) -> Twine
{
2295 return (K
== HeadSibAction
) ? " " : (K
== OtherSibAction
) ? "| " : "";
2298 Twine SibIndent
= Indent
+ getSibIndent(Kind
);
2299 int SibKind
= HeadSibAction
;
2300 os
<< Action::getClassName(A
->getKind()) << ", ";
2301 if (InputAction
*IA
= dyn_cast
<InputAction
>(A
)) {
2302 os
<< "\"" << IA
->getInputArg().getValue() << "\"";
2303 } else if (BindArchAction
*BIA
= dyn_cast
<BindArchAction
>(A
)) {
2304 os
<< '"' << BIA
->getArchName() << '"' << ", {"
2305 << PrintActions1(C
, *BIA
->input_begin(), Ids
, SibIndent
, SibKind
) << "}";
2306 } else if (OffloadAction
*OA
= dyn_cast
<OffloadAction
>(A
)) {
2307 bool IsFirst
= true;
2308 OA
->doOnEachDependence(
2309 [&](Action
*A
, const ToolChain
*TC
, const char *BoundArch
) {
2310 assert(TC
&& "Unknown host toolchain");
2311 // E.g. for two CUDA device dependences whose bound arch is sm_20 and
2312 // sm_35 this will generate:
2313 // "cuda-device" (nvptx64-nvidia-cuda:sm_20) {#ID}, "cuda-device"
2314 // (nvptx64-nvidia-cuda:sm_35) {#ID}
2318 os
<< A
->getOffloadingKindPrefix();
2320 os
<< TC
->getTriple().normalize();
2322 os
<< ":" << BoundArch
;
2325 os
<< " {" << PrintActions1(C
, A
, Ids
, SibIndent
, SibKind
) << "}";
2327 SibKind
= OtherSibAction
;
2330 const ActionList
*AL
= &A
->getInputs();
2333 const char *Prefix
= "{";
2334 for (Action
*PreRequisite
: *AL
) {
2335 os
<< Prefix
<< PrintActions1(C
, PreRequisite
, Ids
, SibIndent
, SibKind
);
2337 SibKind
= OtherSibAction
;
2344 // Append offload info for all options other than the offloading action
2345 // itself (e.g. (cuda-device, sm_20) or (cuda-host)).
2346 std::string offload_str
;
2347 llvm::raw_string_ostream
offload_os(offload_str
);
2348 if (!isa
<OffloadAction
>(A
)) {
2349 auto S
= A
->getOffloadingKindPrefix();
2351 offload_os
<< ", (" << S
;
2352 if (A
->getOffloadingArch())
2353 offload_os
<< ", " << A
->getOffloadingArch();
2358 auto getSelfIndent
= [](int K
) -> Twine
{
2359 return (K
== HeadSibAction
) ? "+- " : (K
== OtherSibAction
) ? "|- " : "";
2362 unsigned Id
= Ids
.size();
2364 llvm::errs() << Indent
+ getSelfIndent(Kind
) << Id
<< ": " << os
.str() << ", "
2365 << types::getTypeName(A
->getType()) << offload_os
.str() << "\n";
2370 // Print the action graphs in a compilation C.
2371 // For example "clang -c file1.c file2.c" is composed of two subgraphs.
2372 void Driver::PrintActions(const Compilation
&C
) const {
2373 std::map
<Action
*, unsigned> Ids
;
2374 for (Action
*A
: C
.getActions())
2375 PrintActions1(C
, A
, Ids
);
2378 /// Check whether the given input tree contains any compilation or
2379 /// assembly actions.
2380 static bool ContainsCompileOrAssembleAction(const Action
*A
) {
2381 if (isa
<CompileJobAction
>(A
) || isa
<BackendJobAction
>(A
) ||
2382 isa
<AssembleJobAction
>(A
))
2385 return llvm::any_of(A
->inputs(), ContainsCompileOrAssembleAction
);
2388 void Driver::BuildUniversalActions(Compilation
&C
, const ToolChain
&TC
,
2389 const InputList
&BAInputs
) const {
2390 DerivedArgList
&Args
= C
.getArgs();
2391 ActionList
&Actions
= C
.getActions();
2392 llvm::PrettyStackTraceString
CrashInfo("Building universal build actions");
2393 // Collect the list of architectures. Duplicates are allowed, but should only
2394 // be handled once (in the order seen).
2395 llvm::StringSet
<> ArchNames
;
2396 SmallVector
<const char *, 4> Archs
;
2397 for (Arg
*A
: Args
) {
2398 if (A
->getOption().matches(options::OPT_arch
)) {
2399 // Validate the option here; we don't save the type here because its
2400 // particular spelling may participate in other driver choices.
2401 llvm::Triple::ArchType Arch
=
2402 tools::darwin::getArchTypeForMachOArchName(A
->getValue());
2403 if (Arch
== llvm::Triple::UnknownArch
) {
2404 Diag(clang::diag::err_drv_invalid_arch_name
) << A
->getAsString(Args
);
2409 if (ArchNames
.insert(A
->getValue()).second
)
2410 Archs
.push_back(A
->getValue());
2414 // When there is no explicit arch for this platform, make sure we still bind
2415 // the architecture (to the default) so that -Xarch_ is handled correctly.
2417 Archs
.push_back(Args
.MakeArgString(TC
.getDefaultUniversalArchName()));
2419 ActionList SingleActions
;
2420 BuildActions(C
, Args
, BAInputs
, SingleActions
);
2422 // Add in arch bindings for every top level action, as well as lipo and
2423 // dsymutil steps if needed.
2424 for (Action
* Act
: SingleActions
) {
2425 // Make sure we can lipo this kind of output. If not (and it is an actual
2426 // output) then we disallow, since we can't create an output file with the
2427 // right name without overwriting it. We could remove this oddity by just
2428 // changing the output names to include the arch, which would also fix
2429 // -save-temps. Compatibility wins for now.
2431 if (Archs
.size() > 1 && !types::canLipoType(Act
->getType()))
2432 Diag(clang::diag::err_drv_invalid_output_with_multiple_archs
)
2433 << types::getTypeName(Act
->getType());
2436 for (unsigned i
= 0, e
= Archs
.size(); i
!= e
; ++i
)
2437 Inputs
.push_back(C
.MakeAction
<BindArchAction
>(Act
, Archs
[i
]));
2439 // Lipo if necessary, we do it this way because we need to set the arch flag
2440 // so that -Xarch_ gets overwritten.
2441 if (Inputs
.size() == 1 || Act
->getType() == types::TY_Nothing
)
2442 Actions
.append(Inputs
.begin(), Inputs
.end());
2444 Actions
.push_back(C
.MakeAction
<LipoJobAction
>(Inputs
, Act
->getType()));
2446 // Handle debug info queries.
2447 Arg
*A
= Args
.getLastArg(options::OPT_g_Group
);
2448 bool enablesDebugInfo
= A
&& !A
->getOption().matches(options::OPT_g0
) &&
2449 !A
->getOption().matches(options::OPT_gstabs
);
2450 if ((enablesDebugInfo
|| willEmitRemarks(Args
)) &&
2451 ContainsCompileOrAssembleAction(Actions
.back())) {
2453 // Add a 'dsymutil' step if necessary, when debug info is enabled and we
2454 // have a compile input. We need to run 'dsymutil' ourselves in such cases
2455 // because the debug info will refer to a temporary object file which
2456 // will be removed at the end of the compilation process.
2457 if (Act
->getType() == types::TY_Image
) {
2459 Inputs
.push_back(Actions
.back());
2462 C
.MakeAction
<DsymutilJobAction
>(Inputs
, types::TY_dSYM
));
2465 // Verify the debug info output.
2466 if (Args
.hasArg(options::OPT_verify_debug_info
)) {
2467 Action
* LastAction
= Actions
.back();
2469 Actions
.push_back(C
.MakeAction
<VerifyDebugInfoJobAction
>(
2470 LastAction
, types::TY_Nothing
));
2476 bool Driver::DiagnoseInputExistence(const DerivedArgList
&Args
, StringRef Value
,
2477 types::ID Ty
, bool TypoCorrect
) const {
2478 if (!getCheckInputsExist())
2481 // stdin always exists.
2485 // If it's a header to be found in the system or user search path, then defer
2486 // complaints about its absence until those searches can be done. When we
2487 // are definitely processing headers for C++20 header units, extend this to
2488 // allow the user to put "-fmodule-header -xc++-header vector" for example.
2489 if (Ty
== types::TY_CXXSHeader
|| Ty
== types::TY_CXXUHeader
||
2490 (ModulesModeCXX20
&& Ty
== types::TY_CXXHeader
))
2493 if (getVFS().exists(Value
))
2497 // Check if the filename is a typo for an option flag. OptTable thinks
2498 // that all args that are not known options and that start with / are
2499 // filenames, but e.g. `/diagnostic:caret` is more likely a typo for
2500 // the option `/diagnostics:caret` than a reference to a file in the root
2502 std::string Nearest
;
2503 if (getOpts().findNearest(Value
, Nearest
, getOptionVisibilityMask()) <= 1) {
2504 Diag(clang::diag::err_drv_no_such_file_with_suggestion
)
2505 << Value
<< Nearest
;
2510 // In CL mode, don't error on apparently non-existent linker inputs, because
2511 // they can be influenced by linker flags the clang driver might not
2514 // - `clang-cl main.cc ole32.lib` in a non-MSVC shell will make the driver
2515 // module look for an MSVC installation in the registry. (We could ask
2516 // the MSVCToolChain object if it can find `ole32.lib`, but the logic to
2517 // look in the registry might move into lld-link in the future so that
2518 // lld-link invocations in non-MSVC shells just work too.)
2519 // - `clang-cl ... /link ...` can pass arbitrary flags to the linker,
2520 // including /libpath:, which is used to find .lib and .obj files.
2521 // So do not diagnose this on the driver level. Rely on the linker diagnosing
2522 // it. (If we don't end up invoking the linker, this means we'll emit a
2523 // "'linker' input unused [-Wunused-command-line-argument]" warning instead
2526 // Only do this skip after the typo correction step above. `/Brepo` is treated
2527 // as TY_Object, but it's clearly a typo for `/Brepro`. It seems fine to emit
2528 // an error if we have a flag that's within an edit distance of 1 from a
2529 // flag. (Users can use `-Wl,` or `/linker` to launder the flag past the
2530 // driver in the unlikely case they run into this.)
2532 // Don't do this for inputs that start with a '/', else we'd pass options
2533 // like /libpath: through to the linker silently.
2535 // Emitting an error for linker inputs can also cause incorrect diagnostics
2536 // with the gcc driver. The command
2537 // clang -fuse-ld=lld -Wl,--chroot,some/dir /file.o
2538 // will make lld look for some/dir/file.o, while we will diagnose here that
2539 // `/file.o` does not exist. However, configure scripts check if
2540 // `clang /GR-` compiles without error to see if the compiler is cl.exe,
2541 // so we can't downgrade diagnostics for `/GR-` from an error to a warning
2542 // in cc mode. (We can in cl mode because cl.exe itself only warns on
2544 if (IsCLMode() && Ty
== types::TY_Object
&& !Value
.startswith("/"))
2547 Diag(clang::diag::err_drv_no_such_file
) << Value
;
2551 // Get the C++20 Header Unit type corresponding to the input type.
2552 static types::ID
CXXHeaderUnitType(ModuleHeaderMode HM
) {
2554 case HeaderMode_User
:
2555 return types::TY_CXXUHeader
;
2556 case HeaderMode_System
:
2557 return types::TY_CXXSHeader
;
2558 case HeaderMode_Default
:
2560 case HeaderMode_None
:
2561 llvm_unreachable("should not be called in this case");
2563 return types::TY_CXXHUHeader
;
2566 // Construct a the list of inputs and their types.
2567 void Driver::BuildInputs(const ToolChain
&TC
, DerivedArgList
&Args
,
2568 InputList
&Inputs
) const {
2569 const llvm::opt::OptTable
&Opts
= getOpts();
2570 // Track the current user specified (-x) input. We also explicitly track the
2571 // argument used to set the type; we only want to claim the type when we
2572 // actually use it, so we warn about unused -x arguments.
2573 types::ID InputType
= types::TY_Nothing
;
2574 Arg
*InputTypeArg
= nullptr;
2576 // The last /TC or /TP option sets the input type to C or C++ globally.
2577 if (Arg
*TCTP
= Args
.getLastArgNoClaim(options::OPT__SLASH_TC
,
2578 options::OPT__SLASH_TP
)) {
2579 InputTypeArg
= TCTP
;
2580 InputType
= TCTP
->getOption().matches(options::OPT__SLASH_TC
)
2584 Arg
*Previous
= nullptr;
2585 bool ShowNote
= false;
2587 Args
.filtered(options::OPT__SLASH_TC
, options::OPT__SLASH_TP
)) {
2589 Diag(clang::diag::warn_drv_overriding_option
)
2590 << Previous
->getSpelling() << A
->getSpelling();
2596 Diag(clang::diag::note_drv_t_option_is_global
);
2599 // CUDA/HIP and their preprocessor expansions can be accepted by CL mode.
2600 // Warn -x after last input file has no effect
2601 auto LastXArg
= Args
.getLastArgValue(options::OPT_x
);
2602 const llvm::StringSet
<> ValidXArgs
= {"cuda", "hip", "cui", "hipi"};
2603 if (!IsCLMode() || ValidXArgs
.contains(LastXArg
)) {
2604 Arg
*LastXArg
= Args
.getLastArgNoClaim(options::OPT_x
);
2605 Arg
*LastInputArg
= Args
.getLastArgNoClaim(options::OPT_INPUT
);
2606 if (LastXArg
&& LastInputArg
&&
2607 LastInputArg
->getIndex() < LastXArg
->getIndex())
2608 Diag(clang::diag::warn_drv_unused_x
) << LastXArg
->getValue();
2610 // In CL mode suggest /TC or /TP since -x doesn't make sense if passed via
2612 if (auto *A
= Args
.getLastArg(options::OPT_x
))
2613 Diag(diag::err_drv_unsupported_opt_with_suggestion
)
2614 << A
->getAsString(Args
) << "/TC' or '/TP";
2617 for (Arg
*A
: Args
) {
2618 if (A
->getOption().getKind() == Option::InputClass
) {
2619 const char *Value
= A
->getValue();
2620 types::ID Ty
= types::TY_INVALID
;
2622 // Infer the input type if necessary.
2623 if (InputType
== types::TY_Nothing
) {
2624 // If there was an explicit arg for this, claim it.
2626 InputTypeArg
->claim();
2628 // stdin must be handled specially.
2629 if (memcmp(Value
, "-", 2) == 0) {
2630 if (IsFlangMode()) {
2631 Ty
= types::TY_Fortran
;
2632 } else if (IsDXCMode()) {
2633 Ty
= types::TY_HLSL
;
2635 // If running with -E, treat as a C input (this changes the
2636 // builtin macros, for example). This may be overridden by -ObjC
2639 // Otherwise emit an error but still use a valid type to avoid
2640 // spurious errors (e.g., no inputs).
2641 assert(!CCGenDiagnostics
&& "stdin produces no crash reproducer");
2642 if (!Args
.hasArgNoClaim(options::OPT_E
) && !CCCIsCPP())
2643 Diag(IsCLMode() ? clang::diag::err_drv_unknown_stdin_type_clang_cl
2644 : clang::diag::err_drv_unknown_stdin_type
);
2648 // Otherwise lookup by extension.
2649 // Fallback is C if invoked as C preprocessor, C++ if invoked with
2650 // clang-cl /E, or Object otherwise.
2651 // We use a host hook here because Darwin at least has its own
2652 // idea of what .s is.
2653 if (const char *Ext
= strrchr(Value
, '.'))
2654 Ty
= TC
.LookupTypeForExtension(Ext
+ 1);
2656 if (Ty
== types::TY_INVALID
) {
2657 if (IsCLMode() && (Args
.hasArgNoClaim(options::OPT_E
) || CCGenDiagnostics
))
2659 else if (CCCIsCPP() || CCGenDiagnostics
)
2662 Ty
= types::TY_Object
;
2665 // If the driver is invoked as C++ compiler (like clang++ or c++) it
2666 // should autodetect some input files as C++ for g++ compatibility.
2668 types::ID OldTy
= Ty
;
2669 Ty
= types::lookupCXXTypeForCType(Ty
);
2671 // Do not complain about foo.h, when we are known to be processing
2672 // it as a C++20 header unit.
2673 if (Ty
!= OldTy
&& !(OldTy
== types::TY_CHeader
&& hasHeaderMode()))
2674 Diag(clang::diag::warn_drv_treating_input_as_cxx
)
2675 << getTypeName(OldTy
) << getTypeName(Ty
);
2678 // If running with -fthinlto-index=, extensions that normally identify
2679 // native object files actually identify LLVM bitcode files.
2680 if (Args
.hasArgNoClaim(options::OPT_fthinlto_index_EQ
) &&
2681 Ty
== types::TY_Object
)
2682 Ty
= types::TY_LLVM_BC
;
2685 // -ObjC and -ObjC++ override the default language, but only for "source
2686 // files". We just treat everything that isn't a linker input as a
2689 // FIXME: Clean this up if we move the phase sequence into the type.
2690 if (Ty
!= types::TY_Object
) {
2691 if (Args
.hasArg(options::OPT_ObjC
))
2692 Ty
= types::TY_ObjC
;
2693 else if (Args
.hasArg(options::OPT_ObjCXX
))
2694 Ty
= types::TY_ObjCXX
;
2697 // Disambiguate headers that are meant to be header units from those
2698 // intended to be PCH. Avoid missing '.h' cases that are counted as
2699 // C headers by default - we know we are in C++ mode and we do not
2700 // want to issue a complaint about compiling things in the wrong mode.
2701 if ((Ty
== types::TY_CXXHeader
|| Ty
== types::TY_CHeader
) &&
2703 Ty
= CXXHeaderUnitType(CXX20HeaderType
);
2705 assert(InputTypeArg
&& "InputType set w/o InputTypeArg");
2706 if (!InputTypeArg
->getOption().matches(options::OPT_x
)) {
2707 // If emulating cl.exe, make sure that /TC and /TP don't affect input
2709 const char *Ext
= strrchr(Value
, '.');
2710 if (Ext
&& TC
.LookupTypeForExtension(Ext
+ 1) == types::TY_Object
)
2711 Ty
= types::TY_Object
;
2713 if (Ty
== types::TY_INVALID
) {
2715 InputTypeArg
->claim();
2719 if ((Ty
== types::TY_C
|| Ty
== types::TY_CXX
) &&
2720 Args
.hasArgNoClaim(options::OPT_hipstdpar
))
2723 if (DiagnoseInputExistence(Args
, Value
, Ty
, /*TypoCorrect=*/true))
2724 Inputs
.push_back(std::make_pair(Ty
, A
));
2726 } else if (A
->getOption().matches(options::OPT__SLASH_Tc
)) {
2727 StringRef Value
= A
->getValue();
2728 if (DiagnoseInputExistence(Args
, Value
, types::TY_C
,
2729 /*TypoCorrect=*/false)) {
2730 Arg
*InputArg
= MakeInputArg(Args
, Opts
, A
->getValue());
2731 Inputs
.push_back(std::make_pair(types::TY_C
, InputArg
));
2734 } else if (A
->getOption().matches(options::OPT__SLASH_Tp
)) {
2735 StringRef Value
= A
->getValue();
2736 if (DiagnoseInputExistence(Args
, Value
, types::TY_CXX
,
2737 /*TypoCorrect=*/false)) {
2738 Arg
*InputArg
= MakeInputArg(Args
, Opts
, A
->getValue());
2739 Inputs
.push_back(std::make_pair(types::TY_CXX
, InputArg
));
2742 } else if (A
->getOption().hasFlag(options::LinkerInput
)) {
2743 // Just treat as object type, we could make a special type for this if
2745 Inputs
.push_back(std::make_pair(types::TY_Object
, A
));
2747 } else if (A
->getOption().matches(options::OPT_x
)) {
2749 InputType
= types::lookupTypeForTypeSpecifier(A
->getValue());
2752 // Follow gcc behavior and treat as linker input for invalid -x
2753 // options. Its not clear why we shouldn't just revert to unknown; but
2754 // this isn't very important, we might as well be bug compatible.
2756 Diag(clang::diag::err_drv_unknown_language
) << A
->getValue();
2757 InputType
= types::TY_Object
;
2760 // If the user has put -fmodule-header{,=} then we treat C++ headers as
2761 // header unit inputs. So we 'promote' -xc++-header appropriately.
2762 if (InputType
== types::TY_CXXHeader
&& hasHeaderMode())
2763 InputType
= CXXHeaderUnitType(CXX20HeaderType
);
2764 } else if (A
->getOption().getID() == options::OPT_U
) {
2765 assert(A
->getNumValues() == 1 && "The /U option has one value.");
2766 StringRef Val
= A
->getValue(0);
2767 if (Val
.find_first_of("/\\") != StringRef::npos
) {
2768 // Warn about e.g. "/Users/me/myfile.c".
2769 Diag(diag::warn_slash_u_filename
) << Val
;
2770 Diag(diag::note_use_dashdash
);
2774 if (CCCIsCPP() && Inputs
.empty()) {
2775 // If called as standalone preprocessor, stdin is processed
2776 // if no other input is present.
2777 Arg
*A
= MakeInputArg(Args
, Opts
, "-");
2778 Inputs
.push_back(std::make_pair(types::TY_C
, A
));
2783 /// Provides a convenient interface for different programming models to generate
2784 /// the required device actions.
2785 class OffloadingActionBuilder final
{
2786 /// Flag used to trace errors in the builder.
2787 bool IsValid
= false;
2789 /// The compilation that is using this builder.
2792 /// Map between an input argument and the offload kinds used to process it.
2793 std::map
<const Arg
*, unsigned> InputArgToOffloadKindMap
;
2795 /// Map between a host action and its originating input argument.
2796 std::map
<Action
*, const Arg
*> HostActionToInputArgMap
;
2798 /// Builder interface. It doesn't build anything or keep any state.
2799 class DeviceActionBuilder
{
2801 typedef const llvm::SmallVectorImpl
<phases::ID
> PhasesTy
;
2803 enum ActionBuilderReturnCode
{
2804 // The builder acted successfully on the current action.
2806 // The builder didn't have to act on the current action.
2808 // The builder was successful and requested the host action to not be
2814 /// Compilation associated with this builder.
2817 /// Tool chains associated with this builder. The same programming
2818 /// model may have associated one or more tool chains.
2819 SmallVector
<const ToolChain
*, 2> ToolChains
;
2821 /// The derived arguments associated with this builder.
2822 DerivedArgList
&Args
;
2824 /// The inputs associated with this builder.
2825 const Driver::InputList
&Inputs
;
2827 /// The associated offload kind.
2828 Action::OffloadKind AssociatedOffloadKind
= Action::OFK_None
;
2831 DeviceActionBuilder(Compilation
&C
, DerivedArgList
&Args
,
2832 const Driver::InputList
&Inputs
,
2833 Action::OffloadKind AssociatedOffloadKind
)
2834 : C(C
), Args(Args
), Inputs(Inputs
),
2835 AssociatedOffloadKind(AssociatedOffloadKind
) {}
2836 virtual ~DeviceActionBuilder() {}
2838 /// Fill up the array \a DA with all the device dependences that should be
2839 /// added to the provided host action \a HostAction. By default it is
2841 virtual ActionBuilderReturnCode
2842 getDeviceDependences(OffloadAction::DeviceDependences
&DA
,
2843 phases::ID CurPhase
, phases::ID FinalPhase
,
2845 return ABRT_Inactive
;
2848 /// Update the state to include the provided host action \a HostAction as a
2849 /// dependency of the current device action. By default it is inactive.
2850 virtual ActionBuilderReturnCode
addDeviceDependences(Action
*HostAction
) {
2851 return ABRT_Inactive
;
2854 /// Append top level actions generated by the builder.
2855 virtual void appendTopLevelActions(ActionList
&AL
) {}
2857 /// Append linker device actions generated by the builder.
2858 virtual void appendLinkDeviceActions(ActionList
&AL
) {}
2860 /// Append linker host action generated by the builder.
2861 virtual Action
* appendLinkHostActions(ActionList
&AL
) { return nullptr; }
2863 /// Append linker actions generated by the builder.
2864 virtual void appendLinkDependences(OffloadAction::DeviceDependences
&DA
) {}
2866 /// Initialize the builder. Return true if any initialization errors are
2868 virtual bool initialize() { return false; }
2870 /// Return true if the builder can use bundling/unbundling.
2871 virtual bool canUseBundlerUnbundler() const { return false; }
2873 /// Return true if this builder is valid. We have a valid builder if we have
2874 /// associated device tool chains.
2875 bool isValid() { return !ToolChains
.empty(); }
2877 /// Return the associated offload kind.
2878 Action::OffloadKind
getAssociatedOffloadKind() {
2879 return AssociatedOffloadKind
;
2883 /// Base class for CUDA/HIP action builder. It injects device code in
2884 /// the host backend action.
2885 class CudaActionBuilderBase
: public DeviceActionBuilder
{
2887 /// Flags to signal if the user requested host-only or device-only
2889 bool CompileHostOnly
= false;
2890 bool CompileDeviceOnly
= false;
2891 bool EmitLLVM
= false;
2892 bool EmitAsm
= false;
2894 /// ID to identify each device compilation. For CUDA it is simply the
2895 /// GPU arch string. For HIP it is either the GPU arch string or GPU
2896 /// arch string plus feature strings delimited by a plus sign, e.g.
2899 /// Target ID string which is persistent throughout the compilation.
2901 TargetID(CudaArch Arch
) { ID
= CudaArchToString(Arch
); }
2902 TargetID(const char *ID
) : ID(ID
) {}
2903 operator const char *() { return ID
; }
2904 operator StringRef() { return StringRef(ID
); }
2906 /// List of GPU architectures to use in this compilation.
2907 SmallVector
<TargetID
, 4> GpuArchList
;
2909 /// The CUDA actions for the current input.
2910 ActionList CudaDeviceActions
;
2912 /// The CUDA fat binary if it was generated for the current input.
2913 Action
*CudaFatBinary
= nullptr;
2915 /// Flag that is set to true if this builder acted on the current input.
2916 bool IsActive
= false;
2918 /// Flag for -fgpu-rdc.
2919 bool Relocatable
= false;
2921 /// Default GPU architecture if there's no one specified.
2922 CudaArch DefaultCudaArch
= CudaArch::UNKNOWN
;
2924 /// Method to generate compilation unit ID specified by option
2926 enum UseCUIDKind
{ CUID_Hash
, CUID_Random
, CUID_None
, CUID_Invalid
};
2927 UseCUIDKind UseCUID
= CUID_Hash
;
2929 /// Compilation unit ID specified by option '-cuid='.
2930 StringRef FixedCUID
;
2933 CudaActionBuilderBase(Compilation
&C
, DerivedArgList
&Args
,
2934 const Driver::InputList
&Inputs
,
2935 Action::OffloadKind OFKind
)
2936 : DeviceActionBuilder(C
, Args
, Inputs
, OFKind
) {
2938 CompileDeviceOnly
= C
.getDriver().offloadDeviceOnly();
2939 Relocatable
= Args
.hasFlag(options::OPT_fgpu_rdc
,
2940 options::OPT_fno_gpu_rdc
, /*Default=*/false);
2943 ActionBuilderReturnCode
addDeviceDependences(Action
*HostAction
) override
{
2944 // While generating code for CUDA, we only depend on the host input action
2945 // to trigger the creation of all the CUDA device actions.
2947 // If we are dealing with an input action, replicate it for each GPU
2948 // architecture. If we are in host-only mode we return 'success' so that
2949 // the host uses the CUDA offload kind.
2950 if (auto *IA
= dyn_cast
<InputAction
>(HostAction
)) {
2951 assert(!GpuArchList
.empty() &&
2952 "We should have at least one GPU architecture.");
2954 // If the host input is not CUDA or HIP, we don't need to bother about
2956 if (!(IA
->getType() == types::TY_CUDA
||
2957 IA
->getType() == types::TY_HIP
||
2958 IA
->getType() == types::TY_PP_HIP
)) {
2959 // The builder will ignore this input.
2961 return ABRT_Inactive
;
2964 // Set the flag to true, so that the builder acts on the current input.
2967 if (CompileHostOnly
)
2968 return ABRT_Success
;
2970 // Replicate inputs for each GPU architecture.
2971 auto Ty
= IA
->getType() == types::TY_HIP
? types::TY_HIP_DEVICE
2972 : types::TY_CUDA_DEVICE
;
2973 std::string CUID
= FixedCUID
.str();
2975 if (UseCUID
== CUID_Random
)
2976 CUID
= llvm::utohexstr(llvm::sys::Process::GetRandomNumber(),
2977 /*LowerCase=*/true);
2978 else if (UseCUID
== CUID_Hash
) {
2980 llvm::MD5::MD5Result Hash
;
2981 SmallString
<256> RealPath
;
2982 llvm::sys::fs::real_path(IA
->getInputArg().getValue(), RealPath
,
2983 /*expand_tilde=*/true);
2984 Hasher
.update(RealPath
);
2985 for (auto *A
: Args
) {
2986 if (A
->getOption().matches(options::OPT_INPUT
))
2988 Hasher
.update(A
->getAsString(Args
));
2991 CUID
= llvm::utohexstr(Hash
.low(), /*LowerCase=*/true);
2996 for (unsigned I
= 0, E
= GpuArchList
.size(); I
!= E
; ++I
) {
2997 CudaDeviceActions
.push_back(
2998 C
.MakeAction
<InputAction
>(IA
->getInputArg(), Ty
, IA
->getId()));
3001 return ABRT_Success
;
3004 // If this is an unbundling action use it as is for each CUDA toolchain.
3005 if (auto *UA
= dyn_cast
<OffloadUnbundlingJobAction
>(HostAction
)) {
3007 // If -fgpu-rdc is disabled, should not unbundle since there is no
3008 // device code to link.
3009 if (UA
->getType() == types::TY_Object
&& !Relocatable
)
3010 return ABRT_Inactive
;
3012 CudaDeviceActions
.clear();
3013 auto *IA
= cast
<InputAction
>(UA
->getInputs().back());
3014 std::string FileName
= IA
->getInputArg().getAsString(Args
);
3015 // Check if the type of the file is the same as the action. Do not
3016 // unbundle it if it is not. Do not unbundle .so files, for example,
3017 // which are not object files. Files with extension ".lib" is classified
3018 // as TY_Object but they are actually archives, therefore should not be
3019 // unbundled here as objects. They will be handled at other places.
3020 const StringRef LibFileExt
= ".lib";
3021 if (IA
->getType() == types::TY_Object
&&
3022 (!llvm::sys::path::has_extension(FileName
) ||
3023 types::lookupTypeForExtension(
3024 llvm::sys::path::extension(FileName
).drop_front()) !=
3026 llvm::sys::path::extension(FileName
) == LibFileExt
))
3027 return ABRT_Inactive
;
3029 for (auto Arch
: GpuArchList
) {
3030 CudaDeviceActions
.push_back(UA
);
3031 UA
->registerDependentActionInfo(ToolChains
[0], Arch
,
3032 AssociatedOffloadKind
);
3035 return ABRT_Success
;
3038 return IsActive
? ABRT_Success
: ABRT_Inactive
;
3041 void appendTopLevelActions(ActionList
&AL
) override
{
3042 // Utility to append actions to the top level list.
3043 auto AddTopLevel
= [&](Action
*A
, TargetID TargetID
) {
3044 OffloadAction::DeviceDependences Dep
;
3045 Dep
.add(*A
, *ToolChains
.front(), TargetID
, AssociatedOffloadKind
);
3046 AL
.push_back(C
.MakeAction
<OffloadAction
>(Dep
, A
->getType()));
3049 // If we have a fat binary, add it to the list.
3050 if (CudaFatBinary
) {
3051 AddTopLevel(CudaFatBinary
, CudaArch::UNUSED
);
3052 CudaDeviceActions
.clear();
3053 CudaFatBinary
= nullptr;
3057 if (CudaDeviceActions
.empty())
3060 // If we have CUDA actions at this point, that's because we have a have
3061 // partial compilation, so we should have an action for each GPU
3063 assert(CudaDeviceActions
.size() == GpuArchList
.size() &&
3064 "Expecting one action per GPU architecture.");
3065 assert(ToolChains
.size() == 1 &&
3066 "Expecting to have a single CUDA toolchain.");
3067 for (unsigned I
= 0, E
= GpuArchList
.size(); I
!= E
; ++I
)
3068 AddTopLevel(CudaDeviceActions
[I
], GpuArchList
[I
]);
3070 CudaDeviceActions
.clear();
3073 /// Get canonicalized offload arch option. \returns empty StringRef if the
3074 /// option is invalid.
3075 virtual StringRef
getCanonicalOffloadArch(StringRef Arch
) = 0;
3077 virtual std::optional
<std::pair
<llvm::StringRef
, llvm::StringRef
>>
3078 getConflictOffloadArchCombination(const std::set
<StringRef
> &GpuArchs
) = 0;
3080 bool initialize() override
{
3081 assert(AssociatedOffloadKind
== Action::OFK_Cuda
||
3082 AssociatedOffloadKind
== Action::OFK_HIP
);
3084 // We don't need to support CUDA.
3085 if (AssociatedOffloadKind
== Action::OFK_Cuda
&&
3086 !C
.hasOffloadToolChain
<Action::OFK_Cuda
>())
3089 // We don't need to support HIP.
3090 if (AssociatedOffloadKind
== Action::OFK_HIP
&&
3091 !C
.hasOffloadToolChain
<Action::OFK_HIP
>())
3094 const ToolChain
*HostTC
= C
.getSingleOffloadToolChain
<Action::OFK_Host
>();
3095 assert(HostTC
&& "No toolchain for host compilation.");
3096 if (HostTC
->getTriple().isNVPTX() ||
3097 HostTC
->getTriple().getArch() == llvm::Triple::amdgcn
) {
3098 // We do not support targeting NVPTX/AMDGCN for host compilation. Throw
3099 // an error and abort pipeline construction early so we don't trip
3100 // asserts that assume device-side compilation.
3101 C
.getDriver().Diag(diag::err_drv_cuda_host_arch
)
3102 << HostTC
->getTriple().getArchName();
3106 ToolChains
.push_back(
3107 AssociatedOffloadKind
== Action::OFK_Cuda
3108 ? C
.getSingleOffloadToolChain
<Action::OFK_Cuda
>()
3109 : C
.getSingleOffloadToolChain
<Action::OFK_HIP
>());
3111 CompileHostOnly
= C
.getDriver().offloadHostOnly();
3112 EmitLLVM
= Args
.getLastArg(options::OPT_emit_llvm
);
3113 EmitAsm
= Args
.getLastArg(options::OPT_S
);
3114 FixedCUID
= Args
.getLastArgValue(options::OPT_cuid_EQ
);
3115 if (Arg
*A
= Args
.getLastArg(options::OPT_fuse_cuid_EQ
)) {
3116 StringRef UseCUIDStr
= A
->getValue();
3117 UseCUID
= llvm::StringSwitch
<UseCUIDKind
>(UseCUIDStr
)
3118 .Case("hash", CUID_Hash
)
3119 .Case("random", CUID_Random
)
3120 .Case("none", CUID_None
)
3121 .Default(CUID_Invalid
);
3122 if (UseCUID
== CUID_Invalid
) {
3123 C
.getDriver().Diag(diag::err_drv_invalid_value
)
3124 << A
->getAsString(Args
) << UseCUIDStr
;
3125 C
.setContainsError();
3130 // --offload and --offload-arch options are mutually exclusive.
3131 if (Args
.hasArgNoClaim(options::OPT_offload_EQ
) &&
3132 Args
.hasArgNoClaim(options::OPT_offload_arch_EQ
,
3133 options::OPT_no_offload_arch_EQ
)) {
3134 C
.getDriver().Diag(diag::err_opt_not_valid_with_opt
) << "--offload-arch"
3138 // Collect all offload arch parameters, removing duplicates.
3139 std::set
<StringRef
> GpuArchs
;
3141 for (Arg
*A
: Args
) {
3142 if (!(A
->getOption().matches(options::OPT_offload_arch_EQ
) ||
3143 A
->getOption().matches(options::OPT_no_offload_arch_EQ
)))
3147 for (StringRef ArchStr
: llvm::split(A
->getValue(), ",")) {
3148 if (A
->getOption().matches(options::OPT_no_offload_arch_EQ
) &&
3151 } else if (ArchStr
== "native") {
3152 const ToolChain
&TC
= *ToolChains
.front();
3153 auto GPUsOrErr
= ToolChains
.front()->getSystemGPUArchs(Args
);
3155 TC
.getDriver().Diag(diag::err_drv_undetermined_gpu_arch
)
3156 << llvm::Triple::getArchTypeName(TC
.getArch())
3157 << llvm::toString(GPUsOrErr
.takeError()) << "--offload-arch";
3161 for (auto GPU
: *GPUsOrErr
) {
3162 GpuArchs
.insert(Args
.MakeArgString(GPU
));
3165 ArchStr
= getCanonicalOffloadArch(ArchStr
);
3166 if (ArchStr
.empty()) {
3168 } else if (A
->getOption().matches(options::OPT_offload_arch_EQ
))
3169 GpuArchs
.insert(ArchStr
);
3170 else if (A
->getOption().matches(options::OPT_no_offload_arch_EQ
))
3171 GpuArchs
.erase(ArchStr
);
3173 llvm_unreachable("Unexpected option.");
3178 auto &&ConflictingArchs
= getConflictOffloadArchCombination(GpuArchs
);
3179 if (ConflictingArchs
) {
3180 C
.getDriver().Diag(clang::diag::err_drv_bad_offload_arch_combo
)
3181 << ConflictingArchs
->first
<< ConflictingArchs
->second
;
3182 C
.setContainsError();
3186 // Collect list of GPUs remaining in the set.
3187 for (auto Arch
: GpuArchs
)
3188 GpuArchList
.push_back(Arch
.data());
3190 // Default to sm_20 which is the lowest common denominator for
3191 // supported GPUs. sm_20 code should work correctly, if
3192 // suboptimally, on all newer GPUs.
3193 if (GpuArchList
.empty()) {
3194 if (ToolChains
.front()->getTriple().isSPIRV())
3195 GpuArchList
.push_back(CudaArch::Generic
);
3197 GpuArchList
.push_back(DefaultCudaArch
);
3204 /// \brief CUDA action builder. It injects device code in the host backend
3206 class CudaActionBuilder final
: public CudaActionBuilderBase
{
3208 CudaActionBuilder(Compilation
&C
, DerivedArgList
&Args
,
3209 const Driver::InputList
&Inputs
)
3210 : CudaActionBuilderBase(C
, Args
, Inputs
, Action::OFK_Cuda
) {
3211 DefaultCudaArch
= CudaArch::SM_35
;
3214 StringRef
getCanonicalOffloadArch(StringRef ArchStr
) override
{
3215 CudaArch Arch
= StringToCudaArch(ArchStr
);
3216 if (Arch
== CudaArch::UNKNOWN
|| !IsNVIDIAGpuArch(Arch
)) {
3217 C
.getDriver().Diag(clang::diag::err_drv_cuda_bad_gpu_arch
) << ArchStr
;
3220 return CudaArchToString(Arch
);
3223 std::optional
<std::pair
<llvm::StringRef
, llvm::StringRef
>>
3224 getConflictOffloadArchCombination(
3225 const std::set
<StringRef
> &GpuArchs
) override
{
3226 return std::nullopt
;
3229 ActionBuilderReturnCode
3230 getDeviceDependences(OffloadAction::DeviceDependences
&DA
,
3231 phases::ID CurPhase
, phases::ID FinalPhase
,
3232 PhasesTy
&Phases
) override
{
3234 return ABRT_Inactive
;
3236 // If we don't have more CUDA actions, we don't have any dependences to
3237 // create for the host.
3238 if (CudaDeviceActions
.empty())
3239 return ABRT_Success
;
3241 assert(CudaDeviceActions
.size() == GpuArchList
.size() &&
3242 "Expecting one action per GPU architecture.");
3243 assert(!CompileHostOnly
&&
3244 "Not expecting CUDA actions in host-only compilation.");
3246 // If we are generating code for the device or we are in a backend phase,
3247 // we attempt to generate the fat binary. We compile each arch to ptx and
3248 // assemble to cubin, then feed the cubin *and* the ptx into a device
3249 // "link" action, which uses fatbinary to combine these cubins into one
3250 // fatbin. The fatbin is then an input to the host action if not in
3251 // device-only mode.
3252 if (CompileDeviceOnly
|| CurPhase
== phases::Backend
) {
3253 ActionList DeviceActions
;
3254 for (unsigned I
= 0, E
= GpuArchList
.size(); I
!= E
; ++I
) {
3255 // Produce the device action from the current phase up to the assemble
3257 for (auto Ph
: Phases
) {
3258 // Skip the phases that were already dealt with.
3261 // We have to be consistent with the host final phase.
3262 if (Ph
> FinalPhase
)
3265 CudaDeviceActions
[I
] = C
.getDriver().ConstructPhaseAction(
3266 C
, Args
, Ph
, CudaDeviceActions
[I
], Action::OFK_Cuda
);
3268 if (Ph
== phases::Assemble
)
3272 // If we didn't reach the assemble phase, we can't generate the fat
3273 // binary. We don't need to generate the fat binary if we are not in
3274 // device-only mode.
3275 if (!isa
<AssembleJobAction
>(CudaDeviceActions
[I
]) ||
3279 Action
*AssembleAction
= CudaDeviceActions
[I
];
3280 assert(AssembleAction
->getType() == types::TY_Object
);
3281 assert(AssembleAction
->getInputs().size() == 1);
3283 Action
*BackendAction
= AssembleAction
->getInputs()[0];
3284 assert(BackendAction
->getType() == types::TY_PP_Asm
);
3286 for (auto &A
: {AssembleAction
, BackendAction
}) {
3287 OffloadAction::DeviceDependences DDep
;
3288 DDep
.add(*A
, *ToolChains
.front(), GpuArchList
[I
], Action::OFK_Cuda
);
3289 DeviceActions
.push_back(
3290 C
.MakeAction
<OffloadAction
>(DDep
, A
->getType()));
3294 // We generate the fat binary if we have device input actions.
3295 if (!DeviceActions
.empty()) {
3297 C
.MakeAction
<LinkJobAction
>(DeviceActions
, types::TY_CUDA_FATBIN
);
3299 if (!CompileDeviceOnly
) {
3300 DA
.add(*CudaFatBinary
, *ToolChains
.front(), /*BoundArch=*/nullptr,
3302 // Clear the fat binary, it is already a dependence to an host
3304 CudaFatBinary
= nullptr;
3307 // Remove the CUDA actions as they are already connected to an host
3308 // action or fat binary.
3309 CudaDeviceActions
.clear();
3312 // We avoid creating host action in device-only mode.
3313 return CompileDeviceOnly
? ABRT_Ignore_Host
: ABRT_Success
;
3314 } else if (CurPhase
> phases::Backend
) {
3315 // If we are past the backend phase and still have a device action, we
3316 // don't have to do anything as this action is already a device
3317 // top-level action.
3318 return ABRT_Success
;
3321 assert(CurPhase
< phases::Backend
&& "Generating single CUDA "
3322 "instructions should only occur "
3323 "before the backend phase!");
3325 // By default, we produce an action for each device arch.
3326 for (Action
*&A
: CudaDeviceActions
)
3327 A
= C
.getDriver().ConstructPhaseAction(C
, Args
, CurPhase
, A
);
3329 return ABRT_Success
;
3332 /// \brief HIP action builder. It injects device code in the host backend
3334 class HIPActionBuilder final
: public CudaActionBuilderBase
{
3335 /// The linker inputs obtained for each device arch.
3336 SmallVector
<ActionList
, 8> DeviceLinkerInputs
;
3337 // The default bundling behavior depends on the type of output, therefore
3338 // BundleOutput needs to be tri-value: None, true, or false.
3339 // Bundle code objects except --no-gpu-output is specified for device
3340 // only compilation. Bundle other type of output files only if
3341 // --gpu-bundle-output is specified for device only compilation.
3342 std::optional
<bool> BundleOutput
;
3343 std::optional
<bool> EmitReloc
;
3346 HIPActionBuilder(Compilation
&C
, DerivedArgList
&Args
,
3347 const Driver::InputList
&Inputs
)
3348 : CudaActionBuilderBase(C
, Args
, Inputs
, Action::OFK_HIP
) {
3350 DefaultCudaArch
= CudaArch::GFX906
;
3352 if (Args
.hasArg(options::OPT_fhip_emit_relocatable
,
3353 options::OPT_fno_hip_emit_relocatable
)) {
3354 EmitReloc
= Args
.hasFlag(options::OPT_fhip_emit_relocatable
,
3355 options::OPT_fno_hip_emit_relocatable
, false);
3359 C
.getDriver().Diag(diag::err_opt_not_valid_with_opt
)
3360 << "-fhip-emit-relocatable"
3364 if (!CompileDeviceOnly
) {
3365 C
.getDriver().Diag(diag::err_opt_not_valid_without_opt
)
3366 << "-fhip-emit-relocatable"
3367 << "--cuda-device-only";
3372 if (Args
.hasArg(options::OPT_gpu_bundle_output
,
3373 options::OPT_no_gpu_bundle_output
))
3374 BundleOutput
= Args
.hasFlag(options::OPT_gpu_bundle_output
,
3375 options::OPT_no_gpu_bundle_output
, true) &&
3376 (!EmitReloc
|| !*EmitReloc
);
3379 bool canUseBundlerUnbundler() const override
{ return true; }
3381 StringRef
getCanonicalOffloadArch(StringRef IdStr
) override
{
3382 llvm::StringMap
<bool> Features
;
3383 // getHIPOffloadTargetTriple() is known to return valid value as it has
3384 // been called successfully in the CreateOffloadingDeviceToolChains().
3385 auto ArchStr
= parseTargetID(
3386 *getHIPOffloadTargetTriple(C
.getDriver(), C
.getInputArgs()), IdStr
,
3389 C
.getDriver().Diag(clang::diag::err_drv_bad_target_id
) << IdStr
;
3390 C
.setContainsError();
3393 auto CanId
= getCanonicalTargetID(*ArchStr
, Features
);
3394 return Args
.MakeArgStringRef(CanId
);
3397 std::optional
<std::pair
<llvm::StringRef
, llvm::StringRef
>>
3398 getConflictOffloadArchCombination(
3399 const std::set
<StringRef
> &GpuArchs
) override
{
3400 return getConflictTargetIDCombination(GpuArchs
);
3403 ActionBuilderReturnCode
3404 getDeviceDependences(OffloadAction::DeviceDependences
&DA
,
3405 phases::ID CurPhase
, phases::ID FinalPhase
,
3406 PhasesTy
&Phases
) override
{
3408 return ABRT_Inactive
;
3410 // amdgcn does not support linking of object files, therefore we skip
3411 // backend and assemble phases to output LLVM IR. Except for generating
3412 // non-relocatable device code, where we generate fat binary for device
3413 // code and pass to host in Backend phase.
3414 if (CudaDeviceActions
.empty())
3415 return ABRT_Success
;
3417 assert(((CurPhase
== phases::Link
&& Relocatable
) ||
3418 CudaDeviceActions
.size() == GpuArchList
.size()) &&
3419 "Expecting one action per GPU architecture.");
3420 assert(!CompileHostOnly
&&
3421 "Not expecting HIP actions in host-only compilation.");
3423 bool ShouldLink
= !EmitReloc
|| !*EmitReloc
;
3425 if (!Relocatable
&& CurPhase
== phases::Backend
&& !EmitLLVM
&&
3426 !EmitAsm
&& ShouldLink
) {
3427 // If we are in backend phase, we attempt to generate the fat binary.
3428 // We compile each arch to IR and use a link action to generate code
3429 // object containing ISA. Then we use a special "link" action to create
3430 // a fat binary containing all the code objects for different GPU's.
3431 // The fat binary is then an input to the host action.
3432 for (unsigned I
= 0, E
= GpuArchList
.size(); I
!= E
; ++I
) {
3433 if (C
.getDriver().isUsingLTO(/*IsOffload=*/true)) {
3434 // When LTO is enabled, skip the backend and assemble phases and
3435 // use lld to link the bitcode.
3437 AL
.push_back(CudaDeviceActions
[I
]);
3438 // Create a link action to link device IR with device library
3439 // and generate ISA.
3440 CudaDeviceActions
[I
] =
3441 C
.MakeAction
<LinkJobAction
>(AL
, types::TY_Image
);
3443 // When LTO is not enabled, we follow the conventional
3444 // compiler phases, including backend and assemble phases.
3446 Action
*BackendAction
= nullptr;
3447 if (ToolChains
.front()->getTriple().isSPIRV()) {
3448 // Emit LLVM bitcode for SPIR-V targets. SPIR-V device tool chain
3449 // (HIPSPVToolChain) runs post-link LLVM IR passes.
3450 types::ID Output
= Args
.hasArg(options::OPT_S
)
3452 : types::TY_LLVM_BC
;
3454 C
.MakeAction
<BackendJobAction
>(CudaDeviceActions
[I
], Output
);
3456 BackendAction
= C
.getDriver().ConstructPhaseAction(
3457 C
, Args
, phases::Backend
, CudaDeviceActions
[I
],
3458 AssociatedOffloadKind
);
3459 auto AssembleAction
= C
.getDriver().ConstructPhaseAction(
3460 C
, Args
, phases::Assemble
, BackendAction
,
3461 AssociatedOffloadKind
);
3462 AL
.push_back(AssembleAction
);
3463 // Create a link action to link device IR with device library
3464 // and generate ISA.
3465 CudaDeviceActions
[I
] =
3466 C
.MakeAction
<LinkJobAction
>(AL
, types::TY_Image
);
3469 // OffloadingActionBuilder propagates device arch until an offload
3470 // action. Since the next action for creating fatbin does
3471 // not have device arch, whereas the above link action and its input
3472 // have device arch, an offload action is needed to stop the null
3473 // device arch of the next action being propagated to the above link
3475 OffloadAction::DeviceDependences DDep
;
3476 DDep
.add(*CudaDeviceActions
[I
], *ToolChains
.front(), GpuArchList
[I
],
3477 AssociatedOffloadKind
);
3478 CudaDeviceActions
[I
] = C
.MakeAction
<OffloadAction
>(
3479 DDep
, CudaDeviceActions
[I
]->getType());
3482 if (!CompileDeviceOnly
|| !BundleOutput
|| *BundleOutput
) {
3483 // Create HIP fat binary with a special "link" action.
3484 CudaFatBinary
= C
.MakeAction
<LinkJobAction
>(CudaDeviceActions
,
3485 types::TY_HIP_FATBIN
);
3487 if (!CompileDeviceOnly
) {
3488 DA
.add(*CudaFatBinary
, *ToolChains
.front(), /*BoundArch=*/nullptr,
3489 AssociatedOffloadKind
);
3490 // Clear the fat binary, it is already a dependence to an host
3492 CudaFatBinary
= nullptr;
3495 // Remove the CUDA actions as they are already connected to an host
3496 // action or fat binary.
3497 CudaDeviceActions
.clear();
3500 return CompileDeviceOnly
? ABRT_Ignore_Host
: ABRT_Success
;
3501 } else if (CurPhase
== phases::Link
) {
3503 return ABRT_Success
;
3504 // Save CudaDeviceActions to DeviceLinkerInputs for each GPU subarch.
3505 // This happens to each device action originated from each input file.
3506 // Later on, device actions in DeviceLinkerInputs are used to create
3507 // device link actions in appendLinkDependences and the created device
3508 // link actions are passed to the offload action as device dependence.
3509 DeviceLinkerInputs
.resize(CudaDeviceActions
.size());
3510 auto LI
= DeviceLinkerInputs
.begin();
3511 for (auto *A
: CudaDeviceActions
) {
3516 // We will pass the device action as a host dependence, so we don't
3517 // need to do anything else with them.
3518 CudaDeviceActions
.clear();
3519 return CompileDeviceOnly
? ABRT_Ignore_Host
: ABRT_Success
;
3522 // By default, we produce an action for each device arch.
3523 for (Action
*&A
: CudaDeviceActions
)
3524 A
= C
.getDriver().ConstructPhaseAction(C
, Args
, CurPhase
, A
,
3525 AssociatedOffloadKind
);
3527 if (CompileDeviceOnly
&& CurPhase
== FinalPhase
&& BundleOutput
&&
3529 for (unsigned I
= 0, E
= GpuArchList
.size(); I
!= E
; ++I
) {
3530 OffloadAction::DeviceDependences DDep
;
3531 DDep
.add(*CudaDeviceActions
[I
], *ToolChains
.front(), GpuArchList
[I
],
3532 AssociatedOffloadKind
);
3533 CudaDeviceActions
[I
] = C
.MakeAction
<OffloadAction
>(
3534 DDep
, CudaDeviceActions
[I
]->getType());
3537 C
.MakeAction
<OffloadBundlingJobAction
>(CudaDeviceActions
);
3538 CudaDeviceActions
.clear();
3541 return (CompileDeviceOnly
&&
3542 (CurPhase
== FinalPhase
||
3543 (!ShouldLink
&& CurPhase
== phases::Assemble
)))
3548 void appendLinkDeviceActions(ActionList
&AL
) override
{
3549 if (DeviceLinkerInputs
.size() == 0)
3552 assert(DeviceLinkerInputs
.size() == GpuArchList
.size() &&
3553 "Linker inputs and GPU arch list sizes do not match.");
3557 // Append a new link action for each device.
3558 // Each entry in DeviceLinkerInputs corresponds to a GPU arch.
3559 for (auto &LI
: DeviceLinkerInputs
) {
3561 types::ID Output
= Args
.hasArg(options::OPT_emit_llvm
)
3565 auto *DeviceLinkAction
= C
.MakeAction
<LinkJobAction
>(LI
, Output
);
3566 // Linking all inputs for the current GPU arch.
3567 // LI contains all the inputs for the linker.
3568 OffloadAction::DeviceDependences DeviceLinkDeps
;
3569 DeviceLinkDeps
.add(*DeviceLinkAction
, *ToolChains
[0],
3570 GpuArchList
[I
], AssociatedOffloadKind
);
3571 Actions
.push_back(C
.MakeAction
<OffloadAction
>(
3572 DeviceLinkDeps
, DeviceLinkAction
->getType()));
3575 DeviceLinkerInputs
.clear();
3577 // If emitting LLVM, do not generate final host/device compilation action
3578 if (Args
.hasArg(options::OPT_emit_llvm
)) {
3583 // Create a host object from all the device images by embedding them
3584 // in a fat binary for mixed host-device compilation. For device-only
3585 // compilation, creates a fat binary.
3586 OffloadAction::DeviceDependences DDeps
;
3587 if (!CompileDeviceOnly
|| !BundleOutput
|| *BundleOutput
) {
3588 auto *TopDeviceLinkAction
= C
.MakeAction
<LinkJobAction
>(
3590 CompileDeviceOnly
? types::TY_HIP_FATBIN
: types::TY_Object
);
3591 DDeps
.add(*TopDeviceLinkAction
, *ToolChains
[0], nullptr,
3592 AssociatedOffloadKind
);
3593 // Offload the host object to the host linker.
3595 C
.MakeAction
<OffloadAction
>(DDeps
, TopDeviceLinkAction
->getType()));
3601 Action
* appendLinkHostActions(ActionList
&AL
) override
{ return AL
.back(); }
3603 void appendLinkDependences(OffloadAction::DeviceDependences
&DA
) override
{}
3607 /// TODO: Add the implementation for other specialized builders here.
3610 /// Specialized builders being used by this offloading action builder.
3611 SmallVector
<DeviceActionBuilder
*, 4> SpecializedBuilders
;
3613 /// Flag set to true if all valid builders allow file bundling/unbundling.
3617 OffloadingActionBuilder(Compilation
&C
, DerivedArgList
&Args
,
3618 const Driver::InputList
&Inputs
)
3620 // Create a specialized builder for each device toolchain.
3624 // Create a specialized builder for CUDA.
3625 SpecializedBuilders
.push_back(new CudaActionBuilder(C
, Args
, Inputs
));
3627 // Create a specialized builder for HIP.
3628 SpecializedBuilders
.push_back(new HIPActionBuilder(C
, Args
, Inputs
));
3631 // TODO: Build other specialized builders here.
3634 // Initialize all the builders, keeping track of errors. If all valid
3635 // builders agree that we can use bundling, set the flag to true.
3636 unsigned ValidBuilders
= 0u;
3637 unsigned ValidBuildersSupportingBundling
= 0u;
3638 for (auto *SB
: SpecializedBuilders
) {
3639 IsValid
= IsValid
&& !SB
->initialize();
3641 // Update the counters if the builder is valid.
3642 if (SB
->isValid()) {
3644 if (SB
->canUseBundlerUnbundler())
3645 ++ValidBuildersSupportingBundling
;
3649 ValidBuilders
&& ValidBuilders
== ValidBuildersSupportingBundling
;
3652 ~OffloadingActionBuilder() {
3653 for (auto *SB
: SpecializedBuilders
)
3657 /// Record a host action and its originating input argument.
3658 void recordHostAction(Action
*HostAction
, const Arg
*InputArg
) {
3659 assert(HostAction
&& "Invalid host action");
3660 assert(InputArg
&& "Invalid input argument");
3661 auto Loc
= HostActionToInputArgMap
.find(HostAction
);
3662 if (Loc
== HostActionToInputArgMap
.end())
3663 HostActionToInputArgMap
[HostAction
] = InputArg
;
3664 assert(HostActionToInputArgMap
[HostAction
] == InputArg
&&
3665 "host action mapped to multiple input arguments");
3668 /// Generate an action that adds device dependences (if any) to a host action.
3669 /// If no device dependence actions exist, just return the host action \a
3670 /// HostAction. If an error is found or if no builder requires the host action
3671 /// to be generated, return nullptr.
3673 addDeviceDependencesToHostAction(Action
*HostAction
, const Arg
*InputArg
,
3674 phases::ID CurPhase
, phases::ID FinalPhase
,
3675 DeviceActionBuilder::PhasesTy
&Phases
) {
3679 if (SpecializedBuilders
.empty())
3682 assert(HostAction
&& "Invalid host action!");
3683 recordHostAction(HostAction
, InputArg
);
3685 OffloadAction::DeviceDependences DDeps
;
3686 // Check if all the programming models agree we should not emit the host
3687 // action. Also, keep track of the offloading kinds employed.
3688 auto &OffloadKind
= InputArgToOffloadKindMap
[InputArg
];
3689 unsigned InactiveBuilders
= 0u;
3690 unsigned IgnoringBuilders
= 0u;
3691 for (auto *SB
: SpecializedBuilders
) {
3692 if (!SB
->isValid()) {
3697 SB
->getDeviceDependences(DDeps
, CurPhase
, FinalPhase
, Phases
);
3699 // If the builder explicitly says the host action should be ignored,
3700 // we need to increment the variable that tracks the builders that request
3701 // the host object to be ignored.
3702 if (RetCode
== DeviceActionBuilder::ABRT_Ignore_Host
)
3705 // Unless the builder was inactive for this action, we have to record the
3706 // offload kind because the host will have to use it.
3707 if (RetCode
!= DeviceActionBuilder::ABRT_Inactive
)
3708 OffloadKind
|= SB
->getAssociatedOffloadKind();
3711 // If all builders agree that the host object should be ignored, just return
3713 if (IgnoringBuilders
&&
3714 SpecializedBuilders
.size() == (InactiveBuilders
+ IgnoringBuilders
))
3717 if (DDeps
.getActions().empty())
3720 // We have dependences we need to bundle together. We use an offload action
3722 OffloadAction::HostDependence
HDep(
3723 *HostAction
, *C
.getSingleOffloadToolChain
<Action::OFK_Host
>(),
3724 /*BoundArch=*/nullptr, DDeps
);
3725 return C
.MakeAction
<OffloadAction
>(HDep
, DDeps
);
3728 /// Generate an action that adds a host dependence to a device action. The
3729 /// results will be kept in this action builder. Return true if an error was
3731 bool addHostDependenceToDeviceActions(Action
*&HostAction
,
3732 const Arg
*InputArg
) {
3736 recordHostAction(HostAction
, InputArg
);
3738 // If we are supporting bundling/unbundling and the current action is an
3739 // input action of non-source file, we replace the host action by the
3740 // unbundling action. The bundler tool has the logic to detect if an input
3741 // is a bundle or not and if the input is not a bundle it assumes it is a
3742 // host file. Therefore it is safe to create an unbundling action even if
3743 // the input is not a bundle.
3744 if (CanUseBundler
&& isa
<InputAction
>(HostAction
) &&
3745 InputArg
->getOption().getKind() == llvm::opt::Option::InputClass
&&
3746 (!types::isSrcFile(HostAction
->getType()) ||
3747 HostAction
->getType() == types::TY_PP_HIP
)) {
3748 auto UnbundlingHostAction
=
3749 C
.MakeAction
<OffloadUnbundlingJobAction
>(HostAction
);
3750 UnbundlingHostAction
->registerDependentActionInfo(
3751 C
.getSingleOffloadToolChain
<Action::OFK_Host
>(),
3752 /*BoundArch=*/StringRef(), Action::OFK_Host
);
3753 HostAction
= UnbundlingHostAction
;
3754 recordHostAction(HostAction
, InputArg
);
3757 assert(HostAction
&& "Invalid host action!");
3759 // Register the offload kinds that are used.
3760 auto &OffloadKind
= InputArgToOffloadKindMap
[InputArg
];
3761 for (auto *SB
: SpecializedBuilders
) {
3765 auto RetCode
= SB
->addDeviceDependences(HostAction
);
3767 // Host dependences for device actions are not compatible with that same
3768 // action being ignored.
3769 assert(RetCode
!= DeviceActionBuilder::ABRT_Ignore_Host
&&
3770 "Host dependence not expected to be ignored.!");
3772 // Unless the builder was inactive for this action, we have to record the
3773 // offload kind because the host will have to use it.
3774 if (RetCode
!= DeviceActionBuilder::ABRT_Inactive
)
3775 OffloadKind
|= SB
->getAssociatedOffloadKind();
3778 // Do not use unbundler if the Host does not depend on device action.
3779 if (OffloadKind
== Action::OFK_None
&& CanUseBundler
)
3780 if (auto *UA
= dyn_cast
<OffloadUnbundlingJobAction
>(HostAction
))
3781 HostAction
= UA
->getInputs().back();
3786 /// Add the offloading top level actions to the provided action list. This
3787 /// function can replace the host action by a bundling action if the
3788 /// programming models allow it.
3789 bool appendTopLevelActions(ActionList
&AL
, Action
*HostAction
,
3790 const Arg
*InputArg
) {
3792 recordHostAction(HostAction
, InputArg
);
3794 // Get the device actions to be appended.
3795 ActionList OffloadAL
;
3796 for (auto *SB
: SpecializedBuilders
) {
3799 SB
->appendTopLevelActions(OffloadAL
);
3802 // If we can use the bundler, replace the host action by the bundling one in
3803 // the resulting list. Otherwise, just append the device actions. For
3804 // device only compilation, HostAction is a null pointer, therefore only do
3805 // this when HostAction is not a null pointer.
3806 if (CanUseBundler
&& HostAction
&&
3807 HostAction
->getType() != types::TY_Nothing
&& !OffloadAL
.empty()) {
3808 // Add the host action to the list in order to create the bundling action.
3809 OffloadAL
.push_back(HostAction
);
3811 // We expect that the host action was just appended to the action list
3812 // before this method was called.
3813 assert(HostAction
== AL
.back() && "Host action not in the list??");
3814 HostAction
= C
.MakeAction
<OffloadBundlingJobAction
>(OffloadAL
);
3815 recordHostAction(HostAction
, InputArg
);
3816 AL
.back() = HostAction
;
3818 AL
.append(OffloadAL
.begin(), OffloadAL
.end());
3820 // Propagate to the current host action (if any) the offload information
3821 // associated with the current input.
3823 HostAction
->propagateHostOffloadInfo(InputArgToOffloadKindMap
[InputArg
],
3824 /*BoundArch=*/nullptr);
3828 void appendDeviceLinkActions(ActionList
&AL
) {
3829 for (DeviceActionBuilder
*SB
: SpecializedBuilders
) {
3832 SB
->appendLinkDeviceActions(AL
);
3836 Action
*makeHostLinkAction() {
3837 // Build a list of device linking actions.
3838 ActionList DeviceAL
;
3839 appendDeviceLinkActions(DeviceAL
);
3840 if (DeviceAL
.empty())
3843 // Let builders add host linking actions.
3844 Action
* HA
= nullptr;
3845 for (DeviceActionBuilder
*SB
: SpecializedBuilders
) {
3848 HA
= SB
->appendLinkHostActions(DeviceAL
);
3849 // This created host action has no originating input argument, therefore
3850 // needs to set its offloading kind directly.
3852 HA
->propagateHostOffloadInfo(SB
->getAssociatedOffloadKind(),
3853 /*BoundArch=*/nullptr);
3858 /// Processes the host linker action. This currently consists of replacing it
3859 /// with an offload action if there are device link objects and propagate to
3860 /// the host action all the offload kinds used in the current compilation. The
3861 /// resulting action is returned.
3862 Action
*processHostLinkAction(Action
*HostAction
) {
3863 // Add all the dependences from the device linking actions.
3864 OffloadAction::DeviceDependences DDeps
;
3865 for (auto *SB
: SpecializedBuilders
) {
3869 SB
->appendLinkDependences(DDeps
);
3872 // Calculate all the offload kinds used in the current compilation.
3873 unsigned ActiveOffloadKinds
= 0u;
3874 for (auto &I
: InputArgToOffloadKindMap
)
3875 ActiveOffloadKinds
|= I
.second
;
3877 // If we don't have device dependencies, we don't have to create an offload
3879 if (DDeps
.getActions().empty()) {
3880 // Set all the active offloading kinds to the link action. Given that it
3881 // is a link action it is assumed to depend on all actions generated so
3883 HostAction
->setHostOffloadInfo(ActiveOffloadKinds
,
3884 /*BoundArch=*/nullptr);
3885 // Propagate active offloading kinds for each input to the link action.
3886 // Each input may have different active offloading kind.
3887 for (auto *A
: HostAction
->inputs()) {
3888 auto ArgLoc
= HostActionToInputArgMap
.find(A
);
3889 if (ArgLoc
== HostActionToInputArgMap
.end())
3891 auto OFKLoc
= InputArgToOffloadKindMap
.find(ArgLoc
->second
);
3892 if (OFKLoc
== InputArgToOffloadKindMap
.end())
3894 A
->propagateHostOffloadInfo(OFKLoc
->second
, /*BoundArch=*/nullptr);
3899 // Create the offload action with all dependences. When an offload action
3900 // is created the kinds are propagated to the host action, so we don't have
3901 // to do that explicitly here.
3902 OffloadAction::HostDependence
HDep(
3903 *HostAction
, *C
.getSingleOffloadToolChain
<Action::OFK_Host
>(),
3904 /*BoundArch*/ nullptr, ActiveOffloadKinds
);
3905 return C
.MakeAction
<OffloadAction
>(HDep
, DDeps
);
3908 } // anonymous namespace.
3910 void Driver::handleArguments(Compilation
&C
, DerivedArgList
&Args
,
3911 const InputList
&Inputs
,
3912 ActionList
&Actions
) const {
3914 // Ignore /Yc/Yu if both /Yc and /Yu passed but with different filenames.
3915 Arg
*YcArg
= Args
.getLastArg(options::OPT__SLASH_Yc
);
3916 Arg
*YuArg
= Args
.getLastArg(options::OPT__SLASH_Yu
);
3917 if (YcArg
&& YuArg
&& strcmp(YcArg
->getValue(), YuArg
->getValue()) != 0) {
3918 Diag(clang::diag::warn_drv_ycyu_different_arg_clang_cl
);
3919 Args
.eraseArg(options::OPT__SLASH_Yc
);
3920 Args
.eraseArg(options::OPT__SLASH_Yu
);
3921 YcArg
= YuArg
= nullptr;
3923 if (YcArg
&& Inputs
.size() > 1) {
3924 Diag(clang::diag::warn_drv_yc_multiple_inputs_clang_cl
);
3925 Args
.eraseArg(options::OPT__SLASH_Yc
);
3930 phases::ID FinalPhase
= getFinalPhase(Args
, &FinalPhaseArg
);
3932 if (FinalPhase
== phases::Link
) {
3933 if (Args
.hasArgNoClaim(options::OPT_hipstdpar
)) {
3934 Args
.AddFlagArg(nullptr, getOpts().getOption(options::OPT_hip_link
));
3935 Args
.AddFlagArg(nullptr,
3936 getOpts().getOption(options::OPT_frtlib_add_rpath
));
3938 // Emitting LLVM while linking disabled except in HIPAMD Toolchain
3939 if (Args
.hasArg(options::OPT_emit_llvm
) && !Args
.hasArg(options::OPT_hip_link
))
3940 Diag(clang::diag::err_drv_emit_llvm_link
);
3941 if (IsCLMode() && LTOMode
!= LTOK_None
&&
3942 !Args
.getLastArgValue(options::OPT_fuse_ld_EQ
)
3943 .equals_insensitive("lld"))
3944 Diag(clang::diag::err_drv_lto_without_lld
);
3946 // If -dumpdir is not specified, give a default prefix derived from the link
3947 // output filename. For example, `clang -g -gsplit-dwarf a.c -o x` passes
3948 // `-dumpdir x-` to cc1. If -o is unspecified, use
3949 // stem(getDefaultImageName()) (usually stem("a.out") = "a").
3950 if (!Args
.hasArg(options::OPT_dumpdir
)) {
3951 Arg
*FinalOutput
= Args
.getLastArg(options::OPT_o
, options::OPT__SLASH_o
);
3952 Arg
*Arg
= Args
.MakeSeparateArg(
3953 nullptr, getOpts().getOption(options::OPT_dumpdir
),
3955 (FinalOutput
? FinalOutput
->getValue()
3956 : llvm::sys::path::stem(getDefaultImageName())) +
3963 if (FinalPhase
== phases::Preprocess
|| Args
.hasArg(options::OPT__SLASH_Y_
)) {
3964 // If only preprocessing or /Y- is used, all pch handling is disabled.
3965 // Rather than check for it everywhere, just remove clang-cl pch-related
3967 Args
.eraseArg(options::OPT__SLASH_Fp
);
3968 Args
.eraseArg(options::OPT__SLASH_Yc
);
3969 Args
.eraseArg(options::OPT__SLASH_Yu
);
3970 YcArg
= YuArg
= nullptr;
3973 unsigned LastPLSize
= 0;
3974 for (auto &I
: Inputs
) {
3975 types::ID InputType
= I
.first
;
3976 const Arg
*InputArg
= I
.second
;
3978 auto PL
= types::getCompilationPhases(InputType
);
3979 LastPLSize
= PL
.size();
3981 // If the first step comes after the final phase we are doing as part of
3982 // this compilation, warn the user about it.
3983 phases::ID InitialPhase
= PL
[0];
3984 if (InitialPhase
> FinalPhase
) {
3985 if (InputArg
->isClaimed())
3988 // Claim here to avoid the more general unused warning.
3991 // Suppress all unused style warnings with -Qunused-arguments
3992 if (Args
.hasArg(options::OPT_Qunused_arguments
))
3995 // Special case when final phase determined by binary name, rather than
3996 // by a command-line argument with a corresponding Arg.
3998 Diag(clang::diag::warn_drv_input_file_unused_by_cpp
)
3999 << InputArg
->getAsString(Args
) << getPhaseName(InitialPhase
);
4000 // Special case '-E' warning on a previously preprocessed file to make
4002 else if (InitialPhase
== phases::Compile
&&
4003 (Args
.getLastArg(options::OPT__SLASH_EP
,
4004 options::OPT__SLASH_P
) ||
4005 Args
.getLastArg(options::OPT_E
) ||
4006 Args
.getLastArg(options::OPT_M
, options::OPT_MM
)) &&
4007 getPreprocessedType(InputType
) == types::TY_INVALID
)
4008 Diag(clang::diag::warn_drv_preprocessed_input_file_unused
)
4009 << InputArg
->getAsString(Args
) << !!FinalPhaseArg
4010 << (FinalPhaseArg
? FinalPhaseArg
->getOption().getName() : "");
4012 Diag(clang::diag::warn_drv_input_file_unused
)
4013 << InputArg
->getAsString(Args
) << getPhaseName(InitialPhase
)
4015 << (FinalPhaseArg
? FinalPhaseArg
->getOption().getName() : "");
4020 // Add a separate precompile phase for the compile phase.
4021 if (FinalPhase
>= phases::Compile
) {
4022 const types::ID HeaderType
= lookupHeaderTypeForSourceType(InputType
);
4023 // Build the pipeline for the pch file.
4024 Action
*ClangClPch
= C
.MakeAction
<InputAction
>(*InputArg
, HeaderType
);
4025 for (phases::ID Phase
: types::getCompilationPhases(HeaderType
))
4026 ClangClPch
= ConstructPhaseAction(C
, Args
, Phase
, ClangClPch
);
4028 Actions
.push_back(ClangClPch
);
4029 // The driver currently exits after the first failed command. This
4030 // relies on that behavior, to make sure if the pch generation fails,
4031 // the main compilation won't run.
4032 // FIXME: If the main compilation fails, the PCH generation should
4033 // probably not be considered successful either.
4038 // If we are linking, claim any options which are obviously only used for
4040 // FIXME: Understand why the last Phase List length is used here.
4041 if (FinalPhase
== phases::Link
&& LastPLSize
== 1) {
4042 Args
.ClaimAllArgs(options::OPT_CompileOnly_Group
);
4043 Args
.ClaimAllArgs(options::OPT_cl_compile_Group
);
4047 void Driver::BuildActions(Compilation
&C
, DerivedArgList
&Args
,
4048 const InputList
&Inputs
, ActionList
&Actions
) const {
4049 llvm::PrettyStackTraceString
CrashInfo("Building compilation actions");
4051 if (!SuppressMissingInputWarning
&& Inputs
.empty()) {
4052 Diag(clang::diag::err_drv_no_input_files
);
4056 // Diagnose misuse of /Fo.
4057 if (Arg
*A
= Args
.getLastArg(options::OPT__SLASH_Fo
)) {
4058 StringRef V
= A
->getValue();
4059 if (Inputs
.size() > 1 && !V
.empty() &&
4060 !llvm::sys::path::is_separator(V
.back())) {
4061 // Check whether /Fo tries to name an output file for multiple inputs.
4062 Diag(clang::diag::err_drv_out_file_argument_with_multiple_sources
)
4063 << A
->getSpelling() << V
;
4064 Args
.eraseArg(options::OPT__SLASH_Fo
);
4068 // Diagnose misuse of /Fa.
4069 if (Arg
*A
= Args
.getLastArg(options::OPT__SLASH_Fa
)) {
4070 StringRef V
= A
->getValue();
4071 if (Inputs
.size() > 1 && !V
.empty() &&
4072 !llvm::sys::path::is_separator(V
.back())) {
4073 // Check whether /Fa tries to name an asm file for multiple inputs.
4074 Diag(clang::diag::err_drv_out_file_argument_with_multiple_sources
)
4075 << A
->getSpelling() << V
;
4076 Args
.eraseArg(options::OPT__SLASH_Fa
);
4080 // Diagnose misuse of /o.
4081 if (Arg
*A
= Args
.getLastArg(options::OPT__SLASH_o
)) {
4082 if (A
->getValue()[0] == '\0') {
4083 // It has to have a value.
4084 Diag(clang::diag::err_drv_missing_argument
) << A
->getSpelling() << 1;
4085 Args
.eraseArg(options::OPT__SLASH_o
);
4089 handleArguments(C
, Args
, Inputs
, Actions
);
4091 bool UseNewOffloadingDriver
=
4092 C
.isOffloadingHostKind(Action::OFK_OpenMP
) ||
4093 Args
.hasFlag(options::OPT_offload_new_driver
,
4094 options::OPT_no_offload_new_driver
, false);
4096 // Builder to be used to build offloading actions.
4097 std::unique_ptr
<OffloadingActionBuilder
> OffloadBuilder
=
4098 !UseNewOffloadingDriver
4099 ? std::make_unique
<OffloadingActionBuilder
>(C
, Args
, Inputs
)
4102 // Construct the actions to perform.
4103 ExtractAPIJobAction
*ExtractAPIAction
= nullptr;
4104 ActionList LinkerInputs
;
4105 ActionList MergerInputs
;
4107 for (auto &I
: Inputs
) {
4108 types::ID InputType
= I
.first
;
4109 const Arg
*InputArg
= I
.second
;
4111 auto PL
= types::getCompilationPhases(*this, Args
, InputType
);
4115 auto FullPL
= types::getCompilationPhases(InputType
);
4117 // Build the pipeline for this file.
4118 Action
*Current
= C
.MakeAction
<InputAction
>(*InputArg
, InputType
);
4120 // Use the current host action in any of the offloading actions, if
4122 if (!UseNewOffloadingDriver
)
4123 if (OffloadBuilder
->addHostDependenceToDeviceActions(Current
, InputArg
))
4126 for (phases::ID Phase
: PL
) {
4128 // Add any offload action the host action depends on.
4129 if (!UseNewOffloadingDriver
)
4130 Current
= OffloadBuilder
->addDeviceDependencesToHostAction(
4131 Current
, InputArg
, Phase
, PL
.back(), FullPL
);
4135 // Queue linker inputs.
4136 if (Phase
== phases::Link
) {
4137 assert(Phase
== PL
.back() && "linking must be final compilation step.");
4138 // We don't need to generate additional link commands if emitting AMD
4139 // bitcode or compiling only for the offload device
4140 if (!(C
.getInputArgs().hasArg(options::OPT_hip_link
) &&
4141 (C
.getInputArgs().hasArg(options::OPT_emit_llvm
))) &&
4142 !offloadDeviceOnly())
4143 LinkerInputs
.push_back(Current
);
4148 // TODO: Consider removing this because the merged may not end up being
4149 // the final Phase in the pipeline. Perhaps the merged could just merge
4150 // and then pass an artifact of some sort to the Link Phase.
4151 // Queue merger inputs.
4152 if (Phase
== phases::IfsMerge
) {
4153 assert(Phase
== PL
.back() && "merging must be final compilation step.");
4154 MergerInputs
.push_back(Current
);
4159 if (Phase
== phases::Precompile
&& ExtractAPIAction
) {
4160 ExtractAPIAction
->addHeaderInput(Current
);
4165 // FIXME: Should we include any prior module file outputs as inputs of
4166 // later actions in the same command line?
4168 // Otherwise construct the appropriate action.
4169 Action
*NewCurrent
= ConstructPhaseAction(C
, Args
, Phase
, Current
);
4171 // We didn't create a new action, so we will just move to the next phase.
4172 if (NewCurrent
== Current
)
4175 if (auto *EAA
= dyn_cast
<ExtractAPIJobAction
>(NewCurrent
))
4176 ExtractAPIAction
= EAA
;
4178 Current
= NewCurrent
;
4180 // Try to build the offloading actions and add the result as a dependency
4182 if (UseNewOffloadingDriver
)
4183 Current
= BuildOffloadingActions(C
, Args
, I
, Current
);
4184 // Use the current host action in any of the offloading actions, if
4186 else if (OffloadBuilder
->addHostDependenceToDeviceActions(Current
,
4190 if (Current
->getType() == types::TY_Nothing
)
4194 // If we ended with something, add to the output list.
4196 Actions
.push_back(Current
);
4198 // Add any top level actions generated for offloading.
4199 if (!UseNewOffloadingDriver
)
4200 OffloadBuilder
->appendTopLevelActions(Actions
, Current
, InputArg
);
4202 Current
->propagateHostOffloadInfo(C
.getActiveOffloadKinds(),
4203 /*BoundArch=*/nullptr);
4206 // Add a link action if necessary.
4208 if (LinkerInputs
.empty()) {
4210 if (getFinalPhase(Args
, &FinalPhaseArg
) == phases::Link
)
4211 if (!UseNewOffloadingDriver
)
4212 OffloadBuilder
->appendDeviceLinkActions(Actions
);
4215 if (!LinkerInputs
.empty()) {
4216 if (!UseNewOffloadingDriver
)
4217 if (Action
*Wrapper
= OffloadBuilder
->makeHostLinkAction())
4218 LinkerInputs
.push_back(Wrapper
);
4220 // Check if this Linker Job should emit a static library.
4221 if (ShouldEmitStaticLibrary(Args
)) {
4222 LA
= C
.MakeAction
<StaticLibJobAction
>(LinkerInputs
, types::TY_Image
);
4223 } else if (UseNewOffloadingDriver
||
4224 Args
.hasArg(options::OPT_offload_link
)) {
4225 LA
= C
.MakeAction
<LinkerWrapperJobAction
>(LinkerInputs
, types::TY_Image
);
4226 LA
->propagateHostOffloadInfo(C
.getActiveOffloadKinds(),
4227 /*BoundArch=*/nullptr);
4229 LA
= C
.MakeAction
<LinkJobAction
>(LinkerInputs
, types::TY_Image
);
4231 if (!UseNewOffloadingDriver
)
4232 LA
= OffloadBuilder
->processHostLinkAction(LA
);
4233 Actions
.push_back(LA
);
4236 // Add an interface stubs merge action if necessary.
4237 if (!MergerInputs
.empty())
4239 C
.MakeAction
<IfsMergeJobAction
>(MergerInputs
, types::TY_Image
));
4241 if (Args
.hasArg(options::OPT_emit_interface_stubs
)) {
4242 auto PhaseList
= types::getCompilationPhases(
4244 Args
.hasArg(options::OPT_c
) ? phases::Compile
: phases::IfsMerge
);
4246 ActionList MergerInputs
;
4248 for (auto &I
: Inputs
) {
4249 types::ID InputType
= I
.first
;
4250 const Arg
*InputArg
= I
.second
;
4252 // Currently clang and the llvm assembler do not support generating symbol
4253 // stubs from assembly, so we skip the input on asm files. For ifs files
4254 // we rely on the normal pipeline setup in the pipeline setup code above.
4255 if (InputType
== types::TY_IFS
|| InputType
== types::TY_PP_Asm
||
4256 InputType
== types::TY_Asm
)
4259 Action
*Current
= C
.MakeAction
<InputAction
>(*InputArg
, InputType
);
4261 for (auto Phase
: PhaseList
) {
4265 "IFS Pipeline can only consist of Compile followed by IfsMerge.");
4266 case phases::Compile
: {
4267 // Only IfsMerge (llvm-ifs) can handle .o files by looking for ifs
4268 // files where the .o file is located. The compile action can not
4270 if (InputType
== types::TY_Object
)
4273 Current
= C
.MakeAction
<CompileJobAction
>(Current
, types::TY_IFS_CPP
);
4276 case phases::IfsMerge
: {
4277 assert(Phase
== PhaseList
.back() &&
4278 "merging must be final compilation step.");
4279 MergerInputs
.push_back(Current
);
4286 // If we ended with something, add to the output list.
4288 Actions
.push_back(Current
);
4291 // Add an interface stubs merge action if necessary.
4292 if (!MergerInputs
.empty())
4294 C
.MakeAction
<IfsMergeJobAction
>(MergerInputs
, types::TY_Image
));
4297 for (auto Opt
: {options::OPT_print_supported_cpus
,
4298 options::OPT_print_supported_extensions
}) {
4299 // If --print-supported-cpus, -mcpu=? or -mtune=? is specified, build a
4300 // custom Compile phase that prints out supported cpu models and quits.
4302 // If --print-supported-extensions is specified, call the helper function
4303 // RISCVMarchHelp in RISCVISAInfo.cpp that prints out supported extensions
4305 if (Arg
*A
= Args
.getLastArg(Opt
)) {
4306 if (Opt
== options::OPT_print_supported_extensions
&&
4307 !C
.getDefaultToolChain().getTriple().isRISCV() &&
4308 !C
.getDefaultToolChain().getTriple().isAArch64() &&
4309 !C
.getDefaultToolChain().getTriple().isARM()) {
4310 C
.getDriver().Diag(diag::err_opt_not_valid_on_target
)
4311 << "--print-supported-extensions";
4315 // Use the -mcpu=? flag as the dummy input to cc1.
4317 Action
*InputAc
= C
.MakeAction
<InputAction
>(*A
, types::TY_C
);
4319 C
.MakeAction
<PrecompileJobAction
>(InputAc
, types::TY_Nothing
));
4320 for (auto &I
: Inputs
)
4325 // Call validator for dxil when -Vd not in Args.
4326 if (C
.getDefaultToolChain().getTriple().isDXIL()) {
4327 // Only add action when needValidation.
4329 static_cast<const toolchains::HLSLToolChain
&>(C
.getDefaultToolChain());
4330 if (TC
.requiresValidation(Args
)) {
4331 Action
*LastAction
= Actions
.back();
4332 Actions
.push_back(C
.MakeAction
<BinaryAnalyzeJobAction
>(
4333 LastAction
, types::TY_DX_CONTAINER
));
4337 // Claim ignored clang-cl options.
4338 Args
.ClaimAllArgs(options::OPT_cl_ignored_Group
);
4341 /// Returns the canonical name for the offloading architecture when using a HIP
4342 /// or CUDA architecture.
4343 static StringRef
getCanonicalArchString(Compilation
&C
,
4344 const llvm::opt::DerivedArgList
&Args
,
4346 const llvm::Triple
&Triple
,
4347 bool SuppressError
= false) {
4348 // Lookup the CUDA / HIP architecture string. Only report an error if we were
4349 // expecting the triple to be only NVPTX / AMDGPU.
4350 CudaArch Arch
= StringToCudaArch(getProcessorFromTargetID(Triple
, ArchStr
));
4351 if (!SuppressError
&& Triple
.isNVPTX() &&
4352 (Arch
== CudaArch::UNKNOWN
|| !IsNVIDIAGpuArch(Arch
))) {
4353 C
.getDriver().Diag(clang::diag::err_drv_offload_bad_gpu_arch
)
4354 << "CUDA" << ArchStr
;
4356 } else if (!SuppressError
&& Triple
.isAMDGPU() &&
4357 (Arch
== CudaArch::UNKNOWN
|| !IsAMDGpuArch(Arch
))) {
4358 C
.getDriver().Diag(clang::diag::err_drv_offload_bad_gpu_arch
)
4359 << "HIP" << ArchStr
;
4363 if (IsNVIDIAGpuArch(Arch
))
4364 return Args
.MakeArgStringRef(CudaArchToString(Arch
));
4366 if (IsAMDGpuArch(Arch
)) {
4367 llvm::StringMap
<bool> Features
;
4368 auto HIPTriple
= getHIPOffloadTargetTriple(C
.getDriver(), C
.getInputArgs());
4371 auto Arch
= parseTargetID(*HIPTriple
, ArchStr
, &Features
);
4373 C
.getDriver().Diag(clang::diag::err_drv_bad_target_id
) << ArchStr
;
4374 C
.setContainsError();
4377 return Args
.MakeArgStringRef(getCanonicalTargetID(*Arch
, Features
));
4380 // If the input isn't CUDA or HIP just return the architecture.
4384 /// Checks if the set offloading architectures does not conflict. Returns the
4385 /// incompatible pair if a conflict occurs.
4386 static std::optional
<std::pair
<llvm::StringRef
, llvm::StringRef
>>
4387 getConflictOffloadArchCombination(const llvm::DenseSet
<StringRef
> &Archs
,
4388 llvm::Triple Triple
) {
4389 if (!Triple
.isAMDGPU())
4390 return std::nullopt
;
4392 std::set
<StringRef
> ArchSet
;
4393 llvm::copy(Archs
, std::inserter(ArchSet
, ArchSet
.begin()));
4394 return getConflictTargetIDCombination(ArchSet
);
4397 llvm::DenseSet
<StringRef
>
4398 Driver::getOffloadArchs(Compilation
&C
, const llvm::opt::DerivedArgList
&Args
,
4399 Action::OffloadKind Kind
, const ToolChain
*TC
,
4400 bool SuppressError
) const {
4402 TC
= &C
.getDefaultToolChain();
4404 // --offload and --offload-arch options are mutually exclusive.
4405 if (Args
.hasArgNoClaim(options::OPT_offload_EQ
) &&
4406 Args
.hasArgNoClaim(options::OPT_offload_arch_EQ
,
4407 options::OPT_no_offload_arch_EQ
)) {
4408 C
.getDriver().Diag(diag::err_opt_not_valid_with_opt
)
4410 << (Args
.hasArgNoClaim(options::OPT_offload_arch_EQ
)
4412 : "--no-offload-arch");
4415 if (KnownArchs
.contains(TC
))
4416 return KnownArchs
.lookup(TC
);
4418 llvm::DenseSet
<StringRef
> Archs
;
4419 for (auto *Arg
: Args
) {
4420 // Extract any '--[no-]offload-arch' arguments intended for this toolchain.
4421 std::unique_ptr
<llvm::opt::Arg
> ExtractedArg
= nullptr;
4422 if (Arg
->getOption().matches(options::OPT_Xopenmp_target_EQ
) &&
4423 ToolChain::getOpenMPTriple(Arg
->getValue(0)) == TC
->getTriple()) {
4425 unsigned Index
= Args
.getBaseArgs().MakeIndex(Arg
->getValue(1));
4426 ExtractedArg
= getOpts().ParseOneArg(Args
, Index
);
4427 Arg
= ExtractedArg
.get();
4430 // Add or remove the seen architectures in order of appearance. If an
4431 // invalid architecture is given we simply exit.
4432 if (Arg
->getOption().matches(options::OPT_offload_arch_EQ
)) {
4433 for (StringRef Arch
: llvm::split(Arg
->getValue(), ",")) {
4434 if (Arch
== "native" || Arch
.empty()) {
4435 auto GPUsOrErr
= TC
->getSystemGPUArchs(Args
);
4438 llvm::consumeError(GPUsOrErr
.takeError());
4440 TC
->getDriver().Diag(diag::err_drv_undetermined_gpu_arch
)
4441 << llvm::Triple::getArchTypeName(TC
->getArch())
4442 << llvm::toString(GPUsOrErr
.takeError()) << "--offload-arch";
4446 for (auto ArchStr
: *GPUsOrErr
) {
4448 getCanonicalArchString(C
, Args
, Args
.MakeArgString(ArchStr
),
4449 TC
->getTriple(), SuppressError
));
4452 StringRef ArchStr
= getCanonicalArchString(
4453 C
, Args
, Arch
, TC
->getTriple(), SuppressError
);
4454 if (ArchStr
.empty())
4456 Archs
.insert(ArchStr
);
4459 } else if (Arg
->getOption().matches(options::OPT_no_offload_arch_EQ
)) {
4460 for (StringRef Arch
: llvm::split(Arg
->getValue(), ",")) {
4461 if (Arch
== "all") {
4464 StringRef ArchStr
= getCanonicalArchString(
4465 C
, Args
, Arch
, TC
->getTriple(), SuppressError
);
4466 if (ArchStr
.empty())
4468 Archs
.erase(ArchStr
);
4474 if (auto ConflictingArchs
=
4475 getConflictOffloadArchCombination(Archs
, TC
->getTriple())) {
4476 C
.getDriver().Diag(clang::diag::err_drv_bad_offload_arch_combo
)
4477 << ConflictingArchs
->first
<< ConflictingArchs
->second
;
4478 C
.setContainsError();
4481 // Skip filling defaults if we're just querying what is availible.
4485 if (Archs
.empty()) {
4486 if (Kind
== Action::OFK_Cuda
)
4487 Archs
.insert(CudaArchToString(CudaArch::CudaDefault
));
4488 else if (Kind
== Action::OFK_HIP
)
4489 Archs
.insert(CudaArchToString(CudaArch::HIPDefault
));
4490 else if (Kind
== Action::OFK_OpenMP
)
4491 Archs
.insert(StringRef());
4493 Args
.ClaimAllArgs(options::OPT_offload_arch_EQ
);
4494 Args
.ClaimAllArgs(options::OPT_no_offload_arch_EQ
);
4500 Action
*Driver::BuildOffloadingActions(Compilation
&C
,
4501 llvm::opt::DerivedArgList
&Args
,
4502 const InputTy
&Input
,
4503 Action
*HostAction
) const {
4504 // Don't build offloading actions if explicitly disabled or we do not have a
4505 // valid source input and compile action to embed it in. If preprocessing only
4506 // ignore embedding.
4507 if (offloadHostOnly() || !types::isSrcFile(Input
.first
) ||
4508 !(isa
<CompileJobAction
>(HostAction
) ||
4509 getFinalPhase(Args
) == phases::Preprocess
))
4512 ActionList OffloadActions
;
4513 OffloadAction::DeviceDependences DDeps
;
4515 const Action::OffloadKind OffloadKinds
[] = {
4516 Action::OFK_OpenMP
, Action::OFK_Cuda
, Action::OFK_HIP
};
4518 for (Action::OffloadKind Kind
: OffloadKinds
) {
4519 SmallVector
<const ToolChain
*, 2> ToolChains
;
4520 ActionList DeviceActions
;
4522 auto TCRange
= C
.getOffloadToolChains(Kind
);
4523 for (auto TI
= TCRange
.first
, TE
= TCRange
.second
; TI
!= TE
; ++TI
)
4524 ToolChains
.push_back(TI
->second
);
4526 if (ToolChains
.empty())
4529 types::ID InputType
= Input
.first
;
4530 const Arg
*InputArg
= Input
.second
;
4532 // The toolchain can be active for unsupported file types.
4533 if ((Kind
== Action::OFK_Cuda
&& !types::isCuda(InputType
)) ||
4534 (Kind
== Action::OFK_HIP
&& !types::isHIP(InputType
)))
4537 // Get the product of all bound architectures and toolchains.
4538 SmallVector
<std::pair
<const ToolChain
*, StringRef
>> TCAndArchs
;
4539 for (const ToolChain
*TC
: ToolChains
)
4540 for (StringRef Arch
: getOffloadArchs(C
, Args
, Kind
, TC
))
4541 TCAndArchs
.push_back(std::make_pair(TC
, Arch
));
4543 for (unsigned I
= 0, E
= TCAndArchs
.size(); I
!= E
; ++I
)
4544 DeviceActions
.push_back(C
.MakeAction
<InputAction
>(*InputArg
, InputType
));
4546 if (DeviceActions
.empty())
4549 auto PL
= types::getCompilationPhases(*this, Args
, InputType
);
4551 for (phases::ID Phase
: PL
) {
4552 if (Phase
== phases::Link
) {
4553 assert(Phase
== PL
.back() && "linking must be final compilation step.");
4557 auto TCAndArch
= TCAndArchs
.begin();
4558 for (Action
*&A
: DeviceActions
) {
4559 if (A
->getType() == types::TY_Nothing
)
4562 // Propagate the ToolChain so we can use it in ConstructPhaseAction.
4563 A
->propagateDeviceOffloadInfo(Kind
, TCAndArch
->second
.data(),
4565 A
= ConstructPhaseAction(C
, Args
, Phase
, A
, Kind
);
4567 if (isa
<CompileJobAction
>(A
) && isa
<CompileJobAction
>(HostAction
) &&
4568 Kind
== Action::OFK_OpenMP
&&
4569 HostAction
->getType() != types::TY_Nothing
) {
4570 // OpenMP offloading has a dependency on the host compile action to
4571 // identify which declarations need to be emitted. This shouldn't be
4572 // collapsed with any other actions so we can use it in the device.
4573 HostAction
->setCannotBeCollapsedWithNextDependentAction();
4574 OffloadAction::HostDependence
HDep(
4575 *HostAction
, *C
.getSingleOffloadToolChain
<Action::OFK_Host
>(),
4576 TCAndArch
->second
.data(), Kind
);
4577 OffloadAction::DeviceDependences DDep
;
4578 DDep
.add(*A
, *TCAndArch
->first
, TCAndArch
->second
.data(), Kind
);
4579 A
= C
.MakeAction
<OffloadAction
>(HDep
, DDep
);
4586 // Compiling HIP in non-RDC mode requires linking each action individually.
4587 for (Action
*&A
: DeviceActions
) {
4588 if ((A
->getType() != types::TY_Object
&&
4589 A
->getType() != types::TY_LTO_BC
) ||
4590 Kind
!= Action::OFK_HIP
||
4591 Args
.hasFlag(options::OPT_fgpu_rdc
, options::OPT_fno_gpu_rdc
, false))
4593 ActionList LinkerInput
= {A
};
4594 A
= C
.MakeAction
<LinkJobAction
>(LinkerInput
, types::TY_Image
);
4597 auto TCAndArch
= TCAndArchs
.begin();
4598 for (Action
*A
: DeviceActions
) {
4599 DDeps
.add(*A
, *TCAndArch
->first
, TCAndArch
->second
.data(), Kind
);
4600 OffloadAction::DeviceDependences DDep
;
4601 DDep
.add(*A
, *TCAndArch
->first
, TCAndArch
->second
.data(), Kind
);
4602 OffloadActions
.push_back(C
.MakeAction
<OffloadAction
>(DDep
, A
->getType()));
4607 if (offloadDeviceOnly())
4608 return C
.MakeAction
<OffloadAction
>(DDeps
, types::TY_Nothing
);
4610 if (OffloadActions
.empty())
4613 OffloadAction::DeviceDependences DDep
;
4614 if (C
.isOffloadingHostKind(Action::OFK_Cuda
) &&
4615 !Args
.hasFlag(options::OPT_fgpu_rdc
, options::OPT_fno_gpu_rdc
, false)) {
4616 // If we are not in RDC-mode we just emit the final CUDA fatbinary for
4617 // each translation unit without requiring any linking.
4618 Action
*FatbinAction
=
4619 C
.MakeAction
<LinkJobAction
>(OffloadActions
, types::TY_CUDA_FATBIN
);
4620 DDep
.add(*FatbinAction
, *C
.getSingleOffloadToolChain
<Action::OFK_Cuda
>(),
4621 nullptr, Action::OFK_Cuda
);
4622 } else if (C
.isOffloadingHostKind(Action::OFK_HIP
) &&
4623 !Args
.hasFlag(options::OPT_fgpu_rdc
, options::OPT_fno_gpu_rdc
,
4625 // If we are not in RDC-mode we just emit the final HIP fatbinary for each
4626 // translation unit, linking each input individually.
4627 Action
*FatbinAction
=
4628 C
.MakeAction
<LinkJobAction
>(OffloadActions
, types::TY_HIP_FATBIN
);
4629 DDep
.add(*FatbinAction
, *C
.getSingleOffloadToolChain
<Action::OFK_HIP
>(),
4630 nullptr, Action::OFK_HIP
);
4632 // Package all the offloading actions into a single output that can be
4633 // embedded in the host and linked.
4634 Action
*PackagerAction
=
4635 C
.MakeAction
<OffloadPackagerJobAction
>(OffloadActions
, types::TY_Image
);
4636 DDep
.add(*PackagerAction
, *C
.getSingleOffloadToolChain
<Action::OFK_Host
>(),
4637 nullptr, C
.getActiveOffloadKinds());
4640 // If we are unable to embed a single device output into the host, we need to
4641 // add each device output as a host dependency to ensure they are still built.
4642 bool SingleDeviceOutput
= !llvm::any_of(OffloadActions
, [](Action
*A
) {
4643 return A
->getType() == types::TY_Nothing
;
4644 }) && isa
<CompileJobAction
>(HostAction
);
4645 OffloadAction::HostDependence
HDep(
4646 *HostAction
, *C
.getSingleOffloadToolChain
<Action::OFK_Host
>(),
4647 /*BoundArch=*/nullptr, SingleDeviceOutput
? DDep
: DDeps
);
4648 return C
.MakeAction
<OffloadAction
>(HDep
, SingleDeviceOutput
? DDep
: DDeps
);
4651 Action
*Driver::ConstructPhaseAction(
4652 Compilation
&C
, const ArgList
&Args
, phases::ID Phase
, Action
*Input
,
4653 Action::OffloadKind TargetDeviceOffloadKind
) const {
4654 llvm::PrettyStackTraceString
CrashInfo("Constructing phase actions");
4656 // Some types skip the assembler phase (e.g., llvm-bc), but we can't
4657 // encode this in the steps because the intermediate type depends on
4658 // arguments. Just special case here.
4659 if (Phase
== phases::Assemble
&& Input
->getType() != types::TY_PP_Asm
)
4662 // Build the appropriate action.
4665 llvm_unreachable("link action invalid here.");
4666 case phases::IfsMerge
:
4667 llvm_unreachable("ifsmerge action invalid here.");
4668 case phases::Preprocess
: {
4670 // -M and -MM specify the dependency file name by altering the output type,
4671 // -if -MD and -MMD are not specified.
4672 if (Args
.hasArg(options::OPT_M
, options::OPT_MM
) &&
4673 !Args
.hasArg(options::OPT_MD
, options::OPT_MMD
)) {
4674 OutputTy
= types::TY_Dependencies
;
4676 OutputTy
= Input
->getType();
4677 // For these cases, the preprocessor is only translating forms, the Output
4678 // still needs preprocessing.
4679 if (!Args
.hasFlag(options::OPT_frewrite_includes
,
4680 options::OPT_fno_rewrite_includes
, false) &&
4681 !Args
.hasFlag(options::OPT_frewrite_imports
,
4682 options::OPT_fno_rewrite_imports
, false) &&
4683 !Args
.hasFlag(options::OPT_fdirectives_only
,
4684 options::OPT_fno_directives_only
, false) &&
4686 OutputTy
= types::getPreprocessedType(OutputTy
);
4687 assert(OutputTy
!= types::TY_INVALID
&&
4688 "Cannot preprocess this input type!");
4690 return C
.MakeAction
<PreprocessJobAction
>(Input
, OutputTy
);
4692 case phases::Precompile
: {
4693 // API extraction should not generate an actual precompilation action.
4694 if (Args
.hasArg(options::OPT_extract_api
))
4695 return C
.MakeAction
<ExtractAPIJobAction
>(Input
, types::TY_API_INFO
);
4697 types::ID OutputTy
= getPrecompiledType(Input
->getType());
4698 assert(OutputTy
!= types::TY_INVALID
&&
4699 "Cannot precompile this input type!");
4701 // If we're given a module name, precompile header file inputs as a
4702 // module, not as a precompiled header.
4703 const char *ModName
= nullptr;
4704 if (OutputTy
== types::TY_PCH
) {
4705 if (Arg
*A
= Args
.getLastArg(options::OPT_fmodule_name_EQ
))
4706 ModName
= A
->getValue();
4708 OutputTy
= types::TY_ModuleFile
;
4711 if (Args
.hasArg(options::OPT_fsyntax_only
)) {
4712 // Syntax checks should not emit a PCH file
4713 OutputTy
= types::TY_Nothing
;
4716 return C
.MakeAction
<PrecompileJobAction
>(Input
, OutputTy
);
4718 case phases::Compile
: {
4719 if (Args
.hasArg(options::OPT_fsyntax_only
))
4720 return C
.MakeAction
<CompileJobAction
>(Input
, types::TY_Nothing
);
4721 if (Args
.hasArg(options::OPT_rewrite_objc
))
4722 return C
.MakeAction
<CompileJobAction
>(Input
, types::TY_RewrittenObjC
);
4723 if (Args
.hasArg(options::OPT_rewrite_legacy_objc
))
4724 return C
.MakeAction
<CompileJobAction
>(Input
,
4725 types::TY_RewrittenLegacyObjC
);
4726 if (Args
.hasArg(options::OPT__analyze
))
4727 return C
.MakeAction
<AnalyzeJobAction
>(Input
, types::TY_Plist
);
4728 if (Args
.hasArg(options::OPT__migrate
))
4729 return C
.MakeAction
<MigrateJobAction
>(Input
, types::TY_Remap
);
4730 if (Args
.hasArg(options::OPT_emit_ast
))
4731 return C
.MakeAction
<CompileJobAction
>(Input
, types::TY_AST
);
4732 if (Args
.hasArg(options::OPT_module_file_info
))
4733 return C
.MakeAction
<CompileJobAction
>(Input
, types::TY_ModuleFile
);
4734 if (Args
.hasArg(options::OPT_verify_pch
))
4735 return C
.MakeAction
<VerifyPCHJobAction
>(Input
, types::TY_Nothing
);
4736 if (Args
.hasArg(options::OPT_extract_api
))
4737 return C
.MakeAction
<ExtractAPIJobAction
>(Input
, types::TY_API_INFO
);
4738 return C
.MakeAction
<CompileJobAction
>(Input
, types::TY_LLVM_BC
);
4740 case phases::Backend
: {
4741 if (isUsingLTO() && TargetDeviceOffloadKind
== Action::OFK_None
) {
4743 if (Args
.hasArg(options::OPT_S
))
4744 Output
= types::TY_LTO_IR
;
4745 else if (Args
.hasArg(options::OPT_ffat_lto_objects
))
4746 Output
= types::TY_PP_Asm
;
4748 Output
= types::TY_LTO_BC
;
4749 return C
.MakeAction
<BackendJobAction
>(Input
, Output
);
4751 if (isUsingLTO(/* IsOffload */ true) &&
4752 TargetDeviceOffloadKind
!= Action::OFK_None
) {
4754 Args
.hasArg(options::OPT_S
) ? types::TY_LTO_IR
: types::TY_LTO_BC
;
4755 return C
.MakeAction
<BackendJobAction
>(Input
, Output
);
4757 if (Args
.hasArg(options::OPT_emit_llvm
) ||
4758 (((Input
->getOffloadingToolChain() &&
4759 Input
->getOffloadingToolChain()->getTriple().isAMDGPU()) ||
4760 TargetDeviceOffloadKind
== Action::OFK_HIP
) &&
4761 (Args
.hasFlag(options::OPT_fgpu_rdc
, options::OPT_fno_gpu_rdc
,
4763 TargetDeviceOffloadKind
== Action::OFK_OpenMP
))) {
4765 Args
.hasArg(options::OPT_S
) &&
4766 (TargetDeviceOffloadKind
== Action::OFK_None
||
4767 offloadDeviceOnly() ||
4768 (TargetDeviceOffloadKind
== Action::OFK_HIP
&&
4769 !Args
.hasFlag(options::OPT_offload_new_driver
,
4770 options::OPT_no_offload_new_driver
, false)))
4772 : types::TY_LLVM_BC
;
4773 return C
.MakeAction
<BackendJobAction
>(Input
, Output
);
4775 return C
.MakeAction
<BackendJobAction
>(Input
, types::TY_PP_Asm
);
4777 case phases::Assemble
:
4778 return C
.MakeAction
<AssembleJobAction
>(std::move(Input
), types::TY_Object
);
4781 llvm_unreachable("invalid phase in ConstructPhaseAction");
4784 void Driver::BuildJobs(Compilation
&C
) const {
4785 llvm::PrettyStackTraceString
CrashInfo("Building compilation jobs");
4787 Arg
*FinalOutput
= C
.getArgs().getLastArg(options::OPT_o
);
4789 // It is an error to provide a -o option if we are making multiple output
4790 // files. There are exceptions:
4792 // IfsMergeJob: when generating interface stubs enabled we want to be able to
4793 // generate the stub file at the same time that we generate the real
4794 // library/a.out. So when a .o, .so, etc are the output, with clang interface
4795 // stubs there will also be a .ifs and .ifso at the same location.
4797 // CompileJob of type TY_IFS_CPP: when generating interface stubs is enabled
4798 // and -c is passed, we still want to be able to generate a .ifs file while
4799 // we are also generating .o files. So we allow more than one output file in
4800 // this case as well.
4802 // OffloadClass of type TY_Nothing: device-only output will place many outputs
4803 // into a single offloading action. We should count all inputs to the action
4804 // as outputs. Also ignore device-only outputs if we're compiling with
4807 unsigned NumOutputs
= 0;
4808 unsigned NumIfsOutputs
= 0;
4809 for (const Action
*A
: C
.getActions()) {
4810 if (A
->getType() != types::TY_Nothing
&&
4811 A
->getType() != types::TY_DX_CONTAINER
&&
4812 !(A
->getKind() == Action::IfsMergeJobClass
||
4813 (A
->getType() == clang::driver::types::TY_IFS_CPP
&&
4814 A
->getKind() == clang::driver::Action::CompileJobClass
&&
4815 0 == NumIfsOutputs
++) ||
4816 (A
->getKind() == Action::BindArchClass
&& A
->getInputs().size() &&
4817 A
->getInputs().front()->getKind() == Action::IfsMergeJobClass
)))
4819 else if (A
->getKind() == Action::OffloadClass
&&
4820 A
->getType() == types::TY_Nothing
&&
4821 !C
.getArgs().hasArg(options::OPT_fsyntax_only
))
4822 NumOutputs
+= A
->size();
4825 if (NumOutputs
> 1) {
4826 Diag(clang::diag::err_drv_output_argument_with_multiple_files
);
4827 FinalOutput
= nullptr;
4831 const llvm::Triple
&RawTriple
= C
.getDefaultToolChain().getTriple();
4833 // Collect the list of architectures.
4834 llvm::StringSet
<> ArchNames
;
4835 if (RawTriple
.isOSBinFormatMachO())
4836 for (const Arg
*A
: C
.getArgs())
4837 if (A
->getOption().matches(options::OPT_arch
))
4838 ArchNames
.insert(A
->getValue());
4840 // Set of (Action, canonical ToolChain triple) pairs we've built jobs for.
4841 std::map
<std::pair
<const Action
*, std::string
>, InputInfoList
> CachedResults
;
4842 for (Action
*A
: C
.getActions()) {
4843 // If we are linking an image for multiple archs then the linker wants
4844 // -arch_multiple and -final_output <final image name>. Unfortunately, this
4845 // doesn't fit in cleanly because we have to pass this information down.
4847 // FIXME: This is a hack; find a cleaner way to integrate this into the
4849 const char *LinkingOutput
= nullptr;
4850 if (isa
<LipoJobAction
>(A
)) {
4852 LinkingOutput
= FinalOutput
->getValue();
4854 LinkingOutput
= getDefaultImageName();
4857 BuildJobsForAction(C
, A
, &C
.getDefaultToolChain(),
4858 /*BoundArch*/ StringRef(),
4859 /*AtTopLevel*/ true,
4860 /*MultipleArchs*/ ArchNames
.size() > 1,
4861 /*LinkingOutput*/ LinkingOutput
, CachedResults
,
4862 /*TargetDeviceOffloadKind*/ Action::OFK_None
);
4865 // If we have more than one job, then disable integrated-cc1 for now. Do this
4866 // also when we need to report process execution statistics.
4867 if (C
.getJobs().size() > 1 || CCPrintProcessStats
)
4868 for (auto &J
: C
.getJobs())
4869 J
.InProcess
= false;
4871 if (CCPrintProcessStats
) {
4872 C
.setPostCallback([=](const Command
&Cmd
, int Res
) {
4873 std::optional
<llvm::sys::ProcessStatistics
> ProcStat
=
4874 Cmd
.getProcessStatistics();
4878 const char *LinkingOutput
= nullptr;
4880 LinkingOutput
= FinalOutput
->getValue();
4881 else if (!Cmd
.getOutputFilenames().empty())
4882 LinkingOutput
= Cmd
.getOutputFilenames().front().c_str();
4884 LinkingOutput
= getDefaultImageName();
4886 if (CCPrintStatReportFilename
.empty()) {
4887 using namespace llvm
;
4888 // Human readable output.
4889 outs() << sys::path::filename(Cmd
.getExecutable()) << ": "
4890 << "output=" << LinkingOutput
;
4891 outs() << ", total="
4892 << format("%.3f", ProcStat
->TotalTime
.count() / 1000.) << " ms"
4894 << format("%.3f", ProcStat
->UserTime
.count() / 1000.) << " ms"
4895 << ", mem=" << ProcStat
->PeakMemory
<< " Kb\n";
4899 llvm::raw_string_ostream
Out(Buffer
);
4900 llvm::sys::printArg(Out
, llvm::sys::path::filename(Cmd
.getExecutable()),
4903 llvm::sys::printArg(Out
, LinkingOutput
, true);
4904 Out
<< ',' << ProcStat
->TotalTime
.count() << ','
4905 << ProcStat
->UserTime
.count() << ',' << ProcStat
->PeakMemory
4909 llvm::raw_fd_ostream
OS(CCPrintStatReportFilename
, EC
,
4910 llvm::sys::fs::OF_Append
|
4911 llvm::sys::fs::OF_Text
);
4916 llvm::errs() << "ERROR: Cannot lock file "
4917 << CCPrintStatReportFilename
<< ": "
4918 << toString(L
.takeError()) << "\n";
4927 // If the user passed -Qunused-arguments or there were errors, don't warn
4928 // about any unused arguments.
4929 if (Diags
.hasErrorOccurred() ||
4930 C
.getArgs().hasArg(options::OPT_Qunused_arguments
))
4933 // Claim -fdriver-only here.
4934 (void)C
.getArgs().hasArg(options::OPT_fdriver_only
);
4936 (void)C
.getArgs().hasArg(options::OPT__HASH_HASH_HASH
);
4938 // Claim --driver-mode, --rsp-quoting, it was handled earlier.
4939 (void)C
.getArgs().hasArg(options::OPT_driver_mode
);
4940 (void)C
.getArgs().hasArg(options::OPT_rsp_quoting
);
4942 bool HasAssembleJob
= llvm::any_of(C
.getJobs(), [](auto &J
) {
4943 // Match ClangAs and other derived assemblers of Tool. ClangAs uses a
4944 // longer ShortName "clang integrated assembler" while other assemblers just
4946 return strstr(J
.getCreator().getShortName(), "assembler");
4948 for (Arg
*A
: C
.getArgs()) {
4949 // FIXME: It would be nice to be able to send the argument to the
4950 // DiagnosticsEngine, so that extra values, position, and so on could be
4952 if (!A
->isClaimed()) {
4953 if (A
->getOption().hasFlag(options::NoArgumentUnused
))
4956 // Suppress the warning automatically if this is just a flag, and it is an
4957 // instance of an argument we already claimed.
4958 const Option
&Opt
= A
->getOption();
4959 if (Opt
.getKind() == Option::FlagClass
) {
4960 bool DuplicateClaimed
= false;
4962 for (const Arg
*AA
: C
.getArgs().filtered(&Opt
)) {
4963 if (AA
->isClaimed()) {
4964 DuplicateClaimed
= true;
4969 if (DuplicateClaimed
)
4973 // In clang-cl, don't mention unknown arguments here since they have
4974 // already been warned about.
4975 if (!IsCLMode() || !A
->getOption().matches(options::OPT_UNKNOWN
)) {
4976 if (A
->getOption().hasFlag(options::TargetSpecific
) &&
4977 !A
->isIgnoredTargetSpecific() && !HasAssembleJob
&&
4978 // When for example -### or -v is used
4979 // without a file, target specific options are not
4980 // consumed/validated.
4981 // Instead emitting an error emit a warning instead.
4982 !C
.getActions().empty()) {
4983 Diag(diag::err_drv_unsupported_opt_for_target
)
4984 << A
->getSpelling() << getTargetTriple();
4986 Diag(clang::diag::warn_drv_unused_argument
)
4987 << A
->getAsString(C
.getArgs());
4995 /// Utility class to control the collapse of dependent actions and select the
4996 /// tools accordingly.
4997 class ToolSelector final
{
4998 /// The tool chain this selector refers to.
4999 const ToolChain
&TC
;
5001 /// The compilation this selector refers to.
5002 const Compilation
&C
;
5004 /// The base action this selector refers to.
5005 const JobAction
*BaseAction
;
5007 /// Set to true if the current toolchain refers to host actions.
5008 bool IsHostSelector
;
5010 /// Set to true if save-temps and embed-bitcode functionalities are active.
5014 /// Get previous dependent action or null if that does not exist. If
5015 /// \a CanBeCollapsed is false, that action must be legal to collapse or
5016 /// null will be returned.
5017 const JobAction
*getPrevDependentAction(const ActionList
&Inputs
,
5018 ActionList
&SavedOffloadAction
,
5019 bool CanBeCollapsed
= true) {
5020 // An option can be collapsed only if it has a single input.
5021 if (Inputs
.size() != 1)
5024 Action
*CurAction
= *Inputs
.begin();
5025 if (CanBeCollapsed
&&
5026 !CurAction
->isCollapsingWithNextDependentActionLegal())
5029 // If the input action is an offload action. Look through it and save any
5030 // offload action that can be dropped in the event of a collapse.
5031 if (auto *OA
= dyn_cast
<OffloadAction
>(CurAction
)) {
5032 // If the dependent action is a device action, we will attempt to collapse
5033 // only with other device actions. Otherwise, we would do the same but
5034 // with host actions only.
5035 if (!IsHostSelector
) {
5036 if (OA
->hasSingleDeviceDependence(/*DoNotConsiderHostActions=*/true)) {
5038 OA
->getSingleDeviceDependence(/*DoNotConsiderHostActions=*/true);
5039 if (CanBeCollapsed
&&
5040 !CurAction
->isCollapsingWithNextDependentActionLegal())
5042 SavedOffloadAction
.push_back(OA
);
5043 return dyn_cast
<JobAction
>(CurAction
);
5045 } else if (OA
->hasHostDependence()) {
5046 CurAction
= OA
->getHostDependence();
5047 if (CanBeCollapsed
&&
5048 !CurAction
->isCollapsingWithNextDependentActionLegal())
5050 SavedOffloadAction
.push_back(OA
);
5051 return dyn_cast
<JobAction
>(CurAction
);
5056 return dyn_cast
<JobAction
>(CurAction
);
5059 /// Return true if an assemble action can be collapsed.
5060 bool canCollapseAssembleAction() const {
5061 return TC
.useIntegratedAs() && !SaveTemps
&&
5062 !C
.getArgs().hasArg(options::OPT_via_file_asm
) &&
5063 !C
.getArgs().hasArg(options::OPT__SLASH_FA
) &&
5064 !C
.getArgs().hasArg(options::OPT__SLASH_Fa
) &&
5065 !C
.getArgs().hasArg(options::OPT_dxc_Fc
);
5068 /// Return true if a preprocessor action can be collapsed.
5069 bool canCollapsePreprocessorAction() const {
5070 return !C
.getArgs().hasArg(options::OPT_no_integrated_cpp
) &&
5071 !C
.getArgs().hasArg(options::OPT_traditional_cpp
) && !SaveTemps
&&
5072 !C
.getArgs().hasArg(options::OPT_rewrite_objc
);
5075 /// Struct that relates an action with the offload actions that would be
5076 /// collapsed with it.
5077 struct JobActionInfo final
{
5078 /// The action this info refers to.
5079 const JobAction
*JA
= nullptr;
5080 /// The offload actions we need to take care off if this action is
5082 ActionList SavedOffloadAction
;
5085 /// Append collapsed offload actions from the give nnumber of elements in the
5086 /// action info array.
5087 static void AppendCollapsedOffloadAction(ActionList
&CollapsedOffloadAction
,
5088 ArrayRef
<JobActionInfo
> &ActionInfo
,
5089 unsigned ElementNum
) {
5090 assert(ElementNum
<= ActionInfo
.size() && "Invalid number of elements.");
5091 for (unsigned I
= 0; I
< ElementNum
; ++I
)
5092 CollapsedOffloadAction
.append(ActionInfo
[I
].SavedOffloadAction
.begin(),
5093 ActionInfo
[I
].SavedOffloadAction
.end());
5096 /// Functions that attempt to perform the combining. They detect if that is
5097 /// legal, and if so they update the inputs \a Inputs and the offload action
5098 /// that were collapsed in \a CollapsedOffloadAction. A tool that deals with
5099 /// the combined action is returned. If the combining is not legal or if the
5100 /// tool does not exist, null is returned.
5101 /// Currently three kinds of collapsing are supported:
5102 /// - Assemble + Backend + Compile;
5103 /// - Assemble + Backend ;
5104 /// - Backend + Compile.
5106 combineAssembleBackendCompile(ArrayRef
<JobActionInfo
> ActionInfo
,
5108 ActionList
&CollapsedOffloadAction
) {
5109 if (ActionInfo
.size() < 3 || !canCollapseAssembleAction())
5111 auto *AJ
= dyn_cast
<AssembleJobAction
>(ActionInfo
[0].JA
);
5112 auto *BJ
= dyn_cast
<BackendJobAction
>(ActionInfo
[1].JA
);
5113 auto *CJ
= dyn_cast
<CompileJobAction
>(ActionInfo
[2].JA
);
5114 if (!AJ
|| !BJ
|| !CJ
)
5117 // Get compiler tool.
5118 const Tool
*T
= TC
.SelectTool(*CJ
);
5122 // Can't collapse if we don't have codegen support unless we are
5123 // emitting LLVM IR.
5124 bool OutputIsLLVM
= types::isLLVMIR(ActionInfo
[0].JA
->getType());
5125 if (!T
->hasIntegratedBackend() && !(OutputIsLLVM
&& T
->canEmitIR()))
5128 // When using -fembed-bitcode, it is required to have the same tool (clang)
5129 // for both CompilerJA and BackendJA. Otherwise, combine two stages.
5131 const Tool
*BT
= TC
.SelectTool(*BJ
);
5136 if (!T
->hasIntegratedAssembler())
5139 Inputs
= CJ
->getInputs();
5140 AppendCollapsedOffloadAction(CollapsedOffloadAction
, ActionInfo
,
5144 const Tool
*combineAssembleBackend(ArrayRef
<JobActionInfo
> ActionInfo
,
5146 ActionList
&CollapsedOffloadAction
) {
5147 if (ActionInfo
.size() < 2 || !canCollapseAssembleAction())
5149 auto *AJ
= dyn_cast
<AssembleJobAction
>(ActionInfo
[0].JA
);
5150 auto *BJ
= dyn_cast
<BackendJobAction
>(ActionInfo
[1].JA
);
5154 // Get backend tool.
5155 const Tool
*T
= TC
.SelectTool(*BJ
);
5159 if (!T
->hasIntegratedAssembler())
5162 Inputs
= BJ
->getInputs();
5163 AppendCollapsedOffloadAction(CollapsedOffloadAction
, ActionInfo
,
5167 const Tool
*combineBackendCompile(ArrayRef
<JobActionInfo
> ActionInfo
,
5169 ActionList
&CollapsedOffloadAction
) {
5170 if (ActionInfo
.size() < 2)
5172 auto *BJ
= dyn_cast
<BackendJobAction
>(ActionInfo
[0].JA
);
5173 auto *CJ
= dyn_cast
<CompileJobAction
>(ActionInfo
[1].JA
);
5177 // Check if the initial input (to the compile job or its predessor if one
5178 // exists) is LLVM bitcode. In that case, no preprocessor step is required
5179 // and we can still collapse the compile and backend jobs when we have
5180 // -save-temps. I.e. there is no need for a separate compile job just to
5181 // emit unoptimized bitcode.
5182 bool InputIsBitcode
= true;
5183 for (size_t i
= 1; i
< ActionInfo
.size(); i
++)
5184 if (ActionInfo
[i
].JA
->getType() != types::TY_LLVM_BC
&&
5185 ActionInfo
[i
].JA
->getType() != types::TY_LTO_BC
) {
5186 InputIsBitcode
= false;
5189 if (!InputIsBitcode
&& !canCollapsePreprocessorAction())
5192 // Get compiler tool.
5193 const Tool
*T
= TC
.SelectTool(*CJ
);
5197 // Can't collapse if we don't have codegen support unless we are
5198 // emitting LLVM IR.
5199 bool OutputIsLLVM
= types::isLLVMIR(ActionInfo
[0].JA
->getType());
5200 if (!T
->hasIntegratedBackend() && !(OutputIsLLVM
&& T
->canEmitIR()))
5203 if (T
->canEmitIR() && ((SaveTemps
&& !InputIsBitcode
) || EmbedBitcode
))
5206 Inputs
= CJ
->getInputs();
5207 AppendCollapsedOffloadAction(CollapsedOffloadAction
, ActionInfo
,
5212 /// Updates the inputs if the obtained tool supports combining with
5213 /// preprocessor action, and the current input is indeed a preprocessor
5214 /// action. If combining results in the collapse of offloading actions, those
5215 /// are appended to \a CollapsedOffloadAction.
5216 void combineWithPreprocessor(const Tool
*T
, ActionList
&Inputs
,
5217 ActionList
&CollapsedOffloadAction
) {
5218 if (!T
|| !canCollapsePreprocessorAction() || !T
->hasIntegratedCPP())
5221 // Attempt to get a preprocessor action dependence.
5222 ActionList PreprocessJobOffloadActions
;
5223 ActionList NewInputs
;
5224 for (Action
*A
: Inputs
) {
5225 auto *PJ
= getPrevDependentAction({A
}, PreprocessJobOffloadActions
);
5226 if (!PJ
|| !isa
<PreprocessJobAction
>(PJ
)) {
5227 NewInputs
.push_back(A
);
5231 // This is legal to combine. Append any offload action we found and add the
5232 // current input to preprocessor inputs.
5233 CollapsedOffloadAction
.append(PreprocessJobOffloadActions
.begin(),
5234 PreprocessJobOffloadActions
.end());
5235 NewInputs
.append(PJ
->input_begin(), PJ
->input_end());
5241 ToolSelector(const JobAction
*BaseAction
, const ToolChain
&TC
,
5242 const Compilation
&C
, bool SaveTemps
, bool EmbedBitcode
)
5243 : TC(TC
), C(C
), BaseAction(BaseAction
), SaveTemps(SaveTemps
),
5244 EmbedBitcode(EmbedBitcode
) {
5245 assert(BaseAction
&& "Invalid base action.");
5246 IsHostSelector
= BaseAction
->getOffloadingDeviceKind() == Action::OFK_None
;
5249 /// Check if a chain of actions can be combined and return the tool that can
5250 /// handle the combination of actions. The pointer to the current inputs \a
5251 /// Inputs and the list of offload actions \a CollapsedOffloadActions
5252 /// connected to collapsed actions are updated accordingly. The latter enables
5253 /// the caller of the selector to process them afterwards instead of just
5254 /// dropping them. If no suitable tool is found, null will be returned.
5255 const Tool
*getTool(ActionList
&Inputs
,
5256 ActionList
&CollapsedOffloadAction
) {
5258 // Get the largest chain of actions that we could combine.
5261 SmallVector
<JobActionInfo
, 5> ActionChain(1);
5262 ActionChain
.back().JA
= BaseAction
;
5263 while (ActionChain
.back().JA
) {
5264 const Action
*CurAction
= ActionChain
.back().JA
;
5266 // Grow the chain by one element.
5267 ActionChain
.resize(ActionChain
.size() + 1);
5268 JobActionInfo
&AI
= ActionChain
.back();
5270 // Attempt to fill it with the
5272 getPrevDependentAction(CurAction
->getInputs(), AI
.SavedOffloadAction
);
5275 // Pop the last action info as it could not be filled.
5276 ActionChain
.pop_back();
5279 // Attempt to combine actions. If all combining attempts failed, just return
5280 // the tool of the provided action. At the end we attempt to combine the
5281 // action with any preprocessor action it may depend on.
5284 const Tool
*T
= combineAssembleBackendCompile(ActionChain
, Inputs
,
5285 CollapsedOffloadAction
);
5287 T
= combineAssembleBackend(ActionChain
, Inputs
, CollapsedOffloadAction
);
5289 T
= combineBackendCompile(ActionChain
, Inputs
, CollapsedOffloadAction
);
5291 Inputs
= BaseAction
->getInputs();
5292 T
= TC
.SelectTool(*BaseAction
);
5295 combineWithPreprocessor(T
, Inputs
, CollapsedOffloadAction
);
5301 /// Return a string that uniquely identifies the result of a job. The bound arch
5302 /// is not necessarily represented in the toolchain's triple -- for example,
5303 /// armv7 and armv7s both map to the same triple -- so we need both in our map.
5304 /// Also, we need to add the offloading device kind, as the same tool chain can
5305 /// be used for host and device for some programming models, e.g. OpenMP.
5306 static std::string
GetTriplePlusArchString(const ToolChain
*TC
,
5307 StringRef BoundArch
,
5308 Action::OffloadKind OffloadKind
) {
5309 std::string TriplePlusArch
= TC
->getTriple().normalize();
5310 if (!BoundArch
.empty()) {
5311 TriplePlusArch
+= "-";
5312 TriplePlusArch
+= BoundArch
;
5314 TriplePlusArch
+= "-";
5315 TriplePlusArch
+= Action::GetOffloadKindName(OffloadKind
);
5316 return TriplePlusArch
;
5319 InputInfoList
Driver::BuildJobsForAction(
5320 Compilation
&C
, const Action
*A
, const ToolChain
*TC
, StringRef BoundArch
,
5321 bool AtTopLevel
, bool MultipleArchs
, const char *LinkingOutput
,
5322 std::map
<std::pair
<const Action
*, std::string
>, InputInfoList
>
5324 Action::OffloadKind TargetDeviceOffloadKind
) const {
5325 std::pair
<const Action
*, std::string
> ActionTC
= {
5326 A
, GetTriplePlusArchString(TC
, BoundArch
, TargetDeviceOffloadKind
)};
5327 auto CachedResult
= CachedResults
.find(ActionTC
);
5328 if (CachedResult
!= CachedResults
.end()) {
5329 return CachedResult
->second
;
5331 InputInfoList Result
= BuildJobsForActionNoCache(
5332 C
, A
, TC
, BoundArch
, AtTopLevel
, MultipleArchs
, LinkingOutput
,
5333 CachedResults
, TargetDeviceOffloadKind
);
5334 CachedResults
[ActionTC
] = Result
;
5338 static void handleTimeTrace(Compilation
&C
, const ArgList
&Args
,
5339 const JobAction
*JA
, const char *BaseInput
,
5340 const InputInfo
&Result
) {
5342 Args
.getLastArg(options::OPT_ftime_trace
, options::OPT_ftime_trace_EQ
);
5345 SmallString
<128> Path
;
5346 if (A
->getOption().matches(options::OPT_ftime_trace_EQ
)) {
5347 Path
= A
->getValue();
5348 if (llvm::sys::fs::is_directory(Path
)) {
5349 SmallString
<128> Tmp(Result
.getFilename());
5350 llvm::sys::path::replace_extension(Tmp
, "json");
5351 llvm::sys::path::append(Path
, llvm::sys::path::filename(Tmp
));
5354 if (Arg
*DumpDir
= Args
.getLastArgNoClaim(options::OPT_dumpdir
)) {
5355 // The trace file is ${dumpdir}${basename}.json. Note that dumpdir may not
5356 // end with a path separator.
5357 Path
= DumpDir
->getValue();
5358 Path
+= llvm::sys::path::filename(BaseInput
);
5360 Path
= Result
.getFilename();
5362 llvm::sys::path::replace_extension(Path
, "json");
5364 const char *ResultFile
= C
.getArgs().MakeArgString(Path
);
5365 C
.addTimeTraceFile(ResultFile
, JA
);
5366 C
.addResultFile(ResultFile
, JA
);
5369 InputInfoList
Driver::BuildJobsForActionNoCache(
5370 Compilation
&C
, const Action
*A
, const ToolChain
*TC
, StringRef BoundArch
,
5371 bool AtTopLevel
, bool MultipleArchs
, const char *LinkingOutput
,
5372 std::map
<std::pair
<const Action
*, std::string
>, InputInfoList
>
5374 Action::OffloadKind TargetDeviceOffloadKind
) const {
5375 llvm::PrettyStackTraceString
CrashInfo("Building compilation jobs");
5377 InputInfoList OffloadDependencesInputInfo
;
5378 bool BuildingForOffloadDevice
= TargetDeviceOffloadKind
!= Action::OFK_None
;
5379 if (const OffloadAction
*OA
= dyn_cast
<OffloadAction
>(A
)) {
5380 // The 'Darwin' toolchain is initialized only when its arguments are
5381 // computed. Get the default arguments for OFK_None to ensure that
5382 // initialization is performed before processing the offload action.
5383 // FIXME: Remove when darwin's toolchain is initialized during construction.
5384 C
.getArgsForToolChain(TC
, BoundArch
, Action::OFK_None
);
5386 // The offload action is expected to be used in four different situations.
5388 // a) Set a toolchain/architecture/kind for a host action:
5389 // Host Action 1 -> OffloadAction -> Host Action 2
5391 // b) Set a toolchain/architecture/kind for a device action;
5392 // Device Action 1 -> OffloadAction -> Device Action 2
5394 // c) Specify a device dependence to a host action;
5395 // Device Action 1 _
5397 // Host Action 1 ---> OffloadAction -> Host Action 2
5399 // d) Specify a host dependence to a device action.
5402 // Device Action 1 ---> OffloadAction -> Device Action 2
5404 // For a) and b), we just return the job generated for the dependences. For
5405 // c) and d) we override the current action with the host/device dependence
5406 // if the current toolchain is host/device and set the offload dependences
5407 // info with the jobs obtained from the device/host dependence(s).
5409 // If there is a single device option or has no host action, just generate
5411 if (OA
->hasSingleDeviceDependence() || !OA
->hasHostDependence()) {
5413 OA
->doOnEachDeviceDependence([&](Action
*DepA
, const ToolChain
*DepTC
,
5414 const char *DepBoundArch
) {
5415 DevA
.append(BuildJobsForAction(C
, DepA
, DepTC
, DepBoundArch
, AtTopLevel
,
5416 /*MultipleArchs*/ !!DepBoundArch
,
5417 LinkingOutput
, CachedResults
,
5418 DepA
->getOffloadingDeviceKind()));
5423 // If 'Action 2' is host, we generate jobs for the device dependences and
5424 // override the current action with the host dependence. Otherwise, we
5425 // generate the host dependences and override the action with the device
5426 // dependence. The dependences can't therefore be a top-level action.
5427 OA
->doOnEachDependence(
5428 /*IsHostDependence=*/BuildingForOffloadDevice
,
5429 [&](Action
*DepA
, const ToolChain
*DepTC
, const char *DepBoundArch
) {
5430 OffloadDependencesInputInfo
.append(BuildJobsForAction(
5431 C
, DepA
, DepTC
, DepBoundArch
, /*AtTopLevel=*/false,
5432 /*MultipleArchs*/ !!DepBoundArch
, LinkingOutput
, CachedResults
,
5433 DepA
->getOffloadingDeviceKind()));
5436 A
= BuildingForOffloadDevice
5437 ? OA
->getSingleDeviceDependence(/*DoNotConsiderHostActions=*/true)
5438 : OA
->getHostDependence();
5440 // We may have already built this action as a part of the offloading
5441 // toolchain, return the cached input if so.
5442 std::pair
<const Action
*, std::string
> ActionTC
= {
5443 OA
->getHostDependence(),
5444 GetTriplePlusArchString(TC
, BoundArch
, TargetDeviceOffloadKind
)};
5445 if (CachedResults
.find(ActionTC
) != CachedResults
.end()) {
5446 InputInfoList Inputs
= CachedResults
[ActionTC
];
5447 Inputs
.append(OffloadDependencesInputInfo
);
5452 if (const InputAction
*IA
= dyn_cast
<InputAction
>(A
)) {
5453 // FIXME: It would be nice to not claim this here; maybe the old scheme of
5454 // just using Args was better?
5455 const Arg
&Input
= IA
->getInputArg();
5457 if (Input
.getOption().matches(options::OPT_INPUT
)) {
5458 const char *Name
= Input
.getValue();
5459 return {InputInfo(A
, Name
, /* _BaseInput = */ Name
)};
5461 return {InputInfo(A
, &Input
, /* _BaseInput = */ "")};
5464 if (const BindArchAction
*BAA
= dyn_cast
<BindArchAction
>(A
)) {
5465 const ToolChain
*TC
;
5466 StringRef ArchName
= BAA
->getArchName();
5468 if (!ArchName
.empty())
5469 TC
= &getToolChain(C
.getArgs(),
5470 computeTargetTriple(*this, TargetTriple
,
5471 C
.getArgs(), ArchName
));
5473 TC
= &C
.getDefaultToolChain();
5475 return BuildJobsForAction(C
, *BAA
->input_begin(), TC
, ArchName
, AtTopLevel
,
5476 MultipleArchs
, LinkingOutput
, CachedResults
,
5477 TargetDeviceOffloadKind
);
5481 ActionList Inputs
= A
->getInputs();
5483 const JobAction
*JA
= cast
<JobAction
>(A
);
5484 ActionList CollapsedOffloadActions
;
5486 ToolSelector
TS(JA
, *TC
, C
, isSaveTempsEnabled(),
5487 embedBitcodeInObject() && !isUsingLTO());
5488 const Tool
*T
= TS
.getTool(Inputs
, CollapsedOffloadActions
);
5491 return {InputInfo()};
5493 // If we've collapsed action list that contained OffloadAction we
5494 // need to build jobs for host/device-side inputs it may have held.
5495 for (const auto *OA
: CollapsedOffloadActions
)
5496 cast
<OffloadAction
>(OA
)->doOnEachDependence(
5497 /*IsHostDependence=*/BuildingForOffloadDevice
,
5498 [&](Action
*DepA
, const ToolChain
*DepTC
, const char *DepBoundArch
) {
5499 OffloadDependencesInputInfo
.append(BuildJobsForAction(
5500 C
, DepA
, DepTC
, DepBoundArch
, /* AtTopLevel */ false,
5501 /*MultipleArchs=*/!!DepBoundArch
, LinkingOutput
, CachedResults
,
5502 DepA
->getOffloadingDeviceKind()));
5505 // Only use pipes when there is exactly one input.
5506 InputInfoList InputInfos
;
5507 for (const Action
*Input
: Inputs
) {
5508 // Treat dsymutil and verify sub-jobs as being at the top-level too, they
5509 // shouldn't get temporary output names.
5510 // FIXME: Clean this up.
5511 bool SubJobAtTopLevel
=
5512 AtTopLevel
&& (isa
<DsymutilJobAction
>(A
) || isa
<VerifyJobAction
>(A
));
5513 InputInfos
.append(BuildJobsForAction(
5514 C
, Input
, TC
, BoundArch
, SubJobAtTopLevel
, MultipleArchs
, LinkingOutput
,
5515 CachedResults
, A
->getOffloadingDeviceKind()));
5518 // Always use the first file input as the base input.
5519 const char *BaseInput
= InputInfos
[0].getBaseInput();
5520 for (auto &Info
: InputInfos
) {
5521 if (Info
.isFilename()) {
5522 BaseInput
= Info
.getBaseInput();
5527 // ... except dsymutil actions, which use their actual input as the base
5529 if (JA
->getType() == types::TY_dSYM
)
5530 BaseInput
= InputInfos
[0].getFilename();
5532 // Append outputs of offload device jobs to the input list
5533 if (!OffloadDependencesInputInfo
.empty())
5534 InputInfos
.append(OffloadDependencesInputInfo
.begin(),
5535 OffloadDependencesInputInfo
.end());
5537 // Set the effective triple of the toolchain for the duration of this job.
5538 llvm::Triple EffectiveTriple
;
5539 const ToolChain
&ToolTC
= T
->getToolChain();
5540 const ArgList
&Args
=
5541 C
.getArgsForToolChain(TC
, BoundArch
, A
->getOffloadingDeviceKind());
5542 if (InputInfos
.size() != 1) {
5543 EffectiveTriple
= llvm::Triple(ToolTC
.ComputeEffectiveClangTriple(Args
));
5545 // Pass along the input type if it can be unambiguously determined.
5546 EffectiveTriple
= llvm::Triple(
5547 ToolTC
.ComputeEffectiveClangTriple(Args
, InputInfos
[0].getType()));
5549 RegisterEffectiveTriple
TripleRAII(ToolTC
, EffectiveTriple
);
5551 // Determine the place to write output to, if any.
5553 InputInfoList UnbundlingResults
;
5554 if (auto *UA
= dyn_cast
<OffloadUnbundlingJobAction
>(JA
)) {
5555 // If we have an unbundling job, we need to create results for all the
5556 // outputs. We also update the results cache so that other actions using
5557 // this unbundling action can get the right results.
5558 for (auto &UI
: UA
->getDependentActionsInfo()) {
5559 assert(UI
.DependentOffloadKind
!= Action::OFK_None
&&
5560 "Unbundling with no offloading??");
5562 // Unbundling actions are never at the top level. When we generate the
5563 // offloading prefix, we also do that for the host file because the
5564 // unbundling action does not change the type of the output which can
5565 // cause a overwrite.
5566 std::string OffloadingPrefix
= Action::GetOffloadingFileNamePrefix(
5567 UI
.DependentOffloadKind
,
5568 UI
.DependentToolChain
->getTriple().normalize(),
5569 /*CreatePrefixForHost=*/true);
5570 auto CurI
= InputInfo(
5572 GetNamedOutputPath(C
, *UA
, BaseInput
, UI
.DependentBoundArch
,
5573 /*AtTopLevel=*/false,
5575 UI
.DependentOffloadKind
== Action::OFK_HIP
,
5578 // Save the unbundling result.
5579 UnbundlingResults
.push_back(CurI
);
5581 // Get the unique string identifier for this dependence and cache the
5584 if (TargetDeviceOffloadKind
== Action::OFK_HIP
) {
5585 if (UI
.DependentOffloadKind
== Action::OFK_Host
)
5588 Arch
= UI
.DependentBoundArch
;
5592 CachedResults
[{A
, GetTriplePlusArchString(UI
.DependentToolChain
, Arch
,
5593 UI
.DependentOffloadKind
)}] = {
5597 // Now that we have all the results generated, select the one that should be
5598 // returned for the current depending action.
5599 std::pair
<const Action
*, std::string
> ActionTC
= {
5600 A
, GetTriplePlusArchString(TC
, BoundArch
, TargetDeviceOffloadKind
)};
5601 assert(CachedResults
.find(ActionTC
) != CachedResults
.end() &&
5602 "Result does not exist??");
5603 Result
= CachedResults
[ActionTC
].front();
5604 } else if (JA
->getType() == types::TY_Nothing
)
5605 Result
= {InputInfo(A
, BaseInput
)};
5607 // We only have to generate a prefix for the host if this is not a top-level
5609 std::string OffloadingPrefix
= Action::GetOffloadingFileNamePrefix(
5610 A
->getOffloadingDeviceKind(), TC
->getTriple().normalize(),
5611 /*CreatePrefixForHost=*/isa
<OffloadPackagerJobAction
>(A
) ||
5612 !(A
->getOffloadingHostActiveKinds() == Action::OFK_None
||
5614 Result
= InputInfo(A
, GetNamedOutputPath(C
, *JA
, BaseInput
, BoundArch
,
5615 AtTopLevel
, MultipleArchs
,
5618 if (T
->canEmitIR() && OffloadingPrefix
.empty())
5619 handleTimeTrace(C
, Args
, JA
, BaseInput
, Result
);
5622 if (CCCPrintBindings
&& !CCGenDiagnostics
) {
5623 llvm::errs() << "# \"" << T
->getToolChain().getTripleString() << '"'
5624 << " - \"" << T
->getName() << "\", inputs: [";
5625 for (unsigned i
= 0, e
= InputInfos
.size(); i
!= e
; ++i
) {
5626 llvm::errs() << InputInfos
[i
].getAsString();
5628 llvm::errs() << ", ";
5630 if (UnbundlingResults
.empty())
5631 llvm::errs() << "], output: " << Result
.getAsString() << "\n";
5633 llvm::errs() << "], outputs: [";
5634 for (unsigned i
= 0, e
= UnbundlingResults
.size(); i
!= e
; ++i
) {
5635 llvm::errs() << UnbundlingResults
[i
].getAsString();
5637 llvm::errs() << ", ";
5639 llvm::errs() << "] \n";
5642 if (UnbundlingResults
.empty())
5644 C
, *JA
, Result
, InputInfos
,
5645 C
.getArgsForToolChain(TC
, BoundArch
, JA
->getOffloadingDeviceKind()),
5648 T
->ConstructJobMultipleOutputs(
5649 C
, *JA
, UnbundlingResults
, InputInfos
,
5650 C
.getArgsForToolChain(TC
, BoundArch
, JA
->getOffloadingDeviceKind()),
5656 const char *Driver::getDefaultImageName() const {
5657 llvm::Triple
Target(llvm::Triple::normalize(TargetTriple
));
5658 return Target
.isOSWindows() ? "a.exe" : "a.out";
5661 /// Create output filename based on ArgValue, which could either be a
5662 /// full filename, filename without extension, or a directory. If ArgValue
5663 /// does not provide a filename, then use BaseName, and use the extension
5664 /// suitable for FileType.
5665 static const char *MakeCLOutputFilename(const ArgList
&Args
, StringRef ArgValue
,
5667 types::ID FileType
) {
5668 SmallString
<128> Filename
= ArgValue
;
5670 if (ArgValue
.empty()) {
5671 // If the argument is empty, output to BaseName in the current dir.
5672 Filename
= BaseName
;
5673 } else if (llvm::sys::path::is_separator(Filename
.back())) {
5674 // If the argument is a directory, output to BaseName in that dir.
5675 llvm::sys::path::append(Filename
, BaseName
);
5678 if (!llvm::sys::path::has_extension(ArgValue
)) {
5679 // If the argument didn't provide an extension, then set it.
5680 const char *Extension
= types::getTypeTempSuffix(FileType
, true);
5682 if (FileType
== types::TY_Image
&&
5683 Args
.hasArg(options::OPT__SLASH_LD
, options::OPT__SLASH_LDd
)) {
5684 // The output file is a dll.
5688 llvm::sys::path::replace_extension(Filename
, Extension
);
5691 return Args
.MakeArgString(Filename
.c_str());
5694 static bool HasPreprocessOutput(const Action
&JA
) {
5695 if (isa
<PreprocessJobAction
>(JA
))
5697 if (isa
<OffloadAction
>(JA
) && isa
<PreprocessJobAction
>(JA
.getInputs()[0]))
5699 if (isa
<OffloadBundlingJobAction
>(JA
) &&
5700 HasPreprocessOutput(*(JA
.getInputs()[0])))
5705 const char *Driver::CreateTempFile(Compilation
&C
, StringRef Prefix
,
5706 StringRef Suffix
, bool MultipleArchs
,
5707 StringRef BoundArch
,
5708 bool NeedUniqueDirectory
) const {
5709 SmallString
<128> TmpName
;
5710 Arg
*A
= C
.getArgs().getLastArg(options::OPT_fcrash_diagnostics_dir
);
5711 std::optional
<std::string
> CrashDirectory
=
5712 CCGenDiagnostics
&& A
5713 ? std::string(A
->getValue())
5714 : llvm::sys::Process::GetEnv("CLANG_CRASH_DIAGNOSTICS_DIR");
5715 if (CrashDirectory
) {
5716 if (!getVFS().exists(*CrashDirectory
))
5717 llvm::sys::fs::create_directories(*CrashDirectory
);
5718 SmallString
<128> Path(*CrashDirectory
);
5719 llvm::sys::path::append(Path
, Prefix
);
5720 const char *Middle
= !Suffix
.empty() ? "-%%%%%%." : "-%%%%%%";
5721 if (std::error_code EC
=
5722 llvm::sys::fs::createUniqueFile(Path
+ Middle
+ Suffix
, TmpName
)) {
5723 Diag(clang::diag::err_unable_to_make_temp
) << EC
.message();
5727 if (MultipleArchs
&& !BoundArch
.empty()) {
5728 if (NeedUniqueDirectory
) {
5729 TmpName
= GetTemporaryDirectory(Prefix
);
5730 llvm::sys::path::append(TmpName
,
5731 Twine(Prefix
) + "-" + BoundArch
+ "." + Suffix
);
5734 GetTemporaryPath((Twine(Prefix
) + "-" + BoundArch
).str(), Suffix
);
5738 TmpName
= GetTemporaryPath(Prefix
, Suffix
);
5741 return C
.addTempFile(C
.getArgs().MakeArgString(TmpName
));
5744 // Calculate the output path of the module file when compiling a module unit
5745 // with the `-fmodule-output` option or `-fmodule-output=` option specified.
5747 // - If `-fmodule-output=` is specfied, then the module file is
5748 // writing to the value.
5749 // - Otherwise if the output object file of the module unit is specified, the
5751 // of the module file should be the same with the output object file except
5752 // the corresponding suffix. This requires both `-o` and `-c` are specified.
5753 // - Otherwise, the output path of the module file will be the same with the
5754 // input with the corresponding suffix.
5755 static const char *GetModuleOutputPath(Compilation
&C
, const JobAction
&JA
,
5756 const char *BaseInput
) {
5757 assert(isa
<PrecompileJobAction
>(JA
) && JA
.getType() == types::TY_ModuleFile
&&
5758 (C
.getArgs().hasArg(options::OPT_fmodule_output
) ||
5759 C
.getArgs().hasArg(options::OPT_fmodule_output_EQ
)));
5761 if (Arg
*ModuleOutputEQ
=
5762 C
.getArgs().getLastArg(options::OPT_fmodule_output_EQ
))
5763 return C
.addResultFile(ModuleOutputEQ
->getValue(), &JA
);
5765 SmallString
<64> OutputPath
;
5766 Arg
*FinalOutput
= C
.getArgs().getLastArg(options::OPT_o
);
5767 if (FinalOutput
&& C
.getArgs().hasArg(options::OPT_c
))
5768 OutputPath
= FinalOutput
->getValue();
5770 OutputPath
= BaseInput
;
5772 const char *Extension
= types::getTypeTempSuffix(JA
.getType());
5773 llvm::sys::path::replace_extension(OutputPath
, Extension
);
5774 return C
.addResultFile(C
.getArgs().MakeArgString(OutputPath
.c_str()), &JA
);
5777 const char *Driver::GetNamedOutputPath(Compilation
&C
, const JobAction
&JA
,
5778 const char *BaseInput
,
5779 StringRef OrigBoundArch
, bool AtTopLevel
,
5781 StringRef OffloadingPrefix
) const {
5782 std::string BoundArch
= OrigBoundArch
.str();
5783 if (is_style_windows(llvm::sys::path::Style::native
)) {
5784 // BoundArch may contains ':', which is invalid in file names on Windows,
5785 // therefore replace it with '%'.
5786 std::replace(BoundArch
.begin(), BoundArch
.end(), ':', '@');
5789 llvm::PrettyStackTraceString
CrashInfo("Computing output path");
5790 // Output to a user requested destination?
5791 if (AtTopLevel
&& !isa
<DsymutilJobAction
>(JA
) && !isa
<VerifyJobAction
>(JA
)) {
5792 if (Arg
*FinalOutput
= C
.getArgs().getLastArg(options::OPT_o
))
5793 return C
.addResultFile(FinalOutput
->getValue(), &JA
);
5796 // For /P, preprocess to file named after BaseInput.
5797 if (C
.getArgs().hasArg(options::OPT__SLASH_P
)) {
5798 assert(AtTopLevel
&& isa
<PreprocessJobAction
>(JA
));
5799 StringRef BaseName
= llvm::sys::path::filename(BaseInput
);
5801 if (Arg
*A
= C
.getArgs().getLastArg(options::OPT__SLASH_Fi
))
5802 NameArg
= A
->getValue();
5803 return C
.addResultFile(
5804 MakeCLOutputFilename(C
.getArgs(), NameArg
, BaseName
, types::TY_PP_C
),
5808 // Default to writing to stdout?
5809 if (AtTopLevel
&& !CCGenDiagnostics
&& HasPreprocessOutput(JA
)) {
5813 if (JA
.getType() == types::TY_ModuleFile
&&
5814 C
.getArgs().getLastArg(options::OPT_module_file_info
)) {
5818 if (JA
.getType() == types::TY_PP_Asm
&&
5819 C
.getArgs().hasArg(options::OPT_dxc_Fc
)) {
5820 StringRef FcValue
= C
.getArgs().getLastArgValue(options::OPT_dxc_Fc
);
5821 // TODO: Should we use `MakeCLOutputFilename` here? If so, we can probably
5822 // handle this as part of the SLASH_Fa handling below.
5823 return C
.addResultFile(C
.getArgs().MakeArgString(FcValue
.str()), &JA
);
5826 if (JA
.getType() == types::TY_Object
&&
5827 C
.getArgs().hasArg(options::OPT_dxc_Fo
)) {
5828 StringRef FoValue
= C
.getArgs().getLastArgValue(options::OPT_dxc_Fo
);
5829 // TODO: Should we use `MakeCLOutputFilename` here? If so, we can probably
5830 // handle this as part of the SLASH_Fo handling below.
5831 return C
.addResultFile(C
.getArgs().MakeArgString(FoValue
.str()), &JA
);
5834 // Is this the assembly listing for /FA?
5835 if (JA
.getType() == types::TY_PP_Asm
&&
5836 (C
.getArgs().hasArg(options::OPT__SLASH_FA
) ||
5837 C
.getArgs().hasArg(options::OPT__SLASH_Fa
))) {
5838 // Use /Fa and the input filename to determine the asm file name.
5839 StringRef BaseName
= llvm::sys::path::filename(BaseInput
);
5840 StringRef FaValue
= C
.getArgs().getLastArgValue(options::OPT__SLASH_Fa
);
5841 return C
.addResultFile(
5842 MakeCLOutputFilename(C
.getArgs(), FaValue
, BaseName
, JA
.getType()),
5846 // DXC defaults to standard out when generating assembly. We check this after
5847 // any DXC flags that might specify a file.
5848 if (AtTopLevel
&& JA
.getType() == types::TY_PP_Asm
&& IsDXCMode())
5851 bool SpecifiedModuleOutput
=
5852 C
.getArgs().hasArg(options::OPT_fmodule_output
) ||
5853 C
.getArgs().hasArg(options::OPT_fmodule_output_EQ
);
5854 if (MultipleArchs
&& SpecifiedModuleOutput
)
5855 Diag(clang::diag::err_drv_module_output_with_multiple_arch
);
5857 // If we're emitting a module output with the specified option
5858 // `-fmodule-output`.
5859 if (!AtTopLevel
&& isa
<PrecompileJobAction
>(JA
) &&
5860 JA
.getType() == types::TY_ModuleFile
&& SpecifiedModuleOutput
)
5861 return GetModuleOutputPath(C
, JA
, BaseInput
);
5863 // Output to a temporary file?
5864 if ((!AtTopLevel
&& !isSaveTempsEnabled() &&
5865 !C
.getArgs().hasArg(options::OPT__SLASH_Fo
)) ||
5867 StringRef Name
= llvm::sys::path::filename(BaseInput
);
5868 std::pair
<StringRef
, StringRef
> Split
= Name
.split('.');
5869 const char *Suffix
=
5870 types::getTypeTempSuffix(JA
.getType(), IsCLMode() || IsDXCMode());
5871 // The non-offloading toolchain on Darwin requires deterministic input
5872 // file name for binaries to be deterministic, therefore it needs unique
5874 llvm::Triple
Triple(C
.getDriver().getTargetTriple());
5875 bool NeedUniqueDirectory
=
5876 (JA
.getOffloadingDeviceKind() == Action::OFK_None
||
5877 JA
.getOffloadingDeviceKind() == Action::OFK_Host
) &&
5878 Triple
.isOSDarwin();
5879 return CreateTempFile(C
, Split
.first
, Suffix
, MultipleArchs
, BoundArch
,
5880 NeedUniqueDirectory
);
5883 SmallString
<128> BasePath(BaseInput
);
5884 SmallString
<128> ExternalPath("");
5887 // Dsymutil actions should use the full path.
5888 if (isa
<DsymutilJobAction
>(JA
) && C
.getArgs().hasArg(options::OPT_dsym_dir
)) {
5889 ExternalPath
+= C
.getArgs().getLastArg(options::OPT_dsym_dir
)->getValue();
5890 // We use posix style here because the tests (specifically
5891 // darwin-dsymutil.c) demonstrate that posix style paths are acceptable
5892 // even on Windows and if we don't then the similar test covering this
5894 llvm::sys::path::append(ExternalPath
, llvm::sys::path::Style::posix
,
5895 llvm::sys::path::filename(BasePath
));
5896 BaseName
= ExternalPath
;
5897 } else if (isa
<DsymutilJobAction
>(JA
) || isa
<VerifyJobAction
>(JA
))
5898 BaseName
= BasePath
;
5900 BaseName
= llvm::sys::path::filename(BasePath
);
5902 // Determine what the derived output name should be.
5903 const char *NamedOutput
;
5905 if ((JA
.getType() == types::TY_Object
|| JA
.getType() == types::TY_LTO_BC
) &&
5906 C
.getArgs().hasArg(options::OPT__SLASH_Fo
, options::OPT__SLASH_o
)) {
5907 // The /Fo or /o flag decides the object filename.
5910 .getLastArg(options::OPT__SLASH_Fo
, options::OPT__SLASH_o
)
5913 MakeCLOutputFilename(C
.getArgs(), Val
, BaseName
, types::TY_Object
);
5914 } else if (JA
.getType() == types::TY_Image
&&
5915 C
.getArgs().hasArg(options::OPT__SLASH_Fe
,
5916 options::OPT__SLASH_o
)) {
5917 // The /Fe or /o flag names the linked file.
5920 .getLastArg(options::OPT__SLASH_Fe
, options::OPT__SLASH_o
)
5923 MakeCLOutputFilename(C
.getArgs(), Val
, BaseName
, types::TY_Image
);
5924 } else if (JA
.getType() == types::TY_Image
) {
5926 // clang-cl uses BaseName for the executable name.
5928 MakeCLOutputFilename(C
.getArgs(), "", BaseName
, types::TY_Image
);
5930 SmallString
<128> Output(getDefaultImageName());
5931 // HIP image for device compilation with -fno-gpu-rdc is per compilation
5933 bool IsHIPNoRDC
= JA
.getOffloadingDeviceKind() == Action::OFK_HIP
&&
5934 !C
.getArgs().hasFlag(options::OPT_fgpu_rdc
,
5935 options::OPT_fno_gpu_rdc
, false);
5936 bool UseOutExtension
= IsHIPNoRDC
|| isa
<OffloadPackagerJobAction
>(JA
);
5937 if (UseOutExtension
) {
5939 llvm::sys::path::replace_extension(Output
, "");
5941 Output
+= OffloadingPrefix
;
5942 if (MultipleArchs
&& !BoundArch
.empty()) {
5944 Output
.append(BoundArch
);
5946 if (UseOutExtension
)
5948 NamedOutput
= C
.getArgs().MakeArgString(Output
.c_str());
5950 } else if (JA
.getType() == types::TY_PCH
&& IsCLMode()) {
5951 NamedOutput
= C
.getArgs().MakeArgString(GetClPchPath(C
, BaseName
));
5952 } else if ((JA
.getType() == types::TY_Plist
|| JA
.getType() == types::TY_AST
) &&
5953 C
.getArgs().hasArg(options::OPT__SLASH_o
)) {
5956 .getLastArg(options::OPT__SLASH_o
)
5959 MakeCLOutputFilename(C
.getArgs(), Val
, BaseName
, types::TY_Object
);
5961 const char *Suffix
=
5962 types::getTypeTempSuffix(JA
.getType(), IsCLMode() || IsDXCMode());
5963 assert(Suffix
&& "All types used for output should have a suffix.");
5965 std::string::size_type End
= std::string::npos
;
5966 if (!types::appendSuffixForType(JA
.getType()))
5967 End
= BaseName
.rfind('.');
5968 SmallString
<128> Suffixed(BaseName
.substr(0, End
));
5969 Suffixed
+= OffloadingPrefix
;
5970 if (MultipleArchs
&& !BoundArch
.empty()) {
5972 Suffixed
.append(BoundArch
);
5974 // When using both -save-temps and -emit-llvm, use a ".tmp.bc" suffix for
5975 // the unoptimized bitcode so that it does not get overwritten by the ".bc"
5976 // optimized bitcode output.
5977 auto IsAMDRDCInCompilePhase
= [](const JobAction
&JA
,
5978 const llvm::opt::DerivedArgList
&Args
) {
5979 // The relocatable compilation in HIP and OpenMP implies -emit-llvm.
5980 // Similarly, use a ".tmp.bc" suffix for the unoptimized bitcode
5981 // (generated in the compile phase.)
5982 const ToolChain
*TC
= JA
.getOffloadingToolChain();
5983 return isa
<CompileJobAction
>(JA
) &&
5984 ((JA
.getOffloadingDeviceKind() == Action::OFK_HIP
&&
5985 Args
.hasFlag(options::OPT_fgpu_rdc
, options::OPT_fno_gpu_rdc
,
5987 (JA
.getOffloadingDeviceKind() == Action::OFK_OpenMP
&& TC
&&
5988 TC
->getTriple().isAMDGPU()));
5990 if (!AtTopLevel
&& JA
.getType() == types::TY_LLVM_BC
&&
5991 (C
.getArgs().hasArg(options::OPT_emit_llvm
) ||
5992 IsAMDRDCInCompilePhase(JA
, C
.getArgs())))
5996 NamedOutput
= C
.getArgs().MakeArgString(Suffixed
.c_str());
5999 // Prepend object file path if -save-temps=obj
6000 if (!AtTopLevel
&& isSaveTempsObj() && C
.getArgs().hasArg(options::OPT_o
) &&
6001 JA
.getType() != types::TY_PCH
) {
6002 Arg
*FinalOutput
= C
.getArgs().getLastArg(options::OPT_o
);
6003 SmallString
<128> TempPath(FinalOutput
->getValue());
6004 llvm::sys::path::remove_filename(TempPath
);
6005 StringRef OutputFileName
= llvm::sys::path::filename(NamedOutput
);
6006 llvm::sys::path::append(TempPath
, OutputFileName
);
6007 NamedOutput
= C
.getArgs().MakeArgString(TempPath
.c_str());
6010 // If we're saving temps and the temp file conflicts with the input file,
6011 // then avoid overwriting input file.
6012 if (!AtTopLevel
&& isSaveTempsEnabled() && NamedOutput
== BaseName
) {
6013 bool SameFile
= false;
6014 SmallString
<256> Result
;
6015 llvm::sys::fs::current_path(Result
);
6016 llvm::sys::path::append(Result
, BaseName
);
6017 llvm::sys::fs::equivalent(BaseInput
, Result
.c_str(), SameFile
);
6018 // Must share the same path to conflict.
6020 StringRef Name
= llvm::sys::path::filename(BaseInput
);
6021 std::pair
<StringRef
, StringRef
> Split
= Name
.split('.');
6022 std::string TmpName
= GetTemporaryPath(
6024 types::getTypeTempSuffix(JA
.getType(), IsCLMode() || IsDXCMode()));
6025 return C
.addTempFile(C
.getArgs().MakeArgString(TmpName
));
6029 // As an annoying special case, PCH generation doesn't strip the pathname.
6030 if (JA
.getType() == types::TY_PCH
&& !IsCLMode()) {
6031 llvm::sys::path::remove_filename(BasePath
);
6032 if (BasePath
.empty())
6033 BasePath
= NamedOutput
;
6035 llvm::sys::path::append(BasePath
, NamedOutput
);
6036 return C
.addResultFile(C
.getArgs().MakeArgString(BasePath
.c_str()), &JA
);
6039 return C
.addResultFile(NamedOutput
, &JA
);
6042 std::string
Driver::GetFilePath(StringRef Name
, const ToolChain
&TC
) const {
6043 // Search for Name in a list of paths.
6044 auto SearchPaths
= [&](const llvm::SmallVectorImpl
<std::string
> &P
)
6045 -> std::optional
<std::string
> {
6046 // Respect a limited subset of the '-Bprefix' functionality in GCC by
6047 // attempting to use this prefix when looking for file paths.
6048 for (const auto &Dir
: P
) {
6051 SmallString
<128> P(Dir
[0] == '=' ? SysRoot
+ Dir
.substr(1) : Dir
);
6052 llvm::sys::path::append(P
, Name
);
6053 if (llvm::sys::fs::exists(Twine(P
)))
6054 return std::string(P
);
6056 return std::nullopt
;
6059 if (auto P
= SearchPaths(PrefixDirs
))
6062 SmallString
<128> R(ResourceDir
);
6063 llvm::sys::path::append(R
, Name
);
6064 if (llvm::sys::fs::exists(Twine(R
)))
6065 return std::string(R
.str());
6067 SmallString
<128> P(TC
.getCompilerRTPath());
6068 llvm::sys::path::append(P
, Name
);
6069 if (llvm::sys::fs::exists(Twine(P
)))
6070 return std::string(P
.str());
6072 SmallString
<128> D(Dir
);
6073 llvm::sys::path::append(D
, "..", Name
);
6074 if (llvm::sys::fs::exists(Twine(D
)))
6075 return std::string(D
.str());
6077 if (auto P
= SearchPaths(TC
.getLibraryPaths()))
6080 if (auto P
= SearchPaths(TC
.getFilePaths()))
6083 return std::string(Name
);
6086 void Driver::generatePrefixedToolNames(
6087 StringRef Tool
, const ToolChain
&TC
,
6088 SmallVectorImpl
<std::string
> &Names
) const {
6089 // FIXME: Needs a better variable than TargetTriple
6090 Names
.emplace_back((TargetTriple
+ "-" + Tool
).str());
6091 Names
.emplace_back(Tool
);
6094 static bool ScanDirForExecutable(SmallString
<128> &Dir
, StringRef Name
) {
6095 llvm::sys::path::append(Dir
, Name
);
6096 if (llvm::sys::fs::can_execute(Twine(Dir
)))
6098 llvm::sys::path::remove_filename(Dir
);
6102 std::string
Driver::GetProgramPath(StringRef Name
, const ToolChain
&TC
) const {
6103 SmallVector
<std::string
, 2> TargetSpecificExecutables
;
6104 generatePrefixedToolNames(Name
, TC
, TargetSpecificExecutables
);
6106 // Respect a limited subset of the '-Bprefix' functionality in GCC by
6107 // attempting to use this prefix when looking for program paths.
6108 for (const auto &PrefixDir
: PrefixDirs
) {
6109 if (llvm::sys::fs::is_directory(PrefixDir
)) {
6110 SmallString
<128> P(PrefixDir
);
6111 if (ScanDirForExecutable(P
, Name
))
6112 return std::string(P
.str());
6114 SmallString
<128> P((PrefixDir
+ Name
).str());
6115 if (llvm::sys::fs::can_execute(Twine(P
)))
6116 return std::string(P
.str());
6120 const ToolChain::path_list
&List
= TC
.getProgramPaths();
6121 for (const auto &TargetSpecificExecutable
: TargetSpecificExecutables
) {
6122 // For each possible name of the tool look for it in
6123 // program paths first, then the path.
6124 // Higher priority names will be first, meaning that
6125 // a higher priority name in the path will be found
6126 // instead of a lower priority name in the program path.
6127 // E.g. <triple>-gcc on the path will be found instead
6128 // of gcc in the program path
6129 for (const auto &Path
: List
) {
6130 SmallString
<128> P(Path
);
6131 if (ScanDirForExecutable(P
, TargetSpecificExecutable
))
6132 return std::string(P
.str());
6135 // Fall back to the path
6136 if (llvm::ErrorOr
<std::string
> P
=
6137 llvm::sys::findProgramByName(TargetSpecificExecutable
))
6141 return std::string(Name
);
6144 std::string
Driver::GetTemporaryPath(StringRef Prefix
, StringRef Suffix
) const {
6145 SmallString
<128> Path
;
6146 std::error_code EC
= llvm::sys::fs::createTemporaryFile(Prefix
, Suffix
, Path
);
6148 Diag(clang::diag::err_unable_to_make_temp
) << EC
.message();
6152 return std::string(Path
.str());
6155 std::string
Driver::GetTemporaryDirectory(StringRef Prefix
) const {
6156 SmallString
<128> Path
;
6157 std::error_code EC
= llvm::sys::fs::createUniqueDirectory(Prefix
, Path
);
6159 Diag(clang::diag::err_unable_to_make_temp
) << EC
.message();
6163 return std::string(Path
.str());
6166 std::string
Driver::GetClPchPath(Compilation
&C
, StringRef BaseName
) const {
6167 SmallString
<128> Output
;
6168 if (Arg
*FpArg
= C
.getArgs().getLastArg(options::OPT__SLASH_Fp
)) {
6169 // FIXME: If anybody needs it, implement this obscure rule:
6170 // "If you specify a directory without a file name, the default file name
6171 // is VCx0.pch., where x is the major version of Visual C++ in use."
6172 Output
= FpArg
->getValue();
6174 // "If you do not specify an extension as part of the path name, an
6175 // extension of .pch is assumed. "
6176 if (!llvm::sys::path::has_extension(Output
))
6179 if (Arg
*YcArg
= C
.getArgs().getLastArg(options::OPT__SLASH_Yc
))
6180 Output
= YcArg
->getValue();
6183 llvm::sys::path::replace_extension(Output
, ".pch");
6185 return std::string(Output
.str());
6188 const ToolChain
&Driver::getToolChain(const ArgList
&Args
,
6189 const llvm::Triple
&Target
) const {
6191 auto &TC
= ToolChains
[Target
.str()];
6193 switch (Target
.getOS()) {
6194 case llvm::Triple::AIX
:
6195 TC
= std::make_unique
<toolchains::AIX
>(*this, Target
, Args
);
6197 case llvm::Triple::Haiku
:
6198 TC
= std::make_unique
<toolchains::Haiku
>(*this, Target
, Args
);
6200 case llvm::Triple::Darwin
:
6201 case llvm::Triple::MacOSX
:
6202 case llvm::Triple::IOS
:
6203 case llvm::Triple::TvOS
:
6204 case llvm::Triple::WatchOS
:
6205 case llvm::Triple::DriverKit
:
6206 TC
= std::make_unique
<toolchains::DarwinClang
>(*this, Target
, Args
);
6208 case llvm::Triple::DragonFly
:
6209 TC
= std::make_unique
<toolchains::DragonFly
>(*this, Target
, Args
);
6211 case llvm::Triple::OpenBSD
:
6212 TC
= std::make_unique
<toolchains::OpenBSD
>(*this, Target
, Args
);
6214 case llvm::Triple::NetBSD
:
6215 TC
= std::make_unique
<toolchains::NetBSD
>(*this, Target
, Args
);
6217 case llvm::Triple::FreeBSD
:
6219 TC
= std::make_unique
<toolchains::PPCFreeBSDToolChain
>(*this, Target
,
6222 TC
= std::make_unique
<toolchains::FreeBSD
>(*this, Target
, Args
);
6224 case llvm::Triple::Linux
:
6225 case llvm::Triple::ELFIAMCU
:
6226 if (Target
.getArch() == llvm::Triple::hexagon
)
6227 TC
= std::make_unique
<toolchains::HexagonToolChain
>(*this, Target
,
6229 else if ((Target
.getVendor() == llvm::Triple::MipsTechnologies
) &&
6230 !Target
.hasEnvironment())
6231 TC
= std::make_unique
<toolchains::MipsLLVMToolChain
>(*this, Target
,
6233 else if (Target
.isPPC())
6234 TC
= std::make_unique
<toolchains::PPCLinuxToolChain
>(*this, Target
,
6236 else if (Target
.getArch() == llvm::Triple::ve
)
6237 TC
= std::make_unique
<toolchains::VEToolChain
>(*this, Target
, Args
);
6238 else if (Target
.isOHOSFamily())
6239 TC
= std::make_unique
<toolchains::OHOS
>(*this, Target
, Args
);
6241 TC
= std::make_unique
<toolchains::Linux
>(*this, Target
, Args
);
6243 case llvm::Triple::NaCl
:
6244 TC
= std::make_unique
<toolchains::NaClToolChain
>(*this, Target
, Args
);
6246 case llvm::Triple::Fuchsia
:
6247 TC
= std::make_unique
<toolchains::Fuchsia
>(*this, Target
, Args
);
6249 case llvm::Triple::Solaris
:
6250 TC
= std::make_unique
<toolchains::Solaris
>(*this, Target
, Args
);
6252 case llvm::Triple::CUDA
:
6253 TC
= std::make_unique
<toolchains::NVPTXToolChain
>(*this, Target
, Args
);
6255 case llvm::Triple::AMDHSA
:
6256 TC
= std::make_unique
<toolchains::ROCMToolChain
>(*this, Target
, Args
);
6258 case llvm::Triple::AMDPAL
:
6259 case llvm::Triple::Mesa3D
:
6260 TC
= std::make_unique
<toolchains::AMDGPUToolChain
>(*this, Target
, Args
);
6262 case llvm::Triple::Win32
:
6263 switch (Target
.getEnvironment()) {
6265 if (Target
.isOSBinFormatELF())
6266 TC
= std::make_unique
<toolchains::Generic_ELF
>(*this, Target
, Args
);
6267 else if (Target
.isOSBinFormatMachO())
6268 TC
= std::make_unique
<toolchains::MachO
>(*this, Target
, Args
);
6270 TC
= std::make_unique
<toolchains::Generic_GCC
>(*this, Target
, Args
);
6272 case llvm::Triple::GNU
:
6273 TC
= std::make_unique
<toolchains::MinGW
>(*this, Target
, Args
);
6275 case llvm::Triple::Itanium
:
6276 TC
= std::make_unique
<toolchains::CrossWindowsToolChain
>(*this, Target
,
6279 case llvm::Triple::MSVC
:
6280 case llvm::Triple::UnknownEnvironment
:
6281 if (Args
.getLastArgValue(options::OPT_fuse_ld_EQ
)
6282 .starts_with_insensitive("bfd"))
6283 TC
= std::make_unique
<toolchains::CrossWindowsToolChain
>(
6284 *this, Target
, Args
);
6287 std::make_unique
<toolchains::MSVCToolChain
>(*this, Target
, Args
);
6291 case llvm::Triple::PS4
:
6292 TC
= std::make_unique
<toolchains::PS4CPU
>(*this, Target
, Args
);
6294 case llvm::Triple::PS5
:
6295 TC
= std::make_unique
<toolchains::PS5CPU
>(*this, Target
, Args
);
6297 case llvm::Triple::Hurd
:
6298 TC
= std::make_unique
<toolchains::Hurd
>(*this, Target
, Args
);
6300 case llvm::Triple::LiteOS
:
6301 TC
= std::make_unique
<toolchains::OHOS
>(*this, Target
, Args
);
6303 case llvm::Triple::ZOS
:
6304 TC
= std::make_unique
<toolchains::ZOS
>(*this, Target
, Args
);
6306 case llvm::Triple::ShaderModel
:
6307 TC
= std::make_unique
<toolchains::HLSLToolChain
>(*this, Target
, Args
);
6310 // Of these targets, Hexagon is the only one that might have
6311 // an OS of Linux, in which case it got handled above already.
6312 switch (Target
.getArch()) {
6313 case llvm::Triple::tce
:
6314 TC
= std::make_unique
<toolchains::TCEToolChain
>(*this, Target
, Args
);
6316 case llvm::Triple::tcele
:
6317 TC
= std::make_unique
<toolchains::TCELEToolChain
>(*this, Target
, Args
);
6319 case llvm::Triple::hexagon
:
6320 TC
= std::make_unique
<toolchains::HexagonToolChain
>(*this, Target
,
6323 case llvm::Triple::lanai
:
6324 TC
= std::make_unique
<toolchains::LanaiToolChain
>(*this, Target
, Args
);
6326 case llvm::Triple::xcore
:
6327 TC
= std::make_unique
<toolchains::XCoreToolChain
>(*this, Target
, Args
);
6329 case llvm::Triple::wasm32
:
6330 case llvm::Triple::wasm64
:
6331 TC
= std::make_unique
<toolchains::WebAssembly
>(*this, Target
, Args
);
6333 case llvm::Triple::avr
:
6334 TC
= std::make_unique
<toolchains::AVRToolChain
>(*this, Target
, Args
);
6336 case llvm::Triple::msp430
:
6338 std::make_unique
<toolchains::MSP430ToolChain
>(*this, Target
, Args
);
6340 case llvm::Triple::riscv32
:
6341 case llvm::Triple::riscv64
:
6342 if (toolchains::RISCVToolChain::hasGCCToolchain(*this, Args
))
6344 std::make_unique
<toolchains::RISCVToolChain
>(*this, Target
, Args
);
6346 TC
= std::make_unique
<toolchains::BareMetal
>(*this, Target
, Args
);
6348 case llvm::Triple::ve
:
6349 TC
= std::make_unique
<toolchains::VEToolChain
>(*this, Target
, Args
);
6351 case llvm::Triple::spirv32
:
6352 case llvm::Triple::spirv64
:
6353 TC
= std::make_unique
<toolchains::SPIRVToolChain
>(*this, Target
, Args
);
6355 case llvm::Triple::csky
:
6356 TC
= std::make_unique
<toolchains::CSKYToolChain
>(*this, Target
, Args
);
6359 if (toolchains::BareMetal::handlesTarget(Target
))
6360 TC
= std::make_unique
<toolchains::BareMetal
>(*this, Target
, Args
);
6361 else if (Target
.isOSBinFormatELF())
6362 TC
= std::make_unique
<toolchains::Generic_ELF
>(*this, Target
, Args
);
6363 else if (Target
.isOSBinFormatMachO())
6364 TC
= std::make_unique
<toolchains::MachO
>(*this, Target
, Args
);
6366 TC
= std::make_unique
<toolchains::Generic_GCC
>(*this, Target
, Args
);
6374 const ToolChain
&Driver::getOffloadingDeviceToolChain(
6375 const ArgList
&Args
, const llvm::Triple
&Target
, const ToolChain
&HostTC
,
6376 const Action::OffloadKind
&TargetDeviceOffloadKind
) const {
6377 // Use device / host triples as the key into the ToolChains map because the
6378 // device ToolChain we create depends on both.
6379 auto &TC
= ToolChains
[Target
.str() + "/" + HostTC
.getTriple().str()];
6381 // Categorized by offload kind > arch rather than OS > arch like
6382 // the normal getToolChain call, as it seems a reasonable way to categorize
6384 switch (TargetDeviceOffloadKind
) {
6385 case Action::OFK_HIP
: {
6386 if (Target
.getArch() == llvm::Triple::amdgcn
&&
6387 Target
.getVendor() == llvm::Triple::AMD
&&
6388 Target
.getOS() == llvm::Triple::AMDHSA
)
6389 TC
= std::make_unique
<toolchains::HIPAMDToolChain
>(*this, Target
,
6391 else if (Target
.getArch() == llvm::Triple::spirv64
&&
6392 Target
.getVendor() == llvm::Triple::UnknownVendor
&&
6393 Target
.getOS() == llvm::Triple::UnknownOS
)
6394 TC
= std::make_unique
<toolchains::HIPSPVToolChain
>(*this, Target
,
6406 bool Driver::ShouldUseClangCompiler(const JobAction
&JA
) const {
6407 // Say "no" if there is not exactly one input of a type clang understands.
6408 if (JA
.size() != 1 ||
6409 !types::isAcceptedByClang((*JA
.input_begin())->getType()))
6412 // And say "no" if this is not a kind of action clang understands.
6413 if (!isa
<PreprocessJobAction
>(JA
) && !isa
<PrecompileJobAction
>(JA
) &&
6414 !isa
<CompileJobAction
>(JA
) && !isa
<BackendJobAction
>(JA
) &&
6415 !isa
<ExtractAPIJobAction
>(JA
))
6421 bool Driver::ShouldUseFlangCompiler(const JobAction
&JA
) const {
6422 // Say "no" if there is not exactly one input of a type flang understands.
6423 if (JA
.size() != 1 ||
6424 !types::isAcceptedByFlang((*JA
.input_begin())->getType()))
6427 // And say "no" if this is not a kind of action flang understands.
6428 if (!isa
<PreprocessJobAction
>(JA
) && !isa
<CompileJobAction
>(JA
) &&
6429 !isa
<BackendJobAction
>(JA
))
6435 bool Driver::ShouldEmitStaticLibrary(const ArgList
&Args
) const {
6436 // Only emit static library if the flag is set explicitly.
6437 if (Args
.hasArg(options::OPT_emit_static_lib
))
6442 /// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the
6443 /// grouped values as integers. Numbers which are not provided are set to 0.
6445 /// \return True if the entire string was parsed (9.2), or all groups were
6446 /// parsed (10.3.5extrastuff).
6447 bool Driver::GetReleaseVersion(StringRef Str
, unsigned &Major
, unsigned &Minor
,
6448 unsigned &Micro
, bool &HadExtra
) {
6451 Major
= Minor
= Micro
= 0;
6455 if (Str
.consumeInteger(10, Major
))
6462 Str
= Str
.drop_front(1);
6464 if (Str
.consumeInteger(10, Minor
))
6470 Str
= Str
.drop_front(1);
6472 if (Str
.consumeInteger(10, Micro
))
6479 /// Parse digits from a string \p Str and fulfill \p Digits with
6480 /// the parsed numbers. This method assumes that the max number of
6481 /// digits to look for is equal to Digits.size().
6483 /// \return True if the entire string was parsed and there are
6484 /// no extra characters remaining at the end.
6485 bool Driver::GetReleaseVersion(StringRef Str
,
6486 MutableArrayRef
<unsigned> Digits
) {
6490 unsigned CurDigit
= 0;
6491 while (CurDigit
< Digits
.size()) {
6493 if (Str
.consumeInteger(10, Digit
))
6495 Digits
[CurDigit
] = Digit
;
6500 Str
= Str
.drop_front(1);
6504 // More digits than requested, bail out...
6508 llvm::opt::Visibility
6509 Driver::getOptionVisibilityMask(bool UseDriverMode
) const {
6511 return llvm::opt::Visibility(options::ClangOption
);
6513 return llvm::opt::Visibility(options::CLOption
);
6515 return llvm::opt::Visibility(options::DXCOption
);
6516 if (IsFlangMode()) {
6517 return llvm::opt::Visibility(options::FlangOption
);
6519 return llvm::opt::Visibility(options::ClangOption
);
6522 const char *Driver::getExecutableForDriverMode(DriverMode Mode
) {
6538 llvm_unreachable("Unhandled Mode");
6541 bool clang::driver::isOptimizationLevelFast(const ArgList
&Args
) {
6542 return Args
.hasFlag(options::OPT_Ofast
, options::OPT_O_Group
, false);
6545 bool clang::driver::willEmitRemarks(const ArgList
&Args
) {
6546 // -fsave-optimization-record enables it.
6547 if (Args
.hasFlag(options::OPT_fsave_optimization_record
,
6548 options::OPT_fno_save_optimization_record
, false))
6551 // -fsave-optimization-record=<format> enables it as well.
6552 if (Args
.hasFlag(options::OPT_fsave_optimization_record_EQ
,
6553 options::OPT_fno_save_optimization_record
, false))
6556 // -foptimization-record-file alone enables it too.
6557 if (Args
.hasFlag(options::OPT_foptimization_record_file_EQ
,
6558 options::OPT_fno_save_optimization_record
, false))
6561 // -foptimization-record-passes alone enables it too.
6562 if (Args
.hasFlag(options::OPT_foptimization_record_passes_EQ
,
6563 options::OPT_fno_save_optimization_record
, false))
6568 llvm::StringRef
clang::driver::getDriverMode(StringRef ProgName
,
6569 ArrayRef
<const char *> Args
) {
6570 static StringRef OptName
=
6571 getDriverOptTable().getOption(options::OPT_driver_mode
).getPrefixedName();
6572 llvm::StringRef Opt
;
6573 for (StringRef Arg
: Args
) {
6574 if (!Arg
.startswith(OptName
))
6579 Opt
= ToolChain::getTargetAndModeFromProgramName(ProgName
).DriverMode
;
6580 return Opt
.consume_front(OptName
) ? Opt
: "";
6583 bool driver::IsClangCL(StringRef DriverMode
) { return DriverMode
.equals("cl"); }
6585 llvm::Error
driver::expandResponseFiles(SmallVectorImpl
<const char *> &Args
,
6587 llvm::BumpPtrAllocator
&Alloc
,
6588 llvm::vfs::FileSystem
*FS
) {
6589 // Parse response files using the GNU syntax, unless we're in CL mode. There
6590 // are two ways to put clang in CL compatibility mode: ProgName is either
6591 // clang-cl or cl, or --driver-mode=cl is on the command line. The normal
6592 // command line parsing can't happen until after response file parsing, so we
6593 // have to manually search for a --driver-mode=cl argument the hard way.
6594 // Finally, our -cc1 tools don't care which tokenization mode we use because
6595 // response files written by clang will tokenize the same way in either mode.
6596 enum { Default
, POSIX
, Windows
} RSPQuoting
= Default
;
6597 for (const char *F
: Args
) {
6598 if (strcmp(F
, "--rsp-quoting=posix") == 0)
6600 else if (strcmp(F
, "--rsp-quoting=windows") == 0)
6601 RSPQuoting
= Windows
;
6604 // Determines whether we want nullptr markers in Args to indicate response
6605 // files end-of-lines. We only use this for the /LINK driver argument with
6606 // clang-cl.exe on Windows.
6607 bool MarkEOLs
= ClangCLMode
;
6609 llvm::cl::TokenizerCallback Tokenizer
;
6610 if (RSPQuoting
== Windows
|| (RSPQuoting
== Default
&& ClangCLMode
))
6611 Tokenizer
= &llvm::cl::TokenizeWindowsCommandLine
;
6613 Tokenizer
= &llvm::cl::TokenizeGNUCommandLine
;
6615 if (MarkEOLs
&& Args
.size() > 1 && StringRef(Args
[1]).startswith("-cc1"))
6618 llvm::cl::ExpansionContext
ECtx(Alloc
, Tokenizer
);
6619 ECtx
.setMarkEOLs(MarkEOLs
);
6623 if (llvm::Error Err
= ECtx
.expandResponseFiles(Args
))
6626 // If -cc1 came from a response file, remove the EOL sentinels.
6627 auto FirstArg
= llvm::find_if(llvm::drop_begin(Args
),
6628 [](const char *A
) { return A
!= nullptr; });
6629 if (FirstArg
!= Args
.end() && StringRef(*FirstArg
).startswith("-cc1")) {
6630 // If -cc1 came from a response file, remove the EOL sentinels.
6632 auto newEnd
= std::remove(Args
.begin(), Args
.end(), nullptr);
6633 Args
.resize(newEnd
- Args
.begin());
6637 return llvm::Error::success();