clang 24.0.0git
AMDGPU.h
Go to the documentation of this file.
1//===--- AMDGPU.h - Declare AMDGPU target feature support -------*- 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// This file declares AMDGPU TargetInfo objects.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_LIB_BASIC_TARGETS_AMDGPU_H
14#define LLVM_CLANG_LIB_BASIC_TARGETS_AMDGPU_H
15
19#include "llvm/ADT/StringSet.h"
20#include "llvm/Support/AMDGPUAddrSpace.h"
21#include "llvm/Support/Compiler.h"
22#include "llvm/TargetParser/AMDGPUTargetParser.h"
23#include "llvm/TargetParser/Triple.h"
24#include <optional>
25
26namespace clang {
27namespace targets {
28
29class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final : public TargetInfo {
30
31 static const char *const GCCRegNames[];
32
33 static const LangASMap AMDGPUAddrSpaceMap;
34
35 llvm::AMDGPU::GPUKind GPUKind;
36 unsigned GPUFeatures;
37 unsigned WavefrontSize;
38
39 /// Whether to use cumode or WGP mode. True for cumode. False for WGP mode.
40 bool CUMode;
41
42 /// Whether having image instructions.
43 bool HasImage = false;
44
45 /// Target ID is device name followed by optional feature name postfixed
46 /// by plus or minus sign delimitted by colon, e.g. gfx908:xnack+:sramecc-.
47 /// If the target ID contains feature+, map it to true.
48 /// If the target ID contains feature-, map it to false.
49 /// If the target ID does not contain a feature (default), do not map it.
50 llvm::StringMap<bool> OffloadArchFeatures;
51 std::string TargetID;
52
53 bool hasFP64() const { return getTriple().isAMDGCN(); }
54
55 /// Has fast fma f32
56 bool hasFastFMAF() const {
57 return getTriple().isAMDGCN() &&
58 llvm::AMDGPU::getFeatureBitset(GPUKind).test(
59 llvm::AMDGPU::FEAT_FAST_FMAF);
60 }
61
62 /// Has fast fma f64
63 bool hasFastFMA() const { return getTriple().isAMDGCN(); }
64
65 bool hasFMAF() const {
66 return getTriple().isAMDGCN() ||
67 !!(GPUFeatures & llvm::AMDGPU::R600_FEATURE_FMA);
68 }
69
70 bool hasFullRateDenormalsF32() const {
71 return getTriple().isAMDGCN() &&
72 llvm::AMDGPU::getFeatureBitset(GPUKind).test(
73 llvm::AMDGPU::FEAT_FAST_DENORMAL_F32);
74 }
75
76 bool hasLDEXPF() const { return getTriple().isAMDGCN(); }
77
78 static bool isR600(const llvm::Triple &TT) {
79 return TT.getArch() == llvm::Triple::r600;
80 }
81
82 bool hasFlatSupport() const {
83 if (GPUKind >= llvm::AMDGPU::GK_GFX700)
84 return true;
85
86 // Dummy target is assumed to be gfx700+ for amdhsa.
87 if (GPUKind == llvm::AMDGPU::GK_NONE &&
88 getTriple().getOS() == llvm::Triple::AMDHSA)
89 return true;
90
91 return false;
92 }
93
94public:
95 AMDGPUTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts);
96
97 void adjust(DiagnosticsEngine &Diags, LangOptions &Opts,
98 const TargetInfo *Aux) override;
99
100 uint64_t getPointerWidthV(LangAS AS) const override {
101 if (isR600(getTriple()))
102 return 32;
103 unsigned TargetAS = getTargetAddressSpace(AS);
104
105 if (TargetAS == llvm::AMDGPUAS::PRIVATE_ADDRESS ||
106 TargetAS == llvm::AMDGPUAS::LOCAL_ADDRESS)
107 return 32;
108
109 return 64;
110 }
111
112 uint64_t getPointerAlignV(LangAS AddrSpace) const override {
113 return getPointerWidthV(AddrSpace);
114 }
115
116 virtual bool isAddressSpaceSupersetOf(LangAS A, LangAS B) const override {
117 // The flat address space AS(0) is a superset of all the other address
118 // spaces used by the backend target.
119 return A == B ||
120 ((A == LangAS::Default ||
122 toTargetAddressSpace(A) == llvm::AMDGPUAS::FLAT_ADDRESS)) &&
124 toTargetAddressSpace(B) >= llvm::AMDGPUAS::FLAT_ADDRESS &&
125 toTargetAddressSpace(B) <= llvm::AMDGPUAS::PRIVATE_ADDRESS &&
126 toTargetAddressSpace(B) != llvm::AMDGPUAS::REGION_ADDRESS);
127 }
128
129 uint64_t getMaxPointerWidth() const override {
130 return getTriple().isAMDGCN() ? 64 : 32;
131 }
132
133 bool hasBFloat16Type() const override { return getTriple().isAMDGCN(); }
134
135 std::string_view getClobbers() const override { return ""; }
136
137 ArrayRef<const char *> getGCCRegNames() const override;
138
140 return {};
141 }
142
143 /// Accepted register names: (n, m is unsigned integer, n < m)
144 /// v
145 /// s
146 /// a
147 /// {vn}, {v[n]}
148 /// {sn}, {s[n]}
149 /// {an}, {a[n]}
150 /// {S} , where S is a special register name
151 ////{v[n:m]}
152 /// {s[n:m]}
153 /// {a[n:m]}
154 bool validateAsmConstraint(const char *&Name,
155 TargetInfo::ConstraintInfo &Info) const override {
156 static const ::llvm::StringSet<> SpecialRegs({
157 "exec", "vcc", "flat_scratch", "m0", "scc", "tba", "tma",
158 "flat_scratch_lo", "flat_scratch_hi", "vcc_lo", "vcc_hi", "exec_lo",
159 "exec_hi", "tma_lo", "tma_hi", "tba_lo", "tba_hi",
160 });
161
162 switch (*Name) {
163 case 'I':
164 Info.setRequiresImmediate(-16, 64);
165 return true;
166 case 'J':
167 Info.setRequiresImmediate(-32768, 32767);
168 return true;
169 case 'A':
170 case 'B':
171 case 'C':
173 return true;
174 default:
175 break;
176 }
177
178 StringRef S(Name);
179
180 if (S == "DA" || S == "DB") {
181 Name++;
183 return true;
184 }
185
186 bool HasLeftParen = S.consume_front("{");
187 if (S.empty())
188 return false;
189 if (S.front() != 'v' && S.front() != 's' && S.front() != 'a') {
190 if (!HasLeftParen)
191 return false;
192 auto E = S.find('}');
193 if (!SpecialRegs.count(S.substr(0, E)))
194 return false;
195 S = S.drop_front(E + 1);
196 if (!S.empty())
197 return false;
198 // Found {S} where S is a special register.
199 Info.setAllowsRegister();
200 Name = S.data() - 1;
201 return true;
202 }
203 S = S.drop_front();
204 if (!HasLeftParen) {
205 if (!S.empty())
206 return false;
207 // Found s, v or a.
208 Info.setAllowsRegister();
209 Name = S.data() - 1;
210 return true;
211 }
212 bool HasLeftBracket = S.consume_front("[");
213 unsigned long long N;
214 if (S.empty() || consumeUnsignedInteger(S, 10, N))
215 return false;
216 if (S.consume_front(":")) {
217 if (!HasLeftBracket)
218 return false;
219 unsigned long long M;
220 if (consumeUnsignedInteger(S, 10, M) || N >= M)
221 return false;
222 }
223 if (HasLeftBracket) {
224 if (!S.consume_front("]"))
225 return false;
226 }
227 if (!S.consume_front("}"))
228 return false;
229 if (!S.empty())
230 return false;
231 // Found {vn}, {sn}, {an}, {v[n]}, {s[n]}, {a[n]}, {v[n:m]}, {s[n:m]}
232 // or {a[n:m]}.
233 Info.setAllowsRegister();
234 Name = S.data() - 1;
235 return true;
236 }
237
238 // \p Constraint will be left pointing at the last character of
239 // the constraint. In practice, it won't be changed unless the
240 // constraint is longer than one character.
241 std::string convertConstraint(const char *&Constraint) const override {
242
243 StringRef S(Constraint);
244 if (S == "DA" || S == "DB") {
245 return std::string("^") + std::string(Constraint++, 2);
246 }
247
248 const char *Begin = Constraint;
249 TargetInfo::ConstraintInfo Info("", "");
250 if (validateAsmConstraint(Constraint, Info))
251 return std::string(Begin).substr(0, Constraint - Begin + 1);
252
253 Constraint = Begin;
254 return std::string(1, *Constraint);
255 }
256
257 bool
258 initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
259 StringRef CPU,
260 const std::vector<std::string> &FeatureVec) const override;
261
262 llvm::SmallVector<Builtin::InfosShard> getTargetBuiltins() const override;
263
264 void getTargetDefines(const LangOptions &Opts,
265 MacroBuilder &Builder) const override;
266
270
271 bool isValidCPUName(StringRef Name) const override {
272 if (getTriple().isAMDGCN()) {
273 return llvm::AMDGPU::isCPUValidForSubArch(getTriple().getSubArch(),
274 Name) &&
275 !llvm::AMDGPU::isPseudoTarget(Name);
276 }
277 return llvm::AMDGPU::parseArchR600(Name) != llvm::AMDGPU::GK_NONE;
278 }
279
280 void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;
281
282 bool setCPU(StringRef Name) override {
283 if (getTriple().isAMDGCN()) {
284 GPUKind = llvm::AMDGPU::parseArchAMDGCN(Name);
285 GPUFeatures = llvm::AMDGPU::FEATURE_NONE;
286 return llvm::AMDGPU::isCPUValidForSubArch(getTriple().getSubArch(),
287 GPUKind) &&
288 !llvm::AMDGPU::isPseudoTarget(GPUKind);
289 }
290 GPUKind = llvm::AMDGPU::parseArchR600(Name);
291 GPUFeatures = llvm::AMDGPU::getArchAttrR600(GPUKind);
292 return GPUKind != llvm::AMDGPU::GK_NONE;
293 }
294
295 void setSupportedOpenCLOpts() override {
296 auto &Opts = getSupportedOpenCLOpts();
297 Opts["cl_clang_storage_class_specifiers"] = true;
298 Opts["__cl_clang_variadic_functions"] = true;
299 Opts["__cl_clang_function_pointers"] = true;
300 Opts["__cl_clang_function_scope_local_variables"] = true;
301 Opts["__cl_clang_non_portable_kernel_param_types"] = true;
302 Opts["__cl_clang_bitfields"] = true;
303
304 bool IsAMDGCN = getTriple().isAMDGCN();
305
306 Opts["cl_khr_fp64"] = hasFP64();
307 Opts["__opencl_c_fp64"] = hasFP64();
308
309 if (IsAMDGCN || GPUKind >= llvm::AMDGPU::GK_CEDAR) {
310 Opts["cl_khr_byte_addressable_store"] = true;
311 Opts["cl_khr_global_int32_base_atomics"] = true;
312 Opts["cl_khr_global_int32_extended_atomics"] = true;
313 Opts["cl_khr_local_int32_base_atomics"] = true;
314 Opts["cl_khr_local_int32_extended_atomics"] = true;
315 }
316
317 if (IsAMDGCN) {
318 Opts["cl_khr_fp16"] = true;
319 Opts["cl_khr_int64_base_atomics"] = true;
320 Opts["cl_khr_int64_extended_atomics"] = true;
321 Opts["cl_khr_mipmap_image"] = true;
322 Opts["cl_khr_mipmap_image_writes"] = true;
323 Opts["cl_khr_subgroups"] = true;
324 Opts["cl_amd_media_ops"] = true;
325 Opts["cl_amd_media_ops2"] = true;
326
327 // FIXME: Check subtarget for image support.
328 Opts["__opencl_c_images"] = true;
329 Opts["__opencl_c_3d_image_writes"] = true;
330 Opts["__opencl_c_read_write_images"] = true;
331 Opts["cl_khr_3d_image_writes"] = true;
332 Opts["__opencl_c_program_scope_global_variables"] = true;
333 Opts["__opencl_c_atomic_order_acq_rel"] = true;
334 Opts["__opencl_c_atomic_order_seq_cst"] = true;
335 Opts["__opencl_c_atomic_scope_device"] = true;
336 Opts["__opencl_c_atomic_scope_all_devices"] = true;
337 Opts["__opencl_c_work_group_collective_functions"] = true;
338
339 if (hasFlatSupport()) {
340 Opts["__opencl_c_generic_address_space"] = true;
341 Opts["__opencl_c_device_enqueue"] = true;
342 Opts["__opencl_c_pipes"] = true;
343 }
344
345 if (getTriple().getEnvironment() == llvm::Triple::LLVM) {
346 Opts["cl_khr_subgroup_extended_types"] = true;
347 }
348 }
349 }
350
352 switch (TK) {
353 case OCLTK_Image:
355
356 case OCLTK_ClkEvent:
357 case OCLTK_Queue:
358 case OCLTK_ReserveID:
360
361 default:
363 }
364 }
365
366 LangAS getOpenCLBuiltinAddressSpace(unsigned AS) const override {
367 switch (AS) {
368 case 0:
370 case 1:
372 case 3:
374 case 4:
376 case 5:
378 default:
379 return getLangASFromTargetAS(AS);
380 }
381 }
382
383 LangAS getCUDABuiltinAddressSpace(unsigned AS) const override {
384 switch (AS) {
385 case 0:
386 return LangAS::Default;
387 case 1:
388 return LangAS::cuda_device;
389 case 3:
390 return LangAS::cuda_shared;
391 case 4:
393 default:
394 return getLangASFromTargetAS(AS);
395 }
396 }
397
398 std::optional<LangAS> getConstantAddressSpace() const override {
399 return getLangASFromTargetAS(llvm::AMDGPUAS::CONSTANT_ADDRESS);
400 }
401
402 const llvm::omp::GV &getGridValue() const override {
403 switch (WavefrontSize) {
404 case 32:
405 return llvm::omp::getAMDGPUGridValues<32>();
406 case 64:
407 return llvm::omp::getAMDGPUGridValues<64>();
408 default:
409 llvm_unreachable("getGridValue not implemented for this wavesize");
410 }
411 }
412
413 /// \returns Target specific vtbl ptr address space.
414 unsigned getVtblPtrAddressSpace() const override {
415 return static_cast<unsigned>(llvm::AMDGPUAS::CONSTANT_ADDRESS);
416 }
417
418 /// \returns If a target requires an address within a target specific address
419 /// space \p AddressSpace to be converted in order to be used, then return the
420 /// corresponding target specific DWARF address space.
421 ///
422 /// \returns Otherwise return std::nullopt and no conversion will be emitted
423 /// in the DWARF.
424 std::optional<unsigned>
425 getDWARFAddressSpace(unsigned AddressSpace) const override {
426 int DWARFAS = llvm::AMDGPU::mapToDWARFAddrSpace(AddressSpace);
427 // If there is no corresponding address space identifier, or it would be
428 // the default, then don't emit the attribute.
429 if (DWARFAS == -1 || DWARFAS == llvm::AMDGPU::DWARFAS::DEFAULT)
430 return std::nullopt;
431 return DWARFAS;
432 }
433
435 switch (CC) {
436 default:
437 return CCCR_Warning;
438 case CC_C:
439 case CC_DeviceKernel:
440 return CCCR_OK;
441 }
442 }
443
444 // In amdgcn target the null pointer in global, constant, and generic
445 // address space has value 0 but in private and local address space has
446 // value ~0.
447 uint64_t getNullPointerValue(LangAS AS) const override {
448 // Check language-specific address spaces
449 if (AS == LangAS::opencl_local || AS == LangAS::opencl_private ||
451 return ~0;
452 if (isTargetAddressSpace(AS))
453 return llvm::AMDGPU::getNullPointerValue(toTargetAddressSpace(AS));
454 return 0;
455 }
456
457 void setAuxTarget(const TargetInfo *Aux) override;
458
459 bool hasBitIntType() const override { return true; }
460
461 // Record offload arch features since they are needed for defining the
462 // pre-defined macros.
463 bool handleTargetFeatures(std::vector<std::string> &Features,
464 DiagnosticsEngine &Diags) override {
465 HasFullBFloat16 = true;
466 auto TargetIDFeatures =
467 getAllPossibleTargetIDFeatures(getTriple(), getArchNameAMDGCN(GPUKind));
468 for (const auto &F : Features) {
469 assert(F.front() == '+' || F.front() == '-');
470 if (F == "+wavefrontsize64")
471 WavefrontSize = 64;
472 else if (F == "+cumode")
473 CUMode = true;
474 else if (F == "-cumode")
475 CUMode = false;
476 else if (F == "+image-insts")
477 HasImage = true;
478 bool IsOn = F.front() == '+';
479 StringRef Name = StringRef(F).drop_front();
480 if (!llvm::is_contained(TargetIDFeatures, Name))
481 continue;
482 assert(!OffloadArchFeatures.contains(Name));
483 OffloadArchFeatures[Name] = IsOn;
484 }
485 return true;
486 }
487
488 bool isProcessorName(StringRef Name) const override {
489 llvm::AMDGPU::GPUKind NameKind = getTriple().isAMDGCN()
490 ? llvm::AMDGPU::parseArchAMDGCN(Name)
491 : llvm::AMDGPU::parseArchR600(Name);
492 return NameKind == GPUKind;
493 }
494
495 bool hasHIPImageSupport() const override { return HasImage; }
496
497 std::pair<unsigned, unsigned> hardwareInterferenceSizes() const override {
498 // This is imprecise as the value can vary between 64, 128 (even 256!) bytes
499 // depending on the level of cache and the target architecture. We select
500 // the size that corresponds to the largest L1 cache line for all
501 // architectures.
502 return std::make_pair(128, 128);
503 }
504};
505
506} // namespace targets
507} // namespace clang
508
509#endif // LLVM_CLANG_LIB_BASIC_TARGETS_AMDGPU_H
static StringRef getTriple(const Command &Job)
Defines the clang::TargetOptions class.
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:234
The type of a lookup table which maps from language-specific address spaces to target-specific ones.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Exposes information about the current target.
Definition TargetInfo.h:226
TargetInfo(const llvm::Triple &T)
BuiltinVaListKind
The different kinds of __builtin_va_list types defined by the target implementation.
Definition TargetInfo.h:339
@ CharPtrBuiltinVaList
typedef char* __builtin_va_list;
Definition TargetInfo.h:341
virtual LangAS getOpenCLTypeAddrSpace(OpenCLTypeKind TK) const
Get address space for OpenCL type.
llvm::StringMap< bool > & getSupportedOpenCLOpts()
Get supported OpenCL extensions and optional core features.
unsigned getTargetAddressSpace(LangAS AS) const
Options for controlling the target.
const llvm::omp::GV & getGridValue() const override
Definition AMDGPU.h:402
std::string_view getClobbers() const override
Returns a string of target-specific clobbers, in LLVM format.
Definition AMDGPU.h:135
uint64_t getPointerAlignV(LangAS AddrSpace) const override
Definition AMDGPU.h:112
LangAS getOpenCLBuiltinAddressSpace(unsigned AS) const override
Map from the address space field in builtin description strings to the language address space.
Definition AMDGPU.h:366
AMDGPUTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition AMDGPU.cpp:192
uint64_t getPointerWidthV(LangAS AS) const override
Definition AMDGPU.h:100
bool isProcessorName(StringRef Name) const override
Returns true if the target's processor is compatible with the processor named by Name,...
Definition AMDGPU.h:488
ArrayRef< TargetInfo::GCCRegAlias > getGCCRegAliases() const override
Definition AMDGPU.h:139
bool hasBFloat16Type() const override
Determine whether the _BFloat16 type is supported on this target.
Definition AMDGPU.h:133
uint64_t getNullPointerValue(LangAS AS) const override
Get integer value for null pointer.
Definition AMDGPU.h:447
void adjust(DiagnosticsEngine &Diags, LangOptions &Opts, const TargetInfo *Aux) override
Set forced language options.
Definition AMDGPU.cpp:257
LangAS getCUDABuiltinAddressSpace(unsigned AS) const override
Map from the address space field in builtin description strings to the language address space.
Definition AMDGPU.h:383
virtual bool isAddressSpaceSupersetOf(LangAS A, LangAS B) const override
Returns true if an address space can be safely converted to another.
Definition AMDGPU.h:116
bool handleTargetFeatures(std::vector< std::string > &Features, DiagnosticsEngine &Diags) override
Perform initialization based on the user configured set of features (e.g., +sse4).
Definition AMDGPU.h:463
bool validateAsmConstraint(const char *&Name, TargetInfo::ConstraintInfo &Info) const override
Accepted register names: (n, m is unsigned integer, n < m) v s a {vn}, {v[n]} {sn}...
Definition AMDGPU.h:154
std::optional< unsigned > getDWARFAddressSpace(unsigned AddressSpace) const override
Definition AMDGPU.h:425
std::string convertConstraint(const char *&Constraint) const override
Definition AMDGPU.h:241
std::pair< unsigned, unsigned > hardwareInterferenceSizes() const override
The first value in the pair is the minimum offset between two objects to avoid false sharing (destruc...
Definition AMDGPU.h:497
unsigned getVtblPtrAddressSpace() const override
Definition AMDGPU.h:414
std::optional< LangAS > getConstantAddressSpace() const override
Return an AST address space which can be used opportunistically for constant global memory.
Definition AMDGPU.h:398
bool isValidCPUName(StringRef Name) const override
Determine whether this TargetInfo supports the given CPU name.
Definition AMDGPU.h:271
bool hasBitIntType() const override
Determine whether the _BitInt type is supported on this target.
Definition AMDGPU.h:459
bool hasHIPImageSupport() const override
Whether to support HIP image/texture API's.
Definition AMDGPU.h:495
bool setCPU(StringRef Name) override
Target the specified CPU.
Definition AMDGPU.h:282
uint64_t getMaxPointerWidth() const override
Return the maximum width of pointers on this target.
Definition AMDGPU.h:129
CallingConvCheckResult checkCallingConvention(CallingConv CC) const override
Determines whether a given calling convention is valid for the target.
Definition AMDGPU.h:434
LangAS getOpenCLTypeAddrSpace(OpenCLTypeKind TK) const override
Get address space for OpenCL type.
Definition AMDGPU.h:351
void setSupportedOpenCLOpts() override
Set supported OpenCL extensions and optional core features.
Definition AMDGPU.h:295
BuiltinVaListKind getBuiltinVaListKind() const override
Returns the kind of __builtin_va_list type that should be used with this target.
Definition AMDGPU.h:267
Defines the clang::TargetInfo interface.
Top level wrappers for InstallAPI frontend operations.
bool isTargetAddressSpace(LangAS AS)
OpenCLTypeKind
OpenCL type kinds.
Definition TargetInfo.h:212
@ OCLTK_ReserveID
Definition TargetInfo.h:219
@ OCLTK_Image
Definition TargetInfo.h:216
@ OCLTK_ClkEvent
Definition TargetInfo.h:214
@ OCLTK_Queue
Definition TargetInfo.h:218
unsigned toTargetAddressSpace(LangAS AS)
llvm::SmallVector< llvm::StringRef, 4 > getAllPossibleTargetIDFeatures(const llvm::Triple &T, llvm::StringRef Processor)
Get all feature strings that can be used in target ID for Processor.
Definition TargetID.cpp:43
LangAS
Defines the address space values used by the address space qualifier of QualType.
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition Specifiers.h:279
@ CC_DeviceKernel
Definition Specifiers.h:292
LangAS getLangASFromTargetAS(unsigned TargetAS)
void setRequiresImmediate(int Min, int Max)