clang 17.0.0git
SPIR.h
Go to the documentation of this file.
1//===--- SPIR.h - Declare SPIR and SPIR-V 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 SPIR and SPIR-V TargetInfo objects.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_LIB_BASIC_TARGETS_SPIR_H
14#define LLVM_CLANG_LIB_BASIC_TARGETS_SPIR_H
15
18#include "llvm/ADT/Triple.h"
19#include "llvm/Support/Compiler.h"
20#include <optional>
21
22namespace clang {
23namespace targets {
24
25// Used by both the SPIR and SPIR-V targets.
26static const unsigned SPIRDefIsPrivMap[] = {
27 0, // Default
28 1, // opencl_global
29 3, // opencl_local
30 2, // opencl_constant
31 0, // opencl_private
32 4, // opencl_generic
33 5, // opencl_global_device
34 6, // opencl_global_host
35 0, // cuda_device
36 0, // cuda_constant
37 0, // cuda_shared
38 // SYCL address space values for this map are dummy
39 0, // sycl_global
40 0, // sycl_global_device
41 0, // sycl_global_host
42 0, // sycl_local
43 0, // sycl_private
44 0, // ptr32_sptr
45 0, // ptr32_uptr
46 0, // ptr64
47 0, // hlsl_groupshared
48};
49
50// Used by both the SPIR and SPIR-V targets.
51static const unsigned SPIRDefIsGenMap[] = {
52 4, // Default
53 // OpenCL address space values for this map are dummy and they can't be used
54 0, // opencl_global
55 0, // opencl_local
56 0, // opencl_constant
57 0, // opencl_private
58 0, // opencl_generic
59 0, // opencl_global_device
60 0, // opencl_global_host
61 // cuda_* address space mapping is intended for HIPSPV (HIP to SPIR-V
62 // translation). This mapping is enabled when the language mode is HIP.
63 1, // cuda_device
64 // cuda_constant pointer can be casted to default/"flat" pointer, but in
65 // SPIR-V casts between constant and generic pointers are not allowed. For
66 // this reason cuda_constant is mapped to SPIR-V CrossWorkgroup.
67 1, // cuda_constant
68 3, // cuda_shared
69 1, // sycl_global
70 5, // sycl_global_device
71 6, // sycl_global_host
72 3, // sycl_local
73 0, // sycl_private
74 0, // ptr32_sptr
75 0, // ptr32_uptr
76 0, // ptr64
77 0, // hlsl_groupshared
78};
79
80// Base class for SPIR and SPIR-V target info.
81class LLVM_LIBRARY_VISIBILITY BaseSPIRTargetInfo : public TargetInfo {
82protected:
83 BaseSPIRTargetInfo(const llvm::Triple &Triple, const TargetOptions &)
84 : TargetInfo(Triple) {
85 assert((Triple.isSPIR() || Triple.isSPIRV()) &&
86 "Invalid architecture for SPIR or SPIR-V.");
87 assert(getTriple().getOS() == llvm::Triple::UnknownOS &&
88 "SPIR(-V) target must use unknown OS");
89 assert(getTriple().getEnvironment() == llvm::Triple::UnknownEnvironment &&
90 "SPIR(-V) target must use unknown environment type");
91 TLSSupported = false;
92 VLASupported = false;
93 LongWidth = LongAlign = 64;
94 AddrSpaceMap = &SPIRDefIsPrivMap;
95 UseAddrSpaceMapMangling = true;
96 HasLegalHalfType = true;
97 HasFloat16 = true;
98 // Define available target features
99 // These must be defined in sorted order!
100 NoAsmVariants = true;
101 }
102
103public:
104 // SPIR supports the half type and the only llvm intrinsic allowed in SPIR is
105 // memcpy as per section 3 of the SPIR spec.
106 bool useFP16ConversionIntrinsics() const override { return false; }
107
109 return std::nullopt;
110 }
111
112 const char *getClobbers() const override { return ""; }
113
115 return std::nullopt;
116 }
117
118 bool validateAsmConstraint(const char *&Name,
119 TargetInfo::ConstraintInfo &info) const override {
120 return true;
121 }
122
124 return std::nullopt;
125 }
126
128 return TargetInfo::VoidPtrBuiltinVaList;
129 }
130
131 std::optional<unsigned>
132 getDWARFAddressSpace(unsigned AddressSpace) const override {
133 return AddressSpace;
134 }
135
137 return (CC == CC_SpirFunction || CC == CC_OpenCLKernel) ? CCCR_OK
138 : CCCR_Warning;
139 }
140
142 return CC_SpirFunction;
143 }
144
145 void setAddressSpaceMap(bool DefaultIsGeneric) {
146 AddrSpaceMap = DefaultIsGeneric ? &SPIRDefIsGenMap : &SPIRDefIsPrivMap;
147 }
148
149 void adjust(DiagnosticsEngine &Diags, LangOptions &Opts) override {
150 TargetInfo::adjust(Diags, Opts);
151 // FIXME: SYCL specification considers unannotated pointers and references
152 // to be pointing to the generic address space. See section 5.9.3 of
153 // SYCL 2020 specification.
154 // Currently, there is no way of representing SYCL's and HIP/CUDA's default
155 // address space language semantic along with the semantics of embedded C's
156 // default address space in the same address space map. Hence the map needs
157 // to be reset to allow mapping to the desired value of 'Default' entry for
158 // SYCL and HIP/CUDA.
159 setAddressSpaceMap(
160 /*DefaultIsGeneric=*/Opts.SYCLIsDevice ||
161 // The address mapping from HIP/CUDA language for device code is only
162 // defined for SPIR-V.
163 (getTriple().isSPIRV() && Opts.CUDAIsDevice));
164 }
165
166 void setSupportedOpenCLOpts() override {
167 // Assume all OpenCL extensions and optional core features are supported
168 // for SPIR and SPIR-V since they are generic targets.
169 supportAllOpenCLOpts();
170 }
171
172 bool hasBitIntType() const override { return true; }
173
174 bool hasInt128Type() const override { return false; }
175};
176
177class LLVM_LIBRARY_VISIBILITY SPIRTargetInfo : public BaseSPIRTargetInfo {
178public:
179 SPIRTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
180 : BaseSPIRTargetInfo(Triple, Opts) {
181 assert(Triple.isSPIR() && "Invalid architecture for SPIR.");
182 assert(getTriple().getOS() == llvm::Triple::UnknownOS &&
183 "SPIR target must use unknown OS");
184 assert(getTriple().getEnvironment() == llvm::Triple::UnknownEnvironment &&
185 "SPIR target must use unknown environment type");
186 }
187
188 void getTargetDefines(const LangOptions &Opts,
189 MacroBuilder &Builder) const override;
190
191 bool hasFeature(StringRef Feature) const override {
192 return Feature == "spir";
193 }
194
195 bool checkArithmeticFenceSupported() const override { return true; }
196};
197
198class LLVM_LIBRARY_VISIBILITY SPIR32TargetInfo : public SPIRTargetInfo {
199public:
200 SPIR32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
201 : SPIRTargetInfo(Triple, Opts) {
202 assert(Triple.getArch() == llvm::Triple::spir &&
203 "Invalid architecture for 32-bit SPIR.");
204 PointerWidth = PointerAlign = 32;
205 SizeType = TargetInfo::UnsignedInt;
206 PtrDiffType = IntPtrType = TargetInfo::SignedInt;
207 resetDataLayout("e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-"
208 "v96:128-v192:256-v256:256-v512:512-v1024:1024");
209 }
210
211 void getTargetDefines(const LangOptions &Opts,
212 MacroBuilder &Builder) const override;
213};
214
215class LLVM_LIBRARY_VISIBILITY SPIR64TargetInfo : public SPIRTargetInfo {
216public:
217 SPIR64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
218 : SPIRTargetInfo(Triple, Opts) {
219 assert(Triple.getArch() == llvm::Triple::spir64 &&
220 "Invalid architecture for 64-bit SPIR.");
221 PointerWidth = PointerAlign = 64;
222 SizeType = TargetInfo::UnsignedLong;
223 PtrDiffType = IntPtrType = TargetInfo::SignedLong;
224 resetDataLayout("e-i64:64-v16:16-v24:32-v32:32-v48:64-"
225 "v96:128-v192:256-v256:256-v512:512-v1024:1024");
226 }
227
228 void getTargetDefines(const LangOptions &Opts,
229 MacroBuilder &Builder) const override;
230};
231
232class LLVM_LIBRARY_VISIBILITY SPIRVTargetInfo : public BaseSPIRTargetInfo {
233public:
234 SPIRVTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
235 : BaseSPIRTargetInfo(Triple, Opts) {
236 assert(Triple.isSPIRV() && "Invalid architecture for SPIR-V.");
237 assert(getTriple().getOS() == llvm::Triple::UnknownOS &&
238 "SPIR-V target must use unknown OS");
239 assert(getTriple().getEnvironment() == llvm::Triple::UnknownEnvironment &&
240 "SPIR-V target must use unknown environment type");
241 }
242
243 void getTargetDefines(const LangOptions &Opts,
244 MacroBuilder &Builder) const override;
245
246 bool hasFeature(StringRef Feature) const override {
247 return Feature == "spirv";
248 }
249};
250
251class LLVM_LIBRARY_VISIBILITY SPIRV32TargetInfo : public SPIRVTargetInfo {
252public:
253 SPIRV32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
254 : SPIRVTargetInfo(Triple, Opts) {
255 assert(Triple.getArch() == llvm::Triple::spirv32 &&
256 "Invalid architecture for 32-bit SPIR-V.");
257 PointerWidth = PointerAlign = 32;
258 SizeType = TargetInfo::UnsignedInt;
259 PtrDiffType = IntPtrType = TargetInfo::SignedInt;
260 resetDataLayout("e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-"
261 "v96:128-v192:256-v256:256-v512:512-v1024:1024");
262 }
263
264 void getTargetDefines(const LangOptions &Opts,
265 MacroBuilder &Builder) const override;
266};
267
268class LLVM_LIBRARY_VISIBILITY SPIRV64TargetInfo : public SPIRVTargetInfo {
269public:
270 SPIRV64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
271 : SPIRVTargetInfo(Triple, Opts) {
272 assert(Triple.getArch() == llvm::Triple::spirv64 &&
273 "Invalid architecture for 64-bit SPIR-V.");
274 PointerWidth = PointerAlign = 64;
275 SizeType = TargetInfo::UnsignedLong;
276 PtrDiffType = IntPtrType = TargetInfo::SignedLong;
277 resetDataLayout("e-i64:64-v16:16-v24:32-v32:32-v48:64-"
278 "v96:128-v192:256-v256:256-v512:512-v1024:1024");
279 }
280
281 void getTargetDefines(const LangOptions &Opts,
282 MacroBuilder &Builder) const override;
283};
284
285} // namespace targets
286} // namespace clang
287#endif // LLVM_CLANG_LIB_BASIC_TARGETS_SPIR_H
Defines the clang::TargetOptions class.
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:192
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:82
Exposes information about the current target.
Definition: TargetInfo.h:206
BuiltinVaListKind
The different kinds of __builtin_va_list types defined by the target implementation.
Definition: TargetInfo.h:288
Options for controlling the target.
Definition: TargetOptions.h:26
ArrayRef< Builtin::Info > getTargetBuiltins() const override
Return information about target-specific builtins for the current primary target, and info about whic...
Definition: SPIR.h:108
BuiltinVaListKind getBuiltinVaListKind() const override
Returns the kind of __builtin_va_list type that should be used with this target.
Definition: SPIR.h:127
ArrayRef< const char * > getGCCRegNames() const override
Definition: SPIR.h:114
void adjust(DiagnosticsEngine &Diags, LangOptions &Opts) override
Set forced language options.
Definition: SPIR.h:149
bool validateAsmConstraint(const char *&Name, TargetInfo::ConstraintInfo &info) const override
Definition: SPIR.h:118
bool hasInt128Type() const override
Determine whether the __int128 type is supported on this target.
Definition: SPIR.h:174
const char * getClobbers() const override
Returns a string of target-specific clobbers, in LLVM format.
Definition: SPIR.h:112
BaseSPIRTargetInfo(const llvm::Triple &Triple, const TargetOptions &)
Definition: SPIR.h:83
void setAddressSpaceMap(bool DefaultIsGeneric)
Definition: SPIR.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: SPIR.h:106
ArrayRef< TargetInfo::GCCRegAlias > getGCCRegAliases() const override
Definition: SPIR.h:123
CallingConvCheckResult checkCallingConvention(CallingConv CC) const override
Determines whether a given calling convention is valid for the target.
Definition: SPIR.h:136
std::optional< unsigned > getDWARFAddressSpace(unsigned AddressSpace) const override
Definition: SPIR.h:132
bool hasBitIntType() const override
Determine whether the _BitInt type is supported on this target.
Definition: SPIR.h:172
void setSupportedOpenCLOpts() override
Set supported OpenCL extensions and optional core features.
Definition: SPIR.h:166
CallingConv getDefaultCallingConv() const override
Gets the default calling convention for the given target and declaration context.
Definition: SPIR.h:141
SPIR32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: SPIR.h:200
SPIR64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: SPIR.h:217
SPIRTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: SPIR.h:179
bool checkArithmeticFenceSupported() const override
Controls if __arithmetic_fence is supported in the targeted backend.
Definition: SPIR.h:195
bool hasFeature(StringRef Feature) const override
Determine whether the given target has the given feature.
Definition: SPIR.h:191
SPIRV32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: SPIR.h:253
SPIRV64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: SPIR.h:270
bool hasFeature(StringRef Feature) const override
Determine whether the given target has the given feature.
Definition: SPIR.h:246
SPIRVTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: SPIR.h:234
Defines the clang::TargetInfo interface.
static const unsigned SPIRDefIsPrivMap[]
Definition: SPIR.h:26
static const unsigned SPIRDefIsGenMap[]
Definition: SPIR.h:51
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:266
@ CC_OpenCLKernel
Definition: Specifiers.h:280
@ CC_SpirFunction
Definition: Specifiers.h:279