1 //===--- Cuda.cpp - Cuda Tool and ToolChain Implementations -----*- C++ -*-===// 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 "Cuda.h" 10 #include "CommonArgs.h" 11 #include "clang/Basic/Cuda.h" 12 #include "clang/Config/config.h" 13 #include "clang/Driver/Compilation.h" 14 #include "clang/Driver/Distro.h" 15 #include "clang/Driver/Driver.h" 16 #include "clang/Driver/DriverDiagnostic.h" 17 #include "clang/Driver/InputInfo.h" 18 #include "clang/Driver/Options.h" 19 #include "llvm/ADT/Optional.h" 20 #include "llvm/ADT/StringExtras.h" 21 #include "llvm/Option/ArgList.h" 22 #include "llvm/Support/FileSystem.h" 23 #include "llvm/Support/Host.h" 24 #include "llvm/Support/Path.h" 25 #include "llvm/Support/Process.h" 26 #include "llvm/Support/Program.h" 27 #include "llvm/Support/TargetParser.h" 28 #include "llvm/Support/VirtualFileSystem.h" 29 #include <system_error> 30 31 using namespace clang::driver; 32 using namespace clang::driver::toolchains; 33 using namespace clang::driver::tools; 34 using namespace clang; 35 using namespace llvm::opt; 36 37 namespace { 38 39 CudaVersion getCudaVersion(uint32_t raw_version) { 40 if (raw_version < 7050) 41 return CudaVersion::CUDA_70; 42 if (raw_version < 8000) 43 return CudaVersion::CUDA_75; 44 if (raw_version < 9000) 45 return CudaVersion::CUDA_80; 46 if (raw_version < 9010) 47 return CudaVersion::CUDA_90; 48 if (raw_version < 9020) 49 return CudaVersion::CUDA_91; 50 if (raw_version < 10000) 51 return CudaVersion::CUDA_92; 52 if (raw_version < 10010) 53 return CudaVersion::CUDA_100; 54 if (raw_version < 10020) 55 return CudaVersion::CUDA_101; 56 if (raw_version < 11000) 57 return CudaVersion::CUDA_102; 58 if (raw_version < 11010) 59 return CudaVersion::CUDA_110; 60 if (raw_version < 11020) 61 return CudaVersion::CUDA_111; 62 if (raw_version < 11030) 63 return CudaVersion::CUDA_112; 64 if (raw_version < 11040) 65 return CudaVersion::CUDA_113; 66 if (raw_version < 11050) 67 return CudaVersion::CUDA_114; 68 return CudaVersion::NEW; 69 } 70 71 CudaVersion parseCudaHFile(llvm::StringRef Input) { 72 // Helper lambda which skips the words if the line starts with them or returns 73 // None otherwise. 74 auto StartsWithWords = 75 [](llvm::StringRef Line, 76 const SmallVector<StringRef, 3> words) -> llvm::Optional<StringRef> { 77 for (StringRef word : words) { 78 if (!Line.consume_front(word)) 79 return {}; 80 Line = Line.ltrim(); 81 } 82 return Line; 83 }; 84 85 Input = Input.ltrim(); 86 while (!Input.empty()) { 87 if (auto Line = 88 StartsWithWords(Input.ltrim(), {"#", "define", "CUDA_VERSION"})) { 89 uint32_t RawVersion; 90 Line->consumeInteger(10, RawVersion); 91 return getCudaVersion(RawVersion); 92 } 93 // Find next non-empty line. 94 Input = Input.drop_front(Input.find_first_of("\n\r")).ltrim(); 95 } 96 return CudaVersion::UNKNOWN; 97 } 98 } // namespace 99 100 void CudaInstallationDetector::WarnIfUnsupportedVersion() { 101 if (Version > CudaVersion::PARTIALLY_SUPPORTED) { 102 std::string VersionString = CudaVersionToString(Version); 103 if (!VersionString.empty()) 104 VersionString.insert(0, " "); 105 D.Diag(diag::warn_drv_new_cuda_version) 106 << VersionString 107 << (CudaVersion::PARTIALLY_SUPPORTED != CudaVersion::FULLY_SUPPORTED) 108 << CudaVersionToString(CudaVersion::PARTIALLY_SUPPORTED); 109 } else if (Version > CudaVersion::FULLY_SUPPORTED) 110 D.Diag(diag::warn_drv_partially_supported_cuda_version) 111 << CudaVersionToString(Version); 112 } 113 114 CudaInstallationDetector::CudaInstallationDetector( 115 const Driver &D, const llvm::Triple &HostTriple, 116 const llvm::opt::ArgList &Args) 117 : D(D) { 118 struct Candidate { 119 std::string Path; 120 bool StrictChecking; 121 122 Candidate(std::string Path, bool StrictChecking = false) 123 : Path(Path), StrictChecking(StrictChecking) {} 124 }; 125 SmallVector<Candidate, 4> Candidates; 126 127 // In decreasing order so we prefer newer versions to older versions. 128 std::initializer_list<const char *> Versions = {"8.0", "7.5", "7.0"}; 129 auto &FS = D.getVFS(); 130 131 if (Args.hasArg(clang::driver::options::OPT_cuda_path_EQ)) { 132 Candidates.emplace_back( 133 Args.getLastArgValue(clang::driver::options::OPT_cuda_path_EQ).str()); 134 } else if (HostTriple.isOSWindows()) { 135 for (const char *Ver : Versions) 136 Candidates.emplace_back( 137 D.SysRoot + "/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v" + 138 Ver); 139 } else { 140 if (!Args.hasArg(clang::driver::options::OPT_cuda_path_ignore_env)) { 141 // Try to find ptxas binary. If the executable is located in a directory 142 // called 'bin/', its parent directory might be a good guess for a valid 143 // CUDA installation. 144 // However, some distributions might installs 'ptxas' to /usr/bin. In that 145 // case the candidate would be '/usr' which passes the following checks 146 // because '/usr/include' exists as well. To avoid this case, we always 147 // check for the directory potentially containing files for libdevice, 148 // even if the user passes -nocudalib. 149 if (llvm::ErrorOr<std::string> ptxas = 150 llvm::sys::findProgramByName("ptxas")) { 151 SmallString<256> ptxasAbsolutePath; 152 llvm::sys::fs::real_path(*ptxas, ptxasAbsolutePath); 153 154 StringRef ptxasDir = llvm::sys::path::parent_path(ptxasAbsolutePath); 155 if (llvm::sys::path::filename(ptxasDir) == "bin") 156 Candidates.emplace_back( 157 std::string(llvm::sys::path::parent_path(ptxasDir)), 158 /*StrictChecking=*/true); 159 } 160 } 161 162 Candidates.emplace_back(D.SysRoot + "/usr/local/cuda"); 163 for (const char *Ver : Versions) 164 Candidates.emplace_back(D.SysRoot + "/usr/local/cuda-" + Ver); 165 166 Distro Dist(FS, llvm::Triple(llvm::sys::getProcessTriple())); 167 if (Dist.IsDebian() || Dist.IsUbuntu()) 168 // Special case for Debian to have nvidia-cuda-toolkit work 169 // out of the box. More info on http://bugs.debian.org/882505 170 Candidates.emplace_back(D.SysRoot + "/usr/lib/cuda"); 171 } 172 173 bool NoCudaLib = Args.hasArg(options::OPT_nogpulib); 174 175 for (const auto &Candidate : Candidates) { 176 InstallPath = Candidate.Path; 177 if (InstallPath.empty() || !FS.exists(InstallPath)) 178 continue; 179 180 BinPath = InstallPath + "/bin"; 181 IncludePath = InstallPath + "/include"; 182 LibDevicePath = InstallPath + "/nvvm/libdevice"; 183 184 if (!(FS.exists(IncludePath) && FS.exists(BinPath))) 185 continue; 186 bool CheckLibDevice = (!NoCudaLib || Candidate.StrictChecking); 187 if (CheckLibDevice && !FS.exists(LibDevicePath)) 188 continue; 189 190 // On Linux, we have both lib and lib64 directories, and we need to choose 191 // based on our triple. On MacOS, we have only a lib directory. 192 // 193 // It's sufficient for our purposes to be flexible: If both lib and lib64 194 // exist, we choose whichever one matches our triple. Otherwise, if only 195 // lib exists, we use it. 196 if (HostTriple.isArch64Bit() && FS.exists(InstallPath + "/lib64")) 197 LibPath = InstallPath + "/lib64"; 198 else if (FS.exists(InstallPath + "/lib")) 199 LibPath = InstallPath + "/lib"; 200 else 201 continue; 202 203 Version = CudaVersion::UNKNOWN; 204 if (auto CudaHFile = FS.getBufferForFile(InstallPath + "/include/cuda.h")) 205 Version = parseCudaHFile((*CudaHFile)->getBuffer()); 206 // As the last resort, make an educated guess between CUDA-7.0, which had 207 // old-style libdevice bitcode, and an unknown recent CUDA version. 208 if (Version == CudaVersion::UNKNOWN) { 209 Version = FS.exists(LibDevicePath + "/libdevice.10.bc") 210 ? CudaVersion::NEW 211 : CudaVersion::CUDA_70; 212 } 213 214 if (Version >= CudaVersion::CUDA_90) { 215 // CUDA-9+ uses single libdevice file for all GPU variants. 216 std::string FilePath = LibDevicePath + "/libdevice.10.bc"; 217 if (FS.exists(FilePath)) { 218 for (int Arch = (int)CudaArch::SM_30, E = (int)CudaArch::LAST; Arch < E; 219 ++Arch) { 220 CudaArch GpuArch = static_cast<CudaArch>(Arch); 221 if (!IsNVIDIAGpuArch(GpuArch)) 222 continue; 223 std::string GpuArchName(CudaArchToString(GpuArch)); 224 LibDeviceMap[GpuArchName] = FilePath; 225 } 226 } 227 } else { 228 std::error_code EC; 229 for (llvm::vfs::directory_iterator LI = FS.dir_begin(LibDevicePath, EC), 230 LE; 231 !EC && LI != LE; LI = LI.increment(EC)) { 232 StringRef FilePath = LI->path(); 233 StringRef FileName = llvm::sys::path::filename(FilePath); 234 // Process all bitcode filenames that look like 235 // libdevice.compute_XX.YY.bc 236 const StringRef LibDeviceName = "libdevice."; 237 if (!(FileName.startswith(LibDeviceName) && FileName.endswith(".bc"))) 238 continue; 239 StringRef GpuArch = FileName.slice( 240 LibDeviceName.size(), FileName.find('.', LibDeviceName.size())); 241 LibDeviceMap[GpuArch] = FilePath.str(); 242 // Insert map entries for specific devices with this compute 243 // capability. NVCC's choice of the libdevice library version is 244 // rather peculiar and depends on the CUDA version. 245 if (GpuArch == "compute_20") { 246 LibDeviceMap["sm_20"] = std::string(FilePath); 247 LibDeviceMap["sm_21"] = std::string(FilePath); 248 LibDeviceMap["sm_32"] = std::string(FilePath); 249 } else if (GpuArch == "compute_30") { 250 LibDeviceMap["sm_30"] = std::string(FilePath); 251 if (Version < CudaVersion::CUDA_80) { 252 LibDeviceMap["sm_50"] = std::string(FilePath); 253 LibDeviceMap["sm_52"] = std::string(FilePath); 254 LibDeviceMap["sm_53"] = std::string(FilePath); 255 } 256 LibDeviceMap["sm_60"] = std::string(FilePath); 257 LibDeviceMap["sm_61"] = std::string(FilePath); 258 LibDeviceMap["sm_62"] = std::string(FilePath); 259 } else if (GpuArch == "compute_35") { 260 LibDeviceMap["sm_35"] = std::string(FilePath); 261 LibDeviceMap["sm_37"] = std::string(FilePath); 262 } else if (GpuArch == "compute_50") { 263 if (Version >= CudaVersion::CUDA_80) { 264 LibDeviceMap["sm_50"] = std::string(FilePath); 265 LibDeviceMap["sm_52"] = std::string(FilePath); 266 LibDeviceMap["sm_53"] = std::string(FilePath); 267 } 268 } 269 } 270 } 271 272 // Check that we have found at least one libdevice that we can link in if 273 // -nocudalib hasn't been specified. 274 if (LibDeviceMap.empty() && !NoCudaLib) 275 continue; 276 277 IsValid = true; 278 break; 279 } 280 } 281 282 void CudaInstallationDetector::AddCudaIncludeArgs( 283 const ArgList &DriverArgs, ArgStringList &CC1Args) const { 284 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 285 // Add cuda_wrappers/* to our system include path. This lets us wrap 286 // standard library headers. 287 SmallString<128> P(D.ResourceDir); 288 llvm::sys::path::append(P, "include"); 289 llvm::sys::path::append(P, "cuda_wrappers"); 290 CC1Args.push_back("-internal-isystem"); 291 CC1Args.push_back(DriverArgs.MakeArgString(P)); 292 } 293 294 if (DriverArgs.hasArg(options::OPT_nogpuinc)) 295 return; 296 297 if (!isValid()) { 298 D.Diag(diag::err_drv_no_cuda_installation); 299 return; 300 } 301 302 CC1Args.push_back("-include"); 303 CC1Args.push_back("__clang_cuda_runtime_wrapper.h"); 304 } 305 306 void CudaInstallationDetector::CheckCudaVersionSupportsArch( 307 CudaArch Arch) const { 308 if (Arch == CudaArch::UNKNOWN || Version == CudaVersion::UNKNOWN || 309 ArchsWithBadVersion[(int)Arch]) 310 return; 311 312 auto MinVersion = MinVersionForCudaArch(Arch); 313 auto MaxVersion = MaxVersionForCudaArch(Arch); 314 if (Version < MinVersion || Version > MaxVersion) { 315 ArchsWithBadVersion[(int)Arch] = true; 316 D.Diag(diag::err_drv_cuda_version_unsupported) 317 << CudaArchToString(Arch) << CudaVersionToString(MinVersion) 318 << CudaVersionToString(MaxVersion) << InstallPath 319 << CudaVersionToString(Version); 320 } 321 } 322 323 void CudaInstallationDetector::print(raw_ostream &OS) const { 324 if (isValid()) 325 OS << "Found CUDA installation: " << InstallPath << ", version " 326 << CudaVersionToString(Version) << "\n"; 327 } 328 329 namespace { 330 /// Debug info level for the NVPTX devices. We may need to emit different debug 331 /// info level for the host and for the device itselfi. This type controls 332 /// emission of the debug info for the devices. It either prohibits disable info 333 /// emission completely, or emits debug directives only, or emits same debug 334 /// info as for the host. 335 enum DeviceDebugInfoLevel { 336 DisableDebugInfo, /// Do not emit debug info for the devices. 337 DebugDirectivesOnly, /// Emit only debug directives. 338 EmitSameDebugInfoAsHost, /// Use the same debug info level just like for the 339 /// host. 340 }; 341 } // anonymous namespace 342 343 /// Define debug info level for the NVPTX devices. If the debug info for both 344 /// the host and device are disabled (-g0/-ggdb0 or no debug options at all). If 345 /// only debug directives are requested for the both host and device 346 /// (-gline-directvies-only), or the debug info only for the device is disabled 347 /// (optimization is on and --cuda-noopt-device-debug was not specified), the 348 /// debug directves only must be emitted for the device. Otherwise, use the same 349 /// debug info level just like for the host (with the limitations of only 350 /// supported DWARF2 standard). 351 static DeviceDebugInfoLevel mustEmitDebugInfo(const ArgList &Args) { 352 const Arg *A = Args.getLastArg(options::OPT_O_Group); 353 bool IsDebugEnabled = !A || A->getOption().matches(options::OPT_O0) || 354 Args.hasFlag(options::OPT_cuda_noopt_device_debug, 355 options::OPT_no_cuda_noopt_device_debug, 356 /*Default=*/false); 357 if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) { 358 const Option &Opt = A->getOption(); 359 if (Opt.matches(options::OPT_gN_Group)) { 360 if (Opt.matches(options::OPT_g0) || Opt.matches(options::OPT_ggdb0)) 361 return DisableDebugInfo; 362 if (Opt.matches(options::OPT_gline_directives_only)) 363 return DebugDirectivesOnly; 364 } 365 return IsDebugEnabled ? EmitSameDebugInfoAsHost : DebugDirectivesOnly; 366 } 367 return willEmitRemarks(Args) ? DebugDirectivesOnly : DisableDebugInfo; 368 } 369 370 void NVPTX::Assembler::ConstructJob(Compilation &C, const JobAction &JA, 371 const InputInfo &Output, 372 const InputInfoList &Inputs, 373 const ArgList &Args, 374 const char *LinkingOutput) const { 375 const auto &TC = 376 static_cast<const toolchains::CudaToolChain &>(getToolChain()); 377 assert(TC.getTriple().isNVPTX() && "Wrong platform"); 378 379 StringRef GPUArchName; 380 // If this is an OpenMP action we need to extract the device architecture 381 // from the -march=arch option. This option may come from -Xopenmp-target 382 // flag or the default value. 383 if (JA.isDeviceOffloading(Action::OFK_OpenMP)) { 384 GPUArchName = Args.getLastArgValue(options::OPT_march_EQ); 385 assert(!GPUArchName.empty() && "Must have an architecture passed in."); 386 } else 387 GPUArchName = JA.getOffloadingArch(); 388 389 // Obtain architecture from the action. 390 CudaArch gpu_arch = StringToCudaArch(GPUArchName); 391 assert(gpu_arch != CudaArch::UNKNOWN && 392 "Device action expected to have an architecture."); 393 394 // Check that our installation's ptxas supports gpu_arch. 395 if (!Args.hasArg(options::OPT_no_cuda_version_check)) { 396 TC.CudaInstallation.CheckCudaVersionSupportsArch(gpu_arch); 397 } 398 399 ArgStringList CmdArgs; 400 CmdArgs.push_back(TC.getTriple().isArch64Bit() ? "-m64" : "-m32"); 401 DeviceDebugInfoLevel DIKind = mustEmitDebugInfo(Args); 402 if (DIKind == EmitSameDebugInfoAsHost) { 403 // ptxas does not accept -g option if optimization is enabled, so 404 // we ignore the compiler's -O* options if we want debug info. 405 CmdArgs.push_back("-g"); 406 CmdArgs.push_back("--dont-merge-basicblocks"); 407 CmdArgs.push_back("--return-at-end"); 408 } else if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { 409 // Map the -O we received to -O{0,1,2,3}. 410 // 411 // TODO: Perhaps we should map host -O2 to ptxas -O3. -O3 is ptxas's 412 // default, so it may correspond more closely to the spirit of clang -O2. 413 414 // -O3 seems like the least-bad option when -Osomething is specified to 415 // clang but it isn't handled below. 416 StringRef OOpt = "3"; 417 if (A->getOption().matches(options::OPT_O4) || 418 A->getOption().matches(options::OPT_Ofast)) 419 OOpt = "3"; 420 else if (A->getOption().matches(options::OPT_O0)) 421 OOpt = "0"; 422 else if (A->getOption().matches(options::OPT_O)) { 423 // -Os, -Oz, and -O(anything else) map to -O2, for lack of better options. 424 OOpt = llvm::StringSwitch<const char *>(A->getValue()) 425 .Case("1", "1") 426 .Case("2", "2") 427 .Case("3", "3") 428 .Case("s", "2") 429 .Case("z", "2") 430 .Default("2"); 431 } 432 CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt)); 433 } else { 434 // If no -O was passed, pass -O0 to ptxas -- no opt flag should correspond 435 // to no optimizations, but ptxas's default is -O3. 436 CmdArgs.push_back("-O0"); 437 } 438 if (DIKind == DebugDirectivesOnly) 439 CmdArgs.push_back("-lineinfo"); 440 441 // Pass -v to ptxas if it was passed to the driver. 442 if (Args.hasArg(options::OPT_v)) 443 CmdArgs.push_back("-v"); 444 445 CmdArgs.push_back("--gpu-name"); 446 CmdArgs.push_back(Args.MakeArgString(CudaArchToString(gpu_arch))); 447 CmdArgs.push_back("--output-file"); 448 CmdArgs.push_back(Args.MakeArgString(TC.getInputFilename(Output))); 449 for (const auto& II : Inputs) 450 CmdArgs.push_back(Args.MakeArgString(II.getFilename())); 451 452 for (const auto& A : Args.getAllArgValues(options::OPT_Xcuda_ptxas)) 453 CmdArgs.push_back(Args.MakeArgString(A)); 454 455 bool Relocatable = false; 456 if (JA.isOffloading(Action::OFK_OpenMP)) 457 // In OpenMP we need to generate relocatable code. 458 Relocatable = Args.hasFlag(options::OPT_fopenmp_relocatable_target, 459 options::OPT_fnoopenmp_relocatable_target, 460 /*Default=*/true); 461 else if (JA.isOffloading(Action::OFK_Cuda)) 462 Relocatable = Args.hasFlag(options::OPT_fgpu_rdc, 463 options::OPT_fno_gpu_rdc, /*Default=*/false); 464 465 if (Relocatable) 466 CmdArgs.push_back("-c"); 467 468 const char *Exec; 469 if (Arg *A = Args.getLastArg(options::OPT_ptxas_path_EQ)) 470 Exec = A->getValue(); 471 else 472 Exec = Args.MakeArgString(TC.GetProgramPath("ptxas")); 473 C.addCommand(std::make_unique<Command>( 474 JA, *this, 475 ResponseFileSupport{ResponseFileSupport::RF_Full, llvm::sys::WEM_UTF8, 476 "--options-file"}, 477 Exec, CmdArgs, Inputs, Output)); 478 } 479 480 static bool shouldIncludePTX(const ArgList &Args, const char *gpu_arch) { 481 bool includePTX = true; 482 for (Arg *A : Args) { 483 if (!(A->getOption().matches(options::OPT_cuda_include_ptx_EQ) || 484 A->getOption().matches(options::OPT_no_cuda_include_ptx_EQ))) 485 continue; 486 A->claim(); 487 const StringRef ArchStr = A->getValue(); 488 if (ArchStr == "all" || ArchStr == gpu_arch) { 489 includePTX = A->getOption().matches(options::OPT_cuda_include_ptx_EQ); 490 continue; 491 } 492 } 493 return includePTX; 494 } 495 496 // All inputs to this linker must be from CudaDeviceActions, as we need to look 497 // at the Inputs' Actions in order to figure out which GPU architecture they 498 // correspond to. 499 void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA, 500 const InputInfo &Output, 501 const InputInfoList &Inputs, 502 const ArgList &Args, 503 const char *LinkingOutput) const { 504 const auto &TC = 505 static_cast<const toolchains::CudaToolChain &>(getToolChain()); 506 assert(TC.getTriple().isNVPTX() && "Wrong platform"); 507 508 ArgStringList CmdArgs; 509 if (TC.CudaInstallation.version() <= CudaVersion::CUDA_100) 510 CmdArgs.push_back("--cuda"); 511 CmdArgs.push_back(TC.getTriple().isArch64Bit() ? "-64" : "-32"); 512 CmdArgs.push_back(Args.MakeArgString("--create")); 513 CmdArgs.push_back(Args.MakeArgString(Output.getFilename())); 514 if (mustEmitDebugInfo(Args) == EmitSameDebugInfoAsHost) 515 CmdArgs.push_back("-g"); 516 517 for (const auto& II : Inputs) { 518 auto *A = II.getAction(); 519 assert(A->getInputs().size() == 1 && 520 "Device offload action is expected to have a single input"); 521 const char *gpu_arch_str = A->getOffloadingArch(); 522 assert(gpu_arch_str && 523 "Device action expected to have associated a GPU architecture!"); 524 CudaArch gpu_arch = StringToCudaArch(gpu_arch_str); 525 526 if (II.getType() == types::TY_PP_Asm && 527 !shouldIncludePTX(Args, gpu_arch_str)) 528 continue; 529 // We need to pass an Arch of the form "sm_XX" for cubin files and 530 // "compute_XX" for ptx. 531 const char *Arch = (II.getType() == types::TY_PP_Asm) 532 ? CudaArchToVirtualArchString(gpu_arch) 533 : gpu_arch_str; 534 CmdArgs.push_back(Args.MakeArgString(llvm::Twine("--image=profile=") + 535 Arch + ",file=" + II.getFilename())); 536 } 537 538 for (const auto& A : Args.getAllArgValues(options::OPT_Xcuda_fatbinary)) 539 CmdArgs.push_back(Args.MakeArgString(A)); 540 541 const char *Exec = Args.MakeArgString(TC.GetProgramPath("fatbinary")); 542 C.addCommand(std::make_unique<Command>( 543 JA, *this, 544 ResponseFileSupport{ResponseFileSupport::RF_Full, llvm::sys::WEM_UTF8, 545 "--options-file"}, 546 Exec, CmdArgs, Inputs, Output)); 547 } 548 549 void NVPTX::OpenMPLinker::ConstructJob(Compilation &C, const JobAction &JA, 550 const InputInfo &Output, 551 const InputInfoList &Inputs, 552 const ArgList &Args, 553 const char *LinkingOutput) const { 554 const auto &TC = 555 static_cast<const toolchains::CudaToolChain &>(getToolChain()); 556 assert(TC.getTriple().isNVPTX() && "Wrong platform"); 557 558 ArgStringList CmdArgs; 559 560 // OpenMP uses nvlink to link cubin files. The result will be embedded in the 561 // host binary by the host linker. 562 assert(!JA.isHostOffloading(Action::OFK_OpenMP) && 563 "CUDA toolchain not expected for an OpenMP host device."); 564 565 if (Output.isFilename()) { 566 CmdArgs.push_back("-o"); 567 CmdArgs.push_back(Output.getFilename()); 568 } else 569 assert(Output.isNothing() && "Invalid output."); 570 if (mustEmitDebugInfo(Args) == EmitSameDebugInfoAsHost) 571 CmdArgs.push_back("-g"); 572 573 if (Args.hasArg(options::OPT_v)) 574 CmdArgs.push_back("-v"); 575 576 StringRef GPUArch = 577 Args.getLastArgValue(options::OPT_march_EQ); 578 assert(!GPUArch.empty() && "At least one GPU Arch required for ptxas."); 579 580 CmdArgs.push_back("-arch"); 581 CmdArgs.push_back(Args.MakeArgString(GPUArch)); 582 583 // Add paths specified in LIBRARY_PATH environment variable as -L options. 584 addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH"); 585 586 // Add paths for the default clang library path. 587 SmallString<256> DefaultLibPath = 588 llvm::sys::path::parent_path(TC.getDriver().Dir); 589 llvm::sys::path::append(DefaultLibPath, "lib" CLANG_LIBDIR_SUFFIX); 590 CmdArgs.push_back(Args.MakeArgString(Twine("-L") + DefaultLibPath)); 591 592 for (const auto &II : Inputs) { 593 if (II.getType() == types::TY_LLVM_IR || 594 II.getType() == types::TY_LTO_IR || 595 II.getType() == types::TY_LTO_BC || 596 II.getType() == types::TY_LLVM_BC) { 597 C.getDriver().Diag(diag::err_drv_no_linker_llvm_support) 598 << getToolChain().getTripleString(); 599 continue; 600 } 601 602 // Currently, we only pass the input files to the linker, we do not pass 603 // any libraries that may be valid only for the host. 604 if (!II.isFilename()) 605 continue; 606 607 const char *CubinF = C.addTempFile( 608 C.getArgs().MakeArgString(getToolChain().getInputFilename(II))); 609 610 CmdArgs.push_back(CubinF); 611 } 612 613 AddStaticDeviceLibsLinking(C, *this, JA, Inputs, Args, CmdArgs, "nvptx", GPUArch, 614 false, false); 615 616 // Find nvlink and pass it as "--nvlink-path=" argument of 617 // clang-nvlink-wrapper. 618 CmdArgs.push_back(Args.MakeArgString( 619 Twine("--nvlink-path=" + getToolChain().GetProgramPath("nvlink")))); 620 621 const char *Exec = 622 Args.MakeArgString(getToolChain().GetProgramPath("clang-nvlink-wrapper")); 623 C.addCommand(std::make_unique<Command>( 624 JA, *this, 625 ResponseFileSupport{ResponseFileSupport::RF_Full, llvm::sys::WEM_UTF8, 626 "--options-file"}, 627 Exec, CmdArgs, Inputs, Output)); 628 } 629 630 /// CUDA toolchain. Our assembler is ptxas, and our "linker" is fatbinary, 631 /// which isn't properly a linker but nonetheless performs the step of stitching 632 /// together object files from the assembler into a single blob. 633 634 CudaToolChain::CudaToolChain(const Driver &D, const llvm::Triple &Triple, 635 const ToolChain &HostTC, const ArgList &Args, 636 const Action::OffloadKind OK) 637 : ToolChain(D, Triple, Args), HostTC(HostTC), 638 CudaInstallation(D, HostTC.getTriple(), Args), OK(OK) { 639 if (CudaInstallation.isValid()) { 640 CudaInstallation.WarnIfUnsupportedVersion(); 641 getProgramPaths().push_back(std::string(CudaInstallation.getBinPath())); 642 } 643 // Lookup binaries into the driver directory, this is used to 644 // discover the clang-offload-bundler executable. 645 getProgramPaths().push_back(getDriver().Dir); 646 } 647 648 std::string CudaToolChain::getInputFilename(const InputInfo &Input) const { 649 // Only object files are changed, for example assembly files keep their .s 650 // extensions. CUDA also continues to use .o as they don't use nvlink but 651 // fatbinary. 652 if (!(OK == Action::OFK_OpenMP && Input.getType() == types::TY_Object)) 653 return ToolChain::getInputFilename(Input); 654 655 // Replace extension for object files with cubin because nvlink relies on 656 // these particular file names. 657 SmallString<256> Filename(ToolChain::getInputFilename(Input)); 658 llvm::sys::path::replace_extension(Filename, "cubin"); 659 return std::string(Filename.str()); 660 } 661 662 void CudaToolChain::addClangTargetOptions( 663 const llvm::opt::ArgList &DriverArgs, 664 llvm::opt::ArgStringList &CC1Args, 665 Action::OffloadKind DeviceOffloadingKind) const { 666 HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind); 667 668 StringRef GpuArch = DriverArgs.getLastArgValue(options::OPT_march_EQ); 669 assert(!GpuArch.empty() && "Must have an explicit GPU arch."); 670 assert((DeviceOffloadingKind == Action::OFK_OpenMP || 671 DeviceOffloadingKind == Action::OFK_Cuda) && 672 "Only OpenMP or CUDA offloading kinds are supported for NVIDIA GPUs."); 673 674 if (DeviceOffloadingKind == Action::OFK_Cuda) { 675 CC1Args.append( 676 {"-fcuda-is-device", "-mllvm", "-enable-memcpyopt-without-libcalls"}); 677 678 if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals, 679 options::OPT_fno_cuda_approx_transcendentals, false)) 680 CC1Args.push_back("-fcuda-approx-transcendentals"); 681 } 682 683 if (DriverArgs.hasArg(options::OPT_nogpulib)) 684 return; 685 686 if (DeviceOffloadingKind == Action::OFK_OpenMP && 687 DriverArgs.hasArg(options::OPT_S)) 688 return; 689 690 std::string LibDeviceFile = CudaInstallation.getLibDeviceFile(GpuArch); 691 if (LibDeviceFile.empty()) { 692 getDriver().Diag(diag::err_drv_no_cuda_libdevice) << GpuArch; 693 return; 694 } 695 696 CC1Args.push_back("-mlink-builtin-bitcode"); 697 CC1Args.push_back(DriverArgs.MakeArgString(LibDeviceFile)); 698 699 clang::CudaVersion CudaInstallationVersion = CudaInstallation.version(); 700 701 // New CUDA versions often introduce new instructions that are only supported 702 // by new PTX version, so we need to raise PTX level to enable them in NVPTX 703 // back-end. 704 const char *PtxFeature = nullptr; 705 switch (CudaInstallationVersion) { 706 #define CASE_CUDA_VERSION(CUDA_VER, PTX_VER) \ 707 case CudaVersion::CUDA_##CUDA_VER: \ 708 PtxFeature = "+ptx" #PTX_VER; \ 709 break; 710 CASE_CUDA_VERSION(114, 74); 711 CASE_CUDA_VERSION(113, 73); 712 CASE_CUDA_VERSION(112, 72); 713 CASE_CUDA_VERSION(111, 71); 714 CASE_CUDA_VERSION(110, 70); 715 CASE_CUDA_VERSION(102, 65); 716 CASE_CUDA_VERSION(101, 64); 717 CASE_CUDA_VERSION(100, 63); 718 CASE_CUDA_VERSION(92, 61); 719 CASE_CUDA_VERSION(91, 61); 720 CASE_CUDA_VERSION(90, 60); 721 #undef CASE_CUDA_VERSION 722 default: 723 PtxFeature = "+ptx42"; 724 } 725 CC1Args.append({"-target-feature", PtxFeature}); 726 if (DriverArgs.hasFlag(options::OPT_fcuda_short_ptr, 727 options::OPT_fno_cuda_short_ptr, false)) 728 CC1Args.append({"-mllvm", "--nvptx-short-ptr"}); 729 730 if (CudaInstallationVersion >= CudaVersion::UNKNOWN) 731 CC1Args.push_back( 732 DriverArgs.MakeArgString(Twine("-target-sdk-version=") + 733 CudaVersionToString(CudaInstallationVersion))); 734 735 if (DeviceOffloadingKind == Action::OFK_OpenMP) { 736 if (CudaInstallationVersion < CudaVersion::CUDA_92) { 737 getDriver().Diag( 738 diag::err_drv_omp_offload_target_cuda_version_not_support) 739 << CudaVersionToString(CudaInstallationVersion); 740 return; 741 } 742 743 std::string BitcodeSuffix; 744 if (DriverArgs.hasFlag(options::OPT_fopenmp_target_new_runtime, 745 options::OPT_fno_openmp_target_new_runtime, false)) 746 BitcodeSuffix = "new-nvptx-" + GpuArch.str(); 747 else 748 BitcodeSuffix = "nvptx-" + GpuArch.str(); 749 750 addOpenMPDeviceRTL(getDriver(), DriverArgs, CC1Args, BitcodeSuffix, 751 getTriple()); 752 AddStaticDeviceLibsPostLinking(getDriver(), DriverArgs, CC1Args, "nvptx", GpuArch, 753 /* bitcode SDL?*/ true, /* PostClang Link? */ true); 754 } 755 } 756 757 llvm::DenormalMode CudaToolChain::getDefaultDenormalModeForType( 758 const llvm::opt::ArgList &DriverArgs, const JobAction &JA, 759 const llvm::fltSemantics *FPType) const { 760 if (JA.getOffloadingDeviceKind() == Action::OFK_Cuda) { 761 if (FPType && FPType == &llvm::APFloat::IEEEsingle() && 762 DriverArgs.hasFlag(options::OPT_fgpu_flush_denormals_to_zero, 763 options::OPT_fno_gpu_flush_denormals_to_zero, false)) 764 return llvm::DenormalMode::getPreserveSign(); 765 } 766 767 assert(JA.getOffloadingDeviceKind() != Action::OFK_Host); 768 return llvm::DenormalMode::getIEEE(); 769 } 770 771 bool CudaToolChain::supportsDebugInfoOption(const llvm::opt::Arg *A) const { 772 const Option &O = A->getOption(); 773 return (O.matches(options::OPT_gN_Group) && 774 !O.matches(options::OPT_gmodules)) || 775 O.matches(options::OPT_g_Flag) || 776 O.matches(options::OPT_ggdbN_Group) || O.matches(options::OPT_ggdb) || 777 O.matches(options::OPT_gdwarf) || O.matches(options::OPT_gdwarf_2) || 778 O.matches(options::OPT_gdwarf_3) || O.matches(options::OPT_gdwarf_4) || 779 O.matches(options::OPT_gdwarf_5) || 780 O.matches(options::OPT_gcolumn_info); 781 } 782 783 void CudaToolChain::adjustDebugInfoKind( 784 codegenoptions::DebugInfoKind &DebugInfoKind, const ArgList &Args) const { 785 switch (mustEmitDebugInfo(Args)) { 786 case DisableDebugInfo: 787 DebugInfoKind = codegenoptions::NoDebugInfo; 788 break; 789 case DebugDirectivesOnly: 790 DebugInfoKind = codegenoptions::DebugDirectivesOnly; 791 break; 792 case EmitSameDebugInfoAsHost: 793 // Use same debug info level as the host. 794 break; 795 } 796 } 797 798 void CudaToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs, 799 ArgStringList &CC1Args) const { 800 // Check our CUDA version if we're going to include the CUDA headers. 801 if (!DriverArgs.hasArg(options::OPT_nogpuinc) && 802 !DriverArgs.hasArg(options::OPT_no_cuda_version_check)) { 803 StringRef Arch = DriverArgs.getLastArgValue(options::OPT_march_EQ); 804 assert(!Arch.empty() && "Must have an explicit GPU arch."); 805 CudaInstallation.CheckCudaVersionSupportsArch(StringToCudaArch(Arch)); 806 } 807 CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args); 808 } 809 810 llvm::opt::DerivedArgList * 811 CudaToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args, 812 StringRef BoundArch, 813 Action::OffloadKind DeviceOffloadKind) const { 814 DerivedArgList *DAL = 815 HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind); 816 if (!DAL) 817 DAL = new DerivedArgList(Args.getBaseArgs()); 818 819 const OptTable &Opts = getDriver().getOpts(); 820 821 // For OpenMP device offloading, append derived arguments. Make sure 822 // flags are not duplicated. 823 // Also append the compute capability. 824 if (DeviceOffloadKind == Action::OFK_OpenMP) { 825 for (Arg *A : Args) 826 if (!llvm::is_contained(*DAL, A)) 827 DAL->append(A); 828 829 StringRef Arch = DAL->getLastArgValue(options::OPT_march_EQ); 830 if (Arch.empty()) 831 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ), 832 CLANG_OPENMP_NVPTX_DEFAULT_ARCH); 833 834 return DAL; 835 } 836 837 for (Arg *A : Args) { 838 DAL->append(A); 839 } 840 841 if (!BoundArch.empty()) { 842 DAL->eraseArg(options::OPT_march_EQ); 843 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ), BoundArch); 844 } 845 return DAL; 846 } 847 848 Tool *CudaToolChain::buildAssembler() const { 849 return new tools::NVPTX::Assembler(*this); 850 } 851 852 Tool *CudaToolChain::buildLinker() const { 853 if (OK == Action::OFK_OpenMP) 854 return new tools::NVPTX::OpenMPLinker(*this); 855 return new tools::NVPTX::Linker(*this); 856 } 857 858 void CudaToolChain::addClangWarningOptions(ArgStringList &CC1Args) const { 859 HostTC.addClangWarningOptions(CC1Args); 860 } 861 862 ToolChain::CXXStdlibType 863 CudaToolChain::GetCXXStdlibType(const ArgList &Args) const { 864 return HostTC.GetCXXStdlibType(Args); 865 } 866 867 void CudaToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 868 ArgStringList &CC1Args) const { 869 HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args); 870 871 if (!DriverArgs.hasArg(options::OPT_nogpuinc) && CudaInstallation.isValid()) 872 CC1Args.append( 873 {"-internal-isystem", 874 DriverArgs.MakeArgString(CudaInstallation.getIncludePath())}); 875 } 876 877 void CudaToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &Args, 878 ArgStringList &CC1Args) const { 879 HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args); 880 } 881 882 void CudaToolChain::AddIAMCUIncludeArgs(const ArgList &Args, 883 ArgStringList &CC1Args) const { 884 HostTC.AddIAMCUIncludeArgs(Args, CC1Args); 885 } 886 887 SanitizerMask CudaToolChain::getSupportedSanitizers() const { 888 // The CudaToolChain only supports sanitizers in the sense that it allows 889 // sanitizer arguments on the command line if they are supported by the host 890 // toolchain. The CudaToolChain will actually ignore any command line 891 // arguments for any of these "supported" sanitizers. That means that no 892 // sanitization of device code is actually supported at this time. 893 // 894 // This behavior is necessary because the host and device toolchains 895 // invocations often share the command line, so the device toolchain must 896 // tolerate flags meant only for the host toolchain. 897 return HostTC.getSupportedSanitizers(); 898 } 899 900 VersionTuple CudaToolChain::computeMSVCVersion(const Driver *D, 901 const ArgList &Args) const { 902 return HostTC.computeMSVCVersion(D, Args); 903 } 904