MLIR 22.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
16namespace mlir {
17namespace pdll {
18namespace ods {
19class Operation;
20} // namespace ods
21
22namespace ast {
23class Context;
24
25namespace detail {
29struct RangeTypeStorage;
31struct TupleTypeStorage;
32struct TypeTypeStorage;
33struct ValueTypeStorage;
34} // namespace detail
35
36//===----------------------------------------------------------------------===//
37// Type
38//===----------------------------------------------------------------------===//
39
40class Type {
41public:
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
81protected:
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
89private:
90 Storage *impl;
91};
92
93inline llvm::hash_code hash_value(Type type) {
95}
96
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.
107class AttributeType : public Type::TypeBase<detail::AttributeTypeStorage> {
108public:
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.
121class ConstraintType : public Type::TypeBase<detail::ConstraintTypeStorage> {
122public:
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.
134class OperationType : public Type::TypeBase<detail::OperationTypeStorage> {
135public:
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.
159class RangeType : public Type::TypeBase<detail::RangeTypeStorage> {
160public:
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.
175class TypeRangeType : public RangeType {
176public:
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.
191class ValueRangeType : public RangeType {
192public:
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.
208class RewriteType : public Type::TypeBase<detail::RewriteTypeStorage> {
209public:
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.
222class TupleType : public Type::TypeBase<detail::TupleTypeStorage> {
223public:
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, ArrayRef<Type> elementTypes = {});
230
231 /// Return the element types of this tuple.
233
234 /// Return the element names of this tuple.
236
237 /// Return the number of elements within this tuple.
238 size_t size() const { return getElementTypes().size(); }
239
240 /// Return if the tuple has no elements.
241 bool empty() const { return size() == 0; }
242};
243
244//===----------------------------------------------------------------------===//
245// TypeType
246//===----------------------------------------------------------------------===//
247
248/// This class represents a PDLL type that corresponds to an mlir::Type.
249class TypeType : public Type::TypeBase<detail::TypeTypeStorage> {
250public:
251 using Base::Base;
252
253 /// Return an instance of the Type type.
254 static TypeType get(Context &context);
255};
256
257//===----------------------------------------------------------------------===//
258// ValueType
259//===----------------------------------------------------------------------===//
260
261/// This class represents a PDLL type that corresponds to an mlir::Value.
262class ValueType : public Type::TypeBase<detail::ValueTypeStorage> {
263public:
264 using Base::Base;
265
266 /// Return an instance of the Value type.
267 static ValueType get(Context &context);
268};
269
270} // namespace ast
271} // namespace pdll
272} // namespace mlir
273
282
283namespace llvm {
284template <>
285struct DenseMapInfo<mlir::pdll::ast::Type> {
289 static_cast<mlir::pdll::ast::Type::Storage *>(pointer));
290 }
296 static unsigned getHashValue(mlir::pdll::ast::Type val) {
297 return llvm::hash_value(val.getImpl());
298 }
300 return lhs == rhs;
301 }
302};
303
304/// Add support for llvm style casts.
305/// We provide a cast between To and From if From is mlir::pdll::ast::Type or
306/// derives from it
307template <typename To, typename From>
308struct CastInfo<
309 To, From,
310 std::enable_if_t<
311 std::is_same_v<mlir::pdll::ast::Type, std::remove_const_t<From>> ||
312 std::is_base_of_v<mlir::pdll::ast::Type, From>>>
314 DefaultDoCastIfPossible<To, From, CastInfo<To, From>> {
315 static inline bool isPossible(mlir::pdll::ast::Type ty) {
316 /// Return a constant true instead of a dynamic true when casting to self or
317 /// up the hierarchy.
318 if constexpr (std::is_base_of_v<To, From>) {
319 return true;
320 } else {
321 return To::classof(ty);
322 };
323 }
324 static inline To doCast(mlir::pdll::ast::Type ty) { return To(ty.getImpl()); }
325};
326} // namespace llvm
327
328#endif // MLIR_TOOLS_PDLL_AST_TYPES_H_
lhs
#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
static TypeID get()
Construct a type info object for the given type T.
Definition TypeID.h:245
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:238
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:241
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:249
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
Storage * getImpl() const
Return the internal storage instance of this type.
Definition Types.h:66
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
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:262
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.
llvm::DenseMapInfo< T, Enable > DenseMapInfo
Definition LLVM.h:122
static bool isEqual(mlir::pdll::ast::Type lhs, mlir::pdll::ast::Type rhs)
Definition Types.h:299
static unsigned getHashValue(mlir::pdll::ast::Type val)
Definition Types.h:296
static mlir::pdll::ast::Type getTombstoneKey()
Definition Types.h:291
static mlir::pdll::ast::Type getEmptyKey()
Definition Types.h:286