MLIR  16.0.0git
Types.h
Go to the documentation of this file.
1 //===- Types.h - MLIR 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_TYPES_H
10 #define MLIR_IR_TYPES_H
11 
12 #include "mlir/IR/TypeSupport.h"
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/DenseMapInfo.h"
15 #include "llvm/Support/PointerLikeTypeTraits.h"
16 
17 namespace mlir {
18 class AsmState;
19 
20 /// Instances of the Type class are uniqued, have an immutable identifier and an
21 /// optional mutable component. They wrap a pointer to the storage object owned
22 /// by MLIRContext. Therefore, instances of Type are passed around by value.
23 ///
24 /// Some types are "primitives" meaning they do not have any parameters, for
25 /// example the Index type. Parametric types have additional information that
26 /// differentiates the types of the same class, for example the Integer type has
27 /// bitwidth, making i8 and i16 belong to the same kind by be different
28 /// instances of the IntegerType. Type parameters are part of the unique
29 /// immutable key. The mutable component of the type can be modified after the
30 /// type is created, but cannot affect the identity of the type.
31 ///
32 /// Types are constructed and uniqued via the 'detail::TypeUniquer' class.
33 ///
34 /// Derived type classes are expected to implement several required
35 /// implementation hooks:
36 /// * Optional:
37 /// - static LogicalResult verify(
38 /// function_ref<InFlightDiagnostic()> emitError,
39 /// Args... args)
40 /// * This method is invoked when calling the 'TypeBase::get/getChecked'
41 /// methods to ensure that the arguments passed in are valid to construct
42 /// a type instance with.
43 /// * This method is expected to return failure if a type cannot be
44 /// constructed with 'args', success otherwise.
45 /// * 'args' must correspond with the arguments passed into the
46 /// 'TypeBase::get' call.
47 ///
48 ///
49 /// Type storage objects inherit from TypeStorage and contain the following:
50 /// - The dialect that defined the type.
51 /// - Any parameters of the type.
52 /// - An optional mutable component.
53 /// For non-parametric types, a convenience DefaultTypeStorage is provided.
54 /// Parametric storage types must derive TypeStorage and respect the following:
55 /// - Define a type alias, KeyTy, to a type that uniquely identifies the
56 /// instance of the type.
57 /// * The key type must be constructible from the values passed into the
58 /// detail::TypeUniquer::get call.
59 /// * If the KeyTy does not have an llvm::DenseMapInfo specialization, the
60 /// storage class must define a hashing method:
61 /// 'static unsigned hashKey(const KeyTy &)'
62 ///
63 /// - Provide a method, 'bool operator==(const KeyTy &) const', to
64 /// compare the storage instance against an instance of the key type.
65 ///
66 /// - Provide a static construction method:
67 /// 'DerivedStorage *construct(TypeStorageAllocator &, const KeyTy &key)'
68 /// that builds a unique instance of the derived storage. The arguments to
69 /// this function are an allocator to store any uniqued data within the
70 /// context and the key type for this storage.
71 ///
72 /// - If they have a mutable component, this component must not be a part of
73 /// the key.
74 class Type {
75 public:
76  /// Utility class for implementing types.
77  template <typename ConcreteType, typename BaseType, typename StorageType,
78  template <typename T> class... Traits>
79  using TypeBase = detail::StorageUserBase<ConcreteType, BaseType, StorageType,
80  detail::TypeUniquer, Traits...>;
81 
83 
85 
86  constexpr Type() {}
87  /* implicit */ Type(const ImplType *impl)
88  : impl(const_cast<ImplType *>(impl)) {}
89 
90  Type(const Type &other) = default;
91  Type &operator=(const Type &other) = default;
92 
93  bool operator==(Type other) const { return impl == other.impl; }
94  bool operator!=(Type other) const { return !(*this == other); }
95  explicit operator bool() const { return impl; }
96 
97  bool operator!() const { return impl == nullptr; }
98 
99  template <typename... Tys>
100  bool isa() const;
101  template <typename... Tys>
102  bool isa_and_nonnull() const;
103  template <typename U>
104  U dyn_cast() const;
105  template <typename U>
106  U dyn_cast_or_null() const;
107  template <typename U>
108  U cast() const;
109 
110  // Support type casting Type to itself.
111  static bool classof(Type) { return true; }
112 
113  /// Return a unique identifier for the concrete type. This is used to support
114  /// dynamic type casting.
115  TypeID getTypeID() { return impl->getAbstractType().getTypeID(); }
116 
117  /// Return the MLIRContext in which this type was uniqued.
118  MLIRContext *getContext() const;
119 
120  /// Get the dialect this type is registered to.
121  Dialect &getDialect() const { return impl->getAbstractType().getDialect(); }
122 
123  // Convenience predicates. This is only for floating point types,
124  // derived types should use isa/dyn_cast.
125  bool isIndex() const;
126  bool isFloat8E5M2() const;
127  bool isBF16() const;
128  bool isF16() const;
129  bool isF32() const;
130  bool isF64() const;
131  bool isF80() const;
132  bool isF128() const;
133 
134  /// Return true if this is an integer type with the specified width.
135  bool isInteger(unsigned width) const;
136  /// Return true if this is a signless integer type (with the specified width).
137  bool isSignlessInteger() const;
138  bool isSignlessInteger(unsigned width) const;
139  /// Return true if this is a signed integer type (with the specified width).
140  bool isSignedInteger() const;
141  bool isSignedInteger(unsigned width) const;
142  /// Return true if this is an unsigned integer type (with the specified
143  /// width).
144  bool isUnsignedInteger() const;
145  bool isUnsignedInteger(unsigned width) const;
146 
147  /// Return the bit width of an integer or a float type, assert failure on
148  /// other types.
149  unsigned getIntOrFloatBitWidth() const;
150 
151  /// Return true if this is a signless integer or index type.
152  bool isSignlessIntOrIndex() const;
153  /// Return true if this is a signless integer, index, or float type.
154  bool isSignlessIntOrIndexOrFloat() const;
155  /// Return true of this is a signless integer or a float type.
156  bool isSignlessIntOrFloat() const;
157 
158  /// Return true if this is an integer (of any signedness) or an index type.
159  bool isIntOrIndex() const;
160  /// Return true if this is an integer (of any signedness) or a float type.
161  bool isIntOrFloat() const;
162  /// Return true if this is an integer (of any signedness), index, or float
163  /// type.
164  bool isIntOrIndexOrFloat() const;
165 
166  /// Print the current type.
167  void print(raw_ostream &os) const;
168  void print(raw_ostream &os, AsmState &state) const;
169  void dump() const;
170 
171  friend ::llvm::hash_code hash_value(Type arg);
172 
173  /// Methods for supporting PointerLikeTypeTraits.
174  const void *getAsOpaquePointer() const {
175  return static_cast<const void *>(impl);
176  }
177  static Type getFromOpaquePointer(const void *pointer) {
178  return Type(reinterpret_cast<ImplType *>(const_cast<void *>(pointer)));
179  }
180 
181  /// Returns true if the type was registered with a particular trait.
182  template <template <typename T> class Trait>
183  bool hasTrait() {
184  return getAbstractType().hasTrait<Trait>();
185  }
186 
187  /// Return the abstract type descriptor for this type.
188  const AbstractTy &getAbstractType() { return impl->getAbstractType(); }
189 
190  /// Return the Type implementation.
191  ImplType *getImpl() const { return impl; }
192 
193 protected:
194  ImplType *impl{nullptr};
195 };
196 
197 inline raw_ostream &operator<<(raw_ostream &os, Type type) {
198  type.print(os);
199  return os;
200 }
201 
202 //===----------------------------------------------------------------------===//
203 // TypeTraitBase
204 //===----------------------------------------------------------------------===//
205 
206 namespace TypeTrait {
207 /// This class represents the base of a type trait.
208 template <typename ConcreteType, template <typename> class TraitType>
210 } // namespace TypeTrait
211 
212 //===----------------------------------------------------------------------===//
213 // TypeInterface
214 //===----------------------------------------------------------------------===//
215 
216 /// This class represents the base of a type interface. See the definition of
217 /// `detail::Interface` for requirements on the `Traits` type.
218 template <typename ConcreteType, typename Traits>
219 class TypeInterface : public detail::Interface<ConcreteType, Type, Traits, Type,
220  TypeTrait::TraitBase> {
221 public:
223  using InterfaceBase =
226 
227 private:
228  /// Returns the impl interface instance for the given type.
229  static typename InterfaceBase::Concept *getInterfaceFor(Type type) {
230  return type.getAbstractType().getInterface<ConcreteType>();
231  }
232 
233  /// Allow access to 'getInterfaceFor'.
234  friend InterfaceBase;
235 };
236 
237 //===----------------------------------------------------------------------===//
238 // Core TypeTrait
239 //===----------------------------------------------------------------------===//
240 
241 /// This trait is used to determine if a type is mutable or not. It is attached
242 /// on a type if the corresponding ImplType defines a `mutate` function with
243 /// a proper signature.
244 namespace TypeTrait {
245 template <typename ConcreteType>
247 } // namespace TypeTrait
248 
249 //===----------------------------------------------------------------------===//
250 // Type Utils
251 //===----------------------------------------------------------------------===//
252 
253 // Make Type hashable.
254 inline ::llvm::hash_code hash_value(Type arg) {
256 }
257 
258 template <typename... Tys>
259 bool Type::isa() const {
260  return llvm::isa<Tys...>(*this);
261 }
262 
263 template <typename... Tys>
264 bool Type::isa_and_nonnull() const {
265  return llvm::isa_and_present<Tys...>(*this);
266 }
267 
268 template <typename U>
269 U Type::dyn_cast() const {
270  return llvm::dyn_cast<U>(*this);
271 }
272 
273 template <typename U>
275  return llvm::dyn_cast_or_null<U>(*this);
276 }
277 
278 template <typename U>
279 U Type::cast() const {
280  return llvm::cast<U>(*this);
281 }
282 
283 } // namespace mlir
284 
285 namespace llvm {
286 
287 // Type hash just like pointers.
288 template <>
291  auto *pointer = llvm::DenseMapInfo<void *>::getEmptyKey();
292  return mlir::Type(static_cast<mlir::Type::ImplType *>(pointer));
293  }
296  return mlir::Type(static_cast<mlir::Type::ImplType *>(pointer));
297  }
298  static unsigned getHashValue(mlir::Type val) { return mlir::hash_value(val); }
299  static bool isEqual(mlir::Type LHS, mlir::Type RHS) { return LHS == RHS; }
300 };
301 template <typename T>
302 struct DenseMapInfo<T, std::enable_if_t<std::is_base_of<mlir::Type, T>::value &&
303  !mlir::detail::IsInterface<T>::value>>
304  : public DenseMapInfo<mlir::Type> {
305  static T getEmptyKey() {
306  const void *pointer = llvm::DenseMapInfo<const void *>::getEmptyKey();
307  return T::getFromOpaquePointer(pointer);
308  }
309  static T getTombstoneKey() {
311  return T::getFromOpaquePointer(pointer);
312  }
313 };
314 
315 /// We align TypeStorage by 8, so allow LLVM to steal the low bits.
316 template <>
317 struct PointerLikeTypeTraits<mlir::Type> {
318 public:
319  static inline void *getAsVoidPointer(mlir::Type I) {
320  return const_cast<void *>(I.getAsOpaquePointer());
321  }
322  static inline mlir::Type getFromVoidPointer(void *P) {
324  }
325  static constexpr int NumLowBitsAvailable = 3;
326 };
327 
328 /// Add support for llvm style casts.
329 /// We provide a cast between To and From if From is mlir::Type or derives from
330 /// it
331 template <typename To, typename From>
332 struct CastInfo<
333  To, From,
334  std::enable_if_t<std::is_same_v<mlir::Type, std::remove_const_t<From>> ||
335  std::is_base_of_v<mlir::Type, From>>>
337  DefaultDoCastIfPossible<To, From, CastInfo<To, From>> {
338  /// Arguments are taken as mlir::Type here and not as `From`, because when
339  /// casting from an intermediate type of the hierarchy to one of its children,
340  /// the val.getTypeID() inside T::classof will use the static getTypeID of the
341  /// parent instead of the non-static Type::getTypeID that returns the dynamic
342  /// ID. This means that T::classof would end up comparing the static TypeID of
343  /// the children to the static TypeID of its parent, making it impossible to
344  /// downcast from the parent to the child.
345  static inline bool isPossible(mlir::Type ty) {
346  /// Return a constant true instead of a dynamic true when casting to self or
347  /// up the hierarchy.
348  return std::is_same_v<To, std::remove_const_t<From>> ||
349  std::is_base_of_v<To, From> || To::classof(ty);
350  }
351  static inline To doCast(mlir::Type ty) { return To(ty.getImpl()); }
352 };
353 
354 } // namespace llvm
355 
356 #endif // MLIR_IR_TYPES_H
bool operator!() const
Definition: Types.h:97
Include the generated interface declarations.
const void * getAsOpaquePointer() const
Methods for supporting PointerLikeTypeTraits.
Definition: Types.h:174
const AbstractTy & getAbstractType()
Return the abstract type descriptor for this type.
Definition: Types.h:188
bool isF32() const
Definition: Types.cpp:24
ImplType * impl
Definition: Types.h:194
Dialect & getDialect() const
Get the dialect this type is registered to.
Definition: Types.h:121
ImplType * getImpl() const
Return the Type implementation.
Definition: Types.h:191
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition: CallGraph.h:229
constexpr Type()
Definition: Types.h:86
static bool isEqual(mlir::Type LHS, mlir::Type RHS)
Definition: Types.h:299
bool isFloat8E5M2() const
Definition: Types.cpp:21
Type & operator=(const Type &other)=default
bool isIntOrIndexOrFloat() const
Return true if this is an integer (of any signedness), index, or float type.
Definition: Types.cpp:90
Base storage class appearing in a Type.
Definition: TypeSupport.h:122
bool isInteger(unsigned width) const
Return true if this is an integer type with the specified width.
Definition: Types.cpp:32
bool isIntOrFloat() const
Return true if this is an integer (of any signedness) or a float type.
Definition: Types.cpp:88
This class represents the base of a type interface.
Definition: Types.h:219
bool hasTrait() const
Returns true if the type has a particular trait.
Definition: TypeSupport.h:74
This class represents an abstract interface.
bool isUnsignedInteger() const
Return true if this is an unsigned integer type (with the specified width).
Definition: Types.cpp:62
raw_ostream & operator<<(raw_ostream &os, const AliasResult &result)
Definition: AliasAnalysis.h:78
This class contains all of the static information common to all instances of a registered Type...
Definition: TypeSupport.h:30
void print(raw_ostream &os) const
Print the current type.
static bool isPossible(mlir::Type ty)
Arguments are taken as mlir::Type here and not as From, because when casting from an intermediate typ...
Definition: Types.h:345
This class provides an efficient unique identifier for a specific C++ type.
Definition: TypeID.h:104
static bool classof(Type)
Definition: Types.h:111
static unsigned getHashValue(mlir::Type val)
Definition: Types.h:298
bool isSignlessIntOrIndexOrFloat() const
Return true if this is a signless integer, index, or float type.
Definition: Types.cpp:78
bool isF80() const
Definition: Types.cpp:26
U dyn_cast_or_null() const
Definition: Types.h:274
static mlir::Type getFromVoidPointer(void *P)
Definition: Types.h:322
A utility class to get, or create, unique instances of types within an MLIRContext.
Definition: TypeSupport.h:166
U dyn_cast() const
Definition: Types.h:269
bool isF16() const
Definition: Types.cpp:23
T::Concept * getInterface() const
Returns an instance of the concept object for the given interface if it was registered to this type...
Definition: TypeSupport.h:63
bool isSignlessIntOrIndex() const
Return true if this is a signless integer or index type.
Definition: Types.cpp:74
This trait is used to determine if a storage user, like Type, is mutable or not.
bool isIndex() const
Definition: Types.cpp:29
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition: Dialect.h:41
static Type getFromOpaquePointer(const void *pointer)
Definition: Types.h:177
bool isSignlessInteger() const
Return true if this is a signless integer type (with the specified width).
Definition: Types.cpp:38
bool isF128() const
Definition: Types.cpp:27
bool isSignedInteger() const
Return true if this is a signed integer type (with the specified width).
Definition: Types.cpp:50
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
bool operator==(Type other) const
Definition: Types.h:93
MLIRContext * getContext() const
Return the MLIRContext in which this type was uniqued.
Definition: Types.cpp:19
inline ::llvm::hash_code hash_value(AffineExpr arg)
Make AffineExpr hashable.
Definition: AffineExpr.h:240
bool isF64() const
Definition: Types.cpp:25
void dump() const
TypeID getTypeID()
Return a unique identifier for the concrete type.
Definition: Types.h:115
Type(const ImplType *impl)
Definition: Types.h:87
friend ::llvm::hash_code hash_value(Type arg)
Definition: Types.h:254
Utility class for implementing users of storage classes uniqued by a StorageUniquer.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:56
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
Definition: Types.cpp:92
bool isIntOrIndex() const
Return true if this is an integer (of any signedness) or an index type.
Definition: Types.cpp:86
static void * getAsVoidPointer(mlir::Type I)
Definition: Types.h:319
bool isa_and_nonnull() const
Definition: Types.h:264
bool isSignlessIntOrFloat() const
Return true of this is a signless integer or a float type.
Definition: Types.cpp:82
static mlir::Type getTombstoneKey()
Definition: Types.h:294
bool hasTrait()
Returns true if the type was registered with a particular trait.
Definition: Types.h:183
Helper class for implementing traits for storage classes.
bool isa() const
Definition: Types.h:259
static mlir::Type getEmptyKey()
Definition: Types.h:290
This class provides management for the lifetime of the state used when printing the IR...
Definition: AsmState.h:523
bool isBF16() const
Definition: Types.cpp:22
U cast() const
Definition: Types.h:279
bool operator!=(Type other) const
Definition: Types.h:94