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 // FIXME: Device ASan is provided by the ROCm device library, not compiler-rt.
671 if (!getToolChain().getSanitizerArgs(JobArgs: Args).needsAsanRt())
672 addSanitizerRuntimes(TC: getToolChain(), Args, CmdArgs, C);
673
674 if (Args.hasArg(Ids: options::OPT_stdlib))
675 CmdArgs.append(IL: {"-lc", "-lm"});
676 if (Args.hasArg(Ids: options::OPT_startfiles)) {
677 std::optional<std::string> IncludePath = getToolChain().getStdlibPath();
678 if (!IncludePath)
679 IncludePath = "/lib";
680 SmallString<128> P(*IncludePath);
681 llvm::sys::path::append(path&: P, a: "crt1.o");
682 CmdArgs.push_back(Elt: Args.MakeArgString(Str: P));
683 }
684
685 CmdArgs.push_back(Elt: "-o");
686 CmdArgs.push_back(Elt: Output.getFilename());
687 C.addCommand(Cmd: std::make_unique<Command>(
688 args: JA, args: *this, args: ResponseFileSupport::AtFileCurCP(), args: Args.MakeArgString(Str: Linker),
689 args&: CmdArgs, args: Inputs, args: Output));
690}
691
692void amdgpu::getAMDGPUTargetFeatures(const Driver &D,
693 const llvm::Triple &Triple,
694 const llvm::opt::ArgList &Args,
695 std::vector<StringRef> &Features,
696 bool ForAS) {
697 if (Args.hasFlag(Pos: options::OPT_mwavefrontsize64,
698 Neg: options::OPT_mno_wavefrontsize64, Default: false))
699 Features.push_back(x: "+wavefrontsize64");
700
701 if (Args.hasFlag(Pos: options::OPT_mamdgpu_precise_memory_op,
702 Neg: options::OPT_mno_amdgpu_precise_memory_op, Default: false))
703 Features.push_back(x: "+precise-memory");
704
705 // When assembling, the xnack/sramecc mode cannot come from a module flag
706 // (there is no module), so forward it to the assembler as a feature.
707 if (ForAS) {
708 if (Arg *A = Args.getLastArg(Ids: options::OPT_mxnack, Ids: options::OPT_mno_xnack)) {
709 Features.push_back(
710 x: A->getOption().matches(ID: options::OPT_mxnack) ? "+xnack" : "-xnack");
711 }
712
713 if (Arg *A =
714 Args.getLastArg(Ids: options::OPT_msramecc, Ids: options::OPT_mno_sramecc)) {
715 Features.push_back(x: A->getOption().matches(ID: options::OPT_msramecc)
716 ? "+sramecc"
717 : "-sramecc");
718 }
719 }
720
721 handleTargetFeaturesGroup(D, Triple, Args, Features,
722 Group: options::OPT_m_amdgpu_Features_Group);
723}
724
725/// AMDGPU Toolchain
726AMDGPUToolChain::AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple,
727 const ArgList &Args, const ToolChain *HostTC_,
728 Action::OffloadKind Kind,
729 bool ShouldLinkDeviceLibs)
730 : Generic_ELF(D, Triple, Args),
731 OptionsDefault(
732 {{options::OPT_O, "3"}, {options::OPT_cl_std_EQ, "CL1.2"}}),
733 HostTC(HostTC_), UseHIPLinker(Kind == Action::OFK_HIP),
734 ShouldLinkDeviceLibs(ShouldLinkDeviceLibs) {
735 loadMultilibsFromYAML(Args, D);
736
737 // Check code object version options. Emit warnings for legacy options
738 // and errors for the last invalid code object version options.
739 // It is done here to avoid repeated warning or error messages for
740 // each tool invocation.
741 checkAMDGPUCodeObjectVersion(D, Args);
742
743 bool UsesLLVMOffloading = Args.hasFlag(
744 Pos: options::OPT_foffload_via_llvm, Neg: options::OPT_fno_offload_via_llvm, Default: false);
745 if (Triple.getOS() == llvm::Triple::AMDHSA &&
746 Triple.getEnvironment() != llvm::Triple::LLVM && !UsesLLVMOffloading)
747 RocmInstallation->detectDeviceLibrary();
748
749 if (HostTC)
750 getProgramPaths().push_back(Elt: getDriver().Dir);
751}
752
753Tool *AMDGPUToolChain::buildLinker() const {
754 // FIXME: Should not have 2 linker paths.
755 if (UseHIPLinker)
756 return new tools::AMDGCN::Linker(*this);
757 return new tools::amdgpu::Linker(*this);
758}
759
760DerivedArgList *
761AMDGPUToolChain::TranslateArgs(const DerivedArgList &Args, BoundArch BA,
762 Action::OffloadKind DeviceOffloadKind) const {
763 DerivedArgList *DAL = Generic_ELF::TranslateArgs(Args, BA, DeviceOffloadKind);
764 if (!DAL) {
765 DAL = new DerivedArgList(Args.getBaseArgs());
766 for (Arg *A : Args)
767 DAL->append(A);
768 }
769
770 const OptTable &Opts = getDriver().getOpts();
771
772 if (DeviceOffloadKind == Action::OFK_None) {
773 // AMDGPU is intended to use `-mcpu` but we accept `-march` for legacy.
774 if (Arg *A = DAL->getLastArg(Ids: options::OPT_march_EQ)) {
775 DAL->eraseArg(Id: options::OPT_march_EQ);
776 if (!DAL->hasArg(Ids: options::OPT_mcpu_EQ))
777 DAL->AddJoinedArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_mcpu_EQ),
778 Value: A->getValue());
779 }
780 }
781
782 // Replace -mcpu=native with detected GPU.
783 Arg *LastMCPUArg = DAL->getLastArg(Ids: options::OPT_mcpu_EQ);
784 if (LastMCPUArg && StringRef(LastMCPUArg->getValue()) == "native") {
785 DAL->eraseArg(Id: options::OPT_mcpu_EQ);
786 auto GPUsOrErr = getSystemGPUArchs(Args);
787 if (!GPUsOrErr) {
788 getDriver().Diag(DiagID: diag::err_drv_undetermined_gpu_arch)
789 << getArchName() << llvm::toString(E: GPUsOrErr.takeError()) << "-mcpu";
790 } else {
791 auto &GPUs = *GPUsOrErr;
792 if (!llvm::all_equal(Range&: GPUs))
793 getDriver().Diag(DiagID: diag::warn_drv_multi_gpu_arch)
794 << getArchName() << llvm::join(R&: GPUs, Separator: ", ") << "-mcpu";
795 DAL->AddJoinedArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_mcpu_EQ),
796 Value: Args.MakeArgString(Str: GPUs.front()));
797 }
798 }
799
800 if (!BA.empty()) {
801 DAL->eraseArg(Id: options::OPT_mcpu_EQ);
802 DAL->AddJoinedArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_mcpu_EQ),
803 Value: BA.ArchName);
804 }
805
806 if (!getTriple().isSPIRV()) {
807 AMDGPUToolChain::ParsedTargetIDType PTID = checkTargetID(DriverArgs: *DAL);
808
809 // Synthesize feature flags for target ID modifiers (xnack, sramecc).
810 if (PTID.OptionalFeatureMap) {
811 const llvm::StringMap<bool> &FeatureMap = *PTID.OptionalFeatureMap;
812
813 auto XnackIt = FeatureMap.find(Key: "xnack");
814 if (XnackIt != FeatureMap.end()) {
815 DAL->AddFlagArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: XnackIt->second
816 ? options::OPT_mxnack
817 : options::OPT_mno_xnack));
818 }
819
820 auto SrameccIt = FeatureMap.find(Key: "sramecc");
821 if (SrameccIt != FeatureMap.end()) {
822 DAL->AddFlagArg(BaseArg: nullptr,
823 Opt: Opts.getOption(Opt: SrameccIt->second
824 ? options::OPT_msramecc
825 : options::OPT_mno_sramecc));
826 }
827 }
828 }
829
830 // Filter out sanitizer coverage options that are not supported for AMDGPU.
831 for (Arg *A : Args) {
832 // Sanitizer coverage is currently not supported for AMDGPU.
833 if (A->getOption().matches(ID: options::OPT_fsan_cov_Group)) {
834 // Upgrade to error if the option was explicitly specified for device
835 bool IsExplicitDevice =
836 A->getBaseArg().getOption().matches(ID: options::OPT_Xarch_device);
837 getDriver().Diag(DiagID: IsExplicitDevice
838 ? diag::err_drv_unsupported_option_for_target
839 : diag::warn_drv_unsupported_option_for_target)
840 << A->getAsString(Args) << getTriple().str();
841 }
842 }
843
844 if (Args.getLastArgValue(Id: options::OPT_x) != "cl")
845 return DAL;
846
847 // Phase 1 (.cl -> .bc)
848 if (Args.hasArg(Ids: options::OPT_c) && Args.hasArg(Ids: options::OPT_emit_llvm)) {
849 // Have to check OPT_O4, OPT_O0 & OPT_Ofast separately
850 // as they defined that way in Options.td
851 if (!Args.hasArg(Ids: options::OPT_O, Ids: options::OPT_O0, Ids: options::OPT_O4,
852 Ids: options::OPT_Ofast))
853 DAL->AddJoinedArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_O),
854 Value: getOptionDefault(OptID: options::OPT_O));
855 }
856
857 return DAL;
858}
859
860bool AMDGPUToolChain::getDefaultDenormsAreZeroForTarget(
861 llvm::AMDGPU::GPUKind Kind) {
862
863 // Assume nothing without a specific target.
864 if (Kind == llvm::AMDGPU::GK_NONE)
865 return false;
866
867 const llvm::AMDGPU::AMDGPUFeatureBitset &Features =
868 llvm::AMDGPU::getFeatureBitset(AK: Kind);
869
870 // Default to enabling f32 denormals by default on subtargets where fma is
871 // fast with denormals
872 const bool BothDenormAndFMAFast =
873 Features.test(I: llvm::AMDGPU::FEAT_FAST_FMAF) &&
874 Features.test(I: llvm::AMDGPU::FEAT_FAST_DENORMAL_F32);
875 return !BothDenormAndFMAFast;
876}
877
878llvm::DenormalMode AMDGPUToolChain::getDefaultDenormalModeForType(
879 const llvm::opt::ArgList &DriverArgs, const JobAction &JA,
880 const llvm::fltSemantics *FPType) const {
881 // Denormals should always be enabled for f16 and f64.
882 if (!FPType || FPType != &llvm::APFloat::IEEEsingle())
883 return llvm::DenormalMode::getIEEE();
884
885 if (JA.getOffloadingDeviceKind() == Action::OFK_HIP ||
886 JA.getOffloadingDeviceKind() == Action::OFK_Cuda) {
887 BoundArch BA = JA.getOffloadingArch();
888 // FIXME: Missing conversion from OffloadArch to GPUKind
889 auto Arch = getProcessorFromTargetID(T: getTriple(), OffloadArch: BA.ArchName);
890 auto Kind = llvm::AMDGPU::parseArchAMDGCN(CPU: Arch);
891 if (FPType && FPType == &llvm::APFloat::IEEEsingle() &&
892 DriverArgs.hasFlag(Pos: options::OPT_fgpu_flush_denormals_to_zero,
893 Neg: options::OPT_fno_gpu_flush_denormals_to_zero,
894 Default: getDefaultDenormsAreZeroForTarget(Kind)))
895 return llvm::DenormalMode::getPreserveSign();
896
897 return llvm::DenormalMode::getIEEE();
898 }
899
900 const StringRef GpuArch = getGPUArch(DriverArgs);
901 auto Kind = llvm::AMDGPU::parseArchAMDGCN(CPU: GpuArch);
902
903 // TODO: There are way too many flags that change this. Do we need to check
904 // them all?
905 bool DAZ = DriverArgs.hasArg(Ids: options::OPT_cl_denorms_are_zero) ||
906 getDefaultDenormsAreZeroForTarget(Kind);
907
908 // Outputs are flushed to zero (FTZ), preserving sign. Denormal inputs are
909 // also implicit treated as zero (DAZ).
910 return DAZ ? llvm::DenormalMode::getPreserveSign() :
911 llvm::DenormalMode::getIEEE();
912}
913
914bool AMDGPUToolChain::isWave64(const llvm::opt::ArgList &DriverArgs,
915 llvm::AMDGPU::GPUKind Kind) {
916 bool HasWave32 = llvm::AMDGPU::getFeatureBitset(AK: Kind).test(
917 I: llvm::AMDGPU::FEAT_SUPPORTS_WAVE32);
918
919 return !HasWave32 || DriverArgs.hasFlag(
920 Pos: options::OPT_mwavefrontsize64, Neg: options::OPT_mno_wavefrontsize64, Default: false);
921}
922
923void AMDGPUToolChain::addClangTargetOptions(
924 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
925 BoundArch BA, Action::OffloadKind DeviceOffloadingKind) const {
926 bool UsesLLVMOffloading = DriverArgs.hasFlag(
927 Pos: options::OPT_foffload_via_llvm, Neg: options::OPT_fno_offload_via_llvm, Default: false);
928 if (DeviceOffloadingKind == Action::OFK_HIP ||
929 (DeviceOffloadingKind == Action::OFK_Cuda && UsesLLVMOffloading)) {
930 CC1Args.append(IL: {"-fcuda-is-device", "-fno-threadsafe-statics"});
931
932 if (!DriverArgs.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc,
933 Default: false)) {
934 CC1Args.append(IL: {"-mllvm", "-amdgpu-internalize-symbols"});
935 if (DriverArgs.hasArgNoClaim(Ids: options::OPT_hipstdpar))
936 CC1Args.append(IL: {"-mllvm", "-amdgpu-enable-hipstdpar"});
937 }
938 }
939
940 DriverArgs.AddLastArg(Output&: CC1Args, Ids: options::OPT_gpu_max_threads_per_block_EQ);
941
942 // Default to "hidden" visibility, as object level linking will not be
943 // supported for the foreseeable future.
944 // TODO: remove the SPIR-V bypass once it can encode (hidden) visibility.
945 if (!DriverArgs.hasArg(Ids: options::OPT_fvisibility_EQ,
946 Ids: options::OPT_fvisibility_ms_compat) &&
947 !getEffectiveTriple().isSPIRV() && !getDriver().IsFlangMode()) {
948 CC1Args.push_back(Elt: "-fvisibility=hidden");
949 CC1Args.push_back(Elt: "-fapply-global-visibility-to-externs");
950 }
951
952 if (getEffectiveTriple().isSPIRV()) {
953 // For HIP + SPIRV, embed the command-line into the generated binary
954 if (DeviceOffloadingKind == Action::OFK_HIP &&
955 !DriverArgs.hasArg(Ids: options::OPT_fembed_bitcode_marker))
956 CC1Args.push_back(Elt: "-fembed-bitcode=marker");
957
958 // For SPIR-V we want to retain the pristine output of Clang CodeGen, since
959 // optimizations might lose structure / information that is necessary for
960 // generating optimal concrete AMDGPU code.
961 //
962 // For standalone SPIR-V, use -disable-llvm-optzns
963 // TODO: using the below option is a temporary placeholder until Clang
964 // provides the required functionality, which essentially boils down
965 // to -O0 being refactored / reworked to not imply optnone / remove
966 // TBAA. Once that is added, we should pivot to that functionality,
967 // being mindful to not corrupt the user provided and subsequently
968 // embedded command-line (i.e. if the user asks for -O3 this is what
969 // the finalisation should use).
970 if (!DriverArgs.hasArg(Ids: options::OPT_disable_llvm_optzns))
971 CC1Args.push_back(Elt: "-disable-llvm-optzns");
972
973 return; // No DeviceLibs for SPIR-V.
974 }
975
976 if (DeviceOffloadingKind == Action::OFK_None) {
977 // For the OpenCL case where there is no offload target, accept -nostdlib to
978 // disable bitcode linking.
979 if (DriverArgs.hasArg(Ids: options::OPT_nostdlib))
980 return;
981
982 if (addOpenCLBuiltinsLib(D: getDriver(), TT: getTriple(), DriverArgs, CC1Args))
983 return;
984 }
985
986 if (!DriverArgs.hasFlag(Pos: options::OPT_offloadlib, Neg: options::OPT_no_offloadlib,
987 Default: true))
988 return;
989
990 // With an LLVM environment, only use libraries provided by the resource
991 // directory.
992 if (getEffectiveTriple().getEnvironment() == llvm::Triple::LLVM)
993 return;
994
995 // Link device libraries for OpenCL, HIP, and OpenMP
996 for (auto BCFile : getDeviceLibs(Args: DriverArgs, BA, DeviceOffloadKind: DeviceOffloadingKind)) {
997 CC1Args.push_back(Elt: BCFile.ShouldInternalize ? "-mlink-builtin-bitcode"
998 : "-mlink-bitcode-file");
999 CC1Args.push_back(Elt: DriverArgs.MakeArgStringRef(Str: BCFile.Path));
1000 }
1001}
1002
1003void AMDGPUToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
1004 // AMDGPU does not support atomic lib call. Treat atomic alignment
1005 // warnings as errors.
1006 CC1Args.push_back(Elt: "-Werror=atomic-alignment");
1007}
1008
1009void AMDGPUToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
1010 ArgStringList &CC1Args) const {
1011 // In an offloading compilation the device toolchain must pick up the host's
1012 // system include paths, even when compiling device code.
1013 if (HostTC) {
1014 HostTC->AddClangSystemIncludeArgs(DriverArgs, CC1Args);
1015 return;
1016 }
1017
1018 if (DriverArgs.hasArg(Ids: options::OPT_nostdinc) ||
1019 DriverArgs.hasArg(Ids: options::OPT_nostdlibinc))
1020 return;
1021
1022 // Add multilib variant include paths in priority order.
1023 for (const Multilib &M : getOrderedMultilibs()) {
1024 if (M.isDefault())
1025 continue;
1026 if (std::optional<std::string> StdlibIncDir = getStdlibIncludePath()) {
1027 SmallString<128> Dir(*StdlibIncDir);
1028 llvm::sys::path::append(path&: Dir, a: M.includeSuffix());
1029 if (getDriver().getVFS().exists(Path: Dir))
1030 addSystemInclude(DriverArgs, CC1Args, Path: Dir);
1031 }
1032 }
1033
1034 if (std::optional<std::string> Path = getStdlibIncludePath())
1035 addSystemInclude(DriverArgs, CC1Args, Path: *Path);
1036}
1037
1038StringRef
1039AMDGPUToolChain::getGPUArch(const llvm::opt::ArgList &DriverArgs) const {
1040 return getProcessorFromTargetID(
1041 T: getTriple(), OffloadArch: DriverArgs.getLastArgValue(Id: options::OPT_mcpu_EQ));
1042}
1043
1044AMDGPUToolChain::ParsedTargetIDType
1045AMDGPUToolChain::getParsedTargetID(const llvm::opt::ArgList &DriverArgs) const {
1046 StringRef TargetID = DriverArgs.getLastArgValue(Id: options::OPT_mcpu_EQ);
1047 if (TargetID.empty())
1048 return {};
1049
1050 llvm::StringMap<bool> FeatureMap;
1051 auto OptionalGpuArch = parseTargetID(T: getTriple(), OffloadArch: TargetID, FeatureMap: &FeatureMap);
1052 if (!OptionalGpuArch)
1053 return {.OptionalTargetID: TargetID.str(), .OptionalGPUArch: std::nullopt, .OptionalFeatureMap: std::nullopt};
1054
1055 return {.OptionalTargetID: TargetID.str(), .OptionalGPUArch: OptionalGpuArch->str(), .OptionalFeatureMap: FeatureMap};
1056}
1057
1058AMDGPUToolChain::ParsedTargetIDType
1059AMDGPUToolChain::checkTargetID(const llvm::opt::ArgList &DriverArgs) const {
1060 auto PTID = getParsedTargetID(DriverArgs);
1061 if (PTID.OptionalTargetID && !PTID.OptionalGPUArch) {
1062 getDriver().Diag(DiagID: clang::diag::err_drv_bad_target_id)
1063 << *PTID.OptionalTargetID;
1064 return PTID;
1065 }
1066
1067 if (getTriple().getSubArch() != llvm::Triple::NoSubArch &&
1068 PTID.OptionalGPUArch) {
1069 llvm::AMDGPU::GPUKind Kind =
1070 llvm::AMDGPU::parseArchAMDGCN(CPU: *PTID.OptionalGPUArch);
1071 llvm::Triple::SubArchType KindSubArch =
1072 static_cast<llvm::Triple::SubArchType>(llvm::AMDGPU::getSubArch(AK: Kind));
1073 if (getTriple().getSubArch() != KindSubArch &&
1074 getTriple().getSubArch() !=
1075 llvm::AMDGPU::getMajorSubArch(SubArch: KindSubArch)) {
1076 getDriver().Diag(DiagID: clang::diag::err_target_unsupported_arch)
1077 << *PTID.OptionalGPUArch << getTriple().getArchName();
1078 }
1079 }
1080 return PTID;
1081}
1082
1083Expected<SmallVector<std::string>>
1084AMDGPUToolChain::getSystemGPUArchs(const ArgList &Args) const {
1085 // Detect AMD GPUs availible on the system.
1086 std::string Program;
1087 if (Arg *A = Args.getLastArg(Ids: options::OPT_offload_arch_tool_EQ))
1088 Program = A->getValue();
1089 else
1090 Program = GetProgramPath(Name: "amdgpu-arch");
1091
1092 auto StdoutOrErr = getDriver().executeProgram(Args: {Program});
1093 if (!StdoutOrErr)
1094 return StdoutOrErr.takeError();
1095
1096 SmallVector<std::string, 1> GPUArchs;
1097 for (StringRef Arch : llvm::split(Str: (*StdoutOrErr)->getBuffer(), Separator: "\n"))
1098 if (!Arch.empty())
1099 GPUArchs.push_back(Elt: Arch.str());
1100
1101 if (GPUArchs.empty())
1102 return llvm::createStringError(EC: std::error_code(),
1103 S: "No AMD GPU detected in the system");
1104
1105 return std::move(GPUArchs);
1106}
1107
1108bool RocmInstallationDetector::checkCommonBitcodeLibs(
1109 StringRef GPUArch, StringRef LibDeviceFile,
1110 DeviceLibABIVersion ABIVer) const {
1111 if (!hasDeviceLibrary()) {
1112 D.Diag(DiagID: diag::err_drv_no_rocm_device_lib) << 0;
1113 return false;
1114 }
1115 if (LibDeviceFile.empty()) {
1116 D.Diag(DiagID: diag::err_drv_no_rocm_device_lib) << 1 << GPUArch;
1117 return false;
1118 }
1119 if (ABIVer.requiresLibrary() && getABIVersionPath(ABIVer).empty()) {
1120 // Starting from COV6, we will report minimum ROCm version requirement in
1121 // the error message.
1122 if (ABIVer.getAsCodeObjectVersion() < 6)
1123 D.Diag(DiagID: diag::err_drv_no_rocm_device_lib) << 2 << ABIVer.toString() << 0;
1124 else
1125 D.Diag(DiagID: diag::err_drv_no_rocm_device_lib)
1126 << 2 << ABIVer.toString() << 1 << "6.3";
1127 return false;
1128 }
1129 return true;
1130}
1131
1132llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
1133RocmInstallationDetector::getCommonBitcodeLibs(
1134 const llvm::opt::ArgList &DriverArgs, StringRef LibDeviceFile,
1135 StringRef GPUArch, const Action::OffloadKind DeviceOffloadingKind,
1136 const bool NeedsASanRT) const {
1137 llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12> BCLibs;
1138
1139 CommonBitcodeLibsPreferences Pref{D, DriverArgs, GPUArch,
1140 DeviceOffloadingKind, NeedsASanRT};
1141
1142 auto AddBCLib = [&](ToolChain::BitCodeLibraryInfo BCLib,
1143 bool Internalize = true) {
1144 if (!BCLib.Path.empty()) {
1145 BCLib.ShouldInternalize = Internalize;
1146 BCLibs.emplace_back(Args&: BCLib);
1147 }
1148 };
1149 auto AddSanBCLibs = [&]() {
1150 if (Pref.GPUSan)
1151 AddBCLib(getAsanRTLPath(), false);
1152 };
1153
1154 AddSanBCLibs();
1155 AddBCLib(getOCMLPath());
1156 if (!Pref.IsOpenMP)
1157 AddBCLib(getOCKLPath());
1158 else if (Pref.GPUSan && Pref.IsOpenMP)
1159 AddBCLib(getOCKLPath());
1160 AddBCLib(getUnsafeMathPath(Enabled: Pref.UnsafeMathOpt || Pref.FastRelaxedMath));
1161 AddBCLib(getFiniteOnlyPath(Enabled: Pref.FiniteOnly || Pref.FastRelaxedMath));
1162 AddBCLib(getWavefrontSize64Path(Enabled: Pref.Wave64));
1163 AddBCLib(LibDeviceFile);
1164 auto ABIVerPath = getABIVersionPath(ABIVer: Pref.ABIVer);
1165 if (!ABIVerPath.empty())
1166 AddBCLib(ABIVerPath);
1167
1168 return BCLibs;
1169}
1170
1171llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
1172AMDGPUToolChain::getCommonDeviceLibNames(
1173 const llvm::opt::ArgList &DriverArgs, llvm::StringRef TargetID,
1174 llvm::StringRef GPUArch, Action::OffloadKind DeviceOffloadingKind) const {
1175 auto Kind = llvm::AMDGPU::parseArchAMDGCN(CPU: GPUArch);
1176 const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(AK: Kind);
1177
1178 StringRef LibDeviceFile = RocmInstallation->getLibDeviceFile(Gpu: CanonArch);
1179 auto ABIVer = DeviceLibABIVersion::fromCodeObjectVersion(
1180 CodeObjectVersion: getAMDGPUCodeObjectVersion(D: getDriver(), Args: DriverArgs));
1181 if (!RocmInstallation->checkCommonBitcodeLibs(GPUArch: CanonArch, LibDeviceFile,
1182 ABIVer))
1183 return {};
1184
1185 return RocmInstallation->getCommonBitcodeLibs(
1186 DriverArgs, LibDeviceFile, GPUArch, DeviceOffloadingKind,
1187 NeedsASanRT: getSanitizerArgs(JobArgs: DriverArgs, BA: BoundArch(TargetID), DeviceOffloadKind: DeviceOffloadingKind)
1188 .needsAsanRt());
1189}
1190
1191llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
1192AMDGPUToolChain::getDeviceLibs(const llvm::opt::ArgList &DriverArgs,
1193 BoundArch BA,
1194 Action::OffloadKind DeviceOffloadKind) const {
1195 assert(getEffectiveTriple().isAMDGPU() &&
1196 "spirv should not try to link device libs");
1197
1198 if (!DriverArgs.hasFlag(Pos: options::OPT_offloadlib, Neg: options::OPT_no_offloadlib,
1199 Default: true) ||
1200 getEffectiveTriple().getEnvironment() == llvm::Triple::LLVM)
1201 return {};
1202
1203 if (getTriple().getOS() != llvm::Triple::AMDHSA)
1204 return {};
1205
1206 StringRef GpuArch;
1207 StringRef TargetID;
1208 if (DeviceOffloadKind == Action::OFK_None) {
1209 TargetID = DriverArgs.getLastArgValue(Id: options::OPT_mcpu_EQ);
1210 GpuArch = getProcessorFromTargetID(T: getTriple(), OffloadArch: TargetID);
1211 } else {
1212 TargetID = BA.ArchName;
1213 GpuArch = getProcessorFromTargetID(T: getTriple(), OffloadArch: BA.ArchName);
1214 }
1215
1216 llvm::SmallVector<BitCodeLibraryInfo, 12> BCLibs;
1217
1218 // HIP-specific handling
1219 if (DeviceOffloadKind == Action::OFK_HIP) {
1220 // Handle --hip-device-lib manual override
1221 auto BCLibArgs = DriverArgs.getAllArgValues(Id: options::OPT_hip_device_lib_EQ);
1222 if (!BCLibArgs.empty()) {
1223 ArgStringList LibraryPaths;
1224 for (StringRef Path : RocmInstallation->getRocmDeviceLibPathArg())
1225 LibraryPaths.push_back(Elt: DriverArgs.MakeArgStringRef(Str: Path));
1226 addDirectoryList(Args: DriverArgs, CmdArgs&: LibraryPaths, ArgName: "", EnvVar: "HIP_DEVICE_LIB_PATH");
1227
1228 for (StringRef BCName : BCLibArgs) {
1229 bool Found = false;
1230 for (StringRef LibraryPath : LibraryPaths) {
1231 SmallString<128> Path(LibraryPath);
1232 llvm::sys::path::append(path&: Path, a: BCName);
1233 if (llvm::sys::fs::exists(Path)) {
1234 BCLibs.emplace_back(Args&: Path);
1235 Found = true;
1236 break;
1237 }
1238 }
1239 if (!Found)
1240 getDriver().Diag(DiagID: diag::err_drv_no_such_file) << BCName;
1241 }
1242 return BCLibs;
1243 }
1244
1245 if (!RocmInstallation->hasDeviceLibrary()) {
1246 getDriver().Diag(DiagID: diag::err_drv_no_rocm_device_lib) << 0;
1247 return {};
1248 }
1249
1250 // Add common device libraries
1251 for (auto N : getCommonDeviceLibNames(DriverArgs, TargetID, GPUArch: GpuArch,
1252 DeviceOffloadingKind: DeviceOffloadKind))
1253 BCLibs.emplace_back(Args&: N);
1254
1255 // Add instrument lib for HIP
1256 auto InstLib =
1257 DriverArgs.getLastArgValue(Id: options::OPT_gpu_instrument_lib_EQ);
1258 if (!InstLib.empty()) {
1259 if (llvm::sys::fs::exists(Path: InstLib))
1260 BCLibs.emplace_back(Args&: InstLib);
1261 else
1262 getDriver().Diag(DiagID: diag::err_drv_no_such_file) << InstLib;
1263 }
1264
1265 return BCLibs;
1266 }
1267
1268 // OpenMP handling
1269 if (DeviceOffloadKind == Action::OFK_OpenMP) {
1270 for (auto BCLib : getCommonDeviceLibNames(DriverArgs, TargetID, GPUArch: GpuArch,
1271 DeviceOffloadingKind: DeviceOffloadKind))
1272 BCLibs.emplace_back(Args&: BCLib);
1273 return BCLibs;
1274 }
1275
1276 // The libraries are currently only built for amdhsa.
1277 if (getTriple().getOS() != llvm::Triple::AMDHSA)
1278 return {};
1279
1280 // Only link device libraries if requested (set by Driver based on input type)
1281 if (!ShouldLinkDeviceLibs)
1282 return {};
1283
1284 StringRef LibDeviceFile = RocmInstallation->getLibDeviceFile(Gpu: GpuArch);
1285
1286 auto ABIVer = DeviceLibABIVersion::fromCodeObjectVersion(
1287 CodeObjectVersion: getAMDGPUCodeObjectVersion(D: getDriver(), Args: DriverArgs));
1288 if (!RocmInstallation->checkCommonBitcodeLibs(GPUArch: GpuArch, LibDeviceFile, ABIVer))
1289 return {};
1290
1291 // Add the OpenCL specific bitcode library
1292 BCLibs.emplace_back(Args: RocmInstallation->getOpenCLPath().str());
1293
1294 // Add the generic set of libraries
1295 BCLibs.append(RHS: RocmInstallation->getCommonBitcodeLibs(
1296 DriverArgs, LibDeviceFile, GPUArch: GpuArch, DeviceOffloadingKind: DeviceOffloadKind,
1297 NeedsASanRT: getSanitizerArgs(JobArgs: DriverArgs, BA: BoundArch{TargetID}, DeviceOffloadKind)
1298 .needsAsanRt()));
1299
1300 return BCLibs;
1301}
1302
1303ToolChain::CXXStdlibType
1304AMDGPUToolChain::GetCXXStdlibType(const ArgList &Args) const {
1305 if (HostTC)
1306 return HostTC->GetCXXStdlibType(Args);
1307 return ToolChain::GetCXXStdlibType(Args);
1308}
1309
1310void AMDGPUToolChain::AddClangCXXStdlibIncludeArgs(
1311 const ArgList &Args, ArgStringList &CC1Args) const {
1312 if (HostTC)
1313 HostTC->AddClangCXXStdlibIncludeArgs(DriverArgs: Args, CC1Args);
1314 else
1315 ToolChain::AddClangCXXStdlibIncludeArgs(DriverArgs: Args, CC1Args);
1316}
1317
1318void AMDGPUToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
1319 ArgStringList &CC1Args) const {
1320 if (HostTC)
1321 HostTC->AddIAMCUIncludeArgs(DriverArgs: Args, CC1Args);
1322}
1323
1324void AMDGPUToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs,
1325 ArgStringList &CC1Args) const {
1326 if (getTriple().getEnvironment() == llvm::Triple::LLVM) {
1327 if (DriverArgs.hasFlag(Pos: options::OPT_offload_inc,
1328 Neg: options::OPT_no_offload_inc, Default: true) &&
1329 !DriverArgs.hasArg(Ids: options::OPT_nohipwrapperinc) &&
1330 !DriverArgs.hasArg(Ids: options::OPT_nobuiltininc)) {
1331 SmallString<128> P(getDriver().ResourceDir);
1332 llvm::sys::path::append(path&: P, a: "include", b: "hip_wrappers");
1333 CC1Args.push_back(Elt: "-internal-isystem");
1334 CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: P));
1335 }
1336 return;
1337 }
1338
1339 RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args);
1340}
1341
1342VersionTuple AMDGPUToolChain::computeMSVCVersion(const Driver *D,
1343 const ArgList &Args) const {
1344 if (HostTC)
1345 return HostTC->computeMSVCVersion(D, Args);
1346 return ToolChain::computeMSVCVersion(D, Args);
1347}
1348
1349LTOKind AMDGPUToolChain::getDefaultLTOMode() const {
1350 // Offload toolchains use full LTO by default.
1351 return HostTC == nullptr ? LTOK_None : LTOK_Full;
1352}
1353
1354LTOKind AMDGPUToolChain::getLTOMode(const ArgList &Args,
1355 Action::OffloadKind Kind) const {
1356 if (getTriple().isAMDGCN() && getDriver().offloadDeviceOnly() &&
1357 !Args.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc, Default: false) &&
1358 !Args.hasArg(Ids: options::OPT_foffload_lto, Ids: options::OPT_foffload_lto_EQ))
1359 return LTOK_None;
1360 return ToolChain::getLTOMode(Args, Kind);
1361}
1362
1363static bool isXnackAvailable(const llvm::Triple &TT, llvm::StringRef TargetID) {
1364 // Arch-specific check - only report as supported if arch has xnack+
1365 if (!TT.isAMDGCN())
1366 return false;
1367 llvm::StringRef Processor = getProcessorFromTargetID(T: TT, OffloadArch: TargetID);
1368 llvm::AMDGPU::GPUKind ProcKind = llvm::AMDGPU::parseArchAMDGCN(CPU: Processor);
1369 const llvm::AMDGPU::AMDGPUFeatureBitset &Features =
1370 llvm::AMDGPU::getFeatureBitset(AK: ProcKind);
1371
1372 // If processor has xnack but doesn't support on/off modes, xnack is always on
1373 bool XnackAlwaysOn = Features.test(I: llvm::AMDGPU::FEAT_XNACK_SUPPORT) &&
1374 !Features.test(I: llvm::AMDGPU::FEAT_XNACK_ON_OFF_MODES);
1375 if (XnackAlwaysOn)
1376 return true;
1377
1378 // Otherwise, check if xnack+ is explicitly enabled in the target ID
1379 llvm::StringMap<bool> FeatureMap;
1380 auto OptionalGpuArch = parseTargetID(T: TT, OffloadArch: TargetID, FeatureMap: &FeatureMap);
1381 if (!OptionalGpuArch)
1382 return false;
1383 auto Loc = FeatureMap.find(Key: "xnack");
1384 return (Loc != FeatureMap.end() && Loc->second);
1385}
1386
1387SanitizerMask AMDGPUToolChain::getSupportedSanitizers(
1388 BoundArch BA, Action::OffloadKind DeviceOffloadKind) const {
1389 SanitizerMask SupportedMask =
1390 ToolChain::getSupportedSanitizers(BA, DeviceOffloadKind);
1391
1392 // Address sanitizer is potentially supported, but depends on the exact target
1393 // arch xnack support.
1394 if (!BA || isXnackAvailable(TT: getTriple(), TargetID: BA.ArchName))
1395 SupportedMask |= SanitizerKind::Address;
1396
1397 return SupportedMask;
1398}
1399
1400StringRef AMDGPUToolChain::getSanitizerRequirement(SanitizerMask Kinds,
1401 BoundArch BA) const {
1402 // Address sanitizer requires xnack+ feature
1403 if ((Kinds & SanitizerKind::Address) && BA &&
1404 !isXnackAvailable(TT: getTriple(), TargetID: BA.ArchName)) {
1405 return "xnack+";
1406 }
1407 return "";
1408}
1409