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