clang 24.0.0git
HIPAMD.cpp
Go to the documentation of this file.
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"
16#include "clang/Driver/Driver.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(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(options::OPT_mcpu_EQ);
49 AddStaticDeviceLibsLinking(C, *this, JA, Inputs, Args, LinkerInputs, "amdgcn",
50 TargetID, /*IsBitCodeSDL=*/true);
51 tools::constructLLVMLinkCommand(C, *this, JA, 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(options::OPT_hipstdpar))
69 LldArgs.push_back("-plugin-opt=-amdgpu-enable-hipstdpar");
70
71 auto &TC = getToolChain();
72 auto &D = TC.getDriver();
73 bool IsThinLTO = TC.getLTOMode(Args, Action::OFK_HIP) == LTOK_Thin;
74 addLTOOptions(TC, Args, LldArgs, Output, Inputs, IsThinLTO);
75
76 // Extract all the -m options
77 std::vector<llvm::StringRef> Features;
78 amdgpu::getAMDGPUTargetFeatures(D, 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(Args.MakeArgStringRef(OneFeature));
84 if (OneFeature != Features.back())
85 MAttrString.append(",");
86 }
87 if (!Features.empty())
88 LldArgs.push_back(Args.MakeArgString(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("-plugin-opt=-force-import-all");
95 LldArgs.push_back("-plugin-opt=-avail-extern-to-local");
96 LldArgs.push_back("-plugin-opt=-avail-extern-gv-in-addrspace-to-local=3");
97 }
98
99 for (const Arg *A : Args.filtered(options::OPT_mllvm)) {
100 LldArgs.push_back(
101 Args.MakeArgString(Twine("-plugin-opt=") + A->getValue(0)));
102 }
103
104 if (C.getDriver().isSaveTempsEnabled())
105 LldArgs.push_back("-save-temps");
106
107 addLinkerCompressDebugSectionsOption(TC, Args, 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("--whole-archive");
120
121 for (auto *Arg : Args.filtered(options::OPT_Xoffload_linker)) {
122 StringRef ArgVal = Arg->getValue(1);
123 auto SplitArg = ArgVal.split("-mllvm=");
124 if (!SplitArg.second.empty()) {
125 LldArgs.push_back(
126 Args.MakeArgString(Twine("-plugin-opt=") + SplitArg.second));
127 } else {
128 LldArgs.push_back(Args.MakeArgStringRef(ArgVal));
129 }
130 Arg->claim();
131 }
132
133 LldArgs.append({"-o", Output.getFilename()});
134 for (auto Input : Inputs)
135 LldArgs.push_back(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(options::OPT_mcpu_EQ);
140 AddStaticDeviceLibsLinking(C, *this, JA, Inputs, Args, LldArgs, "amdgcn",
141 TargetID, /*IsBitCodeSDL=*/true);
142
143 LldArgs.push_back("--no-whole-archive");
144
145 const char *Lld = Args.MakeArgStringRef(getToolChain().GetProgramPath("lld"));
146 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
147 Lld, LldArgs, Inputs, 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(Output.getFilename()), "-linked").str());
162 const char *LinkedBCFilePath = HIP::getTempFile(C, LinkedBCFilePrefix, "bc");
163 InputInfo LinkedBCFile(&JA, LinkedBCFilePath, Output.getBaseInput());
164
165 bool UseSPIRVBackend = Args.hasFlag(options::OPT_use_spirv_backend,
166 options::OPT_no_use_spirv_backend,
167 /*Default=*/true);
168
169 constructLLVMLinkCommand(C, JA, Inputs, 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({"-cc1", "-triple=spirv64-amd-amdhsa", "-emit-obj",
179 "-disable-llvm-optzns", "-mllvm", "-spirv-preserve-auxdata",
180 LinkedBCFile.getFilename(), "-o", Output.getFilename()});
181
182 const Driver &Driver = getToolChain().getDriver();
183 const char *Exec = Driver.getDriverProgramPath();
184 C.addCommand(std::make_unique<Command>(
185 JA, *this, ResponseFileSupport::None(), Exec, CmdArgs, LinkedBCFile,
186 Output, 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, *this, JA, Output, LinkedBCFile,
197 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.
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)
212 Args, JA, *this);
213
214 if (JA.getType() == types::TY_HIP_FATBIN)
215 return HIP::constructHIPFatbinCommand(C, JA, Output.getFilename(), Inputs,
216 Args, *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
228 const llvm::Triple &Triple,
229 const llvm::opt::ArgList &Args)
230 : AMDGPUToolChain(D, Triple, Args) {}
231
233 return new tools::AMDGCN::Linker(*this);
234}
types::ID getType() const
Definition Action.h:154
Compilation - A set of tasks to perform for a single driver invocation.
Definition Compilation.h:46
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition Driver.h:95
InputInfo - Wrapper for information about an input source.
Definition InputInfo.h:22
const char * getBaseInput() const
Definition InputInfo.h:78
const char * getFilename() const
Definition InputInfo.h:83
Tool - Information on a specific compilation tool.
Definition Tool.h:32
const ToolChain & getToolChain() const
Definition Tool.h:52
AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args, const ToolChain *HostTC=nullptr, Action::OffloadKind Kind=Action::OFK_None, bool ShouldLinkDeviceLibs=false)
AMDGPU Toolchain.
Definition AMDGPU.cpp:678
SPIRVAMDToolChain(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition HIPAMD.cpp:227
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 HIPAMD.cpp:204
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 getAMDGPUTargetFeatures(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args, std::vector< StringRef > &Features)
Definition AMDGPU.cpp:661
SmallVector< StringRef > unifyTargetFeatures(ArrayRef< StringRef > Features)
If there are multiple +xxx or -xxx features, keep the last one.
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 addLTOOptions(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const InputInfo &Output, const InputInfoList &Inputs, bool IsThinLTO)
void addLinkerCompressDebugSectionsOption(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
void constructLLVMLinkCommand(Compilation &C, const Tool &T, const JobAction &JA, const InputInfoList &JobInputs, const llvm::opt::ArgStringList &LinkerInputs, const InputInfo &Output, const llvm::opt::ArgList &Args, const char *OutputFilename=nullptr)
SmallVector< InputInfo, 4 > InputInfoList
Definition Driver.h:51
The JSON file list parser is used to communicate input to InstallAPI.
bool(*)(llvm::ArrayRef< const char * >, llvm::raw_ostream &, llvm::raw_ostream &, bool, bool) Driver
Definition Wasm.cpp:36
static constexpr ResponseFileSupport None()
Returns a ResponseFileSupport indicating that response files are not supported.
Definition Job.h:79