12#include "clang/Config/config.h"
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/Option/ArgList.h"
20#include "llvm/Support/Error.h"
21#include "llvm/Support/LineIterator.h"
22#include "llvm/Support/Path.h"
23#include "llvm/Support/Process.h"
24#include "llvm/Support/VirtualFileSystem.h"
25#include "llvm/TargetParser/Host.h"
27#include <system_error>
42RocmInstallationDetector::findSPACKPackage(
const Candidate &Cand,
43 StringRef PackageName) {
47 std::string Prefix = Twine(PackageName +
"-" + Cand.SPACKReleaseStr).str();
49 for (llvm::vfs::directory_iterator
File = D.
getVFS().dir_begin(Cand.Path, EC),
51 File != FileEnd && !EC;
File.increment(EC)) {
52 llvm::StringRef
FileName = llvm::sys::path::filename(
File->path());
55 if (SubDirs.size() > 1)
59 if (SubDirs.size() == 1) {
60 auto PackagePath = Cand.Path;
61 llvm::sys::path::append(PackagePath, SubDirs[0]);
64 if (SubDirs.size() == 0 && Verbose) {
65 llvm::errs() <<
"SPACK package " << Prefix <<
" not found at " << Cand.Path
70 if (SubDirs.size() > 1 && Verbose) {
71 llvm::errs() <<
"Cannot use SPACK package " << Prefix <<
" at " << Cand.Path
72 <<
" due to multiple installations for the same version\n";
77void RocmInstallationDetector::scanLibDevicePath(llvm::StringRef
Path) {
78 assert(!
Path.empty());
80 const StringRef Suffix(
".bc");
81 const StringRef Suffix2(
".amdgcn.bc");
84 for (llvm::vfs::directory_iterator LI = D.
getVFS().dir_begin(
Path, EC), LE;
85 !EC && LI != LE; LI = LI.increment(EC)) {
86 StringRef FilePath = LI->path();
87 StringRef
FileName = llvm::sys::path::filename(FilePath);
93 BaseName =
FileName.drop_back(Suffix2.size());
95 BaseName =
FileName.drop_back(Suffix.size());
97 const StringRef ABIVersionPrefix =
"oclc_abi_version_";
98 if (BaseName ==
"ocml") {
100 }
else if (BaseName ==
"ockl") {
102 }
else if (BaseName ==
"opencl") {
104 }
else if (BaseName ==
"hip") {
106 }
else if (BaseName ==
"asanrtl") {
108 }
else if (BaseName ==
"oclc_finite_only_off") {
109 FiniteOnly.Off = FilePath;
110 }
else if (BaseName ==
"oclc_finite_only_on") {
111 FiniteOnly.On = FilePath;
112 }
else if (BaseName ==
"oclc_daz_opt_on") {
113 DenormalsAreZero.On = FilePath;
114 }
else if (BaseName ==
"oclc_daz_opt_off") {
115 DenormalsAreZero.Off = FilePath;
116 }
else if (BaseName ==
"oclc_correctly_rounded_sqrt_on") {
117 CorrectlyRoundedSqrt.On = FilePath;
118 }
else if (BaseName ==
"oclc_correctly_rounded_sqrt_off") {
119 CorrectlyRoundedSqrt.Off = FilePath;
120 }
else if (BaseName ==
"oclc_unsafe_math_on") {
121 UnsafeMath.On = FilePath;
122 }
else if (BaseName ==
"oclc_unsafe_math_off") {
123 UnsafeMath.Off = FilePath;
124 }
else if (BaseName ==
"oclc_wavefrontsize64_on") {
125 WavefrontSize64.On = FilePath;
126 }
else if (BaseName ==
"oclc_wavefrontsize64_off") {
127 WavefrontSize64.Off = FilePath;
128 }
else if (BaseName.starts_with(ABIVersionPrefix)) {
129 unsigned ABIVersionNumber;
130 if (BaseName.drop_front(ABIVersionPrefix.size())
131 .getAsInteger(0, ABIVersionNumber))
133 ABIVersionMap[ABIVersionNumber] = FilePath.str();
137 const StringRef DeviceLibPrefix =
"oclc_isa_version_";
138 if (!BaseName.starts_with(DeviceLibPrefix))
141 StringRef IsaVersionNumber =
142 BaseName.drop_front(DeviceLibPrefix.size());
144 llvm::Twine GfxName = Twine(
"gfx") + IsaVersionNumber;
147 std::make_pair(GfxName.toStringRef(Tmp), FilePath.str()));
154bool RocmInstallationDetector::parseHIPVersionFile(llvm::StringRef
V) {
156 V.split(VersionParts,
'\n');
157 unsigned Major = ~0
U;
158 unsigned Minor = ~0
U;
159 for (
auto Part : VersionParts) {
160 auto Splits = Part.rtrim().split(
'=');
161 if (Splits.first ==
"HIP_VERSION_MAJOR") {
162 if (Splits.second.getAsInteger(0, Major))
164 }
else if (Splits.first ==
"HIP_VERSION_MINOR") {
165 if (Splits.second.getAsInteger(0, Minor))
167 }
else if (Splits.first ==
"HIP_VERSION_PATCH")
168 VersionPatch = Splits.second.str();
170 if (Major == ~0
U || Minor == ~0
U)
172 VersionMajorMinor = llvm::VersionTuple(Major, Minor);
174 (Twine(Major) +
"." + Twine(Minor) +
"." + VersionPatch).str();
181RocmInstallationDetector::getInstallationPathCandidates() {
184 if (!ROCmSearchDirs.empty())
185 return ROCmSearchDirs;
187 auto DoPrintROCmSearchDirs = [&]() {
188 if (PrintROCmSearchDirs)
189 for (
auto Cand : ROCmSearchDirs) {
190 llvm::errs() <<
"ROCm installation search path";
192 llvm::errs() <<
" (Spack " << Cand.SPACKReleaseStr <<
")";
193 llvm::errs() <<
": " << Cand.Path <<
'\n';
199 if (!RocmPathArg.empty()) {
200 ROCmSearchDirs.emplace_back(RocmPathArg.str());
201 DoPrintROCmSearchDirs();
202 return ROCmSearchDirs;
203 }
else if (std::optional<std::string> RocmPathEnv =
204 llvm::sys::Process::GetEnv(
"ROCM_PATH")) {
205 if (!RocmPathEnv->empty()) {
206 ROCmSearchDirs.emplace_back(std::move(*RocmPathEnv));
207 DoPrintROCmSearchDirs();
208 return ROCmSearchDirs;
213 StringRef InstallDir = D.
Dir;
218 auto DeduceROCmPath = [](StringRef ClangPath) {
220 StringRef ParentDir = llvm::sys::path::parent_path(ClangPath);
221 StringRef ParentName = llvm::sys::path::filename(ParentDir);
224 if (ParentName ==
"bin") {
225 ParentDir = llvm::sys::path::parent_path(ParentDir);
226 ParentName = llvm::sys::path::filename(ParentDir);
234 if (ParentName.starts_with(
"llvm-amdgpu-")) {
236 ParentName.drop_front(strlen(
"llvm-amdgpu-")).split(
'-');
237 auto SPACKReleaseStr = SPACKPostfix.first;
238 if (!SPACKReleaseStr.empty()) {
239 ParentDir = llvm::sys::path::parent_path(ParentDir);
240 return Candidate(ParentDir.str(),
true,
247 if (ParentName ==
"llvm" || ParentName.starts_with(
"aomp"))
248 ParentDir = llvm::sys::path::parent_path(ParentDir);
250 return Candidate(ParentDir.str(),
true);
255 ROCmSearchDirs.emplace_back(DeduceROCmPath(InstallDir));
261 auto ParentPath = llvm::sys::path::parent_path(RealClangPath);
262 if (ParentPath != InstallDir)
263 ROCmSearchDirs.emplace_back(DeduceROCmPath(ParentPath));
266 auto ClangRoot = llvm::sys::path::parent_path(InstallDir);
267 auto RealClangRoot = llvm::sys::path::parent_path(ParentPath);
268 ROCmSearchDirs.emplace_back(ClangRoot.str(),
true);
269 if (RealClangRoot != ClangRoot)
270 ROCmSearchDirs.emplace_back(RealClangRoot.str(),
true);
274 ROCmSearchDirs.emplace_back(D.
SysRoot +
"/opt/rocm",
279 std::string LatestROCm;
280 llvm::VersionTuple LatestVer;
282 auto GetROCmVersion = [](StringRef DirName) {
283 llvm::VersionTuple
V;
284 std::string VerStr = DirName.drop_front(strlen(
"rocm-")).str();
287 std::replace(VerStr.begin(), VerStr.end(),
'-',
'.');
291 for (llvm::vfs::directory_iterator
294 File != FileEnd && !EC;
File.increment(EC)) {
295 llvm::StringRef
FileName = llvm::sys::path::filename(
File->path());
298 if (LatestROCm.empty()) {
300 LatestVer = GetROCmVersion(LatestROCm);
303 auto Ver = GetROCmVersion(
FileName);
304 if (LatestVer < Ver) {
309 if (!LatestROCm.empty())
310 ROCmSearchDirs.emplace_back(D.
SysRoot +
"/opt/" + LatestROCm,
313 ROCmSearchDirs.emplace_back(D.
SysRoot +
"/usr/local",
315 ROCmSearchDirs.emplace_back(D.
SysRoot +
"/usr",
318 DoPrintROCmSearchDirs();
319 return ROCmSearchDirs;
323 const Driver &
D,
const llvm::Triple &HostTriple,
324 const llvm::opt::ArgList &Args,
bool DetectHIPRuntime,
bool DetectDeviceLib)
326 Verbose = Args.hasArg(options::OPT_v);
327 RocmPathArg = Args.getLastArgValue(clang::driver::options::OPT_rocm_path_EQ);
328 PrintROCmSearchDirs =
329 Args.hasArg(clang::driver::options::OPT_print_rocm_search_dirs);
330 RocmDeviceLibPathArg =
331 Args.getAllArgValues(clang::driver::options::OPT_rocm_device_lib_path_EQ);
332 HIPPathArg = Args.getLastArgValue(clang::driver::options::OPT_hip_path_EQ);
334 Args.getLastArgValue(clang::driver::options::OPT_hipstdpar_path_EQ);
335 HasHIPStdParLibrary =
336 !HIPStdParPathArg.empty() &&
D.getVFS().exists(HIPStdParPathArg +
337 "/hipstdpar_lib.hpp");
338 HIPRocThrustPathArg =
339 Args.getLastArgValue(clang::driver::options::OPT_hipstdpar_thrust_path_EQ);
340 HasRocThrustLibrary = !HIPRocThrustPathArg.empty() &&
341 D.getVFS().exists(HIPRocThrustPathArg +
"/thrust");
343 Args.getLastArgValue(clang::driver::options::OPT_hipstdpar_prim_path_EQ);
344 HasRocPrimLibrary = !HIPRocPrimPathArg.empty() &&
345 D.getVFS().exists(HIPRocPrimPathArg +
"/rocprim");
347 if (
auto *A = Args.getLastArg(clang::driver::options::OPT_hip_version_EQ)) {
348 HIPVersionArg = A->getValue();
349 unsigned Major = ~0
U;
350 unsigned Minor = ~0
U;
352 HIPVersionArg.split(Parts,
'.');
354 Parts[0].getAsInteger(0, Major);
355 if (Parts.size() > 1)
356 Parts[1].getAsInteger(0, Minor);
357 if (Parts.size() > 2)
358 VersionPatch = Parts[2].str();
359 if (VersionPatch.empty())
361 if (Major != ~0
U && Minor == ~0
U)
363 if (Major == ~0
U || Minor == ~0
U)
364 D.Diag(diag::err_drv_invalid_value)
365 << A->getAsString(Args) << HIPVersionArg;
367 VersionMajorMinor = llvm::VersionTuple(Major, Minor);
369 (Twine(Major) +
"." + Twine(Minor) +
"." + VersionPatch).str();
371 VersionPatch = DefaultVersionPatch;
373 llvm::VersionTuple(DefaultVersionMajor, DefaultVersionMinor);
374 DetectedVersion = (Twine(DefaultVersionMajor) +
"." +
375 Twine(DefaultVersionMinor) +
"." + VersionPatch)
379 if (DetectHIPRuntime)
386 assert(LibDevicePath.empty());
388 if (!RocmDeviceLibPathArg.empty())
389 LibDevicePath = RocmDeviceLibPathArg[RocmDeviceLibPathArg.size() - 1];
390 else if (std::optional<std::string> LibPathEnv =
391 llvm::sys::Process::GetEnv(
"HIP_DEVICE_LIB_PATH"))
392 LibDevicePath = std::move(*LibPathEnv);
395 if (!LibDevicePath.empty()) {
399 if (!FS.exists(LibDevicePath))
402 scanLibDevicePath(LibDevicePath);
403 HasDeviceLibrary = allGenericLibsValid() && !LibDeviceMap.empty();
408 auto CheckDeviceLib = [&](StringRef
Path,
bool StrictChecking) {
409 bool CheckLibDevice = (!NoBuiltinLibs || StrictChecking);
410 if (CheckLibDevice && !FS.exists(
Path))
413 scanLibDevicePath(
Path);
415 if (!NoBuiltinLibs) {
417 if (!allGenericLibsValid())
422 if (LibDeviceMap.empty())
430 llvm::sys::path::append(LibDevicePath, CLANG_INSTALL_LIBDIR_BASENAME,
431 "amdgcn",
"bitcode");
432 HasDeviceLibrary = CheckDeviceLib(LibDevicePath,
true);
433 if (HasDeviceLibrary)
438 auto &ROCmDirs = getInstallationPathCandidates();
439 for (
const auto &Candidate : ROCmDirs) {
440 LibDevicePath = Candidate.Path;
441 llvm::sys::path::append(LibDevicePath,
"amdgcn",
"bitcode");
442 HasDeviceLibrary = CheckDeviceLib(LibDevicePath, Candidate.StrictChecking);
443 if (HasDeviceLibrary)
450 if (!HIPPathArg.empty())
451 HIPSearchDirs.emplace_back(HIPPathArg.str());
452 else if (std::optional<std::string> HIPPathEnv =
453 llvm::sys::Process::GetEnv(
"HIP_PATH")) {
454 if (!HIPPathEnv->empty())
455 HIPSearchDirs.emplace_back(std::move(*HIPPathEnv));
457 if (HIPSearchDirs.empty())
458 HIPSearchDirs.append(getInstallationPathCandidates());
461 for (
const auto &Candidate : HIPSearchDirs) {
462 InstallPath = Candidate.Path;
463 if (InstallPath.empty() || !FS.exists(InstallPath))
467 auto SPACKPath = findSPACKPackage(Candidate,
"hip");
468 InstallPath = SPACKPath.empty() ? InstallPath : SPACKPath;
470 BinPath = InstallPath;
471 llvm::sys::path::append(BinPath,
"bin");
472 IncludePath = InstallPath;
473 llvm::sys::path::append(IncludePath,
"include");
474 LibPath = InstallPath;
475 llvm::sys::path::append(LibPath,
"lib");
476 SharePath = InstallPath;
477 llvm::sys::path::append(SharePath,
"share");
480 SmallString<0> ParentSharePath = llvm::sys::path::parent_path(InstallPath);
481 llvm::sys::path::append(ParentSharePath,
"share");
484 const Twine &
c =
"",
const Twine &d =
"") {
486 llvm::sys::path::append(newpath, a,
b,
c, d);
490 std::vector<SmallString<0>> VersionFilePaths = {
491 Append(SharePath,
"hip",
"version"),
492 InstallPath != D.
SysRoot +
"/usr/local"
493 ?
Append(ParentSharePath,
"hip",
"version")
495 Append(BinPath,
".hipVersion")};
497 for (
const auto &VersionFilePath : VersionFilePaths) {
498 if (VersionFilePath.empty())
500 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> VersionFile =
501 FS.getBufferForFile(VersionFilePath);
504 if (HIPVersionArg.empty() && VersionFile)
505 if (parseHIPVersionFile((*VersionFile)->getBuffer()))
508 HasHIPRuntime =
true;
513 if (!Candidate.StrictChecking) {
514 HasHIPRuntime =
true;
518 HasHIPRuntime =
false;
523 OS <<
"Found HIP installation: " << InstallPath <<
", version "
524 << DetectedVersion <<
'\n';
528 ArgStringList &CC1Args)
const {
529 bool UsesRuntimeWrapper = VersionMajorMinor > llvm::VersionTuple(3, 5) &&
530 !DriverArgs.hasArg(options::OPT_nohipwrapperinc);
531 bool HasHipStdPar = DriverArgs.hasArg(options::OPT_hipstdpar);
533 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
548 if (UsesRuntimeWrapper)
549 llvm::sys::path::append(
P,
"include",
"cuda_wrappers");
550 CC1Args.push_back(
"-internal-isystem");
551 CC1Args.push_back(DriverArgs.MakeArgString(
P));
554 const auto HandleHipStdPar = [=, &DriverArgs, &CC1Args]() {
559 if (!HIPStdParPathArg.empty() ||
560 !FS.exists(Inc +
"/thrust/system/hip/hipstdpar/hipstdpar_lib.hpp")) {
561 D.
Diag(diag::err_drv_no_hipstdpar_lib);
564 if (!HasRocThrustLibrary && !FS.exists(Inc +
"/thrust")) {
565 D.
Diag(diag::err_drv_no_hipstdpar_thrust_lib);
568 if (!HasRocPrimLibrary && !FS.exists(Inc +
"/rocprim")) {
569 D.
Diag(diag::err_drv_no_hipstdpar_prim_lib);
572 const char *ThrustPath;
573 if (HasRocThrustLibrary)
574 ThrustPath = DriverArgs.MakeArgString(HIPRocThrustPathArg);
576 ThrustPath = DriverArgs.MakeArgString(Inc +
"/thrust");
578 const char *HIPStdParPath;
580 HIPStdParPath = DriverArgs.MakeArgString(HIPStdParPathArg);
582 HIPStdParPath = DriverArgs.MakeArgString(StringRef(ThrustPath) +
583 "/system/hip/hipstdpar");
585 const char *PrimPath;
586 if (HasRocPrimLibrary)
587 PrimPath = DriverArgs.MakeArgString(HIPRocPrimPathArg);
589 PrimPath = DriverArgs.MakeArgString(
getIncludePath() +
"/rocprim");
591 CC1Args.append({
"-idirafter", ThrustPath,
"-idirafter", PrimPath,
592 "-idirafter", HIPStdParPath,
"-include",
593 "hipstdpar_lib.hpp"});
596 if (DriverArgs.hasArg(options::OPT_nogpuinc)) {
604 D.
Diag(diag::err_drv_no_hip_runtime);
608 CC1Args.push_back(
"-idirafter");
610 if (UsesRuntimeWrapper)
611 CC1Args.append({
"-include",
"__clang_hip_runtime_wrapper.h"});
620 const char *LinkingOutput)
const {
621 std::string
Linker = getToolChain().GetLinkerPath();
622 ArgStringList CmdArgs;
623 CmdArgs.push_back(
"--no-undefined");
624 CmdArgs.push_back(
"-shared");
627 Args.AddAllArgs(CmdArgs, options::OPT_L);
628 getToolChain().AddFilePathLibArgs(Args, CmdArgs);
630 if (
C.getDriver().isUsingLTO())
631 addLTOOptions(getToolChain(), Args, CmdArgs, Output, Inputs[0],
633 else if (Args.hasArg(options::OPT_mcpu_EQ))
634 CmdArgs.push_back(Args.MakeArgString(
635 "-plugin-opt=mcpu=" + Args.getLastArgValue(options::OPT_mcpu_EQ)));
639 CmdArgs.push_back(
"-o");
641 C.addCommand(std::make_unique<Command>(
643 CmdArgs, Inputs, Output));
647 const llvm::Triple &Triple,
648 const llvm::opt::ArgList &Args,
649 std::vector<StringRef> &Features) {
653 if (Args.hasArg(options::OPT_mcpu_EQ))
654 TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ);
655 else if (Args.hasArg(options::OPT_march_EQ))
656 TargetID = Args.getLastArgValue(options::OPT_march_EQ);
657 if (!TargetID.empty()) {
658 llvm::StringMap<bool> FeatureMap;
659 auto OptionalGpuArch =
parseTargetID(Triple, TargetID, &FeatureMap);
660 if (OptionalGpuArch) {
661 StringRef GpuArch = *OptionalGpuArch;
667 auto Pos = FeatureMap.find(Feature);
668 if (Pos == FeatureMap.end())
670 Features.push_back(Args.MakeArgStringRef(
671 (Twine(Pos->second ?
"+" :
"-") + Feature).str()));
676 if (Args.hasFlag(options::OPT_mwavefrontsize64,
677 options::OPT_mno_wavefrontsize64,
false))
678 Features.push_back(
"+wavefrontsize64");
680 if (Args.hasFlag(options::OPT_mamdgpu_precise_memory_op,
681 options::OPT_mno_amdgpu_precise_memory_op,
false))
682 Features.push_back(
"+precise-memory");
685 options::OPT_m_amdgpu_Features_Group);
693 {{options::OPT_O,
"3"}, {options::OPT_cl_std_EQ,
"CL1.2"}}) {
709 DerivedArgList *DAL =
715 DAL =
new DerivedArgList(Args.getBaseArgs());
721 Arg *LastMCPUArg = DAL->getLastArg(options::OPT_mcpu_EQ);
722 if (LastMCPUArg && StringRef(LastMCPUArg->getValue()) ==
"native") {
723 DAL->eraseArg(options::OPT_mcpu_EQ);
727 << llvm::Triple::getArchTypeName(
getArch())
728 << llvm::toString(GPUsOrErr.takeError()) <<
"-mcpu";
730 auto &GPUs = *GPUsOrErr;
731 if (GPUs.size() > 1) {
733 << llvm::Triple::getArchTypeName(
getArch())
734 << llvm::join(GPUs,
", ") <<
"-mcpu";
736 DAL->AddJoinedArg(
nullptr, Opts.getOption(options::OPT_mcpu_EQ),
737 Args.MakeArgString(GPUs.front()));
743 if (Args.getLastArgValue(options::OPT_x) !=
"cl")
747 if (Args.hasArg(options::OPT_c) && Args.hasArg(options::OPT_emit_llvm)) {
748 DAL->AddFlagArg(
nullptr, Opts.getOption(
getTriple().isArch64Bit()
750 : options::OPT_m32));
754 if (!Args.hasArg(options::OPT_O, options::OPT_O0, options::OPT_O4,
756 DAL->AddJoinedArg(
nullptr, Opts.getOption(options::OPT_O),
764 llvm::AMDGPU::GPUKind Kind) {
767 if (Kind == llvm::AMDGPU::GK_NONE)
770 const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(Kind);
774 const bool BothDenormAndFMAFast =
775 (ArchAttr & llvm::AMDGPU::FEATURE_FAST_FMA_F32) &&
776 (ArchAttr & llvm::AMDGPU::FEATURE_FAST_DENORMAL_F32);
777 return !BothDenormAndFMAFast;
781 const llvm::opt::ArgList &DriverArgs,
const JobAction &JA,
782 const llvm::fltSemantics *FPType)
const {
784 if (!FPType || FPType != &llvm::APFloat::IEEEsingle())
785 return llvm::DenormalMode::getIEEE();
790 auto Kind = llvm::AMDGPU::parseArchAMDGCN(Arch);
791 if (FPType && FPType == &llvm::APFloat::IEEEsingle() &&
792 DriverArgs.hasFlag(options::OPT_fgpu_flush_denormals_to_zero,
793 options::OPT_fno_gpu_flush_denormals_to_zero,
795 return llvm::DenormalMode::getPreserveSign();
797 return llvm::DenormalMode::getIEEE();
800 const StringRef GpuArch =
getGPUArch(DriverArgs);
801 auto Kind = llvm::AMDGPU::parseArchAMDGCN(GpuArch);
805 bool DAZ = DriverArgs.hasArg(options::OPT_cl_denorms_are_zero) ||
810 return DAZ ? llvm::DenormalMode::getPreserveSign() :
811 llvm::DenormalMode::getIEEE();
815 llvm::AMDGPU::GPUKind Kind) {
816 const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(Kind);
817 bool HasWave32 = (ArchAttr & llvm::AMDGPU::FEATURE_WAVE32);
819 return !HasWave32 || DriverArgs.hasFlag(
820 options::OPT_mwavefrontsize64, options::OPT_mno_wavefrontsize64,
false);
832 const llvm::opt::ArgList &DriverArgs,
833 llvm::opt::ArgStringList &CC1Args,
837 if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
838 options::OPT_fvisibility_ms_compat)) {
839 CC1Args.push_back(
"-fvisibility=hidden");
840 CC1Args.push_back(
"-fapply-global-visibility-to-externs");
847 CC1Args.push_back(
"-Werror=atomic-alignment");
853 getTriple(), DriverArgs.getLastArgValue(options::OPT_mcpu_EQ));
858 StringRef TargetID = DriverArgs.getLastArgValue(options::OPT_mcpu_EQ);
859 if (TargetID.empty())
860 return {std::nullopt, std::nullopt, std::nullopt};
862 llvm::StringMap<bool> FeatureMap;
864 if (!OptionalGpuArch)
865 return {TargetID.str(), std::nullopt, std::nullopt};
867 return {TargetID.str(), OptionalGpuArch->str(), FeatureMap};
871 const llvm::opt::ArgList &DriverArgs)
const {
873 if (PTID.OptionalTargetID && !PTID.OptionalGPUArch) {
875 << *PTID.OptionalTargetID;
883 if (Arg *A = Args.getLastArg(options::OPT_amdgpu_arch_tool_EQ))
884 Program = A->getValue();
890 return StdoutOrErr.takeError();
893 for (StringRef Arch : llvm::split((*StdoutOrErr)->getBuffer(),
"\n"))
895 GPUArchs.push_back(Arch.str());
897 if (GPUArchs.empty())
898 return llvm::createStringError(std::error_code(),
899 "No AMD GPU detected in the system");
901 return std::move(GPUArchs);
905 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
908 DeviceOffloadingKind);
913 DriverArgs.hasArg(options::OPT_nostdlib))
916 if (DriverArgs.hasArg(options::OPT_nogpulib))
920 const StringRef GpuArch =
getGPUArch(DriverArgs);
921 auto Kind = llvm::AMDGPU::parseArchAMDGCN(GpuArch);
922 const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(Kind);
930 bool Wave64 =
isWave64(DriverArgs, Kind);
934 bool DAZ = DriverArgs.hasArg(options::OPT_cl_denorms_are_zero) ||
936 bool FiniteOnly = DriverArgs.hasArg(options::OPT_cl_finite_math_only);
939 DriverArgs.hasArg(options::OPT_cl_unsafe_math_optimizations);
940 bool FastRelaxedMath = DriverArgs.hasArg(options::OPT_cl_fast_relaxed_math);
942 DriverArgs.hasArg(options::OPT_cl_fp32_correctly_rounded_divide_sqrt);
950 DriverArgs, LibDeviceFile, Wave64, DAZ, FiniteOnly, UnsafeMathOpt,
951 FastRelaxedMath, CorrectSqrt, ABIVer,
false));
954 CC1Args.push_back(
"-mlink-bitcode-file");
958 for (StringRef BCFile : BCLibs) {
959 CC1Args.push_back(
"-mlink-builtin-bitcode");
960 CC1Args.push_back(DriverArgs.MakeArgString(BCFile));
965 StringRef GPUArch, StringRef LibDeviceFile,
967 if (!hasDeviceLibrary()) {
968 D.Diag(diag::err_drv_no_rocm_device_lib) << 0;
971 if (LibDeviceFile.empty()) {
972 D.Diag(diag::err_drv_no_rocm_device_lib) << 1 << GPUArch;
976 D.Diag(diag::err_drv_no_rocm_device_lib) << 2 << ABIVer.
toString();
984 const llvm::opt::ArgList &DriverArgs, StringRef LibDeviceFile,
bool Wave64,
985 bool DAZ,
bool FiniteOnly,
bool UnsafeMathOpt,
bool FastRelaxedMath,
989 auto AddBCLib = [&](StringRef BCFile) { BCLibs.push_back(BCFile.str()); };
991 AddBCLib(getOCMLPath());
993 AddBCLib(getOCKLPath());
994 AddBCLib(getDenormalsAreZeroPath(DAZ));
995 AddBCLib(getUnsafeMathPath(UnsafeMathOpt || FastRelaxedMath));
996 AddBCLib(getFiniteOnlyPath(FiniteOnly || FastRelaxedMath));
997 AddBCLib(getCorrectlyRoundedSqrtPath(CorrectSqrt));
998 AddBCLib(getWavefrontSize64Path(Wave64));
999 AddBCLib(LibDeviceFile);
1000 auto ABIVerPath = getABIVersionPath(ABIVer);
1001 if (!ABIVerPath.empty())
1002 AddBCLib(ABIVerPath);
1009 const std::string &GPUArch,
1010 bool isOpenMP)
const {
1011 auto Kind = llvm::AMDGPU::parseArchAMDGCN(GPUArch);
1012 const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(Kind);
1024 bool DAZ = DriverArgs.hasFlag(options::OPT_fgpu_flush_denormals_to_zero,
1025 options::OPT_fno_gpu_flush_denormals_to_zero,
1027 bool FiniteOnly = DriverArgs.hasFlag(
1028 options::OPT_ffinite_math_only, options::OPT_fno_finite_math_only,
false);
1029 bool UnsafeMathOpt =
1030 DriverArgs.hasFlag(options::OPT_funsafe_math_optimizations,
1031 options::OPT_fno_unsafe_math_optimizations,
false);
1032 bool FastRelaxedMath = DriverArgs.hasFlag(options::OPT_ffast_math,
1033 options::OPT_fno_fast_math,
false);
1034 bool CorrectSqrt = DriverArgs.hasFlag(
1035 options::OPT_fhip_fp32_correctly_rounded_divide_sqrt,
1036 options::OPT_fno_hip_fp32_correctly_rounded_divide_sqrt,
true);
1037 bool Wave64 =
isWave64(DriverArgs, Kind);
1040 DriverArgs, LibDeviceFile, Wave64, DAZ, FiniteOnly, UnsafeMathOpt,
1041 FastRelaxedMath, CorrectSqrt, ABIVer, isOpenMP);
static void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize, unsigned &BufferCapacity)
__device__ __2f16 float c
const char * getOffloadingArch() const
OffloadKind getOffloadingDeviceKind() const
Compilation - A set of tasks to perform for a single driver invocation.
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
std::string SysRoot
sysroot, if present
const char * getClangProgramPath() const
Get the path to the main clang executable.
DiagnosticBuilder Diag(unsigned DiagID) const
const llvm::opt::OptTable & getOpts() const
std::string ResourceDir
The path to the compiler resource directory.
llvm::vfs::FileSystem & getVFS() const
std::string Dir
The path the driver executable was in, as invoked from the command line.
StringRef getIncludePath() const
Get the detected path to Rocm's bin directory.
RocmInstallationDetector(const Driver &D, const llvm::Triple &HostTriple, const llvm::opt::ArgList &Args, bool DetectHIPRuntime=true, bool DetectDeviceLib=false)
bool checkCommonBitcodeLibs(StringRef GPUArch, StringRef LibDeviceFile, DeviceLibABIVersion ABIVer) const
Check file paths of default bitcode libraries common to AMDGPU based toolchains.
bool hasHIPStdParLibrary() const
Check whether we detected a valid HIP STDPAR Acceleration library.
bool hasHIPRuntime() const
Check whether we detected a valid HIP runtime.
llvm::SmallVector< std::string, 12 > getCommonBitcodeLibs(const llvm::opt::ArgList &DriverArgs, StringRef LibDeviceFile, bool Wave64, bool DAZ, bool FiniteOnly, bool UnsafeMathOpt, bool FastRelaxedMath, bool CorrectSqrt, DeviceLibABIVersion ABIVer, bool isOpenMP) const
Get file paths of default bitcode libraries common to AMDGPU based toolchains.
void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
void detectDeviceLibrary()
void print(raw_ostream &OS) const
Print information about the detected ROCm installation.
The JSON file list parser is used to communicate input to InstallAPI.
std::optional< llvm::StringRef > parseTargetID(const llvm::Triple &T, llvm::StringRef OffloadArch, llvm::StringMap< bool > *FeatureMap)
Parse a target ID to get processor and feature map.
llvm::StringRef getProcessorFromTargetID(const llvm::Triple &T, llvm::StringRef OffloadArch)
Get processor name from target ID.
llvm::SmallVector< llvm::StringRef, 4 > getAllPossibleTargetIDFeatures(const llvm::Triple &T, llvm::StringRef Processor)
Get all feature strings that can be used in target ID for Processor.
ABI version of device library.
static DeviceLibABIVersion fromCodeObjectVersion(unsigned CodeObjectVersion)
bool requiresLibrary()
Whether ABI version bc file is requested.
static constexpr ResponseFileSupport AtFileCurCP()