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 TC.addProfileRTLibs(Args, CmdArgs&: LldArgs);
137
138 // Look for archive of bundled bitcode in arguments, and add temporary files
139 // for the extracted archive of bitcode to inputs.
140 auto TargetID = Args.getLastArgValue(Id: options::OPT_mcpu_EQ);
141 AddStaticDeviceLibsLinking(C, T: *this, JA, Inputs, DriverArgs: Args, CmdArgs&: LldArgs, Arch: "amdgcn",
142 Target: TargetID, /*IsBitCodeSDL=*/isBitCodeSDL: true);
143
144 LldArgs.push_back(Elt: "--no-whole-archive");
145
146 const char *Lld = Args.MakeArgStringRef(Str: getToolChain().GetProgramPath(Name: "lld"));
147 C.addCommand(Cmd: std::make_unique<Command>(args: JA, args: *this, args: ResponseFileSupport::None(),
148 args&: Lld, args&: LldArgs, args: Inputs, args: Output));
149}
150
151// For SPIR-V the inputs for the job are device AMDGCN SPIR-V flavoured bitcode
152// and the output is either a compiled SPIR-V binary or bitcode (-emit-llvm). It
153// calls llvm-link and then the llvm-spirv translator or the SPIR-V BE.
154// TODO: consider if we want to run any targeted optimisations over IR here,
155// over generic SPIR-V.
156void AMDGCN::Linker::constructLinkAndEmitSpirvCommand(
157 Compilation &C, const JobAction &JA, const InputInfoList &Inputs,
158 const InputInfo &Output, const llvm::opt::ArgList &Args) const {
159 assert(!Inputs.empty() && "Must have at least one input.");
160
161 std::string LinkedBCFilePrefix(
162 Twine(llvm::sys::path::stem(path: Output.getFilename()), "-linked").str());
163 const char *LinkedBCFilePath = HIP::getTempFile(C, Prefix: LinkedBCFilePrefix, Extension: "bc");
164 InputInfo LinkedBCFile(&JA, LinkedBCFilePath, Output.getBaseInput());
165
166 bool UseSPIRVBackend = Args.hasFlag(Pos: options::OPT_use_spirv_backend,
167 Neg: options::OPT_no_use_spirv_backend,
168 /*Default=*/true);
169
170 constructLLVMLinkCommand(C, JA, Inputs, Output: LinkedBCFile, Args);
171
172 if (UseSPIRVBackend) {
173 // This code handles the case in the new driver when --offload-device-only
174 // is unset and clang-linker-wrapper forwards the bitcode that must be
175 // compiled to SPIR-V.
176
177 llvm::opt::ArgStringList CmdArgs;
178
179 CmdArgs.append(IL: {"-cc1", "-triple=spirv64-amd-amdhsa", "-emit-obj",
180 "-disable-llvm-optzns", "-mllvm", "-spirv-preserve-auxdata",
181 LinkedBCFile.getFilename(), "-o", Output.getFilename()});
182
183 const Driver &Driver = getToolChain().getDriver();
184 const char *Exec = Driver.getDriverProgramPath();
185 C.addCommand(Cmd: std::make_unique<Command>(
186 args: JA, args: *this, args: ResponseFileSupport::None(), args&: Exec, args&: CmdArgs, args&: LinkedBCFile,
187 args: Output, args: Driver.getPrependArg()));
188 } else {
189 // Emit SPIR-V binary using the translator
190 llvm::opt::ArgStringList TrArgs{
191 "--spirv-max-version=1.6",
192 "--spirv-ext=+all",
193 "--spirv-allow-unknown-intrinsics",
194 "--spirv-lower-const-expr",
195 "--spirv-preserve-auxdata",
196 "--spirv-debug-info-version=nonsemantic-shader-200"};
197 SPIRV::constructTranslateCommand(C, T: *this, JA, Output, Input: LinkedBCFile,
198 Args: TrArgs);
199 }
200}
201
202// For amdgcn the inputs of the linker job are device bitcode and output is
203// either an object file or bitcode (-emit-llvm). It calls llvm-link, opt,
204// llc, then lld steps.
205void AMDGCN::Linker::ConstructJob(Compilation &C, const JobAction &JA,
206 const InputInfo &Output,
207 const InputInfoList &Inputs,
208 const ArgList &Args,
209 const char *LinkingOutput) const {
210 if (!Inputs.empty() && Inputs[0].getType() == types::TY_Image &&
211 JA.getType() == types::TY_Object)
212 return HIP::constructGenerateObjFileFromHIPFatBinary(C, Output, Inputs,
213 Args, JA, T: *this);
214
215 if (JA.getType() == types::TY_HIP_FATBIN)
216 return HIP::constructHIPFatbinCommand(C, JA, OutputFileName: Output.getFilename(), Inputs,
217 TCArgs: Args, T: *this);
218
219 if (JA.getType() == types::TY_LLVM_BC)
220 return constructLLVMLinkCommand(C, JA, Inputs, Output, Args);
221
222 if (getToolChain().getEffectiveTriple().isSPIRV())
223 return constructLinkAndEmitSpirvCommand(C, JA, Inputs, Output, Args);
224
225 return constructLldCommand(C, JA, Inputs, Output, Args);
226}
227
228SPIRVAMDToolChain::SPIRVAMDToolChain(const Driver &D,
229 const llvm::Triple &Triple,
230 const llvm::opt::ArgList &Args)
231 : AMDGPUToolChain(D, Triple, Args) {}
232
233Tool *SPIRVAMDToolChain::buildLinker() const {
234 return new tools::AMDGCN::Linker(*this);
235}
236