#include "HIPSPV.h"
#include "CommonArgs.h"
#include "HIPUtility.h"
#include "clang/Driver/Compilation.h"
#include "clang/Driver/Driver.h"
#include "clang/Driver/DriverDiagnostic.h"
#include "clang/Driver/InputInfo.h"
#include "clang/Driver/Options.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
using namespace clang::driver;
using namespace clang::driver::toolchains;
using namespace clang::driver::tools;
using namespace clang;
using namespace llvm::opt;
static const char *getTempFile(Compilation &C, StringRef Prefix,
StringRef Extension) {
if (C.getDriver().isSaveTempsEnabled()) {
return C.getArgs().MakeArgString(Prefix + "." + Extension);
}
auto TmpFile = C.getDriver().GetTemporaryPath(Prefix, Extension);
return C.addTempFile(C.getArgs().MakeArgString(TmpFile));
}
static std::string findPassPlugin(const Driver &D,
const llvm::opt::ArgList &Args) {
StringRef Path = Args.getLastArgValue(options::OPT_hipspv_pass_plugin_EQ);
if (!Path.empty()) {
if (llvm::sys::fs::exists(Path))
return Path.str();
D.Diag(diag::err_drv_no_such_file) << Path;
}
StringRef hipPath = Args.getLastArgValue(options::OPT_hip_path_EQ);
if (!hipPath.empty()) {
SmallString<128> PluginPath(hipPath);
llvm::sys::path::append(PluginPath, "lib", "libLLVMHipSpvPasses.so");
if (llvm::sys::fs::exists(PluginPath))
return PluginPath.str().str();
PluginPath.assign(hipPath);
llvm::sys::path::append(PluginPath, "lib", "llvm",
"libLLVMHipSpvPasses.so");
if (llvm::sys::fs::exists(PluginPath))
return PluginPath.str().str();
}
return std::string();
}
void HIPSPV::Linker::constructLinkAndEmitSpirvCommand(
Compilation &C, const JobAction &JA, const InputInfoList &Inputs,
const InputInfo &Output, const llvm::opt::ArgList &Args) const {
assert(!Inputs.empty() && "Must have at least one input.");
std::string Name = std::string(llvm::sys::path::stem(Output.getFilename()));
const char *TempFile = getTempFile(C, Name + "-link", "bc");
ArgStringList LinkArgs{};
for (auto Input : Inputs)
LinkArgs.push_back(Input.getFilename());
LinkArgs.append({"-o", TempFile});
const char *LlvmLink =
Args.MakeArgString(getToolChain().GetProgramPath("llvm-link"));
C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
LlvmLink, LinkArgs, Inputs, Output));
auto PassPluginPath = findPassPlugin(C.getDriver(), Args);
if (!PassPluginPath.empty()) {
const char *PassPathCStr = C.getArgs().MakeArgString(PassPluginPath);
const char *OptOutput = getTempFile(C, Name + "-lower", "bc");
ArgStringList OptArgs{TempFile, "-load-pass-plugin",
PassPathCStr, "-passes=hip-post-link-passes",
"-o", OptOutput};
const char *Opt = Args.MakeArgString(getToolChain().GetProgramPath("opt"));
C.addCommand(std::make_unique<Command>(
JA, *this, ResponseFileSupport::None(), Opt, OptArgs, Inputs, Output));
TempFile = OptOutput;
}
llvm::opt::ArgStringList TrArgs{"--spirv-max-version=1.1",
"--spirv-ext=+all"};
InputInfo TrInput = InputInfo(types::TY_LLVM_BC, TempFile, "");
SPIRV::constructTranslateCommand(C, *this, JA, Output, TrInput, TrArgs);
}
void HIPSPV::Linker::ConstructJob(Compilation &C, const JobAction &JA,
const InputInfo &Output,
const InputInfoList &Inputs,
const ArgList &Args,
const char *LinkingOutput) const {
if (Inputs.size() > 0 && Inputs[0].getType() == types::TY_Image &&
JA.getType() == types::TY_Object)
return HIP::constructGenerateObjFileFromHIPFatBinary(C, Output, Inputs,
Args, JA, *this);
if (JA.getType() == types::TY_HIP_FATBIN)
return HIP::constructHIPFatbinCommand(C, JA, Output.getFilename(), Inputs,
Args, *this);
constructLinkAndEmitSpirvCommand(C, JA, Inputs, Output, Args);
}
HIPSPVToolChain::HIPSPVToolChain(const Driver &D, const llvm::Triple &Triple,
const ToolChain &HostTC, const ArgList &Args)
: ToolChain(D, Triple, Args), HostTC(HostTC) {
getProgramPaths().push_back(getDriver().Dir);
}
void HIPSPVToolChain::addClangTargetOptions(
const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
Action::OffloadKind DeviceOffloadingKind) const {
HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
assert(DeviceOffloadingKind == Action::OFK_HIP &&
"Only HIP offloading kinds are supported for GPUs.");
CC1Args.append(
{"-fcuda-is-device", "-fcuda-allow-variadic-functions",
"-mllvm", "-vectorize-loops=false", "-mllvm", "-vectorize-slp=false"});
if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals,
options::OPT_fno_cuda_approx_transcendentals, false))
CC1Args.push_back("-fcuda-approx-transcendentals");
if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
options::OPT_fvisibility_ms_compat))
CC1Args.append(
{"-fvisibility", "hidden", "-fapply-global-visibility-to-externs"});
llvm::for_each(getHIPDeviceLibs(DriverArgs),
[&](const BitCodeLibraryInfo &BCFile) {
CC1Args.append({"-mlink-builtin-bitcode",
DriverArgs.MakeArgString(BCFile.Path)});
});
}
Tool *HIPSPVToolChain::buildLinker() const {
assert(getTriple().getArch() == llvm::Triple::spirv64);
return new tools::HIPSPV::Linker(*this);
}
void HIPSPVToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
HostTC.addClangWarningOptions(CC1Args);
}
ToolChain::CXXStdlibType
HIPSPVToolChain::GetCXXStdlibType(const ArgList &Args) const {
return HostTC.GetCXXStdlibType(Args);
}
void HIPSPVToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
ArgStringList &CC1Args) const {
HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
}
void HIPSPVToolChain::AddClangCXXStdlibIncludeArgs(
const ArgList &Args, ArgStringList &CC1Args) const {
HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args);
}
void HIPSPVToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
ArgStringList &CC1Args) const {
HostTC.AddIAMCUIncludeArgs(Args, CC1Args);
}
void HIPSPVToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs,
ArgStringList &CC1Args) const {
if (DriverArgs.hasArg(options::OPT_nogpuinc))
return;
StringRef hipPath = DriverArgs.getLastArgValue(options::OPT_hip_path_EQ);
if (hipPath.empty()) {
getDriver().Diag(diag::err_drv_hipspv_no_hip_path) << 1 << "'-nogpuinc'";
return;
}
SmallString<128> P(hipPath);
llvm::sys::path::append(P, "include");
CC1Args.append({"-isystem", DriverArgs.MakeArgString(P)});
}
llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
HIPSPVToolChain::getHIPDeviceLibs(const llvm::opt::ArgList &DriverArgs) const {
llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12> BCLibs;
if (DriverArgs.hasArg(options::OPT_nogpulib))
return {};
ArgStringList LibraryPaths;
auto HipDeviceLibPathArgs = DriverArgs.getAllArgValues(
clang::driver::options::OPT_rocm_device_lib_path_EQ);
for (auto Path : HipDeviceLibPathArgs)
LibraryPaths.push_back(DriverArgs.MakeArgString(Path));
StringRef HipPath = DriverArgs.getLastArgValue(options::OPT_hip_path_EQ);
if (!HipPath.empty()) {
SmallString<128> Path(HipPath);
llvm::sys::path::append(Path, "lib", "hip-device-lib");
LibraryPaths.push_back(DriverArgs.MakeArgString(Path));
}
addDirectoryList(DriverArgs, LibraryPaths, "", "HIP_DEVICE_LIB_PATH");
auto BCLibArgs = DriverArgs.getAllArgValues(options::OPT_hip_device_lib_EQ);
if (!BCLibArgs.empty()) {
llvm::for_each(BCLibArgs, [&](StringRef BCName) {
StringRef FullName;
for (std::string LibraryPath : LibraryPaths) {
SmallString<128> Path(LibraryPath);
llvm::sys::path::append(Path, BCName);
FullName = Path;
if (llvm::sys::fs::exists(FullName)) {
BCLibs.emplace_back(FullName.str());
return;
}
}
getDriver().Diag(diag::err_drv_no_such_file) << BCName;
});
} else {
auto TT = getTriple().normalize();
std::string BCName = "hipspv-" + TT + ".bc";
for (auto *LibPath : LibraryPaths) {
SmallString<128> Path(LibPath);
llvm::sys::path::append(Path, BCName);
if (llvm::sys::fs::exists(Path)) {
BCLibs.emplace_back(Path.str().str());
return BCLibs;
}
}
getDriver().Diag(diag::err_drv_no_hipspv_device_lib)
<< 1 << ("'" + TT + "' target");
return {};
}
return BCLibs;
}
SanitizerMask HIPSPVToolChain::getSupportedSanitizers() const {
return HostTC.getSupportedSanitizers();
}
VersionTuple HIPSPVToolChain::computeMSVCVersion(const Driver *D,
const ArgList &Args) const {
return HostTC.computeMSVCVersion(D, Args);
}
void HIPSPVToolChain::adjustDebugInfoKind(
codegenoptions::DebugInfoKind &DebugInfoKind,
const llvm::opt::ArgList &Args) const {
DebugInfoKind = codegenoptions::NoDebugInfo;
}