xref: /llvm-project/clang/lib/Driver/ToolChains/HIPAMD.cpp (revision c805df69994a1f1e8f095b690c9af9e9c463906b)
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 "SPIRV.h"
14 #include "clang/Basic/Cuda.h"
15 #include "clang/Basic/TargetID.h"
16 #include "clang/Driver/Compilation.h"
17 #include "clang/Driver/Driver.h"
18 #include "clang/Driver/DriverDiagnostic.h"
19 #include "clang/Driver/InputInfo.h"
20 #include "clang/Driver/Options.h"
21 #include "clang/Driver/SanitizerArgs.h"
22 #include "llvm/Support/Alignment.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/Path.h"
25 #include "llvm/TargetParser/TargetParser.h"
26 
27 using namespace clang::driver;
28 using namespace clang::driver::toolchains;
29 using namespace clang::driver::tools;
30 using namespace clang;
31 using namespace llvm::opt;
32 
33 #if defined(_WIN32) || defined(_WIN64)
34 #define NULL_FILE "nul"
35 #else
36 #define NULL_FILE "/dev/null"
37 #endif
38 
39 void AMDGCN::Linker::constructLlvmLinkCommand(Compilation &C,
40                                          const JobAction &JA,
41                                          const InputInfoList &Inputs,
42                                          const InputInfo &Output,
43                                          const llvm::opt::ArgList &Args) const {
44   // Construct llvm-link command.
45   // The output from llvm-link is a bitcode file.
46   ArgStringList LlvmLinkArgs;
47 
48   assert(!Inputs.empty() && "Must have at least one input.");
49 
50   LlvmLinkArgs.append({"-o", Output.getFilename()});
51   for (auto Input : Inputs)
52     LlvmLinkArgs.push_back(Input.getFilename());
53 
54   // Look for archive of bundled bitcode in arguments, and add temporary files
55   // for the extracted archive of bitcode to inputs.
56   auto TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ);
57   AddStaticDeviceLibsLinking(C, *this, JA, Inputs, Args, LlvmLinkArgs, "amdgcn",
58                              TargetID, /*IsBitCodeSDL=*/true);
59 
60   const char *LlvmLink =
61     Args.MakeArgString(getToolChain().GetProgramPath("llvm-link"));
62   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
63                                          LlvmLink, LlvmLinkArgs, Inputs,
64                                          Output));
65 }
66 
67 void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA,
68                                          const InputInfoList &Inputs,
69                                          const InputInfo &Output,
70                                          const llvm::opt::ArgList &Args) const {
71   // Construct lld command.
72   // The output from ld.lld is an HSA code object file.
73   ArgStringList LldArgs{"-flavor",
74                         "gnu",
75                         "-m",
76                         "elf64_amdgpu",
77                         "--no-undefined",
78                         "-shared",
79                         "-plugin-opt=-amdgpu-internalize-symbols"};
80   if (Args.hasArg(options::OPT_hipstdpar))
81     LldArgs.push_back("-plugin-opt=-amdgpu-enable-hipstdpar");
82 
83   auto &TC = getToolChain();
84   auto &D = TC.getDriver();
85   assert(!Inputs.empty() && "Must have at least one input.");
86   bool IsThinLTO = D.getOffloadLTOMode() == LTOK_Thin;
87   addLTOOptions(TC, Args, LldArgs, Output, Inputs[0], IsThinLTO);
88 
89   // Extract all the -m options
90   std::vector<llvm::StringRef> Features;
91   amdgpu::getAMDGPUTargetFeatures(D, TC.getTriple(), Args, Features);
92 
93   // Add features to mattr such as cumode
94   std::string MAttrString = "-plugin-opt=-mattr=";
95   for (auto OneFeature : unifyTargetFeatures(Features)) {
96     MAttrString.append(Args.MakeArgString(OneFeature));
97     if (OneFeature != Features.back())
98       MAttrString.append(",");
99   }
100   if (!Features.empty())
101     LldArgs.push_back(Args.MakeArgString(MAttrString));
102 
103   // ToDo: Remove this option after AMDGPU backend supports ISA-level linking.
104   // Since AMDGPU backend currently does not support ISA-level linking, all
105   // called functions need to be imported.
106   if (IsThinLTO)
107     LldArgs.push_back(Args.MakeArgString("-plugin-opt=-force-import-all"));
108 
109   for (const Arg *A : Args.filtered(options::OPT_mllvm)) {
110     LldArgs.push_back(
111         Args.MakeArgString(Twine("-plugin-opt=") + A->getValue(0)));
112   }
113 
114   if (C.getDriver().isSaveTempsEnabled())
115     LldArgs.push_back("-save-temps");
116 
117   addLinkerCompressDebugSectionsOption(TC, Args, LldArgs);
118 
119   // Given that host and device linking happen in separate processes, the device
120   // linker doesn't always have the visibility as to which device symbols are
121   // needed by a program, especially for the device symbol dependencies that are
122   // introduced through the host symbol resolution.
123   // For example: host_A() (A.obj) --> host_B(B.obj) --> device_kernel_B()
124   // (B.obj) In this case, the device linker doesn't know that A.obj actually
125   // depends on the kernel functions in B.obj.  When linking to static device
126   // library, the device linker may drop some of the device global symbols if
127   // they aren't referenced.  As a workaround, we are adding to the
128   // --whole-archive flag such that all global symbols would be linked in.
129   LldArgs.push_back("--whole-archive");
130 
131   for (auto *Arg : Args.filtered(options::OPT_Xoffload_linker)) {
132     StringRef ArgVal = Arg->getValue(1);
133     auto SplitArg = ArgVal.split("-mllvm=");
134     if (!SplitArg.second.empty()) {
135       LldArgs.push_back(
136           Args.MakeArgString(Twine("-plugin-opt=") + SplitArg.second));
137     } else {
138       LldArgs.push_back(Args.MakeArgString(ArgVal));
139     }
140     Arg->claim();
141   }
142 
143   LldArgs.append({"-o", Output.getFilename()});
144   for (auto Input : Inputs)
145     LldArgs.push_back(Input.getFilename());
146 
147   // Look for archive of bundled bitcode in arguments, and add temporary files
148   // for the extracted archive of bitcode to inputs.
149   auto TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ);
150   AddStaticDeviceLibsLinking(C, *this, JA, Inputs, Args, LldArgs, "amdgcn",
151                              TargetID, /*IsBitCodeSDL=*/true);
152 
153   LldArgs.push_back("--no-whole-archive");
154 
155   const char *Lld = Args.MakeArgString(getToolChain().GetProgramPath("lld"));
156   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
157                                          Lld, LldArgs, Inputs, Output));
158 }
159 
160 // For SPIR-V the inputs for the job are device AMDGCN SPIR-V flavoured bitcode
161 // and the output is either a compiled SPIR-V binary or bitcode (-emit-llvm). It
162 // calls llvm-link and then the llvm-spirv translator. Once the SPIR-V BE will
163 // be promoted from experimental, we will switch to using that. TODO: consider
164 // if we want to run any targeted optimisations over IR here, over generic
165 // SPIR-V.
166 void AMDGCN::Linker::constructLinkAndEmitSpirvCommand(
167     Compilation &C, const JobAction &JA, const InputInfoList &Inputs,
168     const InputInfo &Output, const llvm::opt::ArgList &Args) const {
169   assert(!Inputs.empty() && "Must have at least one input.");
170 
171   constructLlvmLinkCommand(C, JA, Inputs, Output, Args);
172 
173   // Linked BC is now in Output
174 
175   // Emit SPIR-V binary.
176   llvm::opt::ArgStringList TrArgs{
177       "--spirv-max-version=1.6",
178       "--spirv-ext=+all",
179       "--spirv-allow-unknown-intrinsics",
180       "--spirv-lower-const-expr",
181       "--spirv-preserve-auxdata",
182       "--spirv-debug-info-version=nonsemantic-shader-200"};
183   SPIRV::constructTranslateCommand(C, *this, JA, Output, Output, TrArgs);
184 }
185 
186 // For amdgcn the inputs of the linker job are device bitcode and output is
187 // either an object file or bitcode (-emit-llvm). It calls llvm-link, opt,
188 // llc, then lld steps.
189 void AMDGCN::Linker::ConstructJob(Compilation &C, const JobAction &JA,
190                                   const InputInfo &Output,
191                                   const InputInfoList &Inputs,
192                                   const ArgList &Args,
193                                   const char *LinkingOutput) const {
194   if (Inputs.size() > 0 &&
195       Inputs[0].getType() == types::TY_Image &&
196       JA.getType() == types::TY_Object)
197     return HIP::constructGenerateObjFileFromHIPFatBinary(C, Output, Inputs,
198                                                          Args, JA, *this);
199 
200   if (JA.getType() == types::TY_HIP_FATBIN)
201     return HIP::constructHIPFatbinCommand(C, JA, Output.getFilename(), Inputs,
202                                           Args, *this);
203 
204   if (JA.getType() == types::TY_LLVM_BC)
205     return constructLlvmLinkCommand(C, JA, Inputs, Output, Args);
206 
207   if (getToolChain().getEffectiveTriple().isSPIRV())
208     return constructLinkAndEmitSpirvCommand(C, JA, Inputs, Output, Args);
209 
210   return constructLldCommand(C, JA, Inputs, Output, Args);
211 }
212 
213 HIPAMDToolChain::HIPAMDToolChain(const Driver &D, const llvm::Triple &Triple,
214                                  const ToolChain &HostTC, const ArgList &Args)
215     : ROCMToolChain(D, Triple, Args), HostTC(HostTC) {
216   // Lookup binaries into the driver directory, this is used to
217   // discover the clang-offload-bundler executable.
218   getProgramPaths().push_back(getDriver().Dir);
219 
220   // Diagnose unsupported sanitizer options only once.
221   if (!Args.hasFlag(options::OPT_fgpu_sanitize, options::OPT_fno_gpu_sanitize,
222                     true))
223     return;
224   for (auto *A : Args.filtered(options::OPT_fsanitize_EQ)) {
225     SanitizerMask K = parseSanitizerValue(A->getValue(), /*AllowGroups=*/false);
226     if (K != SanitizerKind::Address)
227       D.getDiags().Report(clang::diag::warn_drv_unsupported_option_for_target)
228           << A->getAsString(Args) << getTriple().str();
229   }
230 }
231 
232 void HIPAMDToolChain::addClangTargetOptions(
233     const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
234     Action::OffloadKind DeviceOffloadingKind) const {
235   HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
236 
237   assert(DeviceOffloadingKind == Action::OFK_HIP &&
238          "Only HIP offloading kinds are supported for GPUs.");
239 
240   CC1Args.append({"-fcuda-is-device", "-fno-threadsafe-statics"});
241 
242   if (!DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
243                           false))
244     CC1Args.append({"-mllvm", "-amdgpu-internalize-symbols"});
245   if (DriverArgs.hasArgNoClaim(options::OPT_hipstdpar))
246     CC1Args.append({"-mllvm", "-amdgpu-enable-hipstdpar"});
247 
248   StringRef MaxThreadsPerBlock =
249       DriverArgs.getLastArgValue(options::OPT_gpu_max_threads_per_block_EQ);
250   if (!MaxThreadsPerBlock.empty()) {
251     std::string ArgStr =
252         (Twine("--gpu-max-threads-per-block=") + MaxThreadsPerBlock).str();
253     CC1Args.push_back(DriverArgs.MakeArgStringRef(ArgStr));
254   }
255 
256   CC1Args.push_back("-fcuda-allow-variadic-functions");
257 
258   // Default to "hidden" visibility, as object level linking will not be
259   // supported for the foreseeable future.
260   if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
261                          options::OPT_fvisibility_ms_compat)) {
262     CC1Args.append({"-fvisibility=hidden"});
263     CC1Args.push_back("-fapply-global-visibility-to-externs");
264   }
265 
266   if (getEffectiveTriple().isSPIRV()) {
267     // For SPIR-V we embed the command-line into the generated binary, in order
268     // to retrieve it at JIT time and be able to do target specific compilation
269     // with options that match the user-supplied ones.
270     if (!DriverArgs.hasArg(options::OPT_fembed_bitcode_marker))
271       CC1Args.push_back("-fembed-bitcode=marker");
272     return; // No DeviceLibs for SPIR-V.
273   }
274 
275   for (auto BCFile : getDeviceLibs(DriverArgs)) {
276     CC1Args.push_back(BCFile.ShouldInternalize ? "-mlink-builtin-bitcode"
277                                                : "-mlink-bitcode-file");
278     CC1Args.push_back(DriverArgs.MakeArgString(BCFile.Path));
279   }
280 }
281 
282 llvm::opt::DerivedArgList *
283 HIPAMDToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
284                                StringRef BoundArch,
285                                Action::OffloadKind DeviceOffloadKind) const {
286   DerivedArgList *DAL =
287       HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind);
288   if (!DAL)
289     DAL = new DerivedArgList(Args.getBaseArgs());
290 
291   const OptTable &Opts = getDriver().getOpts();
292 
293   for (Arg *A : Args) {
294     if (!shouldSkipSanitizeOption(*this, Args, BoundArch, A))
295       DAL->append(A);
296   }
297 
298   if (!BoundArch.empty()) {
299     DAL->eraseArg(options::OPT_mcpu_EQ);
300     DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_mcpu_EQ), BoundArch);
301     checkTargetID(*DAL);
302   }
303 
304   return DAL;
305 }
306 
307 Tool *HIPAMDToolChain::buildLinker() const {
308   assert(getTriple().getArch() == llvm::Triple::amdgcn ||
309          getTriple().getArch() == llvm::Triple::spirv64);
310   return new tools::AMDGCN::Linker(*this);
311 }
312 
313 void HIPAMDToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
314   AMDGPUToolChain::addClangWarningOptions(CC1Args);
315   HostTC.addClangWarningOptions(CC1Args);
316 }
317 
318 ToolChain::CXXStdlibType
319 HIPAMDToolChain::GetCXXStdlibType(const ArgList &Args) const {
320   return HostTC.GetCXXStdlibType(Args);
321 }
322 
323 void HIPAMDToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
324                                                 ArgStringList &CC1Args) const {
325   HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
326 }
327 
328 void HIPAMDToolChain::AddClangCXXStdlibIncludeArgs(
329     const ArgList &Args, ArgStringList &CC1Args) const {
330   HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args);
331 }
332 
333 void HIPAMDToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
334                                           ArgStringList &CC1Args) const {
335   HostTC.AddIAMCUIncludeArgs(Args, CC1Args);
336 }
337 
338 void HIPAMDToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs,
339                                         ArgStringList &CC1Args) const {
340   RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args);
341 }
342 
343 SanitizerMask HIPAMDToolChain::getSupportedSanitizers() const {
344   // The HIPAMDToolChain only supports sanitizers in the sense that it allows
345   // sanitizer arguments on the command line if they are supported by the host
346   // toolchain. The HIPAMDToolChain will actually ignore any command line
347   // arguments for any of these "supported" sanitizers. That means that no
348   // sanitization of device code is actually supported at this time.
349   //
350   // This behavior is necessary because the host and device toolchains
351   // invocations often share the command line, so the device toolchain must
352   // tolerate flags meant only for the host toolchain.
353   return HostTC.getSupportedSanitizers();
354 }
355 
356 VersionTuple HIPAMDToolChain::computeMSVCVersion(const Driver *D,
357                                                  const ArgList &Args) const {
358   return HostTC.computeMSVCVersion(D, Args);
359 }
360 
361 llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
362 HIPAMDToolChain::getDeviceLibs(const llvm::opt::ArgList &DriverArgs) const {
363   llvm::SmallVector<BitCodeLibraryInfo, 12> BCLibs;
364   if (DriverArgs.hasArg(options::OPT_nogpulib) ||
365       getGPUArch(DriverArgs) == "amdgcnspirv")
366     return {};
367   ArgStringList LibraryPaths;
368 
369   // Find in --hip-device-lib-path and HIP_LIBRARY_PATH.
370   for (StringRef Path : RocmInstallation->getRocmDeviceLibPathArg())
371     LibraryPaths.push_back(DriverArgs.MakeArgString(Path));
372 
373   addDirectoryList(DriverArgs, LibraryPaths, "", "HIP_DEVICE_LIB_PATH");
374 
375   // Maintain compatability with --hip-device-lib.
376   auto BCLibArgs = DriverArgs.getAllArgValues(options::OPT_hip_device_lib_EQ);
377   if (!BCLibArgs.empty()) {
378     llvm::for_each(BCLibArgs, [&](StringRef BCName) {
379       StringRef FullName;
380       for (StringRef LibraryPath : LibraryPaths) {
381         SmallString<128> Path(LibraryPath);
382         llvm::sys::path::append(Path, BCName);
383         FullName = Path;
384         if (llvm::sys::fs::exists(FullName)) {
385           BCLibs.push_back(FullName);
386           return;
387         }
388       }
389       getDriver().Diag(diag::err_drv_no_such_file) << BCName;
390     });
391   } else {
392     if (!RocmInstallation->hasDeviceLibrary()) {
393       getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 0;
394       return {};
395     }
396     StringRef GpuArch = getGPUArch(DriverArgs);
397     assert(!GpuArch.empty() && "Must have an explicit GPU arch.");
398 
399     // If --hip-device-lib is not set, add the default bitcode libraries.
400     if (DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
401                            options::OPT_fno_gpu_sanitize, true) &&
402         getSanitizerArgs(DriverArgs).needsAsanRt()) {
403       auto AsanRTL = RocmInstallation->getAsanRTLPath();
404       if (AsanRTL.empty()) {
405         unsigned DiagID = getDriver().getDiags().getCustomDiagID(
406             DiagnosticsEngine::Error,
407             "AMDGPU address sanitizer runtime library (asanrtl) is not found. "
408             "Please install ROCm device library which supports address "
409             "sanitizer");
410         getDriver().Diag(DiagID);
411         return {};
412       } else
413         BCLibs.emplace_back(AsanRTL, /*ShouldInternalize=*/false);
414     }
415 
416     // Add the HIP specific bitcode library.
417     BCLibs.push_back(RocmInstallation->getHIPPath());
418 
419     // Add common device libraries like ocml etc.
420     for (StringRef N : getCommonDeviceLibNames(DriverArgs, GpuArch.str()))
421       BCLibs.emplace_back(N);
422 
423     // Add instrument lib.
424     auto InstLib =
425         DriverArgs.getLastArgValue(options::OPT_gpu_instrument_lib_EQ);
426     if (InstLib.empty())
427       return BCLibs;
428     if (llvm::sys::fs::exists(InstLib))
429       BCLibs.push_back(InstLib);
430     else
431       getDriver().Diag(diag::err_drv_no_such_file) << InstLib;
432   }
433 
434   return BCLibs;
435 }
436 
437 void HIPAMDToolChain::checkTargetID(
438     const llvm::opt::ArgList &DriverArgs) const {
439   auto PTID = getParsedTargetID(DriverArgs);
440   if (PTID.OptionalTargetID && !PTID.OptionalGPUArch &&
441       PTID.OptionalTargetID != "amdgcnspirv")
442     getDriver().Diag(clang::diag::err_drv_bad_target_id)
443         << *PTID.OptionalTargetID;
444 }
445