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