clang 20.0.0git
NVPTX.h
Go to the documentation of this file.
1//===--- NVPTX.h - Declare NVPTX target feature support ---------*- C++ -*-===//
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 declares NVPTX TargetInfo objects.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_LIB_BASIC_TARGETS_NVPTX_H
14#define LLVM_CLANG_LIB_BASIC_TARGETS_NVPTX_H
15
16#include "clang/Basic/Cuda.h"
19#include "llvm/Support/Compiler.h"
20#include "llvm/Support/NVPTXAddrSpace.h"
21#include "llvm/TargetParser/Triple.h"
22#include <optional>
23
24namespace clang {
25namespace targets {
26
27static const unsigned NVPTXAddrSpaceMap[] = {
28 0, // Default
29 1, // opencl_global
30 3, // opencl_local
31 4, // opencl_constant
32 0, // opencl_private
33 // FIXME: generic has to be added to the target
34 0, // opencl_generic
35 1, // opencl_global_device
36 1, // opencl_global_host
37 1, // cuda_device
38 4, // cuda_constant
39 3, // cuda_shared
40 1, // sycl_global
41 1, // sycl_global_device
42 1, // sycl_global_host
43 3, // sycl_local
44 0, // sycl_private
45 0, // ptr32_sptr
46 0, // ptr32_uptr
47 0, // ptr64
48 0, // hlsl_groupshared
49 // Wasm address space values for this target are dummy values,
50 // as it is only enabled for Wasm targets.
51 20, // wasm_funcref
52};
53
54/// The DWARF address class. Taken from
55/// https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
56static const int NVPTXDWARFAddrSpaceMap[] = {
57 -1, // Default, opencl_private or opencl_generic - not defined
58 5, // opencl_global
59 -1,
60 8, // opencl_local or cuda_shared
61 4, // opencl_constant or cuda_constant
62};
63
64class LLVM_LIBRARY_VISIBILITY NVPTXTargetInfo : public TargetInfo {
65 static const char *const GCCRegNames[];
66 OffloadArch GPU;
67 uint32_t PTXVersion;
68 std::unique_ptr<TargetInfo> HostTarget;
69
70public:
71 NVPTXTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts,
72 unsigned TargetPointerWidth);
73
74 void getTargetDefines(const LangOptions &Opts,
75 MacroBuilder &Builder) const override;
76
77 ArrayRef<Builtin::Info> getTargetBuiltins() const override;
78
79 bool useFP16ConversionIntrinsics() const override { return false; }
80
81 bool
82 initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
83 StringRef CPU,
84 const std::vector<std::string> &FeaturesVec) const override {
85 if (GPU != OffloadArch::UNUSED)
86 Features[OffloadArchToString(GPU)] = true;
87 Features["ptx" + std::to_string(PTXVersion)] = true;
88 return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
89 }
90
91 bool hasFeature(StringRef Feature) const override;
92
93 virtual bool isAddressSpaceSupersetOf(LangAS A, LangAS B) const override {
94 // The generic address space AS(0) is a superset of all the other address
95 // spaces used by the backend target.
96 return A == B ||
97 ((A == LangAS::Default ||
100 llvm::NVPTXAS::ADDRESS_SPACE_GENERIC)) &&
102 toTargetAddressSpace(B) >= llvm::NVPTXAS::ADDRESS_SPACE_GENERIC &&
103 toTargetAddressSpace(B) <= llvm::NVPTXAS::ADDRESS_SPACE_LOCAL &&
104 toTargetAddressSpace(B) != 2);
105 }
106
107 ArrayRef<const char *> getGCCRegNames() const override;
108
110 // No aliases.
111 return {};
112 }
113
114 bool validateAsmConstraint(const char *&Name,
115 TargetInfo::ConstraintInfo &Info) const override {
116 switch (*Name) {
117 default:
118 return false;
119 case 'c':
120 case 'h':
121 case 'r':
122 case 'l':
123 case 'f':
124 case 'd':
125 case 'q':
126 Info.setAllowsRegister();
127 return true;
128 }
129 }
130
131 std::string_view getClobbers() const override {
132 // FIXME: Is this really right?
133 return "";
134 }
135
137 return TargetInfo::CharPtrBuiltinVaList;
138 }
139
140 bool isValidCPUName(StringRef Name) const override {
141 return StringToOffloadArch(Name) != OffloadArch::UNKNOWN;
142 }
143
144 void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override {
145 for (int i = static_cast<int>(OffloadArch::SM_20);
146 i < static_cast<int>(OffloadArch::Generic); ++i)
147 Values.emplace_back(OffloadArchToString(static_cast<OffloadArch>(i)));
148 }
149
150 bool setCPU(const std::string &Name) override {
151 GPU = StringToOffloadArch(Name);
152 return GPU != OffloadArch::UNKNOWN;
153 }
154
155 void setSupportedOpenCLOpts() override {
156 auto &Opts = getSupportedOpenCLOpts();
157 Opts["cl_clang_storage_class_specifiers"] = true;
158 Opts["__cl_clang_function_pointers"] = true;
159 Opts["__cl_clang_variadic_functions"] = true;
160 Opts["__cl_clang_non_portable_kernel_param_types"] = true;
161 Opts["__cl_clang_bitfields"] = true;
162
163 Opts["cl_khr_fp64"] = true;
164 Opts["__opencl_c_fp64"] = true;
165 Opts["cl_khr_byte_addressable_store"] = true;
166 Opts["cl_khr_global_int32_base_atomics"] = true;
167 Opts["cl_khr_global_int32_extended_atomics"] = true;
168 Opts["cl_khr_local_int32_base_atomics"] = true;
169 Opts["cl_khr_local_int32_extended_atomics"] = true;
170 }
171
172 const llvm::omp::GV &getGridValue() const override {
173 return llvm::omp::NVPTXGridValues;
174 }
175
176 /// \returns If a target requires an address within a target specific address
177 /// space \p AddressSpace to be converted in order to be used, then return the
178 /// corresponding target specific DWARF address space.
179 ///
180 /// \returns Otherwise return std::nullopt and no conversion will be emitted
181 /// in the DWARF.
182 std::optional<unsigned>
183 getDWARFAddressSpace(unsigned AddressSpace) const override {
184 if (AddressSpace >= std::size(NVPTXDWARFAddrSpaceMap) ||
185 NVPTXDWARFAddrSpaceMap[AddressSpace] < 0)
186 return std::nullopt;
187 return NVPTXDWARFAddrSpaceMap[AddressSpace];
188 }
189
191 // CUDA compilations support all of the host's calling conventions.
192 //
193 // TODO: We should warn if you apply a non-default CC to anything other than
194 // a host function.
195 if (HostTarget)
196 return HostTarget->checkCallingConvention(CC);
197 return CCCR_Warning;
198 }
199
200 bool hasBitIntType() const override { return true; }
201 bool hasBFloat16Type() const override { return true; }
202
203 OffloadArch getGPU() const { return GPU; }
204};
205} // namespace targets
206} // namespace clang
207#endif // LLVM_CLANG_LIB_BASIC_TARGETS_NVPTX_H
static bool hasFeature(StringRef Feature, const LangOptions &LangOpts, const TargetInfo &Target)
Determine whether a translation unit built using the current language options has the given feature.
Definition: Module.cpp:96
Defines the clang::TargetOptions class.
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:231
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:499
Exposes information about the current target.
Definition: TargetInfo.h:220
BuiltinVaListKind
The different kinds of __builtin_va_list types defined by the target implementation.
Definition: TargetInfo.h:318
Options for controlling the target.
Definition: TargetOptions.h:26
bool hasBitIntType() const override
Determine whether the _BitInt type is supported on this target.
Definition: NVPTX.h:200
BuiltinVaListKind getBuiltinVaListKind() const override
Returns the kind of __builtin_va_list type that should be used with this target.
Definition: NVPTX.h:136
OffloadArch getGPU() const
Definition: NVPTX.h:203
virtual bool isAddressSpaceSupersetOf(LangAS A, LangAS B) const override
Returns true if an address space can be safely converted to another.
Definition: NVPTX.h:93
ArrayRef< TargetInfo::GCCRegAlias > getGCCRegAliases() const override
Definition: NVPTX.h:109
void fillValidCPUList(SmallVectorImpl< StringRef > &Values) const override
Fill a SmallVectorImpl with the valid values to setCPU.
Definition: NVPTX.h:144
bool validateAsmConstraint(const char *&Name, TargetInfo::ConstraintInfo &Info) const override
Definition: NVPTX.h:114
bool hasBFloat16Type() const override
Determine whether the _BFloat16 type is supported on this target.
Definition: NVPTX.h:201
std::string_view getClobbers() const override
Returns a string of target-specific clobbers, in LLVM format.
Definition: NVPTX.h:131
std::optional< unsigned > getDWARFAddressSpace(unsigned AddressSpace) const override
Definition: NVPTX.h:183
const llvm::omp::GV & getGridValue() const override
Definition: NVPTX.h:172
bool setCPU(const std::string &Name) override
Target the specified CPU.
Definition: NVPTX.h:150
CallingConvCheckResult checkCallingConvention(CallingConv CC) const override
Determines whether a given calling convention is valid for the target.
Definition: NVPTX.h:190
bool isValidCPUName(StringRef Name) const override
Determine whether this TargetInfo supports the given CPU name.
Definition: NVPTX.h:140
bool useFP16ConversionIntrinsics() const override
Check whether llvm intrinsics such as llvm.convert.to.fp16 should be used to convert to and from __fp...
Definition: NVPTX.h:79
bool initFeatureMap(llvm::StringMap< bool > &Features, DiagnosticsEngine &Diags, StringRef CPU, const std::vector< std::string > &FeaturesVec) const override
Initialize the map with the default set of target features for the CPU this should include all legal ...
Definition: NVPTX.h:82
void setSupportedOpenCLOpts() override
Set supported OpenCL extensions and optional core features.
Definition: NVPTX.h:155
Defines the clang::TargetInfo interface.
static const unsigned NVPTXAddrSpaceMap[]
Definition: NVPTX.h:27
static const int NVPTXDWARFAddrSpaceMap[]
The DWARF address class.
Definition: NVPTX.h:56
The JSON file list parser is used to communicate input to InstallAPI.
bool isTargetAddressSpace(LangAS AS)
Definition: AddressSpaces.h:77
OffloadArch
Definition: Cuda.h:56
unsigned toTargetAddressSpace(LangAS AS)
Definition: AddressSpaces.h:81
OffloadArch StringToOffloadArch(llvm::StringRef S)
Definition: Cuda.cpp:180
LangAS
Defines the address space values used by the address space qualifier of QualType.
Definition: AddressSpaces.h:25
const char * OffloadArchToString(OffloadArch A)
Definition: Cuda.cpp:162
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:278