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/SmallSet.h"
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/Option/ArgList.h"
20#include "llvm/Support/Error.h"
21#include "llvm/Support/LineIterator.h"
22#include "llvm/Support/Path.h"
23#include "llvm/Support/Process.h"
24#include "llvm/Support/VirtualFileSystem.h"
25#include "llvm/TargetParser/Host.h"
26#include "llvm/TargetParser/TargetParser.h"
27#include <optional>
28#include <system_error>
29
30using namespace clang::driver;
31using namespace clang::driver::tools;
32using namespace clang::driver::toolchains;
33using namespace clang;
34using namespace llvm::opt;
35
36RocmInstallationDetector::CommonBitcodeLibsPreferences::
37 CommonBitcodeLibsPreferences(const Driver &D,
38 const llvm::opt::ArgList &DriverArgs,
39 StringRef GPUArch,
40 const Action::OffloadKind DeviceOffloadingKind,
41 const bool NeedsASanRT)
42 : ABIVer(DeviceLibABIVersion::fromCodeObjectVersion(
43 CodeObjectVersion: tools::getAMDGPUCodeObjectVersion(D, Args: DriverArgs))) {
44 const auto Kind = llvm::AMDGPU::parseArchAMDGCN(CPU: GPUArch);
45 const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(AK: Kind);
46
47 IsOpenMP = DeviceOffloadingKind == Action::OFK_OpenMP;
48
49 const bool HasWave32 = (ArchAttr & llvm::AMDGPU::FEATURE_WAVE32);
50 Wave64 =
51 !HasWave32 || DriverArgs.hasFlag(Pos: options::OPT_mwavefrontsize64,
52 Neg: options::OPT_mno_wavefrontsize64, Default: false);
53
54 const bool IsKnownOffloading = DeviceOffloadingKind == Action::OFK_OpenMP ||
55 DeviceOffloadingKind == Action::OFK_HIP;
56
57 // Default to enabling f32 denormals on subtargets where fma is fast with
58 // denormals
59 const bool DefaultDAZ =
60 (Kind == llvm::AMDGPU::GK_NONE)
61 ? false
62 : !((ArchAttr & llvm::AMDGPU::FEATURE_FAST_FMA_F32) &&
63 (ArchAttr & llvm::AMDGPU::FEATURE_FAST_DENORMAL_F32));
64 // TODO: There are way too many flags that change this. Do we need to
65 // check them all?
66 DAZ = IsKnownOffloading
67 ? DriverArgs.hasFlag(Pos: options::OPT_fgpu_flush_denormals_to_zero,
68 Neg: options::OPT_fno_gpu_flush_denormals_to_zero,
69 Default: DefaultDAZ)
70 : DriverArgs.hasArg(Ids: options::OPT_cl_denorms_are_zero) || DefaultDAZ;
71
72 FiniteOnly = DriverArgs.hasArg(Ids: options::OPT_cl_finite_math_only) ||
73 DriverArgs.hasFlag(Pos: options::OPT_ffinite_math_only,
74 Neg: options::OPT_fno_finite_math_only, Default: false);
75
76 UnsafeMathOpt =
77 DriverArgs.hasArg(Ids: options::OPT_cl_unsafe_math_optimizations) ||
78 DriverArgs.hasFlag(Pos: options::OPT_funsafe_math_optimizations,
79 Neg: options::OPT_fno_unsafe_math_optimizations, Default: false);
80
81 FastRelaxedMath = DriverArgs.hasArg(Ids: options::OPT_cl_fast_relaxed_math) ||
82 DriverArgs.hasFlag(Pos: options::OPT_ffast_math,
83 Neg: options::OPT_fno_fast_math, Default: false);
84
85 // GPU Sanitizer currently only supports ASan and is enabled through host
86 // ASan.
87 GPUSan = (DriverArgs.hasFlag(Pos: options::OPT_fgpu_sanitize,
88 Neg: options::OPT_fno_gpu_sanitize, Default: true) &&
89 NeedsASanRT);
90}
91
92void RocmInstallationDetector::scanLibDevicePath(llvm::StringRef Path) {
93 assert(!Path.empty());
94
95 const StringRef Suffix(".bc");
96 const StringRef Suffix2(".amdgcn.bc");
97
98 std::error_code EC;
99 for (llvm::vfs::directory_iterator LI = D.getVFS().dir_begin(Dir: Path, EC), LE;
100 !EC && LI != LE; LI = LI.increment(EC)) {
101 StringRef FilePath = LI->path();
102 StringRef FileName = llvm::sys::path::filename(path: FilePath);
103 if (!FileName.ends_with(Suffix))
104 continue;
105
106 StringRef BaseName;
107 if (FileName.ends_with(Suffix: Suffix2))
108 BaseName = FileName.drop_back(N: Suffix2.size());
109 else if (FileName.ends_with(Suffix))
110 BaseName = FileName.drop_back(N: Suffix.size());
111
112 const StringRef ABIVersionPrefix = "oclc_abi_version_";
113 if (BaseName == "ocml") {
114 OCML = FilePath;
115 } else if (BaseName == "ockl") {
116 OCKL = FilePath;
117 } else if (BaseName == "opencl") {
118 OpenCL = FilePath;
119 } else if (BaseName == "asanrtl") {
120 AsanRTL = FilePath;
121 } else if (BaseName == "oclc_finite_only_off") {
122 FiniteOnly.Off = FilePath;
123 } else if (BaseName == "oclc_finite_only_on") {
124 FiniteOnly.On = FilePath;
125 } else if (BaseName == "oclc_unsafe_math_on") {
126 UnsafeMath.On = FilePath;
127 } else if (BaseName == "oclc_unsafe_math_off") {
128 UnsafeMath.Off = FilePath;
129 } else if (BaseName == "oclc_wavefrontsize64_on") {
130 WavefrontSize64.On = FilePath;
131 } else if (BaseName == "oclc_wavefrontsize64_off") {
132 WavefrontSize64.Off = FilePath;
133 } else if (BaseName.starts_with(Prefix: ABIVersionPrefix)) {
134 unsigned ABIVersionNumber;
135 if (BaseName.drop_front(N: ABIVersionPrefix.size())
136 .getAsInteger(/*Redex=*/Radix: 0, Result&: ABIVersionNumber))
137 continue;
138 ABIVersionMap[ABIVersionNumber] = FilePath.str();
139 } else {
140 // Process all bitcode filenames that look like
141 // ocl_isa_version_XXX.amdgcn.bc
142 const StringRef DeviceLibPrefix = "oclc_isa_version_";
143 if (!BaseName.starts_with(Prefix: DeviceLibPrefix))
144 continue;
145
146 StringRef IsaVersionNumber =
147 BaseName.drop_front(N: DeviceLibPrefix.size());
148
149 llvm::Twine GfxName = Twine("gfx") + IsaVersionNumber;
150 SmallString<8> Tmp;
151 LibDeviceMap.insert(KV: {GfxName.toStringRef(Out&: Tmp), 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.empty())
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.back();
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 getToolChain().addProfileRTLibs(Args, CmdArgs);
636
637 if (Args.hasArg(Ids: options::OPT_stdlib))
638 CmdArgs.append(IL: {"-lc", "-lm"});
639 if (Args.hasArg(Ids: options::OPT_startfiles)) {
640 std::optional<std::string> IncludePath = getToolChain().getStdlibPath();
641 if (!IncludePath)
642 IncludePath = "/lib";
643 SmallString<128> P(*IncludePath);
644 llvm::sys::path::append(path&: P, a: "crt1.o");
645 CmdArgs.push_back(Elt: Args.MakeArgString(Str: P));
646 }
647
648 CmdArgs.push_back(Elt: "-o");
649 CmdArgs.push_back(Elt: Output.getFilename());
650 C.addCommand(C: std::make_unique<Command>(
651 args: JA, args: *this, args: ResponseFileSupport::AtFileCurCP(), args: Args.MakeArgString(Str: Linker),
652 args&: CmdArgs, args: Inputs, args: Output));
653}
654
655void amdgpu::getAMDGPUTargetFeatures(const Driver &D,
656 const llvm::Triple &Triple,
657 const llvm::opt::ArgList &Args,
658 std::vector<StringRef> &Features) {
659 // Add target ID features to -target-feature options. No diagnostics should
660 // be emitted here since invalid target ID is diagnosed at other places.
661 StringRef TargetID;
662 if (Args.hasArg(Ids: options::OPT_mcpu_EQ))
663 TargetID = Args.getLastArgValue(Id: options::OPT_mcpu_EQ);
664 else if (Args.hasArg(Ids: options::OPT_march_EQ))
665 TargetID = Args.getLastArgValue(Id: options::OPT_march_EQ);
666 if (!TargetID.empty()) {
667 llvm::StringMap<bool> FeatureMap;
668 auto OptionalGpuArch = parseTargetID(T: Triple, OffloadArch: TargetID, FeatureMap: &FeatureMap);
669 if (OptionalGpuArch) {
670 StringRef GpuArch = *OptionalGpuArch;
671 // Iterate through all possible target ID features for the given GPU.
672 // If it is mapped to true, add +feature.
673 // If it is mapped to false, add -feature.
674 // If it is not in the map (default), do not add it
675 for (auto &&Feature : getAllPossibleTargetIDFeatures(T: Triple, Processor: GpuArch)) {
676 auto Pos = FeatureMap.find(Key: Feature);
677 if (Pos == FeatureMap.end())
678 continue;
679 Features.push_back(x: Args.MakeArgStringRef(
680 Str: (Twine(Pos->second ? "+" : "-") + Feature).str()));
681 }
682 }
683 }
684
685 if (Args.hasFlag(Pos: options::OPT_mwavefrontsize64,
686 Neg: options::OPT_mno_wavefrontsize64, Default: false))
687 Features.push_back(x: "+wavefrontsize64");
688
689 if (Args.hasFlag(Pos: options::OPT_mamdgpu_precise_memory_op,
690 Neg: options::OPT_mno_amdgpu_precise_memory_op, Default: false))
691 Features.push_back(x: "+precise-memory");
692
693 handleTargetFeaturesGroup(D, Triple, Args, Features,
694 Group: options::OPT_m_amdgpu_Features_Group);
695}
696
697/// AMDGPU Toolchain
698AMDGPUToolChain::AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple,
699 const ArgList &Args)
700 : Generic_ELF(D, Triple, Args),
701 OptionsDefault(
702 {{options::OPT_O, "3"}, {options::OPT_cl_std_EQ, "CL1.2"}}) {
703 // Check code object version options. Emit warnings for legacy options
704 // and errors for the last invalid code object version options.
705 // It is done here to avoid repeated warning or error messages for
706 // each tool invocation.
707 checkAMDGPUCodeObjectVersion(D, Args);
708}
709
710Tool *AMDGPUToolChain::buildLinker() const {
711 return new tools::amdgpu::Linker(*this);
712}
713
714DerivedArgList *
715AMDGPUToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
716 Action::OffloadKind DeviceOffloadKind) const {
717
718 DerivedArgList *DAL =
719 Generic_ELF::TranslateArgs(Args, BoundArch, DeviceOffloadKind);
720
721 const OptTable &Opts = getDriver().getOpts();
722
723 if (!DAL)
724 DAL = new DerivedArgList(Args.getBaseArgs());
725
726 for (Arg *A : Args)
727 DAL->append(A);
728
729 // Replace -mcpu=native with detected GPU.
730 Arg *LastMCPUArg = DAL->getLastArg(Ids: options::OPT_mcpu_EQ);
731 if (LastMCPUArg && StringRef(LastMCPUArg->getValue()) == "native") {
732 DAL->eraseArg(Id: options::OPT_mcpu_EQ);
733 auto GPUsOrErr = getSystemGPUArchs(Args);
734 if (!GPUsOrErr) {
735 getDriver().Diag(DiagID: diag::err_drv_undetermined_gpu_arch)
736 << llvm::Triple::getArchTypeName(Kind: getArch())
737 << llvm::toString(E: GPUsOrErr.takeError()) << "-mcpu";
738 } else {
739 auto &GPUs = *GPUsOrErr;
740 if (llvm::SmallSet<std::string, 1>(GPUs.begin(), GPUs.end()).size() > 1)
741 getDriver().Diag(DiagID: diag::warn_drv_multi_gpu_arch)
742 << llvm::Triple::getArchTypeName(Kind: getArch())
743 << llvm::join(R&: GPUs, Separator: ", ") << "-mcpu";
744 DAL->AddJoinedArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_mcpu_EQ),
745 Value: Args.MakeArgString(Str: GPUs.front()));
746 }
747 }
748
749 checkTargetID(DriverArgs: *DAL);
750
751 if (Args.getLastArgValue(Id: options::OPT_x) != "cl")
752 return DAL;
753
754 // Phase 1 (.cl -> .bc)
755 if (Args.hasArg(Ids: options::OPT_c) && Args.hasArg(Ids: options::OPT_emit_llvm)) {
756 DAL->AddFlagArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: getTriple().isArch64Bit()
757 ? options::OPT_m64
758 : options::OPT_m32));
759
760 // Have to check OPT_O4, OPT_O0 & OPT_Ofast separately
761 // as they defined that way in Options.td
762 if (!Args.hasArg(Ids: options::OPT_O, Ids: options::OPT_O0, Ids: options::OPT_O4,
763 Ids: options::OPT_Ofast))
764 DAL->AddJoinedArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_O),
765 Value: getOptionDefault(OptID: options::OPT_O));
766 }
767
768 return DAL;
769}
770
771bool AMDGPUToolChain::getDefaultDenormsAreZeroForTarget(
772 llvm::AMDGPU::GPUKind Kind) {
773
774 // Assume nothing without a specific target.
775 if (Kind == llvm::AMDGPU::GK_NONE)
776 return false;
777
778 const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(AK: Kind);
779
780 // Default to enabling f32 denormals by default on subtargets where fma is
781 // fast with denormals
782 const bool BothDenormAndFMAFast =
783 (ArchAttr & llvm::AMDGPU::FEATURE_FAST_FMA_F32) &&
784 (ArchAttr & llvm::AMDGPU::FEATURE_FAST_DENORMAL_F32);
785 return !BothDenormAndFMAFast;
786}
787
788llvm::DenormalMode AMDGPUToolChain::getDefaultDenormalModeForType(
789 const llvm::opt::ArgList &DriverArgs, const JobAction &JA,
790 const llvm::fltSemantics *FPType) const {
791 // Denormals should always be enabled for f16 and f64.
792 if (!FPType || FPType != &llvm::APFloat::IEEEsingle())
793 return llvm::DenormalMode::getIEEE();
794
795 if (JA.getOffloadingDeviceKind() == Action::OFK_HIP ||
796 JA.getOffloadingDeviceKind() == Action::OFK_Cuda) {
797 auto Arch = getProcessorFromTargetID(T: getTriple(), OffloadArch: JA.getOffloadingArch());
798 auto Kind = llvm::AMDGPU::parseArchAMDGCN(CPU: Arch);
799 if (FPType && FPType == &llvm::APFloat::IEEEsingle() &&
800 DriverArgs.hasFlag(Pos: options::OPT_fgpu_flush_denormals_to_zero,
801 Neg: options::OPT_fno_gpu_flush_denormals_to_zero,
802 Default: getDefaultDenormsAreZeroForTarget(Kind)))
803 return llvm::DenormalMode::getPreserveSign();
804
805 return llvm::DenormalMode::getIEEE();
806 }
807
808 const StringRef GpuArch = getGPUArch(DriverArgs);
809 auto Kind = llvm::AMDGPU::parseArchAMDGCN(CPU: GpuArch);
810
811 // TODO: There are way too many flags that change this. Do we need to check
812 // them all?
813 bool DAZ = DriverArgs.hasArg(Ids: options::OPT_cl_denorms_are_zero) ||
814 getDefaultDenormsAreZeroForTarget(Kind);
815
816 // Outputs are flushed to zero (FTZ), preserving sign. Denormal inputs are
817 // also implicit treated as zero (DAZ).
818 return DAZ ? llvm::DenormalMode::getPreserveSign() :
819 llvm::DenormalMode::getIEEE();
820}
821
822bool AMDGPUToolChain::isWave64(const llvm::opt::ArgList &DriverArgs,
823 llvm::AMDGPU::GPUKind Kind) {
824 const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(AK: Kind);
825 bool HasWave32 = (ArchAttr & llvm::AMDGPU::FEATURE_WAVE32);
826
827 return !HasWave32 || DriverArgs.hasFlag(
828 Pos: options::OPT_mwavefrontsize64, Neg: options::OPT_mno_wavefrontsize64, Default: false);
829}
830
831
832/// ROCM Toolchain
833ROCMToolChain::ROCMToolChain(const Driver &D, const llvm::Triple &Triple,
834 const ArgList &Args)
835 : AMDGPUToolChain(D, Triple, Args) {
836 if (Triple.getEnvironment() != llvm::Triple::LLVM)
837 RocmInstallation->detectDeviceLibrary();
838}
839
840void AMDGPUToolChain::addClangTargetOptions(
841 const llvm::opt::ArgList &DriverArgs,
842 llvm::opt::ArgStringList &CC1Args,
843 Action::OffloadKind DeviceOffloadingKind) const {
844 // Default to "hidden" visibility, as object level linking will not be
845 // supported for the foreseeable future.
846 if (!DriverArgs.hasArg(Ids: options::OPT_fvisibility_EQ,
847 Ids: options::OPT_fvisibility_ms_compat)) {
848 CC1Args.push_back(Elt: "-fvisibility=hidden");
849 CC1Args.push_back(Elt: "-fapply-global-visibility-to-externs");
850 }
851
852 // For SPIR-V we want to retain the pristine output of Clang CodeGen, since
853 // optimizations might lose structure / information that is necessary for
854 // generating optimal concrete AMDGPU code.
855 // TODO: using the below option is a temporary placeholder until Clang
856 // provides the required functionality, which essentially boils down to
857 // -O0 being refactored / reworked to not imply optnone / remove TBAA.
858 // Once that is added, we should pivot to that functionality, being
859 // mindful to not corrupt the user provided and subsequently embedded
860 // command-line (i.e. if the user asks for -O3 this is what the
861 // finalisation should use).
862 if (getTriple().isSPIRV() &&
863 !DriverArgs.hasArg(Ids: options::OPT_disable_llvm_optzns))
864 CC1Args.push_back(Elt: "-disable-llvm-optzns");
865
866 if (DeviceOffloadingKind == Action::OFK_None)
867 addOpenCLBuiltinsLib(D: getDriver(), TT: getTriple(), DriverArgs, CC1Args);
868}
869
870void AMDGPUToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
871 // AMDGPU does not support atomic lib call. Treat atomic alignment
872 // warnings as errors.
873 CC1Args.push_back(Elt: "-Werror=atomic-alignment");
874}
875
876void AMDGPUToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
877 ArgStringList &CC1Args) const {
878 if (DriverArgs.hasArg(Ids: options::OPT_nostdinc) ||
879 DriverArgs.hasArg(Ids: options::OPT_nostdlibinc))
880 return;
881
882 if (std::optional<std::string> Path = getStdlibIncludePath())
883 addSystemInclude(DriverArgs, CC1Args, Path: *Path);
884}
885
886StringRef
887AMDGPUToolChain::getGPUArch(const llvm::opt::ArgList &DriverArgs) const {
888 return getProcessorFromTargetID(
889 T: getTriple(), OffloadArch: DriverArgs.getLastArgValue(Id: options::OPT_mcpu_EQ));
890}
891
892AMDGPUToolChain::ParsedTargetIDType
893AMDGPUToolChain::getParsedTargetID(const llvm::opt::ArgList &DriverArgs) const {
894 StringRef TargetID = DriverArgs.getLastArgValue(Id: options::OPT_mcpu_EQ);
895 if (TargetID.empty())
896 return {.OptionalTargetID: std::nullopt, .OptionalGPUArch: std::nullopt, .OptionalFeatures: std::nullopt};
897
898 llvm::StringMap<bool> FeatureMap;
899 auto OptionalGpuArch = parseTargetID(T: getTriple(), OffloadArch: TargetID, FeatureMap: &FeatureMap);
900 if (!OptionalGpuArch)
901 return {.OptionalTargetID: TargetID.str(), .OptionalGPUArch: std::nullopt, .OptionalFeatures: std::nullopt};
902
903 return {.OptionalTargetID: TargetID.str(), .OptionalGPUArch: OptionalGpuArch->str(), .OptionalFeatures: FeatureMap};
904}
905
906void AMDGPUToolChain::checkTargetID(
907 const llvm::opt::ArgList &DriverArgs) const {
908 auto PTID = getParsedTargetID(DriverArgs);
909 if (PTID.OptionalTargetID && !PTID.OptionalGPUArch) {
910 getDriver().Diag(DiagID: clang::diag::err_drv_bad_target_id)
911 << *PTID.OptionalTargetID;
912 }
913}
914
915Expected<SmallVector<std::string>>
916AMDGPUToolChain::getSystemGPUArchs(const ArgList &Args) const {
917 // Detect AMD GPUs availible on the system.
918 std::string Program;
919 if (Arg *A = Args.getLastArg(Ids: options::OPT_offload_arch_tool_EQ))
920 Program = A->getValue();
921 else
922 Program = GetProgramPath(Name: "amdgpu-arch");
923
924 auto StdoutOrErr = getDriver().executeProgram(Args: {Program});
925 if (!StdoutOrErr)
926 return StdoutOrErr.takeError();
927
928 SmallVector<std::string, 1> GPUArchs;
929 for (StringRef Arch : llvm::split(Str: (*StdoutOrErr)->getBuffer(), Separator: "\n"))
930 if (!Arch.empty())
931 GPUArchs.push_back(Elt: Arch.str());
932
933 if (GPUArchs.empty())
934 return llvm::createStringError(EC: std::error_code(),
935 S: "No AMD GPU detected in the system");
936
937 return std::move(GPUArchs);
938}
939
940void ROCMToolChain::addClangTargetOptions(
941 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
942 Action::OffloadKind DeviceOffloadingKind) const {
943 AMDGPUToolChain::addClangTargetOptions(DriverArgs, CC1Args,
944 DeviceOffloadingKind);
945
946 // For the OpenCL case where there is no offload target, accept -nostdlib to
947 // disable bitcode linking.
948 if (DeviceOffloadingKind == Action::OFK_None &&
949 DriverArgs.hasArg(Ids: options::OPT_nostdlib))
950 return;
951
952 if (!DriverArgs.hasFlag(Pos: options::OPT_offloadlib, Neg: options::OPT_no_offloadlib,
953 Default: true))
954 return;
955
956 // For SPIR-V (SPIRVAMDToolChain) we must not link any device libraries so we
957 // skip it.
958 const llvm::Triple &TT = this->getEffectiveTriple();
959 if (TT.isSPIRV())
960 return;
961
962 // With an LLVM environment, only use libraries provided by the resource
963 // directory.
964 if (TT.getEnvironment() == llvm::Triple::LLVM)
965 return;
966
967 // Get the device name and canonicalize it
968 const StringRef GpuArch = getGPUArch(DriverArgs);
969 auto Kind = llvm::AMDGPU::parseArchAMDGCN(CPU: GpuArch);
970 const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(AK: Kind);
971 StringRef LibDeviceFile = RocmInstallation->getLibDeviceFile(Gpu: CanonArch);
972 auto ABIVer = DeviceLibABIVersion::fromCodeObjectVersion(
973 CodeObjectVersion: getAMDGPUCodeObjectVersion(D: getDriver(), Args: DriverArgs));
974 if (!RocmInstallation->checkCommonBitcodeLibs(GPUArch: CanonArch, LibDeviceFile,
975 ABIVer))
976 return;
977
978 // Add the OpenCL specific bitcode library.
979 llvm::SmallVector<BitCodeLibraryInfo, 12> BCLibs;
980 BCLibs.emplace_back(Args: RocmInstallation->getOpenCLPath().str());
981
982 // Add the generic set of libraries.
983 BCLibs.append(RHS: RocmInstallation->getCommonBitcodeLibs(
984 DriverArgs, LibDeviceFile, GPUArch: GpuArch, DeviceOffloadingKind,
985 NeedsASanRT: getSanitizerArgs(JobArgs: DriverArgs).needsAsanRt()));
986
987 for (auto [BCFile, Internalize] : BCLibs) {
988 if (Internalize)
989 CC1Args.push_back(Elt: "-mlink-builtin-bitcode");
990 else
991 CC1Args.push_back(Elt: "-mlink-bitcode-file");
992 CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: BCFile));
993 }
994}
995
996bool RocmInstallationDetector::checkCommonBitcodeLibs(
997 StringRef GPUArch, StringRef LibDeviceFile,
998 DeviceLibABIVersion ABIVer) const {
999 if (!hasDeviceLibrary()) {
1000 D.Diag(DiagID: diag::err_drv_no_rocm_device_lib) << 0;
1001 return false;
1002 }
1003 if (LibDeviceFile.empty()) {
1004 D.Diag(DiagID: diag::err_drv_no_rocm_device_lib) << 1 << GPUArch;
1005 return false;
1006 }
1007 if (ABIVer.requiresLibrary() && getABIVersionPath(ABIVer).empty()) {
1008 // Starting from COV6, we will report minimum ROCm version requirement in
1009 // the error message.
1010 if (ABIVer.getAsCodeObjectVersion() < 6)
1011 D.Diag(DiagID: diag::err_drv_no_rocm_device_lib) << 2 << ABIVer.toString() << 0;
1012 else
1013 D.Diag(DiagID: diag::err_drv_no_rocm_device_lib)
1014 << 2 << ABIVer.toString() << 1 << "6.3";
1015 return false;
1016 }
1017 return true;
1018}
1019
1020llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
1021RocmInstallationDetector::getCommonBitcodeLibs(
1022 const llvm::opt::ArgList &DriverArgs, StringRef LibDeviceFile,
1023 StringRef GPUArch, const Action::OffloadKind DeviceOffloadingKind,
1024 const bool NeedsASanRT) const {
1025 llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12> BCLibs;
1026
1027 CommonBitcodeLibsPreferences Pref{D, DriverArgs, GPUArch,
1028 DeviceOffloadingKind, NeedsASanRT};
1029
1030 auto AddBCLib = [&](ToolChain::BitCodeLibraryInfo BCLib,
1031 bool Internalize = true) {
1032 if (!BCLib.Path.empty()) {
1033 BCLib.ShouldInternalize = Internalize;
1034 BCLibs.emplace_back(Args&: BCLib);
1035 }
1036 };
1037 auto AddSanBCLibs = [&]() {
1038 if (Pref.GPUSan)
1039 AddBCLib(getAsanRTLPath(), false);
1040 };
1041
1042 AddSanBCLibs();
1043 AddBCLib(getOCMLPath());
1044 if (!Pref.IsOpenMP)
1045 AddBCLib(getOCKLPath());
1046 else if (Pref.GPUSan && Pref.IsOpenMP)
1047 AddBCLib(getOCKLPath());
1048 AddBCLib(getUnsafeMathPath(Enabled: Pref.UnsafeMathOpt || Pref.FastRelaxedMath));
1049 AddBCLib(getFiniteOnlyPath(Enabled: Pref.FiniteOnly || Pref.FastRelaxedMath));
1050 AddBCLib(getWavefrontSize64Path(Enabled: Pref.Wave64));
1051 AddBCLib(LibDeviceFile);
1052 auto ABIVerPath = getABIVersionPath(ABIVer: Pref.ABIVer);
1053 if (!ABIVerPath.empty())
1054 AddBCLib(ABIVerPath);
1055
1056 return BCLibs;
1057}
1058
1059llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
1060ROCMToolChain::getCommonDeviceLibNames(
1061 const llvm::opt::ArgList &DriverArgs, const std::string &GPUArch,
1062 Action::OffloadKind DeviceOffloadingKind) const {
1063 auto Kind = llvm::AMDGPU::parseArchAMDGCN(CPU: GPUArch);
1064 const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(AK: Kind);
1065
1066 StringRef LibDeviceFile = RocmInstallation->getLibDeviceFile(Gpu: CanonArch);
1067 auto ABIVer = DeviceLibABIVersion::fromCodeObjectVersion(
1068 CodeObjectVersion: getAMDGPUCodeObjectVersion(D: getDriver(), Args: DriverArgs));
1069 if (!RocmInstallation->checkCommonBitcodeLibs(GPUArch: CanonArch, LibDeviceFile,
1070 ABIVer))
1071 return {};
1072
1073 return RocmInstallation->getCommonBitcodeLibs(
1074 DriverArgs, LibDeviceFile, GPUArch, DeviceOffloadingKind,
1075 NeedsASanRT: getSanitizerArgs(JobArgs: DriverArgs).needsAsanRt());
1076}
1077
1078bool AMDGPUToolChain::shouldSkipSanitizeOption(
1079 const ToolChain &TC, const llvm::opt::ArgList &DriverArgs,
1080 StringRef TargetID, const llvm::opt::Arg *A) const {
1081 auto &Diags = TC.getDriver().getDiags();
1082 bool IsExplicitDevice =
1083 A->getBaseArg().getOption().matches(ID: options::OPT_Xarch_device);
1084
1085 // Check 'xnack+' availability by default
1086 llvm::StringRef Processor =
1087 getProcessorFromTargetID(T: TC.getTriple(), OffloadArch: TargetID);
1088 auto ProcKind = TC.getTriple().isAMDGCN()
1089 ? llvm::AMDGPU::parseArchAMDGCN(CPU: Processor)
1090 : llvm::AMDGPU::parseArchR600(CPU: Processor);
1091 auto Features = TC.getTriple().isAMDGCN()
1092 ? llvm::AMDGPU::getArchAttrAMDGCN(AK: ProcKind)
1093 : llvm::AMDGPU::getArchAttrR600(AK: ProcKind);
1094 if (Features & llvm::AMDGPU::FEATURE_XNACK_ALWAYS)
1095 return false;
1096
1097 // Look for the xnack feature in TargetID
1098 llvm::StringMap<bool> FeatureMap;
1099 auto OptionalGpuArch = parseTargetID(T: TC.getTriple(), OffloadArch: TargetID, FeatureMap: &FeatureMap);
1100 assert(OptionalGpuArch && "Invalid Target ID");
1101 (void)OptionalGpuArch;
1102 auto Loc = FeatureMap.find(Key: "xnack");
1103 if (Loc == FeatureMap.end() || !Loc->second) {
1104 if (IsExplicitDevice) {
1105 Diags.Report(
1106 DiagID: clang::diag::err_drv_unsupported_option_for_offload_arch_req_feature)
1107 << A->getAsString(Args: DriverArgs) << TargetID << "xnack+";
1108 } else {
1109 Diags.Report(
1110 DiagID: clang::diag::warn_drv_unsupported_option_for_offload_arch_req_feature)
1111 << A->getAsString(Args: DriverArgs) << TargetID << "xnack+";
1112 }
1113 return true;
1114 }
1115
1116 return false;
1117}
1118