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 // TODO: Make shortptr a proper ABI?
73 Triple.computeDataLayout(Opts.NVPTXUseShortPointers ? "shortptr" : "");
74
75 // If possible, get a TargetInfo for our host triple, so we can match its
76 // types.
77 llvm::Triple HostTriple(Opts.HostTriple);
78 if (!HostTriple.isNVPTX())
79 HostTarget = AllocateTarget(llvm::Triple(Opts.HostTriple), Opts);
80
81 // If no host target, make some guesses about the data layout and return.
82 if (!HostTarget) {
83 LongWidth = LongAlign = TargetPointerWidth;
84 PointerWidth = PointerAlign = TargetPointerWidth;
85 switch (TargetPointerWidth) {
86 case 32:
90 break;
91 case 64:
95 break;
96 default:
97 llvm_unreachable("TargetPointerWidth must be 32 or 64");
98 }
99
100 MaxAtomicInlineWidth = TargetPointerWidth;
101 return;
102 }
103
104 // Copy properties from host target.
105 PointerWidth = HostTarget->getPointerWidth(LangAS::Default);
106 PointerAlign = HostTarget->getPointerAlign(LangAS::Default);
107 BoolWidth = HostTarget->getBoolWidth();
108 BoolAlign = HostTarget->getBoolAlign();
109 IntWidth = HostTarget->getIntWidth();
110 IntAlign = HostTarget->getIntAlign();
111 HalfWidth = HostTarget->getHalfWidth();
112 HalfAlign = HostTarget->getHalfAlign();
113 FloatWidth = HostTarget->getFloatWidth();
114 FloatAlign = HostTarget->getFloatAlign();
115 DoubleWidth = HostTarget->getDoubleWidth();
116 DoubleAlign = HostTarget->getDoubleAlign();
117 LongWidth = HostTarget->getLongWidth();
118 LongAlign = HostTarget->getLongAlign();
119 LongLongWidth = HostTarget->getLongLongWidth();
120 LongLongAlign = HostTarget->getLongLongAlign();
121 MinGlobalAlign = HostTarget->getMinGlobalAlign(/* TypeSize = */ 0,
122 /* HasNonWeakDef = */ true);
123 NewAlign = HostTarget->getNewAlign();
125 HostTarget->getDefaultAlignForAttributeAligned();
126 SizeType = HostTarget->getSizeType();
127 IntMaxType = HostTarget->getIntMaxType();
128 PtrDiffType = HostTarget->getPtrDiffType(LangAS::Default);
129 IntPtrType = HostTarget->getIntPtrType();
130 WCharType = HostTarget->getWCharType();
131 WIntType = HostTarget->getWIntType();
132 Char16Type = HostTarget->getChar16Type();
133 Char32Type = HostTarget->getChar32Type();
134 Int64Type = HostTarget->getInt64Type();
135 SigAtomicType = HostTarget->getSigAtomicType();
136 ProcessIDType = HostTarget->getProcessIDType();
137
138 UseBitFieldTypeAlignment = HostTarget->useBitFieldTypeAlignment();
139 UseZeroLengthBitfieldAlignment = HostTarget->useZeroLengthBitfieldAlignment();
140 UseExplicitBitFieldAlignment = HostTarget->useExplicitBitFieldAlignment();
141 ZeroLengthBitfieldBoundary = HostTarget->getZeroLengthBitfieldBoundary();
142
143 // This is a bit of a lie, but it controls __GCC_ATOMIC_XXX_LOCK_FREE, and
144 // we need those macros to be identical on host and device, because (among
145 // other things) they affect which standard library classes are defined, and
146 // we need all classes to be defined on both the host and device.
147 MaxAtomicInlineWidth = HostTarget->getMaxAtomicInlineWidth();
148
149 // Properties intentionally not copied from host:
150 // - LargeArrayMinWidth, LargeArrayAlign: Not visible across the
151 // host/device boundary.
152 // - SuitableAlign: Not visible across the host/device boundary, and may
153 // correctly be different on host/device, e.g. if host has wider vector
154 // types than device.
155 // - LongDoubleWidth, LongDoubleAlign: nvptx's long double type is the same
156 // as its double type, but that's not necessarily true on the host.
157 // TODO: nvcc emits a warning when using long double on device; we should
158 // do the same.
159}
160
164
166 return llvm::StringSwitch<bool>(Feature)
167 .Cases({"ptx", "nvptx"}, true)
168 .Default(false);
169}
170
172 MacroBuilder &Builder) const {
173 Builder.defineMacro("__PTX__");
174 Builder.defineMacro("__NVPTX__");
175
176 // Skip setting architecture dependent macros if undefined.
177 if (GPU == OffloadArch::UNUSED && !HostTarget)
178 return;
179
180 if (Opts.CUDAIsDevice || Opts.OpenMPIsTargetDevice || !HostTarget) {
181 // Set __CUDA_ARCH__ for the GPU specified.
182 llvm::StringRef CUDAArchCode = [this] {
183 switch (GPU) {
242 break;
244 assert(false && "No GPU arch when compiling CUDA device code.");
245 return "";
248 return "200";
250 return "210";
252 return "300";
254 return "320";
256 return "350";
258 return "370";
260 return "500";
262 return "520";
264 return "530";
266 return "600";
268 return "610";
270 return "620";
272 return "700";
274 return "720";
276 return "750";
278 return "800";
280 return "860";
282 return "870";
284 return "880";
286 return "890";
289 return "900";
292 return "1000";
295 return "1010";
298 return "1030";
301 return "1100";
304 return "1200";
307 return "1210";
308 }
309 llvm_unreachable("unhandled OffloadArch");
310 }();
311 Builder.defineMacro("__CUDA_ARCH__", CUDAArchCode);
312 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
static constexpr llvm::StringTable BuiltinStrings
Definition AMDGPU.cpp:91
static constexpr Builtin::Info BuiltinInfos[]
Definition Builtins.cpp:38
static constexpr unsigned NumBuiltins
Definition Builtins.cpp:32
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...
TargetInfo(const llvm::Triple &T)
const LangASMap * AddrSpaceMap
Definition TargetInfo.h:259
bool UseAddrSpaceMapMangling
Specify if mangling based on address space map should be used or not for language specific address sp...
Definition TargetInfo.h:385
std::string DataLayoutString
Definition TargetInfo.h:253
unsigned char MaxAtomicInlineWidth
Definition TargetInfo.h:252
Options for controlling the target.
std::string HostTriple
When compiling for the device side, contains the triple used to compile for the host.
std::vector< std::string > FeaturesAsWritten
The list of target specific features to enable or disable, as written on the command line.
bool NVPTXUseShortPointers
If enabled, use 32-bit pointers for accessing const/local/shared address space.
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:171
bool hasFeature(StringRef Feature) const override
Determine whether the given target has the given feature.
Definition NVPTX.cpp:165
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:161
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:79
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