clang  20.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 
16 #include "clang/Basic/TargetID.h"
17 #include "clang/Basic/TargetInfo.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"
24 #include <optional>
25 
26 namespace clang {
27 namespace targets {
28 
29 class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final : public TargetInfo {
30 
31  static const char *const GCCRegNames[];
32 
33  static const LangASMap AMDGPUDefIsGenMap;
34  static const LangASMap AMDGPUDefIsPrivMap;
35 
36  llvm::AMDGPU::GPUKind GPUKind;
37  unsigned GPUFeatures;
38  unsigned WavefrontSize;
39 
40  /// Whether to use cumode or WGP mode. True for cumode. False for WGP mode.
41  bool CUMode;
42 
43  /// Whether having image instructions.
44  bool HasImage = false;
45 
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;
52  std::string TargetID;
53 
54  bool hasFP64() const {
55  return getTriple().getArch() == llvm::Triple::amdgcn ||
56  !!(GPUFeatures & llvm::AMDGPU::FEATURE_FP64);
57  }
58 
59  /// Has fast fma f32
60  bool hasFastFMAF() const {
61  return !!(GPUFeatures & llvm::AMDGPU::FEATURE_FAST_FMA_F32);
62  }
63 
64  /// Has fast fma f64
65  bool hasFastFMA() const {
66  return getTriple().getArch() == llvm::Triple::amdgcn;
67  }
68 
69  bool hasFMAF() const {
70  return getTriple().getArch() == llvm::Triple::amdgcn ||
71  !!(GPUFeatures & llvm::AMDGPU::FEATURE_FMA);
72  }
73 
74  bool hasFullRateDenormalsF32() const {
75  return !!(GPUFeatures & llvm::AMDGPU::FEATURE_FAST_DENORMAL_F32);
76  }
77 
78  bool hasLDEXPF() const {
79  return getTriple().getArch() == llvm::Triple::amdgcn ||
80  !!(GPUFeatures & llvm::AMDGPU::FEATURE_LDEXP);
81  }
82 
83  static bool isAMDGCN(const llvm::Triple &TT) {
84  return TT.getArch() == llvm::Triple::amdgcn;
85  }
86 
87  static bool isR600(const llvm::Triple &TT) {
88  return TT.getArch() == llvm::Triple::r600;
89  }
90 
91 public:
92  AMDGPUTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts);
93 
94  void setAddressSpaceMap(bool DefaultIsPrivate);
95 
96  void adjust(DiagnosticsEngine &Diags, LangOptions &Opts) override;
97 
98  uint64_t getPointerWidthV(LangAS AS) const override {
99  if (isR600(getTriple()))
100  return 32;
101  unsigned TargetAS = getTargetAddressSpace(AS);
102 
103  if (TargetAS == llvm::AMDGPUAS::PRIVATE_ADDRESS ||
104  TargetAS == llvm::AMDGPUAS::LOCAL_ADDRESS)
105  return 32;
106 
107  return 64;
108  }
109 
110  uint64_t getPointerAlignV(LangAS AddrSpace) const override {
111  return getPointerWidthV(AddrSpace);
112  }
113 
114  uint64_t getMaxPointerWidth() const override {
115  return getTriple().getArch() == llvm::Triple::amdgcn ? 64 : 32;
116  }
117 
118  bool hasBFloat16Type() const override { return isAMDGCN(getTriple()); }
119 
120  std::string_view getClobbers() const override { return ""; }
121 
122  ArrayRef<const char *> getGCCRegNames() const override;
123 
125  return std::nullopt;
126  }
127 
128  /// Accepted register names: (n, m is unsigned integer, n < m)
129  /// v
130  /// s
131  /// a
132  /// {vn}, {v[n]}
133  /// {sn}, {s[n]}
134  /// {an}, {a[n]}
135  /// {S} , where S is a special register name
136  ////{v[n:m]}
137  /// {s[n:m]}
138  /// {a[n:m]}
139  bool validateAsmConstraint(const char *&Name,
140  TargetInfo::ConstraintInfo &Info) const override {
141  static const ::llvm::StringSet<> SpecialRegs({
142  "exec", "vcc", "flat_scratch", "m0", "scc", "tba", "tma",
143  "flat_scratch_lo", "flat_scratch_hi", "vcc_lo", "vcc_hi", "exec_lo",
144  "exec_hi", "tma_lo", "tma_hi", "tba_lo", "tba_hi",
145  });
146 
147  switch (*Name) {
148  case 'I':
149  Info.setRequiresImmediate(-16, 64);
150  return true;
151  case 'J':
152  Info.setRequiresImmediate(-32768, 32767);
153  return true;
154  case 'A':
155  case 'B':
156  case 'C':
157  Info.setRequiresImmediate();
158  return true;
159  default:
160  break;
161  }
162 
163  StringRef S(Name);
164 
165  if (S == "DA" || S == "DB") {
166  Name++;
167  Info.setRequiresImmediate();
168  return true;
169  }
170 
171  bool HasLeftParen = S.consume_front("{");
172  if (S.empty())
173  return false;
174  if (S.front() != 'v' && S.front() != 's' && S.front() != 'a') {
175  if (!HasLeftParen)
176  return false;
177  auto E = S.find('}');
178  if (!SpecialRegs.count(S.substr(0, E)))
179  return false;
180  S = S.drop_front(E + 1);
181  if (!S.empty())
182  return false;
183  // Found {S} where S is a special register.
184  Info.setAllowsRegister();
185  Name = S.data() - 1;
186  return true;
187  }
188  S = S.drop_front();
189  if (!HasLeftParen) {
190  if (!S.empty())
191  return false;
192  // Found s, v or a.
193  Info.setAllowsRegister();
194  Name = S.data() - 1;
195  return true;
196  }
197  bool HasLeftBracket = S.consume_front("[");
198  unsigned long long N;
199  if (S.empty() || consumeUnsignedInteger(S, 10, N))
200  return false;
201  if (S.consume_front(":")) {
202  if (!HasLeftBracket)
203  return false;
204  unsigned long long M;
205  if (consumeUnsignedInteger(S, 10, M) || N >= M)
206  return false;
207  }
208  if (HasLeftBracket) {
209  if (!S.consume_front("]"))
210  return false;
211  }
212  if (!S.consume_front("}"))
213  return false;
214  if (!S.empty())
215  return false;
216  // Found {vn}, {sn}, {an}, {v[n]}, {s[n]}, {a[n]}, {v[n:m]}, {s[n:m]}
217  // or {a[n:m]}.
218  Info.setAllowsRegister();
219  Name = S.data() - 1;
220  return true;
221  }
222 
223  // \p Constraint will be left pointing at the last character of
224  // the constraint. In practice, it won't be changed unless the
225  // constraint is longer than one character.
226  std::string convertConstraint(const char *&Constraint) const override {
227 
228  StringRef S(Constraint);
229  if (S == "DA" || S == "DB") {
230  return std::string("^") + std::string(Constraint++, 2);
231  }
232 
233  const char *Begin = Constraint;
234  TargetInfo::ConstraintInfo Info("", "");
235  if (validateAsmConstraint(Constraint, Info))
236  return std::string(Begin).substr(0, Constraint - Begin + 1);
237 
238  Constraint = Begin;
239  return std::string(1, *Constraint);
240  }
241 
242  bool
243  initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
244  StringRef CPU,
245  const std::vector<std::string> &FeatureVec) const override;
246 
247  ArrayRef<Builtin::Info> getTargetBuiltins() const override;
248 
249  bool useFP16ConversionIntrinsics() const override { return false; }
250 
251  void getTargetDefines(const LangOptions &Opts,
252  MacroBuilder &Builder) const override;
253 
256  }
257 
258  bool isValidCPUName(StringRef Name) const override {
259  if (getTriple().getArch() == llvm::Triple::amdgcn)
260  return llvm::AMDGPU::parseArchAMDGCN(Name) != llvm::AMDGPU::GK_NONE;
261  return llvm::AMDGPU::parseArchR600(Name) != llvm::AMDGPU::GK_NONE;
262  }
263 
264  void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;
265 
266  bool setCPU(const std::string &Name) override {
267  if (getTriple().getArch() == llvm::Triple::amdgcn) {
268  GPUKind = llvm::AMDGPU::parseArchAMDGCN(Name);
269  GPUFeatures = llvm::AMDGPU::getArchAttrAMDGCN(GPUKind);
270  } else {
271  GPUKind = llvm::AMDGPU::parseArchR600(Name);
272  GPUFeatures = llvm::AMDGPU::getArchAttrR600(GPUKind);
273  }
274 
275  return GPUKind != llvm::AMDGPU::GK_NONE;
276  }
277 
278  void setSupportedOpenCLOpts() override {
279  auto &Opts = getSupportedOpenCLOpts();
280  Opts["cl_clang_storage_class_specifiers"] = true;
281  Opts["__cl_clang_variadic_functions"] = true;
282  Opts["__cl_clang_function_pointers"] = true;
283  Opts["__cl_clang_non_portable_kernel_param_types"] = true;
284  Opts["__cl_clang_bitfields"] = true;
285 
286  bool IsAMDGCN = isAMDGCN(getTriple());
287 
288  Opts["cl_khr_fp64"] = hasFP64();
289  Opts["__opencl_c_fp64"] = hasFP64();
290 
291  if (IsAMDGCN || GPUKind >= llvm::AMDGPU::GK_CEDAR) {
292  Opts["cl_khr_byte_addressable_store"] = true;
293  Opts["cl_khr_global_int32_base_atomics"] = true;
294  Opts["cl_khr_global_int32_extended_atomics"] = true;
295  Opts["cl_khr_local_int32_base_atomics"] = true;
296  Opts["cl_khr_local_int32_extended_atomics"] = true;
297  }
298 
299  if (IsAMDGCN) {
300  Opts["cl_khr_fp16"] = true;
301  Opts["cl_khr_int64_base_atomics"] = true;
302  Opts["cl_khr_int64_extended_atomics"] = true;
303  Opts["cl_khr_mipmap_image"] = true;
304  Opts["cl_khr_mipmap_image_writes"] = true;
305  Opts["cl_khr_subgroups"] = true;
306  Opts["cl_amd_media_ops"] = true;
307  Opts["cl_amd_media_ops2"] = true;
308 
309  Opts["__opencl_c_images"] = true;
310  Opts["__opencl_c_3d_image_writes"] = true;
311  Opts["cl_khr_3d_image_writes"] = true;
312  }
313  }
314 
316  switch (TK) {
317  case OCLTK_Image:
319 
320  case OCLTK_ClkEvent:
321  case OCLTK_Queue:
322  case OCLTK_ReserveID:
323  return LangAS::opencl_global;
324  case OCLTK_Event:
325  return LangAS::opencl_private;
326 
327  default:
329  }
330  }
331 
332  LangAS getOpenCLBuiltinAddressSpace(unsigned AS) const override {
333  switch (AS) {
334  case 0:
335  return LangAS::opencl_generic;
336  case 1:
337  return LangAS::opencl_global;
338  case 3:
339  return LangAS::opencl_local;
340  case 4:
342  case 5:
343  return LangAS::opencl_private;
344  default:
345  return getLangASFromTargetAS(AS);
346  }
347  }
348 
349  LangAS getCUDABuiltinAddressSpace(unsigned AS) const override {
350  switch (AS) {
351  case 0:
352  return LangAS::Default;
353  case 1:
354  return LangAS::cuda_device;
355  case 3:
356  return LangAS::cuda_shared;
357  case 4:
358  return LangAS::cuda_constant;
359  default:
360  return getLangASFromTargetAS(AS);
361  }
362  }
363 
364  std::optional<LangAS> getConstantAddressSpace() const override {
365  return getLangASFromTargetAS(llvm::AMDGPUAS::CONSTANT_ADDRESS);
366  }
367 
368  const llvm::omp::GV &getGridValue() const override {
369  switch (WavefrontSize) {
370  case 32:
371  return llvm::omp::getAMDGPUGridValues<32>();
372  case 64:
373  return llvm::omp::getAMDGPUGridValues<64>();
374  default:
375  llvm_unreachable("getGridValue not implemented for this wavesize");
376  }
377  }
378 
379  /// \returns Target specific vtbl ptr address space.
380  unsigned getVtblPtrAddressSpace() const override {
381  return static_cast<unsigned>(llvm::AMDGPUAS::CONSTANT_ADDRESS);
382  }
383 
384  /// \returns If a target requires an address within a target specific address
385  /// space \p AddressSpace to be converted in order to be used, then return the
386  /// corresponding target specific DWARF address space.
387  ///
388  /// \returns Otherwise return std::nullopt and no conversion will be emitted
389  /// in the DWARF.
390  std::optional<unsigned>
391  getDWARFAddressSpace(unsigned AddressSpace) const override {
392  const unsigned DWARF_Private = 1;
393  const unsigned DWARF_Local = 2;
394  if (AddressSpace == llvm::AMDGPUAS::PRIVATE_ADDRESS) {
395  return DWARF_Private;
396  } else if (AddressSpace == llvm::AMDGPUAS::LOCAL_ADDRESS) {
397  return DWARF_Local;
398  } else {
399  return std::nullopt;
400  }
401  }
402 
404  switch (CC) {
405  default:
406  return CCCR_Warning;
407  case CC_C:
408  case CC_OpenCLKernel:
409  case CC_AMDGPUKernelCall:
410  return CCCR_OK;
411  }
412  }
413 
414  // In amdgcn target the null pointer in global, constant, and generic
415  // address space has value 0 but in private and local address space has
416  // value ~0.
417  uint64_t getNullPointerValue(LangAS AS) const override {
418  // FIXME: Also should handle region.
419  return (AS == LangAS::opencl_local || AS == LangAS::opencl_private ||
421  ? ~0
422  : 0;
423  }
424 
425  void setAuxTarget(const TargetInfo *Aux) override;
426 
427  bool hasBitIntType() const override { return true; }
428 
429  // Record offload arch features since they are needed for defining the
430  // pre-defined macros.
431  bool handleTargetFeatures(std::vector<std::string> &Features,
432  DiagnosticsEngine &Diags) override {
433  auto TargetIDFeatures =
434  getAllPossibleTargetIDFeatures(getTriple(), getArchNameAMDGCN(GPUKind));
435  for (const auto &F : Features) {
436  assert(F.front() == '+' || F.front() == '-');
437  if (F == "+wavefrontsize64")
438  WavefrontSize = 64;
439  else if (F == "+cumode")
440  CUMode = true;
441  else if (F == "-cumode")
442  CUMode = false;
443  else if (F == "+image-insts")
444  HasImage = true;
445  bool IsOn = F.front() == '+';
446  StringRef Name = StringRef(F).drop_front();
447  if (!llvm::is_contained(TargetIDFeatures, Name))
448  continue;
449  assert(!OffloadArchFeatures.contains(Name));
450  OffloadArchFeatures[Name] = IsOn;
451  }
452  return true;
453  }
454 
455  std::optional<std::string> getTargetID() const override {
456  if (!isAMDGCN(getTriple()))
457  return std::nullopt;
458  // When -target-cpu is not set, we assume generic code that it is valid
459  // for all GPU and use an empty string as target ID to represent that.
460  if (GPUKind == llvm::AMDGPU::GK_NONE)
461  return std::string("");
462  return getCanonicalTargetID(getArchNameAMDGCN(GPUKind),
463  OffloadArchFeatures);
464  }
465 
466  bool hasHIPImageSupport() const override { return HasImage; }
467 };
468 
469 } // namespace targets
470 } // namespace clang
471 
472 #endif // LLVM_CLANG_LIB_BASIC_TARGETS_AMDGPU_H
Expr * E
Defines the clang::TargetOptions class.
SourceLocation Begin
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:193
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:480
Exposes information about the current target.
Definition: TargetInfo.h:218
BuiltinVaListKind
The different kinds of __builtin_va_list types defined by the target implementation.
Definition: TargetInfo.h:319
@ CharPtrBuiltinVaList
typedef char* __builtin_va_list;
Definition: TargetInfo.h:321
virtual LangAS getOpenCLTypeAddrSpace(OpenCLTypeKind TK) const
Get address space for OpenCL type.
Definition: TargetInfo.cpp:638
Options for controlling the target.
Definition: TargetOptions.h:26
std::optional< std::string > getTargetID() const override
Returns the target ID if supported.
Definition: AMDGPU.h:455
std::optional< LangAS > getConstantAddressSpace() const override
Return an AST address space which can be used opportunistically for constant global memory.
Definition: AMDGPU.h:364
std::string_view getClobbers() const override
Returns a string of target-specific clobbers, in LLVM format.
Definition: AMDGPU.h:120
uint64_t getPointerAlignV(LangAS AddrSpace) const override
Definition: AMDGPU.h:110
LangAS getOpenCLBuiltinAddressSpace(unsigned AS) const override
Map from the address space field in builtin description strings to the language address space.
Definition: AMDGPU.h:332
uint64_t getPointerWidthV(LangAS AS) const override
Definition: AMDGPU.h:98
std::optional< unsigned > getDWARFAddressSpace(unsigned AddressSpace) const override
Definition: AMDGPU.h:391
bool setCPU(const std::string &Name) override
Target the specified CPU.
Definition: AMDGPU.h:266
bool hasBFloat16Type() const override
Determine whether the _BFloat16 type is supported on this target.
Definition: AMDGPU.h:118
uint64_t getNullPointerValue(LangAS AS) const override
Get integer value for null pointer.
Definition: AMDGPU.h:417
LangAS getCUDABuiltinAddressSpace(unsigned AS) const override
Map from the address space field in builtin description strings to the language address space.
Definition: AMDGPU.h:349
const llvm::omp::GV & getGridValue() const override
Definition: AMDGPU.h:368
bool useFP16ConversionIntrinsics() const override
Check whether llvm intrinsics such as llvm.convert.to.fp16 should be used to convert to and from __fp...
Definition: AMDGPU.h:249
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:431
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:139
std::string convertConstraint(const char *&Constraint) const override
Definition: AMDGPU.h:226
unsigned getVtblPtrAddressSpace() const override
Definition: AMDGPU.h:380
bool isValidCPUName(StringRef Name) const override
Determine whether this TargetInfo supports the given CPU name.
Definition: AMDGPU.h:258
bool hasBitIntType() const override
Determine whether the _BitInt type is supported on this target.
Definition: AMDGPU.h:427
ArrayRef< TargetInfo::GCCRegAlias > getGCCRegAliases() const override
Definition: AMDGPU.h:124
bool hasHIPImageSupport() const override
Whether to support HIP image/texture API's.
Definition: AMDGPU.h:466
uint64_t getMaxPointerWidth() const override
Return the maximum width of pointers on this target.
Definition: AMDGPU.h:114
CallingConvCheckResult checkCallingConvention(CallingConv CC) const override
Determines whether a given calling convention is valid for the target.
Definition: AMDGPU.h:403
LangAS getOpenCLTypeAddrSpace(OpenCLTypeKind TK) const override
Get address space for OpenCL type.
Definition: AMDGPU.h:315
void setSupportedOpenCLOpts() override
Set supported OpenCL extensions and optional core features.
Definition: AMDGPU.h:278
BuiltinVaListKind getBuiltinVaListKind() const override
Returns the kind of __builtin_va_list type that should be used with this target.
Definition: AMDGPU.h:254
Defines the clang::TargetInfo interface.
static const char *const GCCRegNames[]
Definition: X86.cpp:44
The JSON file list parser is used to communicate input to InstallAPI.
unsigned[(unsigned) LangAS::FirstTargetAddressSpace] LangASMap
The type of a lookup table which maps from language-specific address spaces to target-specific ones.
Definition: AddressSpaces.h:73
OpenCLTypeKind
OpenCL type kinds.
Definition: TargetInfo.h:204
@ OCLTK_ReserveID
Definition: TargetInfo.h:211
@ OCLTK_Image
Definition: TargetInfo.h:208
@ OCLTK_ClkEvent
Definition: TargetInfo.h:206
@ OCLTK_Event
Definition: TargetInfo.h:207
@ OCLTK_Queue
Definition: TargetInfo.h:210
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:38
LangAS
Defines the address space values used by the address space qualifier of QualType.
Definition: AddressSpaces.h:25
std::string getCanonicalTargetID(llvm::StringRef Processor, const llvm::StringMap< bool > &Features)
Returns canonical target ID, assuming Processor is canonical and all entries in Features are valid.
Definition: TargetID.cpp:130
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:278
@ CC_OpenCLKernel
Definition: Specifiers.h:292
@ CC_C
Definition: Specifiers.h:279
@ CC_AMDGPUKernelCall
Definition: Specifiers.h:299
LangAS getLangASFromTargetAS(unsigned TargetAS)
Definition: AddressSpaces.h:86
unsigned long uint64_t
void setRequiresImmediate(int Min, int Max)
Definition: TargetInfo.h:1153