My Project
TypeDetail.h
Go to the documentation of this file.
1 //===- TypeDetail.h - MLIR Type storage details -----------------*- 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 holds implementation details of Type.
10 //
11 //===----------------------------------------------------------------------===//
12 #ifndef TYPEDETAIL_H_
13 #define TYPEDETAIL_H_
14 
15 #include "mlir/IR/AffineMap.h"
16 #include "mlir/IR/Identifier.h"
17 #include "mlir/IR/MLIRContext.h"
18 #include "mlir/IR/TypeSupport.h"
19 #include "mlir/IR/Types.h"
20 #include "llvm/Support/TrailingObjects.h"
21 
22 namespace mlir {
23 
24 class MLIRContext;
25 
26 namespace detail {
27 
29 struct OpaqueTypeStorage : public TypeStorage {
31  : dialectNamespace(dialectNamespace), typeData(typeData) {}
32 
34  using KeyTy = std::pair<Identifier, StringRef>;
35  bool operator==(const KeyTy &key) const {
36  return key == KeyTy(dialectNamespace, typeData);
37  }
38 
40  const KeyTy &key) {
41  StringRef tyData = allocator.copyInto(key.second);
42  return new (allocator.allocate<OpaqueTypeStorage>())
43  OpaqueTypeStorage(key.first, tyData);
44  }
45 
46  // The dialect namespace.
48 
49  // The parser type data for this opaque type.
50  StringRef typeData;
51 };
52 
55  IntegerTypeStorage(unsigned width) : width(width) {}
56 
58  using KeyTy = unsigned;
59  bool operator==(const KeyTy &key) const { return key == width; }
60 
62  KeyTy bitwidth) {
63  return new (allocator.allocate<IntegerTypeStorage>())
64  IntegerTypeStorage(bitwidth);
65  }
66 
67  unsigned width;
68 };
69 
72  FunctionTypeStorage(unsigned numInputs, unsigned numResults,
73  Type const *inputsAndResults)
74  : TypeStorage(numInputs), numResults(numResults),
75  inputsAndResults(inputsAndResults) {}
76 
78  using KeyTy = std::pair<ArrayRef<Type>, ArrayRef<Type>>;
79  bool operator==(const KeyTy &key) const {
80  return key == KeyTy(getInputs(), getResults());
81  }
82 
85  const KeyTy &key) {
86  ArrayRef<Type> inputs = key.first, results = key.second;
87 
88  // Copy the inputs and results into the bump pointer.
90  types.reserve(inputs.size() + results.size());
91  types.append(inputs.begin(), inputs.end());
92  types.append(results.begin(), results.end());
93  auto typesList = allocator.copyInto(ArrayRef<Type>(types));
94 
95  // Initialize the memory using placement new.
96  return new (allocator.allocate<FunctionTypeStorage>())
97  FunctionTypeStorage(inputs.size(), results.size(), typesList.data());
98  }
99 
100  ArrayRef<Type> getInputs() const {
101  return ArrayRef<Type>(inputsAndResults, getSubclassData());
102  }
103  ArrayRef<Type> getResults() const {
104  return ArrayRef<Type>(inputsAndResults + getSubclassData(), numResults);
105  }
106 
107  unsigned numResults;
109 };
110 
113  ShapedTypeStorage(Type elementTy, unsigned subclassData = 0)
114  : TypeStorage(subclassData), elementType(elementTy) {}
115 
117  using KeyTy = Type;
118  bool operator==(const KeyTy &key) const { return key == elementType; }
119 
121 };
122 
125  VectorTypeStorage(unsigned shapeSize, Type elementTy,
126  const int64_t *shapeElements)
127  : ShapedTypeStorage(elementTy, shapeSize), shapeElements(shapeElements) {}
128 
130  using KeyTy = std::pair<ArrayRef<int64_t>, Type>;
131  bool operator==(const KeyTy &key) const {
132  return key == KeyTy(getShape(), elementType);
133  }
134 
137  const KeyTy &key) {
138  // Copy the shape into the bump pointer.
139  ArrayRef<int64_t> shape = allocator.copyInto(key.first);
140 
141  // Initialize the memory using placement new.
142  return new (allocator.allocate<VectorTypeStorage>())
143  VectorTypeStorage(shape.size(), key.second, shape.data());
144  }
145 
147  return ArrayRef<int64_t>(shapeElements, getSubclassData());
148  }
149 
150  const int64_t *shapeElements;
151 };
152 
154  RankedTensorTypeStorage(unsigned shapeSize, Type elementTy,
155  const int64_t *shapeElements)
156  : ShapedTypeStorage(elementTy, shapeSize), shapeElements(shapeElements) {}
157 
159  using KeyTy = std::pair<ArrayRef<int64_t>, Type>;
160  bool operator==(const KeyTy &key) const {
161  return key == KeyTy(getShape(), elementType);
162  }
163 
166  const KeyTy &key) {
167  // Copy the shape into the bump pointer.
168  ArrayRef<int64_t> shape = allocator.copyInto(key.first);
169 
170  // Initialize the memory using placement new.
171  return new (allocator.allocate<RankedTensorTypeStorage>())
172  RankedTensorTypeStorage(shape.size(), key.second, shape.data());
173  }
174 
176  return ArrayRef<int64_t>(shapeElements, getSubclassData());
177  }
178 
179  const int64_t *shapeElements;
180 };
181 
185 
188  Type elementTy) {
189  return new (allocator.allocate<UnrankedTensorTypeStorage>())
190  UnrankedTensorTypeStorage(elementTy);
191  }
192 };
193 
195  MemRefTypeStorage(unsigned shapeSize, Type elementType,
196  const int64_t *shapeElements, const unsigned numAffineMaps,
197  AffineMap const *affineMapList, const unsigned memorySpace)
198  : ShapedTypeStorage(elementType, shapeSize), shapeElements(shapeElements),
199  numAffineMaps(numAffineMaps), affineMapList(affineMapList),
200  memorySpace(memorySpace) {}
201 
203  // MemRefs are uniqued based on their shape, element type, affine map
204  // composition, and memory space.
205  using KeyTy =
206  std::tuple<ArrayRef<int64_t>, Type, ArrayRef<AffineMap>, unsigned>;
207  bool operator==(const KeyTy &key) const {
208  return key == KeyTy(getShape(), elementType, getAffineMaps(), memorySpace);
209  }
210 
213  const KeyTy &key) {
214  // Copy the shape into the bump pointer.
215  ArrayRef<int64_t> shape = allocator.copyInto(std::get<0>(key));
216 
217  // Copy the affine map composition into the bump pointer.
218  ArrayRef<AffineMap> affineMapComposition =
219  allocator.copyInto(std::get<2>(key));
220 
221  // Initialize the memory using placement new.
222  return new (allocator.allocate<MemRefTypeStorage>())
223  MemRefTypeStorage(shape.size(), std::get<1>(key), shape.data(),
224  affineMapComposition.size(),
225  affineMapComposition.data(), std::get<3>(key));
226  }
227 
229  return ArrayRef<int64_t>(shapeElements, getSubclassData());
230  }
231 
232  ArrayRef<AffineMap> getAffineMaps() const {
233  return ArrayRef<AffineMap>(affineMapList, numAffineMaps);
234  }
235 
237  const int64_t *shapeElements;
239  const unsigned numAffineMaps;
243  const unsigned memorySpace;
244 };
245 
249 
250  UnrankedMemRefTypeStorage(Type elementTy, const unsigned memorySpace)
251  : ShapedTypeStorage(elementTy), memorySpace(memorySpace) {}
252 
254  using KeyTy = std::tuple<Type, unsigned>;
255  bool operator==(const KeyTy &key) const {
256  return key == KeyTy(elementType, memorySpace);
257  }
258 
261  const KeyTy &key) {
262 
263  // Initialize the memory using placement new.
264  return new (allocator.allocate<UnrankedMemRefTypeStorage>())
265  UnrankedMemRefTypeStorage(std::get<0>(key), std::get<1>(key));
266  }
268  const unsigned memorySpace;
269 };
270 
273  ComplexTypeStorage(Type elementType) : elementType(elementType) {}
274 
276  using KeyTy = Type;
277  bool operator==(const KeyTy &key) const { return key == elementType; }
278 
281  Type elementType) {
282  return new (allocator.allocate<ComplexTypeStorage>())
283  ComplexTypeStorage(elementType);
284  }
285 
287 };
288 
290 struct TupleTypeStorage final
291  : public TypeStorage,
292  public llvm::TrailingObjects<TupleTypeStorage, Type> {
294 
295  TupleTypeStorage(unsigned numTypes) : TypeStorage(numTypes) {}
296 
299  ArrayRef<Type> key) {
300  // Allocate a new storage instance.
301  auto byteSize = TupleTypeStorage::totalSizeToAlloc<Type>(key.size());
302  auto rawMem = allocator.allocate(byteSize, alignof(TupleTypeStorage));
303  auto result = ::new (rawMem) TupleTypeStorage(key.size());
304 
305  // Copy in the element types into the trailing storage.
306  std::uninitialized_copy(key.begin(), key.end(),
307  result->getTrailingObjects<Type>());
308  return result;
309  }
310 
311  bool operator==(const KeyTy &key) const { return key == getTypes(); }
312 
314  unsigned size() const { return getSubclassData(); }
315 
318  return {getTrailingObjects<Type>(), size()};
319  }
320 };
321 
322 } // namespace detail
323 } // namespace mlir
324 #endif // TYPEDETAIL_H_
static TupleTypeStorage * construct(TypeStorageAllocator &allocator, ArrayRef< Type > key)
Construction.
Definition: TypeDetail.h:298
Definition: InferTypeOpInterface.cpp:20
Opaque Type Storage and Uniquing.
Definition: TypeDetail.h:29
Type elementType
Definition: TypeDetail.h:286
AffineMap const * affineMapList
List of affine maps in the memref&#39;s layout/index map composition.
Definition: TypeDetail.h:241
Definition: Attributes.h:139
FunctionTypeStorage(unsigned numInputs, unsigned numResults, Type const *inputsAndResults)
Definition: TypeDetail.h:72
StringRef typeData
Definition: TypeDetail.h:50
Type const * inputsAndResults
Definition: TypeDetail.h:108
TupleTypeStorage(unsigned numTypes)
Definition: TypeDetail.h:295
MemRefTypeStorage(unsigned shapeSize, Type elementType, const int64_t *shapeElements, const unsigned numAffineMaps, AffineMap const *affineMapList, const unsigned memorySpace)
Definition: TypeDetail.h:195
Base storage class appearing in a Type.
Definition: TypeSupport.h:33
static FunctionTypeStorage * construct(TypeStorageAllocator &allocator, const KeyTy &key)
Construction.
Definition: TypeDetail.h:84
Definition: StorageUniquer.h:89
Shaped Type Storage.
Definition: TypeDetail.h:112
ComplexTypeStorage(Type elementType)
Definition: TypeDetail.h:273
Definition: Identifier.h:26
bool operator==(const KeyTy &key) const
Definition: TypeDetail.h:35
bool operator==(const KeyTy &key) const
Definition: TypeDetail.h:59
Type KeyTy
The hash key used for uniquing.
Definition: TypeDetail.h:117
unsigned getSubclassData() const
Get the subclass data.
Definition: TypeSupport.h:51
static MemRefTypeStorage * construct(TypeStorageAllocator &allocator, const KeyTy &key)
Construction.
Definition: TypeDetail.h:212
Definition: TypeDetail.h:153
static UnrankedTensorTypeStorage * construct(TypeStorageAllocator &allocator, Type elementTy)
Construction.
Definition: TypeDetail.h:187
RankedTensorTypeStorage(unsigned shapeSize, Type elementTy, const int64_t *shapeElements)
Definition: TypeDetail.h:154
bool operator==(const KeyTy &key) const
Definition: TypeDetail.h:207
T * allocate()
Allocate an instance of the provided type.
Definition: StorageUniquer.h:109
unsigned width
Definition: TypeDetail.h:67
OpaqueTypeStorage(Identifier dialectNamespace, StringRef typeData)
Definition: TypeDetail.h:30
std::pair< ArrayRef< Type >, ArrayRef< Type > > KeyTy
The hash key used for uniquing.
Definition: TypeDetail.h:78
Definition: LLVM.h:37
Identifier dialectNamespace
Definition: TypeDetail.h:47
const unsigned numAffineMaps
The number of affine maps in the &#39;affineMapList&#39; array.
Definition: TypeDetail.h:239
ArrayRef< Type > getTypes() const
Return the held types.
Definition: TypeDetail.h:317
A type representing a collection of other types.
Definition: TypeDetail.h:290
unsigned numResults
Definition: TypeDetail.h:107
ArrayRef< int64_t > getShape() const
Definition: TypeDetail.h:228
Type elementType
Definition: TypeDetail.h:120
static VectorTypeStorage * construct(TypeStorageAllocator &allocator, const KeyTy &key)
Construction.
Definition: TypeDetail.h:136
bool operator==(const KeyTy &key) const
Definition: TypeDetail.h:311
Definition: AffineMap.h:37
bool operator==(const KeyTy &key) const
Definition: TypeDetail.h:255
ArrayRef< int64_t > getShape() const
Definition: TypeDetail.h:146
ArrayRef< Type > getInputs() const
Definition: TypeDetail.h:100
bool operator==(const KeyTy &key) const
Definition: TypeDetail.h:131
Definition: Types.h:84
ShapedTypeStorage(Type elementTy, unsigned subclassData=0)
Definition: TypeDetail.h:113
ArrayRef< AffineMap > getAffineMaps() const
Definition: TypeDetail.h:232
Function Type Storage and Uniquing.
Definition: TypeDetail.h:71
Definition: LLVM.h:35
static ComplexTypeStorage * construct(TypeStorageAllocator &allocator, Type elementType)
Construction.
Definition: TypeDetail.h:280
Definition: TypeDetail.h:194
ArrayRef< Type > getResults() const
Definition: TypeDetail.h:103
const unsigned memorySpace
Memory space in which data referenced by memref resides.
Definition: TypeDetail.h:268
const int64_t * shapeElements
Definition: TypeDetail.h:179
unsigned KeyTy
The hash key used for uniquing.
Definition: TypeDetail.h:58
static OpaqueTypeStorage * construct(TypeStorageAllocator &allocator, const KeyTy &key)
Definition: TypeDetail.h:39
bool operator==(const KeyTy &key) const
Definition: TypeDetail.h:160
static RankedTensorTypeStorage * construct(TypeStorageAllocator &allocator, const KeyTy &key)
Construction.
Definition: TypeDetail.h:165
const int64_t * shapeElements
Definition: TypeDetail.h:150
bool operator==(const KeyTy &key) const
Definition: TypeDetail.h:277
static UnrankedMemRefTypeStorage * construct(TypeStorageAllocator &allocator, const KeyTy &key)
Construction.
Definition: TypeDetail.h:260
ArrayRef< T > copyInto(ArrayRef< T > elements)
Definition: StorageUniquer.h:93
ArrayRef< int64_t > getShape() const
Definition: TypeDetail.h:175
UnrankedMemRefTypeStorage(Type elementTy, const unsigned memorySpace)
Definition: TypeDetail.h:250
VectorTypeStorage(unsigned shapeSize, Type elementTy, const int64_t *shapeElements)
Definition: TypeDetail.h:125
Definition: TypeDetail.h:248
std::pair< Identifier, StringRef > KeyTy
The hash key used for uniquing.
Definition: TypeDetail.h:34
unsigned size() const
Return the number of held types.
Definition: TypeDetail.h:314
bool operator==(const KeyTy &key) const
Definition: TypeDetail.h:79
const unsigned memorySpace
Memory space in which data referenced by memref resides.
Definition: TypeDetail.h:243
bool operator==(const KeyTy &key) const
Definition: TypeDetail.h:118
static IntegerTypeStorage * construct(TypeStorageAllocator &allocator, KeyTy bitwidth)
Definition: TypeDetail.h:61
Definition: TypeDetail.h:182
IntegerTypeStorage(unsigned width)
Definition: TypeDetail.h:55
Vector Type Storage and Uniquing.
Definition: TypeDetail.h:124
Complex Type Storage.
Definition: TypeDetail.h:272
Integer Type Storage and Uniquing.
Definition: TypeDetail.h:54
const int64_t * shapeElements
An array of integers which stores the shape dimension sizes.
Definition: TypeDetail.h:237