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