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 "clang/Basic/Builtins.h"
15#include "clang/Basic/MacroBuilder.h"
16#include "clang/Basic/TargetBuiltins.h"
17#include "llvm/ADT/StringSwitch.h"
18
19using namespace clang;
20using namespace clang::targets;
21
22static constexpr int NumBuiltins =
23 clang::NVPTX::LastTSBuiltin - Builtin::FirstTSBuiltin;
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(Prefix: "+ptx") ||
51 Feature.drop_front(N: 4).getAsInteger(Radix: 10, Result&: PTXV))
52 continue;
53 PTXVersion = PTXV; // TODO: should it be max(PTXVersion, PTXV)?
54 }
55
56 TLSSupported = false;
57 VLASupported = false;
58 AddrSpaceMap = &NVPTXAddrSpaceMap;
59 UseAddrSpaceMapMangling = true;
60 // __bf16 is always available as a load/store only type.
61 BFloat16Width = BFloat16Align = 16;
62 BFloat16Format = &llvm::APFloat::BFloat();
63
64 // Define available target features
65 // These must be defined in sorted order!
66 NoAsmVariants = true;
67 GPU = OffloadArch::getUnused();
68
69 // Architectures are in the feature map only to gate builtins; the backend
70 // takes the architecture from `target-cpu`.
71#define NVPTX_GPU(NAME, KIND, VIRTUAL, SM_ID, MIN_VER, MAX_VER, SUFFIX) \
72 ReadOnlyFeatures.insert(NAME);
73#include "llvm/TargetParser/NVPTXTargetParser.def"
74
75 // PTX supports f16 as a fundamental type.
76 HasFastHalfType = true;
77 HasFloat16 = true;
78
79 DataLayoutString = Triple.computeDataLayout();
80
81 // If possible, get a TargetInfo for our host triple, so we can match its
82 // types.
83 llvm::Triple HostTriple(Opts.HostTriple);
84 if (!HostTriple.isNVPTX())
85 HostTarget = AllocateTarget(Triple: llvm::Triple(Opts.HostTriple), Opts);
86
87 // If no host target, make some guesses about the data layout and return.
88 if (!HostTarget) {
89 LongWidth = LongAlign = TargetPointerWidth;
90 PointerWidth = PointerAlign = TargetPointerWidth;
91 switch (TargetPointerWidth) {
92 case 32:
93 SizeType = TargetInfo::UnsignedInt;
94 PtrDiffType = TargetInfo::SignedInt;
95 IntPtrType = TargetInfo::SignedInt;
96 break;
97 case 64:
98 SizeType = TargetInfo::UnsignedLong;
99 PtrDiffType = TargetInfo::SignedLong;
100 IntPtrType = TargetInfo::SignedLong;
101 break;
102 default:
103 llvm_unreachable("TargetPointerWidth must be 32 or 64");
104 }
105
106 MaxAtomicInlineWidth = TargetPointerWidth;
107 return;
108 }
109
110 // Copy properties from host target.
111 PointerWidth = HostTarget->getPointerWidth(AddrSpace: LangAS::Default);
112 PointerAlign = HostTarget->getPointerAlign(AddrSpace: LangAS::Default);
113 BoolWidth = HostTarget->getBoolWidth();
114 BoolAlign = HostTarget->getBoolAlign();
115 IntWidth = HostTarget->getIntWidth();
116 IntAlign = HostTarget->getIntAlign();
117 HalfWidth = HostTarget->getHalfWidth();
118 HalfAlign = HostTarget->getHalfAlign();
119 FloatWidth = HostTarget->getFloatWidth();
120 FloatAlign = HostTarget->getFloatAlign();
121 DoubleWidth = HostTarget->getDoubleWidth();
122 DoubleAlign = HostTarget->getDoubleAlign();
123 LongWidth = HostTarget->getLongWidth();
124 LongAlign = HostTarget->getLongAlign();
125 LongLongWidth = HostTarget->getLongLongWidth();
126 LongLongAlign = HostTarget->getLongLongAlign();
127 MinGlobalAlign = HostTarget->getMinGlobalAlign(/* TypeSize = */ Size: 0,
128 /* HasNonWeakDef = */ true);
129 NewAlign = HostTarget->getNewAlign();
130 DefaultAlignForAttributeAligned =
131 HostTarget->getDefaultAlignForAttributeAligned();
132 SizeType = HostTarget->getSizeType();
133 IntMaxType = HostTarget->getIntMaxType();
134 PtrDiffType = HostTarget->getPtrDiffType(AddrSpace: LangAS::Default);
135 IntPtrType = HostTarget->getIntPtrType();
136 WCharType = HostTarget->getWCharType();
137 WIntType = HostTarget->getWIntType();
138 Char16Type = HostTarget->getChar16Type();
139 Char32Type = HostTarget->getChar32Type();
140 Int64Type = HostTarget->getInt64Type();
141 SigAtomicType = HostTarget->getSigAtomicType();
142 ProcessIDType = HostTarget->getProcessIDType();
143
144 UseBitFieldTypeAlignment = HostTarget->useBitFieldTypeAlignment();
145 UseZeroLengthBitfieldAlignment = HostTarget->useZeroLengthBitfieldAlignment();
146 UseExplicitBitFieldAlignment = HostTarget->useExplicitBitFieldAlignment();
147 ZeroLengthBitfieldBoundary = HostTarget->getZeroLengthBitfieldBoundary();
148
149 // This is a bit of a lie, but it controls __GCC_ATOMIC_XXX_LOCK_FREE, and
150 // we need those macros to be identical on host and device, because (among
151 // other things) they affect which standard library classes are defined, and
152 // we need all classes to be defined on both the host and device.
153 MaxAtomicInlineWidth = HostTarget->getMaxAtomicInlineWidth();
154
155 // Properties intentionally not copied from host:
156 // - LargeArrayMinWidth, LargeArrayAlign: Not visible across the
157 // host/device boundary.
158 // - SuitableAlign: Not visible across the host/device boundary, and may
159 // correctly be different on host/device, e.g. if host has wider vector
160 // types than device.
161 // - LongDoubleWidth, LongDoubleAlign: nvptx's long double type is the same
162 // as its double type, but that's not necessarily true on the host.
163 // TODO: nvcc emits a warning when using long double on device; we should
164 // do the same.
165}
166
167bool NVPTXTargetInfo::setABI(const std::string &Name) {
168 if (Name != "shortptr")
169 return false;
170
171 resetDataLayout(DL: getTriple().computeDataLayout(ABIName: Name));
172 return true;
173}
174
175ArrayRef<const char *> NVPTXTargetInfo::getGCCRegNames() const {
176 return llvm::ArrayRef(GCCRegNames);
177}
178
179bool NVPTXTargetInfo::hasFeature(StringRef Feature) const {
180 return llvm::StringSwitch<bool>(Feature)
181 .Cases(CaseStrings: {"ptx", "nvptx"}, Value: true)
182 .Default(Value: false);
183}
184
185void NVPTXTargetInfo::getTargetDefines(const LangOptions &Opts,
186 MacroBuilder &Builder) const {
187 Builder.defineMacro(Name: "__PTX__");
188 Builder.defineMacro(Name: "__NVPTX__");
189
190 // Skip setting architecture dependent macros if undefined.
191 if (!GPU.isNVPTX())
192 return;
193
194 if (Opts.CUDAIsDevice || Opts.OpenMPIsTargetDevice || !HostTarget) {
195 // Set __CUDA_ARCH__ for the GPU specified.
196 unsigned ArchID = CudaArchToID(Arch: GPU);
197 Builder.defineMacro(Name: "__CUDA_ARCH__", Value: llvm::Twine(ArchID));
198
199 if (IsNVIDIAAcceleratedOffloadArch(Arch: GPU)) {
200 Builder.defineMacro(Name: "__CUDA_ARCH_SPECIFIC__", Value: llvm::Twine(ArchID));
201 Builder.defineMacro(
202 Name: "__CUDA_ARCH_FEAT_SM" + llvm::Twine(ArchID / 10) + "_ALL", Value: "1");
203 }
204 if (IsNVIDIAFamilySpecificOffloadArch(Arch: GPU))
205 Builder.defineMacro(Name: "__CUDA_ARCH_FAMILY_SPECIFIC__", Value: llvm::Twine(ArchID));
206 }
207}
208
209llvm::SmallVector<Builtin::InfosShard>
210NVPTXTargetInfo::getTargetBuiltins() const {
211 return {{.Strings: &BuiltinStrings, .Infos: BuiltinInfos}};
212}
213