clang 22.0.0git
HIPSPV.cpp
Go to the documentation of this file.
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"
13#include "clang/Driver/Driver.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 -mcpu
70 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 translate
81 // 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
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)
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 to
124 // discover the clang-offload-bundler executable.
125 getProgramPaths().push_back(getDriver().Dir);
126}
127
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 the
139 // 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 be
144 // 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
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
166HIPSPVToolChain::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
176 const ArgList &Args, ArgStringList &CC1Args) const {
177 HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args);
178}
179
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
203 const llvm::opt::ArgList &DriverArgs,
204 const Action::OffloadKind DeviceOffloadingKind) const {
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 clang::driver::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
267 // The HIPSPVToolChain only supports sanitizers in the sense that it allows
268 // sanitizer arguments on the command line if they are supported by the host
269 // toolchain. The HIPSPVToolChain will actually ignore any command line
270 // arguments for any of these "supported" sanitizers. That means that no
271 // sanitization of device code is actually supported at this time.
272 //
273 // This behavior is necessary because the host and device toolchains
274 // invocations often share the command line, so the device toolchain must
275 // tolerate flags meant only for the host toolchain.
276 return HostTC.getSupportedSanitizers();
277}
278
280 const ArgList &Args) const {
281 return HostTC.computeMSVCVersion(D, Args);
282}
283
285 llvm::codegenoptions::DebugInfoKind &DebugInfoKind,
286 const llvm::opt::ArgList &Args) const {
287 // Debug info generation is disabled for SPIRV-LLVM-Translator
288 // 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}
static std::string findPassPlugin(const Driver &D, const llvm::opt::ArgList &Args)
Definition HIPSPV.cpp:26
types::ID getType() const
Definition Action.h:150
Compilation - A set of tasks to perform for a single driver invocation.
Definition Compilation.h:45
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition Driver.h:99
DiagnosticBuilder Diag(unsigned DiagID) const
Definition Driver.h:169
InputInfo - Wrapper for information about an input source.
Definition InputInfo.h:22
const char * getFilename() const
Definition InputInfo.h:83
llvm::Triple::ArchType getArch() const
Definition ToolChain.h:269
const Driver & getDriver() const
Definition ToolChain.h:253
ToolChain(const Driver &D, const llvm::Triple &T, const llvm::opt::ArgList &Args)
Definition ToolChain.cpp:89
path_list & getProgramPaths()
Definition ToolChain.h:298
const llvm::Triple & getTriple() const
Definition ToolChain.h:255
Tool - Information on a specific compilation tool.
Definition Tool.h:32
const ToolChain & getToolChain() const
Definition Tool.h:52
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, Action::OffloadKind DeviceOffloadKind) const override
Add options that need to be passed to cc1 for this target.
Definition HIPSPV.cpp:128
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition HIPSPV.cpp:170
void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const override
Add warning options that need to be passed to cc1 for this target.
Definition HIPSPV.cpp:161
Tool * buildLinker() const override
Definition HIPSPV.cpp:156
llvm::SmallVector< BitCodeLibraryInfo, 12 > getDeviceLibs(const llvm::opt::ArgList &Args, const Action::OffloadKind DeviceOffloadKind) const override
Get paths for device libraries.
Definition HIPSPV.cpp:202
HIPSPVToolChain(const Driver &D, const llvm::Triple &Triple, const ToolChain &HostTC, const llvm::opt::ArgList &Args)
Definition HIPSPV.cpp:120
void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add arguments to use system-specific HIP includes.
Definition HIPSPV.cpp:185
CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const override
Definition HIPSPV.cpp:166
void AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CC1Args) const override
AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set the include paths to use for...
Definition HIPSPV.cpp:175
VersionTuple computeMSVCVersion(const Driver *D, const llvm::opt::ArgList &Args) const override
On Windows, returns the MSVC compatibility version.
Definition HIPSPV.cpp:279
void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add arguments to use MCU GCC toolchain includes.
Definition HIPSPV.cpp:180
void adjustDebugInfoKind(llvm::codegenoptions::DebugInfoKind &DebugInfoKind, const llvm::opt::ArgList &Args) const override
Adjust debug information kind considering all passed options.
Definition HIPSPV.cpp:284
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition HIPSPV.cpp:266
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition HIPSPV.cpp:103
void constructHIPFatbinCommand(Compilation &C, const JobAction &JA, StringRef OutputFileName, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const Tool &T)
const char * getTempFile(Compilation &C, StringRef Prefix, StringRef Extension)
void constructGenerateObjFileFromHIPFatBinary(Compilation &C, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, const JobAction &JA, const Tool &T)
void constructTranslateCommand(Compilation &C, const Tool &T, const JobAction &JA, const InputInfo &Output, const InputInfo &Input, const llvm::opt::ArgStringList &Args)
Definition SPIRV.cpp:20
void AddStaticDeviceLibsLinking(Compilation &C, const Tool &T, const JobAction &JA, const InputInfoList &Inputs, const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CmdArgs, StringRef Arch, StringRef Target, bool isBitCodeSDL)
void addDirectoryList(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const char *ArgName, const char *EnvVar)
EnvVar is split by system delimiter for environment variables.
SmallVector< InputInfo, 4 > InputInfoList
Definition Driver.h:50
The JSON file list parser is used to communicate input to InstallAPI.
static constexpr ResponseFileSupport None()
Returns a ResponseFileSupport indicating that response files are not supported.
Definition Job.h:78