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