10b57cec5SDimitry Andric //===--- Cuda.cpp - Cuda Tool and ToolChain Implementations -----*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "Cuda.h" 100b57cec5SDimitry Andric #include "CommonArgs.h" 110b57cec5SDimitry Andric #include "InputInfo.h" 120b57cec5SDimitry Andric #include "clang/Basic/Cuda.h" 130b57cec5SDimitry Andric #include "clang/Config/config.h" 140b57cec5SDimitry Andric #include "clang/Driver/Compilation.h" 150b57cec5SDimitry Andric #include "clang/Driver/Distro.h" 160b57cec5SDimitry Andric #include "clang/Driver/Driver.h" 170b57cec5SDimitry Andric #include "clang/Driver/DriverDiagnostic.h" 180b57cec5SDimitry Andric #include "clang/Driver/Options.h" 190b57cec5SDimitry Andric #include "llvm/Option/ArgList.h" 200b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h" 210b57cec5SDimitry Andric #include "llvm/Support/Path.h" 220b57cec5SDimitry Andric #include "llvm/Support/Process.h" 230b57cec5SDimitry Andric #include "llvm/Support/Program.h" 240b57cec5SDimitry Andric #include "llvm/Support/VirtualFileSystem.h" 250b57cec5SDimitry Andric #include <system_error> 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric using namespace clang::driver; 280b57cec5SDimitry Andric using namespace clang::driver::toolchains; 290b57cec5SDimitry Andric using namespace clang::driver::tools; 300b57cec5SDimitry Andric using namespace clang; 310b57cec5SDimitry Andric using namespace llvm::opt; 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric // Parses the contents of version.txt in an CUDA installation. It should 340b57cec5SDimitry Andric // contain one line of the from e.g. "CUDA Version 7.5.2". 350b57cec5SDimitry Andric static CudaVersion ParseCudaVersionFile(llvm::StringRef V) { 360b57cec5SDimitry Andric if (!V.startswith("CUDA Version ")) 370b57cec5SDimitry Andric return CudaVersion::UNKNOWN; 380b57cec5SDimitry Andric V = V.substr(strlen("CUDA Version ")); 390b57cec5SDimitry Andric int Major = -1, Minor = -1; 400b57cec5SDimitry Andric auto First = V.split('.'); 410b57cec5SDimitry Andric auto Second = First.second.split('.'); 420b57cec5SDimitry Andric if (First.first.getAsInteger(10, Major) || 430b57cec5SDimitry Andric Second.first.getAsInteger(10, Minor)) 440b57cec5SDimitry Andric return CudaVersion::UNKNOWN; 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric if (Major == 7 && Minor == 0) { 470b57cec5SDimitry Andric // This doesn't appear to ever happen -- version.txt doesn't exist in the 480b57cec5SDimitry Andric // CUDA 7 installs I've seen. But no harm in checking. 490b57cec5SDimitry Andric return CudaVersion::CUDA_70; 500b57cec5SDimitry Andric } 510b57cec5SDimitry Andric if (Major == 7 && Minor == 5) 520b57cec5SDimitry Andric return CudaVersion::CUDA_75; 530b57cec5SDimitry Andric if (Major == 8 && Minor == 0) 540b57cec5SDimitry Andric return CudaVersion::CUDA_80; 550b57cec5SDimitry Andric if (Major == 9 && Minor == 0) 560b57cec5SDimitry Andric return CudaVersion::CUDA_90; 570b57cec5SDimitry Andric if (Major == 9 && Minor == 1) 580b57cec5SDimitry Andric return CudaVersion::CUDA_91; 590b57cec5SDimitry Andric if (Major == 9 && Minor == 2) 600b57cec5SDimitry Andric return CudaVersion::CUDA_92; 610b57cec5SDimitry Andric if (Major == 10 && Minor == 0) 620b57cec5SDimitry Andric return CudaVersion::CUDA_100; 630b57cec5SDimitry Andric if (Major == 10 && Minor == 1) 640b57cec5SDimitry Andric return CudaVersion::CUDA_101; 650b57cec5SDimitry Andric return CudaVersion::UNKNOWN; 660b57cec5SDimitry Andric } 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric CudaInstallationDetector::CudaInstallationDetector( 690b57cec5SDimitry Andric const Driver &D, const llvm::Triple &HostTriple, 700b57cec5SDimitry Andric const llvm::opt::ArgList &Args) 710b57cec5SDimitry Andric : D(D) { 720b57cec5SDimitry Andric struct Candidate { 730b57cec5SDimitry Andric std::string Path; 740b57cec5SDimitry Andric bool StrictChecking; 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric Candidate(std::string Path, bool StrictChecking = false) 770b57cec5SDimitry Andric : Path(Path), StrictChecking(StrictChecking) {} 780b57cec5SDimitry Andric }; 790b57cec5SDimitry Andric SmallVector<Candidate, 4> Candidates; 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric // In decreasing order so we prefer newer versions to older versions. 820b57cec5SDimitry Andric std::initializer_list<const char *> Versions = {"8.0", "7.5", "7.0"}; 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric if (Args.hasArg(clang::driver::options::OPT_cuda_path_EQ)) { 850b57cec5SDimitry Andric Candidates.emplace_back( 860b57cec5SDimitry Andric Args.getLastArgValue(clang::driver::options::OPT_cuda_path_EQ).str()); 870b57cec5SDimitry Andric } else if (HostTriple.isOSWindows()) { 880b57cec5SDimitry Andric for (const char *Ver : Versions) 890b57cec5SDimitry Andric Candidates.emplace_back( 900b57cec5SDimitry Andric D.SysRoot + "/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v" + 910b57cec5SDimitry Andric Ver); 920b57cec5SDimitry Andric } else { 930b57cec5SDimitry Andric if (!Args.hasArg(clang::driver::options::OPT_cuda_path_ignore_env)) { 940b57cec5SDimitry Andric // Try to find ptxas binary. If the executable is located in a directory 950b57cec5SDimitry Andric // called 'bin/', its parent directory might be a good guess for a valid 960b57cec5SDimitry Andric // CUDA installation. 970b57cec5SDimitry Andric // However, some distributions might installs 'ptxas' to /usr/bin. In that 980b57cec5SDimitry Andric // case the candidate would be '/usr' which passes the following checks 990b57cec5SDimitry Andric // because '/usr/include' exists as well. To avoid this case, we always 1000b57cec5SDimitry Andric // check for the directory potentially containing files for libdevice, 1010b57cec5SDimitry Andric // even if the user passes -nocudalib. 1020b57cec5SDimitry Andric if (llvm::ErrorOr<std::string> ptxas = 1030b57cec5SDimitry Andric llvm::sys::findProgramByName("ptxas")) { 1040b57cec5SDimitry Andric SmallString<256> ptxasAbsolutePath; 1050b57cec5SDimitry Andric llvm::sys::fs::real_path(*ptxas, ptxasAbsolutePath); 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric StringRef ptxasDir = llvm::sys::path::parent_path(ptxasAbsolutePath); 1080b57cec5SDimitry Andric if (llvm::sys::path::filename(ptxasDir) == "bin") 1090b57cec5SDimitry Andric Candidates.emplace_back(llvm::sys::path::parent_path(ptxasDir), 1100b57cec5SDimitry Andric /*StrictChecking=*/true); 1110b57cec5SDimitry Andric } 1120b57cec5SDimitry Andric } 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric Candidates.emplace_back(D.SysRoot + "/usr/local/cuda"); 1150b57cec5SDimitry Andric for (const char *Ver : Versions) 1160b57cec5SDimitry Andric Candidates.emplace_back(D.SysRoot + "/usr/local/cuda-" + Ver); 1170b57cec5SDimitry Andric 1180b57cec5SDimitry Andric if (Distro(D.getVFS()).IsDebian() || Distro(D.getVFS()).IsUbuntu()) 1190b57cec5SDimitry Andric // Special case for Debian to have nvidia-cuda-toolkit work 1200b57cec5SDimitry Andric // out of the box. More info on http://bugs.debian.org/882505 1210b57cec5SDimitry Andric Candidates.emplace_back(D.SysRoot + "/usr/lib/cuda"); 1220b57cec5SDimitry Andric } 1230b57cec5SDimitry Andric 124*a7dea167SDimitry Andric bool NoCudaLib = Args.hasArg(options::OPT_nogpulib); 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric for (const auto &Candidate : Candidates) { 1270b57cec5SDimitry Andric InstallPath = Candidate.Path; 1280b57cec5SDimitry Andric if (InstallPath.empty() || !D.getVFS().exists(InstallPath)) 1290b57cec5SDimitry Andric continue; 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric BinPath = InstallPath + "/bin"; 1320b57cec5SDimitry Andric IncludePath = InstallPath + "/include"; 1330b57cec5SDimitry Andric LibDevicePath = InstallPath + "/nvvm/libdevice"; 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric auto &FS = D.getVFS(); 1360b57cec5SDimitry Andric if (!(FS.exists(IncludePath) && FS.exists(BinPath))) 1370b57cec5SDimitry Andric continue; 1380b57cec5SDimitry Andric bool CheckLibDevice = (!NoCudaLib || Candidate.StrictChecking); 1390b57cec5SDimitry Andric if (CheckLibDevice && !FS.exists(LibDevicePath)) 1400b57cec5SDimitry Andric continue; 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric // On Linux, we have both lib and lib64 directories, and we need to choose 1430b57cec5SDimitry Andric // based on our triple. On MacOS, we have only a lib directory. 1440b57cec5SDimitry Andric // 1450b57cec5SDimitry Andric // It's sufficient for our purposes to be flexible: If both lib and lib64 1460b57cec5SDimitry Andric // exist, we choose whichever one matches our triple. Otherwise, if only 1470b57cec5SDimitry Andric // lib exists, we use it. 1480b57cec5SDimitry Andric if (HostTriple.isArch64Bit() && FS.exists(InstallPath + "/lib64")) 1490b57cec5SDimitry Andric LibPath = InstallPath + "/lib64"; 1500b57cec5SDimitry Andric else if (FS.exists(InstallPath + "/lib")) 1510b57cec5SDimitry Andric LibPath = InstallPath + "/lib"; 1520b57cec5SDimitry Andric else 1530b57cec5SDimitry Andric continue; 1540b57cec5SDimitry Andric 1550b57cec5SDimitry Andric llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> VersionFile = 1560b57cec5SDimitry Andric FS.getBufferForFile(InstallPath + "/version.txt"); 1570b57cec5SDimitry Andric if (!VersionFile) { 1580b57cec5SDimitry Andric // CUDA 7.0 doesn't have a version.txt, so guess that's our version if 1590b57cec5SDimitry Andric // version.txt isn't present. 1600b57cec5SDimitry Andric Version = CudaVersion::CUDA_70; 1610b57cec5SDimitry Andric } else { 1620b57cec5SDimitry Andric Version = ParseCudaVersionFile((*VersionFile)->getBuffer()); 1630b57cec5SDimitry Andric } 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric if (Version >= CudaVersion::CUDA_90) { 1660b57cec5SDimitry Andric // CUDA-9+ uses single libdevice file for all GPU variants. 1670b57cec5SDimitry Andric std::string FilePath = LibDevicePath + "/libdevice.10.bc"; 1680b57cec5SDimitry Andric if (FS.exists(FilePath)) { 1690b57cec5SDimitry Andric for (const char *GpuArchName : 1700b57cec5SDimitry Andric {"sm_30", "sm_32", "sm_35", "sm_37", "sm_50", "sm_52", "sm_53", 1710b57cec5SDimitry Andric "sm_60", "sm_61", "sm_62", "sm_70", "sm_72", "sm_75"}) { 1720b57cec5SDimitry Andric const CudaArch GpuArch = StringToCudaArch(GpuArchName); 1730b57cec5SDimitry Andric if (Version >= MinVersionForCudaArch(GpuArch) && 1740b57cec5SDimitry Andric Version <= MaxVersionForCudaArch(GpuArch)) 1750b57cec5SDimitry Andric LibDeviceMap[GpuArchName] = FilePath; 1760b57cec5SDimitry Andric } 1770b57cec5SDimitry Andric } 1780b57cec5SDimitry Andric } else { 1790b57cec5SDimitry Andric std::error_code EC; 1800b57cec5SDimitry Andric for (llvm::sys::fs::directory_iterator LI(LibDevicePath, EC), LE; 1810b57cec5SDimitry Andric !EC && LI != LE; LI = LI.increment(EC)) { 1820b57cec5SDimitry Andric StringRef FilePath = LI->path(); 1830b57cec5SDimitry Andric StringRef FileName = llvm::sys::path::filename(FilePath); 1840b57cec5SDimitry Andric // Process all bitcode filenames that look like 1850b57cec5SDimitry Andric // libdevice.compute_XX.YY.bc 1860b57cec5SDimitry Andric const StringRef LibDeviceName = "libdevice."; 1870b57cec5SDimitry Andric if (!(FileName.startswith(LibDeviceName) && FileName.endswith(".bc"))) 1880b57cec5SDimitry Andric continue; 1890b57cec5SDimitry Andric StringRef GpuArch = FileName.slice( 1900b57cec5SDimitry Andric LibDeviceName.size(), FileName.find('.', LibDeviceName.size())); 1910b57cec5SDimitry Andric LibDeviceMap[GpuArch] = FilePath.str(); 1920b57cec5SDimitry Andric // Insert map entries for specific devices with this compute 1930b57cec5SDimitry Andric // capability. NVCC's choice of the libdevice library version is 1940b57cec5SDimitry Andric // rather peculiar and depends on the CUDA version. 1950b57cec5SDimitry Andric if (GpuArch == "compute_20") { 1960b57cec5SDimitry Andric LibDeviceMap["sm_20"] = FilePath; 1970b57cec5SDimitry Andric LibDeviceMap["sm_21"] = FilePath; 1980b57cec5SDimitry Andric LibDeviceMap["sm_32"] = FilePath; 1990b57cec5SDimitry Andric } else if (GpuArch == "compute_30") { 2000b57cec5SDimitry Andric LibDeviceMap["sm_30"] = FilePath; 2010b57cec5SDimitry Andric if (Version < CudaVersion::CUDA_80) { 2020b57cec5SDimitry Andric LibDeviceMap["sm_50"] = FilePath; 2030b57cec5SDimitry Andric LibDeviceMap["sm_52"] = FilePath; 2040b57cec5SDimitry Andric LibDeviceMap["sm_53"] = FilePath; 2050b57cec5SDimitry Andric } 2060b57cec5SDimitry Andric LibDeviceMap["sm_60"] = FilePath; 2070b57cec5SDimitry Andric LibDeviceMap["sm_61"] = FilePath; 2080b57cec5SDimitry Andric LibDeviceMap["sm_62"] = FilePath; 2090b57cec5SDimitry Andric } else if (GpuArch == "compute_35") { 2100b57cec5SDimitry Andric LibDeviceMap["sm_35"] = FilePath; 2110b57cec5SDimitry Andric LibDeviceMap["sm_37"] = FilePath; 2120b57cec5SDimitry Andric } else if (GpuArch == "compute_50") { 2130b57cec5SDimitry Andric if (Version >= CudaVersion::CUDA_80) { 2140b57cec5SDimitry Andric LibDeviceMap["sm_50"] = FilePath; 2150b57cec5SDimitry Andric LibDeviceMap["sm_52"] = FilePath; 2160b57cec5SDimitry Andric LibDeviceMap["sm_53"] = FilePath; 2170b57cec5SDimitry Andric } 2180b57cec5SDimitry Andric } 2190b57cec5SDimitry Andric } 2200b57cec5SDimitry Andric } 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric // Check that we have found at least one libdevice that we can link in if 2230b57cec5SDimitry Andric // -nocudalib hasn't been specified. 2240b57cec5SDimitry Andric if (LibDeviceMap.empty() && !NoCudaLib) 2250b57cec5SDimitry Andric continue; 2260b57cec5SDimitry Andric 2270b57cec5SDimitry Andric IsValid = true; 2280b57cec5SDimitry Andric break; 2290b57cec5SDimitry Andric } 2300b57cec5SDimitry Andric } 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric void CudaInstallationDetector::AddCudaIncludeArgs( 2330b57cec5SDimitry Andric const ArgList &DriverArgs, ArgStringList &CC1Args) const { 2340b57cec5SDimitry Andric if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 2350b57cec5SDimitry Andric // Add cuda_wrappers/* to our system include path. This lets us wrap 2360b57cec5SDimitry Andric // standard library headers. 2370b57cec5SDimitry Andric SmallString<128> P(D.ResourceDir); 2380b57cec5SDimitry Andric llvm::sys::path::append(P, "include"); 2390b57cec5SDimitry Andric llvm::sys::path::append(P, "cuda_wrappers"); 2400b57cec5SDimitry Andric CC1Args.push_back("-internal-isystem"); 2410b57cec5SDimitry Andric CC1Args.push_back(DriverArgs.MakeArgString(P)); 2420b57cec5SDimitry Andric } 2430b57cec5SDimitry Andric 2440b57cec5SDimitry Andric if (DriverArgs.hasArg(options::OPT_nocudainc)) 2450b57cec5SDimitry Andric return; 2460b57cec5SDimitry Andric 2470b57cec5SDimitry Andric if (!isValid()) { 2480b57cec5SDimitry Andric D.Diag(diag::err_drv_no_cuda_installation); 2490b57cec5SDimitry Andric return; 2500b57cec5SDimitry Andric } 2510b57cec5SDimitry Andric 2520b57cec5SDimitry Andric CC1Args.push_back("-internal-isystem"); 2530b57cec5SDimitry Andric CC1Args.push_back(DriverArgs.MakeArgString(getIncludePath())); 2540b57cec5SDimitry Andric CC1Args.push_back("-include"); 2550b57cec5SDimitry Andric CC1Args.push_back("__clang_cuda_runtime_wrapper.h"); 2560b57cec5SDimitry Andric } 2570b57cec5SDimitry Andric 2580b57cec5SDimitry Andric void CudaInstallationDetector::CheckCudaVersionSupportsArch( 2590b57cec5SDimitry Andric CudaArch Arch) const { 2600b57cec5SDimitry Andric if (Arch == CudaArch::UNKNOWN || Version == CudaVersion::UNKNOWN || 2610b57cec5SDimitry Andric ArchsWithBadVersion.count(Arch) > 0) 2620b57cec5SDimitry Andric return; 2630b57cec5SDimitry Andric 2640b57cec5SDimitry Andric auto MinVersion = MinVersionForCudaArch(Arch); 2650b57cec5SDimitry Andric auto MaxVersion = MaxVersionForCudaArch(Arch); 2660b57cec5SDimitry Andric if (Version < MinVersion || Version > MaxVersion) { 2670b57cec5SDimitry Andric ArchsWithBadVersion.insert(Arch); 2680b57cec5SDimitry Andric D.Diag(diag::err_drv_cuda_version_unsupported) 2690b57cec5SDimitry Andric << CudaArchToString(Arch) << CudaVersionToString(MinVersion) 2700b57cec5SDimitry Andric << CudaVersionToString(MaxVersion) << InstallPath 2710b57cec5SDimitry Andric << CudaVersionToString(Version); 2720b57cec5SDimitry Andric } 2730b57cec5SDimitry Andric } 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric void CudaInstallationDetector::print(raw_ostream &OS) const { 2760b57cec5SDimitry Andric if (isValid()) 2770b57cec5SDimitry Andric OS << "Found CUDA installation: " << InstallPath << ", version " 2780b57cec5SDimitry Andric << CudaVersionToString(Version) << "\n"; 2790b57cec5SDimitry Andric } 2800b57cec5SDimitry Andric 2810b57cec5SDimitry Andric namespace { 2820b57cec5SDimitry Andric /// Debug info level for the NVPTX devices. We may need to emit different debug 2830b57cec5SDimitry Andric /// info level for the host and for the device itselfi. This type controls 2840b57cec5SDimitry Andric /// emission of the debug info for the devices. It either prohibits disable info 2850b57cec5SDimitry Andric /// emission completely, or emits debug directives only, or emits same debug 2860b57cec5SDimitry Andric /// info as for the host. 2870b57cec5SDimitry Andric enum DeviceDebugInfoLevel { 2880b57cec5SDimitry Andric DisableDebugInfo, /// Do not emit debug info for the devices. 2890b57cec5SDimitry Andric DebugDirectivesOnly, /// Emit only debug directives. 2900b57cec5SDimitry Andric EmitSameDebugInfoAsHost, /// Use the same debug info level just like for the 2910b57cec5SDimitry Andric /// host. 2920b57cec5SDimitry Andric }; 2930b57cec5SDimitry Andric } // anonymous namespace 2940b57cec5SDimitry Andric 2950b57cec5SDimitry Andric /// Define debug info level for the NVPTX devices. If the debug info for both 2960b57cec5SDimitry Andric /// the host and device are disabled (-g0/-ggdb0 or no debug options at all). If 2970b57cec5SDimitry Andric /// only debug directives are requested for the both host and device 2980b57cec5SDimitry Andric /// (-gline-directvies-only), or the debug info only for the device is disabled 2990b57cec5SDimitry Andric /// (optimization is on and --cuda-noopt-device-debug was not specified), the 3000b57cec5SDimitry Andric /// debug directves only must be emitted for the device. Otherwise, use the same 3010b57cec5SDimitry Andric /// debug info level just like for the host (with the limitations of only 3020b57cec5SDimitry Andric /// supported DWARF2 standard). 3030b57cec5SDimitry Andric static DeviceDebugInfoLevel mustEmitDebugInfo(const ArgList &Args) { 3040b57cec5SDimitry Andric const Arg *A = Args.getLastArg(options::OPT_O_Group); 3050b57cec5SDimitry Andric bool IsDebugEnabled = !A || A->getOption().matches(options::OPT_O0) || 3060b57cec5SDimitry Andric Args.hasFlag(options::OPT_cuda_noopt_device_debug, 3070b57cec5SDimitry Andric options::OPT_no_cuda_noopt_device_debug, 3080b57cec5SDimitry Andric /*Default=*/false); 3090b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) { 3100b57cec5SDimitry Andric const Option &Opt = A->getOption(); 3110b57cec5SDimitry Andric if (Opt.matches(options::OPT_gN_Group)) { 3120b57cec5SDimitry Andric if (Opt.matches(options::OPT_g0) || Opt.matches(options::OPT_ggdb0)) 3130b57cec5SDimitry Andric return DisableDebugInfo; 3140b57cec5SDimitry Andric if (Opt.matches(options::OPT_gline_directives_only)) 3150b57cec5SDimitry Andric return DebugDirectivesOnly; 3160b57cec5SDimitry Andric } 3170b57cec5SDimitry Andric return IsDebugEnabled ? EmitSameDebugInfoAsHost : DebugDirectivesOnly; 3180b57cec5SDimitry Andric } 3190b57cec5SDimitry Andric return DisableDebugInfo; 3200b57cec5SDimitry Andric } 3210b57cec5SDimitry Andric 3220b57cec5SDimitry Andric void NVPTX::Assembler::ConstructJob(Compilation &C, const JobAction &JA, 3230b57cec5SDimitry Andric const InputInfo &Output, 3240b57cec5SDimitry Andric const InputInfoList &Inputs, 3250b57cec5SDimitry Andric const ArgList &Args, 3260b57cec5SDimitry Andric const char *LinkingOutput) const { 3270b57cec5SDimitry Andric const auto &TC = 3280b57cec5SDimitry Andric static_cast<const toolchains::CudaToolChain &>(getToolChain()); 3290b57cec5SDimitry Andric assert(TC.getTriple().isNVPTX() && "Wrong platform"); 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric StringRef GPUArchName; 3320b57cec5SDimitry Andric // If this is an OpenMP action we need to extract the device architecture 3330b57cec5SDimitry Andric // from the -march=arch option. This option may come from -Xopenmp-target 3340b57cec5SDimitry Andric // flag or the default value. 3350b57cec5SDimitry Andric if (JA.isDeviceOffloading(Action::OFK_OpenMP)) { 3360b57cec5SDimitry Andric GPUArchName = Args.getLastArgValue(options::OPT_march_EQ); 3370b57cec5SDimitry Andric assert(!GPUArchName.empty() && "Must have an architecture passed in."); 3380b57cec5SDimitry Andric } else 3390b57cec5SDimitry Andric GPUArchName = JA.getOffloadingArch(); 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andric // Obtain architecture from the action. 3420b57cec5SDimitry Andric CudaArch gpu_arch = StringToCudaArch(GPUArchName); 3430b57cec5SDimitry Andric assert(gpu_arch != CudaArch::UNKNOWN && 3440b57cec5SDimitry Andric "Device action expected to have an architecture."); 3450b57cec5SDimitry Andric 3460b57cec5SDimitry Andric // Check that our installation's ptxas supports gpu_arch. 3470b57cec5SDimitry Andric if (!Args.hasArg(options::OPT_no_cuda_version_check)) { 3480b57cec5SDimitry Andric TC.CudaInstallation.CheckCudaVersionSupportsArch(gpu_arch); 3490b57cec5SDimitry Andric } 3500b57cec5SDimitry Andric 3510b57cec5SDimitry Andric ArgStringList CmdArgs; 3520b57cec5SDimitry Andric CmdArgs.push_back(TC.getTriple().isArch64Bit() ? "-m64" : "-m32"); 3530b57cec5SDimitry Andric DeviceDebugInfoLevel DIKind = mustEmitDebugInfo(Args); 3540b57cec5SDimitry Andric if (DIKind == EmitSameDebugInfoAsHost) { 3550b57cec5SDimitry Andric // ptxas does not accept -g option if optimization is enabled, so 3560b57cec5SDimitry Andric // we ignore the compiler's -O* options if we want debug info. 3570b57cec5SDimitry Andric CmdArgs.push_back("-g"); 3580b57cec5SDimitry Andric CmdArgs.push_back("--dont-merge-basicblocks"); 3590b57cec5SDimitry Andric CmdArgs.push_back("--return-at-end"); 3600b57cec5SDimitry Andric } else if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { 3610b57cec5SDimitry Andric // Map the -O we received to -O{0,1,2,3}. 3620b57cec5SDimitry Andric // 3630b57cec5SDimitry Andric // TODO: Perhaps we should map host -O2 to ptxas -O3. -O3 is ptxas's 3640b57cec5SDimitry Andric // default, so it may correspond more closely to the spirit of clang -O2. 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric // -O3 seems like the least-bad option when -Osomething is specified to 3670b57cec5SDimitry Andric // clang but it isn't handled below. 3680b57cec5SDimitry Andric StringRef OOpt = "3"; 3690b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_O4) || 3700b57cec5SDimitry Andric A->getOption().matches(options::OPT_Ofast)) 3710b57cec5SDimitry Andric OOpt = "3"; 3720b57cec5SDimitry Andric else if (A->getOption().matches(options::OPT_O0)) 3730b57cec5SDimitry Andric OOpt = "0"; 3740b57cec5SDimitry Andric else if (A->getOption().matches(options::OPT_O)) { 3750b57cec5SDimitry Andric // -Os, -Oz, and -O(anything else) map to -O2, for lack of better options. 3760b57cec5SDimitry Andric OOpt = llvm::StringSwitch<const char *>(A->getValue()) 3770b57cec5SDimitry Andric .Case("1", "1") 3780b57cec5SDimitry Andric .Case("2", "2") 3790b57cec5SDimitry Andric .Case("3", "3") 3800b57cec5SDimitry Andric .Case("s", "2") 3810b57cec5SDimitry Andric .Case("z", "2") 3820b57cec5SDimitry Andric .Default("2"); 3830b57cec5SDimitry Andric } 3840b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt)); 3850b57cec5SDimitry Andric } else { 3860b57cec5SDimitry Andric // If no -O was passed, pass -O0 to ptxas -- no opt flag should correspond 3870b57cec5SDimitry Andric // to no optimizations, but ptxas's default is -O3. 3880b57cec5SDimitry Andric CmdArgs.push_back("-O0"); 3890b57cec5SDimitry Andric } 3900b57cec5SDimitry Andric if (DIKind == DebugDirectivesOnly) 3910b57cec5SDimitry Andric CmdArgs.push_back("-lineinfo"); 3920b57cec5SDimitry Andric 3930b57cec5SDimitry Andric // Pass -v to ptxas if it was passed to the driver. 3940b57cec5SDimitry Andric if (Args.hasArg(options::OPT_v)) 3950b57cec5SDimitry Andric CmdArgs.push_back("-v"); 3960b57cec5SDimitry Andric 3970b57cec5SDimitry Andric CmdArgs.push_back("--gpu-name"); 3980b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(CudaArchToString(gpu_arch))); 3990b57cec5SDimitry Andric CmdArgs.push_back("--output-file"); 4000b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(TC.getInputFilename(Output))); 4010b57cec5SDimitry Andric for (const auto& II : Inputs) 4020b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(II.getFilename())); 4030b57cec5SDimitry Andric 4040b57cec5SDimitry Andric for (const auto& A : Args.getAllArgValues(options::OPT_Xcuda_ptxas)) 4050b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(A)); 4060b57cec5SDimitry Andric 4070b57cec5SDimitry Andric bool Relocatable = false; 4080b57cec5SDimitry Andric if (JA.isOffloading(Action::OFK_OpenMP)) 4090b57cec5SDimitry Andric // In OpenMP we need to generate relocatable code. 4100b57cec5SDimitry Andric Relocatable = Args.hasFlag(options::OPT_fopenmp_relocatable_target, 4110b57cec5SDimitry Andric options::OPT_fnoopenmp_relocatable_target, 4120b57cec5SDimitry Andric /*Default=*/true); 4130b57cec5SDimitry Andric else if (JA.isOffloading(Action::OFK_Cuda)) 4140b57cec5SDimitry Andric Relocatable = Args.hasFlag(options::OPT_fgpu_rdc, 4150b57cec5SDimitry Andric options::OPT_fno_gpu_rdc, /*Default=*/false); 4160b57cec5SDimitry Andric 4170b57cec5SDimitry Andric if (Relocatable) 4180b57cec5SDimitry Andric CmdArgs.push_back("-c"); 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric const char *Exec; 4210b57cec5SDimitry Andric if (Arg *A = Args.getLastArg(options::OPT_ptxas_path_EQ)) 4220b57cec5SDimitry Andric Exec = A->getValue(); 4230b57cec5SDimitry Andric else 4240b57cec5SDimitry Andric Exec = Args.MakeArgString(TC.GetProgramPath("ptxas")); 425*a7dea167SDimitry Andric C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); 4260b57cec5SDimitry Andric } 4270b57cec5SDimitry Andric 4280b57cec5SDimitry Andric static bool shouldIncludePTX(const ArgList &Args, const char *gpu_arch) { 4290b57cec5SDimitry Andric bool includePTX = true; 4300b57cec5SDimitry Andric for (Arg *A : Args) { 4310b57cec5SDimitry Andric if (!(A->getOption().matches(options::OPT_cuda_include_ptx_EQ) || 4320b57cec5SDimitry Andric A->getOption().matches(options::OPT_no_cuda_include_ptx_EQ))) 4330b57cec5SDimitry Andric continue; 4340b57cec5SDimitry Andric A->claim(); 4350b57cec5SDimitry Andric const StringRef ArchStr = A->getValue(); 4360b57cec5SDimitry Andric if (ArchStr == "all" || ArchStr == gpu_arch) { 4370b57cec5SDimitry Andric includePTX = A->getOption().matches(options::OPT_cuda_include_ptx_EQ); 4380b57cec5SDimitry Andric continue; 4390b57cec5SDimitry Andric } 4400b57cec5SDimitry Andric } 4410b57cec5SDimitry Andric return includePTX; 4420b57cec5SDimitry Andric } 4430b57cec5SDimitry Andric 4440b57cec5SDimitry Andric // All inputs to this linker must be from CudaDeviceActions, as we need to look 4450b57cec5SDimitry Andric // at the Inputs' Actions in order to figure out which GPU architecture they 4460b57cec5SDimitry Andric // correspond to. 4470b57cec5SDimitry Andric void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA, 4480b57cec5SDimitry Andric const InputInfo &Output, 4490b57cec5SDimitry Andric const InputInfoList &Inputs, 4500b57cec5SDimitry Andric const ArgList &Args, 4510b57cec5SDimitry Andric const char *LinkingOutput) const { 4520b57cec5SDimitry Andric const auto &TC = 4530b57cec5SDimitry Andric static_cast<const toolchains::CudaToolChain &>(getToolChain()); 4540b57cec5SDimitry Andric assert(TC.getTriple().isNVPTX() && "Wrong platform"); 4550b57cec5SDimitry Andric 4560b57cec5SDimitry Andric ArgStringList CmdArgs; 4570b57cec5SDimitry Andric if (TC.CudaInstallation.version() <= CudaVersion::CUDA_100) 4580b57cec5SDimitry Andric CmdArgs.push_back("--cuda"); 4590b57cec5SDimitry Andric CmdArgs.push_back(TC.getTriple().isArch64Bit() ? "-64" : "-32"); 4600b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString("--create")); 4610b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Output.getFilename())); 4620b57cec5SDimitry Andric if (mustEmitDebugInfo(Args) == EmitSameDebugInfoAsHost) 4630b57cec5SDimitry Andric CmdArgs.push_back("-g"); 4640b57cec5SDimitry Andric 4650b57cec5SDimitry Andric for (const auto& II : Inputs) { 4660b57cec5SDimitry Andric auto *A = II.getAction(); 4670b57cec5SDimitry Andric assert(A->getInputs().size() == 1 && 4680b57cec5SDimitry Andric "Device offload action is expected to have a single input"); 4690b57cec5SDimitry Andric const char *gpu_arch_str = A->getOffloadingArch(); 4700b57cec5SDimitry Andric assert(gpu_arch_str && 4710b57cec5SDimitry Andric "Device action expected to have associated a GPU architecture!"); 4720b57cec5SDimitry Andric CudaArch gpu_arch = StringToCudaArch(gpu_arch_str); 4730b57cec5SDimitry Andric 4740b57cec5SDimitry Andric if (II.getType() == types::TY_PP_Asm && 4750b57cec5SDimitry Andric !shouldIncludePTX(Args, gpu_arch_str)) 4760b57cec5SDimitry Andric continue; 4770b57cec5SDimitry Andric // We need to pass an Arch of the form "sm_XX" for cubin files and 4780b57cec5SDimitry Andric // "compute_XX" for ptx. 4790b57cec5SDimitry Andric const char *Arch = 4800b57cec5SDimitry Andric (II.getType() == types::TY_PP_Asm) 4810b57cec5SDimitry Andric ? CudaVirtualArchToString(VirtualArchForCudaArch(gpu_arch)) 4820b57cec5SDimitry Andric : gpu_arch_str; 4830b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(llvm::Twine("--image=profile=") + 4840b57cec5SDimitry Andric Arch + ",file=" + II.getFilename())); 4850b57cec5SDimitry Andric } 4860b57cec5SDimitry Andric 4870b57cec5SDimitry Andric for (const auto& A : Args.getAllArgValues(options::OPT_Xcuda_fatbinary)) 4880b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(A)); 4890b57cec5SDimitry Andric 4900b57cec5SDimitry Andric const char *Exec = Args.MakeArgString(TC.GetProgramPath("fatbinary")); 491*a7dea167SDimitry Andric C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); 4920b57cec5SDimitry Andric } 4930b57cec5SDimitry Andric 4940b57cec5SDimitry Andric void NVPTX::OpenMPLinker::ConstructJob(Compilation &C, const JobAction &JA, 4950b57cec5SDimitry Andric const InputInfo &Output, 4960b57cec5SDimitry Andric const InputInfoList &Inputs, 4970b57cec5SDimitry Andric const ArgList &Args, 4980b57cec5SDimitry Andric const char *LinkingOutput) const { 4990b57cec5SDimitry Andric const auto &TC = 5000b57cec5SDimitry Andric static_cast<const toolchains::CudaToolChain &>(getToolChain()); 5010b57cec5SDimitry Andric assert(TC.getTriple().isNVPTX() && "Wrong platform"); 5020b57cec5SDimitry Andric 5030b57cec5SDimitry Andric ArgStringList CmdArgs; 5040b57cec5SDimitry Andric 5050b57cec5SDimitry Andric // OpenMP uses nvlink to link cubin files. The result will be embedded in the 5060b57cec5SDimitry Andric // host binary by the host linker. 5070b57cec5SDimitry Andric assert(!JA.isHostOffloading(Action::OFK_OpenMP) && 5080b57cec5SDimitry Andric "CUDA toolchain not expected for an OpenMP host device."); 5090b57cec5SDimitry Andric 5100b57cec5SDimitry Andric if (Output.isFilename()) { 5110b57cec5SDimitry Andric CmdArgs.push_back("-o"); 5120b57cec5SDimitry Andric CmdArgs.push_back(Output.getFilename()); 5130b57cec5SDimitry Andric } else 5140b57cec5SDimitry Andric assert(Output.isNothing() && "Invalid output."); 5150b57cec5SDimitry Andric if (mustEmitDebugInfo(Args) == EmitSameDebugInfoAsHost) 5160b57cec5SDimitry Andric CmdArgs.push_back("-g"); 5170b57cec5SDimitry Andric 5180b57cec5SDimitry Andric if (Args.hasArg(options::OPT_v)) 5190b57cec5SDimitry Andric CmdArgs.push_back("-v"); 5200b57cec5SDimitry Andric 5210b57cec5SDimitry Andric StringRef GPUArch = 5220b57cec5SDimitry Andric Args.getLastArgValue(options::OPT_march_EQ); 5230b57cec5SDimitry Andric assert(!GPUArch.empty() && "At least one GPU Arch required for ptxas."); 5240b57cec5SDimitry Andric 5250b57cec5SDimitry Andric CmdArgs.push_back("-arch"); 5260b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(GPUArch)); 5270b57cec5SDimitry Andric 5280b57cec5SDimitry Andric // Assume that the directory specified with --libomptarget_nvptx_path 5290b57cec5SDimitry Andric // contains the static library libomptarget-nvptx.a. 5300b57cec5SDimitry Andric if (const Arg *A = Args.getLastArg(options::OPT_libomptarget_nvptx_path_EQ)) 5310b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-L") + A->getValue())); 5320b57cec5SDimitry Andric 5330b57cec5SDimitry Andric // Add paths specified in LIBRARY_PATH environment variable as -L options. 5340b57cec5SDimitry Andric addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH"); 5350b57cec5SDimitry Andric 5360b57cec5SDimitry Andric // Add paths for the default clang library path. 5370b57cec5SDimitry Andric SmallString<256> DefaultLibPath = 5380b57cec5SDimitry Andric llvm::sys::path::parent_path(TC.getDriver().Dir); 5390b57cec5SDimitry Andric llvm::sys::path::append(DefaultLibPath, "lib" CLANG_LIBDIR_SUFFIX); 5400b57cec5SDimitry Andric CmdArgs.push_back(Args.MakeArgString(Twine("-L") + DefaultLibPath)); 5410b57cec5SDimitry Andric 5420b57cec5SDimitry Andric // Add linking against library implementing OpenMP calls on NVPTX target. 5430b57cec5SDimitry Andric CmdArgs.push_back("-lomptarget-nvptx"); 5440b57cec5SDimitry Andric 5450b57cec5SDimitry Andric for (const auto &II : Inputs) { 5460b57cec5SDimitry Andric if (II.getType() == types::TY_LLVM_IR || 5470b57cec5SDimitry Andric II.getType() == types::TY_LTO_IR || 5480b57cec5SDimitry Andric II.getType() == types::TY_LTO_BC || 5490b57cec5SDimitry Andric II.getType() == types::TY_LLVM_BC) { 5500b57cec5SDimitry Andric C.getDriver().Diag(diag::err_drv_no_linker_llvm_support) 5510b57cec5SDimitry Andric << getToolChain().getTripleString(); 5520b57cec5SDimitry Andric continue; 5530b57cec5SDimitry Andric } 5540b57cec5SDimitry Andric 5550b57cec5SDimitry Andric // Currently, we only pass the input files to the linker, we do not pass 5560b57cec5SDimitry Andric // any libraries that may be valid only for the host. 5570b57cec5SDimitry Andric if (!II.isFilename()) 5580b57cec5SDimitry Andric continue; 5590b57cec5SDimitry Andric 5600b57cec5SDimitry Andric const char *CubinF = C.addTempFile( 5610b57cec5SDimitry Andric C.getArgs().MakeArgString(getToolChain().getInputFilename(II))); 5620b57cec5SDimitry Andric 5630b57cec5SDimitry Andric CmdArgs.push_back(CubinF); 5640b57cec5SDimitry Andric } 5650b57cec5SDimitry Andric 5660b57cec5SDimitry Andric const char *Exec = 5670b57cec5SDimitry Andric Args.MakeArgString(getToolChain().GetProgramPath("nvlink")); 568*a7dea167SDimitry Andric C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); 5690b57cec5SDimitry Andric } 5700b57cec5SDimitry Andric 5710b57cec5SDimitry Andric /// CUDA toolchain. Our assembler is ptxas, and our "linker" is fatbinary, 5720b57cec5SDimitry Andric /// which isn't properly a linker but nonetheless performs the step of stitching 5730b57cec5SDimitry Andric /// together object files from the assembler into a single blob. 5740b57cec5SDimitry Andric 5750b57cec5SDimitry Andric CudaToolChain::CudaToolChain(const Driver &D, const llvm::Triple &Triple, 5760b57cec5SDimitry Andric const ToolChain &HostTC, const ArgList &Args, 5770b57cec5SDimitry Andric const Action::OffloadKind OK) 5780b57cec5SDimitry Andric : ToolChain(D, Triple, Args), HostTC(HostTC), 5790b57cec5SDimitry Andric CudaInstallation(D, HostTC.getTriple(), Args), OK(OK) { 5800b57cec5SDimitry Andric if (CudaInstallation.isValid()) 5810b57cec5SDimitry Andric getProgramPaths().push_back(CudaInstallation.getBinPath()); 5820b57cec5SDimitry Andric // Lookup binaries into the driver directory, this is used to 5830b57cec5SDimitry Andric // discover the clang-offload-bundler executable. 5840b57cec5SDimitry Andric getProgramPaths().push_back(getDriver().Dir); 5850b57cec5SDimitry Andric } 5860b57cec5SDimitry Andric 5870b57cec5SDimitry Andric std::string CudaToolChain::getInputFilename(const InputInfo &Input) const { 5880b57cec5SDimitry Andric // Only object files are changed, for example assembly files keep their .s 5890b57cec5SDimitry Andric // extensions. CUDA also continues to use .o as they don't use nvlink but 5900b57cec5SDimitry Andric // fatbinary. 5910b57cec5SDimitry Andric if (!(OK == Action::OFK_OpenMP && Input.getType() == types::TY_Object)) 5920b57cec5SDimitry Andric return ToolChain::getInputFilename(Input); 5930b57cec5SDimitry Andric 5940b57cec5SDimitry Andric // Replace extension for object files with cubin because nvlink relies on 5950b57cec5SDimitry Andric // these particular file names. 5960b57cec5SDimitry Andric SmallString<256> Filename(ToolChain::getInputFilename(Input)); 5970b57cec5SDimitry Andric llvm::sys::path::replace_extension(Filename, "cubin"); 5980b57cec5SDimitry Andric return Filename.str(); 5990b57cec5SDimitry Andric } 6000b57cec5SDimitry Andric 6010b57cec5SDimitry Andric void CudaToolChain::addClangTargetOptions( 6020b57cec5SDimitry Andric const llvm::opt::ArgList &DriverArgs, 6030b57cec5SDimitry Andric llvm::opt::ArgStringList &CC1Args, 6040b57cec5SDimitry Andric Action::OffloadKind DeviceOffloadingKind) const { 6050b57cec5SDimitry Andric HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind); 6060b57cec5SDimitry Andric 6070b57cec5SDimitry Andric StringRef GpuArch = DriverArgs.getLastArgValue(options::OPT_march_EQ); 6080b57cec5SDimitry Andric assert(!GpuArch.empty() && "Must have an explicit GPU arch."); 6090b57cec5SDimitry Andric assert((DeviceOffloadingKind == Action::OFK_OpenMP || 6100b57cec5SDimitry Andric DeviceOffloadingKind == Action::OFK_Cuda) && 6110b57cec5SDimitry Andric "Only OpenMP or CUDA offloading kinds are supported for NVIDIA GPUs."); 6120b57cec5SDimitry Andric 6130b57cec5SDimitry Andric if (DeviceOffloadingKind == Action::OFK_Cuda) { 6140b57cec5SDimitry Andric CC1Args.push_back("-fcuda-is-device"); 6150b57cec5SDimitry Andric 6160b57cec5SDimitry Andric if (DriverArgs.hasFlag(options::OPT_fcuda_flush_denormals_to_zero, 6170b57cec5SDimitry Andric options::OPT_fno_cuda_flush_denormals_to_zero, false)) 6180b57cec5SDimitry Andric CC1Args.push_back("-fcuda-flush-denormals-to-zero"); 6190b57cec5SDimitry Andric 6200b57cec5SDimitry Andric if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals, 6210b57cec5SDimitry Andric options::OPT_fno_cuda_approx_transcendentals, false)) 6220b57cec5SDimitry Andric CC1Args.push_back("-fcuda-approx-transcendentals"); 6230b57cec5SDimitry Andric 6240b57cec5SDimitry Andric if (DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, 6250b57cec5SDimitry Andric false)) 6260b57cec5SDimitry Andric CC1Args.push_back("-fgpu-rdc"); 6270b57cec5SDimitry Andric } 6280b57cec5SDimitry Andric 629*a7dea167SDimitry Andric if (DriverArgs.hasArg(options::OPT_nogpulib)) 6300b57cec5SDimitry Andric return; 6310b57cec5SDimitry Andric 6320b57cec5SDimitry Andric std::string LibDeviceFile = CudaInstallation.getLibDeviceFile(GpuArch); 6330b57cec5SDimitry Andric 6340b57cec5SDimitry Andric if (LibDeviceFile.empty()) { 6350b57cec5SDimitry Andric if (DeviceOffloadingKind == Action::OFK_OpenMP && 6360b57cec5SDimitry Andric DriverArgs.hasArg(options::OPT_S)) 6370b57cec5SDimitry Andric return; 6380b57cec5SDimitry Andric 6390b57cec5SDimitry Andric getDriver().Diag(diag::err_drv_no_cuda_libdevice) << GpuArch; 6400b57cec5SDimitry Andric return; 6410b57cec5SDimitry Andric } 6420b57cec5SDimitry Andric 6430b57cec5SDimitry Andric CC1Args.push_back("-mlink-builtin-bitcode"); 6440b57cec5SDimitry Andric CC1Args.push_back(DriverArgs.MakeArgString(LibDeviceFile)); 6450b57cec5SDimitry Andric 6460b57cec5SDimitry Andric // New CUDA versions often introduce new instructions that are only supported 6470b57cec5SDimitry Andric // by new PTX version, so we need to raise PTX level to enable them in NVPTX 6480b57cec5SDimitry Andric // back-end. 6490b57cec5SDimitry Andric const char *PtxFeature = nullptr; 6500b57cec5SDimitry Andric switch(CudaInstallation.version()) { 6510b57cec5SDimitry Andric case CudaVersion::CUDA_101: 6520b57cec5SDimitry Andric PtxFeature = "+ptx64"; 6530b57cec5SDimitry Andric break; 6540b57cec5SDimitry Andric case CudaVersion::CUDA_100: 6550b57cec5SDimitry Andric PtxFeature = "+ptx63"; 6560b57cec5SDimitry Andric break; 6570b57cec5SDimitry Andric case CudaVersion::CUDA_92: 6580b57cec5SDimitry Andric PtxFeature = "+ptx61"; 6590b57cec5SDimitry Andric break; 6600b57cec5SDimitry Andric case CudaVersion::CUDA_91: 6610b57cec5SDimitry Andric PtxFeature = "+ptx61"; 6620b57cec5SDimitry Andric break; 6630b57cec5SDimitry Andric case CudaVersion::CUDA_90: 6640b57cec5SDimitry Andric PtxFeature = "+ptx60"; 6650b57cec5SDimitry Andric break; 6660b57cec5SDimitry Andric default: 6670b57cec5SDimitry Andric PtxFeature = "+ptx42"; 6680b57cec5SDimitry Andric } 6690b57cec5SDimitry Andric CC1Args.append({"-target-feature", PtxFeature}); 6700b57cec5SDimitry Andric if (DriverArgs.hasFlag(options::OPT_fcuda_short_ptr, 6710b57cec5SDimitry Andric options::OPT_fno_cuda_short_ptr, false)) 6720b57cec5SDimitry Andric CC1Args.append({"-mllvm", "--nvptx-short-ptr"}); 6730b57cec5SDimitry Andric 6740b57cec5SDimitry Andric if (CudaInstallation.version() >= CudaVersion::UNKNOWN) 6750b57cec5SDimitry Andric CC1Args.push_back(DriverArgs.MakeArgString( 6760b57cec5SDimitry Andric Twine("-target-sdk-version=") + 6770b57cec5SDimitry Andric CudaVersionToString(CudaInstallation.version()))); 6780b57cec5SDimitry Andric 6790b57cec5SDimitry Andric if (DeviceOffloadingKind == Action::OFK_OpenMP) { 6800b57cec5SDimitry Andric SmallVector<StringRef, 8> LibraryPaths; 6810b57cec5SDimitry Andric if (const Arg *A = DriverArgs.getLastArg(options::OPT_libomptarget_nvptx_path_EQ)) 6820b57cec5SDimitry Andric LibraryPaths.push_back(A->getValue()); 6830b57cec5SDimitry Andric 6840b57cec5SDimitry Andric // Add user defined library paths from LIBRARY_PATH. 6850b57cec5SDimitry Andric llvm::Optional<std::string> LibPath = 6860b57cec5SDimitry Andric llvm::sys::Process::GetEnv("LIBRARY_PATH"); 6870b57cec5SDimitry Andric if (LibPath) { 6880b57cec5SDimitry Andric SmallVector<StringRef, 8> Frags; 6890b57cec5SDimitry Andric const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'}; 6900b57cec5SDimitry Andric llvm::SplitString(*LibPath, Frags, EnvPathSeparatorStr); 6910b57cec5SDimitry Andric for (StringRef Path : Frags) 6920b57cec5SDimitry Andric LibraryPaths.emplace_back(Path.trim()); 6930b57cec5SDimitry Andric } 6940b57cec5SDimitry Andric 6950b57cec5SDimitry Andric // Add path to lib / lib64 folder. 6960b57cec5SDimitry Andric SmallString<256> DefaultLibPath = 6970b57cec5SDimitry Andric llvm::sys::path::parent_path(getDriver().Dir); 6980b57cec5SDimitry Andric llvm::sys::path::append(DefaultLibPath, Twine("lib") + CLANG_LIBDIR_SUFFIX); 6990b57cec5SDimitry Andric LibraryPaths.emplace_back(DefaultLibPath.c_str()); 7000b57cec5SDimitry Andric 7010b57cec5SDimitry Andric std::string LibOmpTargetName = 7020b57cec5SDimitry Andric "libomptarget-nvptx-" + GpuArch.str() + ".bc"; 7030b57cec5SDimitry Andric bool FoundBCLibrary = false; 7040b57cec5SDimitry Andric for (StringRef LibraryPath : LibraryPaths) { 7050b57cec5SDimitry Andric SmallString<128> LibOmpTargetFile(LibraryPath); 7060b57cec5SDimitry Andric llvm::sys::path::append(LibOmpTargetFile, LibOmpTargetName); 7070b57cec5SDimitry Andric if (llvm::sys::fs::exists(LibOmpTargetFile)) { 7080b57cec5SDimitry Andric CC1Args.push_back("-mlink-builtin-bitcode"); 7090b57cec5SDimitry Andric CC1Args.push_back(DriverArgs.MakeArgString(LibOmpTargetFile)); 7100b57cec5SDimitry Andric FoundBCLibrary = true; 7110b57cec5SDimitry Andric break; 7120b57cec5SDimitry Andric } 7130b57cec5SDimitry Andric } 7140b57cec5SDimitry Andric if (!FoundBCLibrary) 7150b57cec5SDimitry Andric getDriver().Diag(diag::warn_drv_omp_offload_target_missingbcruntime) 7160b57cec5SDimitry Andric << LibOmpTargetName; 7170b57cec5SDimitry Andric } 7180b57cec5SDimitry Andric } 7190b57cec5SDimitry Andric 7200b57cec5SDimitry Andric bool CudaToolChain::supportsDebugInfoOption(const llvm::opt::Arg *A) const { 7210b57cec5SDimitry Andric const Option &O = A->getOption(); 7220b57cec5SDimitry Andric return (O.matches(options::OPT_gN_Group) && 7230b57cec5SDimitry Andric !O.matches(options::OPT_gmodules)) || 7240b57cec5SDimitry Andric O.matches(options::OPT_g_Flag) || 7250b57cec5SDimitry Andric O.matches(options::OPT_ggdbN_Group) || O.matches(options::OPT_ggdb) || 7260b57cec5SDimitry Andric O.matches(options::OPT_gdwarf) || O.matches(options::OPT_gdwarf_2) || 7270b57cec5SDimitry Andric O.matches(options::OPT_gdwarf_3) || O.matches(options::OPT_gdwarf_4) || 7280b57cec5SDimitry Andric O.matches(options::OPT_gdwarf_5) || 7290b57cec5SDimitry Andric O.matches(options::OPT_gcolumn_info); 7300b57cec5SDimitry Andric } 7310b57cec5SDimitry Andric 7320b57cec5SDimitry Andric void CudaToolChain::adjustDebugInfoKind( 7330b57cec5SDimitry Andric codegenoptions::DebugInfoKind &DebugInfoKind, const ArgList &Args) const { 7340b57cec5SDimitry Andric switch (mustEmitDebugInfo(Args)) { 7350b57cec5SDimitry Andric case DisableDebugInfo: 7360b57cec5SDimitry Andric DebugInfoKind = codegenoptions::NoDebugInfo; 7370b57cec5SDimitry Andric break; 7380b57cec5SDimitry Andric case DebugDirectivesOnly: 7390b57cec5SDimitry Andric DebugInfoKind = codegenoptions::DebugDirectivesOnly; 7400b57cec5SDimitry Andric break; 7410b57cec5SDimitry Andric case EmitSameDebugInfoAsHost: 7420b57cec5SDimitry Andric // Use same debug info level as the host. 7430b57cec5SDimitry Andric break; 7440b57cec5SDimitry Andric } 7450b57cec5SDimitry Andric } 7460b57cec5SDimitry Andric 7470b57cec5SDimitry Andric void CudaToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs, 7480b57cec5SDimitry Andric ArgStringList &CC1Args) const { 7490b57cec5SDimitry Andric // Check our CUDA version if we're going to include the CUDA headers. 7500b57cec5SDimitry Andric if (!DriverArgs.hasArg(options::OPT_nocudainc) && 7510b57cec5SDimitry Andric !DriverArgs.hasArg(options::OPT_no_cuda_version_check)) { 7520b57cec5SDimitry Andric StringRef Arch = DriverArgs.getLastArgValue(options::OPT_march_EQ); 7530b57cec5SDimitry Andric assert(!Arch.empty() && "Must have an explicit GPU arch."); 7540b57cec5SDimitry Andric CudaInstallation.CheckCudaVersionSupportsArch(StringToCudaArch(Arch)); 7550b57cec5SDimitry Andric } 7560b57cec5SDimitry Andric CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args); 7570b57cec5SDimitry Andric } 7580b57cec5SDimitry Andric 7590b57cec5SDimitry Andric llvm::opt::DerivedArgList * 7600b57cec5SDimitry Andric CudaToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args, 7610b57cec5SDimitry Andric StringRef BoundArch, 7620b57cec5SDimitry Andric Action::OffloadKind DeviceOffloadKind) const { 7630b57cec5SDimitry Andric DerivedArgList *DAL = 7640b57cec5SDimitry Andric HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind); 7650b57cec5SDimitry Andric if (!DAL) 7660b57cec5SDimitry Andric DAL = new DerivedArgList(Args.getBaseArgs()); 7670b57cec5SDimitry Andric 7680b57cec5SDimitry Andric const OptTable &Opts = getDriver().getOpts(); 7690b57cec5SDimitry Andric 7700b57cec5SDimitry Andric // For OpenMP device offloading, append derived arguments. Make sure 7710b57cec5SDimitry Andric // flags are not duplicated. 7720b57cec5SDimitry Andric // Also append the compute capability. 7730b57cec5SDimitry Andric if (DeviceOffloadKind == Action::OFK_OpenMP) { 7740b57cec5SDimitry Andric for (Arg *A : Args) { 7750b57cec5SDimitry Andric bool IsDuplicate = false; 7760b57cec5SDimitry Andric for (Arg *DALArg : *DAL) { 7770b57cec5SDimitry Andric if (A == DALArg) { 7780b57cec5SDimitry Andric IsDuplicate = true; 7790b57cec5SDimitry Andric break; 7800b57cec5SDimitry Andric } 7810b57cec5SDimitry Andric } 7820b57cec5SDimitry Andric if (!IsDuplicate) 7830b57cec5SDimitry Andric DAL->append(A); 7840b57cec5SDimitry Andric } 7850b57cec5SDimitry Andric 7860b57cec5SDimitry Andric StringRef Arch = DAL->getLastArgValue(options::OPT_march_EQ); 7870b57cec5SDimitry Andric if (Arch.empty()) 7880b57cec5SDimitry Andric DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ), 7890b57cec5SDimitry Andric CLANG_OPENMP_NVPTX_DEFAULT_ARCH); 7900b57cec5SDimitry Andric 7910b57cec5SDimitry Andric return DAL; 7920b57cec5SDimitry Andric } 7930b57cec5SDimitry Andric 7940b57cec5SDimitry Andric for (Arg *A : Args) { 7950b57cec5SDimitry Andric if (A->getOption().matches(options::OPT_Xarch__)) { 7960b57cec5SDimitry Andric // Skip this argument unless the architecture matches BoundArch 7970b57cec5SDimitry Andric if (BoundArch.empty() || A->getValue(0) != BoundArch) 7980b57cec5SDimitry Andric continue; 7990b57cec5SDimitry Andric 8000b57cec5SDimitry Andric unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1)); 8010b57cec5SDimitry Andric unsigned Prev = Index; 8020b57cec5SDimitry Andric std::unique_ptr<Arg> XarchArg(Opts.ParseOneArg(Args, Index)); 8030b57cec5SDimitry Andric 8040b57cec5SDimitry Andric // If the argument parsing failed or more than one argument was 8050b57cec5SDimitry Andric // consumed, the -Xarch_ argument's parameter tried to consume 8060b57cec5SDimitry Andric // extra arguments. Emit an error and ignore. 8070b57cec5SDimitry Andric // 8080b57cec5SDimitry Andric // We also want to disallow any options which would alter the 8090b57cec5SDimitry Andric // driver behavior; that isn't going to work in our model. We 8100b57cec5SDimitry Andric // use isDriverOption() as an approximation, although things 8110b57cec5SDimitry Andric // like -O4 are going to slip through. 8120b57cec5SDimitry Andric if (!XarchArg || Index > Prev + 1) { 8130b57cec5SDimitry Andric getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args) 8140b57cec5SDimitry Andric << A->getAsString(Args); 8150b57cec5SDimitry Andric continue; 8160b57cec5SDimitry Andric } else if (XarchArg->getOption().hasFlag(options::DriverOption)) { 8170b57cec5SDimitry Andric getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver) 8180b57cec5SDimitry Andric << A->getAsString(Args); 8190b57cec5SDimitry Andric continue; 8200b57cec5SDimitry Andric } 8210b57cec5SDimitry Andric XarchArg->setBaseArg(A); 8220b57cec5SDimitry Andric A = XarchArg.release(); 8230b57cec5SDimitry Andric DAL->AddSynthesizedArg(A); 8240b57cec5SDimitry Andric } 8250b57cec5SDimitry Andric DAL->append(A); 8260b57cec5SDimitry Andric } 8270b57cec5SDimitry Andric 8280b57cec5SDimitry Andric if (!BoundArch.empty()) { 8290b57cec5SDimitry Andric DAL->eraseArg(options::OPT_march_EQ); 8300b57cec5SDimitry Andric DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ), BoundArch); 8310b57cec5SDimitry Andric } 8320b57cec5SDimitry Andric return DAL; 8330b57cec5SDimitry Andric } 8340b57cec5SDimitry Andric 8350b57cec5SDimitry Andric Tool *CudaToolChain::buildAssembler() const { 8360b57cec5SDimitry Andric return new tools::NVPTX::Assembler(*this); 8370b57cec5SDimitry Andric } 8380b57cec5SDimitry Andric 8390b57cec5SDimitry Andric Tool *CudaToolChain::buildLinker() const { 8400b57cec5SDimitry Andric if (OK == Action::OFK_OpenMP) 8410b57cec5SDimitry Andric return new tools::NVPTX::OpenMPLinker(*this); 8420b57cec5SDimitry Andric return new tools::NVPTX::Linker(*this); 8430b57cec5SDimitry Andric } 8440b57cec5SDimitry Andric 8450b57cec5SDimitry Andric void CudaToolChain::addClangWarningOptions(ArgStringList &CC1Args) const { 8460b57cec5SDimitry Andric HostTC.addClangWarningOptions(CC1Args); 8470b57cec5SDimitry Andric } 8480b57cec5SDimitry Andric 8490b57cec5SDimitry Andric ToolChain::CXXStdlibType 8500b57cec5SDimitry Andric CudaToolChain::GetCXXStdlibType(const ArgList &Args) const { 8510b57cec5SDimitry Andric return HostTC.GetCXXStdlibType(Args); 8520b57cec5SDimitry Andric } 8530b57cec5SDimitry Andric 8540b57cec5SDimitry Andric void CudaToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 8550b57cec5SDimitry Andric ArgStringList &CC1Args) const { 8560b57cec5SDimitry Andric HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args); 8570b57cec5SDimitry Andric } 8580b57cec5SDimitry Andric 8590b57cec5SDimitry Andric void CudaToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &Args, 8600b57cec5SDimitry Andric ArgStringList &CC1Args) const { 8610b57cec5SDimitry Andric HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args); 8620b57cec5SDimitry Andric } 8630b57cec5SDimitry Andric 8640b57cec5SDimitry Andric void CudaToolChain::AddIAMCUIncludeArgs(const ArgList &Args, 8650b57cec5SDimitry Andric ArgStringList &CC1Args) const { 8660b57cec5SDimitry Andric HostTC.AddIAMCUIncludeArgs(Args, CC1Args); 8670b57cec5SDimitry Andric } 8680b57cec5SDimitry Andric 8690b57cec5SDimitry Andric SanitizerMask CudaToolChain::getSupportedSanitizers() const { 8700b57cec5SDimitry Andric // The CudaToolChain only supports sanitizers in the sense that it allows 8710b57cec5SDimitry Andric // sanitizer arguments on the command line if they are supported by the host 8720b57cec5SDimitry Andric // toolchain. The CudaToolChain will actually ignore any command line 8730b57cec5SDimitry Andric // arguments for any of these "supported" sanitizers. That means that no 8740b57cec5SDimitry Andric // sanitization of device code is actually supported at this time. 8750b57cec5SDimitry Andric // 8760b57cec5SDimitry Andric // This behavior is necessary because the host and device toolchains 8770b57cec5SDimitry Andric // invocations often share the command line, so the device toolchain must 8780b57cec5SDimitry Andric // tolerate flags meant only for the host toolchain. 8790b57cec5SDimitry Andric return HostTC.getSupportedSanitizers(); 8800b57cec5SDimitry Andric } 8810b57cec5SDimitry Andric 8820b57cec5SDimitry Andric VersionTuple CudaToolChain::computeMSVCVersion(const Driver *D, 8830b57cec5SDimitry Andric const ArgList &Args) const { 8840b57cec5SDimitry Andric return HostTC.computeMSVCVersion(D, Args); 8850b57cec5SDimitry Andric } 886