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