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" |
17 | #include "clang/Basic/TargetInfo.h" |
18 | #include "clang/Basic/TargetOptions.h" |
19 | #include "llvm/Support/Compiler.h" |
20 | #include "llvm/TargetParser/Triple.h" |
21 | #include <optional> |
22 | |
23 | namespace clang { |
24 | namespace targets { |
25 | |
26 | static const unsigned NVPTXAddrSpaceMap[] = { |
27 | 0, // Default |
28 | 1, // opencl_global |
29 | 3, // opencl_local |
30 | 4, // opencl_constant |
31 | 0, // opencl_private |
32 | // FIXME: generic has to be added to the target |
33 | 0, // opencl_generic |
34 | 1, // opencl_global_device |
35 | 1, // opencl_global_host |
36 | 1, // cuda_device |
37 | 4, // cuda_constant |
38 | 3, // cuda_shared |
39 | 1, // sycl_global |
40 | 1, // sycl_global_device |
41 | 1, // sycl_global_host |
42 | 3, // sycl_local |
43 | 0, // sycl_private |
44 | 0, // ptr32_sptr |
45 | 0, // ptr32_uptr |
46 | 0, // ptr64 |
47 | 0, // hlsl_groupshared |
48 | // Wasm address space values for this target are dummy values, |
49 | // as it is only enabled for Wasm targets. |
50 | 20, // wasm_funcref |
51 | }; |
52 | |
53 | /// The DWARF address class. Taken from |
54 | /// https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf |
55 | static const int NVPTXDWARFAddrSpaceMap[] = { |
56 | -1, // Default, opencl_private or opencl_generic - not defined |
57 | 5, // opencl_global |
58 | -1, |
59 | 8, // opencl_local or cuda_shared |
60 | 4, // opencl_constant or cuda_constant |
61 | }; |
62 | |
63 | class LLVM_LIBRARY_VISIBILITY NVPTXTargetInfo : public TargetInfo { |
64 | static const char *const GCCRegNames[]; |
65 | CudaArch GPU; |
66 | uint32_t PTXVersion; |
67 | std::unique_ptr<TargetInfo> HostTarget; |
68 | |
69 | public: |
70 | NVPTXTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts, |
71 | unsigned TargetPointerWidth); |
72 | |
73 | void getTargetDefines(const LangOptions &Opts, |
74 | MacroBuilder &Builder) const override; |
75 | |
76 | ArrayRef<Builtin::Info> getTargetBuiltins() const override; |
77 | |
78 | bool |
79 | initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, |
80 | StringRef CPU, |
81 | const std::vector<std::string> &FeaturesVec) const override { |
82 | if (GPU != CudaArch::UNUSED) |
83 | Features[CudaArchToString(A: GPU)] = true; |
84 | Features["ptx" + std::to_string(val: PTXVersion)] = true; |
85 | return TargetInfo::initFeatureMap(Features, Diags, CPU, FeatureVec: FeaturesVec); |
86 | } |
87 | |
88 | bool hasFeature(StringRef Feature) const override; |
89 | |
90 | ArrayRef<const char *> getGCCRegNames() const override; |
91 | |
92 | ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const override { |
93 | // No aliases. |
94 | return std::nullopt; |
95 | } |
96 | |
97 | bool validateAsmConstraint(const char *&Name, |
98 | TargetInfo::ConstraintInfo &Info) const override { |
99 | switch (*Name) { |
100 | default: |
101 | return false; |
102 | case 'c': |
103 | case 'h': |
104 | case 'r': |
105 | case 'l': |
106 | case 'f': |
107 | case 'd': |
108 | Info.setAllowsRegister(); |
109 | return true; |
110 | } |
111 | } |
112 | |
113 | std::string_view getClobbers() const override { |
114 | // FIXME: Is this really right? |
115 | return "" ; |
116 | } |
117 | |
118 | BuiltinVaListKind getBuiltinVaListKind() const override { |
119 | // FIXME: implement |
120 | return TargetInfo::CharPtrBuiltinVaList; |
121 | } |
122 | |
123 | bool isValidCPUName(StringRef Name) const override { |
124 | return StringToCudaArch(S: Name) != CudaArch::UNKNOWN; |
125 | } |
126 | |
127 | void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override { |
128 | for (int i = static_cast<int>(CudaArch::SM_20); |
129 | i < static_cast<int>(CudaArch::Generic); ++i) |
130 | Values.emplace_back(Args: CudaArchToString(A: static_cast<CudaArch>(i))); |
131 | } |
132 | |
133 | bool setCPU(const std::string &Name) override { |
134 | GPU = StringToCudaArch(S: Name); |
135 | return GPU != CudaArch::UNKNOWN; |
136 | } |
137 | |
138 | void setSupportedOpenCLOpts() override { |
139 | auto &Opts = getSupportedOpenCLOpts(); |
140 | Opts["cl_clang_storage_class_specifiers" ] = true; |
141 | Opts["__cl_clang_function_pointers" ] = true; |
142 | Opts["__cl_clang_variadic_functions" ] = true; |
143 | Opts["__cl_clang_non_portable_kernel_param_types" ] = true; |
144 | Opts["__cl_clang_bitfields" ] = true; |
145 | |
146 | Opts["cl_khr_fp64" ] = true; |
147 | Opts["__opencl_c_fp64" ] = true; |
148 | Opts["cl_khr_byte_addressable_store" ] = true; |
149 | Opts["cl_khr_global_int32_base_atomics" ] = true; |
150 | Opts["cl_khr_global_int32_extended_atomics" ] = true; |
151 | Opts["cl_khr_local_int32_base_atomics" ] = true; |
152 | Opts["cl_khr_local_int32_extended_atomics" ] = true; |
153 | } |
154 | |
155 | const llvm::omp::GV &getGridValue() const override { |
156 | return llvm::omp::NVPTXGridValues; |
157 | } |
158 | |
159 | /// \returns If a target requires an address within a target specific address |
160 | /// space \p AddressSpace to be converted in order to be used, then return the |
161 | /// corresponding target specific DWARF address space. |
162 | /// |
163 | /// \returns Otherwise return std::nullopt and no conversion will be emitted |
164 | /// in the DWARF. |
165 | std::optional<unsigned> |
166 | getDWARFAddressSpace(unsigned AddressSpace) const override { |
167 | if (AddressSpace >= std::size(NVPTXDWARFAddrSpaceMap) || |
168 | NVPTXDWARFAddrSpaceMap[AddressSpace] < 0) |
169 | return std::nullopt; |
170 | return NVPTXDWARFAddrSpaceMap[AddressSpace]; |
171 | } |
172 | |
173 | CallingConvCheckResult checkCallingConvention(CallingConv CC) const override { |
174 | // CUDA compilations support all of the host's calling conventions. |
175 | // |
176 | // TODO: We should warn if you apply a non-default CC to anything other than |
177 | // a host function. |
178 | if (HostTarget) |
179 | return HostTarget->checkCallingConvention(CC); |
180 | return CCCR_Warning; |
181 | } |
182 | |
183 | bool hasBitIntType() const override { return true; } |
184 | bool hasBFloat16Type() const override { return true; } |
185 | |
186 | CudaArch getGPU() const { return GPU; } |
187 | }; |
188 | } // namespace targets |
189 | } // namespace clang |
190 | #endif // LLVM_CLANG_LIB_BASIC_TARGETS_NVPTX_H |
191 | |