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 "HIPAMD.h"
11#include "clang/Basic/TargetID.h"
12#include "clang/Config/config.h"
13#include "clang/Driver/CommonArgs.h"
14#include "clang/Driver/Compilation.h"
15#include "clang/Driver/Driver.h"
16#include "clang/Driver/InputInfo.h"
17#include "clang/Driver/SanitizerArgs.h"
18#include "clang/Options/Options.h"
19#include "llvm/ADT/SmallSet.h"
20#include "llvm/ADT/StringExtras.h"
21#include "llvm/Option/ArgList.h"
22#include "llvm/Support/Error.h"
23#include "llvm/Support/LineIterator.h"
24#include "llvm/Support/Path.h"
25#include "llvm/Support/Process.h"
26#include "llvm/Support/VirtualFileSystem.h"
27#include "llvm/TargetParser/AMDGPUTargetParser.h"
28#include "llvm/TargetParser/Host.h"
29#include <optional>
30#include <system_error>
31
32using namespace clang::driver;
33using namespace clang::driver::tools;
34using namespace clang::driver::toolchains;
35using namespace clang;
36using namespace llvm::opt;
37
38RocmInstallationDetector::CommonBitcodeLibsPreferences::
39 CommonBitcodeLibsPreferences(const Driver &D,
40 const llvm::opt::ArgList &DriverArgs,
41 StringRef GPUArch,
42 const Action::OffloadKind DeviceOffloadingKind,
43 const bool NeedsASanRT)
44 : ABIVer(DeviceLibABIVersion::fromCodeObjectVersion(
45 CodeObjectVersion: tools::getAMDGPUCodeObjectVersion(D, Args: DriverArgs))) {
46 const auto Kind = llvm::AMDGPU::parseArchAMDGCN(CPU: GPUArch);
47 const llvm::AMDGPU::AMDGPUFeatureBitset &Features =
48 llvm::AMDGPU::getFeatureBitset(AK: Kind);
49
50 IsOpenMP = DeviceOffloadingKind == Action::OFK_OpenMP;
51
52 const bool HasWave32 = Features.test(I: llvm::AMDGPU::FEAT_SUPPORTS_WAVE32);
53 Wave64 =
54 !HasWave32 || DriverArgs.hasFlag(Pos: options::OPT_mwavefrontsize64,
55 Neg: options::OPT_mno_wavefrontsize64, Default: false);
56
57 const bool IsKnownOffloading = DeviceOffloadingKind == Action::OFK_OpenMP ||
58 DeviceOffloadingKind == Action::OFK_HIP;
59
60 // Default to enabling f32 denormals on subtargets where fma is fast with
61 // denormals
62 const bool DefaultDAZ =
63 (Kind == llvm::AMDGPU::GK_NONE)
64 ? false
65 : !(Features.test(I: llvm::AMDGPU::FEAT_FAST_FMAF) &&
66 Features.test(I: llvm::AMDGPU::FEAT_FAST_DENORMAL_F32));
67 // TODO: There are way too many flags that change this. Do we need to
68 // check them all?
69 DAZ = IsKnownOffloading
70 ? DriverArgs.hasFlag(Pos: options::OPT_fgpu_flush_denormals_to_zero,
71 Neg: options::OPT_fno_gpu_flush_denormals_to_zero,
72 Default: DefaultDAZ)
73 : DriverArgs.hasArg(Ids: options::OPT_cl_denorms_are_zero) || DefaultDAZ;
74
75 FiniteOnly = DriverArgs.hasArg(Ids: options::OPT_cl_finite_math_only) ||
76 DriverArgs.hasFlag(Pos: options::OPT_ffinite_math_only,
77 Neg: options::OPT_fno_finite_math_only, Default: false);
78
79 UnsafeMathOpt =
80 DriverArgs.hasArg(Ids: options::OPT_cl_unsafe_math_optimizations) ||
81 DriverArgs.hasFlag(Pos: options::OPT_funsafe_math_optimizations,
82 Neg: options::OPT_fno_unsafe_math_optimizations, Default: false);
83
84 FastRelaxedMath = DriverArgs.hasArg(Ids: options::OPT_cl_fast_relaxed_math) ||
85 DriverArgs.hasFlag(Pos: options::OPT_ffast_math,
86 Neg: options::OPT_fno_fast_math, Default: false);
87
88 // GPU Sanitizer currently only supports ASan and is enabled through host
89 // ASan.
90 GPUSan = (DriverArgs.hasFlag(Pos: options::OPT_fgpu_sanitize,
91 Neg: options::OPT_fno_gpu_sanitize, Default: true) &&
92 NeedsASanRT);
93}
94
95void RocmInstallationDetector::scanLibDevicePath(llvm::StringRef Path) {
96 assert(!Path.empty());
97
98 const StringRef Suffix(".bc");
99 const StringRef Suffix2(".amdgcn.bc");
100
101 std::error_code EC;
102 for (llvm::vfs::directory_iterator LI = D.getVFS().dir_begin(Dir: Path, EC), LE;
103 !EC && LI != LE; LI = LI.increment(EC)) {
104 StringRef FilePath = LI->path();
105 StringRef FileName = llvm::sys::path::filename(path: FilePath);
106 if (!FileName.ends_with(Suffix))
107 continue;
108
109 StringRef BaseName;
110 if (FileName.ends_with(Suffix: Suffix2))
111 BaseName = FileName.drop_back(N: Suffix2.size());
112 else if (FileName.ends_with(Suffix))
113 BaseName = FileName.drop_back(N: Suffix.size());
114
115 const StringRef ABIVersionPrefix = "oclc_abi_version_";
116 if (BaseName == "ocml") {
117 OCML = FilePath;
118 } else if (BaseName == "ockl") {
119 OCKL = FilePath;
120 } else if (BaseName == "opencl") {
121 OpenCL = FilePath;
122 } else if (BaseName == "asanrtl") {
123 AsanRTL = FilePath;
124 } else if (BaseName == "oclc_finite_only_off") {
125 FiniteOnly.Off = FilePath;
126 } else if (BaseName == "oclc_finite_only_on") {
127 FiniteOnly.On = FilePath;
128 } else if (BaseName == "oclc_unsafe_math_on") {
129 UnsafeMath.On = FilePath;
130 } else if (BaseName == "oclc_unsafe_math_off") {
131 UnsafeMath.Off = FilePath;
132 } else if (BaseName == "oclc_wavefrontsize64_on") {
133 WavefrontSize64.On = FilePath;
134 } else if (BaseName == "oclc_wavefrontsize64_off") {
135 WavefrontSize64.Off = FilePath;
136 } else if (BaseName.starts_with(Prefix: ABIVersionPrefix)) {
137 unsigned ABIVersionNumber;
138 if (BaseName.drop_front(N: ABIVersionPrefix.size())
139 .getAsInteger(/*Redex=*/Radix: 0, Result&: ABIVersionNumber))
140 continue;
141 ABIVersionMap[ABIVersionNumber] = FilePath.str();
142 } else {
143 // Process all bitcode filenames that look like
144 // ocl_isa_version_XXX.amdgcn.bc
145 const StringRef DeviceLibPrefix = "oclc_isa_version_";
146 if (!BaseName.starts_with(Prefix: DeviceLibPrefix))
147 continue;
148
149 StringRef IsaVersionNumber =
150 BaseName.drop_front(N: DeviceLibPrefix.size());
151
152 llvm::Twine GfxName = Twine("gfx") + IsaVersionNumber;
153 SmallString<8> Tmp;
154 LibDeviceMap.insert(KV: {GfxName.toStringRef(Out&: Tmp), FilePath.str()});
155 }
156 }
157}
158
159// Parse and extract version numbers from `.hipVersion`. Return `true` if
160// the parsing fails.
161bool RocmInstallationDetector::parseHIPVersionFile(llvm::StringRef V) {
162 SmallVector<StringRef, 4> VersionParts;
163 V.split(A&: VersionParts, Separator: '\n');
164 unsigned Major = ~0U;
165 unsigned Minor = ~0U;
166 for (auto Part : VersionParts) {
167 auto Splits = Part.rtrim().split(Separator: '=');
168 if (Splits.first == "HIP_VERSION_MAJOR") {
169 if (Splits.second.getAsInteger(Radix: 0, Result&: Major))
170 return true;
171 } else if (Splits.first == "HIP_VERSION_MINOR") {
172 if (Splits.second.getAsInteger(Radix: 0, Result&: Minor))
173 return true;
174 } else if (Splits.first == "HIP_VERSION_PATCH")
175 VersionPatch = Splits.second.str();
176 }
177 if (Major == ~0U || Minor == ~0U)
178 return true;
179 VersionMajorMinor = llvm::VersionTuple(Major, Minor);
180 DetectedVersion =
181 (Twine(Major) + "." + Twine(Minor) + "." + VersionPatch).str();
182 return false;
183}
184
185/// \returns a list of candidate directories for ROCm installation, which is
186/// cached and populated only once.
187const SmallVectorImpl<RocmInstallationDetector::Candidate> &
188RocmInstallationDetector::getInstallationPathCandidates() {
189
190 // Return the cached candidate list if it has already been populated.
191 if (!ROCmSearchDirs.empty())
192 return ROCmSearchDirs;
193
194 auto DoPrintROCmSearchDirs = [&]() {
195 if (PrintROCmSearchDirs)
196 for (auto Cand : ROCmSearchDirs) {
197 llvm::errs() << "ROCm installation search path: " << Cand.Path << '\n';
198 }
199 };
200
201 // For candidate specified by --rocm-path we do not do strict check, i.e.,
202 // checking existence of HIP version file and device library files.
203 if (!RocmPathArg.empty()) {
204 ROCmSearchDirs.emplace_back(Args: RocmPathArg.str());
205 DoPrintROCmSearchDirs();
206 return ROCmSearchDirs;
207 } else if (std::optional<std::string> RocmPathEnv =
208 llvm::sys::Process::GetEnv(name: "ROCM_PATH")) {
209 if (!RocmPathEnv->empty()) {
210 ROCmSearchDirs.emplace_back(Args: std::move(*RocmPathEnv));
211 DoPrintROCmSearchDirs();
212 return ROCmSearchDirs;
213 }
214 }
215
216 // Try to find relative to the compiler binary.
217 StringRef InstallDir = D.Dir;
218
219 // Check both a normal Unix prefix position of the clang binary, as well as
220 // the Windows-esque layout the ROCm packages use with the host architecture
221 // subdirectory of bin.
222 auto DeduceROCmPath = [](StringRef ClangPath) {
223 // Strip off directory (usually bin)
224 StringRef ParentDir = llvm::sys::path::parent_path(path: ClangPath);
225 StringRef ParentName = llvm::sys::path::filename(path: ParentDir);
226
227 // Some builds use bin/{host arch}, so go up again.
228 if (ParentName == "bin") {
229 ParentDir = llvm::sys::path::parent_path(path: ParentDir);
230 ParentName = llvm::sys::path::filename(path: ParentDir);
231 }
232
233 // Some versions of the rocm llvm package install to /opt/rocm/llvm/bin
234 // Some versions of the aomp package install to /opt/rocm/aomp/bin
235 if (ParentName == "llvm" || ParentName.starts_with(Prefix: "aomp")) {
236 ParentDir = llvm::sys::path::parent_path(path: ParentDir);
237 ParentName = llvm::sys::path::filename(path: ParentDir);
238
239 // Some versions of the rocm llvm package install to
240 // /opt/rocm/lib/llvm/bin, so also back up if within the lib dir still
241 if (ParentName == "lib")
242 ParentDir = llvm::sys::path::parent_path(path: ParentDir);
243 }
244
245 return Candidate(ParentDir.str(), /*StrictChecking=*/true);
246 };
247
248 // Deduce ROCm path by the path used to invoke clang. Do not resolve symbolic
249 // link of clang itself.
250 ROCmSearchDirs.emplace_back(Args: DeduceROCmPath(InstallDir));
251
252 // Deduce ROCm path by the real path of the invoked clang, resolving symbolic
253 // link of clang itself.
254 llvm::SmallString<256> RealClangPath;
255 llvm::sys::fs::real_path(path: D.getDriverProgramPath(), output&: RealClangPath);
256 auto ParentPath = llvm::sys::path::parent_path(path: RealClangPath);
257 if (ParentPath != InstallDir)
258 ROCmSearchDirs.emplace_back(Args: DeduceROCmPath(ParentPath));
259
260 // Device library may be installed in clang or resource directory.
261 auto ClangRoot = llvm::sys::path::parent_path(path: InstallDir);
262 auto RealClangRoot = llvm::sys::path::parent_path(path: ParentPath);
263 ROCmSearchDirs.emplace_back(Args: ClangRoot.str(), /*StrictChecking=*/Args: true);
264 if (RealClangRoot != ClangRoot)
265 ROCmSearchDirs.emplace_back(Args: RealClangRoot.str(), /*StrictChecking=*/Args: true);
266 ROCmSearchDirs.emplace_back(Args: D.ResourceDir,
267 /*StrictChecking=*/Args: true);
268
269 ROCmSearchDirs.emplace_back(Args: D.SysRoot + "/opt/rocm",
270 /*StrictChecking=*/Args: true);
271
272 // Find the latest /opt/rocm-{release} directory.
273 std::error_code EC;
274 std::string LatestROCm;
275 llvm::VersionTuple LatestVer;
276 // Get ROCm version from ROCm directory name.
277 auto GetROCmVersion = [](StringRef DirName) {
278 llvm::VersionTuple V;
279 std::string VerStr = DirName.drop_front(N: strlen(s: "rocm-")).str();
280 // The ROCm directory name follows the format of
281 // rocm-{major}.{minor}.{subMinor}[-{build}]
282 llvm::replace(Range&: VerStr, OldValue: '-', NewValue: '.');
283 V.tryParse(string: VerStr);
284 return V;
285 };
286 for (llvm::vfs::directory_iterator
287 File = D.getVFS().dir_begin(Dir: D.SysRoot + "/opt", EC),
288 FileEnd;
289 File != FileEnd && !EC; File.increment(EC)) {
290 llvm::StringRef FileName = llvm::sys::path::filename(path: File->path());
291 if (!FileName.starts_with(Prefix: "rocm-"))
292 continue;
293 if (LatestROCm.empty()) {
294 LatestROCm = FileName.str();
295 LatestVer = GetROCmVersion(LatestROCm);
296 continue;
297 }
298 auto Ver = GetROCmVersion(FileName);
299 if (LatestVer < Ver) {
300 LatestROCm = FileName.str();
301 LatestVer = Ver;
302 }
303 }
304 if (!LatestROCm.empty())
305 ROCmSearchDirs.emplace_back(Args: D.SysRoot + "/opt/" + LatestROCm,
306 /*StrictChecking=*/Args: true);
307
308 ROCmSearchDirs.emplace_back(Args: D.SysRoot + "/usr/local",
309 /*StrictChecking=*/Args: true);
310 ROCmSearchDirs.emplace_back(Args: D.SysRoot + "/usr",
311 /*StrictChecking=*/Args: true);
312
313 DoPrintROCmSearchDirs();
314 return ROCmSearchDirs;
315}
316
317RocmInstallationDetector::RocmInstallationDetector(
318 const Driver &D, const llvm::Triple &HostTriple,
319 const llvm::opt::ArgList &Args, bool DetectHIPRuntime)
320 : D(D) {
321 Verbose = Args.hasArg(Ids: options::OPT_v);
322 RocmPathArg = Args.getLastArgValue(Id: options::OPT_rocm_path_EQ);
323 PrintROCmSearchDirs = Args.hasArg(Ids: options::OPT_print_rocm_search_dirs);
324 RocmDeviceLibPathArg =
325 Args.getAllArgValues(Id: options::OPT_rocm_device_lib_path_EQ);
326 HIPPathArg = Args.getLastArgValue(Id: options::OPT_hip_path_EQ);
327 HIPStdParPathArg = Args.getLastArgValue(Id: options::OPT_hipstdpar_path_EQ);
328 HasHIPStdParLibrary =
329 !HIPStdParPathArg.empty() && D.getVFS().exists(Path: HIPStdParPathArg +
330 "/hipstdpar_lib.hpp");
331 HIPRocThrustPathArg =
332 Args.getLastArgValue(Id: options::OPT_hipstdpar_thrust_path_EQ);
333 HasRocThrustLibrary = !HIPRocThrustPathArg.empty() &&
334 D.getVFS().exists(Path: HIPRocThrustPathArg + "/thrust");
335 HIPRocPrimPathArg = Args.getLastArgValue(Id: options::OPT_hipstdpar_prim_path_EQ);
336 HasRocPrimLibrary = !HIPRocPrimPathArg.empty() &&
337 D.getVFS().exists(Path: HIPRocPrimPathArg + "/rocprim");
338
339 if (auto *A = Args.getLastArg(Ids: options::OPT_hip_version_EQ)) {
340 HIPVersionArg = A->getValue();
341 unsigned Major = ~0U;
342 unsigned Minor = ~0U;
343 SmallVector<StringRef, 3> Parts;
344 HIPVersionArg.split(A&: Parts, Separator: '.');
345 if (!Parts.empty())
346 Parts[0].getAsInteger(Radix: 0, Result&: Major);
347 if (Parts.size() > 1)
348 Parts[1].getAsInteger(Radix: 0, Result&: Minor);
349 if (Parts.size() > 2)
350 VersionPatch = Parts[2].str();
351 if (VersionPatch.empty())
352 VersionPatch = "0";
353 if (Major != ~0U && Minor == ~0U)
354 Minor = 0;
355 if (Major == ~0U || Minor == ~0U)
356 D.Diag(DiagID: diag::err_drv_invalid_value)
357 << A->getAsString(Args) << HIPVersionArg;
358
359 VersionMajorMinor = llvm::VersionTuple(Major, Minor);
360 DetectedVersion =
361 (Twine(Major) + "." + Twine(Minor) + "." + VersionPatch).str();
362 } else {
363 VersionPatch = DefaultVersionPatch;
364 VersionMajorMinor =
365 llvm::VersionTuple(DefaultVersionMajor, DefaultVersionMinor);
366 DetectedVersion = (Twine(DefaultVersionMajor) + "." +
367 Twine(DefaultVersionMinor) + "." + VersionPatch)
368 .str();
369 }
370
371 if (DetectHIPRuntime)
372 detectHIPRuntime(HostTriple);
373}
374
375void RocmInstallationDetector::detectDeviceLibrary() {
376 assert(LibDevicePath.empty());
377
378 if (!RocmDeviceLibPathArg.empty())
379 LibDevicePath = RocmDeviceLibPathArg.back();
380 else if (std::optional<std::string> LibPathEnv =
381 llvm::sys::Process::GetEnv(name: "HIP_DEVICE_LIB_PATH"))
382 LibDevicePath = std::move(*LibPathEnv);
383
384 auto &FS = D.getVFS();
385 if (!LibDevicePath.empty()) {
386 // Maintain compatability with HIP flag/envvar pointing directly at the
387 // bitcode library directory. This points directly at the library path instead
388 // of the rocm root installation.
389 if (!FS.exists(Path: LibDevicePath))
390 return;
391
392 scanLibDevicePath(Path: LibDevicePath);
393 HasDeviceLibrary = allGenericLibsValid() && !LibDeviceMap.empty();
394 return;
395 }
396
397 // Check device library exists at the given path.
398 auto CheckDeviceLib = [&](StringRef Path, bool StrictChecking) {
399 bool CheckLibDevice = (!NoBuiltinLibs || StrictChecking);
400 if (CheckLibDevice && !FS.exists(Path))
401 return false;
402
403 scanLibDevicePath(Path);
404
405 if (!NoBuiltinLibs) {
406 // Check that the required non-target libraries are all available.
407 if (!allGenericLibsValid())
408 return false;
409
410 // Check that we have found at least one libdevice that we can link in
411 // if -nobuiltinlib hasn't been specified.
412 if (LibDeviceMap.empty())
413 return false;
414 }
415 return true;
416 };
417
418 // Find device libraries in <LLVM_DIR>/lib/clang/<ver>/lib/amdgcn/bitcode
419 LibDevicePath = D.ResourceDir;
420 llvm::sys::path::append(path&: LibDevicePath, CLANG_INSTALL_LIBDIR_BASENAME,
421 b: "amdgcn", c: "bitcode");
422 HasDeviceLibrary = CheckDeviceLib(LibDevicePath, true);
423 if (HasDeviceLibrary)
424 return;
425
426 // Find device libraries in a legacy ROCm directory structure
427 // ${ROCM_ROOT}/amdgcn/bitcode/*
428 auto &ROCmDirs = getInstallationPathCandidates();
429 for (const auto &Candidate : ROCmDirs) {
430 LibDevicePath = Candidate.Path;
431 llvm::sys::path::append(path&: LibDevicePath, a: "amdgcn", b: "bitcode");
432 HasDeviceLibrary = CheckDeviceLib(LibDevicePath, Candidate.StrictChecking);
433 if (HasDeviceLibrary)
434 return;
435 }
436}
437
438void RocmInstallationDetector::detectHIPRuntime(
439 const llvm::Triple &HostTriple) {
440 SmallVector<Candidate, 4> HIPSearchDirs;
441 if (!HIPPathArg.empty())
442 HIPSearchDirs.emplace_back(Args: HIPPathArg.str());
443 else if (std::optional<std::string> HIPPathEnv =
444 llvm::sys::Process::GetEnv(name: "HIP_PATH")) {
445 if (!HIPPathEnv->empty())
446 HIPSearchDirs.emplace_back(Args: std::move(*HIPPathEnv));
447 }
448 if (HIPSearchDirs.empty())
449 HIPSearchDirs.append(RHS: getInstallationPathCandidates());
450 auto &FS = D.getVFS();
451
452 for (const auto &Candidate : HIPSearchDirs) {
453 InstallPath = Candidate.Path;
454 if (InstallPath.empty() || !FS.exists(Path: InstallPath))
455 continue;
456
457 BinPath = InstallPath;
458 llvm::sys::path::append(path&: BinPath, a: "bin");
459 IncludePath = InstallPath;
460 llvm::sys::path::append(path&: IncludePath, a: "include");
461
462 // ROCm's lib path is the place where the amdhsa64 library is located.
463 // Probe for it and fallback to /rocm/lib if we cannot find it.
464 StringRef LibAmdHip64 =
465 HostTriple.isOSMSVCRT() ? "amdhip64.lib" : "libamdhip64.so";
466 LibPath.clear();
467 for (StringRef LibPathSuffix : {"lib", "lib64"}) {
468 SmallString<0> LibAmdHip64Location;
469 llvm::sys::path::append(path&: LibAmdHip64Location, a: InstallPath, b: LibPathSuffix,
470 c: LibAmdHip64);
471 if (FS.exists(Path: LibAmdHip64Location)) {
472 llvm::sys::path::append(path&: LibPath, a: InstallPath, b: LibPathSuffix);
473 break;
474 }
475 }
476
477 if (LibPath.empty())
478 llvm::sys::path::append(path&: LibPath, a: InstallPath, b: "lib");
479
480 SharePath = InstallPath;
481 llvm::sys::path::append(path&: SharePath, a: "share");
482
483 // Get parent of InstallPath and append "share"
484 SmallString<0> ParentSharePath = llvm::sys::path::parent_path(path: InstallPath);
485 llvm::sys::path::append(path&: ParentSharePath, a: "share");
486
487 auto Append = [](SmallString<0> &path, const Twine &a, const Twine &b = "",
488 const Twine &c = "", const Twine &d = "") {
489 SmallString<0> newpath = path;
490 llvm::sys::path::append(path&: newpath, a, b, c, d);
491 return newpath;
492 };
493 // If HIP version file can be found and parsed, use HIP version from there.
494 std::vector<SmallString<0>> VersionFilePaths = {
495 Append(SharePath, "hip", "version"),
496 InstallPath != D.SysRoot + "/usr/local"
497 ? Append(ParentSharePath, "hip", "version")
498 : SmallString<0>(),
499 Append(BinPath, ".hipVersion")};
500
501 for (const auto &VersionFilePath : VersionFilePaths) {
502 if (VersionFilePath.empty())
503 continue;
504 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> VersionFile =
505 FS.getBufferForFile(Name: VersionFilePath);
506 if (!VersionFile)
507 continue;
508 if (HIPVersionArg.empty() && VersionFile)
509 if (parseHIPVersionFile(V: (*VersionFile)->getBuffer()))
510 continue;
511
512 HasHIPRuntime = true;
513 return;
514 }
515 // Otherwise, if -rocm-path is specified (no strict checking), use the
516 // default HIP version or specified by --hip-version.
517 if (!Candidate.StrictChecking) {
518 HasHIPRuntime = true;
519 return;
520 }
521 }
522 HasHIPRuntime = false;
523}
524
525void RocmInstallationDetector::print(raw_ostream &OS) const {
526 if (hasHIPRuntime())
527 OS << "Found HIP installation: " << InstallPath << ", version "
528 << DetectedVersion << '\n';
529}
530
531void RocmInstallationDetector::AddHIPIncludeArgs(const ArgList &DriverArgs,
532 ArgStringList &CC1Args) const {
533 bool UsesRuntimeWrapper = VersionMajorMinor > llvm::VersionTuple(3, 5) &&
534 !DriverArgs.hasArg(Ids: options::OPT_nohipwrapperinc);
535 bool HasHipStdPar = DriverArgs.hasArg(Ids: options::OPT_hipstdpar);
536
537 if (DriverArgs.hasFlag(Pos: options::OPT_foffload_via_llvm,
538 Neg: options::OPT_fno_offload_via_llvm, Default: false))
539 return;
540
541 if (!DriverArgs.hasArg(Ids: options::OPT_nobuiltininc)) {
542 // HIP header includes standard library wrapper headers under clang
543 // cuda_wrappers directory. Since these wrapper headers include_next
544 // standard C++ headers, whereas libc++ headers include_next other clang
545 // headers. The include paths have to follow this order:
546 // - wrapper include path
547 // - standard C++ include path
548 // - other clang include path
549 // Since standard C++ and other clang include paths are added in other
550 // places after this function, here we only need to make sure wrapper
551 // include path is added.
552 //
553 // ROCm 3.5 does not fully support the wrapper headers. Therefore it needs
554 // a workaround.
555 SmallString<128> P(D.ResourceDir);
556 if (UsesRuntimeWrapper)
557 llvm::sys::path::append(path&: P, a: "include", b: "cuda_wrappers");
558 CC1Args.push_back(Elt: "-internal-isystem");
559 CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: P));
560 }
561
562 const auto HandleHipStdPar = [=, &DriverArgs, &CC1Args]() {
563 StringRef Inc = getIncludePath();
564 auto &FS = D.getVFS();
565
566 if (!hasHIPStdParLibrary())
567 if (!HIPStdParPathArg.empty() ||
568 !FS.exists(Path: Inc + "/thrust/system/hip/hipstdpar/hipstdpar_lib.hpp")) {
569 D.Diag(DiagID: diag::err_drv_no_hipstdpar_lib);
570 return;
571 }
572 if (!HasRocThrustLibrary && !FS.exists(Path: Inc + "/thrust")) {
573 D.Diag(DiagID: diag::err_drv_no_hipstdpar_thrust_lib);
574 return;
575 }
576 if (!HasRocPrimLibrary && !FS.exists(Path: Inc + "/rocprim")) {
577 D.Diag(DiagID: diag::err_drv_no_hipstdpar_prim_lib);
578 return;
579 }
580 const char *ThrustPath;
581 if (HasRocThrustLibrary)
582 ThrustPath = DriverArgs.MakeArgString(Str: HIPRocThrustPathArg);
583 else
584 ThrustPath = DriverArgs.MakeArgString(Str: Inc + "/thrust");
585
586 const char *HIPStdParPath;
587 if (hasHIPStdParLibrary())
588 HIPStdParPath = DriverArgs.MakeArgString(Str: HIPStdParPathArg);
589 else
590 HIPStdParPath = DriverArgs.MakeArgString(Str: StringRef(ThrustPath) +
591 "/system/hip/hipstdpar");
592
593 const char *PrimPath;
594 if (HasRocPrimLibrary)
595 PrimPath = DriverArgs.MakeArgString(Str: HIPRocPrimPathArg);
596 else
597 PrimPath = DriverArgs.MakeArgString(Str: getIncludePath() + "/rocprim");
598
599 CC1Args.append(IL: {"-idirafter", ThrustPath, "-idirafter", PrimPath,
600 "-idirafter", HIPStdParPath, "-include",
601 "hipstdpar_lib.hpp"});
602 };
603
604 if (!DriverArgs.hasFlag(Pos: options::OPT_offload_inc, Neg: options::OPT_no_offload_inc,
605 Default: true)) {
606 if (HasHipStdPar)
607 HandleHipStdPar();
608
609 return;
610 }
611
612 if (!hasHIPRuntime()) {
613 D.Diag(DiagID: diag::err_drv_no_hip_runtime);
614 return;
615 }
616
617 CC1Args.push_back(Elt: "-idirafter");
618 CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: getIncludePath()));
619 SmallString<128> LibHipCxxPath(getIncludePath());
620 llvm::sys::path::append(path&: LibHipCxxPath, a: "libhipcxx");
621 if (D.getVFS().exists(Path: LibHipCxxPath)) {
622 CC1Args.push_back(Elt: "-idirafter");
623 CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: LibHipCxxPath));
624 }
625 if (UsesRuntimeWrapper)
626 CC1Args.append(IL: {"-include", "__clang_hip_runtime_wrapper.h"});
627 if (HasHipStdPar)
628 HandleHipStdPar();
629}
630
631void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
632 const InputInfo &Output,
633 const InputInfoList &Inputs,
634 const ArgList &Args,
635 const char *LinkingOutput) const {
636 std::string Linker = getToolChain().GetLinkerPath();
637 ArgStringList CmdArgs;
638 if (!Args.hasArg(Ids: options::OPT_r)) {
639 CmdArgs.push_back(Elt: "--no-undefined");
640 CmdArgs.push_back(Elt: "-shared");
641 }
642
643 if (Args.hasArg(Ids: options::OPT_hipstdpar))
644 CmdArgs.push_back(Elt: "-plugin-opt=-amdgpu-enable-hipstdpar");
645
646 if (auto LTO = getToolChain().getLTOMode(Args); LTO != LTOK_None) {
647 addLTOOptions(ToolChain: getToolChain(), Args, CmdArgs, Output, Inputs,
648 IsThinLTO: LTO == LTOK_Thin);
649 } else if (Args.hasArg(Ids: options::OPT_mcpu_EQ)) {
650 CmdArgs.push_back(Elt: Args.MakeArgString(
651 Str: "-plugin-opt=mcpu=" +
652 getProcessorFromTargetID(T: getToolChain().getTriple(),
653 OffloadArch: Args.getLastArgValue(Id: options::OPT_mcpu_EQ))));
654 }
655 addLinkerCompressDebugSectionsOption(TC: getToolChain(), Args, CmdArgs);
656 getToolChain().AddFilePathLibArgs(Args, CmdArgs);
657 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_L);
658 AddLinkerInputs(TC: getToolChain(), Inputs, Args, CmdArgs, JA);
659
660 // Always pass the target-id features to the LTO job.
661 std::vector<StringRef> Features;
662 getAMDGPUTargetFeatures(D: C.getDriver(), Triple: getToolChain().getEffectiveTriple(),
663 Args, Features);
664 if (!Features.empty()) {
665 CmdArgs.push_back(
666 Elt: Args.MakeArgString(Str: "-plugin-opt=-mattr=" + llvm::join(R&: Features, Separator: ",")));
667 }
668
669 getToolChain().addProfileRTLibs(Args, CmdArgs);
670 addSanitizerRuntimes(TC: getToolChain(), Args, CmdArgs);
671
672 if (Args.hasArg(Ids: options::OPT_stdlib))
673 CmdArgs.append(IL: {"-lc", "-lm"});
674 if (Args.hasArg(Ids: options::OPT_startfiles)) {
675 std::optional<std::string> IncludePath = getToolChain().getStdlibPath();
676 if (!IncludePath)
677 IncludePath = "/lib";
678 SmallString<128> P(*IncludePath);
679 llvm::sys::path::append(path&: P, a: "crt1.o");
680 CmdArgs.push_back(Elt: Args.MakeArgString(Str: P));
681 }
682
683 CmdArgs.push_back(Elt: "-o");
684 CmdArgs.push_back(Elt: Output.getFilename());
685 C.addCommand(Cmd: std::make_unique<Command>(
686 args: JA, args: *this, args: ResponseFileSupport::AtFileCurCP(), args: Args.MakeArgString(Str: Linker),
687 args&: CmdArgs, args: Inputs, args: Output));
688}
689
690void amdgpu::getAMDGPUTargetFeatures(const Driver &D,
691 const llvm::Triple &Triple,
692 const llvm::opt::ArgList &Args,
693 std::vector<StringRef> &Features,
694 bool ForAS) {
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 // When assembling, the xnack/sramecc mode cannot come from a module flag
704 // (there is no module), so forward it to the assembler as a feature.
705 if (ForAS) {
706 if (Arg *A = Args.getLastArg(Ids: options::OPT_mxnack, Ids: options::OPT_mno_xnack)) {
707 Features.push_back(
708 x: A->getOption().matches(ID: options::OPT_mxnack) ? "+xnack" : "-xnack");
709 }
710
711 if (Arg *A =
712 Args.getLastArg(Ids: options::OPT_msramecc, Ids: options::OPT_mno_sramecc)) {
713 Features.push_back(x: A->getOption().matches(ID: options::OPT_msramecc)
714 ? "+sramecc"
715 : "-sramecc");
716 }
717 }
718
719 handleTargetFeaturesGroup(D, Triple, Args, Features,
720 Group: options::OPT_m_amdgpu_Features_Group);
721}
722
723/// AMDGPU Toolchain
724AMDGPUToolChain::AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple,
725 const ArgList &Args, const ToolChain *HostTC_,
726 Action::OffloadKind Kind,
727 bool ShouldLinkDeviceLibs)
728 : Generic_ELF(D, Triple, Args),
729 OptionsDefault(
730 {{options::OPT_O, "3"}, {options::OPT_cl_std_EQ, "CL1.2"}}),
731 HostTC(HostTC_), UseHIPLinker(Kind == Action::OFK_HIP),
732 ShouldLinkDeviceLibs(ShouldLinkDeviceLibs) {
733 loadMultilibsFromYAML(Args, D);
734
735 // Check code object version options. Emit warnings for legacy options
736 // and errors for the last invalid code object version options.
737 // It is done here to avoid repeated warning or error messages for
738 // each tool invocation.
739 checkAMDGPUCodeObjectVersion(D, Args);
740
741 bool UsesLLVMOffloading = Args.hasFlag(
742 Pos: options::OPT_foffload_via_llvm, Neg: options::OPT_fno_offload_via_llvm, Default: false);
743 if (Triple.getOS() == llvm::Triple::AMDHSA &&
744 Triple.getEnvironment() != llvm::Triple::LLVM && !UsesLLVMOffloading)
745 RocmInstallation->detectDeviceLibrary();
746
747 if (HostTC)
748 getProgramPaths().push_back(Elt: getDriver().Dir);
749}
750
751Tool *AMDGPUToolChain::buildLinker() const {
752 // FIXME: Should not have 2 linker paths.
753 if (UseHIPLinker)
754 return new tools::AMDGCN::Linker(*this);
755 return new tools::amdgpu::Linker(*this);
756}
757
758DerivedArgList *
759AMDGPUToolChain::TranslateArgs(const DerivedArgList &Args, BoundArch BA,
760 Action::OffloadKind DeviceOffloadKind) const {
761 DerivedArgList *DAL = Generic_ELF::TranslateArgs(Args, BA, DeviceOffloadKind);
762 if (!DAL) {
763 DAL = new DerivedArgList(Args.getBaseArgs());
764 for (Arg *A : Args)
765 DAL->append(A);
766 }
767
768 const OptTable &Opts = getDriver().getOpts();
769
770 if (DeviceOffloadKind == Action::OFK_None) {
771 // AMDGPU is intended to use `-mcpu` but we accept `-march` for legacy.
772 if (Arg *A = DAL->getLastArg(Ids: options::OPT_march_EQ)) {
773 DAL->eraseArg(Id: options::OPT_march_EQ);
774 if (!DAL->hasArg(Ids: options::OPT_mcpu_EQ))
775 DAL->AddJoinedArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_mcpu_EQ),
776 Value: A->getValue());
777 }
778 }
779
780 // Replace -mcpu=native with detected GPU.
781 Arg *LastMCPUArg = DAL->getLastArg(Ids: options::OPT_mcpu_EQ);
782 if (LastMCPUArg && StringRef(LastMCPUArg->getValue()) == "native") {
783 DAL->eraseArg(Id: options::OPT_mcpu_EQ);
784 auto GPUsOrErr = getSystemGPUArchs(Args);
785 if (!GPUsOrErr) {
786 getDriver().Diag(DiagID: diag::err_drv_undetermined_gpu_arch)
787 << getArchName() << llvm::toString(E: GPUsOrErr.takeError()) << "-mcpu";
788 } else {
789 auto &GPUs = *GPUsOrErr;
790 if (!llvm::all_equal(Range&: GPUs))
791 getDriver().Diag(DiagID: diag::warn_drv_multi_gpu_arch)
792 << getArchName() << llvm::join(R&: GPUs, Separator: ", ") << "-mcpu";
793 DAL->AddJoinedArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_mcpu_EQ),
794 Value: Args.MakeArgString(Str: GPUs.front()));
795 }
796 }
797
798 if (!BA.empty()) {
799 DAL->eraseArg(Id: options::OPT_mcpu_EQ);
800 DAL->AddJoinedArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_mcpu_EQ),
801 Value: BA.ArchName);
802 }
803
804 if (!getTriple().isSPIRV()) {
805 AMDGPUToolChain::ParsedTargetIDType PTID = checkTargetID(DriverArgs: *DAL);
806
807 // Synthesize feature flags for target ID modifiers (xnack, sramecc).
808 if (PTID.OptionalFeatureMap) {
809 const llvm::StringMap<bool> &FeatureMap = *PTID.OptionalFeatureMap;
810
811 auto XnackIt = FeatureMap.find(Key: "xnack");
812 if (XnackIt != FeatureMap.end()) {
813 DAL->AddFlagArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: XnackIt->second
814 ? options::OPT_mxnack
815 : options::OPT_mno_xnack));
816 }
817
818 auto SrameccIt = FeatureMap.find(Key: "sramecc");
819 if (SrameccIt != FeatureMap.end()) {
820 DAL->AddFlagArg(BaseArg: nullptr,
821 Opt: Opts.getOption(Opt: SrameccIt->second
822 ? options::OPT_msramecc
823 : options::OPT_mno_sramecc));
824 }
825 }
826 }
827
828 // Filter out sanitizer coverage options that are not supported for AMDGPU.
829 for (Arg *A : Args) {
830 // Sanitizer coverage is currently not supported for AMDGPU.
831 if (A->getOption().matches(ID: options::OPT_fsan_cov_Group)) {
832 // Upgrade to error if the option was explicitly specified for device
833 bool IsExplicitDevice =
834 A->getBaseArg().getOption().matches(ID: options::OPT_Xarch_device);
835 getDriver().Diag(DiagID: IsExplicitDevice
836 ? diag::err_drv_unsupported_option_for_target
837 : diag::warn_drv_unsupported_option_for_target)
838 << A->getAsString(Args) << getTriple().str();
839 }
840 }
841
842 if (Args.getLastArgValue(Id: options::OPT_x) != "cl")
843 return DAL;
844
845 // Phase 1 (.cl -> .bc)
846 if (Args.hasArg(Ids: options::OPT_c) && Args.hasArg(Ids: options::OPT_emit_llvm)) {
847 // Have to check OPT_O4, OPT_O0 & OPT_Ofast separately
848 // as they defined that way in Options.td
849 if (!Args.hasArg(Ids: options::OPT_O, Ids: options::OPT_O0, Ids: options::OPT_O4,
850 Ids: options::OPT_Ofast))
851 DAL->AddJoinedArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_O),
852 Value: getOptionDefault(OptID: options::OPT_O));
853 }
854
855 return DAL;
856}
857
858bool AMDGPUToolChain::getDefaultDenormsAreZeroForTarget(
859 llvm::AMDGPU::GPUKind Kind) {
860
861 // Assume nothing without a specific target.
862 if (Kind == llvm::AMDGPU::GK_NONE)
863 return false;
864
865 const llvm::AMDGPU::AMDGPUFeatureBitset &Features =
866 llvm::AMDGPU::getFeatureBitset(AK: Kind);
867
868 // Default to enabling f32 denormals by default on subtargets where fma is
869 // fast with denormals
870 const bool BothDenormAndFMAFast =
871 Features.test(I: llvm::AMDGPU::FEAT_FAST_FMAF) &&
872 Features.test(I: llvm::AMDGPU::FEAT_FAST_DENORMAL_F32);
873 return !BothDenormAndFMAFast;
874}
875
876llvm::DenormalMode AMDGPUToolChain::getDefaultDenormalModeForType(
877 const llvm::opt::ArgList &DriverArgs, const JobAction &JA,
878 const llvm::fltSemantics *FPType) const {
879 // Denormals should always be enabled for f16 and f64.
880 if (!FPType || FPType != &llvm::APFloat::IEEEsingle())
881 return llvm::DenormalMode::getIEEE();
882
883 if (JA.getOffloadingDeviceKind() == Action::OFK_HIP ||
884 JA.getOffloadingDeviceKind() == Action::OFK_Cuda) {
885 BoundArch BA = JA.getOffloadingArch();
886 // FIXME: Missing conversion from OffloadArch to GPUKind
887 auto Arch = getProcessorFromTargetID(T: getTriple(), OffloadArch: BA.ArchName);
888 auto Kind = llvm::AMDGPU::parseArchAMDGCN(CPU: Arch);
889 if (FPType && FPType == &llvm::APFloat::IEEEsingle() &&
890 DriverArgs.hasFlag(Pos: options::OPT_fgpu_flush_denormals_to_zero,
891 Neg: options::OPT_fno_gpu_flush_denormals_to_zero,
892 Default: getDefaultDenormsAreZeroForTarget(Kind)))
893 return llvm::DenormalMode::getPreserveSign();
894
895 return llvm::DenormalMode::getIEEE();
896 }
897
898 const StringRef GpuArch = getGPUArch(DriverArgs);
899 auto Kind = llvm::AMDGPU::parseArchAMDGCN(CPU: GpuArch);
900
901 // TODO: There are way too many flags that change this. Do we need to check
902 // them all?
903 bool DAZ = DriverArgs.hasArg(Ids: options::OPT_cl_denorms_are_zero) ||
904 getDefaultDenormsAreZeroForTarget(Kind);
905
906 // Outputs are flushed to zero (FTZ), preserving sign. Denormal inputs are
907 // also implicit treated as zero (DAZ).
908 return DAZ ? llvm::DenormalMode::getPreserveSign() :
909 llvm::DenormalMode::getIEEE();
910}
911
912bool AMDGPUToolChain::isWave64(const llvm::opt::ArgList &DriverArgs,
913 llvm::AMDGPU::GPUKind Kind) {
914 bool HasWave32 = llvm::AMDGPU::getFeatureBitset(AK: Kind).test(
915 I: llvm::AMDGPU::FEAT_SUPPORTS_WAVE32);
916
917 return !HasWave32 || DriverArgs.hasFlag(
918 Pos: options::OPT_mwavefrontsize64, Neg: options::OPT_mno_wavefrontsize64, Default: false);
919}
920
921void AMDGPUToolChain::addClangTargetOptions(
922 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
923 BoundArch BA, Action::OffloadKind DeviceOffloadingKind) const {
924 bool UsesLLVMOffloading = DriverArgs.hasFlag(
925 Pos: options::OPT_foffload_via_llvm, Neg: options::OPT_fno_offload_via_llvm, Default: false);
926 if (DeviceOffloadingKind == Action::OFK_HIP ||
927 (DeviceOffloadingKind == Action::OFK_Cuda && UsesLLVMOffloading)) {
928 CC1Args.append(IL: {"-fcuda-is-device", "-fno-threadsafe-statics"});
929
930 if (!DriverArgs.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc,
931 Default: false)) {
932 CC1Args.append(IL: {"-mllvm", "-amdgpu-internalize-symbols"});
933 if (DriverArgs.hasArgNoClaim(Ids: options::OPT_hipstdpar))
934 CC1Args.append(IL: {"-mllvm", "-amdgpu-enable-hipstdpar"});
935 }
936 }
937
938 DriverArgs.AddLastArg(Output&: CC1Args, Ids: options::OPT_gpu_max_threads_per_block_EQ);
939
940 // Default to "hidden" visibility, as object level linking will not be
941 // supported for the foreseeable future.
942 // TODO: remove the SPIR-V bypass once it can encode (hidden) visibility.
943 if (!DriverArgs.hasArg(Ids: options::OPT_fvisibility_EQ,
944 Ids: options::OPT_fvisibility_ms_compat) &&
945 !getEffectiveTriple().isSPIRV() && !getDriver().IsFlangMode()) {
946 CC1Args.push_back(Elt: "-fvisibility=hidden");
947 CC1Args.push_back(Elt: "-fapply-global-visibility-to-externs");
948 }
949
950 if (getEffectiveTriple().isSPIRV()) {
951 // For HIP + SPIRV, embed the command-line into the generated binary
952 if (DeviceOffloadingKind == Action::OFK_HIP &&
953 !DriverArgs.hasArg(Ids: options::OPT_fembed_bitcode_marker))
954 CC1Args.push_back(Elt: "-fembed-bitcode=marker");
955
956 // For SPIR-V we want to retain the pristine output of Clang CodeGen, since
957 // optimizations might lose structure / information that is necessary for
958 // generating optimal concrete AMDGPU code.
959 //
960 // For standalone SPIR-V, use -disable-llvm-optzns
961 // TODO: using the below option is a temporary placeholder until Clang
962 // provides the required functionality, which essentially boils down
963 // to -O0 being refactored / reworked to not imply optnone / remove
964 // TBAA. Once that is added, we should pivot to that functionality,
965 // being mindful to not corrupt the user provided and subsequently
966 // embedded command-line (i.e. if the user asks for -O3 this is what
967 // the finalisation should use).
968 if (!DriverArgs.hasArg(Ids: options::OPT_disable_llvm_optzns))
969 CC1Args.push_back(Elt: "-disable-llvm-optzns");
970
971 return; // No DeviceLibs for SPIR-V.
972 }
973
974 if (DeviceOffloadingKind == Action::OFK_None) {
975 // For the OpenCL case where there is no offload target, accept -nostdlib to
976 // disable bitcode linking.
977 if (DriverArgs.hasArg(Ids: options::OPT_nostdlib))
978 return;
979
980 if (addOpenCLBuiltinsLib(D: getDriver(), TT: getTriple(), DriverArgs, CC1Args))
981 return;
982 }
983
984 if (!DriverArgs.hasFlag(Pos: options::OPT_offloadlib, Neg: options::OPT_no_offloadlib,
985 Default: true))
986 return;
987
988 // With an LLVM environment, only use libraries provided by the resource
989 // directory.
990 if (getEffectiveTriple().getEnvironment() == llvm::Triple::LLVM)
991 return;
992
993 // Link device libraries for OpenCL, HIP, and OpenMP
994 for (auto BCFile : getDeviceLibs(Args: DriverArgs, BA, DeviceOffloadKind: DeviceOffloadingKind)) {
995 CC1Args.push_back(Elt: BCFile.ShouldInternalize ? "-mlink-builtin-bitcode"
996 : "-mlink-bitcode-file");
997 CC1Args.push_back(Elt: DriverArgs.MakeArgStringRef(Str: BCFile.Path));
998 }
999}
1000
1001void AMDGPUToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
1002 // AMDGPU does not support atomic lib call. Treat atomic alignment
1003 // warnings as errors.
1004 CC1Args.push_back(Elt: "-Werror=atomic-alignment");
1005}
1006
1007void AMDGPUToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
1008 ArgStringList &CC1Args) const {
1009 // In an offloading compilation the device toolchain must pick up the host's
1010 // system include paths, even when compiling device code.
1011 if (HostTC) {
1012 HostTC->AddClangSystemIncludeArgs(DriverArgs, CC1Args);
1013 return;
1014 }
1015
1016 if (DriverArgs.hasArg(Ids: options::OPT_nostdinc) ||
1017 DriverArgs.hasArg(Ids: options::OPT_nostdlibinc))
1018 return;
1019
1020 // Add multilib variant include paths in priority order.
1021 for (const Multilib &M : getOrderedMultilibs()) {
1022 if (M.isDefault())
1023 continue;
1024 if (std::optional<std::string> StdlibIncDir = getStdlibIncludePath()) {
1025 SmallString<128> Dir(*StdlibIncDir);
1026 llvm::sys::path::append(path&: Dir, a: M.includeSuffix());
1027 if (getDriver().getVFS().exists(Path: Dir))
1028 addSystemInclude(DriverArgs, CC1Args, Path: Dir);
1029 }
1030 }
1031
1032 if (std::optional<std::string> Path = getStdlibIncludePath())
1033 addSystemInclude(DriverArgs, CC1Args, Path: *Path);
1034}
1035
1036StringRef
1037AMDGPUToolChain::getGPUArch(const llvm::opt::ArgList &DriverArgs) const {
1038 return getProcessorFromTargetID(
1039 T: getTriple(), OffloadArch: DriverArgs.getLastArgValue(Id: options::OPT_mcpu_EQ));
1040}
1041
1042AMDGPUToolChain::ParsedTargetIDType
1043AMDGPUToolChain::getParsedTargetID(const llvm::opt::ArgList &DriverArgs) const {
1044 StringRef TargetID = DriverArgs.getLastArgValue(Id: options::OPT_mcpu_EQ);
1045 if (TargetID.empty())
1046 return {};
1047
1048 llvm::StringMap<bool> FeatureMap;
1049 auto OptionalGpuArch = parseTargetID(T: getTriple(), OffloadArch: TargetID, FeatureMap: &FeatureMap);
1050 if (!OptionalGpuArch)
1051 return {.OptionalTargetID: TargetID.str(), .OptionalGPUArch: std::nullopt, .OptionalFeatureMap: std::nullopt};
1052
1053 return {.OptionalTargetID: TargetID.str(), .OptionalGPUArch: OptionalGpuArch->str(), .OptionalFeatureMap: FeatureMap};
1054}
1055
1056AMDGPUToolChain::ParsedTargetIDType
1057AMDGPUToolChain::checkTargetID(const llvm::opt::ArgList &DriverArgs) const {
1058 auto PTID = getParsedTargetID(DriverArgs);
1059 if (PTID.OptionalTargetID && !PTID.OptionalGPUArch) {
1060 getDriver().Diag(DiagID: clang::diag::err_drv_bad_target_id)
1061 << *PTID.OptionalTargetID;
1062 return PTID;
1063 }
1064
1065 if (getTriple().getSubArch() != llvm::Triple::NoSubArch &&
1066 PTID.OptionalGPUArch) {
1067 llvm::AMDGPU::GPUKind Kind =
1068 llvm::AMDGPU::parseArchAMDGCN(CPU: *PTID.OptionalGPUArch);
1069 llvm::Triple::SubArchType KindSubArch =
1070 static_cast<llvm::Triple::SubArchType>(llvm::AMDGPU::getSubArch(AK: Kind));
1071 if (getTriple().getSubArch() != KindSubArch &&
1072 getTriple().getSubArch() !=
1073 llvm::AMDGPU::getMajorSubArch(SubArch: KindSubArch)) {
1074 getDriver().Diag(DiagID: clang::diag::err_target_unsupported_arch)
1075 << *PTID.OptionalGPUArch << getTriple().getArchName();
1076 }
1077 }
1078 return PTID;
1079}
1080
1081Expected<SmallVector<std::string>>
1082AMDGPUToolChain::getSystemGPUArchs(const ArgList &Args) const {
1083 // Detect AMD GPUs availible on the system.
1084 std::string Program;
1085 if (Arg *A = Args.getLastArg(Ids: options::OPT_offload_arch_tool_EQ))
1086 Program = A->getValue();
1087 else
1088 Program = GetProgramPath(Name: "amdgpu-arch");
1089
1090 auto StdoutOrErr = getDriver().executeProgram(Args: {Program});
1091 if (!StdoutOrErr)
1092 return StdoutOrErr.takeError();
1093
1094 SmallVector<std::string, 1> GPUArchs;
1095 for (StringRef Arch : llvm::split(Str: (*StdoutOrErr)->getBuffer(), Separator: "\n"))
1096 if (!Arch.empty())
1097 GPUArchs.push_back(Elt: Arch.str());
1098
1099 if (GPUArchs.empty())
1100 return llvm::createStringError(EC: std::error_code(),
1101 S: "No AMD GPU detected in the system");
1102
1103 return std::move(GPUArchs);
1104}
1105
1106bool RocmInstallationDetector::checkCommonBitcodeLibs(
1107 StringRef GPUArch, StringRef LibDeviceFile,
1108 DeviceLibABIVersion ABIVer) const {
1109 if (!hasDeviceLibrary()) {
1110 D.Diag(DiagID: diag::err_drv_no_rocm_device_lib) << 0;
1111 return false;
1112 }
1113 if (LibDeviceFile.empty()) {
1114 D.Diag(DiagID: diag::err_drv_no_rocm_device_lib) << 1 << GPUArch;
1115 return false;
1116 }
1117 if (ABIVer.requiresLibrary() && getABIVersionPath(ABIVer).empty()) {
1118 // Starting from COV6, we will report minimum ROCm version requirement in
1119 // the error message.
1120 if (ABIVer.getAsCodeObjectVersion() < 6)
1121 D.Diag(DiagID: diag::err_drv_no_rocm_device_lib) << 2 << ABIVer.toString() << 0;
1122 else
1123 D.Diag(DiagID: diag::err_drv_no_rocm_device_lib)
1124 << 2 << ABIVer.toString() << 1 << "6.3";
1125 return false;
1126 }
1127 return true;
1128}
1129
1130llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
1131RocmInstallationDetector::getCommonBitcodeLibs(
1132 const llvm::opt::ArgList &DriverArgs, StringRef LibDeviceFile,
1133 StringRef GPUArch, const Action::OffloadKind DeviceOffloadingKind,
1134 const bool NeedsASanRT) const {
1135 llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12> BCLibs;
1136
1137 CommonBitcodeLibsPreferences Pref{D, DriverArgs, GPUArch,
1138 DeviceOffloadingKind, NeedsASanRT};
1139
1140 auto AddBCLib = [&](ToolChain::BitCodeLibraryInfo BCLib,
1141 bool Internalize = true) {
1142 if (!BCLib.Path.empty()) {
1143 BCLib.ShouldInternalize = Internalize;
1144 BCLibs.emplace_back(Args&: BCLib);
1145 }
1146 };
1147 auto AddSanBCLibs = [&]() {
1148 if (Pref.GPUSan)
1149 AddBCLib(getAsanRTLPath(), false);
1150 };
1151
1152 AddSanBCLibs();
1153 AddBCLib(getOCMLPath());
1154 if (!Pref.IsOpenMP)
1155 AddBCLib(getOCKLPath());
1156 else if (Pref.GPUSan && Pref.IsOpenMP)
1157 AddBCLib(getOCKLPath());
1158 AddBCLib(getUnsafeMathPath(Enabled: Pref.UnsafeMathOpt || Pref.FastRelaxedMath));
1159 AddBCLib(getFiniteOnlyPath(Enabled: Pref.FiniteOnly || Pref.FastRelaxedMath));
1160 AddBCLib(getWavefrontSize64Path(Enabled: Pref.Wave64));
1161 AddBCLib(LibDeviceFile);
1162 auto ABIVerPath = getABIVersionPath(ABIVer: Pref.ABIVer);
1163 if (!ABIVerPath.empty())
1164 AddBCLib(ABIVerPath);
1165
1166 return BCLibs;
1167}
1168
1169llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
1170AMDGPUToolChain::getCommonDeviceLibNames(
1171 const llvm::opt::ArgList &DriverArgs, llvm::StringRef TargetID,
1172 llvm::StringRef GPUArch, Action::OffloadKind DeviceOffloadingKind) const {
1173 auto Kind = llvm::AMDGPU::parseArchAMDGCN(CPU: GPUArch);
1174 const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(AK: Kind);
1175
1176 StringRef LibDeviceFile = RocmInstallation->getLibDeviceFile(Gpu: CanonArch);
1177 auto ABIVer = DeviceLibABIVersion::fromCodeObjectVersion(
1178 CodeObjectVersion: getAMDGPUCodeObjectVersion(D: getDriver(), Args: DriverArgs));
1179 if (!RocmInstallation->checkCommonBitcodeLibs(GPUArch: CanonArch, LibDeviceFile,
1180 ABIVer))
1181 return {};
1182
1183 return RocmInstallation->getCommonBitcodeLibs(
1184 DriverArgs, LibDeviceFile, GPUArch, DeviceOffloadingKind,
1185 NeedsASanRT: getSanitizerArgs(JobArgs: DriverArgs, BA: BoundArch(TargetID), DeviceOffloadKind: DeviceOffloadingKind)
1186 .needsAsanRt());
1187}
1188
1189llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
1190AMDGPUToolChain::getDeviceLibs(const llvm::opt::ArgList &DriverArgs,
1191 BoundArch BA,
1192 Action::OffloadKind DeviceOffloadKind) const {
1193 assert(getEffectiveTriple().isAMDGPU() &&
1194 "spirv should not try to link device libs");
1195
1196 if (!DriverArgs.hasFlag(Pos: options::OPT_offloadlib, Neg: options::OPT_no_offloadlib,
1197 Default: true) ||
1198 getEffectiveTriple().getEnvironment() == llvm::Triple::LLVM)
1199 return {};
1200
1201 if (getTriple().getOS() != llvm::Triple::AMDHSA)
1202 return {};
1203
1204 StringRef GpuArch;
1205 StringRef TargetID;
1206 if (DeviceOffloadKind == Action::OFK_None) {
1207 TargetID = DriverArgs.getLastArgValue(Id: options::OPT_mcpu_EQ);
1208 GpuArch = getProcessorFromTargetID(T: getTriple(), OffloadArch: TargetID);
1209 } else {
1210 TargetID = BA.ArchName;
1211 GpuArch = getProcessorFromTargetID(T: getTriple(), OffloadArch: BA.ArchName);
1212 }
1213
1214 llvm::SmallVector<BitCodeLibraryInfo, 12> BCLibs;
1215
1216 // HIP-specific handling
1217 if (DeviceOffloadKind == Action::OFK_HIP) {
1218 // Handle --hip-device-lib manual override
1219 auto BCLibArgs = DriverArgs.getAllArgValues(Id: options::OPT_hip_device_lib_EQ);
1220 if (!BCLibArgs.empty()) {
1221 ArgStringList LibraryPaths;
1222 for (StringRef Path : RocmInstallation->getRocmDeviceLibPathArg())
1223 LibraryPaths.push_back(Elt: DriverArgs.MakeArgStringRef(Str: Path));
1224 addDirectoryList(Args: DriverArgs, CmdArgs&: LibraryPaths, ArgName: "", EnvVar: "HIP_DEVICE_LIB_PATH");
1225
1226 for (StringRef BCName : BCLibArgs) {
1227 bool Found = false;
1228 for (StringRef LibraryPath : LibraryPaths) {
1229 SmallString<128> Path(LibraryPath);
1230 llvm::sys::path::append(path&: Path, a: BCName);
1231 if (llvm::sys::fs::exists(Path)) {
1232 BCLibs.emplace_back(Args&: Path);
1233 Found = true;
1234 break;
1235 }
1236 }
1237 if (!Found)
1238 getDriver().Diag(DiagID: diag::err_drv_no_such_file) << BCName;
1239 }
1240 return BCLibs;
1241 }
1242
1243 if (!RocmInstallation->hasDeviceLibrary()) {
1244 getDriver().Diag(DiagID: diag::err_drv_no_rocm_device_lib) << 0;
1245 return {};
1246 }
1247
1248 // Add common device libraries
1249 for (auto N : getCommonDeviceLibNames(DriverArgs, TargetID, GPUArch: GpuArch,
1250 DeviceOffloadingKind: DeviceOffloadKind))
1251 BCLibs.emplace_back(Args&: N);
1252
1253 // Add instrument lib for HIP
1254 auto InstLib =
1255 DriverArgs.getLastArgValue(Id: options::OPT_gpu_instrument_lib_EQ);
1256 if (!InstLib.empty()) {
1257 if (llvm::sys::fs::exists(Path: InstLib))
1258 BCLibs.emplace_back(Args&: InstLib);
1259 else
1260 getDriver().Diag(DiagID: diag::err_drv_no_such_file) << InstLib;
1261 }
1262
1263 return BCLibs;
1264 }
1265
1266 // OpenMP handling
1267 if (DeviceOffloadKind == Action::OFK_OpenMP) {
1268 for (auto BCLib : getCommonDeviceLibNames(DriverArgs, TargetID, GPUArch: GpuArch,
1269 DeviceOffloadingKind: DeviceOffloadKind))
1270 BCLibs.emplace_back(Args&: BCLib);
1271 return BCLibs;
1272 }
1273
1274 // The libraries are currently only built for amdhsa.
1275 if (getTriple().getOS() != llvm::Triple::AMDHSA)
1276 return {};
1277
1278 // Only link device libraries if requested (set by Driver based on input type)
1279 if (!ShouldLinkDeviceLibs)
1280 return {};
1281
1282 StringRef LibDeviceFile = RocmInstallation->getLibDeviceFile(Gpu: GpuArch);
1283
1284 auto ABIVer = DeviceLibABIVersion::fromCodeObjectVersion(
1285 CodeObjectVersion: getAMDGPUCodeObjectVersion(D: getDriver(), Args: DriverArgs));
1286 if (!RocmInstallation->checkCommonBitcodeLibs(GPUArch: GpuArch, LibDeviceFile, ABIVer))
1287 return {};
1288
1289 // Add the OpenCL specific bitcode library
1290 BCLibs.emplace_back(Args: RocmInstallation->getOpenCLPath().str());
1291
1292 // Add the generic set of libraries
1293 BCLibs.append(RHS: RocmInstallation->getCommonBitcodeLibs(
1294 DriverArgs, LibDeviceFile, GPUArch: GpuArch, DeviceOffloadingKind: DeviceOffloadKind,
1295 NeedsASanRT: getSanitizerArgs(JobArgs: DriverArgs, BA: BoundArch{TargetID}, DeviceOffloadKind)
1296 .needsAsanRt()));
1297
1298 return BCLibs;
1299}
1300
1301ToolChain::CXXStdlibType
1302AMDGPUToolChain::GetCXXStdlibType(const ArgList &Args) const {
1303 if (HostTC)
1304 return HostTC->GetCXXStdlibType(Args);
1305 return ToolChain::GetCXXStdlibType(Args);
1306}
1307
1308void AMDGPUToolChain::AddClangCXXStdlibIncludeArgs(
1309 const ArgList &Args, ArgStringList &CC1Args) const {
1310 if (HostTC)
1311 HostTC->AddClangCXXStdlibIncludeArgs(DriverArgs: Args, CC1Args);
1312 else
1313 ToolChain::AddClangCXXStdlibIncludeArgs(DriverArgs: Args, CC1Args);
1314}
1315
1316void AMDGPUToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
1317 ArgStringList &CC1Args) const {
1318 if (HostTC)
1319 HostTC->AddIAMCUIncludeArgs(DriverArgs: Args, CC1Args);
1320}
1321
1322void AMDGPUToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs,
1323 ArgStringList &CC1Args) const {
1324 if (getTriple().getEnvironment() == llvm::Triple::LLVM) {
1325 if (DriverArgs.hasFlag(Pos: options::OPT_offload_inc,
1326 Neg: options::OPT_no_offload_inc, Default: true) &&
1327 !DriverArgs.hasArg(Ids: options::OPT_nohipwrapperinc) &&
1328 !DriverArgs.hasArg(Ids: options::OPT_nobuiltininc)) {
1329 SmallString<128> P(getDriver().ResourceDir);
1330 llvm::sys::path::append(path&: P, a: "include", b: "hip_wrappers");
1331 CC1Args.push_back(Elt: "-internal-isystem");
1332 CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: P));
1333 }
1334 return;
1335 }
1336
1337 RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args);
1338}
1339
1340VersionTuple AMDGPUToolChain::computeMSVCVersion(const Driver *D,
1341 const ArgList &Args) const {
1342 if (HostTC)
1343 return HostTC->computeMSVCVersion(D, Args);
1344 return ToolChain::computeMSVCVersion(D, Args);
1345}
1346
1347LTOKind AMDGPUToolChain::getDefaultLTOMode() const {
1348 // Offload toolchains use full LTO by default.
1349 return HostTC == nullptr ? LTOK_None : LTOK_Full;
1350}
1351
1352LTOKind AMDGPUToolChain::getLTOMode(const ArgList &Args,
1353 Action::OffloadKind Kind) const {
1354 if (getTriple().isAMDGCN() && getDriver().offloadDeviceOnly() &&
1355 !Args.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc, Default: false) &&
1356 !Args.hasArg(Ids: options::OPT_foffload_lto, Ids: options::OPT_foffload_lto_EQ))
1357 return LTOK_None;
1358 return ToolChain::getLTOMode(Args, Kind);
1359}
1360
1361static bool isXnackAvailable(const llvm::Triple &TT, llvm::StringRef TargetID) {
1362 // Arch-specific check - only report as supported if arch has xnack+
1363 if (!TT.isAMDGCN())
1364 return false;
1365 llvm::StringRef Processor = getProcessorFromTargetID(T: TT, OffloadArch: TargetID);
1366 llvm::AMDGPU::GPUKind ProcKind = llvm::AMDGPU::parseArchAMDGCN(CPU: Processor);
1367 const llvm::AMDGPU::AMDGPUFeatureBitset &Features =
1368 llvm::AMDGPU::getFeatureBitset(AK: ProcKind);
1369
1370 // If processor has xnack but doesn't support on/off modes, xnack is always on
1371 bool XnackAlwaysOn = Features.test(I: llvm::AMDGPU::FEAT_XNACK_SUPPORT) &&
1372 !Features.test(I: llvm::AMDGPU::FEAT_XNACK_ON_OFF_MODES);
1373 if (XnackAlwaysOn)
1374 return true;
1375
1376 // Otherwise, check if xnack+ is explicitly enabled in the target ID
1377 llvm::StringMap<bool> FeatureMap;
1378 auto OptionalGpuArch = parseTargetID(T: TT, OffloadArch: TargetID, FeatureMap: &FeatureMap);
1379 if (!OptionalGpuArch)
1380 return false;
1381 auto Loc = FeatureMap.find(Key: "xnack");
1382 return (Loc != FeatureMap.end() && Loc->second);
1383}
1384
1385SanitizerMask AMDGPUToolChain::getSupportedSanitizers(
1386 BoundArch BA, Action::OffloadKind DeviceOffloadKind) const {
1387 SanitizerMask SupportedMask =
1388 ToolChain::getSupportedSanitizers(BA, DeviceOffloadKind);
1389
1390 // Address sanitizer is potentially supported, but depends on the exact target
1391 // arch xnack support.
1392 if (!BA || isXnackAvailable(TT: getTriple(), TargetID: BA.ArchName))
1393 SupportedMask |= SanitizerKind::Address;
1394
1395 return SupportedMask;
1396}
1397
1398StringRef AMDGPUToolChain::getSanitizerRequirement(SanitizerMask Kinds,
1399 BoundArch BA) const {
1400 // Address sanitizer requires xnack+ feature
1401 if ((Kinds & SanitizerKind::Address) && BA &&
1402 !isXnackAvailable(TT: getTriple(), TargetID: BA.ArchName)) {
1403 return "xnack+";
1404 }
1405 return "";
1406}
1407