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