blob: c83d066fa26e353656ad6e8c037e6db9977894ff [file] [log] [blame]
David L. Jonesf561aba2017-03-08 01:02:16 +00001//===--- 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 Bercea2c926932017-08-08 14:33:05 +000011#include "CommonArgs.h"
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +000012#include "InputInfo.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000013#include "clang/Basic/Cuda.h"
14#include "clang/Basic/VirtualFileSystem.h"
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +000015#include "clang/Config/config.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000016#include "clang/Driver/Compilation.h"
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +000017#include "clang/Driver/Distro.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000018#include "clang/Driver/Driver.h"
19#include "clang/Driver/DriverDiagnostic.h"
20#include "clang/Driver/Options.h"
21#include "llvm/Option/ArgList.h"
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +000022#include "llvm/Support/FileSystem.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000023#include "llvm/Support/Path.h"
Gheorghe-Teodor Bercea148046c2018-03-13 19:39:19 +000024#include "llvm/Support/Process.h"
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +000025#include "llvm/Support/Program.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000026#include <system_error>
27
28using namespace clang::driver;
29using namespace clang::driver::toolchains;
30using namespace clang::driver::tools;
31using namespace clang;
32using 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".
36static 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 Belevich8af4e232017-09-07 18:14:32 +000056 if (Major == 9 && Minor == 0)
57 return CudaVersion::CUDA_90;
Artem Belevichfbc56a92018-01-30 00:00:12 +000058 if (Major == 9 && Minor == 1)
59 return CudaVersion::CUDA_91;
David L. Jonesf561aba2017-03-08 01:02:16 +000060 return CudaVersion::UNKNOWN;
61}
62
63CudaInstallationDetector::CudaInstallationDetector(
64 const Driver &D, const llvm::Triple &HostTriple,
65 const llvm::opt::ArgList &Args)
66 : D(D) {
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +000067 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. Jonesf561aba2017-03-08 01:02:16 +000075
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 Hahnfeld7f9c5182018-01-31 08:26:51 +000080 Candidates.emplace_back(
81 Args.getLastArgValue(clang::driver::options::OPT_cuda_path_EQ).str());
David L. Jonesf561aba2017-03-08 01:02:16 +000082 } else if (HostTriple.isOSWindows()) {
83 for (const char *Ver : Versions)
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +000084 Candidates.emplace_back(
David L. Jonesf561aba2017-03-08 01:02:16 +000085 D.SysRoot + "/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v" +
86 Ver);
87 } else {
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +000088 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. Jonesf561aba2017-03-08 01:02:16 +0000110 for (const char *Ver : Versions)
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +0000111 Candidates.emplace_back(D.SysRoot + "/usr/local/cuda-" + Ver);
Sylvestre Ledru0cfcdc32017-11-29 15:03:28 +0000112
Ismail Donmez64f99df2017-11-29 15:18:02 +0000113 if (Distro(D.getVFS()).IsDebian())
Sylvestre Ledru0cfcdc32017-11-29 15:03:28 +0000114 // Special case for Debian to have nvidia-cuda-toolkit work
115 // out of the box. More info on http://bugs.debian.org/882505
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +0000116 Candidates.emplace_back(D.SysRoot + "/usr/lib/cuda");
David L. Jonesf561aba2017-03-08 01:02:16 +0000117 }
118
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +0000119 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. Jonesf561aba2017-03-08 01:02:16 +0000124 continue;
125
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +0000126 BinPath = InstallPath + "/bin";
David L. Jonesf561aba2017-03-08 01:02:16 +0000127 IncludePath = InstallPath + "/include";
128 LibDevicePath = InstallPath + "/nvvm/libdevice";
129
130 auto &FS = D.getVFS();
Jonas Hahnfelde2c342f2017-10-16 13:31:30 +0000131 if (!(FS.exists(IncludePath) && FS.exists(BinPath)))
David L. Jonesf561aba2017-03-08 01:02:16 +0000132 continue;
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +0000133 bool CheckLibDevice = (!NoCudaLib || Candidate.StrictChecking);
134 if (CheckLibDevice && !FS.exists(LibDevicePath))
135 continue;
David L. Jonesf561aba2017-03-08 01:02:16 +0000136
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 Belevichfbc56a92018-01-30 00:00:12 +0000160 if (Version >= CudaVersion::CUDA_90) {
161 // CUDA-9+ uses single libdevice file for all GPU variants.
Artem Belevich8af4e232017-09-07 18:14:32 +0000162 std::string FilePath = LibDevicePath + "/libdevice.10.bc";
163 if (FS.exists(FilePath)) {
Artem Belevichfbc56a92018-01-30 00:00:12 +0000164 for (const char *GpuArchName :
Artem Belevich8af4e232017-09-07 18:14:32 +0000165 {"sm_20", "sm_30", "sm_32", "sm_35", "sm_50", "sm_52", "sm_53",
Artem Belevichfbc56a92018-01-30 00:00:12 +0000166 "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 Belevich8af4e232017-09-07 18:14:32 +0000172 }
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 Kornienko2a8c18d2018-04-06 15:14:32 +0000187 // Insert map entries for specific devices with this compute
Artem Belevich8af4e232017-09-07 18:14:32 +0000188 // 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. Jonesf561aba2017-03-08 01:02:16 +0000213 }
214 }
215 }
216
Jonas Hahnfelde2c342f2017-10-16 13:31:30 +0000217 // Check that we have found at least one libdevice that we can link in if
218 // -nocudalib hasn't been specified.
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +0000219 if (LibDeviceMap.empty() && !NoCudaLib)
Gheorghe-Teodor Bercea9c525742017-08-11 15:46:22 +0000220 continue;
221
David L. Jonesf561aba2017-03-08 01:02:16 +0000222 IsValid = true;
223 break;
224 }
225}
226
227void 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
253void CudaInstallationDetector::CheckCudaVersionSupportsArch(
254 CudaArch Arch) const {
255 if (Arch == CudaArch::UNKNOWN || Version == CudaVersion::UNKNOWN ||
Justin Lebar066494d2017-10-25 21:32:06 +0000256 ArchsWithBadVersion.count(Arch) > 0)
David L. Jonesf561aba2017-03-08 01:02:16 +0000257 return;
258
Justin Lebar066494d2017-10-25 21:32:06 +0000259 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. Jonesf561aba2017-03-08 01:02:16 +0000267 }
268}
269
270void CudaInstallationDetector::print(raw_ostream &OS) const {
271 if (isValid())
272 OS << "Found CUDA installation: " << InstallPath << ", version "
273 << CudaVersionToString(Version) << "\n";
274}
275
276void NVPTX::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
277 const InputInfo &Output,
278 const InputInfoList &Inputs,
279 const ArgList &Args,
280 const char *LinkingOutput) const {
281 const auto &TC =
282 static_cast<const toolchains::CudaToolChain &>(getToolChain());
283 assert(TC.getTriple().isNVPTX() && "Wrong platform");
284
Gheorghe-Teodor Bercea47e0cf32017-08-07 15:39:11 +0000285 StringRef GPUArchName;
286 // If this is an OpenMP action we need to extract the device architecture
287 // from the -march=arch option. This option may come from -Xopenmp-target
288 // flag or the default value.
289 if (JA.isDeviceOffloading(Action::OFK_OpenMP)) {
290 GPUArchName = Args.getLastArgValue(options::OPT_march_EQ);
291 assert(!GPUArchName.empty() && "Must have an architecture passed in.");
292 } else
293 GPUArchName = JA.getOffloadingArch();
294
David L. Jonesf561aba2017-03-08 01:02:16 +0000295 // Obtain architecture from the action.
Gheorghe-Teodor Bercea47e0cf32017-08-07 15:39:11 +0000296 CudaArch gpu_arch = StringToCudaArch(GPUArchName);
David L. Jonesf561aba2017-03-08 01:02:16 +0000297 assert(gpu_arch != CudaArch::UNKNOWN &&
298 "Device action expected to have an architecture.");
299
300 // Check that our installation's ptxas supports gpu_arch.
301 if (!Args.hasArg(options::OPT_no_cuda_version_check)) {
302 TC.CudaInstallation.CheckCudaVersionSupportsArch(gpu_arch);
303 }
304
305 ArgStringList CmdArgs;
306 CmdArgs.push_back(TC.getTriple().isArch64Bit() ? "-m64" : "-m32");
307 if (Args.hasFlag(options::OPT_cuda_noopt_device_debug,
308 options::OPT_no_cuda_noopt_device_debug, false)) {
309 // ptxas does not accept -g option if optimization is enabled, so
310 // we ignore the compiler's -O* options if we want debug info.
311 CmdArgs.push_back("-g");
312 CmdArgs.push_back("--dont-merge-basicblocks");
313 CmdArgs.push_back("--return-at-end");
314 } else if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
315 // Map the -O we received to -O{0,1,2,3}.
316 //
317 // TODO: Perhaps we should map host -O2 to ptxas -O3. -O3 is ptxas's
318 // default, so it may correspond more closely to the spirit of clang -O2.
319
320 // -O3 seems like the least-bad option when -Osomething is specified to
321 // clang but it isn't handled below.
322 StringRef OOpt = "3";
323 if (A->getOption().matches(options::OPT_O4) ||
324 A->getOption().matches(options::OPT_Ofast))
325 OOpt = "3";
326 else if (A->getOption().matches(options::OPT_O0))
327 OOpt = "0";
328 else if (A->getOption().matches(options::OPT_O)) {
329 // -Os, -Oz, and -O(anything else) map to -O2, for lack of better options.
330 OOpt = llvm::StringSwitch<const char *>(A->getValue())
331 .Case("1", "1")
332 .Case("2", "2")
333 .Case("3", "3")
334 .Case("s", "2")
335 .Case("z", "2")
336 .Default("2");
337 }
338 CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
339 } else {
340 // If no -O was passed, pass -O0 to ptxas -- no opt flag should correspond
341 // to no optimizations, but ptxas's default is -O3.
342 CmdArgs.push_back("-O0");
343 }
344
Gheorghe-Teodor Bercea53431bc2017-08-07 20:19:23 +0000345 // Pass -v to ptxas if it was passed to the driver.
346 if (Args.hasArg(options::OPT_v))
347 CmdArgs.push_back("-v");
348
David L. Jonesf561aba2017-03-08 01:02:16 +0000349 CmdArgs.push_back("--gpu-name");
350 CmdArgs.push_back(Args.MakeArgString(CudaArchToString(gpu_arch)));
351 CmdArgs.push_back("--output-file");
Jonas Hahnfeld7c78cc52017-11-21 14:44:45 +0000352 CmdArgs.push_back(Args.MakeArgString(TC.getInputFilename(Output)));
David L. Jonesf561aba2017-03-08 01:02:16 +0000353 for (const auto& II : Inputs)
354 CmdArgs.push_back(Args.MakeArgString(II.getFilename()));
355
356 for (const auto& A : Args.getAllArgValues(options::OPT_Xcuda_ptxas))
357 CmdArgs.push_back(Args.MakeArgString(A));
358
Jonas Hahnfeld5379c6d2018-02-12 10:46:45 +0000359 bool Relocatable = false;
360 if (JA.isOffloading(Action::OFK_OpenMP))
361 // In OpenMP we need to generate relocatable code.
362 Relocatable = Args.hasFlag(options::OPT_fopenmp_relocatable_target,
363 options::OPT_fnoopenmp_relocatable_target,
364 /*Default=*/true);
365 else if (JA.isOffloading(Action::OFK_Cuda))
366 Relocatable = Args.hasFlag(options::OPT_fcuda_rdc,
367 options::OPT_fno_cuda_rdc, /*Default=*/false);
368
369 if (Relocatable)
Gheorghe-Teodor Berceab9d11722017-08-09 14:59:35 +0000370 CmdArgs.push_back("-c");
371
David L. Jonesf561aba2017-03-08 01:02:16 +0000372 const char *Exec;
373 if (Arg *A = Args.getLastArg(options::OPT_ptxas_path_EQ))
374 Exec = A->getValue();
375 else
376 Exec = Args.MakeArgString(TC.GetProgramPath("ptxas"));
377 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
378}
379
Artem Belevichdde3dc22018-04-10 18:38:22 +0000380static bool shouldIncludePTX(const ArgList &Args, const char *gpu_arch) {
381 bool includePTX = true;
382 for (Arg *A : Args) {
383 if (!(A->getOption().matches(options::OPT_cuda_include_ptx_EQ) ||
384 A->getOption().matches(options::OPT_no_cuda_include_ptx_EQ)))
385 continue;
386 A->claim();
387 const StringRef ArchStr = A->getValue();
388 if (ArchStr == "all" || ArchStr == gpu_arch) {
389 includePTX = A->getOption().matches(options::OPT_cuda_include_ptx_EQ);
390 continue;
391 }
392 }
393 return includePTX;
394}
395
David L. Jonesf561aba2017-03-08 01:02:16 +0000396// All inputs to this linker must be from CudaDeviceActions, as we need to look
397// at the Inputs' Actions in order to figure out which GPU architecture they
398// correspond to.
399void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
400 const InputInfo &Output,
401 const InputInfoList &Inputs,
402 const ArgList &Args,
403 const char *LinkingOutput) const {
404 const auto &TC =
405 static_cast<const toolchains::CudaToolChain &>(getToolChain());
406 assert(TC.getTriple().isNVPTX() && "Wrong platform");
407
408 ArgStringList CmdArgs;
409 CmdArgs.push_back("--cuda");
410 CmdArgs.push_back(TC.getTriple().isArch64Bit() ? "-64" : "-32");
411 CmdArgs.push_back(Args.MakeArgString("--create"));
412 CmdArgs.push_back(Args.MakeArgString(Output.getFilename()));
413
414 for (const auto& II : Inputs) {
415 auto *A = II.getAction();
416 assert(A->getInputs().size() == 1 &&
417 "Device offload action is expected to have a single input");
418 const char *gpu_arch_str = A->getOffloadingArch();
419 assert(gpu_arch_str &&
420 "Device action expected to have associated a GPU architecture!");
421 CudaArch gpu_arch = StringToCudaArch(gpu_arch_str);
422
Artem Belevichdde3dc22018-04-10 18:38:22 +0000423 if (II.getType() == types::TY_PP_Asm &&
424 !shouldIncludePTX(Args, gpu_arch_str))
425 continue;
David L. Jonesf561aba2017-03-08 01:02:16 +0000426 // We need to pass an Arch of the form "sm_XX" for cubin files and
427 // "compute_XX" for ptx.
428 const char *Arch =
429 (II.getType() == types::TY_PP_Asm)
430 ? CudaVirtualArchToString(VirtualArchForCudaArch(gpu_arch))
431 : gpu_arch_str;
432 CmdArgs.push_back(Args.MakeArgString(llvm::Twine("--image=profile=") +
433 Arch + ",file=" + II.getFilename()));
434 }
435
436 for (const auto& A : Args.getAllArgValues(options::OPT_Xcuda_fatbinary))
437 CmdArgs.push_back(Args.MakeArgString(A));
438
439 const char *Exec = Args.MakeArgString(TC.GetProgramPath("fatbinary"));
440 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
441}
442
Gheorghe-Teodor Bercea2c926932017-08-08 14:33:05 +0000443void NVPTX::OpenMPLinker::ConstructJob(Compilation &C, const JobAction &JA,
444 const InputInfo &Output,
445 const InputInfoList &Inputs,
446 const ArgList &Args,
447 const char *LinkingOutput) const {
448 const auto &TC =
449 static_cast<const toolchains::CudaToolChain &>(getToolChain());
450 assert(TC.getTriple().isNVPTX() && "Wrong platform");
451
452 ArgStringList CmdArgs;
453
454 // OpenMP uses nvlink to link cubin files. The result will be embedded in the
455 // host binary by the host linker.
456 assert(!JA.isHostOffloading(Action::OFK_OpenMP) &&
457 "CUDA toolchain not expected for an OpenMP host device.");
458
459 if (Output.isFilename()) {
460 CmdArgs.push_back("-o");
461 CmdArgs.push_back(Output.getFilename());
462 } else
463 assert(Output.isNothing() && "Invalid output.");
464 if (Args.hasArg(options::OPT_g_Flag))
465 CmdArgs.push_back("-g");
466
467 if (Args.hasArg(options::OPT_v))
468 CmdArgs.push_back("-v");
469
470 StringRef GPUArch =
471 Args.getLastArgValue(options::OPT_march_EQ);
472 assert(!GPUArch.empty() && "At least one GPU Arch required for ptxas.");
473
474 CmdArgs.push_back("-arch");
475 CmdArgs.push_back(Args.MakeArgString(GPUArch));
476
477 // Add paths specified in LIBRARY_PATH environment variable as -L options.
478 addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH");
479
480 // Add paths for the default clang library path.
481 SmallString<256> DefaultLibPath =
482 llvm::sys::path::parent_path(TC.getDriver().Dir);
483 llvm::sys::path::append(DefaultLibPath, "lib" CLANG_LIBDIR_SUFFIX);
484 CmdArgs.push_back(Args.MakeArgString(Twine("-L") + DefaultLibPath));
485
486 // Add linking against library implementing OpenMP calls on NVPTX target.
487 CmdArgs.push_back("-lomptarget-nvptx");
488
489 for (const auto &II : Inputs) {
490 if (II.getType() == types::TY_LLVM_IR ||
491 II.getType() == types::TY_LTO_IR ||
492 II.getType() == types::TY_LTO_BC ||
493 II.getType() == types::TY_LLVM_BC) {
494 C.getDriver().Diag(diag::err_drv_no_linker_llvm_support)
495 << getToolChain().getTripleString();
496 continue;
497 }
498
499 // Currently, we only pass the input files to the linker, we do not pass
500 // any libraries that may be valid only for the host.
501 if (!II.isFilename())
502 continue;
503
Jonas Hahnfeld7c78cc52017-11-21 14:44:45 +0000504 const char *CubinF = C.addTempFile(
505 C.getArgs().MakeArgString(getToolChain().getInputFilename(II)));
Gheorghe-Teodor Bercea2c926932017-08-08 14:33:05 +0000506
507 CmdArgs.push_back(CubinF);
508 }
509
510 AddOpenMPLinkerScript(getToolChain(), C, Output, Inputs, Args, CmdArgs, JA);
511
512 const char *Exec =
513 Args.MakeArgString(getToolChain().GetProgramPath("nvlink"));
514 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
515}
516
David L. Jonesf561aba2017-03-08 01:02:16 +0000517/// CUDA toolchain. Our assembler is ptxas, and our "linker" is fatbinary,
518/// which isn't properly a linker but nonetheless performs the step of stitching
519/// together object files from the assembler into a single blob.
520
521CudaToolChain::CudaToolChain(const Driver &D, const llvm::Triple &Triple,
Gheorghe-Teodor Bercea2c926932017-08-08 14:33:05 +0000522 const ToolChain &HostTC, const ArgList &Args,
523 const Action::OffloadKind OK)
David L. Jonesf561aba2017-03-08 01:02:16 +0000524 : ToolChain(D, Triple, Args), HostTC(HostTC),
Gheorghe-Teodor Bercea2c926932017-08-08 14:33:05 +0000525 CudaInstallation(D, HostTC.getTriple(), Args), OK(OK) {
David L. Jonesf561aba2017-03-08 01:02:16 +0000526 if (CudaInstallation.isValid())
527 getProgramPaths().push_back(CudaInstallation.getBinPath());
Gheorghe-Teodor Bercea690f6f92017-08-09 19:52:28 +0000528 // Lookup binaries into the driver directory, this is used to
529 // discover the clang-offload-bundler executable.
530 getProgramPaths().push_back(getDriver().Dir);
David L. Jonesf561aba2017-03-08 01:02:16 +0000531}
532
Jonas Hahnfeld7c78cc52017-11-21 14:44:45 +0000533std::string CudaToolChain::getInputFilename(const InputInfo &Input) const {
534 // Only object files are changed, for example assembly files keep their .s
535 // extensions. CUDA also continues to use .o as they don't use nvlink but
536 // fatbinary.
537 if (!(OK == Action::OFK_OpenMP && Input.getType() == types::TY_Object))
538 return ToolChain::getInputFilename(Input);
539
540 // Replace extension for object files with cubin because nvlink relies on
541 // these particular file names.
542 SmallString<256> Filename(ToolChain::getInputFilename(Input));
543 llvm::sys::path::replace_extension(Filename, "cubin");
544 return Filename.str();
545}
546
David L. Jonesf561aba2017-03-08 01:02:16 +0000547void CudaToolChain::addClangTargetOptions(
548 const llvm::opt::ArgList &DriverArgs,
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000549 llvm::opt::ArgStringList &CC1Args,
550 Action::OffloadKind DeviceOffloadingKind) const {
551 HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
David L. Jonesf561aba2017-03-08 01:02:16 +0000552
553 StringRef GpuArch = DriverArgs.getLastArgValue(options::OPT_march_EQ);
554 assert(!GpuArch.empty() && "Must have an explicit GPU arch.");
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000555 assert((DeviceOffloadingKind == Action::OFK_OpenMP ||
556 DeviceOffloadingKind == Action::OFK_Cuda) &&
557 "Only OpenMP or CUDA offloading kinds are supported for NVIDIA GPUs.");
558
559 if (DeviceOffloadingKind == Action::OFK_Cuda) {
560 CC1Args.push_back("-fcuda-is-device");
561
562 if (DriverArgs.hasFlag(options::OPT_fcuda_flush_denormals_to_zero,
563 options::OPT_fno_cuda_flush_denormals_to_zero, false))
564 CC1Args.push_back("-fcuda-flush-denormals-to-zero");
565
566 if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals,
567 options::OPT_fno_cuda_approx_transcendentals, false))
568 CC1Args.push_back("-fcuda-approx-transcendentals");
Jonas Hahnfeld5379c6d2018-02-12 10:46:45 +0000569
570 if (DriverArgs.hasFlag(options::OPT_fcuda_rdc, options::OPT_fno_cuda_rdc,
571 false))
572 CC1Args.push_back("-fcuda-rdc");
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000573 }
574
Gheorghe-Teodor Bercea20789a52017-09-25 21:56:32 +0000575 if (DriverArgs.hasArg(options::OPT_nocudalib))
576 return;
577
David L. Jonesf561aba2017-03-08 01:02:16 +0000578 std::string LibDeviceFile = CudaInstallation.getLibDeviceFile(GpuArch);
579
580 if (LibDeviceFile.empty()) {
Gheorghe-Teodor Bercea5a3608c2017-09-26 15:36:20 +0000581 if (DeviceOffloadingKind == Action::OFK_OpenMP &&
582 DriverArgs.hasArg(options::OPT_S))
583 return;
584
David L. Jonesf561aba2017-03-08 01:02:16 +0000585 getDriver().Diag(diag::err_drv_no_cuda_libdevice) << GpuArch;
586 return;
587 }
588
589 CC1Args.push_back("-mlink-cuda-bitcode");
590 CC1Args.push_back(DriverArgs.MakeArgString(LibDeviceFile));
591
Artem Belevich4654dc82017-09-20 21:23:07 +0000592 if (CudaInstallation.version() >= CudaVersion::CUDA_90) {
593 // CUDA-9 uses new instructions that are only available in PTX6.0
594 CC1Args.push_back("-target-feature");
595 CC1Args.push_back("+ptx60");
596 } else {
597 // Libdevice in CUDA-7.0 requires PTX version that's more recent
598 // than LLVM defaults to. Use PTX4.2 which is the PTX version that
599 // came with CUDA-7.0.
600 CC1Args.push_back("-target-feature");
601 CC1Args.push_back("+ptx42");
602 }
Gheorghe-Teodor Bercea0d5aa842018-03-13 23:19:52 +0000603
604 if (DeviceOffloadingKind == Action::OFK_OpenMP) {
605 SmallVector<StringRef, 8> LibraryPaths;
606 // Add path to lib and/or lib64 folders.
607 SmallString<256> DefaultLibPath =
608 llvm::sys::path::parent_path(getDriver().Dir);
609 llvm::sys::path::append(DefaultLibPath,
610 Twine("lib") + CLANG_LIBDIR_SUFFIX);
611 LibraryPaths.emplace_back(DefaultLibPath.c_str());
612
613 // Add user defined library paths from LIBRARY_PATH.
614 llvm::Optional<std::string> LibPath =
615 llvm::sys::Process::GetEnv("LIBRARY_PATH");
616 if (LibPath) {
617 SmallVector<StringRef, 8> Frags;
618 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'};
619 llvm::SplitString(*LibPath, Frags, EnvPathSeparatorStr);
620 for (StringRef Path : Frags)
621 LibraryPaths.emplace_back(Path.trim());
622 }
623
624 std::string LibOmpTargetName =
625 "libomptarget-nvptx-" + GpuArch.str() + ".bc";
626 bool FoundBCLibrary = false;
627 for (StringRef LibraryPath : LibraryPaths) {
628 SmallString<128> LibOmpTargetFile(LibraryPath);
629 llvm::sys::path::append(LibOmpTargetFile, LibOmpTargetName);
630 if (llvm::sys::fs::exists(LibOmpTargetFile)) {
631 CC1Args.push_back("-mlink-cuda-bitcode");
632 CC1Args.push_back(DriverArgs.MakeArgString(LibOmpTargetFile));
633 FoundBCLibrary = true;
634 break;
635 }
636 }
637 if (!FoundBCLibrary)
638 getDriver().Diag(diag::warn_drv_omp_offload_target_missingbcruntime)
639 << LibOmpTargetName;
640 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000641}
642
643void CudaToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs,
644 ArgStringList &CC1Args) const {
645 // Check our CUDA version if we're going to include the CUDA headers.
646 if (!DriverArgs.hasArg(options::OPT_nocudainc) &&
647 !DriverArgs.hasArg(options::OPT_no_cuda_version_check)) {
648 StringRef Arch = DriverArgs.getLastArgValue(options::OPT_march_EQ);
649 assert(!Arch.empty() && "Must have an explicit GPU arch.");
650 CudaInstallation.CheckCudaVersionSupportsArch(StringToCudaArch(Arch));
651 }
652 CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
653}
654
655llvm::opt::DerivedArgList *
656CudaToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
657 StringRef BoundArch,
658 Action::OffloadKind DeviceOffloadKind) const {
659 DerivedArgList *DAL =
660 HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind);
661 if (!DAL)
662 DAL = new DerivedArgList(Args.getBaseArgs());
663
664 const OptTable &Opts = getDriver().getOpts();
665
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000666 // For OpenMP device offloading, append derived arguments. Make sure
667 // flags are not duplicated.
Gheorghe-Teodor Bercea47e0cf32017-08-07 15:39:11 +0000668 // Also append the compute capability.
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000669 if (DeviceOffloadKind == Action::OFK_OpenMP) {
Jonas Hahnfeld30b44182017-10-17 13:37:36 +0000670 for (Arg *A : Args) {
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000671 bool IsDuplicate = false;
Jonas Hahnfeld30b44182017-10-17 13:37:36 +0000672 for (Arg *DALArg : *DAL) {
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000673 if (A == DALArg) {
674 IsDuplicate = true;
675 break;
676 }
677 }
678 if (!IsDuplicate)
679 DAL->append(A);
680 }
Gheorghe-Teodor Bercea47e0cf32017-08-07 15:39:11 +0000681
682 StringRef Arch = DAL->getLastArgValue(options::OPT_march_EQ);
Jonas Hahnfeld30b44182017-10-17 13:37:36 +0000683 if (Arch.empty())
684 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ),
685 CLANG_OPENMP_NVPTX_DEFAULT_ARCH);
Gheorghe-Teodor Bercea47e0cf32017-08-07 15:39:11 +0000686
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000687 return DAL;
688 }
689
David L. Jonesf561aba2017-03-08 01:02:16 +0000690 for (Arg *A : Args) {
691 if (A->getOption().matches(options::OPT_Xarch__)) {
692 // Skip this argument unless the architecture matches BoundArch
693 if (BoundArch.empty() || A->getValue(0) != BoundArch)
694 continue;
695
696 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
697 unsigned Prev = Index;
698 std::unique_ptr<Arg> XarchArg(Opts.ParseOneArg(Args, Index));
699
700 // If the argument parsing failed or more than one argument was
701 // consumed, the -Xarch_ argument's parameter tried to consume
702 // extra arguments. Emit an error and ignore.
703 //
704 // We also want to disallow any options which would alter the
705 // driver behavior; that isn't going to work in our model. We
706 // use isDriverOption() as an approximation, although things
707 // like -O4 are going to slip through.
708 if (!XarchArg || Index > Prev + 1) {
709 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
710 << A->getAsString(Args);
711 continue;
712 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
713 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
714 << A->getAsString(Args);
715 continue;
716 }
717 XarchArg->setBaseArg(A);
718 A = XarchArg.release();
719 DAL->AddSynthesizedArg(A);
720 }
721 DAL->append(A);
722 }
723
724 if (!BoundArch.empty()) {
725 DAL->eraseArg(options::OPT_march_EQ);
726 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ), BoundArch);
727 }
728 return DAL;
729}
730
731Tool *CudaToolChain::buildAssembler() const {
732 return new tools::NVPTX::Assembler(*this);
733}
734
735Tool *CudaToolChain::buildLinker() const {
Gheorghe-Teodor Bercea2c926932017-08-08 14:33:05 +0000736 if (OK == Action::OFK_OpenMP)
737 return new tools::NVPTX::OpenMPLinker(*this);
David L. Jonesf561aba2017-03-08 01:02:16 +0000738 return new tools::NVPTX::Linker(*this);
739}
740
741void CudaToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
742 HostTC.addClangWarningOptions(CC1Args);
743}
744
745ToolChain::CXXStdlibType
746CudaToolChain::GetCXXStdlibType(const ArgList &Args) const {
747 return HostTC.GetCXXStdlibType(Args);
748}
749
750void CudaToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
751 ArgStringList &CC1Args) const {
752 HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
753}
754
755void CudaToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &Args,
756 ArgStringList &CC1Args) const {
757 HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args);
758}
759
760void CudaToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
761 ArgStringList &CC1Args) const {
762 HostTC.AddIAMCUIncludeArgs(Args, CC1Args);
763}
764
765SanitizerMask CudaToolChain::getSupportedSanitizers() const {
766 // The CudaToolChain only supports sanitizers in the sense that it allows
767 // sanitizer arguments on the command line if they are supported by the host
768 // toolchain. The CudaToolChain will actually ignore any command line
769 // arguments for any of these "supported" sanitizers. That means that no
770 // sanitization of device code is actually supported at this time.
771 //
772 // This behavior is necessary because the host and device toolchains
773 // invocations often share the command line, so the device toolchain must
774 // tolerate flags meant only for the host toolchain.
775 return HostTC.getSupportedSanitizers();
776}
777
778VersionTuple CudaToolChain::computeMSVCVersion(const Driver *D,
779 const ArgList &Args) const {
780 return HostTC.computeMSVCVersion(D, Args);
781}