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