blob: 8c704a3078adcb2489b7940118f72537a3e74cc6 [file] [log] [blame]
David L. Jonesf561aba2017-03-08 01:02:16 +00001//===--- Cuda.cpp - Cuda Tool and ToolChain Implementations -----*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
David L. Jonesf561aba2017-03-08 01:02:16 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "Cuda.h"
Gheorghe-Teodor Bercea2c926932017-08-08 14:33:05 +000010#include "CommonArgs.h"
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +000011#include "InputInfo.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000012#include "clang/Basic/Cuda.h"
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +000013#include "clang/Config/config.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000014#include "clang/Driver/Compilation.h"
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +000015#include "clang/Driver/Distro.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000016#include "clang/Driver/Driver.h"
17#include "clang/Driver/DriverDiagnostic.h"
18#include "clang/Driver/Options.h"
19#include "llvm/Option/ArgList.h"
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +000020#include "llvm/Support/FileSystem.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000021#include "llvm/Support/Path.h"
Gheorghe-Teodor Bercea148046c2018-03-13 19:39:19 +000022#include "llvm/Support/Process.h"
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +000023#include "llvm/Support/Program.h"
Jonas Devliegherefc514902018-10-10 13:27:25 +000024#include "llvm/Support/VirtualFileSystem.h"
David L. Jonesf561aba2017-03-08 01:02:16 +000025#include <system_error>
26
27using namespace clang::driver;
28using namespace clang::driver::toolchains;
29using namespace clang::driver::tools;
30using namespace clang;
31using namespace llvm::opt;
32
33// Parses the contents of version.txt in an CUDA installation. It should
34// contain one line of the from e.g. "CUDA Version 7.5.2".
35static CudaVersion ParseCudaVersionFile(llvm::StringRef V) {
36 if (!V.startswith("CUDA Version "))
37 return CudaVersion::UNKNOWN;
38 V = V.substr(strlen("CUDA Version "));
39 int Major = -1, Minor = -1;
40 auto First = V.split('.');
41 auto Second = First.second.split('.');
42 if (First.first.getAsInteger(10, Major) ||
43 Second.first.getAsInteger(10, Minor))
44 return CudaVersion::UNKNOWN;
45
46 if (Major == 7 && Minor == 0) {
47 // This doesn't appear to ever happen -- version.txt doesn't exist in the
48 // CUDA 7 installs I've seen. But no harm in checking.
49 return CudaVersion::CUDA_70;
50 }
51 if (Major == 7 && Minor == 5)
52 return CudaVersion::CUDA_75;
53 if (Major == 8 && Minor == 0)
54 return CudaVersion::CUDA_80;
Artem Belevich8af4e232017-09-07 18:14:32 +000055 if (Major == 9 && Minor == 0)
56 return CudaVersion::CUDA_90;
Artem Belevichfbc56a92018-01-30 00:00:12 +000057 if (Major == 9 && Minor == 1)
58 return CudaVersion::CUDA_91;
Artem Belevich3cce3072018-04-24 18:23:19 +000059 if (Major == 9 && Minor == 2)
60 return CudaVersion::CUDA_92;
Artem Belevich44ecb0e2018-09-24 23:10:44 +000061 if (Major == 10 && Minor == 0)
62 return CudaVersion::CUDA_100;
Artem Belevich40717632019-02-05 22:38:58 +000063 if (Major == 10 && Minor == 1)
64 return CudaVersion::CUDA_101;
David L. Jonesf561aba2017-03-08 01:02:16 +000065 return CudaVersion::UNKNOWN;
66}
67
68CudaInstallationDetector::CudaInstallationDetector(
69 const Driver &D, const llvm::Triple &HostTriple,
70 const llvm::opt::ArgList &Args)
71 : D(D) {
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +000072 struct Candidate {
73 std::string Path;
74 bool StrictChecking;
75
76 Candidate(std::string Path, bool StrictChecking = false)
77 : Path(Path), StrictChecking(StrictChecking) {}
78 };
79 SmallVector<Candidate, 4> Candidates;
David L. Jonesf561aba2017-03-08 01:02:16 +000080
81 // In decreasing order so we prefer newer versions to older versions.
82 std::initializer_list<const char *> Versions = {"8.0", "7.5", "7.0"};
83
84 if (Args.hasArg(clang::driver::options::OPT_cuda_path_EQ)) {
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +000085 Candidates.emplace_back(
86 Args.getLastArgValue(clang::driver::options::OPT_cuda_path_EQ).str());
David L. Jonesf561aba2017-03-08 01:02:16 +000087 } else if (HostTriple.isOSWindows()) {
88 for (const char *Ver : Versions)
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +000089 Candidates.emplace_back(
David L. Jonesf561aba2017-03-08 01:02:16 +000090 D.SysRoot + "/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v" +
91 Ver);
92 } else {
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +000093 if (!Args.hasArg(clang::driver::options::OPT_cuda_path_ignore_env)) {
94 // Try to find ptxas binary. If the executable is located in a directory
95 // called 'bin/', its parent directory might be a good guess for a valid
96 // CUDA installation.
97 // However, some distributions might installs 'ptxas' to /usr/bin. In that
98 // case the candidate would be '/usr' which passes the following checks
99 // because '/usr/include' exists as well. To avoid this case, we always
100 // check for the directory potentially containing files for libdevice,
101 // even if the user passes -nocudalib.
102 if (llvm::ErrorOr<std::string> ptxas =
103 llvm::sys::findProgramByName("ptxas")) {
104 SmallString<256> ptxasAbsolutePath;
105 llvm::sys::fs::real_path(*ptxas, ptxasAbsolutePath);
106
107 StringRef ptxasDir = llvm::sys::path::parent_path(ptxasAbsolutePath);
108 if (llvm::sys::path::filename(ptxasDir) == "bin")
109 Candidates.emplace_back(llvm::sys::path::parent_path(ptxasDir),
110 /*StrictChecking=*/true);
111 }
112 }
113
114 Candidates.emplace_back(D.SysRoot + "/usr/local/cuda");
David L. Jonesf561aba2017-03-08 01:02:16 +0000115 for (const char *Ver : Versions)
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +0000116 Candidates.emplace_back(D.SysRoot + "/usr/local/cuda-" + Ver);
Sylvestre Ledru0cfcdc32017-11-29 15:03:28 +0000117
Joel E. Denny6dd34dc2018-12-06 17:46:17 +0000118 if (Distro(D.getVFS()).IsDebian() || Distro(D.getVFS()).IsUbuntu())
Sylvestre Ledru0cfcdc32017-11-29 15:03:28 +0000119 // Special case for Debian to have nvidia-cuda-toolkit work
120 // out of the box. More info on http://bugs.debian.org/882505
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +0000121 Candidates.emplace_back(D.SysRoot + "/usr/lib/cuda");
David L. Jonesf561aba2017-03-08 01:02:16 +0000122 }
123
Yaxun Liu99d0d3a2019-10-03 18:59:56 +0000124 bool NoCudaLib = Args.hasArg(options::OPT_nogpulib);
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +0000125
126 for (const auto &Candidate : Candidates) {
127 InstallPath = Candidate.Path;
128 if (InstallPath.empty() || !D.getVFS().exists(InstallPath))
David L. Jonesf561aba2017-03-08 01:02:16 +0000129 continue;
130
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +0000131 BinPath = InstallPath + "/bin";
David L. Jonesf561aba2017-03-08 01:02:16 +0000132 IncludePath = InstallPath + "/include";
133 LibDevicePath = InstallPath + "/nvvm/libdevice";
134
135 auto &FS = D.getVFS();
Jonas Hahnfelde2c342f2017-10-16 13:31:30 +0000136 if (!(FS.exists(IncludePath) && FS.exists(BinPath)))
David L. Jonesf561aba2017-03-08 01:02:16 +0000137 continue;
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +0000138 bool CheckLibDevice = (!NoCudaLib || Candidate.StrictChecking);
139 if (CheckLibDevice && !FS.exists(LibDevicePath))
140 continue;
David L. Jonesf561aba2017-03-08 01:02:16 +0000141
142 // On Linux, we have both lib and lib64 directories, and we need to choose
143 // based on our triple. On MacOS, we have only a lib directory.
144 //
145 // It's sufficient for our purposes to be flexible: If both lib and lib64
146 // exist, we choose whichever one matches our triple. Otherwise, if only
147 // lib exists, we use it.
148 if (HostTriple.isArch64Bit() && FS.exists(InstallPath + "/lib64"))
149 LibPath = InstallPath + "/lib64";
150 else if (FS.exists(InstallPath + "/lib"))
151 LibPath = InstallPath + "/lib";
152 else
153 continue;
154
155 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> VersionFile =
156 FS.getBufferForFile(InstallPath + "/version.txt");
157 if (!VersionFile) {
158 // CUDA 7.0 doesn't have a version.txt, so guess that's our version if
159 // version.txt isn't present.
160 Version = CudaVersion::CUDA_70;
161 } else {
162 Version = ParseCudaVersionFile((*VersionFile)->getBuffer());
163 }
164
Artem Belevichfbc56a92018-01-30 00:00:12 +0000165 if (Version >= CudaVersion::CUDA_90) {
166 // CUDA-9+ uses single libdevice file for all GPU variants.
Artem Belevich8af4e232017-09-07 18:14:32 +0000167 std::string FilePath = LibDevicePath + "/libdevice.10.bc";
168 if (FS.exists(FilePath)) {
Artem Belevichfbc56a92018-01-30 00:00:12 +0000169 for (const char *GpuArchName :
Artem Belevich578653a2018-05-23 16:45:23 +0000170 {"sm_30", "sm_32", "sm_35", "sm_37", "sm_50", "sm_52", "sm_53",
Artem Belevich44ecb0e2018-09-24 23:10:44 +0000171 "sm_60", "sm_61", "sm_62", "sm_70", "sm_72", "sm_75"}) {
Artem Belevichfbc56a92018-01-30 00:00:12 +0000172 const CudaArch GpuArch = StringToCudaArch(GpuArchName);
173 if (Version >= MinVersionForCudaArch(GpuArch) &&
174 Version <= MaxVersionForCudaArch(GpuArch))
175 LibDeviceMap[GpuArchName] = FilePath;
176 }
Artem Belevich8af4e232017-09-07 18:14:32 +0000177 }
178 } else {
179 std::error_code EC;
180 for (llvm::sys::fs::directory_iterator LI(LibDevicePath, EC), LE;
181 !EC && LI != LE; LI = LI.increment(EC)) {
182 StringRef FilePath = LI->path();
183 StringRef FileName = llvm::sys::path::filename(FilePath);
184 // Process all bitcode filenames that look like
185 // libdevice.compute_XX.YY.bc
186 const StringRef LibDeviceName = "libdevice.";
187 if (!(FileName.startswith(LibDeviceName) && FileName.endswith(".bc")))
188 continue;
189 StringRef GpuArch = FileName.slice(
190 LibDeviceName.size(), FileName.find('.', LibDeviceName.size()));
191 LibDeviceMap[GpuArch] = FilePath.str();
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +0000192 // Insert map entries for specific devices with this compute
Artem Belevich8af4e232017-09-07 18:14:32 +0000193 // capability. NVCC's choice of the libdevice library version is
194 // rather peculiar and depends on the CUDA version.
195 if (GpuArch == "compute_20") {
196 LibDeviceMap["sm_20"] = FilePath;
197 LibDeviceMap["sm_21"] = FilePath;
198 LibDeviceMap["sm_32"] = FilePath;
199 } else if (GpuArch == "compute_30") {
200 LibDeviceMap["sm_30"] = FilePath;
201 if (Version < CudaVersion::CUDA_80) {
202 LibDeviceMap["sm_50"] = FilePath;
203 LibDeviceMap["sm_52"] = FilePath;
204 LibDeviceMap["sm_53"] = FilePath;
205 }
206 LibDeviceMap["sm_60"] = FilePath;
207 LibDeviceMap["sm_61"] = FilePath;
208 LibDeviceMap["sm_62"] = FilePath;
209 } else if (GpuArch == "compute_35") {
210 LibDeviceMap["sm_35"] = FilePath;
211 LibDeviceMap["sm_37"] = FilePath;
212 } else if (GpuArch == "compute_50") {
213 if (Version >= CudaVersion::CUDA_80) {
214 LibDeviceMap["sm_50"] = FilePath;
215 LibDeviceMap["sm_52"] = FilePath;
216 LibDeviceMap["sm_53"] = FilePath;
217 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000218 }
219 }
220 }
221
Jonas Hahnfelde2c342f2017-10-16 13:31:30 +0000222 // Check that we have found at least one libdevice that we can link in if
223 // -nocudalib hasn't been specified.
Jonas Hahnfeld7f9c5182018-01-31 08:26:51 +0000224 if (LibDeviceMap.empty() && !NoCudaLib)
Gheorghe-Teodor Bercea9c525742017-08-11 15:46:22 +0000225 continue;
226
David L. Jonesf561aba2017-03-08 01:02:16 +0000227 IsValid = true;
228 break;
229 }
230}
231
232void CudaInstallationDetector::AddCudaIncludeArgs(
233 const ArgList &DriverArgs, ArgStringList &CC1Args) const {
234 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
235 // Add cuda_wrappers/* to our system include path. This lets us wrap
236 // standard library headers.
237 SmallString<128> P(D.ResourceDir);
238 llvm::sys::path::append(P, "include");
239 llvm::sys::path::append(P, "cuda_wrappers");
240 CC1Args.push_back("-internal-isystem");
241 CC1Args.push_back(DriverArgs.MakeArgString(P));
242 }
243
244 if (DriverArgs.hasArg(options::OPT_nocudainc))
245 return;
246
247 if (!isValid()) {
248 D.Diag(diag::err_drv_no_cuda_installation);
249 return;
250 }
251
252 CC1Args.push_back("-internal-isystem");
253 CC1Args.push_back(DriverArgs.MakeArgString(getIncludePath()));
254 CC1Args.push_back("-include");
255 CC1Args.push_back("__clang_cuda_runtime_wrapper.h");
256}
257
258void CudaInstallationDetector::CheckCudaVersionSupportsArch(
259 CudaArch Arch) const {
260 if (Arch == CudaArch::UNKNOWN || Version == CudaVersion::UNKNOWN ||
Justin Lebar066494d2017-10-25 21:32:06 +0000261 ArchsWithBadVersion.count(Arch) > 0)
David L. Jonesf561aba2017-03-08 01:02:16 +0000262 return;
263
Justin Lebar066494d2017-10-25 21:32:06 +0000264 auto MinVersion = MinVersionForCudaArch(Arch);
265 auto MaxVersion = MaxVersionForCudaArch(Arch);
266 if (Version < MinVersion || Version > MaxVersion) {
267 ArchsWithBadVersion.insert(Arch);
268 D.Diag(diag::err_drv_cuda_version_unsupported)
269 << CudaArchToString(Arch) << CudaVersionToString(MinVersion)
270 << CudaVersionToString(MaxVersion) << InstallPath
271 << CudaVersionToString(Version);
David L. Jonesf561aba2017-03-08 01:02:16 +0000272 }
273}
274
275void CudaInstallationDetector::print(raw_ostream &OS) const {
276 if (isValid())
277 OS << "Found CUDA installation: " << InstallPath << ", version "
278 << CudaVersionToString(Version) << "\n";
279}
280
Alexey Bataeve36c67b2018-04-18 16:31:09 +0000281namespace {
Alexey Bataevc92fc3c2018-12-12 14:52:27 +0000282/// Debug info level for the NVPTX devices. We may need to emit different debug
283/// info level for the host and for the device itselfi. This type controls
284/// emission of the debug info for the devices. It either prohibits disable info
285/// emission completely, or emits debug directives only, or emits same debug
286/// info as for the host.
287enum DeviceDebugInfoLevel {
288 DisableDebugInfo, /// Do not emit debug info for the devices.
289 DebugDirectivesOnly, /// Emit only debug directives.
290 EmitSameDebugInfoAsHost, /// Use the same debug info level just like for the
291 /// host.
Alexey Bataeve36c67b2018-04-18 16:31:09 +0000292};
293} // anonymous namespace
294
Alexey Bataevc92fc3c2018-12-12 14:52:27 +0000295/// Define debug info level for the NVPTX devices. If the debug info for both
296/// the host and device are disabled (-g0/-ggdb0 or no debug options at all). If
297/// only debug directives are requested for the both host and device
298/// (-gline-directvies-only), or the debug info only for the device is disabled
299/// (optimization is on and --cuda-noopt-device-debug was not specified), the
300/// debug directves only must be emitted for the device. Otherwise, use the same
301/// debug info level just like for the host (with the limitations of only
302/// supported DWARF2 standard).
303static DeviceDebugInfoLevel mustEmitDebugInfo(const ArgList &Args) {
304 const Arg *A = Args.getLastArg(options::OPT_O_Group);
305 bool IsDebugEnabled = !A || A->getOption().matches(options::OPT_O0) ||
306 Args.hasFlag(options::OPT_cuda_noopt_device_debug,
307 options::OPT_no_cuda_noopt_device_debug,
308 /*Default=*/false);
309 if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) {
310 const Option &Opt = A->getOption();
311 if (Opt.matches(options::OPT_gN_Group)) {
312 if (Opt.matches(options::OPT_g0) || Opt.matches(options::OPT_ggdb0))
313 return DisableDebugInfo;
314 if (Opt.matches(options::OPT_gline_directives_only))
315 return DebugDirectivesOnly;
Alexey Bataeve36c67b2018-04-18 16:31:09 +0000316 }
Alexey Bataevc92fc3c2018-12-12 14:52:27 +0000317 return IsDebugEnabled ? EmitSameDebugInfoAsHost : DebugDirectivesOnly;
Alexey Bataeve36c67b2018-04-18 16:31:09 +0000318 }
Alexey Bataevc92fc3c2018-12-12 14:52:27 +0000319 return DisableDebugInfo;
Alexey Bataeve36c67b2018-04-18 16:31:09 +0000320}
321
David L. Jonesf561aba2017-03-08 01:02:16 +0000322void NVPTX::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
323 const InputInfo &Output,
324 const InputInfoList &Inputs,
325 const ArgList &Args,
326 const char *LinkingOutput) const {
327 const auto &TC =
328 static_cast<const toolchains::CudaToolChain &>(getToolChain());
329 assert(TC.getTriple().isNVPTX() && "Wrong platform");
330
Gheorghe-Teodor Bercea47e0cf32017-08-07 15:39:11 +0000331 StringRef GPUArchName;
332 // If this is an OpenMP action we need to extract the device architecture
333 // from the -march=arch option. This option may come from -Xopenmp-target
334 // flag or the default value.
335 if (JA.isDeviceOffloading(Action::OFK_OpenMP)) {
336 GPUArchName = Args.getLastArgValue(options::OPT_march_EQ);
337 assert(!GPUArchName.empty() && "Must have an architecture passed in.");
338 } else
339 GPUArchName = JA.getOffloadingArch();
340
David L. Jonesf561aba2017-03-08 01:02:16 +0000341 // Obtain architecture from the action.
Gheorghe-Teodor Bercea47e0cf32017-08-07 15:39:11 +0000342 CudaArch gpu_arch = StringToCudaArch(GPUArchName);
David L. Jonesf561aba2017-03-08 01:02:16 +0000343 assert(gpu_arch != CudaArch::UNKNOWN &&
344 "Device action expected to have an architecture.");
345
346 // Check that our installation's ptxas supports gpu_arch.
347 if (!Args.hasArg(options::OPT_no_cuda_version_check)) {
348 TC.CudaInstallation.CheckCudaVersionSupportsArch(gpu_arch);
349 }
350
351 ArgStringList CmdArgs;
352 CmdArgs.push_back(TC.getTriple().isArch64Bit() ? "-m64" : "-m32");
Alexey Bataevc92fc3c2018-12-12 14:52:27 +0000353 DeviceDebugInfoLevel DIKind = mustEmitDebugInfo(Args);
354 if (DIKind == EmitSameDebugInfoAsHost) {
David L. Jonesf561aba2017-03-08 01:02:16 +0000355 // ptxas does not accept -g option if optimization is enabled, so
356 // we ignore the compiler's -O* options if we want debug info.
357 CmdArgs.push_back("-g");
358 CmdArgs.push_back("--dont-merge-basicblocks");
359 CmdArgs.push_back("--return-at-end");
360 } else if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
361 // Map the -O we received to -O{0,1,2,3}.
362 //
363 // TODO: Perhaps we should map host -O2 to ptxas -O3. -O3 is ptxas's
364 // default, so it may correspond more closely to the spirit of clang -O2.
365
366 // -O3 seems like the least-bad option when -Osomething is specified to
367 // clang but it isn't handled below.
368 StringRef OOpt = "3";
369 if (A->getOption().matches(options::OPT_O4) ||
370 A->getOption().matches(options::OPT_Ofast))
371 OOpt = "3";
372 else if (A->getOption().matches(options::OPT_O0))
373 OOpt = "0";
374 else if (A->getOption().matches(options::OPT_O)) {
375 // -Os, -Oz, and -O(anything else) map to -O2, for lack of better options.
376 OOpt = llvm::StringSwitch<const char *>(A->getValue())
377 .Case("1", "1")
378 .Case("2", "2")
379 .Case("3", "3")
380 .Case("s", "2")
381 .Case("z", "2")
382 .Default("2");
383 }
384 CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
385 } else {
386 // If no -O was passed, pass -O0 to ptxas -- no opt flag should correspond
387 // to no optimizations, but ptxas's default is -O3.
388 CmdArgs.push_back("-O0");
389 }
Alexey Bataevc92fc3c2018-12-12 14:52:27 +0000390 if (DIKind == DebugDirectivesOnly)
Alexey Bataeve36c67b2018-04-18 16:31:09 +0000391 CmdArgs.push_back("-lineinfo");
David L. Jonesf561aba2017-03-08 01:02:16 +0000392
Gheorghe-Teodor Bercea53431bc2017-08-07 20:19:23 +0000393 // Pass -v to ptxas if it was passed to the driver.
394 if (Args.hasArg(options::OPT_v))
395 CmdArgs.push_back("-v");
396
David L. Jonesf561aba2017-03-08 01:02:16 +0000397 CmdArgs.push_back("--gpu-name");
398 CmdArgs.push_back(Args.MakeArgString(CudaArchToString(gpu_arch)));
399 CmdArgs.push_back("--output-file");
Jonas Hahnfeld7c78cc52017-11-21 14:44:45 +0000400 CmdArgs.push_back(Args.MakeArgString(TC.getInputFilename(Output)));
David L. Jonesf561aba2017-03-08 01:02:16 +0000401 for (const auto& II : Inputs)
402 CmdArgs.push_back(Args.MakeArgString(II.getFilename()));
403
404 for (const auto& A : Args.getAllArgValues(options::OPT_Xcuda_ptxas))
405 CmdArgs.push_back(Args.MakeArgString(A));
406
Jonas Hahnfeld5379c6d2018-02-12 10:46:45 +0000407 bool Relocatable = false;
408 if (JA.isOffloading(Action::OFK_OpenMP))
409 // In OpenMP we need to generate relocatable code.
410 Relocatable = Args.hasFlag(options::OPT_fopenmp_relocatable_target,
411 options::OPT_fnoopenmp_relocatable_target,
412 /*Default=*/true);
413 else if (JA.isOffloading(Action::OFK_Cuda))
Yaxun Liu97670892018-10-02 17:48:54 +0000414 Relocatable = Args.hasFlag(options::OPT_fgpu_rdc,
415 options::OPT_fno_gpu_rdc, /*Default=*/false);
Jonas Hahnfeld5379c6d2018-02-12 10:46:45 +0000416
417 if (Relocatable)
Gheorghe-Teodor Berceab9d11722017-08-09 14:59:35 +0000418 CmdArgs.push_back("-c");
419
David L. Jonesf561aba2017-03-08 01:02:16 +0000420 const char *Exec;
421 if (Arg *A = Args.getLastArg(options::OPT_ptxas_path_EQ))
422 Exec = A->getValue();
423 else
424 Exec = Args.MakeArgString(TC.GetProgramPath("ptxas"));
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +0000425 C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
David L. Jonesf561aba2017-03-08 01:02:16 +0000426}
427
Artem Belevichdde3dc22018-04-10 18:38:22 +0000428static bool shouldIncludePTX(const ArgList &Args, const char *gpu_arch) {
429 bool includePTX = true;
430 for (Arg *A : Args) {
431 if (!(A->getOption().matches(options::OPT_cuda_include_ptx_EQ) ||
432 A->getOption().matches(options::OPT_no_cuda_include_ptx_EQ)))
433 continue;
434 A->claim();
435 const StringRef ArchStr = A->getValue();
436 if (ArchStr == "all" || ArchStr == gpu_arch) {
437 includePTX = A->getOption().matches(options::OPT_cuda_include_ptx_EQ);
438 continue;
439 }
440 }
441 return includePTX;
442}
443
David L. Jonesf561aba2017-03-08 01:02:16 +0000444// All inputs to this linker must be from CudaDeviceActions, as we need to look
445// at the Inputs' Actions in order to figure out which GPU architecture they
446// correspond to.
447void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
448 const InputInfo &Output,
449 const InputInfoList &Inputs,
450 const ArgList &Args,
451 const char *LinkingOutput) const {
452 const auto &TC =
453 static_cast<const toolchains::CudaToolChain &>(getToolChain());
454 assert(TC.getTriple().isNVPTX() && "Wrong platform");
455
456 ArgStringList CmdArgs;
Artem Belevich4cbb2352019-05-02 22:37:19 +0000457 if (TC.CudaInstallation.version() <= CudaVersion::CUDA_100)
458 CmdArgs.push_back("--cuda");
David L. Jonesf561aba2017-03-08 01:02:16 +0000459 CmdArgs.push_back(TC.getTriple().isArch64Bit() ? "-64" : "-32");
460 CmdArgs.push_back(Args.MakeArgString("--create"));
461 CmdArgs.push_back(Args.MakeArgString(Output.getFilename()));
Alexey Bataevc92fc3c2018-12-12 14:52:27 +0000462 if (mustEmitDebugInfo(Args) == EmitSameDebugInfoAsHost)
Alexey Bataeve36c67b2018-04-18 16:31:09 +0000463 CmdArgs.push_back("-g");
David L. Jonesf561aba2017-03-08 01:02:16 +0000464
465 for (const auto& II : Inputs) {
466 auto *A = II.getAction();
467 assert(A->getInputs().size() == 1 &&
468 "Device offload action is expected to have a single input");
469 const char *gpu_arch_str = A->getOffloadingArch();
470 assert(gpu_arch_str &&
471 "Device action expected to have associated a GPU architecture!");
472 CudaArch gpu_arch = StringToCudaArch(gpu_arch_str);
473
Artem Belevichdde3dc22018-04-10 18:38:22 +0000474 if (II.getType() == types::TY_PP_Asm &&
475 !shouldIncludePTX(Args, gpu_arch_str))
476 continue;
David L. Jonesf561aba2017-03-08 01:02:16 +0000477 // We need to pass an Arch of the form "sm_XX" for cubin files and
478 // "compute_XX" for ptx.
479 const char *Arch =
480 (II.getType() == types::TY_PP_Asm)
481 ? CudaVirtualArchToString(VirtualArchForCudaArch(gpu_arch))
482 : gpu_arch_str;
483 CmdArgs.push_back(Args.MakeArgString(llvm::Twine("--image=profile=") +
484 Arch + ",file=" + II.getFilename()));
485 }
486
487 for (const auto& A : Args.getAllArgValues(options::OPT_Xcuda_fatbinary))
488 CmdArgs.push_back(Args.MakeArgString(A));
489
490 const char *Exec = Args.MakeArgString(TC.GetProgramPath("fatbinary"));
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +0000491 C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
David L. Jonesf561aba2017-03-08 01:02:16 +0000492}
493
Gheorghe-Teodor Bercea2c926932017-08-08 14:33:05 +0000494void NVPTX::OpenMPLinker::ConstructJob(Compilation &C, const JobAction &JA,
495 const InputInfo &Output,
496 const InputInfoList &Inputs,
497 const ArgList &Args,
498 const char *LinkingOutput) const {
499 const auto &TC =
500 static_cast<const toolchains::CudaToolChain &>(getToolChain());
501 assert(TC.getTriple().isNVPTX() && "Wrong platform");
502
503 ArgStringList CmdArgs;
504
505 // OpenMP uses nvlink to link cubin files. The result will be embedded in the
506 // host binary by the host linker.
507 assert(!JA.isHostOffloading(Action::OFK_OpenMP) &&
508 "CUDA toolchain not expected for an OpenMP host device.");
509
510 if (Output.isFilename()) {
511 CmdArgs.push_back("-o");
512 CmdArgs.push_back(Output.getFilename());
513 } else
514 assert(Output.isNothing() && "Invalid output.");
Alexey Bataevc92fc3c2018-12-12 14:52:27 +0000515 if (mustEmitDebugInfo(Args) == EmitSameDebugInfoAsHost)
Gheorghe-Teodor Bercea2c926932017-08-08 14:33:05 +0000516 CmdArgs.push_back("-g");
517
518 if (Args.hasArg(options::OPT_v))
519 CmdArgs.push_back("-v");
520
521 StringRef GPUArch =
522 Args.getLastArgValue(options::OPT_march_EQ);
523 assert(!GPUArch.empty() && "At least one GPU Arch required for ptxas.");
524
525 CmdArgs.push_back("-arch");
526 CmdArgs.push_back(Args.MakeArgString(GPUArch));
527
Jonas Hahnfelda981f672018-09-27 16:12:32 +0000528 // Assume that the directory specified with --libomptarget_nvptx_path
529 // contains the static library libomptarget-nvptx.a.
530 if (const Arg *A = Args.getLastArg(options::OPT_libomptarget_nvptx_path_EQ))
531 CmdArgs.push_back(Args.MakeArgString(Twine("-L") + A->getValue()));
532
Gheorghe-Teodor Bercea2c926932017-08-08 14:33:05 +0000533 // Add paths specified in LIBRARY_PATH environment variable as -L options.
534 addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH");
535
536 // Add paths for the default clang library path.
537 SmallString<256> DefaultLibPath =
538 llvm::sys::path::parent_path(TC.getDriver().Dir);
539 llvm::sys::path::append(DefaultLibPath, "lib" CLANG_LIBDIR_SUFFIX);
540 CmdArgs.push_back(Args.MakeArgString(Twine("-L") + DefaultLibPath));
541
542 // Add linking against library implementing OpenMP calls on NVPTX target.
543 CmdArgs.push_back("-lomptarget-nvptx");
544
545 for (const auto &II : Inputs) {
546 if (II.getType() == types::TY_LLVM_IR ||
547 II.getType() == types::TY_LTO_IR ||
548 II.getType() == types::TY_LTO_BC ||
549 II.getType() == types::TY_LLVM_BC) {
550 C.getDriver().Diag(diag::err_drv_no_linker_llvm_support)
551 << getToolChain().getTripleString();
552 continue;
553 }
554
555 // Currently, we only pass the input files to the linker, we do not pass
556 // any libraries that may be valid only for the host.
557 if (!II.isFilename())
558 continue;
559
Jonas Hahnfeld7c78cc52017-11-21 14:44:45 +0000560 const char *CubinF = C.addTempFile(
561 C.getArgs().MakeArgString(getToolChain().getInputFilename(II)));
Gheorghe-Teodor Bercea2c926932017-08-08 14:33:05 +0000562
563 CmdArgs.push_back(CubinF);
564 }
565
Gheorghe-Teodor Bercea2c926932017-08-08 14:33:05 +0000566 const char *Exec =
567 Args.MakeArgString(getToolChain().GetProgramPath("nvlink"));
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +0000568 C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
Gheorghe-Teodor Bercea2c926932017-08-08 14:33:05 +0000569}
570
David L. Jonesf561aba2017-03-08 01:02:16 +0000571/// CUDA toolchain. Our assembler is ptxas, and our "linker" is fatbinary,
572/// which isn't properly a linker but nonetheless performs the step of stitching
573/// together object files from the assembler into a single blob.
574
575CudaToolChain::CudaToolChain(const Driver &D, const llvm::Triple &Triple,
Gheorghe-Teodor Bercea2c926932017-08-08 14:33:05 +0000576 const ToolChain &HostTC, const ArgList &Args,
577 const Action::OffloadKind OK)
David L. Jonesf561aba2017-03-08 01:02:16 +0000578 : ToolChain(D, Triple, Args), HostTC(HostTC),
Gheorghe-Teodor Bercea2c926932017-08-08 14:33:05 +0000579 CudaInstallation(D, HostTC.getTriple(), Args), OK(OK) {
David L. Jonesf561aba2017-03-08 01:02:16 +0000580 if (CudaInstallation.isValid())
581 getProgramPaths().push_back(CudaInstallation.getBinPath());
Gheorghe-Teodor Bercea690f6f92017-08-09 19:52:28 +0000582 // Lookup binaries into the driver directory, this is used to
583 // discover the clang-offload-bundler executable.
584 getProgramPaths().push_back(getDriver().Dir);
David L. Jonesf561aba2017-03-08 01:02:16 +0000585}
586
Jonas Hahnfeld7c78cc52017-11-21 14:44:45 +0000587std::string CudaToolChain::getInputFilename(const InputInfo &Input) const {
588 // Only object files are changed, for example assembly files keep their .s
589 // extensions. CUDA also continues to use .o as they don't use nvlink but
590 // fatbinary.
591 if (!(OK == Action::OFK_OpenMP && Input.getType() == types::TY_Object))
592 return ToolChain::getInputFilename(Input);
593
594 // Replace extension for object files with cubin because nvlink relies on
595 // these particular file names.
596 SmallString<256> Filename(ToolChain::getInputFilename(Input));
597 llvm::sys::path::replace_extension(Filename, "cubin");
598 return Filename.str();
599}
600
David L. Jonesf561aba2017-03-08 01:02:16 +0000601void CudaToolChain::addClangTargetOptions(
602 const llvm::opt::ArgList &DriverArgs,
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000603 llvm::opt::ArgStringList &CC1Args,
604 Action::OffloadKind DeviceOffloadingKind) const {
605 HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
David L. Jonesf561aba2017-03-08 01:02:16 +0000606
607 StringRef GpuArch = DriverArgs.getLastArgValue(options::OPT_march_EQ);
608 assert(!GpuArch.empty() && "Must have an explicit GPU arch.");
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000609 assert((DeviceOffloadingKind == Action::OFK_OpenMP ||
610 DeviceOffloadingKind == Action::OFK_Cuda) &&
611 "Only OpenMP or CUDA offloading kinds are supported for NVIDIA GPUs.");
612
613 if (DeviceOffloadingKind == Action::OFK_Cuda) {
614 CC1Args.push_back("-fcuda-is-device");
615
616 if (DriverArgs.hasFlag(options::OPT_fcuda_flush_denormals_to_zero,
617 options::OPT_fno_cuda_flush_denormals_to_zero, false))
618 CC1Args.push_back("-fcuda-flush-denormals-to-zero");
619
620 if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals,
621 options::OPT_fno_cuda_approx_transcendentals, false))
622 CC1Args.push_back("-fcuda-approx-transcendentals");
Jonas Hahnfeld5379c6d2018-02-12 10:46:45 +0000623
Yaxun Liu97670892018-10-02 17:48:54 +0000624 if (DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
Jonas Hahnfeld5379c6d2018-02-12 10:46:45 +0000625 false))
Yaxun Liu97670892018-10-02 17:48:54 +0000626 CC1Args.push_back("-fgpu-rdc");
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000627 }
628
Yaxun Liu99d0d3a2019-10-03 18:59:56 +0000629 if (DriverArgs.hasArg(options::OPT_nogpulib))
Gheorghe-Teodor Bercea20789a52017-09-25 21:56:32 +0000630 return;
631
David L. Jonesf561aba2017-03-08 01:02:16 +0000632 std::string LibDeviceFile = CudaInstallation.getLibDeviceFile(GpuArch);
633
634 if (LibDeviceFile.empty()) {
Gheorghe-Teodor Bercea5a3608c2017-09-26 15:36:20 +0000635 if (DeviceOffloadingKind == Action::OFK_OpenMP &&
636 DriverArgs.hasArg(options::OPT_S))
637 return;
638
David L. Jonesf561aba2017-03-08 01:02:16 +0000639 getDriver().Diag(diag::err_drv_no_cuda_libdevice) << GpuArch;
640 return;
641 }
642
Matt Arsenaulta13746b2018-08-20 18:16:48 +0000643 CC1Args.push_back("-mlink-builtin-bitcode");
David L. Jonesf561aba2017-03-08 01:02:16 +0000644 CC1Args.push_back(DriverArgs.MakeArgString(LibDeviceFile));
645
Artem Belevich5fe85a02019-04-25 22:28:09 +0000646 // New CUDA versions often introduce new instructions that are only supported
647 // by new PTX version, so we need to raise PTX level to enable them in NVPTX
648 // back-end.
649 const char *PtxFeature = nullptr;
650 switch(CudaInstallation.version()) {
651 case CudaVersion::CUDA_101:
652 PtxFeature = "+ptx64";
653 break;
654 case CudaVersion::CUDA_100:
655 PtxFeature = "+ptx63";
656 break;
Gheorghe-Teodor Berceadb900e32019-05-03 17:59:18 +0000657 case CudaVersion::CUDA_92:
658 PtxFeature = "+ptx61";
659 break;
Artem Belevich5fe85a02019-04-25 22:28:09 +0000660 case CudaVersion::CUDA_91:
661 PtxFeature = "+ptx61";
662 break;
663 case CudaVersion::CUDA_90:
664 PtxFeature = "+ptx60";
665 break;
666 default:
667 PtxFeature = "+ptx42";
Artem Belevich4654dc82017-09-20 21:23:07 +0000668 }
Artem Belevich679dafe2018-05-09 23:10:09 +0000669 CC1Args.append({"-target-feature", PtxFeature});
670 if (DriverArgs.hasFlag(options::OPT_fcuda_short_ptr,
671 options::OPT_fno_cuda_short_ptr, false))
672 CC1Args.append({"-mllvm", "--nvptx-short-ptr"});
Gheorghe-Teodor Bercea0d5aa842018-03-13 23:19:52 +0000673
Artem Belevich8fa28a02019-01-31 21:32:24 +0000674 if (CudaInstallation.version() >= CudaVersion::UNKNOWN)
675 CC1Args.push_back(DriverArgs.MakeArgString(
676 Twine("-target-sdk-version=") +
677 CudaVersionToString(CudaInstallation.version())));
678
Gheorghe-Teodor Bercea0d5aa842018-03-13 23:19:52 +0000679 if (DeviceOffloadingKind == Action::OFK_OpenMP) {
680 SmallVector<StringRef, 8> LibraryPaths;
Jonas Hahnfelda981f672018-09-27 16:12:32 +0000681 if (const Arg *A = DriverArgs.getLastArg(options::OPT_libomptarget_nvptx_path_EQ))
682 LibraryPaths.push_back(A->getValue());
Gheorghe-Teodor Bercea0d5aa842018-03-13 23:19:52 +0000683
684 // Add user defined library paths from LIBRARY_PATH.
685 llvm::Optional<std::string> LibPath =
686 llvm::sys::Process::GetEnv("LIBRARY_PATH");
687 if (LibPath) {
688 SmallVector<StringRef, 8> Frags;
689 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'};
690 llvm::SplitString(*LibPath, Frags, EnvPathSeparatorStr);
691 for (StringRef Path : Frags)
692 LibraryPaths.emplace_back(Path.trim());
693 }
694
Jonas Hahnfelda981f672018-09-27 16:12:32 +0000695 // Add path to lib / lib64 folder.
696 SmallString<256> DefaultLibPath =
697 llvm::sys::path::parent_path(getDriver().Dir);
698 llvm::sys::path::append(DefaultLibPath, Twine("lib") + CLANG_LIBDIR_SUFFIX);
699 LibraryPaths.emplace_back(DefaultLibPath.c_str());
700
Gheorghe-Teodor Bercea0d5aa842018-03-13 23:19:52 +0000701 std::string LibOmpTargetName =
702 "libomptarget-nvptx-" + GpuArch.str() + ".bc";
703 bool FoundBCLibrary = false;
704 for (StringRef LibraryPath : LibraryPaths) {
705 SmallString<128> LibOmpTargetFile(LibraryPath);
706 llvm::sys::path::append(LibOmpTargetFile, LibOmpTargetName);
707 if (llvm::sys::fs::exists(LibOmpTargetFile)) {
Matt Arsenaulta13746b2018-08-20 18:16:48 +0000708 CC1Args.push_back("-mlink-builtin-bitcode");
Gheorghe-Teodor Bercea0d5aa842018-03-13 23:19:52 +0000709 CC1Args.push_back(DriverArgs.MakeArgString(LibOmpTargetFile));
710 FoundBCLibrary = true;
711 break;
712 }
713 }
714 if (!FoundBCLibrary)
715 getDriver().Diag(diag::warn_drv_omp_offload_target_missingbcruntime)
716 << LibOmpTargetName;
717 }
David L. Jonesf561aba2017-03-08 01:02:16 +0000718}
719
Alexey Bataevb83b4e42018-07-27 19:45:14 +0000720bool CudaToolChain::supportsDebugInfoOption(const llvm::opt::Arg *A) const {
721 const Option &O = A->getOption();
722 return (O.matches(options::OPT_gN_Group) &&
723 !O.matches(options::OPT_gmodules)) ||
724 O.matches(options::OPT_g_Flag) ||
725 O.matches(options::OPT_ggdbN_Group) || O.matches(options::OPT_ggdb) ||
726 O.matches(options::OPT_gdwarf) || O.matches(options::OPT_gdwarf_2) ||
727 O.matches(options::OPT_gdwarf_3) || O.matches(options::OPT_gdwarf_4) ||
728 O.matches(options::OPT_gdwarf_5) ||
729 O.matches(options::OPT_gcolumn_info);
730}
731
Alexey Bataevc92fc3c2018-12-12 14:52:27 +0000732void CudaToolChain::adjustDebugInfoKind(
733 codegenoptions::DebugInfoKind &DebugInfoKind, const ArgList &Args) const {
734 switch (mustEmitDebugInfo(Args)) {
735 case DisableDebugInfo:
736 DebugInfoKind = codegenoptions::NoDebugInfo;
737 break;
738 case DebugDirectivesOnly:
739 DebugInfoKind = codegenoptions::DebugDirectivesOnly;
740 break;
741 case EmitSameDebugInfoAsHost:
742 // Use same debug info level as the host.
743 break;
744 }
745}
746
David L. Jonesf561aba2017-03-08 01:02:16 +0000747void CudaToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs,
748 ArgStringList &CC1Args) const {
749 // Check our CUDA version if we're going to include the CUDA headers.
750 if (!DriverArgs.hasArg(options::OPT_nocudainc) &&
751 !DriverArgs.hasArg(options::OPT_no_cuda_version_check)) {
752 StringRef Arch = DriverArgs.getLastArgValue(options::OPT_march_EQ);
753 assert(!Arch.empty() && "Must have an explicit GPU arch.");
754 CudaInstallation.CheckCudaVersionSupportsArch(StringToCudaArch(Arch));
755 }
756 CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
757}
758
759llvm::opt::DerivedArgList *
760CudaToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
761 StringRef BoundArch,
762 Action::OffloadKind DeviceOffloadKind) const {
763 DerivedArgList *DAL =
764 HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind);
765 if (!DAL)
766 DAL = new DerivedArgList(Args.getBaseArgs());
767
768 const OptTable &Opts = getDriver().getOpts();
769
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000770 // For OpenMP device offloading, append derived arguments. Make sure
771 // flags are not duplicated.
Gheorghe-Teodor Bercea47e0cf32017-08-07 15:39:11 +0000772 // Also append the compute capability.
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000773 if (DeviceOffloadKind == Action::OFK_OpenMP) {
Jonas Hahnfeld30b44182017-10-17 13:37:36 +0000774 for (Arg *A : Args) {
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000775 bool IsDuplicate = false;
Jonas Hahnfeld30b44182017-10-17 13:37:36 +0000776 for (Arg *DALArg : *DAL) {
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000777 if (A == DALArg) {
778 IsDuplicate = true;
779 break;
780 }
781 }
782 if (!IsDuplicate)
783 DAL->append(A);
784 }
Gheorghe-Teodor Bercea47e0cf32017-08-07 15:39:11 +0000785
786 StringRef Arch = DAL->getLastArgValue(options::OPT_march_EQ);
Jonas Hahnfeld30b44182017-10-17 13:37:36 +0000787 if (Arch.empty())
788 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ),
789 CLANG_OPENMP_NVPTX_DEFAULT_ARCH);
Gheorghe-Teodor Bercea47e0cf32017-08-07 15:39:11 +0000790
Gheorghe-Teodor Berceaf0f29602017-07-06 16:22:21 +0000791 return DAL;
792 }
793
David L. Jonesf561aba2017-03-08 01:02:16 +0000794 for (Arg *A : Args) {
795 if (A->getOption().matches(options::OPT_Xarch__)) {
796 // Skip this argument unless the architecture matches BoundArch
797 if (BoundArch.empty() || A->getValue(0) != BoundArch)
798 continue;
799
800 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
801 unsigned Prev = Index;
802 std::unique_ptr<Arg> XarchArg(Opts.ParseOneArg(Args, Index));
803
804 // If the argument parsing failed or more than one argument was
805 // consumed, the -Xarch_ argument's parameter tried to consume
806 // extra arguments. Emit an error and ignore.
807 //
808 // We also want to disallow any options which would alter the
809 // driver behavior; that isn't going to work in our model. We
810 // use isDriverOption() as an approximation, although things
811 // like -O4 are going to slip through.
812 if (!XarchArg || Index > Prev + 1) {
813 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
814 << A->getAsString(Args);
815 continue;
816 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
817 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
818 << A->getAsString(Args);
819 continue;
820 }
821 XarchArg->setBaseArg(A);
822 A = XarchArg.release();
823 DAL->AddSynthesizedArg(A);
824 }
825 DAL->append(A);
826 }
827
828 if (!BoundArch.empty()) {
829 DAL->eraseArg(options::OPT_march_EQ);
830 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ), BoundArch);
831 }
832 return DAL;
833}
834
835Tool *CudaToolChain::buildAssembler() const {
836 return new tools::NVPTX::Assembler(*this);
837}
838
839Tool *CudaToolChain::buildLinker() const {
Gheorghe-Teodor Bercea2c926932017-08-08 14:33:05 +0000840 if (OK == Action::OFK_OpenMP)
841 return new tools::NVPTX::OpenMPLinker(*this);
David L. Jonesf561aba2017-03-08 01:02:16 +0000842 return new tools::NVPTX::Linker(*this);
843}
844
845void CudaToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
846 HostTC.addClangWarningOptions(CC1Args);
847}
848
849ToolChain::CXXStdlibType
850CudaToolChain::GetCXXStdlibType(const ArgList &Args) const {
851 return HostTC.GetCXXStdlibType(Args);
852}
853
854void CudaToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
855 ArgStringList &CC1Args) const {
856 HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
857}
858
859void CudaToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &Args,
860 ArgStringList &CC1Args) const {
861 HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args);
862}
863
864void CudaToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
865 ArgStringList &CC1Args) const {
866 HostTC.AddIAMCUIncludeArgs(Args, CC1Args);
867}
868
869SanitizerMask CudaToolChain::getSupportedSanitizers() const {
870 // The CudaToolChain only supports sanitizers in the sense that it allows
871 // sanitizer arguments on the command line if they are supported by the host
872 // toolchain. The CudaToolChain will actually ignore any command line
873 // arguments for any of these "supported" sanitizers. That means that no
874 // sanitization of device code is actually supported at this time.
875 //
876 // This behavior is necessary because the host and device toolchains
877 // invocations often share the command line, so the device toolchain must
878 // tolerate flags meant only for the host toolchain.
879 return HostTC.getSupportedSanitizers();
880}
881
882VersionTuple CudaToolChain::computeMSVCVersion(const Driver *D,
883 const ArgList &Args) const {
884 return HostTC.computeMSVCVersion(D, Args);
885}