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" 2406c3fb27SDimitry 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", 94*5f757f3fSDimitry Andric TargetID, /*IsBitCodeSDL=*/true); 9581ad6265SDimitry Andric 9681ad6265SDimitry Andric const char *LlvmLink = 9781ad6265SDimitry Andric Args.MakeArgString(getToolChain().GetProgramPath("llvm-link")); 9881ad6265SDimitry Andric C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(), 9981ad6265SDimitry Andric LlvmLink, LlvmLinkArgs, Inputs, 10081ad6265SDimitry Andric Output)); 10181ad6265SDimitry Andric } 10281ad6265SDimitry Andric 1030eae32dcSDimitry Andric void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA, 1040eae32dcSDimitry Andric const InputInfoList &Inputs, 1050eae32dcSDimitry Andric const InputInfo &Output, 1060eae32dcSDimitry Andric const llvm::opt::ArgList &Args) const { 1070eae32dcSDimitry Andric // Construct lld command. 1080eae32dcSDimitry Andric // The output from ld.lld is an HSA code object file. 109bdd1243dSDimitry Andric ArgStringList LldArgs{"-flavor", 110bdd1243dSDimitry Andric "gnu", 111bdd1243dSDimitry Andric "-m", 112bdd1243dSDimitry Andric "elf64_amdgpu", 113bdd1243dSDimitry Andric "--no-undefined", 114bdd1243dSDimitry Andric "-shared", 1150eae32dcSDimitry Andric "-plugin-opt=-amdgpu-internalize-symbols"}; 116*5f757f3fSDimitry Andric if (Args.hasArg(options::OPT_hipstdpar)) 117*5f757f3fSDimitry Andric LldArgs.push_back("-plugin-opt=-amdgpu-enable-hipstdpar"); 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 15006c3fb27SDimitry Andric // Given that host and device linking happen in separate processes, the device 15106c3fb27SDimitry Andric // linker doesn't always have the visibility as to which device symbols are 15206c3fb27SDimitry Andric // needed by a program, especially for the device symbol dependencies that are 15306c3fb27SDimitry Andric // introduced through the host symbol resolution. 15406c3fb27SDimitry Andric // For example: host_A() (A.obj) --> host_B(B.obj) --> device_kernel_B() 15506c3fb27SDimitry Andric // (B.obj) In this case, the device linker doesn't know that A.obj actually 15606c3fb27SDimitry Andric // depends on the kernel functions in B.obj. When linking to static device 15706c3fb27SDimitry Andric // library, the device linker may drop some of the device global symbols if 15806c3fb27SDimitry Andric // they aren't referenced. As a workaround, we are adding to the 15906c3fb27SDimitry Andric // --whole-archive flag such that all global symbols would be linked in. 16006c3fb27SDimitry Andric LldArgs.push_back("--whole-archive"); 16106c3fb27SDimitry Andric 16206c3fb27SDimitry Andric for (auto *Arg : Args.filtered(options::OPT_Xoffload_linker)) { 16306c3fb27SDimitry Andric StringRef ArgVal = Arg->getValue(1); 16406c3fb27SDimitry Andric auto SplitArg = ArgVal.split("-mllvm="); 16506c3fb27SDimitry Andric if (!SplitArg.second.empty()) { 16606c3fb27SDimitry Andric LldArgs.push_back( 16706c3fb27SDimitry Andric Args.MakeArgString(Twine("-plugin-opt=") + SplitArg.second)); 16806c3fb27SDimitry Andric } else { 16906c3fb27SDimitry Andric LldArgs.push_back(Args.MakeArgString(ArgVal)); 17006c3fb27SDimitry Andric } 17106c3fb27SDimitry Andric Arg->claim(); 17206c3fb27SDimitry 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", 182*5f757f3fSDimitry Andric TargetID, /*IsBitCodeSDL=*/true); 18381ad6265SDimitry Andric 18406c3fb27SDimitry Andric LldArgs.push_back("--no-whole-archive"); 18506c3fb27SDimitry Andric 1860eae32dcSDimitry Andric const char *Lld = Args.MakeArgString(getToolChain().GetProgramPath("lld")); 1870eae32dcSDimitry Andric C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(), 1880eae32dcSDimitry Andric Lld, LldArgs, Inputs, Output)); 1890eae32dcSDimitry Andric } 1900eae32dcSDimitry Andric 1910eae32dcSDimitry Andric // For amdgcn the inputs of the linker job are device bitcode and output is 19281ad6265SDimitry Andric // either an object file or bitcode (-emit-llvm). It calls llvm-link, opt, 19381ad6265SDimitry Andric // llc, then lld steps. 1940eae32dcSDimitry Andric void AMDGCN::Linker::ConstructJob(Compilation &C, const JobAction &JA, 1950eae32dcSDimitry Andric const InputInfo &Output, 1960eae32dcSDimitry Andric const InputInfoList &Inputs, 1970eae32dcSDimitry Andric const ArgList &Args, 1980eae32dcSDimitry Andric const char *LinkingOutput) const { 1990eae32dcSDimitry Andric if (Inputs.size() > 0 && 2000eae32dcSDimitry Andric Inputs[0].getType() == types::TY_Image && 2010eae32dcSDimitry Andric JA.getType() == types::TY_Object) 2020eae32dcSDimitry Andric return HIP::constructGenerateObjFileFromHIPFatBinary(C, Output, Inputs, 2030eae32dcSDimitry Andric Args, JA, *this); 2040eae32dcSDimitry Andric 2050eae32dcSDimitry Andric if (JA.getType() == types::TY_HIP_FATBIN) 2060eae32dcSDimitry Andric return HIP::constructHIPFatbinCommand(C, JA, Output.getFilename(), Inputs, 2070eae32dcSDimitry Andric Args, *this); 2080eae32dcSDimitry Andric 20981ad6265SDimitry Andric if (JA.getType() == types::TY_LLVM_BC) 21081ad6265SDimitry Andric return constructLlvmLinkCommand(C, JA, Inputs, Output, Args); 21181ad6265SDimitry Andric 2120eae32dcSDimitry Andric return constructLldCommand(C, JA, Inputs, Output, Args); 2130eae32dcSDimitry Andric } 2140eae32dcSDimitry Andric 2150eae32dcSDimitry Andric HIPAMDToolChain::HIPAMDToolChain(const Driver &D, const llvm::Triple &Triple, 2160eae32dcSDimitry Andric const ToolChain &HostTC, const ArgList &Args) 2170eae32dcSDimitry Andric : ROCMToolChain(D, Triple, Args), HostTC(HostTC) { 2180eae32dcSDimitry Andric // Lookup binaries into the driver directory, this is used to 2190eae32dcSDimitry Andric // discover the clang-offload-bundler executable. 2200eae32dcSDimitry Andric getProgramPaths().push_back(getDriver().Dir); 2210eae32dcSDimitry Andric 2220eae32dcSDimitry Andric // Diagnose unsupported sanitizer options only once. 22381ad6265SDimitry Andric if (!Args.hasFlag(options::OPT_fgpu_sanitize, options::OPT_fno_gpu_sanitize, 22481ad6265SDimitry Andric true)) 22581ad6265SDimitry Andric return; 226bdd1243dSDimitry Andric for (auto *A : Args.filtered(options::OPT_fsanitize_EQ)) { 2270eae32dcSDimitry Andric SanitizerMask K = parseSanitizerValue(A->getValue(), /*AllowGroups=*/false); 2280eae32dcSDimitry Andric if (K != SanitizerKind::Address) 2290eae32dcSDimitry Andric D.getDiags().Report(clang::diag::warn_drv_unsupported_option_for_target) 2300eae32dcSDimitry Andric << A->getAsString(Args) << getTriple().str(); 2310eae32dcSDimitry Andric } 2320eae32dcSDimitry Andric } 2330eae32dcSDimitry Andric 2340eae32dcSDimitry Andric void HIPAMDToolChain::addClangTargetOptions( 2350eae32dcSDimitry Andric const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, 2360eae32dcSDimitry Andric Action::OffloadKind DeviceOffloadingKind) const { 2370eae32dcSDimitry Andric HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind); 2380eae32dcSDimitry Andric 2390eae32dcSDimitry Andric assert(DeviceOffloadingKind == Action::OFK_HIP && 2400eae32dcSDimitry Andric "Only HIP offloading kinds are supported for GPUs."); 2410eae32dcSDimitry Andric 2420eae32dcSDimitry Andric CC1Args.push_back("-fcuda-is-device"); 2430eae32dcSDimitry Andric 2440eae32dcSDimitry Andric if (!DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, 2450eae32dcSDimitry Andric false)) 2460eae32dcSDimitry Andric CC1Args.append({"-mllvm", "-amdgpu-internalize-symbols"}); 247*5f757f3fSDimitry Andric if (DriverArgs.hasArgNoClaim(options::OPT_hipstdpar)) 248*5f757f3fSDimitry Andric CC1Args.append({"-mllvm", "-amdgpu-enable-hipstdpar"}); 2490eae32dcSDimitry Andric 2500eae32dcSDimitry Andric StringRef MaxThreadsPerBlock = 2510eae32dcSDimitry Andric DriverArgs.getLastArgValue(options::OPT_gpu_max_threads_per_block_EQ); 2520eae32dcSDimitry Andric if (!MaxThreadsPerBlock.empty()) { 2530eae32dcSDimitry Andric std::string ArgStr = 254bdd1243dSDimitry Andric (Twine("--gpu-max-threads-per-block=") + MaxThreadsPerBlock).str(); 2550eae32dcSDimitry Andric CC1Args.push_back(DriverArgs.MakeArgStringRef(ArgStr)); 2560eae32dcSDimitry Andric } 2570eae32dcSDimitry Andric 2580eae32dcSDimitry Andric CC1Args.push_back("-fcuda-allow-variadic-functions"); 2590eae32dcSDimitry Andric 2600eae32dcSDimitry Andric // Default to "hidden" visibility, as object level linking will not be 2610eae32dcSDimitry Andric // supported for the foreseeable future. 2620eae32dcSDimitry Andric if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ, 2630eae32dcSDimitry Andric options::OPT_fvisibility_ms_compat)) { 264bdd1243dSDimitry Andric CC1Args.append({"-fvisibility=hidden"}); 2650eae32dcSDimitry Andric CC1Args.push_back("-fapply-global-visibility-to-externs"); 2660eae32dcSDimitry Andric } 2670eae32dcSDimitry Andric 268bdd1243dSDimitry Andric for (auto BCFile : getDeviceLibs(DriverArgs)) { 2690eae32dcSDimitry Andric CC1Args.push_back(BCFile.ShouldInternalize ? "-mlink-builtin-bitcode" 2700eae32dcSDimitry Andric : "-mlink-bitcode-file"); 2710eae32dcSDimitry Andric CC1Args.push_back(DriverArgs.MakeArgString(BCFile.Path)); 27281ad6265SDimitry Andric } 2730eae32dcSDimitry Andric } 2740eae32dcSDimitry Andric 2750eae32dcSDimitry Andric llvm::opt::DerivedArgList * 2760eae32dcSDimitry Andric HIPAMDToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args, 2770eae32dcSDimitry Andric StringRef BoundArch, 2780eae32dcSDimitry Andric Action::OffloadKind DeviceOffloadKind) const { 2790eae32dcSDimitry Andric DerivedArgList *DAL = 2800eae32dcSDimitry Andric HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind); 2810eae32dcSDimitry Andric if (!DAL) 2820eae32dcSDimitry Andric DAL = new DerivedArgList(Args.getBaseArgs()); 2830eae32dcSDimitry Andric 2840eae32dcSDimitry Andric const OptTable &Opts = getDriver().getOpts(); 2850eae32dcSDimitry Andric 2860eae32dcSDimitry Andric for (Arg *A : Args) { 28706c3fb27SDimitry Andric if (!shouldSkipSanitizeOption(*this, Args, BoundArch, A)) 2880eae32dcSDimitry Andric DAL->append(A); 2890eae32dcSDimitry Andric } 2900eae32dcSDimitry Andric 2910eae32dcSDimitry Andric if (!BoundArch.empty()) { 2920eae32dcSDimitry Andric DAL->eraseArg(options::OPT_mcpu_EQ); 2930eae32dcSDimitry Andric DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_mcpu_EQ), BoundArch); 2940eae32dcSDimitry Andric checkTargetID(*DAL); 2950eae32dcSDimitry Andric } 2960eae32dcSDimitry Andric 2970eae32dcSDimitry Andric return DAL; 2980eae32dcSDimitry Andric } 2990eae32dcSDimitry Andric 3000eae32dcSDimitry Andric Tool *HIPAMDToolChain::buildLinker() const { 3010eae32dcSDimitry Andric assert(getTriple().getArch() == llvm::Triple::amdgcn); 3020eae32dcSDimitry Andric return new tools::AMDGCN::Linker(*this); 3030eae32dcSDimitry Andric } 3040eae32dcSDimitry Andric 3050eae32dcSDimitry Andric void HIPAMDToolChain::addClangWarningOptions(ArgStringList &CC1Args) const { 3060eae32dcSDimitry Andric HostTC.addClangWarningOptions(CC1Args); 3070eae32dcSDimitry Andric } 3080eae32dcSDimitry Andric 3090eae32dcSDimitry Andric ToolChain::CXXStdlibType 3100eae32dcSDimitry Andric HIPAMDToolChain::GetCXXStdlibType(const ArgList &Args) const { 3110eae32dcSDimitry Andric return HostTC.GetCXXStdlibType(Args); 3120eae32dcSDimitry Andric } 3130eae32dcSDimitry Andric 3140eae32dcSDimitry Andric void HIPAMDToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 3150eae32dcSDimitry Andric ArgStringList &CC1Args) const { 3160eae32dcSDimitry Andric HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args); 3170eae32dcSDimitry Andric } 3180eae32dcSDimitry Andric 3190eae32dcSDimitry Andric void HIPAMDToolChain::AddClangCXXStdlibIncludeArgs( 3200eae32dcSDimitry Andric const ArgList &Args, ArgStringList &CC1Args) const { 3210eae32dcSDimitry Andric HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args); 3220eae32dcSDimitry Andric } 3230eae32dcSDimitry Andric 3240eae32dcSDimitry Andric void HIPAMDToolChain::AddIAMCUIncludeArgs(const ArgList &Args, 3250eae32dcSDimitry Andric ArgStringList &CC1Args) const { 3260eae32dcSDimitry Andric HostTC.AddIAMCUIncludeArgs(Args, CC1Args); 3270eae32dcSDimitry Andric } 3280eae32dcSDimitry Andric 3290eae32dcSDimitry Andric void HIPAMDToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs, 3300eae32dcSDimitry Andric ArgStringList &CC1Args) const { 33106c3fb27SDimitry Andric RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args); 3320eae32dcSDimitry Andric } 3330eae32dcSDimitry Andric 3340eae32dcSDimitry Andric SanitizerMask HIPAMDToolChain::getSupportedSanitizers() const { 3350eae32dcSDimitry Andric // The HIPAMDToolChain only supports sanitizers in the sense that it allows 3360eae32dcSDimitry Andric // sanitizer arguments on the command line if they are supported by the host 3370eae32dcSDimitry Andric // toolchain. The HIPAMDToolChain will actually ignore any command line 3380eae32dcSDimitry Andric // arguments for any of these "supported" sanitizers. That means that no 3390eae32dcSDimitry Andric // sanitization of device code is actually supported at this time. 3400eae32dcSDimitry Andric // 3410eae32dcSDimitry Andric // This behavior is necessary because the host and device toolchains 3420eae32dcSDimitry Andric // invocations often share the command line, so the device toolchain must 3430eae32dcSDimitry Andric // tolerate flags meant only for the host toolchain. 3440eae32dcSDimitry Andric return HostTC.getSupportedSanitizers(); 3450eae32dcSDimitry Andric } 3460eae32dcSDimitry Andric 3470eae32dcSDimitry Andric VersionTuple HIPAMDToolChain::computeMSVCVersion(const Driver *D, 3480eae32dcSDimitry Andric const ArgList &Args) const { 3490eae32dcSDimitry Andric return HostTC.computeMSVCVersion(D, Args); 3500eae32dcSDimitry Andric } 3510eae32dcSDimitry Andric 3520eae32dcSDimitry Andric llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12> 353bdd1243dSDimitry Andric HIPAMDToolChain::getDeviceLibs(const llvm::opt::ArgList &DriverArgs) const { 3540eae32dcSDimitry Andric llvm::SmallVector<BitCodeLibraryInfo, 12> BCLibs; 3550eae32dcSDimitry Andric if (DriverArgs.hasArg(options::OPT_nogpulib)) 3560eae32dcSDimitry Andric return {}; 3570eae32dcSDimitry Andric ArgStringList LibraryPaths; 3580eae32dcSDimitry Andric 3590eae32dcSDimitry Andric // Find in --hip-device-lib-path and HIP_LIBRARY_PATH. 36006c3fb27SDimitry Andric for (StringRef Path : RocmInstallation->getRocmDeviceLibPathArg()) 3610eae32dcSDimitry Andric LibraryPaths.push_back(DriverArgs.MakeArgString(Path)); 3620eae32dcSDimitry Andric 3630eae32dcSDimitry Andric addDirectoryList(DriverArgs, LibraryPaths, "", "HIP_DEVICE_LIB_PATH"); 3640eae32dcSDimitry Andric 3650eae32dcSDimitry Andric // Maintain compatability with --hip-device-lib. 3660eae32dcSDimitry Andric auto BCLibArgs = DriverArgs.getAllArgValues(options::OPT_hip_device_lib_EQ); 3670eae32dcSDimitry Andric if (!BCLibArgs.empty()) { 3680eae32dcSDimitry Andric llvm::for_each(BCLibArgs, [&](StringRef BCName) { 3690eae32dcSDimitry Andric StringRef FullName; 370bdd1243dSDimitry Andric for (StringRef LibraryPath : LibraryPaths) { 3710eae32dcSDimitry Andric SmallString<128> Path(LibraryPath); 3720eae32dcSDimitry Andric llvm::sys::path::append(Path, BCName); 3730eae32dcSDimitry Andric FullName = Path; 3740eae32dcSDimitry Andric if (llvm::sys::fs::exists(FullName)) { 3750eae32dcSDimitry Andric BCLibs.push_back(FullName); 3760eae32dcSDimitry Andric return; 3770eae32dcSDimitry Andric } 3780eae32dcSDimitry Andric } 3790eae32dcSDimitry Andric getDriver().Diag(diag::err_drv_no_such_file) << BCName; 3800eae32dcSDimitry Andric }); 3810eae32dcSDimitry Andric } else { 38206c3fb27SDimitry Andric if (!RocmInstallation->hasDeviceLibrary()) { 3830eae32dcSDimitry Andric getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 0; 3840eae32dcSDimitry Andric return {}; 3850eae32dcSDimitry Andric } 3860eae32dcSDimitry Andric StringRef GpuArch = getGPUArch(DriverArgs); 3870eae32dcSDimitry Andric assert(!GpuArch.empty() && "Must have an explicit GPU arch."); 3880eae32dcSDimitry Andric 3890eae32dcSDimitry Andric // If --hip-device-lib is not set, add the default bitcode libraries. 3900eae32dcSDimitry Andric if (DriverArgs.hasFlag(options::OPT_fgpu_sanitize, 39181ad6265SDimitry Andric options::OPT_fno_gpu_sanitize, true) && 3920eae32dcSDimitry Andric getSanitizerArgs(DriverArgs).needsAsanRt()) { 39306c3fb27SDimitry Andric auto AsanRTL = RocmInstallation->getAsanRTLPath(); 3940eae32dcSDimitry Andric if (AsanRTL.empty()) { 3950eae32dcSDimitry Andric unsigned DiagID = getDriver().getDiags().getCustomDiagID( 3960eae32dcSDimitry Andric DiagnosticsEngine::Error, 3970eae32dcSDimitry Andric "AMDGPU address sanitizer runtime library (asanrtl) is not found. " 3980eae32dcSDimitry Andric "Please install ROCm device library which supports address " 3990eae32dcSDimitry Andric "sanitizer"); 4000eae32dcSDimitry Andric getDriver().Diag(DiagID); 4010eae32dcSDimitry Andric return {}; 4020eae32dcSDimitry Andric } else 403bdd1243dSDimitry Andric BCLibs.emplace_back(AsanRTL, /*ShouldInternalize=*/false); 4040eae32dcSDimitry Andric } 4050eae32dcSDimitry Andric 4060eae32dcSDimitry Andric // Add the HIP specific bitcode library. 40706c3fb27SDimitry Andric BCLibs.push_back(RocmInstallation->getHIPPath()); 4080eae32dcSDimitry Andric 4090eae32dcSDimitry Andric // Add common device libraries like ocml etc. 410bdd1243dSDimitry Andric for (StringRef N : getCommonDeviceLibNames(DriverArgs, GpuArch.str())) 411bdd1243dSDimitry Andric BCLibs.emplace_back(N); 4120eae32dcSDimitry Andric 4130eae32dcSDimitry Andric // Add instrument lib. 4140eae32dcSDimitry Andric auto InstLib = 4150eae32dcSDimitry Andric DriverArgs.getLastArgValue(options::OPT_gpu_instrument_lib_EQ); 4160eae32dcSDimitry Andric if (InstLib.empty()) 4170eae32dcSDimitry Andric return BCLibs; 4180eae32dcSDimitry Andric if (llvm::sys::fs::exists(InstLib)) 4190eae32dcSDimitry Andric BCLibs.push_back(InstLib); 4200eae32dcSDimitry Andric else 4210eae32dcSDimitry Andric getDriver().Diag(diag::err_drv_no_such_file) << InstLib; 4220eae32dcSDimitry Andric } 4230eae32dcSDimitry Andric 4240eae32dcSDimitry Andric return BCLibs; 4250eae32dcSDimitry Andric } 4260eae32dcSDimitry Andric 4270eae32dcSDimitry Andric void HIPAMDToolChain::checkTargetID( 4280eae32dcSDimitry Andric const llvm::opt::ArgList &DriverArgs) const { 4290eae32dcSDimitry Andric auto PTID = getParsedTargetID(DriverArgs); 4300eae32dcSDimitry Andric if (PTID.OptionalTargetID && !PTID.OptionalGPUArch) { 4310eae32dcSDimitry Andric getDriver().Diag(clang::diag::err_drv_bad_target_id) 43281ad6265SDimitry Andric << *PTID.OptionalTargetID; 4330eae32dcSDimitry Andric } 4340eae32dcSDimitry Andric } 435