clang 22.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"
17#include "llvm/ADT/StringSwitch.h"
18
19using namespace clang;
20using namespace clang::targets;
21
22static constexpr int NumBuiltins =
24
25#define GET_BUILTIN_STR_TABLE
26#include "clang/Basic/BuiltinsNVPTX.inc"
27#undef GET_BUILTIN_STR_TABLE
28
29static constexpr Builtin::Info BuiltinInfos[] = {
30#define GET_BUILTIN_INFOS
31#include "clang/Basic/BuiltinsNVPTX.inc"
32#undef GET_BUILTIN_INFOS
33};
34static_assert(std::size(BuiltinInfos) == NumBuiltins);
35
36const char *const NVPTXTargetInfo::GCCRegNames[] = {"r0"};
37
38NVPTXTargetInfo::NVPTXTargetInfo(const llvm::Triple &Triple,
39 const TargetOptions &Opts,
40 unsigned TargetPointerWidth)
41 : TargetInfo(Triple) {
42 assert((TargetPointerWidth == 32 || TargetPointerWidth == 64) &&
43 "NVPTX only supports 32- and 64-bit modes.");
44
45 PTXVersion = 32;
46 for (const StringRef Feature : Opts.FeaturesAsWritten) {
47 int PTXV;
48 if (!Feature.starts_with("+ptx") ||
49 Feature.drop_front(4).getAsInteger(10, PTXV))
50 continue;
51 PTXVersion = PTXV; // TODO: should it be max(PTXVersion, PTXV)?
52 }
53
54 TLSSupported = false;
55 VLASupported = false;
58 // __bf16 is always available as a load/store only type.
60 BFloat16Format = &llvm::APFloat::BFloat();
61
62 // Define available target features
63 // These must be defined in sorted order!
64 NoAsmVariants = true;
66
67 // PTX supports f16 as a fundamental type.
68 HasFastHalfType = true;
69 HasFloat16 = true;
70
71 if (TargetPointerWidth == 32)
72 resetDataLayout("e-p:32:32-p6:32:32-p7:32:32-i64:64-i128:128-i256:256-v16:"
73 "16-v32:32-n16:32:64");
74 else if (Opts.NVPTXUseShortPointers)
75 resetDataLayout("e-p3:32:32-p4:32:32-p5:32:32-p6:32:32-p7:32:32-i64:64-"
76 "i128:128-i256:256-v16:"
77 "16-v32:32-n16:32:64");
78 else
80 "e-p6:32:32-i64:64-i128:128-i256:256-v16:16-v32:32-n16:32:64");
81
82 // If possible, get a TargetInfo for our host triple, so we can match its
83 // types.
84 llvm::Triple HostTriple(Opts.HostTriple);
85 if (!HostTriple.isNVPTX())
86 HostTarget = AllocateTarget(llvm::Triple(Opts.HostTriple), Opts);
87
88 // If no host target, make some guesses about the data layout and return.
89 if (!HostTarget) {
90 LongWidth = LongAlign = TargetPointerWidth;
91 PointerWidth = PointerAlign = TargetPointerWidth;
92 switch (TargetPointerWidth) {
93 case 32:
97 break;
98 case 64:
102 break;
103 default:
104 llvm_unreachable("TargetPointerWidth must be 32 or 64");
105 }
106
107 MaxAtomicInlineWidth = TargetPointerWidth;
108 return;
109 }
110
111 // Copy properties from host target.
112 PointerWidth = HostTarget->getPointerWidth(LangAS::Default);
113 PointerAlign = HostTarget->getPointerAlign(LangAS::Default);
114 BoolWidth = HostTarget->getBoolWidth();
115 BoolAlign = HostTarget->getBoolAlign();
116 IntWidth = HostTarget->getIntWidth();
117 IntAlign = HostTarget->getIntAlign();
118 HalfWidth = HostTarget->getHalfWidth();
119 HalfAlign = HostTarget->getHalfAlign();
120 FloatWidth = HostTarget->getFloatWidth();
121 FloatAlign = HostTarget->getFloatAlign();
122 DoubleWidth = HostTarget->getDoubleWidth();
123 DoubleAlign = HostTarget->getDoubleAlign();
124 LongWidth = HostTarget->getLongWidth();
125 LongAlign = HostTarget->getLongAlign();
126 LongLongWidth = HostTarget->getLongLongWidth();
127 LongLongAlign = HostTarget->getLongLongAlign();
128 MinGlobalAlign = HostTarget->getMinGlobalAlign(/* TypeSize = */ 0,
129 /* HasNonWeakDef = */ true);
130 NewAlign = HostTarget->getNewAlign();
132 HostTarget->getDefaultAlignForAttributeAligned();
133 SizeType = HostTarget->getSizeType();
134 IntMaxType = HostTarget->getIntMaxType();
135 PtrDiffType = HostTarget->getPtrDiffType(LangAS::Default);
136 IntPtrType = HostTarget->getIntPtrType();
137 WCharType = HostTarget->getWCharType();
138 WIntType = HostTarget->getWIntType();
139 Char16Type = HostTarget->getChar16Type();
140 Char32Type = HostTarget->getChar32Type();
141 Int64Type = HostTarget->getInt64Type();
142 SigAtomicType = HostTarget->getSigAtomicType();
143 ProcessIDType = HostTarget->getProcessIDType();
144
145 UseBitFieldTypeAlignment = HostTarget->useBitFieldTypeAlignment();
146 UseZeroLengthBitfieldAlignment = HostTarget->useZeroLengthBitfieldAlignment();
147 UseExplicitBitFieldAlignment = HostTarget->useExplicitBitFieldAlignment();
148 ZeroLengthBitfieldBoundary = HostTarget->getZeroLengthBitfieldBoundary();
149
150 // This is a bit of a lie, but it controls __GCC_ATOMIC_XXX_LOCK_FREE, and
151 // we need those macros to be identical on host and device, because (among
152 // other things) they affect which standard library classes are defined, and
153 // we need all classes to be defined on both the host and device.
154 MaxAtomicInlineWidth = HostTarget->getMaxAtomicInlineWidth();
155
156 // Properties intentionally not copied from host:
157 // - LargeArrayMinWidth, LargeArrayAlign: Not visible across the
158 // host/device boundary.
159 // - SuitableAlign: Not visible across the host/device boundary, and may
160 // correctly be different on host/device, e.g. if host has wider vector
161 // types than device.
162 // - LongDoubleWidth, LongDoubleAlign: nvptx's long double type is the same
163 // as its double type, but that's not necessarily true on the host.
164 // TODO: nvcc emits a warning when using long double on device; we should
165 // do the same.
166}
167
169 return llvm::ArrayRef(GCCRegNames);
170}
171
173 return llvm::StringSwitch<bool>(Feature)
174 .Cases("ptx", "nvptx", true)
175 .Default(false);
176}
177
179 MacroBuilder &Builder) const {
180 Builder.defineMacro("__PTX__");
181 Builder.defineMacro("__NVPTX__");
182
183 // Skip setting architecture dependent macros if undefined.
184 if (GPU == OffloadArch::UNUSED && !HostTarget)
185 return;
186
187 if (Opts.CUDAIsDevice || Opts.OpenMPIsTargetDevice || !HostTarget) {
188 // Set __CUDA_ARCH__ for the GPU specified.
189 llvm::StringRef CUDAArchCode = [this] {
190 switch (GPU) {
248 break;
250 assert(false && "No GPU arch when compiling CUDA device code.");
251 return "";
254 return "200";
256 return "210";
258 return "300";
260 return "320";
262 return "350";
264 return "370";
266 return "500";
268 return "520";
270 return "530";
272 return "600";
274 return "610";
276 return "620";
278 return "700";
280 return "720";
282 return "750";
284 return "800";
286 return "860";
288 return "870";
290 return "890";
293 return "900";
296 return "1000";
299 return "1010";
302 return "1030";
305 return "1200";
308 return "1210";
309 }
310 llvm_unreachable("unhandled OffloadArch");
311 }();
312 Builder.defineMacro("__CUDA_ARCH__", CUDAArchCode);
313 switch(GPU) {
320 Builder.defineMacro("__CUDA_ARCH_FEAT_SM" + CUDAArchCode.drop_back() + "_ALL", "1");
321 break;
322 default:
323 // Do nothing if this is not an enhanced architecture.
324 break;
325 }
326 }
327}
328
331 return {{&BuiltinStrings, BuiltinInfos}};
332}
static constexpr llvm::StringTable BuiltinStrings
Definition: AMDGPU.cpp:101
static constexpr Builtin::Info BuiltinInfos[]
Definition: NVPTX.cpp:29
static constexpr int NumBuiltins
Definition: NVPTX.cpp:22
static constexpr Builtin::Info BuiltinInfos[]
Definition: Builtins.cpp:38
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:434
Exposes information about the current target.
Definition: TargetInfo.h:226
const LangASMap * AddrSpaceMap
Definition: TargetInfo.h:258
void resetDataLayout(StringRef DL, const char *UserLabelPrefix="")
Definition: TargetInfo.cpp:197
bool UseAddrSpaceMapMangling
Specify if mangling based on address space map should be used or not for language specific address sp...
Definition: TargetInfo.h:382
unsigned char MaxAtomicInlineWidth
Definition: TargetInfo.h:251
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:38
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
Definition: NVPTX.cpp:178
bool hasFeature(StringRef Feature) const override
Determine whether the given target has the given feature.
Definition: NVPTX.cpp:172
llvm::SmallVector< Builtin::InfosShard > getTargetBuiltins() const override
Return information about target-specific builtins for the current primary target, and info about whic...
Definition: NVPTX.cpp:330
ArrayRef< const char * > getGCCRegNames() const override
Definition: NVPTX.cpp:168
static const unsigned NVPTXAddrSpaceMap[]
Definition: NVPTX.h:27
std::unique_ptr< clang::TargetInfo > AllocateTarget(const llvm::Triple &Triple, const clang::TargetOptions &Opts)
Definition: Targets.cpp:111
The JSON file list parser is used to communicate input to InstallAPI.
The info used to represent each builtin.
Definition: Builtins.h:78
unsigned UseZeroLengthBitfieldAlignment
Whether zero length bitfields (e.g., int : 0;) force alignment of the next bitfield.
Definition: TargetInfo.h:187
unsigned UseExplicitBitFieldAlignment
Whether explicit bit field alignment attributes are honored.
Definition: TargetInfo.h:196
unsigned ZeroLengthBitfieldBoundary
If non-zero, specifies a fixed alignment value for bitfields that follow zero length bitfield,...
Definition: TargetInfo.h:200
unsigned UseBitFieldTypeAlignment
Control whether the alignment of bit-field types is respected when laying out structures.
Definition: TargetInfo.h:178
const llvm::fltSemantics * BFloat16Format
Definition: TargetInfo.h:142
unsigned char DefaultAlignForAttributeAligned
Definition: TargetInfo.h:134