22#include "llvm/Support/Alignment.h"
23#include "llvm/Support/FileSystem.h"
24#include "llvm/Support/Path.h"
25#include "llvm/TargetParser/TargetParser.h"
33#if defined(_WIN32) || defined(_WIN64)
34#define NULL_FILE "nul"
36#define NULL_FILE "/dev/null"
39void AMDGCN::Linker::constructLlvmLinkCommand(
Compilation &
C,
43 const llvm::opt::ArgList &Args)
const {
46 ArgStringList LlvmLinkArgs;
48 assert(!Inputs.empty() &&
"Must have at least one input.");
51 for (
auto Input : Inputs)
52 LlvmLinkArgs.push_back(Input.getFilename());
56 auto TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ);
60 const char *LlvmLink =
61 Args.MakeArgString(
getToolChain().GetProgramPath(
"llvm-link"));
63 LlvmLink, LlvmLinkArgs, Inputs,
70 const llvm::opt::ArgList &Args)
const {
73 ArgStringList LldArgs{
"-flavor",
79 "-plugin-opt=-amdgpu-internalize-symbols"};
80 if (Args.hasArg(options::OPT_hipstdpar))
81 LldArgs.push_back(
"-plugin-opt=-amdgpu-enable-hipstdpar");
83 auto &TC = getToolChain();
84 auto &
D = TC.getDriver();
85 assert(!Inputs.empty() &&
"Must have at least one input.");
86 bool IsThinLTO =
D.getOffloadLTOMode() ==
LTOK_Thin;
87 addLTOOptions(TC, Args, LldArgs, Output, Inputs[0], IsThinLTO);
90 std::vector<llvm::StringRef> Features;
94 std::string MAttrString =
"-plugin-opt=-mattr=";
96 MAttrString.append(Args.MakeArgString(OneFeature));
97 if (OneFeature != Features.back())
98 MAttrString.append(
",");
100 if (!Features.empty())
101 LldArgs.push_back(Args.MakeArgString(MAttrString));
107 LldArgs.push_back(Args.MakeArgString(
"-plugin-opt=-force-import-all"));
109 for (
const Arg *A : Args.filtered(options::OPT_mllvm)) {
111 Args.MakeArgString(Twine(
"-plugin-opt=") + A->getValue(0)));
114 if (
C.getDriver().isSaveTempsEnabled())
115 LldArgs.push_back(
"-save-temps");
129 LldArgs.push_back(
"--whole-archive");
131 for (
auto *Arg : Args.filtered(options::OPT_Xoffload_linker)) {
132 StringRef ArgVal = Arg->getValue(1);
133 auto SplitArg = ArgVal.split(
"-mllvm=");
134 if (!SplitArg.second.empty()) {
136 Args.MakeArgString(Twine(
"-plugin-opt=") + SplitArg.second));
138 LldArgs.push_back(Args.MakeArgString(ArgVal));
144 for (
auto Input : Inputs)
145 LldArgs.push_back(Input.getFilename());
149 auto TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ);
153 LldArgs.push_back(
"--no-whole-archive");
155 const char *Lld = Args.MakeArgString(getToolChain().GetProgramPath(
"lld"));
157 Lld, LldArgs, Inputs, Output));
166void AMDGCN::Linker::constructLinkAndEmitSpirvCommand(
168 const InputInfo &Output,
const llvm::opt::ArgList &Args)
const {
169 assert(!Inputs.empty() &&
"Must have at least one input.");
171 constructLlvmLinkCommand(
C, JA, Inputs, Output, Args);
176 llvm::opt::ArgStringList TrArgs{
177 "--spirv-max-version=1.6",
179 "--spirv-allow-extra-diexpressions",
180 "--spirv-allow-unknown-intrinsics",
181 "--spirv-lower-const-expr",
182 "--spirv-preserve-auxdata",
183 "--spirv-debug-info-version=nonsemantic-shader-200"};
194 const char *LinkingOutput)
const {
195 if (Inputs.size() > 0 &&
196 Inputs[0].getType() == types::TY_Image &&
197 JA.
getType() == types::TY_Object)
201 if (JA.
getType() == types::TY_HIP_FATBIN)
205 if (JA.
getType() == types::TY_LLVM_BC)
206 return constructLlvmLinkCommand(
C, JA, Inputs, Output, Args);
208 if (getToolChain().getEffectiveTriple().isSPIRV())
209 return constructLinkAndEmitSpirvCommand(
C, JA, Inputs, Output, Args);
211 return constructLldCommand(
C, JA, Inputs, Output, Args);
215 const ToolChain &HostTC,
const ArgList &Args)
222 if (!Args.hasFlag(options::OPT_fgpu_sanitize, options::OPT_fno_gpu_sanitize,
225 for (
auto *A : Args.filtered(options::OPT_fsanitize_EQ)) {
227 if (K != SanitizerKind::Address)
228 D.getDiags().Report(clang::diag::warn_drv_unsupported_option_for_target)
229 << A->getAsString(Args) <<
getTriple().str();
234 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
239 "Only HIP offloading kinds are supported for GPUs.");
241 CC1Args.append({
"-fcuda-is-device",
"-fno-threadsafe-statics"});
243 if (!DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
245 CC1Args.append({
"-mllvm",
"-amdgpu-internalize-symbols"});
246 if (DriverArgs.hasArgNoClaim(options::OPT_hipstdpar))
247 CC1Args.append({
"-mllvm",
"-amdgpu-enable-hipstdpar"});
249 StringRef MaxThreadsPerBlock =
250 DriverArgs.getLastArgValue(options::OPT_gpu_max_threads_per_block_EQ);
251 if (!MaxThreadsPerBlock.empty()) {
253 (Twine(
"--gpu-max-threads-per-block=") + MaxThreadsPerBlock).str();
254 CC1Args.push_back(DriverArgs.MakeArgStringRef(ArgStr));
257 CC1Args.push_back(
"-fcuda-allow-variadic-functions");
261 if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
262 options::OPT_fvisibility_ms_compat)) {
263 CC1Args.append({
"-fvisibility=hidden"});
264 CC1Args.push_back(
"-fapply-global-visibility-to-externs");
271 if (!DriverArgs.hasArg(options::OPT_fembed_bitcode_marker))
272 CC1Args.push_back(
"-fembed-bitcode=marker");
277 CC1Args.push_back(BCFile.ShouldInternalize ?
"-mlink-builtin-bitcode"
278 :
"-mlink-bitcode-file");
279 CC1Args.push_back(DriverArgs.MakeArgString(BCFile.Path));
283llvm::opt::DerivedArgList *
287 DerivedArgList *DAL =
290 DAL =
new DerivedArgList(Args.getBaseArgs());
294 for (Arg *A : Args) {
299 if (!BoundArch.empty()) {
300 DAL->eraseArg(options::OPT_mcpu_EQ);
301 DAL->AddJoinedArg(
nullptr, Opts.getOption(options::OPT_mcpu_EQ), BoundArch);
325 ArgStringList &CC1Args)
const {
330 const ArgList &Args, ArgStringList &CC1Args)
const {
335 ArgStringList &CC1Args)
const {
340 ArgStringList &CC1Args)
const {
358 const ArgList &Args)
const {
365 if (DriverArgs.hasArg(options::OPT_nogpulib) ||
368 ArgStringList LibraryPaths;
372 LibraryPaths.push_back(DriverArgs.MakeArgString(
Path));
377 auto BCLibArgs = DriverArgs.getAllArgValues(options::OPT_hip_device_lib_EQ);
378 if (!BCLibArgs.empty()) {
379 llvm::for_each(BCLibArgs, [&](StringRef BCName) {
381 for (StringRef LibraryPath : LibraryPaths) {
383 llvm::sys::path::append(
Path, BCName);
385 if (llvm::sys::fs::exists(
FullName)) {
398 assert(!GpuArch.empty() &&
"Must have an explicit GPU arch.");
401 if (DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
402 options::OPT_fno_gpu_sanitize,
true) &&
405 if (AsanRTL.empty()) {
408 "AMDGPU address sanitizer runtime library (asanrtl) is not found. "
409 "Please install ROCm device library which supports address "
414 BCLibs.emplace_back(AsanRTL,
false);
422 BCLibs.emplace_back(N);
426 DriverArgs.getLastArgValue(options::OPT_gpu_instrument_lib_EQ);
429 if (llvm::sys::fs::exists(InstLib))
430 BCLibs.push_back(InstLib);
439 const llvm::opt::ArgList &DriverArgs)
const {
441 if (PTID.OptionalTargetID && !PTID.OptionalGPUArch &&
442 PTID.OptionalTargetID !=
"amdgcnspirv")
444 << *PTID.OptionalTargetID;
unsigned getCustomDiagID(Level L, const char(&FormatString)[N])
Return an ID for a diagnostic with the specified format string and level.
types::ID getType() const
Compilation - A set of tasks to perform for a single driver invocation.
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
DiagnosticsEngine & getDiags() const
DiagnosticBuilder Diag(unsigned DiagID) const
const llvm::opt::OptTable & getOpts() const
The JSON file list parser is used to communicate input to InstallAPI.
SanitizerMask parseSanitizerValue(StringRef Value, bool AllowGroups)
Parse a single value from a -fsanitize= or -fno-sanitize= value list.
static constexpr ResponseFileSupport None()
Returns a ResponseFileSupport indicating that response files are not supported.