1//===--- HIPAMD.cpp - HIP Tool and ToolChain Implementations ----*- 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 "HIPAMD.h"
10#include "AMDGPU.h"
11#include "HIPUtility.h"
12#include "SPIRV.h"
13#include "clang/Basic/Cuda.h"
14#include "clang/Driver/CommonArgs.h"
15#include "clang/Driver/Compilation.h"
16#include "clang/Driver/Driver.h"
17#include "clang/Driver/InputInfo.h"
18#include "clang/Driver/SanitizerArgs.h"
19#include "clang/Options/Options.h"
20#include "llvm/Support/FileSystem.h"
21#include "llvm/Support/Path.h"
22#include "llvm/TargetParser/TargetParser.h"
23
24using namespace clang::driver;
25using namespace clang::driver::toolchains;
26using namespace clang::driver::tools;
27using namespace clang;
28using namespace llvm::opt;
29
30#if defined(_WIN32) || defined(_WIN64)
31#define NULL_FILE "nul"
32#else
33#define NULL_FILE "/dev/null"
34#endif
35
36void AMDGCN::Linker::constructLLVMLinkCommand(
37 Compilation &C, const JobAction &JA, const InputInfoList &Inputs,
38 const InputInfo &Output, const llvm::opt::ArgList &Args) const {
39
40 ArgStringList LinkerInputs;
41
42 for (auto Input : Inputs)
43 if (Input.isFilename())
44 LinkerInputs.push_back(Elt: Input.getFilename());
45
46 // Look for archive of bundled bitcode in arguments, and add temporary files
47 // for the extracted archive of bitcode to inputs.
48 auto TargetID = Args.getLastArgValue(Id: options::OPT_mcpu_EQ);
49 AddStaticDeviceLibsLinking(C, T: *this, JA, Inputs, DriverArgs: Args, CmdArgs&: LinkerInputs, Arch: "amdgcn",
50 Target: TargetID, /*IsBitCodeSDL=*/isBitCodeSDL: true);
51 tools::constructLLVMLinkCommand(C, T: *this, JA, JobInputs: Inputs, LinkerInputs, Output,
52 Args);
53}
54
55void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA,
56 const InputInfoList &Inputs,
57 const InputInfo &Output,
58 const llvm::opt::ArgList &Args) const {
59 // Construct lld command.
60 // The output from ld.lld is an HSA code object file.
61 ArgStringList LldArgs{"-flavor",
62 "gnu",
63 "-m",
64 "elf64_amdgpu",
65 "--no-undefined",
66 "-shared",
67 "-plugin-opt=-amdgpu-internalize-symbols"};
68 if (Args.hasArg(Ids: options::OPT_hipstdpar))
69 LldArgs.push_back(Elt: "-plugin-opt=-amdgpu-enable-hipstdpar");
70
71 auto &TC = getToolChain();
72 auto &D = TC.getDriver();
73 bool IsThinLTO = TC.getLTOMode(Args, Kind: Action::OFK_HIP) == LTOK_Thin;
74 addLTOOptions(ToolChain: TC, Args, CmdArgs&: LldArgs, Output, Inputs, IsThinLTO);
75
76 // Extract all the -m options
77 std::vector<llvm::StringRef> Features;
78 amdgpu::getAMDGPUTargetFeatures(D, Triple: TC.getEffectiveTriple(), Args, Features);
79
80 // Add features to mattr such as cumode
81 std::string MAttrString = "-plugin-opt=-mattr=";
82 for (auto OneFeature : unifyTargetFeatures(Features)) {
83 MAttrString.append(s: Args.MakeArgStringRef(Str: OneFeature));
84 if (OneFeature != Features.back())
85 MAttrString.append(s: ",");
86 }
87 if (!Features.empty())
88 LldArgs.push_back(Elt: Args.MakeArgString(Str: MAttrString));
89
90 // ToDo: Remove this option after AMDGPU backend supports ISA-level linking.
91 // Since AMDGPU backend currently does not support ISA-level linking, all
92 // called functions need to be imported.
93 if (IsThinLTO) {
94 LldArgs.push_back(Elt: "-plugin-opt=-force-import-all");
95 LldArgs.push_back(Elt: "-plugin-opt=-avail-extern-to-local");
96 LldArgs.push_back(Elt: "-plugin-opt=-avail-extern-gv-in-addrspace-to-local=3");
97 }
98
99 for (const Arg *A : Args.filtered(Ids: options::OPT_mllvm)) {
100 LldArgs.push_back(
101 Elt: Args.MakeArgString(Str: Twine("-plugin-opt=") + A->getValue(N: 0)));
102 }
103
104 if (C.getDriver().isSaveTempsEnabled())
105 LldArgs.push_back(Elt: "-save-temps");
106
107 addLinkerCompressDebugSectionsOption(TC, Args, CmdArgs&: LldArgs);
108
109 // Given that host and device linking happen in separate processes, the device
110 // linker doesn't always have the visibility as to which device symbols are
111 // needed by a program, especially for the device symbol dependencies that are
112 // introduced through the host symbol resolution.
113 // For example: host_A() (A.obj) --> host_B(B.obj) --> device_kernel_B()
114 // (B.obj) In this case, the device linker doesn't know that A.obj actually
115 // depends on the kernel functions in B.obj. When linking to static device
116 // library, the device linker may drop some of the device global symbols if
117 // they aren't referenced. As a workaround, we are adding to the
118 // --whole-archive flag such that all global symbols would be linked in.
119 LldArgs.push_back(Elt: "--whole-archive");
120
121 for (auto *Arg : Args.filtered(Ids: options::OPT_Xoffload_linker)) {
122 StringRef ArgVal = Arg->getValue(N: 1);
123 auto SplitArg = ArgVal.split(Separator: "-mllvm=");
124 if (!SplitArg.second.empty()) {
125 LldArgs.push_back(
126 Elt: Args.MakeArgString(Str: Twine("-plugin-opt=") + SplitArg.second));
127 } else {
128 LldArgs.push_back(Elt: Args.MakeArgStringRef(Str: ArgVal));
129 }
130 Arg->claim();
131 }
132
133 LldArgs.append(IL: {"-o", Output.getFilename()});
134 for (auto Input : Inputs)
135 LldArgs.push_back(Elt: Input.getFilename());
136
137 // Look for archive of bundled bitcode in arguments, and add temporary files
138 // for the extracted archive of bitcode to inputs.
139 auto TargetID = Args.getLastArgValue(Id: options::OPT_mcpu_EQ);
140 AddStaticDeviceLibsLinking(C, T: *this, JA, Inputs, DriverArgs: Args, CmdArgs&: LldArgs, Arch: "amdgcn",
141 Target: TargetID, /*IsBitCodeSDL=*/isBitCodeSDL: true);
142
143 LldArgs.push_back(Elt: "--no-whole-archive");
144
145 const char *Lld = Args.MakeArgStringRef(Str: getToolChain().GetProgramPath(Name: "lld"));
146 C.addCommand(Cmd: std::make_unique<Command>(args: JA, args: *this, args: ResponseFileSupport::None(),
147 args&: Lld, args&: LldArgs, args: Inputs, args: Output));
148}
149
150// For SPIR-V the inputs for the job are device AMDGCN SPIR-V flavoured bitcode
151// and the output is either a compiled SPIR-V binary or bitcode (-emit-llvm). It
152// calls llvm-link and then the llvm-spirv translator or the SPIR-V BE.
153// TODO: consider if we want to run any targeted optimisations over IR here,
154// over generic SPIR-V.
155void AMDGCN::Linker::constructLinkAndEmitSpirvCommand(
156 Compilation &C, const JobAction &JA, const InputInfoList &Inputs,
157 const InputInfo &Output, const llvm::opt::ArgList &Args) const {
158 assert(!Inputs.empty() && "Must have at least one input.");
159
160 std::string LinkedBCFilePrefix(
161 Twine(llvm::sys::path::stem(path: Output.getFilename()), "-linked").str());
162 const char *LinkedBCFilePath = HIP::getTempFile(C, Prefix: LinkedBCFilePrefix, Extension: "bc");
163 InputInfo LinkedBCFile(&JA, LinkedBCFilePath, Output.getBaseInput());
164
165 bool UseSPIRVBackend = Args.hasFlag(Pos: options::OPT_use_spirv_backend,
166 Neg: options::OPT_no_use_spirv_backend,
167 /*Default=*/true);
168
169 constructLLVMLinkCommand(C, JA, Inputs, Output: LinkedBCFile, Args);
170
171 if (UseSPIRVBackend) {
172 // This code handles the case in the new driver when --offload-device-only
173 // is unset and clang-linker-wrapper forwards the bitcode that must be
174 // compiled to SPIR-V.
175
176 llvm::opt::ArgStringList CmdArgs;
177
178 CmdArgs.append(IL: {"-cc1", "-triple=spirv64-amd-amdhsa", "-emit-obj",
179 "-disable-llvm-optzns", LinkedBCFile.getFilename(), "-o",
180 Output.getFilename()});
181
182 const Driver &Driver = getToolChain().getDriver();
183 const char *Exec = Driver.getDriverProgramPath();
184 C.addCommand(Cmd: std::make_unique<Command>(
185 args: JA, args: *this, args: ResponseFileSupport::None(), args&: Exec, args&: CmdArgs, args&: LinkedBCFile,
186 args: Output, args: Driver.getPrependArg()));
187 } else {
188 // Emit SPIR-V binary using the translator
189 llvm::opt::ArgStringList TrArgs{
190 "--spirv-max-version=1.6",
191 "--spirv-ext=+all",
192 "--spirv-allow-unknown-intrinsics",
193 "--spirv-lower-const-expr",
194 "--spirv-preserve-auxdata",
195 "--spirv-debug-info-version=nonsemantic-shader-200"};
196 SPIRV::constructTranslateCommand(C, T: *this, JA, Output, Input: LinkedBCFile,
197 Args: TrArgs);
198 }
199}
200
201// For amdgcn the inputs of the linker job are device bitcode and output is
202// either an object file or bitcode (-emit-llvm). It calls llvm-link, opt,
203// llc, then lld steps.
204void AMDGCN::Linker::ConstructJob(Compilation &C, const JobAction &JA,
205 const InputInfo &Output,
206 const InputInfoList &Inputs,
207 const ArgList &Args,
208 const char *LinkingOutput) const {
209 if (!Inputs.empty() && Inputs[0].getType() == types::TY_Image &&
210 JA.getType() == types::TY_Object)
211 return HIP::constructGenerateObjFileFromHIPFatBinary(C, Output, Inputs,
212 Args, JA, T: *this);
213
214 if (JA.getType() == types::TY_HIP_FATBIN)
215 return HIP::constructHIPFatbinCommand(C, JA, OutputFileName: Output.getFilename(), Inputs,
216 TCArgs: Args, T: *this);
217
218 if (JA.getType() == types::TY_LLVM_BC)
219 return constructLLVMLinkCommand(C, JA, Inputs, Output, Args);
220
221 if (getToolChain().getEffectiveTriple().isSPIRV())
222 return constructLinkAndEmitSpirvCommand(C, JA, Inputs, Output, Args);
223
224 return constructLldCommand(C, JA, Inputs, Output, Args);
225}
226
227HIPAMDToolChain::HIPAMDToolChain(const Driver &D, const llvm::Triple &Triple,
228 const ToolChain &HostTC, const ArgList &Args)
229 : ROCMToolChain(D, Triple, Args), HostTC(HostTC) {
230 // Lookup binaries into the driver directory, this is used to
231 // discover the clang-offload-bundler executable.
232 getProgramPaths().push_back(Elt: getDriver().Dir);
233}
234
235void HIPAMDToolChain::addClangTargetOptions(
236 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
237 BoundArch BA, Action::OffloadKind DeviceOffloadingKind) const {
238 assert(DeviceOffloadingKind == Action::OFK_HIP &&
239 "Only HIP offloading kinds are supported for GPUs.");
240
241 CC1Args.append(IL: {"-fcuda-is-device", "-fno-threadsafe-statics"});
242
243 if (!DriverArgs.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc,
244 Default: false)) {
245 CC1Args.append(IL: {"-mllvm", "-amdgpu-internalize-symbols"});
246 if (DriverArgs.hasArgNoClaim(Ids: options::OPT_hipstdpar))
247 CC1Args.append(IL: {"-mllvm", "-amdgpu-enable-hipstdpar"});
248 }
249
250 StringRef MaxThreadsPerBlock =
251 DriverArgs.getLastArgValue(Id: options::OPT_gpu_max_threads_per_block_EQ);
252 if (!MaxThreadsPerBlock.empty()) {
253 CC1Args.push_back(Elt: DriverArgs.MakeArgString(
254 Str: Twine("--gpu-max-threads-per-block=") + MaxThreadsPerBlock));
255 }
256
257 // Default to "hidden" visibility, as object level linking will not be
258 // supported for the foreseeable future.
259 // TODO: remove the SPIR-V bypass once it can encode (hidden) visibility.
260 if (!DriverArgs.hasArg(Ids: options::OPT_fvisibility_EQ,
261 Ids: options::OPT_fvisibility_ms_compat) &&
262 !getEffectiveTriple().isSPIRV() && !getDriver().IsFlangMode()) {
263 CC1Args.append(IL: {"-fvisibility=hidden"});
264 CC1Args.push_back(Elt: "-fapply-global-visibility-to-externs");
265 }
266
267 if (getEffectiveTriple().isSPIRV()) {
268 // For SPIR-V we embed the command-line into the generated binary, in order
269 // to retrieve it at JIT time and be able to do target specific compilation
270 // with options that match the user-supplied ones.
271 if (!DriverArgs.hasArg(Ids: options::OPT_fembed_bitcode_marker))
272 CC1Args.push_back(Elt: "-fembed-bitcode=marker");
273 // For SPIR-V we want to retain the pristine output of Clang CodeGen, since
274 // optimizations might lose structure / information that is necessary for
275 // generating optimal concrete AMDGPU code. We duplicate this because the
276 // HIP TC doesn't invoke the base AMDGPU TC addClangTargetOptions.
277 if (!DriverArgs.hasArg(Ids: options::OPT_disable_llvm_passes))
278 CC1Args.push_back(Elt: "-disable-llvm-passes");
279 return; // No DeviceLibs for SPIR-V.
280 }
281
282 for (auto BCFile : getDeviceLibs(Args: DriverArgs, BA, DeviceOffloadKind: DeviceOffloadingKind)) {
283 CC1Args.push_back(Elt: BCFile.ShouldInternalize ? "-mlink-builtin-bitcode"
284 : "-mlink-bitcode-file");
285 CC1Args.push_back(Elt: DriverArgs.MakeArgStringRef(Str: BCFile.Path));
286 }
287}
288
289llvm::opt::DerivedArgList *
290HIPAMDToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
291 BoundArch BA,
292 Action::OffloadKind DeviceOffloadKind) const {
293 llvm::opt::DerivedArgList *DAL =
294 ROCMToolChain::TranslateArgs(Args, BA, DeviceOffloadKind);
295
296 return DAL;
297}
298
299Tool *HIPAMDToolChain::buildLinker() const {
300 assert(getTriple().isAMDGCN() ||
301 getTriple().getArch() == llvm::Triple::spirv64);
302 return new tools::AMDGCN::Linker(*this);
303}
304
305ToolChain::CXXStdlibType
306HIPAMDToolChain::GetCXXStdlibType(const ArgList &Args) const {
307 return HostTC.GetCXXStdlibType(Args);
308}
309
310void HIPAMDToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
311 ArgStringList &CC1Args) const {
312 HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
313}
314
315void HIPAMDToolChain::AddClangCXXStdlibIncludeArgs(
316 const ArgList &Args, ArgStringList &CC1Args) const {
317 HostTC.AddClangCXXStdlibIncludeArgs(DriverArgs: Args, CC1Args);
318}
319
320void HIPAMDToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
321 ArgStringList &CC1Args) const {
322 HostTC.AddIAMCUIncludeArgs(DriverArgs: Args, CC1Args);
323}
324
325void HIPAMDToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs,
326 ArgStringList &CC1Args) const {
327 RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args);
328}
329
330VersionTuple HIPAMDToolChain::computeMSVCVersion(const Driver *D,
331 const ArgList &Args) const {
332 return HostTC.computeMSVCVersion(D, Args);
333}
334
335llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
336HIPAMDToolChain::getDeviceLibs(const llvm::opt::ArgList &DriverArgs,
337 BoundArch BA,
338 Action::OffloadKind DeviceOffloadingKind) const {
339 assert(BA && "Must have an explicit GPU arch.");
340
341 llvm::SmallVector<BitCodeLibraryInfo, 12> BCLibs;
342 const llvm::Triple &TT = getEffectiveTriple();
343
344 if (!DriverArgs.hasFlag(Pos: options::OPT_offloadlib, Neg: options::OPT_no_offloadlib,
345 Default: true) ||
346 TT.getEnvironment() == llvm::Triple::LLVM)
347 return {};
348
349 StringRef GpuArch = getProcessorFromTargetID(T: getTriple(), OffloadArch: BA.ArchName);
350 if (GpuArch == "amdgcnspirv")
351 return {};
352
353 ArgStringList LibraryPaths;
354
355 // Find in --hip-device-lib-path and HIP_LIBRARY_PATH.
356 for (StringRef Path : RocmInstallation->getRocmDeviceLibPathArg())
357 LibraryPaths.push_back(Elt: DriverArgs.MakeArgStringRef(Str: Path));
358
359 addDirectoryList(Args: DriverArgs, CmdArgs&: LibraryPaths, ArgName: "", EnvVar: "HIP_DEVICE_LIB_PATH");
360
361 // Maintain compatability with --hip-device-lib.
362 auto BCLibArgs = DriverArgs.getAllArgValues(Id: options::OPT_hip_device_lib_EQ);
363 if (!BCLibArgs.empty()) {
364 for (StringRef BCName : BCLibArgs) {
365 StringRef FullName;
366 bool Found = false;
367 for (StringRef LibraryPath : LibraryPaths) {
368 SmallString<128> Path(LibraryPath);
369 llvm::sys::path::append(path&: Path, a: BCName);
370 FullName = Path;
371 if (llvm::sys::fs::exists(Path: FullName)) {
372 BCLibs.emplace_back(Args&: FullName);
373 Found = true;
374 break;
375 }
376 }
377 if (!Found)
378 getDriver().Diag(DiagID: diag::err_drv_no_such_file) << BCName;
379 }
380 } else {
381 if (!RocmInstallation->hasDeviceLibrary()) {
382 getDriver().Diag(DiagID: diag::err_drv_no_rocm_device_lib) << 0;
383 return {};
384 }
385
386 // Add common device libraries like ocml etc.
387 for (auto N : getCommonDeviceLibNames(DriverArgs, TargetID: BA.ArchName, GPUArch: GpuArch,
388 DeviceOffloadingKind))
389 BCLibs.emplace_back(Args&: N);
390
391 // Add instrument lib.
392 auto InstLib =
393 DriverArgs.getLastArgValue(Id: options::OPT_gpu_instrument_lib_EQ);
394 if (InstLib.empty())
395 return BCLibs;
396 if (llvm::sys::fs::exists(Path: InstLib))
397 BCLibs.emplace_back(Args&: InstLib);
398 else
399 getDriver().Diag(DiagID: diag::err_drv_no_such_file) << InstLib;
400 }
401
402 return BCLibs;
403}
404
405HIPAMDToolChain::ParsedTargetIDType
406HIPAMDToolChain::checkTargetID(const llvm::opt::ArgList &DriverArgs) const {
407 auto PTID = getParsedTargetID(DriverArgs);
408 if (PTID.OptionalTargetID && !PTID.OptionalGPUArch &&
409 PTID.OptionalTargetID != "amdgcnspirv")
410 getDriver().Diag(DiagID: clang::diag::err_drv_bad_target_id)
411 << *PTID.OptionalTargetID;
412 return PTID;
413}
414
415SPIRVAMDToolChain::SPIRVAMDToolChain(const Driver &D,
416 const llvm::Triple &Triple,
417 const ArgList &Args)
418 : ROCMToolChain(D, Triple, Args) {
419 getProgramPaths().push_back(Elt: getDriver().Dir);
420}
421
422Tool *SPIRVAMDToolChain::buildLinker() const {
423 assert(getTriple().getArch() == llvm::Triple::spirv64);
424 return new tools::AMDGCN::Linker(*this);
425}
426