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