| 1 |
//===--- AMDGPU.h - AMDGPU 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 |
#ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_AMDGPU_H |
| 10 |
#define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_AMDGPU_H |
| 11 |
|
| 12 |
#include "Gnu.h" |
| 13 |
#include "clang/Basic/TargetID.h" |
| 14 |
#include "clang/Driver/Tool.h" |
| 15 |
#include "clang/Driver/ToolChain.h" |
| 16 |
#include "clang/Options/Options.h" |
| 17 |
#include "llvm/ADT/SmallString.h" |
| 18 |
#include "llvm/TargetParser/TargetParser.h" |
| 19 |
|
| 20 |
#include <map> |
| 21 |
|
| 22 |
namespace clang { |
| 23 |
namespace driver { |
| 24 |
|
| 25 |
namespace tools { |
| 26 |
namespace amdgpu { |
| 27 |
|
| 28 |
class LLVM_LIBRARY_VISIBILITY Linker final : public Tool { |
| 29 |
public: |
| 30 |
Linker(const ToolChain &TC) : Tool("amdgpu::Linker", "ld.lld", TC) {} |
| 31 |
bool isLinkJob() const override { return true; } |
| 32 |
bool hasIntegratedCPP() const override { return false; } |
| 33 |
void ConstructJob(Compilation &C, const JobAction &JA, |
| 34 |
const InputInfo &Output, const InputInfoList &Inputs, |
| 35 |
const llvm::opt::ArgList &TCArgs, |
| 36 |
const char *LinkingOutput) const override; |
| 37 |
}; |
| 38 |
|
| 39 |
void getAMDGPUTargetFeatures(const Driver &D, const llvm::Triple &Triple, |
| 40 |
const llvm::opt::ArgList &Args, |
| 41 |
std::vector<StringRef> &Features); |
| 42 |
|
| 43 |
void addFullLTOPartitionOption(const Driver &D, const llvm::opt::ArgList &Args, |
| 44 |
llvm::opt::ArgStringList &CmdArgs); |
| 45 |
} // end namespace amdgpu |
| 46 |
} // end namespace tools |
| 47 |
|
| 48 |
namespace toolchains { |
| 49 |
|
| 50 |
class LLVM_LIBRARY_VISIBILITY AMDGPUToolChain : public Generic_ELF { |
| 51 |
protected: |
| 52 |
const std::map<options::ID, const StringRef> OptionsDefault; |
| 53 |
|
| 54 |
Tool *buildLinker() const override; |
| 55 |
StringRef getOptionDefault(options::ID OptID) const { |
| 56 |
auto opt = OptionsDefault.find(OptID); |
| 57 |
assert(opt != OptionsDefault.end() && "No Default for Option"); |
| 58 |
return opt->second; |
| 59 |
} |
| 60 |
|
| 61 |
public: |
| 62 |
AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple, |
| 63 |
const llvm::opt::ArgList &Args); |
| 64 |
unsigned GetDefaultDwarfVersion() const override { return 5; } |
| 65 |
|
| 66 |
bool IsMathErrnoDefault() const override { return false; } |
| 67 |
bool isCrossCompiling() const override { return true; } |
| 68 |
bool isPICDefault() const override { return true; } |
| 69 |
bool isPIEDefault(const llvm::opt::ArgList &Args) const override { |
| 70 |
return false; |
| 71 |
} |
| 72 |
bool isPICDefaultForced() const override { return true; } |
| 73 |
bool SupportsProfiling() const override { return false; } |
| 74 |
|
| 75 |
llvm::opt::DerivedArgList * |
| 76 |
TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch, |
| 77 |
Action::OffloadKind DeviceOffloadKind) const override; |
| 78 |
|
| 79 |
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, |
| 80 |
llvm::opt::ArgStringList &CC1Args, |
| 81 |
Action::OffloadKind DeviceOffloadKind) const override; |
| 82 |
void |
| 83 |
AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, |
| 84 |
llvm::opt::ArgStringList &CC1Args) const override; |
| 85 |
|
| 86 |
/// Return whether denormals should be flushed, and treated as 0 by default |
| 87 |
/// for the subtarget. |
| 88 |
static bool getDefaultDenormsAreZeroForTarget(llvm::AMDGPU::GPUKind GPUKind); |
| 89 |
|
| 90 |
llvm::DenormalMode getDefaultDenormalModeForType( |
| 91 |
const llvm::opt::ArgList &DriverArgs, const JobAction &JA, |
| 92 |
const llvm::fltSemantics *FPType = nullptr) const override; |
| 93 |
|
| 94 |
static bool isWave64(const llvm::opt::ArgList &DriverArgs, |
| 95 |
llvm::AMDGPU::GPUKind Kind); |
| 96 |
/// Needed for using lto. |
| 97 |
bool HasNativeLLVMSupport() const override { |
| 98 |
return true; |
| 99 |
} |
| 100 |
|
| 101 |
/// Needed for translating LTO options. |
| 102 |
const char *getDefaultLinker() const override { return "ld.lld"; } |
| 103 |
|
| 104 |
/// Should skip sanitize option. |
| 105 |
bool shouldSkipSanitizeOption(const ToolChain &TC, |
| 106 |
const llvm::opt::ArgList &DriverArgs, |
| 107 |
StringRef TargetID, |
| 108 |
const llvm::opt::Arg *A) const; |
| 109 |
|
| 110 |
/// Uses amdgpu-arch tool to get arch of the system GPU. Will return error |
| 111 |
/// if unable to find one. |
| 112 |
virtual Expected<SmallVector<std::string>> |
| 113 |
getSystemGPUArchs(const llvm::opt::ArgList &Args) const override; |
| 114 |
|
| 115 |
protected: |
| 116 |
/// Check and diagnose invalid target ID specified by -mcpu. |
| 117 |
virtual void checkTargetID(const llvm::opt::ArgList &DriverArgs) const; |
| 118 |
|
| 119 |
/// The struct type returned by getParsedTargetID. |
| 120 |
struct ParsedTargetIDType { |
| 121 |
std::optional<std::string> OptionalTargetID; |
| 122 |
std::optional<std::string> OptionalGPUArch; |
| 123 |
std::optional<llvm::StringMap<bool>> OptionalFeatures; |
| 124 |
}; |
| 125 |
|
| 126 |
/// Get target ID, GPU arch, and target ID features if the target ID is |
| 127 |
/// specified and valid. |
| 128 |
ParsedTargetIDType |
| 129 |
getParsedTargetID(const llvm::opt::ArgList &DriverArgs) const; |
| 130 |
|
| 131 |
/// Get GPU arch from -mcpu without checking. |
| 132 |
StringRef getGPUArch(const llvm::opt::ArgList &DriverArgs) const; |
| 133 |
|
| 134 |
/// Common warning options shared by AMDGPU HIP, OpenCL and OpenMP toolchains. |
| 135 |
/// Language specific warning options should go to derived classes. |
| 136 |
void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const override; |
| 137 |
}; |
| 138 |
|
| 139 |
class LLVM_LIBRARY_VISIBILITY ROCMToolChain : public AMDGPUToolChain { |
| 140 |
public: |
| 141 |
ROCMToolChain(const Driver &D, const llvm::Triple &Triple, |
| 142 |
const llvm::opt::ArgList &Args); |
| 143 |
void |
| 144 |
addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, |
| 145 |
llvm::opt::ArgStringList &CC1Args, |
| 146 |
Action::OffloadKind DeviceOffloadKind) const override; |
| 147 |
|
| 148 |
// Returns a list of device library names shared by different languages |
| 149 |
llvm::SmallVector<BitCodeLibraryInfo, 12> |
| 150 |
getCommonDeviceLibNames(const llvm::opt::ArgList &DriverArgs, |
| 151 |
const std::string &GPUArch, |
| 152 |
Action::OffloadKind DeviceOffloadingKind) const; |
| 153 |
|
| 154 |
SanitizerMask getSupportedSanitizers() const override { |
| 155 |
return SanitizerKind::Address; |
| 156 |
} |
| 157 |
|
| 158 |
bool diagnoseUnsupportedOption(const llvm::opt::Arg *A, |
| 159 |
const llvm::opt::DerivedArgList &DAL, |
| 160 |
const llvm::opt::ArgList &DriverArgs, |
| 161 |
const char *Value = nullptr) const { |
| 162 |
auto &Diags = getDriver().getDiags(); |
| 163 |
bool IsExplicitDevice = |
| 164 |
A->getBaseArg().getOption().matches(options::OPT_Xarch_device); |
| 165 |
|
| 166 |
if (Value) { |
| 167 |
unsigned DiagID = |
| 168 |
IsExplicitDevice |
| 169 |
? clang::diag::err_drv_unsupported_option_part_for_target |
| 170 |
: clang::diag::warn_drv_unsupported_option_part_for_target; |
| 171 |
Diags.Report(DiagID) << Value << A->getAsString(DriverArgs) |
| 172 |
<< getTriple().str(); |
| 173 |
} else { |
| 174 |
unsigned DiagID = |
| 175 |
IsExplicitDevice |
| 176 |
? clang::diag::err_drv_unsupported_option_for_target |
| 177 |
: clang::diag::warn_drv_unsupported_option_for_target; |
| 178 |
Diags.Report(DiagID) << A->getAsString(DAL) << getTriple().str(); |
| 179 |
} |
| 180 |
return true; |
| 181 |
} |
| 182 |
|
| 183 |
bool handleSanitizeOption(const ToolChain &TC, llvm::opt::DerivedArgList &DAL, |
| 184 |
const llvm::opt::ArgList &DriverArgs, |
| 185 |
StringRef TargetID, const llvm::opt::Arg *A) const { |
| 186 |
if (TargetID.empty()) |
| 187 |
return false; |
| 188 |
// If we shouldn't do sanitizing, skip it. |
| 189 |
if (!DriverArgs.hasFlag(options::OPT_fgpu_sanitize, |
| 190 |
options::OPT_fno_gpu_sanitize, true)) |
| 191 |
return true; |
| 192 |
const llvm::opt::Option &Opt = A->getOption(); |
| 193 |
// Sanitizer coverage is currently not supported for AMDGPU, so warn/error |
| 194 |
// on every related option. |
| 195 |
if (Opt.matches(options::OPT_fsan_cov_Group)) { |
| 196 |
diagnoseUnsupportedOption(A, DAL, DriverArgs); |
| 197 |
} |
| 198 |
// If this isn't a sanitizer option, don't handle it. |
| 199 |
if (!Opt.matches(options::OPT_fsanitize_EQ)) |
| 200 |
return false; |
| 201 |
|
| 202 |
SmallVector<const char *, 4> SupportedSanitizers; |
| 203 |
SmallVector<const char *, 4> UnSupportedSanitizers; |
| 204 |
|
| 205 |
for (const char *Value : A->getValues()) { |
| 206 |
SanitizerMask K = parseSanitizerValue(Value, /*Allow Groups*/ false); |
| 207 |
if (K & ROCMToolChain::getSupportedSanitizers()) |
| 208 |
SupportedSanitizers.push_back(Value); |
| 209 |
else |
| 210 |
UnSupportedSanitizers.push_back(Value); |
| 211 |
} |
| 212 |
|
| 213 |
// If there are no supported sanitizers, drop the whole argument. |
| 214 |
if (SupportedSanitizers.empty()) { |
| 215 |
diagnoseUnsupportedOption(A, DAL, DriverArgs); |
| 216 |
return true; |
| 217 |
} |
| 218 |
// If only some sanitizers are unsupported, report each one individually. |
| 219 |
if (!UnSupportedSanitizers.empty()) { |
| 220 |
for (const char *Value : UnSupportedSanitizers) { |
| 221 |
diagnoseUnsupportedOption(A, DAL, DriverArgs, Value); |
| 222 |
} |
| 223 |
} |
| 224 |
// If we know the target arch, check if the sanitizer is supported for it. |
| 225 |
if (shouldSkipSanitizeOption(TC, DriverArgs, TargetID, A)) |
| 226 |
return true; |
| 227 |
|
| 228 |
// Add a new argument with only the supported sanitizers. |
| 229 |
DAL.AddJoinedArg(A, A->getOption(), llvm::join(SupportedSanitizers, ",")); |
| 230 |
return true; |
| 231 |
} |
| 232 |
}; |
| 233 |
|
| 234 |
} // end namespace toolchains |
| 235 |
} // end namespace driver |
| 236 |
} // end namespace clang |
| 237 |
|
| 238 |
#endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_AMDGPU_H |