xref: /freebsd-src/contrib/llvm-project/clang/lib/Driver/ToolChains/HIPAMD.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
10eae32dcSDimitry Andric //===--- HIPAMD.cpp - HIP Tool and ToolChain Implementations ----*- C++ -*-===//
20eae32dcSDimitry Andric //
30eae32dcSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40eae32dcSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50eae32dcSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60eae32dcSDimitry Andric //
70eae32dcSDimitry Andric //===----------------------------------------------------------------------===//
80eae32dcSDimitry Andric 
90eae32dcSDimitry Andric #include "HIPAMD.h"
100eae32dcSDimitry Andric #include "AMDGPU.h"
110eae32dcSDimitry Andric #include "CommonArgs.h"
120eae32dcSDimitry Andric #include "HIPUtility.h"
130eae32dcSDimitry Andric #include "clang/Basic/Cuda.h"
140eae32dcSDimitry Andric #include "clang/Basic/TargetID.h"
150eae32dcSDimitry Andric #include "clang/Driver/Compilation.h"
160eae32dcSDimitry Andric #include "clang/Driver/Driver.h"
170eae32dcSDimitry Andric #include "clang/Driver/DriverDiagnostic.h"
180eae32dcSDimitry Andric #include "clang/Driver/InputInfo.h"
190eae32dcSDimitry Andric #include "clang/Driver/Options.h"
200eae32dcSDimitry Andric #include "clang/Driver/SanitizerArgs.h"
210eae32dcSDimitry Andric #include "llvm/Support/Alignment.h"
220eae32dcSDimitry Andric #include "llvm/Support/FileSystem.h"
230eae32dcSDimitry Andric #include "llvm/Support/Path.h"
240eae32dcSDimitry Andric #include "llvm/Support/TargetParser.h"
250eae32dcSDimitry Andric 
260eae32dcSDimitry Andric using namespace clang::driver;
270eae32dcSDimitry Andric using namespace clang::driver::toolchains;
280eae32dcSDimitry Andric using namespace clang::driver::tools;
290eae32dcSDimitry Andric using namespace clang;
300eae32dcSDimitry Andric using namespace llvm::opt;
310eae32dcSDimitry Andric 
320eae32dcSDimitry Andric #if defined(_WIN32) || defined(_WIN64)
330eae32dcSDimitry Andric #define NULL_FILE "nul"
340eae32dcSDimitry Andric #else
350eae32dcSDimitry Andric #define NULL_FILE "/dev/null"
360eae32dcSDimitry Andric #endif
370eae32dcSDimitry Andric 
380eae32dcSDimitry Andric static bool shouldSkipSanitizeOption(const ToolChain &TC,
390eae32dcSDimitry Andric                                      const llvm::opt::ArgList &DriverArgs,
400eae32dcSDimitry Andric                                      StringRef TargetID,
410eae32dcSDimitry Andric                                      const llvm::opt::Arg *A) {
420eae32dcSDimitry Andric   // For actions without targetID, do nothing.
430eae32dcSDimitry Andric   if (TargetID.empty())
440eae32dcSDimitry Andric     return false;
450eae32dcSDimitry Andric   Option O = A->getOption();
460eae32dcSDimitry Andric   if (!O.matches(options::OPT_fsanitize_EQ))
470eae32dcSDimitry Andric     return false;
480eae32dcSDimitry Andric 
490eae32dcSDimitry Andric   if (!DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
5081ad6265SDimitry Andric                           options::OPT_fno_gpu_sanitize, true))
510eae32dcSDimitry Andric     return true;
520eae32dcSDimitry Andric 
530eae32dcSDimitry Andric   auto &Diags = TC.getDriver().getDiags();
540eae32dcSDimitry Andric 
550eae32dcSDimitry Andric   // For simplicity, we only allow -fsanitize=address
560eae32dcSDimitry Andric   SanitizerMask K = parseSanitizerValue(A->getValue(), /*AllowGroups=*/false);
570eae32dcSDimitry Andric   if (K != SanitizerKind::Address)
580eae32dcSDimitry Andric     return true;
590eae32dcSDimitry Andric 
600eae32dcSDimitry Andric   llvm::StringMap<bool> FeatureMap;
610eae32dcSDimitry Andric   auto OptionalGpuArch = parseTargetID(TC.getTriple(), TargetID, &FeatureMap);
620eae32dcSDimitry Andric 
630eae32dcSDimitry Andric   assert(OptionalGpuArch && "Invalid Target ID");
640eae32dcSDimitry Andric   (void)OptionalGpuArch;
650eae32dcSDimitry Andric   auto Loc = FeatureMap.find("xnack");
660eae32dcSDimitry Andric   if (Loc == FeatureMap.end() || !Loc->second) {
670eae32dcSDimitry Andric     Diags.Report(
680eae32dcSDimitry Andric         clang::diag::warn_drv_unsupported_option_for_offload_arch_req_feature)
690eae32dcSDimitry Andric         << A->getAsString(DriverArgs) << TargetID << "xnack+";
700eae32dcSDimitry Andric     return true;
710eae32dcSDimitry Andric   }
720eae32dcSDimitry Andric   return false;
730eae32dcSDimitry Andric }
740eae32dcSDimitry Andric 
7581ad6265SDimitry Andric void AMDGCN::Linker::constructLlvmLinkCommand(Compilation &C,
7681ad6265SDimitry Andric                                          const JobAction &JA,
7781ad6265SDimitry Andric                                          const InputInfoList &Inputs,
7881ad6265SDimitry Andric                                          const InputInfo &Output,
7981ad6265SDimitry Andric                                          const llvm::opt::ArgList &Args) const {
8081ad6265SDimitry Andric   // Construct llvm-link command.
8181ad6265SDimitry Andric   // The output from llvm-link is a bitcode file.
8281ad6265SDimitry Andric   ArgStringList LlvmLinkArgs;
8381ad6265SDimitry Andric 
8481ad6265SDimitry Andric   assert(!Inputs.empty() && "Must have at least one input.");
8581ad6265SDimitry Andric 
8681ad6265SDimitry Andric   LlvmLinkArgs.append({"-o", Output.getFilename()});
8781ad6265SDimitry Andric   for (auto Input : Inputs)
8881ad6265SDimitry Andric     LlvmLinkArgs.push_back(Input.getFilename());
8981ad6265SDimitry Andric 
9081ad6265SDimitry Andric   // Look for archive of bundled bitcode in arguments, and add temporary files
9181ad6265SDimitry Andric   // for the extracted archive of bitcode to inputs.
9281ad6265SDimitry Andric   auto TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ);
9381ad6265SDimitry Andric   AddStaticDeviceLibsLinking(C, *this, JA, Inputs, Args, LlvmLinkArgs, "amdgcn",
9481ad6265SDimitry Andric                              TargetID,
9581ad6265SDimitry Andric                              /*IsBitCodeSDL=*/true,
9681ad6265SDimitry Andric                              /*PostClangLink=*/false);
9781ad6265SDimitry Andric 
9881ad6265SDimitry Andric   const char *LlvmLink =
9981ad6265SDimitry Andric     Args.MakeArgString(getToolChain().GetProgramPath("llvm-link"));
10081ad6265SDimitry Andric   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
10181ad6265SDimitry Andric                                          LlvmLink, LlvmLinkArgs, Inputs,
10281ad6265SDimitry Andric                                          Output));
10381ad6265SDimitry Andric }
10481ad6265SDimitry Andric 
1050eae32dcSDimitry Andric void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA,
1060eae32dcSDimitry Andric                                          const InputInfoList &Inputs,
1070eae32dcSDimitry Andric                                          const InputInfo &Output,
1080eae32dcSDimitry Andric                                          const llvm::opt::ArgList &Args) const {
1090eae32dcSDimitry Andric   // Construct lld command.
1100eae32dcSDimitry Andric   // The output from ld.lld is an HSA code object file.
111*bdd1243dSDimitry Andric   ArgStringList LldArgs{"-flavor",
112*bdd1243dSDimitry Andric                         "gnu",
113*bdd1243dSDimitry Andric                         "-m",
114*bdd1243dSDimitry Andric                         "elf64_amdgpu",
115*bdd1243dSDimitry Andric                         "--no-undefined",
116*bdd1243dSDimitry Andric                         "-shared",
1170eae32dcSDimitry Andric                         "-plugin-opt=-amdgpu-internalize-symbols"};
1180eae32dcSDimitry Andric 
1190eae32dcSDimitry Andric   auto &TC = getToolChain();
1200eae32dcSDimitry Andric   auto &D = TC.getDriver();
1210eae32dcSDimitry Andric   assert(!Inputs.empty() && "Must have at least one input.");
1220eae32dcSDimitry Andric   bool IsThinLTO = D.getLTOMode(/*IsOffload=*/true) == LTOK_Thin;
1230eae32dcSDimitry Andric   addLTOOptions(TC, Args, LldArgs, Output, Inputs[0], IsThinLTO);
1240eae32dcSDimitry Andric 
1250eae32dcSDimitry Andric   // Extract all the -m options
1260eae32dcSDimitry Andric   std::vector<llvm::StringRef> Features;
1270eae32dcSDimitry Andric   amdgpu::getAMDGPUTargetFeatures(D, TC.getTriple(), Args, Features);
1280eae32dcSDimitry Andric 
1290eae32dcSDimitry Andric   // Add features to mattr such as cumode
1300eae32dcSDimitry Andric   std::string MAttrString = "-plugin-opt=-mattr=";
1310eae32dcSDimitry Andric   for (auto OneFeature : unifyTargetFeatures(Features)) {
1320eae32dcSDimitry Andric     MAttrString.append(Args.MakeArgString(OneFeature));
1330eae32dcSDimitry Andric     if (OneFeature != Features.back())
1340eae32dcSDimitry Andric       MAttrString.append(",");
1350eae32dcSDimitry Andric   }
1360eae32dcSDimitry Andric   if (!Features.empty())
1370eae32dcSDimitry Andric     LldArgs.push_back(Args.MakeArgString(MAttrString));
1380eae32dcSDimitry Andric 
1390eae32dcSDimitry Andric   // ToDo: Remove this option after AMDGPU backend supports ISA-level linking.
1400eae32dcSDimitry Andric   // Since AMDGPU backend currently does not support ISA-level linking, all
1410eae32dcSDimitry Andric   // called functions need to be imported.
1420eae32dcSDimitry Andric   if (IsThinLTO)
1430eae32dcSDimitry Andric     LldArgs.push_back(Args.MakeArgString("-plugin-opt=-force-import-all"));
1440eae32dcSDimitry Andric 
1450eae32dcSDimitry Andric   for (const Arg *A : Args.filtered(options::OPT_mllvm)) {
1460eae32dcSDimitry Andric     LldArgs.push_back(
1470eae32dcSDimitry Andric         Args.MakeArgString(Twine("-plugin-opt=") + A->getValue(0)));
1480eae32dcSDimitry Andric   }
1490eae32dcSDimitry Andric 
1500eae32dcSDimitry Andric   if (C.getDriver().isSaveTempsEnabled())
1510eae32dcSDimitry Andric     LldArgs.push_back("-save-temps");
1520eae32dcSDimitry Andric 
1530eae32dcSDimitry Andric   addLinkerCompressDebugSectionsOption(TC, Args, LldArgs);
1540eae32dcSDimitry Andric 
15581ad6265SDimitry Andric   for (auto *Arg : Args.filtered(options::OPT_Xoffload_linker))
15681ad6265SDimitry Andric     LldArgs.push_back(Arg->getValue(1));
15781ad6265SDimitry Andric 
1580eae32dcSDimitry Andric   LldArgs.append({"-o", Output.getFilename()});
1590eae32dcSDimitry Andric   for (auto Input : Inputs)
1600eae32dcSDimitry Andric     LldArgs.push_back(Input.getFilename());
1610eae32dcSDimitry Andric 
16281ad6265SDimitry Andric   // Look for archive of bundled bitcode in arguments, and add temporary files
16381ad6265SDimitry Andric   // for the extracted archive of bitcode to inputs.
16481ad6265SDimitry Andric   auto TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ);
16581ad6265SDimitry Andric   AddStaticDeviceLibsLinking(C, *this, JA, Inputs, Args, LldArgs, "amdgcn",
16681ad6265SDimitry Andric                              TargetID,
16781ad6265SDimitry Andric                              /*IsBitCodeSDL=*/true,
16881ad6265SDimitry Andric                              /*PostClangLink=*/false);
16981ad6265SDimitry Andric 
1700eae32dcSDimitry Andric   const char *Lld = Args.MakeArgString(getToolChain().GetProgramPath("lld"));
1710eae32dcSDimitry Andric   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
1720eae32dcSDimitry Andric                                          Lld, LldArgs, Inputs, Output));
1730eae32dcSDimitry Andric }
1740eae32dcSDimitry Andric 
1750eae32dcSDimitry Andric // For amdgcn the inputs of the linker job are device bitcode and output is
17681ad6265SDimitry Andric // either an object file or bitcode (-emit-llvm). It calls llvm-link, opt,
17781ad6265SDimitry Andric // llc, then lld steps.
1780eae32dcSDimitry Andric void AMDGCN::Linker::ConstructJob(Compilation &C, const JobAction &JA,
1790eae32dcSDimitry Andric                                   const InputInfo &Output,
1800eae32dcSDimitry Andric                                   const InputInfoList &Inputs,
1810eae32dcSDimitry Andric                                   const ArgList &Args,
1820eae32dcSDimitry Andric                                   const char *LinkingOutput) const {
1830eae32dcSDimitry Andric   if (Inputs.size() > 0 &&
1840eae32dcSDimitry Andric       Inputs[0].getType() == types::TY_Image &&
1850eae32dcSDimitry Andric       JA.getType() == types::TY_Object)
1860eae32dcSDimitry Andric     return HIP::constructGenerateObjFileFromHIPFatBinary(C, Output, Inputs,
1870eae32dcSDimitry Andric                                                          Args, JA, *this);
1880eae32dcSDimitry Andric 
1890eae32dcSDimitry Andric   if (JA.getType() == types::TY_HIP_FATBIN)
1900eae32dcSDimitry Andric     return HIP::constructHIPFatbinCommand(C, JA, Output.getFilename(), Inputs,
1910eae32dcSDimitry Andric                                           Args, *this);
1920eae32dcSDimitry Andric 
19381ad6265SDimitry Andric   if (JA.getType() == types::TY_LLVM_BC)
19481ad6265SDimitry Andric     return constructLlvmLinkCommand(C, JA, Inputs, Output, Args);
19581ad6265SDimitry Andric 
1960eae32dcSDimitry Andric   return constructLldCommand(C, JA, Inputs, Output, Args);
1970eae32dcSDimitry Andric }
1980eae32dcSDimitry Andric 
1990eae32dcSDimitry Andric HIPAMDToolChain::HIPAMDToolChain(const Driver &D, const llvm::Triple &Triple,
2000eae32dcSDimitry Andric                                  const ToolChain &HostTC, const ArgList &Args)
2010eae32dcSDimitry Andric     : ROCMToolChain(D, Triple, Args), HostTC(HostTC) {
2020eae32dcSDimitry Andric   // Lookup binaries into the driver directory, this is used to
2030eae32dcSDimitry Andric   // discover the clang-offload-bundler executable.
2040eae32dcSDimitry Andric   getProgramPaths().push_back(getDriver().Dir);
2050eae32dcSDimitry Andric 
2060eae32dcSDimitry Andric   // Diagnose unsupported sanitizer options only once.
20781ad6265SDimitry Andric   if (!Args.hasFlag(options::OPT_fgpu_sanitize, options::OPT_fno_gpu_sanitize,
20881ad6265SDimitry Andric                     true))
20981ad6265SDimitry Andric     return;
210*bdd1243dSDimitry Andric   for (auto *A : Args.filtered(options::OPT_fsanitize_EQ)) {
2110eae32dcSDimitry Andric     SanitizerMask K = parseSanitizerValue(A->getValue(), /*AllowGroups=*/false);
2120eae32dcSDimitry Andric     if (K != SanitizerKind::Address)
2130eae32dcSDimitry Andric       D.getDiags().Report(clang::diag::warn_drv_unsupported_option_for_target)
2140eae32dcSDimitry Andric           << A->getAsString(Args) << getTriple().str();
2150eae32dcSDimitry Andric   }
2160eae32dcSDimitry Andric }
2170eae32dcSDimitry Andric 
2180eae32dcSDimitry Andric void HIPAMDToolChain::addClangTargetOptions(
2190eae32dcSDimitry Andric     const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
2200eae32dcSDimitry Andric     Action::OffloadKind DeviceOffloadingKind) const {
2210eae32dcSDimitry Andric   HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
2220eae32dcSDimitry Andric 
2230eae32dcSDimitry Andric   assert(DeviceOffloadingKind == Action::OFK_HIP &&
2240eae32dcSDimitry Andric          "Only HIP offloading kinds are supported for GPUs.");
2250eae32dcSDimitry Andric 
2260eae32dcSDimitry Andric   CC1Args.push_back("-fcuda-is-device");
2270eae32dcSDimitry Andric 
2280eae32dcSDimitry Andric   if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals,
2290eae32dcSDimitry Andric                          options::OPT_fno_cuda_approx_transcendentals, false))
2300eae32dcSDimitry Andric     CC1Args.push_back("-fcuda-approx-transcendentals");
2310eae32dcSDimitry Andric 
2320eae32dcSDimitry Andric   if (!DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
2330eae32dcSDimitry Andric                           false))
2340eae32dcSDimitry Andric     CC1Args.append({"-mllvm", "-amdgpu-internalize-symbols"});
2350eae32dcSDimitry Andric 
2360eae32dcSDimitry Andric   StringRef MaxThreadsPerBlock =
2370eae32dcSDimitry Andric       DriverArgs.getLastArgValue(options::OPT_gpu_max_threads_per_block_EQ);
2380eae32dcSDimitry Andric   if (!MaxThreadsPerBlock.empty()) {
2390eae32dcSDimitry Andric     std::string ArgStr =
240*bdd1243dSDimitry Andric         (Twine("--gpu-max-threads-per-block=") + MaxThreadsPerBlock).str();
2410eae32dcSDimitry Andric     CC1Args.push_back(DriverArgs.MakeArgStringRef(ArgStr));
2420eae32dcSDimitry Andric   }
2430eae32dcSDimitry Andric 
2440eae32dcSDimitry Andric   CC1Args.push_back("-fcuda-allow-variadic-functions");
2450eae32dcSDimitry Andric 
2460eae32dcSDimitry Andric   // Default to "hidden" visibility, as object level linking will not be
2470eae32dcSDimitry Andric   // supported for the foreseeable future.
2480eae32dcSDimitry Andric   if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
2490eae32dcSDimitry Andric                          options::OPT_fvisibility_ms_compat)) {
250*bdd1243dSDimitry Andric     CC1Args.append({"-fvisibility=hidden"});
2510eae32dcSDimitry Andric     CC1Args.push_back("-fapply-global-visibility-to-externs");
2520eae32dcSDimitry Andric   }
2530eae32dcSDimitry Andric 
254*bdd1243dSDimitry Andric   for (auto BCFile : getDeviceLibs(DriverArgs)) {
2550eae32dcSDimitry Andric     CC1Args.push_back(BCFile.ShouldInternalize ? "-mlink-builtin-bitcode"
2560eae32dcSDimitry Andric                                                : "-mlink-bitcode-file");
2570eae32dcSDimitry Andric     CC1Args.push_back(DriverArgs.MakeArgString(BCFile.Path));
25881ad6265SDimitry Andric   }
2590eae32dcSDimitry Andric }
2600eae32dcSDimitry Andric 
2610eae32dcSDimitry Andric llvm::opt::DerivedArgList *
2620eae32dcSDimitry Andric HIPAMDToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
2630eae32dcSDimitry Andric                                StringRef BoundArch,
2640eae32dcSDimitry Andric                                Action::OffloadKind DeviceOffloadKind) const {
2650eae32dcSDimitry Andric   DerivedArgList *DAL =
2660eae32dcSDimitry Andric       HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind);
2670eae32dcSDimitry Andric   if (!DAL)
2680eae32dcSDimitry Andric     DAL = new DerivedArgList(Args.getBaseArgs());
2690eae32dcSDimitry Andric 
2700eae32dcSDimitry Andric   const OptTable &Opts = getDriver().getOpts();
2710eae32dcSDimitry Andric 
2720eae32dcSDimitry Andric   for (Arg *A : Args) {
2730eae32dcSDimitry Andric     if (!shouldSkipArgument(A) &&
2740eae32dcSDimitry Andric         !shouldSkipSanitizeOption(*this, Args, BoundArch, A))
2750eae32dcSDimitry Andric       DAL->append(A);
2760eae32dcSDimitry Andric   }
2770eae32dcSDimitry Andric 
2780eae32dcSDimitry Andric   if (!BoundArch.empty()) {
2790eae32dcSDimitry Andric     DAL->eraseArg(options::OPT_mcpu_EQ);
2800eae32dcSDimitry Andric     DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_mcpu_EQ), BoundArch);
2810eae32dcSDimitry Andric     checkTargetID(*DAL);
2820eae32dcSDimitry Andric   }
2830eae32dcSDimitry Andric 
2840eae32dcSDimitry Andric   return DAL;
2850eae32dcSDimitry Andric }
2860eae32dcSDimitry Andric 
2870eae32dcSDimitry Andric Tool *HIPAMDToolChain::buildLinker() const {
2880eae32dcSDimitry Andric   assert(getTriple().getArch() == llvm::Triple::amdgcn);
2890eae32dcSDimitry Andric   return new tools::AMDGCN::Linker(*this);
2900eae32dcSDimitry Andric }
2910eae32dcSDimitry Andric 
2920eae32dcSDimitry Andric void HIPAMDToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
2930eae32dcSDimitry Andric   HostTC.addClangWarningOptions(CC1Args);
2940eae32dcSDimitry Andric }
2950eae32dcSDimitry Andric 
2960eae32dcSDimitry Andric ToolChain::CXXStdlibType
2970eae32dcSDimitry Andric HIPAMDToolChain::GetCXXStdlibType(const ArgList &Args) const {
2980eae32dcSDimitry Andric   return HostTC.GetCXXStdlibType(Args);
2990eae32dcSDimitry Andric }
3000eae32dcSDimitry Andric 
3010eae32dcSDimitry Andric void HIPAMDToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
3020eae32dcSDimitry Andric                                                 ArgStringList &CC1Args) const {
3030eae32dcSDimitry Andric   HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
3040eae32dcSDimitry Andric }
3050eae32dcSDimitry Andric 
3060eae32dcSDimitry Andric void HIPAMDToolChain::AddClangCXXStdlibIncludeArgs(
3070eae32dcSDimitry Andric     const ArgList &Args, ArgStringList &CC1Args) const {
3080eae32dcSDimitry Andric   HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args);
3090eae32dcSDimitry Andric }
3100eae32dcSDimitry Andric 
3110eae32dcSDimitry Andric void HIPAMDToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
3120eae32dcSDimitry Andric                                           ArgStringList &CC1Args) const {
3130eae32dcSDimitry Andric   HostTC.AddIAMCUIncludeArgs(Args, CC1Args);
3140eae32dcSDimitry Andric }
3150eae32dcSDimitry Andric 
3160eae32dcSDimitry Andric void HIPAMDToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs,
3170eae32dcSDimitry Andric                                         ArgStringList &CC1Args) const {
3180eae32dcSDimitry Andric   RocmInstallation.AddHIPIncludeArgs(DriverArgs, CC1Args);
3190eae32dcSDimitry Andric }
3200eae32dcSDimitry Andric 
3210eae32dcSDimitry Andric SanitizerMask HIPAMDToolChain::getSupportedSanitizers() const {
3220eae32dcSDimitry Andric   // The HIPAMDToolChain only supports sanitizers in the sense that it allows
3230eae32dcSDimitry Andric   // sanitizer arguments on the command line if they are supported by the host
3240eae32dcSDimitry Andric   // toolchain. The HIPAMDToolChain will actually ignore any command line
3250eae32dcSDimitry Andric   // arguments for any of these "supported" sanitizers. That means that no
3260eae32dcSDimitry Andric   // sanitization of device code is actually supported at this time.
3270eae32dcSDimitry Andric   //
3280eae32dcSDimitry Andric   // This behavior is necessary because the host and device toolchains
3290eae32dcSDimitry Andric   // invocations often share the command line, so the device toolchain must
3300eae32dcSDimitry Andric   // tolerate flags meant only for the host toolchain.
3310eae32dcSDimitry Andric   return HostTC.getSupportedSanitizers();
3320eae32dcSDimitry Andric }
3330eae32dcSDimitry Andric 
3340eae32dcSDimitry Andric VersionTuple HIPAMDToolChain::computeMSVCVersion(const Driver *D,
3350eae32dcSDimitry Andric                                                  const ArgList &Args) const {
3360eae32dcSDimitry Andric   return HostTC.computeMSVCVersion(D, Args);
3370eae32dcSDimitry Andric }
3380eae32dcSDimitry Andric 
3390eae32dcSDimitry Andric llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
340*bdd1243dSDimitry Andric HIPAMDToolChain::getDeviceLibs(const llvm::opt::ArgList &DriverArgs) const {
3410eae32dcSDimitry Andric   llvm::SmallVector<BitCodeLibraryInfo, 12> BCLibs;
3420eae32dcSDimitry Andric   if (DriverArgs.hasArg(options::OPT_nogpulib))
3430eae32dcSDimitry Andric     return {};
3440eae32dcSDimitry Andric   ArgStringList LibraryPaths;
3450eae32dcSDimitry Andric 
3460eae32dcSDimitry Andric   // Find in --hip-device-lib-path and HIP_LIBRARY_PATH.
347*bdd1243dSDimitry Andric   for (StringRef Path : RocmInstallation.getRocmDeviceLibPathArg())
3480eae32dcSDimitry Andric     LibraryPaths.push_back(DriverArgs.MakeArgString(Path));
3490eae32dcSDimitry Andric 
3500eae32dcSDimitry Andric   addDirectoryList(DriverArgs, LibraryPaths, "", "HIP_DEVICE_LIB_PATH");
3510eae32dcSDimitry Andric 
3520eae32dcSDimitry Andric   // Maintain compatability with --hip-device-lib.
3530eae32dcSDimitry Andric   auto BCLibArgs = DriverArgs.getAllArgValues(options::OPT_hip_device_lib_EQ);
3540eae32dcSDimitry Andric   if (!BCLibArgs.empty()) {
3550eae32dcSDimitry Andric     llvm::for_each(BCLibArgs, [&](StringRef BCName) {
3560eae32dcSDimitry Andric       StringRef FullName;
357*bdd1243dSDimitry Andric       for (StringRef LibraryPath : LibraryPaths) {
3580eae32dcSDimitry Andric         SmallString<128> Path(LibraryPath);
3590eae32dcSDimitry Andric         llvm::sys::path::append(Path, BCName);
3600eae32dcSDimitry Andric         FullName = Path;
3610eae32dcSDimitry Andric         if (llvm::sys::fs::exists(FullName)) {
3620eae32dcSDimitry Andric           BCLibs.push_back(FullName);
3630eae32dcSDimitry Andric           return;
3640eae32dcSDimitry Andric         }
3650eae32dcSDimitry Andric       }
3660eae32dcSDimitry Andric       getDriver().Diag(diag::err_drv_no_such_file) << BCName;
3670eae32dcSDimitry Andric     });
3680eae32dcSDimitry Andric   } else {
3690eae32dcSDimitry Andric     if (!RocmInstallation.hasDeviceLibrary()) {
3700eae32dcSDimitry Andric       getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 0;
3710eae32dcSDimitry Andric       return {};
3720eae32dcSDimitry Andric     }
3730eae32dcSDimitry Andric     StringRef GpuArch = getGPUArch(DriverArgs);
3740eae32dcSDimitry Andric     assert(!GpuArch.empty() && "Must have an explicit GPU arch.");
3750eae32dcSDimitry Andric 
3760eae32dcSDimitry Andric     // If --hip-device-lib is not set, add the default bitcode libraries.
3770eae32dcSDimitry Andric     if (DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
37881ad6265SDimitry Andric                            options::OPT_fno_gpu_sanitize, true) &&
3790eae32dcSDimitry Andric         getSanitizerArgs(DriverArgs).needsAsanRt()) {
3800eae32dcSDimitry Andric       auto AsanRTL = RocmInstallation.getAsanRTLPath();
3810eae32dcSDimitry Andric       if (AsanRTL.empty()) {
3820eae32dcSDimitry Andric         unsigned DiagID = getDriver().getDiags().getCustomDiagID(
3830eae32dcSDimitry Andric             DiagnosticsEngine::Error,
3840eae32dcSDimitry Andric             "AMDGPU address sanitizer runtime library (asanrtl) is not found. "
3850eae32dcSDimitry Andric             "Please install ROCm device library which supports address "
3860eae32dcSDimitry Andric             "sanitizer");
3870eae32dcSDimitry Andric         getDriver().Diag(DiagID);
3880eae32dcSDimitry Andric         return {};
3890eae32dcSDimitry Andric       } else
390*bdd1243dSDimitry Andric         BCLibs.emplace_back(AsanRTL, /*ShouldInternalize=*/false);
3910eae32dcSDimitry Andric     }
3920eae32dcSDimitry Andric 
3930eae32dcSDimitry Andric     // Add the HIP specific bitcode library.
3940eae32dcSDimitry Andric     BCLibs.push_back(RocmInstallation.getHIPPath());
3950eae32dcSDimitry Andric 
3960eae32dcSDimitry Andric     // Add common device libraries like ocml etc.
397*bdd1243dSDimitry Andric     for (StringRef N : getCommonDeviceLibNames(DriverArgs, GpuArch.str()))
398*bdd1243dSDimitry Andric       BCLibs.emplace_back(N);
3990eae32dcSDimitry Andric 
4000eae32dcSDimitry Andric     // Add instrument lib.
4010eae32dcSDimitry Andric     auto InstLib =
4020eae32dcSDimitry Andric         DriverArgs.getLastArgValue(options::OPT_gpu_instrument_lib_EQ);
4030eae32dcSDimitry Andric     if (InstLib.empty())
4040eae32dcSDimitry Andric       return BCLibs;
4050eae32dcSDimitry Andric     if (llvm::sys::fs::exists(InstLib))
4060eae32dcSDimitry Andric       BCLibs.push_back(InstLib);
4070eae32dcSDimitry Andric     else
4080eae32dcSDimitry Andric       getDriver().Diag(diag::err_drv_no_such_file) << InstLib;
4090eae32dcSDimitry Andric   }
4100eae32dcSDimitry Andric 
4110eae32dcSDimitry Andric   return BCLibs;
4120eae32dcSDimitry Andric }
4130eae32dcSDimitry Andric 
4140eae32dcSDimitry Andric void HIPAMDToolChain::checkTargetID(
4150eae32dcSDimitry Andric     const llvm::opt::ArgList &DriverArgs) const {
4160eae32dcSDimitry Andric   auto PTID = getParsedTargetID(DriverArgs);
4170eae32dcSDimitry Andric   if (PTID.OptionalTargetID && !PTID.OptionalGPUArch) {
4180eae32dcSDimitry Andric     getDriver().Diag(clang::diag::err_drv_bad_target_id)
41981ad6265SDimitry Andric         << *PTID.OptionalTargetID;
4200eae32dcSDimitry Andric   }
4210eae32dcSDimitry Andric }
422