MLIR  20.0.0git
Types.h
Go to the documentation of this file.
1 //===- Types.h --------------------------------------------------*- 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_TOOLS_PDLL_AST_TYPES_H_
10 #define MLIR_TOOLS_PDLL_AST_TYPES_H_
11 
12 #include "mlir/Support/LLVM.h"
14 #include <optional>
15 
16 namespace mlir {
17 namespace pdll {
18 namespace ods {
19 class Operation;
20 } // namespace ods
21 
22 namespace ast {
23 class Context;
24 
25 namespace detail {
29 struct RangeTypeStorage;
30 struct RewriteTypeStorage;
31 struct TupleTypeStorage;
32 struct TypeTypeStorage;
33 struct ValueTypeStorage;
34 } // namespace detail
35 
36 //===----------------------------------------------------------------------===//
37 // Type
38 //===----------------------------------------------------------------------===//
39 
40 class Type {
41 public:
42  /// This class represents the internal storage of the Type class.
43  struct Storage;
44 
45  /// This class provides several utilities when defining derived type classes.
46  template <typename ImplT, typename BaseT = Type>
47  class TypeBase : public BaseT {
48  public:
50  using ImplTy = ImplT;
51  using BaseT::BaseT;
52 
53  /// Provide type casting support.
54  static bool classof(Type type) {
55  return type.getTypeID() == TypeID::get<ImplTy>();
56  }
57  };
58 
59  Type(Storage *impl = nullptr) : impl(impl) {}
60 
61  bool operator==(const Type &other) const { return impl == other.impl; }
62  bool operator!=(const Type &other) const { return !(*this == other); }
63  explicit operator bool() const { return impl; }
64 
65  /// Provide type casting support.
66  template <typename U>
67  [[deprecated("Use mlir::isa<U>() instead")]]
68  bool isa() const {
69  assert(impl && "isa<> used on a null type.");
70  return U::classof(*this);
71  }
72  template <typename U, typename V, typename... Others>
73  [[deprecated("Use mlir::isa<U>() instead")]]
74  bool isa() const {
75  return isa<U>() || isa<V, Others...>();
76  }
77  template <typename U>
78  [[deprecated("Use mlir::dyn_cast<U>() instead")]]
79  U dyn_cast() const {
80  return isa<U>() ? U(impl) : U(nullptr);
81  }
82  template <typename U>
83  [[deprecated("Use mlir::dyn_cast_or_null<U>() instead")]]
84  U dyn_cast_or_null() const {
85  return (impl && isa<U>()) ? U(impl) : U(nullptr);
86  }
87  template <typename U>
88  [[deprecated("Use mlir::cast<U>() instead")]]
89  U cast() const {
90  assert(isa<U>());
91  return U(impl);
92  }
93 
94  /// Return the internal storage instance of this type.
95  Storage *getImpl() const { return impl; }
96 
97  /// Return the TypeID instance of this type.
98  TypeID getTypeID() const;
99 
100  /// Print this type to the given stream.
101  void print(raw_ostream &os) const;
102 
103  /// Try to refine this type with the one provided. Given two compatible types,
104  /// this will return a merged type contains as much detail from the two types.
105  /// For example, if refining two operation types and one contains a name,
106  /// while the other doesn't, the refined type contains the name. If the two
107  /// types are incompatible, null is returned.
108  Type refineWith(Type other) const;
109 
110 protected:
111  /// Return the internal storage instance of this type reinterpreted as the
112  /// given derived storage type.
113  template <typename T>
114  const T *getImplAs() const {
115  return static_cast<const T *>(impl);
116  }
117 
118 private:
119  Storage *impl;
120 };
121 
122 inline llvm::hash_code hash_value(Type type) {
124 }
125 
126 inline raw_ostream &operator<<(raw_ostream &os, Type type) {
127  type.print(os);
128  return os;
129 }
130 
131 //===----------------------------------------------------------------------===//
132 // AttributeType
133 //===----------------------------------------------------------------------===//
134 
135 /// This class represents a PDLL type that corresponds to an mlir::Attribute.
136 class AttributeType : public Type::TypeBase<detail::AttributeTypeStorage> {
137 public:
138  using Base::Base;
139 
140  /// Return an instance of the Attribute type.
141  static AttributeType get(Context &context);
142 };
143 
144 //===----------------------------------------------------------------------===//
145 // ConstraintType
146 //===----------------------------------------------------------------------===//
147 
148 /// This class represents a PDLL type that corresponds to a constraint. This
149 /// type has no MLIR C++ API correspondance.
150 class ConstraintType : public Type::TypeBase<detail::ConstraintTypeStorage> {
151 public:
152  using Base::Base;
153 
154  /// Return an instance of the Constraint type.
155  static ConstraintType get(Context &context);
156 };
157 
158 //===----------------------------------------------------------------------===//
159 // OperationType
160 //===----------------------------------------------------------------------===//
161 
162 /// This class represents a PDLL type that corresponds to an mlir::Operation.
163 class OperationType : public Type::TypeBase<detail::OperationTypeStorage> {
164 public:
165  using Base::Base;
166 
167  /// Return an instance of the Operation type with an optional operation name.
168  /// If no name is provided, this type may refer to any operation.
169  static OperationType get(Context &context,
170  std::optional<StringRef> name = std::nullopt,
171  const ods::Operation *odsOp = nullptr);
172 
173  /// Return the name of this operation type, or std::nullopt if it doesn't have
174  /// on.
175  std::optional<StringRef> getName() const;
176 
177  /// Return the ODS operation that this type refers to, or nullptr if the ODS
178  /// operation is unknown.
179  const ods::Operation *getODSOperation() const;
180 };
181 
182 //===----------------------------------------------------------------------===//
183 // RangeType
184 //===----------------------------------------------------------------------===//
185 
186 /// This class represents a PDLL type that corresponds to a range of elements
187 /// with a given element type.
188 class RangeType : public Type::TypeBase<detail::RangeTypeStorage> {
189 public:
190  using Base::Base;
191 
192  /// Return an instance of the Range type with the given element type.
193  static RangeType get(Context &context, Type elementType);
194 
195  /// Return the element type of this range.
196  Type getElementType() const;
197 };
198 
199 //===----------------------------------------------------------------------===//
200 // TypeRangeType
201 
202 /// This class represents a PDLL type that corresponds to an mlir::TypeRange.
203 class TypeRangeType : public RangeType {
204 public:
205  using RangeType::RangeType;
206 
207  /// Provide type casting support.
208  static bool classof(Type type);
209 
210  /// Return an instance of the TypeRange type.
211  static TypeRangeType get(Context &context);
212 };
213 
214 //===----------------------------------------------------------------------===//
215 // ValueRangeType
216 
217 /// This class represents a PDLL type that corresponds to an mlir::ValueRange.
218 class ValueRangeType : public RangeType {
219 public:
220  using RangeType::RangeType;
221 
222  /// Provide type casting support.
223  static bool classof(Type type);
224 
225  /// Return an instance of the ValueRange type.
226  static ValueRangeType get(Context &context);
227 };
228 
229 //===----------------------------------------------------------------------===//
230 // RewriteType
231 //===----------------------------------------------------------------------===//
232 
233 /// This class represents a PDLL type that corresponds to a rewrite reference.
234 /// This type has no MLIR C++ API correspondance.
235 class RewriteType : public Type::TypeBase<detail::RewriteTypeStorage> {
236 public:
237  using Base::Base;
238 
239  /// Return an instance of the Rewrite type.
240  static RewriteType get(Context &context);
241 };
242 
243 //===----------------------------------------------------------------------===//
244 // TupleType
245 //===----------------------------------------------------------------------===//
246 
247 /// This class represents a PDLL tuple type, i.e. an ordered set of element
248 /// types with optional names.
249 class TupleType : public Type::TypeBase<detail::TupleTypeStorage> {
250 public:
251  using Base::Base;
252 
253  /// Return an instance of the Tuple type.
254  static TupleType get(Context &context, ArrayRef<Type> elementTypes,
255  ArrayRef<StringRef> elementNames);
256  static TupleType get(Context &context,
257  ArrayRef<Type> elementTypes = std::nullopt);
258 
259  /// Return the element types of this tuple.
261 
262  /// Return the element names of this tuple.
264 
265  /// Return the number of elements within this tuple.
266  size_t size() const { return getElementTypes().size(); }
267 
268  /// Return if the tuple has no elements.
269  bool empty() const { return size() == 0; }
270 };
271 
272 //===----------------------------------------------------------------------===//
273 // TypeType
274 //===----------------------------------------------------------------------===//
275 
276 /// This class represents a PDLL type that corresponds to an mlir::Type.
277 class TypeType : public Type::TypeBase<detail::TypeTypeStorage> {
278 public:
279  using Base::Base;
280 
281  /// Return an instance of the Type type.
282  static TypeType get(Context &context);
283 };
284 
285 //===----------------------------------------------------------------------===//
286 // ValueType
287 //===----------------------------------------------------------------------===//
288 
289 /// This class represents a PDLL type that corresponds to an mlir::Value.
290 class ValueType : public Type::TypeBase<detail::ValueTypeStorage> {
291 public:
292  using Base::Base;
293 
294  /// Return an instance of the Value type.
295  static ValueType get(Context &context);
296 };
297 
298 } // namespace ast
299 } // namespace pdll
300 } // namespace mlir
301 
310 
311 namespace llvm {
312 template <>
313 struct DenseMapInfo<mlir::pdll::ast::Type> {
315  void *pointer = llvm::DenseMapInfo<void *>::getEmptyKey();
316  return mlir::pdll::ast::Type(
317  static_cast<mlir::pdll::ast::Type::Storage *>(pointer));
318  }
321  return mlir::pdll::ast::Type(
322  static_cast<mlir::pdll::ast::Type::Storage *>(pointer));
323  }
324  static unsigned getHashValue(mlir::pdll::ast::Type val) {
325  return llvm::hash_value(val.getImpl());
326  }
328  return lhs == rhs;
329  }
330 };
331 
332 /// Add support for llvm style casts.
333 /// We provide a cast between To and From if From is mlir::pdll::ast::Type or
334 /// derives from it
335 template <typename To, typename From>
336 struct CastInfo<
337  To, From,
338  std::enable_if_t<
339  std::is_same_v<mlir::pdll::ast::Type, std::remove_const_t<From>> ||
340  std::is_base_of_v<mlir::pdll::ast::Type, From>>>
342  DefaultDoCastIfPossible<To, From, CastInfo<To, From>> {
343  static inline bool isPossible(mlir::pdll::ast::Type ty) {
344  /// Return a constant true instead of a dynamic true when casting to self or
345  /// up the hierarchy.
346  if constexpr (std::is_base_of_v<To, From>) {
347  return true;
348  } else {
349  return To::classof(ty);
350  };
351  }
352  static inline To doCast(mlir::pdll::ast::Type ty) { return To(ty.getImpl()); }
353 };
354 } // namespace llvm
355 
356 #endif // MLIR_TOOLS_PDLL_AST_TYPES_H_
#define MLIR_DECLARE_EXPLICIT_TYPE_ID(CLASS_NAME)
Definition: TypeID.h:249
This class provides an efficient unique identifier for a specific C++ type.
Definition: TypeID.h:104
This class represents a PDLL type that corresponds to an mlir::Attribute.
Definition: Types.h:136
static AttributeType get(Context &context)
Return an instance of the Attribute type.
Definition: Types.cpp:57
This class represents a PDLL type that corresponds to a constraint.
Definition: Types.h:150
static ConstraintType get(Context &context)
Return an instance of the Constraint type.
Definition: Types.cpp:65
This class represents the main context of the PDLL AST.
Definition: Context.h:25
This class represents a PDLL type that corresponds to an mlir::Operation.
Definition: Types.h:163
const ods::Operation * getODSOperation() const
Return the ODS operation that this type refers to, or nullptr if the ODS operation is unknown.
Definition: Types.cpp:87
static OperationType get(Context &context, std::optional< StringRef > name=std::nullopt, const ods::Operation *odsOp=nullptr)
Return an instance of the Operation type with an optional operation name.
Definition: Types.cpp:73
std::optional< StringRef > getName() const
Return the name of this operation type, or std::nullopt if it doesn't have on.
Definition: Types.cpp:81
This class represents a PDLL type that corresponds to a range of elements with a given element type.
Definition: Types.h:188
Type getElementType() const
Return the element type of this range.
Definition: Types.cpp:100
static RangeType get(Context &context, Type elementType)
Return an instance of the Range type with the given element type.
Definition: Types.cpp:95
This class represents a PDLL type that corresponds to a rewrite reference.
Definition: Types.h:235
static RewriteType get(Context &context)
Return an instance of the Rewrite type.
Definition: Types.cpp:134
This class represents a PDLL tuple type, i.e.
Definition: Types.h:249
size_t size() const
Return the number of elements within this tuple.
Definition: Types.h:266
ArrayRef< StringRef > getElementNames() const
Return the element names of this tuple.
Definition: Types.cpp:157
bool empty() const
Return if the tuple has no elements.
Definition: Types.h:269
ArrayRef< Type > getElementTypes() const
Return the element types of this tuple.
Definition: Types.cpp:153
static TupleType get(Context &context, ArrayRef< Type > elementTypes, ArrayRef< StringRef > elementNames)
Return an instance of the Tuple type.
Definition: Types.cpp:142
This class represents a PDLL type that corresponds to an mlir::TypeRange.
Definition: Types.h:203
static TypeRangeType get(Context &context)
Return an instance of the TypeRange type.
Definition: Types.cpp:112
static bool classof(Type type)
Provide type casting support.
Definition: Types.cpp:107
This class represents a PDLL type that corresponds to an mlir::Type.
Definition: Types.h:277
static TypeType get(Context &context)
Return an instance of the Type type.
Definition: Types.cpp:165
This class provides several utilities when defining derived type classes.
Definition: Types.h:47
TypeBase< ImplT, BaseT > Base
Definition: Types.h:49
static bool classof(Type type)
Provide type casting support.
Definition: Types.h:54
U dyn_cast_or_null() const
Definition: Types.h:84
TypeID getTypeID() const
Return the TypeID instance of this type.
Definition: Types.cpp:31
bool isa() const
Definition: Types.h:74
bool operator!=(const Type &other) const
Definition: Types.h:62
void print(raw_ostream &os) const
Print this type to the given stream.
Type(Storage *impl=nullptr)
Definition: Types.h:59
const T * getImplAs() const
Return the internal storage instance of this type reinterpreted as the given derived storage type.
Definition: Types.h:114
U dyn_cast() const
Definition: Types.h:79
Storage * getImpl() const
Return the internal storage instance of this type.
Definition: Types.h:95
U cast() const
Definition: Types.h:89
Type refineWith(Type other) const
Try to refine this type with the one provided.
Definition: Types.cpp:33
bool isa() const
Provide type casting support.
Definition: Types.h:68
bool operator==(const Type &other) const
Definition: Types.h:61
This class represents a PDLL type that corresponds to an mlir::ValueRange.
Definition: Types.h:218
static bool classof(Type type)
Provide type casting support.
Definition: Types.cpp:120
static ValueRangeType get(Context &context)
Return an instance of the ValueRange type.
Definition: Types.cpp:125
This class represents a PDLL type that corresponds to an mlir::Value.
Definition: Types.h:290
static ValueType get(Context &context)
Return an instance of the Value type.
Definition: Types.cpp:173
This class provides an ODS representation of a specific operation.
Definition: Operation.h:125
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition: CallGraph.h:229
llvm::hash_code hash_value(Type type)
Definition: Types.h:122
raw_ostream & operator<<(raw_ostream &os, Type type)
Definition: Types.h:126
Include the generated interface declarations.
static bool isEqual(mlir::pdll::ast::Type lhs, mlir::pdll::ast::Type rhs)
Definition: Types.h:327
static unsigned getHashValue(mlir::pdll::ast::Type val)
Definition: Types.h:324
static mlir::pdll::ast::Type getTombstoneKey()
Definition: Types.h:319
static mlir::pdll::ast::Type getEmptyKey()
Definition: Types.h:314