xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/HIPAMD.cpp (revision 62ff619dcc3540659a319be71c9a489f1659e14a)
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))
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   const char *Lld = Args.MakeArgString(getToolChain().GetProgramPath("lld"));
125   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
126                                          Lld, LldArgs, Inputs, Output));
127 }
128 
129 // For amdgcn the inputs of the linker job are device bitcode and output is
130 // object file. It calls llvm-link, opt, llc, then lld steps.
131 void AMDGCN::Linker::ConstructJob(Compilation &C, const JobAction &JA,
132                                   const InputInfo &Output,
133                                   const InputInfoList &Inputs,
134                                   const ArgList &Args,
135                                   const char *LinkingOutput) const {
136   if (Inputs.size() > 0 &&
137       Inputs[0].getType() == types::TY_Image &&
138       JA.getType() == types::TY_Object)
139     return HIP::constructGenerateObjFileFromHIPFatBinary(C, Output, Inputs,
140                                                          Args, JA, *this);
141 
142   if (JA.getType() == types::TY_HIP_FATBIN)
143     return HIP::constructHIPFatbinCommand(C, JA, Output.getFilename(), Inputs,
144                                           Args, *this);
145 
146   return constructLldCommand(C, JA, Inputs, Output, Args);
147 }
148 
149 HIPAMDToolChain::HIPAMDToolChain(const Driver &D, const llvm::Triple &Triple,
150                                  const ToolChain &HostTC, const ArgList &Args)
151     : ROCMToolChain(D, Triple, Args), HostTC(HostTC) {
152   // Lookup binaries into the driver directory, this is used to
153   // discover the clang-offload-bundler executable.
154   getProgramPaths().push_back(getDriver().Dir);
155 
156   // Diagnose unsupported sanitizer options only once.
157   for (auto A : Args.filtered(options::OPT_fsanitize_EQ)) {
158     SanitizerMask K = parseSanitizerValue(A->getValue(), /*AllowGroups=*/false);
159     if (K != SanitizerKind::Address)
160       D.getDiags().Report(clang::diag::warn_drv_unsupported_option_for_target)
161           << A->getAsString(Args) << getTriple().str();
162   }
163 }
164 
165 void HIPAMDToolChain::addClangTargetOptions(
166     const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
167     Action::OffloadKind DeviceOffloadingKind) const {
168   HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
169 
170   assert(DeviceOffloadingKind == Action::OFK_HIP &&
171          "Only HIP offloading kinds are supported for GPUs.");
172 
173   CC1Args.push_back("-fcuda-is-device");
174 
175   if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals,
176                          options::OPT_fno_cuda_approx_transcendentals, false))
177     CC1Args.push_back("-fcuda-approx-transcendentals");
178 
179   if (!DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
180                           false))
181     CC1Args.append({"-mllvm", "-amdgpu-internalize-symbols"});
182 
183   StringRef MaxThreadsPerBlock =
184       DriverArgs.getLastArgValue(options::OPT_gpu_max_threads_per_block_EQ);
185   if (!MaxThreadsPerBlock.empty()) {
186     std::string ArgStr =
187         std::string("--gpu-max-threads-per-block=") + MaxThreadsPerBlock.str();
188     CC1Args.push_back(DriverArgs.MakeArgStringRef(ArgStr));
189   }
190 
191   CC1Args.push_back("-fcuda-allow-variadic-functions");
192 
193   // Default to "hidden" visibility, as object level linking will not be
194   // supported for the foreseeable future.
195   if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
196                          options::OPT_fvisibility_ms_compat)) {
197     CC1Args.append({"-fvisibility", "hidden"});
198     CC1Args.push_back("-fapply-global-visibility-to-externs");
199   }
200 
201   llvm::for_each(getHIPDeviceLibs(DriverArgs), [&](auto BCFile) {
202     CC1Args.push_back(BCFile.ShouldInternalize ? "-mlink-builtin-bitcode"
203                                                : "-mlink-bitcode-file");
204     CC1Args.push_back(DriverArgs.MakeArgString(BCFile.Path));
205   });
206 }
207 
208 llvm::opt::DerivedArgList *
209 HIPAMDToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
210                                StringRef BoundArch,
211                                Action::OffloadKind DeviceOffloadKind) const {
212   DerivedArgList *DAL =
213       HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind);
214   if (!DAL)
215     DAL = new DerivedArgList(Args.getBaseArgs());
216 
217   const OptTable &Opts = getDriver().getOpts();
218 
219   for (Arg *A : Args) {
220     if (!shouldSkipArgument(A) &&
221         !shouldSkipSanitizeOption(*this, Args, BoundArch, A))
222       DAL->append(A);
223   }
224 
225   if (!BoundArch.empty()) {
226     DAL->eraseArg(options::OPT_mcpu_EQ);
227     DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_mcpu_EQ), BoundArch);
228     checkTargetID(*DAL);
229   }
230 
231   return DAL;
232 }
233 
234 Tool *HIPAMDToolChain::buildLinker() const {
235   assert(getTriple().getArch() == llvm::Triple::amdgcn);
236   return new tools::AMDGCN::Linker(*this);
237 }
238 
239 void HIPAMDToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
240   HostTC.addClangWarningOptions(CC1Args);
241 }
242 
243 ToolChain::CXXStdlibType
244 HIPAMDToolChain::GetCXXStdlibType(const ArgList &Args) const {
245   return HostTC.GetCXXStdlibType(Args);
246 }
247 
248 void HIPAMDToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
249                                                 ArgStringList &CC1Args) const {
250   HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
251 }
252 
253 void HIPAMDToolChain::AddClangCXXStdlibIncludeArgs(
254     const ArgList &Args, ArgStringList &CC1Args) const {
255   HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args);
256 }
257 
258 void HIPAMDToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
259                                           ArgStringList &CC1Args) const {
260   HostTC.AddIAMCUIncludeArgs(Args, CC1Args);
261 }
262 
263 void HIPAMDToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs,
264                                         ArgStringList &CC1Args) const {
265   RocmInstallation.AddHIPIncludeArgs(DriverArgs, CC1Args);
266 }
267 
268 SanitizerMask HIPAMDToolChain::getSupportedSanitizers() const {
269   // The HIPAMDToolChain only supports sanitizers in the sense that it allows
270   // sanitizer arguments on the command line if they are supported by the host
271   // toolchain. The HIPAMDToolChain will actually ignore any command line
272   // arguments for any of these "supported" sanitizers. That means that no
273   // sanitization of device code is actually supported at this time.
274   //
275   // This behavior is necessary because the host and device toolchains
276   // invocations often share the command line, so the device toolchain must
277   // tolerate flags meant only for the host toolchain.
278   return HostTC.getSupportedSanitizers();
279 }
280 
281 VersionTuple HIPAMDToolChain::computeMSVCVersion(const Driver *D,
282                                                  const ArgList &Args) const {
283   return HostTC.computeMSVCVersion(D, Args);
284 }
285 
286 llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
287 HIPAMDToolChain::getHIPDeviceLibs(const llvm::opt::ArgList &DriverArgs) const {
288   llvm::SmallVector<BitCodeLibraryInfo, 12> BCLibs;
289   if (DriverArgs.hasArg(options::OPT_nogpulib))
290     return {};
291   ArgStringList LibraryPaths;
292 
293   // Find in --hip-device-lib-path and HIP_LIBRARY_PATH.
294   for (auto Path : RocmInstallation.getRocmDeviceLibPathArg())
295     LibraryPaths.push_back(DriverArgs.MakeArgString(Path));
296 
297   addDirectoryList(DriverArgs, LibraryPaths, "", "HIP_DEVICE_LIB_PATH");
298 
299   // Maintain compatability with --hip-device-lib.
300   auto BCLibArgs = DriverArgs.getAllArgValues(options::OPT_hip_device_lib_EQ);
301   if (!BCLibArgs.empty()) {
302     llvm::for_each(BCLibArgs, [&](StringRef BCName) {
303       StringRef FullName;
304       for (std::string LibraryPath : LibraryPaths) {
305         SmallString<128> Path(LibraryPath);
306         llvm::sys::path::append(Path, BCName);
307         FullName = Path;
308         if (llvm::sys::fs::exists(FullName)) {
309           BCLibs.push_back(FullName);
310           return;
311         }
312       }
313       getDriver().Diag(diag::err_drv_no_such_file) << BCName;
314     });
315   } else {
316     if (!RocmInstallation.hasDeviceLibrary()) {
317       getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 0;
318       return {};
319     }
320     StringRef GpuArch = getGPUArch(DriverArgs);
321     assert(!GpuArch.empty() && "Must have an explicit GPU arch.");
322 
323     // If --hip-device-lib is not set, add the default bitcode libraries.
324     if (DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
325                            options::OPT_fno_gpu_sanitize) &&
326         getSanitizerArgs(DriverArgs).needsAsanRt()) {
327       auto AsanRTL = RocmInstallation.getAsanRTLPath();
328       if (AsanRTL.empty()) {
329         unsigned DiagID = getDriver().getDiags().getCustomDiagID(
330             DiagnosticsEngine::Error,
331             "AMDGPU address sanitizer runtime library (asanrtl) is not found. "
332             "Please install ROCm device library which supports address "
333             "sanitizer");
334         getDriver().Diag(DiagID);
335         return {};
336       } else
337         BCLibs.push_back({AsanRTL.str(), /*ShouldInternalize=*/false});
338     }
339 
340     // Add the HIP specific bitcode library.
341     BCLibs.push_back(RocmInstallation.getHIPPath());
342 
343     // Add common device libraries like ocml etc.
344     for (auto N : getCommonDeviceLibNames(DriverArgs, GpuArch.str()))
345       BCLibs.push_back(StringRef(N));
346 
347     // Add instrument lib.
348     auto InstLib =
349         DriverArgs.getLastArgValue(options::OPT_gpu_instrument_lib_EQ);
350     if (InstLib.empty())
351       return BCLibs;
352     if (llvm::sys::fs::exists(InstLib))
353       BCLibs.push_back(InstLib);
354     else
355       getDriver().Diag(diag::err_drv_no_such_file) << InstLib;
356   }
357 
358   return BCLibs;
359 }
360 
361 void HIPAMDToolChain::checkTargetID(
362     const llvm::opt::ArgList &DriverArgs) const {
363   auto PTID = getParsedTargetID(DriverArgs);
364   if (PTID.OptionalTargetID && !PTID.OptionalGPUArch) {
365     getDriver().Diag(clang::diag::err_drv_bad_target_id)
366         << PTID.OptionalTargetID.getValue();
367   }
368 }
369