xref: /freebsd-src/contrib/llvm-project/clang/lib/Driver/ToolChains/HIPAMD.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
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"
24*06c3fb27SDimitry Andric #include "llvm/TargetParser/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.
111bdd1243dSDimitry Andric   ArgStringList LldArgs{"-flavor",
112bdd1243dSDimitry Andric                         "gnu",
113bdd1243dSDimitry Andric                         "-m",
114bdd1243dSDimitry Andric                         "elf64_amdgpu",
115bdd1243dSDimitry Andric                         "--no-undefined",
116bdd1243dSDimitry 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   if (C.getDriver().isSaveTempsEnabled())
1460eae32dcSDimitry Andric     LldArgs.push_back("-save-temps");
1470eae32dcSDimitry Andric 
1480eae32dcSDimitry Andric   addLinkerCompressDebugSectionsOption(TC, Args, LldArgs);
1490eae32dcSDimitry Andric 
150*06c3fb27SDimitry Andric   // Given that host and device linking happen in separate processes, the device
151*06c3fb27SDimitry Andric   // linker doesn't always have the visibility as to which device symbols are
152*06c3fb27SDimitry Andric   // needed by a program, especially for the device symbol dependencies that are
153*06c3fb27SDimitry Andric   // introduced through the host symbol resolution.
154*06c3fb27SDimitry Andric   // For example: host_A() (A.obj) --> host_B(B.obj) --> device_kernel_B()
155*06c3fb27SDimitry Andric   // (B.obj) In this case, the device linker doesn't know that A.obj actually
156*06c3fb27SDimitry Andric   // depends on the kernel functions in B.obj.  When linking to static device
157*06c3fb27SDimitry Andric   // library, the device linker may drop some of the device global symbols if
158*06c3fb27SDimitry Andric   // they aren't referenced.  As a workaround, we are adding to the
159*06c3fb27SDimitry Andric   // --whole-archive flag such that all global symbols would be linked in.
160*06c3fb27SDimitry Andric   LldArgs.push_back("--whole-archive");
161*06c3fb27SDimitry Andric 
162*06c3fb27SDimitry Andric   for (auto *Arg : Args.filtered(options::OPT_Xoffload_linker)) {
163*06c3fb27SDimitry Andric     StringRef ArgVal = Arg->getValue(1);
164*06c3fb27SDimitry Andric     auto SplitArg = ArgVal.split("-mllvm=");
165*06c3fb27SDimitry Andric     if (!SplitArg.second.empty()) {
166*06c3fb27SDimitry Andric       LldArgs.push_back(
167*06c3fb27SDimitry Andric           Args.MakeArgString(Twine("-plugin-opt=") + SplitArg.second));
168*06c3fb27SDimitry Andric     } else {
169*06c3fb27SDimitry Andric       LldArgs.push_back(Args.MakeArgString(ArgVal));
170*06c3fb27SDimitry Andric     }
171*06c3fb27SDimitry Andric     Arg->claim();
172*06c3fb27SDimitry Andric   }
17381ad6265SDimitry Andric 
1740eae32dcSDimitry Andric   LldArgs.append({"-o", Output.getFilename()});
1750eae32dcSDimitry Andric   for (auto Input : Inputs)
1760eae32dcSDimitry Andric     LldArgs.push_back(Input.getFilename());
1770eae32dcSDimitry Andric 
17881ad6265SDimitry Andric   // Look for archive of bundled bitcode in arguments, and add temporary files
17981ad6265SDimitry Andric   // for the extracted archive of bitcode to inputs.
18081ad6265SDimitry Andric   auto TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ);
18181ad6265SDimitry Andric   AddStaticDeviceLibsLinking(C, *this, JA, Inputs, Args, LldArgs, "amdgcn",
18281ad6265SDimitry Andric                              TargetID,
18381ad6265SDimitry Andric                              /*IsBitCodeSDL=*/true,
18481ad6265SDimitry Andric                              /*PostClangLink=*/false);
18581ad6265SDimitry Andric 
186*06c3fb27SDimitry Andric   LldArgs.push_back("--no-whole-archive");
187*06c3fb27SDimitry Andric 
1880eae32dcSDimitry Andric   const char *Lld = Args.MakeArgString(getToolChain().GetProgramPath("lld"));
1890eae32dcSDimitry Andric   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
1900eae32dcSDimitry Andric                                          Lld, LldArgs, Inputs, Output));
1910eae32dcSDimitry Andric }
1920eae32dcSDimitry Andric 
1930eae32dcSDimitry Andric // For amdgcn the inputs of the linker job are device bitcode and output is
19481ad6265SDimitry Andric // either an object file or bitcode (-emit-llvm). It calls llvm-link, opt,
19581ad6265SDimitry Andric // llc, then lld steps.
1960eae32dcSDimitry Andric void AMDGCN::Linker::ConstructJob(Compilation &C, const JobAction &JA,
1970eae32dcSDimitry Andric                                   const InputInfo &Output,
1980eae32dcSDimitry Andric                                   const InputInfoList &Inputs,
1990eae32dcSDimitry Andric                                   const ArgList &Args,
2000eae32dcSDimitry Andric                                   const char *LinkingOutput) const {
2010eae32dcSDimitry Andric   if (Inputs.size() > 0 &&
2020eae32dcSDimitry Andric       Inputs[0].getType() == types::TY_Image &&
2030eae32dcSDimitry Andric       JA.getType() == types::TY_Object)
2040eae32dcSDimitry Andric     return HIP::constructGenerateObjFileFromHIPFatBinary(C, Output, Inputs,
2050eae32dcSDimitry Andric                                                          Args, JA, *this);
2060eae32dcSDimitry Andric 
2070eae32dcSDimitry Andric   if (JA.getType() == types::TY_HIP_FATBIN)
2080eae32dcSDimitry Andric     return HIP::constructHIPFatbinCommand(C, JA, Output.getFilename(), Inputs,
2090eae32dcSDimitry Andric                                           Args, *this);
2100eae32dcSDimitry Andric 
21181ad6265SDimitry Andric   if (JA.getType() == types::TY_LLVM_BC)
21281ad6265SDimitry Andric     return constructLlvmLinkCommand(C, JA, Inputs, Output, Args);
21381ad6265SDimitry Andric 
2140eae32dcSDimitry Andric   return constructLldCommand(C, JA, Inputs, Output, Args);
2150eae32dcSDimitry Andric }
2160eae32dcSDimitry Andric 
2170eae32dcSDimitry Andric HIPAMDToolChain::HIPAMDToolChain(const Driver &D, const llvm::Triple &Triple,
2180eae32dcSDimitry Andric                                  const ToolChain &HostTC, const ArgList &Args)
2190eae32dcSDimitry Andric     : ROCMToolChain(D, Triple, Args), HostTC(HostTC) {
2200eae32dcSDimitry Andric   // Lookup binaries into the driver directory, this is used to
2210eae32dcSDimitry Andric   // discover the clang-offload-bundler executable.
2220eae32dcSDimitry Andric   getProgramPaths().push_back(getDriver().Dir);
2230eae32dcSDimitry Andric 
2240eae32dcSDimitry Andric   // Diagnose unsupported sanitizer options only once.
22581ad6265SDimitry Andric   if (!Args.hasFlag(options::OPT_fgpu_sanitize, options::OPT_fno_gpu_sanitize,
22681ad6265SDimitry Andric                     true))
22781ad6265SDimitry Andric     return;
228bdd1243dSDimitry Andric   for (auto *A : Args.filtered(options::OPT_fsanitize_EQ)) {
2290eae32dcSDimitry Andric     SanitizerMask K = parseSanitizerValue(A->getValue(), /*AllowGroups=*/false);
2300eae32dcSDimitry Andric     if (K != SanitizerKind::Address)
2310eae32dcSDimitry Andric       D.getDiags().Report(clang::diag::warn_drv_unsupported_option_for_target)
2320eae32dcSDimitry Andric           << A->getAsString(Args) << getTriple().str();
2330eae32dcSDimitry Andric   }
2340eae32dcSDimitry Andric }
2350eae32dcSDimitry Andric 
2360eae32dcSDimitry Andric void HIPAMDToolChain::addClangTargetOptions(
2370eae32dcSDimitry Andric     const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
2380eae32dcSDimitry Andric     Action::OffloadKind DeviceOffloadingKind) const {
2390eae32dcSDimitry Andric   HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
2400eae32dcSDimitry Andric 
2410eae32dcSDimitry Andric   assert(DeviceOffloadingKind == Action::OFK_HIP &&
2420eae32dcSDimitry Andric          "Only HIP offloading kinds are supported for GPUs.");
2430eae32dcSDimitry Andric 
2440eae32dcSDimitry Andric   CC1Args.push_back("-fcuda-is-device");
2450eae32dcSDimitry Andric 
2460eae32dcSDimitry Andric   if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals,
2470eae32dcSDimitry Andric                          options::OPT_fno_cuda_approx_transcendentals, false))
2480eae32dcSDimitry Andric     CC1Args.push_back("-fcuda-approx-transcendentals");
2490eae32dcSDimitry Andric 
2500eae32dcSDimitry Andric   if (!DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
2510eae32dcSDimitry Andric                           false))
2520eae32dcSDimitry Andric     CC1Args.append({"-mllvm", "-amdgpu-internalize-symbols"});
2530eae32dcSDimitry Andric 
2540eae32dcSDimitry Andric   StringRef MaxThreadsPerBlock =
2550eae32dcSDimitry Andric       DriverArgs.getLastArgValue(options::OPT_gpu_max_threads_per_block_EQ);
2560eae32dcSDimitry Andric   if (!MaxThreadsPerBlock.empty()) {
2570eae32dcSDimitry Andric     std::string ArgStr =
258bdd1243dSDimitry Andric         (Twine("--gpu-max-threads-per-block=") + MaxThreadsPerBlock).str();
2590eae32dcSDimitry Andric     CC1Args.push_back(DriverArgs.MakeArgStringRef(ArgStr));
2600eae32dcSDimitry Andric   }
2610eae32dcSDimitry Andric 
2620eae32dcSDimitry Andric   CC1Args.push_back("-fcuda-allow-variadic-functions");
2630eae32dcSDimitry Andric 
2640eae32dcSDimitry Andric   // Default to "hidden" visibility, as object level linking will not be
2650eae32dcSDimitry Andric   // supported for the foreseeable future.
2660eae32dcSDimitry Andric   if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
2670eae32dcSDimitry Andric                          options::OPT_fvisibility_ms_compat)) {
268bdd1243dSDimitry Andric     CC1Args.append({"-fvisibility=hidden"});
2690eae32dcSDimitry Andric     CC1Args.push_back("-fapply-global-visibility-to-externs");
2700eae32dcSDimitry Andric   }
2710eae32dcSDimitry Andric 
272bdd1243dSDimitry Andric   for (auto BCFile : getDeviceLibs(DriverArgs)) {
2730eae32dcSDimitry Andric     CC1Args.push_back(BCFile.ShouldInternalize ? "-mlink-builtin-bitcode"
2740eae32dcSDimitry Andric                                                : "-mlink-bitcode-file");
2750eae32dcSDimitry Andric     CC1Args.push_back(DriverArgs.MakeArgString(BCFile.Path));
27681ad6265SDimitry Andric   }
2770eae32dcSDimitry Andric }
2780eae32dcSDimitry Andric 
2790eae32dcSDimitry Andric llvm::opt::DerivedArgList *
2800eae32dcSDimitry Andric HIPAMDToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
2810eae32dcSDimitry Andric                                StringRef BoundArch,
2820eae32dcSDimitry Andric                                Action::OffloadKind DeviceOffloadKind) const {
2830eae32dcSDimitry Andric   DerivedArgList *DAL =
2840eae32dcSDimitry Andric       HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind);
2850eae32dcSDimitry Andric   if (!DAL)
2860eae32dcSDimitry Andric     DAL = new DerivedArgList(Args.getBaseArgs());
2870eae32dcSDimitry Andric 
2880eae32dcSDimitry Andric   const OptTable &Opts = getDriver().getOpts();
2890eae32dcSDimitry Andric 
2900eae32dcSDimitry Andric   for (Arg *A : Args) {
291*06c3fb27SDimitry Andric     if (!shouldSkipSanitizeOption(*this, Args, BoundArch, A))
2920eae32dcSDimitry Andric       DAL->append(A);
2930eae32dcSDimitry Andric   }
2940eae32dcSDimitry Andric 
2950eae32dcSDimitry Andric   if (!BoundArch.empty()) {
2960eae32dcSDimitry Andric     DAL->eraseArg(options::OPT_mcpu_EQ);
2970eae32dcSDimitry Andric     DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_mcpu_EQ), BoundArch);
2980eae32dcSDimitry Andric     checkTargetID(*DAL);
2990eae32dcSDimitry Andric   }
3000eae32dcSDimitry Andric 
3010eae32dcSDimitry Andric   return DAL;
3020eae32dcSDimitry Andric }
3030eae32dcSDimitry Andric 
3040eae32dcSDimitry Andric Tool *HIPAMDToolChain::buildLinker() const {
3050eae32dcSDimitry Andric   assert(getTriple().getArch() == llvm::Triple::amdgcn);
3060eae32dcSDimitry Andric   return new tools::AMDGCN::Linker(*this);
3070eae32dcSDimitry Andric }
3080eae32dcSDimitry Andric 
3090eae32dcSDimitry Andric void HIPAMDToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
3100eae32dcSDimitry Andric   HostTC.addClangWarningOptions(CC1Args);
3110eae32dcSDimitry Andric }
3120eae32dcSDimitry Andric 
3130eae32dcSDimitry Andric ToolChain::CXXStdlibType
3140eae32dcSDimitry Andric HIPAMDToolChain::GetCXXStdlibType(const ArgList &Args) const {
3150eae32dcSDimitry Andric   return HostTC.GetCXXStdlibType(Args);
3160eae32dcSDimitry Andric }
3170eae32dcSDimitry Andric 
3180eae32dcSDimitry Andric void HIPAMDToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
3190eae32dcSDimitry Andric                                                 ArgStringList &CC1Args) const {
3200eae32dcSDimitry Andric   HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
3210eae32dcSDimitry Andric }
3220eae32dcSDimitry Andric 
3230eae32dcSDimitry Andric void HIPAMDToolChain::AddClangCXXStdlibIncludeArgs(
3240eae32dcSDimitry Andric     const ArgList &Args, ArgStringList &CC1Args) const {
3250eae32dcSDimitry Andric   HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args);
3260eae32dcSDimitry Andric }
3270eae32dcSDimitry Andric 
3280eae32dcSDimitry Andric void HIPAMDToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
3290eae32dcSDimitry Andric                                           ArgStringList &CC1Args) const {
3300eae32dcSDimitry Andric   HostTC.AddIAMCUIncludeArgs(Args, CC1Args);
3310eae32dcSDimitry Andric }
3320eae32dcSDimitry Andric 
3330eae32dcSDimitry Andric void HIPAMDToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs,
3340eae32dcSDimitry Andric                                         ArgStringList &CC1Args) const {
335*06c3fb27SDimitry Andric   RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args);
3360eae32dcSDimitry Andric }
3370eae32dcSDimitry Andric 
3380eae32dcSDimitry Andric SanitizerMask HIPAMDToolChain::getSupportedSanitizers() const {
3390eae32dcSDimitry Andric   // The HIPAMDToolChain only supports sanitizers in the sense that it allows
3400eae32dcSDimitry Andric   // sanitizer arguments on the command line if they are supported by the host
3410eae32dcSDimitry Andric   // toolchain. The HIPAMDToolChain will actually ignore any command line
3420eae32dcSDimitry Andric   // arguments for any of these "supported" sanitizers. That means that no
3430eae32dcSDimitry Andric   // sanitization of device code is actually supported at this time.
3440eae32dcSDimitry Andric   //
3450eae32dcSDimitry Andric   // This behavior is necessary because the host and device toolchains
3460eae32dcSDimitry Andric   // invocations often share the command line, so the device toolchain must
3470eae32dcSDimitry Andric   // tolerate flags meant only for the host toolchain.
3480eae32dcSDimitry Andric   return HostTC.getSupportedSanitizers();
3490eae32dcSDimitry Andric }
3500eae32dcSDimitry Andric 
3510eae32dcSDimitry Andric VersionTuple HIPAMDToolChain::computeMSVCVersion(const Driver *D,
3520eae32dcSDimitry Andric                                                  const ArgList &Args) const {
3530eae32dcSDimitry Andric   return HostTC.computeMSVCVersion(D, Args);
3540eae32dcSDimitry Andric }
3550eae32dcSDimitry Andric 
3560eae32dcSDimitry Andric llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
357bdd1243dSDimitry Andric HIPAMDToolChain::getDeviceLibs(const llvm::opt::ArgList &DriverArgs) const {
3580eae32dcSDimitry Andric   llvm::SmallVector<BitCodeLibraryInfo, 12> BCLibs;
3590eae32dcSDimitry Andric   if (DriverArgs.hasArg(options::OPT_nogpulib))
3600eae32dcSDimitry Andric     return {};
3610eae32dcSDimitry Andric   ArgStringList LibraryPaths;
3620eae32dcSDimitry Andric 
3630eae32dcSDimitry Andric   // Find in --hip-device-lib-path and HIP_LIBRARY_PATH.
364*06c3fb27SDimitry Andric   for (StringRef Path : RocmInstallation->getRocmDeviceLibPathArg())
3650eae32dcSDimitry Andric     LibraryPaths.push_back(DriverArgs.MakeArgString(Path));
3660eae32dcSDimitry Andric 
3670eae32dcSDimitry Andric   addDirectoryList(DriverArgs, LibraryPaths, "", "HIP_DEVICE_LIB_PATH");
3680eae32dcSDimitry Andric 
3690eae32dcSDimitry Andric   // Maintain compatability with --hip-device-lib.
3700eae32dcSDimitry Andric   auto BCLibArgs = DriverArgs.getAllArgValues(options::OPT_hip_device_lib_EQ);
3710eae32dcSDimitry Andric   if (!BCLibArgs.empty()) {
3720eae32dcSDimitry Andric     llvm::for_each(BCLibArgs, [&](StringRef BCName) {
3730eae32dcSDimitry Andric       StringRef FullName;
374bdd1243dSDimitry Andric       for (StringRef LibraryPath : LibraryPaths) {
3750eae32dcSDimitry Andric         SmallString<128> Path(LibraryPath);
3760eae32dcSDimitry Andric         llvm::sys::path::append(Path, BCName);
3770eae32dcSDimitry Andric         FullName = Path;
3780eae32dcSDimitry Andric         if (llvm::sys::fs::exists(FullName)) {
3790eae32dcSDimitry Andric           BCLibs.push_back(FullName);
3800eae32dcSDimitry Andric           return;
3810eae32dcSDimitry Andric         }
3820eae32dcSDimitry Andric       }
3830eae32dcSDimitry Andric       getDriver().Diag(diag::err_drv_no_such_file) << BCName;
3840eae32dcSDimitry Andric     });
3850eae32dcSDimitry Andric   } else {
386*06c3fb27SDimitry Andric     if (!RocmInstallation->hasDeviceLibrary()) {
3870eae32dcSDimitry Andric       getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 0;
3880eae32dcSDimitry Andric       return {};
3890eae32dcSDimitry Andric     }
3900eae32dcSDimitry Andric     StringRef GpuArch = getGPUArch(DriverArgs);
3910eae32dcSDimitry Andric     assert(!GpuArch.empty() && "Must have an explicit GPU arch.");
3920eae32dcSDimitry Andric 
3930eae32dcSDimitry Andric     // If --hip-device-lib is not set, add the default bitcode libraries.
3940eae32dcSDimitry Andric     if (DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
39581ad6265SDimitry Andric                            options::OPT_fno_gpu_sanitize, true) &&
3960eae32dcSDimitry Andric         getSanitizerArgs(DriverArgs).needsAsanRt()) {
397*06c3fb27SDimitry Andric       auto AsanRTL = RocmInstallation->getAsanRTLPath();
3980eae32dcSDimitry Andric       if (AsanRTL.empty()) {
3990eae32dcSDimitry Andric         unsigned DiagID = getDriver().getDiags().getCustomDiagID(
4000eae32dcSDimitry Andric             DiagnosticsEngine::Error,
4010eae32dcSDimitry Andric             "AMDGPU address sanitizer runtime library (asanrtl) is not found. "
4020eae32dcSDimitry Andric             "Please install ROCm device library which supports address "
4030eae32dcSDimitry Andric             "sanitizer");
4040eae32dcSDimitry Andric         getDriver().Diag(DiagID);
4050eae32dcSDimitry Andric         return {};
4060eae32dcSDimitry Andric       } else
407bdd1243dSDimitry Andric         BCLibs.emplace_back(AsanRTL, /*ShouldInternalize=*/false);
4080eae32dcSDimitry Andric     }
4090eae32dcSDimitry Andric 
4100eae32dcSDimitry Andric     // Add the HIP specific bitcode library.
411*06c3fb27SDimitry Andric     BCLibs.push_back(RocmInstallation->getHIPPath());
4120eae32dcSDimitry Andric 
4130eae32dcSDimitry Andric     // Add common device libraries like ocml etc.
414bdd1243dSDimitry Andric     for (StringRef N : getCommonDeviceLibNames(DriverArgs, GpuArch.str()))
415bdd1243dSDimitry Andric       BCLibs.emplace_back(N);
4160eae32dcSDimitry Andric 
4170eae32dcSDimitry Andric     // Add instrument lib.
4180eae32dcSDimitry Andric     auto InstLib =
4190eae32dcSDimitry Andric         DriverArgs.getLastArgValue(options::OPT_gpu_instrument_lib_EQ);
4200eae32dcSDimitry Andric     if (InstLib.empty())
4210eae32dcSDimitry Andric       return BCLibs;
4220eae32dcSDimitry Andric     if (llvm::sys::fs::exists(InstLib))
4230eae32dcSDimitry Andric       BCLibs.push_back(InstLib);
4240eae32dcSDimitry Andric     else
4250eae32dcSDimitry Andric       getDriver().Diag(diag::err_drv_no_such_file) << InstLib;
4260eae32dcSDimitry Andric   }
4270eae32dcSDimitry Andric 
4280eae32dcSDimitry Andric   return BCLibs;
4290eae32dcSDimitry Andric }
4300eae32dcSDimitry Andric 
4310eae32dcSDimitry Andric void HIPAMDToolChain::checkTargetID(
4320eae32dcSDimitry Andric     const llvm::opt::ArgList &DriverArgs) const {
4330eae32dcSDimitry Andric   auto PTID = getParsedTargetID(DriverArgs);
4340eae32dcSDimitry Andric   if (PTID.OptionalTargetID && !PTID.OptionalGPUArch) {
4350eae32dcSDimitry Andric     getDriver().Diag(clang::diag::err_drv_bad_target_id)
43681ad6265SDimitry Andric         << *PTID.OptionalTargetID;
4370eae32dcSDimitry Andric   }
4380eae32dcSDimitry Andric }
439