My Project
Types.h
Go to the documentation of this file.
1 //===- Types.h - MLIR Type Classes ------------------------------*- 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 #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 
16 namespace mlir {
17 class FloatType;
18 class Identifier;
19 class IndexType;
20 class IntegerType;
21 class MLIRContext;
22 class TypeStorage;
23 
24 namespace detail {
25 struct FunctionTypeStorage;
26 struct OpaqueTypeStorage;
27 } // namespace detail
28 
84 class Type {
85 public:
91  enum Kind {
92  // Builtin types.
95  LAST_BUILTIN_TYPE = Opaque,
96 
97  // Reserve type kinds for dialect specific type system extensions.
98 #define DEFINE_SYM_KIND_RANGE(Dialect) \
99  FIRST_##Dialect##_TYPE, LAST_##Dialect##_TYPE = FIRST_##Dialect##_TYPE + 0xff,
100 #include "DialectSymbolRegistry.def"
101  };
102 
104  template <typename ConcreteType, typename BaseType,
105  typename StorageType = DefaultTypeStorage>
106  using TypeBase = detail::StorageUserBase<ConcreteType, BaseType, StorageType,
108 
110 
111  Type() : impl(nullptr) {}
112  /* implicit */ Type(const ImplType *impl)
113  : impl(const_cast<ImplType *>(impl)) {}
114 
115  Type(const Type &other) = default;
116  Type &operator=(const Type &other) = default;
117 
118  bool operator==(Type other) const { return impl == other.impl; }
119  bool operator!=(Type other) const { return !(*this == other); }
120  explicit operator bool() const { return impl; }
121 
122  bool operator!() const { return impl == nullptr; }
123 
124  template <typename U> bool isa() const;
125  template <typename U> U dyn_cast() const;
126  template <typename U> U dyn_cast_or_null() const;
127  template <typename U> U cast() const;
128 
129  // Support type casting Type to itself.
130  static bool classof(Type) { return true; }
131 
133  unsigned getKind() const;
134 
136  MLIRContext *getContext() const;
137 
139  Dialect &getDialect() const;
140 
141  // Convenience predicates. This is only for floating point types,
142  // derived types should use isa/dyn_cast.
143  bool isIndex();
144  bool isBF16();
145  bool isF16();
146  bool isF32();
147  bool isF64();
148 
150  bool isInteger(unsigned width);
151 
154  unsigned getIntOrFloatBitWidth();
155 
157  bool isIntOrIndex();
159  bool isIntOrIndexOrFloat();
161  bool isIntOrFloat();
162 
164  void print(raw_ostream &os);
165  void dump();
166 
167  friend ::llvm::hash_code hash_value(Type arg);
168 
169  unsigned getSubclassData() const;
170  void setSubclassData(unsigned val);
171 
173  const void *getAsOpaquePointer() const {
174  return static_cast<const void *>(impl);
175  }
176  static Type getFromOpaquePointer(const void *pointer) {
177  return Type(reinterpret_cast<ImplType *>(const_cast<void *>(pointer)));
178  }
179 
180 protected:
182 };
183 
184 inline raw_ostream &operator<<(raw_ostream &os, Type type) {
185  type.print(os);
186  return os;
187 }
188 
191  : public Type::TypeBase<FunctionType, Type, detail::FunctionTypeStorage> {
192 public:
193  using Base::Base;
194 
195  static FunctionType get(ArrayRef<Type> inputs, ArrayRef<Type> results,
196  MLIRContext *context);
197 
198  // Input types.
199  unsigned getNumInputs() const { return getSubclassData(); }
200 
201  Type getInput(unsigned i) const { return getInputs()[i]; }
202 
203  ArrayRef<Type> getInputs() const;
204 
205  // Result types.
206  unsigned getNumResults() const;
207 
208  Type getResult(unsigned i) const { return getResults()[i]; }
209 
210  ArrayRef<Type> getResults() const;
211 
213  static bool kindof(unsigned kind) { return kind == Kind::Function; }
214 };
215 
220  : public Type::TypeBase<OpaqueType, Type, detail::OpaqueTypeStorage> {
221 public:
222  using Base::Base;
223 
225  static OpaqueType get(Identifier dialect, StringRef typeData,
226  MLIRContext *context);
227 
231  static OpaqueType getChecked(Identifier dialect, StringRef typeData,
232  MLIRContext *context, Location location);
233 
235  Identifier getDialectNamespace() const;
236 
238  StringRef getTypeData() const;
239 
241  static LogicalResult verifyConstructionInvariants(Optional<Location> loc,
242  MLIRContext *context,
243  Identifier dialect,
244  StringRef typeData);
245 
246  static bool kindof(unsigned kind) { return kind == Kind::Opaque; }
247 };
248 
249 // Make Type hashable.
250 inline ::llvm::hash_code hash_value(Type arg) {
252 }
253 
254 template <typename U> bool Type::isa() const {
255  assert(impl && "isa<> used on a null type.");
256  return U::classof(*this);
257 }
258 template <typename U> U Type::dyn_cast() const {
259  return isa<U>() ? U(impl) : U(nullptr);
260 }
261 template <typename U> U Type::dyn_cast_or_null() const {
262  return (impl && isa<U>()) ? U(impl) : U(nullptr);
263 }
264 template <typename U> U Type::cast() const {
265  assert(isa<U>());
266  return U(impl);
267 }
268 
269 } // end namespace mlir
270 
271 namespace llvm {
272 
273 // Type hash just like pointers.
274 template <> struct DenseMapInfo<mlir::Type> {
277  return mlir::Type(static_cast<mlir::Type::ImplType *>(pointer));
278  }
281  return mlir::Type(static_cast<mlir::Type::ImplType *>(pointer));
282  }
283  static unsigned getHashValue(mlir::Type val) { return mlir::hash_value(val); }
284  static bool isEqual(mlir::Type LHS, mlir::Type RHS) { return LHS == RHS; }
285 };
286 
288 template <> struct PointerLikeTypeTraits<mlir::Type> {
289 public:
290  static inline void *getAsVoidPointer(mlir::Type I) {
291  return const_cast<void *>(I.getAsOpaquePointer());
292  }
293  static inline mlir::Type getFromVoidPointer(void *P) {
295  }
296  enum { NumLowBitsAvailable = 3 };
297 };
298 
299 } // namespace llvm
300 
301 #endif // MLIR_IR_TYPES_H
bool operator!() const
Definition: Types.h:122
Definition: InferTypeOpInterface.cpp:20
const void * getAsOpaquePointer() const
Methods for supporting PointerLikeTypeTraits.
Definition: Types.h:173
ImplType * impl
Definition: Types.h:181
Type getResult(unsigned i) const
Definition: Types.h:208
Definition: PassRegistry.cpp:413
Kind
Definition: Types.h:91
Definition: Attributes.h:139
static bool isEqual(mlir::Type LHS, mlir::Type RHS)
Definition: Types.h:284
Definition: LLVM.h:45
Base storage class appearing in a Type.
Definition: TypeSupport.h:33
Function types map from a list of inputs to a list of results.
Definition: Types.h:190
Definition: Identifier.h:26
Definition: LLVM.h:40
Definition: Types.h:93
Type getInput(unsigned i) const
Definition: Types.h:201
Definition: Location.h:52
static bool classof(Type)
Definition: Types.h:130
static unsigned getHashValue(mlir::Type val)
Definition: Types.h:283
Definition: LogicalResult.h:18
Definition: LLVM.h:37
U dyn_cast_or_null() const
Definition: Types.h:261
static mlir::Type getFromVoidPointer(void *P)
Definition: Types.h:293
static bool kindof(unsigned kind)
Definition: Types.h:246
U dyn_cast() const
Definition: Types.h:258
Type()
Definition: Types.h:111
Definition: Dialect.h:39
Definition: TypeSupport.h:86
static Type getFromOpaquePointer(const void *pointer)
Definition: Types.h:176
Definition: Types.h:94
inline ::llvm::hash_code hash_value(Type arg)
Definition: Types.h:250
unsigned getNumInputs() const
Definition: Types.h:199
void print(raw_ostream &os)
Print the current type.
Definition: AsmPrinter.cpp:2070
Definition: Types.h:84
bool operator==(Type other) const
Definition: Types.h:118
Definition: Attributes.h:136
inline ::llvm::hash_code hash_value(AffineExpr arg)
Make AffineExpr hashable.
Definition: AffineExpr.h:201
static bool kindof(unsigned kind)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: Types.h:213
Type(const ImplType *impl)
Definition: Types.h:112
Definition: Types.h:219
Definition: StorageUniquerSupport.h:30
Definition: MLIRContext.h:34
void print(OpAsmPrinter &p, AffineIfOp op)
Definition: AffineOps.cpp:1671
static void * getAsVoidPointer(mlir::Type I)
Definition: Types.h:290
static mlir::Type getTombstoneKey()
Definition: Types.h:279
raw_ostream & operator<<(raw_ostream &os, SubViewOp::Range &range)
Definition: Ops.cpp:2759
bool isa() const
Definition: Types.h:254
static mlir::Type getEmptyKey()
Definition: Types.h:275
TypeStorage DefaultTypeStorage
Definition: TypeSupport.h:70
U cast() const
Definition: Types.h:264
bool operator!=(Type other) const
Definition: Types.h:119