MLIR 22.0.0git
BuiltinTypes.h
Go to the documentation of this file.
1//===- BuiltinTypes.h - MLIR Builtin Type Classes ---------------*- 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#ifndef MLIR_IR_BUILTINTYPES_H
10#define MLIR_IR_BUILTINTYPES_H
11
15
16namespace llvm {
17class BitVector;
18struct fltSemantics;
19} // namespace llvm
20
21//===----------------------------------------------------------------------===//
22// Tablegen Interface Declarations
23//===----------------------------------------------------------------------===//
24
25namespace mlir {
26class AffineExpr;
27class AffineMap;
28class IndexType;
29class IntegerType;
30class MemRefType;
31class RankedTensorType;
32class StringAttr;
33class TypeRange;
34
35namespace detail {
38struct TupleTypeStorage;
39} // namespace detail
40
41/// Type trait indicating that the type has value semantics.
42template <typename ConcreteType>
44 : public TypeTrait::TraitBase<ConcreteType, ValueSemantics> {};
45
46//===----------------------------------------------------------------------===//
47// TensorType
48//===----------------------------------------------------------------------===//
49
50/// Tensor types represent multi-dimensional arrays, and have two variants:
51/// RankedTensorType and UnrankedTensorType.
52/// Note: This class attaches the ShapedType trait to act as a mixin to
53/// provide many useful utility functions. This inheritance has no effect
54/// on derived tensor types.
55class TensorType : public Type, public ShapedType::Trait<TensorType> {
56public:
57 using Type::Type;
58
59 /// Returns the element type of this tensor type.
60 Type getElementType() const;
61
62 /// Returns if this type is ranked, i.e. it has a known number of dimensions.
63 bool hasRank() const;
64
65 /// Returns the shape of this tensor type.
67
68 /// Clone this type with the given shape and element type. If the
69 /// provided shape is `std::nullopt`, the current shape of the type is used.
71 Type elementType) const;
72
73 // Make sure that base class overloads are visible.
74 using ShapedType::Trait<TensorType>::clone;
75
76 /// Return a clone of this type with the given new shape and element type.
77 /// The returned type is ranked, even if this type is unranked.
78 RankedTensorType clone(ArrayRef<int64_t> shape, Type elementType) const;
79
80 /// Return a clone of this type with the given new shape. The returned type
81 /// is ranked, even if this type is unranked.
82 RankedTensorType clone(ArrayRef<int64_t> shape) const;
83
84 /// Return true if the specified element type is ok in a tensor.
85 static bool isValidElementType(Type type);
86
87 /// Methods for support type inquiry through isa, cast, and dyn_cast.
88 static bool classof(Type type);
89
90 /// Allow implicit conversion to ShapedType.
91 operator ShapedType() const { return llvm::cast<ShapedType>(*this); }
92};
93
94//===----------------------------------------------------------------------===//
95// BaseMemRefType
96//===----------------------------------------------------------------------===//
97
98/// This class provides a shared interface for ranked and unranked memref types.
99/// Note: This class attaches the ShapedType trait to act as a mixin to
100/// provide many useful utility functions. This inheritance has no effect
101/// on derived memref types.
102class BaseMemRefType : public Type,
103 public PtrLikeTypeInterface::Trait<BaseMemRefType>,
104 public ShapedType::Trait<BaseMemRefType> {
105public:
106 using Type::Type;
107
108 /// Returns the element type of this memref type.
109 Type getElementType() const;
110
111 /// Returns if this type is ranked, i.e. it has a known number of dimensions.
112 bool hasRank() const;
113
114 /// Returns the shape of this memref type.
116
117 /// Clone this type with the given shape and element type. If the
118 /// provided shape is `std::nullopt`, the current shape of the type is used.
120 Type elementType) const;
121
122 /// Clone this type with the given memory space and element type. If the
123 /// provided element type is `std::nullopt`, the current element type of the
124 /// type is used.
125 FailureOr<PtrLikeTypeInterface>
126 clonePtrWith(Attribute memorySpace, std::optional<Type> elementType) const;
127
128 // Make sure that base class overloads are visible.
129 using ShapedType::Trait<BaseMemRefType>::clone;
130
131 /// Return a clone of this type with the given new shape and element type.
132 /// The returned type is ranked, even if this type is unranked.
133 MemRefType clone(ArrayRef<int64_t> shape, Type elementType) const;
134
135 /// Return a clone of this type with the given new shape. The returned type
136 /// is ranked, even if this type is unranked.
137 MemRefType clone(ArrayRef<int64_t> shape) const;
138
139 /// Return true if the specified element type is ok in a memref.
140 static bool isValidElementType(Type type);
141
142 /// Methods for support type inquiry through isa, cast, and dyn_cast.
143 static bool classof(Type type);
144
145 /// Returns the memory space in which data referred to by this memref resides.
147
148 /// [deprecated] Returns the memory space in old raw integer representation.
149 /// New `Attribute getMemorySpace()` method should be used instead.
150 unsigned getMemorySpaceAsInt() const;
151
152 /// Returns that this ptr-like object has non-empty ptr metadata.
153 bool hasPtrMetadata() const { return true; }
154
155 /// Allow implicit conversion to ShapedType.
156 operator ShapedType() const { return llvm::cast<ShapedType>(*this); }
157
158 /// Allow implicit conversion to PtrLikeTypeInterface.
159 operator PtrLikeTypeInterface() const {
160 return llvm::cast<PtrLikeTypeInterface>(*this);
161 }
162};
163
164} // namespace mlir
165
166//===----------------------------------------------------------------------===//
167// Tablegen Type Declarations
168//===----------------------------------------------------------------------===//
169
170#define GET_TYPEDEF_CLASSES
171#include "mlir/IR/BuiltinTypes.h.inc"
172
173namespace mlir {
174#include "mlir/IR/BuiltinTypeConstraints.h.inc"
175
176//===----------------------------------------------------------------------===//
177// MemRefType
178//===----------------------------------------------------------------------===//
179
180/// This is a builder type that keeps local references to arguments. Arguments
181/// that are passed into the builder must outlive the builder.
183public:
184 // Build from another MemRefType.
185 explicit Builder(MemRefType other)
186 : shape(other.getShape()), elementType(other.getElementType()),
187 layout(other.getLayout()), memorySpace(other.getMemorySpace()) {}
188
189 // Build from scratch.
190 Builder(ArrayRef<int64_t> shape, Type elementType)
191 : shape(shape), elementType(elementType) {}
192
194 shape = newShape;
195 return *this;
196 }
197
198 Builder &setElementType(Type newElementType) {
199 elementType = newElementType;
200 return *this;
201 }
202
203 Builder &setLayout(MemRefLayoutAttrInterface newLayout) {
204 layout = newLayout;
205 return *this;
206 }
207
209 memorySpace = newMemorySpace;
210 return *this;
211 }
212
213 operator MemRefType() {
214 return MemRefType::get(shape, elementType, layout, memorySpace);
215 }
216
217private:
219 Type elementType;
220 MemRefLayoutAttrInterface layout;
221 Attribute memorySpace;
222};
223
224//===----------------------------------------------------------------------===//
225// RankedTensorType
226//===----------------------------------------------------------------------===//
227
228/// This is a builder type that keeps local references to arguments. Arguments
229/// that are passed into the builder must outlive the builder.
231public:
232 /// Build from another RankedTensorType.
233 explicit Builder(RankedTensorType other)
234 : shape(other.getShape()), elementType(other.getElementType()),
235 encoding(other.getEncoding()) {}
236
237 /// Build from scratch.
238 Builder(ArrayRef<int64_t> shape, Type elementType, Attribute encoding)
239 : shape(shape), elementType(elementType), encoding(encoding) {}
240
242 shape = newShape;
243 return *this;
244 }
245
246 Builder &setElementType(Type newElementType) {
247 elementType = newElementType;
248 return *this;
249 }
250
252 encoding = newEncoding;
253 return *this;
254 }
255
256 /// Erase a dim from shape @pos.
257 Builder &dropDim(unsigned pos) {
258 assert(pos < shape.size() && "overflow");
259 shape.erase(pos);
260 return *this;
261 }
262
263 /// Insert a val into shape @pos.
264 Builder &insertDim(int64_t val, unsigned pos) {
265 assert(pos <= shape.size() && "overflow");
266 shape.insert(pos, val);
267 return *this;
268 }
269
270 operator RankedTensorType() {
271 return RankedTensorType::get(shape, elementType, encoding);
272 }
273
274private:
276 Type elementType;
277 Attribute encoding;
278};
279
280//===----------------------------------------------------------------------===//
281// VectorType
282//===----------------------------------------------------------------------===//
283
284/// This is a builder type that keeps local references to arguments. Arguments
285/// that are passed into the builder must outlive the builder.
287public:
288 /// Build from another VectorType.
289 explicit Builder(VectorType other)
290 : elementType(other.getElementType()), shape(other.getShape()),
291 scalableDims(other.getScalableDims()) {}
292
293 /// Build from scratch.
294 Builder(ArrayRef<int64_t> shape, Type elementType,
295 ArrayRef<bool> scalableDims = {})
296 : elementType(elementType), shape(shape), scalableDims(scalableDims) {}
297
299 ArrayRef<bool> newIsScalableDim = {}) {
300 shape = newShape;
301 scalableDims = newIsScalableDim;
302 return *this;
303 }
304
305 Builder &setElementType(Type newElementType) {
306 elementType = newElementType;
307 return *this;
308 }
309
310 /// Erase a dim from shape @pos.
311 Builder &dropDim(unsigned pos) {
312 assert(pos < shape.size() && "overflow");
313 shape.erase(pos);
314 if (!scalableDims.empty())
315 scalableDims.erase(pos);
316 return *this;
317 }
318
319 /// Set a dim in shape @pos to val.
320 Builder &setDim(unsigned pos, int64_t val) {
321 assert(pos < shape.size() && "overflow");
322 shape.set(pos, val);
323 return *this;
324 }
325
326 operator VectorType() {
327 return VectorType::get(shape, elementType, scalableDims);
328 }
329
330private:
331 Type elementType;
333 CopyOnWriteArrayRef<bool> scalableDims;
334};
335
336/// Given an `originalShape` and a `reducedShape` assumed to be a subset of
337/// `originalShape` with some `1` entries erased, return the set of indices
338/// that specifies which of the entries of `originalShape` are dropped to obtain
339/// `reducedShape`. The returned mask can be applied as a projection to
340/// `originalShape` to obtain the `reducedShape`. This mask is useful to track
341/// which dimensions must be kept when e.g. compute MemRef strides under
342/// rank-reducing operations. Return std::nullopt if reducedShape cannot be
343/// obtained by dropping only `1` entries in `originalShape`.
344/// If `matchDynamic` is true, then dynamic dims in `originalShape` and
345/// `reducedShape` will be considered matching with non-dynamic dims, unless
346/// the non-dynamic dim is from `originalShape` and equal to 1. For example,
347/// in ([1, 3, ?], [?, 5]), the mask would be {1, 0, 0}, since 3 and 5 will
348/// match with the corresponding dynamic dims.
349std::optional<llvm::SmallDenseSet<unsigned>>
351 ArrayRef<int64_t> reducedShape,
352 bool matchDynamic = false);
353
354/// Enum that captures information related to verifier error conditions on
355/// slice insert/extract type of ops.
361 // Error codes to ops with a memory space and a layout annotation.
364};
365
366/// Check if `originalType` can be rank reduced to `candidateReducedType` type
367/// by dropping some dimensions with static size `1`.
368/// Return `SliceVerificationResult::Success` on success or an appropriate error
369/// code.
370SliceVerificationResult isRankReducedType(ShapedType originalType,
371 ShapedType candidateReducedType);
372
373//===----------------------------------------------------------------------===//
374// Convenience wrappers for VectorType
375//
376// These are provided to allow idiomatic code like:
377// * isa<vector::ScalableVectorType>(type)
378//===----------------------------------------------------------------------===//
379/// A vector type containing at least one scalable dimension.
380class ScalableVectorType : public VectorType {
381public:
382 using VectorType::VectorType;
383
384 static bool classof(Type type) {
385 auto vecTy = llvm::dyn_cast<VectorType>(type);
386 if (!vecTy)
387 return false;
388 return vecTy.isScalable();
389 }
390};
391
392/// A vector type with no scalable dimensions.
393class FixedVectorType : public VectorType {
394public:
395 using VectorType::VectorType;
396
397 static bool classof(Type type) {
398 auto vecTy = llvm::dyn_cast<VectorType>(type);
399 if (!vecTy)
400 return false;
401 return !vecTy.isScalable();
402 }
403};
404
405//===----------------------------------------------------------------------===//
406// Deferred Method Definitions
407//===----------------------------------------------------------------------===//
408
409inline bool BaseMemRefType::classof(Type type) {
410 return llvm::isa<MemRefType, UnrankedMemRefType>(type);
411}
412
414 return type.isIntOrIndexOrFloat() ||
415 llvm::isa<ComplexType, MemRefType, VectorType, UnrankedMemRefType>(
416 type) ||
417 llvm::isa<MemRefElementTypeInterface>(type);
418}
419
420inline bool TensorType::classof(Type type) {
421 return llvm::isa<RankedTensorType, UnrankedTensorType>(type);
422}
423
424//===----------------------------------------------------------------------===//
425// Type Utilities
426//===----------------------------------------------------------------------===//
427
428/// Given MemRef `sizes` that are either static or dynamic, returns the
429/// canonical "contiguous" strides AffineExpr. Strides are multiplicative and
430/// once a dynamic dimension is encountered, all canonical strides become
431/// dynamic and need to be encoded with a different symbol.
432/// For canonical strides expressions, the offset is always 0 and the fastest
433/// varying stride is always `1`.
434///
435/// Examples:
436/// - memref<3x4x5xf32> has canonical stride expression
437/// `20*exprs[0] + 5*exprs[1] + exprs[2]`.
438/// - memref<3x?x5xf32> has canonical stride expression
439/// `s0*exprs[0] + 5*exprs[1] + exprs[2]`.
440/// - memref<3x4x?xf32> has canonical stride expression
441/// `s1*exprs[0] + s0*exprs[1] + exprs[2]`.
444 MLIRContext *context);
445
446/// Return the result of makeCanonicalStrudedLayoutExpr for the common case
447/// where `exprs` is {d0, d1, .., d_(sizes.size()-1)}
449 MLIRContext *context);
450} // namespace mlir
451
452#endif // MLIR_IR_BUILTINTYPES_H
static Type getElementType(Type type)
Determine the element type of type.
static ArrayRef< int64_t > getShape(Type type)
Returns the shape of the given type.
Definition Traits.cpp:117
Base type for affine expression.
Definition AffineExpr.h:68
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
Definition AffineMap.h:46
Attributes are known-constant values of operations.
Definition Attributes.h:25
This class provides a shared interface for ranked and unranked memref types.
MemRefType clone(ArrayRef< int64_t > shape) const
Return a clone of this type with the given new shape.
ArrayRef< int64_t > getShape() const
Returns the shape of this memref type.
static bool isValidElementType(Type type)
Return true if the specified element type is ok in a memref.
FailureOr< PtrLikeTypeInterface > clonePtrWith(Attribute memorySpace, std::optional< Type > elementType) const
Clone this type with the given memory space and element type.
bool hasPtrMetadata() const
Returns that this ptr-like object has non-empty ptr metadata.
constexpr Type()=default
Attribute getMemorySpace() const
Returns the memory space in which data referred to by this memref resides.
static bool classof(Type type)
Methods for support type inquiry through isa, cast, and dyn_cast.
unsigned getMemorySpaceAsInt() const
[deprecated] Returns the memory space in old raw integer representation.
BaseMemRefType cloneWith(std::optional< ArrayRef< int64_t > > shape, Type elementType) const
Clone this type with the given shape and element type.
bool hasRank() const
Returns if this type is ranked, i.e. it has a known number of dimensions.
Type getElementType() const
Returns the element type of this memref type.
MemRefType clone(ArrayRef< int64_t > shape, Type elementType) const
Return a clone of this type with the given new shape and element type.
A vector type with no scalable dimensions.
static bool classof(Type type)
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
This is a builder type that keeps local references to arguments.
Builder(ArrayRef< int64_t > shape, Type elementType)
Builder & setShape(ArrayRef< int64_t > newShape)
Builder(MemRefType other)
Builder & setMemorySpace(Attribute newMemorySpace)
Builder & setElementType(Type newElementType)
Builder & setLayout(MemRefLayoutAttrInterface newLayout)
This is a builder type that keeps local references to arguments.
Builder & setEncoding(Attribute newEncoding)
Builder(ArrayRef< int64_t > shape, Type elementType, Attribute encoding)
Build from scratch.
Builder & setElementType(Type newElementType)
Builder & setShape(ArrayRef< int64_t > newShape)
Builder(RankedTensorType other)
Build from another RankedTensorType.
Builder & insertDim(int64_t val, unsigned pos)
Insert a val into shape @pos.
Builder & dropDim(unsigned pos)
Erase a dim from shape @pos.
A vector type containing at least one scalable dimension.
static bool classof(Type type)
Tensor types represent multi-dimensional arrays, and have two variants: RankedTensorType and Unranked...
TensorType cloneWith(std::optional< ArrayRef< int64_t > > shape, Type elementType) const
Clone this type with the given shape and element type.
RankedTensorType clone(ArrayRef< int64_t > shape) const
Return a clone of this type with the given new shape.
static bool classof(Type type)
Methods for support type inquiry through isa, cast, and dyn_cast.
constexpr Type()=default
static bool isValidElementType(Type type)
Return true if the specified element type is ok in a tensor.
ArrayRef< int64_t > getShape() const
Returns the shape of this tensor type.
bool hasRank() const
Returns if this type is ranked, i.e. it has a known number of dimensions.
RankedTensorType clone(ArrayRef< int64_t > shape, Type elementType) const
Return a clone of this type with the given new shape and element type.
Type getElementType() const
Returns the element type of this tensor type.
This class provides an abstraction over the various different ranges of value types.
Definition TypeRange.h:37
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
constexpr Type()=default
bool isIntOrIndexOrFloat() const
Return true if this is an integer (of any signedness), index, or float type.
Definition Types.cpp:120
Type trait indicating that the type has value semantics.
This is a builder type that keeps local references to arguments.
Builder & setElementType(Type newElementType)
Builder & setDim(unsigned pos, int64_t val)
Set a dim in shape @pos to val.
Builder(VectorType other)
Build from another VectorType.
Builder & setShape(ArrayRef< int64_t > newShape, ArrayRef< bool > newIsScalableDim={})
Builder & dropDim(unsigned pos)
Erase a dim from shape @pos.
Builder(ArrayRef< int64_t > shape, Type elementType, ArrayRef< bool > scalableDims={})
Build from scratch.
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229
detail::StorageUserTraitBase< ConcreteType, TraitType > TraitBase
This class represents the base of a type trait.
Definition Types.h:249
AttrTypeReplacer.
Include the generated interface declarations.
SliceVerificationResult
Enum that captures information related to verifier error conditions on slice insert/extract type of o...
AffineExpr makeCanonicalStridedLayoutExpr(ArrayRef< int64_t > sizes, ArrayRef< AffineExpr > exprs, MLIRContext *context)
Given MemRef sizes that are either static or dynamic, returns the canonical "contiguous" strides Affi...
std::optional< llvm::SmallDenseSet< unsigned > > computeRankReductionMask(ArrayRef< int64_t > originalShape, ArrayRef< int64_t > reducedShape, bool matchDynamic=false)
Given an originalShape and a reducedShape assumed to be a subset of originalShape with some 1 entries...
SliceVerificationResult isRankReducedType(ShapedType originalType, ShapedType candidateReducedType)
Check if originalType can be rank reduced to candidateReducedType type by dropping some dimensions wi...
Function Type Storage and Uniquing.
Definition TypeDetail.h:57
Integer Type Storage and Uniquing.
Definition TypeDetail.h:28
A type representing a collection of other types.
Definition TypeDetail.h:105