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