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