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, 50*81ad6265SDimitry 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 75*81ad6265SDimitry Andric void AMDGCN::Linker::constructLlvmLinkCommand(Compilation &C, 76*81ad6265SDimitry Andric const JobAction &JA, 77*81ad6265SDimitry Andric const InputInfoList &Inputs, 78*81ad6265SDimitry Andric const InputInfo &Output, 79*81ad6265SDimitry Andric const llvm::opt::ArgList &Args) const { 80*81ad6265SDimitry Andric // Construct llvm-link command. 81*81ad6265SDimitry Andric // The output from llvm-link is a bitcode file. 82*81ad6265SDimitry Andric ArgStringList LlvmLinkArgs; 83*81ad6265SDimitry Andric 84*81ad6265SDimitry Andric assert(!Inputs.empty() && "Must have at least one input."); 85*81ad6265SDimitry Andric 86*81ad6265SDimitry Andric LlvmLinkArgs.append({"-o", Output.getFilename()}); 87*81ad6265SDimitry Andric for (auto Input : Inputs) 88*81ad6265SDimitry Andric LlvmLinkArgs.push_back(Input.getFilename()); 89*81ad6265SDimitry Andric 90*81ad6265SDimitry Andric // Look for archive of bundled bitcode in arguments, and add temporary files 91*81ad6265SDimitry Andric // for the extracted archive of bitcode to inputs. 92*81ad6265SDimitry Andric auto TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ); 93*81ad6265SDimitry Andric AddStaticDeviceLibsLinking(C, *this, JA, Inputs, Args, LlvmLinkArgs, "amdgcn", 94*81ad6265SDimitry Andric TargetID, 95*81ad6265SDimitry Andric /*IsBitCodeSDL=*/true, 96*81ad6265SDimitry Andric /*PostClangLink=*/false); 97*81ad6265SDimitry Andric 98*81ad6265SDimitry Andric const char *LlvmLink = 99*81ad6265SDimitry Andric Args.MakeArgString(getToolChain().GetProgramPath("llvm-link")); 100*81ad6265SDimitry Andric C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(), 101*81ad6265SDimitry Andric LlvmLink, LlvmLinkArgs, Inputs, 102*81ad6265SDimitry Andric Output)); 103*81ad6265SDimitry Andric } 104*81ad6265SDimitry 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. 1110eae32dcSDimitry Andric ArgStringList LldArgs{"-flavor", "gnu", "--no-undefined", "-shared", 1120eae32dcSDimitry Andric "-plugin-opt=-amdgpu-internalize-symbols"}; 1130eae32dcSDimitry Andric 1140eae32dcSDimitry Andric auto &TC = getToolChain(); 1150eae32dcSDimitry Andric auto &D = TC.getDriver(); 1160eae32dcSDimitry Andric assert(!Inputs.empty() && "Must have at least one input."); 1170eae32dcSDimitry Andric bool IsThinLTO = D.getLTOMode(/*IsOffload=*/true) == LTOK_Thin; 1180eae32dcSDimitry Andric addLTOOptions(TC, Args, LldArgs, Output, Inputs[0], IsThinLTO); 1190eae32dcSDimitry Andric 1200eae32dcSDimitry Andric // Extract all the -m options 1210eae32dcSDimitry Andric std::vector<llvm::StringRef> Features; 1220eae32dcSDimitry Andric amdgpu::getAMDGPUTargetFeatures(D, TC.getTriple(), Args, Features); 1230eae32dcSDimitry Andric 1240eae32dcSDimitry Andric // Add features to mattr such as cumode 1250eae32dcSDimitry Andric std::string MAttrString = "-plugin-opt=-mattr="; 1260eae32dcSDimitry Andric for (auto OneFeature : unifyTargetFeatures(Features)) { 1270eae32dcSDimitry Andric MAttrString.append(Args.MakeArgString(OneFeature)); 1280eae32dcSDimitry Andric if (OneFeature != Features.back()) 1290eae32dcSDimitry Andric MAttrString.append(","); 1300eae32dcSDimitry Andric } 1310eae32dcSDimitry Andric if (!Features.empty()) 1320eae32dcSDimitry Andric LldArgs.push_back(Args.MakeArgString(MAttrString)); 1330eae32dcSDimitry Andric 1340eae32dcSDimitry Andric // ToDo: Remove this option after AMDGPU backend supports ISA-level linking. 1350eae32dcSDimitry Andric // Since AMDGPU backend currently does not support ISA-level linking, all 1360eae32dcSDimitry Andric // called functions need to be imported. 1370eae32dcSDimitry Andric if (IsThinLTO) 1380eae32dcSDimitry Andric LldArgs.push_back(Args.MakeArgString("-plugin-opt=-force-import-all")); 1390eae32dcSDimitry Andric 1400eae32dcSDimitry Andric for (const Arg *A : Args.filtered(options::OPT_mllvm)) { 1410eae32dcSDimitry Andric LldArgs.push_back( 1420eae32dcSDimitry Andric Args.MakeArgString(Twine("-plugin-opt=") + A->getValue(0))); 1430eae32dcSDimitry Andric } 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*81ad6265SDimitry Andric for (auto *Arg : Args.filtered(options::OPT_Xoffload_linker)) 151*81ad6265SDimitry Andric LldArgs.push_back(Arg->getValue(1)); 152*81ad6265SDimitry Andric 1530eae32dcSDimitry Andric LldArgs.append({"-o", Output.getFilename()}); 1540eae32dcSDimitry Andric for (auto Input : Inputs) 1550eae32dcSDimitry Andric LldArgs.push_back(Input.getFilename()); 1560eae32dcSDimitry Andric 157*81ad6265SDimitry Andric // Look for archive of bundled bitcode in arguments, and add temporary files 158*81ad6265SDimitry Andric // for the extracted archive of bitcode to inputs. 159*81ad6265SDimitry Andric auto TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ); 160*81ad6265SDimitry Andric AddStaticDeviceLibsLinking(C, *this, JA, Inputs, Args, LldArgs, "amdgcn", 161*81ad6265SDimitry Andric TargetID, 162*81ad6265SDimitry Andric /*IsBitCodeSDL=*/true, 163*81ad6265SDimitry Andric /*PostClangLink=*/false); 164*81ad6265SDimitry Andric 1650eae32dcSDimitry Andric const char *Lld = Args.MakeArgString(getToolChain().GetProgramPath("lld")); 1660eae32dcSDimitry Andric C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(), 1670eae32dcSDimitry Andric Lld, LldArgs, Inputs, Output)); 1680eae32dcSDimitry Andric } 1690eae32dcSDimitry Andric 1700eae32dcSDimitry Andric // For amdgcn the inputs of the linker job are device bitcode and output is 171*81ad6265SDimitry Andric // either an object file or bitcode (-emit-llvm). It calls llvm-link, opt, 172*81ad6265SDimitry Andric // llc, then lld steps. 1730eae32dcSDimitry Andric void AMDGCN::Linker::ConstructJob(Compilation &C, const JobAction &JA, 1740eae32dcSDimitry Andric const InputInfo &Output, 1750eae32dcSDimitry Andric const InputInfoList &Inputs, 1760eae32dcSDimitry Andric const ArgList &Args, 1770eae32dcSDimitry Andric const char *LinkingOutput) const { 1780eae32dcSDimitry Andric if (Inputs.size() > 0 && 1790eae32dcSDimitry Andric Inputs[0].getType() == types::TY_Image && 1800eae32dcSDimitry Andric JA.getType() == types::TY_Object) 1810eae32dcSDimitry Andric return HIP::constructGenerateObjFileFromHIPFatBinary(C, Output, Inputs, 1820eae32dcSDimitry Andric Args, JA, *this); 1830eae32dcSDimitry Andric 1840eae32dcSDimitry Andric if (JA.getType() == types::TY_HIP_FATBIN) 1850eae32dcSDimitry Andric return HIP::constructHIPFatbinCommand(C, JA, Output.getFilename(), Inputs, 1860eae32dcSDimitry Andric Args, *this); 1870eae32dcSDimitry Andric 188*81ad6265SDimitry Andric if (JA.getType() == types::TY_LLVM_BC) 189*81ad6265SDimitry Andric return constructLlvmLinkCommand(C, JA, Inputs, Output, Args); 190*81ad6265SDimitry Andric 1910eae32dcSDimitry Andric return constructLldCommand(C, JA, Inputs, Output, Args); 1920eae32dcSDimitry Andric } 1930eae32dcSDimitry Andric 1940eae32dcSDimitry Andric HIPAMDToolChain::HIPAMDToolChain(const Driver &D, const llvm::Triple &Triple, 1950eae32dcSDimitry Andric const ToolChain &HostTC, const ArgList &Args) 1960eae32dcSDimitry Andric : ROCMToolChain(D, Triple, Args), HostTC(HostTC) { 1970eae32dcSDimitry Andric // Lookup binaries into the driver directory, this is used to 1980eae32dcSDimitry Andric // discover the clang-offload-bundler executable. 1990eae32dcSDimitry Andric getProgramPaths().push_back(getDriver().Dir); 2000eae32dcSDimitry Andric 2010eae32dcSDimitry Andric // Diagnose unsupported sanitizer options only once. 202*81ad6265SDimitry Andric if (!Args.hasFlag(options::OPT_fgpu_sanitize, options::OPT_fno_gpu_sanitize, 203*81ad6265SDimitry Andric true)) 204*81ad6265SDimitry Andric return; 2050eae32dcSDimitry Andric for (auto A : Args.filtered(options::OPT_fsanitize_EQ)) { 2060eae32dcSDimitry Andric SanitizerMask K = parseSanitizerValue(A->getValue(), /*AllowGroups=*/false); 2070eae32dcSDimitry Andric if (K != SanitizerKind::Address) 2080eae32dcSDimitry Andric D.getDiags().Report(clang::diag::warn_drv_unsupported_option_for_target) 2090eae32dcSDimitry Andric << A->getAsString(Args) << getTriple().str(); 2100eae32dcSDimitry Andric } 2110eae32dcSDimitry Andric } 2120eae32dcSDimitry Andric 2130eae32dcSDimitry Andric void HIPAMDToolChain::addClangTargetOptions( 2140eae32dcSDimitry Andric const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, 2150eae32dcSDimitry Andric Action::OffloadKind DeviceOffloadingKind) const { 2160eae32dcSDimitry Andric HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind); 2170eae32dcSDimitry Andric 2180eae32dcSDimitry Andric assert(DeviceOffloadingKind == Action::OFK_HIP && 2190eae32dcSDimitry Andric "Only HIP offloading kinds are supported for GPUs."); 2200eae32dcSDimitry Andric 2210eae32dcSDimitry Andric CC1Args.push_back("-fcuda-is-device"); 2220eae32dcSDimitry Andric 2230eae32dcSDimitry Andric if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals, 2240eae32dcSDimitry Andric options::OPT_fno_cuda_approx_transcendentals, false)) 2250eae32dcSDimitry Andric CC1Args.push_back("-fcuda-approx-transcendentals"); 2260eae32dcSDimitry Andric 2270eae32dcSDimitry Andric if (!DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, 2280eae32dcSDimitry Andric false)) 2290eae32dcSDimitry Andric CC1Args.append({"-mllvm", "-amdgpu-internalize-symbols"}); 2300eae32dcSDimitry Andric 2310eae32dcSDimitry Andric StringRef MaxThreadsPerBlock = 2320eae32dcSDimitry Andric DriverArgs.getLastArgValue(options::OPT_gpu_max_threads_per_block_EQ); 2330eae32dcSDimitry Andric if (!MaxThreadsPerBlock.empty()) { 2340eae32dcSDimitry Andric std::string ArgStr = 2350eae32dcSDimitry Andric std::string("--gpu-max-threads-per-block=") + MaxThreadsPerBlock.str(); 2360eae32dcSDimitry Andric CC1Args.push_back(DriverArgs.MakeArgStringRef(ArgStr)); 2370eae32dcSDimitry Andric } 2380eae32dcSDimitry Andric 2390eae32dcSDimitry Andric CC1Args.push_back("-fcuda-allow-variadic-functions"); 2400eae32dcSDimitry Andric 2410eae32dcSDimitry Andric // Default to "hidden" visibility, as object level linking will not be 2420eae32dcSDimitry Andric // supported for the foreseeable future. 2430eae32dcSDimitry Andric if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ, 2440eae32dcSDimitry Andric options::OPT_fvisibility_ms_compat)) { 2450eae32dcSDimitry Andric CC1Args.append({"-fvisibility", "hidden"}); 2460eae32dcSDimitry Andric CC1Args.push_back("-fapply-global-visibility-to-externs"); 2470eae32dcSDimitry Andric } 2480eae32dcSDimitry Andric 249*81ad6265SDimitry Andric for (auto BCFile : getHIPDeviceLibs(DriverArgs)) { 2500eae32dcSDimitry Andric CC1Args.push_back(BCFile.ShouldInternalize ? "-mlink-builtin-bitcode" 2510eae32dcSDimitry Andric : "-mlink-bitcode-file"); 2520eae32dcSDimitry Andric CC1Args.push_back(DriverArgs.MakeArgString(BCFile.Path)); 253*81ad6265SDimitry Andric } 2540eae32dcSDimitry Andric } 2550eae32dcSDimitry Andric 2560eae32dcSDimitry Andric llvm::opt::DerivedArgList * 2570eae32dcSDimitry Andric HIPAMDToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args, 2580eae32dcSDimitry Andric StringRef BoundArch, 2590eae32dcSDimitry Andric Action::OffloadKind DeviceOffloadKind) const { 2600eae32dcSDimitry Andric DerivedArgList *DAL = 2610eae32dcSDimitry Andric HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind); 2620eae32dcSDimitry Andric if (!DAL) 2630eae32dcSDimitry Andric DAL = new DerivedArgList(Args.getBaseArgs()); 2640eae32dcSDimitry Andric 2650eae32dcSDimitry Andric const OptTable &Opts = getDriver().getOpts(); 2660eae32dcSDimitry Andric 2670eae32dcSDimitry Andric for (Arg *A : Args) { 2680eae32dcSDimitry Andric if (!shouldSkipArgument(A) && 2690eae32dcSDimitry Andric !shouldSkipSanitizeOption(*this, Args, BoundArch, A)) 2700eae32dcSDimitry Andric DAL->append(A); 2710eae32dcSDimitry Andric } 2720eae32dcSDimitry Andric 2730eae32dcSDimitry Andric if (!BoundArch.empty()) { 2740eae32dcSDimitry Andric DAL->eraseArg(options::OPT_mcpu_EQ); 2750eae32dcSDimitry Andric DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_mcpu_EQ), BoundArch); 2760eae32dcSDimitry Andric checkTargetID(*DAL); 2770eae32dcSDimitry Andric } 2780eae32dcSDimitry Andric 2790eae32dcSDimitry Andric return DAL; 2800eae32dcSDimitry Andric } 2810eae32dcSDimitry Andric 2820eae32dcSDimitry Andric Tool *HIPAMDToolChain::buildLinker() const { 2830eae32dcSDimitry Andric assert(getTriple().getArch() == llvm::Triple::amdgcn); 2840eae32dcSDimitry Andric return new tools::AMDGCN::Linker(*this); 2850eae32dcSDimitry Andric } 2860eae32dcSDimitry Andric 2870eae32dcSDimitry Andric void HIPAMDToolChain::addClangWarningOptions(ArgStringList &CC1Args) const { 2880eae32dcSDimitry Andric HostTC.addClangWarningOptions(CC1Args); 2890eae32dcSDimitry Andric } 2900eae32dcSDimitry Andric 2910eae32dcSDimitry Andric ToolChain::CXXStdlibType 2920eae32dcSDimitry Andric HIPAMDToolChain::GetCXXStdlibType(const ArgList &Args) const { 2930eae32dcSDimitry Andric return HostTC.GetCXXStdlibType(Args); 2940eae32dcSDimitry Andric } 2950eae32dcSDimitry Andric 2960eae32dcSDimitry Andric void HIPAMDToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 2970eae32dcSDimitry Andric ArgStringList &CC1Args) const { 2980eae32dcSDimitry Andric HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args); 2990eae32dcSDimitry Andric } 3000eae32dcSDimitry Andric 3010eae32dcSDimitry Andric void HIPAMDToolChain::AddClangCXXStdlibIncludeArgs( 3020eae32dcSDimitry Andric const ArgList &Args, ArgStringList &CC1Args) const { 3030eae32dcSDimitry Andric HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args); 3040eae32dcSDimitry Andric } 3050eae32dcSDimitry Andric 3060eae32dcSDimitry Andric void HIPAMDToolChain::AddIAMCUIncludeArgs(const ArgList &Args, 3070eae32dcSDimitry Andric ArgStringList &CC1Args) const { 3080eae32dcSDimitry Andric HostTC.AddIAMCUIncludeArgs(Args, CC1Args); 3090eae32dcSDimitry Andric } 3100eae32dcSDimitry Andric 3110eae32dcSDimitry Andric void HIPAMDToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs, 3120eae32dcSDimitry Andric ArgStringList &CC1Args) const { 3130eae32dcSDimitry Andric RocmInstallation.AddHIPIncludeArgs(DriverArgs, CC1Args); 3140eae32dcSDimitry Andric } 3150eae32dcSDimitry Andric 3160eae32dcSDimitry Andric SanitizerMask HIPAMDToolChain::getSupportedSanitizers() const { 3170eae32dcSDimitry Andric // The HIPAMDToolChain only supports sanitizers in the sense that it allows 3180eae32dcSDimitry Andric // sanitizer arguments on the command line if they are supported by the host 3190eae32dcSDimitry Andric // toolchain. The HIPAMDToolChain will actually ignore any command line 3200eae32dcSDimitry Andric // arguments for any of these "supported" sanitizers. That means that no 3210eae32dcSDimitry Andric // sanitization of device code is actually supported at this time. 3220eae32dcSDimitry Andric // 3230eae32dcSDimitry Andric // This behavior is necessary because the host and device toolchains 3240eae32dcSDimitry Andric // invocations often share the command line, so the device toolchain must 3250eae32dcSDimitry Andric // tolerate flags meant only for the host toolchain. 3260eae32dcSDimitry Andric return HostTC.getSupportedSanitizers(); 3270eae32dcSDimitry Andric } 3280eae32dcSDimitry Andric 3290eae32dcSDimitry Andric VersionTuple HIPAMDToolChain::computeMSVCVersion(const Driver *D, 3300eae32dcSDimitry Andric const ArgList &Args) const { 3310eae32dcSDimitry Andric return HostTC.computeMSVCVersion(D, Args); 3320eae32dcSDimitry Andric } 3330eae32dcSDimitry Andric 3340eae32dcSDimitry Andric llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12> 3350eae32dcSDimitry Andric HIPAMDToolChain::getHIPDeviceLibs(const llvm::opt::ArgList &DriverArgs) const { 3360eae32dcSDimitry Andric llvm::SmallVector<BitCodeLibraryInfo, 12> BCLibs; 3370eae32dcSDimitry Andric if (DriverArgs.hasArg(options::OPT_nogpulib)) 3380eae32dcSDimitry Andric return {}; 3390eae32dcSDimitry Andric ArgStringList LibraryPaths; 3400eae32dcSDimitry Andric 3410eae32dcSDimitry Andric // Find in --hip-device-lib-path and HIP_LIBRARY_PATH. 3420eae32dcSDimitry Andric for (auto Path : RocmInstallation.getRocmDeviceLibPathArg()) 3430eae32dcSDimitry Andric LibraryPaths.push_back(DriverArgs.MakeArgString(Path)); 3440eae32dcSDimitry Andric 3450eae32dcSDimitry Andric addDirectoryList(DriverArgs, LibraryPaths, "", "HIP_DEVICE_LIB_PATH"); 3460eae32dcSDimitry Andric 3470eae32dcSDimitry Andric // Maintain compatability with --hip-device-lib. 3480eae32dcSDimitry Andric auto BCLibArgs = DriverArgs.getAllArgValues(options::OPT_hip_device_lib_EQ); 3490eae32dcSDimitry Andric if (!BCLibArgs.empty()) { 3500eae32dcSDimitry Andric llvm::for_each(BCLibArgs, [&](StringRef BCName) { 3510eae32dcSDimitry Andric StringRef FullName; 3520eae32dcSDimitry Andric for (std::string LibraryPath : LibraryPaths) { 3530eae32dcSDimitry Andric SmallString<128> Path(LibraryPath); 3540eae32dcSDimitry Andric llvm::sys::path::append(Path, BCName); 3550eae32dcSDimitry Andric FullName = Path; 3560eae32dcSDimitry Andric if (llvm::sys::fs::exists(FullName)) { 3570eae32dcSDimitry Andric BCLibs.push_back(FullName); 3580eae32dcSDimitry Andric return; 3590eae32dcSDimitry Andric } 3600eae32dcSDimitry Andric } 3610eae32dcSDimitry Andric getDriver().Diag(diag::err_drv_no_such_file) << BCName; 3620eae32dcSDimitry Andric }); 3630eae32dcSDimitry Andric } else { 3640eae32dcSDimitry Andric if (!RocmInstallation.hasDeviceLibrary()) { 3650eae32dcSDimitry Andric getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 0; 3660eae32dcSDimitry Andric return {}; 3670eae32dcSDimitry Andric } 3680eae32dcSDimitry Andric StringRef GpuArch = getGPUArch(DriverArgs); 3690eae32dcSDimitry Andric assert(!GpuArch.empty() && "Must have an explicit GPU arch."); 3700eae32dcSDimitry Andric 3710eae32dcSDimitry Andric // If --hip-device-lib is not set, add the default bitcode libraries. 3720eae32dcSDimitry Andric if (DriverArgs.hasFlag(options::OPT_fgpu_sanitize, 373*81ad6265SDimitry Andric options::OPT_fno_gpu_sanitize, true) && 3740eae32dcSDimitry Andric getSanitizerArgs(DriverArgs).needsAsanRt()) { 3750eae32dcSDimitry Andric auto AsanRTL = RocmInstallation.getAsanRTLPath(); 3760eae32dcSDimitry Andric if (AsanRTL.empty()) { 3770eae32dcSDimitry Andric unsigned DiagID = getDriver().getDiags().getCustomDiagID( 3780eae32dcSDimitry Andric DiagnosticsEngine::Error, 3790eae32dcSDimitry Andric "AMDGPU address sanitizer runtime library (asanrtl) is not found. " 3800eae32dcSDimitry Andric "Please install ROCm device library which supports address " 3810eae32dcSDimitry Andric "sanitizer"); 3820eae32dcSDimitry Andric getDriver().Diag(DiagID); 3830eae32dcSDimitry Andric return {}; 3840eae32dcSDimitry Andric } else 3850eae32dcSDimitry Andric BCLibs.push_back({AsanRTL.str(), /*ShouldInternalize=*/false}); 3860eae32dcSDimitry Andric } 3870eae32dcSDimitry Andric 3880eae32dcSDimitry Andric // Add the HIP specific bitcode library. 3890eae32dcSDimitry Andric BCLibs.push_back(RocmInstallation.getHIPPath()); 3900eae32dcSDimitry Andric 3910eae32dcSDimitry Andric // Add common device libraries like ocml etc. 3920eae32dcSDimitry Andric for (auto N : getCommonDeviceLibNames(DriverArgs, GpuArch.str())) 3930eae32dcSDimitry Andric BCLibs.push_back(StringRef(N)); 3940eae32dcSDimitry Andric 3950eae32dcSDimitry Andric // Add instrument lib. 3960eae32dcSDimitry Andric auto InstLib = 3970eae32dcSDimitry Andric DriverArgs.getLastArgValue(options::OPT_gpu_instrument_lib_EQ); 3980eae32dcSDimitry Andric if (InstLib.empty()) 3990eae32dcSDimitry Andric return BCLibs; 4000eae32dcSDimitry Andric if (llvm::sys::fs::exists(InstLib)) 4010eae32dcSDimitry Andric BCLibs.push_back(InstLib); 4020eae32dcSDimitry Andric else 4030eae32dcSDimitry Andric getDriver().Diag(diag::err_drv_no_such_file) << InstLib; 4040eae32dcSDimitry Andric } 4050eae32dcSDimitry Andric 4060eae32dcSDimitry Andric return BCLibs; 4070eae32dcSDimitry Andric } 4080eae32dcSDimitry Andric 4090eae32dcSDimitry Andric void HIPAMDToolChain::checkTargetID( 4100eae32dcSDimitry Andric const llvm::opt::ArgList &DriverArgs) const { 4110eae32dcSDimitry Andric auto PTID = getParsedTargetID(DriverArgs); 4120eae32dcSDimitry Andric if (PTID.OptionalTargetID && !PTID.OptionalGPUArch) { 4130eae32dcSDimitry Andric getDriver().Diag(clang::diag::err_drv_bad_target_id) 414*81ad6265SDimitry Andric << *PTID.OptionalTargetID; 4150eae32dcSDimitry Andric } 4160eae32dcSDimitry Andric } 417