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