clang 23.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 is 0 by default, meaning "use the minimum for the SM target".
46 // Only set it if the user explicitly requested a PTX version.
47 PTXVersion = 0;
48 for (const StringRef Feature : Opts.FeaturesAsWritten) {
49 int PTXV;
50 if (!Feature.starts_with("+ptx") ||
51 Feature.drop_front(4).getAsInteger(10, PTXV))
52 continue;
53 PTXVersion = PTXV; // TODO: should it be max(PTXVersion, PTXV)?
54 }
55
56 TLSSupported = false;
57 VLASupported = false;
60 // __bf16 is always available as a load/store only type.
62 BFloat16Format = &llvm::APFloat::BFloat();
63
64 // Define available target features
65 // These must be defined in sorted order!
66 NoAsmVariants = true;
68
69 // PTX supports f16 as a fundamental type.
70 HasFastHalfType = true;
71 HasFloat16 = true;
72
73 // TODO: Make shortptr a proper ABI?
75 Triple.computeDataLayout(Opts.NVPTXUseShortPointers ? "shortptr" : "");
76
77 // If possible, get a TargetInfo for our host triple, so we can match its
78 // types.
79 llvm::Triple HostTriple(Opts.HostTriple);
80 if (!HostTriple.isNVPTX())
81 HostTarget = AllocateTarget(llvm::Triple(Opts.HostTriple), Opts);
82
83 // If no host target, make some guesses about the data layout and return.
84 if (!HostTarget) {
85 LongWidth = LongAlign = TargetPointerWidth;
86 PointerWidth = PointerAlign = TargetPointerWidth;
87 switch (TargetPointerWidth) {
88 case 32:
92 break;
93 case 64:
97 break;
98 default:
99 llvm_unreachable("TargetPointerWidth must be 32 or 64");
100 }
101
102 MaxAtomicInlineWidth = TargetPointerWidth;
103 return;
104 }
105
106 // Copy properties from host target.
107 PointerWidth = HostTarget->getPointerWidth(LangAS::Default);
108 PointerAlign = HostTarget->getPointerAlign(LangAS::Default);
109 BoolWidth = HostTarget->getBoolWidth();
110 BoolAlign = HostTarget->getBoolAlign();
111 IntWidth = HostTarget->getIntWidth();
112 IntAlign = HostTarget->getIntAlign();
113 HalfWidth = HostTarget->getHalfWidth();
114 HalfAlign = HostTarget->getHalfAlign();
115 FloatWidth = HostTarget->getFloatWidth();
116 FloatAlign = HostTarget->getFloatAlign();
117 DoubleWidth = HostTarget->getDoubleWidth();
118 DoubleAlign = HostTarget->getDoubleAlign();
119 LongWidth = HostTarget->getLongWidth();
120 LongAlign = HostTarget->getLongAlign();
121 LongLongWidth = HostTarget->getLongLongWidth();
122 LongLongAlign = HostTarget->getLongLongAlign();
123 MinGlobalAlign = HostTarget->getMinGlobalAlign(/* TypeSize = */ 0,
124 /* HasNonWeakDef = */ true);
125 NewAlign = HostTarget->getNewAlign();
127 HostTarget->getDefaultAlignForAttributeAligned();
128 SizeType = HostTarget->getSizeType();
129 IntMaxType = HostTarget->getIntMaxType();
130 PtrDiffType = HostTarget->getPtrDiffType(LangAS::Default);
131 IntPtrType = HostTarget->getIntPtrType();
132 WCharType = HostTarget->getWCharType();
133 WIntType = HostTarget->getWIntType();
134 Char16Type = HostTarget->getChar16Type();
135 Char32Type = HostTarget->getChar32Type();
136 Int64Type = HostTarget->getInt64Type();
137 SigAtomicType = HostTarget->getSigAtomicType();
138 ProcessIDType = HostTarget->getProcessIDType();
139
140 UseBitFieldTypeAlignment = HostTarget->useBitFieldTypeAlignment();
141 UseZeroLengthBitfieldAlignment = HostTarget->useZeroLengthBitfieldAlignment();
142 UseExplicitBitFieldAlignment = HostTarget->useExplicitBitFieldAlignment();
143 ZeroLengthBitfieldBoundary = HostTarget->getZeroLengthBitfieldBoundary();
144
145 // This is a bit of a lie, but it controls __GCC_ATOMIC_XXX_LOCK_FREE, and
146 // we need those macros to be identical on host and device, because (among
147 // other things) they affect which standard library classes are defined, and
148 // we need all classes to be defined on both the host and device.
149 MaxAtomicInlineWidth = HostTarget->getMaxAtomicInlineWidth();
150
151 // Properties intentionally not copied from host:
152 // - LargeArrayMinWidth, LargeArrayAlign: Not visible across the
153 // host/device boundary.
154 // - SuitableAlign: Not visible across the host/device boundary, and may
155 // correctly be different on host/device, e.g. if host has wider vector
156 // types than device.
157 // - LongDoubleWidth, LongDoubleAlign: nvptx's long double type is the same
158 // as its double type, but that's not necessarily true on the host.
159 // TODO: nvcc emits a warning when using long double on device; we should
160 // do the same.
161}
162
166
168 return llvm::StringSwitch<bool>(Feature)
169 .Cases({"ptx", "nvptx"}, true)
170 .Default(false);
171}
172
174 MacroBuilder &Builder) const {
175 Builder.defineMacro("__PTX__");
176 Builder.defineMacro("__NVPTX__");
177
178 // Skip setting architecture dependent macros if undefined.
179 if (!IsNVIDIAOffloadArch(GPU))
180 return;
181
182 if (Opts.CUDAIsDevice || Opts.OpenMPIsTargetDevice || !HostTarget) {
183 // Set __CUDA_ARCH__ for the GPU specified.
184 unsigned ArchID = CudaArchToID(GPU);
185 Builder.defineMacro("__CUDA_ARCH__", llvm::Twine(ArchID));
186
188 Builder.defineMacro(
189 "__CUDA_ARCH_FEAT_SM" + llvm::Twine(ArchID / 10) + "_ALL", "1");
190 }
191}
192
static constexpr llvm::StringTable BuiltinStrings
Definition ARM.cpp:1115
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:173
bool hasFeature(StringRef Feature) const override
Determine whether the given target has the given feature.
Definition NVPTX.cpp:167
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:194
ArrayRef< const char * > getGCCRegNames() const override
Definition NVPTX.cpp:163
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.
unsigned CudaArchToID(OffloadArch Arch)
Get the numeric ID (e.g. 700) of a CUDA architecture.
Definition Cuda.cpp:177
static bool IsNVIDIAOffloadArch(OffloadArch A)
bool IsNVIDIAAcceleratedOffloadArch(OffloadArch Arch)
Check if the CUDA architecture is an accelerated variant (e.g. sm_90a).
Definition Cuda.cpp:246
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