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