MLIR 22.0.0git
Types.cpp
Go to the documentation of this file.
1//===- Types.cpp ----------------------------------------------------------===//
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
11#include <optional>
12
13using namespace mlir;
14using namespace mlir::pdll;
15using namespace mlir::pdll::ast;
16
25
26//===----------------------------------------------------------------------===//
27// Type
28//===----------------------------------------------------------------------===//
29
30TypeID Type::getTypeID() const { return impl->typeID; }
31
33 if (*this == other)
34 return *this;
35
36 // Operation types are compatible if the operation names don't conflict.
37 if (auto opTy = mlir::dyn_cast<OperationType>(*this)) {
38 auto otherOpTy = mlir::dyn_cast<ast::OperationType>(other);
39 if (!otherOpTy)
40 return nullptr;
41 if (!otherOpTy.getName())
42 return *this;
43 if (!opTy.getName())
44 return other;
45
46 return nullptr;
47 }
48
49 return nullptr;
50}
51
52//===----------------------------------------------------------------------===//
53// AttributeType
54//===----------------------------------------------------------------------===//
55
57 return context.getTypeUniquer().get<ImplTy>();
58}
59
60//===----------------------------------------------------------------------===//
61// ConstraintType
62//===----------------------------------------------------------------------===//
63
65 return context.getTypeUniquer().get<ImplTy>();
66}
67
68//===----------------------------------------------------------------------===//
69// OperationType
70//===----------------------------------------------------------------------===//
71
73 std::optional<StringRef> name,
74 const ods::Operation *odsOp) {
75 return context.getTypeUniquer().get<ImplTy>(
77 std::make_pair(name.value_or(""), odsOp));
78}
79
80std::optional<StringRef> OperationType::getName() const {
81 StringRef name = getImplAs<ImplTy>()->getValue().first;
82 return name.empty() ? std::optional<StringRef>()
83 : std::optional<StringRef>(name);
84}
85
87 return getImplAs<ImplTy>()->getValue().second;
88}
89
90//===----------------------------------------------------------------------===//
91// RangeType
92//===----------------------------------------------------------------------===//
93
94RangeType RangeType::get(Context &context, Type elementType) {
95 return context.getTypeUniquer().get<ImplTy>(
96 /*initFn=*/function_ref<void(ImplTy *)>(), elementType);
97}
98
100 return getImplAs<ImplTy>()->getValue();
101}
102
103//===----------------------------------------------------------------------===//
104// TypeRangeType
105//===----------------------------------------------------------------------===//
106
108 RangeType range = mlir::dyn_cast<RangeType>(type);
109 return range && mlir::isa<TypeType>(range.getElementType());
110}
111
113 return mlir::cast<TypeRangeType>(
114 RangeType::get(context, TypeType::get(context)));
115}
116
117//===----------------------------------------------------------------------===//
118// ValueRangeType
119//===----------------------------------------------------------------------===//
120
122 RangeType range = mlir::dyn_cast<RangeType>(type);
123 return range && mlir::isa<ValueType>(range.getElementType());
124}
125
127 return mlir::cast<ValueRangeType>(
128 RangeType::get(context, ValueType::get(context)));
129}
130
131//===----------------------------------------------------------------------===//
132// RewriteType
133//===----------------------------------------------------------------------===//
134
136 return context.getTypeUniquer().get<ImplTy>();
137}
138
139//===----------------------------------------------------------------------===//
140// TupleType
141//===----------------------------------------------------------------------===//
142
144 ArrayRef<StringRef> elementNames) {
145 assert(elementTypes.size() == elementNames.size());
146 return context.getTypeUniquer().get<ImplTy>(
147 /*initFn=*/function_ref<void(ImplTy *)>(), elementTypes, elementNames);
148}
150 SmallVector<StringRef> elementNames(elementTypes.size());
151 return get(context, elementTypes, elementNames);
152}
153
155 return getImplAs<ImplTy>()->getValue().first;
156}
157
159 return getImplAs<ImplTy>()->getValue().second;
160}
161
162//===----------------------------------------------------------------------===//
163// TypeType
164//===----------------------------------------------------------------------===//
165
167 return context.getTypeUniquer().get<ImplTy>();
168}
169
170//===----------------------------------------------------------------------===//
171// ValueType
172//===----------------------------------------------------------------------===//
173
175 return context.getTypeUniquer().get<ImplTy>();
176}
#define MLIR_DEFINE_EXPLICIT_TYPE_ID(CLASS_NAME)
Definition TypeID.h:323
Storage * get(function_ref< void(Storage *)> initFn, TypeID id, Args &&...args)
Gets a uniqued instance of 'Storage'.
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:217
static AttributeType get(Context &context)
Return an instance of the Attribute type.
Definition Types.cpp:56
This class represents a PDLL type that corresponds to a constraint.
Definition Types.h:231
static ConstraintType get(Context &context)
Return an instance of the Constraint type.
Definition Types.cpp:64
This class represents the main context of the PDLL AST.
Definition Context.h:25
StorageUniquer & getTypeUniquer()
Return the storage uniquer used for AST types.
Definition Context.h:35
This class represents a PDLL type that corresponds to an mlir::Operation.
Definition Types.h:244
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:86
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:72
std::optional< StringRef > getName() const
Return the name of this operation type, or std::nullopt if it doesn't have on.
Definition Types.cpp:80
This class represents a PDLL type that corresponds to a range of elements with a given element type.
Definition Types.h:269
Type getElementType() const
Return the element type of this range.
Definition Types.cpp:99
static RangeType get(Context &context, Type elementType)
Return an instance of the Range type with the given element type.
Definition Types.cpp:94
This class represents a PDLL type that corresponds to a rewrite reference.
Definition Types.h:318
static RewriteType get(Context &context)
Return an instance of the Rewrite type.
Definition Types.cpp:135
This class represents a PDLL tuple type, i.e.
Definition Types.h:332
ArrayRef< StringRef > getElementNames() const
Return the element names of this tuple.
Definition Types.cpp:158
ArrayRef< Type > getElementTypes() const
Return the element types of this tuple.
Definition Types.cpp:154
static TupleType get(Context &context, ArrayRef< Type > elementTypes, ArrayRef< StringRef > elementNames)
Return an instance of the Tuple type.
Definition Types.cpp:143
This class represents a PDLL type that corresponds to an mlir::TypeRange.
Definition Types.h:285
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:359
static TypeType get(Context &context)
Return an instance of the Type type.
Definition Types.cpp:166
TypeID getTypeID() const
Return the TypeID instance of this type.
Definition Types.cpp:30
Type(Storage *impl=nullptr)
Definition Types.h:48
const T * getImplAs() const
Return the internal storage instance of this type reinterpreted as the given derived storage type.
Definition Types.h:74
Type refineWith(Type other) const
Try to refine this type with the one provided.
Definition Types.cpp:32
This class represents a PDLL type that corresponds to an mlir::ValueRange.
Definition Types.h:301
static bool classof(Type type)
Provide type casting support.
Definition Types.cpp:121
static ValueRangeType get(Context &context)
Return an instance of the ValueRange type.
Definition Types.cpp:126
This class represents a PDLL type that corresponds to an mlir::Value.
Definition Types.h:372
static ValueType get(Context &context)
Return an instance of the Value type.
Definition Types.cpp:174
This class provides an ODS representation of a specific operation.
Definition Operation.h:125
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
llvm::function_ref< Fn > function_ref
Definition LLVM.h:152