clang 24.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
46
47/// The DWARF address class. Taken from
48/// https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
49static const int NVPTXDWARFAddrSpaceMap[] = {
50 -1, // Default, opencl_private or opencl_generic - not defined
51 5, // opencl_global
52 -1,
53 8, // opencl_local or cuda_shared
54 4, // opencl_constant or cuda_constant
55};
56
57class LLVM_LIBRARY_VISIBILITY NVPTXTargetInfo : public TargetInfo {
58 static const char *const GCCRegNames[];
59 OffloadArch GPU;
60 uint32_t PTXVersion;
61 std::unique_ptr<TargetInfo> HostTarget;
62
63public:
64 NVPTXTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts,
65 unsigned TargetPointerWidth);
66
67 void getTargetDefines(const LangOptions &Opts,
68 MacroBuilder &Builder) const override;
69
71
72 bool isCLZForZeroUndef() const override { return false; }
73
74 bool
75 initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
76 StringRef CPU,
77 const std::vector<std::string> &FeaturesVec) const override {
78 if (!GPU.isUnused())
79 Features[OffloadArchToString(GPU)] = true;
80 // Only add PTX feature if explicitly requested. Otherwise, let the backend
81 // use the minimum required PTX version for the target SM.
82 if (PTXVersion != 0)
83 Features["ptx" + std::to_string(PTXVersion)] = true;
84 return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
85 }
86
87 bool hasFeature(StringRef Feature) const override;
88
89 bool setABI(const std::string &Name) override;
90
91 virtual bool isAddressSpaceSupersetOf(LangAS A, LangAS B) const override {
92 // The generic address space AS(0) is a superset of all the other address
93 // spaces used by the backend target.
94 return A == B ||
95 ((A == LangAS::Default ||
98 llvm::NVPTXAS::ADDRESS_SPACE_GENERIC)) &&
100 toTargetAddressSpace(B) >= llvm::NVPTXAS::ADDRESS_SPACE_GENERIC &&
101 toTargetAddressSpace(B) <= llvm::NVPTXAS::ADDRESS_SPACE_LOCAL &&
102 toTargetAddressSpace(B) != 2);
103 }
104
105 ArrayRef<const char *> getGCCRegNames() const override;
106
108 // No aliases.
109 return {};
110 }
111
112 bool validateAsmConstraint(const char *&Name,
113 TargetInfo::ConstraintInfo &Info) const override {
114 switch (*Name) {
115 default:
116 return false;
117 case 'c':
118 case 'h':
119 case 'r':
120 case 'l':
121 case 'f':
122 case 'd':
123 case 'q':
124 Info.setAllowsRegister();
125 return true;
126 }
127 }
128
129 std::string_view getClobbers() const override {
130 // FIXME: Is this really right?
131 return "";
132 }
133
137
138 bool isValidCPUName(StringRef Name) const override {
139 return !StringToOffloadArch(Name).isUnknown();
140 }
141
142 void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override {
144 }
145
146 bool setCPU(StringRef Name) override {
147 GPU = StringToOffloadArch(Name);
148 return !GPU.isUnknown();
149 }
150
151 void setSupportedOpenCLOpts() override {
152 auto &Opts = getSupportedOpenCLOpts();
153 Opts["cl_clang_storage_class_specifiers"] = true;
154 Opts["__cl_clang_function_pointers"] = true;
155 Opts["__cl_clang_variadic_functions"] = true;
156 Opts["__cl_clang_function_scope_local_variables"] = true;
157 Opts["__cl_clang_non_portable_kernel_param_types"] = true;
158 Opts["__cl_clang_bitfields"] = true;
159
160 Opts["cl_khr_fp64"] = true;
161 Opts["__opencl_c_fp64"] = true;
162 Opts["cl_khr_byte_addressable_store"] = true;
163 Opts["cl_khr_global_int32_base_atomics"] = true;
164 Opts["cl_khr_global_int32_extended_atomics"] = true;
165 Opts["cl_khr_local_int32_base_atomics"] = true;
166 Opts["cl_khr_local_int32_extended_atomics"] = true;
167
168 Opts["__opencl_c_images"] = true;
169 Opts["__opencl_c_3d_image_writes"] = true;
170 Opts["cl_khr_3d_image_writes"] = true;
171
172 Opts["__opencl_c_generic_address_space"] = true;
173 }
174
175 const llvm::omp::GV &getGridValue() const override {
176 return llvm::omp::NVPTXGridValues;
177 }
178
179 /// \returns If a target requires an address within a target specific address
180 /// space \p AddressSpace to be converted in order to be used, then return the
181 /// corresponding target specific DWARF address space.
182 ///
183 /// \returns Otherwise return std::nullopt and no conversion will be emitted
184 /// in the DWARF.
185 std::optional<unsigned>
186 getDWARFAddressSpace(unsigned AddressSpace) const override {
187 if (AddressSpace >= std::size(NVPTXDWARFAddrSpaceMap) ||
188 NVPTXDWARFAddrSpaceMap[AddressSpace] < 0)
189 return std::nullopt;
190 return NVPTXDWARFAddrSpaceMap[AddressSpace];
191 }
192
194 // CUDA compilations support all of the host's calling conventions.
195 //
196 // TODO: We should warn if you apply a non-default CC to anything other than
197 // a host function.
198 if (HostTarget)
199 return HostTarget->checkCallingConvention(CC);
200 return CC == CC_DeviceKernel ? CCCR_OK : CCCR_Warning;
201 }
202
203 bool hasBitIntType() const override { return true; }
204 bool hasBFloat16Type() const override { return true; }
205
206 OffloadArch getGPU() const { return GPU; }
207};
208} // namespace targets
209} // namespace clang
210#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
The type of a lookup table which maps from language-specific address spaces to target-specific ones.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
A processor an offloading action can target.
Definition OffloadArch.h:32
TargetInfo(const llvm::Triple &T)
BuiltinVaListKind
The different kinds of __builtin_va_list types defined by the target implementation.
Definition TargetInfo.h:339
@ CharPtrBuiltinVaList
typedef char* __builtin_va_list;
Definition TargetInfo.h:341
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:203
BuiltinVaListKind getBuiltinVaListKind() const override
Returns the kind of __builtin_va_list type that should be used with this target.
Definition NVPTX.h:134
OffloadArch getGPU() const
Definition NVPTX.h:206
virtual bool isAddressSpaceSupersetOf(LangAS A, LangAS B) const override
Returns true if an address space can be safely converted to another.
Definition NVPTX.h:91
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:185
ArrayRef< TargetInfo::GCCRegAlias > getGCCRegAliases() const override
Definition NVPTX.h:107
void fillValidCPUList(SmallVectorImpl< StringRef > &Values) const override
Fill a SmallVectorImpl with the valid values to setCPU.
Definition NVPTX.h:142
bool isCLZForZeroUndef() const override
The __builtin_clz* and __builtin_ctz* built-in functions are specified to have undefined results for ...
Definition NVPTX.h:72
bool validateAsmConstraint(const char *&Name, TargetInfo::ConstraintInfo &Info) const override
Definition NVPTX.h:112
bool hasBFloat16Type() const override
Determine whether the _BFloat16 type is supported on this target.
Definition NVPTX.h:204
std::string_view getClobbers() const override
Returns a string of target-specific clobbers, in LLVM format.
Definition NVPTX.h:129
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:210
std::optional< unsigned > getDWARFAddressSpace(unsigned AddressSpace) const override
Definition NVPTX.h:186
const llvm::omp::GV & getGridValue() const override
Definition NVPTX.h:175
CallingConvCheckResult checkCallingConvention(CallingConv CC) const override
Determines whether a given calling convention is valid for the target.
Definition NVPTX.h:193
bool setCPU(StringRef Name) override
Target the specified CPU.
Definition NVPTX.h:146
bool isValidCPUName(StringRef Name) const override
Determine whether this TargetInfo supports the given CPU name.
Definition NVPTX.h:138
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:75
void setSupportedOpenCLOpts() override
Set supported OpenCL extensions and optional core features.
Definition NVPTX.h:151
Defines the clang::TargetInfo interface.
static constexpr LangASMap NVPTXAddrSpaceMap
Definition NVPTX.h:27
static const int NVPTXDWARFAddrSpaceMap[]
The DWARF address class.
Definition NVPTX.h:49
Top level wrappers for InstallAPI frontend operations.
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)
void fillValidOffloadArchList(llvm::SmallVectorImpl< llvm::StringRef > &Values)
Append the canonical names of all NVIDIA and AMDGPU GPUs.
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition Specifiers.h:279
@ CC_DeviceKernel
Definition Specifiers.h:292
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 uint32_t