1 //===--- NVPTX.h - Declare NVPTX target feature support ---------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file declares NVPTX TargetInfo objects.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_LIB_BASIC_TARGETS_NVPTX_H
14 #define LLVM_CLANG_LIB_BASIC_TARGETS_NVPTX_H
16 #include "clang/Basic/Cuda.h"
17 #include "clang/Basic/TargetInfo.h"
18 #include "clang/Basic/TargetOptions.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/TargetParser/Triple.h"
26 static const unsigned NVPTXAddrSpaceMap
[] = {
32 // FIXME: generic has to be added to the target
34 1, // opencl_global_device
35 1, // opencl_global_host
40 1, // sycl_global_device
41 1, // sycl_global_host
47 0, // hlsl_groupshared
50 /// The DWARF address class. Taken from
51 /// https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
52 static const int NVPTXDWARFAddrSpaceMap
[] = {
53 -1, // Default, opencl_private or opencl_generic - not defined
56 8, // opencl_local or cuda_shared
57 4, // opencl_constant or cuda_constant
60 class LLVM_LIBRARY_VISIBILITY NVPTXTargetInfo
: public TargetInfo
{
61 static const char *const GCCRegNames
[];
64 std::unique_ptr
<TargetInfo
> HostTarget
;
67 NVPTXTargetInfo(const llvm::Triple
&Triple
, const TargetOptions
&Opts
,
68 unsigned TargetPointerWidth
);
70 void getTargetDefines(const LangOptions
&Opts
,
71 MacroBuilder
&Builder
) const override
;
73 ArrayRef
<Builtin::Info
> getTargetBuiltins() const override
;
76 initFeatureMap(llvm::StringMap
<bool> &Features
, DiagnosticsEngine
&Diags
,
78 const std::vector
<std::string
> &FeaturesVec
) const override
{
79 Features
[CudaArchToString(GPU
)] = true;
80 Features
["ptx" + std::to_string(PTXVersion
)] = true;
81 return TargetInfo::initFeatureMap(Features
, Diags
, CPU
, FeaturesVec
);
84 bool hasFeature(StringRef Feature
) const override
;
86 ArrayRef
<const char *> getGCCRegNames() const override
;
88 ArrayRef
<TargetInfo::GCCRegAlias
> getGCCRegAliases() const override
{
93 bool validateAsmConstraint(const char *&Name
,
94 TargetInfo::ConstraintInfo
&Info
) const override
{
104 Info
.setAllowsRegister();
109 const char *getClobbers() const override
{
110 // FIXME: Is this really right?
114 BuiltinVaListKind
getBuiltinVaListKind() const override
{
116 return TargetInfo::CharPtrBuiltinVaList
;
119 bool isValidCPUName(StringRef Name
) const override
{
120 return StringToCudaArch(Name
) != CudaArch::UNKNOWN
;
123 void fillValidCPUList(SmallVectorImpl
<StringRef
> &Values
) const override
{
124 for (int i
= static_cast<int>(CudaArch::SM_20
);
125 i
< static_cast<int>(CudaArch::Generic
); ++i
)
126 Values
.emplace_back(CudaArchToString(static_cast<CudaArch
>(i
)));
129 bool setCPU(const std::string
&Name
) override
{
130 GPU
= StringToCudaArch(Name
);
131 return GPU
!= CudaArch::UNKNOWN
;
134 void setSupportedOpenCLOpts() override
{
135 auto &Opts
= getSupportedOpenCLOpts();
136 Opts
["cl_clang_storage_class_specifiers"] = true;
137 Opts
["__cl_clang_function_pointers"] = true;
138 Opts
["__cl_clang_variadic_functions"] = true;
139 Opts
["__cl_clang_non_portable_kernel_param_types"] = true;
140 Opts
["__cl_clang_bitfields"] = true;
142 Opts
["cl_khr_fp64"] = true;
143 Opts
["__opencl_c_fp64"] = true;
144 Opts
["cl_khr_byte_addressable_store"] = true;
145 Opts
["cl_khr_global_int32_base_atomics"] = true;
146 Opts
["cl_khr_global_int32_extended_atomics"] = true;
147 Opts
["cl_khr_local_int32_base_atomics"] = true;
148 Opts
["cl_khr_local_int32_extended_atomics"] = true;
151 const llvm::omp::GV
&getGridValue() const override
{
152 return llvm::omp::NVPTXGridValues
;
155 /// \returns If a target requires an address within a target specific address
156 /// space \p AddressSpace to be converted in order to be used, then return the
157 /// corresponding target specific DWARF address space.
159 /// \returns Otherwise return std::nullopt and no conversion will be emitted
161 std::optional
<unsigned>
162 getDWARFAddressSpace(unsigned AddressSpace
) const override
{
163 if (AddressSpace
>= std::size(NVPTXDWARFAddrSpaceMap
) ||
164 NVPTXDWARFAddrSpaceMap
[AddressSpace
] < 0)
166 return NVPTXDWARFAddrSpaceMap
[AddressSpace
];
169 CallingConvCheckResult
checkCallingConvention(CallingConv CC
) const override
{
170 // CUDA compilations support all of the host's calling conventions.
172 // TODO: We should warn if you apply a non-default CC to anything other than
175 return HostTarget
->checkCallingConvention(CC
);
179 bool hasBitIntType() const override
{ return true; }
180 bool hasBFloat16Type() const override
{ return true; }
181 const char *getBFloat16Mangling() const override
{ return "u6__bf16"; };
183 } // namespace targets
185 #endif // LLVM_CLANG_LIB_BASIC_TARGETS_NVPTX_H