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