1//===--- Driver.cpp - Clang GCC Compatible Driver -------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "clang/Driver/Driver.h"
10#include "ToolChains/AIX.h"
11#include "ToolChains/AMDGPU.h"
12#include "ToolChains/AVR.h"
13#include "ToolChains/Arch/RISCV.h"
14#include "ToolChains/BareMetal.h"
15#include "ToolChains/CSKYToolChain.h"
16#include "ToolChains/Clang.h"
17#include "ToolChains/CrossWindows.h"
18#include "ToolChains/Cuda.h"
19#include "ToolChains/Cygwin.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/LFILinux.h"
32#include "ToolChains/Lanai.h"
33#include "ToolChains/Linux.h"
34#include "ToolChains/MSP430.h"
35#include "ToolChains/MSVC.h"
36#include "ToolChains/Managarm.h"
37#include "ToolChains/MinGW.h"
38#include "ToolChains/MipsLinux.h"
39#include "ToolChains/NetBSD.h"
40#include "ToolChains/OHOS.h"
41#include "ToolChains/OpenBSD.h"
42#include "ToolChains/PPCFreeBSD.h"
43#include "ToolChains/PPCLinux.h"
44#include "ToolChains/PS4CPU.h"
45#include "ToolChains/SPIRV.h"
46#include "ToolChains/SPIRVOpenMP.h"
47#include "ToolChains/SYCL.h"
48#include "ToolChains/Serenity.h"
49#include "ToolChains/Solaris.h"
50#include "ToolChains/TCE.h"
51#include "ToolChains/UEFI.h"
52#include "ToolChains/VEToolchain.h"
53#include "ToolChains/WebAssembly.h"
54#include "ToolChains/XCore.h"
55#include "ToolChains/ZOS.h"
56#include "clang/Basic/DiagnosticDriver.h"
57#include "clang/Basic/TargetID.h"
58#include "clang/Basic/Version.h"
59#include "clang/Config/config.h"
60#include "clang/Driver/Action.h"
61#include "clang/Driver/CommonArgs.h"
62#include "clang/Driver/Compilation.h"
63#include "clang/Driver/InputInfo.h"
64#include "clang/Driver/Job.h"
65#include "clang/Driver/ModulesDriver.h"
66#include "clang/Driver/Phases.h"
67#include "clang/Driver/SanitizerArgs.h"
68#include "clang/Driver/Tool.h"
69#include "clang/Driver/ToolChain.h"
70#include "clang/Driver/Types.h"
71#include "clang/Options/OptionUtils.h"
72#include "clang/Options/Options.h"
73#include "clang/ScalableStaticAnalysis/Core/Serialization/SerializationFormatRegistry.h"
74#include "clang/ScalableStaticAnalysis/Core/TUSummary/ExtractorRegistry.h"
75#include "clang/ScalableStaticAnalysis/SSAFForceLinker.h" // IWYU pragma: keep
76#include "llvm/ADT/ArrayRef.h"
77#include "llvm/ADT/STLExtras.h"
78#include "llvm/ADT/ScopeExit.h"
79#include "llvm/ADT/SmallSet.h"
80#include "llvm/ADT/SmallVector.h"
81#include "llvm/ADT/StringExtras.h"
82#include "llvm/ADT/StringRef.h"
83#include "llvm/ADT/StringSet.h"
84#include "llvm/ADT/StringSwitch.h"
85#include "llvm/Config/llvm-config.h"
86#include "llvm/MC/TargetRegistry.h"
87#include "llvm/Option/Arg.h"
88#include "llvm/Option/ArgList.h"
89#include "llvm/Option/OptSpecifier.h"
90#include "llvm/Option/OptTable.h"
91#include "llvm/Option/Option.h"
92#include "llvm/Support/CommandLine.h"
93#include "llvm/Support/ErrorHandling.h"
94#include "llvm/Support/ExitCodes.h"
95#include "llvm/Support/FileSystem.h"
96#include "llvm/Support/FileUtilities.h"
97#include "llvm/Support/FormatVariadic.h"
98#include "llvm/Support/IOSandbox.h"
99#include "llvm/Support/JSON.h"
100#include "llvm/Support/MD5.h"
101#include "llvm/Support/MemoryBuffer.h"
102#include "llvm/Support/Path.h"
103#include "llvm/Support/PrettyStackTrace.h"
104#include "llvm/Support/Process.h"
105#include "llvm/Support/Program.h"
106#include "llvm/Support/Regex.h"
107#include "llvm/Support/StringSaver.h"
108#include "llvm/Support/TarWriter.h"
109#include "llvm/Support/VirtualFileSystem.h"
110#include "llvm/Support/raw_ostream.h"
111#include "llvm/TargetParser/Host.h"
112#include "llvm/TargetParser/RISCVISAInfo.h"
113#include <cstdlib> // ::getenv
114#include <map>
115#include <memory>
116#include <optional>
117#include <set>
118#include <string>
119#include <utility>
120#if LLVM_ON_UNIX
121#include <unistd.h> // getpid
122#endif
123
124using namespace clang::driver;
125using namespace clang;
126using namespace llvm::opt;
127
128template <typename F> static bool usesInput(const ArgList &Args, F &&Fn) {
129 return llvm::any_of(Args, [&](Arg *A) {
130 return (A->getOption().matches(ID: options::OPT_x) &&
131 Fn(types::lookupTypeForTypeSpecifier(Name: A->getValue()))) ||
132 (A->getOption().getKind() == Option::InputClass &&
133 StringRef(A->getValue()).rfind(C: '.') != StringRef::npos &&
134 Fn(types::lookupTypeForExtension(
135 Ext: &A->getValue()[StringRef(A->getValue()).rfind(C: '.') + 1])));
136 });
137}
138
139static bool isIncludeDirArg(StringRef Arg) {
140 return Arg == "-internal-isystem" || Arg == "-internal-externc-isystem" ||
141 Arg == "-isystem" || Arg == "-cxx-isystem" || Arg == "-idirafter";
142}
143
144static void printCXXStdlibIncludeDirs(const ToolChain &TC,
145 const ArgList &Args) {
146 ArgStringList CC1Args;
147 if (Args.hasArg(Ids: options::OPT_stdlibxx_isystem))
148 TC.AddClangCXXStdlibIsystemArgs(DriverArgs: Args, CC1Args);
149 else
150 TC.AddClangCXXStdlibIncludeArgs(DriverArgs: Args, CC1Args);
151
152 for (size_t I = 0; I < CC1Args.size(); ++I) {
153 StringRef Arg(CC1Args[I]);
154 if (isIncludeDirArg(Arg) && I + 1 < CC1Args.size())
155 llvm::outs() << CC1Args[++I] << '\n';
156 }
157}
158
159CUIDOptions::CUIDOptions(llvm::opt::DerivedArgList &Args, const Driver &D)
160 : UseCUID(Kind::Hash) {
161 if (Arg *A = Args.getLastArg(Ids: options::OPT_fuse_cuid_EQ)) {
162 StringRef UseCUIDStr = A->getValue();
163 UseCUID = llvm::StringSwitch<Kind>(UseCUIDStr)
164 .Case(S: "hash", Value: Kind::Hash)
165 .Case(S: "random", Value: Kind::Random)
166 .Case(S: "none", Value: Kind::None)
167 .Default(Value: Kind::Invalid);
168 if (UseCUID == Kind::Invalid)
169 D.Diag(DiagID: clang::diag::err_drv_invalid_value)
170 << A->getAsString(Args) << UseCUIDStr;
171 }
172
173 FixedCUID = Args.getLastArgValue(Id: options::OPT_cuid_EQ);
174 if (!FixedCUID.empty())
175 UseCUID = Kind::Fixed;
176}
177
178std::string CUIDOptions::getCUID(StringRef InputFile,
179 llvm::opt::DerivedArgList &Args) const {
180 std::string CUID = FixedCUID.str();
181 if (CUID.empty()) {
182 if (UseCUID == Kind::Random)
183 CUID = llvm::utohexstr(X: llvm::sys::Process::GetRandomNumber(),
184 /*LowerCase=*/true);
185 else if (UseCUID == Kind::Hash) {
186 llvm::MD5 Hasher;
187 llvm::MD5::MD5Result Hash;
188 Hasher.update(Str: InputFile);
189 for (auto *A : Args) {
190 if (A->getOption().matches(ID: options::OPT_INPUT))
191 continue;
192 Hasher.update(Str: A->getAsString(Args));
193 }
194 Hasher.final(Result&: Hash);
195 CUID = llvm::utohexstr(X: Hash.low(), /*LowerCase=*/true);
196 }
197 }
198 return CUID;
199}
200Driver::Driver(StringRef DriverExecutable, StringRef TargetTriple,
201 DiagnosticsEngine &Diags, std::string Title,
202 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS)
203 : Diags(Diags), VFS(std::move(VFS)), Mode(GCCMode),
204 SaveTemps(SaveTempsNone), BitcodeEmbed(EmbedNone),
205 Offload(OffloadHostDevice), CXX20HeaderType(HeaderMode_None),
206 ModulesModeCXX20(false), DriverExecutable(DriverExecutable),
207 SysRoot(DEFAULT_SYSROOT), DriverTitle(Title), CCCPrintBindings(false),
208 CCPrintOptions(false), CCLogDiagnostics(false), CCGenDiagnostics(false),
209 CCPrintProcessStats(false), CCPrintInternalStats(false),
210 TargetTriple(TargetTriple), Saver(Alloc), PrependArg(nullptr),
211 PreferredLinker(CLANG_DEFAULT_LINKER), CheckInputsExist(true),
212 ProbePrecompiled(true), SuppressMissingInputWarning(false) {
213 // Provide a sane fallback if no VFS is specified.
214 if (!this->VFS)
215 this->VFS = llvm::vfs::getRealFileSystem();
216
217 Name = std::string(llvm::sys::path::filename(path: DriverExecutable));
218 Dir = std::string(llvm::sys::path::parent_path(path: DriverExecutable));
219
220 if ((!SysRoot.empty()) && llvm::sys::path::is_relative(path: SysRoot)) {
221 // Prepend InstalledDir if SysRoot is relative
222 SmallString<128> P(Dir);
223 llvm::sys::path::append(path&: P, a: SysRoot);
224 SysRoot = std::string(P);
225 }
226
227#if defined(CLANG_CONFIG_FILE_SYSTEM_DIR)
228 if (llvm::sys::path::is_absolute(CLANG_CONFIG_FILE_SYSTEM_DIR)) {
229 SystemConfigDir = CLANG_CONFIG_FILE_SYSTEM_DIR;
230 } else {
231 SmallString<128> configFileDir(Dir);
232 llvm::sys::path::append(configFileDir, CLANG_CONFIG_FILE_SYSTEM_DIR);
233 llvm::sys::path::remove_dots(configFileDir, true);
234 SystemConfigDir = static_cast<std::string>(configFileDir);
235 }
236#endif
237#if defined(CLANG_CONFIG_FILE_USER_DIR)
238 {
239 SmallString<128> P;
240 llvm::sys::fs::expand_tilde(CLANG_CONFIG_FILE_USER_DIR, P);
241 UserConfigDir = static_cast<std::string>(P);
242 }
243#endif
244
245 // Compute the path to the resource directory.
246 ResourceDir = GetResourcesPath(BinaryPath: DriverExecutable);
247}
248
249void Driver::setDriverMode(StringRef Value) {
250 static StringRef OptName =
251 getOpts().getOption(Opt: options::OPT_driver_mode).getPrefixedName();
252 if (auto M = llvm::StringSwitch<std::optional<DriverMode>>(Value)
253 .Case(S: "gcc", Value: GCCMode)
254 .Case(S: "g++", Value: GXXMode)
255 .Case(S: "cpp", Value: CPPMode)
256 .Case(S: "cl", Value: CLMode)
257 .Case(S: "flang", Value: FlangMode)
258 .Case(S: "dxc", Value: DXCMode)
259 .Default(Value: std::nullopt))
260 Mode = *M;
261 else
262 Diag(DiagID: diag::err_drv_unsupported_option_argument) << OptName << Value;
263}
264
265InputArgList Driver::ParseArgStrings(ArrayRef<const char *> ArgStrings,
266 bool UseDriverMode,
267 bool &ContainsError) const {
268 llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
269 ContainsError = false;
270
271 llvm::opt::Visibility VisibilityMask = getOptionVisibilityMask(UseDriverMode);
272 unsigned MissingArgIndex, MissingArgCount;
273 InputArgList Args = getOpts().ParseArgs(Args: ArgStrings, MissingArgIndex,
274 MissingArgCount, VisibilityMask);
275
276 // Check for missing argument error.
277 if (MissingArgCount) {
278 Diag(DiagID: diag::err_drv_missing_argument)
279 << Args.getArgString(Index: MissingArgIndex) << MissingArgCount;
280 ContainsError |=
281 Diags.getDiagnosticLevel(DiagID: diag::err_drv_missing_argument,
282 Loc: SourceLocation()) > DiagnosticsEngine::Warning;
283 }
284
285 // Check for unsupported options.
286 for (const Arg *A : Args) {
287 if (A->getOption().hasFlag(Val: options::Unsupported)) {
288 Diag(DiagID: diag::err_drv_unsupported_opt) << A->getAsString(Args);
289 ContainsError |= Diags.getDiagnosticLevel(DiagID: diag::err_drv_unsupported_opt,
290 Loc: SourceLocation()) >
291 DiagnosticsEngine::Warning;
292 continue;
293 }
294
295 // Warn about -mcpu= without an argument.
296 if (A->getOption().matches(ID: options::OPT_mcpu_EQ) && A->containsValue(Value: "")) {
297 Diag(DiagID: diag::warn_drv_empty_joined_argument) << A->getAsString(Args);
298 ContainsError |= Diags.getDiagnosticLevel(
299 DiagID: diag::warn_drv_empty_joined_argument,
300 Loc: SourceLocation()) > DiagnosticsEngine::Warning;
301 }
302 }
303
304 for (const Arg *A : Args.filtered(Ids: options::OPT_UNKNOWN)) {
305 unsigned DiagID;
306 auto ArgString = A->getAsString(Args);
307 std::string Nearest;
308 if (getOpts().findNearest(Option: ArgString, NearestString&: Nearest, VisibilityMask) > 1) {
309 if (IsFlangMode()) {
310 if (getOpts().findExact(Option: ArgString, ExactString&: Nearest,
311 VisibilityMask: llvm::opt::Visibility(options::FC1Option))) {
312 DiagID = diag::err_drv_unknown_argument_with_suggestion;
313 Diags.Report(DiagID) << ArgString << "-Xflang " + Nearest;
314 } else {
315 DiagID = diag::err_drv_unknown_argument;
316 Diags.Report(DiagID) << ArgString;
317 }
318 } else if (!IsCLMode() && getOpts().findExact(Option: ArgString, ExactString&: Nearest,
319 VisibilityMask: llvm::opt::Visibility(
320 options::CC1Option))) {
321 DiagID = diag::err_drv_unknown_argument_with_suggestion;
322 Diags.Report(DiagID) << ArgString << "-Xclang " + Nearest;
323 } else {
324 DiagID = IsCLMode() ? diag::warn_drv_unknown_argument_clang_cl
325 : diag::err_drv_unknown_argument;
326 Diags.Report(DiagID) << ArgString;
327 }
328 } else {
329 DiagID = IsCLMode()
330 ? diag::warn_drv_unknown_argument_clang_cl_with_suggestion
331 : diag::err_drv_unknown_argument_with_suggestion;
332 Diags.Report(DiagID) << ArgString << Nearest;
333 }
334 ContainsError |= Diags.getDiagnosticLevel(DiagID, Loc: SourceLocation()) >
335 DiagnosticsEngine::Warning;
336 }
337
338 for (const Arg *A : Args.filtered(Ids: options::OPT_o)) {
339 if (ArgStrings[A->getIndex()] == A->getSpelling())
340 continue;
341
342 // Warn on joined arguments that are similar to a long argument.
343 std::string ArgString = ArgStrings[A->getIndex()];
344 std::string Nearest;
345 if (getOpts().findExact(Option: "-" + ArgString, ExactString&: Nearest, VisibilityMask))
346 Diags.Report(DiagID: diag::warn_drv_potentially_misspelled_joined_argument)
347 << A->getAsString(Args) << Nearest;
348 }
349
350 return Args;
351}
352
353// Determine which compilation mode we are in. We look for options which
354// affect the phase, starting with the earliest phases, and record which
355// option we used to determine the final phase.
356phases::ID Driver::getFinalPhase(const DerivedArgList &DAL,
357 Arg **FinalPhaseArg) const {
358 Arg *PhaseArg = nullptr;
359 phases::ID FinalPhase;
360
361 // -{E,EP,P,M,MM} only run the preprocessor.
362 if (CCCIsCPP() || (PhaseArg = DAL.getLastArg(Ids: options::OPT_E)) ||
363 (PhaseArg = DAL.getLastArg(Ids: options::OPT__SLASH_EP)) ||
364 (PhaseArg = DAL.getLastArg(Ids: options::OPT_M, Ids: options::OPT_MM)) ||
365 (PhaseArg = DAL.getLastArg(Ids: options::OPT__SLASH_P)) ||
366 CCGenDiagnostics) {
367 FinalPhase = phases::Preprocess;
368
369 // --precompile only runs up to precompilation.
370 // Options that cause the output of C++20 compiled module interfaces or
371 // header units have the same effect.
372 } else if ((PhaseArg = DAL.getLastArg(Ids: options::OPT__precompile)) ||
373 (PhaseArg =
374 DAL.getLastArg(Ids: options::OPT__precompile_reduced_bmi)) ||
375 (PhaseArg = DAL.getLastArg(Ids: options::OPT_extract_api)) ||
376 (PhaseArg = DAL.getLastArg(Ids: options::OPT_fmodule_header,
377 Ids: options::OPT_fmodule_header_EQ))) {
378 FinalPhase = phases::Precompile;
379 // -{fsyntax-only,-analyze,emit-ast} only run up to the compiler.
380 } else if ((PhaseArg = DAL.getLastArg(Ids: options::OPT_fsyntax_only)) ||
381 (PhaseArg = DAL.getLastArg(Ids: options::OPT_print_supported_cpus)) ||
382 (PhaseArg =
383 DAL.getLastArg(Ids: options::OPT_print_enabled_extensions)) ||
384 (PhaseArg = DAL.getLastArg(Ids: options::OPT_module_file_info)) ||
385 (PhaseArg = DAL.getLastArg(Ids: options::OPT_verify_pch)) ||
386 (PhaseArg = DAL.getLastArg(Ids: options::OPT_rewrite_objc)) ||
387 (PhaseArg = DAL.getLastArg(Ids: options::OPT_rewrite_legacy_objc)) ||
388 (PhaseArg = DAL.getLastArg(Ids: options::OPT__analyze)) ||
389 (PhaseArg = DAL.getLastArg(Ids: options::OPT_emit_cir)) ||
390 (PhaseArg = DAL.getLastArg(Ids: options::OPT_emit_ast))) {
391 FinalPhase = phases::Compile;
392
393 // -S only runs up to the backend.
394 } else if ((PhaseArg = DAL.getLastArg(Ids: options::OPT_S))) {
395 FinalPhase = phases::Backend;
396
397 // -c compilation only runs up to the assembler.
398 } else if ((PhaseArg = DAL.getLastArg(Ids: options::OPT_c))) {
399 FinalPhase = phases::Assemble;
400
401 } else if ((PhaseArg = DAL.getLastArg(Ids: options::OPT_emit_interface_stubs))) {
402 FinalPhase = phases::IfsMerge;
403
404 // Otherwise do everything.
405 } else
406 FinalPhase = phases::Link;
407
408 if (FinalPhaseArg)
409 *FinalPhaseArg = PhaseArg;
410
411 return FinalPhase;
412}
413
414llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
415Driver::executeProgram(llvm::ArrayRef<llvm::StringRef> Args) const {
416 llvm::SmallString<64> OutputFile;
417 llvm::sys::fs::createTemporaryFile(Prefix: "driver-program", Suffix: "txt", ResultPath&: OutputFile,
418 Flags: llvm::sys::fs::OF_Text);
419 llvm::FileRemover OutputRemover(OutputFile.c_str());
420 std::optional<llvm::StringRef> Redirects[] = {
421 {""},
422 OutputFile.str(),
423 {""},
424 };
425
426 std::string ErrorMessage;
427 int SecondsToWait = 60;
428 if (std::optional<std::string> Str =
429 llvm::sys::Process::GetEnv(name: "CLANG_TOOLCHAIN_PROGRAM_TIMEOUT")) {
430 if (!llvm::to_integer(S: *Str, Num&: SecondsToWait))
431 return llvm::createStringError(EC: std::error_code(),
432 S: "CLANG_TOOLCHAIN_PROGRAM_TIMEOUT expected "
433 "an integer, got '" +
434 *Str + "'");
435 SecondsToWait = std::max(a: SecondsToWait, b: 0); // infinite
436 }
437 StringRef Executable = Args[0];
438 if (llvm::sys::ExecuteAndWait(Program: Executable, Args, Env: {}, Redirects, SecondsToWait,
439 /*MemoryLimit=*/0, ErrMsg: &ErrorMessage))
440 return llvm::createStringError(EC: std::error_code(),
441 S: Executable + ": " + ErrorMessage);
442
443 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> OutputBuf =
444 llvm::MemoryBuffer::getFile(Filename: OutputFile.c_str());
445 if (!OutputBuf)
446 return llvm::createStringError(EC: OutputBuf.getError(),
447 S: "Failed to read stdout of " + Executable +
448 ": " + OutputBuf.getError().message());
449 return std::move(*OutputBuf);
450}
451
452Arg *clang::driver::makeInputArg(DerivedArgList &Args, const OptTable &Opts,
453 StringRef Value, bool Claim) {
454 Arg *A = new Arg(Opts.getOption(Opt: options::OPT_INPUT), Value,
455 Args.getBaseArgs().MakeIndex(String0: Value), Value.data());
456 Args.AddSynthesizedArg(A);
457 if (Claim)
458 A->claim();
459 return A;
460}
461
462DerivedArgList *Driver::TranslateInputArgs(const InputArgList &Args) const {
463 const llvm::opt::OptTable &Opts = getOpts();
464 DerivedArgList *DAL = new DerivedArgList(Args);
465
466 bool HasNostdlib = Args.hasArg(Ids: options::OPT_nostdlib);
467 bool HasNostdlibxx = Args.hasArg(Ids: options::OPT_nostdlibxx);
468 bool HasNodefaultlib = Args.hasArg(Ids: options::OPT_nodefaultlibs);
469 bool IgnoreUnused = false;
470 for (Arg *A : Args) {
471 if (IgnoreUnused)
472 A->claim();
473
474 if (A->getOption().matches(ID: options::OPT_start_no_unused_arguments)) {
475 IgnoreUnused = true;
476 continue;
477 }
478 if (A->getOption().matches(ID: options::OPT_end_no_unused_arguments)) {
479 IgnoreUnused = false;
480 continue;
481 }
482
483 // Unfortunately, we have to parse some forwarding options (-Xassembler,
484 // -Xlinker, -Xpreprocessor) because we either integrate their functionality
485 // (assembler and preprocessor), or bypass a previous driver ('collect2').
486
487 // Rewrite linker options, to replace --no-demangle with a custom internal
488 // option.
489 if ((A->getOption().matches(ID: options::OPT_Wl_COMMA) ||
490 A->getOption().matches(ID: options::OPT_Xlinker)) &&
491 A->containsValue(Value: "--no-demangle")) {
492 // Add the rewritten no-demangle argument.
493 DAL->AddFlagArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_Z_Xlinker__no_demangle));
494
495 // Add the remaining values as Xlinker arguments.
496 for (StringRef Val : A->getValues())
497 if (Val != "--no-demangle")
498 DAL->AddSeparateArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_Xlinker), Value: Val);
499
500 continue;
501 }
502
503 // Rewrite preprocessor options, to replace -Wp,-MD,FOO which is used by
504 // some build systems. We don't try to be complete here because we don't
505 // care to encourage this usage model.
506 if (A->getOption().matches(ID: options::OPT_Wp_COMMA) &&
507 A->getNumValues() > 0 &&
508 (A->getValue(N: 0) == StringRef("-MD") ||
509 A->getValue(N: 0) == StringRef("-MMD"))) {
510 // Rewrite to -MD/-MMD along with -MF.
511 if (A->getValue(N: 0) == StringRef("-MD"))
512 DAL->AddFlagArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_MD));
513 else
514 DAL->AddFlagArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_MMD));
515 if (A->getNumValues() == 2)
516 DAL->AddSeparateArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_MF), Value: A->getValue(N: 1));
517 continue;
518 }
519
520 // Rewrite reserved library names.
521 if (A->getOption().matches(ID: options::OPT_l)) {
522 StringRef Value = A->getValue();
523
524 // Rewrite unless -nostdlib is present.
525 if (!HasNostdlib && !HasNodefaultlib && !HasNostdlibxx &&
526 Value == "stdc++") {
527 DAL->AddFlagArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_Z_reserved_lib_stdcxx));
528 continue;
529 }
530
531 // Rewrite unconditionally.
532 if (Value == "cc_kext") {
533 DAL->AddFlagArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_Z_reserved_lib_cckext));
534 continue;
535 }
536 }
537
538 // Pick up inputs via the -- option.
539 if (A->getOption().matches(ID: options::OPT__DASH_DASH)) {
540 A->claim();
541 for (StringRef Val : A->getValues())
542 DAL->append(A: makeInputArg(Args&: *DAL, Opts, Value: Val, Claim: false));
543 continue;
544 }
545
546 DAL->append(A);
547 }
548
549 // DXC mode quits before assembly if an output object file isn't specified.
550 if (IsDXCMode() && !Args.hasArg(Ids: options::OPT_dxc_Fo))
551 DAL->AddFlagArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_S));
552
553 // Enforce -static if -miamcu is present.
554 if (Args.hasFlag(Pos: options::OPT_miamcu, Neg: options::OPT_mno_iamcu, Default: false))
555 DAL->AddFlagArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_static));
556
557// Add a default value of -mlinker-version=, if one was given and the user
558// didn't specify one.
559#if defined(HOST_LINK_VERSION)
560 if (!Args.hasArg(options::OPT_mlinker_version_EQ) &&
561 strlen(HOST_LINK_VERSION) > 0) {
562 DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mlinker_version_EQ),
563 HOST_LINK_VERSION);
564 DAL->getLastArg(options::OPT_mlinker_version_EQ)->claim();
565 }
566#endif
567
568 return DAL;
569}
570
571static void setZosTargetVersion(const Driver &D, llvm::Triple &Target,
572 StringRef ArgTarget) {
573
574 static bool BeSilent = false;
575 auto IsTooOldToBeSupported = [](int v, int r) -> bool { return v < 3; };
576
577 /* expect CURRENT, zOSVnRn, or 0xnnnnnnnn */
578 if (ArgTarget.equals_insensitive(RHS: "CURRENT")) {
579 /* If the user gives CURRENT, then we rely on the LE to set */
580 /* __TARGET_LIB__. There's nothing more we need to do. */
581 } else {
582 unsigned int Version = 0;
583 unsigned int Release = 0;
584 unsigned int Modification = 0;
585 bool IsOk = true;
586 llvm::Regex ZOsvRegex("[zZ][oO][sS][vV]([0-9])[rR]([0-9])");
587 llvm::Regex HexRegex(
588 "0x4" /* product */
589 "([0-9a-fA-F])" /* version */
590 "([0-9a-fA-F][0-9a-fA-F])" /* release */
591 "([0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])" /* modification */);
592 SmallVector<StringRef> Matches;
593
594 if (ZOsvRegex.match(String: ArgTarget, Matches: &Matches)) {
595 Matches[1].getAsInteger(Radix: 10, Result&: Version);
596 Matches[2].getAsInteger(Radix: 10, Result&: Release);
597 Modification = 0;
598 if (IsTooOldToBeSupported(Version, Release)) {
599 if (!BeSilent)
600 D.Diag(DiagID: diag::err_zos_target_release_discontinued) << ArgTarget;
601 IsOk = false;
602 }
603 } else if (HexRegex.match(String: ArgTarget, Matches: &Matches)) {
604 Matches[1].getAsInteger(Radix: 16, Result&: Version);
605 Matches[2].getAsInteger(Radix: 16, Result&: Release);
606 Matches[3].getAsInteger(Radix: 16, Result&: Modification);
607 if (IsTooOldToBeSupported(Version, Release)) {
608 if (!BeSilent)
609 D.Diag(DiagID: diag::err_zos_target_release_discontinued) << ArgTarget;
610 IsOk = false;
611 }
612 } else {
613 /* something else: need to report an error */
614 if (!BeSilent)
615 D.Diag(DiagID: diag::err_zos_target_unrecognized_release) << ArgTarget;
616 IsOk = false;
617 }
618
619 if (IsOk) {
620 llvm::VersionTuple V(Version, Release, Modification);
621 llvm::VersionTuple TV = Target.getOSVersion();
622 // The goal is to pick the minimally supported version of
623 // the OS. Pick the lesser as the target.
624 if (TV.empty() || V < TV) {
625 SmallString<16> Str;
626 Str = llvm::Triple::getOSTypeName(Kind: Target.getOS());
627 Str += V.getAsString();
628 Target.setOSName(Str);
629 }
630 }
631 }
632 BeSilent = true;
633}
634
635/// Compute target triple from args.
636///
637/// This routine provides the logic to compute a target triple from various
638/// args passed to the driver and the default triple string.
639static llvm::Triple computeTargetTriple(const Driver &D, StringRef TargetTriple,
640 const ArgList &Args,
641 StringRef ArchName = "") {
642 // FIXME: Already done in Compilation *Driver::BuildCompilation
643 if (const Arg *A = Args.getLastArg(Ids: options::OPT_target))
644 TargetTriple = A->getValue();
645
646 llvm::Triple Target(llvm::Triple::normalize(Str: TargetTriple));
647
648 // GNU/Hurd's triples should have been -hurd-gnu*, but were historically made
649 // -gnu* only, and we can not change this, so we have to detect that case as
650 // being the Hurd OS.
651 if (TargetTriple.contains(Other: "-unknown-gnu") || TargetTriple.contains(Other: "-pc-gnu"))
652 Target.setOSName("hurd");
653
654 // Handle Apple-specific options available here.
655 if (Target.isOSBinFormatMachO()) {
656 // If an explicit Darwin arch name is given, that trumps all.
657 if (!ArchName.empty()) {
658 tools::darwin::setTripleTypeForMachOArchName(T&: Target, Str: ArchName, Args);
659 return llvm::Triple(Target.normalize());
660 }
661
662 // Handle the Darwin '-arch' flag.
663 if (Arg *A = Args.getLastArg(Ids: options::OPT_arch)) {
664 StringRef ArchName = A->getValue();
665 tools::darwin::setTripleTypeForMachOArchName(T&: Target, Str: ArchName, Args);
666 }
667 } else if (!ArchName.empty()) {
668 Target.setArchName(ArchName);
669 return Target;
670 }
671
672 // Handle pseudo-target flags '-mlittle-endian'/'-EL' and
673 // '-mbig-endian'/'-EB'.
674 if (Arg *A = Args.getLastArgNoClaim(Ids: options::OPT_mlittle_endian,
675 Ids: options::OPT_mbig_endian)) {
676 llvm::Triple T = A->getOption().matches(ID: options::OPT_mlittle_endian)
677 ? Target.getLittleEndianArchVariant()
678 : Target.getBigEndianArchVariant();
679 if (T.getArch() != llvm::Triple::UnknownArch) {
680 Target = llvm::Triple(T.normalize());
681 Args.claimAllArgs(Ids: options::OPT_mlittle_endian, Ids: options::OPT_mbig_endian);
682 }
683 }
684
685 // Skip further flag support on OSes which don't support '-m32' or '-m64'.
686 if (Target.getArch() == llvm::Triple::tce)
687 return Target;
688
689 // On AIX, the env OBJECT_MODE may affect the resulting arch variant.
690 // However, if --target was explicitly specified, it takes precedence.
691#ifdef _AIX
692 if (!Args.hasArg(options::OPT_target)) {
693 if (std::optional<std::string> ObjectModeValue =
694 llvm::sys::Process::GetEnv("OBJECT_MODE")) {
695 StringRef ObjectMode = *ObjectModeValue;
696 llvm::Triple::ArchType AT = llvm::Triple::UnknownArch;
697
698 if (D.IsFlangMode()) {
699 if (ObjectMode == "64") {
700 AT = Target.get64BitArchVariant().getArch();
701 } else if (ObjectMode == "32" || ObjectMode == "32_64" ||
702 ObjectMode == "any") {
703 // OBJECT_MODE setting can be overridden by -maix64/-m64
704 if (Args.hasArg(options::OPT_maix64, options::OPT_m64))
705 AT = Target.get64BitArchVariant().getArch();
706 else
707 D.Diag(diag::err_drv_compile_mode_unsupported_aix);
708 } else {
709 D.Diag(diag::err_drv_invalid_object_mode) << ObjectMode;
710 }
711 } else {
712 // Silently accept '32_64' and 'any'
713 const bool OtherAllowedMode =
714 ObjectMode == "32_64" || ObjectMode == "any";
715 if (ObjectMode == "64")
716 AT = Target.get64BitArchVariant().getArch();
717 else if (ObjectMode == "32")
718 AT = Target.get32BitArchVariant().getArch();
719 else if (!OtherAllowedMode)
720 D.Diag(diag::err_drv_invalid_object_mode) << ObjectMode;
721 }
722
723 if (AT != llvm::Triple::UnknownArch && AT != Target.getArch()) {
724 Target.setArch(AT);
725 Target = llvm::Triple(Target.normalize());
726 }
727 } else if (D.IsFlangMode() &&
728 !Args.hasArg(options::OPT_maix64, options::OPT_m64)) {
729 // For flang on AIX, if OBJECT_MODE is unset and neither
730 // -maix64 nor -m64 is specified, issue an error.
731 D.Diag(diag::err_drv_compile_mode_unsupported_aix);
732 }
733 }
734#endif
735
736 // Currently the only architecture supported by *-uefi triples are x86_64.
737 if (Target.isUEFI() && Target.getArch() != llvm::Triple::x86_64)
738 D.Diag(DiagID: diag::err_target_unknown_triple) << Target.str();
739
740 // The `-maix[32|64]` flags are only valid for AIX targets.
741 if (Arg *A = Args.getLastArgNoClaim(Ids: options::OPT_maix32, Ids: options::OPT_maix64);
742 A && !Target.isOSAIX())
743 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
744 << A->getAsString(Args) << Target.str();
745
746 // Handle pseudo-target flags '-m64', '-mx32', '-m32' and '-m16'.
747 Arg *A = Args.getLastArg(Ids: options::OPT_m64, Ids: options::OPT_mx32,
748 Ids: options::OPT_m32, Ids: options::OPT_m16,
749 Ids: options::OPT_maix32, Ids: options::OPT_maix64);
750 if (A) {
751 llvm::Triple::ArchType AT = llvm::Triple::UnknownArch;
752
753 if (A->getOption().matches(ID: options::OPT_m64) ||
754 A->getOption().matches(ID: options::OPT_maix64)) {
755 AT = Target.get64BitArchVariant().getArch();
756 if (Target.getEnvironment() == llvm::Triple::GNUX32 ||
757 Target.getEnvironment() == llvm::Triple::GNUT64)
758 Target.setEnvironment(llvm::Triple::GNU);
759 else if (Target.getEnvironment() == llvm::Triple::MuslX32)
760 Target.setEnvironment(llvm::Triple::Musl);
761 } else if (A->getOption().matches(ID: options::OPT_mx32) &&
762 Target.get64BitArchVariant().getArch() == llvm::Triple::x86_64) {
763 AT = llvm::Triple::x86_64;
764 if (Target.getEnvironment() == llvm::Triple::Musl)
765 Target.setEnvironment(llvm::Triple::MuslX32);
766 else
767 Target.setEnvironment(llvm::Triple::GNUX32);
768 } else if (A->getOption().matches(ID: options::OPT_m32) ||
769 A->getOption().matches(ID: options::OPT_maix32)) {
770 if (D.IsFlangMode()) {
771 if (Target.isOSAIX()) {
772 D.Diag(DiagID: diag::err_drv_compile_mode_unsupported_aix);
773 } else {
774 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
775 << A->getAsString(Args) << Target.str();
776 }
777 } else {
778 AT = Target.get32BitArchVariant().getArch();
779 if (Target.getEnvironment() == llvm::Triple::GNUX32)
780 Target.setEnvironment(llvm::Triple::GNU);
781 else if (Target.getEnvironment() == llvm::Triple::MuslX32)
782 Target.setEnvironment(llvm::Triple::Musl);
783 }
784 } else if (A->getOption().matches(ID: options::OPT_m16) &&
785 Target.get32BitArchVariant().getArch() == llvm::Triple::x86) {
786 AT = llvm::Triple::x86;
787 Target.setEnvironment(llvm::Triple::CODE16);
788 }
789
790 if (AT != llvm::Triple::UnknownArch && AT != Target.getArch()) {
791 Target.setArch(Kind: AT);
792 if (Target.isWindowsGNUEnvironment())
793 toolchains::MinGW::fixTripleArch(D, Triple&: Target, Args);
794 }
795
796 Target = llvm::Triple(Target.normalize());
797 }
798
799 if (Target.isOSzOS()) {
800 if ((A = Args.getLastArg(Ids: options::OPT_mzos_target_EQ))) {
801 setZosTargetVersion(D, Target, ArgTarget: A->getValue());
802 }
803 }
804
805 // Handle -miamcu flag.
806 if (Args.hasFlag(Pos: options::OPT_miamcu, Neg: options::OPT_mno_iamcu, Default: false)) {
807 if (Target.get32BitArchVariant().getArch() != llvm::Triple::x86)
808 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target) << "-miamcu"
809 << Target.str();
810
811 if (A && !A->getOption().matches(ID: options::OPT_m32))
812 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with)
813 << "-miamcu" << A->getBaseArg().getAsString(Args);
814
815 Target.setArch(Kind: llvm::Triple::x86);
816 Target.setArchName("i586");
817 Target.setEnvironmentName("");
818 Target.setOS(llvm::Triple::ELFIAMCU);
819 Target.setVendor(llvm::Triple::Intel);
820 }
821
822 // If target is MIPS adjust the target triple
823 // accordingly to provided ABI name.
824 if (Target.isMIPS()) {
825 if ((A = Args.getLastArg(Ids: options::OPT_mabi_EQ))) {
826 StringRef ABIName = A->getValue();
827 if (ABIName == "32") {
828 Target = Target.get32BitArchVariant();
829 if (Target.getEnvironment() == llvm::Triple::GNUABI64 ||
830 Target.getEnvironment() == llvm::Triple::GNUABIN32)
831 Target.setEnvironment(llvm::Triple::GNU);
832 } else if (ABIName == "n32") {
833 Target = Target.get64BitArchVariant();
834 if (Target.getEnvironment() == llvm::Triple::GNU ||
835 Target.getEnvironment() == llvm::Triple::GNUT64 ||
836 Target.getEnvironment() == llvm::Triple::GNUABI64)
837 Target.setEnvironment(llvm::Triple::GNUABIN32);
838 else if (Target.getEnvironment() == llvm::Triple::Musl ||
839 Target.getEnvironment() == llvm::Triple::MuslABI64)
840 Target.setEnvironment(llvm::Triple::MuslABIN32);
841 } else if (ABIName == "64") {
842 Target = Target.get64BitArchVariant();
843 if (Target.getEnvironment() == llvm::Triple::GNU ||
844 Target.getEnvironment() == llvm::Triple::GNUT64 ||
845 Target.getEnvironment() == llvm::Triple::GNUABIN32)
846 Target.setEnvironment(llvm::Triple::GNUABI64);
847 else if (Target.getEnvironment() == llvm::Triple::Musl ||
848 Target.getEnvironment() == llvm::Triple::MuslABIN32)
849 Target.setEnvironment(llvm::Triple::MuslABI64);
850 }
851
852 Target = llvm::Triple(Target.normalize());
853 }
854 }
855
856 // If target is RISC-V adjust the target triple according to
857 // provided architecture name
858 if (Target.isRISCV()) {
859 if (Args.hasArg(Ids: options::OPT_march_EQ) ||
860 Args.hasArg(Ids: options::OPT_mcpu_EQ)) {
861 std::string ArchName = tools::riscv::getRISCVArch(Args, Triple: Target);
862 auto ISAInfo = llvm::RISCVISAInfo::parseArchString(
863 Arch: ArchName, /*EnableExperimentalExtensions=*/EnableExperimentalExtension: true);
864 if (!llvm::errorToBool(Err: ISAInfo.takeError())) {
865 unsigned XLen = (*ISAInfo)->getXLen();
866 if (XLen == 32) {
867 if (Target.isLittleEndian())
868 Target.setArch(Kind: llvm::Triple::riscv32);
869 else
870 Target.setArch(Kind: llvm::Triple::riscv32be);
871 Target = llvm::Triple(Target.normalize());
872 } else if (XLen == 64) {
873 if (Target.isLittleEndian())
874 Target.setArch(Kind: llvm::Triple::riscv64);
875 else
876 Target.setArch(Kind: llvm::Triple::riscv64be);
877 Target = llvm::Triple(Target.normalize());
878 }
879 }
880 }
881 }
882
883 if (Target.getArch() == llvm::Triple::riscv32be ||
884 Target.getArch() == llvm::Triple::riscv64be) {
885 static bool WarnedRISCVBE = false;
886 if (!WarnedRISCVBE) {
887 D.Diag(DiagID: diag::warn_drv_riscv_be_experimental);
888 WarnedRISCVBE = true;
889 }
890 }
891
892 return Target;
893}
894
895/// Compute the desired OpenMP runtime from the flags provided.
896Driver::OpenMPRuntimeKind Driver::getOpenMPRuntime(const ArgList &Args) const {
897 StringRef RuntimeName(CLANG_DEFAULT_OPENMP_RUNTIME);
898
899 const Arg *A = Args.getLastArg(Ids: options::OPT_fopenmp_EQ);
900 if (A)
901 RuntimeName = A->getValue();
902
903 auto RT = llvm::StringSwitch<OpenMPRuntimeKind>(RuntimeName)
904 .Case(S: "libomp", Value: OMPRT_OMP)
905 .Case(S: "libgomp", Value: OMPRT_GOMP)
906 .Case(S: "libiomp5", Value: OMPRT_IOMP5)
907 .Default(Value: OMPRT_Unknown);
908
909 if (RT == OMPRT_Unknown) {
910 if (A)
911 Diag(DiagID: diag::err_drv_unsupported_option_argument)
912 << A->getSpelling() << A->getValue();
913 else
914 // FIXME: We could use a nicer diagnostic here.
915 Diag(DiagID: diag::err_drv_unsupported_opt) << "-fopenmp";
916 }
917
918 return RT;
919}
920
921// Handles `native` offload architectures by using the 'offload-arch' utility.
922static llvm::SmallVector<std::string>
923getSystemOffloadArchs(Compilation &C, Action::OffloadKind Kind) {
924 StringRef Program = C.getArgs().getLastArgValue(
925 Id: options::OPT_offload_arch_tool_EQ, Default: "offload-arch");
926
927 SmallVector<std::string> GPUArchs;
928 if (llvm::ErrorOr<std::string> Executable =
929 llvm::sys::findProgramByName(Name: Program, Paths: {C.getDriver().Dir})) {
930 llvm::SmallVector<StringRef> Args{*Executable};
931 bool UsesLLVMOffloading =
932 C.getArgs().hasFlag(Pos: options::OPT_foffload_via_llvm,
933 Neg: options::OPT_fno_offload_via_llvm, Default: false);
934 if (!UsesLLVMOffloading) {
935 if (Kind == Action::OFK_HIP)
936 Args.push_back(Elt: "--only=amdgpu");
937 else if (Kind == Action::OFK_Cuda)
938 Args.push_back(Elt: "--only=nvptx");
939 }
940 auto StdoutOrErr = C.getDriver().executeProgram(Args);
941
942 if (!StdoutOrErr) {
943 C.getDriver().Diag(DiagID: diag::err_drv_undetermined_gpu_arch)
944 << Action::GetOffloadKindName(Kind) << StdoutOrErr.takeError()
945 << "--offload-arch";
946 return GPUArchs;
947 }
948 if ((*StdoutOrErr)->getBuffer().empty()) {
949 C.getDriver().Diag(DiagID: diag::err_drv_undetermined_gpu_arch)
950 << Action::GetOffloadKindName(Kind) << "No GPU detected in the system"
951 << "--offload-arch";
952 return GPUArchs;
953 }
954
955 for (StringRef Arch : llvm::split(Str: (*StdoutOrErr)->getBuffer(), Separator: "\n"))
956 if (!Arch.empty())
957 GPUArchs.push_back(Elt: Arch.str());
958 } else {
959 C.getDriver().Diag(DiagID: diag::err_drv_command_failure) << "offload-arch";
960 }
961 return GPUArchs;
962}
963
964using TripleSet = std::multiset<llvm::Triple>;
965
966// Attempts to infer the correct offloading toolchain triple by looking at the
967// requested offloading kind and architectures.
968static TripleSet inferOffloadToolchains(Compilation &C,
969 Action::OffloadKind Kind) {
970 std::set<std::string> Archs;
971 for (Arg *A : C.getInputArgs()) {
972 for (StringRef Arch : A->getValues()) {
973 if (A->getOption().matches(ID: options::OPT_offload_arch_EQ)) {
974 if (Arch == "native") {
975 for (StringRef Str : getSystemOffloadArchs(C, Kind))
976 Archs.insert(x: Str.str());
977 } else {
978 Archs.insert(x: Arch.str());
979 }
980 } else if (A->getOption().matches(ID: options::OPT_no_offload_arch_EQ)) {
981 if (Arch == "all")
982 Archs.clear();
983 else
984 Archs.erase(x: Arch.str());
985 }
986 }
987 }
988
989 TripleSet Triples;
990 for (llvm::StringRef Arch : Archs) {
991 OffloadArch ID = StringToOffloadArch(S: Arch);
992 if (ID.isUnknown()) {
993 llvm::Triple AMDGPUTriple(llvm::Triple::amdgpu, llvm::Triple::NoSubArch,
994 llvm::Triple::AMD, llvm::Triple::AMDHSA);
995 ID = StringToOffloadArch(S: getProcessorFromTargetID(T: AMDGPUTriple, OffloadArch: Arch));
996 }
997
998 bool UsesLLVMOffloading =
999 C.getArgs().hasFlag(Pos: options::OPT_foffload_via_llvm,
1000 Neg: options::OPT_fno_offload_via_llvm, Default: false);
1001 if (!UsesLLVMOffloading) {
1002 if (Kind == Action::OFK_HIP && !ID.isAMDGPU() && !ID.isAMDGCNSPIRV()) {
1003 C.getDriver().Diag(DiagID: clang::diag::err_drv_offload_bad_gpu_arch)
1004 << "HIP" << Arch;
1005 return {};
1006 }
1007 if (Kind == Action::OFK_Cuda && !ID.isNVPTX()) {
1008 C.getDriver().Diag(DiagID: clang::diag::err_drv_offload_bad_gpu_arch)
1009 << "CUDA" << Arch;
1010 return {};
1011 }
1012 }
1013 if (Kind == Action::OFK_OpenMP && (ID.isUnknown() || ID.isUnused())) {
1014 C.getDriver().Diag(DiagID: clang::diag::err_drv_failed_to_deduce_target_from_arch)
1015 << Arch;
1016 return {};
1017 }
1018 if (ID.isUnknown() || ID.isUnused()) {
1019 C.getDriver().Diag(DiagID: clang::diag::err_drv_offload_bad_gpu_arch)
1020 << "offload" << Arch;
1021 return {};
1022 }
1023
1024 llvm::Triple Triple =
1025 OffloadArchToTriple(DefaultToolchainTriple: C.getDefaultToolChain().getTriple(), ID);
1026 if (UsesLLVMOffloading)
1027 Triple.setEnvironment(llvm::Triple::LLVM);
1028
1029 // Make a new argument that dispatches this argument to the appropriate
1030 // toolchain. This is required when we infer it and create potentially
1031 // incompatible toolchains from the global option.
1032 Option Opt = C.getDriver().getOpts().getOption(Opt: options::OPT_Xarch__);
1033 unsigned Index = C.getArgs().getBaseArgs().MakeIndex(String0: "-Xarch_");
1034 Arg *A = new Arg(Opt, C.getArgs().getArgString(Index), Index,
1035 C.getArgs().MakeArgString(Str: Triple.getArchName()),
1036 C.getArgs().MakeArgString(Str: "--offload-arch=" + Arch));
1037 A->claim();
1038 C.getArgs().append(A);
1039 C.getArgs().AddSynthesizedArg(A);
1040
1041 auto It = Triples.lower_bound(x: Triple);
1042 if (It == Triples.end() || *It != Triple)
1043 Triples.insert(position: It, x: Triple);
1044 }
1045
1046 // Infer the default target triple if no specific architectures are given.
1047 if (Archs.empty() && Kind == Action::OFK_HIP) {
1048 Triples.insert(x: llvm::Triple(llvm::Triple::amdgpu, llvm::Triple::NoSubArch,
1049 llvm::Triple::AMD, llvm::Triple::AMDHSA));
1050 } else if (Archs.empty() && Kind == Action::OFK_Cuda) {
1051 llvm::Triple::ArchType Arch =
1052 C.getDefaultToolChain().getTriple().isArch64Bit()
1053 ? llvm::Triple::nvptx64
1054 : llvm::Triple::nvptx;
1055 Triples.insert(x: llvm::Triple(Arch, llvm::Triple::NoSubArch,
1056 llvm::Triple::NVIDIA, llvm::Triple::CUDA));
1057 } else if (Archs.empty() && Kind == Action::OFK_SYCL)
1058 Triples.insert(
1059 x: llvm::Triple(C.getDefaultToolChain().getTriple().isArch64Bit()
1060 ? llvm::Triple::spirv64
1061 : llvm::Triple::spirv32));
1062
1063 // We need to dispatch these to the appropriate toolchain now.
1064 C.getArgs().eraseArg(Id: options::OPT_offload_arch_EQ);
1065 C.getArgs().eraseArg(Id: options::OPT_no_offload_arch_EQ);
1066
1067 return Triples;
1068}
1069
1070void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
1071 InputList &Inputs) {
1072 bool IsCuda =
1073 llvm::any_of(Range&: Inputs, P: [](std::pair<types::ID, const llvm::opt::Arg *> &I) {
1074 return types::isCuda(Id: I.first);
1075 });
1076 bool IsHIP =
1077 (llvm::any_of(Range&: Inputs,
1078 P: [](std::pair<types::ID, const llvm::opt::Arg *> &I) {
1079 return types::isHIP(Id: I.first);
1080 }) ||
1081 C.getInputArgs().hasArg(Ids: options::OPT_hip_link) ||
1082 C.getInputArgs().hasArg(Ids: options::OPT_hipstdpar));
1083 bool IsSYCL = C.getInputArgs().hasFlag(Pos: options::OPT_fsycl,
1084 Neg: options::OPT_fno_sycl, Default: false);
1085 bool IsOpenMPOffloading =
1086 (C.getInputArgs().hasFlag(Pos: options::OPT_fopenmp, PosAlias: options::OPT_fopenmp_EQ,
1087 Neg: options::OPT_fno_openmp, Default: false) &&
1088 (C.getInputArgs().hasArg(Ids: options::OPT_offload_targets_EQ) ||
1089 (C.getInputArgs().hasArg(Ids: options::OPT_offload_arch_EQ) &&
1090 !(IsCuda || IsHIP))));
1091
1092 llvm::SmallSet<Action::OffloadKind, 4> Kinds;
1093 const std::pair<bool, Action::OffloadKind> ActiveKinds[] = {
1094 {IsCuda, Action::OFK_Cuda},
1095 {IsHIP, Action::OFK_HIP},
1096 {IsOpenMPOffloading, Action::OFK_OpenMP},
1097 {IsSYCL, Action::OFK_SYCL}};
1098 for (const auto &[Active, Kind] : ActiveKinds)
1099 if (Active)
1100 Kinds.insert(V: Kind);
1101
1102 // We currently don't support any kind of mixed offloading.
1103 if (Kinds.size() > 1) {
1104 Diag(DiagID: clang::diag::err_drv_mix_offload)
1105 << Action::GetOffloadKindName(Kind: *Kinds.begin()).upper()
1106 << Action::GetOffloadKindName(Kind: *(++Kinds.begin())).upper();
1107 return;
1108 }
1109
1110 // Initialize the compilation identifier used for unique CUDA / HIP names.
1111 if (IsCuda || IsHIP)
1112 CUIDOpts = CUIDOptions(C.getArgs(), *this);
1113
1114 // Get the list of requested offloading toolchains. If they were not
1115 // explicitly specified we will infer them based on the offloading language
1116 // and requested architectures.
1117 TripleSet Triples;
1118 if (C.getInputArgs().hasArg(Ids: options::OPT_offload_targets_EQ)) {
1119 std::vector<std::string> ArgValues =
1120 C.getInputArgs().getAllArgValues(Id: options::OPT_offload_targets_EQ);
1121 for (llvm::StringRef Target : ArgValues) {
1122 Triples.insert(x: ToolChain::normalizeOffloadTriple(OrigTT: Target));
1123 }
1124
1125 if (ArgValues.empty())
1126 Diag(DiagID: clang::diag::warn_drv_empty_joined_argument)
1127 << C.getInputArgs()
1128 .getLastArg(Ids: options::OPT_offload_targets_EQ)
1129 ->getAsString(Args: C.getInputArgs());
1130 } else {
1131 for (Action::OffloadKind Kind : Kinds)
1132 Triples = inferOffloadToolchains(C, Kind);
1133 }
1134
1135 // Build an offloading toolchain for every requested target and kind.
1136 llvm::StringMap<StringRef> FoundNormalizedTriples;
1137 for (const llvm::Triple &Target : Triples) {
1138 // OpenMP offloading requires a compatible libomp.
1139 if (Kinds.contains(V: Action::OFK_OpenMP)) {
1140 OpenMPRuntimeKind RuntimeKind = getOpenMPRuntime(Args: C.getInputArgs());
1141 if (RuntimeKind != OMPRT_OMP && RuntimeKind != OMPRT_IOMP5) {
1142 Diag(DiagID: clang::diag::err_drv_expecting_fopenmp_with_fopenmp_targets);
1143 return;
1144 }
1145 }
1146
1147 // Certain options are not allowed when combined with SYCL compilation.
1148 if (Kinds.contains(V: Action::OFK_SYCL)) {
1149 for (auto ID :
1150 {options::OPT_static_libstdcxx, options::OPT_ffreestanding})
1151 if (Arg *IncompatArg = C.getInputArgs().getLastArg(Ids: ID))
1152 Diag(DiagID: clang::diag::err_drv_argument_not_allowed_with)
1153 << IncompatArg->getSpelling() << "-fsycl";
1154 }
1155
1156 // Create a device toolchain for every specified kind and triple.
1157 for (Action::OffloadKind Kind : Kinds) {
1158 if (Target.getArch() == llvm::Triple::ArchType::UnknownArch) {
1159 Diag(DiagID: diag::err_drv_invalid_or_unsupported_offload_target)
1160 << Target.str();
1161 continue;
1162 }
1163
1164 std::string NormalizedName = Target.normalize();
1165 auto [TripleIt, Inserted] =
1166 FoundNormalizedTriples.try_emplace(Key: NormalizedName, Args: Target.str());
1167 if (!Inserted) {
1168 Diag(DiagID: clang::diag::warn_drv_omp_offload_target_duplicate)
1169 << Target.str() << TripleIt->second;
1170 continue;
1171 }
1172
1173 auto &TC = getOffloadToolChain(Args: C.getInputArgs(), Kind, Target,
1174 AuxTarget: C.getDefaultToolChain().getTriple());
1175
1176 // Emit a warning if the detected CUDA version is too new.
1177 if (Kind == Action::OFK_Cuda && Target.getOS() == llvm::Triple::CUDA) {
1178 auto &CudaInstallation =
1179 static_cast<const toolchains::CudaToolChain &>(TC).CudaInstallation;
1180 if (CudaInstallation.isValid())
1181 CudaInstallation.WarnIfUnsupportedVersion();
1182 }
1183
1184 C.addOffloadDeviceToolChain(DeviceToolChain: &TC, OffloadKind: Kind);
1185 }
1186 }
1187}
1188
1189bool Driver::loadZOSCustomizationFile(llvm::cl::ExpansionContext &ExpCtx) {
1190 if (IsCLMode() || IsDXCMode() || IsFlangMode())
1191 return false;
1192
1193 SmallString<128> CustomizationFile;
1194 StringRef PathLIBEnv = StringRef(getenv(name: "CLANG_CONFIG_PATH")).trim();
1195 // If the env var is a directory then append "/clang.cfg" and treat
1196 // that as the config file. Otherwise treat the env var as the
1197 // config file.
1198 if (!PathLIBEnv.empty()) {
1199 llvm::sys::path::append(path&: CustomizationFile, a: PathLIBEnv);
1200 if (llvm::sys::fs::is_directory(Path: PathLIBEnv))
1201 llvm::sys::path::append(path&: CustomizationFile, a: "/clang.cfg");
1202 if (llvm::sys::fs::is_regular_file(Path: CustomizationFile))
1203 return readConfigFile(FileName: CustomizationFile, ExpCtx);
1204 Diag(DiagID: diag::err_drv_config_file_not_found) << CustomizationFile;
1205 return true;
1206 }
1207
1208 SmallString<128> BaseDir(llvm::sys::path::parent_path(path: Dir));
1209 llvm::sys::path::append(path&: CustomizationFile, a: BaseDir + "/etc/clang.cfg");
1210 if (llvm::sys::fs::is_regular_file(Path: CustomizationFile))
1211 return readConfigFile(FileName: CustomizationFile, ExpCtx);
1212
1213 // If no customization file, just return
1214 return false;
1215}
1216
1217static void appendOneArg(InputArgList &Args, const Arg *Opt) {
1218 // The args for config files or /clang: flags belong to different InputArgList
1219 // objects than Args. This copies an Arg from one of those other InputArgLists
1220 // to the ownership of Args.
1221 unsigned Index = Args.MakeIndex(String0: Opt->getSpelling());
1222 Arg *Copy = new Arg(Opt->getOption(), Args.getArgString(Index), Index);
1223 Copy->getValues() = Opt->getValues();
1224 if (Opt->isClaimed())
1225 Copy->claim();
1226 Copy->setOwnsValues(Opt->getOwnsValues());
1227 Opt->setOwnsValues(false);
1228 Args.append(A: Copy);
1229 if (Opt->getAlias()) {
1230 const Arg *Alias = Opt->getAlias();
1231 unsigned Index = Args.MakeIndex(String0: Alias->getSpelling());
1232 auto AliasCopy = std::make_unique<Arg>(args: Alias->getOption(),
1233 args: Args.getArgString(Index), args&: Index);
1234 AliasCopy->getValues() = Alias->getValues();
1235 AliasCopy->setOwnsValues(false);
1236 if (Alias->isClaimed())
1237 AliasCopy->claim();
1238 Copy->setAlias(std::move(AliasCopy));
1239 }
1240}
1241
1242bool Driver::readConfigFile(StringRef FileName,
1243 llvm::cl::ExpansionContext &ExpCtx) {
1244 // Try opening the given file.
1245 auto Status = getVFS().status(Path: FileName);
1246 if (!Status) {
1247 Diag(DiagID: diag::err_drv_cannot_open_config_file)
1248 << FileName << Status.getError().message();
1249 return true;
1250 }
1251 if (Status->getType() != llvm::sys::fs::file_type::regular_file) {
1252 Diag(DiagID: diag::err_drv_cannot_open_config_file)
1253 << FileName << "not a regular file";
1254 return true;
1255 }
1256
1257 // Try reading the given file.
1258 SmallVector<const char *, 32> NewCfgFileArgs;
1259 if (llvm::Error Err = ExpCtx.readConfigFile(CfgFile: FileName, Argv&: NewCfgFileArgs)) {
1260 Diag(DiagID: diag::err_drv_cannot_read_config_file)
1261 << FileName << toString(E: std::move(Err));
1262 return true;
1263 }
1264
1265 // Populate head and tail lists. The tail list is used only when linking.
1266 SmallVector<const char *, 32> NewCfgHeadArgs, NewCfgTailArgs;
1267 for (const char *Opt : NewCfgFileArgs) {
1268 // An $-prefixed option should go to the tail list.
1269 if (Opt[0] == '$' && Opt[1])
1270 NewCfgTailArgs.push_back(Elt: Opt + 1);
1271 else
1272 NewCfgHeadArgs.push_back(Elt: Opt);
1273 }
1274
1275 // Read options from config file.
1276 llvm::SmallString<128> CfgFileName(FileName);
1277 llvm::sys::path::native(path&: CfgFileName);
1278 bool ContainErrors = false;
1279 auto NewHeadOptions = std::make_unique<InputArgList>(
1280 args: ParseArgStrings(ArgStrings: NewCfgHeadArgs, /*UseDriverMode=*/true, ContainsError&: ContainErrors));
1281 if (ContainErrors)
1282 return true;
1283 auto NewTailOptions = std::make_unique<InputArgList>(
1284 args: ParseArgStrings(ArgStrings: NewCfgTailArgs, /*UseDriverMode=*/true, ContainsError&: ContainErrors));
1285 if (ContainErrors)
1286 return true;
1287
1288 // Claim all arguments that come from a configuration file so that the driver
1289 // does not warn on any that is unused.
1290 for (Arg *A : *NewHeadOptions)
1291 A->claim();
1292 for (Arg *A : *NewTailOptions)
1293 A->claim();
1294
1295 if (!CfgOptionsHead)
1296 CfgOptionsHead = std::move(NewHeadOptions);
1297 else {
1298 // If this is a subsequent config file, append options to the previous one.
1299 for (auto *Opt : *NewHeadOptions)
1300 appendOneArg(Args&: *CfgOptionsHead, Opt);
1301 }
1302
1303 if (!CfgOptionsTail)
1304 CfgOptionsTail = std::move(NewTailOptions);
1305 else {
1306 // If this is a subsequent config file, append options to the previous one.
1307 for (auto *Opt : *NewTailOptions)
1308 appendOneArg(Args&: *CfgOptionsTail, Opt);
1309 }
1310
1311 ConfigFiles.push_back(x: std::string(CfgFileName));
1312 return false;
1313}
1314
1315bool Driver::loadConfigFiles() {
1316 llvm::cl::ExpansionContext ExpCtx(Saver.getAllocator(),
1317 llvm::cl::tokenizeConfigFile, &getVFS());
1318
1319 // Process options that change search path for config files.
1320 if (CLOptions) {
1321 if (CLOptions->hasArg(Ids: options::OPT_config_system_dir_EQ)) {
1322 SmallString<128> CfgDir;
1323 CfgDir.append(
1324 RHS: CLOptions->getLastArgValue(Id: options::OPT_config_system_dir_EQ));
1325 if (CfgDir.empty() || getVFS().makeAbsolute(Path&: CfgDir))
1326 SystemConfigDir.clear();
1327 else
1328 SystemConfigDir = static_cast<std::string>(CfgDir);
1329 }
1330 if (CLOptions->hasArg(Ids: options::OPT_config_user_dir_EQ)) {
1331 SmallString<128> CfgDir;
1332 llvm::sys::fs::expand_tilde(
1333 path: CLOptions->getLastArgValue(Id: options::OPT_config_user_dir_EQ), output&: CfgDir);
1334 if (CfgDir.empty() || getVFS().makeAbsolute(Path&: CfgDir))
1335 UserConfigDir.clear();
1336 else
1337 UserConfigDir = static_cast<std::string>(CfgDir);
1338 }
1339 }
1340
1341 // Prepare list of directories where config file is searched for.
1342 StringRef CfgFileSearchDirs[] = {UserConfigDir, SystemConfigDir, Dir};
1343 ExpCtx.setSearchDirs(CfgFileSearchDirs);
1344
1345 // First try to load configuration from the default files, return on error.
1346 if (loadDefaultConfigFiles(ExpCtx))
1347 return true;
1348
1349 // Then load configuration files specified explicitly.
1350 SmallString<128> CfgFilePath;
1351 if (CLOptions) {
1352 for (auto CfgFileName : CLOptions->getAllArgValues(Id: options::OPT_config)) {
1353 // If argument contains directory separator, treat it as a path to
1354 // configuration file.
1355 if (llvm::sys::path::has_parent_path(path: CfgFileName)) {
1356 CfgFilePath.assign(RHS: CfgFileName);
1357 if (llvm::sys::path::is_relative(path: CfgFilePath)) {
1358 if (getVFS().makeAbsolute(Path&: CfgFilePath)) {
1359 Diag(DiagID: diag::err_drv_cannot_open_config_file)
1360 << CfgFilePath << "cannot get absolute path";
1361 return true;
1362 }
1363 }
1364 } else if (!ExpCtx.findConfigFile(FileName: CfgFileName, FilePath&: CfgFilePath)) {
1365 // Report an error that the config file could not be found.
1366 Diag(DiagID: diag::err_drv_config_file_not_found) << CfgFileName;
1367 for (const StringRef &SearchDir : CfgFileSearchDirs)
1368 if (!SearchDir.empty())
1369 Diag(DiagID: diag::note_drv_config_file_searched_in) << SearchDir;
1370 return true;
1371 }
1372
1373 // Try to read the config file, return on error.
1374 if (readConfigFile(FileName: CfgFilePath, ExpCtx))
1375 return true;
1376 }
1377 }
1378
1379 // No error occurred.
1380 return false;
1381}
1382
1383static bool findTripleConfigFile(llvm::cl::ExpansionContext &ExpCtx,
1384 SmallString<128> &ConfigFilePath,
1385 llvm::Triple Triple, std::string Suffix) {
1386 // First, try the full unmodified triple.
1387 if (ExpCtx.findConfigFile(FileName: Triple.str() + Suffix, FilePath&: ConfigFilePath))
1388 return true;
1389
1390 // Don't continue if we didn't find a parsable version in the triple.
1391 VersionTuple OSVersion = Triple.getOSVersion();
1392 if (!OSVersion.getMinor().has_value())
1393 return false;
1394
1395 std::string BaseOSName = Triple.getOSTypeName(Kind: Triple.getOS()).str();
1396
1397 // Next try strip the version to only include the major component.
1398 // e.g. arm64-apple-darwin23.6.0 -> arm64-apple-darwin23
1399 if (OSVersion.getMajor() != 0) {
1400 Triple.setOSName(BaseOSName + llvm::utostr(X: OSVersion.getMajor()));
1401 if (ExpCtx.findConfigFile(FileName: Triple.str() + Suffix, FilePath&: ConfigFilePath))
1402 return true;
1403 }
1404
1405 // Finally, try without any version suffix at all.
1406 // e.g. arm64-apple-darwin23.6.0 -> arm64-apple-darwin
1407 Triple.setOSName(BaseOSName);
1408 return ExpCtx.findConfigFile(FileName: Triple.str() + Suffix, FilePath&: ConfigFilePath);
1409}
1410
1411bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
1412 // Disable default config if CLANG_NO_DEFAULT_CONFIG is set to a non-empty
1413 // value.
1414 if (const char *NoConfigEnv = ::getenv(name: "CLANG_NO_DEFAULT_CONFIG")) {
1415 if (*NoConfigEnv)
1416 return false;
1417 }
1418 if (CLOptions && CLOptions->hasArg(Ids: options::OPT_no_default_config))
1419 return false;
1420
1421 std::string RealMode = getExecutableForDriverMode(Mode);
1422 llvm::Triple Triple;
1423
1424 // If name prefix is present, no --target= override was passed via CLOptions
1425 // and the name prefix is not a valid triple, force it for backwards
1426 // compatibility.
1427 if (!ClangNameParts.TargetPrefix.empty() &&
1428 computeTargetTriple(D: *this, TargetTriple: "/invalid/", Args: *CLOptions).str() ==
1429 "/invalid/") {
1430 llvm::Triple PrefixTriple{ClangNameParts.TargetPrefix};
1431 if (PrefixTriple.getArch() == llvm::Triple::UnknownArch ||
1432 PrefixTriple.isOSUnknown())
1433 Triple = std::move(PrefixTriple);
1434 }
1435
1436 // Otherwise, use the real triple as used by the driver.
1437 llvm::Triple RealTriple =
1438 computeTargetTriple(D: *this, TargetTriple, Args: *CLOptions);
1439 if (Triple.str().empty()) {
1440 Triple = RealTriple;
1441 assert(!Triple.str().empty());
1442 }
1443
1444 // On z/OS, start by loading the customization file before loading
1445 // the usual default config file(s).
1446 if (RealTriple.isOSzOS() && loadZOSCustomizationFile(ExpCtx))
1447 return true;
1448
1449 // Search for config files in the following order:
1450 // 1. <triple>-<mode>.cfg using real driver mode
1451 // (e.g. i386-pc-linux-gnu-clang++.cfg).
1452 // 2. <triple>-<mode>.cfg using executable suffix
1453 // (e.g. i386-pc-linux-gnu-clang-g++.cfg for *clang-g++).
1454 // 3. <triple>.cfg + <mode>.cfg using real driver mode
1455 // (e.g. i386-pc-linux-gnu.cfg + clang++.cfg).
1456 // 4. <triple>.cfg + <mode>.cfg using executable suffix
1457 // (e.g. i386-pc-linux-gnu.cfg + clang-g++.cfg for *clang-g++).
1458
1459 // Try loading <triple>-<mode>.cfg, and return if we find a match.
1460 SmallString<128> CfgFilePath;
1461 if (findTripleConfigFile(ExpCtx, ConfigFilePath&: CfgFilePath, Triple,
1462 Suffix: "-" + RealMode + ".cfg"))
1463 return readConfigFile(FileName: CfgFilePath, ExpCtx);
1464
1465 bool TryModeSuffix = !ClangNameParts.ModeSuffix.empty() &&
1466 ClangNameParts.ModeSuffix != RealMode;
1467 if (TryModeSuffix) {
1468 if (findTripleConfigFile(ExpCtx, ConfigFilePath&: CfgFilePath, Triple,
1469 Suffix: "-" + ClangNameParts.ModeSuffix + ".cfg"))
1470 return readConfigFile(FileName: CfgFilePath, ExpCtx);
1471 }
1472
1473 // Try loading <mode>.cfg, and return if loading failed. If a matching file
1474 // was not found, still proceed on to try <triple>.cfg.
1475 std::string CfgFileName = RealMode + ".cfg";
1476 if (ExpCtx.findConfigFile(FileName: CfgFileName, FilePath&: CfgFilePath)) {
1477 if (readConfigFile(FileName: CfgFilePath, ExpCtx))
1478 return true;
1479 } else if (TryModeSuffix) {
1480 CfgFileName = ClangNameParts.ModeSuffix + ".cfg";
1481 if (ExpCtx.findConfigFile(FileName: CfgFileName, FilePath&: CfgFilePath) &&
1482 readConfigFile(FileName: CfgFilePath, ExpCtx))
1483 return true;
1484 }
1485
1486 // Try loading <triple>.cfg and return if we find a match.
1487 if (findTripleConfigFile(ExpCtx, ConfigFilePath&: CfgFilePath, Triple, Suffix: ".cfg"))
1488 return readConfigFile(FileName: CfgFilePath, ExpCtx);
1489
1490 // If we were unable to find a config file deduced from executable name,
1491 // that is not an error.
1492 return false;
1493}
1494
1495Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
1496 llvm::PrettyStackTraceString CrashInfo("Compilation construction");
1497
1498 // FIXME: Handle environment options which affect driver behavior, somewhere
1499 // (client?). GCC_EXEC_PREFIX, LPATH, CC_PRINT_OPTIONS.
1500
1501 // We look for the driver mode option early, because the mode can affect
1502 // how other options are parsed.
1503
1504 auto DriverMode = getDriverMode(ProgName: DriverExecutable, Args: ArgList.slice(N: 1));
1505 if (!DriverMode.empty())
1506 setDriverMode(DriverMode);
1507
1508 // FIXME: What are we going to do with -V and -b?
1509
1510 // Arguments specified in command line.
1511 bool ContainsError;
1512 CLOptions = std::make_unique<InputArgList>(
1513 args: ParseArgStrings(ArgStrings: ArgList.slice(N: 1), /*UseDriverMode=*/true, ContainsError));
1514
1515 // Try parsing configuration file.
1516 if (!ContainsError)
1517 ContainsError = loadConfigFiles();
1518 bool HasConfigFileHead = !ContainsError && CfgOptionsHead;
1519 bool HasConfigFileTail = !ContainsError && CfgOptionsTail;
1520
1521 // All arguments, from both config file and command line.
1522 InputArgList Args =
1523 HasConfigFileHead ? std::move(*CfgOptionsHead) : std::move(*CLOptions);
1524
1525 if (HasConfigFileHead)
1526 for (auto *Opt : *CLOptions)
1527 if (!Opt->getOption().matches(ID: options::OPT_config))
1528 appendOneArg(Args, Opt);
1529
1530 // In CL mode, look for any pass-through arguments
1531 if (IsCLMode() && !ContainsError) {
1532 SmallVector<const char *, 16> CLModePassThroughArgList;
1533 for (const auto *A : Args.filtered(Ids: options::OPT__SLASH_clang)) {
1534 A->claim();
1535 CLModePassThroughArgList.push_back(Elt: A->getValue());
1536 }
1537
1538 if (!CLModePassThroughArgList.empty()) {
1539 // Parse any pass through args using default clang processing rather
1540 // than clang-cl processing.
1541 auto CLModePassThroughOptions = std::make_unique<InputArgList>(
1542 args: ParseArgStrings(ArgStrings: CLModePassThroughArgList, /*UseDriverMode=*/false,
1543 ContainsError));
1544
1545 if (!ContainsError)
1546 for (auto *Opt : *CLModePassThroughOptions)
1547 appendOneArg(Args, Opt);
1548 }
1549 }
1550
1551 // Check for working directory option before accessing any files
1552 if (Arg *WD = Args.getLastArg(Ids: options::OPT_working_directory))
1553 if (VFS->setCurrentWorkingDirectory(WD->getValue()))
1554 Diag(DiagID: diag::err_drv_unable_to_set_working_directory) << WD->getValue();
1555
1556 // Check for missing include directories.
1557 if (!Diags.isIgnored(DiagID: diag::warn_missing_include_dirs, Loc: SourceLocation())) {
1558 for (auto IncludeDir : Args.getAllArgValues(Id: options::OPT_I_Group)) {
1559 if (!VFS->exists(Path: IncludeDir))
1560 Diag(DiagID: diag::warn_missing_include_dirs) << IncludeDir;
1561 }
1562 }
1563
1564 // FIXME: This stuff needs to go into the Compilation, not the driver.
1565 bool CCCPrintPhases;
1566
1567 // -canonical-prefixes, -no-canonical-prefixes are used very early in main.
1568 Args.ClaimAllArgs(Id0: options::OPT_canonical_prefixes);
1569 Args.ClaimAllArgs(Id0: options::OPT_no_canonical_prefixes);
1570
1571 // f(no-)integated-cc1 is also used very early in main.
1572 Args.ClaimAllArgs(Id0: options::OPT_fintegrated_cc1);
1573 Args.ClaimAllArgs(Id0: options::OPT_fno_integrated_cc1);
1574
1575 // Ignore -pipe.
1576 Args.ClaimAllArgs(Id0: options::OPT_pipe);
1577
1578 // Extract -ccc args.
1579 //
1580 // FIXME: We need to figure out where this behavior should live. Most of it
1581 // should be outside in the client; the parts that aren't should have proper
1582 // options, either by introducing new ones or by overloading gcc ones like -V
1583 // or -b.
1584 CCCPrintPhases = Args.hasArg(Ids: options::OPT_ccc_print_phases);
1585 CCCPrintBindings = Args.hasArg(Ids: options::OPT_ccc_print_bindings);
1586 if (const Arg *A = Args.getLastArg(Ids: options::OPT_ccc_gcc_name))
1587 CCCGenericGCCName = A->getValue();
1588
1589 // Process -fproc-stat-report options.
1590 if (const Arg *A = Args.getLastArg(Ids: options::OPT_fproc_stat_report_EQ)) {
1591 CCPrintProcessStats = true;
1592 CCPrintStatReportFilename = A->getValue();
1593 }
1594 if (Args.hasArg(Ids: options::OPT_fproc_stat_report))
1595 CCPrintProcessStats = true;
1596
1597 // FIXME: TargetTriple is used by the target-prefixed calls to as/ld
1598 // and getToolChain is const.
1599 if (IsCLMode()) {
1600 // clang-cl targets MSVC-style Win32.
1601 llvm::Triple T(TargetTriple);
1602 T.setOS(llvm::Triple::Win32);
1603 T.setVendor(llvm::Triple::PC);
1604 T.setEnvironment(llvm::Triple::MSVC);
1605 T.setObjectFormat(llvm::Triple::COFF);
1606 if (Args.hasArg(Ids: options::OPT__SLASH_arm64EC))
1607 T.setArch(Kind: llvm::Triple::aarch64, SubArch: llvm::Triple::AArch64SubArch_arm64ec);
1608 TargetTriple = T.str();
1609 } else if (IsDXCMode()) {
1610 // Build TargetTriple from target_profile option for clang-dxc.
1611 if (const Arg *A = Args.getLastArg(Ids: options::OPT_target_profile)) {
1612 StringRef TargetProfile = A->getValue();
1613 if (auto Triple =
1614 toolchains::HLSLToolChain::parseTargetProfile(TargetProfile))
1615 TargetTriple = *Triple;
1616 else
1617 Diag(DiagID: diag::err_drv_invalid_directx_shader_module) << TargetProfile;
1618
1619 A->claim();
1620
1621 if (Args.hasArg(Ids: options::OPT_spirv)) {
1622 const llvm::StringMap<llvm::Triple::SubArchType> ValidTargets = {
1623 {"vulkan1.2", llvm::Triple::SPIRVSubArch_v15},
1624 {"vulkan1.3", llvm::Triple::SPIRVSubArch_v16}};
1625 llvm::Triple T(TargetTriple);
1626
1627 // Set specific Vulkan version. Default to vulkan1.3.
1628 auto TargetInfo = ValidTargets.find(Key: "vulkan1.3");
1629 assert(TargetInfo != ValidTargets.end());
1630 if (const Arg *A = Args.getLastArg(Ids: options::OPT_fspv_target_env_EQ)) {
1631 TargetInfo = ValidTargets.find(Key: A->getValue());
1632 if (TargetInfo == ValidTargets.end()) {
1633 Diag(DiagID: diag::err_drv_invalid_value)
1634 << A->getAsString(Args) << A->getValue();
1635 }
1636 A->claim();
1637 }
1638 if (TargetInfo != ValidTargets.end()) {
1639 T.setOSName(TargetInfo->getKey());
1640 T.setArch(Kind: llvm::Triple::spirv, SubArch: TargetInfo->getValue());
1641 TargetTriple = T.str();
1642 }
1643 }
1644 } else {
1645 Diag(DiagID: diag::err_drv_dxc_missing_target_profile);
1646 }
1647 }
1648
1649 if (const Arg *A = Args.getLastArg(Ids: options::OPT_target))
1650 TargetTriple = A->getValue();
1651 if (const Arg *A = Args.getLastArg(Ids: options::OPT_ccc_install_dir))
1652 Dir = A->getValue();
1653 for (const Arg *A : Args.filtered(Ids: options::OPT_B)) {
1654 A->claim();
1655 PrefixDirs.push_back(Elt: A->getValue(N: 0));
1656 }
1657 if (std::optional<std::string> CompilerPathValue =
1658 llvm::sys::Process::GetEnv(name: "COMPILER_PATH")) {
1659 StringRef CompilerPath = *CompilerPathValue;
1660 while (!CompilerPath.empty()) {
1661 std::pair<StringRef, StringRef> Split =
1662 CompilerPath.split(Separator: llvm::sys::EnvPathSeparator);
1663 PrefixDirs.push_back(Elt: std::string(Split.first));
1664 CompilerPath = Split.second;
1665 }
1666 }
1667 if (const Arg *A = Args.getLastArg(Ids: options::OPT__sysroot_EQ))
1668 SysRoot = A->getValue();
1669 if (const Arg *A = Args.getLastArg(Ids: options::OPT__dyld_prefix_EQ))
1670 DyldPrefix = A->getValue();
1671
1672 if (const Arg *A = Args.getLastArg(Ids: options::OPT_resource_dir))
1673 ResourceDir = A->getValue();
1674
1675 if (const Arg *A = Args.getLastArg(Ids: options::OPT_save_temps_EQ)) {
1676 SaveTemps = llvm::StringSwitch<SaveTempsMode>(A->getValue())
1677 .Case(S: "cwd", Value: SaveTempsCwd)
1678 .Case(S: "obj", Value: SaveTempsObj)
1679 .Default(Value: SaveTempsCwd);
1680 }
1681
1682 if (const Arg *A = Args.getLastArg(Ids: options::OPT_offload_host_only,
1683 Ids: options::OPT_offload_device_only,
1684 Ids: options::OPT_offload_host_device)) {
1685 if (A->getOption().matches(ID: options::OPT_offload_host_only))
1686 Offload = OffloadHost;
1687 else if (A->getOption().matches(ID: options::OPT_offload_device_only))
1688 Offload = OffloadDevice;
1689 else
1690 Offload = OffloadHostDevice;
1691 }
1692
1693 // Process -fembed-bitcode= flags.
1694 if (Arg *A = Args.getLastArg(Ids: options::OPT_fembed_bitcode_EQ)) {
1695 StringRef Name = A->getValue();
1696 unsigned Model = llvm::StringSwitch<unsigned>(Name)
1697 .Case(S: "off", Value: EmbedNone)
1698 .Case(S: "all", Value: EmbedBitcode)
1699 .Case(S: "bitcode", Value: EmbedBitcode)
1700 .Case(S: "marker", Value: EmbedMarker)
1701 .Default(Value: ~0U);
1702 if (Model == ~0U) {
1703 Diags.Report(DiagID: diag::err_drv_invalid_value) << A->getAsString(Args)
1704 << Name;
1705 } else
1706 BitcodeEmbed = static_cast<BitcodeEmbedMode>(Model);
1707 }
1708
1709 // Remove existing compilation database so that each job can append to it.
1710 if (Arg *A = Args.getLastArg(Ids: options::OPT_MJ))
1711 llvm::sys::fs::remove(path: A->getValue());
1712
1713 // Setting up the jobs for some precompile cases depends on whether we are
1714 // treating them as PCH, implicit modules or C++20 ones.
1715 // TODO: inferring the mode like this seems fragile (it meets the objective
1716 // of not requiring anything new for operation, however).
1717 const Arg *Std = Args.getLastArg(Ids: options::OPT_std_EQ);
1718 ModulesModeCXX20 =
1719 !Args.hasArg(Ids: options::OPT_fmodules) && Std &&
1720 (Std->containsValue(Value: "c++20") || Std->containsValue(Value: "c++2a") ||
1721 Std->containsValue(Value: "c++23") || Std->containsValue(Value: "c++2b") ||
1722 Std->containsValue(Value: "c++26") || Std->containsValue(Value: "c++2c") ||
1723 Std->containsValue(Value: "c++2d") || Std->containsValue(Value: "c++latest"));
1724
1725 // Process -fmodule-header{=} flags.
1726 if (Arg *A = Args.getLastArg(Ids: options::OPT_fmodule_header_EQ,
1727 Ids: options::OPT_fmodule_header)) {
1728 // These flags force C++20 handling of headers.
1729 ModulesModeCXX20 = true;
1730 if (A->getOption().matches(ID: options::OPT_fmodule_header))
1731 CXX20HeaderType = HeaderMode_Default;
1732 else {
1733 StringRef ArgName = A->getValue();
1734 unsigned Kind = llvm::StringSwitch<unsigned>(ArgName)
1735 .Case(S: "user", Value: HeaderMode_User)
1736 .Case(S: "system", Value: HeaderMode_System)
1737 .Default(Value: ~0U);
1738 if (Kind == ~0U) {
1739 Diags.Report(DiagID: diag::err_drv_invalid_value)
1740 << A->getAsString(Args) << ArgName;
1741 } else
1742 CXX20HeaderType = static_cast<ModuleHeaderMode>(Kind);
1743 }
1744 }
1745
1746 std::unique_ptr<llvm::opt::InputArgList> UArgs =
1747 std::make_unique<InputArgList>(args: std::move(Args));
1748
1749 // Owned by the host.
1750 const ToolChain &TC =
1751 getToolChain(Args: *UArgs, Target: computeTargetTriple(D: *this, TargetTriple, Args: *UArgs));
1752
1753 {
1754 SmallVector<std::string> MultilibMacroDefinesStr =
1755 TC.getMultilibMacroDefinesStr(Args&: *UArgs);
1756 SmallVector<const char *> MLMacroDefinesChar(
1757 llvm::map_range(C&: MultilibMacroDefinesStr, F: [&UArgs](const auto &S) {
1758 return UArgs->MakeArgString(Str: Twine("-D") + Twine(S));
1759 }));
1760 bool MLContainsError;
1761 auto MultilibMacroDefineList =
1762 std::make_unique<InputArgList>(args: ParseArgStrings(
1763 ArgStrings: MLMacroDefinesChar, /*UseDriverMode=*/false, ContainsError&: MLContainsError));
1764 if (!MLContainsError) {
1765 for (auto *Opt : *MultilibMacroDefineList) {
1766 appendOneArg(Args&: *UArgs, Opt);
1767 }
1768 }
1769 }
1770
1771 // Perform the default argument translations.
1772 DerivedArgList *TranslatedArgs = TranslateInputArgs(Args: *UArgs);
1773
1774 // Check if the environment version is valid except wasm case.
1775 llvm::Triple Triple = TC.getTriple();
1776 if (!Triple.isWasm()) {
1777 StringRef TripleVersionName = Triple.getEnvironmentVersionString();
1778 StringRef TripleObjectFormat =
1779 Triple.getObjectFormatTypeName(ObjectFormat: Triple.getObjectFormat());
1780 if (Triple.getEnvironmentVersion().empty() && TripleVersionName != "" &&
1781 TripleVersionName != TripleObjectFormat) {
1782 Diags.Report(DiagID: diag::err_drv_triple_version_invalid)
1783 << TripleVersionName << TC.getTripleString();
1784 ContainsError = true;
1785 }
1786 }
1787
1788 // Report warning when arm64EC option is overridden by specified target
1789 if ((TC.getTriple().getArch() != llvm::Triple::aarch64 ||
1790 TC.getTriple().getSubArch() != llvm::Triple::AArch64SubArch_arm64ec) &&
1791 UArgs->hasArg(Ids: options::OPT__SLASH_arm64EC)) {
1792 getDiags().Report(DiagID: clang::diag::warn_target_override_arm64ec)
1793 << TC.getTripleString();
1794 }
1795
1796 // A common user mistake is specifying a target of aarch64-none-eabi or
1797 // arm-none-elf whereas the correct names are aarch64-none-elf &
1798 // arm-none-eabi. Detect these cases and issue a warning.
1799 if (TC.getTriple().getOS() == llvm::Triple::UnknownOS &&
1800 TC.getTriple().getVendor() == llvm::Triple::UnknownVendor) {
1801 switch (TC.getTriple().getArch()) {
1802 case llvm::Triple::arm:
1803 case llvm::Triple::armeb:
1804 case llvm::Triple::thumb:
1805 case llvm::Triple::thumbeb:
1806 if (TC.getTriple().getEnvironmentName() == "elf") {
1807 Diag(DiagID: diag::warn_target_unrecognized_env)
1808 << TargetTriple
1809 << (TC.getTriple().getArchName().str() + "-none-eabi");
1810 }
1811 break;
1812 case llvm::Triple::aarch64:
1813 case llvm::Triple::aarch64_be:
1814 case llvm::Triple::aarch64_32:
1815 if (TC.getTriple().getEnvironmentName().starts_with(Prefix: "eabi")) {
1816 Diag(DiagID: diag::warn_target_unrecognized_env)
1817 << TargetTriple
1818 << (TC.getTriple().getArchName().str() + "-none-elf");
1819 }
1820 break;
1821 default:
1822 break;
1823 }
1824 }
1825
1826 // The compilation takes ownership of Args.
1827 Compilation *C = new Compilation(*this, TC, UArgs.release(), TranslatedArgs,
1828 ContainsError);
1829
1830 if (!HandleImmediateArgs(C&: *C))
1831 return C;
1832
1833 // Construct the list of inputs.
1834 InputList Inputs;
1835 BuildInputs(TC: C->getDefaultToolChain(), Args&: *TranslatedArgs, Inputs);
1836 if (HasConfigFileTail && Inputs.size()) {
1837 Arg *FinalPhaseArg;
1838 if (getFinalPhase(DAL: *TranslatedArgs, FinalPhaseArg: &FinalPhaseArg) == phases::Link) {
1839 DerivedArgList TranslatedLinkerIns(*CfgOptionsTail);
1840 for (Arg *A : *CfgOptionsTail)
1841 TranslatedLinkerIns.append(A);
1842 BuildInputs(TC: C->getDefaultToolChain(), Args&: TranslatedLinkerIns, Inputs);
1843 }
1844 }
1845
1846 // Populate the tool chains for the offloading devices, if any.
1847 CreateOffloadingDeviceToolChains(C&: *C, Inputs);
1848
1849 bool UseModulesDriver = C->getArgs().hasFlag(
1850 Pos: options::OPT_fmodules_driver, Neg: options::OPT_fno_modules_driver, Default: false);
1851 modules::StdModuleManifest ModulesManifest;
1852 if (UseModulesDriver) {
1853 Diags.Report(DiagID: diag::remark_performing_driver_managed_module_build);
1854
1855 modules::diagnoseModulesDriverArgs(DAL&: C->getArgs(), Diags);
1856
1857 // Read the Standard library module manifest and, if available, add all
1858 // discovered modules to this Compilation. Jobs for modules specified in
1859 // the manifest that are not required by any command-line input are pruned
1860 // later.
1861 const auto StdModuleManifestPath =
1862 GetStdModuleManifestPath(C: *C, TC: C->getDefaultToolChain());
1863
1864 if (!llvm::sys::fs::exists(Path: StdModuleManifestPath))
1865 Diags.Report(DiagID: diag::remark_modules_manifest_not_found);
1866 else {
1867 Diags.Report(DiagID: diag::remark_using_modules_manifest)
1868 << StdModuleManifestPath;
1869 if (auto ManifestOrErr =
1870 modules::readStdModuleManifest(ManifestPath: StdModuleManifestPath, VFS&: getVFS())) {
1871 ModulesManifest = std::move(*ManifestOrErr);
1872 // Only allow on-demand imports of standard library modules for now.
1873 llvm::erase_if(C&: ModulesManifest.Modules, P: [](const auto &ModuleEntry) {
1874 return !ModuleEntry.IsStdlib;
1875 });
1876 modules::buildStdModuleManifestInputs(ManifestEntries: ModulesManifest.Modules, C&: *C,
1877 Inputs);
1878 } else {
1879 llvm::handleAllErrors(
1880 E: ManifestOrErr.takeError(),
1881 Handlers: [&](llvm::json::ParseError &Err) {
1882 Diags.Report(DiagID: diag::err_modules_manifest_failed_parse)
1883 << Err.message();
1884 },
1885 Handlers: [&](llvm::FileError &Err) {
1886 Diags.Report(DiagID: diag::err_cannot_open_file)
1887 << Err.getFileName() << Err.messageWithoutFileInfo();
1888 });
1889 }
1890 }
1891 }
1892
1893 // Construct the list of abstract actions to perform for this compilation. On
1894 // MachO targets this uses the driver-driver and universal actions.
1895 if (TC.getTriple().isOSBinFormatMachO())
1896 BuildUniversalActions(C&: *C, TC: C->getDefaultToolChain(), BAInputs: Inputs);
1897 else
1898 BuildActions(C&: *C, Args&: C->getArgs(), Inputs, Actions&: C->getActions());
1899
1900 if (CCCPrintPhases) {
1901 PrintActions(C: *C);
1902 return C;
1903 }
1904
1905 BuildJobs(C&: *C);
1906
1907 if (UseModulesDriver)
1908 modules::runModulesDriver(C&: *C, ManifestEntries: ModulesManifest.Modules);
1909
1910 return C;
1911}
1912
1913static void printArgList(raw_ostream &OS, const llvm::opt::ArgList &Args) {
1914 llvm::opt::ArgStringList ASL;
1915 for (const auto *A : Args) {
1916 // Use user's original spelling of flags. For example, use
1917 // `/source-charset:utf-8` instead of `-finput-charset=utf-8` if the user
1918 // wrote the former.
1919 while (A->getAlias())
1920 A = A->getAlias();
1921 A->render(Args, Output&: ASL);
1922 }
1923
1924 for (auto I = ASL.begin(), E = ASL.end(); I != E; ++I) {
1925 if (I != ASL.begin())
1926 OS << ' ';
1927 llvm::sys::printArg(OS, Arg: *I, Quote: true);
1928 }
1929 OS << '\n';
1930}
1931
1932bool Driver::getCrashDiagnosticFile(StringRef ReproCrashFilename,
1933 SmallString<128> &CrashDiagDir) {
1934 using namespace llvm::sys;
1935 assert(llvm::Triple(llvm::sys::getProcessTriple()).isOSDarwin() &&
1936 "Only knows about .crash files on Darwin");
1937 // This is not a formal output of the compiler, let's bypass the sandbox.
1938 auto BypassSandbox = sandbox::scopedDisable();
1939
1940 // The .crash file can be found on at ~/Library/Logs/DiagnosticReports/
1941 // (or /Library/Logs/DiagnosticReports for root) and has the filename pattern
1942 // clang-<VERSION>_<YYYY-MM-DD-HHMMSS>_<hostname>.crash.
1943 path::home_directory(result&: CrashDiagDir);
1944 if (CrashDiagDir.starts_with(Prefix: "/var/root"))
1945 CrashDiagDir = "/";
1946 path::append(path&: CrashDiagDir, a: "Library/Logs/DiagnosticReports");
1947 int PID =
1948#if LLVM_ON_UNIX
1949 getpid();
1950#else
1951 0;
1952#endif
1953 std::error_code EC;
1954 fs::file_status FileStatus;
1955 TimePoint<> LastAccessTime;
1956 SmallString<128> CrashFilePath;
1957 // Lookup the .crash files and get the one generated by a subprocess spawned
1958 // by this driver invocation.
1959 for (fs::directory_iterator File(CrashDiagDir, EC), FileEnd;
1960 File != FileEnd && !EC; File.increment(ec&: EC)) {
1961 StringRef FileName = path::filename(path: File->path());
1962 if (!FileName.starts_with(Prefix: Name))
1963 continue;
1964 if (fs::status(path: File->path(), result&: FileStatus))
1965 continue;
1966 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> CrashFile =
1967 llvm::MemoryBuffer::getFile(Filename: File->path());
1968 if (!CrashFile)
1969 continue;
1970 // The first line should start with "Process:", otherwise this isn't a real
1971 // .crash file.
1972 StringRef Data = CrashFile.get()->getBuffer();
1973 if (!Data.starts_with(Prefix: "Process:"))
1974 continue;
1975 // Parse parent process pid line, e.g: "Parent Process: clang-4.0 [79141]"
1976 size_t ParentProcPos = Data.find(Str: "Parent Process:");
1977 if (ParentProcPos == StringRef::npos)
1978 continue;
1979 size_t LineEnd = Data.find_first_of(Chars: "\n", From: ParentProcPos);
1980 if (LineEnd == StringRef::npos)
1981 continue;
1982 StringRef ParentProcess = Data.slice(Start: ParentProcPos+15, End: LineEnd).trim();
1983 int OpenBracket = -1, CloseBracket = -1;
1984 for (size_t i = 0, e = ParentProcess.size(); i < e; ++i) {
1985 if (ParentProcess[i] == '[')
1986 OpenBracket = i;
1987 if (ParentProcess[i] == ']')
1988 CloseBracket = i;
1989 }
1990 // Extract the parent process PID from the .crash file and check whether
1991 // it matches this driver invocation pid.
1992 int CrashPID;
1993 if (OpenBracket < 0 || CloseBracket < 0 ||
1994 ParentProcess.slice(Start: OpenBracket + 1, End: CloseBracket)
1995 .getAsInteger(Radix: 10, Result&: CrashPID) || CrashPID != PID) {
1996 continue;
1997 }
1998
1999 // Found a .crash file matching the driver pid. To avoid getting an older
2000 // and misleading crash file, continue looking for the most recent.
2001 // FIXME: the driver can dispatch multiple cc1 invocations, leading to
2002 // multiple crashes poiting to the same parent process. Since the driver
2003 // does not collect pid information for the dispatched invocation there's
2004 // currently no way to distinguish among them.
2005 const auto FileAccessTime = FileStatus.getLastModificationTime();
2006 if (FileAccessTime > LastAccessTime) {
2007 CrashFilePath.assign(RHS: File->path());
2008 LastAccessTime = FileAccessTime;
2009 }
2010 }
2011
2012 // If found, copy it over to the location of other reproducer files.
2013 if (!CrashFilePath.empty()) {
2014 EC = fs::copy_file(From: CrashFilePath, To: ReproCrashFilename);
2015 if (EC)
2016 return false;
2017 return true;
2018 }
2019
2020 return false;
2021}
2022
2023static const char BugReportMsg[] =
2024 "\n********************\n\n"
2025 "PLEASE ATTACH THE FOLLOWING CRASH REPRODUCER FILES TO THE BUG REPORT:";
2026
2027// When clang crashes, produce diagnostic information including the fully
2028// preprocessed source file(s). Request that the developer attach the
2029// diagnostic information to a bug report.
2030void Driver::generateCompilationDiagnostics(
2031 Compilation &C, const Command &FailingCommand,
2032 StringRef AdditionalInformation, CompilationDiagnosticReport *Report) {
2033 if (C.getArgs().hasArg(Ids: options::OPT_fno_crash_diagnostics))
2034 return;
2035
2036 bool HasCrashTar = C.getArgs().hasArg(Ids: options::OPT_fcrash_diagnostics_tar);
2037
2038 unsigned Level = 1;
2039 if (Arg *A = C.getArgs().getLastArg(Ids: options::OPT_fcrash_diagnostics_EQ)) {
2040 Level = llvm::StringSwitch<unsigned>(A->getValue())
2041 .Case(S: "off", Value: 0)
2042 .Case(S: "compiler", Value: 1)
2043 .Case(S: "all", Value: 2)
2044 .Default(Value: 1);
2045 }
2046 if (!Level)
2047 return;
2048
2049 // Don't try to generate diagnostics for dsymutil jobs.
2050 if (FailingCommand.getCreator().isDsymutilJob())
2051 return;
2052
2053 bool IsLLD = false;
2054 ArgStringList SavedTemps;
2055 if (FailingCommand.getCreator().isLinkJob()) {
2056 C.getDefaultToolChain().GetLinkerPath(LinkerIsLLD: &IsLLD);
2057 if (!IsLLD || Level < 2)
2058 return;
2059
2060 // If lld crashed, we will re-run the same command with the input it used
2061 // to have. In that case we should not remove temp files in
2062 // initCompilationForDiagnostics yet. They will be added back and removed
2063 // later.
2064 SavedTemps = std::move(C.getTempFiles());
2065 assert(!C.getTempFiles().size());
2066 }
2067
2068 // Print the version of the compiler.
2069 PrintVersion(C, OS&: llvm::errs());
2070
2071 // Suppress driver output and emit preprocessor output to temp file.
2072 CCGenDiagnostics = true;
2073
2074 // Save the original job command(s).
2075 Command Cmd = FailingCommand;
2076
2077 // Keep track of whether we produce any errors while trying to produce
2078 // preprocessed sources.
2079 DiagnosticErrorTrap Trap(Diags);
2080
2081 // Suppress tool output.
2082 C.initCompilationForDiagnostics();
2083
2084 // If lld failed, rerun it again with --reproduce.
2085 if (IsLLD) {
2086 const char *TmpName = CreateTempFile(C, Prefix: "linker-crash", Suffix: "tar");
2087 Command NewLLDInvocation = Cmd;
2088 llvm::opt::ArgStringList ArgList = NewLLDInvocation.getArguments();
2089 StringRef ReproduceOption =
2090 C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment()
2091 ? "/reproduce:"
2092 : "--reproduce=";
2093 ArgList.push_back(Elt: Saver.save(S: Twine(ReproduceOption) + TmpName).data());
2094 NewLLDInvocation.replaceArguments(List: std::move(ArgList));
2095
2096 // Redirect stdout/stderr to /dev/null.
2097 NewLLDInvocation.Execute(Redirects: {std::nullopt, {""}, {""}}, ErrMsg: nullptr, ExecutionFailed: nullptr);
2098 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) << BugReportMsg;
2099 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) << TmpName;
2100 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg)
2101 << "\n\n********************";
2102 if (Report)
2103 Report->TemporaryFiles.push_back(Elt: TmpName);
2104 return;
2105 }
2106
2107 // Construct the list of inputs.
2108 InputList Inputs;
2109 BuildInputs(TC: C.getDefaultToolChain(), Args&: C.getArgs(), Inputs);
2110
2111 ArgStringList IRInputs;
2112 for (InputList::iterator it = Inputs.begin(), ie = Inputs.end(); it != ie;) {
2113 bool IgnoreInput = false;
2114
2115 // Save IR inputs separately, ignore input from stdin or any other inputs
2116 // that cannot be preprocessed. Check type first as not all linker inputs
2117 // have a value.
2118 if (types::isLLVMIR(Id: it->first)) {
2119 IRInputs.push_back(Elt: it->second->getValue());
2120 IgnoreInput = true;
2121 } else if (types::getPreprocessedType(Id: it->first) == types::TY_INVALID) {
2122 IgnoreInput = true;
2123 } else if (!strcmp(s1: it->second->getValue(), s2: "-")) {
2124 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg)
2125 << "Error generating preprocessed source(s) - "
2126 "ignoring input from stdin.";
2127 IgnoreInput = true;
2128 }
2129
2130 if (IgnoreInput) {
2131 it = Inputs.erase(CI: it);
2132 ie = Inputs.end();
2133 } else {
2134 ++it;
2135 }
2136 }
2137
2138 if (Inputs.empty() && IRInputs.empty()) {
2139 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg)
2140 << "Error generating preprocessed source(s) - "
2141 "no preprocessable inputs.";
2142 return;
2143 }
2144
2145 // If there are multiple -arch options, build a reproducer only for the bound
2146 // arch that crashed.
2147 llvm::StringSet<> ArchNames;
2148 for (const Arg *A : C.getArgs()) {
2149 if (A->getOption().matches(ID: options::OPT_arch)) {
2150 StringRef ArchName = A->getValue();
2151 ArchNames.insert(key: ArchName);
2152 }
2153 }
2154 if (ArchNames.size() > 1) {
2155 // Build a reproducer only for the bound arch that crashed.
2156 StringRef FailingArch = Cmd.getBoundArch().ArchName;
2157 if (FailingArch.empty()) {
2158 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg)
2159 << "Error generating preprocessed source(s) - cannot generate "
2160 "preprocessed source with multiple -arch options.";
2161 return;
2162 }
2163 C.getArgs().eraseArg(Id: options::OPT_arch);
2164 C.getArgs().AddJoinedArg(BaseArg: nullptr, Opt: getOpts().getOption(Opt: options::OPT_arch),
2165 Value: FailingArch);
2166 }
2167
2168 // If we only have IR inputs there's no need for preprocessing.
2169 if (!Inputs.empty()) {
2170 // Construct the list of abstract actions to perform for this compilation.
2171 // On Darwin OSes this uses the driver-driver and builds universal actions.
2172 const ToolChain &TC = C.getDefaultToolChain();
2173 if (TC.getTriple().isOSBinFormatMachO())
2174 BuildUniversalActions(C, TC, BAInputs: Inputs);
2175 else
2176 BuildActions(C, Args&: C.getArgs(), Inputs, Actions&: C.getActions());
2177
2178 BuildJobs(C);
2179
2180 // If there were errors building the compilation, quit now.
2181 if (Trap.hasErrorOccurred()) {
2182 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg)
2183 << "Error generating preprocessed source(s).";
2184 return;
2185 }
2186 // Generate preprocessed output.
2187 SmallVector<std::pair<int, const Command *>, 4> FailingCommands;
2188 C.ExecuteJobs(Jobs: C.getJobs(), FailingCommands);
2189
2190 // If any of the preprocessing commands failed, clean up and exit.
2191 if (!FailingCommands.empty()) {
2192 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg)
2193 << "Error generating preprocessed source(s).";
2194 return;
2195 }
2196
2197 const ArgStringList &TempFiles = C.getTempFiles();
2198 if (TempFiles.empty()) {
2199 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg)
2200 << "Error generating preprocessed source(s).";
2201 return;
2202 }
2203 }
2204
2205 // Copying filenames due to ownership.
2206 const ArgStringList &Files = C.getTempFiles();
2207 SmallVector<std::string> TempFiles(Files.begin(), Files.end());
2208
2209 // We'd like to copy the IR input file into our own temp file
2210 // because the build system might try to clean-up after itself.
2211 for (auto const *Input : IRInputs) {
2212 int FD;
2213 llvm::SmallVector<char, 64> Path;
2214
2215 StringRef extension = llvm::sys::path::extension(path: Input);
2216 if (!extension.empty())
2217 extension = extension.drop_front();
2218
2219 std::error_code EC = llvm::sys::fs::createTemporaryFile(
2220 Prefix: llvm::sys::path::stem(path: Input), Suffix: extension, ResultFD&: FD, ResultPath&: Path);
2221 if (EC) {
2222 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg)
2223 << "Error generating run script: " << "Failed copying IR input files"
2224 << " " << EC.message();
2225 return;
2226 }
2227
2228 EC = llvm::sys::fs::copy_file(From: Input, ToFD: FD);
2229 if (EC) {
2230 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg)
2231 << "Error generating run script: " << "Failed copying IR input files"
2232 << " " << EC.message();
2233 return;
2234 }
2235
2236 TempFiles.push_back(Elt: std::string(Path.begin(), Path.end()));
2237 }
2238
2239 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) << BugReportMsg;
2240
2241 SmallString<128> VFS;
2242 SmallString<128> ReproCrashFilename;
2243 for (std::string &TempFile : TempFiles) {
2244 if (!HasCrashTar)
2245 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) << TempFile;
2246 if (Report)
2247 Report->TemporaryFiles.push_back(Elt: TempFile);
2248 if (ReproCrashFilename.empty()) {
2249 ReproCrashFilename = TempFile;
2250 llvm::sys::path::replace_extension(path&: ReproCrashFilename, extension: ".crash");
2251 }
2252 if (StringRef(TempFile).ends_with(Suffix: ".cache")) {
2253 // In some cases (modules) we'll dump extra data to help with reproducing
2254 // the crash into a directory next to the output.
2255 VFS = llvm::sys::path::filename(path: TempFile);
2256 llvm::sys::path::append(path&: VFS, a: "vfs", b: "vfs.yaml");
2257 }
2258 }
2259
2260 for (const char *TempFile : SavedTemps)
2261 TempFiles.push_back(Elt: TempFile);
2262
2263 // Assume associated files are based off of the first temporary file.
2264 CrashReportInfo CrashInfo(TempFiles[0], VFS);
2265
2266 llvm::SmallString<128> Script(CrashInfo.Filename);
2267 llvm::sys::path::replace_extension(path&: Script, extension: "sh");
2268 std::error_code EC;
2269 llvm::raw_fd_ostream ScriptOS(Script, EC, llvm::sys::fs::CD_CreateNew,
2270 llvm::sys::fs::FA_Write,
2271 llvm::sys::fs::OF_Text);
2272 if (EC) {
2273 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg)
2274 << "Error generating run script: " << Script << " " << EC.message();
2275 } else {
2276 ScriptOS << "# Crash reproducer for " << getClangFullVersion() << "\n"
2277 << "# Driver args: ";
2278 printArgList(OS&: ScriptOS, Args: C.getInputArgs());
2279 ScriptOS << "# Original command: ";
2280 Cmd.Print(OS&: ScriptOS, Terminator: "\n", /*Quote=*/true);
2281 Cmd.Print(OS&: ScriptOS, Terminator: "\n", /*Quote=*/true, CrashInfo: &CrashInfo);
2282 if (!AdditionalInformation.empty())
2283 ScriptOS << "\n# Additional information: " << AdditionalInformation
2284 << "\n";
2285 if (Report)
2286 Report->TemporaryFiles.push_back(Elt: std::string(Script));
2287 TempFiles.push_back(Elt: std::string(Script));
2288 ScriptOS.close();
2289 if (!HasCrashTar)
2290 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) << Script;
2291 }
2292
2293 if (Arg *A = C.getArgs().getLastArg(Ids: options::OPT_fcrash_diagnostics_tar)) {
2294 StringRef CrashDiagnosticsTar = A->getValue();
2295 Expected<std::unique_ptr<llvm::TarWriter>> TarOrErr =
2296 llvm::TarWriter::create(OutputPath: CrashDiagnosticsTar,
2297 BaseDir: llvm::sys::path::stem(path: CrashDiagnosticsTar));
2298 if (!TarOrErr) {
2299 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg)
2300 << (std::string("Error creating reproducer tarball: ") +
2301 llvm::toString(E: TarOrErr.takeError()));
2302 } else {
2303 std::unique_ptr<llvm::TarWriter> &Tar = *TarOrErr;
2304 for (const std::string &TempFile : TempFiles) {
2305 if (llvm::sys::fs::is_directory(Path: TempFile)) {
2306 std::error_code EC;
2307 for (llvm::sys::fs::recursive_directory_iterator I(TempFile, EC), E;
2308 I != E && !EC; I.increment(ec&: EC)) {
2309 if (llvm::sys::fs::is_regular_file(Path: I->path())) {
2310 auto BufferOrErr = llvm::MemoryBuffer::getFile(Filename: I->path());
2311 if (BufferOrErr) {
2312 // Construct path of file relative to TempFile.
2313 llvm::SmallString<128> PathInTar =
2314 llvm::sys::path::filename(path: TempFile);
2315 StringRef SubPath = I->path();
2316 if (SubPath.consume_front(Prefix: TempFile)) {
2317 if (!SubPath.empty() &&
2318 llvm::sys::path::is_separator(value: SubPath.front())) {
2319 SubPath = SubPath.drop_front();
2320 }
2321 llvm::sys::path::append(path&: PathInTar, a: SubPath);
2322 Tar->append(Path: PathInTar, Data: (*BufferOrErr)->getBuffer());
2323 }
2324 } else {
2325 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg)
2326 << (std::string("Error reading file for tarball: ") +
2327 I->path());
2328 }
2329 }
2330 }
2331 if (EC) {
2332 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg)
2333 << (std::string("Error iterating directory for tarball: ") +
2334 TempFile + " " + EC.message());
2335 }
2336 } else {
2337 auto BufferOrErr = llvm::MemoryBuffer::getFile(Filename: TempFile);
2338 if (BufferOrErr) {
2339 Tar->append(Path: llvm::sys::path::filename(path: TempFile),
2340 Data: (*BufferOrErr)->getBuffer());
2341 } else {
2342 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg)
2343 << (std::string("Error reading file for tarball: ") + TempFile);
2344 }
2345 }
2346 }
2347 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg)
2348 << CrashDiagnosticsTar;
2349 }
2350 }
2351
2352 // On darwin, provide information about the .crash diagnostic report.
2353 if (llvm::Triple(llvm::sys::getProcessTriple()).isOSDarwin()) {
2354 SmallString<128> CrashDiagDir;
2355 if (getCrashDiagnosticFile(ReproCrashFilename, CrashDiagDir)) {
2356 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg)
2357 << ReproCrashFilename.str();
2358 } else { // Suggest a directory for the user to look for .crash files.
2359 llvm::sys::path::append(path&: CrashDiagDir, a: Name);
2360 CrashDiagDir += "_<YYYY-MM-DD-HHMMSS>_<hostname>.crash";
2361 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg)
2362 << "Crash backtrace is located in";
2363 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg)
2364 << CrashDiagDir.str();
2365 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg)
2366 << "(choose the .crash file that corresponds to your crash)";
2367 }
2368 }
2369
2370 Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg)
2371 << "\n\n********************";
2372}
2373
2374void Driver::setUpResponseFiles(Compilation &C, Command &Cmd) {
2375 // Since commandLineFitsWithinSystemLimits() may underestimate system's
2376 // capacity if the tool does not support response files, there is a chance/
2377 // that things will just work without a response file, so we silently just
2378 // skip it.
2379 if (Cmd.getResponseFileSupport().ResponseKind ==
2380 ResponseFileSupport::RF_None ||
2381 llvm::sys::commandLineFitsWithinSystemLimits(Program: Cmd.getExecutable(),
2382 Args: Cmd.getArguments()))
2383 return;
2384
2385 std::string TmpName = GetTemporaryPath(Prefix: "response", Suffix: "txt");
2386 Cmd.setResponseFile(C.addTempFile(Name: C.getArgs().MakeArgString(Str: TmpName)));
2387}
2388
2389int Driver::ExecuteCompilation(
2390 Compilation &C,
2391 SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) {
2392 if (C.getArgs().hasArg(Ids: options::OPT_fdriver_only)) {
2393 if (C.getArgs().hasArg(Ids: options::OPT_v))
2394 C.getJobs().Print(OS&: llvm::errs(), Terminator: "\n", Quote: true);
2395
2396 C.ExecuteJobs(Jobs: C.getJobs(), FailingCommands, /*LogOnly=*/true);
2397
2398 // If there were errors building the compilation, quit now.
2399 if (!FailingCommands.empty() || Diags.hasErrorOccurred())
2400 return 1;
2401
2402 return 0;
2403 }
2404
2405 // Just print if -### was present.
2406 if (C.getArgs().hasArg(Ids: options::OPT__HASH_HASH_HASH)) {
2407 C.getJobs().Print(OS&: llvm::errs(), Terminator: "\n", Quote: true);
2408 return Diags.hasErrorOccurred() ? 1 : 0;
2409 }
2410
2411 // If there were errors building the compilation, quit now.
2412 if (Diags.hasErrorOccurred())
2413 return 1;
2414
2415 // Set up response file names for each command, if necessary.
2416 for (auto &Job : C.getJobs())
2417 setUpResponseFiles(C, Cmd&: Job);
2418
2419 C.ExecuteJobs(Jobs: C.getJobs(), FailingCommands);
2420
2421 // If the command succeeded, we are done.
2422 if (FailingCommands.empty())
2423 return 0;
2424
2425 // Otherwise, remove result files and print extra information about abnormal
2426 // failures.
2427 int Res = 0;
2428 for (const auto &CmdPair : FailingCommands) {
2429 int CommandRes = CmdPair.first;
2430 const Command *FailingCommand = CmdPair.second;
2431
2432 // Remove result files if we're not saving temps.
2433 if (!isSaveTempsEnabled()) {
2434 const JobAction *JA = cast<JobAction>(Val: &FailingCommand->getSource());
2435 C.CleanupFileMap(Files: C.getResultFiles(), JA, IssueErrors: true);
2436
2437 // Failure result files are valid unless we crashed.
2438 if (CommandRes < 0)
2439 C.CleanupFileMap(Files: C.getFailureResultFiles(), JA, IssueErrors: true);
2440 }
2441
2442 // llvm/lib/Support/*/Signals.inc will exit with a special return code
2443 // for SIGPIPE. Do not print diagnostics for this case.
2444 if (CommandRes == EX_IOERR) {
2445 Res = CommandRes;
2446 continue;
2447 }
2448
2449 // Print extra information about abnormal failures, if possible.
2450 //
2451 // This is ad-hoc, but we don't want to be excessively noisy. If the result
2452 // status was 1, assume the command failed normally. In particular, if it
2453 // was the compiler then assume it gave a reasonable error code. Failures
2454 // in other tools are less common, and they generally have worse
2455 // diagnostics, so always print the diagnostic there.
2456 const Tool &FailingTool = FailingCommand->getCreator();
2457
2458 if (!FailingCommand->getCreator().hasGoodDiagnostics() || CommandRes != 1) {
2459 // FIXME: See FIXME above regarding result code interpretation.
2460#if LLVM_ON_UNIX
2461 // On Unix, signals are represented by return codes of 128 plus the
2462 // signal number. Return code 255 is excluded because some tools,
2463 // such as llvm-ifs, exit with code 255 (-1) on failure.
2464 if (CommandRes > 128 && CommandRes != 255)
2465#else
2466 if (CommandRes < 0)
2467#endif
2468 Diag(DiagID: clang::diag::err_drv_command_signalled)
2469 << FailingTool.getShortName();
2470 else
2471 Diag(DiagID: clang::diag::err_drv_command_failed)
2472 << FailingTool.getShortName() << CommandRes;
2473 }
2474 }
2475 return Res;
2476}
2477
2478void Driver::PrintHelp(bool ShowHidden) const {
2479 llvm::opt::Visibility VisibilityMask = getOptionVisibilityMask();
2480
2481 std::string Usage = llvm::formatv(Fmt: "{0} [options] file...", Vals: Name).str();
2482 getOpts().printHelp(OS&: llvm::outs(), Usage: Usage.c_str(), Title: DriverTitle.c_str(),
2483 ShowHidden, /*ShowAllAliases=*/false,
2484 VisibilityMask);
2485}
2486
2487void Driver::PrintVersion(const Compilation &C, raw_ostream &OS) const {
2488 if (IsFlangMode()) {
2489 OS << getClangToolFullVersion(ToolName: "flang") << '\n';
2490 } else {
2491 // FIXME: The following handlers should use a callback mechanism, we don't
2492 // know what the client would like to do.
2493 OS << getClangFullVersion() << '\n';
2494 }
2495 const ToolChain &TC = C.getDefaultToolChain();
2496 OS << "Target: " << TC.getTripleString() << '\n';
2497
2498 // Print the threading model.
2499 if (Arg *A = C.getArgs().getLastArg(Ids: options::OPT_mthread_model)) {
2500 // Don't print if the ToolChain would have barfed on it already
2501 if (TC.isThreadModelSupported(Model: A->getValue()))
2502 OS << "Thread model: " << A->getValue();
2503 } else
2504 OS << "Thread model: " << TC.getThreadModel();
2505 OS << '\n';
2506
2507 // Print out the install directory.
2508 OS << "InstalledDir: " << Dir << '\n';
2509
2510 // Print the build config if it's non-default.
2511 // Intended to help LLVM developers understand the configs of compilers
2512 // they're investigating.
2513 if (!llvm::cl::getCompilerBuildConfig().empty())
2514 llvm::cl::printBuildConfig(OS);
2515
2516 // If configuration files were used, print their paths.
2517 for (auto ConfigFile : ConfigFiles)
2518 OS << "Configuration file: " << ConfigFile << '\n';
2519}
2520
2521/// PrintDiagnosticCategories - Implement the --print-diagnostic-categories
2522/// option.
2523static void PrintDiagnosticCategories(raw_ostream &OS) {
2524 // Skip the empty category.
2525 for (unsigned i = 1, max = DiagnosticIDs::getNumberOfCategories(); i != max;
2526 ++i)
2527 OS << i << ',' << DiagnosticIDs::getCategoryNameFromID(CategoryID: i) << '\n';
2528}
2529
2530void Driver::HandleAutocompletions(StringRef PassedFlags) const {
2531 if (PassedFlags == "")
2532 return;
2533 // Print out all options that start with a given argument. This is used for
2534 // shell autocompletion.
2535 std::vector<std::string> SuggestedCompletions;
2536 std::vector<std::string> Flags;
2537
2538 llvm::opt::Visibility VisibilityMask(options::ClangOption);
2539
2540 // Make sure that Flang-only options don't pollute the Clang output
2541 // TODO: Make sure that Clang-only options don't pollute Flang output
2542 if (IsFlangMode())
2543 VisibilityMask = llvm::opt::Visibility(options::FlangOption);
2544
2545 // Distinguish "--autocomplete=-someflag" and "--autocomplete=-someflag,"
2546 // because the latter indicates that the user put space before pushing tab
2547 // which should end up in a file completion.
2548 const bool HasSpace = PassedFlags.ends_with(Suffix: ",");
2549
2550 // Parse PassedFlags by "," as all the command-line flags are passed to this
2551 // function separated by ","
2552 StringRef TargetFlags = PassedFlags;
2553 while (TargetFlags != "") {
2554 StringRef CurFlag;
2555 std::tie(args&: CurFlag, args&: TargetFlags) = TargetFlags.split(Separator: ",");
2556 Flags.push_back(x: std::string(CurFlag));
2557 }
2558
2559 // We want to show cc1-only options only when clang is invoked with -cc1 or
2560 // -Xclang.
2561 if (llvm::is_contained(Range&: Flags, Element: "-Xclang") || llvm::is_contained(Range&: Flags, Element: "-cc1"))
2562 VisibilityMask = llvm::opt::Visibility(options::CC1Option);
2563
2564 const llvm::opt::OptTable &Opts = getOpts();
2565 StringRef Cur;
2566 Cur = Flags.at(n: Flags.size() - 1);
2567 StringRef Prev;
2568 if (Flags.size() >= 2) {
2569 Prev = Flags.at(n: Flags.size() - 2);
2570 SuggestedCompletions = Opts.suggestValueCompletions(Option: Prev, Arg: Cur);
2571 }
2572
2573 if (SuggestedCompletions.empty())
2574 SuggestedCompletions = Opts.suggestValueCompletions(Option: Cur, Arg: "");
2575
2576 // If Flags were empty, it means the user typed `clang [tab]` where we should
2577 // list all possible flags. If there was no value completion and the user
2578 // pressed tab after a space, we should fall back to a file completion.
2579 // We're printing a newline to be consistent with what we print at the end of
2580 // this function.
2581 if (SuggestedCompletions.empty() && HasSpace && !Flags.empty()) {
2582 llvm::outs() << '\n';
2583 return;
2584 }
2585
2586 // When flag ends with '=' and there was no value completion, return empty
2587 // string and fall back to the file autocompletion.
2588 if (SuggestedCompletions.empty() && !Cur.ends_with(Suffix: "=")) {
2589 // If the flag is in the form of "--autocomplete=-foo",
2590 // we were requested to print out all option names that start with "-foo".
2591 // For example, "--autocomplete=-fsyn" is expanded to "-fsyntax-only".
2592 SuggestedCompletions = Opts.findByPrefix(
2593 Cur, VisibilityMask,
2594 /*DisableFlags=*/options::Unsupported | options::Ignored);
2595
2596 // We have to query the -W flags manually as they're not in the OptTable.
2597 // TODO: Find a good way to add them to OptTable instead and them remove
2598 // this code.
2599 for (StringRef S : DiagnosticIDs::getDiagnosticFlags())
2600 if (S.starts_with(Prefix: Cur))
2601 SuggestedCompletions.push_back(x: std::string(S));
2602 }
2603
2604 // Sort the autocomplete candidates so that shells print them out in a
2605 // deterministic order. We could sort in any way, but we chose
2606 // case-insensitive sorting for consistency with the -help option
2607 // which prints out options in the case-insensitive alphabetical order.
2608 llvm::sort(C&: SuggestedCompletions, Comp: [](StringRef A, StringRef B) {
2609 if (int X = A.compare_insensitive(RHS: B))
2610 return X < 0;
2611 return A.compare(RHS: B) > 0;
2612 });
2613
2614 llvm::outs() << llvm::join(R&: SuggestedCompletions, Separator: "\n") << '\n';
2615}
2616
2617bool Driver::HandleImmediateArgs(Compilation &C) {
2618 // The order these options are handled in gcc is all over the place, but we
2619 // don't expect inconsistencies w.r.t. that to matter in practice.
2620
2621 if (C.getArgs().hasArg(Ids: options::OPT_dumpmachine)) {
2622 llvm::outs() << C.getDefaultToolChain().getTripleString() << '\n';
2623 return false;
2624 }
2625
2626 if (C.getArgs().hasArg(Ids: options::OPT_dumpversion)) {
2627 // Since -dumpversion is only implemented for pedantic GCC compatibility, we
2628 // return an answer which matches our definition of __VERSION__.
2629 llvm::outs() << CLANG_VERSION_STRING << "\n";
2630 return false;
2631 }
2632
2633 if (C.getArgs().hasArg(Ids: options::OPT__print_diagnostic_categories)) {
2634 PrintDiagnosticCategories(OS&: llvm::outs());
2635 return false;
2636 }
2637
2638 if (C.getArgs().hasArg(Ids: options::OPT_help) ||
2639 C.getArgs().hasArg(Ids: options::OPT__help_hidden)) {
2640 PrintHelp(ShowHidden: C.getArgs().hasArg(Ids: options::OPT__help_hidden));
2641 return false;
2642 }
2643
2644 if (C.getArgs().hasArg(Ids: options::OPT__version)) {
2645 // Follow gcc behavior and use stdout for --version and stderr for -v.
2646 PrintVersion(C, OS&: llvm::outs());
2647 return false;
2648 }
2649
2650 // Honor --ssaf-list-extractors, --ssaf-list-formats and their combinations.
2651 bool ListExtractors = C.getArgs().hasArg(Ids: options::OPT__ssaf_list_extractors);
2652 bool ListFormats = C.getArgs().hasArg(Ids: options::OPT__ssaf_list_formats);
2653 if (ListExtractors || ListFormats) {
2654 if (ListExtractors)
2655 ssaf::printAvailableTUSummaryExtractors(OS&: llvm::outs());
2656 if (ListFormats)
2657 ssaf::printAvailableFormats(OS&: llvm::outs());
2658 return false;
2659 }
2660
2661 if (C.getArgs().hasArg(Ids: options::OPT__ssaf_list_formats)) {
2662 ssaf::printAvailableFormats(OS&: llvm::outs());
2663 return false;
2664 }
2665
2666 if (C.getArgs().hasArg(Ids: options::OPT_v) ||
2667 C.getArgs().hasArg(Ids: options::OPT__HASH_HASH_HASH) ||
2668 C.getArgs().hasArg(Ids: options::OPT_print_supported_cpus) ||
2669 C.getArgs().hasArg(Ids: options::OPT_print_supported_extensions) ||
2670 C.getArgs().hasArg(Ids: options::OPT_print_enabled_extensions)) {
2671 PrintVersion(C, OS&: llvm::errs());
2672 SuppressMissingInputWarning = true;
2673 }
2674
2675 if (C.getArgs().hasArg(Ids: options::OPT_v)) {
2676 if (!SystemConfigDir.empty())
2677 llvm::errs() << "System configuration file directory: "
2678 << SystemConfigDir << "\n";
2679 if (!UserConfigDir.empty())
2680 llvm::errs() << "User configuration file directory: "
2681 << UserConfigDir << "\n";
2682 }
2683
2684 const ToolChain &TC = C.getDefaultToolChain();
2685
2686 if (C.getArgs().hasArg(Ids: options::OPT_v))
2687 TC.printVerboseInfo(OS&: llvm::errs());
2688
2689 if (C.getArgs().hasArg(Ids: options::OPT_print_resource_dir)) {
2690 llvm::outs() << ResourceDir << '\n';
2691 return false;
2692 }
2693
2694 if (C.getArgs().hasArg(Ids: options::OPT_print_search_dirs)) {
2695 llvm::outs() << "programs: =";
2696 bool separator = false;
2697 // Print -B and COMPILER_PATH.
2698 for (const std::string &Path : PrefixDirs) {
2699 if (separator)
2700 llvm::outs() << llvm::sys::EnvPathSeparator;
2701 llvm::outs() << Path;
2702 separator = true;
2703 }
2704 for (const std::string &Path : TC.getProgramPaths()) {
2705 if (separator)
2706 llvm::outs() << llvm::sys::EnvPathSeparator;
2707 llvm::outs() << Path;
2708 separator = true;
2709 }
2710 llvm::outs() << "\n";
2711 llvm::outs() << "libraries: =" << ResourceDir;
2712
2713 StringRef sysroot = C.getSysRoot();
2714
2715 for (const std::string &Path : TC.getFilePaths()) {
2716 // Always print a separator. ResourceDir was the first item shown.
2717 llvm::outs() << llvm::sys::EnvPathSeparator;
2718 // Interpretation of leading '=' is needed only for NetBSD.
2719 if (Path[0] == '=')
2720 llvm::outs() << sysroot << Path.substr(pos: 1);
2721 else
2722 llvm::outs() << Path;
2723 }
2724 llvm::outs() << "\n";
2725 return false;
2726 }
2727
2728 if (C.getArgs().hasArg(Ids: options::OPT_print_cxx_stdlib)) {
2729 llvm::outs() << TC.GetCXXStdlibName(Args: C.getArgs()) << '\n';
2730 return false;
2731 }
2732
2733 if (C.getArgs().hasArg(Ids: options::OPT_print_cxx_stdlib_include_dirs)) {
2734 printCXXStdlibIncludeDirs(TC, Args: C.getArgs());
2735 return false;
2736 }
2737
2738 if (C.getArgs().hasArg(Ids: options::OPT_print_std_module_manifest_path)) {
2739 llvm::outs() << GetStdModuleManifestPath(C, TC: C.getDefaultToolChain())
2740 << '\n';
2741 return false;
2742 }
2743
2744 if (C.getArgs().hasArg(Ids: options::OPT_print_runtime_dir)) {
2745 for (auto RuntimePath :
2746 {TC.getRuntimePath(), std::make_optional(t: TC.getCompilerRTPath())}) {
2747 if (RuntimePath && getVFS().exists(Path: *RuntimePath)) {
2748 llvm::outs() << *RuntimePath << '\n';
2749 return false;
2750 }
2751 }
2752 llvm::outs() << "(runtime dir is not present)" << '\n';
2753 return false;
2754 }
2755
2756 if (C.getArgs().hasArg(Ids: options::OPT_print_diagnostic_options)) {
2757 std::vector<std::string> Flags = DiagnosticIDs::getDiagnosticFlags();
2758 for (std::size_t I = 0; I != Flags.size(); I += 2)
2759 llvm::outs() << " " << Flags[I] << "\n " << Flags[I + 1] << "\n\n";
2760 return false;
2761 }
2762
2763 // FIXME: The following handlers should use a callback mechanism, we don't
2764 // know what the client would like to do.
2765 if (Arg *A = C.getArgs().getLastArg(Ids: options::OPT_print_file_name_EQ)) {
2766 llvm::outs() << GetFilePath(Name: A->getValue(), TC) << "\n";
2767 return false;
2768 }
2769
2770 if (Arg *A = C.getArgs().getLastArg(Ids: options::OPT_print_prog_name_EQ)) {
2771 StringRef ProgName = A->getValue();
2772
2773 // Null program name cannot have a path.
2774 if (! ProgName.empty())
2775 llvm::outs() << GetProgramPath(Name: ProgName, TC);
2776
2777 llvm::outs() << "\n";
2778 return false;
2779 }
2780
2781 if (Arg *A = C.getArgs().getLastArg(Ids: options::OPT_autocomplete)) {
2782 StringRef PassedFlags = A->getValue();
2783 HandleAutocompletions(PassedFlags);
2784 return false;
2785 }
2786
2787 if (C.getArgs().hasArg(Ids: options::OPT_print_libgcc_file_name)) {
2788 ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(Args: C.getArgs());
2789 const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(Args: C.getArgs()));
2790 // The 'Darwin' toolchain is initialized only when its arguments are
2791 // computed. Get the default arguments for OFK_None to ensure that
2792 // initialization is performed before trying to access properties of
2793 // the toolchain in the functions below.
2794 // FIXME: Remove when darwin's toolchain is initialized during construction.
2795 // FIXME: For some more esoteric targets the default toolchain is not the
2796 // correct one.
2797 C.getArgsForToolChain(TC: &TC, BA: BoundArch(Triple.getArchName()),
2798 DeviceOffloadKind: Action::OFK_Host);
2799 RegisterEffectiveTriple TripleRAII(TC, Triple);
2800 switch (RLT) {
2801 case ToolChain::RLT_CompilerRT:
2802 llvm::outs() << TC.getCompilerRT(Args: C.getArgs(), Component: "builtins") << "\n";
2803 break;
2804 case ToolChain::RLT_Libgcc:
2805 llvm::outs() << GetFilePath(Name: "libgcc.a", TC) << "\n";
2806 break;
2807 }
2808 return false;
2809 }
2810
2811 if (C.getArgs().hasArg(Ids: options::OPT_print_multi_lib)) {
2812 for (const Multilib &Multilib : TC.getMultilibs())
2813 if (!Multilib.isError())
2814 llvm::outs() << Multilib << "\n";
2815 return false;
2816 }
2817
2818 if (C.getArgs().hasArg(Ids: options::OPT_print_multi_flags)) {
2819 Multilib::flags_list ArgFlags = TC.getMultilibFlags(C.getArgs());
2820 llvm::StringSet<> ExpandedFlags = TC.getMultilibs().expandFlags(ArgFlags);
2821 std::set<llvm::StringRef> SortedFlags;
2822 for (const auto &FlagEntry : ExpandedFlags)
2823 SortedFlags.insert(x: FlagEntry.getKey());
2824 for (auto Flag : SortedFlags)
2825 llvm::outs() << Flag << '\n';
2826 return false;
2827 }
2828
2829 if (C.getArgs().hasArg(Ids: options::OPT_print_multi_directory)) {
2830 for (const Multilib &Multilib : TC.getSelectedMultilibs()) {
2831 if (Multilib.gccSuffix().empty())
2832 llvm::outs() << ".\n";
2833 else {
2834 StringRef Suffix(Multilib.gccSuffix());
2835 assert(Suffix.front() == '/');
2836 llvm::outs() << Suffix.substr(Start: 1) << "\n";
2837 }
2838 }
2839 return false;
2840 }
2841
2842 if (C.getArgs().hasArg(Ids: options::OPT_print_target_triple)) {
2843 llvm::outs() << TC.getTripleString() << "\n";
2844 return false;
2845 }
2846
2847 if (C.getArgs().hasArg(Ids: options::OPT_print_effective_triple)) {
2848 const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(Args: C.getArgs()));
2849 llvm::outs() << Triple.getTriple() << "\n";
2850 return false;
2851 }
2852
2853 if (C.getArgs().hasArg(Ids: options::OPT_print_targets)) {
2854 llvm::TargetRegistry::printRegisteredTargetsForVersion(OS&: llvm::outs());
2855 return false;
2856 }
2857
2858 return true;
2859}
2860
2861enum {
2862 TopLevelAction = 0,
2863 HeadSibAction = 1,
2864 OtherSibAction = 2,
2865};
2866
2867// Display an action graph human-readably. Action A is the "sink" node
2868// and latest-occuring action. Traversal is in pre-order, visiting the
2869// inputs to each action before printing the action itself.
2870static unsigned PrintActions1(const Compilation &C, Action *A,
2871 std::map<Action *, unsigned> &Ids,
2872 Twine Indent = {}, int Kind = TopLevelAction) {
2873 if (auto It = Ids.find(x: A); It != Ids.end()) // A was already visited.
2874 return It->second;
2875
2876 std::string str;
2877 llvm::raw_string_ostream os(str);
2878
2879 auto getSibIndent = [](int K) -> Twine {
2880 return (K == HeadSibAction) ? " " : (K == OtherSibAction) ? "| " : "";
2881 };
2882
2883 Twine SibIndent = Indent + getSibIndent(Kind);
2884 int SibKind = HeadSibAction;
2885 os << Action::getClassName(AC: A->getKind()) << ", ";
2886 if (InputAction *IA = dyn_cast<InputAction>(Val: A)) {
2887 os << "\"" << IA->getInputArg().getValue() << "\"";
2888 } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(Val: A)) {
2889 os << '"' << BIA->getArch().ArchName << '"' << ", {"
2890 << PrintActions1(C, A: *BIA->input_begin(), Ids, Indent: SibIndent, Kind: SibKind) << "}";
2891 } else if (OffloadAction *OA = dyn_cast<OffloadAction>(Val: A)) {
2892 bool IsFirst = true;
2893 OA->doOnEachDependence(Work: [&](Action *A, const ToolChain *TC, BoundArch BA) {
2894 assert(TC && "Unknown host toolchain");
2895 // E.g. for two CUDA device dependences whose bound arch is sm_20 and
2896 // sm_35 this will generate:
2897 // "cuda-device" (nvptx64-nvidia-cuda:sm_20) {#ID}, "cuda-device"
2898 // (nvptx64-nvidia-cuda:sm_35) {#ID}
2899 if (!IsFirst)
2900 os << ", ";
2901 os << '"';
2902 os << A->getOffloadingKindPrefix();
2903 os << " (";
2904 os << TC->getTripleString();
2905 if (!BA.empty())
2906 os << ":" << BA.ArchName;
2907 os << ")";
2908 os << '"';
2909 os << " {" << PrintActions1(C, A, Ids, Indent: SibIndent, Kind: SibKind) << "}";
2910 IsFirst = false;
2911 SibKind = OtherSibAction;
2912 });
2913 } else {
2914 const ActionList *AL = &A->getInputs();
2915
2916 if (AL->size()) {
2917 const char *Prefix = "{";
2918 for (Action *PreRequisite : *AL) {
2919 os << Prefix << PrintActions1(C, A: PreRequisite, Ids, Indent: SibIndent, Kind: SibKind);
2920 Prefix = ", ";
2921 SibKind = OtherSibAction;
2922 }
2923 os << "}";
2924 } else
2925 os << "{}";
2926 }
2927
2928 // Append offload info for all options other than the offloading action
2929 // itself (e.g. (cuda-device, sm_20) or (cuda-host)).
2930 std::string offload_str;
2931 llvm::raw_string_ostream offload_os(offload_str);
2932 if (!isa<OffloadAction>(Val: A)) {
2933 auto S = A->getOffloadingKindPrefix();
2934 if (!S.empty()) {
2935 offload_os << ", (" << S;
2936 if (!A->getOffloadingArch().empty())
2937 offload_os << ", " << A->getOffloadingArch().ArchName;
2938 offload_os << ")";
2939 }
2940 }
2941
2942 auto getSelfIndent = [](int K) -> Twine {
2943 return (K == HeadSibAction) ? "+- " : (K == OtherSibAction) ? "|- " : "";
2944 };
2945
2946 unsigned Id = Ids.size();
2947 Ids[A] = Id;
2948 llvm::errs() << Indent + getSelfIndent(Kind) << Id << ": " << os.str() << ", "
2949 << types::getTypeName(Id: A->getType()) << offload_os.str() << "\n";
2950
2951 return Id;
2952}
2953
2954// Print the action graphs in a compilation C.
2955// For example "clang -c file1.c file2.c" is composed of two subgraphs.
2956void Driver::PrintActions(const Compilation &C) const {
2957 std::map<Action *, unsigned> Ids;
2958 for (Action *A : C.getActions())
2959 PrintActions1(C, A, Ids);
2960}
2961
2962/// Check whether the given input tree contains any compilation or
2963/// assembly actions.
2964static bool ContainsCompileOrAssembleAction(const Action *A) {
2965 if (isa<CompileJobAction>(Val: A) || isa<BackendJobAction>(Val: A) ||
2966 isa<AssembleJobAction>(Val: A))
2967 return true;
2968
2969 return llvm::any_of(Range: A->inputs(), P: ContainsCompileOrAssembleAction);
2970}
2971
2972void Driver::BuildUniversalActions(Compilation &C, const ToolChain &TC,
2973 const InputList &BAInputs) const {
2974 DerivedArgList &Args = C.getArgs();
2975 ActionList &Actions = C.getActions();
2976 llvm::PrettyStackTraceString CrashInfo("Building universal build actions");
2977 // Collect the list of architectures. Duplicates are allowed, but should only
2978 // be handled once (in the order seen).
2979 llvm::StringSet<> ArchNames;
2980 SmallVector<const char *, 4> Archs;
2981 for (Arg *A : Args) {
2982 if (A->getOption().matches(ID: options::OPT_arch)) {
2983 // Validate the option here; we don't save the type here because its
2984 // particular spelling may participate in other driver choices.
2985 llvm::Triple::ArchType Arch =
2986 tools::darwin::getArchTypeForMachOArchName(Str: A->getValue());
2987 if (Arch == llvm::Triple::UnknownArch) {
2988 Diag(DiagID: clang::diag::err_drv_invalid_arch_name) << A->getAsString(Args);
2989 continue;
2990 }
2991
2992 A->claim();
2993 if (ArchNames.insert(key: A->getValue()).second)
2994 Archs.push_back(Elt: A->getValue());
2995 }
2996 }
2997
2998 // When there is no explicit arch for this platform, make sure we still bind
2999 // the architecture (to the default) so that -Xarch_ is handled correctly.
3000 if (!Archs.size())
3001 Archs.push_back(Elt: Args.MakeArgString(Str: TC.getDefaultUniversalArchName()));
3002
3003 ActionList SingleActions;
3004 BuildActions(C, Args, Inputs: BAInputs, Actions&: SingleActions);
3005
3006 // Add in arch bindings for every top level action, as well as lipo and
3007 // dsymutil steps if needed.
3008 for (Action* Act : SingleActions) {
3009 // Make sure we can lipo this kind of output. If not (and it is an actual
3010 // output) then we disallow, since we can't create an output file with the
3011 // right name without overwriting it. We could remove this oddity by just
3012 // changing the output names to include the arch, which would also fix
3013 // -save-temps. Compatibility wins for now.
3014
3015 if (Archs.size() > 1 && !types::canLipoType(Id: Act->getType()))
3016 Diag(DiagID: clang::diag::err_drv_invalid_output_with_multiple_archs)
3017 << types::getTypeName(Id: Act->getType());
3018
3019 ActionList Inputs;
3020 for (unsigned i = 0, e = Archs.size(); i != e; ++i)
3021 Inputs.push_back(Elt: C.MakeAction<BindArchAction>(Arg&: Act, Arg: BoundArch(Archs[i])));
3022
3023 // Lipo if necessary, we do it this way because we need to set the arch flag
3024 // so that -Xarch_ gets overwritten.
3025 if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
3026 Actions.append(in_start: Inputs.begin(), in_end: Inputs.end());
3027 else
3028 Actions.push_back(Elt: C.MakeAction<LipoJobAction>(Arg&: Inputs, Arg: Act->getType()));
3029
3030 // Handle debug info queries.
3031 Arg *A = Args.getLastArg(Ids: options::OPT_g_Group);
3032 bool enablesDebugInfo = A && !A->getOption().matches(ID: options::OPT_g0) &&
3033 !A->getOption().matches(ID: options::OPT_gstabs);
3034 bool enablesPseudoProbe =
3035 Args.hasFlag(Pos: options::OPT_fpseudo_probe_for_profiling,
3036 Neg: options::OPT_fno_pseudo_probe_for_profiling, Default: false);
3037 bool enablesDebugInfoForProfiling =
3038 Args.hasFlag(Pos: options::OPT_fdebug_info_for_profiling,
3039 Neg: options::OPT_fno_debug_info_for_profiling, Default: false);
3040 if ((enablesDebugInfo || willEmitRemarks(Args) || enablesPseudoProbe ||
3041 enablesDebugInfoForProfiling) &&
3042 ContainsCompileOrAssembleAction(A: Actions.back())) {
3043
3044 // Add a 'dsymutil' step if necessary, when debug info, remarks, or
3045 // pseudo probes are enabled and we have a compile input. We need to run
3046 // 'dsymutil' ourselves in such cases because the debug info will refer
3047 // to a temporary object file which will be removed at the end of the
3048 // compilation process.
3049 if (Act->getType() == types::TY_Image) {
3050 ActionList Inputs;
3051 Inputs.push_back(Elt: Actions.back());
3052 Actions.pop_back();
3053 Actions.push_back(
3054 Elt: C.MakeAction<DsymutilJobAction>(Arg&: Inputs, Arg: types::TY_dSYM));
3055 }
3056
3057 // Verify the debug info output.
3058 if (Args.hasArg(Ids: options::OPT_verify_debug_info)) {
3059 Action *LastAction = Actions.pop_back_val();
3060 Actions.push_back(Elt: C.MakeAction<VerifyDebugInfoJobAction>(
3061 Arg&: LastAction, Arg: types::TY_Nothing));
3062 }
3063 }
3064 }
3065}
3066
3067bool Driver::DiagnoseInputExistence(StringRef Value, types::ID Ty,
3068 bool TypoCorrect) const {
3069 if (!getCheckInputsExist())
3070 return true;
3071
3072 // stdin always exists.
3073 if (Value == "-")
3074 return true;
3075
3076 // If it's a header to be found in the system or user search path, then defer
3077 // complaints about its absence until those searches can be done. When we
3078 // are definitely processing headers for C++20 header units, extend this to
3079 // allow the user to put "-fmodule-header -xc++-header vector" for example.
3080 if (Ty == types::TY_CXXSHeader || Ty == types::TY_CXXUHeader ||
3081 (ModulesModeCXX20 && Ty == types::TY_CXXHeader))
3082 return true;
3083
3084 if (getVFS().exists(Path: Value))
3085 return true;
3086
3087 if (TypoCorrect) {
3088 // Check if the filename is a typo for an option flag. OptTable thinks
3089 // that all args that are not known options and that start with / are
3090 // filenames, but e.g. `/diagnostic:caret` is more likely a typo for
3091 // the option `/diagnostics:caret` than a reference to a file in the root
3092 // directory.
3093 std::string Nearest;
3094 if (getOpts().findNearest(Option: Value, NearestString&: Nearest, VisibilityMask: getOptionVisibilityMask()) <= 1) {
3095 Diag(DiagID: clang::diag::err_drv_no_such_file_with_suggestion)
3096 << Value << Nearest;
3097 return false;
3098 }
3099 }
3100
3101 // In CL mode, don't error on apparently non-existent linker inputs, because
3102 // they can be influenced by linker flags the clang driver might not
3103 // understand.
3104 // Examples:
3105 // - `clang-cl main.cc ole32.lib` in a non-MSVC shell will make the driver
3106 // module look for an MSVC installation in the registry. (We could ask
3107 // the MSVCToolChain object if it can find `ole32.lib`, but the logic to
3108 // look in the registry might move into lld-link in the future so that
3109 // lld-link invocations in non-MSVC shells just work too.)
3110 // - `clang-cl ... /link ...` can pass arbitrary flags to the linker,
3111 // including /libpath:, which is used to find .lib and .obj files.
3112 // So do not diagnose this on the driver level. Rely on the linker diagnosing
3113 // it. (If we don't end up invoking the linker, this means we'll emit a
3114 // "'linker' input unused [-Wunused-command-line-argument]" warning instead
3115 // of an error.)
3116 //
3117 // Only do this skip after the typo correction step above. `/Brepo` is treated
3118 // as TY_Object, but it's clearly a typo for `/Brepro`. It seems fine to emit
3119 // an error if we have a flag that's within an edit distance of 1 from a
3120 // flag. (Users can use `-Wl,` or `/linker` to launder the flag past the
3121 // driver in the unlikely case they run into this.)
3122 //
3123 // Don't do this for inputs that start with a '/', else we'd pass options
3124 // like /libpath: through to the linker silently.
3125 //
3126 // Emitting an error for linker inputs can also cause incorrect diagnostics
3127 // with the gcc driver. The command
3128 // clang -fuse-ld=lld -Wl,--chroot,some/dir /file.o
3129 // will make lld look for some/dir/file.o, while we will diagnose here that
3130 // `/file.o` does not exist. However, configure scripts check if
3131 // `clang /GR-` compiles without error to see if the compiler is cl.exe,
3132 // so we can't downgrade diagnostics for `/GR-` from an error to a warning
3133 // in cc mode. (We can in cl mode because cl.exe itself only warns on
3134 // unknown flags.)
3135 if (IsCLMode() && Ty == types::TY_Object && !Value.starts_with(Prefix: "/"))
3136 return true;
3137
3138 Diag(DiagID: clang::diag::err_drv_no_such_file) << Value;
3139 return false;
3140}
3141
3142// Get the C++20 Header Unit type corresponding to the input type.
3143static types::ID CXXHeaderUnitType(ModuleHeaderMode HM) {
3144 switch (HM) {
3145 case HeaderMode_User:
3146 return types::TY_CXXUHeader;
3147 case HeaderMode_System:
3148 return types::TY_CXXSHeader;
3149 case HeaderMode_Default:
3150 break;
3151 case HeaderMode_None:
3152 llvm_unreachable("should not be called in this case");
3153 }
3154 return types::TY_CXXHUHeader;
3155}
3156
3157// Construct a the list of inputs and their types.
3158void Driver::BuildInputs(const ToolChain &TC, DerivedArgList &Args,
3159 InputList &Inputs) const {
3160 const llvm::opt::OptTable &Opts = getOpts();
3161 // Track the current user specified (-x) input. We also explicitly track the
3162 // argument used to set the type; we only want to claim the type when we
3163 // actually use it, so we warn about unused -x arguments.
3164 types::ID InputType = types::TY_Nothing;
3165 Arg *InputTypeArg = nullptr;
3166
3167 // The last /TC or /TP option sets the input type to C or C++ globally.
3168 if (Arg *TCTP = Args.getLastArgNoClaim(Ids: options::OPT__SLASH_TC,
3169 Ids: options::OPT__SLASH_TP)) {
3170 InputTypeArg = TCTP;
3171 InputType = TCTP->getOption().matches(ID: options::OPT__SLASH_TC)
3172 ? types::TY_C
3173 : types::TY_CXX;
3174
3175 Arg *Previous = nullptr;
3176 bool ShowNote = false;
3177 for (Arg *A :
3178 Args.filtered(Ids: options::OPT__SLASH_TC, Ids: options::OPT__SLASH_TP)) {
3179 if (Previous) {
3180 Diag(DiagID: clang::diag::warn_drv_overriding_option)
3181 << Previous->getSpelling() << A->getSpelling();
3182 ShowNote = true;
3183 }
3184 Previous = A;
3185 }
3186 if (ShowNote)
3187 Diag(DiagID: clang::diag::note_drv_t_option_is_global);
3188 }
3189
3190 // Warn -x after last input file has no effect
3191 {
3192 Arg *LastXArg = Args.getLastArgNoClaim(Ids: options::OPT_x);
3193 Arg *LastInputArg = Args.getLastArgNoClaim(Ids: options::OPT_INPUT);
3194 if (LastXArg && LastInputArg &&
3195 LastInputArg->getIndex() < LastXArg->getIndex())
3196 Diag(DiagID: clang::diag::warn_drv_unused_x) << LastXArg->getValue();
3197 }
3198
3199 bool IsSYCL = Args.hasFlag(Pos: options::OPT_fsycl, Neg: options::OPT_fno_sycl, Default: false);
3200
3201 for (Arg *A : Args) {
3202 if (A->getOption().getKind() == Option::InputClass) {
3203 const char *Value = A->getValue();
3204 types::ID Ty = types::TY_INVALID;
3205
3206 // Infer the input type if necessary.
3207 if (InputType == types::TY_Nothing) {
3208 // If there was an explicit arg for this, claim it.
3209 if (InputTypeArg)
3210 InputTypeArg->claim();
3211
3212 // stdin must be handled specially.
3213 if (strcmp(s1: Value, s2: "-") == 0) {
3214 if (IsFlangMode()) {
3215 Ty = types::TY_Fortran;
3216 } else if (IsDXCMode()) {
3217 Ty = types::TY_HLSL;
3218 } else if (IsSYCL) {
3219 Ty = types::TY_CXX;
3220 } else {
3221 // If running with -E, treat as a C input (this changes the
3222 // builtin macros, for example). This may be overridden by -ObjC
3223 // below.
3224 //
3225 // Otherwise emit an error but still use a valid type to avoid
3226 // spurious errors (e.g., no inputs).
3227 assert(!CCGenDiagnostics && "stdin produces no crash reproducer");
3228 if (!Args.hasArgNoClaim(Ids: options::OPT_E) && !CCCIsCPP())
3229 Diag(DiagID: IsCLMode() ? clang::diag::err_drv_unknown_stdin_type_clang_cl
3230 : clang::diag::err_drv_unknown_stdin_type);
3231 Ty = types::TY_C;
3232 }
3233 } else {
3234 // Otherwise lookup by extension.
3235 // Fallback is C if invoked as C preprocessor, C++ if invoked with
3236 // clang-cl /E, or Object otherwise.
3237 // We use a host hook here because Darwin at least has its own
3238 // idea of what .s is.
3239 if (const char *Ext = strrchr(s: Value, c: '.'))
3240 Ty = TC.LookupTypeForExtension(Ext: Ext + 1);
3241
3242 if (Ty == types::TY_INVALID) {
3243 if (IsCLMode() && (Args.hasArgNoClaim(Ids: options::OPT_E) || CCGenDiagnostics))
3244 Ty = types::TY_CXX;
3245 else if (CCCIsCPP() || CCGenDiagnostics)
3246 Ty = types::TY_C;
3247 else if (IsDXCMode())
3248 Ty = types::TY_HLSL;
3249 else
3250 Ty = types::TY_Object;
3251 }
3252
3253 // If the driver is invoked as C++ compiler (like clang++ or c++) it
3254 // should autodetect some input files as C++ for g++ compatibility.
3255 if (CCCIsCXX()) {
3256 types::ID OldTy = Ty;
3257 Ty = types::lookupCXXTypeForCType(Id: Ty);
3258
3259 // Do not complain about foo.h, when we are known to be processing
3260 // it as a C++20 header unit.
3261 if (Ty != OldTy && !(OldTy == types::TY_CHeader && hasHeaderMode()))
3262 Diag(DiagID: clang::diag::warn_drv_treating_input_as_cxx)
3263 << getTypeName(Id: OldTy) << getTypeName(Id: Ty);
3264 }
3265
3266 // If running with -fthinlto-index=, extensions that normally identify
3267 // native object files actually identify LLVM bitcode files.
3268 if (Args.hasArgNoClaim(Ids: options::OPT_fthinlto_index_EQ) &&
3269 Ty == types::TY_Object)
3270 Ty = types::TY_LLVM_BC;
3271 }
3272
3273 // -ObjC and -ObjC++ override the default language, but only for "source
3274 // files". We just treat everything that isn't a linker input as a
3275 // source file.
3276 //
3277 // FIXME: Clean this up if we move the phase sequence into the type.
3278 if (Ty != types::TY_Object) {
3279 if (Args.hasArg(Ids: options::OPT_ObjC))
3280 Ty = types::TY_ObjC;
3281 else if (Args.hasArg(Ids: options::OPT_ObjCXX))
3282 Ty = types::TY_ObjCXX;
3283 }
3284
3285 // Disambiguate headers that are meant to be header units from those
3286 // intended to be PCH. Avoid missing '.h' cases that are counted as
3287 // C headers by default - we know we are in C++ mode and we do not
3288 // want to issue a complaint about compiling things in the wrong mode.
3289 if ((Ty == types::TY_CXXHeader || Ty == types::TY_CHeader) &&
3290 hasHeaderMode())
3291 Ty = CXXHeaderUnitType(HM: CXX20HeaderType);
3292 } else {
3293 assert(InputTypeArg && "InputType set w/o InputTypeArg");
3294 if (!InputTypeArg->getOption().matches(ID: options::OPT_x)) {
3295 // If emulating cl.exe, make sure that /TC and /TP don't affect input
3296 // object files.
3297 const char *Ext = strrchr(s: Value, c: '.');
3298 if (Ext && TC.LookupTypeForExtension(Ext: Ext + 1) == types::TY_Object)
3299 Ty = types::TY_Object;
3300 }
3301 if (Ty == types::TY_INVALID) {
3302 Ty = InputType;
3303 InputTypeArg->claim();
3304 }
3305 }
3306
3307 if ((Ty == types::TY_C || Ty == types::TY_CXX) &&
3308 Args.hasArgNoClaim(Ids: options::OPT_hipstdpar))
3309 Ty = types::TY_HIP;
3310
3311 if (DiagnoseInputExistence(Value, Ty, /*TypoCorrect=*/true))
3312 Inputs.push_back(Elt: std::make_pair(x&: Ty, y&: A));
3313
3314 } else if (A->getOption().matches(ID: options::OPT__SLASH_Tc)) {
3315 StringRef Value = A->getValue();
3316 if (DiagnoseInputExistence(Value, Ty: types::TY_C,
3317 /*TypoCorrect=*/false)) {
3318 Arg *InputArg = makeInputArg(Args, Opts, Value: A->getValue());
3319 Inputs.push_back(Elt: std::make_pair(x: types::TY_C, y&: InputArg));
3320 }
3321 A->claim();
3322 } else if (A->getOption().matches(ID: options::OPT__SLASH_Tp)) {
3323 StringRef Value = A->getValue();
3324 if (DiagnoseInputExistence(Value, Ty: types::TY_CXX,
3325 /*TypoCorrect=*/false)) {
3326 Arg *InputArg = makeInputArg(Args, Opts, Value: A->getValue());
3327 Inputs.push_back(Elt: std::make_pair(x: types::TY_CXX, y&: InputArg));
3328 }
3329 A->claim();
3330 } else if (A->getOption().hasFlag(Val: options::LinkerInput)) {
3331 // Just treat as object type, we could make a special type for this if
3332 // necessary.
3333 Inputs.push_back(Elt: std::make_pair(x: types::TY_Object, y&: A));
3334
3335 } else if (A->getOption().matches(ID: options::OPT_x)) {
3336 InputTypeArg = A;
3337 InputType = types::lookupTypeForTypeSpecifier(Name: A->getValue());
3338 A->claim();
3339
3340 // Follow gcc behavior and treat as linker input for invalid -x
3341 // options. Its not clear why we shouldn't just revert to unknown; but
3342 // this isn't very important, we might as well be bug compatible.
3343 if (!InputType) {
3344 Diag(DiagID: clang::diag::err_drv_unknown_language) << A->getValue();
3345 InputType = types::TY_Object;
3346 }
3347
3348 // If the user has put -fmodule-header{,=} then we treat C++ headers as
3349 // header unit inputs. So we 'promote' -xc++-header appropriately.
3350 if (InputType == types::TY_CXXHeader && hasHeaderMode())
3351 InputType = CXXHeaderUnitType(HM: CXX20HeaderType);
3352 } else if (A->getOption().getID() == options::OPT_U) {
3353 assert(A->getNumValues() == 1 && "The /U option has one value.");
3354 StringRef Val = A->getValue(N: 0);
3355 if (Val.find_first_of(Chars: "/\\") != StringRef::npos) {
3356 // Warn about e.g. "/Users/me/myfile.c".
3357 Diag(DiagID: diag::warn_slash_u_filename) << Val;
3358 Diag(DiagID: diag::note_use_dashdash);
3359 }
3360 }
3361 }
3362 if (CCCIsCPP() && Inputs.empty()) {
3363 // If called as standalone preprocessor, stdin is processed
3364 // if no other input is present.
3365 Arg *A = makeInputArg(Args, Opts, Value: "-");
3366 Inputs.push_back(Elt: std::make_pair(x: types::TY_C, y&: A));
3367 }
3368}
3369
3370namespace {
3371/// Provides a convenient interface for different programming models to generate
3372/// the required device actions.
3373class OffloadingActionBuilder final {
3374 /// Flag used to trace errors in the builder.
3375 bool IsValid = false;
3376
3377 /// The compilation that is using this builder.
3378 Compilation &C;
3379
3380 /// Map between an input argument and the offload kinds used to process it.
3381 std::map<const Arg *, unsigned> InputArgToOffloadKindMap;
3382
3383 /// Map between a host action and its originating input argument.
3384 std::map<Action *, const Arg *> HostActionToInputArgMap;
3385
3386 /// Builder interface. It doesn't build anything or keep any state.
3387 class DeviceActionBuilder {
3388 public:
3389 typedef const llvm::SmallVectorImpl<phases::ID> PhasesTy;
3390
3391 enum ActionBuilderReturnCode {
3392 // The builder acted successfully on the current action.
3393 ABRT_Success,
3394 // The builder didn't have to act on the current action.
3395 ABRT_Inactive,
3396 // The builder was successful and requested the host action to not be
3397 // generated.
3398 ABRT_Ignore_Host,
3399 };
3400
3401 protected:
3402 /// Compilation associated with this builder.
3403 Compilation &C;
3404
3405 /// Tool chains associated with this builder. The same programming
3406 /// model may have associated one or more tool chains.
3407 /// There should be one entry for each TargetID.
3408 SmallVector<const ToolChain *, 2> ToolChains;
3409 const ToolChain *FatBinaryToolChain = nullptr;
3410
3411 /// The derived arguments associated with this builder.
3412 DerivedArgList &Args;
3413
3414 /// The inputs associated with this builder.
3415 const InputList &Inputs;
3416
3417 /// The associated offload kind.
3418 Action::OffloadKind AssociatedOffloadKind = Action::OFK_None;
3419
3420 public:
3421 DeviceActionBuilder(Compilation &C, DerivedArgList &Args,
3422 const InputList &Inputs,
3423 Action::OffloadKind AssociatedOffloadKind)
3424 : C(C), Args(Args), Inputs(Inputs),
3425 AssociatedOffloadKind(AssociatedOffloadKind) {}
3426 virtual ~DeviceActionBuilder() {}
3427
3428 /// Fill up the array \a DA with all the device dependences that should be
3429 /// added to the provided host action \a HostAction. By default it is
3430 /// inactive.
3431 virtual ActionBuilderReturnCode
3432 getDeviceDependences(OffloadAction::DeviceDependences &DA,
3433 phases::ID CurPhase, phases::ID FinalPhase,
3434 PhasesTy &Phases) {
3435 return ABRT_Inactive;
3436 }
3437
3438 /// Update the state to include the provided host action \a HostAction as a
3439 /// dependency of the current device action. By default it is inactive.
3440 virtual ActionBuilderReturnCode addDeviceDependences(Action *HostAction) {
3441 return ABRT_Inactive;
3442 }
3443
3444 /// Append top level actions generated by the builder.
3445 virtual void appendTopLevelActions(ActionList &AL) {}
3446
3447 /// Append linker device actions generated by the builder.
3448 virtual void appendLinkDeviceActions(ActionList &AL) {}
3449
3450 /// Append linker host action generated by the builder.
3451 virtual Action* appendLinkHostActions(ActionList &AL) { return nullptr; }
3452
3453 /// Append linker actions generated by the builder.
3454 virtual void appendLinkDependences(OffloadAction::DeviceDependences &DA) {}
3455
3456 /// Initialize the builder. Return true if any initialization errors are
3457 /// found.
3458 virtual bool initialize() { return false; }
3459
3460 /// Return true if the builder can use bundling/unbundling.
3461 virtual bool canUseBundlerUnbundler() const { return false; }
3462
3463 /// Return true if this builder is valid. We have a valid builder if we have
3464 /// associated device tool chains.
3465 bool isValid() { return !ToolChains.empty(); }
3466
3467 /// Return the associated offload kind.
3468 Action::OffloadKind getAssociatedOffloadKind() {
3469 return AssociatedOffloadKind;
3470 }
3471 };
3472
3473 /// Base class for CUDA/HIP action builder. It injects device code in
3474 /// the host backend action.
3475 class CudaActionBuilderBase : public DeviceActionBuilder {
3476 protected:
3477 /// Flags to signal if the user requested host-only or device-only
3478 /// compilation.
3479 bool CompileHostOnly = false;
3480 bool CompileDeviceOnly = false;
3481 bool EmitLLVM = false;
3482 bool EmitAsm = false;
3483
3484 /// List of GPU architectures to use in this compilation.
3485 SmallVector<BoundArch, 4> GpuArchList;
3486
3487 /// The CUDA actions for the current input.
3488 ActionList CudaDeviceActions;
3489
3490 /// The CUDA fat binary if it was generated for the current input.
3491 Action *CudaFatBinary = nullptr;
3492
3493 /// Flag that is set to true if this builder acted on the current input.
3494 bool IsActive = false;
3495
3496 /// Flag for -fgpu-rdc.
3497 bool Relocatable = false;
3498
3499 /// Default GPU architecture if there's no one specified.
3500 OffloadArch DefaultOffloadArch = OffloadArch::getUnknown();
3501
3502 /// Compilation unit ID specified by option '-fuse-cuid=' or'-cuid='.
3503 const CUIDOptions &CUIDOpts;
3504
3505 public:
3506 CudaActionBuilderBase(Compilation &C, DerivedArgList &Args,
3507 const InputList &Inputs, Action::OffloadKind OFKind)
3508 : DeviceActionBuilder(C, Args, Inputs, OFKind),
3509 CUIDOpts(C.getDriver().getCUIDOpts()) {
3510
3511 CompileDeviceOnly = C.getDriver().offloadDeviceOnly();
3512 Relocatable = Args.hasFlag(Pos: options::OPT_fgpu_rdc,
3513 Neg: options::OPT_fno_gpu_rdc, /*Default=*/false);
3514 }
3515
3516 ActionBuilderReturnCode addDeviceDependences(Action *HostAction) override {
3517 // While generating code for CUDA, we only depend on the host input action
3518 // to trigger the creation of all the CUDA device actions.
3519
3520 // If we are dealing with an input action, replicate it for each GPU
3521 // architecture. If we are in host-only mode we return 'success' so that
3522 // the host uses the CUDA offload kind.
3523 if (auto *IA = dyn_cast<InputAction>(Val: HostAction)) {
3524 // If the host input is not CUDA or HIP, we don't need to bother about
3525 // this input.
3526 if (!(IA->getType() == types::TY_CUDA ||
3527 IA->getType() == types::TY_HIP ||
3528 IA->getType() == types::TY_PP_HIP)) {
3529 // The builder will ignore this input.
3530 IsActive = false;
3531 return ABRT_Inactive;
3532 }
3533
3534 // Set the flag to true, so that the builder acts on the current input.
3535 IsActive = true;
3536
3537 if (CUIDOpts.isEnabled())
3538 IA->setId(CUIDOpts.getCUID(InputFile: IA->getInputArg().getValue(), Args));
3539
3540 if (CompileHostOnly)
3541 return ABRT_Success;
3542
3543 // Replicate inputs for each GPU architecture.
3544 auto Ty = IA->getType() == types::TY_HIP ? types::TY_HIP_DEVICE
3545 : types::TY_CUDA_DEVICE;
3546 for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) {
3547 CudaDeviceActions.push_back(
3548 Elt: C.MakeAction<InputAction>(Arg: IA->getInputArg(), Arg&: Ty, Arg: IA->getId()));
3549 }
3550
3551 return ABRT_Success;
3552 }
3553
3554 // If this is an unbundling action use it as is for each CUDA toolchain.
3555 if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(Val: HostAction)) {
3556
3557 // If -fgpu-rdc is disabled, should not unbundle since there is no
3558 // device code to link.
3559 if (UA->getType() == types::TY_Object && !Relocatable)
3560 return ABRT_Inactive;
3561
3562 CudaDeviceActions.clear();
3563 auto *IA = cast<InputAction>(Val: UA->getInputs().back());
3564 std::string FileName = IA->getInputArg().getAsString(Args);
3565 // Check if the type of the file is the same as the action. Do not
3566 // unbundle it if it is not. Do not unbundle .so files, for example,
3567 // which are not object files. Files with extension ".lib" is classified
3568 // as TY_Object but they are actually archives, therefore should not be
3569 // unbundled here as objects. They will be handled at other places.
3570 const StringRef LibFileExt = ".lib";
3571 if (IA->getType() == types::TY_Object &&
3572 (!llvm::sys::path::has_extension(path: FileName) ||
3573 types::lookupTypeForExtension(
3574 Ext: llvm::sys::path::extension(path: FileName).drop_front()) !=
3575 types::TY_Object ||
3576 llvm::sys::path::extension(path: FileName) == LibFileExt))
3577 return ABRT_Inactive;
3578
3579 for (auto [Arch, ToolChain] : llvm::zip(t&: GpuArchList, u&: ToolChains)) {
3580 CudaDeviceActions.push_back(Elt: UA);
3581 UA->registerDependentActionInfo(TC: ToolChain, BA: Arch,
3582 Kind: AssociatedOffloadKind);
3583 }
3584 IsActive = true;
3585 return ABRT_Success;
3586 }
3587
3588 return IsActive ? ABRT_Success : ABRT_Inactive;
3589 }
3590
3591 void appendTopLevelActions(ActionList &AL) override {
3592 // Utility to append actions to the top level list.
3593 auto AddTopLevel = [&](Action *A, BoundArch BA, const ToolChain *TC) {
3594 OffloadAction::DeviceDependences Dep;
3595 Dep.add(A&: *A, TC: *TC, BA, OKind: AssociatedOffloadKind);
3596 AL.push_back(Elt: C.MakeAction<OffloadAction>(Arg&: Dep, Arg: A->getType()));
3597 };
3598
3599 // If we have a fat binary, add it to the list.
3600 if (CudaFatBinary) {
3601 AddTopLevel(CudaFatBinary, {}, FatBinaryToolChain);
3602 CudaDeviceActions.clear();
3603 CudaFatBinary = nullptr;
3604 return;
3605 }
3606
3607 if (CudaDeviceActions.empty())
3608 return;
3609
3610 // If we have CUDA actions at this point, that's because we have a have
3611 // partial compilation, so we should have an action for each GPU
3612 // architecture.
3613 assert(CudaDeviceActions.size() == GpuArchList.size() &&
3614 "Expecting one action per GPU architecture.");
3615 assert(ToolChains.size() == GpuArchList.size() &&
3616 "Expecting to have a toolchain per GPU architecture");
3617 for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I)
3618 AddTopLevel(CudaDeviceActions[I], GpuArchList[I], ToolChains[I]);
3619
3620 CudaDeviceActions.clear();
3621 }
3622
3623 bool initialize() override {
3624 assert(AssociatedOffloadKind == Action::OFK_Cuda ||
3625 AssociatedOffloadKind == Action::OFK_HIP);
3626
3627 // We don't need to support CUDA.
3628 if (AssociatedOffloadKind == Action::OFK_Cuda &&
3629 !C.hasOffloadToolChain<Action::OFK_Cuda>())
3630 return false;
3631
3632 // We don't need to support HIP.
3633 if (AssociatedOffloadKind == Action::OFK_HIP &&
3634 !C.hasOffloadToolChain<Action::OFK_HIP>())
3635 return false;
3636
3637 const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>();
3638 assert(HostTC && "No toolchain for host compilation.");
3639 if (HostTC->getTriple().isNVPTX() || HostTC->getTriple().isAMDGCN()) {
3640 // We do not support targeting NVPTX/AMDGCN for host compilation. Throw
3641 // an error and abort pipeline construction early so we don't trip
3642 // asserts that assume device-side compilation.
3643 C.getDriver().Diag(DiagID: diag::err_drv_cuda_host_arch)
3644 << HostTC->getTriple().getArchName();
3645 return true;
3646 }
3647
3648 std::set<std::pair<BoundArch, const ToolChain *>> GpuArchs;
3649 for (Action::OffloadKind Kind : {Action::OFK_Cuda, Action::OFK_HIP}) {
3650 for (auto &I : llvm::make_range(p: C.getOffloadToolChains(Kind))) {
3651 for (auto Arch :
3652 C.getDriver().getOffloadArchs(C, Args: C.getArgs(), Kind, TC: *I.second))
3653 GpuArchs.insert(x: {Arch, I.second});
3654 }
3655 }
3656
3657 for (auto [Arch, TC] : GpuArchs) {
3658 GpuArchList.push_back(Elt: Arch);
3659 ToolChains.push_back(Elt: TC);
3660 }
3661
3662 FatBinaryToolChain = ToolChains.front();
3663 CompileHostOnly = C.getDriver().offloadHostOnly();
3664 EmitLLVM = Args.getLastArg(Ids: options::OPT_emit_llvm);
3665 EmitAsm = Args.getLastArg(Ids: options::OPT_S);
3666
3667 return false;
3668 }
3669 };
3670
3671 /// \brief CUDA action builder. It injects device code in the host backend
3672 /// action.
3673 class CudaActionBuilder final : public CudaActionBuilderBase {
3674 public:
3675 CudaActionBuilder(Compilation &C, DerivedArgList &Args,
3676 const InputList &Inputs)
3677 : CudaActionBuilderBase(C, Args, Inputs, Action::OFK_Cuda) {
3678 DefaultOffloadArch = OffloadArch::CudaDefault();
3679 }
3680
3681 ActionBuilderReturnCode
3682 getDeviceDependences(OffloadAction::DeviceDependences &DA,
3683 phases::ID CurPhase, phases::ID FinalPhase,
3684 PhasesTy &Phases) override {
3685 if (!IsActive)
3686 return ABRT_Inactive;
3687
3688 // If we don't have more CUDA actions, we don't have any dependences to
3689 // create for the host.
3690 if (CudaDeviceActions.empty())
3691 return ABRT_Success;
3692
3693 assert(CudaDeviceActions.size() == GpuArchList.size() &&
3694 "Expecting one action per GPU architecture.");
3695 assert(!CompileHostOnly &&
3696 "Not expecting CUDA actions in host-only compilation.");
3697
3698 // If we are generating code for the device or we are in a backend phase,
3699 // we attempt to generate the fat binary. We compile each arch to ptx and
3700 // assemble to cubin, then feed the cubin *and* the ptx into a device
3701 // "link" action, which uses fatbinary to combine these cubins into one
3702 // fatbin. The fatbin is then an input to the host action if not in
3703 // device-only mode.
3704 if (CompileDeviceOnly || CurPhase == phases::Backend) {
3705 ActionList DeviceActions;
3706 for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) {
3707 // Produce the device action from the current phase up to the assemble
3708 // phase.
3709 for (auto Ph : Phases) {
3710 // Skip the phases that were already dealt with.
3711 if (Ph < CurPhase)
3712 continue;
3713 // We have to be consistent with the host final phase.
3714 if (Ph > FinalPhase)
3715 break;
3716
3717 CudaDeviceActions[I] = C.getDriver().ConstructPhaseAction(
3718 C, Args, Phase: Ph, Input: CudaDeviceActions[I], TargetDeviceOffloadKind: Action::OFK_Cuda,
3719 TargetLTOMode: ToolChains[I]->getLTOMode(Args, Kind: Action::OFK_Cuda));
3720
3721 if (Ph == phases::Assemble)
3722 break;
3723 }
3724
3725 // If we didn't reach the assemble phase, we can't generate the fat
3726 // binary. We don't need to generate the fat binary if we are not in
3727 // device-only mode.
3728 if (!isa<AssembleJobAction>(Val: CudaDeviceActions[I]) ||
3729 CompileDeviceOnly)
3730 continue;
3731
3732 Action *AssembleAction = CudaDeviceActions[I];
3733 assert(AssembleAction->getType() == types::TY_Object);
3734 assert(AssembleAction->getInputs().size() == 1);
3735
3736 Action *BackendAction = AssembleAction->getInputs()[0];
3737 assert(BackendAction->getType() == types::TY_PP_Asm);
3738
3739 for (auto &A : {AssembleAction, BackendAction}) {
3740 OffloadAction::DeviceDependences DDep;
3741 DDep.add(A&: *A, TC: *ToolChains[I], BA: GpuArchList[I], OKind: Action::OFK_Cuda);
3742 DeviceActions.push_back(
3743 Elt: C.MakeAction<OffloadAction>(Arg&: DDep, Arg: A->getType()));
3744 }
3745 }
3746
3747 // We generate the fat binary if we have device input actions.
3748 if (!DeviceActions.empty()) {
3749 CudaFatBinary =
3750 C.MakeAction<LinkJobAction>(Arg&: DeviceActions, Arg: types::TY_CUDA_FATBIN);
3751
3752 if (!CompileDeviceOnly) {
3753 DA.add(A&: *CudaFatBinary, TC: *FatBinaryToolChain, /*BA=*/{},
3754 OKind: Action::OFK_Cuda);
3755 // Clear the fat binary, it is already a dependence to an host
3756 // action.
3757 CudaFatBinary = nullptr;
3758 }
3759
3760 // Remove the CUDA actions as they are already connected to an host
3761 // action or fat binary.
3762 CudaDeviceActions.clear();
3763 }
3764
3765 // We avoid creating host action in device-only mode.
3766 return CompileDeviceOnly ? ABRT_Ignore_Host : ABRT_Success;
3767 } else if (CurPhase > phases::Backend) {
3768 // If we are past the backend phase and still have a device action, we
3769 // don't have to do anything as this action is already a device
3770 // top-level action.
3771 return ABRT_Success;
3772 }
3773
3774 assert(CurPhase < phases::Backend && "Generating single CUDA "
3775 "instructions should only occur "
3776 "before the backend phase!");
3777
3778 // By default, we produce an action for each device arch.
3779 for (Action *&A : CudaDeviceActions)
3780 A = C.getDriver().ConstructPhaseAction(C, Args, Phase: CurPhase, Input: A);
3781
3782 return ABRT_Success;
3783 }
3784 };
3785 /// \brief HIP action builder. It injects device code in the host backend
3786 /// action.
3787 class HIPActionBuilder final : public CudaActionBuilderBase {
3788 /// The linker inputs obtained for each device arch.
3789 SmallVector<ActionList, 8> DeviceLinkerInputs;
3790 // The default bundling behavior depends on the type of output, therefore
3791 // BundleOutput needs to be tri-value: None, true, or false.
3792 // Bundle code objects except --no-gpu-output is specified for device
3793 // only compilation. Bundle other type of output files only if
3794 // --gpu-bundle-output is specified for device only compilation.
3795 std::optional<bool> BundleOutput;
3796 std::optional<bool> EmitReloc;
3797
3798 public:
3799 HIPActionBuilder(Compilation &C, DerivedArgList &Args,
3800 const InputList &Inputs)
3801 : CudaActionBuilderBase(C, Args, Inputs, Action::OFK_HIP) {
3802
3803 DefaultOffloadArch = OffloadArch::HIPDefault();
3804
3805 if (Args.hasArg(Ids: options::OPT_fhip_emit_relocatable,
3806 Ids: options::OPT_fno_hip_emit_relocatable)) {
3807 EmitReloc = Args.hasFlag(Pos: options::OPT_fhip_emit_relocatable,
3808 Neg: options::OPT_fno_hip_emit_relocatable, Default: false);
3809
3810 if (*EmitReloc) {
3811 if (Relocatable) {
3812 C.getDriver().Diag(DiagID: diag::err_opt_not_valid_with_opt)
3813 << "-fhip-emit-relocatable"
3814 << "-fgpu-rdc";
3815 }
3816
3817 if (!CompileDeviceOnly) {
3818 C.getDriver().Diag(DiagID: diag::err_opt_not_valid_without_opt)
3819 << "-fhip-emit-relocatable"
3820 << "--offload-device-only";
3821 }
3822 }
3823 }
3824
3825 if (Args.hasArg(Ids: options::OPT_gpu_bundle_output,
3826 Ids: options::OPT_no_gpu_bundle_output))
3827 BundleOutput = Args.hasFlag(Pos: options::OPT_gpu_bundle_output,
3828 Neg: options::OPT_no_gpu_bundle_output, Default: true) &&
3829 (!EmitReloc || !*EmitReloc);
3830 }
3831
3832 bool canUseBundlerUnbundler() const override { return true; }
3833
3834 ActionBuilderReturnCode
3835 getDeviceDependences(OffloadAction::DeviceDependences &DA,
3836 phases::ID CurPhase, phases::ID FinalPhase,
3837 PhasesTy &Phases) override {
3838 if (!IsActive)
3839 return ABRT_Inactive;
3840
3841 // amdgcn does not support linking of object files, therefore we skip
3842 // backend and assemble phases to output LLVM IR. Except for generating
3843 // non-relocatable device code, where we generate fat binary for device
3844 // code and pass to host in Backend phase.
3845 if (CudaDeviceActions.empty())
3846 return ABRT_Success;
3847
3848 assert(((CurPhase == phases::Link && Relocatable) ||
3849 CudaDeviceActions.size() == GpuArchList.size()) &&
3850 "Expecting one action per GPU architecture.");
3851 assert(!CompileHostOnly &&
3852 "Not expecting HIP actions in host-only compilation.");
3853
3854 bool ShouldLink = !EmitReloc || !*EmitReloc;
3855
3856 if (!Relocatable && CurPhase == phases::Backend && !EmitLLVM &&
3857 !EmitAsm && ShouldLink) {
3858 // If we are in backend phase, we attempt to generate the fat binary.
3859 // We compile each arch to IR and use a link action to generate code
3860 // object containing ISA. Then we use a special "link" action to create
3861 // a fat binary containing all the code objects for different GPU's.
3862 // The fat binary is then an input to the host action.
3863 bool ExplicitOffloadLTO = Args.hasArg(Ids: options::OPT_foffload_lto,
3864 Ids: options::OPT_foffload_lto_EQ);
3865 for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) {
3866 if (ExplicitOffloadLTO &&
3867 ToolChains[I]->isUsingLTO(Args, Kind: AssociatedOffloadKind)) {
3868 // When LTO is enabled, skip the backend and assemble phases and
3869 // use lld to link the bitcode.
3870 ActionList AL;
3871 AL.push_back(Elt: CudaDeviceActions[I]);
3872 // Create a link action to link device IR with device library
3873 // and generate ISA.
3874 CudaDeviceActions[I] =
3875 C.MakeAction<LinkJobAction>(Arg&: AL, Arg: types::TY_Image);
3876 } else {
3877 // When LTO is not enabled, we follow the conventional
3878 // compiler phases, including backend and assemble phases.
3879 ActionList AL;
3880 Action *BackendAction = nullptr;
3881 if (ToolChains[I]->getTriple().isSPIRV() ||
3882 (ToolChains[I]->getTriple().isAMDGCN() &&
3883 GpuArchList[I].ArchName == StringRef("amdgcnspirv"))) {
3884 // Emit LLVM bitcode for SPIR-V targets. SPIR-V device tool chain
3885 // (HIPSPVToolChain or HIPAMDToolChain) runs post-link LLVM IR
3886 // passes.
3887 types::ID Output = Args.hasArg(Ids: options::OPT_S)
3888 ? types::TY_LLVM_IR
3889 : types::TY_LLVM_BC;
3890 BackendAction =
3891 C.MakeAction<BackendJobAction>(Arg&: CudaDeviceActions[I], Arg&: Output);
3892 } else {
3893 BackendAction = C.getDriver().ConstructPhaseAction(
3894 C, Args, Phase: phases::Backend, Input: CudaDeviceActions[I],
3895 TargetDeviceOffloadKind: AssociatedOffloadKind, TargetLTOMode: LTOK_None);
3896 }
3897 auto AssembleAction = C.getDriver().ConstructPhaseAction(
3898 C, Args, Phase: phases::Assemble, Input: BackendAction,
3899 TargetDeviceOffloadKind: AssociatedOffloadKind);
3900 AL.push_back(Elt: AssembleAction);
3901 // Create a link action to link device IR with device library
3902 // and generate ISA.
3903 CudaDeviceActions[I] =
3904 C.MakeAction<LinkJobAction>(Arg&: AL, Arg: types::TY_Image);
3905 }
3906
3907 // OffloadingActionBuilder propagates device arch until an offload
3908 // action. Since the next action for creating fatbin does
3909 // not have device arch, whereas the above link action and its input
3910 // have device arch, an offload action is needed to stop the null
3911 // device arch of the next action being propagated to the above link
3912 // action.
3913 OffloadAction::DeviceDependences DDep;
3914 DDep.add(A&: *CudaDeviceActions[I], TC: *ToolChains[I], BA: GpuArchList[I],
3915 OKind: AssociatedOffloadKind);
3916 CudaDeviceActions[I] = C.MakeAction<OffloadAction>(
3917 Arg&: DDep, Arg: CudaDeviceActions[I]->getType());
3918 }
3919
3920 if (!CompileDeviceOnly || !BundleOutput || *BundleOutput) {
3921 // Create HIP fat binary with a special "link" action.
3922 CudaFatBinary = C.MakeAction<LinkJobAction>(Arg&: CudaDeviceActions,
3923 Arg: types::TY_HIP_FATBIN);
3924
3925 if (!CompileDeviceOnly) {
3926 DA.add(A&: *CudaFatBinary, TC: *FatBinaryToolChain, /*BA=*/{},
3927 OKind: AssociatedOffloadKind);
3928 // Clear the fat binary, it is already a dependence to an host
3929 // action.
3930 CudaFatBinary = nullptr;
3931 }
3932
3933 // Remove the CUDA actions as they are already connected to an host
3934 // action or fat binary.
3935 CudaDeviceActions.clear();
3936 }
3937
3938 return CompileDeviceOnly ? ABRT_Ignore_Host : ABRT_Success;
3939 } else if (CurPhase == phases::Link) {
3940 if (!ShouldLink)
3941 return ABRT_Success;
3942 // Save CudaDeviceActions to DeviceLinkerInputs for each GPU subarch.
3943 // This happens to each device action originated from each input file.
3944 // Later on, device actions in DeviceLinkerInputs are used to create
3945 // device link actions in appendLinkDependences and the created device
3946 // link actions are passed to the offload action as device dependence.
3947 DeviceLinkerInputs.resize(N: CudaDeviceActions.size());
3948 auto LI = DeviceLinkerInputs.begin();
3949 for (auto *A : CudaDeviceActions) {
3950 LI->push_back(Elt: A);
3951 ++LI;
3952 }
3953
3954 // We will pass the device action as a host dependence, so we don't
3955 // need to do anything else with them.
3956 CudaDeviceActions.clear();
3957 return CompileDeviceOnly ? ABRT_Ignore_Host : ABRT_Success;
3958 }
3959
3960 // By default, we produce an action for each device arch.
3961 for (unsigned I = 0, E = CudaDeviceActions.size(); I != E; ++I)
3962 CudaDeviceActions[I] = C.getDriver().ConstructPhaseAction(
3963 C, Args, Phase: CurPhase, Input: CudaDeviceActions[I], TargetDeviceOffloadKind: AssociatedOffloadKind,
3964 TargetLTOMode: ToolChains[I]->getLTOMode(Args, Kind: AssociatedOffloadKind));
3965
3966 if (CompileDeviceOnly && CurPhase == FinalPhase && BundleOutput &&
3967 *BundleOutput) {
3968 for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) {
3969 OffloadAction::DeviceDependences DDep;
3970 DDep.add(A&: *CudaDeviceActions[I], TC: *ToolChains[I], BA: GpuArchList[I],
3971 OKind: AssociatedOffloadKind);
3972 CudaDeviceActions[I] = C.MakeAction<OffloadAction>(
3973 Arg&: DDep, Arg: CudaDeviceActions[I]->getType());
3974 }
3975 CudaFatBinary =
3976 C.MakeAction<OffloadBundlingJobAction>(Arg&: CudaDeviceActions);
3977 CudaDeviceActions.clear();
3978 }
3979
3980 return (CompileDeviceOnly &&
3981 (CurPhase == FinalPhase ||
3982 (!ShouldLink && CurPhase == phases::Assemble)))
3983 ? ABRT_Ignore_Host
3984 : ABRT_Success;
3985 }
3986
3987 void appendLinkDeviceActions(ActionList &AL) override {
3988 if (DeviceLinkerInputs.size() == 0)
3989 return;
3990
3991 assert(DeviceLinkerInputs.size() == GpuArchList.size() &&
3992 "Linker inputs and GPU arch list sizes do not match.");
3993
3994 ActionList Actions;
3995 unsigned I = 0;
3996 // Append a new link action for each device.
3997 // Each entry in DeviceLinkerInputs corresponds to a GPU arch.
3998 for (auto &LI : DeviceLinkerInputs) {
3999
4000 types::ID Output = Args.hasArg(Ids: options::OPT_emit_llvm)
4001 ? types::TY_LLVM_BC
4002 : types::TY_Image;
4003
4004 auto *DeviceLinkAction = C.MakeAction<LinkJobAction>(Arg&: LI, Arg&: Output);
4005 // Linking all inputs for the current GPU arch.
4006 // LI contains all the inputs for the linker.
4007 OffloadAction::DeviceDependences DeviceLinkDeps;
4008 DeviceLinkDeps.add(A&: *DeviceLinkAction, TC: *ToolChains[I], BA: GpuArchList[I],
4009 OKind: AssociatedOffloadKind);
4010 Actions.push_back(Elt: C.MakeAction<OffloadAction>(
4011 Arg&: DeviceLinkDeps, Arg: DeviceLinkAction->getType()));
4012 ++I;
4013 }
4014 DeviceLinkerInputs.clear();
4015
4016 // If emitting LLVM, do not generate final host/device compilation action
4017 if (Args.hasArg(Ids: options::OPT_emit_llvm)) {
4018 AL.append(RHS: Actions);
4019 return;
4020 }
4021
4022 // Create a host object from all the device images by embedding them
4023 // in a fat binary for mixed host-device compilation. For device-only
4024 // compilation, creates a fat binary.
4025 OffloadAction::DeviceDependences DDeps;
4026 if (!CompileDeviceOnly || !BundleOutput || *BundleOutput) {
4027 auto *TopDeviceLinkAction = C.MakeAction<LinkJobAction>(
4028 Arg&: Actions,
4029 Arg: CompileDeviceOnly ? types::TY_HIP_FATBIN : types::TY_Object);
4030 DDeps.add(A&: *TopDeviceLinkAction, TC: *FatBinaryToolChain, /*BA=*/{},
4031 OKind: AssociatedOffloadKind);
4032 // Offload the host object to the host linker.
4033 AL.push_back(
4034 Elt: C.MakeAction<OffloadAction>(Arg&: DDeps, Arg: TopDeviceLinkAction->getType()));
4035 } else {
4036 AL.append(RHS: Actions);
4037 }
4038 }
4039
4040 Action* appendLinkHostActions(ActionList &AL) override { return AL.back(); }
4041
4042 void appendLinkDependences(OffloadAction::DeviceDependences &DA) override {}
4043 };
4044
4045 ///
4046 /// TODO: Add the implementation for other specialized builders here.
4047 ///
4048
4049 /// Specialized builders being used by this offloading action builder.
4050 SmallVector<DeviceActionBuilder *, 4> SpecializedBuilders;
4051
4052 /// Flag set to true if all valid builders allow file bundling/unbundling.
4053 bool CanUseBundler;
4054
4055 /// Flag set to false if an argument turns off bundling.
4056 bool ShouldUseBundler;
4057
4058public:
4059 OffloadingActionBuilder(Compilation &C, DerivedArgList &Args,
4060 const InputList &Inputs)
4061 : C(C) {
4062 // Create a specialized builder for each device toolchain.
4063
4064 IsValid = true;
4065
4066 // Create a specialized builder for CUDA.
4067 SpecializedBuilders.push_back(Elt: new CudaActionBuilder(C, Args, Inputs));
4068
4069 // Create a specialized builder for HIP.
4070 SpecializedBuilders.push_back(Elt: new HIPActionBuilder(C, Args, Inputs));
4071
4072 //
4073 // TODO: Build other specialized builders here.
4074 //
4075
4076 // Initialize all the builders, keeping track of errors. If all valid
4077 // builders agree that we can use bundling, set the flag to true.
4078 unsigned ValidBuilders = 0u;
4079 unsigned ValidBuildersSupportingBundling = 0u;
4080 for (auto *SB : SpecializedBuilders) {
4081 IsValid = IsValid && !SB->initialize();
4082
4083 // Update the counters if the builder is valid.
4084 if (SB->isValid()) {
4085 ++ValidBuilders;
4086 if (SB->canUseBundlerUnbundler())
4087 ++ValidBuildersSupportingBundling;
4088 }
4089 }
4090 CanUseBundler =
4091 ValidBuilders && ValidBuilders == ValidBuildersSupportingBundling;
4092
4093 ShouldUseBundler = Args.hasFlag(Pos: options::OPT_gpu_bundle_output,
4094 Neg: options::OPT_no_gpu_bundle_output, Default: true);
4095 }
4096
4097 ~OffloadingActionBuilder() {
4098 for (auto *SB : SpecializedBuilders)
4099 delete SB;
4100 }
4101
4102 /// Record a host action and its originating input argument.
4103 void recordHostAction(Action *HostAction, const Arg *InputArg) {
4104 assert(HostAction && "Invalid host action");
4105 assert(InputArg && "Invalid input argument");
4106 auto Loc = HostActionToInputArgMap.try_emplace(k: HostAction, args&: InputArg).first;
4107 assert(Loc->second == InputArg &&
4108 "host action mapped to multiple input arguments");
4109 (void)Loc;
4110 }
4111
4112 /// Generate an action that adds device dependences (if any) to a host action.
4113 /// If no device dependence actions exist, just return the host action \a
4114 /// HostAction. If an error is found or if no builder requires the host action
4115 /// to be generated, return nullptr.
4116 Action *
4117 addDeviceDependencesToHostAction(Action *HostAction, const Arg *InputArg,
4118 phases::ID CurPhase, phases::ID FinalPhase,
4119 DeviceActionBuilder::PhasesTy &Phases) {
4120 if (!IsValid)
4121 return nullptr;
4122
4123 if (SpecializedBuilders.empty())
4124 return HostAction;
4125
4126 assert(HostAction && "Invalid host action!");
4127 recordHostAction(HostAction, InputArg);
4128
4129 OffloadAction::DeviceDependences DDeps;
4130 // Check if all the programming models agree we should not emit the host
4131 // action. Also, keep track of the offloading kinds employed.
4132 auto &OffloadKind = InputArgToOffloadKindMap[InputArg];
4133 unsigned InactiveBuilders = 0u;
4134 unsigned IgnoringBuilders = 0u;
4135 for (auto *SB : SpecializedBuilders) {
4136 if (!SB->isValid()) {
4137 ++InactiveBuilders;
4138 continue;
4139 }
4140 auto RetCode =
4141 SB->getDeviceDependences(DA&: DDeps, CurPhase, FinalPhase, Phases);
4142
4143 // If the builder explicitly says the host action should be ignored,
4144 // we need to increment the variable that tracks the builders that request
4145 // the host object to be ignored.
4146 if (RetCode == DeviceActionBuilder::ABRT_Ignore_Host)
4147 ++IgnoringBuilders;
4148
4149 // Unless the builder was inactive for this action, we have to record the
4150 // offload kind because the host will have to use it.
4151 if (RetCode != DeviceActionBuilder::ABRT_Inactive)
4152 OffloadKind |= SB->getAssociatedOffloadKind();
4153 }
4154
4155 // If all builders agree that the host object should be ignored, just return
4156 // nullptr.
4157 if (IgnoringBuilders &&
4158 SpecializedBuilders.size() == (InactiveBuilders + IgnoringBuilders))
4159 return nullptr;
4160
4161 if (DDeps.getActions().empty())
4162 return HostAction;
4163
4164 // We have dependences we need to bundle together. We use an offload action
4165 // for that.
4166 OffloadAction::HostDependence HDep(
4167 *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(),
4168 /*BA=*/{}, DDeps);
4169 return C.MakeAction<OffloadAction>(Arg&: HDep, Arg&: DDeps);
4170 }
4171
4172 /// Generate an action that adds a host dependence to a device action. The
4173 /// results will be kept in this action builder. Return true if an error was
4174 /// found.
4175 bool addHostDependenceToDeviceActions(Action *&HostAction,
4176 const Arg *InputArg) {
4177 if (!IsValid)
4178 return true;
4179
4180 recordHostAction(HostAction, InputArg);
4181
4182 // If we are supporting bundling/unbundling and the current action is an
4183 // input action of non-source file, we replace the host action by the
4184 // unbundling action. The bundler tool has the logic to detect if an input
4185 // is a bundle or not and if the input is not a bundle it assumes it is a
4186 // host file. Therefore it is safe to create an unbundling action even if
4187 // the input is not a bundle.
4188 if (CanUseBundler && isa<InputAction>(Val: HostAction) &&
4189 InputArg->getOption().getKind() == llvm::opt::Option::InputClass &&
4190 (!types::isSrcFile(Id: HostAction->getType()) ||
4191 HostAction->getType() == types::TY_PP_HIP)) {
4192 auto UnbundlingHostAction =
4193 C.MakeAction<OffloadUnbundlingJobAction>(Arg&: HostAction);
4194 UnbundlingHostAction->registerDependentActionInfo(
4195 TC: C.getSingleOffloadToolChain<Action::OFK_Host>(),
4196 /*BA=*/{}, Kind: Action::OFK_Host);
4197 HostAction = UnbundlingHostAction;
4198 recordHostAction(HostAction, InputArg);
4199 }
4200
4201 assert(HostAction && "Invalid host action!");
4202
4203 // Register the offload kinds that are used.
4204 auto &OffloadKind = InputArgToOffloadKindMap[InputArg];
4205 for (auto *SB : SpecializedBuilders) {
4206 if (!SB->isValid())
4207 continue;
4208
4209 auto RetCode = SB->addDeviceDependences(HostAction);
4210
4211 // Host dependences for device actions are not compatible with that same
4212 // action being ignored.
4213 assert(RetCode != DeviceActionBuilder::ABRT_Ignore_Host &&
4214 "Host dependence not expected to be ignored.!");
4215
4216 // Unless the builder was inactive for this action, we have to record the
4217 // offload kind because the host will have to use it.
4218 if (RetCode != DeviceActionBuilder::ABRT_Inactive)
4219 OffloadKind |= SB->getAssociatedOffloadKind();
4220 }
4221
4222 // Do not use unbundler if the Host does not depend on device action.
4223 if (OffloadKind == Action::OFK_None && CanUseBundler)
4224 if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(Val: HostAction))
4225 HostAction = UA->getInputs().back();
4226
4227 return false;
4228 }
4229
4230 /// Add the offloading top level actions to the provided action list. This
4231 /// function can replace the host action by a bundling action if the
4232 /// programming models allow it.
4233 bool appendTopLevelActions(ActionList &AL, Action *HostAction,
4234 const Arg *InputArg) {
4235 if (HostAction)
4236 recordHostAction(HostAction, InputArg);
4237
4238 // Get the device actions to be appended.
4239 ActionList OffloadAL;
4240 for (auto *SB : SpecializedBuilders) {
4241 if (!SB->isValid())
4242 continue;
4243 SB->appendTopLevelActions(AL&: OffloadAL);
4244 }
4245
4246 // If we can and should use the bundler, replace the host action by the
4247 // bundling one in the resulting list. Otherwise, just append the device
4248 // actions. For device only compilation, HostAction is a null pointer,
4249 // therefore only do this when HostAction is not a null pointer.
4250 if (CanUseBundler && ShouldUseBundler && HostAction &&
4251 HostAction->getType() != types::TY_Nothing && !OffloadAL.empty()) {
4252 // Add the host action to the list in order to create the bundling action.
4253 OffloadAL.push_back(Elt: HostAction);
4254
4255 // We expect that the host action was just appended to the action list
4256 // before this method was called.
4257 assert(HostAction == AL.back() && "Host action not in the list??");
4258 HostAction = C.MakeAction<OffloadBundlingJobAction>(Arg&: OffloadAL);
4259 recordHostAction(HostAction, InputArg);
4260 AL.back() = HostAction;
4261 } else
4262 AL.append(in_start: OffloadAL.begin(), in_end: OffloadAL.end());
4263
4264 // Propagate to the current host action (if any) the offload information
4265 // associated with the current input.
4266 if (HostAction)
4267 HostAction->propagateHostOffloadInfo(OKinds: InputArgToOffloadKindMap[InputArg],
4268 /*BA=*/OArch: {});
4269 return false;
4270 }
4271
4272 void appendDeviceLinkActions(ActionList &AL) {
4273 for (DeviceActionBuilder *SB : SpecializedBuilders) {
4274 if (!SB->isValid())
4275 continue;
4276 SB->appendLinkDeviceActions(AL);
4277 }
4278 }
4279
4280 Action *makeHostLinkAction() {
4281 // Build a list of device linking actions.
4282 ActionList DeviceAL;
4283 appendDeviceLinkActions(AL&: DeviceAL);
4284 if (DeviceAL.empty())
4285 return nullptr;
4286
4287 // Let builders add host linking actions.
4288 Action* HA = nullptr;
4289 for (DeviceActionBuilder *SB : SpecializedBuilders) {
4290 if (!SB->isValid())
4291 continue;
4292 HA = SB->appendLinkHostActions(AL&: DeviceAL);
4293 // This created host action has no originating input argument, therefore
4294 // needs to set its offloading kind directly.
4295 if (HA)
4296 HA->propagateHostOffloadInfo(OKinds: SB->getAssociatedOffloadKind(),
4297 /*BA=*/OArch: {});
4298 }
4299 return HA;
4300 }
4301
4302 /// Processes the host linker action. This currently consists of replacing it
4303 /// with an offload action if there are device link objects and propagate to
4304 /// the host action all the offload kinds used in the current compilation. The
4305 /// resulting action is returned.
4306 Action *processHostLinkAction(Action *HostAction) {
4307 // Add all the dependences from the device linking actions.
4308 OffloadAction::DeviceDependences DDeps;
4309 for (auto *SB : SpecializedBuilders) {
4310 if (!SB->isValid())
4311 continue;
4312
4313 SB->appendLinkDependences(DA&: DDeps);
4314 }
4315
4316 // Calculate all the offload kinds used in the current compilation.
4317 unsigned ActiveOffloadKinds = 0u;
4318 for (auto &I : InputArgToOffloadKindMap)
4319 ActiveOffloadKinds |= I.second;
4320
4321 // If we don't have device dependencies, we don't have to create an offload
4322 // action.
4323 if (DDeps.getActions().empty()) {
4324 // Set all the active offloading kinds to the link action. Given that it
4325 // is a link action it is assumed to depend on all actions generated so
4326 // far.
4327 HostAction->setHostOffloadInfo(OKinds: ActiveOffloadKinds,
4328 /*BA=*/OArch: {});
4329 // Propagate active offloading kinds for each input to the link action.
4330 // Each input may have different active offloading kind.
4331 for (auto *A : HostAction->inputs()) {
4332 auto ArgLoc = HostActionToInputArgMap.find(x: A);
4333 if (ArgLoc == HostActionToInputArgMap.end())
4334 continue;
4335 auto OFKLoc = InputArgToOffloadKindMap.find(x: ArgLoc->second);
4336 if (OFKLoc == InputArgToOffloadKindMap.end())
4337 continue;
4338 A->propagateHostOffloadInfo(OKinds: OFKLoc->second, /*BA=*/OArch: {});
4339 }
4340 return HostAction;
4341 }
4342
4343 // Create the offload action with all dependences. When an offload action
4344 // is created the kinds are propagated to the host action, so we don't have
4345 // to do that explicitly here.
4346 OffloadAction::HostDependence HDep(
4347 *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(),
4348 /*BA=*/{}, ActiveOffloadKinds);
4349 return C.MakeAction<OffloadAction>(Arg&: HDep, Arg&: DDeps);
4350 }
4351};
4352} // anonymous namespace.
4353
4354void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
4355 const InputList &Inputs,
4356 ActionList &Actions) const {
4357
4358 // Diagnose misuse of /Fo.
4359 if (Arg *A = Args.getLastArg(Ids: options::OPT__SLASH_Fo)) {
4360 StringRef V = A->getValue();
4361 if (Inputs.size() > 1 && !V.empty() &&
4362 !llvm::sys::path::is_separator(value: V.back())) {
4363 // Check whether /Fo tries to name an output file for multiple inputs.
4364 Diag(DiagID: clang::diag::err_drv_out_file_argument_with_multiple_sources)
4365 << A->getSpelling() << V;
4366 Args.eraseArg(Id: options::OPT__SLASH_Fo);
4367 }
4368 }
4369
4370 // Diagnose misuse of /Fa.
4371 if (Arg *A = Args.getLastArg(Ids: options::OPT__SLASH_Fa)) {
4372 StringRef V = A->getValue();
4373 if (Inputs.size() > 1 && !V.empty() &&
4374 !llvm::sys::path::is_separator(value: V.back())) {
4375 // Check whether /Fa tries to name an asm file for multiple inputs.
4376 Diag(DiagID: clang::diag::err_drv_out_file_argument_with_multiple_sources)
4377 << A->getSpelling() << V;
4378 Args.eraseArg(Id: options::OPT__SLASH_Fa);
4379 }
4380 }
4381
4382 // Diagnose misuse of /o.
4383 if (Arg *A = Args.getLastArg(Ids: options::OPT__SLASH_o)) {
4384 if (A->getValue()[0] == '\0') {
4385 // It has to have a value.
4386 Diag(DiagID: clang::diag::err_drv_missing_argument) << A->getSpelling() << 1;
4387 Args.eraseArg(Id: options::OPT__SLASH_o);
4388 }
4389 }
4390
4391 // Ignore /Yc/Yu if both /Yc and /Yu passed but with different filenames.
4392 Arg *YcArg = Args.getLastArg(Ids: options::OPT__SLASH_Yc);
4393 Arg *YuArg = Args.getLastArg(Ids: options::OPT__SLASH_Yu);
4394 if (YcArg && YuArg && strcmp(s1: YcArg->getValue(), s2: YuArg->getValue()) != 0) {
4395 Diag(DiagID: clang::diag::warn_drv_ycyu_different_arg_clang_cl);
4396 Args.eraseArg(Id: options::OPT__SLASH_Yc);
4397 Args.eraseArg(Id: options::OPT__SLASH_Yu);
4398 YcArg = YuArg = nullptr;
4399 }
4400 if (YcArg && Inputs.size() > 1) {
4401 Diag(DiagID: clang::diag::warn_drv_yc_multiple_inputs_clang_cl);
4402 Args.eraseArg(Id: options::OPT__SLASH_Yc);
4403 YcArg = nullptr;
4404 }
4405
4406 Arg *FinalPhaseArg;
4407 phases::ID FinalPhase = getFinalPhase(DAL: Args, FinalPhaseArg: &FinalPhaseArg);
4408
4409 if (FinalPhase == phases::Link) {
4410 if (Args.hasArgNoClaim(Ids: options::OPT_hipstdpar)) {
4411 Args.AddFlagArg(BaseArg: nullptr, Opt: getOpts().getOption(Opt: options::OPT_hip_link));
4412 Args.AddFlagArg(BaseArg: nullptr,
4413 Opt: getOpts().getOption(Opt: options::OPT_frtlib_add_rpath));
4414 }
4415 // Emitting LLVM while linking disabled except in the HIPAMD or SPIR-V
4416 // Toolchains
4417 if (Args.hasArg(Ids: options::OPT_emit_llvm) &&
4418 !Args.hasArg(Ids: options::OPT_hip_link) &&
4419 !C.getDefaultToolChain().getTriple().isSPIRV())
4420 Diag(DiagID: clang::diag::err_drv_emit_llvm_link);
4421 if (C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment() &&
4422 C.getDefaultToolChain().isUsingLTO(Args) &&
4423 !Args.getLastArgValue(Id: options::OPT_fuse_ld_EQ)
4424 .starts_with_insensitive(Prefix: "lld"))
4425 Diag(DiagID: clang::diag::err_drv_lto_without_lld);
4426
4427 // If -dumpdir is not specified, give a default prefix derived from the link
4428 // output filename. For example, `clang -g -gsplit-dwarf a.c -o x` passes
4429 // `-dumpdir x-` to cc1. If -o is unspecified, use
4430 // stem(getDefaultImageName()) (usually stem("a.out") = "a").
4431 if (!Args.hasArg(Ids: options::OPT_dumpdir)) {
4432 Arg *FinalOutput = Args.getLastArg(Ids: options::OPT_o, Ids: options::OPT__SLASH_o);
4433 Arg *Arg = Args.MakeSeparateArg(
4434 BaseArg: nullptr, Opt: getOpts().getOption(Opt: options::OPT_dumpdir),
4435 Value: Args.MakeArgString(
4436 Str: (FinalOutput ? FinalOutput->getValue()
4437 : llvm::sys::path::stem(path: getDefaultImageName())) +
4438 "-"));
4439 Arg->claim();
4440 Args.append(A: Arg);
4441 }
4442 }
4443
4444 if (FinalPhase == phases::Preprocess || Args.hasArg(Ids: options::OPT__SLASH_Y_)) {
4445 // If only preprocessing or /Y- is used, all pch handling is disabled.
4446 // Rather than check for it everywhere, just remove clang-cl pch-related
4447 // flags here.
4448 Args.eraseArg(Id: options::OPT__SLASH_Fp);
4449 Args.eraseArg(Id: options::OPT__SLASH_Yc);
4450 Args.eraseArg(Id: options::OPT__SLASH_Yu);
4451 YcArg = YuArg = nullptr;
4452 }
4453
4454 if (Args.hasArg(Ids: options::OPT_include_pch) &&
4455 Args.hasArg(Ids: options::OPT_ignore_pch)) {
4456 // If -ignore-pch is used, -include-pch is disabled. Since -emit-pch is
4457 // CC1option, it will not be added to command argments if -ignore-pch is
4458 // used.
4459 Args.eraseArg(Id: options::OPT_include_pch);
4460 }
4461
4462 bool LinkOnly = phases::Link == FinalPhase && Inputs.size() > 0;
4463 for (auto &I : Inputs) {
4464 types::ID InputType = I.first;
4465 const Arg *InputArg = I.second;
4466
4467 auto PL = types::getCompilationPhases(Id: InputType);
4468
4469 phases::ID InitialPhase = PL[0];
4470 LinkOnly = LinkOnly && phases::Link == InitialPhase && PL.size() == 1;
4471
4472 // If the first step comes after the final phase we are doing as part of
4473 // this compilation, warn the user about it.
4474 if (InitialPhase > FinalPhase) {
4475 if (InputArg->isClaimed())
4476 continue;
4477
4478 // Claim here to avoid the more general unused warning.
4479 InputArg->claim();
4480
4481 // Suppress all unused style warnings with -Qunused-arguments
4482 if (Args.hasArg(Ids: options::OPT_Qunused_arguments))
4483 continue;
4484
4485 // Special case when final phase determined by binary name, rather than
4486 // by a command-line argument with a corresponding Arg.
4487 if (CCCIsCPP())
4488 Diag(DiagID: clang::diag::warn_drv_input_file_unused_by_cpp)
4489 << InputArg->getAsString(Args) << getPhaseName(Id: InitialPhase);
4490 // Special case '-E' warning on a previously preprocessed file to make
4491 // more sense.
4492 else if (InitialPhase == phases::Compile &&
4493 (Args.getLastArg(Ids: options::OPT__SLASH_EP,
4494 Ids: options::OPT__SLASH_P) ||
4495 Args.getLastArg(Ids: options::OPT_E) ||
4496 Args.getLastArg(Ids: options::OPT_M, Ids: options::OPT_MM)) &&
4497 getPreprocessedType(Id: InputType) == types::TY_INVALID)
4498 Diag(DiagID: clang::diag::warn_drv_preprocessed_input_file_unused)
4499 << InputArg->getAsString(Args) << !!FinalPhaseArg
4500 << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : "");
4501 else
4502 Diag(DiagID: clang::diag::warn_drv_input_file_unused)
4503 << InputArg->getAsString(Args) << getPhaseName(Id: InitialPhase)
4504 << !!FinalPhaseArg
4505 << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : "");
4506 continue;
4507 }
4508
4509 if (YcArg) {
4510 // Add a separate precompile phase for the compile phase.
4511 if (FinalPhase >= phases::Compile) {
4512 const types::ID HeaderType = lookupHeaderTypeForSourceType(Id: InputType);
4513 // Build the pipeline for the pch file.
4514 Action *ClangClPch = C.MakeAction<InputAction>(Arg: *InputArg, Arg: HeaderType);
4515 auto HostLTO = C.getDefaultToolChain().getLTOMode(Args);
4516 for (phases::ID Phase : types::getCompilationPhases(Id: HeaderType))
4517 ClangClPch = ConstructPhaseAction(C, Args, Phase, Input: ClangClPch,
4518 TargetDeviceOffloadKind: Action::OFK_None, TargetLTOMode: HostLTO);
4519 assert(ClangClPch);
4520 Actions.push_back(Elt: ClangClPch);
4521 // The driver currently exits after the first failed command. This
4522 // relies on that behavior, to make sure if the pch generation fails,
4523 // the main compilation won't run.
4524 // FIXME: If the main compilation fails, the PCH generation should
4525 // probably not be considered successful either.
4526 }
4527 }
4528 }
4529
4530 // Claim any options which are obviously only used for compilation.
4531 if (LinkOnly) {
4532 Args.ClaimAllArgs(Id0: options::OPT_CompileOnly_Group);
4533 Args.ClaimAllArgs(Id0: options::OPT_cl_compile_Group);
4534 }
4535}
4536
4537/// HIP non-RDC \c -S for AMDGCN: emit host and device assembly separately and
4538/// bundle with \c clang-offload-bundler (new offload driver), instead of
4539/// \c llvm-offload-binary / \c clang-linker-wrapper fatbin embedding.
4540static bool
4541shouldBundleHIPAsmWithNewDriver(const Compilation &C,
4542 const llvm::opt::DerivedArgList &Args,
4543 const Driver &D) {
4544 if (!C.isOffloadingHostKind(Kind: Action::OFK_HIP) ||
4545 !Args.hasArg(Ids: options::OPT_S) || Args.hasArg(Ids: options::OPT_emit_llvm) ||
4546 D.offloadDeviceOnly() ||
4547 Args.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc, Default: false))
4548 return false;
4549
4550 bool HasAMDGCNHIPDevice = false;
4551 auto HIPTCs = C.getOffloadToolChains(Kind: Action::OFK_HIP);
4552 for (auto It = HIPTCs.first; It != HIPTCs.second; ++It) {
4553 const ToolChain *TC = It->second;
4554 const llvm::Triple &Tr = TC->getTriple();
4555 if (!Tr.isAMDGPU())
4556 return false;
4557 HasAMDGCNHIPDevice = true;
4558 }
4559 return HasAMDGCNHIPDevice;
4560}
4561
4562void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
4563 const InputList &Inputs, ActionList &Actions) const {
4564 llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
4565
4566 if (!SuppressMissingInputWarning && Inputs.empty()) {
4567 Diag(DiagID: clang::diag::err_drv_no_input_files);
4568 return;
4569 }
4570
4571 handleArguments(C, Args, Inputs, Actions);
4572
4573 bool UseNewOffloadingDriver = Args.hasFlag(
4574 Pos: options::OPT_offload_new_driver, Neg: options::OPT_no_offload_new_driver,
4575 Default: C.getActiveOffloadKinds() != Action::OFK_None);
4576 bool HIPRDCDeviceOnlyFatBin =
4577 UseNewOffloadingDriver && C.isOffloadingHostKind(Kind: Action::OFK_HIP) &&
4578 offloadDeviceOnly() && Args.hasArg(Ids: options::OPT_hip_link) &&
4579 Args.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc, Default: false) &&
4580 getFinalPhase(DAL: Args) == phases::Link &&
4581 !Args.hasArg(Ids: options::OPT_emit_llvm) &&
4582 Args.hasFlag(Pos: options::OPT_gpu_bundle_output,
4583 Neg: options::OPT_no_gpu_bundle_output, Default: true);
4584
4585 // Builder to be used to build offloading actions.
4586 std::unique_ptr<OffloadingActionBuilder> OffloadBuilder =
4587 !UseNewOffloadingDriver
4588 ? std::make_unique<OffloadingActionBuilder>(args&: C, args&: Args, args: Inputs)
4589 : nullptr;
4590
4591 // Construct the actions to perform.
4592 ExtractAPIJobAction *ExtractAPIAction = nullptr;
4593 ActionList LinkerInputs;
4594 ActionList MergerInputs;
4595
4596 for (auto &I : Inputs) {
4597 types::ID InputType = I.first;
4598 const Arg *InputArg = I.second;
4599
4600 auto PL = types::getCompilationPhases(Driver: *this, DAL&: Args, Id: InputType);
4601 if (PL.empty())
4602 continue;
4603
4604 auto FullPL = types::getCompilationPhases(Id: InputType);
4605
4606 // Build the pipeline for this file.
4607 Action *Current = C.MakeAction<InputAction>(Arg: *InputArg, Arg&: InputType);
4608
4609 // Device-only HIP links consume packaged offload bitcode directly.
4610 if (HIPRDCDeviceOnlyFatBin && InputType == types::TY_LLVM_BC) {
4611 LinkerInputs.push_back(Elt: Current);
4612 continue;
4613 }
4614
4615 std::string CUID;
4616 if (CUIDOpts.isEnabled() && types::isSrcFile(Id: InputType)) {
4617 CUID = CUIDOpts.getCUID(InputFile: InputArg->getValue(), Args);
4618 cast<InputAction>(Val: Current)->setId(CUID);
4619 }
4620
4621 ActionList HIPAsmDeviceActions;
4622
4623 // Use the current host action in any of the offloading actions, if
4624 // required.
4625 if (!UseNewOffloadingDriver)
4626 if (OffloadBuilder->addHostDependenceToDeviceActions(HostAction&: Current, InputArg))
4627 break;
4628
4629 for (phases::ID Phase : PL) {
4630
4631 // Add any offload action the host action depends on.
4632 if (!UseNewOffloadingDriver)
4633 Current = OffloadBuilder->addDeviceDependencesToHostAction(
4634 HostAction: Current, InputArg, CurPhase: Phase, FinalPhase: PL.back(), Phases: FullPL);
4635 if (!Current)
4636 break;
4637
4638 // Queue linker inputs.
4639 if (Phase == phases::Link) {
4640 assert(Phase == PL.back() && "linking must be final compilation step.");
4641 // We don't need to generate additional link commands if emitting AMD
4642 // bitcode or compiling only for the offload device
4643 if (!(C.getInputArgs().hasArg(Ids: options::OPT_hip_link) &&
4644 (C.getInputArgs().hasArg(Ids: options::OPT_emit_llvm))) &&
4645 !offloadDeviceOnly())
4646 LinkerInputs.push_back(Elt: Current);
4647 Current = nullptr;
4648 break;
4649 }
4650
4651 // TODO: Consider removing this because the merged may not end up being
4652 // the final Phase in the pipeline. Perhaps the merged could just merge
4653 // and then pass an artifact of some sort to the Link Phase.
4654 // Queue merger inputs.
4655 if (Phase == phases::IfsMerge) {
4656 assert(Phase == PL.back() && "merging must be final compilation step.");
4657 MergerInputs.push_back(Elt: Current);
4658 Current = nullptr;
4659 break;
4660 }
4661
4662 if (Phase == phases::Precompile && ExtractAPIAction) {
4663 ExtractAPIAction->addHeaderInput(Input: Current);
4664 Current = nullptr;
4665 break;
4666 }
4667
4668 // FIXME: Should we include any prior module file outputs as inputs of
4669 // later actions in the same command line?
4670
4671 // Otherwise construct the appropriate action.
4672 Action *NewCurrent =
4673 ConstructPhaseAction(C, Args, Phase, Input: Current, TargetDeviceOffloadKind: Action::OFK_None,
4674 TargetLTOMode: C.getDefaultToolChain().getLTOMode(Args));
4675
4676 // We didn't create a new action, so we will just move to the next phase.
4677 if (NewCurrent == Current)
4678 continue;
4679
4680 if (auto *EAA = dyn_cast<ExtractAPIJobAction>(Val: NewCurrent))
4681 ExtractAPIAction = EAA;
4682
4683 Current = NewCurrent;
4684
4685 // Try to build the offloading actions and add the result as a dependency
4686 // to the host.
4687 if (UseNewOffloadingDriver)
4688 Current = BuildOffloadingActions(C, Args, Input: I, CUID, HostAction: Current,
4689 HIPAsmBundleDeviceOut: &HIPAsmDeviceActions);
4690 // Use the current host action in any of the offloading actions, if
4691 // required.
4692 else if (OffloadBuilder->addHostDependenceToDeviceActions(HostAction&: Current,
4693 InputArg))
4694 break;
4695
4696 if (Current->getType() == types::TY_Nothing)
4697 break;
4698 }
4699
4700 // HIP non-RDC -S (AMDGCN): bundle host and device assembly like the
4701 // classic driver instead of embedding a fat binary in host asm.
4702 if (Current && !HIPAsmDeviceActions.empty()) {
4703 assert(UseNewOffloadingDriver && "unexpected HIP asm bundle list");
4704 ActionList BundleInputs;
4705 BundleInputs.append(RHS: HIPAsmDeviceActions);
4706 BundleInputs.push_back(Elt: Current);
4707 Current = C.MakeAction<OffloadBundlingJobAction>(Arg&: BundleInputs);
4708 }
4709
4710 // If we ended with something, add to the output list.
4711 if (Current)
4712 Actions.push_back(Elt: Current);
4713
4714 // Add any top level actions generated for offloading.
4715 if (!UseNewOffloadingDriver)
4716 OffloadBuilder->appendTopLevelActions(AL&: Actions, HostAction: Current, InputArg);
4717 else if (Current)
4718 Current->propagateHostOffloadInfo(OKinds: C.getActiveOffloadKinds(),
4719 /*BA=*/OArch: {});
4720 }
4721
4722 // Add a link action if necessary.
4723
4724 if (LinkerInputs.empty()) {
4725 Arg *FinalPhaseArg;
4726 if (getFinalPhase(DAL: Args, FinalPhaseArg: &FinalPhaseArg) == phases::Link)
4727 if (!UseNewOffloadingDriver)
4728 OffloadBuilder->appendDeviceLinkActions(AL&: Actions);
4729 }
4730
4731 if (!LinkerInputs.empty()) {
4732 if (!UseNewOffloadingDriver)
4733 if (Action *Wrapper = OffloadBuilder->makeHostLinkAction())
4734 LinkerInputs.push_back(Elt: Wrapper);
4735 Action *LA;
4736 // Check if this Linker Job should emit a static library.
4737 if (ShouldEmitStaticLibrary(Args)) {
4738 LA = C.MakeAction<StaticLibJobAction>(Arg&: LinkerInputs, Arg: types::TY_Image);
4739 } else if (UseNewOffloadingDriver ||
4740 Args.hasArg(Ids: options::OPT_offload_link)) {
4741 LA = C.MakeAction<LinkerWrapperJobAction>(
4742 Arg&: LinkerInputs,
4743 Arg: HIPRDCDeviceOnlyFatBin ? types::TY_HIP_FATBIN : types::TY_Image);
4744 LA->propagateHostOffloadInfo(OKinds: C.getActiveOffloadKinds(),
4745 /*BA=*/OArch: {});
4746 } else {
4747 // If we are linking but were passed -emit-llvm, we will be calling
4748 // llvm-link, so set the output type accordingly. This is only allowed in
4749 // rare cases, so make sure we aren't going to error about it.
4750 bool LinkingIR = Args.hasArg(Ids: options::OPT_emit_llvm) &&
4751 C.getDefaultToolChain().getTriple().isSPIRV();
4752 types::ID LT = LinkingIR && !Diags.hasErrorOccurred() ? types::TY_LLVM_BC
4753 : types::TY_Image;
4754 LA = C.MakeAction<LinkJobAction>(Arg&: LinkerInputs, Arg&: LT);
4755 }
4756 if (!UseNewOffloadingDriver)
4757 LA = OffloadBuilder->processHostLinkAction(HostAction: LA);
4758 Actions.push_back(Elt: LA);
4759 }
4760
4761 // Add an interface stubs merge action if necessary.
4762 if (!MergerInputs.empty())
4763 Actions.push_back(
4764 Elt: C.MakeAction<IfsMergeJobAction>(Arg&: MergerInputs, Arg: types::TY_Image));
4765
4766 if (Args.hasArg(Ids: options::OPT_emit_interface_stubs)) {
4767 auto PhaseList = types::getCompilationPhases(
4768 Id: types::TY_IFS_CPP,
4769 LastPhase: Args.hasArg(Ids: options::OPT_c) ? phases::Compile : phases::IfsMerge);
4770
4771 ActionList MergerInputs;
4772
4773 for (auto &I : Inputs) {
4774 types::ID InputType = I.first;
4775 const Arg *InputArg = I.second;
4776
4777 // Currently clang and the llvm assembler do not support generating symbol
4778 // stubs from assembly, so we skip the input on asm files. For ifs files
4779 // we rely on the normal pipeline setup in the pipeline setup code above.
4780 if (InputType == types::TY_IFS || InputType == types::TY_PP_Asm ||
4781 InputType == types::TY_Asm)
4782 continue;
4783
4784 Action *Current = C.MakeAction<InputAction>(Arg: *InputArg, Arg&: InputType);
4785
4786 for (auto Phase : PhaseList) {
4787 switch (Phase) {
4788 default:
4789 llvm_unreachable(
4790 "IFS Pipeline can only consist of Compile followed by IfsMerge.");
4791 case phases::Compile: {
4792 // Only IfsMerge (llvm-ifs) can handle .o files by looking for ifs
4793 // files where the .o file is located. The compile action can not
4794 // handle this.
4795 if (InputType == types::TY_Object)
4796 break;
4797
4798 Current = C.MakeAction<CompileJobAction>(Arg&: Current, Arg: types::TY_IFS_CPP);
4799 break;
4800 }
4801 case phases::IfsMerge: {
4802 assert(Phase == PhaseList.back() &&
4803 "merging must be final compilation step.");
4804 MergerInputs.push_back(Elt: Current);
4805 Current = nullptr;
4806 break;
4807 }
4808 }
4809 }
4810
4811 // If we ended with something, add to the output list.
4812 if (Current)
4813 Actions.push_back(Elt: Current);
4814 }
4815
4816 // Add an interface stubs merge action if necessary.
4817 if (!MergerInputs.empty())
4818 Actions.push_back(
4819 Elt: C.MakeAction<IfsMergeJobAction>(Arg&: MergerInputs, Arg: types::TY_Image));
4820 }
4821
4822 for (auto Opt : {options::OPT_print_supported_cpus,
4823 options::OPT_print_supported_extensions,
4824 options::OPT_print_enabled_extensions}) {
4825 // If --print-supported-cpus, -mcpu=? or -mtune=? is specified, build a
4826 // custom Compile phase that prints out supported cpu models and quits.
4827 //
4828 // If either --print-supported-extensions or --print-enabled-extensions is
4829 // specified, call the corresponding helper function that prints out the
4830 // supported/enabled extensions and quits.
4831 if (Arg *A = Args.getLastArg(Ids: Opt)) {
4832 if (Opt == options::OPT_print_supported_extensions &&
4833 !C.getDefaultToolChain().getTriple().isRISCV() &&
4834 !C.getDefaultToolChain().getTriple().isAArch64() &&
4835 !C.getDefaultToolChain().getTriple().isARM()) {
4836 C.getDriver().Diag(DiagID: diag::err_opt_not_valid_on_target)
4837 << "--print-supported-extensions";
4838 return;
4839 }
4840 if (Opt == options::OPT_print_enabled_extensions &&
4841 !C.getDefaultToolChain().getTriple().isRISCV() &&
4842 !C.getDefaultToolChain().getTriple().isAArch64()) {
4843 C.getDriver().Diag(DiagID: diag::err_opt_not_valid_on_target)
4844 << "--print-enabled-extensions";
4845 return;
4846 }
4847
4848 // Use the -mcpu=? flag as the dummy input to cc1.
4849 Actions.clear();
4850 Action *InputAc = C.MakeAction<InputAction>(
4851 Arg&: *A, Arg: IsFlangMode() ? types::TY_Fortran : types::TY_C);
4852 Actions.push_back(
4853 Elt: C.MakeAction<PrecompileJobAction>(Arg&: InputAc, Arg: types::TY_Nothing));
4854 for (auto &I : Inputs)
4855 I.second->claim();
4856 }
4857 }
4858
4859 llvm::Triple TargetTriple(C.getDriver().getTargetTriple());
4860 if (TargetTriple.getOS() == llvm::Triple::Vulkan ||
4861 TargetTriple.getOS() == llvm::Triple::ShaderModel) {
4862 const auto &TC =
4863 static_cast<const toolchains::HLSLToolChain &>(C.getDefaultToolChain());
4864
4865 // Call objcopy for manipulation of the unvalidated DXContainer when an
4866 // option in Args requires it.
4867 if (TC.requiresObjcopy(Args)) {
4868 Action *LastAction = Actions.back();
4869 // llvm-objcopy expects an unvalidated DXIL container (TY_OBJECT).
4870 if (LastAction->getType() == types::TY_Object) {
4871 ActionList ObjcopyActions({LastAction});
4872 Actions.push_back(
4873 Elt: C.MakeAction<ObjcopyJobAction>(Arg&: ObjcopyActions, Arg: types::TY_Object));
4874 }
4875 }
4876
4877 // Call validator when -Vd not in Args.
4878 auto ValInfo = TC.getValidationInfo(Args);
4879 if (ValInfo.NeedsValidation) {
4880 Action *LastAction = Actions.back();
4881 if (LastAction->getType() == types::TY_Object) {
4882 types::ID OutType =
4883 ValInfo.ProducesOutput ? types::TY_DX_CONTAINER : types::TY_Object;
4884 Actions.push_back(
4885 Elt: C.MakeAction<BinaryAnalyzeJobAction>(Arg&: LastAction, Arg&: OutType));
4886 }
4887 }
4888
4889 // Call metal-shaderconverter when targeting metal.
4890 if (TC.requiresBinaryTranslation(Args)) {
4891 Action *LastAction = Actions.back();
4892 // Metal shader converter runs on DXIL containers, which can either be
4893 // validated (in which case they are TY_DX_CONTAINER), or unvalidated
4894 // (TY_OBJECT).
4895 if (LastAction->getType() == types::TY_DX_CONTAINER ||
4896 LastAction->getType() == types::TY_Object)
4897 Actions.push_back(Elt: C.MakeAction<BinaryTranslatorJobAction>(
4898 Arg&: LastAction, Arg: types::TY_DX_CONTAINER));
4899 }
4900 }
4901
4902 // Claim ignored clang-cl options.
4903 Args.ClaimAllArgs(Id0: options::OPT_cl_ignored_Group);
4904}
4905
4906/// Returns the canonical name for the offloading architecture when using a HIP
4907/// or CUDA architecture.
4908static StringRef getCanonicalArchString(Compilation &C,
4909 const llvm::opt::DerivedArgList &Args,
4910 StringRef ArchStr,
4911 const llvm::Triple &Triple) {
4912 // Lookup the CUDA / HIP architecture string. Only report an error if we were
4913 // expecting the triple to be only NVPTX / AMDGPU.
4914 OffloadArch Arch =
4915 StringToOffloadArch(S: getProcessorFromTargetID(T: Triple, OffloadArch: ArchStr));
4916 if (Triple.isNVPTX() && (Arch.isUnknown() || !Arch.isNVPTX())) {
4917 C.getDriver().Diag(DiagID: clang::diag::err_drv_offload_bad_gpu_arch)
4918 << "CUDA" << ArchStr;
4919 return StringRef();
4920 } else if (Triple.isAMDGPU()) {
4921 if (Arch.isUnknown() || (!Arch.isAMDGPU() && !Arch.isAMDGCNSPIRV())) {
4922 C.getDriver().Diag(DiagID: clang::diag::err_drv_offload_bad_gpu_arch)
4923 << "HIP" << ArchStr;
4924 return StringRef();
4925 }
4926
4927 if (Triple.getSubArch() != llvm::Triple::NoSubArch) {
4928 llvm::Triple::SubArchType ArchSubArch = getOffloadArchSubArch(ID: Arch);
4929 if (ArchSubArch != Triple.getSubArch() &&
4930 llvm::AMDGPU::getMajorSubArch(SubArch: ArchSubArch) != Triple.getSubArch()) {
4931 C.getDriver().Diag(DiagID: clang::diag::err_target_unsupported_arch)
4932 << ArchStr << Triple.getArchName();
4933 return StringRef();
4934 }
4935 }
4936 }
4937
4938 if (Arch.isNVPTX())
4939 return Args.MakeArgStringRef(Str: OffloadArchToString(A: Arch));
4940
4941 if (Arch.isAMDGPU() || Arch.isAMDGCNSPIRV()) {
4942 llvm::StringMap<bool> Features;
4943 std::optional<StringRef> Arch = parseTargetID(T: Triple, OffloadArch: ArchStr, FeatureMap: &Features);
4944 if (!Arch) {
4945 C.getDriver().Diag(DiagID: clang::diag::err_drv_bad_target_id) << ArchStr;
4946 return StringRef();
4947 }
4948 return Args.MakeArgStringRef(Str: getCanonicalTargetID(Processor: *Arch, Features));
4949 }
4950
4951 // If the input isn't CUDA or HIP just return the architecture.
4952 return ArchStr;
4953}
4954
4955/// Checks if the set offloading architectures does not conflict. Returns the
4956/// incompatible pair if a conflict occurs.
4957static std::optional<std::pair<llvm::StringRef, llvm::StringRef>>
4958getConflictOffloadArchCombination(const llvm::DenseSet<StringRef> &Archs,
4959 llvm::Triple Triple) {
4960 if (!Triple.isAMDGPU())
4961 return std::nullopt;
4962
4963 std::set<StringRef> ArchSet;
4964 llvm::copy(Range: Archs, Out: std::inserter(x&: ArchSet, i: ArchSet.begin()));
4965 return getConflictTargetIDCombination(TargetIDs: ArchSet);
4966}
4967
4968llvm::SmallVector<BoundArch>
4969Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args,
4970 Action::OffloadKind Kind, const ToolChain &TC) const {
4971 // --offload and --offload-arch options are mutually exclusive.
4972 if (Args.hasArgNoClaim(Ids: options::OPT_offload_EQ) &&
4973 Args.hasArgNoClaim(Ids: options::OPT_offload_arch_EQ,
4974 Ids: options::OPT_no_offload_arch_EQ)) {
4975 C.getDriver().Diag(DiagID: diag::err_opt_not_valid_with_opt)
4976 << "--offload"
4977 << (Args.hasArgNoClaim(Ids: options::OPT_offload_arch_EQ)
4978 ? "--offload-arch"
4979 : "--no-offload-arch");
4980 }
4981
4982 llvm::DenseSet<StringRef> Archs;
4983 for (auto *Arg : C.getArgsForToolChain(TC: &TC, /*BA=*/{}, DeviceOffloadKind: Kind)) {
4984 // Add or remove the seen architectures in order of appearance. If an
4985 // invalid architecture is given we simply exit.
4986 if (Arg->getOption().matches(ID: options::OPT_offload_arch_EQ)) {
4987 for (StringRef Arch : Arg->getValues()) {
4988 if (Arch == "native" || Arch.empty()) {
4989 auto GPUsOrErr = TC.getSystemGPUArchs(Args);
4990 if (!GPUsOrErr) {
4991 TC.getDriver().Diag(DiagID: diag::err_drv_undetermined_gpu_arch)
4992 << TC.getTriple().getArchName()
4993 << llvm::toString(E: GPUsOrErr.takeError()) << "--offload-arch";
4994 continue;
4995 }
4996
4997 for (auto ArchStr : *GPUsOrErr) {
4998 StringRef CanonicalStr = getCanonicalArchString(
4999 C, Args, ArchStr: Args.MakeArgString(Str: ArchStr), Triple: TC.getTriple());
5000 if (!CanonicalStr.empty())
5001 Archs.insert(V: CanonicalStr);
5002 else
5003 return {};
5004 }
5005 } else {
5006 StringRef CanonicalStr =
5007 getCanonicalArchString(C, Args, ArchStr: Arch, Triple: TC.getTriple());
5008 if (!CanonicalStr.empty())
5009 Archs.insert(V: CanonicalStr);
5010 else
5011 return {};
5012 }
5013 }
5014 } else if (Arg->getOption().matches(ID: options::OPT_no_offload_arch_EQ)) {
5015 for (StringRef Arch : Arg->getValues()) {
5016 if (Arch == "all") {
5017 Archs.clear();
5018 } else {
5019 StringRef ArchStr =
5020 getCanonicalArchString(C, Args, ArchStr: Arch, Triple: TC.getTriple());
5021 Archs.erase(V: ArchStr);
5022 }
5023 }
5024 }
5025 }
5026
5027 if (auto ConflictingArchs =
5028 getConflictOffloadArchCombination(Archs, Triple: TC.getTriple()))
5029 C.getDriver().Diag(DiagID: clang::diag::err_drv_bad_offload_arch_combo)
5030 << ConflictingArchs->first << ConflictingArchs->second;
5031
5032 // Fill in the default architectures if not provided explicitly.
5033 bool HasSubArch = TC.getTriple().isAMDGCN() &&
5034 TC.getTriple().getSubArch() != llvm::Triple::NoSubArch;
5035 if (Archs.empty() && !HasSubArch) {
5036 if (Kind == Action::OFK_Cuda) {
5037 Archs.insert(V: OffloadArchToString(A: TC.getTriple().isSPIRV()
5038 ? OffloadArch::getUnused()
5039 : OffloadArch::CudaDefault()));
5040 } else if (Kind == Action::OFK_HIP) {
5041 Archs.insert(V: OffloadArchToString(A: TC.getTriple().isSPIRV()
5042 ? OffloadArch::getGeneric()
5043 : OffloadArch::HIPDefault()));
5044 } else if (Kind == Action::OFK_SYCL) {
5045 Archs.insert(V: StringRef());
5046 } else if (Kind == Action::OFK_OpenMP) {
5047 // Accept legacy `-march` device arguments for OpenMP.
5048 if (auto *Arg = C.getArgsForToolChain(TC: &TC, /*BA=*/{}, DeviceOffloadKind: Kind)
5049 .getLastArg(Ids: options::OPT_march_EQ)) {
5050 Archs.insert(V: Arg->getValue());
5051 } else {
5052 auto ArchsOrErr = TC.getSystemGPUArchs(Args);
5053 if (!ArchsOrErr) {
5054 TC.getDriver().Diag(DiagID: diag::err_drv_undetermined_gpu_arch)
5055 << TC.getArchName() << llvm::toString(E: ArchsOrErr.takeError())
5056 << "--offload-arch";
5057 } else if (!ArchsOrErr->empty()) {
5058 for (auto Arch : *ArchsOrErr)
5059 Archs.insert(V: Args.MakeArgStringRef(Str: Arch));
5060 } else {
5061 Archs.insert(V: StringRef());
5062 }
5063 }
5064 }
5065 } else if (Archs.empty() && HasSubArch) {
5066 // Use default CPU if we have a subarch in the triple.
5067 //
5068 // TODO: We ought to be able to get away with the empty string here, but
5069 // many tests require removal of redundant -target-cpu arguments
5070 OffloadArch TripleOffloadArch =
5071 getSubArchOffloadArch(SubArch: TC.getTriple().getSubArch());
5072 llvm::StringRef ArchStr = TripleOffloadArch.isUnknown()
5073 ? ""
5074 : OffloadArchToString(A: TripleOffloadArch);
5075 StringRef CanonicalStr =
5076 getCanonicalArchString(C, Args, ArchStr, Triple: TC.getTriple());
5077 if (!CanonicalStr.empty())
5078 Archs.insert(V: CanonicalStr);
5079 }
5080
5081 Args.ClaimAllArgs(Id0: options::OPT_offload_arch_EQ);
5082 Args.ClaimAllArgs(Id0: options::OPT_no_offload_arch_EQ);
5083
5084 SmallVector<StringRef> Sorted(Archs.begin(), Archs.end());
5085 llvm::sort(C&: Sorted);
5086
5087 // Convert to BoundArch, parsing each architecture string once
5088 SmallVector<BoundArch> Result;
5089 Result.reserve(N: Sorted.size());
5090 for (StringRef Arch : Sorted)
5091 Result.push_back(Elt: BoundArch(Arch));
5092 return Result;
5093}
5094
5095Action *
5096Driver::BuildOffloadingActions(Compilation &C, llvm::opt::DerivedArgList &Args,
5097 const InputTy &Input, StringRef CUID,
5098 Action *HostAction,
5099 ActionList *HIPAsmBundleDeviceOut) const {
5100 // Don't build offloading actions if explicitly disabled or we do not have a
5101 // valid source input.
5102 if (offloadHostOnly() || !types::isSrcFile(Id: Input.first))
5103 return HostAction;
5104
5105 bool HIPNoRDC =
5106 C.isOffloadingHostKind(Kind: Action::OFK_HIP) &&
5107 !Args.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc, Default: false);
5108
5109 bool HIPRelocatableObj =
5110 C.isOffloadingHostKind(Kind: Action::OFK_HIP) &&
5111 Args.hasFlag(Pos: options::OPT_fhip_emit_relocatable,
5112 Neg: options::OPT_fno_hip_emit_relocatable, Default: false);
5113
5114 if (!HIPNoRDC && HIPRelocatableObj)
5115 C.getDriver().Diag(DiagID: diag::err_opt_not_valid_with_opt)
5116 << "-fhip-emit-relocatable"
5117 << "-fgpu-rdc";
5118
5119 if (!offloadDeviceOnly() && HIPRelocatableObj)
5120 C.getDriver().Diag(DiagID: diag::err_opt_not_valid_without_opt)
5121 << "-fhip-emit-relocatable"
5122 << "--offload-device-only";
5123
5124 // Don't build offloading actions if we do not have a compile action. If
5125 // preprocessing only ignore embedding.
5126 if (!(isa<CompileJobAction>(Val: HostAction) ||
5127 getFinalPhase(DAL: Args) == phases::Preprocess))
5128 return HostAction;
5129
5130 bool UsesLLVMOffloading = Args.hasArg(
5131 Ids: options::OPT_foffload_via_llvm, Ids: options::OPT_fno_offload_via_llvm, Ids: false);
5132
5133 ActionList OffloadActions;
5134 OffloadAction::DeviceDependences DDeps;
5135
5136 const Action::OffloadKind OffloadKinds[] = {
5137 Action::OFK_OpenMP, Action::OFK_Cuda, Action::OFK_HIP, Action::OFK_SYCL};
5138
5139 for (Action::OffloadKind Kind : OffloadKinds) {
5140 SmallVector<const ToolChain *, 2> ToolChains;
5141 ActionList DeviceActions;
5142
5143 auto TCRange = C.getOffloadToolChains(Kind);
5144 for (auto TI = TCRange.first, TE = TCRange.second; TI != TE; ++TI)
5145 ToolChains.push_back(Elt: TI->second);
5146
5147 if (ToolChains.empty())
5148 continue;
5149
5150 types::ID InputType = Input.first;
5151 const Arg *InputArg = Input.second;
5152
5153 // The toolchain can be active for unsupported file types.
5154 if ((Kind == Action::OFK_Cuda && !types::isCuda(Id: InputType)) ||
5155 (Kind == Action::OFK_HIP && !types::isHIP(Id: InputType)))
5156 continue;
5157
5158 // Get the product of all bound architectures and toolchains.
5159 SmallVector<std::pair<const ToolChain *, BoundArch>> TCAndArchs;
5160 for (const ToolChain *TC : ToolChains) {
5161 for (BoundArch Arch : getOffloadArchs(C, Args: C.getArgs(), Kind, TC: *TC)) {
5162 TCAndArchs.push_back(Elt: std::make_pair(x&: TC, y&: Arch));
5163 DeviceActions.push_back(
5164 Elt: C.MakeAction<InputAction>(Arg: *InputArg, Arg&: InputType, Arg&: CUID));
5165 }
5166 }
5167
5168 if (DeviceActions.empty())
5169 return HostAction;
5170
5171 // FIXME: Do not collapse the host side for Darwin targets with SYCL offload
5172 // compilations. The toolchain is not properly initialized for the target.
5173 if (isa<CompileJobAction>(Val: HostAction) && Kind == Action::OFK_SYCL &&
5174 HostAction->getType() != types::TY_Nothing &&
5175 C.getSingleOffloadToolChain<Action::OFK_Host>()
5176 ->getTriple()
5177 .isOSDarwin())
5178 HostAction->setCannotBeCollapsedWithNextDependentAction();
5179
5180 auto PL = types::getCompilationPhases(Driver: *this, DAL&: Args, Id: InputType);
5181
5182 for (phases::ID Phase : PL) {
5183 if (Phase == phases::Link) {
5184 assert(Phase == PL.back() && "linking must be final compilation step.");
5185 break;
5186 }
5187
5188 // Assemble actions are not used for the SYCL device side. Both compile
5189 // and backend actions are used to generate IR and textual IR if needed.
5190 if (Kind == Action::OFK_SYCL && Phase == phases::Assemble)
5191 continue;
5192
5193 auto *TCAndArch = TCAndArchs.begin();
5194 for (Action *&A : DeviceActions) {
5195 if (A->getType() == types::TY_Nothing)
5196 continue;
5197
5198 // Propagate the ToolChain so we can use it in ConstructPhaseAction.
5199 A->propagateDeviceOffloadInfo(OKind: Kind, OArch: TCAndArch->second,
5200 OToolChain: TCAndArch->first);
5201 A = ConstructPhaseAction(C, Args, Phase, Input: A, TargetDeviceOffloadKind: Kind,
5202 TargetLTOMode: TCAndArch->first->getLTOMode(Args, Kind));
5203
5204 if (isa<CompileJobAction>(Val: A) && isa<CompileJobAction>(Val: HostAction) &&
5205 Kind == Action::OFK_OpenMP &&
5206 HostAction->getType() != types::TY_Nothing) {
5207 // OpenMP offloading has a dependency on the host compile action to
5208 // identify which declarations need to be emitted. This shouldn't be
5209 // collapsed with any other actions so we can use it in the device.
5210 HostAction->setCannotBeCollapsedWithNextDependentAction();
5211 OffloadAction::HostDependence HDep(
5212 *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(),
5213 TCAndArch->second, Kind);
5214 OffloadAction::DeviceDependences DDep;
5215 DDep.add(A&: *A, TC: *TCAndArch->first, BA: TCAndArch->second, OKind: Kind);
5216 A = C.MakeAction<OffloadAction>(Arg&: HDep, Arg&: DDep);
5217 }
5218
5219 ++TCAndArch;
5220 }
5221 }
5222
5223 // Compiling HIP in device-only non-RDC mode requires linking each action
5224 // individually.
5225 for (Action *&A : DeviceActions) {
5226 auto *OffloadTriple = A->getOffloadingToolChain()
5227 ? &A->getOffloadingToolChain()->getTriple()
5228 : nullptr;
5229 bool IsHIPSPV =
5230 OffloadTriple && OffloadTriple->isSPIRV() &&
5231 (OffloadTriple->getOS() == llvm::Triple::OSType::AMDHSA ||
5232 OffloadTriple->getOS() == llvm::Triple::OSType::ChipStar);
5233
5234 if ((A->getType() != types::TY_Object && !IsHIPSPV &&
5235 A->getType() != types::TY_LTO_BC) ||
5236 HIPRelocatableObj || !HIPNoRDC || !offloadDeviceOnly())
5237 continue;
5238 ActionList LinkerInput = {A};
5239 A = C.MakeAction<LinkJobAction>(Arg&: LinkerInput, Arg: types::TY_Image);
5240 }
5241
5242 auto *TCAndArch = TCAndArchs.begin();
5243 for (Action *A : DeviceActions) {
5244 DDeps.add(A&: *A, TC: *TCAndArch->first, BA: TCAndArch->second, OKind: Kind);
5245 OffloadAction::DeviceDependences DDep;
5246 DDep.add(A&: *A, TC: *TCAndArch->first, BA: TCAndArch->second, OKind: Kind);
5247
5248 // The legacy CUDA fatbinary path can include PTX alongside the cubin.
5249 // The LLVM offload wrapper path feeds these images through a device
5250 // linker first, and clang-nvlink-wrapper does not accept PTX as input.
5251 for (Action *Input : A->getInputs())
5252 if (!UsesLLVMOffloading && Kind == Action::OFK_Cuda &&
5253 A->getType() == types::TY_Object &&
5254 !Args.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc,
5255 Default: false))
5256 DDep.add(A&: *Input, TC: *TCAndArch->first, BA: TCAndArch->second, OKind: Kind);
5257 OffloadActions.push_back(Elt: C.MakeAction<OffloadAction>(Arg&: DDep, Arg: A->getType()));
5258
5259 ++TCAndArch;
5260 }
5261 }
5262
5263 // HIP code in device-only non-RDC mode will bundle the output if it invoked
5264 // the linker or if the user explicitly requested it.
5265 bool ShouldBundleHIP =
5266 Args.hasFlag(Pos: options::OPT_gpu_bundle_output,
5267 Neg: options::OPT_no_gpu_bundle_output, Default: false) ||
5268 (!Args.getLastArg(Ids: options::OPT_no_gpu_bundle_output) && HIPNoRDC &&
5269 offloadDeviceOnly() && llvm::none_of(Range&: OffloadActions, P: [](Action *A) {
5270 return A->getType() != types::TY_Image;
5271 }));
5272
5273 // All kinds exit now in device-only mode except for non-RDC mode HIP.
5274 if (offloadDeviceOnly() && !ShouldBundleHIP)
5275 return C.MakeAction<OffloadAction>(Arg&: DDeps, Arg: types::TY_Nothing);
5276
5277 if (OffloadActions.empty())
5278 return HostAction;
5279
5280 OffloadAction::DeviceDependences DDep;
5281 if (!UsesLLVMOffloading && C.isOffloadingHostKind(Kind: Action::OFK_Cuda) &&
5282 (!Args.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc, Default: false) ||
5283 Args.hasArg(Ids: options::OPT_cuda_emit_nvcc_abi))) {
5284 // If we are not in RDC-mode or are targeting the NVCC ABI we just emit the
5285 // final CUDA fatbinary for each translation unit without any linking.
5286 Action *FatbinAction =
5287 C.MakeAction<LinkJobAction>(Arg&: OffloadActions, Arg: types::TY_CUDA_FATBIN);
5288 DDep.add(A&: *FatbinAction, TC: *C.getSingleOffloadToolChain<Action::OFK_Cuda>(),
5289 /*BA=*/{}, OKind: Action::OFK_Cuda);
5290 } else if (!UsesLLVMOffloading && HIPNoRDC && offloadDeviceOnly()) {
5291 // If we are in device-only non-RDC-mode we just emit the final HIP
5292 // fatbinary for each translation unit, linking each input individually.
5293 Action *FatbinAction =
5294 C.MakeAction<LinkJobAction>(Arg&: OffloadActions, Arg: types::TY_HIP_FATBIN);
5295 DDep.add(A&: *FatbinAction,
5296 TC: *C.getOffloadToolChains<Action::OFK_HIP>().first->second,
5297 /*BA=*/{}, OKind: Action::OFK_HIP);
5298 } else if (!UsesLLVMOffloading && HIPNoRDC) {
5299 // Host + device assembly: defer to clang-offload-bundler (see
5300 // BuildActions).
5301 if (HIPAsmBundleDeviceOut &&
5302 shouldBundleHIPAsmWithNewDriver(C, Args, D: C.getDriver())) {
5303 for (Action *OA : OffloadActions)
5304 HIPAsmBundleDeviceOut->push_back(Elt: OA);
5305 return HostAction;
5306 }
5307 // Package all the offloading actions into a single output that can be
5308 // embedded in the host and linked.
5309 Action *PackagerAction =
5310 C.MakeAction<OffloadPackagerJobAction>(Arg&: OffloadActions, Arg: types::TY_Image);
5311
5312 // For HIP non-RDC compilation, wrap the device binary with linker wrapper
5313 // before bundling with host code. Do not bind a specific GPU arch here,
5314 // as the packaged image may contain entries for multiple GPUs.
5315 ActionList AL{PackagerAction};
5316 PackagerAction =
5317 C.MakeAction<LinkerWrapperJobAction>(Arg&: AL, Arg: types::TY_HIP_FATBIN);
5318 DDep.add(A&: *PackagerAction,
5319 TC: *C.getOffloadToolChains<Action::OFK_HIP>().first->second,
5320 /*BA=*/{}, OKind: Action::OFK_HIP);
5321 } else {
5322 // Package all the offloading actions into a single output that can be
5323 // embedded in the host and linked.
5324 Action *PackagerAction =
5325 C.MakeAction<OffloadPackagerJobAction>(Arg&: OffloadActions, Arg: types::TY_Image);
5326 DDep.add(A&: *PackagerAction, TC: *C.getSingleOffloadToolChain<Action::OFK_Host>(),
5327 /*BA=*/{}, OffloadKindMask: C.getActiveOffloadKinds());
5328 }
5329
5330 // HIP wants '--offload-device-only' to create a fatbinary by default.
5331 if (offloadDeviceOnly())
5332 return C.MakeAction<OffloadAction>(Arg&: DDep, Arg: types::TY_Nothing);
5333
5334 // If we are unable to embed a single device output into the host, we need to
5335 // add each device output as a host dependency to ensure they are still built.
5336 bool SingleDeviceOutput = !llvm::any_of(Range&: OffloadActions, P: [](Action *A) {
5337 return A->getType() == types::TY_Nothing;
5338 }) && isa<CompileJobAction>(Val: HostAction);
5339 OffloadAction::HostDependence HDep(
5340 *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(),
5341 /*BA=*/{}, SingleDeviceOutput ? DDep : DDeps);
5342 return C.MakeAction<OffloadAction>(Arg&: HDep, Arg&: SingleDeviceOutput ? DDep : DDeps);
5343}
5344
5345Action *Driver::ConstructPhaseAction(
5346 Compilation &C, const ArgList &Args, phases::ID Phase, Action *Input,
5347 Action::OffloadKind TargetDeviceOffloadKind, LTOKind TargetLTOMode) const {
5348 llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
5349
5350 // Some types skip the assembler phase (e.g., llvm-bc), but we can't
5351 // encode this in the steps because the intermediate type depends on
5352 // arguments. Just special case here.
5353 if (Phase == phases::Assemble && Input->getType() != types::TY_PP_Asm)
5354 return Input;
5355
5356 // Use of --sycl-link will only allow for the link phase to occur. This is
5357 // for all input files.
5358 if (Args.hasArg(Ids: options::OPT_sycl_link) && Phase != phases::Link)
5359 return Input;
5360
5361 // Build the appropriate action.
5362 switch (Phase) {
5363 case phases::Link:
5364 llvm_unreachable("link action invalid here.");
5365 case phases::IfsMerge:
5366 llvm_unreachable("ifsmerge action invalid here.");
5367 case phases::Preprocess: {
5368 types::ID OutputTy;
5369 // -M and -MM specify the dependency file name by altering the output type,
5370 // -if -MD and -MMD are not specified.
5371 if (Args.hasArg(Ids: options::OPT_M, Ids: options::OPT_MM) &&
5372 !Args.hasArg(Ids: options::OPT_MD, Ids: options::OPT_MMD)) {
5373 OutputTy = types::TY_Dependencies;
5374 } else {
5375 OutputTy = Input->getType();
5376 // For these cases, the preprocessor is only translating forms, the Output
5377 // still needs preprocessing.
5378 if (!Args.hasFlag(Pos: options::OPT_frewrite_includes,
5379 Neg: options::OPT_fno_rewrite_includes, Default: false) &&
5380 !Args.hasFlag(Pos: options::OPT_frewrite_imports,
5381 Neg: options::OPT_fno_rewrite_imports, Default: false) &&
5382 !Args.hasFlag(Pos: options::OPT_fdirectives_only,
5383 Neg: options::OPT_fno_directives_only, Default: false) &&
5384 !CCGenDiagnostics)
5385 OutputTy = types::getPreprocessedType(Id: OutputTy);
5386 assert(OutputTy != types::TY_INVALID &&
5387 "Cannot preprocess this input type!");
5388 }
5389 return C.MakeAction<PreprocessJobAction>(Arg&: Input, Arg&: OutputTy);
5390 }
5391 case phases::Precompile: {
5392 // API extraction should not generate an actual precompilation action.
5393 if (Args.hasArg(Ids: options::OPT_extract_api))
5394 return C.MakeAction<ExtractAPIJobAction>(Arg&: Input, Arg: types::TY_API_INFO);
5395
5396 // Standard library modules always precompile in -fmodules-driver mode,
5397 // even when -fsyntax-only is specified.
5398 if (Input->getType() == types::TY_CXXStdModule ||
5399 Input->getType() == types::TY_PP_CXXStdModule)
5400 return C.MakeAction<PrecompileJobAction>(
5401 Arg&: Input, Arg: getPrecompiledType(Id: Input->getType()));
5402
5403 // With 'fmodules-reduced-bmi', we don't want to run the
5404 // precompile phase unless the user specified '--precompile' or
5405 // '--precompile-reduced-bmi'. If '--precompile' is specified, we will try
5406 // to emit the reduced BMI as a by product in
5407 // GenerateModuleInterfaceAction. If '--precompile-reduced-bmi' is
5408 // specified, we will generate the reduced BMI directly.
5409 if (!Args.hasArg(Ids: options::OPT_fno_modules_reduced_bmi) &&
5410 (Input->getType() == driver::types::TY_CXXModule ||
5411 Input->getType() == driver::types::TY_PP_CXXModule) &&
5412 !Args.getLastArg(Ids: options::OPT__precompile) &&
5413 !Args.getLastArg(Ids: options::OPT__precompile_reduced_bmi))
5414 return Input;
5415
5416 types::ID OutputTy = getPrecompiledType(Id: Input->getType());
5417 assert(OutputTy != types::TY_INVALID &&
5418 "Cannot precompile this input type!");
5419
5420 // If we're given a module name, precompile header file inputs as a
5421 // module, not as a precompiled header.
5422 const char *ModName = nullptr;
5423 if (OutputTy == types::TY_PCH) {
5424 if (Arg *A = Args.getLastArg(Ids: options::OPT_fmodule_name_EQ))
5425 ModName = A->getValue();
5426 if (ModName)
5427 OutputTy = types::TY_ModuleFile;
5428 }
5429
5430 if (Args.hasArg(Ids: options::OPT_fsyntax_only)) {
5431 // Syntax checks should not emit a PCH file
5432 OutputTy = types::TY_Nothing;
5433 }
5434
5435 return C.MakeAction<PrecompileJobAction>(Arg&: Input, Arg&: OutputTy);
5436 }
5437 case phases::Compile: {
5438 if (Args.hasArg(Ids: options::OPT_fsyntax_only))
5439 return C.MakeAction<CompileJobAction>(Arg&: Input, Arg: types::TY_Nothing);
5440 if (Args.hasArg(Ids: options::OPT_rewrite_objc))
5441 return C.MakeAction<CompileJobAction>(Arg&: Input, Arg: types::TY_RewrittenObjC);
5442 if (Args.hasArg(Ids: options::OPT_rewrite_legacy_objc))
5443 return C.MakeAction<CompileJobAction>(Arg&: Input,
5444 Arg: types::TY_RewrittenLegacyObjC);
5445 if (Args.hasArg(Ids: options::OPT__analyze))
5446 return C.MakeAction<AnalyzeJobAction>(Arg&: Input, Arg: types::TY_Plist);
5447 if (Args.hasArg(Ids: options::OPT_emit_ast))
5448 return C.MakeAction<CompileJobAction>(Arg&: Input, Arg: types::TY_AST);
5449 if (Args.hasArg(Ids: options::OPT_emit_cir))
5450 return C.MakeAction<CompileJobAction>(Arg&: Input, Arg: types::TY_CIR);
5451 if (Args.hasArg(Ids: options::OPT_module_file_info))
5452 return C.MakeAction<CompileJobAction>(Arg&: Input, Arg: types::TY_ModuleFile);
5453 if (Args.hasArg(Ids: options::OPT_verify_pch))
5454 return C.MakeAction<VerifyPCHJobAction>(Arg&: Input, Arg: types::TY_Nothing);
5455 if (Args.hasArg(Ids: options::OPT_extract_api))
5456 return C.MakeAction<ExtractAPIJobAction>(Arg&: Input, Arg: types::TY_API_INFO);
5457 return C.MakeAction<CompileJobAction>(Arg&: Input, Arg: types::TY_LLVM_BC);
5458 }
5459 case phases::Backend: {
5460 if (TargetLTOMode != LTOK_None) {
5461 bool IsDeviceOffload = TargetDeviceOffloadKind != Action::OFK_None;
5462 if (!IsDeviceOffload) {
5463 types::ID Output;
5464 if (Args.hasArg(Ids: options::OPT_ffat_lto_objects) &&
5465 !Args.hasArg(Ids: options::OPT_emit_llvm))
5466 Output = types::TY_PP_Asm;
5467 else if (Args.hasArg(Ids: options::OPT_S))
5468 Output = types::TY_LTO_IR;
5469 else
5470 Output = types::TY_LTO_BC;
5471 return C.MakeAction<BackendJobAction>(Arg&: Input, Arg&: Output);
5472 }
5473 types::ID Output;
5474 if (Args.hasArg(Ids: options::OPT_emit_llvm)) {
5475 Output =
5476 Args.hasArg(Ids: options::OPT_S) ? types::TY_LLVM_IR : types::TY_LLVM_BC;
5477 } else if (Args.hasArg(Ids: options::OPT_S) && offloadDeviceOnly() &&
5478 !Args.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc,
5479 Default: false)) {
5480 // For non-RDC device-only compilations with -S, produce real assembly
5481 // since the user explicitly requested assembly output.
5482 Output = types::TY_PP_Asm;
5483 } else if (Args.hasArg(Ids: options::OPT_S)) {
5484 Output = types::TY_LTO_IR;
5485 } else {
5486 Output = types::TY_LTO_BC;
5487 }
5488 return C.MakeAction<BackendJobAction>(Arg&: Input, Arg&: Output);
5489 }
5490 if (Args.hasArg(Ids: options::OPT_emit_llvm) ||
5491 TargetDeviceOffloadKind == Action::OFK_SYCL) {
5492 types::ID Output =
5493 Args.hasArg(Ids: options::OPT_S) ? types::TY_LLVM_IR : types::TY_LLVM_BC;
5494 return C.MakeAction<BackendJobAction>(Arg&: Input, Arg&: Output);
5495 }
5496
5497 return C.MakeAction<BackendJobAction>(Arg&: Input, Arg: types::TY_PP_Asm);
5498 }
5499 case phases::Assemble:
5500 // When -marm64x is used, construct jobs for the EC and native targets and
5501 // merge them into an archive with llvm-objcopy.
5502 const llvm::Triple Target(llvm::Triple::normalize(Str: TargetTriple));
5503 if (Target.isOSWindows() && Args.hasArg(Ids: options::OPT_marm64x)) {
5504 Action *Act =
5505 C.MakeAction<AssembleJobAction>(Arg: std::move(Input), Arg: types::TY_Object);
5506 ActionList Inputs;
5507 Inputs.push_back(Elt: C.MakeAction<BindArchAction>(Arg&: Act, Arg: BoundArch("aarch64")));
5508 Inputs.push_back(Elt: C.MakeAction<BindArchAction>(Arg&: Act, Arg: BoundArch("arm64ec")));
5509 return C.MakeAction<ObjcopyJobAction>(Arg&: Inputs, Arg: types::TY_Object);
5510 }
5511 return C.MakeAction<AssembleJobAction>(Arg: std::move(Input), Arg: types::TY_Object);
5512 }
5513
5514 llvm_unreachable("invalid phase in ConstructPhaseAction");
5515}
5516
5517static bool isOffloadDeviceCC1JobCandidate(Command &Job) {
5518 const Action &Source = Job.getSource();
5519 if (!isa<CompileJobAction>(Val: Source) && !isa<BackendJobAction>(Val: Source))
5520 return false;
5521
5522 if (Job.getBoundArch().empty() && !Source.getOffloadingArch().empty())
5523 Job.setBoundArch(Source.getOffloadingArch());
5524
5525 if (Job.getBoundArch().empty())
5526 return false;
5527
5528 if (StringRef(Job.getCreator().getName()) != "clang")
5529 return false;
5530
5531 Action::OffloadKind OKind = Source.getOffloadingDeviceKind();
5532 if (OKind != Action::OFK_None && OKind != Action::OFK_Host)
5533 return true;
5534
5535 const llvm::Triple &Triple = Job.getCreator().getToolChain().getTriple();
5536 return Triple.isAMDGPU() || Triple.isNVPTX() || Triple.isSPIROrSPIRV();
5537}
5538
5539static std::string getOffloadDeviceCC1ParallelJobGroup(const Command &Job) {
5540 const Action &Source = Job.getSource();
5541 // This key groups device cc1 jobs that can run in parallel. Jobs may differ
5542 // by offload arch, but must have the same offload kind, target triple,
5543 // action kind, and output type. For example, HIP compile jobs for gfx900 and
5544 // gfx906 can share a group, but HIP and OpenMP jobs cannot.
5545 return (Twine(Action::GetOffloadKindName(Kind: Source.getOffloadingDeviceKind())) +
5546 ":" + Job.getCreator().getToolChain().getTripleString() + ":" +
5547 Source.getClassName() + ":" + types::getTypeName(Id: Source.getType()))
5548 .str();
5549}
5550
5551static void claimAndDiagnoseOffloadJobs(const Driver &D, const ArgList &Args) {
5552 auto OffloadJobs = tools::parseOffloadJobs(Args);
5553 if (!OffloadJobs.A)
5554 return;
5555
5556 if (!OffloadJobs.isValid())
5557 D.Diag(DiagID: diag::err_drv_invalid_int_value)
5558 << OffloadJobs.A->getAsString(Args) << OffloadJobs.Value;
5559
5560 OffloadJobs.A->claim();
5561}
5562
5563static void markOffloadDeviceCC1JobsForParallelExecution(Compilation &C) {
5564 for (auto &Job : C.getJobs()) {
5565 if (!isOffloadDeviceCC1JobCandidate(Job))
5566 continue;
5567
5568 Job.setOffloadDeviceParallelJobGroup(
5569 getOffloadDeviceCC1ParallelJobGroup(Job));
5570 }
5571}
5572
5573void Driver::BuildJobs(Compilation &C) const {
5574 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
5575
5576 Arg *FinalOutput = C.getArgs().getLastArg(Ids: options::OPT_o);
5577
5578 // It is an error to provide a -o option if we are making multiple output
5579 // files. There are exceptions:
5580 //
5581 // IfsMergeJob: when generating interface stubs enabled we want to be able to
5582 // generate the stub file at the same time that we generate the real
5583 // library/a.out. So when a .o, .so, etc are the output, with clang interface
5584 // stubs there will also be a .ifs and .ifso at the same location.
5585 //
5586 // CompileJob of type TY_IFS_CPP: when generating interface stubs is enabled
5587 // and -c is passed, we still want to be able to generate a .ifs file while
5588 // we are also generating .o files. So we allow more than one output file in
5589 // this case as well.
5590 //
5591 // OffloadClass of type TY_Nothing: device-only output will place many outputs
5592 // into a single offloading action. We should count all inputs to the action
5593 // as outputs. Also ignore device-only outputs if we're compiling with
5594 // -fsyntax-only.
5595 if (FinalOutput) {
5596 unsigned NumOutputs = 0;
5597 unsigned NumIfsOutputs = 0;
5598 for (const Action *A : C.getActions()) {
5599 // The actions below do not increase the number of outputs.
5600 if (A->getKind() == clang::driver::Action::BinaryAnalyzeJobClass ||
5601 A->getKind() == clang::driver::Action::BinaryTranslatorJobClass)
5602 continue;
5603
5604 // With -fmodules-driver, Standard library modules should not count toward
5605 // the number of outputs, since they are implicitly added to the input
5606 // list.
5607 if (isa<PrecompileJobAction>(Val: A) && !A->getInputs().empty() &&
5608 (A->getInputs().front()->getType() == types::TY_CXXStdModule ||
5609 A->getInputs().front()->getType() == types::TY_PP_CXXStdModule))
5610 continue;
5611
5612 if (A->getType() != types::TY_Nothing &&
5613 !(A->getKind() == Action::IfsMergeJobClass ||
5614 (A->getType() == clang::driver::types::TY_IFS_CPP &&
5615 A->getKind() == clang::driver::Action::CompileJobClass &&
5616 0 == NumIfsOutputs++) ||
5617 (A->getKind() == Action::BindArchClass && A->getInputs().size() &&
5618 A->getInputs().front()->getKind() == Action::IfsMergeJobClass)))
5619 ++NumOutputs;
5620 else if (A->getKind() == Action::OffloadClass &&
5621 A->getType() == types::TY_Nothing &&
5622 !C.getArgs().hasArg(Ids: options::OPT_fsyntax_only))
5623 NumOutputs += A->size();
5624 }
5625
5626 if (NumOutputs > 1) {
5627 Diag(DiagID: clang::diag::err_drv_output_argument_with_multiple_files);
5628 FinalOutput = nullptr;
5629 }
5630 }
5631
5632 const llvm::Triple &RawTriple = C.getDefaultToolChain().getTriple();
5633
5634 // Collect the list of architectures.
5635 llvm::StringSet<> ArchNames;
5636 if (RawTriple.isOSBinFormatMachO())
5637 for (const Arg *A : C.getArgs())
5638 if (A->getOption().matches(ID: options::OPT_arch))
5639 ArchNames.insert(key: A->getValue());
5640
5641 // Set of (Action, canonical ToolChain triple) pairs we've built jobs for.
5642 std::map<std::pair<const Action *, std::string>, InputInfoList> CachedResults;
5643 for (Action *A : C.getActions()) {
5644 // If we are linking an image for multiple archs then the linker wants
5645 // -arch_multiple and -final_output <final image name>. Unfortunately, this
5646 // doesn't fit in cleanly because we have to pass this information down.
5647 //
5648 // FIXME: This is a hack; find a cleaner way to integrate this into the
5649 // process.
5650 const char *LinkingOutput = nullptr;
5651 if (isa<LipoJobAction>(Val: A)) {
5652 if (FinalOutput)
5653 LinkingOutput = FinalOutput->getValue();
5654 else
5655 LinkingOutput = getDefaultImageName();
5656 }
5657
5658 BuildJobsForAction(C, A, TC: &C.getDefaultToolChain(),
5659 /*BA=*/{},
5660 /*AtTopLevel*/ true,
5661 /*MultipleArchs*/ ArchNames.size() > 1 ||
5662 C.getArgs().hasArgNoClaim(Ids: options::OPT_marm64x),
5663 /*LinkingOutput*/ LinkingOutput, CachedResults,
5664 /*TargetDeviceOffloadKind*/ Action::OFK_None);
5665 }
5666
5667 // If we have more than one job, then disable integrated-cc1 for now. Do this
5668 // also when we need to report process execution statistics.
5669 if (C.getJobs().size() > 1 || CCPrintProcessStats)
5670 for (auto &J : C.getJobs())
5671 J.InProcess = false;
5672
5673 markOffloadDeviceCC1JobsForParallelExecution(C);
5674 if (C.getActiveOffloadKinds() != Action::OFK_None)
5675 claimAndDiagnoseOffloadJobs(D: *this, Args: C.getArgs());
5676
5677 if (CCPrintProcessStats) {
5678 C.setPostCallback([=](const Command &Cmd, int Res) {
5679 std::optional<llvm::sys::ProcessStatistics> ProcStat =
5680 Cmd.getProcessStatistics();
5681 if (!ProcStat)
5682 return;
5683
5684 const char *LinkingOutput = nullptr;
5685 if (FinalOutput)
5686 LinkingOutput = FinalOutput->getValue();
5687 else if (!Cmd.getOutputFilenames().empty())
5688 LinkingOutput = Cmd.getOutputFilenames().front().c_str();
5689 else
5690 LinkingOutput = getDefaultImageName();
5691
5692 if (CCPrintStatReportFilename.empty()) {
5693 using namespace llvm;
5694 // Human readable output.
5695 outs() << sys::path::filename(path: Cmd.getExecutable()) << ": "
5696 << "output=" << LinkingOutput;
5697 outs() << ", total="
5698 << format(Fmt: "%.3f", Vals: ProcStat->TotalTime.count() / 1000.) << " ms"
5699 << ", user="
5700 << format(Fmt: "%.3f", Vals: ProcStat->UserTime.count() / 1000.) << " ms"
5701 << ", mem=" << ProcStat->PeakMemory << " Kb\n";
5702 } else {
5703 // CSV format.
5704 std::string Buffer;
5705 llvm::raw_string_ostream Out(Buffer);
5706 llvm::sys::printArg(OS&: Out, Arg: llvm::sys::path::filename(path: Cmd.getExecutable()),
5707 /*Quote*/ true);
5708 Out << ',';
5709 llvm::sys::printArg(OS&: Out, Arg: LinkingOutput, Quote: true);
5710 Out << ',' << ProcStat->TotalTime.count() << ','
5711 << ProcStat->UserTime.count() << ',' << ProcStat->PeakMemory
5712 << '\n';
5713 Out.flush();
5714 std::error_code EC;
5715 llvm::raw_fd_ostream OS(CCPrintStatReportFilename, EC,
5716 llvm::sys::fs::OF_Append |
5717 llvm::sys::fs::OF_Text);
5718 if (EC)
5719 return;
5720 auto L = OS.lock();
5721 if (!L) {
5722 llvm::errs() << "ERROR: Cannot lock file "
5723 << CCPrintStatReportFilename << ": "
5724 << toString(E: L.takeError()) << "\n";
5725 return;
5726 }
5727 OS << Buffer;
5728 OS.flush();
5729 }
5730 });
5731 }
5732
5733 // If the user passed -Qunused-arguments or there were errors, don't
5734 // warn about any unused arguments.
5735 bool ReportUnusedArguments =
5736 !Diags.hasErrorOccurred() &&
5737 !C.getArgs().hasArg(Ids: options::OPT_Qunused_arguments);
5738
5739 // Claim -fdriver-only here.
5740 (void)C.getArgs().hasArg(Ids: options::OPT_fdriver_only);
5741 // Claim -### here.
5742 (void)C.getArgs().hasArg(Ids: options::OPT__HASH_HASH_HASH);
5743
5744 // Claim --driver-mode, --rsp-quoting, it was handled earlier.
5745 (void)C.getArgs().hasArg(Ids: options::OPT_driver_mode);
5746 (void)C.getArgs().hasArg(Ids: options::OPT_rsp_quoting);
5747
5748 bool HasAssembleJob = llvm::any_of(Range&: C.getJobs(), P: [](auto &J) {
5749 // Match ClangAs and other derived assemblers of Tool. ClangAs uses a
5750 // longer ShortName "clang integrated assembler" while other assemblers just
5751 // use "assembler".
5752 return strstr(J.getCreator().getShortName(), "assembler");
5753 });
5754 for (Arg *A : C.getArgs()) {
5755 // FIXME: It would be nice to be able to send the argument to the
5756 // DiagnosticsEngine, so that extra values, position, and so on could be
5757 // printed.
5758 if (!A->isClaimed()) {
5759 if (A->getOption().hasFlag(Val: options::NoArgumentUnused))
5760 continue;
5761
5762 // Suppress the warning automatically if this is just a flag, and it is an
5763 // instance of an argument we already claimed.
5764 const Option &Opt = A->getOption();
5765 if (Opt.getKind() == Option::FlagClass) {
5766 bool DuplicateClaimed = false;
5767
5768 for (const Arg *AA : C.getArgs().filtered(Ids: &Opt)) {
5769 if (AA->isClaimed()) {
5770 DuplicateClaimed = true;
5771 break;
5772 }
5773 }
5774
5775 if (DuplicateClaimed)
5776 continue;
5777 }
5778
5779 // In clang-cl, don't mention unknown arguments here since they have
5780 // already been warned about.
5781 if (!IsCLMode() || !A->getOption().matches(ID: options::OPT_UNKNOWN)) {
5782 if (A->getOption().hasFlag(Val: options::TargetSpecific) &&
5783 !A->isIgnoredTargetSpecific() && !HasAssembleJob &&
5784 // When for example -### or -v is used
5785 // without a file, target specific options are not
5786 // consumed/validated.
5787 // Instead emitting an error emit a warning instead.
5788 !C.getActions().empty()) {
5789 Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
5790 << A->getSpelling() << getTargetTriple();
5791 } else if (ReportUnusedArguments) {
5792 Diag(DiagID: clang::diag::warn_drv_unused_argument)
5793 << A->getAsString(Args: C.getArgs());
5794 }
5795 }
5796 }
5797 }
5798}
5799
5800namespace {
5801/// Utility class to control the collapse of dependent actions and select the
5802/// tools accordingly.
5803class ToolSelector final {
5804 /// The tool chain this selector refers to.
5805 const ToolChain &TC;
5806
5807 /// The compilation this selector refers to.
5808 const Compilation &C;
5809
5810 /// The base action this selector refers to.
5811 const JobAction *BaseAction;
5812
5813 /// Set to true if the current toolchain refers to host actions.
5814 bool IsHostSelector;
5815
5816 /// Set to true if save-temps and embed-bitcode functionalities are active.
5817 bool SaveTemps;
5818 bool EmbedBitcode;
5819
5820 /// Get previous dependent action or null if that does not exist. If
5821 /// \a CanBeCollapsed is false, that action must be legal to collapse or
5822 /// null will be returned.
5823 const JobAction *getPrevDependentAction(const ActionList &Inputs,
5824 ActionList &SavedOffloadAction,
5825 bool CanBeCollapsed = true) {
5826 // An option can be collapsed only if it has a single input.
5827 if (Inputs.size() != 1)
5828 return nullptr;
5829
5830 Action *CurAction = *Inputs.begin();
5831 if (CanBeCollapsed &&
5832 !CurAction->isCollapsingWithNextDependentActionLegal())
5833 return nullptr;
5834
5835 // If the input action is an offload action. Look through it and save any
5836 // offload action that can be dropped in the event of a collapse.
5837 if (auto *OA = dyn_cast<OffloadAction>(Val: CurAction)) {
5838 // If the dependent action is a device action, we will attempt to collapse
5839 // only with other device actions. Otherwise, we would do the same but
5840 // with host actions only.
5841 if (!IsHostSelector) {
5842 if (OA->hasSingleDeviceDependence(/*DoNotConsiderHostActions=*/true)) {
5843 CurAction =
5844 OA->getSingleDeviceDependence(/*DoNotConsiderHostActions=*/true);
5845 if (CanBeCollapsed &&
5846 !CurAction->isCollapsingWithNextDependentActionLegal())
5847 return nullptr;
5848 SavedOffloadAction.push_back(Elt: OA);
5849 return dyn_cast<JobAction>(Val: CurAction);
5850 }
5851 } else if (OA->hasHostDependence()) {
5852 CurAction = OA->getHostDependence();
5853 if (CanBeCollapsed &&
5854 !CurAction->isCollapsingWithNextDependentActionLegal())
5855 return nullptr;
5856 SavedOffloadAction.push_back(Elt: OA);
5857 return dyn_cast<JobAction>(Val: CurAction);
5858 }
5859 return nullptr;
5860 }
5861
5862 return dyn_cast<JobAction>(Val: CurAction);
5863 }
5864
5865 /// Return true if an assemble action can be collapsed.
5866 bool canCollapseAssembleAction() const {
5867 return TC.useIntegratedAs() && !SaveTemps &&
5868 !C.getArgs().hasArg(Ids: options::OPT_via_file_asm) &&
5869 !C.getArgs().hasArg(Ids: options::OPT__SLASH_FA) &&
5870 !C.getArgs().hasArg(Ids: options::OPT__SLASH_Fa) &&
5871 !C.getArgs().hasArg(Ids: options::OPT_dxc_Fc);
5872 }
5873
5874 /// Return true if a preprocessor action can be collapsed.
5875 bool canCollapsePreprocessorAction() const {
5876 return !C.getArgs().hasArg(Ids: options::OPT_no_integrated_cpp) &&
5877 !C.getArgs().hasArg(Ids: options::OPT_traditional_cpp) && !SaveTemps &&
5878 !C.getArgs().hasArg(Ids: options::OPT_rewrite_objc);
5879 }
5880
5881 /// Struct that relates an action with the offload actions that would be
5882 /// collapsed with it.
5883 struct JobActionInfo final {
5884 /// The action this info refers to.
5885 const JobAction *JA = nullptr;
5886 /// The offload actions we need to take care off if this action is
5887 /// collapsed.
5888 ActionList SavedOffloadAction;
5889 };
5890
5891 /// Append collapsed offload actions from the give number of elements in the
5892 /// action info array.
5893 static void AppendCollapsedOffloadAction(ActionList &CollapsedOffloadAction,
5894 ArrayRef<JobActionInfo> &ActionInfo,
5895 unsigned ElementNum) {
5896 assert(ElementNum <= ActionInfo.size() && "Invalid number of elements.");
5897 for (unsigned I = 0; I < ElementNum; ++I)
5898 CollapsedOffloadAction.append(in_start: ActionInfo[I].SavedOffloadAction.begin(),
5899 in_end: ActionInfo[I].SavedOffloadAction.end());
5900 }
5901
5902 /// Functions that attempt to perform the combining. They detect if that is
5903 /// legal, and if so they update the inputs \a Inputs and the offload action
5904 /// that were collapsed in \a CollapsedOffloadAction. A tool that deals with
5905 /// the combined action is returned. If the combining is not legal or if the
5906 /// tool does not exist, null is returned.
5907 /// Currently three kinds of collapsing are supported:
5908 /// - Assemble + Backend + Compile;
5909 /// - Assemble + Backend ;
5910 /// - Backend + Compile.
5911 const Tool *
5912 combineAssembleBackendCompile(ArrayRef<JobActionInfo> ActionInfo,
5913 ActionList &Inputs,
5914 ActionList &CollapsedOffloadAction) {
5915 if (ActionInfo.size() < 3 || !canCollapseAssembleAction())
5916 return nullptr;
5917 auto *AJ = dyn_cast<AssembleJobAction>(Val: ActionInfo[0].JA);
5918 auto *BJ = dyn_cast<BackendJobAction>(Val: ActionInfo[1].JA);
5919 auto *CJ = dyn_cast<CompileJobAction>(Val: ActionInfo[2].JA);
5920 if (!AJ || !BJ || !CJ)
5921 return nullptr;
5922
5923 // Get compiler tool.
5924 const Tool *T = TC.SelectTool(JA: *CJ);
5925 if (!T)
5926 return nullptr;
5927
5928 // Can't collapse if we don't have codegen support unless we are
5929 // emitting LLVM IR.
5930 bool OutputIsLLVM = types::isLLVMIR(Id: ActionInfo[0].JA->getType());
5931 if (!T->hasIntegratedBackend() && !(OutputIsLLVM && T->canEmitIR()))
5932 return nullptr;
5933
5934 // When using -fembed-bitcode, it is required to have the same tool (clang)
5935 // for both CompilerJA and BackendJA. Otherwise, combine two stages.
5936 if (EmbedBitcode) {
5937 const Tool *BT = TC.SelectTool(JA: *BJ);
5938 if (BT == T)
5939 return nullptr;
5940 }
5941
5942 if (!T->hasIntegratedAssembler())
5943 return nullptr;
5944
5945 Inputs = CJ->getInputs();
5946 AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo,
5947 /*NumElements=*/ElementNum: 3);
5948 return T;
5949 }
5950 const Tool *combineAssembleBackend(ArrayRef<JobActionInfo> ActionInfo,
5951 ActionList &Inputs,
5952 ActionList &CollapsedOffloadAction) {
5953 if (ActionInfo.size() < 2 || !canCollapseAssembleAction())
5954 return nullptr;
5955 auto *AJ = dyn_cast<AssembleJobAction>(Val: ActionInfo[0].JA);
5956 auto *BJ = dyn_cast<BackendJobAction>(Val: ActionInfo[1].JA);
5957 if (!AJ || !BJ)
5958 return nullptr;
5959
5960 // Get backend tool.
5961 const Tool *T = TC.SelectTool(JA: *BJ);
5962 if (!T)
5963 return nullptr;
5964
5965 if (!T->hasIntegratedAssembler())
5966 return nullptr;
5967
5968 Inputs = BJ->getInputs();
5969 AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo,
5970 /*NumElements=*/ElementNum: 2);
5971 return T;
5972 }
5973 const Tool *combineBackendCompile(ArrayRef<JobActionInfo> ActionInfo,
5974 ActionList &Inputs,
5975 ActionList &CollapsedOffloadAction) {
5976 if (ActionInfo.size() < 2)
5977 return nullptr;
5978 auto *BJ = dyn_cast<BackendJobAction>(Val: ActionInfo[0].JA);
5979 auto *CJ = dyn_cast<CompileJobAction>(Val: ActionInfo[1].JA);
5980 if (!BJ || !CJ)
5981 return nullptr;
5982
5983 auto HasBitcodeInput = [](const JobActionInfo &AI) {
5984 for (auto &Input : AI.JA->getInputs())
5985 if (!types::isLLVMIR(Id: Input->getType()))
5986 return false;
5987 return true;
5988 };
5989
5990 // Check if the initial input (to the compile job or its predessor if one
5991 // exists) is LLVM bitcode. In that case, no preprocessor step is required
5992 // and we can still collapse the compile and backend jobs when we have
5993 // -save-temps. I.e. there is no need for a separate compile job just to
5994 // emit unoptimized bitcode.
5995 bool InputIsBitcode = all_of(Range&: ActionInfo, P: HasBitcodeInput);
5996 if (SaveTemps && !InputIsBitcode)
5997 return nullptr;
5998
5999 // Get compiler tool.
6000 const Tool *T = TC.SelectTool(JA: *CJ);
6001 if (!T)
6002 return nullptr;
6003
6004 // Can't collapse if we don't have codegen support unless we are
6005 // emitting LLVM IR.
6006 bool OutputIsLLVM = types::isLLVMIR(Id: ActionInfo[0].JA->getType());
6007 if (!T->hasIntegratedBackend() && !(OutputIsLLVM && T->canEmitIR()))
6008 return nullptr;
6009
6010 if (T->canEmitIR() && EmbedBitcode)
6011 return nullptr;
6012
6013 Inputs = CJ->getInputs();
6014 AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo,
6015 /*NumElements=*/ElementNum: 2);
6016 return T;
6017 }
6018
6019 /// Updates the inputs if the obtained tool supports combining with
6020 /// preprocessor action, and the current input is indeed a preprocessor
6021 /// action. If combining results in the collapse of offloading actions, those
6022 /// are appended to \a CollapsedOffloadAction.
6023 void combineWithPreprocessor(const Tool *T, ActionList &Inputs,
6024 ActionList &CollapsedOffloadAction) {
6025 if (!T || !canCollapsePreprocessorAction() || !T->hasIntegratedCPP())
6026 return;
6027
6028 // Attempt to get a preprocessor action dependence.
6029 ActionList PreprocessJobOffloadActions;
6030 ActionList NewInputs;
6031 for (Action *A : Inputs) {
6032 auto *PJ = getPrevDependentAction(Inputs: {A}, SavedOffloadAction&: PreprocessJobOffloadActions);
6033 if (!PJ || !isa<PreprocessJobAction>(Val: PJ)) {
6034 NewInputs.push_back(Elt: A);
6035 continue;
6036 }
6037
6038 // This is legal to combine. Append any offload action we found and add the
6039 // current input to preprocessor inputs.
6040 CollapsedOffloadAction.append(in_start: PreprocessJobOffloadActions.begin(),
6041 in_end: PreprocessJobOffloadActions.end());
6042 NewInputs.append(in_start: PJ->input_begin(), in_end: PJ->input_end());
6043 }
6044 Inputs = NewInputs;
6045 }
6046
6047public:
6048 ToolSelector(const JobAction *BaseAction, const ToolChain &TC,
6049 const Compilation &C, bool SaveTemps, bool EmbedBitcode)
6050 : TC(TC), C(C), BaseAction(BaseAction), SaveTemps(SaveTemps),
6051 EmbedBitcode(EmbedBitcode) {
6052 assert(BaseAction && "Invalid base action.");
6053 IsHostSelector = BaseAction->getOffloadingDeviceKind() == Action::OFK_None;
6054 }
6055
6056 /// Check if a chain of actions can be combined and return the tool that can
6057 /// handle the combination of actions. The pointer to the current inputs \a
6058 /// Inputs and the list of offload actions \a CollapsedOffloadActions
6059 /// connected to collapsed actions are updated accordingly. The latter enables
6060 /// the caller of the selector to process them afterwards instead of just
6061 /// dropping them. If no suitable tool is found, null will be returned.
6062 const Tool *getTool(ActionList &Inputs,
6063 ActionList &CollapsedOffloadAction) {
6064 //
6065 // Get the largest chain of actions that we could combine.
6066 //
6067
6068 SmallVector<JobActionInfo, 5> ActionChain(1);
6069 ActionChain.back().JA = BaseAction;
6070 while (ActionChain.back().JA) {
6071 const Action *CurAction = ActionChain.back().JA;
6072
6073 // Grow the chain by one element.
6074 ActionChain.resize(N: ActionChain.size() + 1);
6075 JobActionInfo &AI = ActionChain.back();
6076
6077 // Attempt to fill it with the
6078 AI.JA =
6079 getPrevDependentAction(Inputs: CurAction->getInputs(), SavedOffloadAction&: AI.SavedOffloadAction);
6080 }
6081
6082 // Pop the last action info as it could not be filled.
6083 ActionChain.pop_back();
6084
6085 //
6086 // Attempt to combine actions. If all combining attempts failed, just return
6087 // the tool of the provided action. At the end we attempt to combine the
6088 // action with any preprocessor action it may depend on.
6089 //
6090
6091 const Tool *T = combineAssembleBackendCompile(ActionInfo: ActionChain, Inputs,
6092 CollapsedOffloadAction);
6093 if (!T)
6094 T = combineAssembleBackend(ActionInfo: ActionChain, Inputs, CollapsedOffloadAction);
6095 if (!T)
6096 T = combineBackendCompile(ActionInfo: ActionChain, Inputs, CollapsedOffloadAction);
6097 if (!T) {
6098 Inputs = BaseAction->getInputs();
6099 T = TC.SelectTool(JA: *BaseAction);
6100 }
6101
6102 combineWithPreprocessor(T, Inputs, CollapsedOffloadAction);
6103 return T;
6104 }
6105};
6106}
6107
6108/// Return a string that uniquely identifies the result of a job. The bound arch
6109/// is not necessarily represented in the toolchain's triple -- for example,
6110/// armv7 and armv7s both map to the same triple -- so we need both in our map.
6111/// Also, we need to add the offloading device kind, as the same tool chain can
6112/// be used for host and device for some programming models, e.g. OpenMP.
6113static std::string GetTriplePlusArchString(const ToolChain *TC, BoundArch BA,
6114 Action::OffloadKind OffloadKind) {
6115 std::string TriplePlusArch = TC->getTriple().normalize();
6116 if (!BA.empty()) {
6117 TriplePlusArch += "-";
6118 TriplePlusArch += BA.ArchName;
6119 }
6120 TriplePlusArch += "-";
6121 TriplePlusArch += Action::GetOffloadKindName(Kind: OffloadKind);
6122 return TriplePlusArch;
6123}
6124
6125InputInfoList Driver::BuildJobsForAction(
6126 Compilation &C, const Action *A, const ToolChain *TC, BoundArch BA,
6127 bool AtTopLevel, bool MultipleArchs, const char *LinkingOutput,
6128 std::map<std::pair<const Action *, std::string>, InputInfoList>
6129 &CachedResults,
6130 Action::OffloadKind TargetDeviceOffloadKind) const {
6131 std::pair<const Action *, std::string> ActionTC = {
6132 A, GetTriplePlusArchString(TC, BA, OffloadKind: TargetDeviceOffloadKind)};
6133 auto CachedResult = CachedResults.find(x: ActionTC);
6134 if (CachedResult != CachedResults.end()) {
6135 return CachedResult->second;
6136 }
6137 InputInfoList Result = BuildJobsForActionNoCache(
6138 C, A, TC, BA, AtTopLevel, MultipleArchs, LinkingOutput, CachedResults,
6139 TargetDeviceOffloadKind);
6140 CachedResults[ActionTC] = Result;
6141 return Result;
6142}
6143
6144static void handleTimeTrace(Compilation &C, const ArgList &Args,
6145 const JobAction *JA, const char *BaseInput,
6146 const InputInfo &Result) {
6147 Arg *A =
6148 Args.getLastArg(Ids: options::OPT_ftime_trace, Ids: options::OPT_ftime_trace_EQ);
6149 if (!A)
6150 return;
6151
6152 SmallString<64> OffloadingPrefix;
6153 if (JA->getOffloadingDeviceKind() != Action::OFK_None) {
6154 const ToolChain *TC = JA->getOffloadingToolChain();
6155 OffloadingPrefix = Action::GetOffloadingFileNamePrefix(
6156 Kind: JA->getOffloadingDeviceKind(), NormalizedTriple: TC ? TC->getEffectiveTriple().str() : "",
6157 /*CreatePrefixForHost=*/false);
6158 BoundArch Arch = JA->getOffloadingArch();
6159 if (!Arch.empty()) {
6160 OffloadingPrefix += "-";
6161 OffloadingPrefix += Arch.ArchName;
6162 }
6163 } else if (JA->getOffloadingHostActiveKinds() != Action::OFK_None &&
6164 C.getDriver().isSaveTempsEnabled()) {
6165 OffloadingPrefix = Action::GetOffloadingFileNamePrefix(
6166 Kind: Action::OFK_None, NormalizedTriple: C.getDefaultToolChain().getTripleString(),
6167 /*CreatePrefixForHost=*/true);
6168 }
6169
6170 SmallString<128> Path;
6171 if (A->getOption().matches(ID: options::OPT_ftime_trace_EQ)) {
6172 Path = A->getValue();
6173 if (llvm::sys::fs::is_directory(Path)) {
6174 SmallString<128> Tmp(OffloadingPrefix.empty()
6175 ? llvm::sys::path::stem(path: Result.getFilename())
6176 : llvm::sys::path::stem(path: BaseInput));
6177 Tmp += OffloadingPrefix;
6178 Tmp += ".json";
6179 llvm::sys::path::append(path&: Path, a: Tmp);
6180 }
6181 } else {
6182 if (Arg *DumpDir = Args.getLastArgNoClaim(Ids: options::OPT_dumpdir)) {
6183 // The trace file is ${dumpdir}${basename}${offloadprefix}.json. Note
6184 // that dumpdir may not end with a path separator.
6185 Path = DumpDir->getValue();
6186 Path += llvm::sys::path::stem(path: BaseInput);
6187 Path += OffloadingPrefix;
6188 Path += ".json";
6189 } else if (!OffloadingPrefix.empty()) {
6190 // For offloading, derive path from -o output directory combined with
6191 // the input filename and offload prefix.
6192 SmallString<128> TraceName(llvm::sys::path::stem(path: BaseInput));
6193 TraceName += OffloadingPrefix;
6194 if (Arg *FinalOutput = Args.getLastArg(Ids: options::OPT_o))
6195 Path = llvm::sys::path::parent_path(path: FinalOutput->getValue());
6196 llvm::sys::path::append(path&: Path, a: TraceName);
6197 Path += ".json";
6198 } else {
6199 Path = Result.getFilename();
6200 llvm::sys::path::replace_extension(path&: Path, extension: "json");
6201 }
6202 }
6203 const char *ResultFile = C.getArgs().MakeArgString(Str: Path);
6204 C.addTimeTraceFile(Name: ResultFile, JA);
6205 C.addResultFile(Name: ResultFile, JA);
6206}
6207
6208InputInfoList Driver::BuildJobsForActionNoCache(
6209 Compilation &C, const Action *A, const ToolChain *TC, BoundArch BA,
6210 bool AtTopLevel, bool MultipleArchs, const char *LinkingOutput,
6211 std::map<std::pair<const Action *, std::string>, InputInfoList>
6212 &CachedResults,
6213 Action::OffloadKind TargetDeviceOffloadKind) const {
6214 llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
6215
6216 // Track the bound arch for commands constructed in this scope so
6217 // generateCompilationDiagnostics can identify the crashing arch.
6218 BoundArch SavedBoundArch = C.getCurrentBoundArch();
6219 C.setCurrentBoundArch(BA);
6220 auto RestoreBoundArch =
6221 llvm::scope_exit([&] { C.setCurrentBoundArch(SavedBoundArch); });
6222
6223 InputInfoList OffloadDependencesInputInfo;
6224 bool BuildingForOffloadDevice = TargetDeviceOffloadKind != Action::OFK_None;
6225 if (const OffloadAction *OA = dyn_cast<OffloadAction>(Val: A)) {
6226 // The 'Darwin' toolchain is initialized only when its arguments are
6227 // computed. Get the default arguments for OFK_None to ensure that
6228 // initialization is performed before processing the offload action.
6229 // FIXME: Remove when darwin's toolchain is initialized during construction.
6230 C.getArgsForToolChain(TC, BA, DeviceOffloadKind: Action::OFK_Host);
6231
6232 // The offload action is expected to be used in four different situations.
6233 //
6234 // a) Set a toolchain/architecture/kind for a host action:
6235 // Host Action 1 -> OffloadAction -> Host Action 2
6236 //
6237 // b) Set a toolchain/architecture/kind for a device action;
6238 // Device Action 1 -> OffloadAction -> Device Action 2
6239 //
6240 // c) Specify a device dependence to a host action;
6241 // Device Action 1 _
6242 // \
6243 // Host Action 1 ---> OffloadAction -> Host Action 2
6244 //
6245 // d) Specify a host dependence to a device action.
6246 // Host Action 1 _
6247 // \
6248 // Device Action 1 ---> OffloadAction -> Device Action 2
6249 //
6250 // For a) and b), we just return the job generated for the dependences. For
6251 // c) and d) we override the current action with the host/device dependence
6252 // if the current toolchain is host/device and set the offload dependences
6253 // info with the jobs obtained from the device/host dependence(s).
6254
6255 // If there is a single device option or has no host action, just generate
6256 // the job for it.
6257 if (OA->hasSingleDeviceDependence() || !OA->hasHostDependence()) {
6258 InputInfoList DevA;
6259 OA->doOnEachDeviceDependence(Work: [&](Action *DepA, const ToolChain *DepTC,
6260 BoundArch DepBoundArch) {
6261 DevA.append(RHS: BuildJobsForAction(C, A: DepA, TC: DepTC, BA: DepBoundArch, AtTopLevel,
6262 /*MultipleArchs=*/!DepBoundArch.empty(),
6263 LinkingOutput, CachedResults,
6264 TargetDeviceOffloadKind: DepA->getOffloadingDeviceKind()));
6265 });
6266 return DevA;
6267 }
6268
6269 // If 'Action 2' is host, we generate jobs for the device dependences and
6270 // override the current action with the host dependence. Otherwise, we
6271 // generate the host dependences and override the action with the device
6272 // dependence. The dependences can't therefore be a top-level action.
6273 OA->doOnEachDependence(
6274 /*IsHostDependence=*/BuildingForOffloadDevice,
6275 Work: [&](Action *DepA, const ToolChain *DepTC, BoundArch DepBoundArch) {
6276 OffloadDependencesInputInfo.append(RHS: BuildJobsForAction(
6277 C, A: DepA, TC: DepTC, BA: DepBoundArch, /*AtTopLevel=*/false,
6278 /*MultipleArchs=*/!DepBoundArch.empty(), LinkingOutput,
6279 CachedResults, TargetDeviceOffloadKind: DepA->getOffloadingDeviceKind()));
6280 });
6281
6282 A = BuildingForOffloadDevice
6283 ? OA->getSingleDeviceDependence(/*DoNotConsiderHostActions=*/true)
6284 : OA->getHostDependence();
6285
6286 // We may have already built this action as a part of the offloading
6287 // toolchain, return the cached input if so.
6288 std::pair<const Action *, std::string> ActionTC = {
6289 OA->getHostDependence(),
6290 GetTriplePlusArchString(TC, BA, OffloadKind: TargetDeviceOffloadKind)};
6291 auto It = CachedResults.find(x: ActionTC);
6292 if (It != CachedResults.end()) {
6293 InputInfoList Inputs = It->second;
6294 Inputs.append(RHS: OffloadDependencesInputInfo);
6295 return Inputs;
6296 }
6297 }
6298
6299 if (const InputAction *IA = dyn_cast<InputAction>(Val: A)) {
6300 // FIXME: It would be nice to not claim this here; maybe the old scheme of
6301 // just using Args was better?
6302 const Arg &Input = IA->getInputArg();
6303 Input.claim();
6304 if (Input.getOption().matches(ID: options::OPT_INPUT)) {
6305 const char *Name = Input.getValue();
6306 return {InputInfo(A, Name, /* _BaseInput = */ Name)};
6307 }
6308 return {InputInfo(A, &Input, /* _BaseInput = */ "")};
6309 }
6310
6311 if (const BindArchAction *BAA = dyn_cast<BindArchAction>(Val: A)) {
6312 const ToolChain *TC;
6313 BoundArch ArchName = BAA->getArch();
6314
6315 if (!ArchName.empty())
6316 TC = &getToolChain(Args: C.getArgs(),
6317 Target: computeTargetTriple(D: *this, TargetTriple, Args: C.getArgs(),
6318 ArchName: ArchName.ArchName));
6319 else
6320 TC = &C.getDefaultToolChain();
6321
6322 return BuildJobsForAction(C, A: *BAA->input_begin(), TC, BA: ArchName, AtTopLevel,
6323 MultipleArchs, LinkingOutput, CachedResults,
6324 TargetDeviceOffloadKind);
6325 }
6326
6327
6328 ActionList Inputs = A->getInputs();
6329
6330 const JobAction *JA = cast<JobAction>(Val: A);
6331 ActionList CollapsedOffloadActions;
6332
6333 ToolSelector TS(JA, *TC, C, isSaveTempsEnabled(),
6334 embedBitcodeInObject() && !TC->isUsingLTO(Args: C.getArgs()));
6335 const Tool *T = TS.getTool(Inputs, CollapsedOffloadAction&: CollapsedOffloadActions);
6336
6337 if (!T)
6338 return {InputInfo()};
6339
6340 // If we've collapsed action list that contained OffloadAction we
6341 // need to build jobs for host/device-side inputs it may have held.
6342 for (const auto *OA : CollapsedOffloadActions)
6343 cast<OffloadAction>(Val: OA)->doOnEachDependence(
6344 /*IsHostDependence=*/BuildingForOffloadDevice,
6345 Work: [&](Action *DepA, const ToolChain *DepTC, BoundArch DepBoundArch) {
6346 OffloadDependencesInputInfo.append(RHS: BuildJobsForAction(
6347 C, A: DepA, TC: DepTC, BA: DepBoundArch, /*AtTopLevel=*/false,
6348 /*MultipleArchs=*/!DepBoundArch.empty(), LinkingOutput,
6349 CachedResults, TargetDeviceOffloadKind: DepA->getOffloadingDeviceKind()));
6350 });
6351
6352 // Only use pipes when there is exactly one input.
6353 InputInfoList InputInfos;
6354 for (const Action *Input : Inputs) {
6355 // Treat dsymutil and verify sub-jobs as being at the top-level too, they
6356 // shouldn't get temporary output names.
6357 // FIXME: Clean this up.
6358 bool SubJobAtTopLevel =
6359 AtTopLevel && (isa<DsymutilJobAction>(Val: A) || isa<VerifyJobAction>(Val: A));
6360 InputInfos.append(RHS: BuildJobsForAction(
6361 C, A: Input, TC, BA, AtTopLevel: SubJobAtTopLevel, MultipleArchs, LinkingOutput,
6362 CachedResults, TargetDeviceOffloadKind: A->getOffloadingDeviceKind()));
6363 }
6364
6365 // Always use the first file input as the base input.
6366 const char *BaseInput = InputInfos[0].getBaseInput();
6367 for (auto &Info : InputInfos) {
6368 if (Info.isFilename()) {
6369 BaseInput = Info.getBaseInput();
6370 break;
6371 }
6372 }
6373
6374 // ... except dsymutil actions, which use their actual input as the base
6375 // input.
6376 if (JA->getType() == types::TY_dSYM)
6377 BaseInput = InputInfos[0].getFilename();
6378
6379 // Append outputs of offload device jobs to the input list
6380 if (!OffloadDependencesInputInfo.empty())
6381 InputInfos.append(in_start: OffloadDependencesInputInfo.begin(),
6382 in_end: OffloadDependencesInputInfo.end());
6383
6384 // Set the effective triple of the toolchain for the duration of this job.
6385 llvm::Triple EffectiveTriple;
6386 const ToolChain &ToolTC = T->getToolChain();
6387 const ArgList &Args =
6388 C.getArgsForToolChain(TC, BA, DeviceOffloadKind: A->getOffloadingDeviceKind());
6389 if (InputInfos.size() != 1) {
6390 EffectiveTriple =
6391 llvm::Triple(ToolTC.ComputeEffectiveClangTriple(Args, BA));
6392 } else {
6393 // Pass along the input type if it can be unambiguously determined.
6394 EffectiveTriple = llvm::Triple(
6395 ToolTC.ComputeEffectiveClangTriple(Args, BA, InputType: InputInfos[0].getType()));
6396 }
6397 RegisterEffectiveTriple TripleRAII(ToolTC, EffectiveTriple);
6398
6399 // Determine the place to write output to, if any.
6400 InputInfo Result;
6401 InputInfoList UnbundlingResults;
6402 if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(Val: JA)) {
6403 // If we have an unbundling job, we need to create results for all the
6404 // outputs. We also update the results cache so that other actions using
6405 // this unbundling action can get the right results.
6406 for (auto &UI : UA->getDependentActionsInfo()) {
6407 assert(UI.DependentOffloadKind != Action::OFK_None &&
6408 "Unbundling with no offloading??");
6409
6410 // Unbundling actions are never at the top level. When we generate the
6411 // offloading prefix, we also do that for the host file because the
6412 // unbundling action does not change the type of the output which can
6413 // cause a overwrite.
6414 std::string OffloadingPrefix = Action::GetOffloadingFileNamePrefix(
6415 Kind: UI.DependentOffloadKind, NormalizedTriple: UI.DependentToolChain->getTripleString(),
6416 /*CreatePrefixForHost=*/true);
6417 auto CurI = InputInfo(
6418 UA,
6419 GetNamedOutputPath(C, JA: *UA, BaseInput, BA: UI.DependentBoundArch,
6420 /*AtTopLevel=*/false,
6421 MultipleArchs: MultipleArchs ||
6422 UI.DependentOffloadKind == Action::OFK_HIP,
6423 NormalizedTriple: OffloadingPrefix),
6424 BaseInput);
6425 // Save the unbundling result.
6426 UnbundlingResults.push_back(Elt: CurI);
6427
6428 // Get the unique string identifier for this dependence and cache the
6429 // result.
6430 BoundArch Arch;
6431 if (TargetDeviceOffloadKind == Action::OFK_HIP) {
6432 if (UI.DependentOffloadKind == Action::OFK_Host)
6433 Arch = BoundArch();
6434 else
6435 Arch = BoundArch(UI.DependentBoundArch);
6436 } else
6437 Arch = BA;
6438
6439 CachedResults[{A, GetTriplePlusArchString(TC: UI.DependentToolChain, BA: Arch,
6440 OffloadKind: UI.DependentOffloadKind)}] = {
6441 CurI};
6442 }
6443
6444 // Now that we have all the results generated, select the one that should be
6445 // returned for the current depending action.
6446 std::pair<const Action *, std::string> ActionTC = {
6447 A, GetTriplePlusArchString(TC, BA, OffloadKind: TargetDeviceOffloadKind)};
6448 assert(CachedResults.find(ActionTC) != CachedResults.end() &&
6449 "Result does not exist??");
6450 Result = CachedResults[ActionTC].front();
6451 } else if (JA->getType() == types::TY_Nothing)
6452 Result = {InputInfo(A, BaseInput)};
6453 else {
6454 // We only have to generate a prefix for the host if this is not a top-level
6455 // action.
6456 std::string OffloadingPrefix = Action::GetOffloadingFileNamePrefix(
6457 Kind: A->getOffloadingDeviceKind(), NormalizedTriple: EffectiveTriple.str(),
6458 /*CreatePrefixForHost=*/isa<OffloadPackagerJobAction>(Val: A) ||
6459 !(A->getOffloadingHostActiveKinds() == Action::OFK_None ||
6460 AtTopLevel));
6461 Result = InputInfo(A,
6462 GetNamedOutputPath(C, JA: *JA, BaseInput, BA, AtTopLevel,
6463 MultipleArchs, NormalizedTriple: OffloadingPrefix),
6464 BaseInput);
6465 if (T->canEmitIR())
6466 handleTimeTrace(C, Args, JA, BaseInput, Result);
6467 }
6468
6469 if (CCCPrintBindings && !CCGenDiagnostics) {
6470 llvm::errs() << "# \"" << T->getToolChain().getEffectiveTriple().str()
6471 << '"' << " - \"" << T->getName() << "\", inputs: [";
6472 for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
6473 llvm::errs() << InputInfos[i].getAsString();
6474 if (i + 1 != e)
6475 llvm::errs() << ", ";
6476 }
6477 if (UnbundlingResults.empty())
6478 llvm::errs() << "], output: " << Result.getAsString() << "\n";
6479 else {
6480 llvm::errs() << "], outputs: [";
6481 for (unsigned i = 0, e = UnbundlingResults.size(); i != e; ++i) {
6482 llvm::errs() << UnbundlingResults[i].getAsString();
6483 if (i + 1 != e)
6484 llvm::errs() << ", ";
6485 }
6486 llvm::errs() << "] \n";
6487 }
6488 } else {
6489 if (UnbundlingResults.empty())
6490 T->ConstructJob(C, JA: *JA, Output: Result, Inputs: InputInfos, TCArgs: Args, LinkingOutput);
6491 else
6492 T->ConstructJobMultipleOutputs(C, JA: *JA, Outputs: UnbundlingResults, Inputs: InputInfos,
6493 TCArgs: Args, LinkingOutput);
6494 }
6495 return {Result};
6496}
6497
6498const char *Driver::getDefaultImageName() const {
6499 llvm::Triple Target(llvm::Triple::normalize(Str: TargetTriple));
6500 return Target.isOSWindows() ? "a.exe" : "a.out";
6501}
6502
6503/// Create output filename based on ArgValue, which could either be a
6504/// full filename, filename without extension, or a directory. If ArgValue
6505/// does not provide a filename, then use BaseName, and use the extension
6506/// suitable for FileType.
6507static const char *MakeCLOutputFilename(const ArgList &Args, StringRef ArgValue,
6508 StringRef BaseName,
6509 types::ID FileType) {
6510 SmallString<128> Filename = ArgValue;
6511
6512 if (ArgValue.empty()) {
6513 // If the argument is empty, output to BaseName in the current dir.
6514 Filename = BaseName;
6515 } else if (llvm::sys::path::is_separator(value: Filename.back())) {
6516 // If the argument is a directory, output to BaseName in that dir.
6517 llvm::sys::path::append(path&: Filename, a: BaseName);
6518 }
6519
6520 if (!llvm::sys::path::has_extension(path: ArgValue)) {
6521 // If the argument didn't provide an extension, then set it.
6522 const char *Extension = types::getTypeTempSuffix(Id: FileType, CLStyle: true);
6523
6524 if (FileType == types::TY_Image &&
6525 Args.hasArg(Ids: options::OPT__SLASH_LD, Ids: options::OPT__SLASH_LDd)) {
6526 // The output file is a dll.
6527 Extension = "dll";
6528 }
6529
6530 llvm::sys::path::replace_extension(path&: Filename, extension: Extension);
6531 }
6532
6533 return Args.MakeArgString(Str: Filename.c_str());
6534}
6535
6536static bool HasPreprocessOutput(const Action &JA) {
6537 if (isa<PreprocessJobAction>(Val: JA))
6538 return true;
6539 if (isa<OffloadAction>(Val: JA) && isa<PreprocessJobAction>(Val: JA.getInputs()[0]))
6540 return true;
6541 if (isa<OffloadBundlingJobAction>(Val: JA) &&
6542 HasPreprocessOutput(JA: *(JA.getInputs()[0])))
6543 return true;
6544 return false;
6545}
6546
6547const char *Driver::CreateTempFile(Compilation &C, StringRef Prefix,
6548 StringRef Suffix, bool MultipleArchs,
6549 StringRef BoundArchStr,
6550 bool NeedUniqueDirectory) const {
6551 SmallString<128> TmpName;
6552 Arg *A = C.getArgs().getLastArg(Ids: options::OPT_fcrash_diagnostics_dir);
6553 std::optional<std::string> CrashDirectory =
6554 CCGenDiagnostics && A
6555 ? std::string(A->getValue())
6556 : llvm::sys::Process::GetEnv(name: "CLANG_CRASH_DIAGNOSTICS_DIR");
6557 if (CrashDirectory) {
6558 if (!getVFS().exists(Path: *CrashDirectory))
6559 llvm::sys::fs::create_directories(path: *CrashDirectory);
6560 SmallString<128> Path(*CrashDirectory);
6561 llvm::sys::path::append(path&: Path, a: Prefix);
6562 const char *Middle = !Suffix.empty() ? "-%%%%%%." : "-%%%%%%";
6563 if (std::error_code EC =
6564 llvm::sys::fs::createUniqueFile(Model: Path + Middle + Suffix, ResultPath&: TmpName)) {
6565 Diag(DiagID: clang::diag::err_unable_to_make_temp) << EC.message();
6566 return "";
6567 }
6568 } else {
6569 if (MultipleArchs && !BoundArchStr.empty()) {
6570 if (NeedUniqueDirectory) {
6571 TmpName = GetTemporaryDirectory(Prefix);
6572 llvm::sys::path::append(path&: TmpName, a: Twine(Prefix) + "-" + BoundArchStr +
6573 "." + Suffix);
6574 } else {
6575 TmpName = GetTemporaryPath(Prefix: (Twine(Prefix) + "-" + BoundArchStr).str(),
6576 Suffix);
6577 }
6578
6579 } else {
6580 TmpName = GetTemporaryPath(Prefix, Suffix);
6581 }
6582 }
6583 return C.addTempFile(Name: C.getArgs().MakeArgString(Str: TmpName));
6584}
6585
6586// Calculate the output path of the module file when compiling a module unit
6587// with the `-fmodule-output` option or `-fmodule-output=` option specified.
6588// The behavior is:
6589// - If `-fmodule-output=` is specfied, then the module file is
6590// writing to the value.
6591// - Otherwise if the output object file of the module unit is specified, the
6592// output path
6593// of the module file should be the same with the output object file except
6594// the corresponding suffix. This requires both `-o` and `-c` are specified.
6595// - Otherwise, the output path of the module file will be the same with the
6596// input with the corresponding suffix.
6597static const char *GetModuleOutputPath(Compilation &C, const JobAction &JA,
6598 const char *BaseInput) {
6599 assert(isa<PrecompileJobAction>(JA) && JA.getType() == types::TY_ModuleFile &&
6600 (C.getArgs().hasArg(options::OPT_fmodule_output) ||
6601 C.getArgs().hasArg(options::OPT_fmodule_output_EQ)));
6602
6603 SmallString<256> OutputPath =
6604 tools::getCXX20NamedModuleOutputPath(Args: C.getArgs(), BaseInput);
6605
6606 return C.addResultFile(Name: C.getArgs().MakeArgString(Str: OutputPath.c_str()), JA: &JA);
6607}
6608
6609const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA,
6610 const char *BaseInput, BoundArch BA,
6611 bool AtTopLevel, bool MultipleArchs,
6612 StringRef OffloadingPrefix) const {
6613 std::string BoundArchStr = sanitizeTargetIDInFileName(TargetID: BA.ArchName);
6614
6615 llvm::PrettyStackTraceString CrashInfo("Computing output path");
6616
6617 auto CreateTempOutputPath = [&](StringRef Prefix) {
6618 const char *Suffix =
6619 types::getTypeTempSuffix(Id: JA.getType(), CLStyle: IsCLMode() || IsDXCMode());
6620 // The non-offloading toolchain on Darwin requires deterministic input
6621 // file name for binaries to be deterministic, therefore it needs unique
6622 // directory.
6623 const llvm::Triple Triple(C.getDriver().getTargetTriple());
6624 const bool NeedUniqueDirectory =
6625 (JA.getOffloadingDeviceKind() == Action::OFK_None ||
6626 JA.getOffloadingDeviceKind() == Action::OFK_Host) &&
6627 Triple.isOSDarwin();
6628 return CreateTempFile(C, Prefix, Suffix, MultipleArchs, BoundArchStr,
6629 NeedUniqueDirectory);
6630 };
6631
6632 // Standard library output in -fmodules-driver?
6633 if (isa<PrecompileJobAction>(Val: JA) && !JA.getInputs().empty() &&
6634 (JA.getInputs().front()->getType() == types::TY_CXXStdModule ||
6635 JA.getInputs().front()->getType() == types::TY_PP_CXXStdModule)) {
6636 StringRef Filename = llvm::sys::path::filename(path: BaseInput);
6637 StringRef Stem = llvm::sys::path::stem(path: Filename);
6638 return CreateTempOutputPath(Stem);
6639 }
6640
6641 // Output to a user requested destination?
6642 if (AtTopLevel && !isa<DsymutilJobAction>(Val: JA) && !isa<VerifyJobAction>(Val: JA)) {
6643 if (Arg *FinalOutput = C.getArgs().getLastArg(Ids: options::OPT_o))
6644 return C.addResultFile(Name: FinalOutput->getValue(), JA: &JA);
6645 }
6646
6647 // For /P, preprocess to file named after BaseInput.
6648 if (C.getArgs().hasArg(Ids: options::OPT__SLASH_P)) {
6649 assert(AtTopLevel && isa<PreprocessJobAction>(JA));
6650 StringRef BaseName = llvm::sys::path::filename(path: BaseInput);
6651 StringRef NameArg;
6652 if (Arg *A = C.getArgs().getLastArg(Ids: options::OPT__SLASH_Fi))
6653 NameArg = A->getValue();
6654 return C.addResultFile(
6655 Name: MakeCLOutputFilename(Args: C.getArgs(), ArgValue: NameArg, BaseName, FileType: types::TY_PP_C),
6656 JA: &JA);
6657 }
6658
6659 // Default to writing to stdout?
6660 if (AtTopLevel && !CCGenDiagnostics && HasPreprocessOutput(JA)) {
6661 return "-";
6662 }
6663
6664 if (JA.getType() == types::TY_ModuleFile &&
6665 C.getArgs().getLastArg(Ids: options::OPT_module_file_info)) {
6666 return "-";
6667 }
6668
6669 if (JA.getType() == types::TY_PP_Asm &&
6670 C.getArgs().hasArg(Ids: options::OPT_dxc_Fc)) {
6671 StringRef FcValue = C.getArgs().getLastArgValue(Id: options::OPT_dxc_Fc);
6672 // TODO: Should we use `MakeCLOutputFilename` here? If so, we can probably
6673 // handle this as part of the SLASH_Fa handling below.
6674 return C.addResultFile(Name: C.getArgs().MakeArgString(Str: FcValue), JA: &JA);
6675 }
6676
6677 if ((JA.getType() == types::TY_Object &&
6678 C.getArgs().hasArg(Ids: options::OPT_dxc_Fo)) ||
6679 JA.getType() == types::TY_DX_CONTAINER) {
6680 StringRef FoValue = C.getArgs().getLastArgValue(Id: options::OPT_dxc_Fo);
6681 assert((C.getDefaultToolChain().getTriple().isDXIL() ||
6682 C.getDefaultToolChain().getTriple().isSPIRV()) &&
6683 "expected DXIL or SPIR-V triple for HLSL output path");
6684 const auto &TC =
6685 static_cast<const toolchains::HLSLToolChain &>(C.getDefaultToolChain());
6686 // Fo can be empty here if the validator is running for a compiler flow
6687 // that is using Fc or just printing disassembly.
6688 if (TC.isLastOutputProducingJob(Args&: C.getArgs(), AC: JA.getKind()) &&
6689 !FoValue.empty())
6690 return C.addResultFile(Name: C.getArgs().MakeArgString(Str: FoValue), JA: &JA);
6691 StringRef Name = llvm::sys::path::filename(path: BaseInput);
6692 std::pair<StringRef, StringRef> Split = Name.split(Separator: '.');
6693 const char *Suffix = types::getTypeTempSuffix(Id: JA.getType(), CLStyle: true);
6694 return CreateTempFile(C, Prefix: Split.first, Suffix, MultipleArchs: false);
6695 }
6696
6697 // Is this the assembly listing for /FA?
6698 if (JA.getType() == types::TY_PP_Asm &&
6699 (C.getArgs().hasArg(Ids: options::OPT__SLASH_FA) ||
6700 C.getArgs().hasArg(Ids: options::OPT__SLASH_Fa))) {
6701 // Use /Fa and the input filename to determine the asm file name.
6702 StringRef BaseName = llvm::sys::path::filename(path: BaseInput);
6703 StringRef FaValue = C.getArgs().getLastArgValue(Id: options::OPT__SLASH_Fa);
6704 return C.addResultFile(
6705 Name: MakeCLOutputFilename(Args: C.getArgs(), ArgValue: FaValue, BaseName, FileType: JA.getType()),
6706 JA: &JA);
6707 }
6708
6709 if (JA.getType() == types::TY_API_INFO &&
6710 C.getArgs().hasArg(Ids: options::OPT_emit_extension_symbol_graphs) &&
6711 C.getArgs().hasArg(Ids: options::OPT_o))
6712 Diag(DiagID: clang::diag::err_drv_unexpected_symbol_graph_output)
6713 << C.getArgs().getLastArgValue(Id: options::OPT_o);
6714
6715 // DXC defaults to standard out when generating assembly. We check this after
6716 // any DXC flags that might specify a file.
6717 if (AtTopLevel && JA.getType() == types::TY_PP_Asm && IsDXCMode())
6718 return "-";
6719
6720 bool SpecifiedModuleOutput =
6721 C.getArgs().hasArg(Ids: options::OPT_fmodule_output) ||
6722 C.getArgs().hasArg(Ids: options::OPT_fmodule_output_EQ);
6723 if (MultipleArchs && SpecifiedModuleOutput)
6724 Diag(DiagID: clang::diag::err_drv_module_output_with_multiple_arch);
6725
6726 // If we're emitting a module output with the specified option
6727 // `-fmodule-output`.
6728 if (!AtTopLevel && isa<PrecompileJobAction>(Val: JA) &&
6729 JA.getType() == types::TY_ModuleFile && SpecifiedModuleOutput) {
6730 assert(C.getArgs().hasArg(options::OPT_fno_modules_reduced_bmi));
6731 return GetModuleOutputPath(C, JA, BaseInput);
6732 }
6733
6734 // Output to a temporary file?
6735 if ((!AtTopLevel && !isSaveTempsEnabled() &&
6736 !C.getArgs().hasArg(Ids: options::OPT__SLASH_Fo)) ||
6737 CCGenDiagnostics) {
6738 StringRef Name = llvm::sys::path::filename(path: BaseInput);
6739 return CreateTempOutputPath(Name.split(Separator: '.').first);
6740 }
6741
6742 SmallString<128> BasePath(BaseInput);
6743 SmallString<128> ExternalPath("");
6744 StringRef BaseName;
6745
6746 // Dsymutil actions should use the full path.
6747 if (isa<DsymutilJobAction>(Val: JA) && C.getArgs().hasArg(Ids: options::OPT_dsym_dir)) {
6748 ExternalPath += C.getArgs().getLastArg(Ids: options::OPT_dsym_dir)->getValue();
6749 // We use posix style here because the tests (specifically
6750 // darwin-dsymutil.c) demonstrate that posix style paths are acceptable
6751 // even on Windows and if we don't then the similar test covering this
6752 // fails.
6753 llvm::sys::path::append(path&: ExternalPath, style: llvm::sys::path::Style::posix,
6754 a: llvm::sys::path::filename(path: BasePath));
6755 BaseName = ExternalPath;
6756 } else if (isa<DsymutilJobAction>(Val: JA) || isa<VerifyJobAction>(Val: JA))
6757 BaseName = BasePath;
6758 else
6759 BaseName = llvm::sys::path::filename(path: BasePath);
6760
6761 // Determine what the derived output name should be.
6762 const char *NamedOutput;
6763
6764 if ((JA.getType() == types::TY_Object || JA.getType() == types::TY_LTO_BC ||
6765 JA.getType() == types::TY_LLVM_BC ||
6766 JA.getType() == types::TY_LLVM_IR) &&
6767 C.getArgs().hasArg(Ids: options::OPT__SLASH_Fo, Ids: options::OPT__SLASH_o)) {
6768 // The /Fo or /o flag decides the object filename.
6769 StringRef Val =
6770 C.getArgs()
6771 .getLastArg(Ids: options::OPT__SLASH_Fo, Ids: options::OPT__SLASH_o)
6772 ->getValue();
6773 NamedOutput =
6774 MakeCLOutputFilename(Args: C.getArgs(), ArgValue: Val, BaseName, FileType: JA.getType());
6775 } else if (JA.getType() == types::TY_Image &&
6776 C.getArgs().hasArg(Ids: options::OPT__SLASH_Fe,
6777 Ids: options::OPT__SLASH_o)) {
6778 // The /Fe or /o flag names the linked file.
6779 StringRef Val =
6780 C.getArgs()
6781 .getLastArg(Ids: options::OPT__SLASH_Fe, Ids: options::OPT__SLASH_o)
6782 ->getValue();
6783 NamedOutput =
6784 MakeCLOutputFilename(Args: C.getArgs(), ArgValue: Val, BaseName, FileType: types::TY_Image);
6785 } else if (JA.getType() == types::TY_Image) {
6786 if (IsCLMode()) {
6787 // clang-cl uses BaseName for the executable name.
6788 NamedOutput =
6789 MakeCLOutputFilename(Args: C.getArgs(), ArgValue: "", BaseName, FileType: types::TY_Image);
6790 } else {
6791 SmallString<128> Output(getDefaultImageName());
6792 // HIP image for device compilation with -fno-gpu-rdc is per compilation
6793 // unit.
6794 bool IsHIPNoRDC = JA.getOffloadingDeviceKind() == Action::OFK_HIP &&
6795 !C.getArgs().hasFlag(Pos: options::OPT_fgpu_rdc,
6796 Neg: options::OPT_fno_gpu_rdc, Default: false);
6797 bool UseOutExtension = IsHIPNoRDC || isa<OffloadPackagerJobAction>(Val: JA);
6798 if (UseOutExtension) {
6799 Output = BaseName;
6800 llvm::sys::path::replace_extension(path&: Output, extension: "");
6801 }
6802 Output += OffloadingPrefix;
6803 if (MultipleArchs && !BoundArchStr.empty()) {
6804 Output += "-";
6805 Output.append(RHS: BoundArchStr);
6806 }
6807 if (UseOutExtension)
6808 Output += ".out";
6809 NamedOutput = C.getArgs().MakeArgString(Str: Output.c_str());
6810 }
6811 } else if (JA.getType() == types::TY_PCH && IsCLMode()) {
6812 NamedOutput = C.getArgs().MakeArgString(Str: GetClPchPath(C, BaseName));
6813 } else if ((JA.getType() == types::TY_Plist || JA.getType() == types::TY_AST) &&
6814 C.getArgs().hasArg(Ids: options::OPT__SLASH_o)) {
6815 StringRef Val =
6816 C.getArgs()
6817 .getLastArg(Ids: options::OPT__SLASH_o)
6818 ->getValue();
6819 NamedOutput =
6820 MakeCLOutputFilename(Args: C.getArgs(), ArgValue: Val, BaseName, FileType: types::TY_Object);
6821 } else {
6822 const char *Suffix =
6823 types::getTypeTempSuffix(Id: JA.getType(), CLStyle: IsCLMode() || IsDXCMode());
6824 assert(Suffix && "All types used for output should have a suffix.");
6825
6826 std::string::size_type End = std::string::npos;
6827 if (!types::appendSuffixForType(Id: JA.getType()))
6828 End = BaseName.rfind(C: '.');
6829 SmallString<128> Suffixed(BaseName.substr(Start: 0, N: End));
6830 Suffixed += OffloadingPrefix;
6831 if (MultipleArchs && !BoundArchStr.empty()) {
6832 Suffixed += "-";
6833 Suffixed.append(RHS: BoundArchStr);
6834 }
6835 // When using both -save-temps and -emit-llvm, use a ".tmp.bc" suffix for
6836 // the unoptimized bitcode so that it does not get overwritten by the ".bc"
6837 // optimized bitcode output.
6838 auto IsAMDRDCInCompilePhase = [](const JobAction &JA,
6839 const llvm::opt::DerivedArgList &Args) {
6840 // The relocatable compilation in HIP and OpenMP implies -emit-llvm.
6841 // Similarly, use a ".tmp.bc" suffix for the unoptimized bitcode
6842 // (generated in the compile phase.)
6843 const ToolChain *TC = JA.getOffloadingToolChain();
6844 return isa<CompileJobAction>(Val: JA) &&
6845 ((JA.getOffloadingDeviceKind() == Action::OFK_HIP &&
6846 (Args.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc,
6847 Default: false) ||
6848 Args.hasFlag(Pos: options::OPT_offload_new_driver,
6849 Neg: options::OPT_no_offload_new_driver, Default: true))) ||
6850 (JA.getOffloadingDeviceKind() == Action::OFK_OpenMP && TC &&
6851 TC->getTriple().isAMDGPU()));
6852 };
6853
6854 // The linker wrapper may not support the input and output files to be the
6855 // same one, and without it -save-temps can fail.
6856 bool IsLinkerWrapper =
6857 JA.getType() == types::TY_Object && isa<LinkerWrapperJobAction>(Val: JA);
6858 bool IsEmitBitcode = JA.getType() == types::TY_LLVM_BC &&
6859 (C.getArgs().hasArg(Ids: options::OPT_emit_llvm) ||
6860 IsAMDRDCInCompilePhase(JA, C.getArgs()));
6861
6862 if (!AtTopLevel && (IsLinkerWrapper || IsEmitBitcode))
6863 Suffixed += ".tmp";
6864 Suffixed += '.';
6865 Suffixed += Suffix;
6866 NamedOutput = C.getArgs().MakeArgString(Str: Suffixed.c_str());
6867 }
6868
6869 // Prepend object file path if -save-temps=obj
6870 if (!AtTopLevel && isSaveTempsObj() && C.getArgs().hasArg(Ids: options::OPT_o) &&
6871 JA.getType() != types::TY_PCH) {
6872 Arg *FinalOutput = C.getArgs().getLastArg(Ids: options::OPT_o);
6873 SmallString<128> TempPath(FinalOutput->getValue());
6874 llvm::sys::path::remove_filename(path&: TempPath);
6875 StringRef OutputFileName = llvm::sys::path::filename(path: NamedOutput);
6876 llvm::sys::path::append(path&: TempPath, a: OutputFileName);
6877 NamedOutput = C.getArgs().MakeArgString(Str: TempPath.c_str());
6878 }
6879
6880 // If we're saving temps and the temp file conflicts with the input file,
6881 // then avoid overwriting input file.
6882 if (!AtTopLevel && isSaveTempsEnabled() && NamedOutput == BaseName) {
6883 bool SameFile = false;
6884 SmallString<256> Result;
6885 llvm::sys::fs::current_path(result&: Result);
6886 llvm::sys::path::append(path&: Result, a: BaseName);
6887 llvm::sys::fs::equivalent(A: BaseInput, B: Result.c_str(), result&: SameFile);
6888 // Must share the same path to conflict.
6889 if (SameFile) {
6890 StringRef Name = llvm::sys::path::filename(path: BaseInput);
6891 std::pair<StringRef, StringRef> Split = Name.split(Separator: '.');
6892 std::string TmpName = GetTemporaryPath(
6893 Prefix: Split.first,
6894 Suffix: types::getTypeTempSuffix(Id: JA.getType(), CLStyle: IsCLMode() || IsDXCMode()));
6895 return C.addTempFile(Name: C.getArgs().MakeArgString(Str: TmpName));
6896 }
6897 }
6898
6899 // As an annoying special case, PCH generation doesn't strip the pathname.
6900 if (JA.getType() == types::TY_PCH && !IsCLMode()) {
6901 llvm::sys::path::remove_filename(path&: BasePath);
6902 if (BasePath.empty())
6903 BasePath = NamedOutput;
6904 else
6905 llvm::sys::path::append(path&: BasePath, a: NamedOutput);
6906 return C.addResultFile(Name: C.getArgs().MakeArgString(Str: BasePath.c_str()), JA: &JA);
6907 }
6908
6909 return C.addResultFile(Name: NamedOutput, JA: &JA);
6910}
6911
6912std::string Driver::GetFilePath(StringRef Name, const ToolChain &TC) const {
6913 // Search for Name in a list of paths.
6914 auto SearchPaths = [&](const llvm::SmallVectorImpl<std::string> &P)
6915 -> std::optional<std::string> {
6916 // Respect a limited subset of the '-Bprefix' functionality in GCC by
6917 // attempting to use this prefix when looking for file paths.
6918 for (const auto &Dir : P) {
6919 if (Dir.empty())
6920 continue;
6921 SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(pos: 1) : Dir);
6922 llvm::sys::path::append(path&: P, a: Name);
6923 if (llvm::sys::fs::exists(Path: Twine(P)))
6924 return std::string(P);
6925 }
6926 return std::nullopt;
6927 };
6928
6929 if (auto P = SearchPaths(PrefixDirs))
6930 return *P;
6931
6932 SmallString<128> R(ResourceDir);
6933 llvm::sys::path::append(path&: R, a: Name);
6934 if (llvm::sys::fs::exists(Path: Twine(R)))
6935 return std::string(R);
6936
6937 SmallString<128> P(TC.getCompilerRTPath());
6938 llvm::sys::path::append(path&: P, a: Name);
6939 if (llvm::sys::fs::exists(Path: Twine(P)))
6940 return std::string(P);
6941
6942 // With Flang, also look for intrinsic modules
6943 if (IsFlangMode()) {
6944 if (std::optional<std::string> IntrPath =
6945 TC.getDefaultIntrinsicModuleDir()) {
6946 SmallString<128> P(*IntrPath);
6947 llvm::sys::path::append(path&: P, a: Name);
6948 if (llvm::sys::fs::exists(Path: P))
6949 return std::string(P);
6950 }
6951 }
6952
6953 SmallString<128> D(Dir);
6954 llvm::sys::path::append(path&: D, a: "..", b: Name);
6955 if (llvm::sys::fs::exists(Path: Twine(D)))
6956 return std::string(D);
6957
6958 if (auto P = SearchPaths(TC.getLibraryPaths()))
6959 return *P;
6960
6961 if (auto P = SearchPaths(TC.getFilePaths()))
6962 return *P;
6963
6964 SmallString<128> R2(ResourceDir);
6965 llvm::sys::path::append(path&: R2, a: "..", b: "..", c: Name);
6966 if (llvm::sys::fs::exists(Path: Twine(R2)))
6967 return std::string(R2);
6968
6969 return std::string(Name);
6970}
6971
6972void Driver::generatePrefixedToolNames(
6973 StringRef Tool, const ToolChain &TC,
6974 SmallVectorImpl<std::string> &Names) const {
6975 // FIXME: Needs a better variable than TargetTriple
6976 Names.emplace_back(Args: (TargetTriple + "-" + Tool).str());
6977 Names.emplace_back(Args&: Tool);
6978}
6979
6980static bool ScanDirForExecutable(SmallString<128> &Dir, StringRef Name) {
6981 llvm::sys::path::append(path&: Dir, a: Name);
6982 if (llvm::sys::fs::can_execute(Path: Twine(Dir)))
6983 return true;
6984 llvm::sys::path::remove_filename(path&: Dir);
6985 return false;
6986}
6987
6988std::string Driver::GetProgramPath(StringRef Name, const ToolChain &TC) const {
6989 SmallVector<std::string, 2> TargetSpecificExecutables;
6990 generatePrefixedToolNames(Tool: Name, TC, Names&: TargetSpecificExecutables);
6991
6992 // Respect a limited subset of the '-Bprefix' functionality in GCC by
6993 // attempting to use this prefix when looking for program paths.
6994 for (const auto &PrefixDir : PrefixDirs) {
6995 if (llvm::sys::fs::is_directory(Path: PrefixDir)) {
6996 SmallString<128> P(PrefixDir);
6997 if (ScanDirForExecutable(Dir&: P, Name))
6998 return std::string(P);
6999 } else {
7000 SmallString<128> P((PrefixDir + Name).str());
7001 if (llvm::sys::fs::can_execute(Path: Twine(P)))
7002 return std::string(P);
7003 }
7004 }
7005
7006 const ToolChain::path_list &List = TC.getProgramPaths();
7007 for (const auto &TargetSpecificExecutable : TargetSpecificExecutables) {
7008 // For each possible name of the tool look for it in
7009 // program paths first, then the path.
7010 // Higher priority names will be first, meaning that
7011 // a higher priority name in the path will be found
7012 // instead of a lower priority name in the program path.
7013 // E.g. <triple>-gcc on the path will be found instead
7014 // of gcc in the program path
7015 for (const auto &Path : List) {
7016 SmallString<128> P(Path);
7017 if (ScanDirForExecutable(Dir&: P, Name: TargetSpecificExecutable))
7018 return std::string(P);
7019 }
7020
7021 // Fall back to the path
7022 if (llvm::ErrorOr<std::string> P =
7023 llvm::sys::findProgramByName(Name: TargetSpecificExecutable))
7024 return *P;
7025 }
7026
7027 return std::string(Name);
7028}
7029
7030std::string Driver::GetStdModuleManifestPath(const Compilation &C,
7031 const ToolChain &TC) const {
7032 std::string error = "<NOT PRESENT>";
7033
7034 if (C.getArgs().hasArg(Ids: options::OPT_nostdlib))
7035 return error;
7036
7037 switch (TC.GetCXXStdlibType(Args: C.getArgs())) {
7038 case ToolChain::CST_Libcxx: {
7039 auto evaluate = [&](const char *library) -> std::optional<std::string> {
7040 std::string lib = GetFilePath(Name: library, TC);
7041
7042 // Note when there are multiple flavours of libc++ the module json needs
7043 // to look at the command-line arguments for the proper json. These
7044 // flavours do not exist at the moment, but there are plans to provide a
7045 // variant that is built with sanitizer instrumentation enabled.
7046
7047 // For example
7048 // StringRef modules = [&] {
7049 // const SanitizerArgs &Sanitize = TC.getSanitizerArgs(C.getArgs());
7050 // if (Sanitize.needsAsanRt())
7051 // return "libc++.modules-asan.json";
7052 // return "libc++.modules.json";
7053 // }();
7054
7055 SmallString<128> path(lib.begin(), lib.end());
7056 llvm::sys::path::remove_filename(path);
7057 llvm::sys::path::append(path, a: "libc++.modules.json");
7058 if (TC.getVFS().exists(Path: path))
7059 return static_cast<std::string>(path);
7060
7061 return {};
7062 };
7063
7064 if (std::optional<std::string> result = evaluate("libc++.so"); result)
7065 return *result;
7066
7067 return evaluate("libc++.a").value_or(u&: error);
7068 }
7069
7070 case ToolChain::CST_Libstdcxx: {
7071 auto evaluate = [&](const char *library) -> std::optional<std::string> {
7072 std::string lib = GetFilePath(Name: library, TC);
7073
7074 SmallString<128> path(lib.begin(), lib.end());
7075 llvm::sys::path::remove_filename(path);
7076 llvm::sys::path::append(path, a: "libstdc++.modules.json");
7077 if (TC.getVFS().exists(Path: path))
7078 return static_cast<std::string>(path);
7079
7080 return {};
7081 };
7082
7083 if (std::optional<std::string> result = evaluate("libstdc++.so"); result)
7084 return *result;
7085
7086 return evaluate("libstdc++.a").value_or(u&: error);
7087 }
7088 }
7089
7090 return error;
7091}
7092
7093std::string Driver::GetTemporaryPath(StringRef Prefix, StringRef Suffix) const {
7094 SmallString<128> Path;
7095 std::error_code EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, ResultPath&: Path);
7096 if (EC) {
7097 Diag(DiagID: clang::diag::err_unable_to_make_temp) << EC.message();
7098 return "";
7099 }
7100
7101 return std::string(Path);
7102}
7103
7104std::string Driver::GetTemporaryDirectory(StringRef Prefix) const {
7105 SmallString<128> Path;
7106 std::error_code EC = llvm::sys::fs::createUniqueDirectory(Prefix, ResultPath&: Path);
7107 if (EC) {
7108 Diag(DiagID: clang::diag::err_unable_to_make_temp) << EC.message();
7109 return "";
7110 }
7111
7112 return std::string(Path);
7113}
7114
7115std::string Driver::GetClPchPath(Compilation &C, StringRef BaseName) const {
7116 SmallString<128> Output;
7117 if (Arg *FpArg = C.getArgs().getLastArg(Ids: options::OPT__SLASH_Fp)) {
7118 // FIXME: If anybody needs it, implement this obscure rule:
7119 // "If you specify a directory without a file name, the default file name
7120 // is VCx0.pch., where x is the major version of Visual C++ in use."
7121 Output = FpArg->getValue();
7122
7123 // "If you do not specify an extension as part of the path name, an
7124 // extension of .pch is assumed. "
7125 if (!llvm::sys::path::has_extension(path: Output))
7126 Output += ".pch";
7127 } else {
7128 if (Arg *YcArg = C.getArgs().getLastArg(Ids: options::OPT__SLASH_Yc))
7129 Output = YcArg->getValue();
7130 if (Output.empty())
7131 Output = BaseName;
7132 llvm::sys::path::replace_extension(path&: Output, extension: ".pch");
7133 }
7134 return std::string(Output);
7135}
7136
7137const ToolChain &Driver::getOffloadToolChain(
7138 const llvm::opt::ArgList &Args, const Action::OffloadKind Kind,
7139 const llvm::Triple &Target, const llvm::Triple &AuxTarget) const {
7140 std::unique_ptr<ToolChain> &TC =
7141 ToolChains[Target.str() + "/" + AuxTarget.str()];
7142 std::unique_ptr<ToolChain> &HostTC = ToolChains[AuxTarget.str()];
7143
7144 assert(HostTC && "Host toolchain for offloading doesn't exit?");
7145 if (!TC) {
7146 // Detect the toolchain based off of the target operating system.
7147 switch (Target.getOS()) {
7148 case llvm::Triple::CUDA:
7149 TC = std::make_unique<toolchains::CudaToolChain>(args: *this, args: Target, args&: *HostTC,
7150 args: Args);
7151 break;
7152 case llvm::Triple::AMDHSA:
7153 // For AMDHSA offloading (HIP, OpenMP), use the unified AMDGPUToolChain
7154 // This handles both amdgpu-amd-amdhsa and spirv64-amd-amdhsa
7155 // FIXME: This should not key off language or OS.
7156 if (Kind == Action::OFK_HIP || Kind == Action::OFK_OpenMP ||
7157 Kind == Action::OFK_Cuda)
7158 TC = std::make_unique<toolchains::AMDGPUToolChain>(args: *this, args: Target, args: Args,
7159 args: HostTC.get(), args: Kind);
7160 break;
7161 default:
7162 break;
7163 }
7164 }
7165 if (!TC) {
7166 // Detect the toolchain based off of the target architecture if that failed.
7167 switch (Target.getArch()) {
7168 case llvm::Triple::amdgpu:
7169 case llvm::Triple::r600:
7170 TC = std::make_unique<toolchains::AMDGPUToolChain>(args: *this, args: Target, args: Args,
7171 args: HostTC.get(), args: Kind);
7172 break;
7173 case llvm::Triple::spir:
7174 case llvm::Triple::spir64:
7175 case llvm::Triple::spirv:
7176 case llvm::Triple::spirv32:
7177 case llvm::Triple::spirv64:
7178 switch (Kind) {
7179 case Action::OFK_SYCL:
7180 TC = std::make_unique<toolchains::SYCLToolChain>(args: *this, args: Target, args&: *HostTC,
7181 args: Args);
7182 break;
7183 case Action::OFK_HIP:
7184 TC = std::make_unique<toolchains::HIPSPVToolChain>(args: *this, args: Target,
7185 args&: *HostTC, args: Args);
7186 break;
7187 case Action::OFK_OpenMP:
7188 TC = std::make_unique<toolchains::SPIRVOpenMPToolChain>(args: *this, args: Target,
7189 args&: *HostTC, args: Args);
7190 break;
7191 case Action::OFK_Cuda:
7192 TC = std::make_unique<toolchains::CudaToolChain>(args: *this, args: Target, args&: *HostTC,
7193 args: Args);
7194 break;
7195 default:
7196 break;
7197 }
7198 break;
7199 default:
7200 break;
7201 }
7202 }
7203
7204 // If all else fails, just look up the normal toolchain for the target.
7205 if (!TC)
7206 return getToolChain(Args, Target);
7207 return *TC;
7208}
7209
7210const ToolChain &Driver::getToolChain(const ArgList &Args,
7211 const llvm::Triple &Target) const {
7212
7213 auto &TC = ToolChains[Target.str()];
7214 if (!TC) {
7215 switch (Target.getOS()) {
7216 case llvm::Triple::AIX:
7217 TC = std::make_unique<toolchains::AIX>(args: *this, args: Target, args: Args);
7218 break;
7219 case llvm::Triple::Haiku:
7220 TC = std::make_unique<toolchains::Haiku>(args: *this, args: Target, args: Args);
7221 break;
7222 case llvm::Triple::Darwin:
7223 case llvm::Triple::MacOSX:
7224 case llvm::Triple::IOS:
7225 case llvm::Triple::TvOS:
7226 case llvm::Triple::WatchOS:
7227 case llvm::Triple::XROS:
7228 case llvm::Triple::DriverKit:
7229 TC = std::make_unique<toolchains::DarwinClang>(args: *this, args: Target, args: Args);
7230 break;
7231 case llvm::Triple::DragonFly:
7232 TC = std::make_unique<toolchains::DragonFly>(args: *this, args: Target, args: Args);
7233 break;
7234 case llvm::Triple::OpenBSD:
7235 TC = std::make_unique<toolchains::OpenBSD>(args: *this, args: Target, args: Args);
7236 break;
7237 case llvm::Triple::NetBSD:
7238 TC = std::make_unique<toolchains::NetBSD>(args: *this, args: Target, args: Args);
7239 break;
7240 case llvm::Triple::FreeBSD:
7241 if (Target.isPPC())
7242 TC = std::make_unique<toolchains::PPCFreeBSDToolChain>(args: *this, args: Target,
7243 args: Args);
7244 else
7245 TC = std::make_unique<toolchains::FreeBSD>(args: *this, args: Target, args: Args);
7246 break;
7247 case llvm::Triple::Linux:
7248 case llvm::Triple::ELFIAMCU:
7249 if (Target.getArch() == llvm::Triple::hexagon)
7250 TC = std::make_unique<toolchains::HexagonToolChain>(args: *this, args: Target,
7251 args: Args);
7252 else if ((Target.getVendor() == llvm::Triple::MipsTechnologies) &&
7253 !Target.hasEnvironment())
7254 TC = std::make_unique<toolchains::MipsLLVMToolChain>(args: *this, args: Target,
7255 args: Args);
7256 else if (Target.isPPC())
7257 TC = std::make_unique<toolchains::PPCLinuxToolChain>(args: *this, args: Target,
7258 args: Args);
7259 else if (Target.getArch() == llvm::Triple::ve)
7260 TC = std::make_unique<toolchains::VEToolChain>(args: *this, args: Target, args: Args);
7261 else if (Target.isOHOSFamily())
7262 TC = std::make_unique<toolchains::OHOS>(args: *this, args: Target, args: Args);
7263 else if (Target.isWALI())
7264 TC = std::make_unique<toolchains::WebAssembly>(args: *this, args: Target, args: Args);
7265 else if (Target.isLFI())
7266 TC = std::make_unique<toolchains::LFILinux>(args: *this, args: Target, args: Args);
7267 else
7268 TC = std::make_unique<toolchains::Linux>(args: *this, args: Target, args: Args);
7269 break;
7270 case llvm::Triple::Fuchsia:
7271 TC = std::make_unique<toolchains::Fuchsia>(args: *this, args: Target, args: Args);
7272 break;
7273 case llvm::Triple::Managarm:
7274 TC = std::make_unique<toolchains::Managarm>(args: *this, args: Target, args: Args);
7275 break;
7276 case llvm::Triple::Serenity:
7277 TC = std::make_unique<toolchains::Serenity>(args: *this, args: Target, args: Args);
7278 break;
7279 case llvm::Triple::Solaris:
7280 TC = std::make_unique<toolchains::Solaris>(args: *this, args: Target, args: Args);
7281 break;
7282 case llvm::Triple::CUDA:
7283 TC = std::make_unique<toolchains::NVPTXToolChain>(args: *this, args: Target, args: Args);
7284 break;
7285 case llvm::Triple::AMDHSA: {
7286 if (Target.getArch() == llvm::Triple::spirv64) {
7287 TC = std::make_unique<toolchains::SPIRVAMDToolChain>(args: *this, args: Target,
7288 args: Args);
7289 } else {
7290 // Only link device libraries for OpenCL and LLVM IR inputs
7291 bool ShouldLinkDeviceLibs = usesInput(Args, Fn&: types::isOpenCL) ||
7292 usesInput(Args, Fn&: types::isLLVMIR);
7293 TC = std::make_unique<toolchains::AMDGPUToolChain>(
7294 args: *this, args: Target, args: Args, args: nullptr, args: Action::OFK_None,
7295 args&: ShouldLinkDeviceLibs);
7296 }
7297 break;
7298 }
7299 case llvm::Triple::AMDPAL:
7300 case llvm::Triple::Mesa3D:
7301 TC = std::make_unique<toolchains::AMDGPUToolChain>(args: *this, args: Target, args: Args);
7302 break;
7303 case llvm::Triple::UEFI:
7304 TC = std::make_unique<toolchains::UEFI>(args: *this, args: Target, args: Args);
7305 break;
7306 case llvm::Triple::Win32:
7307 switch (Target.getEnvironment()) {
7308 default:
7309 if (Target.isOSBinFormatELF())
7310 TC = std::make_unique<toolchains::Generic_ELF>(args: *this, args: Target, args: Args);
7311 else if (Target.isOSBinFormatMachO())
7312 TC = std::make_unique<toolchains::MachO>(args: *this, args: Target, args: Args);
7313 else
7314 TC = std::make_unique<toolchains::Generic_GCC>(args: *this, args: Target, args: Args);
7315 break;
7316 case llvm::Triple::GNU:
7317 TC = std::make_unique<toolchains::MinGW>(args: *this, args: Target, args: Args);
7318 break;
7319 case llvm::Triple::Cygnus:
7320 TC = std::make_unique<toolchains::Cygwin>(args: *this, args: Target, args: Args);
7321 break;
7322 case llvm::Triple::Itanium:
7323 TC = std::make_unique<toolchains::CrossWindowsToolChain>(args: *this, args: Target,
7324 args: Args);
7325 break;
7326 case llvm::Triple::MSVC:
7327 case llvm::Triple::UnknownEnvironment:
7328 if (Args.getLastArgValue(Id: options::OPT_fuse_ld_EQ)
7329 .starts_with_insensitive(Prefix: "bfd"))
7330 TC = std::make_unique<toolchains::CrossWindowsToolChain>(
7331 args: *this, args: Target, args: Args);
7332 else
7333 TC =
7334 std::make_unique<toolchains::MSVCToolChain>(args: *this, args: Target, args: Args);
7335 break;
7336 }
7337 break;
7338 case llvm::Triple::PS4:
7339 TC = std::make_unique<toolchains::PS4CPU>(args: *this, args: Target, args: Args);
7340 break;
7341 case llvm::Triple::PS5:
7342 TC = std::make_unique<toolchains::PS5CPU>(args: *this, args: Target, args: Args);
7343 break;
7344 case llvm::Triple::Hurd:
7345 TC = std::make_unique<toolchains::Hurd>(args: *this, args: Target, args: Args);
7346 break;
7347 case llvm::Triple::LiteOS:
7348 TC = std::make_unique<toolchains::OHOS>(args: *this, args: Target, args: Args);
7349 break;
7350 case llvm::Triple::ZOS:
7351 TC = std::make_unique<toolchains::ZOS>(args: *this, args: Target, args: Args);
7352 break;
7353 case llvm::Triple::Vulkan:
7354 case llvm::Triple::ShaderModel:
7355 if ((Target.getArch() == llvm::Triple::spirv32 ||
7356 Target.getArch() == llvm::Triple::spirv64) &&
7357 !usesInput(Args, Fn&: types::isHLSL))
7358 TC = std::make_unique<toolchains::SPIRVToolChain>(args: *this, args: Target, args: Args);
7359 else
7360 TC = std::make_unique<toolchains::HLSLToolChain>(args: *this, args: Target, args: Args);
7361 break;
7362 case llvm::Triple::ChipStar:
7363 TC = std::make_unique<toolchains::HIPSPVToolChain>(args: *this, args: Target, args: Args);
7364 break;
7365 default:
7366 // Of these targets, Hexagon is the only one that might have
7367 // an OS of Linux, in which case it got handled above already.
7368 switch (Target.getArch()) {
7369 case llvm::Triple::tce:
7370 TC = std::make_unique<toolchains::TCEToolChain>(args: *this, args: Target, args: Args);
7371 break;
7372 case llvm::Triple::tcele:
7373 TC = std::make_unique<toolchains::TCELEToolChain>(args: *this, args: Target, args: Args);
7374 break;
7375 case llvm::Triple::tcele64:
7376 TC =
7377 std::make_unique<toolchains::TCELE64ToolChain>(args: *this, args: Target, args: Args);
7378 break;
7379 case llvm::Triple::hexagon:
7380 TC = std::make_unique<toolchains::HexagonToolChain>(args: *this, args: Target,
7381 args: Args);
7382 break;
7383 case llvm::Triple::lanai:
7384 TC = std::make_unique<toolchains::LanaiToolChain>(args: *this, args: Target, args: Args);
7385 break;
7386 case llvm::Triple::xcore:
7387 TC = std::make_unique<toolchains::XCoreToolChain>(args: *this, args: Target, args: Args);
7388 break;
7389 case llvm::Triple::wasm32:
7390 case llvm::Triple::wasm64:
7391 TC = std::make_unique<toolchains::WebAssembly>(args: *this, args: Target, args: Args);
7392 break;
7393 case llvm::Triple::avr:
7394 TC = std::make_unique<toolchains::AVRToolChain>(args: *this, args: Target, args: Args);
7395 break;
7396 case llvm::Triple::msp430:
7397 TC = std::make_unique<toolchains::MSP430ToolChain>(args: *this, args: Target, args: Args);
7398 break;
7399 case llvm::Triple::riscv32:
7400 case llvm::Triple::riscv64:
7401 case llvm::Triple::riscv32be:
7402 case llvm::Triple::riscv64be:
7403 TC = std::make_unique<toolchains::BareMetal>(args: *this, args: Target, args: Args);
7404 break;
7405 case llvm::Triple::ve:
7406 TC = std::make_unique<toolchains::VEToolChain>(args: *this, args: Target, args: Args);
7407 break;
7408 case llvm::Triple::spirv32:
7409 case llvm::Triple::spirv64:
7410 TC = std::make_unique<toolchains::SPIRVToolChain>(args: *this, args: Target, args: Args);
7411 break;
7412 case llvm::Triple::csky:
7413 TC = std::make_unique<toolchains::CSKYToolChain>(args: *this, args: Target, args: Args);
7414 break;
7415 case llvm::Triple::amdgpu:
7416 case llvm::Triple::r600:
7417 TC = std::make_unique<toolchains::AMDGPUToolChain>(args: *this, args: Target, args: Args);
7418 break;
7419 default:
7420 if (toolchains::BareMetal::handlesTarget(Triple: Target))
7421 TC = std::make_unique<toolchains::BareMetal>(args: *this, args: Target, args: Args);
7422 else if (Target.isOSBinFormatELF())
7423 TC = std::make_unique<toolchains::Generic_ELF>(args: *this, args: Target, args: Args);
7424 else if (Target.isAppleFirmware())
7425 TC = std::make_unique<toolchains::DarwinClang>(args: *this, args: Target, args: Args);
7426 else if (Target.isAppleMachO())
7427 TC = std::make_unique<toolchains::AppleMachO>(args: *this, args: Target, args: Args);
7428 else if (Target.isOSBinFormatMachO())
7429 TC = std::make_unique<toolchains::MachO>(args: *this, args: Target, args: Args);
7430 else
7431 TC = std::make_unique<toolchains::Generic_GCC>(args: *this, args: Target, args: Args);
7432 }
7433 }
7434 }
7435
7436 return *TC;
7437}
7438
7439bool Driver::ShouldUseClangCompiler(const JobAction &JA) const {
7440 // Say "no" if there is not exactly one input of a type clang understands.
7441 if (JA.size() != 1 ||
7442 !types::isAcceptedByClang(Id: (*JA.input_begin())->getType()))
7443 return false;
7444
7445 // And say "no" if this is not a kind of action clang understands.
7446 if (!isa<PreprocessJobAction>(Val: JA) && !isa<PrecompileJobAction>(Val: JA) &&
7447 !isa<CompileJobAction>(Val: JA) && !isa<BackendJobAction>(Val: JA) &&
7448 !isa<ExtractAPIJobAction>(Val: JA))
7449 return false;
7450
7451 return true;
7452}
7453
7454bool Driver::ShouldUseFlangCompiler(const JobAction &JA) const {
7455 // Say "no" if there is not exactly one input of a type flang understands.
7456 if (JA.size() != 1 ||
7457 !types::isAcceptedByFlang(Id: (*JA.input_begin())->getType()))
7458 return false;
7459
7460 // And say "no" if this is not a kind of action flang understands.
7461 if (!isa<PreprocessJobAction>(Val: JA) && !isa<PrecompileJobAction>(Val: JA) &&
7462 !isa<CompileJobAction>(Val: JA) && !isa<BackendJobAction>(Val: JA))
7463 return false;
7464
7465 return true;
7466}
7467
7468bool Driver::ShouldEmitStaticLibrary(const ArgList &Args) const {
7469 // Only emit static library if the flag is set explicitly.
7470 if (Args.hasArg(Ids: options::OPT_emit_static_lib))
7471 return true;
7472 return false;
7473}
7474
7475/// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the
7476/// grouped values as integers. Numbers which are not provided are set to 0.
7477///
7478/// \return True if the entire string was parsed (9.2), or all groups were
7479/// parsed (10.3.5extrastuff).
7480bool Driver::GetReleaseVersion(StringRef Str, unsigned &Major, unsigned &Minor,
7481 unsigned &Micro, bool &HadExtra) {
7482 HadExtra = false;
7483
7484 Major = Minor = Micro = 0;
7485 if (Str.empty())
7486 return false;
7487
7488 if (Str.consumeInteger(Radix: 10, Result&: Major))
7489 return false;
7490 if (Str.empty())
7491 return true;
7492 if (!Str.consume_front(Prefix: "."))
7493 return false;
7494
7495 if (Str.consumeInteger(Radix: 10, Result&: Minor))
7496 return false;
7497 if (Str.empty())
7498 return true;
7499 if (!Str.consume_front(Prefix: "."))
7500 return false;
7501
7502 if (Str.consumeInteger(Radix: 10, Result&: Micro))
7503 return false;
7504 if (!Str.empty())
7505 HadExtra = true;
7506 return true;
7507}
7508
7509/// Parse digits from a string \p Str and fulfill \p Digits with
7510/// the parsed numbers. This method assumes that the max number of
7511/// digits to look for is equal to Digits.size().
7512///
7513/// \return True if the entire string was parsed and there are
7514/// no extra characters remaining at the end.
7515bool Driver::GetReleaseVersion(StringRef Str,
7516 MutableArrayRef<unsigned> Digits) {
7517 if (Str.empty())
7518 return false;
7519
7520 unsigned CurDigit = 0;
7521 while (CurDigit < Digits.size()) {
7522 unsigned Digit;
7523 if (Str.consumeInteger(Radix: 10, Result&: Digit))
7524 return false;
7525 Digits[CurDigit] = Digit;
7526 if (Str.empty())
7527 return true;
7528 if (!Str.consume_front(Prefix: "."))
7529 return false;
7530 CurDigit++;
7531 }
7532
7533 // More digits than requested, bail out...
7534 return false;
7535}
7536
7537llvm::opt::Visibility
7538Driver::getOptionVisibilityMask(bool UseDriverMode) const {
7539 if (!UseDriverMode)
7540 return llvm::opt::Visibility(options::ClangOption);
7541 if (IsCLMode())
7542 return llvm::opt::Visibility(options::CLOption);
7543 if (IsDXCMode())
7544 return llvm::opt::Visibility(options::DXCOption);
7545 if (IsFlangMode())
7546 return llvm::opt::Visibility(options::FlangOption);
7547 return llvm::opt::Visibility(options::ClangOption);
7548}
7549
7550const char *Driver::getExecutableForDriverMode(DriverMode Mode) {
7551 switch (Mode) {
7552 case GCCMode:
7553 return "clang";
7554 case GXXMode:
7555 return "clang++";
7556 case CPPMode:
7557 return "clang-cpp";
7558 case CLMode:
7559 return "clang-cl";
7560 case FlangMode:
7561 return "flang";
7562 case DXCMode:
7563 return "clang-dxc";
7564 }
7565
7566 llvm_unreachable("Unhandled Mode");
7567}
7568
7569bool clang::driver::isOptimizationLevelFast(const ArgList &Args) {
7570 return Args.hasFlag(Pos: options::OPT_Ofast, Neg: options::OPT_O_Group, Default: false);
7571}
7572
7573bool clang::driver::willEmitRemarks(const ArgList &Args) {
7574 // -fsave-optimization-record enables it.
7575 if (Args.hasFlag(Pos: options::OPT_fsave_optimization_record,
7576 Neg: options::OPT_fno_save_optimization_record, Default: false))
7577 return true;
7578
7579 // -fsave-optimization-record=<format> enables it as well.
7580 if (Args.hasFlag(Pos: options::OPT_fsave_optimization_record_EQ,
7581 Neg: options::OPT_fno_save_optimization_record, Default: false))
7582 return true;
7583
7584 // -foptimization-record-file alone enables it too.
7585 if (Args.hasFlag(Pos: options::OPT_foptimization_record_file_EQ,
7586 Neg: options::OPT_fno_save_optimization_record, Default: false))
7587 return true;
7588
7589 // -foptimization-record-passes alone enables it too.
7590 if (Args.hasFlag(Pos: options::OPT_foptimization_record_passes_EQ,
7591 Neg: options::OPT_fno_save_optimization_record, Default: false))
7592 return true;
7593 return false;
7594}
7595
7596llvm::StringRef clang::driver::getDriverMode(StringRef ProgName,
7597 ArrayRef<const char *> Args) {
7598 static StringRef OptName =
7599 getDriverOptTable().getOption(Opt: options::OPT_driver_mode).getPrefixedName();
7600 llvm::StringRef Opt;
7601 for (StringRef Arg : Args) {
7602 if (!Arg.starts_with(Prefix: OptName))
7603 continue;
7604 Opt = Arg;
7605 }
7606 if (Opt.empty())
7607 Opt = ToolChain::getTargetAndModeFromProgramName(ProgName).DriverMode;
7608 return Opt.consume_front(Prefix: OptName) ? Opt : "";
7609}
7610
7611bool driver::IsClangCL(StringRef DriverMode) { return DriverMode == "cl"; }
7612
7613llvm::Error driver::expandResponseFiles(SmallVectorImpl<const char *> &Args,
7614 bool ClangCLMode,
7615 llvm::BumpPtrAllocator &Alloc,
7616 llvm::vfs::FileSystem *FS) {
7617 // Parse response files using the GNU syntax, unless we're in CL mode. There
7618 // are two ways to put clang in CL compatibility mode: ProgName is either
7619 // clang-cl or cl, or --driver-mode=cl is on the command line. The normal
7620 // command line parsing can't happen until after response file parsing, so we
7621 // have to manually search for a --driver-mode=cl argument the hard way.
7622 // Finally, our -cc1 tools don't care which tokenization mode we use because
7623 // response files written by clang will tokenize the same way in either mode.
7624 enum { Default, POSIX, Windows } RSPQuoting = Default;
7625 for (const char *F : Args) {
7626 if (strcmp(s1: F, s2: "--rsp-quoting=posix") == 0)
7627 RSPQuoting = POSIX;
7628 else if (strcmp(s1: F, s2: "--rsp-quoting=windows") == 0)
7629 RSPQuoting = Windows;
7630 }
7631
7632 // Determines whether we want nullptr markers in Args to indicate response
7633 // files end-of-lines. We only use this for the /LINK driver argument with
7634 // clang-cl.exe on Windows.
7635 bool MarkEOLs = ClangCLMode;
7636
7637 llvm::cl::TokenizerCallback Tokenizer;
7638 if (RSPQuoting == Windows || (RSPQuoting == Default && ClangCLMode))
7639 Tokenizer = &llvm::cl::TokenizeWindowsCommandLine;
7640 else
7641 Tokenizer = &llvm::cl::TokenizeGNUCommandLine;
7642
7643 if (MarkEOLs && Args.size() > 1 && StringRef(Args[1]).starts_with(Prefix: "-cc1"))
7644 MarkEOLs = false;
7645
7646 llvm::cl::ExpansionContext ECtx(Alloc, Tokenizer);
7647 ECtx.setMarkEOLs(MarkEOLs);
7648 if (FS)
7649 ECtx.setVFS(FS);
7650
7651 if (llvm::Error Err = ECtx.expandResponseFiles(Argv&: Args))
7652 return Err;
7653
7654 // If -cc1 came from a response file, remove the EOL sentinels.
7655 auto FirstArg = llvm::find_if(Range: llvm::drop_begin(RangeOrContainer&: Args),
7656 P: [](const char *A) { return A != nullptr; });
7657 if (FirstArg != Args.end() && StringRef(*FirstArg).starts_with(Prefix: "-cc1")) {
7658 // If -cc1 came from a response file, remove the EOL sentinels.
7659 if (MarkEOLs) {
7660 auto newEnd = std::remove(first: Args.begin(), last: Args.end(), value: nullptr);
7661 Args.resize(N: newEnd - Args.begin());
7662 }
7663 }
7664
7665 return llvm::Error::success();
7666}
7667
7668static const char *GetStableCStr(llvm::StringSet<> &SavedStrings, StringRef S) {
7669 return SavedStrings.insert(key: S).first->getKeyData();
7670}
7671
7672/// Apply a list of edits to the input argument lists.
7673///
7674/// The input string is a space separated list of edits to perform,
7675/// they are applied in order to the input argument lists. Edits
7676/// should be one of the following forms:
7677///
7678/// '#': Silence information about the changes to the command line arguments.
7679///
7680/// '^FOO': Add FOO as a new argument at the beginning of the command line
7681/// right after the name of the compiler executable.
7682///
7683/// '+FOO': Add FOO as a new argument at the end of the command line.
7684///
7685/// 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command
7686/// line.
7687///
7688/// 'xOPTION': Removes all instances of the literal argument OPTION.
7689///
7690/// 'XOPTION': Removes all instances of the literal argument OPTION,
7691/// and the following argument.
7692///
7693/// 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox'
7694/// at the end of the command line.
7695///
7696/// \param OS - The stream to write edit information to.
7697/// \param Args - The vector of command line arguments.
7698/// \param Edit - The override command to perform.
7699/// \param SavedStrings - Set to use for storing string representations.
7700static void applyOneOverrideOption(raw_ostream &OS,
7701 SmallVectorImpl<const char *> &Args,
7702 StringRef Edit,
7703 llvm::StringSet<> &SavedStrings) {
7704 // This does not need to be efficient.
7705
7706 if (Edit[0] == '^') {
7707 const char *Str = GetStableCStr(SavedStrings, S: Edit.substr(Start: 1));
7708 OS << "### Adding argument " << Str << " at beginning\n";
7709 Args.insert(I: Args.begin() + 1, Elt: Str);
7710 } else if (Edit[0] == '+') {
7711 const char *Str = GetStableCStr(SavedStrings, S: Edit.substr(Start: 1));
7712 OS << "### Adding argument " << Str << " at end\n";
7713 Args.push_back(Elt: Str);
7714 } else if (Edit[0] == 's' && Edit[1] == '/' && Edit.ends_with(Suffix: "/") &&
7715 Edit.slice(Start: 2, End: Edit.size() - 1).contains(C: '/')) {
7716 StringRef MatchPattern = Edit.substr(Start: 2).split(Separator: '/').first;
7717 StringRef ReplPattern = Edit.substr(Start: 2).split(Separator: '/').second;
7718 ReplPattern = ReplPattern.slice(Start: 0, End: ReplPattern.size() - 1);
7719
7720 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
7721 // Ignore end-of-line response file markers
7722 if (Args[i] == nullptr)
7723 continue;
7724 std::string Repl = llvm::Regex(MatchPattern).sub(Repl: ReplPattern, String: Args[i]);
7725
7726 if (Repl != Args[i]) {
7727 OS << "### Replacing '" << Args[i] << "' with '" << Repl << "'\n";
7728 Args[i] = GetStableCStr(SavedStrings, S: Repl);
7729 }
7730 }
7731 } else if (Edit[0] == 'x' || Edit[0] == 'X') {
7732 auto Option = Edit.substr(Start: 1);
7733 for (unsigned i = 1; i < Args.size();) {
7734 if (Option == Args[i]) {
7735 OS << "### Deleting argument " << Args[i] << '\n';
7736 Args.erase(CI: Args.begin() + i);
7737 if (Edit[0] == 'X') {
7738 if (i < Args.size()) {
7739 OS << "### Deleting argument " << Args[i] << '\n';
7740 Args.erase(CI: Args.begin() + i);
7741 } else
7742 OS << "### Invalid X edit, end of command line!\n";
7743 }
7744 } else
7745 ++i;
7746 }
7747 } else if (Edit[0] == 'O') {
7748 for (unsigned i = 1; i < Args.size();) {
7749 const char *A = Args[i];
7750 // Ignore end-of-line response file markers
7751 if (A == nullptr)
7752 continue;
7753 if (A[0] == '-' && A[1] == 'O' &&
7754 (A[2] == '\0' || (A[3] == '\0' && (A[2] == 's' || A[2] == 'z' ||
7755 ('0' <= A[2] && A[2] <= '9'))))) {
7756 OS << "### Deleting argument " << Args[i] << '\n';
7757 Args.erase(CI: Args.begin() + i);
7758 } else
7759 ++i;
7760 }
7761 OS << "### Adding argument " << Edit << " at end\n";
7762 Args.push_back(Elt: GetStableCStr(SavedStrings, S: '-' + Edit.str()));
7763 } else {
7764 OS << "### Unrecognized edit: " << Edit << "\n";
7765 }
7766}
7767
7768void driver::applyOverrideOptions(SmallVectorImpl<const char *> &Args,
7769 const char *OverrideStr,
7770 llvm::StringSet<> &SavedStrings,
7771 StringRef EnvVar, raw_ostream *OS) {
7772 if (!OS)
7773 OS = &llvm::nulls();
7774
7775 if (OverrideStr[0] == '#') {
7776 ++OverrideStr;
7777 OS = &llvm::nulls();
7778 }
7779
7780 *OS << "### " << EnvVar << ": " << OverrideStr << "\n";
7781
7782 // This does not need to be efficient.
7783
7784 const char *S = OverrideStr;
7785 while (*S) {
7786 const char *End = ::strchr(s: S, c: ' ');
7787 if (!End)
7788 End = S + strlen(s: S);
7789 if (End != S)
7790 applyOneOverrideOption(OS&: *OS, Args, Edit: std::string(S, End), SavedStrings);
7791 S = End;
7792 if (*S != '\0')
7793 ++S;
7794 }
7795}
7796