MLIR  19.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  bool isa() const {
68  assert(impl && "isa<> used on a null type.");
69  return U::classof(*this);
70  }
71  template <typename U, typename V, typename... Others>
72  bool isa() const {
73  return isa<U>() || isa<V, Others...>();
74  }
75  template <typename U>
76  U dyn_cast() const {
77  return isa<U>() ? U(impl) : U(nullptr);
78  }
79  template <typename U>
80  U dyn_cast_or_null() const {
81  return (impl && isa<U>()) ? U(impl) : U(nullptr);
82  }
83  template <typename U>
84  U cast() const {
85  assert(isa<U>());
86  return U(impl);
87  }
88 
89  /// Return the internal storage instance of this type.
90  Storage *getImpl() const { return impl; }
91 
92  /// Return the TypeID instance of this type.
93  TypeID getTypeID() const;
94 
95  /// Print this type to the given stream.
96  void print(raw_ostream &os) const;
97 
98  /// Try to refine this type with the one provided. Given two compatible types,
99  /// this will return a merged type contains as much detail from the two types.
100  /// For example, if refining two operation types and one contains a name,
101  /// while the other doesn't, the refined type contains the name. If the two
102  /// types are incompatible, null is returned.
103  Type refineWith(Type other) const;
104 
105 protected:
106  /// Return the internal storage instance of this type reinterpreted as the
107  /// given derived storage type.
108  template <typename T>
109  const T *getImplAs() const {
110  return static_cast<const T *>(impl);
111  }
112 
113 private:
114  Storage *impl;
115 };
116 
117 inline llvm::hash_code hash_value(Type type) {
119 }
120 
121 inline raw_ostream &operator<<(raw_ostream &os, Type type) {
122  type.print(os);
123  return os;
124 }
125 
126 //===----------------------------------------------------------------------===//
127 // AttributeType
128 //===----------------------------------------------------------------------===//
129 
130 /// This class represents a PDLL type that corresponds to an mlir::Attribute.
131 class AttributeType : public Type::TypeBase<detail::AttributeTypeStorage> {
132 public:
133  using Base::Base;
134 
135  /// Return an instance of the Attribute type.
136  static AttributeType get(Context &context);
137 };
138 
139 //===----------------------------------------------------------------------===//
140 // ConstraintType
141 //===----------------------------------------------------------------------===//
142 
143 /// This class represents a PDLL type that corresponds to a constraint. This
144 /// type has no MLIR C++ API correspondance.
145 class ConstraintType : public Type::TypeBase<detail::ConstraintTypeStorage> {
146 public:
147  using Base::Base;
148 
149  /// Return an instance of the Constraint type.
150  static ConstraintType get(Context &context);
151 };
152 
153 //===----------------------------------------------------------------------===//
154 // OperationType
155 //===----------------------------------------------------------------------===//
156 
157 /// This class represents a PDLL type that corresponds to an mlir::Operation.
158 class OperationType : public Type::TypeBase<detail::OperationTypeStorage> {
159 public:
160  using Base::Base;
161 
162  /// Return an instance of the Operation type with an optional operation name.
163  /// If no name is provided, this type may refer to any operation.
164  static OperationType get(Context &context,
165  std::optional<StringRef> name = std::nullopt,
166  const ods::Operation *odsOp = nullptr);
167 
168  /// Return the name of this operation type, or std::nullopt if it doesn't have
169  /// on.
170  std::optional<StringRef> getName() const;
171 
172  /// Return the ODS operation that this type refers to, or nullptr if the ODS
173  /// operation is unknown.
174  const ods::Operation *getODSOperation() const;
175 };
176 
177 //===----------------------------------------------------------------------===//
178 // RangeType
179 //===----------------------------------------------------------------------===//
180 
181 /// This class represents a PDLL type that corresponds to a range of elements
182 /// with a given element type.
183 class RangeType : public Type::TypeBase<detail::RangeTypeStorage> {
184 public:
185  using Base::Base;
186 
187  /// Return an instance of the Range type with the given element type.
188  static RangeType get(Context &context, Type elementType);
189 
190  /// Return the element type of this range.
191  Type getElementType() const;
192 };
193 
194 //===----------------------------------------------------------------------===//
195 // TypeRangeType
196 
197 /// This class represents a PDLL type that corresponds to an mlir::TypeRange.
198 class TypeRangeType : public RangeType {
199 public:
200  using RangeType::RangeType;
201 
202  /// Provide type casting support.
203  static bool classof(Type type);
204 
205  /// Return an instance of the TypeRange type.
206  static TypeRangeType get(Context &context);
207 };
208 
209 //===----------------------------------------------------------------------===//
210 // ValueRangeType
211 
212 /// This class represents a PDLL type that corresponds to an mlir::ValueRange.
213 class ValueRangeType : public RangeType {
214 public:
215  using RangeType::RangeType;
216 
217  /// Provide type casting support.
218  static bool classof(Type type);
219 
220  /// Return an instance of the ValueRange type.
221  static ValueRangeType get(Context &context);
222 };
223 
224 //===----------------------------------------------------------------------===//
225 // RewriteType
226 //===----------------------------------------------------------------------===//
227 
228 /// This class represents a PDLL type that corresponds to a rewrite reference.
229 /// This type has no MLIR C++ API correspondance.
230 class RewriteType : public Type::TypeBase<detail::RewriteTypeStorage> {
231 public:
232  using Base::Base;
233 
234  /// Return an instance of the Rewrite type.
235  static RewriteType get(Context &context);
236 };
237 
238 //===----------------------------------------------------------------------===//
239 // TupleType
240 //===----------------------------------------------------------------------===//
241 
242 /// This class represents a PDLL tuple type, i.e. an ordered set of element
243 /// types with optional names.
244 class TupleType : public Type::TypeBase<detail::TupleTypeStorage> {
245 public:
246  using Base::Base;
247 
248  /// Return an instance of the Tuple type.
249  static TupleType get(Context &context, ArrayRef<Type> elementTypes,
250  ArrayRef<StringRef> elementNames);
251  static TupleType get(Context &context,
252  ArrayRef<Type> elementTypes = std::nullopt);
253 
254  /// Return the element types of this tuple.
256 
257  /// Return the element names of this tuple.
259 
260  /// Return the number of elements within this tuple.
261  size_t size() const { return getElementTypes().size(); }
262 
263  /// Return if the tuple has no elements.
264  bool empty() const { return size() == 0; }
265 };
266 
267 //===----------------------------------------------------------------------===//
268 // TypeType
269 //===----------------------------------------------------------------------===//
270 
271 /// This class represents a PDLL type that corresponds to an mlir::Type.
272 class TypeType : public Type::TypeBase<detail::TypeTypeStorage> {
273 public:
274  using Base::Base;
275 
276  /// Return an instance of the Type type.
277  static TypeType get(Context &context);
278 };
279 
280 //===----------------------------------------------------------------------===//
281 // ValueType
282 //===----------------------------------------------------------------------===//
283 
284 /// This class represents a PDLL type that corresponds to an mlir::Value.
285 class ValueType : public Type::TypeBase<detail::ValueTypeStorage> {
286 public:
287  using Base::Base;
288 
289  /// Return an instance of the Value type.
290  static ValueType get(Context &context);
291 };
292 
293 } // namespace ast
294 } // namespace pdll
295 } // namespace mlir
296 
305 
306 namespace llvm {
307 template <>
308 struct DenseMapInfo<mlir::pdll::ast::Type> {
310  void *pointer = llvm::DenseMapInfo<void *>::getEmptyKey();
311  return mlir::pdll::ast::Type(
312  static_cast<mlir::pdll::ast::Type::Storage *>(pointer));
313  }
316  return mlir::pdll::ast::Type(
317  static_cast<mlir::pdll::ast::Type::Storage *>(pointer));
318  }
319  static unsigned getHashValue(mlir::pdll::ast::Type val) {
320  return llvm::hash_value(val.getImpl());
321  }
323  return lhs == rhs;
324  }
325 };
326 } // namespace llvm
327 
328 #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:131
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:145
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:158
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:183
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:230
static RewriteType get(Context &context)
Return an instance of the Rewrite type.
Definition: Types.cpp:133
This class represents a PDLL tuple type, i.e.
Definition: Types.h:244
size_t size() const
Return the number of elements within this tuple.
Definition: Types.h:261
ArrayRef< StringRef > getElementNames() const
Return the element names of this tuple.
Definition: Types.cpp:156
bool empty() const
Return if the tuple has no elements.
Definition: Types.h:264
ArrayRef< Type > getElementTypes() const
Return the element types of this tuple.
Definition: Types.cpp:152
static TupleType get(Context &context, ArrayRef< Type > elementTypes, ArrayRef< StringRef > elementNames)
Return an instance of the Tuple type.
Definition: Types.cpp:141
This class represents a PDLL type that corresponds to an mlir::TypeRange.
Definition: Types.h:198
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:272
static TypeType get(Context &context)
Return an instance of the Type type.
Definition: Types.cpp:164
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:80
TypeID getTypeID() const
Return the TypeID instance of this type.
Definition: Types.cpp:31
bool isa() const
Definition: Types.h:72
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:109
U dyn_cast() const
Definition: Types.h:76
Storage * getImpl() const
Return the internal storage instance of this type.
Definition: Types.h:90
U cast() const
Definition: Types.h:84
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:67
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:213
static bool classof(Type type)
Provide type casting support.
Definition: Types.cpp:119
static ValueRangeType get(Context &context)
Return an instance of the ValueRange type.
Definition: Types.cpp:124
This class represents a PDLL type that corresponds to an mlir::Value.
Definition: Types.h:285
static ValueType get(Context &context)
Return an instance of the Value type.
Definition: Types.cpp:172
This class provides an ODS representation of a specific operation.
Definition: Operation.h:125
Include the generated interface declarations.
Definition: CallGraph.h:229
llvm::hash_code hash_value(Type type)
Definition: Types.h:117
raw_ostream & operator<<(raw_ostream &os, Type type)
Definition: Types.h:121
Include the generated interface declarations.
static bool isEqual(mlir::pdll::ast::Type lhs, mlir::pdll::ast::Type rhs)
Definition: Types.h:322
static unsigned getHashValue(mlir::pdll::ast::Type val)
Definition: Types.h:319
static mlir::pdll::ast::Type getTombstoneKey()
Definition: Types.h:314
static mlir::pdll::ast::Type getEmptyKey()
Definition: Types.h:309