1//===--- AMDGPU.cpp - AMDGPU 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 "AMDGPU.h"
10#include "clang/Basic/TargetID.h"
11#include "clang/Config/config.h"
12#include "clang/Driver/CommonArgs.h"
13#include "clang/Driver/Compilation.h"
14#include "clang/Driver/InputInfo.h"
15#include "clang/Driver/SanitizerArgs.h"
16#include "clang/Options/Options.h"
17#include "llvm/ADT/StringExtras.h"
18#include "llvm/Option/ArgList.h"
19#include "llvm/Support/Error.h"
20#include "llvm/Support/LineIterator.h"
21#include "llvm/Support/Path.h"
22#include "llvm/Support/Process.h"
23#include "llvm/Support/VirtualFileSystem.h"
24#include "llvm/TargetParser/Host.h"
25#include "llvm/TargetParser/TargetParser.h"
26#include <optional>
27#include <system_error>
28
29using namespace clang::driver;
30using namespace clang::driver::tools;
31using namespace clang::driver::toolchains;
32using namespace clang;
33using namespace llvm::opt;
34
35RocmInstallationDetector::CommonBitcodeLibsPreferences::
36 CommonBitcodeLibsPreferences(const Driver &D,
37 const llvm::opt::ArgList &DriverArgs,
38 StringRef GPUArch,
39 const Action::OffloadKind DeviceOffloadingKind,
40 const bool NeedsASanRT)
41 : ABIVer(DeviceLibABIVersion::fromCodeObjectVersion(
42 CodeObjectVersion: tools::getAMDGPUCodeObjectVersion(D, Args: DriverArgs))) {
43 const auto Kind = llvm::AMDGPU::parseArchAMDGCN(CPU: GPUArch);
44 const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(AK: Kind);
45
46 IsOpenMP = DeviceOffloadingKind == Action::OFK_OpenMP;
47
48 const bool HasWave32 = (ArchAttr & llvm::AMDGPU::FEATURE_WAVE32);
49 Wave64 =
50 !HasWave32 || DriverArgs.hasFlag(Pos: options::OPT_mwavefrontsize64,
51 Neg: options::OPT_mno_wavefrontsize64, Default: false);
52
53 const bool IsKnownOffloading = DeviceOffloadingKind == Action::OFK_OpenMP ||
54 DeviceOffloadingKind == Action::OFK_HIP;
55
56 // Default to enabling f32 denormals on subtargets where fma is fast with
57 // denormals
58 const bool DefaultDAZ =
59 (Kind == llvm::AMDGPU::GK_NONE)
60 ? false
61 : !((ArchAttr & llvm::AMDGPU::FEATURE_FAST_FMA_F32) &&
62 (ArchAttr & llvm::AMDGPU::FEATURE_FAST_DENORMAL_F32));
63 // TODO: There are way too many flags that change this. Do we need to
64 // check them all?
65 DAZ = IsKnownOffloading
66 ? DriverArgs.hasFlag(Pos: options::OPT_fgpu_flush_denormals_to_zero,
67 Neg: options::OPT_fno_gpu_flush_denormals_to_zero,
68 Default: DefaultDAZ)
69 : DriverArgs.hasArg(Ids: options::OPT_cl_denorms_are_zero) || DefaultDAZ;
70
71 FiniteOnly = DriverArgs.hasArg(Ids: options::OPT_cl_finite_math_only) ||
72 DriverArgs.hasFlag(Pos: options::OPT_ffinite_math_only,
73 Neg: options::OPT_fno_finite_math_only, Default: false);
74
75 UnsafeMathOpt =
76 DriverArgs.hasArg(Ids: options::OPT_cl_unsafe_math_optimizations) ||
77 DriverArgs.hasFlag(Pos: options::OPT_funsafe_math_optimizations,
78 Neg: options::OPT_fno_unsafe_math_optimizations, Default: false);
79
80 FastRelaxedMath = DriverArgs.hasArg(Ids: options::OPT_cl_fast_relaxed_math) ||
81 DriverArgs.hasFlag(Pos: options::OPT_ffast_math,
82 Neg: options::OPT_fno_fast_math, Default: false);
83
84 // GPU Sanitizer currently only supports ASan and is enabled through host
85 // ASan.
86 GPUSan = (DriverArgs.hasFlag(Pos: options::OPT_fgpu_sanitize,
87 Neg: options::OPT_fno_gpu_sanitize, Default: true) &&
88 NeedsASanRT);
89}
90
91void RocmInstallationDetector::scanLibDevicePath(llvm::StringRef Path) {
92 assert(!Path.empty());
93
94 const StringRef Suffix(".bc");
95 const StringRef Suffix2(".amdgcn.bc");
96
97 std::error_code EC;
98 for (llvm::vfs::directory_iterator LI = D.getVFS().dir_begin(Dir: Path, EC), LE;
99 !EC && LI != LE; LI = LI.increment(EC)) {
100 StringRef FilePath = LI->path();
101 StringRef FileName = llvm::sys::path::filename(path: FilePath);
102 if (!FileName.ends_with(Suffix))
103 continue;
104
105 StringRef BaseName;
106 if (FileName.ends_with(Suffix: Suffix2))
107 BaseName = FileName.drop_back(N: Suffix2.size());
108 else if (FileName.ends_with(Suffix))
109 BaseName = FileName.drop_back(N: Suffix.size());
110
111 const StringRef ABIVersionPrefix = "oclc_abi_version_";
112 if (BaseName == "ocml") {
113 OCML = FilePath;
114 } else if (BaseName == "ockl") {
115 OCKL = FilePath;
116 } else if (BaseName == "opencl") {
117 OpenCL = FilePath;
118 } else if (BaseName == "asanrtl") {
119 AsanRTL = FilePath;
120 } else if (BaseName == "oclc_finite_only_off") {
121 FiniteOnly.Off = FilePath;
122 } else if (BaseName == "oclc_finite_only_on") {
123 FiniteOnly.On = FilePath;
124 } else if (BaseName == "oclc_unsafe_math_on") {
125 UnsafeMath.On = FilePath;
126 } else if (BaseName == "oclc_unsafe_math_off") {
127 UnsafeMath.Off = FilePath;
128 } else if (BaseName == "oclc_wavefrontsize64_on") {
129 WavefrontSize64.On = FilePath;
130 } else if (BaseName == "oclc_wavefrontsize64_off") {
131 WavefrontSize64.Off = FilePath;
132 } else if (BaseName.starts_with(Prefix: ABIVersionPrefix)) {
133 unsigned ABIVersionNumber;
134 if (BaseName.drop_front(N: ABIVersionPrefix.size())
135 .getAsInteger(/*Redex=*/Radix: 0, Result&: ABIVersionNumber))
136 continue;
137 ABIVersionMap[ABIVersionNumber] = FilePath.str();
138 } else {
139 // Process all bitcode filenames that look like
140 // ocl_isa_version_XXX.amdgcn.bc
141 const StringRef DeviceLibPrefix = "oclc_isa_version_";
142 if (!BaseName.starts_with(Prefix: DeviceLibPrefix))
143 continue;
144
145 StringRef IsaVersionNumber =
146 BaseName.drop_front(N: DeviceLibPrefix.size());
147
148 llvm::Twine GfxName = Twine("gfx") + IsaVersionNumber;
149 SmallString<8> Tmp;
150 LibDeviceMap.insert(
151 KV: std::make_pair(x: GfxName.toStringRef(Out&: Tmp), y: FilePath.str()));
152 }
153 }
154}
155
156// Parse and extract version numbers from `.hipVersion`. Return `true` if
157// the parsing fails.
158bool RocmInstallationDetector::parseHIPVersionFile(llvm::StringRef V) {
159 SmallVector<StringRef, 4> VersionParts;
160 V.split(A&: VersionParts, Separator: '\n');
161 unsigned Major = ~0U;
162 unsigned Minor = ~0U;
163 for (auto Part : VersionParts) {
164 auto Splits = Part.rtrim().split(Separator: '=');
165 if (Splits.first == "HIP_VERSION_MAJOR") {
166 if (Splits.second.getAsInteger(Radix: 0, Result&: Major))
167 return true;
168 } else if (Splits.first == "HIP_VERSION_MINOR") {
169 if (Splits.second.getAsInteger(Radix: 0, Result&: Minor))
170 return true;
171 } else if (Splits.first == "HIP_VERSION_PATCH")
172 VersionPatch = Splits.second.str();
173 }
174 if (Major == ~0U || Minor == ~0U)
175 return true;
176 VersionMajorMinor = llvm::VersionTuple(Major, Minor);
177 DetectedVersion =
178 (Twine(Major) + "." + Twine(Minor) + "." + VersionPatch).str();
179 return false;
180}
181
182/// \returns a list of candidate directories for ROCm installation, which is
183/// cached and populated only once.
184const SmallVectorImpl<RocmInstallationDetector::Candidate> &
185RocmInstallationDetector::getInstallationPathCandidates() {
186
187 // Return the cached candidate list if it has already been populated.
188 if (!ROCmSearchDirs.empty())
189 return ROCmSearchDirs;
190
191 auto DoPrintROCmSearchDirs = [&]() {
192 if (PrintROCmSearchDirs)
193 for (auto Cand : ROCmSearchDirs) {
194 llvm::errs() << "ROCm installation search path: " << Cand.Path << '\n';
195 }
196 };
197
198 // For candidate specified by --rocm-path we do not do strict check, i.e.,
199 // checking existence of HIP version file and device library files.
200 if (!RocmPathArg.empty()) {
201 ROCmSearchDirs.emplace_back(Args: RocmPathArg.str());
202 DoPrintROCmSearchDirs();
203 return ROCmSearchDirs;
204 } else if (std::optional<std::string> RocmPathEnv =
205 llvm::sys::Process::GetEnv(name: "ROCM_PATH")) {
206 if (!RocmPathEnv->empty()) {
207 ROCmSearchDirs.emplace_back(Args: std::move(*RocmPathEnv));
208 DoPrintROCmSearchDirs();
209 return ROCmSearchDirs;
210 }
211 }
212
213 // Try to find relative to the compiler binary.
214 StringRef InstallDir = D.Dir;
215
216 // Check both a normal Unix prefix position of the clang binary, as well as
217 // the Windows-esque layout the ROCm packages use with the host architecture
218 // subdirectory of bin.
219 auto DeduceROCmPath = [](StringRef ClangPath) {
220 // Strip off directory (usually bin)
221 StringRef ParentDir = llvm::sys::path::parent_path(path: ClangPath);
222 StringRef ParentName = llvm::sys::path::filename(path: ParentDir);
223
224 // Some builds use bin/{host arch}, so go up again.
225 if (ParentName == "bin") {
226 ParentDir = llvm::sys::path::parent_path(path: ParentDir);
227 ParentName = llvm::sys::path::filename(path: ParentDir);
228 }
229
230 // Some versions of the rocm llvm package install to /opt/rocm/llvm/bin
231 // Some versions of the aomp package install to /opt/rocm/aomp/bin
232 if (ParentName == "llvm" || ParentName.starts_with(Prefix: "aomp")) {
233 ParentDir = llvm::sys::path::parent_path(path: ParentDir);
234 ParentName = llvm::sys::path::filename(path: ParentDir);
235
236 // Some versions of the rocm llvm package install to
237 // /opt/rocm/lib/llvm/bin, so also back up if within the lib dir still
238 if (ParentName == "lib")
239 ParentDir = llvm::sys::path::parent_path(path: ParentDir);
240 }
241
242 return Candidate(ParentDir.str(), /*StrictChecking=*/true);
243 };
244
245 // Deduce ROCm path by the path used to invoke clang. Do not resolve symbolic
246 // link of clang itself.
247 ROCmSearchDirs.emplace_back(Args: DeduceROCmPath(InstallDir));
248
249 // Deduce ROCm path by the real path of the invoked clang, resolving symbolic
250 // link of clang itself.
251 llvm::SmallString<256> RealClangPath;
252 llvm::sys::fs::real_path(path: D.getClangProgramPath(), output&: RealClangPath);
253 auto ParentPath = llvm::sys::path::parent_path(path: RealClangPath);
254 if (ParentPath != InstallDir)
255 ROCmSearchDirs.emplace_back(Args: DeduceROCmPath(ParentPath));
256
257 // Device library may be installed in clang or resource directory.
258 auto ClangRoot = llvm::sys::path::parent_path(path: InstallDir);
259 auto RealClangRoot = llvm::sys::path::parent_path(path: ParentPath);
260 ROCmSearchDirs.emplace_back(Args: ClangRoot.str(), /*StrictChecking=*/Args: true);
261 if (RealClangRoot != ClangRoot)
262 ROCmSearchDirs.emplace_back(Args: RealClangRoot.str(), /*StrictChecking=*/Args: true);
263 ROCmSearchDirs.emplace_back(Args: D.ResourceDir,
264 /*StrictChecking=*/Args: true);
265
266 ROCmSearchDirs.emplace_back(Args: D.SysRoot + "/opt/rocm",
267 /*StrictChecking=*/Args: true);
268
269 // Find the latest /opt/rocm-{release} directory.
270 std::error_code EC;
271 std::string LatestROCm;
272 llvm::VersionTuple LatestVer;
273 // Get ROCm version from ROCm directory name.
274 auto GetROCmVersion = [](StringRef DirName) {
275 llvm::VersionTuple V;
276 std::string VerStr = DirName.drop_front(N: strlen(s: "rocm-")).str();
277 // The ROCm directory name follows the format of
278 // rocm-{major}.{minor}.{subMinor}[-{build}]
279 llvm::replace(Range&: VerStr, OldValue: '-', NewValue: '.');
280 V.tryParse(string: VerStr);
281 return V;
282 };
283 for (llvm::vfs::directory_iterator
284 File = D.getVFS().dir_begin(Dir: D.SysRoot + "/opt", EC),
285 FileEnd;
286 File != FileEnd && !EC; File.increment(EC)) {
287 llvm::StringRef FileName = llvm::sys::path::filename(path: File->path());
288 if (!FileName.starts_with(Prefix: "rocm-"))
289 continue;
290 if (LatestROCm.empty()) {
291 LatestROCm = FileName.str();
292 LatestVer = GetROCmVersion(LatestROCm);
293 continue;
294 }
295 auto Ver = GetROCmVersion(FileName);
296 if (LatestVer < Ver) {
297 LatestROCm = FileName.str();
298 LatestVer = Ver;
299 }
300 }
301 if (!LatestROCm.empty())
302 ROCmSearchDirs.emplace_back(Args: D.SysRoot + "/opt/" + LatestROCm,
303 /*StrictChecking=*/Args: true);
304
305 ROCmSearchDirs.emplace_back(Args: D.SysRoot + "/usr/local",
306 /*StrictChecking=*/Args: true);
307 ROCmSearchDirs.emplace_back(Args: D.SysRoot + "/usr",
308 /*StrictChecking=*/Args: true);
309
310 DoPrintROCmSearchDirs();
311 return ROCmSearchDirs;
312}
313
314RocmInstallationDetector::RocmInstallationDetector(
315 const Driver &D, const llvm::Triple &HostTriple,
316 const llvm::opt::ArgList &Args, bool DetectHIPRuntime)
317 : D(D) {
318 Verbose = Args.hasArg(Ids: options::OPT_v);
319 RocmPathArg = Args.getLastArgValue(Id: options::OPT_rocm_path_EQ);
320 PrintROCmSearchDirs = Args.hasArg(Ids: options::OPT_print_rocm_search_dirs);
321 RocmDeviceLibPathArg =
322 Args.getAllArgValues(Id: options::OPT_rocm_device_lib_path_EQ);
323 HIPPathArg = Args.getLastArgValue(Id: options::OPT_hip_path_EQ);
324 HIPStdParPathArg = Args.getLastArgValue(Id: options::OPT_hipstdpar_path_EQ);
325 HasHIPStdParLibrary =
326 !HIPStdParPathArg.empty() && D.getVFS().exists(Path: HIPStdParPathArg +
327 "/hipstdpar_lib.hpp");
328 HIPRocThrustPathArg =
329 Args.getLastArgValue(Id: options::OPT_hipstdpar_thrust_path_EQ);
330 HasRocThrustLibrary = !HIPRocThrustPathArg.empty() &&
331 D.getVFS().exists(Path: HIPRocThrustPathArg + "/thrust");
332 HIPRocPrimPathArg = Args.getLastArgValue(Id: options::OPT_hipstdpar_prim_path_EQ);
333 HasRocPrimLibrary = !HIPRocPrimPathArg.empty() &&
334 D.getVFS().exists(Path: HIPRocPrimPathArg + "/rocprim");
335
336 if (auto *A = Args.getLastArg(Ids: options::OPT_hip_version_EQ)) {
337 HIPVersionArg = A->getValue();
338 unsigned Major = ~0U;
339 unsigned Minor = ~0U;
340 SmallVector<StringRef, 3> Parts;
341 HIPVersionArg.split(A&: Parts, Separator: '.');
342 if (Parts.size())
343 Parts[0].getAsInteger(Radix: 0, Result&: Major);
344 if (Parts.size() > 1)
345 Parts[1].getAsInteger(Radix: 0, Result&: Minor);
346 if (Parts.size() > 2)
347 VersionPatch = Parts[2].str();
348 if (VersionPatch.empty())
349 VersionPatch = "0";
350 if (Major != ~0U && Minor == ~0U)
351 Minor = 0;
352 if (Major == ~0U || Minor == ~0U)
353 D.Diag(DiagID: diag::err_drv_invalid_value)
354 << A->getAsString(Args) << HIPVersionArg;
355
356 VersionMajorMinor = llvm::VersionTuple(Major, Minor);
357 DetectedVersion =
358 (Twine(Major) + "." + Twine(Minor) + "." + VersionPatch).str();
359 } else {
360 VersionPatch = DefaultVersionPatch;
361 VersionMajorMinor =
362 llvm::VersionTuple(DefaultVersionMajor, DefaultVersionMinor);
363 DetectedVersion = (Twine(DefaultVersionMajor) + "." +
364 Twine(DefaultVersionMinor) + "." + VersionPatch)
365 .str();
366 }
367
368 if (DetectHIPRuntime)
369 detectHIPRuntime();
370}
371
372void RocmInstallationDetector::detectDeviceLibrary() {
373 assert(LibDevicePath.empty());
374
375 if (!RocmDeviceLibPathArg.empty())
376 LibDevicePath = RocmDeviceLibPathArg[RocmDeviceLibPathArg.size() - 1];
377 else if (std::optional<std::string> LibPathEnv =
378 llvm::sys::Process::GetEnv(name: "HIP_DEVICE_LIB_PATH"))
379 LibDevicePath = std::move(*LibPathEnv);
380
381 auto &FS = D.getVFS();
382 if (!LibDevicePath.empty()) {
383 // Maintain compatability with HIP flag/envvar pointing directly at the
384 // bitcode library directory. This points directly at the library path instead
385 // of the rocm root installation.
386 if (!FS.exists(Path: LibDevicePath))
387 return;
388
389 scanLibDevicePath(Path: LibDevicePath);
390 HasDeviceLibrary = allGenericLibsValid() && !LibDeviceMap.empty();
391 return;
392 }
393
394 // Check device library exists at the given path.
395 auto CheckDeviceLib = [&](StringRef Path, bool StrictChecking) {
396 bool CheckLibDevice = (!NoBuiltinLibs || StrictChecking);
397 if (CheckLibDevice && !FS.exists(Path))
398 return false;
399
400 scanLibDevicePath(Path);
401
402 if (!NoBuiltinLibs) {
403 // Check that the required non-target libraries are all available.
404 if (!allGenericLibsValid())
405 return false;
406
407 // Check that we have found at least one libdevice that we can link in
408 // if -nobuiltinlib hasn't been specified.
409 if (LibDeviceMap.empty())
410 return false;
411 }
412 return true;
413 };
414
415 // Find device libraries in <LLVM_DIR>/lib/clang/<ver>/lib/amdgcn/bitcode
416 LibDevicePath = D.ResourceDir;
417 llvm::sys::path::append(path&: LibDevicePath, CLANG_INSTALL_LIBDIR_BASENAME,
418 b: "amdgcn", c: "bitcode");
419 HasDeviceLibrary = CheckDeviceLib(LibDevicePath, true);
420 if (HasDeviceLibrary)
421 return;
422
423 // Find device libraries in a legacy ROCm directory structure
424 // ${ROCM_ROOT}/amdgcn/bitcode/*
425 auto &ROCmDirs = getInstallationPathCandidates();
426 for (const auto &Candidate : ROCmDirs) {
427 LibDevicePath = Candidate.Path;
428 llvm::sys::path::append(path&: LibDevicePath, a: "amdgcn", b: "bitcode");
429 HasDeviceLibrary = CheckDeviceLib(LibDevicePath, Candidate.StrictChecking);
430 if (HasDeviceLibrary)
431 return;
432 }
433}
434
435void RocmInstallationDetector::detectHIPRuntime() {
436 SmallVector<Candidate, 4> HIPSearchDirs;
437 if (!HIPPathArg.empty())
438 HIPSearchDirs.emplace_back(Args: HIPPathArg.str());
439 else if (std::optional<std::string> HIPPathEnv =
440 llvm::sys::Process::GetEnv(name: "HIP_PATH")) {
441 if (!HIPPathEnv->empty())
442 HIPSearchDirs.emplace_back(Args: std::move(*HIPPathEnv));
443 }
444 if (HIPSearchDirs.empty())
445 HIPSearchDirs.append(RHS: getInstallationPathCandidates());
446 auto &FS = D.getVFS();
447
448 for (const auto &Candidate : HIPSearchDirs) {
449 InstallPath = Candidate.Path;
450 if (InstallPath.empty() || !FS.exists(Path: InstallPath))
451 continue;
452
453 BinPath = InstallPath;
454 llvm::sys::path::append(path&: BinPath, a: "bin");
455 IncludePath = InstallPath;
456 llvm::sys::path::append(path&: IncludePath, a: "include");
457 LibPath = InstallPath;
458 llvm::sys::path::append(path&: LibPath, a: "lib");
459 SharePath = InstallPath;
460 llvm::sys::path::append(path&: SharePath, a: "share");
461
462 // Get parent of InstallPath and append "share"
463 SmallString<0> ParentSharePath = llvm::sys::path::parent_path(path: InstallPath);
464 llvm::sys::path::append(path&: ParentSharePath, a: "share");
465
466 auto Append = [](SmallString<0> &path, const Twine &a, const Twine &b = "",
467 const Twine &c = "", const Twine &d = "") {
468 SmallString<0> newpath = path;
469 llvm::sys::path::append(path&: newpath, a, b, c, d);
470 return newpath;
471 };
472 // If HIP version file can be found and parsed, use HIP version from there.
473 std::vector<SmallString<0>> VersionFilePaths = {
474 Append(SharePath, "hip", "version"),
475 InstallPath != D.SysRoot + "/usr/local"
476 ? Append(ParentSharePath, "hip", "version")
477 : SmallString<0>(),
478 Append(BinPath, ".hipVersion")};
479
480 for (const auto &VersionFilePath : VersionFilePaths) {
481 if (VersionFilePath.empty())
482 continue;
483 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> VersionFile =
484 FS.getBufferForFile(Name: VersionFilePath);
485 if (!VersionFile)
486 continue;
487 if (HIPVersionArg.empty() && VersionFile)
488 if (parseHIPVersionFile(V: (*VersionFile)->getBuffer()))
489 continue;
490
491 HasHIPRuntime = true;
492 return;
493 }
494 // Otherwise, if -rocm-path is specified (no strict checking), use the
495 // default HIP version or specified by --hip-version.
496 if (!Candidate.StrictChecking) {
497 HasHIPRuntime = true;
498 return;
499 }
500 }
501 HasHIPRuntime = false;
502}
503
504void RocmInstallationDetector::print(raw_ostream &OS) const {
505 if (hasHIPRuntime())
506 OS << "Found HIP installation: " << InstallPath << ", version "
507 << DetectedVersion << '\n';
508}
509
510void RocmInstallationDetector::AddHIPIncludeArgs(const ArgList &DriverArgs,
511 ArgStringList &CC1Args) const {
512 bool UsesRuntimeWrapper = VersionMajorMinor > llvm::VersionTuple(3, 5) &&
513 !DriverArgs.hasArg(Ids: options::OPT_nohipwrapperinc);
514 bool HasHipStdPar = DriverArgs.hasArg(Ids: options::OPT_hipstdpar);
515
516 if (!DriverArgs.hasArg(Ids: options::OPT_nobuiltininc)) {
517 // HIP header includes standard library wrapper headers under clang
518 // cuda_wrappers directory. Since these wrapper headers include_next
519 // standard C++ headers, whereas libc++ headers include_next other clang
520 // headers. The include paths have to follow this order:
521 // - wrapper include path
522 // - standard C++ include path
523 // - other clang include path
524 // Since standard C++ and other clang include paths are added in other
525 // places after this function, here we only need to make sure wrapper
526 // include path is added.
527 //
528 // ROCm 3.5 does not fully support the wrapper headers. Therefore it needs
529 // a workaround.
530 SmallString<128> P(D.ResourceDir);
531 if (UsesRuntimeWrapper)
532 llvm::sys::path::append(path&: P, a: "include", b: "cuda_wrappers");
533 CC1Args.push_back(Elt: "-internal-isystem");
534 CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: P));
535 }
536
537 const auto HandleHipStdPar = [=, &DriverArgs, &CC1Args]() {
538 StringRef Inc = getIncludePath();
539 auto &FS = D.getVFS();
540
541 if (!hasHIPStdParLibrary())
542 if (!HIPStdParPathArg.empty() ||
543 !FS.exists(Path: Inc + "/thrust/system/hip/hipstdpar/hipstdpar_lib.hpp")) {
544 D.Diag(DiagID: diag::err_drv_no_hipstdpar_lib);
545 return;
546 }
547 if (!HasRocThrustLibrary && !FS.exists(Path: Inc + "/thrust")) {
548 D.Diag(DiagID: diag::err_drv_no_hipstdpar_thrust_lib);
549 return;
550 }
551 if (!HasRocPrimLibrary && !FS.exists(Path: Inc + "/rocprim")) {
552 D.Diag(DiagID: diag::err_drv_no_hipstdpar_prim_lib);
553 return;
554 }
555 const char *ThrustPath;
556 if (HasRocThrustLibrary)
557 ThrustPath = DriverArgs.MakeArgString(Str: HIPRocThrustPathArg);
558 else
559 ThrustPath = DriverArgs.MakeArgString(Str: Inc + "/thrust");
560
561 const char *HIPStdParPath;
562 if (hasHIPStdParLibrary())
563 HIPStdParPath = DriverArgs.MakeArgString(Str: HIPStdParPathArg);
564 else
565 HIPStdParPath = DriverArgs.MakeArgString(Str: StringRef(ThrustPath) +
566 "/system/hip/hipstdpar");
567
568 const char *PrimPath;
569 if (HasRocPrimLibrary)
570 PrimPath = DriverArgs.MakeArgString(Str: HIPRocPrimPathArg);
571 else
572 PrimPath = DriverArgs.MakeArgString(Str: getIncludePath() + "/rocprim");
573
574 CC1Args.append(IL: {"-idirafter", ThrustPath, "-idirafter", PrimPath,
575 "-idirafter", HIPStdParPath, "-include",
576 "hipstdpar_lib.hpp"});
577 };
578
579 if (!DriverArgs.hasFlag(Pos: options::OPT_offload_inc, Neg: options::OPT_no_offload_inc,
580 Default: true)) {
581 if (HasHipStdPar)
582 HandleHipStdPar();
583
584 return;
585 }
586
587 if (!hasHIPRuntime()) {
588 D.Diag(DiagID: diag::err_drv_no_hip_runtime);
589 return;
590 }
591
592 CC1Args.push_back(Elt: "-idirafter");
593 CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: getIncludePath()));
594 if (UsesRuntimeWrapper)
595 CC1Args.append(IL: {"-include", "__clang_hip_runtime_wrapper.h"});
596 if (HasHipStdPar)
597 HandleHipStdPar();
598}
599
600void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
601 const InputInfo &Output,
602 const InputInfoList &Inputs,
603 const ArgList &Args,
604 const char *LinkingOutput) const {
605 std::string Linker = getToolChain().GetLinkerPath();
606 ArgStringList CmdArgs;
607 if (!Args.hasArg(Ids: options::OPT_r)) {
608 CmdArgs.push_back(Elt: "--no-undefined");
609 CmdArgs.push_back(Elt: "-shared");
610 }
611
612 if (C.getDriver().isUsingLTO()) {
613 const bool ThinLTO = (C.getDriver().getLTOMode() == LTOK_Thin);
614 addLTOOptions(ToolChain: getToolChain(), Args, CmdArgs, Output, Inputs, IsThinLTO: ThinLTO);
615 } else if (Args.hasArg(Ids: options::OPT_mcpu_EQ)) {
616 CmdArgs.push_back(Elt: Args.MakeArgString(
617 Str: "-plugin-opt=mcpu=" +
618 getProcessorFromTargetID(T: getToolChain().getTriple(),
619 OffloadArch: Args.getLastArgValue(Id: options::OPT_mcpu_EQ))));
620 }
621 addLinkerCompressDebugSectionsOption(TC: getToolChain(), Args, CmdArgs);
622 getToolChain().AddFilePathLibArgs(Args, CmdArgs);
623 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_L);
624 AddLinkerInputs(TC: getToolChain(), Inputs, Args, CmdArgs, JA);
625
626 // Always pass the target-id features to the LTO job.
627 std::vector<StringRef> Features;
628 getAMDGPUTargetFeatures(D: C.getDriver(), Triple: getToolChain().getTriple(), Args,
629 Features);
630 if (!Features.empty()) {
631 CmdArgs.push_back(
632 Elt: Args.MakeArgString(Str: "-plugin-opt=-mattr=" + llvm::join(R&: Features, Separator: ",")));
633 }
634
635 if (Args.hasArg(Ids: options::OPT_stdlib))
636 CmdArgs.append(IL: {"-lc", "-lm"});
637 if (Args.hasArg(Ids: options::OPT_startfiles)) {
638 std::optional<std::string> IncludePath = getToolChain().getStdlibPath();
639 if (!IncludePath)
640 IncludePath = "/lib";
641 SmallString<128> P(*IncludePath);
642 llvm::sys::path::append(path&: P, a: "crt1.o");
643 CmdArgs.push_back(Elt: Args.MakeArgString(Str: P));
644 }
645
646 CmdArgs.push_back(Elt: "-o");
647 CmdArgs.push_back(Elt: Output.getFilename());
648 C.addCommand(C: std::make_unique<Command>(
649 args: JA, args: *this, args: ResponseFileSupport::AtFileCurCP(), args: Args.MakeArgString(Str: Linker),
650 args&: CmdArgs, args: Inputs, args: Output));
651}
652
653void amdgpu::getAMDGPUTargetFeatures(const Driver &D,
654 const llvm::Triple &Triple,
655 const llvm::opt::ArgList &Args,
656 std::vector<StringRef> &Features) {
657 // Add target ID features to -target-feature options. No diagnostics should
658 // be emitted here since invalid target ID is diagnosed at other places.
659 StringRef TargetID;
660 if (Args.hasArg(Ids: options::OPT_mcpu_EQ))
661 TargetID = Args.getLastArgValue(Id: options::OPT_mcpu_EQ);
662 else if (Args.hasArg(Ids: options::OPT_march_EQ))
663 TargetID = Args.getLastArgValue(Id: options::OPT_march_EQ);
664 if (!TargetID.empty()) {
665 llvm::StringMap<bool> FeatureMap;
666 auto OptionalGpuArch = parseTargetID(T: Triple, OffloadArch: TargetID, FeatureMap: &FeatureMap);
667 if (OptionalGpuArch) {
668 StringRef GpuArch = *OptionalGpuArch;
669 // Iterate through all possible target ID features for the given GPU.
670 // If it is mapped to true, add +feature.
671 // If it is mapped to false, add -feature.
672 // If it is not in the map (default), do not add it
673 for (auto &&Feature : getAllPossibleTargetIDFeatures(T: Triple, Processor: GpuArch)) {
674 auto Pos = FeatureMap.find(Key: Feature);
675 if (Pos == FeatureMap.end())
676 continue;
677 Features.push_back(x: Args.MakeArgStringRef(
678 Str: (Twine(Pos->second ? "+" : "-") + Feature).str()));
679 }
680 }
681 }
682
683 if (Args.hasFlag(Pos: options::OPT_mwavefrontsize64,
684 Neg: options::OPT_mno_wavefrontsize64, Default: false))
685 Features.push_back(x: "+wavefrontsize64");
686
687 if (Args.hasFlag(Pos: options::OPT_mamdgpu_precise_memory_op,
688 Neg: options::OPT_mno_amdgpu_precise_memory_op, Default: false))
689 Features.push_back(x: "+precise-memory");
690
691 handleTargetFeaturesGroup(D, Triple, Args, Features,
692 Group: options::OPT_m_amdgpu_Features_Group);
693}
694
695/// AMDGPU Toolchain
696AMDGPUToolChain::AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple,
697 const ArgList &Args)
698 : Generic_ELF(D, Triple, Args),
699 OptionsDefault(
700 {{options::OPT_O, "3"}, {options::OPT_cl_std_EQ, "CL1.2"}}) {
701 // Check code object version options. Emit warnings for legacy options
702 // and errors for the last invalid code object version options.
703 // It is done here to avoid repeated warning or error messages for
704 // each tool invocation.
705 checkAMDGPUCodeObjectVersion(D, Args);
706}
707
708Tool *AMDGPUToolChain::buildLinker() const {
709 return new tools::amdgpu::Linker(*this);
710}
711
712DerivedArgList *
713AMDGPUToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
714 Action::OffloadKind DeviceOffloadKind) const {
715
716 DerivedArgList *DAL =
717 Generic_ELF::TranslateArgs(Args, BoundArch, DeviceOffloadKind);
718
719 const OptTable &Opts = getDriver().getOpts();
720
721 if (!DAL)
722 DAL = new DerivedArgList(Args.getBaseArgs());
723
724 for (Arg *A : Args)
725 DAL->append(A);
726
727 // Replace -mcpu=native with detected GPU.
728 Arg *LastMCPUArg = DAL->getLastArg(Ids: options::OPT_mcpu_EQ);
729 if (LastMCPUArg && StringRef(LastMCPUArg->getValue()) == "native") {
730 DAL->eraseArg(Id: options::OPT_mcpu_EQ);
731 auto GPUsOrErr = getSystemGPUArchs(Args);
732 if (!GPUsOrErr) {
733 getDriver().Diag(DiagID: diag::err_drv_undetermined_gpu_arch)
734 << llvm::Triple::getArchTypeName(Kind: getArch())
735 << llvm::toString(E: GPUsOrErr.takeError()) << "-mcpu";
736 } else {
737 auto &GPUs = *GPUsOrErr;
738 if (GPUs.size() > 1) {
739 getDriver().Diag(DiagID: diag::warn_drv_multi_gpu_arch)
740 << llvm::Triple::getArchTypeName(Kind: getArch())
741 << llvm::join(R&: GPUs, Separator: ", ") << "-mcpu";
742 }
743 DAL->AddJoinedArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_mcpu_EQ),
744 Value: Args.MakeArgString(Str: GPUs.front()));
745 }
746 }
747
748 checkTargetID(DriverArgs: *DAL);
749
750 if (Args.getLastArgValue(Id: options::OPT_x) != "cl")
751 return DAL;
752
753 // Phase 1 (.cl -> .bc)
754 if (Args.hasArg(Ids: options::OPT_c) && Args.hasArg(Ids: options::OPT_emit_llvm)) {
755 DAL->AddFlagArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: getTriple().isArch64Bit()
756 ? options::OPT_m64
757 : options::OPT_m32));
758
759 // Have to check OPT_O4, OPT_O0 & OPT_Ofast separately
760 // as they defined that way in Options.td
761 if (!Args.hasArg(Ids: options::OPT_O, Ids: options::OPT_O0, Ids: options::OPT_O4,
762 Ids: options::OPT_Ofast))
763 DAL->AddJoinedArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_O),
764 Value: getOptionDefault(OptID: options::OPT_O));
765 }
766
767 return DAL;
768}
769
770bool AMDGPUToolChain::getDefaultDenormsAreZeroForTarget(
771 llvm::AMDGPU::GPUKind Kind) {
772
773 // Assume nothing without a specific target.
774 if (Kind == llvm::AMDGPU::GK_NONE)
775 return false;
776
777 const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(AK: Kind);
778
779 // Default to enabling f32 denormals by default on subtargets where fma is
780 // fast with denormals
781 const bool BothDenormAndFMAFast =
782 (ArchAttr & llvm::AMDGPU::FEATURE_FAST_FMA_F32) &&
783 (ArchAttr & llvm::AMDGPU::FEATURE_FAST_DENORMAL_F32);
784 return !BothDenormAndFMAFast;
785}
786
787llvm::DenormalMode AMDGPUToolChain::getDefaultDenormalModeForType(
788 const llvm::opt::ArgList &DriverArgs, const JobAction &JA,
789 const llvm::fltSemantics *FPType) const {
790 // Denormals should always be enabled for f16 and f64.
791 if (!FPType || FPType != &llvm::APFloat::IEEEsingle())
792 return llvm::DenormalMode::getIEEE();
793
794 if (JA.getOffloadingDeviceKind() == Action::OFK_HIP ||
795 JA.getOffloadingDeviceKind() == Action::OFK_Cuda) {
796 auto Arch = getProcessorFromTargetID(T: getTriple(), OffloadArch: JA.getOffloadingArch());
797 auto Kind = llvm::AMDGPU::parseArchAMDGCN(CPU: Arch);
798 if (FPType && FPType == &llvm::APFloat::IEEEsingle() &&
799 DriverArgs.hasFlag(Pos: options::OPT_fgpu_flush_denormals_to_zero,
800 Neg: options::OPT_fno_gpu_flush_denormals_to_zero,
801 Default: getDefaultDenormsAreZeroForTarget(Kind)))
802 return llvm::DenormalMode::getPreserveSign();
803
804 return llvm::DenormalMode::getIEEE();
805 }
806
807 const StringRef GpuArch = getGPUArch(DriverArgs);
808 auto Kind = llvm::AMDGPU::parseArchAMDGCN(CPU: GpuArch);
809
810 // TODO: There are way too many flags that change this. Do we need to check
811 // them all?
812 bool DAZ = DriverArgs.hasArg(Ids: options::OPT_cl_denorms_are_zero) ||
813 getDefaultDenormsAreZeroForTarget(Kind);
814
815 // Outputs are flushed to zero (FTZ), preserving sign. Denormal inputs are
816 // also implicit treated as zero (DAZ).
817 return DAZ ? llvm::DenormalMode::getPreserveSign() :
818 llvm::DenormalMode::getIEEE();
819}
820
821bool AMDGPUToolChain::isWave64(const llvm::opt::ArgList &DriverArgs,
822 llvm::AMDGPU::GPUKind Kind) {
823 const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(AK: Kind);
824 bool HasWave32 = (ArchAttr & llvm::AMDGPU::FEATURE_WAVE32);
825
826 return !HasWave32 || DriverArgs.hasFlag(
827 Pos: options::OPT_mwavefrontsize64, Neg: options::OPT_mno_wavefrontsize64, Default: false);
828}
829
830
831/// ROCM Toolchain
832ROCMToolChain::ROCMToolChain(const Driver &D, const llvm::Triple &Triple,
833 const ArgList &Args)
834 : AMDGPUToolChain(D, Triple, Args) {
835 if (Triple.getEnvironment() != llvm::Triple::LLVM)
836 RocmInstallation->detectDeviceLibrary();
837}
838
839void AMDGPUToolChain::addClangTargetOptions(
840 const llvm::opt::ArgList &DriverArgs,
841 llvm::opt::ArgStringList &CC1Args,
842 Action::OffloadKind DeviceOffloadingKind) const {
843 // Default to "hidden" visibility, as object level linking will not be
844 // supported for the foreseeable future.
845 if (!DriverArgs.hasArg(Ids: options::OPT_fvisibility_EQ,
846 Ids: options::OPT_fvisibility_ms_compat)) {
847 CC1Args.push_back(Elt: "-fvisibility=hidden");
848 CC1Args.push_back(Elt: "-fapply-global-visibility-to-externs");
849 }
850
851 // For SPIR-V we want to retain the pristine output of Clang CodeGen, since
852 // optimizations might lose structure / information that is necessary for
853 // generating optimal concrete AMDGPU code.
854 // TODO: using the below option is a temporary placeholder until Clang
855 // provides the required functionality, which essentially boils down to
856 // -O0 being refactored / reworked to not imply optnone / remove TBAA.
857 // Once that is added, we should pivot to that functionality, being
858 // mindful to not corrupt the user provided and subsequently embedded
859 // command-line (i.e. if the user asks for -O3 this is what the
860 // finalisation should use).
861 if (getTriple().isSPIRV() &&
862 !DriverArgs.hasArg(Ids: options::OPT_disable_llvm_optzns))
863 CC1Args.push_back(Elt: "-disable-llvm-optzns");
864
865 if (DeviceOffloadingKind == Action::OFK_None)
866 addOpenCLBuiltinsLib(D: getDriver(), TT: getTriple(), DriverArgs, CC1Args);
867}
868
869void AMDGPUToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
870 // AMDGPU does not support atomic lib call. Treat atomic alignment
871 // warnings as errors.
872 CC1Args.push_back(Elt: "-Werror=atomic-alignment");
873}
874
875void AMDGPUToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
876 ArgStringList &CC1Args) const {
877 if (DriverArgs.hasArg(Ids: options::OPT_nostdinc) ||
878 DriverArgs.hasArg(Ids: options::OPT_nostdlibinc))
879 return;
880
881 if (std::optional<std::string> Path = getStdlibIncludePath())
882 addSystemInclude(DriverArgs, CC1Args, Path: *Path);
883}
884
885StringRef
886AMDGPUToolChain::getGPUArch(const llvm::opt::ArgList &DriverArgs) const {
887 return getProcessorFromTargetID(
888 T: getTriple(), OffloadArch: DriverArgs.getLastArgValue(Id: options::OPT_mcpu_EQ));
889}
890
891AMDGPUToolChain::ParsedTargetIDType
892AMDGPUToolChain::getParsedTargetID(const llvm::opt::ArgList &DriverArgs) const {
893 StringRef TargetID = DriverArgs.getLastArgValue(Id: options::OPT_mcpu_EQ);
894 if (TargetID.empty())
895 return {.OptionalTargetID: std::nullopt, .OptionalGPUArch: std::nullopt, .OptionalFeatures: std::nullopt};
896
897 llvm::StringMap<bool> FeatureMap;
898 auto OptionalGpuArch = parseTargetID(T: getTriple(), OffloadArch: TargetID, FeatureMap: &FeatureMap);
899 if (!OptionalGpuArch)
900 return {.OptionalTargetID: TargetID.str(), .OptionalGPUArch: std::nullopt, .OptionalFeatures: std::nullopt};
901
902 return {.OptionalTargetID: TargetID.str(), .OptionalGPUArch: OptionalGpuArch->str(), .OptionalFeatures: FeatureMap};
903}
904
905void AMDGPUToolChain::checkTargetID(
906 const llvm::opt::ArgList &DriverArgs) const {
907 auto PTID = getParsedTargetID(DriverArgs);
908 if (PTID.OptionalTargetID && !PTID.OptionalGPUArch) {
909 getDriver().Diag(DiagID: clang::diag::err_drv_bad_target_id)
910 << *PTID.OptionalTargetID;
911 }
912}
913
914Expected<SmallVector<std::string>>
915AMDGPUToolChain::getSystemGPUArchs(const ArgList &Args) const {
916 // Detect AMD GPUs availible on the system.
917 std::string Program;
918 if (Arg *A = Args.getLastArg(Ids: options::OPT_offload_arch_tool_EQ))
919 Program = A->getValue();
920 else
921 Program = GetProgramPath(Name: "amdgpu-arch");
922
923 auto StdoutOrErr = getDriver().executeProgram(Args: {Program});
924 if (!StdoutOrErr)
925 return StdoutOrErr.takeError();
926
927 SmallVector<std::string, 1> GPUArchs;
928 for (StringRef Arch : llvm::split(Str: (*StdoutOrErr)->getBuffer(), Separator: "\n"))
929 if (!Arch.empty())
930 GPUArchs.push_back(Elt: Arch.str());
931
932 if (GPUArchs.empty())
933 return llvm::createStringError(EC: std::error_code(),
934 S: "No AMD GPU detected in the system");
935
936 return std::move(GPUArchs);
937}
938
939void ROCMToolChain::addClangTargetOptions(
940 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
941 Action::OffloadKind DeviceOffloadingKind) const {
942 AMDGPUToolChain::addClangTargetOptions(DriverArgs, CC1Args,
943 DeviceOffloadingKind);
944
945 // For the OpenCL case where there is no offload target, accept -nostdlib to
946 // disable bitcode linking.
947 if (DeviceOffloadingKind == Action::OFK_None &&
948 DriverArgs.hasArg(Ids: options::OPT_nostdlib))
949 return;
950
951 if (!DriverArgs.hasFlag(Pos: options::OPT_offloadlib, Neg: options::OPT_no_offloadlib,
952 Default: true))
953 return;
954
955 // For SPIR-V (SPIRVAMDToolChain) we must not link any device libraries so we
956 // skip it.
957 const llvm::Triple &TT = this->getEffectiveTriple();
958 if (TT.isSPIRV())
959 return;
960
961 // With an LLVM environment, only use libraries provided by the resource
962 // directory.
963 if (TT.getEnvironment() == llvm::Triple::LLVM)
964 return;
965
966 // Get the device name and canonicalize it
967 const StringRef GpuArch = getGPUArch(DriverArgs);
968 auto Kind = llvm::AMDGPU::parseArchAMDGCN(CPU: GpuArch);
969 const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(AK: Kind);
970 StringRef LibDeviceFile = RocmInstallation->getLibDeviceFile(Gpu: CanonArch);
971 auto ABIVer = DeviceLibABIVersion::fromCodeObjectVersion(
972 CodeObjectVersion: getAMDGPUCodeObjectVersion(D: getDriver(), Args: DriverArgs));
973 if (!RocmInstallation->checkCommonBitcodeLibs(GPUArch: CanonArch, LibDeviceFile,
974 ABIVer))
975 return;
976
977 // Add the OpenCL specific bitcode library.
978 llvm::SmallVector<BitCodeLibraryInfo, 12> BCLibs;
979 BCLibs.emplace_back(Args: RocmInstallation->getOpenCLPath().str());
980
981 // Add the generic set of libraries.
982 BCLibs.append(RHS: RocmInstallation->getCommonBitcodeLibs(
983 DriverArgs, LibDeviceFile, GPUArch: GpuArch, DeviceOffloadingKind,
984 NeedsASanRT: getSanitizerArgs(JobArgs: DriverArgs).needsAsanRt()));
985
986 for (auto [BCFile, Internalize] : BCLibs) {
987 if (Internalize)
988 CC1Args.push_back(Elt: "-mlink-builtin-bitcode");
989 else
990 CC1Args.push_back(Elt: "-mlink-bitcode-file");
991 CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: BCFile));
992 }
993}
994
995bool RocmInstallationDetector::checkCommonBitcodeLibs(
996 StringRef GPUArch, StringRef LibDeviceFile,
997 DeviceLibABIVersion ABIVer) const {
998 if (!hasDeviceLibrary()) {
999 D.Diag(DiagID: diag::err_drv_no_rocm_device_lib) << 0;
1000 return false;
1001 }
1002 if (LibDeviceFile.empty()) {
1003 D.Diag(DiagID: diag::err_drv_no_rocm_device_lib) << 1 << GPUArch;
1004 return false;
1005 }
1006 if (ABIVer.requiresLibrary() && getABIVersionPath(ABIVer).empty()) {
1007 // Starting from COV6, we will report minimum ROCm version requirement in
1008 // the error message.
1009 if (ABIVer.getAsCodeObjectVersion() < 6)
1010 D.Diag(DiagID: diag::err_drv_no_rocm_device_lib) << 2 << ABIVer.toString() << 0;
1011 else
1012 D.Diag(DiagID: diag::err_drv_no_rocm_device_lib)
1013 << 2 << ABIVer.toString() << 1 << "6.3";
1014 return false;
1015 }
1016 return true;
1017}
1018
1019llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
1020RocmInstallationDetector::getCommonBitcodeLibs(
1021 const llvm::opt::ArgList &DriverArgs, StringRef LibDeviceFile,
1022 StringRef GPUArch, const Action::OffloadKind DeviceOffloadingKind,
1023 const bool NeedsASanRT) const {
1024 llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12> BCLibs;
1025
1026 CommonBitcodeLibsPreferences Pref{D, DriverArgs, GPUArch,
1027 DeviceOffloadingKind, NeedsASanRT};
1028
1029 auto AddBCLib = [&](ToolChain::BitCodeLibraryInfo BCLib,
1030 bool Internalize = true) {
1031 if (!BCLib.Path.empty()) {
1032 BCLib.ShouldInternalize = Internalize;
1033 BCLibs.emplace_back(Args&: BCLib);
1034 }
1035 };
1036 auto AddSanBCLibs = [&]() {
1037 if (Pref.GPUSan)
1038 AddBCLib(getAsanRTLPath(), false);
1039 };
1040
1041 AddSanBCLibs();
1042 AddBCLib(getOCMLPath());
1043 if (!Pref.IsOpenMP)
1044 AddBCLib(getOCKLPath());
1045 else if (Pref.GPUSan && Pref.IsOpenMP)
1046 AddBCLib(getOCKLPath(), false);
1047 AddBCLib(getUnsafeMathPath(Enabled: Pref.UnsafeMathOpt || Pref.FastRelaxedMath));
1048 AddBCLib(getFiniteOnlyPath(Enabled: Pref.FiniteOnly || Pref.FastRelaxedMath));
1049 AddBCLib(getWavefrontSize64Path(Enabled: Pref.Wave64));
1050 AddBCLib(LibDeviceFile);
1051 auto ABIVerPath = getABIVersionPath(ABIVer: Pref.ABIVer);
1052 if (!ABIVerPath.empty())
1053 AddBCLib(ABIVerPath);
1054
1055 return BCLibs;
1056}
1057
1058llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
1059ROCMToolChain::getCommonDeviceLibNames(
1060 const llvm::opt::ArgList &DriverArgs, const std::string &GPUArch,
1061 Action::OffloadKind DeviceOffloadingKind) const {
1062 auto Kind = llvm::AMDGPU::parseArchAMDGCN(CPU: GPUArch);
1063 const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(AK: Kind);
1064
1065 StringRef LibDeviceFile = RocmInstallation->getLibDeviceFile(Gpu: CanonArch);
1066 auto ABIVer = DeviceLibABIVersion::fromCodeObjectVersion(
1067 CodeObjectVersion: getAMDGPUCodeObjectVersion(D: getDriver(), Args: DriverArgs));
1068 if (!RocmInstallation->checkCommonBitcodeLibs(GPUArch: CanonArch, LibDeviceFile,
1069 ABIVer))
1070 return {};
1071
1072 return RocmInstallation->getCommonBitcodeLibs(
1073 DriverArgs, LibDeviceFile, GPUArch, DeviceOffloadingKind,
1074 NeedsASanRT: getSanitizerArgs(JobArgs: DriverArgs).needsAsanRt());
1075}
1076
1077bool AMDGPUToolChain::shouldSkipSanitizeOption(
1078 const ToolChain &TC, const llvm::opt::ArgList &DriverArgs,
1079 StringRef TargetID, const llvm::opt::Arg *A) const {
1080 auto &Diags = TC.getDriver().getDiags();
1081 bool IsExplicitDevice =
1082 A->getBaseArg().getOption().matches(ID: options::OPT_Xarch_device);
1083
1084 // Check 'xnack+' availability by default
1085 llvm::StringRef Processor =
1086 getProcessorFromTargetID(T: TC.getTriple(), OffloadArch: TargetID);
1087 auto ProcKind = TC.getTriple().isAMDGCN()
1088 ? llvm::AMDGPU::parseArchAMDGCN(CPU: Processor)
1089 : llvm::AMDGPU::parseArchR600(CPU: Processor);
1090 auto Features = TC.getTriple().isAMDGCN()
1091 ? llvm::AMDGPU::getArchAttrAMDGCN(AK: ProcKind)
1092 : llvm::AMDGPU::getArchAttrR600(AK: ProcKind);
1093 if (Features & llvm::AMDGPU::FEATURE_XNACK_ALWAYS)
1094 return false;
1095
1096 // Look for the xnack feature in TargetID
1097 llvm::StringMap<bool> FeatureMap;
1098 auto OptionalGpuArch = parseTargetID(T: TC.getTriple(), OffloadArch: TargetID, FeatureMap: &FeatureMap);
1099 assert(OptionalGpuArch && "Invalid Target ID");
1100 (void)OptionalGpuArch;
1101 auto Loc = FeatureMap.find(Key: "xnack");
1102 if (Loc == FeatureMap.end() || !Loc->second) {
1103 if (IsExplicitDevice) {
1104 Diags.Report(
1105 DiagID: clang::diag::err_drv_unsupported_option_for_offload_arch_req_feature)
1106 << A->getAsString(Args: DriverArgs) << TargetID << "xnack+";
1107 } else {
1108 Diags.Report(
1109 DiagID: clang::diag::warn_drv_unsupported_option_for_offload_arch_req_feature)
1110 << A->getAsString(Args: DriverArgs) << TargetID << "xnack+";
1111 }
1112 return true;
1113 }
1114
1115 return false;
1116}
1117