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
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/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 {
54 return getTriple().isAMDGCN() ||
55 !!(GPUFeatures & llvm::AMDGPU::FEATURE_FP64);
56 }
57
58 /// Has fast fma f32
59 bool hasFastFMAF() const {
60 return !!(GPUFeatures & llvm::AMDGPU::FEATURE_FAST_FMA_F32);
61 }
62
63 /// Has fast fma f64
64 bool hasFastFMA() const { return getTriple().isAMDGCN(); }
65
66 bool hasFMAF() const {
67 return getTriple().isAMDGCN() ||
68 !!(GPUFeatures & llvm::AMDGPU::FEATURE_FMA);
69 }
70
71 bool hasFullRateDenormalsF32() const {
72 return !!(GPUFeatures & llvm::AMDGPU::FEATURE_FAST_DENORMAL_F32);
73 }
74
75 bool hasLDEXPF() const {
76 return getTriple().isAMDGCN() ||
77 !!(GPUFeatures & llvm::AMDGPU::FEATURE_LDEXP);
78 }
79
80 static bool isR600(const llvm::Triple &TT) {
81 return TT.getArch() == llvm::Triple::r600;
82 }
83
84 bool hasFlatSupport() const {
85 if (GPUKind >= llvm::AMDGPU::GK_GFX700)
86 return true;
87
88 // Dummy target is assumed to be gfx700+ for amdhsa.
89 if (GPUKind == llvm::AMDGPU::GK_NONE &&
90 getTriple().getOS() == llvm::Triple::AMDHSA)
91 return true;
92
93 return false;
94 }
95
96public:
97 AMDGPUTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts);
98
99 void adjust(DiagnosticsEngine &Diags, LangOptions &Opts,
100 const TargetInfo *Aux) override;
101
102 uint64_t getPointerWidthV(LangAS AS) const override {
103 if (isR600(TT: getTriple()))
104 return 32;
105 unsigned TargetAS = getTargetAddressSpace(AS);
106
107 if (TargetAS == llvm::AMDGPUAS::PRIVATE_ADDRESS ||
108 TargetAS == llvm::AMDGPUAS::LOCAL_ADDRESS)
109 return 32;
110
111 return 64;
112 }
113
114 uint64_t getPointerAlignV(LangAS AddrSpace) const override {
115 return getPointerWidthV(AS: AddrSpace);
116 }
117
118 virtual bool isAddressSpaceSupersetOf(LangAS A, LangAS B) const override {
119 // The flat address space AS(0) is a superset of all the other address
120 // spaces used by the backend target.
121 return A == B ||
122 ((A == LangAS::Default ||
123 (isTargetAddressSpace(AS: A) &&
124 toTargetAddressSpace(AS: A) == llvm::AMDGPUAS::FLAT_ADDRESS)) &&
125 isTargetAddressSpace(AS: B) &&
126 toTargetAddressSpace(AS: B) >= llvm::AMDGPUAS::FLAT_ADDRESS &&
127 toTargetAddressSpace(AS: B) <= llvm::AMDGPUAS::PRIVATE_ADDRESS &&
128 toTargetAddressSpace(AS: B) != llvm::AMDGPUAS::REGION_ADDRESS);
129 }
130
131 uint64_t getMaxPointerWidth() const override {
132 return getTriple().isAMDGCN() ? 64 : 32;
133 }
134
135 bool hasBFloat16Type() const override { return getTriple().isAMDGCN(); }
136
137 std::string_view getClobbers() const override { return ""; }
138
139 ArrayRef<const char *> getGCCRegNames() const override;
140
141 ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const override {
142 return {};
143 }
144
145 /// Accepted register names: (n, m is unsigned integer, n < m)
146 /// v
147 /// s
148 /// a
149 /// {vn}, {v[n]}
150 /// {sn}, {s[n]}
151 /// {an}, {a[n]}
152 /// {S} , where S is a special register name
153 ////{v[n:m]}
154 /// {s[n:m]}
155 /// {a[n:m]}
156 bool validateAsmConstraint(const char *&Name,
157 TargetInfo::ConstraintInfo &Info) const override {
158 static const ::llvm::StringSet<> SpecialRegs({
159 "exec", "vcc", "flat_scratch", "m0", "scc", "tba", "tma",
160 "flat_scratch_lo", "flat_scratch_hi", "vcc_lo", "vcc_hi", "exec_lo",
161 "exec_hi", "tma_lo", "tma_hi", "tba_lo", "tba_hi",
162 });
163
164 switch (*Name) {
165 case 'I':
166 Info.setRequiresImmediate(Min: -16, Max: 64);
167 return true;
168 case 'J':
169 Info.setRequiresImmediate(Min: -32768, Max: 32767);
170 return true;
171 case 'A':
172 case 'B':
173 case 'C':
174 Info.setRequiresImmediate();
175 return true;
176 default:
177 break;
178 }
179
180 StringRef S(Name);
181
182 if (S == "DA" || S == "DB") {
183 Name++;
184 Info.setRequiresImmediate();
185 return true;
186 }
187
188 bool HasLeftParen = S.consume_front(Prefix: "{");
189 if (S.empty())
190 return false;
191 if (S.front() != 'v' && S.front() != 's' && S.front() != 'a') {
192 if (!HasLeftParen)
193 return false;
194 auto E = S.find(C: '}');
195 if (!SpecialRegs.count(Key: S.substr(Start: 0, N: E)))
196 return false;
197 S = S.drop_front(N: E + 1);
198 if (!S.empty())
199 return false;
200 // Found {S} where S is a special register.
201 Info.setAllowsRegister();
202 Name = S.data() - 1;
203 return true;
204 }
205 S = S.drop_front();
206 if (!HasLeftParen) {
207 if (!S.empty())
208 return false;
209 // Found s, v or a.
210 Info.setAllowsRegister();
211 Name = S.data() - 1;
212 return true;
213 }
214 bool HasLeftBracket = S.consume_front(Prefix: "[");
215 unsigned long long N;
216 if (S.empty() || consumeUnsignedInteger(Str&: S, Radix: 10, Result&: N))
217 return false;
218 if (S.consume_front(Prefix: ":")) {
219 if (!HasLeftBracket)
220 return false;
221 unsigned long long M;
222 if (consumeUnsignedInteger(Str&: S, Radix: 10, Result&: M) || N >= M)
223 return false;
224 }
225 if (HasLeftBracket) {
226 if (!S.consume_front(Prefix: "]"))
227 return false;
228 }
229 if (!S.consume_front(Prefix: "}"))
230 return false;
231 if (!S.empty())
232 return false;
233 // Found {vn}, {sn}, {an}, {v[n]}, {s[n]}, {a[n]}, {v[n:m]}, {s[n:m]}
234 // or {a[n:m]}.
235 Info.setAllowsRegister();
236 Name = S.data() - 1;
237 return true;
238 }
239
240 // \p Constraint will be left pointing at the last character of
241 // the constraint. In practice, it won't be changed unless the
242 // constraint is longer than one character.
243 std::string convertConstraint(const char *&Constraint) const override {
244
245 StringRef S(Constraint);
246 if (S == "DA" || S == "DB") {
247 return std::string("^") + std::string(Constraint++, 2);
248 }
249
250 const char *Begin = Constraint;
251 TargetInfo::ConstraintInfo Info("", "");
252 if (validateAsmConstraint(Name&: Constraint, Info))
253 return std::string(Begin).substr(pos: 0, n: Constraint - Begin + 1);
254
255 Constraint = Begin;
256 return std::string(1, *Constraint);
257 }
258
259 bool
260 initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
261 StringRef CPU,
262 const std::vector<std::string> &FeatureVec) const override;
263
264 llvm::SmallVector<Builtin::InfosShard> getTargetBuiltins() const override;
265
266 void getTargetDefines(const LangOptions &Opts,
267 MacroBuilder &Builder) const override;
268
269 BuiltinVaListKind getBuiltinVaListKind() const override {
270 return TargetInfo::CharPtrBuiltinVaList;
271 }
272
273 bool isValidCPUName(StringRef Name) const override {
274 if (getTriple().isAMDGCN())
275 return llvm::AMDGPU::isCPUValidForSubArch(SubArch: getTriple().getSubArch(), CPU: Name);
276 return llvm::AMDGPU::parseArchR600(CPU: Name) != llvm::AMDGPU::GK_NONE;
277 }
278
279 void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;
280
281 bool setCPU(StringRef Name) override {
282 if (getTriple().isAMDGCN()) {
283 GPUKind = llvm::AMDGPU::parseArchAMDGCN(CPU: Name);
284 GPUFeatures = llvm::AMDGPU::getArchAttrAMDGCN(AK: GPUKind);
285 // Reject a CPU whose subarch is incompatible with the triple's subarch.
286 return llvm::AMDGPU::isCPUValidForSubArch(SubArch: getTriple().getSubArch(),
287 AK: GPUKind);
288 }
289 GPUKind = llvm::AMDGPU::parseArchR600(CPU: Name);
290 GPUFeatures = llvm::AMDGPU::getArchAttrR600(AK: GPUKind);
291 return GPUKind != llvm::AMDGPU::GK_NONE;
292 }
293
294 void setSupportedOpenCLOpts() override {
295 auto &Opts = getSupportedOpenCLOpts();
296 Opts["cl_clang_storage_class_specifiers"] = true;
297 Opts["__cl_clang_variadic_functions"] = true;
298 Opts["__cl_clang_function_pointers"] = true;
299 Opts["__cl_clang_function_scope_local_variables"] = true;
300 Opts["__cl_clang_non_portable_kernel_param_types"] = true;
301 Opts["__cl_clang_bitfields"] = true;
302
303 bool IsAMDGCN = getTriple().isAMDGCN();
304
305 Opts["cl_khr_fp64"] = hasFP64();
306 Opts["__opencl_c_fp64"] = hasFP64();
307
308 if (IsAMDGCN || GPUKind >= llvm::AMDGPU::GK_CEDAR) {
309 Opts["cl_khr_byte_addressable_store"] = true;
310 Opts["cl_khr_global_int32_base_atomics"] = true;
311 Opts["cl_khr_global_int32_extended_atomics"] = true;
312 Opts["cl_khr_local_int32_base_atomics"] = true;
313 Opts["cl_khr_local_int32_extended_atomics"] = true;
314 }
315
316 if (IsAMDGCN) {
317 Opts["cl_khr_fp16"] = true;
318 Opts["cl_khr_int64_base_atomics"] = true;
319 Opts["cl_khr_int64_extended_atomics"] = true;
320 Opts["cl_khr_mipmap_image"] = true;
321 Opts["cl_khr_mipmap_image_writes"] = true;
322 Opts["cl_khr_subgroups"] = true;
323 Opts["cl_amd_media_ops"] = true;
324 Opts["cl_amd_media_ops2"] = true;
325
326 // FIXME: Check subtarget for image support.
327 Opts["__opencl_c_images"] = true;
328 Opts["__opencl_c_3d_image_writes"] = true;
329 Opts["__opencl_c_read_write_images"] = true;
330 Opts["cl_khr_3d_image_writes"] = true;
331 Opts["__opencl_c_program_scope_global_variables"] = true;
332 Opts["__opencl_c_atomic_order_acq_rel"] = true;
333 Opts["__opencl_c_atomic_order_seq_cst"] = true;
334 Opts["__opencl_c_atomic_scope_device"] = true;
335 Opts["__opencl_c_atomic_scope_all_devices"] = true;
336 Opts["__opencl_c_work_group_collective_functions"] = true;
337
338 if (hasFlatSupport()) {
339 Opts["__opencl_c_generic_address_space"] = true;
340 Opts["__opencl_c_device_enqueue"] = true;
341 Opts["__opencl_c_pipes"] = true;
342 }
343
344 if (getTriple().getEnvironment() == llvm::Triple::LLVM) {
345 Opts["cl_khr_subgroup_extended_types"] = true;
346 }
347 }
348 }
349
350 LangAS getOpenCLTypeAddrSpace(OpenCLTypeKind TK) const override {
351 switch (TK) {
352 case OCLTK_Image:
353 return LangAS::opencl_constant;
354
355 case OCLTK_ClkEvent:
356 case OCLTK_Queue:
357 case OCLTK_ReserveID:
358 return LangAS::opencl_global;
359
360 default:
361 return TargetInfo::getOpenCLTypeAddrSpace(TK);
362 }
363 }
364
365 LangAS getOpenCLBuiltinAddressSpace(unsigned AS) const override {
366 switch (AS) {
367 case 0:
368 return LangAS::opencl_generic;
369 case 1:
370 return LangAS::opencl_global;
371 case 3:
372 return LangAS::opencl_local;
373 case 4:
374 return LangAS::opencl_constant;
375 case 5:
376 return LangAS::opencl_private;
377 default:
378 return getLangASFromTargetAS(TargetAS: AS);
379 }
380 }
381
382 LangAS getCUDABuiltinAddressSpace(unsigned AS) const override {
383 switch (AS) {
384 case 0:
385 return LangAS::Default;
386 case 1:
387 return LangAS::cuda_device;
388 case 3:
389 return LangAS::cuda_shared;
390 case 4:
391 return LangAS::cuda_constant;
392 default:
393 return getLangASFromTargetAS(TargetAS: AS);
394 }
395 }
396
397 std::optional<LangAS> getConstantAddressSpace() const override {
398 return getLangASFromTargetAS(TargetAS: llvm::AMDGPUAS::CONSTANT_ADDRESS);
399 }
400
401 const llvm::omp::GV &getGridValue() const override {
402 switch (WavefrontSize) {
403 case 32:
404 return llvm::omp::getAMDGPUGridValues<32>();
405 case 64:
406 return llvm::omp::getAMDGPUGridValues<64>();
407 default:
408 llvm_unreachable("getGridValue not implemented for this wavesize");
409 }
410 }
411
412 /// \returns Target specific vtbl ptr address space.
413 unsigned getVtblPtrAddressSpace() const override {
414 return static_cast<unsigned>(llvm::AMDGPUAS::CONSTANT_ADDRESS);
415 }
416
417 /// \returns If a target requires an address within a target specific address
418 /// space \p AddressSpace to be converted in order to be used, then return the
419 /// corresponding target specific DWARF address space.
420 ///
421 /// \returns Otherwise return std::nullopt and no conversion will be emitted
422 /// in the DWARF.
423 std::optional<unsigned>
424 getDWARFAddressSpace(unsigned AddressSpace) const override {
425 int DWARFAS = llvm::AMDGPU::mapToDWARFAddrSpace(LLVMAddrSpace: AddressSpace);
426 // If there is no corresponding address space identifier, or it would be
427 // the default, then don't emit the attribute.
428 if (DWARFAS == -1 || DWARFAS == llvm::AMDGPU::DWARFAS::DEFAULT)
429 return std::nullopt;
430 return DWARFAS;
431 }
432
433 CallingConvCheckResult checkCallingConvention(CallingConv CC) const override {
434 switch (CC) {
435 default:
436 return CCCR_Warning;
437 case CC_C:
438 case CC_DeviceKernel:
439 return CCCR_OK;
440 }
441 }
442
443 // In amdgcn target the null pointer in global, constant, and generic
444 // address space has value 0 but in private and local address space has
445 // value ~0.
446 uint64_t getNullPointerValue(LangAS AS) const override {
447 // Check language-specific address spaces
448 if (AS == LangAS::opencl_local || AS == LangAS::opencl_private ||
449 AS == LangAS::sycl_local || AS == LangAS::sycl_private)
450 return ~0;
451 if (isTargetAddressSpace(AS))
452 return llvm::AMDGPU::getNullPointerValue(AS: toTargetAddressSpace(AS));
453 return 0;
454 }
455
456 void setAuxTarget(const TargetInfo *Aux) override;
457
458 bool hasBitIntType() const override { return true; }
459
460 // Record offload arch features since they are needed for defining the
461 // pre-defined macros.
462 bool handleTargetFeatures(std::vector<std::string> &Features,
463 DiagnosticsEngine &Diags) override {
464 HasFullBFloat16 = true;
465 auto TargetIDFeatures =
466 getAllPossibleTargetIDFeatures(T: getTriple(), Processor: getArchNameAMDGCN(AK: GPUKind));
467 for (const auto &F : Features) {
468 assert(F.front() == '+' || F.front() == '-');
469 if (F == "+wavefrontsize64")
470 WavefrontSize = 64;
471 else if (F == "+cumode")
472 CUMode = true;
473 else if (F == "-cumode")
474 CUMode = false;
475 else if (F == "+image-insts")
476 HasImage = true;
477 bool IsOn = F.front() == '+';
478 StringRef Name = StringRef(F).drop_front();
479 if (!llvm::is_contained(Range&: TargetIDFeatures, Element: Name))
480 continue;
481 assert(!OffloadArchFeatures.contains(Name));
482 OffloadArchFeatures[Name] = IsOn;
483 }
484 return true;
485 }
486
487 bool isProcessorName(StringRef Name) const override {
488 llvm::AMDGPU::GPUKind NameKind = getTriple().isAMDGCN()
489 ? llvm::AMDGPU::parseArchAMDGCN(CPU: Name)
490 : llvm::AMDGPU::parseArchR600(CPU: Name);
491 return NameKind == GPUKind;
492 }
493
494 bool hasHIPImageSupport() const override { return HasImage; }
495
496 std::pair<unsigned, unsigned> hardwareInterferenceSizes() const override {
497 // This is imprecise as the value can vary between 64, 128 (even 256!) bytes
498 // depending on the level of cache and the target architecture. We select
499 // the size that corresponds to the largest L1 cache line for all
500 // architectures.
501 return std::make_pair(x: 128, y: 128);
502 }
503};
504
505} // namespace targets
506} // namespace clang
507
508#endif // LLVM_CLANG_LIB_BASIC_TARGETS_AMDGPU_H
509