clang 22.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 0, // hlsl_constant
50 0, // hlsl_private
51 0, // hlsl_device
52 0, // hlsl_input
53 0, // hlsl_push_constant
54 // Wasm address space values for this target are dummy values,
55 // as it is only enabled for Wasm targets.
56 20, // wasm_funcref
57};
58
59/// The DWARF address class. Taken from
60/// https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
61static const int NVPTXDWARFAddrSpaceMap[] = {
62 -1, // Default, opencl_private or opencl_generic - not defined
63 5, // opencl_global
64 -1,
65 8, // opencl_local or cuda_shared
66 4, // opencl_constant or cuda_constant
67};
68
69class LLVM_LIBRARY_VISIBILITY NVPTXTargetInfo : public TargetInfo {
70 static const char *const GCCRegNames[];
71 OffloadArch GPU;
72 uint32_t PTXVersion;
73 std::unique_ptr<TargetInfo> HostTarget;
74
75public:
76 NVPTXTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts,
77 unsigned TargetPointerWidth);
78
79 void getTargetDefines(const LangOptions &Opts,
80 MacroBuilder &Builder) const override;
81
83
84 bool useFP16ConversionIntrinsics() const override { return false; }
85
86 bool
87 initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
88 StringRef CPU,
89 const std::vector<std::string> &FeaturesVec) const override {
90 if (GPU != OffloadArch::UNUSED)
91 Features[OffloadArchToString(GPU)] = true;
92 Features["ptx" + std::to_string(PTXVersion)] = true;
93 return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
94 }
95
96 bool hasFeature(StringRef Feature) const override;
97
98 virtual bool isAddressSpaceSupersetOf(LangAS A, LangAS B) const override {
99 // The generic address space AS(0) is a superset of all the other address
100 // spaces used by the backend target.
101 return A == B ||
102 ((A == LangAS::Default ||
105 llvm::NVPTXAS::ADDRESS_SPACE_GENERIC)) &&
107 toTargetAddressSpace(B) >= llvm::NVPTXAS::ADDRESS_SPACE_GENERIC &&
108 toTargetAddressSpace(B) <= llvm::NVPTXAS::ADDRESS_SPACE_LOCAL &&
109 toTargetAddressSpace(B) != 2);
110 }
111
112 ArrayRef<const char *> getGCCRegNames() const override;
113
115 // No aliases.
116 return {};
117 }
118
119 bool validateAsmConstraint(const char *&Name,
120 TargetInfo::ConstraintInfo &Info) const override {
121 switch (*Name) {
122 default:
123 return false;
124 case 'c':
125 case 'h':
126 case 'r':
127 case 'l':
128 case 'f':
129 case 'd':
130 case 'q':
131 Info.setAllowsRegister();
132 return true;
133 }
134 }
135
136 std::string_view getClobbers() const override {
137 // FIXME: Is this really right?
138 return "";
139 }
140
144
145 bool isValidCPUName(StringRef Name) const override {
147 }
148
149 void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override {
150 for (int i = static_cast<int>(OffloadArch::SM_20);
151 i < static_cast<int>(OffloadArch::Generic); ++i)
152 Values.emplace_back(OffloadArchToString(static_cast<OffloadArch>(i)));
153 }
154
155 bool setCPU(const std::string &Name) override {
156 GPU = StringToOffloadArch(Name);
157 return GPU != OffloadArch::UNKNOWN;
158 }
159
160 void setSupportedOpenCLOpts() override {
161 auto &Opts = getSupportedOpenCLOpts();
162 Opts["cl_clang_storage_class_specifiers"] = true;
163 Opts["__cl_clang_function_pointers"] = true;
164 Opts["__cl_clang_variadic_functions"] = true;
165 Opts["__cl_clang_non_portable_kernel_param_types"] = true;
166 Opts["__cl_clang_bitfields"] = true;
167
168 Opts["cl_khr_fp64"] = true;
169 Opts["__opencl_c_fp64"] = true;
170 Opts["cl_khr_byte_addressable_store"] = true;
171 Opts["cl_khr_global_int32_base_atomics"] = true;
172 Opts["cl_khr_global_int32_extended_atomics"] = true;
173 Opts["cl_khr_local_int32_base_atomics"] = true;
174 Opts["cl_khr_local_int32_extended_atomics"] = true;
175
176 Opts["__opencl_c_generic_address_space"] = true;
177 }
178
179 const llvm::omp::GV &getGridValue() const override {
180 return llvm::omp::NVPTXGridValues;
181 }
182
183 /// \returns If a target requires an address within a target specific address
184 /// space \p AddressSpace to be converted in order to be used, then return the
185 /// corresponding target specific DWARF address space.
186 ///
187 /// \returns Otherwise return std::nullopt and no conversion will be emitted
188 /// in the DWARF.
189 std::optional<unsigned>
190 getDWARFAddressSpace(unsigned AddressSpace) const override {
191 if (AddressSpace >= std::size(NVPTXDWARFAddrSpaceMap) ||
192 NVPTXDWARFAddrSpaceMap[AddressSpace] < 0)
193 return std::nullopt;
194 return NVPTXDWARFAddrSpaceMap[AddressSpace];
195 }
196
198 // CUDA compilations support all of the host's calling conventions.
199 //
200 // TODO: We should warn if you apply a non-default CC to anything other than
201 // a host function.
202 if (HostTarget)
203 return HostTarget->checkCallingConvention(CC);
204 return CC == CC_DeviceKernel ? CCCR_OK : CCCR_Warning;
205 }
206
207 bool hasBitIntType() const override { return true; }
208 bool hasBFloat16Type() const override { return true; }
209
210 OffloadArch getGPU() const { return GPU; }
211};
212} // namespace targets
213} // namespace clang
214#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:95
Defines the clang::TargetOptions class.
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:232
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)
BuiltinVaListKind
The different kinds of __builtin_va_list types defined by the target implementation.
Definition TargetInfo.h:333
@ CharPtrBuiltinVaList
typedef char* __builtin_va_list;
Definition TargetInfo.h:335
llvm::StringMap< bool > & getSupportedOpenCLOpts()
Get supported OpenCL extensions and optional core features.
virtual bool initFeatureMap(llvm::StringMap< bool > &Features, DiagnosticsEngine &Diags, StringRef CPU, const std::vector< std::string > &FeatureVec) const
Initialize the map with the default set of target features for the CPU this should include all legal ...
Options for controlling the target.
bool hasBitIntType() const override
Determine whether the _BitInt type is supported on this target.
Definition NVPTX.h:207
BuiltinVaListKind getBuiltinVaListKind() const override
Returns the kind of __builtin_va_list type that should be used with this target.
Definition NVPTX.h:141
OffloadArch getGPU() const
Definition NVPTX.h:210
virtual bool isAddressSpaceSupersetOf(LangAS A, LangAS B) const override
Returns true if an address space can be safely converted to another.
Definition NVPTX.h:98
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
ArrayRef< TargetInfo::GCCRegAlias > getGCCRegAliases() const override
Definition NVPTX.h:114
void fillValidCPUList(SmallVectorImpl< StringRef > &Values) const override
Fill a SmallVectorImpl with the valid values to setCPU.
Definition NVPTX.h:149
bool validateAsmConstraint(const char *&Name, TargetInfo::ConstraintInfo &Info) const override
Definition NVPTX.h:119
bool hasBFloat16Type() const override
Determine whether the _BFloat16 type is supported on this target.
Definition NVPTX.h:208
std::string_view getClobbers() const override
Returns a string of target-specific clobbers, in LLVM format.
Definition NVPTX.h:136
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
std::optional< unsigned > getDWARFAddressSpace(unsigned AddressSpace) const override
Definition NVPTX.h:190
const llvm::omp::GV & getGridValue() const override
Definition NVPTX.h:179
bool setCPU(const std::string &Name) override
Target the specified CPU.
Definition NVPTX.h:155
CallingConvCheckResult checkCallingConvention(CallingConv CC) const override
Determines whether a given calling convention is valid for the target.
Definition NVPTX.h:197
bool isValidCPUName(StringRef Name) const override
Determine whether this TargetInfo supports the given CPU name.
Definition NVPTX.h:145
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:84
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:87
void setSupportedOpenCLOpts() override
Set supported OpenCL extensions and optional core features.
Definition NVPTX.h:160
Defines the clang::TargetInfo interface.
static const unsigned NVPTXAddrSpaceMap[]
Definition NVPTX.h:27
static const int NVPTXDWARFAddrSpaceMap[]
The DWARF address class.
Definition NVPTX.h:61
The JSON file list parser is used to communicate input to InstallAPI.
bool isTargetAddressSpace(LangAS AS)
unsigned toTargetAddressSpace(LangAS AS)
OffloadArch StringToOffloadArch(llvm::StringRef S)
LangAS
Defines the address space values used by the address space qualifier of QualType.
const char * OffloadArchToString(OffloadArch A)
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition Specifiers.h:278
@ CC_DeviceKernel
Definition Specifiers.h:292