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