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>
findSPACKPackage(const Candidate & Cand,StringRef PackageName)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 
scanLibDevicePath(llvm::StringRef Path)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.
parseHIPVersionFile(llvm::StringRef V)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> &
getInstallationPathCandidates()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 
RocmInstallationDetector(const Driver & D,const llvm::Triple & HostTriple,const llvm::opt::ArgList & Args,bool DetectHIPRuntime,bool DetectDeviceLib)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 
detectDeviceLibrary()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 
detectHIPRuntime()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 
print(raw_ostream & OS) const473 void RocmInstallationDetector::print(raw_ostream &OS) const {
474   if (hasHIPRuntime())
475     OS << "Found HIP installation: " << InstallPath << ", version "
476        << DetectedVersion << '\n';
477 }
478 
AddHIPIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const479 void RocmInstallationDetector::AddHIPIncludeArgs(const ArgList &DriverArgs,
480                                                  ArgStringList &CC1Args) const {
481   bool UsesRuntimeWrapper = VersionMajorMinor > llvm::VersionTuple(3, 5);
482 
483   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
484     // HIP header includes standard library wrapper headers under clang
485     // cuda_wrappers directory. Since these wrapper headers include_next
486     // standard C++ headers, whereas libc++ headers include_next other clang
487     // headers. The include paths have to follow this order:
488     // - wrapper include path
489     // - standard C++ include path
490     // - other clang include path
491     // Since standard C++ and other clang include paths are added in other
492     // places after this function, here we only need to make sure wrapper
493     // include path is added.
494     //
495     // ROCm 3.5 does not fully support the wrapper headers. Therefore it needs
496     // a workaround.
497     SmallString<128> P(D.ResourceDir);
498     if (UsesRuntimeWrapper)
499       llvm::sys::path::append(P, "include", "cuda_wrappers");
500     CC1Args.push_back("-internal-isystem");
501     CC1Args.push_back(DriverArgs.MakeArgString(P));
502   }
503 
504   if (DriverArgs.hasArg(options::OPT_nogpuinc))
505     return;
506 
507   if (!hasHIPRuntime()) {
508     D.Diag(diag::err_drv_no_hip_runtime);
509     return;
510   }
511 
512   CC1Args.push_back("-internal-isystem");
513   CC1Args.push_back(DriverArgs.MakeArgString(getIncludePath()));
514   if (UsesRuntimeWrapper)
515     CC1Args.append({"-include", "__clang_hip_runtime_wrapper.h"});
516 }
517 
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const ArgList & Args,const char * LinkingOutput) const518 void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
519                                   const InputInfo &Output,
520                                   const InputInfoList &Inputs,
521                                   const ArgList &Args,
522                                   const char *LinkingOutput) const {
523 
524   std::string Linker = getToolChain().GetProgramPath(getShortName());
525   ArgStringList CmdArgs;
526   addLinkerCompressDebugSectionsOption(getToolChain(), Args, CmdArgs);
527   AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
528   CmdArgs.push_back("-shared");
529   CmdArgs.push_back("-o");
530   CmdArgs.push_back(Output.getFilename());
531   C.addCommand(std::make_unique<Command>(
532       JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
533       CmdArgs, Inputs, Output));
534 }
535 
getAMDGPUTargetFeatures(const Driver & D,const llvm::Triple & Triple,const llvm::opt::ArgList & Args,std::vector<StringRef> & Features)536 void amdgpu::getAMDGPUTargetFeatures(const Driver &D,
537                                      const llvm::Triple &Triple,
538                                      const llvm::opt::ArgList &Args,
539                                      std::vector<StringRef> &Features) {
540   // Add target ID features to -target-feature options. No diagnostics should
541   // be emitted here since invalid target ID is diagnosed at other places.
542   StringRef TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ);
543   if (!TargetID.empty()) {
544     llvm::StringMap<bool> FeatureMap;
545     auto OptionalGpuArch = parseTargetID(Triple, TargetID, &FeatureMap);
546     if (OptionalGpuArch) {
547       StringRef GpuArch = OptionalGpuArch.getValue();
548       // Iterate through all possible target ID features for the given GPU.
549       // If it is mapped to true, add +feature.
550       // If it is mapped to false, add -feature.
551       // If it is not in the map (default), do not add it
552       for (auto &&Feature : getAllPossibleTargetIDFeatures(Triple, GpuArch)) {
553         auto Pos = FeatureMap.find(Feature);
554         if (Pos == FeatureMap.end())
555           continue;
556         Features.push_back(Args.MakeArgStringRef(
557             (Twine(Pos->second ? "+" : "-") + Feature).str()));
558       }
559     }
560   }
561 
562   if (Args.hasFlag(options::OPT_mwavefrontsize64,
563                    options::OPT_mno_wavefrontsize64, false))
564     Features.push_back("+wavefrontsize64");
565 
566   handleTargetFeaturesGroup(
567     Args, Features, options::OPT_m_amdgpu_Features_Group);
568 }
569 
570 /// AMDGPU Toolchain
AMDGPUToolChain(const Driver & D,const llvm::Triple & Triple,const ArgList & Args)571 AMDGPUToolChain::AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple,
572                                  const ArgList &Args)
573     : Generic_ELF(D, Triple, Args),
574       OptionsDefault(
575           {{options::OPT_O, "3"}, {options::OPT_cl_std_EQ, "CL1.2"}}) {
576   // Check code object version options. Emit warnings for legacy options
577   // and errors for the last invalid code object version options.
578   // It is done here to avoid repeated warning or error messages for
579   // each tool invocation.
580   checkAMDGPUCodeObjectVersion(D, Args);
581 }
582 
buildLinker() const583 Tool *AMDGPUToolChain::buildLinker() const {
584   return new tools::amdgpu::Linker(*this);
585 }
586 
587 DerivedArgList *
TranslateArgs(const DerivedArgList & Args,StringRef BoundArch,Action::OffloadKind DeviceOffloadKind) const588 AMDGPUToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
589                                Action::OffloadKind DeviceOffloadKind) const {
590 
591   DerivedArgList *DAL =
592       Generic_ELF::TranslateArgs(Args, BoundArch, DeviceOffloadKind);
593 
594   const OptTable &Opts = getDriver().getOpts();
595 
596   if (!DAL)
597     DAL = new DerivedArgList(Args.getBaseArgs());
598 
599   for (Arg *A : Args) {
600     if (!shouldSkipArgument(A))
601       DAL->append(A);
602   }
603 
604   checkTargetID(*DAL);
605 
606   if (!Args.getLastArgValue(options::OPT_x).equals("cl"))
607     return DAL;
608 
609   // Phase 1 (.cl -> .bc)
610   if (Args.hasArg(options::OPT_c) && Args.hasArg(options::OPT_emit_llvm)) {
611     DAL->AddFlagArg(nullptr, Opts.getOption(getTriple().isArch64Bit()
612                                                 ? options::OPT_m64
613                                                 : options::OPT_m32));
614 
615     // Have to check OPT_O4, OPT_O0 & OPT_Ofast separately
616     // as they defined that way in Options.td
617     if (!Args.hasArg(options::OPT_O, options::OPT_O0, options::OPT_O4,
618                      options::OPT_Ofast))
619       DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_O),
620                         getOptionDefault(options::OPT_O));
621   }
622 
623   return DAL;
624 }
625 
getDefaultDenormsAreZeroForTarget(llvm::AMDGPU::GPUKind Kind)626 bool AMDGPUToolChain::getDefaultDenormsAreZeroForTarget(
627     llvm::AMDGPU::GPUKind Kind) {
628 
629   // Assume nothing without a specific target.
630   if (Kind == llvm::AMDGPU::GK_NONE)
631     return false;
632 
633   const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(Kind);
634 
635   // Default to enabling f32 denormals by default on subtargets where fma is
636   // fast with denormals
637   const bool BothDenormAndFMAFast =
638       (ArchAttr & llvm::AMDGPU::FEATURE_FAST_FMA_F32) &&
639       (ArchAttr & llvm::AMDGPU::FEATURE_FAST_DENORMAL_F32);
640   return !BothDenormAndFMAFast;
641 }
642 
getDefaultDenormalModeForType(const llvm::opt::ArgList & DriverArgs,const JobAction & JA,const llvm::fltSemantics * FPType) const643 llvm::DenormalMode AMDGPUToolChain::getDefaultDenormalModeForType(
644     const llvm::opt::ArgList &DriverArgs, const JobAction &JA,
645     const llvm::fltSemantics *FPType) const {
646   // Denormals should always be enabled for f16 and f64.
647   if (!FPType || FPType != &llvm::APFloat::IEEEsingle())
648     return llvm::DenormalMode::getIEEE();
649 
650   if (JA.getOffloadingDeviceKind() == Action::OFK_HIP ||
651       JA.getOffloadingDeviceKind() == Action::OFK_Cuda) {
652     auto Arch = getProcessorFromTargetID(getTriple(), JA.getOffloadingArch());
653     auto Kind = llvm::AMDGPU::parseArchAMDGCN(Arch);
654     if (FPType && FPType == &llvm::APFloat::IEEEsingle() &&
655         DriverArgs.hasFlag(options::OPT_fgpu_flush_denormals_to_zero,
656                            options::OPT_fno_gpu_flush_denormals_to_zero,
657                            getDefaultDenormsAreZeroForTarget(Kind)))
658       return llvm::DenormalMode::getPreserveSign();
659 
660     return llvm::DenormalMode::getIEEE();
661   }
662 
663   const StringRef GpuArch = getGPUArch(DriverArgs);
664   auto Kind = llvm::AMDGPU::parseArchAMDGCN(GpuArch);
665 
666   // TODO: There are way too many flags that change this. Do we need to check
667   // them all?
668   bool DAZ = DriverArgs.hasArg(options::OPT_cl_denorms_are_zero) ||
669              getDefaultDenormsAreZeroForTarget(Kind);
670 
671   // Outputs are flushed to zero (FTZ), preserving sign. Denormal inputs are
672   // also implicit treated as zero (DAZ).
673   return DAZ ? llvm::DenormalMode::getPreserveSign() :
674                llvm::DenormalMode::getIEEE();
675 }
676 
isWave64(const llvm::opt::ArgList & DriverArgs,llvm::AMDGPU::GPUKind Kind)677 bool AMDGPUToolChain::isWave64(const llvm::opt::ArgList &DriverArgs,
678                                llvm::AMDGPU::GPUKind Kind) {
679   const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(Kind);
680   bool HasWave32 = (ArchAttr & llvm::AMDGPU::FEATURE_WAVE32);
681 
682   return !HasWave32 || DriverArgs.hasFlag(
683     options::OPT_mwavefrontsize64, options::OPT_mno_wavefrontsize64, false);
684 }
685 
686 
687 /// ROCM Toolchain
ROCMToolChain(const Driver & D,const llvm::Triple & Triple,const ArgList & Args)688 ROCMToolChain::ROCMToolChain(const Driver &D, const llvm::Triple &Triple,
689                              const ArgList &Args)
690     : AMDGPUToolChain(D, Triple, Args) {
691   RocmInstallation.detectDeviceLibrary();
692 }
693 
addClangTargetOptions(const llvm::opt::ArgList & DriverArgs,llvm::opt::ArgStringList & CC1Args,Action::OffloadKind DeviceOffloadingKind) const694 void AMDGPUToolChain::addClangTargetOptions(
695     const llvm::opt::ArgList &DriverArgs,
696     llvm::opt::ArgStringList &CC1Args,
697     Action::OffloadKind DeviceOffloadingKind) const {
698   // Default to "hidden" visibility, as object level linking will not be
699   // supported for the foreseeable future.
700   if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
701                          options::OPT_fvisibility_ms_compat)) {
702     CC1Args.push_back("-fvisibility");
703     CC1Args.push_back("hidden");
704     CC1Args.push_back("-fapply-global-visibility-to-externs");
705   }
706 }
707 
708 StringRef
getGPUArch(const llvm::opt::ArgList & DriverArgs) const709 AMDGPUToolChain::getGPUArch(const llvm::opt::ArgList &DriverArgs) const {
710   return getProcessorFromTargetID(
711       getTriple(), DriverArgs.getLastArgValue(options::OPT_mcpu_EQ));
712 }
713 
714 AMDGPUToolChain::ParsedTargetIDType
getParsedTargetID(const llvm::opt::ArgList & DriverArgs) const715 AMDGPUToolChain::getParsedTargetID(const llvm::opt::ArgList &DriverArgs) const {
716   StringRef TargetID = DriverArgs.getLastArgValue(options::OPT_mcpu_EQ);
717   if (TargetID.empty())
718     return {None, None, None};
719 
720   llvm::StringMap<bool> FeatureMap;
721   auto OptionalGpuArch = parseTargetID(getTriple(), TargetID, &FeatureMap);
722   if (!OptionalGpuArch)
723     return {TargetID.str(), None, None};
724 
725   return {TargetID.str(), OptionalGpuArch.getValue().str(), FeatureMap};
726 }
727 
checkTargetID(const llvm::opt::ArgList & DriverArgs) const728 void AMDGPUToolChain::checkTargetID(
729     const llvm::opt::ArgList &DriverArgs) const {
730   auto PTID = getParsedTargetID(DriverArgs);
731   if (PTID.OptionalTargetID && !PTID.OptionalGPUArch) {
732     getDriver().Diag(clang::diag::err_drv_bad_target_id)
733         << PTID.OptionalTargetID.getValue();
734   }
735 }
736 
737 llvm::Error
detectSystemGPUs(const ArgList & Args,SmallVector<std::string,1> & GPUArchs) const738 AMDGPUToolChain::detectSystemGPUs(const ArgList &Args,
739                                   SmallVector<std::string, 1> &GPUArchs) const {
740   std::string Program;
741   if (Arg *A = Args.getLastArg(options::OPT_amdgpu_arch_tool_EQ))
742     Program = A->getValue();
743   else
744     Program = GetProgramPath(AMDGPU_ARCH_PROGRAM_NAME);
745   llvm::SmallString<64> OutputFile;
746   llvm::sys::fs::createTemporaryFile("print-system-gpus", "" /* No Suffix */,
747                                      OutputFile);
748   llvm::FileRemover OutputRemover(OutputFile.c_str());
749   llvm::Optional<llvm::StringRef> Redirects[] = {
750       {""},
751       OutputFile.str(),
752       {""},
753   };
754 
755   std::string ErrorMessage;
756   if (int Result = llvm::sys::ExecuteAndWait(
757           Program.c_str(), {}, {}, Redirects, /* SecondsToWait */ 0,
758           /*MemoryLimit*/ 0, &ErrorMessage)) {
759     if (Result > 0) {
760       ErrorMessage = "Exited with error code " + std::to_string(Result);
761     } else if (Result == -1) {
762       ErrorMessage = "Execute failed: " + ErrorMessage;
763     } else {
764       ErrorMessage = "Crashed: " + ErrorMessage;
765     }
766 
767     return llvm::createStringError(std::error_code(),
768                                    Program + ": " + ErrorMessage);
769   }
770 
771   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> OutputBuf =
772       llvm::MemoryBuffer::getFile(OutputFile.c_str());
773   if (!OutputBuf) {
774     return llvm::createStringError(OutputBuf.getError(),
775                                    "Failed to read stdout of " + Program +
776                                        ": " + OutputBuf.getError().message());
777   }
778 
779   for (llvm::line_iterator LineIt(**OutputBuf); !LineIt.is_at_end(); ++LineIt) {
780     GPUArchs.push_back(LineIt->str());
781   }
782   return llvm::Error::success();
783 }
784 
getSystemGPUArch(const ArgList & Args,std::string & GPUArch) const785 llvm::Error AMDGPUToolChain::getSystemGPUArch(const ArgList &Args,
786                                               std::string &GPUArch) const {
787   // detect the AMDGPU installed in system
788   SmallVector<std::string, 1> GPUArchs;
789   auto Err = detectSystemGPUs(Args, GPUArchs);
790   if (Err) {
791     return Err;
792   }
793   if (GPUArchs.empty()) {
794     return llvm::createStringError(std::error_code(),
795                                    "No AMD GPU detected in the system");
796   }
797   GPUArch = GPUArchs[0];
798   if (GPUArchs.size() > 1) {
799     bool AllSame = std::all_of(
800         GPUArchs.begin(), GPUArchs.end(),
801         [&](const StringRef &GPUArch) { return GPUArch == GPUArchs.front(); });
802     if (!AllSame)
803       return llvm::createStringError(
804           std::error_code(), "Multiple AMD GPUs found with different archs");
805   }
806   return llvm::Error::success();
807 }
808 
addClangTargetOptions(const llvm::opt::ArgList & DriverArgs,llvm::opt::ArgStringList & CC1Args,Action::OffloadKind DeviceOffloadingKind) const809 void ROCMToolChain::addClangTargetOptions(
810     const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
811     Action::OffloadKind DeviceOffloadingKind) const {
812   AMDGPUToolChain::addClangTargetOptions(DriverArgs, CC1Args,
813                                          DeviceOffloadingKind);
814 
815   // For the OpenCL case where there is no offload target, accept -nostdlib to
816   // disable bitcode linking.
817   if (DeviceOffloadingKind == Action::OFK_None &&
818       DriverArgs.hasArg(options::OPT_nostdlib))
819     return;
820 
821   if (DriverArgs.hasArg(options::OPT_nogpulib))
822     return;
823 
824   if (!RocmInstallation.hasDeviceLibrary()) {
825     getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 0;
826     return;
827   }
828 
829   // Get the device name and canonicalize it
830   const StringRef GpuArch = getGPUArch(DriverArgs);
831   auto Kind = llvm::AMDGPU::parseArchAMDGCN(GpuArch);
832   const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(Kind);
833   std::string LibDeviceFile = RocmInstallation.getLibDeviceFile(CanonArch);
834   if (LibDeviceFile.empty()) {
835     getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 1 << GpuArch;
836     return;
837   }
838 
839   bool Wave64 = isWave64(DriverArgs, Kind);
840 
841   // TODO: There are way too many flags that change this. Do we need to check
842   // them all?
843   bool DAZ = DriverArgs.hasArg(options::OPT_cl_denorms_are_zero) ||
844              getDefaultDenormsAreZeroForTarget(Kind);
845   bool FiniteOnly = DriverArgs.hasArg(options::OPT_cl_finite_math_only);
846 
847   bool UnsafeMathOpt =
848       DriverArgs.hasArg(options::OPT_cl_unsafe_math_optimizations);
849   bool FastRelaxedMath = DriverArgs.hasArg(options::OPT_cl_fast_relaxed_math);
850   bool CorrectSqrt =
851       DriverArgs.hasArg(options::OPT_cl_fp32_correctly_rounded_divide_sqrt);
852 
853   // Add the OpenCL specific bitcode library.
854   llvm::SmallVector<std::string, 12> BCLibs;
855   BCLibs.push_back(RocmInstallation.getOpenCLPath().str());
856 
857   // Add the generic set of libraries.
858   BCLibs.append(RocmInstallation.getCommonBitcodeLibs(
859       DriverArgs, LibDeviceFile, Wave64, DAZ, FiniteOnly, UnsafeMathOpt,
860       FastRelaxedMath, CorrectSqrt));
861 
862   llvm::for_each(BCLibs, [&](StringRef BCFile) {
863     CC1Args.push_back("-mlink-builtin-bitcode");
864     CC1Args.push_back(DriverArgs.MakeArgString(BCFile));
865   });
866 }
867 
868 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) const869 RocmInstallationDetector::getCommonBitcodeLibs(
870     const llvm::opt::ArgList &DriverArgs, StringRef LibDeviceFile, bool Wave64,
871     bool DAZ, bool FiniteOnly, bool UnsafeMathOpt, bool FastRelaxedMath,
872     bool CorrectSqrt) const {
873 
874   llvm::SmallVector<std::string, 12> BCLibs;
875 
876   auto AddBCLib = [&](StringRef BCFile) { BCLibs.push_back(BCFile.str()); };
877 
878   AddBCLib(getOCMLPath());
879   AddBCLib(getOCKLPath());
880   AddBCLib(getDenormalsAreZeroPath(DAZ));
881   AddBCLib(getUnsafeMathPath(UnsafeMathOpt || FastRelaxedMath));
882   AddBCLib(getFiniteOnlyPath(FiniteOnly || FastRelaxedMath));
883   AddBCLib(getCorrectlyRoundedSqrtPath(CorrectSqrt));
884   AddBCLib(getWavefrontSize64Path(Wave64));
885   AddBCLib(LibDeviceFile);
886 
887   return BCLibs;
888 }
889 
shouldSkipArgument(const llvm::opt::Arg * A) const890 bool AMDGPUToolChain::shouldSkipArgument(const llvm::opt::Arg *A) const {
891   Option O = A->getOption();
892   if (O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie))
893     return true;
894   return false;
895 }
896 
897 llvm::SmallVector<std::string, 12>
getCommonDeviceLibNames(const llvm::opt::ArgList & DriverArgs,const std::string & GPUArch) const898 ROCMToolChain::getCommonDeviceLibNames(const llvm::opt::ArgList &DriverArgs,
899                                        const std::string &GPUArch) const {
900   auto Kind = llvm::AMDGPU::parseArchAMDGCN(GPUArch);
901   const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(Kind);
902 
903   std::string LibDeviceFile = RocmInstallation.getLibDeviceFile(CanonArch);
904   if (LibDeviceFile.empty()) {
905     getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 1 << GPUArch;
906     return {};
907   }
908 
909   // If --hip-device-lib is not set, add the default bitcode libraries.
910   // TODO: There are way too many flags that change this. Do we need to check
911   // them all?
912   bool DAZ = DriverArgs.hasFlag(options::OPT_fgpu_flush_denormals_to_zero,
913                                 options::OPT_fno_gpu_flush_denormals_to_zero,
914                                 getDefaultDenormsAreZeroForTarget(Kind));
915   bool FiniteOnly = DriverArgs.hasFlag(
916       options::OPT_ffinite_math_only, options::OPT_fno_finite_math_only, false);
917   bool UnsafeMathOpt =
918       DriverArgs.hasFlag(options::OPT_funsafe_math_optimizations,
919                          options::OPT_fno_unsafe_math_optimizations, false);
920   bool FastRelaxedMath = DriverArgs.hasFlag(options::OPT_ffast_math,
921                                             options::OPT_fno_fast_math, false);
922   bool CorrectSqrt = DriverArgs.hasFlag(
923       options::OPT_fhip_fp32_correctly_rounded_divide_sqrt,
924       options::OPT_fno_hip_fp32_correctly_rounded_divide_sqrt);
925   bool Wave64 = isWave64(DriverArgs, Kind);
926 
927   return RocmInstallation.getCommonBitcodeLibs(
928       DriverArgs, LibDeviceFile, Wave64, DAZ, FiniteOnly, UnsafeMathOpt,
929       FastRelaxedMath, CorrectSqrt);
930 }