1 //===--- AMDGPU.h - Declare AMDGPU target feature support -------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file declares AMDGPU TargetInfo objects.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_LIB_BASIC_TARGETS_AMDGPU_H
14 #define LLVM_CLANG_LIB_BASIC_TARGETS_AMDGPU_H
16 #include "clang/Basic/TargetID.h"
17 #include "clang/Basic/TargetInfo.h"
18 #include "clang/Basic/TargetOptions.h"
19 #include "llvm/ADT/StringSet.h"
20 #include "llvm/Support/AMDGPUAddrSpace.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/TargetParser/TargetParser.h"
23 #include "llvm/TargetParser/Triple.h"
29 class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final
: public TargetInfo
{
31 static const char *const GCCRegNames
[];
33 static const LangASMap AMDGPUDefIsGenMap
;
34 static const LangASMap AMDGPUDefIsPrivMap
;
36 llvm::AMDGPU::GPUKind GPUKind
;
38 unsigned WavefrontSize
;
40 /// Whether to use cumode or WGP mode. True for cumode. False for WGP mode.
43 /// Whether having image instructions.
44 bool HasImage
= false;
46 /// Target ID is device name followed by optional feature name postfixed
47 /// by plus or minus sign delimitted by colon, e.g. gfx908:xnack+:sramecc-.
48 /// If the target ID contains feature+, map it to true.
49 /// If the target ID contains feature-, map it to false.
50 /// If the target ID does not contain a feature (default), do not map it.
51 llvm::StringMap
<bool> OffloadArchFeatures
;
54 bool hasFP64() const {
55 return getTriple().getArch() == llvm::Triple::amdgcn
||
56 !!(GPUFeatures
& llvm::AMDGPU::FEATURE_FP64
);
60 bool hasFastFMAF() const {
61 return !!(GPUFeatures
& llvm::AMDGPU::FEATURE_FAST_FMA_F32
);
65 bool hasFastFMA() const {
66 return getTriple().getArch() == llvm::Triple::amdgcn
;
69 bool hasFMAF() const {
70 return getTriple().getArch() == llvm::Triple::amdgcn
||
71 !!(GPUFeatures
& llvm::AMDGPU::FEATURE_FMA
);
74 bool hasFullRateDenormalsF32() const {
75 return !!(GPUFeatures
& llvm::AMDGPU::FEATURE_FAST_DENORMAL_F32
);
78 bool hasLDEXPF() const {
79 return getTriple().getArch() == llvm::Triple::amdgcn
||
80 !!(GPUFeatures
& llvm::AMDGPU::FEATURE_LDEXP
);
83 static bool isAMDGCN(const llvm::Triple
&TT
) {
84 return TT
.getArch() == llvm::Triple::amdgcn
;
87 static bool isR600(const llvm::Triple
&TT
) {
88 return TT
.getArch() == llvm::Triple::r600
;
92 AMDGPUTargetInfo(const llvm::Triple
&Triple
, const TargetOptions
&Opts
);
94 void setAddressSpaceMap(bool DefaultIsPrivate
);
96 void adjust(DiagnosticsEngine
&Diags
, LangOptions
&Opts
) override
;
98 uint64_t getPointerWidthV(LangAS AS
) const override
{
99 if (isR600(getTriple()))
101 unsigned TargetAS
= getTargetAddressSpace(AS
);
103 if (TargetAS
== llvm::AMDGPUAS::PRIVATE_ADDRESS
||
104 TargetAS
== llvm::AMDGPUAS::LOCAL_ADDRESS
)
110 uint64_t getPointerAlignV(LangAS AddrSpace
) const override
{
111 return getPointerWidthV(AddrSpace
);
114 virtual bool isAddressSpaceSupersetOf(LangAS A
, LangAS B
) const override
{
115 // The flat address space AS(0) is a superset of all the other address
116 // spaces used by the backend target.
118 ((A
== LangAS::Default
||
119 (isTargetAddressSpace(A
) &&
120 toTargetAddressSpace(A
) == llvm::AMDGPUAS::FLAT_ADDRESS
)) &&
121 isTargetAddressSpace(B
) &&
122 toTargetAddressSpace(B
) >= llvm::AMDGPUAS::FLAT_ADDRESS
&&
123 toTargetAddressSpace(B
) <= llvm::AMDGPUAS::PRIVATE_ADDRESS
&&
124 toTargetAddressSpace(B
) != llvm::AMDGPUAS::REGION_ADDRESS
);
127 uint64_t getMaxPointerWidth() const override
{
128 return getTriple().getArch() == llvm::Triple::amdgcn
? 64 : 32;
131 bool hasBFloat16Type() const override
{ return isAMDGCN(getTriple()); }
133 std::string_view
getClobbers() const override
{ return ""; }
135 ArrayRef
<const char *> getGCCRegNames() const override
;
137 ArrayRef
<TargetInfo::GCCRegAlias
> getGCCRegAliases() const override
{
141 /// Accepted register names: (n, m is unsigned integer, n < m)
148 /// {S} , where S is a special register name
152 bool validateAsmConstraint(const char *&Name
,
153 TargetInfo::ConstraintInfo
&Info
) const override
{
154 static const ::llvm::StringSet
<> SpecialRegs({
155 "exec", "vcc", "flat_scratch", "m0", "scc", "tba", "tma",
156 "flat_scratch_lo", "flat_scratch_hi", "vcc_lo", "vcc_hi", "exec_lo",
157 "exec_hi", "tma_lo", "tma_hi", "tba_lo", "tba_hi",
162 Info
.setRequiresImmediate(-16, 64);
165 Info
.setRequiresImmediate(-32768, 32767);
170 Info
.setRequiresImmediate();
178 if (S
== "DA" || S
== "DB") {
180 Info
.setRequiresImmediate();
184 bool HasLeftParen
= S
.consume_front("{");
187 if (S
.front() != 'v' && S
.front() != 's' && S
.front() != 'a') {
190 auto E
= S
.find('}');
191 if (!SpecialRegs
.count(S
.substr(0, E
)))
193 S
= S
.drop_front(E
+ 1);
196 // Found {S} where S is a special register.
197 Info
.setAllowsRegister();
206 Info
.setAllowsRegister();
210 bool HasLeftBracket
= S
.consume_front("[");
211 unsigned long long N
;
212 if (S
.empty() || consumeUnsignedInteger(S
, 10, N
))
214 if (S
.consume_front(":")) {
217 unsigned long long M
;
218 if (consumeUnsignedInteger(S
, 10, M
) || N
>= M
)
221 if (HasLeftBracket
) {
222 if (!S
.consume_front("]"))
225 if (!S
.consume_front("}"))
229 // Found {vn}, {sn}, {an}, {v[n]}, {s[n]}, {a[n]}, {v[n:m]}, {s[n:m]}
231 Info
.setAllowsRegister();
236 // \p Constraint will be left pointing at the last character of
237 // the constraint. In practice, it won't be changed unless the
238 // constraint is longer than one character.
239 std::string
convertConstraint(const char *&Constraint
) const override
{
241 StringRef
S(Constraint
);
242 if (S
== "DA" || S
== "DB") {
243 return std::string("^") + std::string(Constraint
++, 2);
246 const char *Begin
= Constraint
;
247 TargetInfo::ConstraintInfo
Info("", "");
248 if (validateAsmConstraint(Constraint
, Info
))
249 return std::string(Begin
).substr(0, Constraint
- Begin
+ 1);
252 return std::string(1, *Constraint
);
256 initFeatureMap(llvm::StringMap
<bool> &Features
, DiagnosticsEngine
&Diags
,
258 const std::vector
<std::string
> &FeatureVec
) const override
;
260 ArrayRef
<Builtin::Info
> getTargetBuiltins() const override
;
262 bool useFP16ConversionIntrinsics() const override
{ return false; }
264 void getTargetDefines(const LangOptions
&Opts
,
265 MacroBuilder
&Builder
) const override
;
267 BuiltinVaListKind
getBuiltinVaListKind() const override
{
268 return TargetInfo::CharPtrBuiltinVaList
;
271 bool isValidCPUName(StringRef Name
) const override
{
272 if (getTriple().getArch() == llvm::Triple::amdgcn
)
273 return llvm::AMDGPU::parseArchAMDGCN(Name
) != llvm::AMDGPU::GK_NONE
;
274 return llvm::AMDGPU::parseArchR600(Name
) != llvm::AMDGPU::GK_NONE
;
277 void fillValidCPUList(SmallVectorImpl
<StringRef
> &Values
) const override
;
279 bool setCPU(const std::string
&Name
) override
{
280 if (getTriple().getArch() == llvm::Triple::amdgcn
) {
281 GPUKind
= llvm::AMDGPU::parseArchAMDGCN(Name
);
282 GPUFeatures
= llvm::AMDGPU::getArchAttrAMDGCN(GPUKind
);
284 GPUKind
= llvm::AMDGPU::parseArchR600(Name
);
285 GPUFeatures
= llvm::AMDGPU::getArchAttrR600(GPUKind
);
288 return GPUKind
!= llvm::AMDGPU::GK_NONE
;
291 void setSupportedOpenCLOpts() override
{
292 auto &Opts
= getSupportedOpenCLOpts();
293 Opts
["cl_clang_storage_class_specifiers"] = true;
294 Opts
["__cl_clang_variadic_functions"] = true;
295 Opts
["__cl_clang_function_pointers"] = true;
296 Opts
["__cl_clang_non_portable_kernel_param_types"] = true;
297 Opts
["__cl_clang_bitfields"] = true;
299 bool IsAMDGCN
= isAMDGCN(getTriple());
301 Opts
["cl_khr_fp64"] = hasFP64();
302 Opts
["__opencl_c_fp64"] = hasFP64();
304 if (IsAMDGCN
|| GPUKind
>= llvm::AMDGPU::GK_CEDAR
) {
305 Opts
["cl_khr_byte_addressable_store"] = true;
306 Opts
["cl_khr_global_int32_base_atomics"] = true;
307 Opts
["cl_khr_global_int32_extended_atomics"] = true;
308 Opts
["cl_khr_local_int32_base_atomics"] = true;
309 Opts
["cl_khr_local_int32_extended_atomics"] = true;
313 Opts
["cl_khr_fp16"] = true;
314 Opts
["cl_khr_int64_base_atomics"] = true;
315 Opts
["cl_khr_int64_extended_atomics"] = true;
316 Opts
["cl_khr_mipmap_image"] = true;
317 Opts
["cl_khr_mipmap_image_writes"] = true;
318 Opts
["cl_khr_subgroups"] = true;
319 Opts
["cl_amd_media_ops"] = true;
320 Opts
["cl_amd_media_ops2"] = true;
322 Opts
["__opencl_c_images"] = true;
323 Opts
["__opencl_c_3d_image_writes"] = true;
324 Opts
["cl_khr_3d_image_writes"] = true;
328 LangAS
getOpenCLTypeAddrSpace(OpenCLTypeKind TK
) const override
{
331 return LangAS::opencl_constant
;
335 case OCLTK_ReserveID
:
336 return LangAS::opencl_global
;
339 return TargetInfo::getOpenCLTypeAddrSpace(TK
);
343 LangAS
getOpenCLBuiltinAddressSpace(unsigned AS
) const override
{
346 return LangAS::opencl_generic
;
348 return LangAS::opencl_global
;
350 return LangAS::opencl_local
;
352 return LangAS::opencl_constant
;
354 return LangAS::opencl_private
;
356 return getLangASFromTargetAS(AS
);
360 LangAS
getCUDABuiltinAddressSpace(unsigned AS
) const override
{
363 return LangAS::Default
;
365 return LangAS::cuda_device
;
367 return LangAS::cuda_shared
;
369 return LangAS::cuda_constant
;
371 return getLangASFromTargetAS(AS
);
375 std::optional
<LangAS
> getConstantAddressSpace() const override
{
376 return getLangASFromTargetAS(llvm::AMDGPUAS::CONSTANT_ADDRESS
);
379 const llvm::omp::GV
&getGridValue() const override
{
380 switch (WavefrontSize
) {
382 return llvm::omp::getAMDGPUGridValues
<32>();
384 return llvm::omp::getAMDGPUGridValues
<64>();
386 llvm_unreachable("getGridValue not implemented for this wavesize");
390 /// \returns Target specific vtbl ptr address space.
391 unsigned getVtblPtrAddressSpace() const override
{
392 return static_cast<unsigned>(llvm::AMDGPUAS::CONSTANT_ADDRESS
);
395 /// \returns If a target requires an address within a target specific address
396 /// space \p AddressSpace to be converted in order to be used, then return the
397 /// corresponding target specific DWARF address space.
399 /// \returns Otherwise return std::nullopt and no conversion will be emitted
401 std::optional
<unsigned>
402 getDWARFAddressSpace(unsigned AddressSpace
) const override
{
403 const unsigned DWARF_Private
= 1;
404 const unsigned DWARF_Local
= 2;
405 if (AddressSpace
== llvm::AMDGPUAS::PRIVATE_ADDRESS
) {
406 return DWARF_Private
;
407 } else if (AddressSpace
== llvm::AMDGPUAS::LOCAL_ADDRESS
) {
414 CallingConvCheckResult
checkCallingConvention(CallingConv CC
) const override
{
419 case CC_OpenCLKernel
:
420 case CC_AMDGPUKernelCall
:
425 // In amdgcn target the null pointer in global, constant, and generic
426 // address space has value 0 but in private and local address space has
428 uint64_t getNullPointerValue(LangAS AS
) const override
{
429 // FIXME: Also should handle region.
430 return (AS
== LangAS::opencl_local
|| AS
== LangAS::opencl_private
||
431 AS
== LangAS::sycl_local
|| AS
== LangAS::sycl_private
)
436 void setAuxTarget(const TargetInfo
*Aux
) override
;
438 bool hasBitIntType() const override
{ return true; }
440 // Record offload arch features since they are needed for defining the
441 // pre-defined macros.
442 bool handleTargetFeatures(std::vector
<std::string
> &Features
,
443 DiagnosticsEngine
&Diags
) override
{
444 auto TargetIDFeatures
=
445 getAllPossibleTargetIDFeatures(getTriple(), getArchNameAMDGCN(GPUKind
));
446 for (const auto &F
: Features
) {
447 assert(F
.front() == '+' || F
.front() == '-');
448 if (F
== "+wavefrontsize64")
450 else if (F
== "+cumode")
452 else if (F
== "-cumode")
454 else if (F
== "+image-insts")
456 bool IsOn
= F
.front() == '+';
457 StringRef Name
= StringRef(F
).drop_front();
458 if (!llvm::is_contained(TargetIDFeatures
, Name
))
460 assert(!OffloadArchFeatures
.contains(Name
));
461 OffloadArchFeatures
[Name
] = IsOn
;
466 std::optional
<std::string
> getTargetID() const override
{
467 if (!isAMDGCN(getTriple()))
469 // When -target-cpu is not set, we assume generic code that it is valid
470 // for all GPU and use an empty string as target ID to represent that.
471 if (GPUKind
== llvm::AMDGPU::GK_NONE
)
472 return std::string("");
473 return getCanonicalTargetID(getArchNameAMDGCN(GPUKind
),
474 OffloadArchFeatures
);
477 bool hasHIPImageSupport() const override
{ return HasImage
; }
479 std::pair
<unsigned, unsigned> hardwareInterferenceSizes() const override
{
480 // This is imprecise as the value can vary between 64, 128 (even 256!) bytes
481 // depending on the level of cache and the target architecture. We select
482 // the size that corresponds to the largest L1 cache line for all
484 return std::make_pair(128, 128);
488 } // namespace targets
491 #endif // LLVM_CLANG_LIB_BASIC_TARGETS_AMDGPU_H