1 //===- ToolChain.cpp - Collections of tools for one platform --------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang/Driver/ToolChain.h" 10 #include "InputInfo.h" 11 #include "ToolChains/Arch/ARM.h" 12 #include "ToolChains/Clang.h" 13 #include "ToolChains/InterfaceStubs.h" 14 #include "ToolChains/Flang.h" 15 #include "clang/Basic/ObjCRuntime.h" 16 #include "clang/Basic/Sanitizers.h" 17 #include "clang/Config/config.h" 18 #include "clang/Driver/Action.h" 19 #include "clang/Driver/Driver.h" 20 #include "clang/Driver/DriverDiagnostic.h" 21 #include "clang/Driver/Job.h" 22 #include "clang/Driver/Options.h" 23 #include "clang/Driver/SanitizerArgs.h" 24 #include "clang/Driver/XRayArgs.h" 25 #include "llvm/ADT/STLExtras.h" 26 #include "llvm/ADT/SmallString.h" 27 #include "llvm/ADT/StringRef.h" 28 #include "llvm/ADT/Triple.h" 29 #include "llvm/ADT/Twine.h" 30 #include "llvm/Config/llvm-config.h" 31 #include "llvm/MC/MCTargetOptions.h" 32 #include "llvm/Option/Arg.h" 33 #include "llvm/Option/ArgList.h" 34 #include "llvm/Option/OptTable.h" 35 #include "llvm/Option/Option.h" 36 #include "llvm/Support/ErrorHandling.h" 37 #include "llvm/Support/FileSystem.h" 38 #include "llvm/Support/Path.h" 39 #include "llvm/Support/TargetParser.h" 40 #include "llvm/Support/TargetRegistry.h" 41 #include "llvm/Support/VersionTuple.h" 42 #include "llvm/Support/VirtualFileSystem.h" 43 #include <cassert> 44 #include <cstddef> 45 #include <cstring> 46 #include <string> 47 48 using namespace clang; 49 using namespace driver; 50 using namespace tools; 51 using namespace llvm; 52 using namespace llvm::opt; 53 54 static llvm::opt::Arg *GetRTTIArgument(const ArgList &Args) { 55 return Args.getLastArg(options::OPT_mkernel, options::OPT_fapple_kext, 56 options::OPT_fno_rtti, options::OPT_frtti); 57 } 58 59 static ToolChain::RTTIMode CalculateRTTIMode(const ArgList &Args, 60 const llvm::Triple &Triple, 61 const Arg *CachedRTTIArg) { 62 // Explicit rtti/no-rtti args 63 if (CachedRTTIArg) { 64 if (CachedRTTIArg->getOption().matches(options::OPT_frtti)) 65 return ToolChain::RM_Enabled; 66 else 67 return ToolChain::RM_Disabled; 68 } 69 70 // -frtti is default, except for the PS4 CPU. 71 return (Triple.isPS4CPU()) ? ToolChain::RM_Disabled : ToolChain::RM_Enabled; 72 } 73 74 ToolChain::ToolChain(const Driver &D, const llvm::Triple &T, 75 const ArgList &Args) 76 : D(D), Triple(T), Args(Args), CachedRTTIArg(GetRTTIArgument(Args)), 77 CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)) { 78 std::string CandidateLibPath = getArchSpecificLibPath(); 79 if (getVFS().exists(CandidateLibPath)) 80 getFilePaths().push_back(CandidateLibPath); 81 } 82 83 void ToolChain::setTripleEnvironment(llvm::Triple::EnvironmentType Env) { 84 Triple.setEnvironment(Env); 85 if (EffectiveTriple != llvm::Triple()) 86 EffectiveTriple.setEnvironment(Env); 87 } 88 89 ToolChain::~ToolChain() = default; 90 91 llvm::vfs::FileSystem &ToolChain::getVFS() const { 92 return getDriver().getVFS(); 93 } 94 95 bool ToolChain::useIntegratedAs() const { 96 return Args.hasFlag(options::OPT_fintegrated_as, 97 options::OPT_fno_integrated_as, 98 IsIntegratedAssemblerDefault()); 99 } 100 101 bool ToolChain::useRelaxRelocations() const { 102 return ENABLE_X86_RELAX_RELOCATIONS; 103 } 104 105 bool ToolChain::isNoExecStackDefault() const { 106 return false; 107 } 108 109 const SanitizerArgs& ToolChain::getSanitizerArgs() const { 110 if (!SanitizerArguments.get()) 111 SanitizerArguments.reset(new SanitizerArgs(*this, Args)); 112 return *SanitizerArguments.get(); 113 } 114 115 const XRayArgs& ToolChain::getXRayArgs() const { 116 if (!XRayArguments.get()) 117 XRayArguments.reset(new XRayArgs(*this, Args)); 118 return *XRayArguments.get(); 119 } 120 121 namespace { 122 123 struct DriverSuffix { 124 const char *Suffix; 125 const char *ModeFlag; 126 }; 127 128 } // namespace 129 130 static const DriverSuffix *FindDriverSuffix(StringRef ProgName, size_t &Pos) { 131 // A list of known driver suffixes. Suffixes are compared against the 132 // program name in order. If there is a match, the frontend type is updated as 133 // necessary by applying the ModeFlag. 134 static const DriverSuffix DriverSuffixes[] = { 135 {"clang", nullptr}, 136 {"clang++", "--driver-mode=g++"}, 137 {"clang-c++", "--driver-mode=g++"}, 138 {"clang-cc", nullptr}, 139 {"clang-cpp", "--driver-mode=cpp"}, 140 {"clang-g++", "--driver-mode=g++"}, 141 {"clang-gcc", nullptr}, 142 {"clang-cl", "--driver-mode=cl"}, 143 {"cc", nullptr}, 144 {"cpp", "--driver-mode=cpp"}, 145 {"cl", "--driver-mode=cl"}, 146 {"++", "--driver-mode=g++"}, 147 {"flang", "--driver-mode=flang"}, 148 }; 149 150 for (size_t i = 0; i < llvm::array_lengthof(DriverSuffixes); ++i) { 151 StringRef Suffix(DriverSuffixes[i].Suffix); 152 if (ProgName.endswith(Suffix)) { 153 Pos = ProgName.size() - Suffix.size(); 154 return &DriverSuffixes[i]; 155 } 156 } 157 return nullptr; 158 } 159 160 /// Normalize the program name from argv[0] by stripping the file extension if 161 /// present and lower-casing the string on Windows. 162 static std::string normalizeProgramName(llvm::StringRef Argv0) { 163 std::string ProgName = std::string(llvm::sys::path::stem(Argv0)); 164 #ifdef _WIN32 165 // Transform to lowercase for case insensitive file systems. 166 std::transform(ProgName.begin(), ProgName.end(), ProgName.begin(), ::tolower); 167 #endif 168 return ProgName; 169 } 170 171 static const DriverSuffix *parseDriverSuffix(StringRef ProgName, size_t &Pos) { 172 // Try to infer frontend type and default target from the program name by 173 // comparing it against DriverSuffixes in order. 174 175 // If there is a match, the function tries to identify a target as prefix. 176 // E.g. "x86_64-linux-clang" as interpreted as suffix "clang" with target 177 // prefix "x86_64-linux". If such a target prefix is found, it may be 178 // added via -target as implicit first argument. 179 const DriverSuffix *DS = FindDriverSuffix(ProgName, Pos); 180 181 if (!DS) { 182 // Try again after stripping any trailing version number: 183 // clang++3.5 -> clang++ 184 ProgName = ProgName.rtrim("0123456789."); 185 DS = FindDriverSuffix(ProgName, Pos); 186 } 187 188 if (!DS) { 189 // Try again after stripping trailing -component. 190 // clang++-tot -> clang++ 191 ProgName = ProgName.slice(0, ProgName.rfind('-')); 192 DS = FindDriverSuffix(ProgName, Pos); 193 } 194 return DS; 195 } 196 197 ParsedClangName 198 ToolChain::getTargetAndModeFromProgramName(StringRef PN) { 199 std::string ProgName = normalizeProgramName(PN); 200 size_t SuffixPos; 201 const DriverSuffix *DS = parseDriverSuffix(ProgName, SuffixPos); 202 if (!DS) 203 return {}; 204 size_t SuffixEnd = SuffixPos + strlen(DS->Suffix); 205 206 size_t LastComponent = ProgName.rfind('-', SuffixPos); 207 if (LastComponent == std::string::npos) 208 return ParsedClangName(ProgName.substr(0, SuffixEnd), DS->ModeFlag); 209 std::string ModeSuffix = ProgName.substr(LastComponent + 1, 210 SuffixEnd - LastComponent - 1); 211 212 // Infer target from the prefix. 213 StringRef Prefix(ProgName); 214 Prefix = Prefix.slice(0, LastComponent); 215 std::string IgnoredError; 216 bool IsRegistered = 217 llvm::TargetRegistry::lookupTarget(std::string(Prefix), IgnoredError); 218 return ParsedClangName{std::string(Prefix), ModeSuffix, DS->ModeFlag, 219 IsRegistered}; 220 } 221 222 StringRef ToolChain::getDefaultUniversalArchName() const { 223 // In universal driver terms, the arch name accepted by -arch isn't exactly 224 // the same as the ones that appear in the triple. Roughly speaking, this is 225 // an inverse of the darwin::getArchTypeForDarwinArchName() function. 226 switch (Triple.getArch()) { 227 case llvm::Triple::aarch64: { 228 if (getTriple().isArm64e()) 229 return "arm64e"; 230 return "arm64"; 231 } 232 case llvm::Triple::aarch64_32: 233 return "arm64_32"; 234 case llvm::Triple::ppc: 235 return "ppc"; 236 case llvm::Triple::ppcle: 237 return "ppcle"; 238 case llvm::Triple::ppc64: 239 return "ppc64"; 240 case llvm::Triple::ppc64le: 241 return "ppc64le"; 242 default: 243 return Triple.getArchName(); 244 } 245 } 246 247 std::string ToolChain::getInputFilename(const InputInfo &Input) const { 248 return Input.getFilename(); 249 } 250 251 bool ToolChain::IsUnwindTablesDefault(const ArgList &Args) const { 252 return false; 253 } 254 255 Tool *ToolChain::getClang() const { 256 if (!Clang) 257 Clang.reset(new tools::Clang(*this)); 258 return Clang.get(); 259 } 260 261 Tool *ToolChain::getFlang() const { 262 if (!Flang) 263 Flang.reset(new tools::Flang(*this)); 264 return Flang.get(); 265 } 266 267 Tool *ToolChain::buildAssembler() const { 268 return new tools::ClangAs(*this); 269 } 270 271 Tool *ToolChain::buildLinker() const { 272 llvm_unreachable("Linking is not supported by this toolchain"); 273 } 274 275 Tool *ToolChain::buildStaticLibTool() const { 276 llvm_unreachable("Creating static lib is not supported by this toolchain"); 277 } 278 279 Tool *ToolChain::getAssemble() const { 280 if (!Assemble) 281 Assemble.reset(buildAssembler()); 282 return Assemble.get(); 283 } 284 285 Tool *ToolChain::getClangAs() const { 286 if (!Assemble) 287 Assemble.reset(new tools::ClangAs(*this)); 288 return Assemble.get(); 289 } 290 291 Tool *ToolChain::getLink() const { 292 if (!Link) 293 Link.reset(buildLinker()); 294 return Link.get(); 295 } 296 297 Tool *ToolChain::getStaticLibTool() const { 298 if (!StaticLibTool) 299 StaticLibTool.reset(buildStaticLibTool()); 300 return StaticLibTool.get(); 301 } 302 303 Tool *ToolChain::getIfsMerge() const { 304 if (!IfsMerge) 305 IfsMerge.reset(new tools::ifstool::Merger(*this)); 306 return IfsMerge.get(); 307 } 308 309 Tool *ToolChain::getOffloadBundler() const { 310 if (!OffloadBundler) 311 OffloadBundler.reset(new tools::OffloadBundler(*this)); 312 return OffloadBundler.get(); 313 } 314 315 Tool *ToolChain::getOffloadWrapper() const { 316 if (!OffloadWrapper) 317 OffloadWrapper.reset(new tools::OffloadWrapper(*this)); 318 return OffloadWrapper.get(); 319 } 320 321 Tool *ToolChain::getTool(Action::ActionClass AC) const { 322 switch (AC) { 323 case Action::AssembleJobClass: 324 return getAssemble(); 325 326 case Action::IfsMergeJobClass: 327 return getIfsMerge(); 328 329 case Action::LinkJobClass: 330 return getLink(); 331 332 case Action::StaticLibJobClass: 333 return getStaticLibTool(); 334 335 case Action::InputClass: 336 case Action::BindArchClass: 337 case Action::OffloadClass: 338 case Action::LipoJobClass: 339 case Action::DsymutilJobClass: 340 case Action::VerifyDebugInfoJobClass: 341 llvm_unreachable("Invalid tool kind."); 342 343 case Action::CompileJobClass: 344 case Action::PrecompileJobClass: 345 case Action::HeaderModulePrecompileJobClass: 346 case Action::PreprocessJobClass: 347 case Action::AnalyzeJobClass: 348 case Action::MigrateJobClass: 349 case Action::VerifyPCHJobClass: 350 case Action::BackendJobClass: 351 return getClang(); 352 353 case Action::OffloadBundlingJobClass: 354 case Action::OffloadUnbundlingJobClass: 355 return getOffloadBundler(); 356 357 case Action::OffloadWrapperJobClass: 358 return getOffloadWrapper(); 359 } 360 361 llvm_unreachable("Invalid tool kind."); 362 } 363 364 static StringRef getArchNameForCompilerRTLib(const ToolChain &TC, 365 const ArgList &Args) { 366 const llvm::Triple &Triple = TC.getTriple(); 367 bool IsWindows = Triple.isOSWindows(); 368 369 if (TC.getArch() == llvm::Triple::arm || TC.getArch() == llvm::Triple::armeb) 370 return (arm::getARMFloatABI(TC, Args) == arm::FloatABI::Hard && !IsWindows) 371 ? "armhf" 372 : "arm"; 373 374 // For historic reasons, Android library is using i686 instead of i386. 375 if (TC.getArch() == llvm::Triple::x86 && Triple.isAndroid()) 376 return "i686"; 377 378 return llvm::Triple::getArchTypeName(TC.getArch()); 379 } 380 381 StringRef ToolChain::getOSLibName() const { 382 switch (Triple.getOS()) { 383 case llvm::Triple::FreeBSD: 384 return "freebsd"; 385 case llvm::Triple::NetBSD: 386 return "netbsd"; 387 case llvm::Triple::OpenBSD: 388 return "openbsd"; 389 case llvm::Triple::Solaris: 390 return "sunos"; 391 case llvm::Triple::AIX: 392 return "aix"; 393 default: 394 return getOS(); 395 } 396 } 397 398 std::string ToolChain::getCompilerRTPath() const { 399 SmallString<128> Path(getDriver().ResourceDir); 400 if (Triple.isOSUnknown()) { 401 llvm::sys::path::append(Path, "lib"); 402 } else { 403 llvm::sys::path::append(Path, "lib", getOSLibName()); 404 } 405 return std::string(Path.str()); 406 } 407 408 std::string ToolChain::getCompilerRTBasename(const ArgList &Args, 409 StringRef Component, 410 FileType Type) const { 411 std::string CRTAbsolutePath = getCompilerRT(Args, Component, Type); 412 return llvm::sys::path::filename(CRTAbsolutePath).str(); 413 } 414 415 std::string ToolChain::buildCompilerRTBasename(const llvm::opt::ArgList &Args, 416 StringRef Component, 417 FileType Type, 418 bool AddArch) const { 419 const llvm::Triple &TT = getTriple(); 420 bool IsITANMSVCWindows = 421 TT.isWindowsMSVCEnvironment() || TT.isWindowsItaniumEnvironment(); 422 423 const char *Prefix = 424 IsITANMSVCWindows || Type == ToolChain::FT_Object ? "" : "lib"; 425 const char *Suffix; 426 switch (Type) { 427 case ToolChain::FT_Object: 428 Suffix = IsITANMSVCWindows ? ".obj" : ".o"; 429 break; 430 case ToolChain::FT_Static: 431 Suffix = IsITANMSVCWindows ? ".lib" : ".a"; 432 break; 433 case ToolChain::FT_Shared: 434 Suffix = TT.isOSWindows() 435 ? (TT.isWindowsGNUEnvironment() ? ".dll.a" : ".lib") 436 : ".so"; 437 break; 438 } 439 440 std::string ArchAndEnv; 441 if (AddArch) { 442 StringRef Arch = getArchNameForCompilerRTLib(*this, Args); 443 const char *Env = TT.isAndroid() ? "-android" : ""; 444 ArchAndEnv = ("-" + Arch + Env).str(); 445 } 446 return (Prefix + Twine("clang_rt.") + Component + ArchAndEnv + Suffix).str(); 447 } 448 449 std::string ToolChain::getCompilerRT(const ArgList &Args, StringRef Component, 450 FileType Type) const { 451 // Check for runtime files in the new layout without the architecture first. 452 std::string CRTBasename = 453 buildCompilerRTBasename(Args, Component, Type, /*AddArch=*/false); 454 for (const auto &LibPath : getLibraryPaths()) { 455 SmallString<128> P(LibPath); 456 llvm::sys::path::append(P, CRTBasename); 457 if (getVFS().exists(P)) 458 return std::string(P.str()); 459 } 460 461 // Fall back to the old expected compiler-rt name if the new one does not 462 // exist. 463 CRTBasename = 464 buildCompilerRTBasename(Args, Component, Type, /*AddArch=*/true); 465 SmallString<128> Path(getCompilerRTPath()); 466 llvm::sys::path::append(Path, CRTBasename); 467 return std::string(Path.str()); 468 } 469 470 const char *ToolChain::getCompilerRTArgString(const llvm::opt::ArgList &Args, 471 StringRef Component, 472 FileType Type) const { 473 return Args.MakeArgString(getCompilerRT(Args, Component, Type)); 474 } 475 476 std::string ToolChain::getRuntimePath() const { 477 SmallString<128> P(D.ResourceDir); 478 llvm::sys::path::append(P, "lib", getMultiarchTriple(D, Triple, D.SysRoot)); 479 return std::string(P.str()); 480 } 481 482 std::string ToolChain::getStdlibPath() const { 483 SmallString<128> P(D.Dir); 484 llvm::sys::path::append(P, "..", "lib", 485 getMultiarchTriple(D, Triple, D.SysRoot)); 486 return std::string(P.str()); 487 } 488 489 std::string ToolChain::getArchSpecificLibPath() const { 490 SmallString<128> Path(getDriver().ResourceDir); 491 llvm::sys::path::append(Path, "lib", getOSLibName(), 492 llvm::Triple::getArchTypeName(getArch())); 493 return std::string(Path.str()); 494 } 495 496 bool ToolChain::needsProfileRT(const ArgList &Args) { 497 if (Args.hasArg(options::OPT_noprofilelib)) 498 return false; 499 500 return Args.hasArg(options::OPT_fprofile_generate) || 501 Args.hasArg(options::OPT_fprofile_generate_EQ) || 502 Args.hasArg(options::OPT_fcs_profile_generate) || 503 Args.hasArg(options::OPT_fcs_profile_generate_EQ) || 504 Args.hasArg(options::OPT_fprofile_instr_generate) || 505 Args.hasArg(options::OPT_fprofile_instr_generate_EQ) || 506 Args.hasArg(options::OPT_fcreate_profile) || 507 Args.hasArg(options::OPT_forder_file_instrumentation); 508 } 509 510 bool ToolChain::needsGCovInstrumentation(const llvm::opt::ArgList &Args) { 511 return Args.hasArg(options::OPT_coverage) || 512 Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs, 513 false); 514 } 515 516 Tool *ToolChain::SelectTool(const JobAction &JA) const { 517 if (D.IsFlangMode() && getDriver().ShouldUseFlangCompiler(JA)) return getFlang(); 518 if (getDriver().ShouldUseClangCompiler(JA)) return getClang(); 519 Action::ActionClass AC = JA.getKind(); 520 if (AC == Action::AssembleJobClass && useIntegratedAs()) 521 return getClangAs(); 522 return getTool(AC); 523 } 524 525 std::string ToolChain::GetFilePath(const char *Name) const { 526 return D.GetFilePath(Name, *this); 527 } 528 529 std::string ToolChain::GetProgramPath(const char *Name) const { 530 return D.GetProgramPath(Name, *this); 531 } 532 533 std::string ToolChain::GetLinkerPath(bool *LinkerIsLLD, 534 bool *LinkerIsLLDDarwinNew) const { 535 if (LinkerIsLLD) 536 *LinkerIsLLD = false; 537 if (LinkerIsLLDDarwinNew) 538 *LinkerIsLLDDarwinNew = false; 539 540 // Get -fuse-ld= first to prevent -Wunused-command-line-argument. -fuse-ld= is 541 // considered as the linker flavor, e.g. "bfd", "gold", or "lld". 542 const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ); 543 StringRef UseLinker = A ? A->getValue() : CLANG_DEFAULT_LINKER; 544 545 // --ld-path= takes precedence over -fuse-ld= and specifies the executable 546 // name. -B, COMPILER_PATH and PATH and consulted if the value does not 547 // contain a path component separator. 548 if (const Arg *A = Args.getLastArg(options::OPT_ld_path_EQ)) { 549 std::string Path(A->getValue()); 550 if (!Path.empty()) { 551 if (llvm::sys::path::parent_path(Path).empty()) 552 Path = GetProgramPath(A->getValue()); 553 if (llvm::sys::fs::can_execute(Path)) 554 return std::string(Path); 555 } 556 getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args); 557 return GetProgramPath(getDefaultLinker()); 558 } 559 // If we're passed -fuse-ld= with no argument, or with the argument ld, 560 // then use whatever the default system linker is. 561 if (UseLinker.empty() || UseLinker == "ld") { 562 const char *DefaultLinker = getDefaultLinker(); 563 if (llvm::sys::path::is_absolute(DefaultLinker)) 564 return std::string(DefaultLinker); 565 else 566 return GetProgramPath(DefaultLinker); 567 } 568 569 // Extending -fuse-ld= to an absolute or relative path is unexpected. Checking 570 // for the linker flavor is brittle. In addition, prepending "ld." or "ld64." 571 // to a relative path is surprising. This is more complex due to priorities 572 // among -B, COMPILER_PATH and PATH. --ld-path= should be used instead. 573 if (UseLinker.find('/') != StringRef::npos) 574 getDriver().Diag(diag::warn_drv_fuse_ld_path); 575 576 if (llvm::sys::path::is_absolute(UseLinker)) { 577 // If we're passed what looks like an absolute path, don't attempt to 578 // second-guess that. 579 if (llvm::sys::fs::can_execute(UseLinker)) 580 return std::string(UseLinker); 581 } else { 582 llvm::SmallString<8> LinkerName; 583 if (Triple.isOSDarwin()) 584 LinkerName.append("ld64."); 585 else 586 LinkerName.append("ld."); 587 LinkerName.append(UseLinker); 588 589 std::string LinkerPath(GetProgramPath(LinkerName.c_str())); 590 if (llvm::sys::fs::can_execute(LinkerPath)) { 591 // FIXME: Remove LinkerIsLLDDarwinNew once there's only one MachO lld. 592 if (LinkerIsLLD) 593 *LinkerIsLLD = UseLinker == "lld" || UseLinker == "lld.darwinold"; 594 if (LinkerIsLLDDarwinNew) 595 *LinkerIsLLDDarwinNew = UseLinker == "lld"; 596 return LinkerPath; 597 } 598 } 599 600 if (A) 601 getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args); 602 603 return GetProgramPath(getDefaultLinker()); 604 } 605 606 std::string ToolChain::GetStaticLibToolPath() const { 607 // TODO: Add support for static lib archiving on Windows 608 return GetProgramPath("llvm-ar"); 609 } 610 611 types::ID ToolChain::LookupTypeForExtension(StringRef Ext) const { 612 types::ID id = types::lookupTypeForExtension(Ext); 613 614 // Flang always runs the preprocessor and has no notion of "preprocessed 615 // fortran". Here, TY_PP_Fortran is coerced to TY_Fortran to avoid treating 616 // them differently. 617 if (D.IsFlangMode() && id == types::TY_PP_Fortran) 618 id = types::TY_Fortran; 619 620 return id; 621 } 622 623 bool ToolChain::HasNativeLLVMSupport() const { 624 return false; 625 } 626 627 bool ToolChain::isCrossCompiling() const { 628 llvm::Triple HostTriple(LLVM_HOST_TRIPLE); 629 switch (HostTriple.getArch()) { 630 // The A32/T32/T16 instruction sets are not separate architectures in this 631 // context. 632 case llvm::Triple::arm: 633 case llvm::Triple::armeb: 634 case llvm::Triple::thumb: 635 case llvm::Triple::thumbeb: 636 return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb && 637 getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb; 638 default: 639 return HostTriple.getArch() != getArch(); 640 } 641 } 642 643 ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const { 644 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC, 645 VersionTuple()); 646 } 647 648 llvm::ExceptionHandling 649 ToolChain::GetExceptionModel(const llvm::opt::ArgList &Args) const { 650 return llvm::ExceptionHandling::None; 651 } 652 653 bool ToolChain::isThreadModelSupported(const StringRef Model) const { 654 if (Model == "single") { 655 // FIXME: 'single' is only supported on ARM and WebAssembly so far. 656 return Triple.getArch() == llvm::Triple::arm || 657 Triple.getArch() == llvm::Triple::armeb || 658 Triple.getArch() == llvm::Triple::thumb || 659 Triple.getArch() == llvm::Triple::thumbeb || Triple.isWasm(); 660 } else if (Model == "posix") 661 return true; 662 663 return false; 664 } 665 666 std::string ToolChain::ComputeLLVMTriple(const ArgList &Args, 667 types::ID InputType) const { 668 switch (getTriple().getArch()) { 669 default: 670 return getTripleString(); 671 672 case llvm::Triple::x86_64: { 673 llvm::Triple Triple = getTriple(); 674 if (!Triple.isOSBinFormatMachO()) 675 return getTripleString(); 676 677 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) { 678 // x86_64h goes in the triple. Other -march options just use the 679 // vanilla triple we already have. 680 StringRef MArch = A->getValue(); 681 if (MArch == "x86_64h") 682 Triple.setArchName(MArch); 683 } 684 return Triple.getTriple(); 685 } 686 case llvm::Triple::aarch64: { 687 llvm::Triple Triple = getTriple(); 688 if (!Triple.isOSBinFormatMachO()) 689 return getTripleString(); 690 691 if (Triple.isArm64e()) 692 return getTripleString(); 693 694 // FIXME: older versions of ld64 expect the "arm64" component in the actual 695 // triple string and query it to determine whether an LTO file can be 696 // handled. Remove this when we don't care any more. 697 Triple.setArchName("arm64"); 698 return Triple.getTriple(); 699 } 700 case llvm::Triple::aarch64_32: 701 return getTripleString(); 702 case llvm::Triple::arm: 703 case llvm::Triple::armeb: 704 case llvm::Triple::thumb: 705 case llvm::Triple::thumbeb: { 706 llvm::Triple Triple = getTriple(); 707 tools::arm::setArchNameInTriple(getDriver(), Args, InputType, Triple); 708 tools::arm::setFloatABIInTriple(getDriver(), Args, Triple); 709 return Triple.getTriple(); 710 } 711 } 712 } 713 714 std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args, 715 types::ID InputType) const { 716 return ComputeLLVMTriple(Args, InputType); 717 } 718 719 std::string ToolChain::computeSysRoot() const { 720 return D.SysRoot; 721 } 722 723 void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 724 ArgStringList &CC1Args) const { 725 // Each toolchain should provide the appropriate include flags. 726 } 727 728 void ToolChain::addClangTargetOptions( 729 const ArgList &DriverArgs, ArgStringList &CC1Args, 730 Action::OffloadKind DeviceOffloadKind) const {} 731 732 void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {} 733 734 void ToolChain::addProfileRTLibs(const llvm::opt::ArgList &Args, 735 llvm::opt::ArgStringList &CmdArgs) const { 736 if (!needsProfileRT(Args) && !needsGCovInstrumentation(Args)) 737 return; 738 739 CmdArgs.push_back(getCompilerRTArgString(Args, "profile")); 740 } 741 742 ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType( 743 const ArgList &Args) const { 744 if (runtimeLibType) 745 return *runtimeLibType; 746 747 const Arg* A = Args.getLastArg(options::OPT_rtlib_EQ); 748 StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_RTLIB; 749 750 // Only use "platform" in tests to override CLANG_DEFAULT_RTLIB! 751 if (LibName == "compiler-rt") 752 runtimeLibType = ToolChain::RLT_CompilerRT; 753 else if (LibName == "libgcc") 754 runtimeLibType = ToolChain::RLT_Libgcc; 755 else if (LibName == "platform") 756 runtimeLibType = GetDefaultRuntimeLibType(); 757 else { 758 if (A) 759 getDriver().Diag(diag::err_drv_invalid_rtlib_name) 760 << A->getAsString(Args); 761 762 runtimeLibType = GetDefaultRuntimeLibType(); 763 } 764 765 return *runtimeLibType; 766 } 767 768 ToolChain::UnwindLibType ToolChain::GetUnwindLibType( 769 const ArgList &Args) const { 770 if (unwindLibType) 771 return *unwindLibType; 772 773 const Arg *A = Args.getLastArg(options::OPT_unwindlib_EQ); 774 StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_UNWINDLIB; 775 776 if (LibName == "none") 777 unwindLibType = ToolChain::UNW_None; 778 else if (LibName == "platform" || LibName == "") { 779 ToolChain::RuntimeLibType RtLibType = GetRuntimeLibType(Args); 780 if (RtLibType == ToolChain::RLT_CompilerRT) { 781 if (getTriple().isAndroid()) 782 unwindLibType = ToolChain::UNW_CompilerRT; 783 else 784 unwindLibType = ToolChain::UNW_None; 785 } else if (RtLibType == ToolChain::RLT_Libgcc) 786 unwindLibType = ToolChain::UNW_Libgcc; 787 } else if (LibName == "libunwind") { 788 if (GetRuntimeLibType(Args) == RLT_Libgcc) 789 getDriver().Diag(diag::err_drv_incompatible_unwindlib); 790 unwindLibType = ToolChain::UNW_CompilerRT; 791 } else if (LibName == "libgcc") 792 unwindLibType = ToolChain::UNW_Libgcc; 793 else { 794 if (A) 795 getDriver().Diag(diag::err_drv_invalid_unwindlib_name) 796 << A->getAsString(Args); 797 798 unwindLibType = GetDefaultUnwindLibType(); 799 } 800 801 return *unwindLibType; 802 } 803 804 ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{ 805 if (cxxStdlibType) 806 return *cxxStdlibType; 807 808 const Arg *A = Args.getLastArg(options::OPT_stdlib_EQ); 809 StringRef LibName = A ? A->getValue() : CLANG_DEFAULT_CXX_STDLIB; 810 811 // Only use "platform" in tests to override CLANG_DEFAULT_CXX_STDLIB! 812 if (LibName == "libc++") 813 cxxStdlibType = ToolChain::CST_Libcxx; 814 else if (LibName == "libstdc++") 815 cxxStdlibType = ToolChain::CST_Libstdcxx; 816 else if (LibName == "platform") 817 cxxStdlibType = GetDefaultCXXStdlibType(); 818 else { 819 if (A) 820 getDriver().Diag(diag::err_drv_invalid_stdlib_name) 821 << A->getAsString(Args); 822 823 cxxStdlibType = GetDefaultCXXStdlibType(); 824 } 825 826 return *cxxStdlibType; 827 } 828 829 /// Utility function to add a system include directory to CC1 arguments. 830 /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs, 831 ArgStringList &CC1Args, 832 const Twine &Path) { 833 CC1Args.push_back("-internal-isystem"); 834 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 835 } 836 837 /// Utility function to add a system include directory with extern "C" 838 /// semantics to CC1 arguments. 839 /// 840 /// Note that this should be used rarely, and only for directories that 841 /// historically and for legacy reasons are treated as having implicit extern 842 /// "C" semantics. These semantics are *ignored* by and large today, but its 843 /// important to preserve the preprocessor changes resulting from the 844 /// classification. 845 /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs, 846 ArgStringList &CC1Args, 847 const Twine &Path) { 848 CC1Args.push_back("-internal-externc-isystem"); 849 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 850 } 851 852 void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs, 853 ArgStringList &CC1Args, 854 const Twine &Path) { 855 if (llvm::sys::fs::exists(Path)) 856 addExternCSystemInclude(DriverArgs, CC1Args, Path); 857 } 858 859 /// Utility function to add a list of system include directories to CC1. 860 /*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs, 861 ArgStringList &CC1Args, 862 ArrayRef<StringRef> Paths) { 863 for (const auto &Path : Paths) { 864 CC1Args.push_back("-internal-isystem"); 865 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 866 } 867 } 868 869 std::string ToolChain::detectLibcxxVersion(StringRef IncludePath) const { 870 std::error_code EC; 871 int MaxVersion = 0; 872 std::string MaxVersionString; 873 SmallString<128> Path(IncludePath); 874 llvm::sys::path::append(Path, "c++"); 875 for (llvm::vfs::directory_iterator LI = getVFS().dir_begin(Path, EC), LE; 876 !EC && LI != LE; LI = LI.increment(EC)) { 877 StringRef VersionText = llvm::sys::path::filename(LI->path()); 878 int Version; 879 if (VersionText[0] == 'v' && 880 !VersionText.slice(1, StringRef::npos).getAsInteger(10, Version)) { 881 if (Version > MaxVersion) { 882 MaxVersion = Version; 883 MaxVersionString = std::string(VersionText); 884 } 885 } 886 } 887 if (!MaxVersion) 888 return ""; 889 return MaxVersionString; 890 } 891 892 void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 893 ArgStringList &CC1Args) const { 894 // Header search paths should be handled by each of the subclasses. 895 // Historically, they have not been, and instead have been handled inside of 896 // the CC1-layer frontend. As the logic is hoisted out, this generic function 897 // will slowly stop being called. 898 // 899 // While it is being called, replicate a bit of a hack to propagate the 900 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++ 901 // header search paths with it. Once all systems are overriding this 902 // function, the CC1 flag and this line can be removed. 903 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ); 904 } 905 906 void ToolChain::AddClangCXXStdlibIsystemArgs( 907 const llvm::opt::ArgList &DriverArgs, 908 llvm::opt::ArgStringList &CC1Args) const { 909 DriverArgs.ClaimAllArgs(options::OPT_stdlibxx_isystem); 910 if (!DriverArgs.hasArg(options::OPT_nostdinc, options::OPT_nostdincxx, 911 options::OPT_nostdlibinc)) 912 for (const auto &P : 913 DriverArgs.getAllArgValues(options::OPT_stdlibxx_isystem)) 914 addSystemInclude(DriverArgs, CC1Args, P); 915 } 916 917 bool ToolChain::ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const { 918 return getDriver().CCCIsCXX() && 919 !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs, 920 options::OPT_nostdlibxx); 921 } 922 923 void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args, 924 ArgStringList &CmdArgs) const { 925 assert(!Args.hasArg(options::OPT_nostdlibxx) && 926 "should not have called this"); 927 CXXStdlibType Type = GetCXXStdlibType(Args); 928 929 switch (Type) { 930 case ToolChain::CST_Libcxx: 931 CmdArgs.push_back("-lc++"); 932 break; 933 934 case ToolChain::CST_Libstdcxx: 935 CmdArgs.push_back("-lstdc++"); 936 break; 937 } 938 } 939 940 void ToolChain::AddFilePathLibArgs(const ArgList &Args, 941 ArgStringList &CmdArgs) const { 942 for (const auto &LibPath : getFilePaths()) 943 if(LibPath.length() > 0) 944 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + LibPath)); 945 } 946 947 void ToolChain::AddCCKextLibArgs(const ArgList &Args, 948 ArgStringList &CmdArgs) const { 949 CmdArgs.push_back("-lcc_kext"); 950 } 951 952 bool ToolChain::isFastMathRuntimeAvailable(const ArgList &Args, 953 std::string &Path) const { 954 // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed 955 // (to keep the linker options consistent with gcc and clang itself). 956 if (!isOptimizationLevelFast(Args)) { 957 // Check if -ffast-math or -funsafe-math. 958 Arg *A = 959 Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math, 960 options::OPT_funsafe_math_optimizations, 961 options::OPT_fno_unsafe_math_optimizations); 962 963 if (!A || A->getOption().getID() == options::OPT_fno_fast_math || 964 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations) 965 return false; 966 } 967 // If crtfastmath.o exists add it to the arguments. 968 Path = GetFilePath("crtfastmath.o"); 969 return (Path != "crtfastmath.o"); // Not found. 970 } 971 972 bool ToolChain::addFastMathRuntimeIfAvailable(const ArgList &Args, 973 ArgStringList &CmdArgs) const { 974 std::string Path; 975 if (isFastMathRuntimeAvailable(Args, Path)) { 976 CmdArgs.push_back(Args.MakeArgString(Path)); 977 return true; 978 } 979 980 return false; 981 } 982 983 SanitizerMask ToolChain::getSupportedSanitizers() const { 984 // Return sanitizers which don't require runtime support and are not 985 // platform dependent. 986 987 SanitizerMask Res = 988 (SanitizerKind::Undefined & ~SanitizerKind::Vptr & 989 ~SanitizerKind::Function) | 990 (SanitizerKind::CFI & ~SanitizerKind::CFIICall) | 991 SanitizerKind::CFICastStrict | SanitizerKind::FloatDivideByZero | 992 SanitizerKind::UnsignedIntegerOverflow | 993 SanitizerKind::UnsignedShiftBase | SanitizerKind::ImplicitConversion | 994 SanitizerKind::Nullability | SanitizerKind::LocalBounds; 995 if (getTriple().getArch() == llvm::Triple::x86 || 996 getTriple().getArch() == llvm::Triple::x86_64 || 997 getTriple().getArch() == llvm::Triple::arm || getTriple().isWasm() || 998 getTriple().isAArch64()) 999 Res |= SanitizerKind::CFIICall; 1000 if (getTriple().getArch() == llvm::Triple::x86_64 || 1001 getTriple().isAArch64(64) || getTriple().isRISCV()) 1002 Res |= SanitizerKind::ShadowCallStack; 1003 if (getTriple().isAArch64(64)) 1004 Res |= SanitizerKind::MemTag; 1005 return Res; 1006 } 1007 1008 void ToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs, 1009 ArgStringList &CC1Args) const {} 1010 1011 void ToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs, 1012 ArgStringList &CC1Args) const {} 1013 1014 llvm::SmallVector<std::string, 12> 1015 ToolChain::getHIPDeviceLibs(const ArgList &DriverArgs) const { 1016 return {}; 1017 } 1018 1019 void ToolChain::AddIAMCUIncludeArgs(const ArgList &DriverArgs, 1020 ArgStringList &CC1Args) const {} 1021 1022 static VersionTuple separateMSVCFullVersion(unsigned Version) { 1023 if (Version < 100) 1024 return VersionTuple(Version); 1025 1026 if (Version < 10000) 1027 return VersionTuple(Version / 100, Version % 100); 1028 1029 unsigned Build = 0, Factor = 1; 1030 for (; Version > 10000; Version = Version / 10, Factor = Factor * 10) 1031 Build = Build + (Version % 10) * Factor; 1032 return VersionTuple(Version / 100, Version % 100, Build); 1033 } 1034 1035 VersionTuple 1036 ToolChain::computeMSVCVersion(const Driver *D, 1037 const llvm::opt::ArgList &Args) const { 1038 const Arg *MSCVersion = Args.getLastArg(options::OPT_fmsc_version); 1039 const Arg *MSCompatibilityVersion = 1040 Args.getLastArg(options::OPT_fms_compatibility_version); 1041 1042 if (MSCVersion && MSCompatibilityVersion) { 1043 if (D) 1044 D->Diag(diag::err_drv_argument_not_allowed_with) 1045 << MSCVersion->getAsString(Args) 1046 << MSCompatibilityVersion->getAsString(Args); 1047 return VersionTuple(); 1048 } 1049 1050 if (MSCompatibilityVersion) { 1051 VersionTuple MSVT; 1052 if (MSVT.tryParse(MSCompatibilityVersion->getValue())) { 1053 if (D) 1054 D->Diag(diag::err_drv_invalid_value) 1055 << MSCompatibilityVersion->getAsString(Args) 1056 << MSCompatibilityVersion->getValue(); 1057 } else { 1058 return MSVT; 1059 } 1060 } 1061 1062 if (MSCVersion) { 1063 unsigned Version = 0; 1064 if (StringRef(MSCVersion->getValue()).getAsInteger(10, Version)) { 1065 if (D) 1066 D->Diag(diag::err_drv_invalid_value) 1067 << MSCVersion->getAsString(Args) << MSCVersion->getValue(); 1068 } else { 1069 return separateMSVCFullVersion(Version); 1070 } 1071 } 1072 1073 return VersionTuple(); 1074 } 1075 1076 llvm::opt::DerivedArgList *ToolChain::TranslateOpenMPTargetArgs( 1077 const llvm::opt::DerivedArgList &Args, bool SameTripleAsHost, 1078 SmallVectorImpl<llvm::opt::Arg *> &AllocatedArgs) const { 1079 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs()); 1080 const OptTable &Opts = getDriver().getOpts(); 1081 bool Modified = false; 1082 1083 // Handle -Xopenmp-target flags 1084 for (auto *A : Args) { 1085 // Exclude flags which may only apply to the host toolchain. 1086 // Do not exclude flags when the host triple (AuxTriple) 1087 // matches the current toolchain triple. If it is not present 1088 // at all, target and host share a toolchain. 1089 if (A->getOption().matches(options::OPT_m_Group)) { 1090 if (SameTripleAsHost) 1091 DAL->append(A); 1092 else 1093 Modified = true; 1094 continue; 1095 } 1096 1097 unsigned Index; 1098 unsigned Prev; 1099 bool XOpenMPTargetNoTriple = 1100 A->getOption().matches(options::OPT_Xopenmp_target); 1101 1102 if (A->getOption().matches(options::OPT_Xopenmp_target_EQ)) { 1103 // Passing device args: -Xopenmp-target=<triple> -opt=val. 1104 if (A->getValue(0) == getTripleString()) 1105 Index = Args.getBaseArgs().MakeIndex(A->getValue(1)); 1106 else 1107 continue; 1108 } else if (XOpenMPTargetNoTriple) { 1109 // Passing device args: -Xopenmp-target -opt=val. 1110 Index = Args.getBaseArgs().MakeIndex(A->getValue(0)); 1111 } else { 1112 DAL->append(A); 1113 continue; 1114 } 1115 1116 // Parse the argument to -Xopenmp-target. 1117 Prev = Index; 1118 std::unique_ptr<Arg> XOpenMPTargetArg(Opts.ParseOneArg(Args, Index)); 1119 if (!XOpenMPTargetArg || Index > Prev + 1) { 1120 getDriver().Diag(diag::err_drv_invalid_Xopenmp_target_with_args) 1121 << A->getAsString(Args); 1122 continue; 1123 } 1124 if (XOpenMPTargetNoTriple && XOpenMPTargetArg && 1125 Args.getAllArgValues(options::OPT_fopenmp_targets_EQ).size() != 1) { 1126 getDriver().Diag(diag::err_drv_Xopenmp_target_missing_triple); 1127 continue; 1128 } 1129 XOpenMPTargetArg->setBaseArg(A); 1130 A = XOpenMPTargetArg.release(); 1131 AllocatedArgs.push_back(A); 1132 DAL->append(A); 1133 Modified = true; 1134 } 1135 1136 if (Modified) 1137 return DAL; 1138 1139 delete DAL; 1140 return nullptr; 1141 } 1142 1143 // TODO: Currently argument values separated by space e.g. 1144 // -Xclang -mframe-pointer=no cannot be passed by -Xarch_. This should be 1145 // fixed. 1146 void ToolChain::TranslateXarchArgs( 1147 const llvm::opt::DerivedArgList &Args, llvm::opt::Arg *&A, 1148 llvm::opt::DerivedArgList *DAL, 1149 SmallVectorImpl<llvm::opt::Arg *> *AllocatedArgs) const { 1150 const OptTable &Opts = getDriver().getOpts(); 1151 unsigned ValuePos = 1; 1152 if (A->getOption().matches(options::OPT_Xarch_device) || 1153 A->getOption().matches(options::OPT_Xarch_host)) 1154 ValuePos = 0; 1155 1156 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(ValuePos)); 1157 unsigned Prev = Index; 1158 std::unique_ptr<llvm::opt::Arg> XarchArg(Opts.ParseOneArg(Args, Index)); 1159 1160 // If the argument parsing failed or more than one argument was 1161 // consumed, the -Xarch_ argument's parameter tried to consume 1162 // extra arguments. Emit an error and ignore. 1163 // 1164 // We also want to disallow any options which would alter the 1165 // driver behavior; that isn't going to work in our model. We 1166 // use options::NoXarchOption to control this. 1167 if (!XarchArg || Index > Prev + 1) { 1168 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args) 1169 << A->getAsString(Args); 1170 return; 1171 } else if (XarchArg->getOption().hasFlag(options::NoXarchOption)) { 1172 auto &Diags = getDriver().getDiags(); 1173 unsigned DiagID = 1174 Diags.getCustomDiagID(DiagnosticsEngine::Error, 1175 "invalid Xarch argument: '%0', not all driver " 1176 "options can be forwared via Xarch argument"); 1177 Diags.Report(DiagID) << A->getAsString(Args); 1178 return; 1179 } 1180 XarchArg->setBaseArg(A); 1181 A = XarchArg.release(); 1182 if (!AllocatedArgs) 1183 DAL->AddSynthesizedArg(A); 1184 else 1185 AllocatedArgs->push_back(A); 1186 } 1187 1188 llvm::opt::DerivedArgList *ToolChain::TranslateXarchArgs( 1189 const llvm::opt::DerivedArgList &Args, StringRef BoundArch, 1190 Action::OffloadKind OFK, 1191 SmallVectorImpl<llvm::opt::Arg *> *AllocatedArgs) const { 1192 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs()); 1193 bool Modified = false; 1194 1195 bool IsGPU = OFK == Action::OFK_Cuda || OFK == Action::OFK_HIP; 1196 for (Arg *A : Args) { 1197 bool NeedTrans = false; 1198 bool Skip = false; 1199 if (A->getOption().matches(options::OPT_Xarch_device)) { 1200 NeedTrans = IsGPU; 1201 Skip = !IsGPU; 1202 } else if (A->getOption().matches(options::OPT_Xarch_host)) { 1203 NeedTrans = !IsGPU; 1204 Skip = IsGPU; 1205 } else if (A->getOption().matches(options::OPT_Xarch__) && IsGPU) { 1206 // Do not translate -Xarch_ options for non CUDA/HIP toolchain since 1207 // they may need special translation. 1208 // Skip this argument unless the architecture matches BoundArch 1209 if (BoundArch.empty() || A->getValue(0) != BoundArch) 1210 Skip = true; 1211 else 1212 NeedTrans = true; 1213 } 1214 if (NeedTrans || Skip) 1215 Modified = true; 1216 if (NeedTrans) 1217 TranslateXarchArgs(Args, A, DAL, AllocatedArgs); 1218 if (!Skip) 1219 DAL->append(A); 1220 } 1221 1222 if (Modified) 1223 return DAL; 1224 1225 delete DAL; 1226 return nullptr; 1227 } 1228