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