clang 20.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
16#include "Targets.h"
19#include "llvm/Support/Compiler.h"
20#include "llvm/Support/VersionTuple.h"
21#include "llvm/TargetParser/Triple.h"
22#include <optional>
23
24namespace clang {
25namespace targets {
26
27// Used by both the SPIR and SPIR-V targets.
28static const unsigned SPIRDefIsPrivMap[] = {
29 0, // Default
30 1, // opencl_global
31 3, // opencl_local
32 2, // opencl_constant
33 0, // opencl_private
34 4, // opencl_generic
35 5, // opencl_global_device
36 6, // opencl_global_host
37 0, // cuda_device
38 0, // cuda_constant
39 0, // cuda_shared
40 // SYCL address space values for this map are dummy
41 0, // sycl_global
42 0, // sycl_global_device
43 0, // sycl_global_host
44 0, // sycl_local
45 0, // sycl_private
46 0, // ptr32_sptr
47 0, // ptr32_uptr
48 0, // ptr64
49 0, // hlsl_groupshared
50 // Wasm address space values for this target are dummy values,
51 // as it is only enabled for Wasm targets.
52 20, // wasm_funcref
53};
54
55// Used by both the SPIR and SPIR-V targets.
56static const unsigned SPIRDefIsGenMap[] = {
57 4, // Default
58 // OpenCL address space values for this map are dummy and they can't be used
59 0, // opencl_global
60 0, // opencl_local
61 0, // opencl_constant
62 0, // opencl_private
63 0, // opencl_generic
64 0, // opencl_global_device
65 0, // opencl_global_host
66 // cuda_* address space mapping is intended for HIPSPV (HIP to SPIR-V
67 // translation). This mapping is enabled when the language mode is HIP.
68 1, // cuda_device
69 // cuda_constant pointer can be casted to default/"flat" pointer, but in
70 // SPIR-V casts between constant and generic pointers are not allowed. For
71 // this reason cuda_constant is mapped to SPIR-V CrossWorkgroup.
72 1, // cuda_constant
73 3, // cuda_shared
74 1, // sycl_global
75 5, // sycl_global_device
76 6, // sycl_global_host
77 3, // sycl_local
78 0, // sycl_private
79 0, // ptr32_sptr
80 0, // ptr32_uptr
81 0, // ptr64
82 0, // hlsl_groupshared
83 // Wasm address space values for this target are dummy values,
84 // as it is only enabled for Wasm targets.
85 20, // wasm_funcref
86};
87
88// Base class for SPIR and SPIR-V target info.
89class LLVM_LIBRARY_VISIBILITY BaseSPIRTargetInfo : public TargetInfo {
90 std::unique_ptr<TargetInfo> HostTarget;
91
92protected:
93 BaseSPIRTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
94 : TargetInfo(Triple) {
95 assert((Triple.isSPIR() || Triple.isSPIRV()) &&
96 "Invalid architecture for SPIR or SPIR-V.");
97 TLSSupported = false;
98 VLASupported = false;
99 LongWidth = LongAlign = 64;
100 AddrSpaceMap = &SPIRDefIsPrivMap;
101 UseAddrSpaceMapMangling = true;
102 HasLegalHalfType = true;
103 HasFloat16 = true;
104 // Define available target features
105 // These must be defined in sorted order!
106 NoAsmVariants = true;
107
108 llvm::Triple HostTriple(Opts.HostTriple);
109 if (!HostTriple.isSPIR() && !HostTriple.isSPIRV() &&
110 HostTriple.getArch() != llvm::Triple::UnknownArch) {
111 HostTarget = AllocateTarget(llvm::Triple(Opts.HostTriple), Opts);
112
113 // Copy properties from host target.
114 BoolWidth = HostTarget->getBoolWidth();
115 BoolAlign = HostTarget->getBoolAlign();
116 IntWidth = HostTarget->getIntWidth();
117 IntAlign = HostTarget->getIntAlign();
118 HalfWidth = HostTarget->getHalfWidth();
119 HalfAlign = HostTarget->getHalfAlign();
120 FloatWidth = HostTarget->getFloatWidth();
121 FloatAlign = HostTarget->getFloatAlign();
122 DoubleWidth = HostTarget->getDoubleWidth();
123 DoubleAlign = HostTarget->getDoubleAlign();
124 LongWidth = HostTarget->getLongWidth();
125 LongAlign = HostTarget->getLongAlign();
126 LongLongWidth = HostTarget->getLongLongWidth();
127 LongLongAlign = HostTarget->getLongLongAlign();
128 MinGlobalAlign =
129 HostTarget->getMinGlobalAlign(/* TypeSize = */ 0,
130 /* HasNonWeakDef = */ true);
131 NewAlign = HostTarget->getNewAlign();
132 DefaultAlignForAttributeAligned =
133 HostTarget->getDefaultAlignForAttributeAligned();
134 IntMaxType = HostTarget->getIntMaxType();
135 WCharType = HostTarget->getWCharType();
136 WIntType = HostTarget->getWIntType();
137 Char16Type = HostTarget->getChar16Type();
138 Char32Type = HostTarget->getChar32Type();
139 Int64Type = HostTarget->getInt64Type();
140 SigAtomicType = HostTarget->getSigAtomicType();
141 ProcessIDType = HostTarget->getProcessIDType();
142
143 UseBitFieldTypeAlignment = HostTarget->useBitFieldTypeAlignment();
144 UseZeroLengthBitfieldAlignment =
145 HostTarget->useZeroLengthBitfieldAlignment();
146 UseExplicitBitFieldAlignment = HostTarget->useExplicitBitFieldAlignment();
147 ZeroLengthBitfieldBoundary = HostTarget->getZeroLengthBitfieldBoundary();
148
149 // This is a bit of a lie, but it controls __GCC_ATOMIC_XXX_LOCK_FREE, and
150 // we need those macros to be identical on host and device, because (among
151 // other things) they affect which standard library classes are defined,
152 // and we need all classes to be defined on both the host and device.
153 MaxAtomicInlineWidth = HostTarget->getMaxAtomicInlineWidth();
154 }
155 }
156
157public:
158 // SPIR supports the half type and the only llvm intrinsic allowed in SPIR is
159 // memcpy as per section 3 of the SPIR spec.
160 bool useFP16ConversionIntrinsics() const override { return false; }
161
162 ArrayRef<Builtin::Info> getTargetBuiltins() const override { return {}; }
163
164 std::string_view getClobbers() const override { return ""; }
165
166 ArrayRef<const char *> getGCCRegNames() const override { return {}; }
167
168 bool validateAsmConstraint(const char *&Name,
169 TargetInfo::ConstraintInfo &info) const override {
170 return true;
171 }
172
174 return {};
175 }
176
178 return TargetInfo::VoidPtrBuiltinVaList;
179 }
180
181 std::optional<unsigned>
182 getDWARFAddressSpace(unsigned AddressSpace) const override {
183 return AddressSpace;
184 }
185
187 return (CC == CC_SpirFunction || CC == CC_OpenCLKernel) ? CCCR_OK
188 : CCCR_Warning;
189 }
190
192 return CC_SpirFunction;
193 }
194
195 void setAddressSpaceMap(bool DefaultIsGeneric) {
196 AddrSpaceMap = DefaultIsGeneric ? &SPIRDefIsGenMap : &SPIRDefIsPrivMap;
197 }
198
199 void adjust(DiagnosticsEngine &Diags, LangOptions &Opts) override {
200 TargetInfo::adjust(Diags, Opts);
201 // FIXME: SYCL specification considers unannotated pointers and references
202 // to be pointing to the generic address space. See section 5.9.3 of
203 // SYCL 2020 specification.
204 // Currently, there is no way of representing SYCL's and HIP/CUDA's default
205 // address space language semantic along with the semantics of embedded C's
206 // default address space in the same address space map. Hence the map needs
207 // to be reset to allow mapping to the desired value of 'Default' entry for
208 // SYCL and HIP/CUDA.
209 setAddressSpaceMap(
210 /*DefaultIsGeneric=*/Opts.SYCLIsDevice ||
211 // The address mapping from HIP/CUDA language for device code is only
212 // defined for SPIR-V.
213 (getTriple().isSPIRV() && Opts.CUDAIsDevice));
214 }
215
216 void setSupportedOpenCLOpts() override {
217 // Assume all OpenCL extensions and optional core features are supported
218 // for SPIR and SPIR-V since they are generic targets.
219 supportAllOpenCLOpts();
220 }
221
222 bool hasBitIntType() const override { return true; }
223
224 bool hasInt128Type() const override { return false; }
225};
226
227class LLVM_LIBRARY_VISIBILITY SPIRTargetInfo : public BaseSPIRTargetInfo {
228public:
229 SPIRTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
230 : BaseSPIRTargetInfo(Triple, Opts) {
231 assert(Triple.isSPIR() && "Invalid architecture for SPIR.");
232 assert(getTriple().getOS() == llvm::Triple::UnknownOS &&
233 "SPIR target must use unknown OS");
234 assert(getTriple().getEnvironment() == llvm::Triple::UnknownEnvironment &&
235 "SPIR target must use unknown environment type");
236 }
237
238 void getTargetDefines(const LangOptions &Opts,
239 MacroBuilder &Builder) const override;
240
241 bool hasFeature(StringRef Feature) const override {
242 return Feature == "spir";
243 }
244
245 bool checkArithmeticFenceSupported() const override { return true; }
246};
247
248class LLVM_LIBRARY_VISIBILITY SPIR32TargetInfo : public SPIRTargetInfo {
249public:
250 SPIR32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
251 : SPIRTargetInfo(Triple, Opts) {
252 assert(Triple.getArch() == llvm::Triple::spir &&
253 "Invalid architecture for 32-bit SPIR.");
254 PointerWidth = PointerAlign = 32;
255 SizeType = TargetInfo::UnsignedInt;
256 PtrDiffType = IntPtrType = TargetInfo::SignedInt;
257 resetDataLayout("e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-"
258 "v96:128-v192:256-v256:256-v512:512-v1024:1024-G1");
259 }
260
261 void getTargetDefines(const LangOptions &Opts,
262 MacroBuilder &Builder) const override;
263};
264
265class LLVM_LIBRARY_VISIBILITY SPIR64TargetInfo : public SPIRTargetInfo {
266public:
267 SPIR64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
268 : SPIRTargetInfo(Triple, Opts) {
269 assert(Triple.getArch() == llvm::Triple::spir64 &&
270 "Invalid architecture for 64-bit SPIR.");
271 PointerWidth = PointerAlign = 64;
272 SizeType = TargetInfo::UnsignedLong;
273 PtrDiffType = IntPtrType = TargetInfo::SignedLong;
274 resetDataLayout("e-i64:64-v16:16-v24:32-v32:32-v48:64-"
275 "v96:128-v192:256-v256:256-v512:512-v1024:1024-G1");
276 }
277
278 void getTargetDefines(const LangOptions &Opts,
279 MacroBuilder &Builder) const override;
280};
281
282class LLVM_LIBRARY_VISIBILITY BaseSPIRVTargetInfo : public BaseSPIRTargetInfo {
283public:
284 BaseSPIRVTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
285 : BaseSPIRTargetInfo(Triple, Opts) {
286 assert(Triple.isSPIRV() && "Invalid architecture for SPIR-V.");
287 }
288
289 bool hasFeature(StringRef Feature) const override {
290 return Feature == "spirv";
291 }
292
293 void getTargetDefines(const LangOptions &Opts,
294 MacroBuilder &Builder) const override;
295};
296
297class LLVM_LIBRARY_VISIBILITY SPIRVTargetInfo : public BaseSPIRVTargetInfo {
298public:
299 SPIRVTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
300 : BaseSPIRVTargetInfo(Triple, Opts) {
301 assert(Triple.getArch() == llvm::Triple::spirv &&
302 "Invalid architecture for Logical SPIR-V.");
303 assert(Triple.getOS() == llvm::Triple::Vulkan &&
304 Triple.getVulkanVersion() != llvm::VersionTuple(0) &&
305 "Logical SPIR-V requires a valid Vulkan environment.");
306 assert(Triple.getEnvironment() >= llvm::Triple::Pixel &&
307 Triple.getEnvironment() <= llvm::Triple::Amplification &&
308 "Logical SPIR-V environment must be a valid shader stage.");
309 PointerWidth = PointerAlign = 64;
310
311 // SPIR-V IDs are represented with a single 32-bit word.
312 SizeType = TargetInfo::UnsignedInt;
313 resetDataLayout("e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-"
314 "v256:256-v512:512-v1024:1024-n8:16:32:64-G1");
315 }
316
317 void getTargetDefines(const LangOptions &Opts,
318 MacroBuilder &Builder) const override;
319};
320
321class LLVM_LIBRARY_VISIBILITY SPIRV32TargetInfo : public BaseSPIRVTargetInfo {
322public:
323 SPIRV32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
324 : BaseSPIRVTargetInfo(Triple, Opts) {
325 assert(Triple.getArch() == llvm::Triple::spirv32 &&
326 "Invalid architecture for 32-bit SPIR-V.");
327 assert(getTriple().getOS() == llvm::Triple::UnknownOS &&
328 "32-bit SPIR-V target must use unknown OS");
329 assert(getTriple().getEnvironment() == llvm::Triple::UnknownEnvironment &&
330 "32-bit SPIR-V target must use unknown environment type");
331 PointerWidth = PointerAlign = 32;
332 SizeType = TargetInfo::UnsignedInt;
333 PtrDiffType = IntPtrType = TargetInfo::SignedInt;
334 // SPIR-V has core support for atomic ops, and Int32 is always available;
335 // we take the maximum because it's possible the Host supports wider types.
336 MaxAtomicInlineWidth = std::max<unsigned char>(MaxAtomicInlineWidth, 32);
337 resetDataLayout("e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-"
338 "v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64-G1");
339 }
340
341 void getTargetDefines(const LangOptions &Opts,
342 MacroBuilder &Builder) const override;
343};
344
345class LLVM_LIBRARY_VISIBILITY SPIRV64TargetInfo : public BaseSPIRVTargetInfo {
346public:
347 SPIRV64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
348 : BaseSPIRVTargetInfo(Triple, Opts) {
349 assert(Triple.getArch() == llvm::Triple::spirv64 &&
350 "Invalid architecture for 64-bit SPIR-V.");
351 assert(getTriple().getOS() == llvm::Triple::UnknownOS &&
352 "64-bit SPIR-V target must use unknown OS");
353 assert(getTriple().getEnvironment() == llvm::Triple::UnknownEnvironment &&
354 "64-bit SPIR-V target must use unknown environment type");
355 PointerWidth = PointerAlign = 64;
356 SizeType = TargetInfo::UnsignedLong;
357 PtrDiffType = IntPtrType = TargetInfo::SignedLong;
358 // SPIR-V has core support for atomic ops, and Int64 is always available;
359 // we take the maximum because it's possible the Host supports wider types.
360 MaxAtomicInlineWidth = std::max<unsigned char>(MaxAtomicInlineWidth, 64);
361 resetDataLayout("e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-"
362 "v256:256-v512:512-v1024:1024-n8:16:32:64-G1");
363 }
364
365 void getTargetDefines(const LangOptions &Opts,
366 MacroBuilder &Builder) const override;
367};
368
369class LLVM_LIBRARY_VISIBILITY SPIRV64AMDGCNTargetInfo final
370 : public BaseSPIRVTargetInfo {
371public:
372 SPIRV64AMDGCNTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
373 : BaseSPIRVTargetInfo(Triple, Opts) {
374 assert(Triple.getArch() == llvm::Triple::spirv64 &&
375 "Invalid architecture for 64-bit AMDGCN SPIR-V.");
376 assert(Triple.getVendor() == llvm::Triple::VendorType::AMD &&
377 "64-bit AMDGCN SPIR-V target must use AMD vendor");
378 assert(getTriple().getOS() == llvm::Triple::OSType::AMDHSA &&
379 "64-bit AMDGCN SPIR-V target must use AMDHSA OS");
380 assert(getTriple().getEnvironment() == llvm::Triple::UnknownEnvironment &&
381 "64-bit SPIR-V target must use unknown environment type");
382 PointerWidth = PointerAlign = 64;
383 SizeType = TargetInfo::UnsignedLong;
384 PtrDiffType = IntPtrType = TargetInfo::SignedLong;
385 AddrSpaceMap = &SPIRDefIsGenMap;
386
387 resetDataLayout("e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-"
388 "v256:256-v512:512-v1024:1024-n32:64-S32-G1-P4-A0");
389
390 BFloat16Width = BFloat16Align = 16;
391 BFloat16Format = &llvm::APFloat::BFloat();
392
393 HasLegalHalfType = true;
394 HasFloat16 = true;
395 HalfArgsAndReturns = true;
396 }
397
398 bool hasBFloat16Type() const override { return true; }
399
400 ArrayRef<const char *> getGCCRegNames() const override;
401
402 bool initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
403 StringRef,
404 const std::vector<std::string> &) const override;
405
406 bool validateAsmConstraint(const char *&Name,
407 TargetInfo::ConstraintInfo &Info) const override;
408
409 std::string convertConstraint(const char *&Constraint) const override;
410
411 ArrayRef<Builtin::Info> getTargetBuiltins() const override;
412
413 void getTargetDefines(const LangOptions &Opts,
414 MacroBuilder &Builder) const override;
415
416 void setAuxTarget(const TargetInfo *Aux) override;
417
418 void adjust(DiagnosticsEngine &Diags, LangOptions &Opts) override {
419 TargetInfo::adjust(Diags, Opts);
420 }
421
422 bool hasInt128Type() const override { return TargetInfo::hasInt128Type(); }
423};
424
425} // namespace targets
426} // namespace clang
427#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:231
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:499
Exposes information about the current target.
Definition: TargetInfo.h:220
BuiltinVaListKind
The different kinds of __builtin_va_list types defined by the target implementation.
Definition: TargetInfo.h:318
Options for controlling the target.
Definition: TargetOptions.h:26
std::string HostTriple
When compiling for the device side, contains the triple used to compile for the host.
Definition: TargetOptions.h:33
ArrayRef< Builtin::Info > getTargetBuiltins() const override
Return information about target-specific builtins for the current primary target, and info about whic...
Definition: SPIR.h:162
BuiltinVaListKind getBuiltinVaListKind() const override
Returns the kind of __builtin_va_list type that should be used with this target.
Definition: SPIR.h:177
ArrayRef< const char * > getGCCRegNames() const override
Definition: SPIR.h:166
void adjust(DiagnosticsEngine &Diags, LangOptions &Opts) override
Set forced language options.
Definition: SPIR.h:199
bool validateAsmConstraint(const char *&Name, TargetInfo::ConstraintInfo &info) const override
Definition: SPIR.h:168
bool hasInt128Type() const override
Determine whether the __int128 type is supported on this target.
Definition: SPIR.h:224
void setAddressSpaceMap(bool DefaultIsGeneric)
Definition: SPIR.h:195
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:160
ArrayRef< TargetInfo::GCCRegAlias > getGCCRegAliases() const override
Definition: SPIR.h:173
CallingConvCheckResult checkCallingConvention(CallingConv CC) const override
Determines whether a given calling convention is valid for the target.
Definition: SPIR.h:186
std::optional< unsigned > getDWARFAddressSpace(unsigned AddressSpace) const override
Definition: SPIR.h:182
BaseSPIRTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: SPIR.h:93
bool hasBitIntType() const override
Determine whether the _BitInt type is supported on this target.
Definition: SPIR.h:222
void setSupportedOpenCLOpts() override
Set supported OpenCL extensions and optional core features.
Definition: SPIR.h:216
std::string_view getClobbers() const override
Returns a string of target-specific clobbers, in LLVM format.
Definition: SPIR.h:164
CallingConv getDefaultCallingConv() const override
Gets the default calling convention for the given target and declaration context.
Definition: SPIR.h:191
bool hasFeature(StringRef Feature) const override
Determine whether the given target has the given feature.
Definition: SPIR.h:289
BaseSPIRVTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: SPIR.h:284
SPIR32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: SPIR.h:250
SPIR64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: SPIR.h:267
SPIRTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: SPIR.h:229
bool checkArithmeticFenceSupported() const override
Controls if __arithmetic_fence is supported in the targeted backend.
Definition: SPIR.h:245
bool hasFeature(StringRef Feature) const override
Determine whether the given target has the given feature.
Definition: SPIR.h:241
SPIRV32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: SPIR.h:323
void adjust(DiagnosticsEngine &Diags, LangOptions &Opts) override
Set forced language options.
Definition: SPIR.h:418
bool hasInt128Type() const override
Determine whether the __int128 type is supported on this target.
Definition: SPIR.h:422
bool hasBFloat16Type() const override
Determine whether the _BFloat16 type is supported on this target.
Definition: SPIR.h:398
SPIRV64AMDGCNTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: SPIR.h:372
SPIRV64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: SPIR.h:347
SPIRVTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Definition: SPIR.h:299
Defines the clang::TargetInfo interface.
static const unsigned SPIRDefIsPrivMap[]
Definition: SPIR.h:28
static const unsigned SPIRDefIsGenMap[]
Definition: SPIR.h:56
std::unique_ptr< clang::TargetInfo > AllocateTarget(const llvm::Triple &Triple, const clang::TargetOptions &Opts)
Definition: Targets.cpp:111
The JSON file list parser is used to communicate input to InstallAPI.
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:278
@ CC_OpenCLKernel
Definition: Specifiers.h:292
@ CC_SpirFunction
Definition: Specifiers.h:291