clang  19.0.0git
NVPTX.cpp
Go to the documentation of this file.
1 //===--- NVPTX.cpp - Implement NVPTX target feature support ---------------===//
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 implements NVPTX TargetInfo objects.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "NVPTX.h"
14 #include "Targets.h"
15 #include "clang/Basic/Builtins.h"
18 #include "llvm/ADT/StringSwitch.h"
19 
20 using namespace clang;
21 using namespace clang::targets;
22 
23 static constexpr Builtin::Info BuiltinInfo[] = {
24 #define BUILTIN(ID, TYPE, ATTRS) \
25  {#ID, TYPE, ATTRS, nullptr, HeaderDesc::NO_HEADER, ALL_LANGUAGES},
26 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) \
27  {#ID, TYPE, ATTRS, nullptr, HeaderDesc::HEADER, ALL_LANGUAGES},
28 #define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) \
29  {#ID, TYPE, ATTRS, FEATURE, HeaderDesc::NO_HEADER, ALL_LANGUAGES},
30 #include "clang/Basic/BuiltinsNVPTX.def"
31 };
32 
33 const char *const NVPTXTargetInfo::GCCRegNames[] = {"r0"};
34 
35 NVPTXTargetInfo::NVPTXTargetInfo(const llvm::Triple &Triple,
36  const TargetOptions &Opts,
37  unsigned TargetPointerWidth)
38  : TargetInfo(Triple) {
39  assert((TargetPointerWidth == 32 || TargetPointerWidth == 64) &&
40  "NVPTX only supports 32- and 64-bit modes.");
41 
42  PTXVersion = 32;
43  for (const StringRef Feature : Opts.FeaturesAsWritten) {
44  int PTXV;
45  if (!Feature.starts_with("+ptx") ||
46  Feature.drop_front(4).getAsInteger(10, PTXV))
47  continue;
48  PTXVersion = PTXV; // TODO: should it be max(PTXVersion, PTXV)?
49  }
50 
51  TLSSupported = false;
52  VLASupported = false;
55  HasLegalHalfType = true;
56  HasFloat16 = true;
57  // __bf16 is always available as a load/store only type.
60 
61  // Define available target features
62  // These must be defined in sorted order!
63  NoAsmVariants = true;
64  GPU = CudaArch::UNUSED;
65 
66  // PTX supports f16 as a fundamental type.
67  HasLegalHalfType = true;
68  HasFloat16 = true;
69 
70  if (TargetPointerWidth == 32)
71  resetDataLayout("e-p:32:32-i64:64-i128:128-v16:16-v32:32-n16:32:64");
72  else if (Opts.NVPTXUseShortPointers)
74  "e-p3:32:32-p4:32:32-p5:32:32-i64:64-i128:128-v16:16-v32:32-n16:32:64");
75  else
76  resetDataLayout("e-i64:64-i128:128-v16:16-v32:32-n16:32:64");
77 
78  // If possible, get a TargetInfo for our host triple, so we can match its
79  // types.
80  llvm::Triple HostTriple(Opts.HostTriple);
81  if (!HostTriple.isNVPTX())
82  HostTarget = AllocateTarget(llvm::Triple(Opts.HostTriple), Opts);
83 
84  // If no host target, make some guesses about the data layout and return.
85  if (!HostTarget) {
86  LongWidth = LongAlign = TargetPointerWidth;
87  PointerWidth = PointerAlign = TargetPointerWidth;
88  switch (TargetPointerWidth) {
89  case 32:
93  break;
94  case 64:
98  break;
99  default:
100  llvm_unreachable("TargetPointerWidth must be 32 or 64");
101  }
102 
103  MaxAtomicInlineWidth = TargetPointerWidth;
104  return;
105  }
106 
107  // Copy properties from host target.
108  PointerWidth = HostTarget->getPointerWidth(LangAS::Default);
109  PointerAlign = HostTarget->getPointerAlign(LangAS::Default);
110  BoolWidth = HostTarget->getBoolWidth();
111  BoolAlign = HostTarget->getBoolAlign();
112  IntWidth = HostTarget->getIntWidth();
113  IntAlign = HostTarget->getIntAlign();
114  HalfWidth = HostTarget->getHalfWidth();
115  HalfAlign = HostTarget->getHalfAlign();
116  FloatWidth = HostTarget->getFloatWidth();
117  FloatAlign = HostTarget->getFloatAlign();
118  DoubleWidth = HostTarget->getDoubleWidth();
119  DoubleAlign = HostTarget->getDoubleAlign();
120  LongWidth = HostTarget->getLongWidth();
121  LongAlign = HostTarget->getLongAlign();
122  LongLongWidth = HostTarget->getLongLongWidth();
123  LongLongAlign = HostTarget->getLongLongAlign();
124  MinGlobalAlign = HostTarget->getMinGlobalAlign(/* TypeSize = */ 0,
125  /* HasNonWeakDef = */ true);
126  NewAlign = HostTarget->getNewAlign();
128  HostTarget->getDefaultAlignForAttributeAligned();
129  SizeType = HostTarget->getSizeType();
130  IntMaxType = HostTarget->getIntMaxType();
131  PtrDiffType = HostTarget->getPtrDiffType(LangAS::Default);
132  IntPtrType = HostTarget->getIntPtrType();
133  WCharType = HostTarget->getWCharType();
134  WIntType = HostTarget->getWIntType();
135  Char16Type = HostTarget->getChar16Type();
136  Char32Type = HostTarget->getChar32Type();
137  Int64Type = HostTarget->getInt64Type();
138  SigAtomicType = HostTarget->getSigAtomicType();
139  ProcessIDType = HostTarget->getProcessIDType();
140 
141  UseBitFieldTypeAlignment = HostTarget->useBitFieldTypeAlignment();
142  UseZeroLengthBitfieldAlignment = HostTarget->useZeroLengthBitfieldAlignment();
143  UseExplicitBitFieldAlignment = HostTarget->useExplicitBitFieldAlignment();
144  ZeroLengthBitfieldBoundary = HostTarget->getZeroLengthBitfieldBoundary();
145 
146  // This is a bit of a lie, but it controls __GCC_ATOMIC_XXX_LOCK_FREE, and
147  // we need those macros to be identical on host and device, because (among
148  // other things) they affect which standard library classes are defined, and
149  // we need all classes to be defined on both the host and device.
150  MaxAtomicInlineWidth = HostTarget->getMaxAtomicInlineWidth();
151 
152  // Properties intentionally not copied from host:
153  // - LargeArrayMinWidth, LargeArrayAlign: Not visible across the
154  // host/device boundary.
155  // - SuitableAlign: Not visible across the host/device boundary, and may
156  // correctly be different on host/device, e.g. if host has wider vector
157  // types than device.
158  // - LongDoubleWidth, LongDoubleAlign: nvptx's long double type is the same
159  // as its double type, but that's not necessarily true on the host.
160  // TODO: nvcc emits a warning when using long double on device; we should
161  // do the same.
162 }
163 
165  return llvm::ArrayRef(GCCRegNames);
166 }
167 
168 bool NVPTXTargetInfo::hasFeature(StringRef Feature) const {
169  return llvm::StringSwitch<bool>(Feature)
170  .Cases("ptx", "nvptx", true)
171  .Default(false);
172 }
173 
175  MacroBuilder &Builder) const {
176  Builder.defineMacro("__PTX__");
177  Builder.defineMacro("__NVPTX__");
178 
179  // Skip setting architecture dependent macros if undefined.
180  if (GPU == CudaArch::UNUSED && !HostTarget)
181  return;
182 
183  if (Opts.CUDAIsDevice || Opts.OpenMPIsTargetDevice || Opts.SYCLIsDevice ||
184  !HostTarget) {
185  // Set __CUDA_ARCH__ for the GPU specified.
186  // The SYCL-specific macro is used to distinguish the SYCL and CUDA APIs.
187  std::string CUDAArchCode = [this] {
188  switch (GPU) {
189  case CudaArch::GFX600:
190  case CudaArch::GFX601:
191  case CudaArch::GFX602:
192  case CudaArch::GFX700:
193  case CudaArch::GFX701:
194  case CudaArch::GFX702:
195  case CudaArch::GFX703:
196  case CudaArch::GFX704:
197  case CudaArch::GFX705:
198  case CudaArch::GFX801:
199  case CudaArch::GFX802:
200  case CudaArch::GFX803:
201  case CudaArch::GFX805:
202  case CudaArch::GFX810:
203  case CudaArch::GFX900:
204  case CudaArch::GFX902:
205  case CudaArch::GFX904:
206  case CudaArch::GFX906:
207  case CudaArch::GFX908:
208  case CudaArch::GFX909:
209  case CudaArch::GFX90a:
210  case CudaArch::GFX90c:
211  case CudaArch::GFX940:
212  case CudaArch::GFX941:
213  case CudaArch::GFX942:
214  case CudaArch::GFX1010:
215  case CudaArch::GFX1011:
216  case CudaArch::GFX1012:
217  case CudaArch::GFX1013:
218  case CudaArch::GFX1030:
219  case CudaArch::GFX1031:
220  case CudaArch::GFX1032:
221  case CudaArch::GFX1033:
222  case CudaArch::GFX1034:
223  case CudaArch::GFX1035:
224  case CudaArch::GFX1036:
225  case CudaArch::GFX1100:
226  case CudaArch::GFX1101:
227  case CudaArch::GFX1102:
228  case CudaArch::GFX1103:
229  case CudaArch::GFX1150:
230  case CudaArch::GFX1151:
231  case CudaArch::GFX1200:
232  case CudaArch::GFX1201:
233  case CudaArch::Generic:
234  case CudaArch::LAST:
235  break;
236  case CudaArch::UNKNOWN:
237  assert(false && "No GPU arch when compiling CUDA device code.");
238  return "";
239  case CudaArch::UNUSED:
240  case CudaArch::SM_20:
241  return "200";
242  case CudaArch::SM_21:
243  return "210";
244  case CudaArch::SM_30:
245  return "300";
246  case CudaArch::SM_32_:
247  return "320";
248  case CudaArch::SM_35:
249  return "350";
250  case CudaArch::SM_37:
251  return "370";
252  case CudaArch::SM_50:
253  return "500";
254  case CudaArch::SM_52:
255  return "520";
256  case CudaArch::SM_53:
257  return "530";
258  case CudaArch::SM_60:
259  return "600";
260  case CudaArch::SM_61:
261  return "610";
262  case CudaArch::SM_62:
263  return "620";
264  case CudaArch::SM_70:
265  return "700";
266  case CudaArch::SM_72:
267  return "720";
268  case CudaArch::SM_75:
269  return "750";
270  case CudaArch::SM_80:
271  return "800";
272  case CudaArch::SM_86:
273  return "860";
274  case CudaArch::SM_87:
275  return "870";
276  case CudaArch::SM_89:
277  return "890";
278  case CudaArch::SM_90:
279  case CudaArch::SM_90a:
280  return "900";
281  }
282  llvm_unreachable("unhandled CudaArch");
283  }();
284 
285  if (Opts.SYCLIsDevice) {
286  Builder.defineMacro("__SYCL_CUDA_ARCH__", CUDAArchCode);
287  } else {
288  Builder.defineMacro("__CUDA_ARCH__", CUDAArchCode);
289  }
290  if (GPU == CudaArch::SM_90a)
291  Builder.defineMacro("__CUDA_ARCH_FEAT_SM90_ALL", "1");
292  }
293 }
294 
298 }
static constexpr Builtin::Info BuiltinInfo[]
Definition: NVPTX.cpp:23
Defines enum values for all the target-independent builtin functions.
Defines the clang::MacroBuilder utility class.
Enumerates target-specific builtins in their own namespaces within namespace clang.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:482
Exposes information about the current target.
Definition: TargetInfo.h:218
const LangASMap * AddrSpaceMap
Definition: TargetInfo.h:248
void resetDataLayout(StringRef DL, const char *UserLabelPrefix="")
Definition: TargetInfo.cpp:190
bool UseAddrSpaceMapMangling
Specify if mangling based on address space map should be used or not for language specific address sp...
Definition: TargetInfo.h:368
unsigned char MaxAtomicInlineWidth
Definition: TargetInfo.h:242
Options for controlling the target.
Definition: TargetOptions.h:26
std::string HostTriple
When compiling for the device side, contains the triple used to compile for the host.
Definition: TargetOptions.h:33
std::vector< std::string > FeaturesAsWritten
The list of target specific features to enable or disable, as written on the command line.
Definition: TargetOptions.h:54
bool NVPTXUseShortPointers
If enabled, use 32-bit pointers for accessing const/local/shared address space.
Definition: TargetOptions.h:76
NVPTXTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts, unsigned TargetPointerWidth)
Definition: NVPTX.cpp:35
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
Definition: NVPTX.cpp:174
ArrayRef< Builtin::Info > getTargetBuiltins() const override
Return information about target-specific builtins for the current primary target, and info about whic...
Definition: NVPTX.cpp:295
bool hasFeature(StringRef Feature) const override
Determine whether the given target has the given feature.
Definition: NVPTX.cpp:168
ArrayRef< const char * > getGCCRegNames() const override
Definition: NVPTX.cpp:164
static const unsigned NVPTXAddrSpaceMap[]
Definition: NVPTX.h:26
std::unique_ptr< TargetInfo > AllocateTarget(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: Targets.cpp:112
The JSON file list parser is used to communicate input to InstallAPI.
unsigned UseZeroLengthBitfieldAlignment
Whether zero length bitfields (e.g., int : 0;) force alignment of the next bitfield.
Definition: TargetInfo.h:183
unsigned UseExplicitBitFieldAlignment
Whether explicit bit field alignment attributes are honored.
Definition: TargetInfo.h:192
unsigned ZeroLengthBitfieldBoundary
If non-zero, specifies a fixed alignment value for bitfields that follow zero length bitfield,...
Definition: TargetInfo.h:196
unsigned UseBitFieldTypeAlignment
Control whether the alignment of bit-field types is respected when laying out structures.
Definition: TargetInfo.h:174
const llvm::fltSemantics * BFloat16Format
Definition: TargetInfo.h:138
unsigned char DefaultAlignForAttributeAligned
Definition: TargetInfo.h:130