xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/AMDGPU.cpp (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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 "CommonArgs.h"
11 #include "clang/Basic/TargetID.h"
12 #include "clang/Driver/Compilation.h"
13 #include "clang/Driver/DriverDiagnostic.h"
14 #include "clang/Driver/InputInfo.h"
15 #include "clang/Driver/Options.h"
16 #include "llvm/Option/ArgList.h"
17 #include "llvm/Support/Error.h"
18 #include "llvm/Support/FileUtilities.h"
19 #include "llvm/Support/LineIterator.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/VirtualFileSystem.h"
22 #include <system_error>
23 
24 #define AMDGPU_ARCH_PROGRAM_NAME "amdgpu-arch"
25 
26 using namespace clang::driver;
27 using namespace clang::driver::tools;
28 using namespace clang::driver::toolchains;
29 using namespace clang;
30 using namespace llvm::opt;
31 
32 // Look for sub-directory starts with PackageName under ROCm candidate path.
33 // If there is one and only one matching sub-directory found, append the
34 // sub-directory to Path. If there is no matching sub-directory or there are
35 // more than one matching sub-directories, diagnose them. Returns the full
36 // path of the package if there is only one matching sub-directory, otherwise
37 // returns an empty string.
38 llvm::SmallString<0>
39 RocmInstallationDetector::findSPACKPackage(const Candidate &Cand,
40                                            StringRef PackageName) {
41   if (!Cand.isSPACK())
42     return {};
43   std::error_code EC;
44   std::string Prefix = Twine(PackageName + "-" + Cand.SPACKReleaseStr).str();
45   llvm::SmallVector<llvm::SmallString<0>> SubDirs;
46   for (llvm::vfs::directory_iterator File = D.getVFS().dir_begin(Cand.Path, EC),
47                                      FileEnd;
48        File != FileEnd && !EC; File.increment(EC)) {
49     llvm::StringRef FileName = llvm::sys::path::filename(File->path());
50     if (FileName.startswith(Prefix)) {
51       SubDirs.push_back(FileName);
52       if (SubDirs.size() > 1)
53         break;
54     }
55   }
56   if (SubDirs.size() == 1) {
57     auto PackagePath = Cand.Path;
58     llvm::sys::path::append(PackagePath, SubDirs[0]);
59     return PackagePath;
60   }
61   if (SubDirs.size() == 0 && Verbose) {
62     llvm::errs() << "SPACK package " << Prefix << " not found at " << Cand.Path
63                  << '\n';
64     return {};
65   }
66 
67   if (SubDirs.size() > 1 && Verbose) {
68     llvm::errs() << "Cannot use SPACK package " << Prefix << " at " << Cand.Path
69                  << " due to multiple installations for the same version\n";
70   }
71   return {};
72 }
73 
74 void RocmInstallationDetector::scanLibDevicePath(llvm::StringRef Path) {
75   assert(!Path.empty());
76 
77   const StringRef Suffix(".bc");
78   const StringRef Suffix2(".amdgcn.bc");
79 
80   std::error_code EC;
81   for (llvm::vfs::directory_iterator LI = D.getVFS().dir_begin(Path, EC), LE;
82        !EC && LI != LE; LI = LI.increment(EC)) {
83     StringRef FilePath = LI->path();
84     StringRef FileName = llvm::sys::path::filename(FilePath);
85     if (!FileName.endswith(Suffix))
86       continue;
87 
88     StringRef BaseName;
89     if (FileName.endswith(Suffix2))
90       BaseName = FileName.drop_back(Suffix2.size());
91     else if (FileName.endswith(Suffix))
92       BaseName = FileName.drop_back(Suffix.size());
93 
94     if (BaseName == "ocml") {
95       OCML = FilePath;
96     } else if (BaseName == "ockl") {
97       OCKL = FilePath;
98     } else if (BaseName == "opencl") {
99       OpenCL = FilePath;
100     } else if (BaseName == "hip") {
101       HIP = FilePath;
102     } else if (BaseName == "asanrtl") {
103       AsanRTL = FilePath;
104     } else if (BaseName == "oclc_finite_only_off") {
105       FiniteOnly.Off = FilePath;
106     } else if (BaseName == "oclc_finite_only_on") {
107       FiniteOnly.On = FilePath;
108     } else if (BaseName == "oclc_daz_opt_on") {
109       DenormalsAreZero.On = FilePath;
110     } else if (BaseName == "oclc_daz_opt_off") {
111       DenormalsAreZero.Off = FilePath;
112     } else if (BaseName == "oclc_correctly_rounded_sqrt_on") {
113       CorrectlyRoundedSqrt.On = FilePath;
114     } else if (BaseName == "oclc_correctly_rounded_sqrt_off") {
115       CorrectlyRoundedSqrt.Off = FilePath;
116     } else if (BaseName == "oclc_unsafe_math_on") {
117       UnsafeMath.On = FilePath;
118     } else if (BaseName == "oclc_unsafe_math_off") {
119       UnsafeMath.Off = FilePath;
120     } else if (BaseName == "oclc_wavefrontsize64_on") {
121       WavefrontSize64.On = FilePath;
122     } else if (BaseName == "oclc_wavefrontsize64_off") {
123       WavefrontSize64.Off = FilePath;
124     } else {
125       // Process all bitcode filenames that look like
126       // ocl_isa_version_XXX.amdgcn.bc
127       const StringRef DeviceLibPrefix = "oclc_isa_version_";
128       if (!BaseName.startswith(DeviceLibPrefix))
129         continue;
130 
131       StringRef IsaVersionNumber =
132         BaseName.drop_front(DeviceLibPrefix.size());
133 
134       llvm::Twine GfxName = Twine("gfx") + IsaVersionNumber;
135       SmallString<8> Tmp;
136       LibDeviceMap.insert(
137         std::make_pair(GfxName.toStringRef(Tmp), FilePath.str()));
138     }
139   }
140 }
141 
142 // Parse and extract version numbers from `.hipVersion`. Return `true` if
143 // the parsing fails.
144 bool RocmInstallationDetector::parseHIPVersionFile(llvm::StringRef V) {
145   SmallVector<StringRef, 4> VersionParts;
146   V.split(VersionParts, '\n');
147   unsigned Major = ~0U;
148   unsigned Minor = ~0U;
149   for (auto Part : VersionParts) {
150     auto Splits = Part.rtrim().split('=');
151     if (Splits.first == "HIP_VERSION_MAJOR") {
152       if (Splits.second.getAsInteger(0, Major))
153         return true;
154     } else if (Splits.first == "HIP_VERSION_MINOR") {
155       if (Splits.second.getAsInteger(0, Minor))
156         return true;
157     } else if (Splits.first == "HIP_VERSION_PATCH")
158       VersionPatch = Splits.second.str();
159   }
160   if (Major == ~0U || Minor == ~0U)
161     return true;
162   VersionMajorMinor = llvm::VersionTuple(Major, Minor);
163   DetectedVersion =
164       (Twine(Major) + "." + Twine(Minor) + "." + VersionPatch).str();
165   return false;
166 }
167 
168 /// \returns a list of candidate directories for ROCm installation, which is
169 /// cached and populated only once.
170 const SmallVectorImpl<RocmInstallationDetector::Candidate> &
171 RocmInstallationDetector::getInstallationPathCandidates() {
172 
173   // Return the cached candidate list if it has already been populated.
174   if (!ROCmSearchDirs.empty())
175     return ROCmSearchDirs;
176 
177   auto DoPrintROCmSearchDirs = [&]() {
178     if (PrintROCmSearchDirs)
179       for (auto Cand : ROCmSearchDirs) {
180         llvm::errs() << "ROCm installation search path";
181         if (Cand.isSPACK())
182           llvm::errs() << " (Spack " << Cand.SPACKReleaseStr << ")";
183         llvm::errs() << ": " << Cand.Path << '\n';
184       }
185   };
186 
187   // For candidate specified by --rocm-path we do not do strict check, i.e.,
188   // checking existence of HIP version file and device library files.
189   if (!RocmPathArg.empty()) {
190     ROCmSearchDirs.emplace_back(RocmPathArg.str());
191     DoPrintROCmSearchDirs();
192     return ROCmSearchDirs;
193   } else if (const char *RocmPathEnv = ::getenv("ROCM_PATH")) {
194     if (!StringRef(RocmPathEnv).empty()) {
195       ROCmSearchDirs.emplace_back(RocmPathEnv);
196       DoPrintROCmSearchDirs();
197       return ROCmSearchDirs;
198     }
199   }
200 
201   // Try to find relative to the compiler binary.
202   const char *InstallDir = D.getInstalledDir();
203 
204   // Check both a normal Unix prefix position of the clang binary, as well as
205   // the Windows-esque layout the ROCm packages use with the host architecture
206   // subdirectory of bin.
207   auto DeduceROCmPath = [](StringRef ClangPath) {
208     // Strip off directory (usually bin)
209     StringRef ParentDir = llvm::sys::path::parent_path(ClangPath);
210     StringRef ParentName = llvm::sys::path::filename(ParentDir);
211 
212     // Some builds use bin/{host arch}, so go up again.
213     if (ParentName == "bin") {
214       ParentDir = llvm::sys::path::parent_path(ParentDir);
215       ParentName = llvm::sys::path::filename(ParentDir);
216     }
217 
218     // Detect ROCm packages built with SPACK.
219     // clang is installed at
220     // <rocm_root>/llvm-amdgpu-<rocm_release_string>-<hash>/bin directory.
221     // We only consider the parent directory of llvm-amdgpu package as ROCm
222     // installation candidate for SPACK.
223     if (ParentName.startswith("llvm-amdgpu-")) {
224       auto SPACKPostfix =
225           ParentName.drop_front(strlen("llvm-amdgpu-")).split('-');
226       auto SPACKReleaseStr = SPACKPostfix.first;
227       if (!SPACKReleaseStr.empty()) {
228         ParentDir = llvm::sys::path::parent_path(ParentDir);
229         return Candidate(ParentDir.str(), /*StrictChecking=*/true,
230                          SPACKReleaseStr);
231       }
232     }
233 
234     // Some versions of the rocm llvm package install to /opt/rocm/llvm/bin
235     // Some versions of the aomp package install to /opt/rocm/aomp/bin
236     if (ParentName == "llvm" || ParentName.startswith("aomp"))
237       ParentDir = llvm::sys::path::parent_path(ParentDir);
238 
239     return Candidate(ParentDir.str(), /*StrictChecking=*/true);
240   };
241 
242   // Deduce ROCm path by the path used to invoke clang. Do not resolve symbolic
243   // link of clang itself.
244   ROCmSearchDirs.emplace_back(DeduceROCmPath(InstallDir));
245 
246   // Deduce ROCm path by the real path of the invoked clang, resolving symbolic
247   // link of clang itself.
248   llvm::SmallString<256> RealClangPath;
249   llvm::sys::fs::real_path(D.getClangProgramPath(), RealClangPath);
250   auto ParentPath = llvm::sys::path::parent_path(RealClangPath);
251   if (ParentPath != InstallDir)
252     ROCmSearchDirs.emplace_back(DeduceROCmPath(ParentPath));
253 
254   // Device library may be installed in clang or resource directory.
255   auto ClangRoot = llvm::sys::path::parent_path(InstallDir);
256   auto RealClangRoot = llvm::sys::path::parent_path(ParentPath);
257   ROCmSearchDirs.emplace_back(ClangRoot.str(), /*StrictChecking=*/true);
258   if (RealClangRoot != ClangRoot)
259     ROCmSearchDirs.emplace_back(RealClangRoot.str(), /*StrictChecking=*/true);
260   ROCmSearchDirs.emplace_back(D.ResourceDir,
261                               /*StrictChecking=*/true);
262 
263   ROCmSearchDirs.emplace_back(D.SysRoot + "/opt/rocm",
264                               /*StrictChecking=*/true);
265 
266   // Find the latest /opt/rocm-{release} directory.
267   std::error_code EC;
268   std::string LatestROCm;
269   llvm::VersionTuple LatestVer;
270   // Get ROCm version from ROCm directory name.
271   auto GetROCmVersion = [](StringRef DirName) {
272     llvm::VersionTuple V;
273     std::string VerStr = DirName.drop_front(strlen("rocm-")).str();
274     // The ROCm directory name follows the format of
275     // rocm-{major}.{minor}.{subMinor}[-{build}]
276     std::replace(VerStr.begin(), VerStr.end(), '-', '.');
277     V.tryParse(VerStr);
278     return V;
279   };
280   for (llvm::vfs::directory_iterator
281            File = D.getVFS().dir_begin(D.SysRoot + "/opt", EC),
282            FileEnd;
283        File != FileEnd && !EC; File.increment(EC)) {
284     llvm::StringRef FileName = llvm::sys::path::filename(File->path());
285     if (!FileName.startswith("rocm-"))
286       continue;
287     if (LatestROCm.empty()) {
288       LatestROCm = FileName.str();
289       LatestVer = GetROCmVersion(LatestROCm);
290       continue;
291     }
292     auto Ver = GetROCmVersion(FileName);
293     if (LatestVer < Ver) {
294       LatestROCm = FileName.str();
295       LatestVer = Ver;
296     }
297   }
298   if (!LatestROCm.empty())
299     ROCmSearchDirs.emplace_back(D.SysRoot + "/opt/" + LatestROCm,
300                                 /*StrictChecking=*/true);
301 
302   DoPrintROCmSearchDirs();
303   return ROCmSearchDirs;
304 }
305 
306 RocmInstallationDetector::RocmInstallationDetector(
307     const Driver &D, const llvm::Triple &HostTriple,
308     const llvm::opt::ArgList &Args, bool DetectHIPRuntime, bool DetectDeviceLib)
309     : D(D) {
310   Verbose = Args.hasArg(options::OPT_v);
311   RocmPathArg = Args.getLastArgValue(clang::driver::options::OPT_rocm_path_EQ);
312   PrintROCmSearchDirs =
313       Args.hasArg(clang::driver::options::OPT_print_rocm_search_dirs);
314   RocmDeviceLibPathArg =
315       Args.getAllArgValues(clang::driver::options::OPT_rocm_device_lib_path_EQ);
316   HIPPathArg = Args.getLastArgValue(clang::driver::options::OPT_hip_path_EQ);
317   if (auto *A = Args.getLastArg(clang::driver::options::OPT_hip_version_EQ)) {
318     HIPVersionArg = A->getValue();
319     unsigned Major = ~0U;
320     unsigned Minor = ~0U;
321     SmallVector<StringRef, 3> Parts;
322     HIPVersionArg.split(Parts, '.');
323     if (Parts.size())
324       Parts[0].getAsInteger(0, Major);
325     if (Parts.size() > 1)
326       Parts[1].getAsInteger(0, Minor);
327     if (Parts.size() > 2)
328       VersionPatch = Parts[2].str();
329     if (VersionPatch.empty())
330       VersionPatch = "0";
331     if (Major != ~0U && Minor == ~0U)
332       Minor = 0;
333     if (Major == ~0U || Minor == ~0U)
334       D.Diag(diag::err_drv_invalid_value)
335           << A->getAsString(Args) << HIPVersionArg;
336 
337     VersionMajorMinor = llvm::VersionTuple(Major, Minor);
338     DetectedVersion =
339         (Twine(Major) + "." + Twine(Minor) + "." + VersionPatch).str();
340   } else {
341     VersionPatch = DefaultVersionPatch;
342     VersionMajorMinor =
343         llvm::VersionTuple(DefaultVersionMajor, DefaultVersionMinor);
344     DetectedVersion = (Twine(DefaultVersionMajor) + "." +
345                        Twine(DefaultVersionMinor) + "." + VersionPatch)
346                           .str();
347   }
348 
349   if (DetectHIPRuntime)
350     detectHIPRuntime();
351   if (DetectDeviceLib)
352     detectDeviceLibrary();
353 }
354 
355 void RocmInstallationDetector::detectDeviceLibrary() {
356   assert(LibDevicePath.empty());
357 
358   if (!RocmDeviceLibPathArg.empty())
359     LibDevicePath = RocmDeviceLibPathArg[RocmDeviceLibPathArg.size() - 1];
360   else if (const char *LibPathEnv = ::getenv("HIP_DEVICE_LIB_PATH"))
361     LibDevicePath = LibPathEnv;
362 
363   auto &FS = D.getVFS();
364   if (!LibDevicePath.empty()) {
365     // Maintain compatability with HIP flag/envvar pointing directly at the
366     // bitcode library directory. This points directly at the library path instead
367     // of the rocm root installation.
368     if (!FS.exists(LibDevicePath))
369       return;
370 
371     scanLibDevicePath(LibDevicePath);
372     HasDeviceLibrary = allGenericLibsValid() && !LibDeviceMap.empty();
373     return;
374   }
375 
376   // The install path situation in old versions of ROCm is a real mess, and
377   // use a different install layout. Multiple copies of the device libraries
378   // exist for each frontend project, and differ depending on which build
379   // system produced the packages. Standalone OpenCL builds also have a
380   // different directory structure from the ROCm OpenCL package.
381   auto &ROCmDirs = getInstallationPathCandidates();
382   for (const auto &Candidate : ROCmDirs) {
383     auto CandidatePath = Candidate.Path;
384 
385     // Check device library exists at the given path.
386     auto CheckDeviceLib = [&](StringRef Path) {
387       bool CheckLibDevice = (!NoBuiltinLibs || Candidate.StrictChecking);
388       if (CheckLibDevice && !FS.exists(Path))
389         return false;
390 
391       scanLibDevicePath(Path);
392 
393       if (!NoBuiltinLibs) {
394         // Check that the required non-target libraries are all available.
395         if (!allGenericLibsValid())
396           return false;
397 
398         // Check that we have found at least one libdevice that we can link in
399         // if -nobuiltinlib hasn't been specified.
400         if (LibDeviceMap.empty())
401           return false;
402       }
403       return true;
404     };
405 
406     // The possible structures are:
407     // - ${ROCM_ROOT}/amdgcn/bitcode/*
408     // - ${ROCM_ROOT}/lib/*
409     // - ${ROCM_ROOT}/lib/bitcode/*
410     // so try to detect these layouts.
411     static constexpr std::array<const char *, 2> SubDirsList[] = {
412         {"amdgcn", "bitcode"},
413         {"lib", ""},
414         {"lib", "bitcode"},
415     };
416 
417     // Make a path by appending sub-directories to InstallPath.
418     auto MakePath = [&](const llvm::ArrayRef<const char *> &SubDirs) {
419       auto Path = CandidatePath;
420       for (auto SubDir : SubDirs)
421         llvm::sys::path::append(Path, SubDir);
422       return Path;
423     };
424 
425     for (auto SubDirs : SubDirsList) {
426       LibDevicePath = MakePath(SubDirs);
427       HasDeviceLibrary = CheckDeviceLib(LibDevicePath);
428       if (HasDeviceLibrary)
429         return;
430     }
431   }
432 }
433 
434 void RocmInstallationDetector::detectHIPRuntime() {
435   SmallVector<Candidate, 4> HIPSearchDirs;
436   if (!HIPPathArg.empty())
437     HIPSearchDirs.emplace_back(HIPPathArg.str(), /*StrictChecking=*/true);
438   else
439     HIPSearchDirs.append(getInstallationPathCandidates());
440   auto &FS = D.getVFS();
441 
442   for (const auto &Candidate : HIPSearchDirs) {
443     InstallPath = Candidate.Path;
444     if (InstallPath.empty() || !FS.exists(InstallPath))
445       continue;
446     // HIP runtime built by SPACK is installed to
447     // <rocm_root>/hip-<rocm_release_string>-<hash> directory.
448     auto SPACKPath = findSPACKPackage(Candidate, "hip");
449     InstallPath = SPACKPath.empty() ? InstallPath : SPACKPath;
450 
451     BinPath = InstallPath;
452     llvm::sys::path::append(BinPath, "bin");
453     IncludePath = InstallPath;
454     llvm::sys::path::append(IncludePath, "include");
455     LibPath = InstallPath;
456     llvm::sys::path::append(LibPath, "lib");
457 
458     llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> VersionFile =
459         FS.getBufferForFile(BinPath + "/.hipVersion");
460     if (!VersionFile && Candidate.StrictChecking)
461       continue;
462 
463     if (HIPVersionArg.empty() && VersionFile)
464       if (parseHIPVersionFile((*VersionFile)->getBuffer()))
465         continue;
466 
467     HasHIPRuntime = true;
468     return;
469   }
470   HasHIPRuntime = false;
471 }
472 
473 void RocmInstallationDetector::print(raw_ostream &OS) const {
474   if (hasHIPRuntime())
475     OS << "Found HIP installation: " << InstallPath << ", version "
476        << DetectedVersion << '\n';
477 }
478 
479 void RocmInstallationDetector::AddHIPIncludeArgs(const ArgList &DriverArgs,
480                                                  ArgStringList &CC1Args) const {
481   bool UsesRuntimeWrapper = VersionMajorMinor > llvm::VersionTuple(3, 5) &&
482                             !DriverArgs.hasArg(options::OPT_nohipwrapperinc);
483 
484   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
485     // HIP header includes standard library wrapper headers under clang
486     // cuda_wrappers directory. Since these wrapper headers include_next
487     // standard C++ headers, whereas libc++ headers include_next other clang
488     // headers. The include paths have to follow this order:
489     // - wrapper include path
490     // - standard C++ include path
491     // - other clang include path
492     // Since standard C++ and other clang include paths are added in other
493     // places after this function, here we only need to make sure wrapper
494     // include path is added.
495     //
496     // ROCm 3.5 does not fully support the wrapper headers. Therefore it needs
497     // a workaround.
498     SmallString<128> P(D.ResourceDir);
499     if (UsesRuntimeWrapper)
500       llvm::sys::path::append(P, "include", "cuda_wrappers");
501     CC1Args.push_back("-internal-isystem");
502     CC1Args.push_back(DriverArgs.MakeArgString(P));
503   }
504 
505   if (DriverArgs.hasArg(options::OPT_nogpuinc))
506     return;
507 
508   if (!hasHIPRuntime()) {
509     D.Diag(diag::err_drv_no_hip_runtime);
510     return;
511   }
512 
513   CC1Args.push_back("-idirafter");
514   CC1Args.push_back(DriverArgs.MakeArgString(getIncludePath()));
515   if (UsesRuntimeWrapper)
516     CC1Args.append({"-include", "__clang_hip_runtime_wrapper.h"});
517 }
518 
519 void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
520                                   const InputInfo &Output,
521                                   const InputInfoList &Inputs,
522                                   const ArgList &Args,
523                                   const char *LinkingOutput) const {
524 
525   std::string Linker = getToolChain().GetProgramPath(getShortName());
526   ArgStringList CmdArgs;
527   addLinkerCompressDebugSectionsOption(getToolChain(), Args, CmdArgs);
528   AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
529   CmdArgs.push_back("-shared");
530   CmdArgs.push_back("-o");
531   CmdArgs.push_back(Output.getFilename());
532   C.addCommand(std::make_unique<Command>(
533       JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
534       CmdArgs, Inputs, Output));
535 }
536 
537 void amdgpu::getAMDGPUTargetFeatures(const Driver &D,
538                                      const llvm::Triple &Triple,
539                                      const llvm::opt::ArgList &Args,
540                                      std::vector<StringRef> &Features) {
541   // Add target ID features to -target-feature options. No diagnostics should
542   // be emitted here since invalid target ID is diagnosed at other places.
543   StringRef TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ);
544   if (!TargetID.empty()) {
545     llvm::StringMap<bool> FeatureMap;
546     auto OptionalGpuArch = parseTargetID(Triple, TargetID, &FeatureMap);
547     if (OptionalGpuArch) {
548       StringRef GpuArch = OptionalGpuArch.getValue();
549       // Iterate through all possible target ID features for the given GPU.
550       // If it is mapped to true, add +feature.
551       // If it is mapped to false, add -feature.
552       // If it is not in the map (default), do not add it
553       for (auto &&Feature : getAllPossibleTargetIDFeatures(Triple, GpuArch)) {
554         auto Pos = FeatureMap.find(Feature);
555         if (Pos == FeatureMap.end())
556           continue;
557         Features.push_back(Args.MakeArgStringRef(
558             (Twine(Pos->second ? "+" : "-") + Feature).str()));
559       }
560     }
561   }
562 
563   if (Args.hasFlag(options::OPT_mwavefrontsize64,
564                    options::OPT_mno_wavefrontsize64, false))
565     Features.push_back("+wavefrontsize64");
566 
567   handleTargetFeaturesGroup(
568     Args, Features, options::OPT_m_amdgpu_Features_Group);
569 }
570 
571 /// AMDGPU Toolchain
572 AMDGPUToolChain::AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple,
573                                  const ArgList &Args)
574     : Generic_ELF(D, Triple, Args),
575       OptionsDefault(
576           {{options::OPT_O, "3"}, {options::OPT_cl_std_EQ, "CL1.2"}}) {
577   // Check code object version options. Emit warnings for legacy options
578   // and errors for the last invalid code object version options.
579   // It is done here to avoid repeated warning or error messages for
580   // each tool invocation.
581   checkAMDGPUCodeObjectVersion(D, Args);
582 }
583 
584 Tool *AMDGPUToolChain::buildLinker() const {
585   return new tools::amdgpu::Linker(*this);
586 }
587 
588 DerivedArgList *
589 AMDGPUToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
590                                Action::OffloadKind DeviceOffloadKind) const {
591 
592   DerivedArgList *DAL =
593       Generic_ELF::TranslateArgs(Args, BoundArch, DeviceOffloadKind);
594 
595   const OptTable &Opts = getDriver().getOpts();
596 
597   if (!DAL)
598     DAL = new DerivedArgList(Args.getBaseArgs());
599 
600   for (Arg *A : Args) {
601     if (!shouldSkipArgument(A))
602       DAL->append(A);
603   }
604 
605   checkTargetID(*DAL);
606 
607   if (!Args.getLastArgValue(options::OPT_x).equals("cl"))
608     return DAL;
609 
610   // Phase 1 (.cl -> .bc)
611   if (Args.hasArg(options::OPT_c) && Args.hasArg(options::OPT_emit_llvm)) {
612     DAL->AddFlagArg(nullptr, Opts.getOption(getTriple().isArch64Bit()
613                                                 ? options::OPT_m64
614                                                 : options::OPT_m32));
615 
616     // Have to check OPT_O4, OPT_O0 & OPT_Ofast separately
617     // as they defined that way in Options.td
618     if (!Args.hasArg(options::OPT_O, options::OPT_O0, options::OPT_O4,
619                      options::OPT_Ofast))
620       DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_O),
621                         getOptionDefault(options::OPT_O));
622   }
623 
624   return DAL;
625 }
626 
627 bool AMDGPUToolChain::getDefaultDenormsAreZeroForTarget(
628     llvm::AMDGPU::GPUKind Kind) {
629 
630   // Assume nothing without a specific target.
631   if (Kind == llvm::AMDGPU::GK_NONE)
632     return false;
633 
634   const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(Kind);
635 
636   // Default to enabling f32 denormals by default on subtargets where fma is
637   // fast with denormals
638   const bool BothDenormAndFMAFast =
639       (ArchAttr & llvm::AMDGPU::FEATURE_FAST_FMA_F32) &&
640       (ArchAttr & llvm::AMDGPU::FEATURE_FAST_DENORMAL_F32);
641   return !BothDenormAndFMAFast;
642 }
643 
644 llvm::DenormalMode AMDGPUToolChain::getDefaultDenormalModeForType(
645     const llvm::opt::ArgList &DriverArgs, const JobAction &JA,
646     const llvm::fltSemantics *FPType) const {
647   // Denormals should always be enabled for f16 and f64.
648   if (!FPType || FPType != &llvm::APFloat::IEEEsingle())
649     return llvm::DenormalMode::getIEEE();
650 
651   if (JA.getOffloadingDeviceKind() == Action::OFK_HIP ||
652       JA.getOffloadingDeviceKind() == Action::OFK_Cuda) {
653     auto Arch = getProcessorFromTargetID(getTriple(), JA.getOffloadingArch());
654     auto Kind = llvm::AMDGPU::parseArchAMDGCN(Arch);
655     if (FPType && FPType == &llvm::APFloat::IEEEsingle() &&
656         DriverArgs.hasFlag(options::OPT_fgpu_flush_denormals_to_zero,
657                            options::OPT_fno_gpu_flush_denormals_to_zero,
658                            getDefaultDenormsAreZeroForTarget(Kind)))
659       return llvm::DenormalMode::getPreserveSign();
660 
661     return llvm::DenormalMode::getIEEE();
662   }
663 
664   const StringRef GpuArch = getGPUArch(DriverArgs);
665   auto Kind = llvm::AMDGPU::parseArchAMDGCN(GpuArch);
666 
667   // TODO: There are way too many flags that change this. Do we need to check
668   // them all?
669   bool DAZ = DriverArgs.hasArg(options::OPT_cl_denorms_are_zero) ||
670              getDefaultDenormsAreZeroForTarget(Kind);
671 
672   // Outputs are flushed to zero (FTZ), preserving sign. Denormal inputs are
673   // also implicit treated as zero (DAZ).
674   return DAZ ? llvm::DenormalMode::getPreserveSign() :
675                llvm::DenormalMode::getIEEE();
676 }
677 
678 bool AMDGPUToolChain::isWave64(const llvm::opt::ArgList &DriverArgs,
679                                llvm::AMDGPU::GPUKind Kind) {
680   const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(Kind);
681   bool HasWave32 = (ArchAttr & llvm::AMDGPU::FEATURE_WAVE32);
682 
683   return !HasWave32 || DriverArgs.hasFlag(
684     options::OPT_mwavefrontsize64, options::OPT_mno_wavefrontsize64, false);
685 }
686 
687 
688 /// ROCM Toolchain
689 ROCMToolChain::ROCMToolChain(const Driver &D, const llvm::Triple &Triple,
690                              const ArgList &Args)
691     : AMDGPUToolChain(D, Triple, Args) {
692   RocmInstallation.detectDeviceLibrary();
693 }
694 
695 void AMDGPUToolChain::addClangTargetOptions(
696     const llvm::opt::ArgList &DriverArgs,
697     llvm::opt::ArgStringList &CC1Args,
698     Action::OffloadKind DeviceOffloadingKind) const {
699   // Default to "hidden" visibility, as object level linking will not be
700   // supported for the foreseeable future.
701   if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
702                          options::OPT_fvisibility_ms_compat)) {
703     CC1Args.push_back("-fvisibility");
704     CC1Args.push_back("hidden");
705     CC1Args.push_back("-fapply-global-visibility-to-externs");
706   }
707 }
708 
709 StringRef
710 AMDGPUToolChain::getGPUArch(const llvm::opt::ArgList &DriverArgs) const {
711   return getProcessorFromTargetID(
712       getTriple(), DriverArgs.getLastArgValue(options::OPT_mcpu_EQ));
713 }
714 
715 AMDGPUToolChain::ParsedTargetIDType
716 AMDGPUToolChain::getParsedTargetID(const llvm::opt::ArgList &DriverArgs) const {
717   StringRef TargetID = DriverArgs.getLastArgValue(options::OPT_mcpu_EQ);
718   if (TargetID.empty())
719     return {None, None, None};
720 
721   llvm::StringMap<bool> FeatureMap;
722   auto OptionalGpuArch = parseTargetID(getTriple(), TargetID, &FeatureMap);
723   if (!OptionalGpuArch)
724     return {TargetID.str(), None, None};
725 
726   return {TargetID.str(), OptionalGpuArch.getValue().str(), FeatureMap};
727 }
728 
729 void AMDGPUToolChain::checkTargetID(
730     const llvm::opt::ArgList &DriverArgs) const {
731   auto PTID = getParsedTargetID(DriverArgs);
732   if (PTID.OptionalTargetID && !PTID.OptionalGPUArch) {
733     getDriver().Diag(clang::diag::err_drv_bad_target_id)
734         << PTID.OptionalTargetID.getValue();
735   }
736 }
737 
738 llvm::Error
739 AMDGPUToolChain::detectSystemGPUs(const ArgList &Args,
740                                   SmallVector<std::string, 1> &GPUArchs) const {
741   std::string Program;
742   if (Arg *A = Args.getLastArg(options::OPT_amdgpu_arch_tool_EQ))
743     Program = A->getValue();
744   else
745     Program = GetProgramPath(AMDGPU_ARCH_PROGRAM_NAME);
746   llvm::SmallString<64> OutputFile;
747   llvm::sys::fs::createTemporaryFile("print-system-gpus", "" /* No Suffix */,
748                                      OutputFile);
749   llvm::FileRemover OutputRemover(OutputFile.c_str());
750   llvm::Optional<llvm::StringRef> Redirects[] = {
751       {""},
752       OutputFile.str(),
753       {""},
754   };
755 
756   std::string ErrorMessage;
757   if (int Result = llvm::sys::ExecuteAndWait(
758           Program, {}, {}, Redirects, /* SecondsToWait */ 0,
759           /*MemoryLimit*/ 0, &ErrorMessage)) {
760     if (Result > 0) {
761       ErrorMessage = "Exited with error code " + std::to_string(Result);
762     } else if (Result == -1) {
763       ErrorMessage = "Execute failed: " + ErrorMessage;
764     } else {
765       ErrorMessage = "Crashed: " + ErrorMessage;
766     }
767 
768     return llvm::createStringError(std::error_code(),
769                                    Program + ": " + ErrorMessage);
770   }
771 
772   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> OutputBuf =
773       llvm::MemoryBuffer::getFile(OutputFile.c_str());
774   if (!OutputBuf) {
775     return llvm::createStringError(OutputBuf.getError(),
776                                    "Failed to read stdout of " + Program +
777                                        ": " + OutputBuf.getError().message());
778   }
779 
780   for (llvm::line_iterator LineIt(**OutputBuf); !LineIt.is_at_end(); ++LineIt) {
781     GPUArchs.push_back(LineIt->str());
782   }
783   return llvm::Error::success();
784 }
785 
786 llvm::Error AMDGPUToolChain::getSystemGPUArch(const ArgList &Args,
787                                               std::string &GPUArch) const {
788   // detect the AMDGPU installed in system
789   SmallVector<std::string, 1> GPUArchs;
790   auto Err = detectSystemGPUs(Args, GPUArchs);
791   if (Err) {
792     return Err;
793   }
794   if (GPUArchs.empty()) {
795     return llvm::createStringError(std::error_code(),
796                                    "No AMD GPU detected in the system");
797   }
798   GPUArch = GPUArchs[0];
799   if (GPUArchs.size() > 1) {
800     bool AllSame = llvm::all_of(GPUArchs, [&](const StringRef &GPUArch) {
801       return GPUArch == GPUArchs.front();
802     });
803     if (!AllSame)
804       return llvm::createStringError(
805           std::error_code(), "Multiple AMD GPUs found with different archs");
806   }
807   return llvm::Error::success();
808 }
809 
810 void ROCMToolChain::addClangTargetOptions(
811     const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
812     Action::OffloadKind DeviceOffloadingKind) const {
813   AMDGPUToolChain::addClangTargetOptions(DriverArgs, CC1Args,
814                                          DeviceOffloadingKind);
815 
816   // For the OpenCL case where there is no offload target, accept -nostdlib to
817   // disable bitcode linking.
818   if (DeviceOffloadingKind == Action::OFK_None &&
819       DriverArgs.hasArg(options::OPT_nostdlib))
820     return;
821 
822   if (DriverArgs.hasArg(options::OPT_nogpulib))
823     return;
824 
825   if (!RocmInstallation.hasDeviceLibrary()) {
826     getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 0;
827     return;
828   }
829 
830   // Get the device name and canonicalize it
831   const StringRef GpuArch = getGPUArch(DriverArgs);
832   auto Kind = llvm::AMDGPU::parseArchAMDGCN(GpuArch);
833   const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(Kind);
834   std::string LibDeviceFile = RocmInstallation.getLibDeviceFile(CanonArch);
835   if (LibDeviceFile.empty()) {
836     getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 1 << GpuArch;
837     return;
838   }
839 
840   bool Wave64 = isWave64(DriverArgs, Kind);
841 
842   // TODO: There are way too many flags that change this. Do we need to check
843   // them all?
844   bool DAZ = DriverArgs.hasArg(options::OPT_cl_denorms_are_zero) ||
845              getDefaultDenormsAreZeroForTarget(Kind);
846   bool FiniteOnly = DriverArgs.hasArg(options::OPT_cl_finite_math_only);
847 
848   bool UnsafeMathOpt =
849       DriverArgs.hasArg(options::OPT_cl_unsafe_math_optimizations);
850   bool FastRelaxedMath = DriverArgs.hasArg(options::OPT_cl_fast_relaxed_math);
851   bool CorrectSqrt =
852       DriverArgs.hasArg(options::OPT_cl_fp32_correctly_rounded_divide_sqrt);
853 
854   // Add the OpenCL specific bitcode library.
855   llvm::SmallVector<std::string, 12> BCLibs;
856   BCLibs.push_back(RocmInstallation.getOpenCLPath().str());
857 
858   // Add the generic set of libraries.
859   BCLibs.append(RocmInstallation.getCommonBitcodeLibs(
860       DriverArgs, LibDeviceFile, Wave64, DAZ, FiniteOnly, UnsafeMathOpt,
861       FastRelaxedMath, CorrectSqrt));
862 
863   llvm::for_each(BCLibs, [&](StringRef BCFile) {
864     CC1Args.push_back("-mlink-builtin-bitcode");
865     CC1Args.push_back(DriverArgs.MakeArgString(BCFile));
866   });
867 }
868 
869 llvm::SmallVector<std::string, 12>
870 RocmInstallationDetector::getCommonBitcodeLibs(
871     const llvm::opt::ArgList &DriverArgs, StringRef LibDeviceFile, bool Wave64,
872     bool DAZ, bool FiniteOnly, bool UnsafeMathOpt, bool FastRelaxedMath,
873     bool CorrectSqrt) const {
874 
875   llvm::SmallVector<std::string, 12> BCLibs;
876 
877   auto AddBCLib = [&](StringRef BCFile) { BCLibs.push_back(BCFile.str()); };
878 
879   AddBCLib(getOCMLPath());
880   AddBCLib(getOCKLPath());
881   AddBCLib(getDenormalsAreZeroPath(DAZ));
882   AddBCLib(getUnsafeMathPath(UnsafeMathOpt || FastRelaxedMath));
883   AddBCLib(getFiniteOnlyPath(FiniteOnly || FastRelaxedMath));
884   AddBCLib(getCorrectlyRoundedSqrtPath(CorrectSqrt));
885   AddBCLib(getWavefrontSize64Path(Wave64));
886   AddBCLib(LibDeviceFile);
887 
888   return BCLibs;
889 }
890 
891 bool AMDGPUToolChain::shouldSkipArgument(const llvm::opt::Arg *A) const {
892   Option O = A->getOption();
893   if (O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie))
894     return true;
895   return false;
896 }
897 
898 llvm::SmallVector<std::string, 12>
899 ROCMToolChain::getCommonDeviceLibNames(const llvm::opt::ArgList &DriverArgs,
900                                        const std::string &GPUArch) const {
901   auto Kind = llvm::AMDGPU::parseArchAMDGCN(GPUArch);
902   const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(Kind);
903 
904   std::string LibDeviceFile = RocmInstallation.getLibDeviceFile(CanonArch);
905   if (LibDeviceFile.empty()) {
906     getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 1 << GPUArch;
907     return {};
908   }
909 
910   // If --hip-device-lib is not set, add the default bitcode libraries.
911   // TODO: There are way too many flags that change this. Do we need to check
912   // them all?
913   bool DAZ = DriverArgs.hasFlag(options::OPT_fgpu_flush_denormals_to_zero,
914                                 options::OPT_fno_gpu_flush_denormals_to_zero,
915                                 getDefaultDenormsAreZeroForTarget(Kind));
916   bool FiniteOnly = DriverArgs.hasFlag(
917       options::OPT_ffinite_math_only, options::OPT_fno_finite_math_only, false);
918   bool UnsafeMathOpt =
919       DriverArgs.hasFlag(options::OPT_funsafe_math_optimizations,
920                          options::OPT_fno_unsafe_math_optimizations, false);
921   bool FastRelaxedMath = DriverArgs.hasFlag(options::OPT_ffast_math,
922                                             options::OPT_fno_fast_math, false);
923   bool CorrectSqrt = DriverArgs.hasFlag(
924       options::OPT_fhip_fp32_correctly_rounded_divide_sqrt,
925       options::OPT_fno_hip_fp32_correctly_rounded_divide_sqrt);
926   bool Wave64 = isWave64(DriverArgs, Kind);
927 
928   return RocmInstallation.getCommonBitcodeLibs(
929       DriverArgs, LibDeviceFile, Wave64, DAZ, FiniteOnly, UnsafeMathOpt,
930       FastRelaxedMath, CorrectSqrt);
931 }
932