1 | //===--- Cuda.cpp - Cuda Tool and ToolChain Implementations -----*- C++ -*-===// |
---|---|
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "Cuda.h" |
10 | #include "clang/Basic/Cuda.h" |
11 | #include "clang/Config/config.h" |
12 | #include "clang/Driver/CommonArgs.h" |
13 | #include "clang/Driver/Compilation.h" |
14 | #include "clang/Driver/Distro.h" |
15 | #include "clang/Driver/Driver.h" |
16 | #include "clang/Driver/InputInfo.h" |
17 | #include "clang/Driver/Options.h" |
18 | #include "llvm/ADT/StringExtras.h" |
19 | #include "llvm/Config/llvm-config.h" // for LLVM_HOST_TRIPLE |
20 | #include "llvm/Option/ArgList.h" |
21 | #include "llvm/Support/FileSystem.h" |
22 | #include "llvm/Support/Path.h" |
23 | #include "llvm/Support/Process.h" |
24 | #include "llvm/Support/Program.h" |
25 | #include "llvm/Support/VirtualFileSystem.h" |
26 | #include "llvm/TargetParser/Host.h" |
27 | #include "llvm/TargetParser/TargetParser.h" |
28 | #include <system_error> |
29 | |
30 | using namespace clang::driver; |
31 | using namespace clang::driver::toolchains; |
32 | using namespace clang::driver::tools; |
33 | using namespace clang; |
34 | using namespace llvm::opt; |
35 | |
36 | namespace { |
37 | |
38 | CudaVersion getCudaVersion(uint32_t raw_version) { |
39 | if (raw_version < 7050) |
40 | return CudaVersion::CUDA_70; |
41 | if (raw_version < 8000) |
42 | return CudaVersion::CUDA_75; |
43 | if (raw_version < 9000) |
44 | return CudaVersion::CUDA_80; |
45 | if (raw_version < 9010) |
46 | return CudaVersion::CUDA_90; |
47 | if (raw_version < 9020) |
48 | return CudaVersion::CUDA_91; |
49 | if (raw_version < 10000) |
50 | return CudaVersion::CUDA_92; |
51 | if (raw_version < 10010) |
52 | return CudaVersion::CUDA_100; |
53 | if (raw_version < 10020) |
54 | return CudaVersion::CUDA_101; |
55 | if (raw_version < 11000) |
56 | return CudaVersion::CUDA_102; |
57 | if (raw_version < 11010) |
58 | return CudaVersion::CUDA_110; |
59 | if (raw_version < 11020) |
60 | return CudaVersion::CUDA_111; |
61 | if (raw_version < 11030) |
62 | return CudaVersion::CUDA_112; |
63 | if (raw_version < 11040) |
64 | return CudaVersion::CUDA_113; |
65 | if (raw_version < 11050) |
66 | return CudaVersion::CUDA_114; |
67 | if (raw_version < 11060) |
68 | return CudaVersion::CUDA_115; |
69 | if (raw_version < 11070) |
70 | return CudaVersion::CUDA_116; |
71 | if (raw_version < 11080) |
72 | return CudaVersion::CUDA_117; |
73 | if (raw_version < 11090) |
74 | return CudaVersion::CUDA_118; |
75 | if (raw_version < 12010) |
76 | return CudaVersion::CUDA_120; |
77 | if (raw_version < 12020) |
78 | return CudaVersion::CUDA_121; |
79 | if (raw_version < 12030) |
80 | return CudaVersion::CUDA_122; |
81 | if (raw_version < 12040) |
82 | return CudaVersion::CUDA_123; |
83 | if (raw_version < 12050) |
84 | return CudaVersion::CUDA_124; |
85 | if (raw_version < 12060) |
86 | return CudaVersion::CUDA_125; |
87 | if (raw_version < 12070) |
88 | return CudaVersion::CUDA_126; |
89 | if (raw_version < 12090) |
90 | return CudaVersion::CUDA_128; |
91 | return CudaVersion::NEW; |
92 | } |
93 | |
94 | CudaVersion parseCudaHFile(llvm::StringRef Input) { |
95 | // Helper lambda which skips the words if the line starts with them or returns |
96 | // std::nullopt otherwise. |
97 | auto StartsWithWords = |
98 | [](llvm::StringRef Line, |
99 | const SmallVector<StringRef, 3> words) -> std::optional<StringRef> { |
100 | for (StringRef word : words) { |
101 | if (!Line.consume_front(Prefix: word)) |
102 | return {}; |
103 | Line = Line.ltrim(); |
104 | } |
105 | return Line; |
106 | }; |
107 | |
108 | Input = Input.ltrim(); |
109 | while (!Input.empty()) { |
110 | if (auto Line = |
111 | StartsWithWords(Input.ltrim(), {"#", "define", "CUDA_VERSION"})) { |
112 | uint32_t RawVersion; |
113 | Line->consumeInteger(Radix: 10, Result&: RawVersion); |
114 | return getCudaVersion(raw_version: RawVersion); |
115 | } |
116 | // Find next non-empty line. |
117 | Input = Input.drop_front(N: Input.find_first_of(Chars: "\n\r")).ltrim(); |
118 | } |
119 | return CudaVersion::UNKNOWN; |
120 | } |
121 | } // namespace |
122 | |
123 | void CudaInstallationDetector::WarnIfUnsupportedVersion() const { |
124 | if (Version > CudaVersion::PARTIALLY_SUPPORTED) { |
125 | std::string VersionString = CudaVersionToString(V: Version); |
126 | if (!VersionString.empty()) |
127 | VersionString.insert(pos: 0, s: " "); |
128 | D.Diag(diag::DiagID: warn_drv_new_cuda_version) |
129 | << VersionString |
130 | << (CudaVersion::PARTIALLY_SUPPORTED != CudaVersion::FULLY_SUPPORTED) |
131 | << CudaVersionToString(V: CudaVersion::PARTIALLY_SUPPORTED); |
132 | } else if (Version > CudaVersion::FULLY_SUPPORTED) |
133 | D.Diag(diag::DiagID: warn_drv_partially_supported_cuda_version) |
134 | << CudaVersionToString(V: Version); |
135 | } |
136 | |
137 | CudaInstallationDetector::CudaInstallationDetector( |
138 | const Driver &D, const llvm::Triple &HostTriple, |
139 | const llvm::opt::ArgList &Args) |
140 | : D(D) { |
141 | struct Candidate { |
142 | std::string Path; |
143 | bool StrictChecking; |
144 | |
145 | Candidate(std::string Path, bool StrictChecking = false) |
146 | : Path(Path), StrictChecking(StrictChecking) {} |
147 | }; |
148 | SmallVector<Candidate, 4> Candidates; |
149 | |
150 | // In decreasing order so we prefer newer versions to older versions. |
151 | std::initializer_list<const char *> Versions = {"8.0", "7.5", "7.0"}; |
152 | auto &FS = D.getVFS(); |
153 | |
154 | if (Args.hasArg(clang::driver::options::OPT_cuda_path_EQ)) { |
155 | Candidates.emplace_back( |
156 | Args.getLastArgValue(clang::driver::options::Id: OPT_cuda_path_EQ).str()); |
157 | } else if (HostTriple.isOSWindows()) { |
158 | for (const char *Ver : Versions) |
159 | Candidates.emplace_back( |
160 | Args: D.SysRoot + "/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v"+ |
161 | Ver); |
162 | } else { |
163 | if (!Args.hasArg(clang::driver::options::OPT_cuda_path_ignore_env)) { |
164 | // Try to find ptxas binary. If the executable is located in a directory |
165 | // called 'bin/', its parent directory might be a good guess for a valid |
166 | // CUDA installation. |
167 | // However, some distributions might installs 'ptxas' to /usr/bin. In that |
168 | // case the candidate would be '/usr' which passes the following checks |
169 | // because '/usr/include' exists as well. To avoid this case, we always |
170 | // check for the directory potentially containing files for libdevice, |
171 | // even if the user passes -nocudalib. |
172 | if (llvm::ErrorOr<std::string> ptxas = |
173 | llvm::sys::findProgramByName(Name: "ptxas")) { |
174 | SmallString<256> ptxasAbsolutePath; |
175 | llvm::sys::fs::real_path(path: *ptxas, output&: ptxasAbsolutePath); |
176 | |
177 | StringRef ptxasDir = llvm::sys::path::parent_path(path: ptxasAbsolutePath); |
178 | if (llvm::sys::path::filename(path: ptxasDir) == "bin") |
179 | Candidates.emplace_back( |
180 | Args: std::string(llvm::sys::path::parent_path(path: ptxasDir)), |
181 | /*StrictChecking=*/Args: true); |
182 | } |
183 | } |
184 | |
185 | Candidates.emplace_back(Args: D.SysRoot + "/usr/local/cuda"); |
186 | for (const char *Ver : Versions) |
187 | Candidates.emplace_back(Args: D.SysRoot + "/usr/local/cuda-"+ Ver); |
188 | |
189 | Distro Dist(FS, llvm::Triple(llvm::sys::getProcessTriple())); |
190 | if (Dist.IsDebian() || Dist.IsUbuntu()) |
191 | // Special case for Debian to have nvidia-cuda-toolkit work |
192 | // out of the box. More info on http://bugs.debian.org/882505 |
193 | Candidates.emplace_back(Args: D.SysRoot + "/usr/lib/cuda"); |
194 | } |
195 | |
196 | bool NoCudaLib = |
197 | !Args.hasFlag(options::OPT_offloadlib, options::OPT_no_offloadlib, true); |
198 | |
199 | for (const auto &Candidate : Candidates) { |
200 | InstallPath = Candidate.Path; |
201 | if (InstallPath.empty() || !FS.exists(Path: InstallPath)) |
202 | continue; |
203 | |
204 | BinPath = InstallPath + "/bin"; |
205 | IncludePath = InstallPath + "/include"; |
206 | LibDevicePath = InstallPath + "/nvvm/libdevice"; |
207 | |
208 | if (!(FS.exists(Path: IncludePath) && FS.exists(Path: BinPath))) |
209 | continue; |
210 | bool CheckLibDevice = (!NoCudaLib || Candidate.StrictChecking); |
211 | if (CheckLibDevice && !FS.exists(Path: LibDevicePath)) |
212 | continue; |
213 | |
214 | Version = CudaVersion::UNKNOWN; |
215 | if (auto CudaHFile = FS.getBufferForFile(Name: InstallPath + "/include/cuda.h")) |
216 | Version = parseCudaHFile(Input: (*CudaHFile)->getBuffer()); |
217 | // As the last resort, make an educated guess between CUDA-7.0, which had |
218 | // old-style libdevice bitcode, and an unknown recent CUDA version. |
219 | if (Version == CudaVersion::UNKNOWN) { |
220 | Version = FS.exists(Path: LibDevicePath + "/libdevice.10.bc") |
221 | ? CudaVersion::NEW |
222 | : CudaVersion::CUDA_70; |
223 | } |
224 | |
225 | if (Version >= CudaVersion::CUDA_90) { |
226 | // CUDA-9+ uses single libdevice file for all GPU variants. |
227 | std::string FilePath = LibDevicePath + "/libdevice.10.bc"; |
228 | if (FS.exists(Path: FilePath)) { |
229 | for (int Arch = (int)OffloadArch::SM_30, E = (int)OffloadArch::LAST; |
230 | Arch < E; ++Arch) { |
231 | OffloadArch OA = static_cast<OffloadArch>(Arch); |
232 | if (!IsNVIDIAOffloadArch(A: OA)) |
233 | continue; |
234 | std::string OffloadArchName(OffloadArchToString(A: OA)); |
235 | LibDeviceMap[OffloadArchName] = FilePath; |
236 | } |
237 | } |
238 | } else { |
239 | std::error_code EC; |
240 | for (llvm::vfs::directory_iterator LI = FS.dir_begin(Dir: LibDevicePath, EC), |
241 | LE; |
242 | !EC && LI != LE; LI = LI.increment(EC)) { |
243 | StringRef FilePath = LI->path(); |
244 | StringRef FileName = llvm::sys::path::filename(path: FilePath); |
245 | // Process all bitcode filenames that look like |
246 | // libdevice.compute_XX.YY.bc |
247 | const StringRef LibDeviceName = "libdevice."; |
248 | if (!(FileName.starts_with(Prefix: LibDeviceName) && FileName.ends_with(Suffix: ".bc"))) |
249 | continue; |
250 | StringRef GpuArch = FileName.slice( |
251 | Start: LibDeviceName.size(), End: FileName.find(C: '.', From: LibDeviceName.size())); |
252 | LibDeviceMap[GpuArch] = FilePath.str(); |
253 | // Insert map entries for specific devices with this compute |
254 | // capability. NVCC's choice of the libdevice library version is |
255 | // rather peculiar and depends on the CUDA version. |
256 | if (GpuArch == "compute_20") { |
257 | LibDeviceMap["sm_20"] = std::string(FilePath); |
258 | LibDeviceMap["sm_21"] = std::string(FilePath); |
259 | LibDeviceMap["sm_32"] = std::string(FilePath); |
260 | } else if (GpuArch == "compute_30") { |
261 | LibDeviceMap["sm_30"] = std::string(FilePath); |
262 | if (Version < CudaVersion::CUDA_80) { |
263 | LibDeviceMap["sm_50"] = std::string(FilePath); |
264 | LibDeviceMap["sm_52"] = std::string(FilePath); |
265 | LibDeviceMap["sm_53"] = std::string(FilePath); |
266 | } |
267 | LibDeviceMap["sm_60"] = std::string(FilePath); |
268 | LibDeviceMap["sm_61"] = std::string(FilePath); |
269 | LibDeviceMap["sm_62"] = std::string(FilePath); |
270 | } else if (GpuArch == "compute_35") { |
271 | LibDeviceMap["sm_35"] = std::string(FilePath); |
272 | LibDeviceMap["sm_37"] = std::string(FilePath); |
273 | } else if (GpuArch == "compute_50") { |
274 | if (Version >= CudaVersion::CUDA_80) { |
275 | LibDeviceMap["sm_50"] = std::string(FilePath); |
276 | LibDeviceMap["sm_52"] = std::string(FilePath); |
277 | LibDeviceMap["sm_53"] = std::string(FilePath); |
278 | } |
279 | } |
280 | } |
281 | } |
282 | |
283 | // Check that we have found at least one libdevice that we can link in if |
284 | // -nocudalib hasn't been specified. |
285 | if (LibDeviceMap.empty() && !NoCudaLib) |
286 | continue; |
287 | |
288 | IsValid = true; |
289 | break; |
290 | } |
291 | } |
292 | |
293 | void CudaInstallationDetector::AddCudaIncludeArgs( |
294 | const ArgList &DriverArgs, ArgStringList &CC1Args) const { |
295 | if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { |
296 | // Add cuda_wrappers/* to our system include path. This lets us wrap |
297 | // standard library headers. |
298 | SmallString<128> P(D.ResourceDir); |
299 | llvm::sys::path::append(path&: P, a: "include"); |
300 | llvm::sys::path::append(path&: P, a: "cuda_wrappers"); |
301 | CC1Args.push_back(Elt: "-internal-isystem"); |
302 | CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: P)); |
303 | } |
304 | |
305 | if (DriverArgs.hasArg(options::OPT_nogpuinc)) |
306 | return; |
307 | |
308 | if (!isValid()) { |
309 | D.Diag(diag::DiagID: err_drv_no_cuda_installation); |
310 | return; |
311 | } |
312 | |
313 | CC1Args.push_back(Elt: "-include"); |
314 | CC1Args.push_back(Elt: "__clang_cuda_runtime_wrapper.h"); |
315 | } |
316 | |
317 | void CudaInstallationDetector::CheckCudaVersionSupportsArch( |
318 | OffloadArch Arch) const { |
319 | if (Arch == OffloadArch::UNKNOWN || Version == CudaVersion::UNKNOWN || |
320 | ArchsWithBadVersion[(int)Arch]) |
321 | return; |
322 | |
323 | auto MinVersion = MinVersionForOffloadArch(A: Arch); |
324 | auto MaxVersion = MaxVersionForOffloadArch(A: Arch); |
325 | if (Version < MinVersion || Version > MaxVersion) { |
326 | ArchsWithBadVersion[(int)Arch] = true; |
327 | D.Diag(diag::DiagID: err_drv_cuda_version_unsupported) |
328 | << OffloadArchToString(A: Arch) << CudaVersionToString(V: MinVersion) |
329 | << CudaVersionToString(V: MaxVersion) << InstallPath |
330 | << CudaVersionToString(V: Version); |
331 | } |
332 | } |
333 | |
334 | void CudaInstallationDetector::print(raw_ostream &OS) const { |
335 | if (isValid()) |
336 | OS << "Found CUDA installation: "<< InstallPath << ", version " |
337 | << CudaVersionToString(V: Version) << "\n"; |
338 | } |
339 | |
340 | namespace { |
341 | /// Debug info level for the NVPTX devices. We may need to emit different debug |
342 | /// info level for the host and for the device itselfi. This type controls |
343 | /// emission of the debug info for the devices. It either prohibits disable info |
344 | /// emission completely, or emits debug directives only, or emits same debug |
345 | /// info as for the host. |
346 | enum DeviceDebugInfoLevel { |
347 | DisableDebugInfo, /// Do not emit debug info for the devices. |
348 | DebugDirectivesOnly, /// Emit only debug directives. |
349 | EmitSameDebugInfoAsHost, /// Use the same debug info level just like for the |
350 | /// host. |
351 | }; |
352 | } // anonymous namespace |
353 | |
354 | /// Define debug info level for the NVPTX devices. If the debug info for both |
355 | /// the host and device are disabled (-g0/-ggdb0 or no debug options at all). If |
356 | /// only debug directives are requested for the both host and device |
357 | /// (-gline-directvies-only), or the debug info only for the device is disabled |
358 | /// (optimization is on and --cuda-noopt-device-debug was not specified), the |
359 | /// debug directves only must be emitted for the device. Otherwise, use the same |
360 | /// debug info level just like for the host (with the limitations of only |
361 | /// supported DWARF2 standard). |
362 | static DeviceDebugInfoLevel mustEmitDebugInfo(const ArgList &Args) { |
363 | const Arg *A = Args.getLastArg(options::OPT_O_Group); |
364 | bool IsDebugEnabled = !A || A->getOption().matches(options::ID: OPT_O0) || |
365 | Args.hasFlag(options::OPT_cuda_noopt_device_debug, |
366 | options::OPT_no_cuda_noopt_device_debug, |
367 | /*Default=*/false); |
368 | if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) { |
369 | const Option &Opt = A->getOption(); |
370 | if (Opt.matches(options::ID: OPT_gN_Group)) { |
371 | if (Opt.matches(options::ID: OPT_g0) || Opt.matches(options::ID: OPT_ggdb0)) |
372 | return DisableDebugInfo; |
373 | if (Opt.matches(options::ID: OPT_gline_directives_only)) |
374 | return DebugDirectivesOnly; |
375 | } |
376 | return IsDebugEnabled ? EmitSameDebugInfoAsHost : DebugDirectivesOnly; |
377 | } |
378 | return willEmitRemarks(Args) ? DebugDirectivesOnly : DisableDebugInfo; |
379 | } |
380 | |
381 | void NVPTX::Assembler::ConstructJob(Compilation &C, const JobAction &JA, |
382 | const InputInfo &Output, |
383 | const InputInfoList &Inputs, |
384 | const ArgList &Args, |
385 | const char *LinkingOutput) const { |
386 | const auto &TC = |
387 | static_cast<const toolchains::NVPTXToolChain &>(getToolChain()); |
388 | assert(TC.getTriple().isNVPTX() && "Wrong platform"); |
389 | |
390 | StringRef GPUArchName; |
391 | // If this is a CUDA action we need to extract the device architecture |
392 | // from the Job's associated architecture, otherwise use the -march=arch |
393 | // option. This option may come from -Xopenmp-target flag or the default |
394 | // value. |
395 | if (JA.isDeviceOffloading(OKind: Action::OFK_Cuda)) { |
396 | GPUArchName = JA.getOffloadingArch(); |
397 | } else { |
398 | GPUArchName = Args.getLastArgValue(options::Id: OPT_march_EQ); |
399 | if (GPUArchName.empty()) { |
400 | C.getDriver().Diag(diag::DiagID: err_drv_offload_missing_gpu_arch) |
401 | << getToolChain().getArchName() << getShortName(); |
402 | return; |
403 | } |
404 | } |
405 | |
406 | // Obtain architecture from the action. |
407 | OffloadArch gpu_arch = StringToOffloadArch(S: GPUArchName); |
408 | assert(gpu_arch != OffloadArch::UNKNOWN && |
409 | "Device action expected to have an architecture."); |
410 | |
411 | // Check that our installation's ptxas supports gpu_arch. |
412 | if (!Args.hasArg(options::OPT_no_cuda_version_check)) { |
413 | TC.CudaInstallation.CheckCudaVersionSupportsArch(Arch: gpu_arch); |
414 | } |
415 | |
416 | ArgStringList CmdArgs; |
417 | CmdArgs.push_back(Elt: TC.getTriple().isArch64Bit() ? "-m64": "-m32"); |
418 | DeviceDebugInfoLevel DIKind = mustEmitDebugInfo(Args); |
419 | if (DIKind == EmitSameDebugInfoAsHost) { |
420 | // ptxas does not accept -g option if optimization is enabled, so |
421 | // we ignore the compiler's -O* options if we want debug info. |
422 | CmdArgs.push_back(Elt: "-g"); |
423 | CmdArgs.push_back(Elt: "--dont-merge-basicblocks"); |
424 | CmdArgs.push_back(Elt: "--return-at-end"); |
425 | } else if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { |
426 | // Map the -O we received to -O{0,1,2,3}. |
427 | // |
428 | // TODO: Perhaps we should map host -O2 to ptxas -O3. -O3 is ptxas's |
429 | // default, so it may correspond more closely to the spirit of clang -O2. |
430 | |
431 | // -O3 seems like the least-bad option when -Osomething is specified to |
432 | // clang but it isn't handled below. |
433 | StringRef OOpt = "3"; |
434 | if (A->getOption().matches(options::ID: OPT_O4) || |
435 | A->getOption().matches(options::ID: OPT_Ofast)) |
436 | OOpt = "3"; |
437 | else if (A->getOption().matches(options::ID: OPT_O0)) |
438 | OOpt = "0"; |
439 | else if (A->getOption().matches(options::ID: OPT_O)) { |
440 | // -Os, -Oz, and -O(anything else) map to -O2, for lack of better options. |
441 | OOpt = llvm::StringSwitch<const char *>(A->getValue()) |
442 | .Case(S: "1", Value: "1") |
443 | .Case(S: "2", Value: "2") |
444 | .Case(S: "3", Value: "3") |
445 | .Case(S: "s", Value: "2") |
446 | .Case(S: "z", Value: "2") |
447 | .Default(Value: "2"); |
448 | } |
449 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: llvm::Twine("-O") + OOpt)); |
450 | } else { |
451 | // If no -O was passed, pass -O0 to ptxas -- no opt flag should correspond |
452 | // to no optimizations, but ptxas's default is -O3. |
453 | CmdArgs.push_back(Elt: "-O0"); |
454 | } |
455 | if (DIKind == DebugDirectivesOnly) |
456 | CmdArgs.push_back(Elt: "-lineinfo"); |
457 | |
458 | // Pass -v to ptxas if it was passed to the driver. |
459 | if (Args.hasArg(options::OPT_v)) |
460 | CmdArgs.push_back(Elt: "-v"); |
461 | |
462 | CmdArgs.push_back(Elt: "--gpu-name"); |
463 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: OffloadArchToString(A: gpu_arch))); |
464 | CmdArgs.push_back(Elt: "--output-file"); |
465 | std::string OutputFileName = TC.getInputFilename(Input: Output); |
466 | |
467 | if (Output.isFilename() && OutputFileName != Output.getFilename()) |
468 | C.addTempFile(Name: Args.MakeArgString(Str: OutputFileName)); |
469 | |
470 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: OutputFileName)); |
471 | for (const auto &II : Inputs) |
472 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: II.getFilename())); |
473 | |
474 | for (const auto &A : Args.getAllArgValues(options::OPT_Xcuda_ptxas)) |
475 | CmdArgs.push_back(Args.MakeArgString(A)); |
476 | |
477 | bool Relocatable; |
478 | if (JA.isOffloading(OKind: Action::OFK_OpenMP)) |
479 | // In OpenMP we need to generate relocatable code. |
480 | Relocatable = Args.hasFlag(options::OPT_fopenmp_relocatable_target, |
481 | options::OPT_fnoopenmp_relocatable_target, |
482 | /*Default=*/true); |
483 | else if (JA.isOffloading(OKind: Action::OFK_Cuda)) |
484 | // In CUDA we generate relocatable code by default. |
485 | Relocatable = Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, |
486 | /*Default=*/false); |
487 | else |
488 | // Otherwise, we are compiling directly and should create linkable output. |
489 | Relocatable = true; |
490 | |
491 | if (Relocatable) |
492 | CmdArgs.push_back(Elt: "-c"); |
493 | |
494 | const char *Exec; |
495 | if (Arg *A = Args.getLastArg(options::OPT_ptxas_path_EQ)) |
496 | Exec = A->getValue(); |
497 | else |
498 | Exec = Args.MakeArgString(Str: TC.GetProgramPath(Name: "ptxas")); |
499 | C.addCommand(C: std::make_unique<Command>( |
500 | args: JA, args: *this, |
501 | args: ResponseFileSupport{.ResponseKind: ResponseFileSupport::RF_Full, .ResponseEncoding: llvm::sys::WEM_UTF8, |
502 | .ResponseFlag: "--options-file"}, |
503 | args&: Exec, args&: CmdArgs, args: Inputs, args: Output)); |
504 | } |
505 | |
506 | static bool shouldIncludePTX(const ArgList &Args, StringRef InputArch) { |
507 | // The new driver does not include PTX by default to avoid overhead. |
508 | bool includePTX = !Args.hasFlag(options::OPT_offload_new_driver, |
509 | options::OPT_no_offload_new_driver, true); |
510 | for (Arg *A : Args.filtered(options::OPT_cuda_include_ptx_EQ, |
511 | options::OPT_no_cuda_include_ptx_EQ)) { |
512 | A->claim(); |
513 | const StringRef ArchStr = A->getValue(); |
514 | if (A->getOption().matches(options::OPT_cuda_include_ptx_EQ) && |
515 | (ArchStr == "all"|| ArchStr == InputArch)) |
516 | includePTX = true; |
517 | else if (A->getOption().matches(options::OPT_no_cuda_include_ptx_EQ) && |
518 | (ArchStr == "all"|| ArchStr == InputArch)) |
519 | includePTX = false; |
520 | } |
521 | return includePTX; |
522 | } |
523 | |
524 | // All inputs to this linker must be from CudaDeviceActions, as we need to look |
525 | // at the Inputs' Actions in order to figure out which GPU architecture they |
526 | // correspond to. |
527 | void NVPTX::FatBinary::ConstructJob(Compilation &C, const JobAction &JA, |
528 | const InputInfo &Output, |
529 | const InputInfoList &Inputs, |
530 | const ArgList &Args, |
531 | const char *LinkingOutput) const { |
532 | const auto &TC = |
533 | static_cast<const toolchains::CudaToolChain &>(getToolChain()); |
534 | assert(TC.getTriple().isNVPTX() && "Wrong platform"); |
535 | |
536 | ArgStringList CmdArgs; |
537 | if (TC.CudaInstallation.version() <= CudaVersion::CUDA_100) |
538 | CmdArgs.push_back(Elt: "--cuda"); |
539 | CmdArgs.push_back(Elt: TC.getTriple().isArch64Bit() ? "-64": "-32"); |
540 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: "--create")); |
541 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: Output.getFilename())); |
542 | if (mustEmitDebugInfo(Args) == EmitSameDebugInfoAsHost) |
543 | CmdArgs.push_back(Elt: "-g"); |
544 | |
545 | for (const auto &II : Inputs) { |
546 | auto *A = II.getAction(); |
547 | assert(A->getInputs().size() == 1 && |
548 | "Device offload action is expected to have a single input"); |
549 | const char *gpu_arch_str = A->getOffloadingArch(); |
550 | assert(gpu_arch_str && |
551 | "Device action expected to have associated a GPU architecture!"); |
552 | OffloadArch gpu_arch = StringToOffloadArch(S: gpu_arch_str); |
553 | |
554 | if (II.getType() == types::TY_PP_Asm && |
555 | !shouldIncludePTX(Args, InputArch: gpu_arch_str)) |
556 | continue; |
557 | // We need to pass an Arch of the form "sm_XX" for cubin files and |
558 | // "compute_XX" for ptx. |
559 | const char *Arch = (II.getType() == types::TY_PP_Asm) |
560 | ? OffloadArchToVirtualArchString(A: gpu_arch) |
561 | : gpu_arch_str; |
562 | CmdArgs.push_back( |
563 | Elt: Args.MakeArgString(Str: llvm::Twine("--image=profile=") + Arch + |
564 | ",file="+ getToolChain().getInputFilename(Input: II))); |
565 | } |
566 | |
567 | for (const auto &A : Args.getAllArgValues(options::OPT_Xcuda_fatbinary)) |
568 | CmdArgs.push_back(Args.MakeArgString(A)); |
569 | |
570 | const char *Exec = Args.MakeArgString(Str: TC.GetProgramPath(Name: "fatbinary")); |
571 | C.addCommand(C: std::make_unique<Command>( |
572 | args: JA, args: *this, |
573 | args: ResponseFileSupport{.ResponseKind: ResponseFileSupport::RF_Full, .ResponseEncoding: llvm::sys::WEM_UTF8, |
574 | .ResponseFlag: "--options-file"}, |
575 | args&: Exec, args&: CmdArgs, args: Inputs, args: Output)); |
576 | } |
577 | |
578 | void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA, |
579 | const InputInfo &Output, |
580 | const InputInfoList &Inputs, |
581 | const ArgList &Args, |
582 | const char *LinkingOutput) const { |
583 | const auto &TC = |
584 | static_cast<const toolchains::NVPTXToolChain &>(getToolChain()); |
585 | ArgStringList CmdArgs; |
586 | |
587 | assert(TC.getTriple().isNVPTX() && "Wrong platform"); |
588 | |
589 | assert((Output.isFilename() || Output.isNothing()) && "Invalid output."); |
590 | if (Output.isFilename()) { |
591 | CmdArgs.push_back(Elt: "-o"); |
592 | CmdArgs.push_back(Elt: Output.getFilename()); |
593 | } |
594 | |
595 | if (mustEmitDebugInfo(Args) == EmitSameDebugInfoAsHost) |
596 | CmdArgs.push_back(Elt: "-g"); |
597 | |
598 | if (Args.hasArg(options::OPT_v)) |
599 | CmdArgs.push_back(Elt: "-v"); |
600 | |
601 | StringRef GPUArch = Args.getLastArgValue(options::Id: OPT_march_EQ); |
602 | if (GPUArch.empty() && !C.getDriver().isUsingLTO()) { |
603 | C.getDriver().Diag(diag::DiagID: err_drv_offload_missing_gpu_arch) |
604 | << getToolChain().getArchName() << getShortName(); |
605 | return; |
606 | } |
607 | |
608 | if (!GPUArch.empty()) { |
609 | CmdArgs.push_back(Elt: "-arch"); |
610 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: GPUArch)); |
611 | } |
612 | |
613 | if (Args.hasArg(options::OPT_ptxas_path_EQ)) |
614 | CmdArgs.push_back(Elt: Args.MakeArgString( |
615 | Str: "--pxtas-path="+ Args.getLastArgValue(options::Id: OPT_ptxas_path_EQ))); |
616 | |
617 | if (Args.hasArg(options::OPT_cuda_path_EQ)) |
618 | CmdArgs.push_back(Elt: Args.MakeArgString( |
619 | Str: "--cuda-path="+ Args.getLastArgValue(options::Id: OPT_cuda_path_EQ))); |
620 | |
621 | // Add paths specified in LIBRARY_PATH environment variable as -L options. |
622 | addDirectoryList(Args, CmdArgs, ArgName: "-L", EnvVar: "LIBRARY_PATH"); |
623 | |
624 | // Add standard library search paths passed on the command line. |
625 | Args.AddAllArgs(Output&: CmdArgs, options::Id0: OPT_L); |
626 | getToolChain().AddFilePathLibArgs(Args, CmdArgs); |
627 | AddLinkerInputs(TC: getToolChain(), Inputs, Args, CmdArgs, JA); |
628 | |
629 | if (C.getDriver().isUsingLTO()) |
630 | addLTOOptions(ToolChain: getToolChain(), Args, CmdArgs, Output, Inputs, |
631 | IsThinLTO: C.getDriver().getLTOMode() == LTOK_Thin); |
632 | |
633 | // Forward the PTX features if the nvlink-wrapper needs it. |
634 | std::vector<StringRef> Features; |
635 | getNVPTXTargetFeatures(D: C.getDriver(), Triple: getToolChain().getTriple(), Args, |
636 | Features); |
637 | CmdArgs.push_back( |
638 | Elt: Args.MakeArgString(Str: "--plugin-opt=-mattr="+ llvm::join(R&: Features, Separator: ","))); |
639 | |
640 | // Add paths for the default clang library path. |
641 | SmallString<256> DefaultLibPath = |
642 | llvm::sys::path::parent_path(path: TC.getDriver().Dir); |
643 | llvm::sys::path::append(path&: DefaultLibPath, CLANG_INSTALL_LIBDIR_BASENAME); |
644 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine("-L") + DefaultLibPath)); |
645 | |
646 | if (Args.hasArg(options::OPT_stdlib)) |
647 | CmdArgs.append(IL: {"-lc", "-lm"}); |
648 | if (Args.hasArg(options::OPT_startfiles)) { |
649 | std::optional<std::string> IncludePath = getToolChain().getStdlibPath(); |
650 | if (!IncludePath) |
651 | IncludePath = "/lib"; |
652 | SmallString<128> P(*IncludePath); |
653 | llvm::sys::path::append(path&: P, a: "crt1.o"); |
654 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: P)); |
655 | } |
656 | |
657 | C.addCommand(C: std::make_unique<Command>( |
658 | args: JA, args: *this, |
659 | args: ResponseFileSupport{.ResponseKind: ResponseFileSupport::RF_Full, .ResponseEncoding: llvm::sys::WEM_UTF8, |
660 | .ResponseFlag: "--options-file"}, |
661 | args: Args.MakeArgString(Str: getToolChain().GetProgramPath(Name: "clang-nvlink-wrapper")), |
662 | args&: CmdArgs, args: Inputs, args: Output)); |
663 | } |
664 | |
665 | void NVPTX::getNVPTXTargetFeatures(const Driver &D, const llvm::Triple &Triple, |
666 | const llvm::opt::ArgList &Args, |
667 | std::vector<StringRef> &Features) { |
668 | if (Args.hasArg(options::OPT_cuda_feature_EQ)) { |
669 | StringRef PtxFeature = |
670 | Args.getLastArgValue(options::OPT_cuda_feature_EQ, "+ptx42"); |
671 | Features.push_back(x: Args.MakeArgString(Str: PtxFeature)); |
672 | return; |
673 | } |
674 | CudaInstallationDetector CudaInstallation(D, Triple, Args); |
675 | |
676 | // New CUDA versions often introduce new instructions that are only supported |
677 | // by new PTX version, so we need to raise PTX level to enable them in NVPTX |
678 | // back-end. |
679 | const char *PtxFeature = nullptr; |
680 | switch (CudaInstallation.version()) { |
681 | #define CASE_CUDA_VERSION(CUDA_VER, PTX_VER) \ |
682 | case CudaVersion::CUDA_##CUDA_VER: \ |
683 | PtxFeature = "+ptx" #PTX_VER; \ |
684 | break; |
685 | CASE_CUDA_VERSION(128, 87); |
686 | CASE_CUDA_VERSION(126, 85); |
687 | CASE_CUDA_VERSION(125, 85); |
688 | CASE_CUDA_VERSION(124, 84); |
689 | CASE_CUDA_VERSION(123, 83); |
690 | CASE_CUDA_VERSION(122, 82); |
691 | CASE_CUDA_VERSION(121, 81); |
692 | CASE_CUDA_VERSION(120, 80); |
693 | CASE_CUDA_VERSION(118, 78); |
694 | CASE_CUDA_VERSION(117, 77); |
695 | CASE_CUDA_VERSION(116, 76); |
696 | CASE_CUDA_VERSION(115, 75); |
697 | CASE_CUDA_VERSION(114, 74); |
698 | CASE_CUDA_VERSION(113, 73); |
699 | CASE_CUDA_VERSION(112, 72); |
700 | CASE_CUDA_VERSION(111, 71); |
701 | CASE_CUDA_VERSION(110, 70); |
702 | CASE_CUDA_VERSION(102, 65); |
703 | CASE_CUDA_VERSION(101, 64); |
704 | CASE_CUDA_VERSION(100, 63); |
705 | CASE_CUDA_VERSION(92, 61); |
706 | CASE_CUDA_VERSION(91, 61); |
707 | CASE_CUDA_VERSION(90, 60); |
708 | #undef CASE_CUDA_VERSION |
709 | // TODO: Use specific CUDA version once it's public. |
710 | case clang::CudaVersion::NEW: |
711 | PtxFeature = "+ptx86"; |
712 | break; |
713 | default: |
714 | PtxFeature = "+ptx42"; |
715 | } |
716 | Features.push_back(x: PtxFeature); |
717 | } |
718 | |
719 | /// NVPTX toolchain. Our assembler is ptxas, and our linker is nvlink. This |
720 | /// operates as a stand-alone version of the NVPTX tools without the host |
721 | /// toolchain. |
722 | NVPTXToolChain::NVPTXToolChain(const Driver &D, const llvm::Triple &Triple, |
723 | const llvm::Triple &HostTriple, |
724 | const ArgList &Args) |
725 | : ToolChain(D, Triple, Args), CudaInstallation(D, HostTriple, Args) { |
726 | if (CudaInstallation.isValid()) |
727 | getProgramPaths().push_back(Elt: std::string(CudaInstallation.getBinPath())); |
728 | // Lookup binaries into the driver directory, this is used to |
729 | // discover the 'nvptx-arch' executable. |
730 | getProgramPaths().push_back(Elt: getDriver().Dir); |
731 | } |
732 | |
733 | /// We only need the host triple to locate the CUDA binary utilities, use the |
734 | /// system's default triple if not provided. |
735 | NVPTXToolChain::NVPTXToolChain(const Driver &D, const llvm::Triple &Triple, |
736 | const ArgList &Args) |
737 | : NVPTXToolChain(D, Triple, llvm::Triple(LLVM_HOST_TRIPLE), Args) {} |
738 | |
739 | llvm::opt::DerivedArgList * |
740 | NVPTXToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args, |
741 | StringRef BoundArch, |
742 | Action::OffloadKind OffloadKind) const { |
743 | DerivedArgList *DAL = ToolChain::TranslateArgs(Args, BoundArch, DeviceOffloadKind: OffloadKind); |
744 | if (!DAL) |
745 | DAL = new DerivedArgList(Args.getBaseArgs()); |
746 | |
747 | const OptTable &Opts = getDriver().getOpts(); |
748 | |
749 | for (Arg *A : Args) |
750 | if (!llvm::is_contained(Range&: *DAL, Element: A)) |
751 | DAL->append(A); |
752 | |
753 | if (!DAL->hasArg(options::OPT_march_EQ) && OffloadKind != Action::OFK_None) { |
754 | DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ), |
755 | OffloadArchToString(OffloadArch::CudaDefault)); |
756 | } else if (DAL->getLastArgValue(options::OPT_march_EQ) == "generic"&& |
757 | OffloadKind == Action::OFK_None) { |
758 | DAL->eraseArg(options::OPT_march_EQ); |
759 | } else if (DAL->getLastArgValue(options::OPT_march_EQ) == "native") { |
760 | auto GPUsOrErr = getSystemGPUArchs(Args); |
761 | if (!GPUsOrErr) { |
762 | getDriver().Diag(diag::err_drv_undetermined_gpu_arch) |
763 | << getArchName() << llvm::toString(GPUsOrErr.takeError()) << "-march"; |
764 | } else { |
765 | if (GPUsOrErr->size() > 1) |
766 | getDriver().Diag(diag::warn_drv_multi_gpu_arch) |
767 | << getArchName() << llvm::join(*GPUsOrErr, ", ") << "-march"; |
768 | DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ), |
769 | Args.MakeArgString(GPUsOrErr->front())); |
770 | } |
771 | } |
772 | |
773 | return DAL; |
774 | } |
775 | |
776 | void NVPTXToolChain::addClangTargetOptions( |
777 | const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, |
778 | Action::OffloadKind DeviceOffloadingKind) const {} |
779 | |
780 | bool NVPTXToolChain::supportsDebugInfoOption(const llvm::opt::Arg *A) const { |
781 | const Option &O = A->getOption(); |
782 | return (O.matches(options::OPT_gN_Group) && |
783 | !O.matches(options::OPT_gmodules)) || |
784 | O.matches(options::OPT_g_Flag) || |
785 | O.matches(options::OPT_ggdbN_Group) || O.matches(options::OPT_ggdb) || |
786 | O.matches(options::OPT_gdwarf) || O.matches(options::OPT_gdwarf_2) || |
787 | O.matches(options::OPT_gdwarf_3) || O.matches(options::OPT_gdwarf_4) || |
788 | O.matches(options::OPT_gdwarf_5) || |
789 | O.matches(options::OPT_gcolumn_info); |
790 | } |
791 | |
792 | void NVPTXToolChain::adjustDebugInfoKind( |
793 | llvm::codegenoptions::DebugInfoKind &DebugInfoKind, |
794 | const ArgList &Args) const { |
795 | switch (mustEmitDebugInfo(Args)) { |
796 | case DisableDebugInfo: |
797 | DebugInfoKind = llvm::codegenoptions::NoDebugInfo; |
798 | break; |
799 | case DebugDirectivesOnly: |
800 | DebugInfoKind = llvm::codegenoptions::DebugDirectivesOnly; |
801 | break; |
802 | case EmitSameDebugInfoAsHost: |
803 | // Use same debug info level as the host. |
804 | break; |
805 | } |
806 | } |
807 | |
808 | Expected<SmallVector<std::string>> |
809 | NVPTXToolChain::getSystemGPUArchs(const ArgList &Args) const { |
810 | // Detect NVIDIA GPUs availible on the system. |
811 | std::string Program; |
812 | if (Arg *A = Args.getLastArg(options::OPT_nvptx_arch_tool_EQ)) |
813 | Program = A->getValue(); |
814 | else |
815 | Program = GetProgramPath(Name: "nvptx-arch"); |
816 | |
817 | auto StdoutOrErr = executeToolChainProgram(Executable: Program); |
818 | if (!StdoutOrErr) |
819 | return StdoutOrErr.takeError(); |
820 | |
821 | SmallVector<std::string, 1> GPUArchs; |
822 | for (StringRef Arch : llvm::split(Str: (*StdoutOrErr)->getBuffer(), Separator: "\n")) |
823 | if (!Arch.empty()) |
824 | GPUArchs.push_back(Elt: Arch.str()); |
825 | |
826 | if (GPUArchs.empty()) |
827 | return llvm::createStringError(EC: std::error_code(), |
828 | S: "No NVIDIA GPU detected in the system"); |
829 | |
830 | return std::move(GPUArchs); |
831 | } |
832 | |
833 | /// CUDA toolchain. Our assembler is ptxas, and our "linker" is fatbinary, |
834 | /// which isn't properly a linker but nonetheless performs the step of stitching |
835 | /// together object files from the assembler into a single blob. |
836 | |
837 | CudaToolChain::CudaToolChain(const Driver &D, const llvm::Triple &Triple, |
838 | const ToolChain &HostTC, const ArgList &Args) |
839 | : NVPTXToolChain(D, Triple, HostTC.getTriple(), Args), HostTC(HostTC) {} |
840 | |
841 | void CudaToolChain::addClangTargetOptions( |
842 | const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, |
843 | Action::OffloadKind DeviceOffloadingKind) const { |
844 | HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadKind: DeviceOffloadingKind); |
845 | |
846 | StringRef GpuArch = DriverArgs.getLastArgValue(options::OPT_march_EQ); |
847 | assert((DeviceOffloadingKind == Action::OFK_OpenMP || |
848 | DeviceOffloadingKind == Action::OFK_Cuda) && |
849 | "Only OpenMP or CUDA offloading kinds are supported for NVIDIA GPUs."); |
850 | |
851 | CC1Args.append(IL: {"-fcuda-is-device", "-mllvm", |
852 | "-enable-memcpyopt-without-libcalls", |
853 | "-fno-threadsafe-statics"}); |
854 | |
855 | // Unsized function arguments used for variadics were introduced in CUDA-9.0 |
856 | // We still do not support generating code that actually uses variadic |
857 | // arguments yet, but we do need to allow parsing them as recent CUDA |
858 | // headers rely on that. https://github.com/llvm/llvm-project/issues/58410 |
859 | if (CudaInstallation.version() >= CudaVersion::CUDA_90) |
860 | CC1Args.push_back(Elt: "-fcuda-allow-variadic-functions"); |
861 | |
862 | if (DriverArgs.hasFlag(options::OPT_fcuda_short_ptr, |
863 | options::OPT_fno_cuda_short_ptr, false)) |
864 | CC1Args.append(IL: {"-mllvm", "--nvptx-short-ptr"}); |
865 | |
866 | if (!DriverArgs.hasFlag(options::OPT_offloadlib, options::OPT_no_offloadlib, |
867 | true)) |
868 | return; |
869 | |
870 | if (DeviceOffloadingKind == Action::OFK_OpenMP && |
871 | DriverArgs.hasArg(options::OPT_S)) |
872 | return; |
873 | |
874 | std::string LibDeviceFile = CudaInstallation.getLibDeviceFile(Gpu: GpuArch); |
875 | if (LibDeviceFile.empty()) { |
876 | getDriver().Diag(diag::err_drv_no_cuda_libdevice) << GpuArch; |
877 | return; |
878 | } |
879 | |
880 | CC1Args.push_back(Elt: "-mlink-builtin-bitcode"); |
881 | CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: LibDeviceFile)); |
882 | |
883 | // For now, we don't use any Offload/OpenMP device runtime when we offload |
884 | // CUDA via LLVM/Offload. We should split the Offload/OpenMP device runtime |
885 | // and include the "generic" (or CUDA-specific) parts. |
886 | if (DriverArgs.hasFlag(options::OPT_foffload_via_llvm, |
887 | options::OPT_fno_offload_via_llvm, false)) |
888 | return; |
889 | |
890 | clang::CudaVersion CudaInstallationVersion = CudaInstallation.version(); |
891 | |
892 | if (CudaInstallationVersion >= CudaVersion::UNKNOWN) |
893 | CC1Args.push_back( |
894 | Elt: DriverArgs.MakeArgString(Str: Twine("-target-sdk-version=") + |
895 | CudaVersionToString(V: CudaInstallationVersion))); |
896 | |
897 | if (DeviceOffloadingKind == Action::OFK_OpenMP) { |
898 | if (CudaInstallationVersion < CudaVersion::CUDA_92) { |
899 | getDriver().Diag( |
900 | diag::err_drv_omp_offload_target_cuda_version_not_support) |
901 | << CudaVersionToString(CudaInstallationVersion); |
902 | return; |
903 | } |
904 | |
905 | // Link the bitcode library late if we're using device LTO. |
906 | if (getDriver().isUsingOffloadLTO()) |
907 | return; |
908 | |
909 | addOpenMPDeviceRTL(D: getDriver(), DriverArgs, CC1Args, BitcodeSuffix: GpuArch.str(), |
910 | Triple: getTriple(), HostTC); |
911 | } |
912 | } |
913 | |
914 | llvm::DenormalMode CudaToolChain::getDefaultDenormalModeForType( |
915 | const llvm::opt::ArgList &DriverArgs, const JobAction &JA, |
916 | const llvm::fltSemantics *FPType) const { |
917 | if (JA.getOffloadingDeviceKind() == Action::OFK_Cuda) { |
918 | if (FPType && FPType == &llvm::APFloat::IEEEsingle() && |
919 | DriverArgs.hasFlag(options::OPT_fgpu_flush_denormals_to_zero, |
920 | options::OPT_fno_gpu_flush_denormals_to_zero, false)) |
921 | return llvm::DenormalMode::getPreserveSign(); |
922 | } |
923 | |
924 | assert(JA.getOffloadingDeviceKind() != Action::OFK_Host); |
925 | return llvm::DenormalMode::getIEEE(); |
926 | } |
927 | |
928 | void CudaToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs, |
929 | ArgStringList &CC1Args) const { |
930 | // Check our CUDA version if we're going to include the CUDA headers. |
931 | if (!DriverArgs.hasArg(options::OPT_nogpuinc) && |
932 | !DriverArgs.hasArg(options::OPT_no_cuda_version_check)) { |
933 | StringRef Arch = DriverArgs.getLastArgValue(options::OPT_march_EQ); |
934 | assert(!Arch.empty() && "Must have an explicit GPU arch."); |
935 | CudaInstallation.CheckCudaVersionSupportsArch(Arch: StringToOffloadArch(S: Arch)); |
936 | } |
937 | CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args); |
938 | } |
939 | |
940 | std::string CudaToolChain::getInputFilename(const InputInfo &Input) const { |
941 | // Only object files are changed, for example assembly files keep their .s |
942 | // extensions. If the user requested device-only compilation don't change it. |
943 | if (Input.getType() != types::TY_Object || getDriver().offloadDeviceOnly()) |
944 | return ToolChain::getInputFilename(Input); |
945 | |
946 | return ToolChain::getInputFilename(Input); |
947 | } |
948 | |
949 | llvm::opt::DerivedArgList * |
950 | CudaToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args, |
951 | StringRef BoundArch, |
952 | Action::OffloadKind DeviceOffloadKind) const { |
953 | DerivedArgList *DAL = |
954 | HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind); |
955 | if (!DAL) |
956 | DAL = new DerivedArgList(Args.getBaseArgs()); |
957 | |
958 | const OptTable &Opts = getDriver().getOpts(); |
959 | |
960 | for (Arg *A : Args) { |
961 | // Make sure flags are not duplicated. |
962 | if (!llvm::is_contained(Range&: *DAL, Element: A)) { |
963 | DAL->append(A); |
964 | } |
965 | } |
966 | |
967 | if (!BoundArch.empty()) { |
968 | DAL->eraseArg(options::OPT_march_EQ); |
969 | DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ), |
970 | BoundArch); |
971 | } |
972 | return DAL; |
973 | } |
974 | |
975 | Tool *NVPTXToolChain::buildAssembler() const { |
976 | return new tools::NVPTX::Assembler(*this); |
977 | } |
978 | |
979 | Tool *NVPTXToolChain::buildLinker() const { |
980 | return new tools::NVPTX::Linker(*this); |
981 | } |
982 | |
983 | Tool *CudaToolChain::buildAssembler() const { |
984 | return new tools::NVPTX::Assembler(*this); |
985 | } |
986 | |
987 | Tool *CudaToolChain::buildLinker() const { |
988 | return new tools::NVPTX::FatBinary(*this); |
989 | } |
990 | |
991 | void CudaToolChain::addClangWarningOptions(ArgStringList &CC1Args) const { |
992 | HostTC.addClangWarningOptions(CC1Args); |
993 | } |
994 | |
995 | ToolChain::CXXStdlibType |
996 | CudaToolChain::GetCXXStdlibType(const ArgList &Args) const { |
997 | return HostTC.GetCXXStdlibType(Args); |
998 | } |
999 | |
1000 | void CudaToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
1001 | ArgStringList &CC1Args) const { |
1002 | HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args); |
1003 | |
1004 | if (!DriverArgs.hasArg(options::OPT_nogpuinc) && CudaInstallation.isValid()) |
1005 | CC1Args.append( |
1006 | IL: {"-internal-isystem", |
1007 | DriverArgs.MakeArgString(Str: CudaInstallation.getIncludePath())}); |
1008 | } |
1009 | |
1010 | void CudaToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &Args, |
1011 | ArgStringList &CC1Args) const { |
1012 | HostTC.AddClangCXXStdlibIncludeArgs(DriverArgs: Args, CC1Args); |
1013 | } |
1014 | |
1015 | void CudaToolChain::AddIAMCUIncludeArgs(const ArgList &Args, |
1016 | ArgStringList &CC1Args) const { |
1017 | HostTC.AddIAMCUIncludeArgs(DriverArgs: Args, CC1Args); |
1018 | } |
1019 | |
1020 | SanitizerMask CudaToolChain::getSupportedSanitizers() const { |
1021 | // The CudaToolChain only supports sanitizers in the sense that it allows |
1022 | // sanitizer arguments on the command line if they are supported by the host |
1023 | // toolchain. The CudaToolChain will actually ignore any command line |
1024 | // arguments for any of these "supported" sanitizers. That means that no |
1025 | // sanitization of device code is actually supported at this time. |
1026 | // |
1027 | // This behavior is necessary because the host and device toolchains |
1028 | // invocations often share the command line, so the device toolchain must |
1029 | // tolerate flags meant only for the host toolchain. |
1030 | return HostTC.getSupportedSanitizers(); |
1031 | } |
1032 | |
1033 | VersionTuple CudaToolChain::computeMSVCVersion(const Driver *D, |
1034 | const ArgList &Args) const { |
1035 | return HostTC.computeMSVCVersion(D, Args); |
1036 | } |
1037 |
Definitions
- getCudaVersion
- parseCudaHFile
- WarnIfUnsupportedVersion
- CudaInstallationDetector
- AddCudaIncludeArgs
- CheckCudaVersionSupportsArch
- DeviceDebugInfoLevel
- mustEmitDebugInfo
- ConstructJob
- shouldIncludePTX
- ConstructJob
- ConstructJob
- getNVPTXTargetFeatures
- NVPTXToolChain
- NVPTXToolChain
- TranslateArgs
- addClangTargetOptions
- supportsDebugInfoOption
- adjustDebugInfoKind
- getSystemGPUArchs
- CudaToolChain
- addClangTargetOptions
- getDefaultDenormalModeForType
- AddCudaIncludeArgs
- getInputFilename
- TranslateArgs
- buildAssembler
- buildLinker
- buildAssembler
- buildLinker
- addClangWarningOptions
- GetCXXStdlibType
- AddClangSystemIncludeArgs
- AddClangCXXStdlibIncludeArgs
- AddIAMCUIncludeArgs
- getSupportedSanitizers
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more