brintos

brintos / llvm-project-archived public Read only

0
0
Text · 11.4 KiB · be0f49d Raw
292 lines · cpp
1//===--- HIPSPV.cpp - HIPSPV ToolChain Implementation -----------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "HIPSPV.h"10#include "HIPUtility.h"11#include "clang/Driver/CommonArgs.h"12#include "clang/Driver/Compilation.h"13#include "clang/Driver/Driver.h"14#include "clang/Driver/InputInfo.h"15#include "clang/Options/Options.h"16#include "llvm/Support/FileSystem.h"17#include "llvm/Support/Path.h"18 19using namespace clang::driver;20using namespace clang::driver::toolchains;21using namespace clang::driver::tools;22using namespace clang;23using namespace llvm::opt;24 25// Locates HIP pass plugin.26static std::string findPassPlugin(const Driver &D,27                                  const llvm::opt::ArgList &Args) {28  StringRef Path = Args.getLastArgValue(options::OPT_hipspv_pass_plugin_EQ);29  if (!Path.empty()) {30    if (llvm::sys::fs::exists(Path))31      return Path.str();32    D.Diag(diag::err_drv_no_such_file) << Path;33  }34 35  StringRef hipPath = Args.getLastArgValue(options::OPT_hip_path_EQ);36  if (!hipPath.empty()) {37    SmallString<128> PluginPath(hipPath);38    llvm::sys::path::append(PluginPath, "lib", "libLLVMHipSpvPasses.so");39    if (llvm::sys::fs::exists(PluginPath))40      return PluginPath.str().str();41    PluginPath.assign(hipPath);42    llvm::sys::path::append(PluginPath, "lib", "llvm",43                            "libLLVMHipSpvPasses.so");44    if (llvm::sys::fs::exists(PluginPath))45      return PluginPath.str().str();46  }47 48  return std::string();49}50 51void HIPSPV::Linker::constructLinkAndEmitSpirvCommand(52    Compilation &C, const JobAction &JA, const InputInfoList &Inputs,53    const InputInfo &Output, const llvm::opt::ArgList &Args) const {54 55  assert(!Inputs.empty() && "Must have at least one input.");56  std::string Name = std::string(llvm::sys::path::stem(Output.getFilename()));57  const char *TempFile = HIP::getTempFile(C, Name + "-link", "bc");58 59  // Link LLVM bitcode.60  ArgStringList LinkArgs{};61 62  for (auto Input : Inputs)63    LinkArgs.push_back(Input.getFilename());64 65  // Add static device libraries using the common helper function.66  // This handles unbundling archives (.a) containing bitcode bundles.67  StringRef Arch = getToolChain().getTriple().getArchName();68  StringRef Target =69      "generic"; // SPIR-V is generic, no specific target ID like -mcpu70  tools::AddStaticDeviceLibsLinking(C, *this, JA, Inputs, Args, LinkArgs, Arch,71                                    Target, /*IsBitCodeSDL=*/true);72  LinkArgs.append({"-o", TempFile});73  const char *LlvmLink =74      Args.MakeArgString(getToolChain().GetProgramPath("llvm-link"));75  C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),76                                         LlvmLink, LinkArgs, Inputs, Output));77 78  // Post-link HIP lowering.79 80  // Run LLVM IR passes to lower/expand/emulate HIP code that does not translate81  // to SPIR-V (E.g. dynamic shared memory).82  auto PassPluginPath = findPassPlugin(C.getDriver(), Args);83  if (!PassPluginPath.empty()) {84    const char *PassPathCStr = C.getArgs().MakeArgString(PassPluginPath);85    const char *OptOutput = HIP::getTempFile(C, Name + "-lower", "bc");86    ArgStringList OptArgs{TempFile,     "-load-pass-plugin",87                          PassPathCStr, "-passes=hip-post-link-passes",88                          "-o",         OptOutput};89    const char *Opt = Args.MakeArgString(getToolChain().GetProgramPath("opt"));90    C.addCommand(std::make_unique<Command>(91        JA, *this, ResponseFileSupport::None(), Opt, OptArgs, Inputs, Output));92    TempFile = OptOutput;93  }94 95  // Emit SPIR-V binary.96 97  llvm::opt::ArgStringList TrArgs{"--spirv-max-version=1.1",98                                  "--spirv-ext=+all"};99  InputInfo TrInput = InputInfo(types::TY_LLVM_BC, TempFile, "");100  SPIRV::constructTranslateCommand(C, *this, JA, Output, TrInput, TrArgs);101}102 103void HIPSPV::Linker::ConstructJob(Compilation &C, const JobAction &JA,104                                  const InputInfo &Output,105                                  const InputInfoList &Inputs,106                                  const ArgList &Args,107                                  const char *LinkingOutput) const {108  if (Inputs.size() > 0 && Inputs[0].getType() == types::TY_Image &&109      JA.getType() == types::TY_Object)110    return HIP::constructGenerateObjFileFromHIPFatBinary(C, Output, Inputs,111                                                         Args, JA, *this);112 113  if (JA.getType() == types::TY_HIP_FATBIN)114    return HIP::constructHIPFatbinCommand(C, JA, Output.getFilename(), Inputs,115                                          Args, *this);116 117  constructLinkAndEmitSpirvCommand(C, JA, Inputs, Output, Args);118}119 120HIPSPVToolChain::HIPSPVToolChain(const Driver &D, const llvm::Triple &Triple,121                                 const ToolChain &HostTC, const ArgList &Args)122    : ToolChain(D, Triple, Args), HostTC(HostTC) {123  // Lookup binaries into the driver directory, this is used to124  // discover the clang-offload-bundler executable.125  getProgramPaths().push_back(getDriver().Dir);126}127 128void HIPSPVToolChain::addClangTargetOptions(129    const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,130    Action::OffloadKind DeviceOffloadingKind) const {131  HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);132 133  assert(DeviceOffloadingKind == Action::OFK_HIP &&134         "Only HIP offloading kinds are supported for GPUs.");135 136  CC1Args.append(137      {"-fcuda-is-device", "-fcuda-allow-variadic-functions",138       // A crude workaround for llvm-spirv which does not handle the139       // autovectorized code well (vector reductions, non-i{8,16,32,64} types).140       // TODO: Allow autovectorization when SPIR-V backend arrives.141       "-mllvm", "-vectorize-loops=false", "-mllvm", "-vectorize-slp=false"});142 143  // Default to "hidden" visibility, as object level linking will not be144  // supported for the foreseeable future.145  if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,146                         options::OPT_fvisibility_ms_compat))147    CC1Args.append(148        {"-fvisibility=hidden", "-fapply-global-visibility-to-externs"});149 150  for (const BitCodeLibraryInfo &BCFile :151       getDeviceLibs(DriverArgs, DeviceOffloadingKind))152    CC1Args.append(153        {"-mlink-builtin-bitcode", DriverArgs.MakeArgString(BCFile.Path)});154}155 156Tool *HIPSPVToolChain::buildLinker() const {157  assert(getTriple().getArch() == llvm::Triple::spirv64);158  return new tools::HIPSPV::Linker(*this);159}160 161void HIPSPVToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {162  HostTC.addClangWarningOptions(CC1Args);163}164 165ToolChain::CXXStdlibType166HIPSPVToolChain::GetCXXStdlibType(const ArgList &Args) const {167  return HostTC.GetCXXStdlibType(Args);168}169 170void HIPSPVToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,171                                                ArgStringList &CC1Args) const {172  HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);173}174 175void HIPSPVToolChain::AddClangCXXStdlibIncludeArgs(176    const ArgList &Args, ArgStringList &CC1Args) const {177  HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args);178}179 180void HIPSPVToolChain::AddIAMCUIncludeArgs(const ArgList &Args,181                                          ArgStringList &CC1Args) const {182  HostTC.AddIAMCUIncludeArgs(Args, CC1Args);183}184 185void HIPSPVToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs,186                                        ArgStringList &CC1Args) const {187  if (!DriverArgs.hasFlag(options::OPT_offload_inc, options::OPT_no_offload_inc,188                          true))189    return;190 191  StringRef hipPath = DriverArgs.getLastArgValue(options::OPT_hip_path_EQ);192  if (hipPath.empty()) {193    getDriver().Diag(diag::err_drv_hipspv_no_hip_path);194    return;195  }196  SmallString<128> P(hipPath);197  llvm::sys::path::append(P, "include");198  CC1Args.append({"-isystem", DriverArgs.MakeArgString(P)});199}200 201llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>202HIPSPVToolChain::getDeviceLibs(203    const llvm::opt::ArgList &DriverArgs,204    const Action::OffloadKind DeviceOffloadingKind) const {205  llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12> BCLibs;206  if (!DriverArgs.hasFlag(options::OPT_offloadlib, options::OPT_no_offloadlib,207                          true))208    return {};209 210  ArgStringList LibraryPaths;211  // Find device libraries in --hip-device-lib-path and HIP_DEVICE_LIB_PATH.212  auto HipDeviceLibPathArgs = DriverArgs.getAllArgValues(213      // --hip-device-lib-path is alias to this option.214      options::OPT_rocm_device_lib_path_EQ);215  for (auto Path : HipDeviceLibPathArgs)216    LibraryPaths.push_back(DriverArgs.MakeArgString(Path));217 218  StringRef HipPath = DriverArgs.getLastArgValue(options::OPT_hip_path_EQ);219  if (!HipPath.empty()) {220    SmallString<128> Path(HipPath);221    llvm::sys::path::append(Path, "lib", "hip-device-lib");222    LibraryPaths.push_back(DriverArgs.MakeArgString(Path));223  }224 225  addDirectoryList(DriverArgs, LibraryPaths, "", "HIP_DEVICE_LIB_PATH");226 227  // Maintain compatability with --hip-device-lib.228  auto BCLibArgs = DriverArgs.getAllArgValues(options::OPT_hip_device_lib_EQ);229  if (!BCLibArgs.empty()) {230    bool Found = false;231    for (StringRef BCName : BCLibArgs) {232      StringRef FullName;233      for (std::string LibraryPath : LibraryPaths) {234        SmallString<128> Path(LibraryPath);235        llvm::sys::path::append(Path, BCName);236        FullName = Path;237        if (llvm::sys::fs::exists(FullName)) {238          BCLibs.emplace_back(FullName.str());239          Found = true;240          break;241        }242      }243      if (!Found)244        getDriver().Diag(diag::err_drv_no_such_file) << BCName;245    }246  } else {247    // Search device library named as 'hipspv-<triple>.bc'.248    auto TT = getTriple().normalize();249    std::string BCName = "hipspv-" + TT + ".bc";250    for (auto *LibPath : LibraryPaths) {251      SmallString<128> Path(LibPath);252      llvm::sys::path::append(Path, BCName);253      if (llvm::sys::fs::exists(Path)) {254        BCLibs.emplace_back(Path.str().str());255        return BCLibs;256      }257    }258    getDriver().Diag(diag::err_drv_no_hipspv_device_lib)259        << 1 << ("'" + TT + "' target");260    return {};261  }262 263  return BCLibs;264}265 266SanitizerMask HIPSPVToolChain::getSupportedSanitizers() const {267  // The HIPSPVToolChain only supports sanitizers in the sense that it allows268  // sanitizer arguments on the command line if they are supported by the host269  // toolchain. The HIPSPVToolChain will actually ignore any command line270  // arguments for any of these "supported" sanitizers. That means that no271  // sanitization of device code is actually supported at this time.272  //273  // This behavior is necessary because the host and device toolchains274  // invocations often share the command line, so the device toolchain must275  // tolerate flags meant only for the host toolchain.276  return HostTC.getSupportedSanitizers();277}278 279VersionTuple HIPSPVToolChain::computeMSVCVersion(const Driver *D,280                                                 const ArgList &Args) const {281  return HostTC.computeMSVCVersion(D, Args);282}283 284void HIPSPVToolChain::adjustDebugInfoKind(285    llvm::codegenoptions::DebugInfoKind &DebugInfoKind,286    const llvm::opt::ArgList &Args) const {287  // Debug info generation is disabled for SPIRV-LLVM-Translator288  // which currently aborts on the presence of DW_OP_LLVM_convert.289  // TODO: Enable debug info when the SPIR-V backend arrives.290  DebugInfoKind = llvm::codegenoptions::NoDebugInfo;291}292