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