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(options::Id: OPT_hipspv_pass_plugin_EQ); |
40 | if (!Path.empty()) { |
41 | if (llvm::sys::fs::exists(Path)) |
42 | return Path.str(); |
43 | D.Diag(diag::DiagID: err_drv_no_such_file) << Path; |
44 | } |
45 | |
46 | StringRef hipPath = Args.getLastArgValue(options::Id: 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(options::OPT_fvisibility_EQ, |
148 | options::OPT_fvisibility_ms_compat)) |
149 | CC1Args.append( |
150 | IL: {"-fvisibility=hidden" , "-fapply-global-visibility-to-externs" }); |
151 | |
152 | llvm::for_each(Range: getDeviceLibs(Args: DriverArgs), |
153 | F: [&](const BitCodeLibraryInfo &BCFile) { |
154 | CC1Args.append(IL: {"-mlink-builtin-bitcode" , |
155 | DriverArgs.MakeArgString(Str: BCFile.Path)}); |
156 | }); |
157 | } |
158 | |
159 | Tool *HIPSPVToolChain::buildLinker() const { |
160 | assert(getTriple().getArch() == llvm::Triple::spirv64); |
161 | return new tools::HIPSPV::Linker(*this); |
162 | } |
163 | |
164 | void HIPSPVToolChain::addClangWarningOptions(ArgStringList &CC1Args) const { |
165 | HostTC.addClangWarningOptions(CC1Args); |
166 | } |
167 | |
168 | ToolChain::CXXStdlibType |
169 | HIPSPVToolChain::GetCXXStdlibType(const ArgList &Args) const { |
170 | return HostTC.GetCXXStdlibType(Args); |
171 | } |
172 | |
173 | void HIPSPVToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
174 | ArgStringList &CC1Args) const { |
175 | HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args); |
176 | } |
177 | |
178 | void HIPSPVToolChain::AddClangCXXStdlibIncludeArgs( |
179 | const ArgList &Args, ArgStringList &CC1Args) const { |
180 | HostTC.AddClangCXXStdlibIncludeArgs(DriverArgs: Args, CC1Args); |
181 | } |
182 | |
183 | void HIPSPVToolChain::AddIAMCUIncludeArgs(const ArgList &Args, |
184 | ArgStringList &CC1Args) const { |
185 | HostTC.AddIAMCUIncludeArgs(DriverArgs: Args, CC1Args); |
186 | } |
187 | |
188 | void HIPSPVToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs, |
189 | ArgStringList &CC1Args) const { |
190 | if (DriverArgs.hasArg(options::OPT_nogpuinc)) |
191 | return; |
192 | |
193 | StringRef hipPath = DriverArgs.getLastArgValue(options::Id: OPT_hip_path_EQ); |
194 | if (hipPath.empty()) { |
195 | getDriver().Diag(diag::DiagID: err_drv_hipspv_no_hip_path); |
196 | return; |
197 | } |
198 | SmallString<128> P(hipPath); |
199 | llvm::sys::path::append(path&: P, a: "include" ); |
200 | CC1Args.append(IL: {"-isystem" , DriverArgs.MakeArgString(Str: P)}); |
201 | } |
202 | |
203 | llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12> |
204 | HIPSPVToolChain::getDeviceLibs(const llvm::opt::ArgList &DriverArgs) 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 | 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::Id: OPT_hip_path_EQ); |
219 | if (!HipPath.empty()) { |
220 | SmallString<128> Path(HipPath); |
221 | llvm::sys::path::append(path&: Path, a: "lib" , b: "hip-device-lib" ); |
222 | LibraryPaths.push_back(Elt: DriverArgs.MakeArgString(Str: Path)); |
223 | } |
224 | |
225 | addDirectoryList(Args: DriverArgs, CmdArgs&: LibraryPaths, ArgName: "" , EnvVar: "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 | llvm::for_each(BCLibArgs, [&](StringRef BCName) { |
231 | StringRef FullName; |
232 | for (std::string LibraryPath : LibraryPaths) { |
233 | SmallString<128> Path(LibraryPath); |
234 | llvm::sys::path::append(path&: Path, a: BCName); |
235 | FullName = Path; |
236 | if (llvm::sys::fs::exists(Path: FullName)) { |
237 | BCLibs.emplace_back(Args: FullName.str()); |
238 | return; |
239 | } |
240 | } |
241 | getDriver().Diag(diag::DiagID: 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(diag::DiagID: 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 | |