1 //===--- Cuda.cpp - Cuda Tool and ToolChain Implementations -----*- C++ -*-===// 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 "Cuda.h" 11 #include "InputInfo.h" 12 #include "CommonArgs.h" 13 #include "clang/Basic/Cuda.h" 14 #include "clang/Config/config.h" 15 #include "clang/Basic/VirtualFileSystem.h" 16 #include "clang/Driver/Compilation.h" 17 #include "clang/Driver/Driver.h" 18 #include "clang/Driver/DriverDiagnostic.h" 19 #include "clang/Driver/Options.h" 20 #include "llvm/Option/ArgList.h" 21 #include "llvm/Support/Path.h" 22 #include <system_error> 23 24 using namespace clang::driver; 25 using namespace clang::driver::toolchains; 26 using namespace clang::driver::tools; 27 using namespace clang; 28 using namespace llvm::opt; 29 30 // Parses the contents of version.txt in an CUDA installation. It should 31 // contain one line of the from e.g. "CUDA Version 7.5.2". 32 static CudaVersion ParseCudaVersionFile(llvm::StringRef V) { 33 if (!V.startswith("CUDA Version ")) 34 return CudaVersion::UNKNOWN; 35 V = V.substr(strlen("CUDA Version ")); 36 int Major = -1, Minor = -1; 37 auto First = V.split('.'); 38 auto Second = First.second.split('.'); 39 if (First.first.getAsInteger(10, Major) || 40 Second.first.getAsInteger(10, Minor)) 41 return CudaVersion::UNKNOWN; 42 43 if (Major == 7 && Minor == 0) { 44 // This doesn't appear to ever happen -- version.txt doesn't exist in the 45 // CUDA 7 installs I've seen. But no harm in checking. 46 return CudaVersion::CUDA_70; 47 } 48 if (Major == 7 && Minor == 5) 49 return CudaVersion::CUDA_75; 50 if (Major == 8 && Minor == 0) 51 return CudaVersion::CUDA_80; 52 return CudaVersion::UNKNOWN; 53 } 54 55 CudaInstallationDetector::CudaInstallationDetector( 56 const Driver &D, const llvm::Triple &HostTriple, 57 const llvm::opt::ArgList &Args) 58 : D(D) { 59 SmallVector<std::string, 4> CudaPathCandidates; 60 61 // In decreasing order so we prefer newer versions to older versions. 62 std::initializer_list<const char *> Versions = {"8.0", "7.5", "7.0"}; 63 64 if (Args.hasArg(clang::driver::options::OPT_cuda_path_EQ)) { 65 CudaPathCandidates.push_back( 66 Args.getLastArgValue(clang::driver::options::OPT_cuda_path_EQ)); 67 } else if (HostTriple.isOSWindows()) { 68 for (const char *Ver : Versions) 69 CudaPathCandidates.push_back( 70 D.SysRoot + "/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v" + 71 Ver); 72 } else { 73 CudaPathCandidates.push_back(D.SysRoot + "/usr/local/cuda"); 74 for (const char *Ver : Versions) 75 CudaPathCandidates.push_back(D.SysRoot + "/usr/local/cuda-" + Ver); 76 } 77 78 for (const auto &CudaPath : CudaPathCandidates) { 79 if (CudaPath.empty() || !D.getVFS().exists(CudaPath)) 80 continue; 81 82 InstallPath = CudaPath; 83 BinPath = CudaPath + "/bin"; 84 IncludePath = InstallPath + "/include"; 85 LibDevicePath = InstallPath + "/nvvm/libdevice"; 86 87 auto &FS = D.getVFS(); 88 if (!(FS.exists(IncludePath) && FS.exists(BinPath) && 89 FS.exists(LibDevicePath))) 90 continue; 91 92 // On Linux, we have both lib and lib64 directories, and we need to choose 93 // based on our triple. On MacOS, we have only a lib directory. 94 // 95 // It's sufficient for our purposes to be flexible: If both lib and lib64 96 // exist, we choose whichever one matches our triple. Otherwise, if only 97 // lib exists, we use it. 98 if (HostTriple.isArch64Bit() && FS.exists(InstallPath + "/lib64")) 99 LibPath = InstallPath + "/lib64"; 100 else if (FS.exists(InstallPath + "/lib")) 101 LibPath = InstallPath + "/lib"; 102 else 103 continue; 104 105 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> VersionFile = 106 FS.getBufferForFile(InstallPath + "/version.txt"); 107 if (!VersionFile) { 108 // CUDA 7.0 doesn't have a version.txt, so guess that's our version if 109 // version.txt isn't present. 110 Version = CudaVersion::CUDA_70; 111 } else { 112 Version = ParseCudaVersionFile((*VersionFile)->getBuffer()); 113 } 114 115 std::error_code EC; 116 for (llvm::sys::fs::directory_iterator LI(LibDevicePath, EC), LE; 117 !EC && LI != LE; LI = LI.increment(EC)) { 118 StringRef FilePath = LI->path(); 119 StringRef FileName = llvm::sys::path::filename(FilePath); 120 // Process all bitcode filenames that look like libdevice.compute_XX.YY.bc 121 const StringRef LibDeviceName = "libdevice."; 122 if (!(FileName.startswith(LibDeviceName) && FileName.endswith(".bc"))) 123 continue; 124 StringRef GpuArch = FileName.slice( 125 LibDeviceName.size(), FileName.find('.', LibDeviceName.size())); 126 LibDeviceMap[GpuArch] = FilePath.str(); 127 // Insert map entries for specifc devices with this compute 128 // capability. NVCC's choice of the libdevice library version is 129 // rather peculiar and depends on the CUDA version. 130 if (GpuArch == "compute_20") { 131 LibDeviceMap["sm_20"] = FilePath; 132 LibDeviceMap["sm_21"] = FilePath; 133 LibDeviceMap["sm_32"] = FilePath; 134 } else if (GpuArch == "compute_30") { 135 LibDeviceMap["sm_30"] = FilePath; 136 if (Version < CudaVersion::CUDA_80) { 137 LibDeviceMap["sm_50"] = FilePath; 138 LibDeviceMap["sm_52"] = FilePath; 139 LibDeviceMap["sm_53"] = FilePath; 140 } 141 LibDeviceMap["sm_60"] = FilePath; 142 LibDeviceMap["sm_61"] = FilePath; 143 LibDeviceMap["sm_62"] = FilePath; 144 } else if (GpuArch == "compute_35") { 145 LibDeviceMap["sm_35"] = FilePath; 146 LibDeviceMap["sm_37"] = FilePath; 147 } else if (GpuArch == "compute_50") { 148 if (Version >= CudaVersion::CUDA_80) { 149 LibDeviceMap["sm_50"] = FilePath; 150 LibDeviceMap["sm_52"] = FilePath; 151 LibDeviceMap["sm_53"] = FilePath; 152 } 153 } 154 } 155 156 IsValid = true; 157 break; 158 } 159 } 160 161 void CudaInstallationDetector::AddCudaIncludeArgs( 162 const ArgList &DriverArgs, ArgStringList &CC1Args) const { 163 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 164 // Add cuda_wrappers/* to our system include path. This lets us wrap 165 // standard library headers. 166 SmallString<128> P(D.ResourceDir); 167 llvm::sys::path::append(P, "include"); 168 llvm::sys::path::append(P, "cuda_wrappers"); 169 CC1Args.push_back("-internal-isystem"); 170 CC1Args.push_back(DriverArgs.MakeArgString(P)); 171 } 172 173 if (DriverArgs.hasArg(options::OPT_nocudainc)) 174 return; 175 176 if (!isValid()) { 177 D.Diag(diag::err_drv_no_cuda_installation); 178 return; 179 } 180 181 CC1Args.push_back("-internal-isystem"); 182 CC1Args.push_back(DriverArgs.MakeArgString(getIncludePath())); 183 CC1Args.push_back("-include"); 184 CC1Args.push_back("__clang_cuda_runtime_wrapper.h"); 185 } 186 187 void CudaInstallationDetector::CheckCudaVersionSupportsArch( 188 CudaArch Arch) const { 189 if (Arch == CudaArch::UNKNOWN || Version == CudaVersion::UNKNOWN || 190 ArchsWithVersionTooLowErrors.count(Arch) > 0) 191 return; 192 193 auto RequiredVersion = MinVersionForCudaArch(Arch); 194 if (Version < RequiredVersion) { 195 ArchsWithVersionTooLowErrors.insert(Arch); 196 D.Diag(diag::err_drv_cuda_version_too_low) 197 << InstallPath << CudaArchToString(Arch) << CudaVersionToString(Version) 198 << CudaVersionToString(RequiredVersion); 199 } 200 } 201 202 void CudaInstallationDetector::print(raw_ostream &OS) const { 203 if (isValid()) 204 OS << "Found CUDA installation: " << InstallPath << ", version " 205 << CudaVersionToString(Version) << "\n"; 206 } 207 208 void NVPTX::Assembler::ConstructJob(Compilation &C, const JobAction &JA, 209 const InputInfo &Output, 210 const InputInfoList &Inputs, 211 const ArgList &Args, 212 const char *LinkingOutput) const { 213 const auto &TC = 214 static_cast<const toolchains::CudaToolChain &>(getToolChain()); 215 assert(TC.getTriple().isNVPTX() && "Wrong platform"); 216 217 StringRef GPUArchName; 218 // If this is an OpenMP action we need to extract the device architecture 219 // from the -march=arch option. This option may come from -Xopenmp-target 220 // flag or the default value. 221 if (JA.isDeviceOffloading(Action::OFK_OpenMP)) { 222 GPUArchName = Args.getLastArgValue(options::OPT_march_EQ); 223 assert(!GPUArchName.empty() && "Must have an architecture passed in."); 224 } else 225 GPUArchName = JA.getOffloadingArch(); 226 227 // Obtain architecture from the action. 228 CudaArch gpu_arch = StringToCudaArch(GPUArchName); 229 assert(gpu_arch != CudaArch::UNKNOWN && 230 "Device action expected to have an architecture."); 231 232 // Check that our installation's ptxas supports gpu_arch. 233 if (!Args.hasArg(options::OPT_no_cuda_version_check)) { 234 TC.CudaInstallation.CheckCudaVersionSupportsArch(gpu_arch); 235 } 236 237 ArgStringList CmdArgs; 238 CmdArgs.push_back(TC.getTriple().isArch64Bit() ? "-m64" : "-m32"); 239 if (Args.hasFlag(options::OPT_cuda_noopt_device_debug, 240 options::OPT_no_cuda_noopt_device_debug, false)) { 241 // ptxas does not accept -g option if optimization is enabled, so 242 // we ignore the compiler's -O* options if we want debug info. 243 CmdArgs.push_back("-g"); 244 CmdArgs.push_back("--dont-merge-basicblocks"); 245 CmdArgs.push_back("--return-at-end"); 246 } else if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { 247 // Map the -O we received to -O{0,1,2,3}. 248 // 249 // TODO: Perhaps we should map host -O2 to ptxas -O3. -O3 is ptxas's 250 // default, so it may correspond more closely to the spirit of clang -O2. 251 252 // -O3 seems like the least-bad option when -Osomething is specified to 253 // clang but it isn't handled below. 254 StringRef OOpt = "3"; 255 if (A->getOption().matches(options::OPT_O4) || 256 A->getOption().matches(options::OPT_Ofast)) 257 OOpt = "3"; 258 else if (A->getOption().matches(options::OPT_O0)) 259 OOpt = "0"; 260 else if (A->getOption().matches(options::OPT_O)) { 261 // -Os, -Oz, and -O(anything else) map to -O2, for lack of better options. 262 OOpt = llvm::StringSwitch<const char *>(A->getValue()) 263 .Case("1", "1") 264 .Case("2", "2") 265 .Case("3", "3") 266 .Case("s", "2") 267 .Case("z", "2") 268 .Default("2"); 269 } 270 CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt)); 271 } else { 272 // If no -O was passed, pass -O0 to ptxas -- no opt flag should correspond 273 // to no optimizations, but ptxas's default is -O3. 274 CmdArgs.push_back("-O0"); 275 } 276 277 // Pass -v to ptxas if it was passed to the driver. 278 if (Args.hasArg(options::OPT_v)) 279 CmdArgs.push_back("-v"); 280 281 CmdArgs.push_back("--gpu-name"); 282 CmdArgs.push_back(Args.MakeArgString(CudaArchToString(gpu_arch))); 283 CmdArgs.push_back("--output-file"); 284 SmallString<256> OutputFileName(Output.getFilename()); 285 if (JA.isOffloading(Action::OFK_OpenMP)) 286 llvm::sys::path::replace_extension(OutputFileName, "cubin"); 287 CmdArgs.push_back(Args.MakeArgString(OutputFileName)); 288 for (const auto& II : Inputs) 289 CmdArgs.push_back(Args.MakeArgString(II.getFilename())); 290 291 for (const auto& A : Args.getAllArgValues(options::OPT_Xcuda_ptxas)) 292 CmdArgs.push_back(Args.MakeArgString(A)); 293 294 // In OpenMP we need to generate relocatable code. 295 if (JA.isOffloading(Action::OFK_OpenMP) && 296 Args.hasFlag(options::OPT_fopenmp_relocatable_target, 297 options::OPT_fnoopenmp_relocatable_target, 298 /*Default=*/ true)) 299 CmdArgs.push_back("-c"); 300 301 const char *Exec; 302 if (Arg *A = Args.getLastArg(options::OPT_ptxas_path_EQ)) 303 Exec = A->getValue(); 304 else 305 Exec = Args.MakeArgString(TC.GetProgramPath("ptxas")); 306 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); 307 } 308 309 // All inputs to this linker must be from CudaDeviceActions, as we need to look 310 // at the Inputs' Actions in order to figure out which GPU architecture they 311 // correspond to. 312 void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA, 313 const InputInfo &Output, 314 const InputInfoList &Inputs, 315 const ArgList &Args, 316 const char *LinkingOutput) const { 317 const auto &TC = 318 static_cast<const toolchains::CudaToolChain &>(getToolChain()); 319 assert(TC.getTriple().isNVPTX() && "Wrong platform"); 320 321 ArgStringList CmdArgs; 322 CmdArgs.push_back("--cuda"); 323 CmdArgs.push_back(TC.getTriple().isArch64Bit() ? "-64" : "-32"); 324 CmdArgs.push_back(Args.MakeArgString("--create")); 325 CmdArgs.push_back(Args.MakeArgString(Output.getFilename())); 326 327 for (const auto& II : Inputs) { 328 auto *A = II.getAction(); 329 assert(A->getInputs().size() == 1 && 330 "Device offload action is expected to have a single input"); 331 const char *gpu_arch_str = A->getOffloadingArch(); 332 assert(gpu_arch_str && 333 "Device action expected to have associated a GPU architecture!"); 334 CudaArch gpu_arch = StringToCudaArch(gpu_arch_str); 335 336 // We need to pass an Arch of the form "sm_XX" for cubin files and 337 // "compute_XX" for ptx. 338 const char *Arch = 339 (II.getType() == types::TY_PP_Asm) 340 ? CudaVirtualArchToString(VirtualArchForCudaArch(gpu_arch)) 341 : gpu_arch_str; 342 CmdArgs.push_back(Args.MakeArgString(llvm::Twine("--image=profile=") + 343 Arch + ",file=" + II.getFilename())); 344 } 345 346 for (const auto& A : Args.getAllArgValues(options::OPT_Xcuda_fatbinary)) 347 CmdArgs.push_back(Args.MakeArgString(A)); 348 349 const char *Exec = Args.MakeArgString(TC.GetProgramPath("fatbinary")); 350 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); 351 } 352 353 void NVPTX::OpenMPLinker::ConstructJob(Compilation &C, const JobAction &JA, 354 const InputInfo &Output, 355 const InputInfoList &Inputs, 356 const ArgList &Args, 357 const char *LinkingOutput) const { 358 const auto &TC = 359 static_cast<const toolchains::CudaToolChain &>(getToolChain()); 360 assert(TC.getTriple().isNVPTX() && "Wrong platform"); 361 362 ArgStringList CmdArgs; 363 364 // OpenMP uses nvlink to link cubin files. The result will be embedded in the 365 // host binary by the host linker. 366 assert(!JA.isHostOffloading(Action::OFK_OpenMP) && 367 "CUDA toolchain not expected for an OpenMP host device."); 368 369 if (Output.isFilename()) { 370 CmdArgs.push_back("-o"); 371 CmdArgs.push_back(Output.getFilename()); 372 } else 373 assert(Output.isNothing() && "Invalid output."); 374 if (Args.hasArg(options::OPT_g_Flag)) 375 CmdArgs.push_back("-g"); 376 377 if (Args.hasArg(options::OPT_v)) 378 CmdArgs.push_back("-v"); 379 380 StringRef GPUArch = 381 Args.getLastArgValue(options::OPT_march_EQ); 382 assert(!GPUArch.empty() && "At least one GPU Arch required for ptxas."); 383 384 CmdArgs.push_back("-arch"); 385 CmdArgs.push_back(Args.MakeArgString(GPUArch)); 386 387 // Add paths specified in LIBRARY_PATH environment variable as -L options. 388 addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH"); 389 390 // Add paths for the default clang library path. 391 SmallString<256> DefaultLibPath = 392 llvm::sys::path::parent_path(TC.getDriver().Dir); 393 llvm::sys::path::append(DefaultLibPath, "lib" CLANG_LIBDIR_SUFFIX); 394 CmdArgs.push_back(Args.MakeArgString(Twine("-L") + DefaultLibPath)); 395 396 // Add linking against library implementing OpenMP calls on NVPTX target. 397 CmdArgs.push_back("-lomptarget-nvptx"); 398 399 for (const auto &II : Inputs) { 400 if (II.getType() == types::TY_LLVM_IR || 401 II.getType() == types::TY_LTO_IR || 402 II.getType() == types::TY_LTO_BC || 403 II.getType() == types::TY_LLVM_BC) { 404 C.getDriver().Diag(diag::err_drv_no_linker_llvm_support) 405 << getToolChain().getTripleString(); 406 continue; 407 } 408 409 // Currently, we only pass the input files to the linker, we do not pass 410 // any libraries that may be valid only for the host. 411 if (!II.isFilename()) 412 continue; 413 414 SmallString<256> Name = llvm::sys::path::filename(II.getFilename()); 415 llvm::sys::path::replace_extension(Name, "cubin"); 416 417 const char *CubinF = 418 C.addTempFile(C.getArgs().MakeArgString(Name)); 419 420 CmdArgs.push_back(CubinF); 421 } 422 423 AddOpenMPLinkerScript(getToolChain(), C, Output, Inputs, Args, CmdArgs, JA); 424 425 const char *Exec = 426 Args.MakeArgString(getToolChain().GetProgramPath("nvlink")); 427 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); 428 } 429 430 /// CUDA toolchain. Our assembler is ptxas, and our "linker" is fatbinary, 431 /// which isn't properly a linker but nonetheless performs the step of stitching 432 /// together object files from the assembler into a single blob. 433 434 CudaToolChain::CudaToolChain(const Driver &D, const llvm::Triple &Triple, 435 const ToolChain &HostTC, const ArgList &Args, 436 const Action::OffloadKind OK) 437 : ToolChain(D, Triple, Args), HostTC(HostTC), 438 CudaInstallation(D, HostTC.getTriple(), Args), OK(OK) { 439 if (CudaInstallation.isValid()) 440 getProgramPaths().push_back(CudaInstallation.getBinPath()); 441 // Lookup binaries into the driver directory, this is used to 442 // discover the clang-offload-bundler executable. 443 getProgramPaths().push_back(getDriver().Dir); 444 } 445 446 void CudaToolChain::addClangTargetOptions( 447 const llvm::opt::ArgList &DriverArgs, 448 llvm::opt::ArgStringList &CC1Args, 449 Action::OffloadKind DeviceOffloadingKind) const { 450 HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind); 451 452 StringRef GpuArch = DriverArgs.getLastArgValue(options::OPT_march_EQ); 453 assert(!GpuArch.empty() && "Must have an explicit GPU arch."); 454 assert((DeviceOffloadingKind == Action::OFK_OpenMP || 455 DeviceOffloadingKind == Action::OFK_Cuda) && 456 "Only OpenMP or CUDA offloading kinds are supported for NVIDIA GPUs."); 457 458 if (DeviceOffloadingKind == Action::OFK_Cuda) { 459 CC1Args.push_back("-fcuda-is-device"); 460 461 if (DriverArgs.hasFlag(options::OPT_fcuda_flush_denormals_to_zero, 462 options::OPT_fno_cuda_flush_denormals_to_zero, false)) 463 CC1Args.push_back("-fcuda-flush-denormals-to-zero"); 464 465 if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals, 466 options::OPT_fno_cuda_approx_transcendentals, false)) 467 CC1Args.push_back("-fcuda-approx-transcendentals"); 468 469 if (DriverArgs.hasArg(options::OPT_nocudalib)) 470 return; 471 } 472 473 std::string LibDeviceFile = CudaInstallation.getLibDeviceFile(GpuArch); 474 475 if (LibDeviceFile.empty()) { 476 getDriver().Diag(diag::err_drv_no_cuda_libdevice) << GpuArch; 477 return; 478 } 479 480 CC1Args.push_back("-mlink-cuda-bitcode"); 481 CC1Args.push_back(DriverArgs.MakeArgString(LibDeviceFile)); 482 483 // Libdevice in CUDA-7.0 requires PTX version that's more recent 484 // than LLVM defaults to. Use PTX4.2 which is the PTX version that 485 // came with CUDA-7.0. 486 CC1Args.push_back("-target-feature"); 487 CC1Args.push_back("+ptx42"); 488 } 489 490 void CudaToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs, 491 ArgStringList &CC1Args) const { 492 // Check our CUDA version if we're going to include the CUDA headers. 493 if (!DriverArgs.hasArg(options::OPT_nocudainc) && 494 !DriverArgs.hasArg(options::OPT_no_cuda_version_check)) { 495 StringRef Arch = DriverArgs.getLastArgValue(options::OPT_march_EQ); 496 assert(!Arch.empty() && "Must have an explicit GPU arch."); 497 CudaInstallation.CheckCudaVersionSupportsArch(StringToCudaArch(Arch)); 498 } 499 CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args); 500 } 501 502 llvm::opt::DerivedArgList * 503 CudaToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args, 504 StringRef BoundArch, 505 Action::OffloadKind DeviceOffloadKind) const { 506 DerivedArgList *DAL = 507 HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind); 508 if (!DAL) 509 DAL = new DerivedArgList(Args.getBaseArgs()); 510 511 const OptTable &Opts = getDriver().getOpts(); 512 513 // For OpenMP device offloading, append derived arguments. Make sure 514 // flags are not duplicated. 515 // Also append the compute capability. 516 if (DeviceOffloadKind == Action::OFK_OpenMP) { 517 for (Arg *A : Args){ 518 bool IsDuplicate = false; 519 for (Arg *DALArg : *DAL){ 520 if (A == DALArg) { 521 IsDuplicate = true; 522 break; 523 } 524 } 525 if (!IsDuplicate) 526 DAL->append(A); 527 } 528 529 StringRef Arch = DAL->getLastArgValue(options::OPT_march_EQ); 530 if (Arch.empty()) 531 // Default compute capability for CUDA toolchain is sm_20. 532 DAL->AddJoinedArg(nullptr, 533 Opts.getOption(options::OPT_march_EQ), "sm_20"); 534 535 return DAL; 536 } 537 538 for (Arg *A : Args) { 539 if (A->getOption().matches(options::OPT_Xarch__)) { 540 // Skip this argument unless the architecture matches BoundArch 541 if (BoundArch.empty() || A->getValue(0) != BoundArch) 542 continue; 543 544 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1)); 545 unsigned Prev = Index; 546 std::unique_ptr<Arg> XarchArg(Opts.ParseOneArg(Args, Index)); 547 548 // If the argument parsing failed or more than one argument was 549 // consumed, the -Xarch_ argument's parameter tried to consume 550 // extra arguments. Emit an error and ignore. 551 // 552 // We also want to disallow any options which would alter the 553 // driver behavior; that isn't going to work in our model. We 554 // use isDriverOption() as an approximation, although things 555 // like -O4 are going to slip through. 556 if (!XarchArg || Index > Prev + 1) { 557 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args) 558 << A->getAsString(Args); 559 continue; 560 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) { 561 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver) 562 << A->getAsString(Args); 563 continue; 564 } 565 XarchArg->setBaseArg(A); 566 A = XarchArg.release(); 567 DAL->AddSynthesizedArg(A); 568 } 569 DAL->append(A); 570 } 571 572 if (!BoundArch.empty()) { 573 DAL->eraseArg(options::OPT_march_EQ); 574 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ), BoundArch); 575 } 576 return DAL; 577 } 578 579 Tool *CudaToolChain::buildAssembler() const { 580 return new tools::NVPTX::Assembler(*this); 581 } 582 583 Tool *CudaToolChain::buildLinker() const { 584 if (OK == Action::OFK_OpenMP) 585 return new tools::NVPTX::OpenMPLinker(*this); 586 return new tools::NVPTX::Linker(*this); 587 } 588 589 void CudaToolChain::addClangWarningOptions(ArgStringList &CC1Args) const { 590 HostTC.addClangWarningOptions(CC1Args); 591 } 592 593 ToolChain::CXXStdlibType 594 CudaToolChain::GetCXXStdlibType(const ArgList &Args) const { 595 return HostTC.GetCXXStdlibType(Args); 596 } 597 598 void CudaToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 599 ArgStringList &CC1Args) const { 600 HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args); 601 } 602 603 void CudaToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &Args, 604 ArgStringList &CC1Args) const { 605 HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args); 606 } 607 608 void CudaToolChain::AddIAMCUIncludeArgs(const ArgList &Args, 609 ArgStringList &CC1Args) const { 610 HostTC.AddIAMCUIncludeArgs(Args, CC1Args); 611 } 612 613 SanitizerMask CudaToolChain::getSupportedSanitizers() const { 614 // The CudaToolChain only supports sanitizers in the sense that it allows 615 // sanitizer arguments on the command line if they are supported by the host 616 // toolchain. The CudaToolChain will actually ignore any command line 617 // arguments for any of these "supported" sanitizers. That means that no 618 // sanitization of device code is actually supported at this time. 619 // 620 // This behavior is necessary because the host and device toolchains 621 // invocations often share the command line, so the device toolchain must 622 // tolerate flags meant only for the host toolchain. 623 return HostTC.getSupportedSanitizers(); 624 } 625 626 VersionTuple CudaToolChain::computeMSVCVersion(const Driver *D, 627 const ArgList &Args) const { 628 return HostTC.computeMSVCVersion(D, Args); 629 } 630