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"
40 const llvm::opt::ArgList &DriverArgs,
42 const llvm::opt::Arg *A) {
46 Option O = A->getOption();
47 if (!O.matches(options::OPT_fsanitize_EQ))
50 if (!DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
51 options::OPT_fno_gpu_sanitize,
true))
58 if (K != SanitizerKind::Address)
61 llvm::StringMap<bool> FeatureMap;
64 assert(OptionalGpuArch &&
"Invalid Target ID");
65 (void)OptionalGpuArch;
66 auto Loc = FeatureMap.find(
"xnack");
67 if (
Loc == FeatureMap.end() || !
Loc->second) {
69 clang::diag::warn_drv_unsupported_option_for_offload_arch_req_feature)
70 << A->getAsString(DriverArgs) << TargetID <<
"xnack+";
76void AMDGCN::Linker::constructLlvmLinkCommand(
Compilation &
C,
80 const llvm::opt::ArgList &Args)
const {
83 ArgStringList LlvmLinkArgs;
85 assert(!Inputs.empty() &&
"Must have at least one input.");
88 for (
auto Input : Inputs)
89 LlvmLinkArgs.push_back(Input.getFilename());
93 auto TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ);
97 const char *LlvmLink =
98 Args.MakeArgString(
getToolChain().GetProgramPath(
"llvm-link"));
100 LlvmLink, LlvmLinkArgs, Inputs,
107 const llvm::opt::ArgList &Args)
const {
110 ArgStringList LldArgs{
"-flavor",
116 "-plugin-opt=-amdgpu-internalize-symbols"};
117 if (Args.hasArg(options::OPT_hipstdpar))
118 LldArgs.push_back(
"-plugin-opt=-amdgpu-enable-hipstdpar");
120 auto &TC = getToolChain();
121 auto &
D = TC.getDriver();
122 assert(!Inputs.empty() &&
"Must have at least one input.");
123 bool IsThinLTO =
D.getOffloadLTOMode() ==
LTOK_Thin;
124 addLTOOptions(TC, Args, LldArgs, Output, Inputs[0], IsThinLTO);
127 std::vector<llvm::StringRef> Features;
131 std::string MAttrString =
"-plugin-opt=-mattr=";
133 MAttrString.append(Args.MakeArgString(OneFeature));
134 if (OneFeature != Features.back())
135 MAttrString.append(
",");
137 if (!Features.empty())
138 LldArgs.push_back(Args.MakeArgString(MAttrString));
144 LldArgs.push_back(Args.MakeArgString(
"-plugin-opt=-force-import-all"));
146 for (
const Arg *A : Args.filtered(options::OPT_mllvm)) {
148 Args.MakeArgString(Twine(
"-plugin-opt=") + A->getValue(0)));
151 if (
C.getDriver().isSaveTempsEnabled())
152 LldArgs.push_back(
"-save-temps");
166 LldArgs.push_back(
"--whole-archive");
168 for (
auto *Arg : Args.filtered(options::OPT_Xoffload_linker)) {
169 StringRef ArgVal = Arg->getValue(1);
170 auto SplitArg = ArgVal.split(
"-mllvm=");
171 if (!SplitArg.second.empty()) {
173 Args.MakeArgString(Twine(
"-plugin-opt=") + SplitArg.second));
175 LldArgs.push_back(Args.MakeArgString(ArgVal));
181 for (
auto Input : Inputs)
182 LldArgs.push_back(Input.getFilename());
186 auto TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ);
190 LldArgs.push_back(
"--no-whole-archive");
192 const char *Lld = Args.MakeArgString(getToolChain().GetProgramPath(
"lld"));
194 Lld, LldArgs, Inputs, Output));
203void AMDGCN::Linker::constructLinkAndEmitSpirvCommand(
205 const InputInfo &Output,
const llvm::opt::ArgList &Args)
const {
206 assert(!Inputs.empty() &&
"Must have at least one input.");
208 constructLlvmLinkCommand(
C, JA, Inputs, Output, Args);
213 llvm::opt::ArgStringList TrArgs{
214 "--spirv-max-version=1.6",
216 "--spirv-allow-extra-diexpressions",
217 "--spirv-allow-unknown-intrinsics",
218 "--spirv-lower-const-expr",
219 "--spirv-preserve-auxdata",
220 "--spirv-debug-info-version=nonsemantic-shader-200"};
231 const char *LinkingOutput)
const {
232 if (Inputs.size() > 0 &&
233 Inputs[0].getType() == types::TY_Image &&
234 JA.
getType() == types::TY_Object)
238 if (JA.
getType() == types::TY_HIP_FATBIN)
242 if (JA.
getType() == types::TY_LLVM_BC)
243 return constructLlvmLinkCommand(
C, JA, Inputs, Output, Args);
245 if (getToolChain().getTriple().isSPIRV())
246 return constructLinkAndEmitSpirvCommand(
C, JA, Inputs, Output, Args);
248 return constructLldCommand(
C, JA, Inputs, Output, Args);
252 const ToolChain &HostTC,
const ArgList &Args)
259 if (!Args.hasFlag(options::OPT_fgpu_sanitize, options::OPT_fno_gpu_sanitize,
262 for (
auto *A : Args.filtered(options::OPT_fsanitize_EQ)) {
264 if (K != SanitizerKind::Address)
265 D.getDiags().Report(clang::diag::warn_drv_unsupported_option_for_target)
266 << A->getAsString(Args) <<
getTriple().str();
271 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
276 "Only HIP offloading kinds are supported for GPUs.");
278 CC1Args.push_back(
"-fcuda-is-device");
280 if (!DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
282 CC1Args.append({
"-mllvm",
"-amdgpu-internalize-symbols"});
283 if (DriverArgs.hasArgNoClaim(options::OPT_hipstdpar))
284 CC1Args.append({
"-mllvm",
"-amdgpu-enable-hipstdpar"});
286 StringRef MaxThreadsPerBlock =
287 DriverArgs.getLastArgValue(options::OPT_gpu_max_threads_per_block_EQ);
288 if (!MaxThreadsPerBlock.empty()) {
290 (Twine(
"--gpu-max-threads-per-block=") + MaxThreadsPerBlock).str();
291 CC1Args.push_back(DriverArgs.MakeArgStringRef(ArgStr));
294 CC1Args.push_back(
"-fcuda-allow-variadic-functions");
298 if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
299 options::OPT_fvisibility_ms_compat)) {
300 CC1Args.append({
"-fvisibility=hidden"});
301 CC1Args.push_back(
"-fapply-global-visibility-to-externs");
308 !DriverArgs.hasArg(options::OPT_fembed_bitcode_marker))
309 CC1Args.push_back(
"-fembed-bitcode=marker");
312 CC1Args.push_back(BCFile.ShouldInternalize ?
"-mlink-builtin-bitcode"
313 :
"-mlink-bitcode-file");
314 CC1Args.push_back(DriverArgs.MakeArgString(BCFile.Path));
318llvm::opt::DerivedArgList *
322 DerivedArgList *DAL =
325 DAL =
new DerivedArgList(Args.getBaseArgs());
329 for (Arg *A : Args) {
334 if (!BoundArch.empty()) {
335 DAL->eraseArg(options::OPT_mcpu_EQ);
336 DAL->AddJoinedArg(
nullptr, Opts.getOption(options::OPT_mcpu_EQ), BoundArch);
360 ArgStringList &CC1Args)
const {
365 const ArgList &Args, ArgStringList &CC1Args)
const {
370 ArgStringList &CC1Args)
const {
375 ArgStringList &CC1Args)
const {
393 const ArgList &Args)
const {
400 if (DriverArgs.hasArg(options::OPT_nogpulib) ||
402 getTriple().getVendor() == llvm::Triple::AMD))
404 ArgStringList LibraryPaths;
408 LibraryPaths.push_back(DriverArgs.MakeArgString(
Path));
413 auto BCLibArgs = DriverArgs.getAllArgValues(options::OPT_hip_device_lib_EQ);
414 if (!BCLibArgs.empty()) {
415 llvm::for_each(BCLibArgs, [&](StringRef BCName) {
417 for (StringRef LibraryPath : LibraryPaths) {
419 llvm::sys::path::append(
Path, BCName);
421 if (llvm::sys::fs::exists(
FullName)) {
434 assert(!GpuArch.empty() &&
"Must have an explicit GPU arch.");
437 if (DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
438 options::OPT_fno_gpu_sanitize,
true) &&
441 if (AsanRTL.empty()) {
444 "AMDGPU address sanitizer runtime library (asanrtl) is not found. "
445 "Please install ROCm device library which supports address "
450 BCLibs.emplace_back(AsanRTL,
false);
458 BCLibs.emplace_back(N);
462 DriverArgs.getLastArgValue(options::OPT_gpu_instrument_lib_EQ);
465 if (llvm::sys::fs::exists(InstLib))
466 BCLibs.push_back(InstLib);
475 const llvm::opt::ArgList &DriverArgs)
const {
477 if (PTID.OptionalTargetID && !PTID.OptionalGPUArch) {
479 << *PTID.OptionalTargetID;
static bool shouldSkipSanitizeOption(const ToolChain &TC, const llvm::opt::ArgList &DriverArgs, StringRef TargetID, const llvm::opt::Arg *A)
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.
std::optional< llvm::StringRef > parseTargetID(const llvm::Triple &T, llvm::StringRef OffloadArch, llvm::StringMap< bool > *FeatureMap)
Parse a target ID to get processor and feature map.
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.