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