1 //===--- HIPAMD.cpp - HIP Tool and 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 "HIPAMD.h"
10 #include "AMDGPU.h"
11 #include "CommonArgs.h"
12 #include "HIPUtility.h"
13 #include "clang/Basic/Cuda.h"
14 #include "clang/Basic/TargetID.h"
15 #include "clang/Driver/Compilation.h"
16 #include "clang/Driver/Driver.h"
17 #include "clang/Driver/DriverDiagnostic.h"
18 #include "clang/Driver/InputInfo.h"
19 #include "clang/Driver/Options.h"
20 #include "clang/Driver/SanitizerArgs.h"
21 #include "llvm/Support/Alignment.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/Path.h"
24 #include "llvm/Support/TargetParser.h"
25 
26 using namespace clang::driver;
27 using namespace clang::driver::toolchains;
28 using namespace clang::driver::tools;
29 using namespace clang;
30 using namespace llvm::opt;
31 
32 #if defined(_WIN32) || defined(_WIN64)
33 #define NULL_FILE "nul"
34 #else
35 #define NULL_FILE "/dev/null"
36 #endif
37 
38 static bool shouldSkipSanitizeOption(const ToolChain &TC,
39                                      const llvm::opt::ArgList &DriverArgs,
40                                      StringRef TargetID,
41                                      const llvm::opt::Arg *A) {
42   // For actions without targetID, do nothing.
43   if (TargetID.empty())
44     return false;
45   Option O = A->getOption();
46   if (!O.matches(options::OPT_fsanitize_EQ))
47     return false;
48 
49   if (!DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
50                           options::OPT_fno_gpu_sanitize, true))
51     return true;
52 
53   auto &Diags = TC.getDriver().getDiags();
54 
55   // For simplicity, we only allow -fsanitize=address
56   SanitizerMask K = parseSanitizerValue(A->getValue(), /*AllowGroups=*/false);
57   if (K != SanitizerKind::Address)
58     return true;
59 
60   llvm::StringMap<bool> FeatureMap;
61   auto OptionalGpuArch = parseTargetID(TC.getTriple(), TargetID, &FeatureMap);
62 
63   assert(OptionalGpuArch && "Invalid Target ID");
64   (void)OptionalGpuArch;
65   auto Loc = FeatureMap.find("xnack");
66   if (Loc == FeatureMap.end() || !Loc->second) {
67     Diags.Report(
68         clang::diag::warn_drv_unsupported_option_for_offload_arch_req_feature)
69         << A->getAsString(DriverArgs) << TargetID << "xnack+";
70     return true;
71   }
72   return false;
73 }
74 
75 void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA,
76                                          const InputInfoList &Inputs,
77                                          const InputInfo &Output,
78                                          const llvm::opt::ArgList &Args) const {
79   // Construct lld command.
80   // The output from ld.lld is an HSA code object file.
81   ArgStringList LldArgs{"-flavor", "gnu", "--no-undefined", "-shared",
82                         "-plugin-opt=-amdgpu-internalize-symbols"};
83 
84   auto &TC = getToolChain();
85   auto &D = TC.getDriver();
86   assert(!Inputs.empty() && "Must have at least one input.");
87   bool IsThinLTO = D.getLTOMode(/*IsOffload=*/true) == LTOK_Thin;
88   addLTOOptions(TC, Args, LldArgs, Output, Inputs[0], IsThinLTO);
89 
90   // Extract all the -m options
91   std::vector<llvm::StringRef> Features;
92   amdgpu::getAMDGPUTargetFeatures(D, TC.getTriple(), Args, Features);
93 
94   // Add features to mattr such as cumode
95   std::string MAttrString = "-plugin-opt=-mattr=";
96   for (auto OneFeature : unifyTargetFeatures(Features)) {
97     MAttrString.append(Args.MakeArgString(OneFeature));
98     if (OneFeature != Features.back())
99       MAttrString.append(",");
100   }
101   if (!Features.empty())
102     LldArgs.push_back(Args.MakeArgString(MAttrString));
103 
104   // ToDo: Remove this option after AMDGPU backend supports ISA-level linking.
105   // Since AMDGPU backend currently does not support ISA-level linking, all
106   // called functions need to be imported.
107   if (IsThinLTO)
108     LldArgs.push_back(Args.MakeArgString("-plugin-opt=-force-import-all"));
109 
110   for (const Arg *A : Args.filtered(options::OPT_mllvm)) {
111     LldArgs.push_back(
112         Args.MakeArgString(Twine("-plugin-opt=") + A->getValue(0)));
113   }
114 
115   if (C.getDriver().isSaveTempsEnabled())
116     LldArgs.push_back("-save-temps");
117 
118   addLinkerCompressDebugSectionsOption(TC, Args, LldArgs);
119 
120   LldArgs.append({"-o", Output.getFilename()});
121   for (auto Input : Inputs)
122     LldArgs.push_back(Input.getFilename());
123 
124   // Look for archive of bundled bitcode in arguments, and add temporary files
125   // for the extracted archive of bitcode to inputs.
126   auto TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ);
127   AddStaticDeviceLibsLinking(C, *this, JA, Inputs, Args, LldArgs, "amdgcn",
128                              TargetID,
129                              /*IsBitCodeSDL=*/true,
130                              /*PostClangLink=*/false);
131 
132   const char *Lld = Args.MakeArgString(getToolChain().GetProgramPath("lld"));
133   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
134                                          Lld, LldArgs, Inputs, Output));
135 }
136 
137 // For amdgcn the inputs of the linker job are device bitcode and output is
138 // object file. It calls llvm-link, opt, llc, then lld steps.
139 void AMDGCN::Linker::ConstructJob(Compilation &C, const JobAction &JA,
140                                   const InputInfo &Output,
141                                   const InputInfoList &Inputs,
142                                   const ArgList &Args,
143                                   const char *LinkingOutput) const {
144   if (Inputs.size() > 0 &&
145       Inputs[0].getType() == types::TY_Image &&
146       JA.getType() == types::TY_Object)
147     return HIP::constructGenerateObjFileFromHIPFatBinary(C, Output, Inputs,
148                                                          Args, JA, *this);
149 
150   if (JA.getType() == types::TY_HIP_FATBIN)
151     return HIP::constructHIPFatbinCommand(C, JA, Output.getFilename(), Inputs,
152                                           Args, *this);
153 
154   return constructLldCommand(C, JA, Inputs, Output, Args);
155 }
156 
157 HIPAMDToolChain::HIPAMDToolChain(const Driver &D, const llvm::Triple &Triple,
158                                  const ToolChain &HostTC, const ArgList &Args)
159     : ROCMToolChain(D, Triple, Args), HostTC(HostTC) {
160   // Lookup binaries into the driver directory, this is used to
161   // discover the clang-offload-bundler executable.
162   getProgramPaths().push_back(getDriver().Dir);
163 
164   // Diagnose unsupported sanitizer options only once.
165   if (!Args.hasFlag(options::OPT_fgpu_sanitize, options::OPT_fno_gpu_sanitize,
166                     true))
167     return;
168   for (auto A : Args.filtered(options::OPT_fsanitize_EQ)) {
169     SanitizerMask K = parseSanitizerValue(A->getValue(), /*AllowGroups=*/false);
170     if (K != SanitizerKind::Address)
171       D.getDiags().Report(clang::diag::warn_drv_unsupported_option_for_target)
172           << A->getAsString(Args) << getTriple().str();
173   }
174 }
175 
176 void HIPAMDToolChain::addClangTargetOptions(
177     const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
178     Action::OffloadKind DeviceOffloadingKind) const {
179   HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
180 
181   assert(DeviceOffloadingKind == Action::OFK_HIP &&
182          "Only HIP offloading kinds are supported for GPUs.");
183 
184   CC1Args.push_back("-fcuda-is-device");
185 
186   if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals,
187                          options::OPT_fno_cuda_approx_transcendentals, false))
188     CC1Args.push_back("-fcuda-approx-transcendentals");
189 
190   if (!DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
191                           false))
192     CC1Args.append({"-mllvm", "-amdgpu-internalize-symbols"});
193 
194   StringRef MaxThreadsPerBlock =
195       DriverArgs.getLastArgValue(options::OPT_gpu_max_threads_per_block_EQ);
196   if (!MaxThreadsPerBlock.empty()) {
197     std::string ArgStr =
198         std::string("--gpu-max-threads-per-block=") + MaxThreadsPerBlock.str();
199     CC1Args.push_back(DriverArgs.MakeArgStringRef(ArgStr));
200   }
201 
202   CC1Args.push_back("-fcuda-allow-variadic-functions");
203 
204   // Default to "hidden" visibility, as object level linking will not be
205   // supported for the foreseeable future.
206   if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
207                          options::OPT_fvisibility_ms_compat)) {
208     CC1Args.append({"-fvisibility", "hidden"});
209     CC1Args.push_back("-fapply-global-visibility-to-externs");
210   }
211 
212   llvm::for_each(getHIPDeviceLibs(DriverArgs), [&](auto BCFile) {
213     CC1Args.push_back(BCFile.ShouldInternalize ? "-mlink-builtin-bitcode"
214                                                : "-mlink-bitcode-file");
215     CC1Args.push_back(DriverArgs.MakeArgString(BCFile.Path));
216   });
217 }
218 
219 llvm::opt::DerivedArgList *
220 HIPAMDToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
221                                StringRef BoundArch,
222                                Action::OffloadKind DeviceOffloadKind) const {
223   DerivedArgList *DAL =
224       HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind);
225   if (!DAL)
226     DAL = new DerivedArgList(Args.getBaseArgs());
227 
228   const OptTable &Opts = getDriver().getOpts();
229 
230   for (Arg *A : Args) {
231     if (!shouldSkipArgument(A) &&
232         !shouldSkipSanitizeOption(*this, Args, BoundArch, A))
233       DAL->append(A);
234   }
235 
236   if (!BoundArch.empty()) {
237     DAL->eraseArg(options::OPT_mcpu_EQ);
238     DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_mcpu_EQ), BoundArch);
239     checkTargetID(*DAL);
240   }
241 
242   return DAL;
243 }
244 
245 Tool *HIPAMDToolChain::buildLinker() const {
246   assert(getTriple().getArch() == llvm::Triple::amdgcn);
247   return new tools::AMDGCN::Linker(*this);
248 }
249 
250 void HIPAMDToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
251   HostTC.addClangWarningOptions(CC1Args);
252 }
253 
254 ToolChain::CXXStdlibType
255 HIPAMDToolChain::GetCXXStdlibType(const ArgList &Args) const {
256   return HostTC.GetCXXStdlibType(Args);
257 }
258 
259 void HIPAMDToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
260                                                 ArgStringList &CC1Args) const {
261   HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
262 }
263 
264 void HIPAMDToolChain::AddClangCXXStdlibIncludeArgs(
265     const ArgList &Args, ArgStringList &CC1Args) const {
266   HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args);
267 }
268 
269 void HIPAMDToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
270                                           ArgStringList &CC1Args) const {
271   HostTC.AddIAMCUIncludeArgs(Args, CC1Args);
272 }
273 
274 void HIPAMDToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs,
275                                         ArgStringList &CC1Args) const {
276   RocmInstallation.AddHIPIncludeArgs(DriverArgs, CC1Args);
277 }
278 
279 SanitizerMask HIPAMDToolChain::getSupportedSanitizers() const {
280   // The HIPAMDToolChain only supports sanitizers in the sense that it allows
281   // sanitizer arguments on the command line if they are supported by the host
282   // toolchain. The HIPAMDToolChain will actually ignore any command line
283   // arguments for any of these "supported" sanitizers. That means that no
284   // sanitization of device code is actually supported at this time.
285   //
286   // This behavior is necessary because the host and device toolchains
287   // invocations often share the command line, so the device toolchain must
288   // tolerate flags meant only for the host toolchain.
289   return HostTC.getSupportedSanitizers();
290 }
291 
292 VersionTuple HIPAMDToolChain::computeMSVCVersion(const Driver *D,
293                                                  const ArgList &Args) const {
294   return HostTC.computeMSVCVersion(D, Args);
295 }
296 
297 llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
298 HIPAMDToolChain::getHIPDeviceLibs(const llvm::opt::ArgList &DriverArgs) const {
299   llvm::SmallVector<BitCodeLibraryInfo, 12> BCLibs;
300   if (DriverArgs.hasArg(options::OPT_nogpulib))
301     return {};
302   ArgStringList LibraryPaths;
303 
304   // Find in --hip-device-lib-path and HIP_LIBRARY_PATH.
305   for (auto Path : RocmInstallation.getRocmDeviceLibPathArg())
306     LibraryPaths.push_back(DriverArgs.MakeArgString(Path));
307 
308   addDirectoryList(DriverArgs, LibraryPaths, "", "HIP_DEVICE_LIB_PATH");
309 
310   // Maintain compatability with --hip-device-lib.
311   auto BCLibArgs = DriverArgs.getAllArgValues(options::OPT_hip_device_lib_EQ);
312   if (!BCLibArgs.empty()) {
313     llvm::for_each(BCLibArgs, [&](StringRef BCName) {
314       StringRef FullName;
315       for (std::string LibraryPath : LibraryPaths) {
316         SmallString<128> Path(LibraryPath);
317         llvm::sys::path::append(Path, BCName);
318         FullName = Path;
319         if (llvm::sys::fs::exists(FullName)) {
320           BCLibs.push_back(FullName);
321           return;
322         }
323       }
324       getDriver().Diag(diag::err_drv_no_such_file) << BCName;
325     });
326   } else {
327     if (!RocmInstallation.hasDeviceLibrary()) {
328       getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 0;
329       return {};
330     }
331     StringRef GpuArch = getGPUArch(DriverArgs);
332     assert(!GpuArch.empty() && "Must have an explicit GPU arch.");
333 
334     // If --hip-device-lib is not set, add the default bitcode libraries.
335     if (DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
336                            options::OPT_fno_gpu_sanitize, true) &&
337         getSanitizerArgs(DriverArgs).needsAsanRt()) {
338       auto AsanRTL = RocmInstallation.getAsanRTLPath();
339       if (AsanRTL.empty()) {
340         unsigned DiagID = getDriver().getDiags().getCustomDiagID(
341             DiagnosticsEngine::Error,
342             "AMDGPU address sanitizer runtime library (asanrtl) is not found. "
343             "Please install ROCm device library which supports address "
344             "sanitizer");
345         getDriver().Diag(DiagID);
346         return {};
347       } else
348         BCLibs.push_back({AsanRTL.str(), /*ShouldInternalize=*/false});
349     }
350 
351     // Add the HIP specific bitcode library.
352     BCLibs.push_back(RocmInstallation.getHIPPath());
353 
354     // Add common device libraries like ocml etc.
355     for (auto N : getCommonDeviceLibNames(DriverArgs, GpuArch.str()))
356       BCLibs.push_back(StringRef(N));
357 
358     // Add instrument lib.
359     auto InstLib =
360         DriverArgs.getLastArgValue(options::OPT_gpu_instrument_lib_EQ);
361     if (InstLib.empty())
362       return BCLibs;
363     if (llvm::sys::fs::exists(InstLib))
364       BCLibs.push_back(InstLib);
365     else
366       getDriver().Diag(diag::err_drv_no_such_file) << InstLib;
367   }
368 
369   return BCLibs;
370 }
371 
372 void HIPAMDToolChain::checkTargetID(
373     const llvm::opt::ArgList &DriverArgs) const {
374   auto PTID = getParsedTargetID(DriverArgs);
375   if (PTID.OptionalTargetID && !PTID.OptionalGPUArch) {
376     getDriver().Diag(clang::diag::err_drv_bad_target_id)
377         << PTID.OptionalTargetID.getValue();
378   }
379 }
380