My Project
SPIRVTypes.h
Go to the documentation of this file.
1 //===- SPIRVTypes.h - MLIR SPIR-V Types -------------------------*- C++ -*-===//
2 //
3 // Part of the MLIR 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 the types in the SPIR-V dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_DIALECT_SPIRV_SPIRVTYPES_H_
14 #define MLIR_DIALECT_SPIRV_SPIRVTYPES_H_
15 
16 #include "mlir/IR/StandardTypes.h"
17 #include "mlir/IR/TypeSupport.h"
18 #include "mlir/IR/Types.h"
19 
20 // Forward declare enum classes related to op availability. Their definitions
21 // are in the TableGen'erated SPIRVEnums.h.inc and can be referenced by other
22 // dclarations in SPIRVEnums.h.inc.
23 namespace mlir {
24 namespace spirv {
25 enum class Version : uint32_t;
26 enum class Extension;
27 enum class Capability : uint32_t;
28 } // namespace spirv
29 } // namespace mlir
30 
31 // Pull in all enum type definitions and utility function declarations
32 #include "mlir/Dialect/SPIRV/SPIRVEnums.h.inc"
33 // Pull in all enum type availability query function declarations
34 #include "mlir/Dialect/SPIRV/SPIRVEnumAvailability.h.inc"
35 
36 #include <tuple>
37 
38 namespace mlir {
39 namespace spirv {
40 
41 namespace detail {
42 struct ArrayTypeStorage;
43 struct ImageTypeStorage;
44 struct PointerTypeStorage;
46 struct StructTypeStorage;
47 } // namespace detail
48 
49 namespace TypeKind {
50 enum Kind {
51  Array = Type::FIRST_SPIRV_TYPE,
57 };
58 }
59 
60 // SPIR-V composite type: VectorType, SPIR-V ArrayType, or SPIR-V StructType.
61 class CompositeType : public Type {
62 public:
63  using Type::Type;
64 
65  static bool classof(Type type);
66 
67  unsigned getNumElements() const;
68 
69  Type getElementType(unsigned) const;
70 };
71 
72 // SPIR-V array type
73 class ArrayType : public Type::TypeBase<ArrayType, CompositeType,
74  detail::ArrayTypeStorage> {
75 public:
76  using Base::Base;
77  // Zero layout specifies that is no layout
78  using LayoutInfo = uint64_t;
79 
80  static bool kindof(unsigned kind) { return kind == TypeKind::Array; }
81 
82  static ArrayType get(Type elementType, unsigned elementCount);
83 
84  static ArrayType get(Type elementType, unsigned elementCount,
85  LayoutInfo layoutInfo);
86 
87  unsigned getNumElements() const;
88 
89  Type getElementType() const;
90 
91  bool hasLayout() const;
92 
93  uint64_t getArrayStride() const;
94 };
95 
96 // SPIR-V image type
97 class ImageType
98  : public Type::TypeBase<ImageType, Type, detail::ImageTypeStorage> {
99 public:
100  using Base::Base;
101 
102  static bool kindof(unsigned kind) { return kind == TypeKind::Image; }
103 
104  static ImageType
105  get(Type elementType, Dim dim,
106  ImageDepthInfo depth = ImageDepthInfo::DepthUnknown,
107  ImageArrayedInfo arrayed = ImageArrayedInfo::NonArrayed,
108  ImageSamplingInfo samplingInfo = ImageSamplingInfo::SingleSampled,
109  ImageSamplerUseInfo samplerUse = ImageSamplerUseInfo::SamplerUnknown,
110  ImageFormat format = ImageFormat::Unknown) {
111  return ImageType::get(
112  std::tuple<Type, Dim, ImageDepthInfo, ImageArrayedInfo,
113  ImageSamplingInfo, ImageSamplerUseInfo, ImageFormat>(
114  elementType, dim, depth, arrayed, samplingInfo, samplerUse,
115  format));
116  }
117 
118  static ImageType
119  get(std::tuple<Type, Dim, ImageDepthInfo, ImageArrayedInfo,
120  ImageSamplingInfo, ImageSamplerUseInfo, ImageFormat>);
121 
122  Type getElementType() const;
123  Dim getDim() const;
124  ImageDepthInfo getDepthInfo() const;
125  ImageArrayedInfo getArrayedInfo() const;
126  ImageSamplingInfo getSamplingInfo() const;
127  ImageSamplerUseInfo getSamplerUseInfo() const;
128  ImageFormat getImageFormat() const;
129  // TODO(ravishankarm): Add support for Access qualifier
130 };
131 
132 // SPIR-V pointer type
134  : public Type::TypeBase<PointerType, Type, detail::PointerTypeStorage> {
135 public:
136  using Base::Base;
137 
138  static bool kindof(unsigned kind) { return kind == TypeKind::Pointer; }
139 
140  static PointerType get(Type pointeeType, StorageClass storageClass);
141 
142  Type getPointeeType() const;
143 
144  StorageClass getStorageClass() const;
145 };
146 
147 // SPIR-V run-time array type
149  : public Type::TypeBase<RuntimeArrayType, Type,
150  detail::RuntimeArrayTypeStorage> {
151 public:
152  using Base::Base;
153 
154  static bool kindof(unsigned kind) { return kind == TypeKind::RuntimeArray; }
155 
156  static RuntimeArrayType get(Type elementType);
157 
158  Type getElementType() const;
159 };
160 
161 // SPIR-V struct type
162 class StructType : public Type::TypeBase<StructType, CompositeType,
163  detail::StructTypeStorage> {
164 public:
165  using Base::Base;
166 
167  // Layout information used for members in a struct in SPIR-V
168  //
169  // TODO(ravishankarm) : For now this only supports the offset type, so uses
170  // uint64_t value to represent the offset, with
171  // std::numeric_limit<uint64_t>::max indicating no offset. Change this to
172  // something that can hold all the information needed for different member
173  // types
174  using LayoutInfo = uint64_t;
175 
176  using MemberDecorationInfo = std::pair<uint32_t, spirv::Decoration>;
177 
178  static bool kindof(unsigned kind) { return kind == TypeKind::Struct; }
179 
181  static StructType get(ArrayRef<Type> memberTypes,
182  ArrayRef<LayoutInfo> layoutInfo = {},
183  ArrayRef<MemberDecorationInfo> memberDecorations = {});
184 
186  static StructType getEmpty(MLIRContext *context);
187 
188  unsigned getNumElements() const;
189 
190  Type getElementType(unsigned) const;
191 
192  bool hasLayout() const;
193 
194  uint64_t getOffset(unsigned) const;
195 
196  // Returns in `allMemberDecorations` the spirv::Decorations (apart from
197  // Offset) associated with all members of the StructType.
198  void getMemberDecorations(SmallVectorImpl<StructType::MemberDecorationInfo>
199  &allMemberDecorations) const;
200 
201  // Returns in `memberDecorations` all the spirv::Decorations (apart from
202  // Offset) associated with the `i`-th member of the StructType.
203  void getMemberDecorations(
204  unsigned i, SmallVectorImpl<spirv::Decoration> &memberDecorations) const;
205 };
206 
207 } // end namespace spirv
208 } // end namespace mlir
209 
210 #endif // MLIR_DIALECT_SPIRV_SPIRVTYPES_H_
Definition: InferTypeOpInterface.cpp:20
static bool kindof(unsigned kind)
Definition: SPIRVTypes.h:154
Definition: Attributes.h:139
Definition: SPIRVTypes.h:148
Definition: SPIRVTypes.h:61
static bool kindof(unsigned kind)
Definition: SPIRVTypes.h:138
uint64_t LayoutInfo
Definition: SPIRVTypes.h:78
Definition: LLVM.h:34
Definition: SPIRVTypes.h:133
Definition: SPIRVTypes.h:52
static bool kindof(unsigned kind)
Definition: SPIRVTypes.h:80
Definition: SPIRVTypes.h:97
Definition: LLVM.h:37
Definition: SPIRVTypes.h:55
Definition: SPIRVTypes.cpp:351
Type()
Definition: Types.h:111
Definition: SPIRVTypes.cpp:32
Definition: SPIRVTypes.cpp:310
Definition: SPIRVTypes.h:56
std::pair< uint32_t, spirv::Decoration > MemberDecorationInfo
Definition: SPIRVTypes.h:176
Definition: Types.h:84
uint64_t LayoutInfo
Definition: SPIRVTypes.h:174
Kind
Definition: SPIRVTypes.h:50
Definition: SPIRVTypes.h:54
static ImageType get(Type elementType, Dim dim, ImageDepthInfo depth=ImageDepthInfo::DepthUnknown, ImageArrayedInfo arrayed=ImageArrayedInfo::NonArrayed, ImageSamplingInfo samplingInfo=ImageSamplingInfo::SingleSampled, ImageSamplerUseInfo samplerUse=ImageSamplerUseInfo::SamplerUnknown, ImageFormat format=ImageFormat::Unknown)
Definition: SPIRVTypes.h:105
Definition: SPIRVTypes.cpp:378
Definition: StorageUniquerSupport.h:30
Definition: MLIRContext.h:34
Definition: SPIRVTypes.h:162
Definition: SPIRVTypes.h:53
static bool kindof(unsigned kind)
Definition: SPIRVTypes.h:102
ValueBuilder< DimOp > dim
Definition: Intrinsics.h:205
Definition: SPIRVTypes.h:73
Definition: SPIRVTypes.h:51
static bool kindof(unsigned kind)
Definition: SPIRVTypes.h:178