MLIR  21.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 
10 #include "TypeDetail.h"
12 #include <optional>
13 
14 using namespace mlir;
15 using namespace mlir::pdll;
16 using namespace mlir::pdll::ast;
17 
26 
27 //===----------------------------------------------------------------------===//
28 // Type
29 //===----------------------------------------------------------------------===//
30 
31 TypeID Type::getTypeID() const { return impl->typeID; }
32 
33 Type Type::refineWith(Type other) const {
34  if (*this == other)
35  return *this;
36 
37  // Operation types are compatible if the operation names don't conflict.
38  if (auto opTy = mlir::dyn_cast<OperationType>(*this)) {
39  auto otherOpTy = mlir::dyn_cast<ast::OperationType>(other);
40  if (!otherOpTy)
41  return nullptr;
42  if (!otherOpTy.getName())
43  return *this;
44  if (!opTy.getName())
45  return other;
46 
47  return nullptr;
48  }
49 
50  return nullptr;
51 }
52 
53 //===----------------------------------------------------------------------===//
54 // AttributeType
55 //===----------------------------------------------------------------------===//
56 
58  return context.getTypeUniquer().get<ImplTy>();
59 }
60 
61 //===----------------------------------------------------------------------===//
62 // ConstraintType
63 //===----------------------------------------------------------------------===//
64 
66  return context.getTypeUniquer().get<ImplTy>();
67 }
68 
69 //===----------------------------------------------------------------------===//
70 // OperationType
71 //===----------------------------------------------------------------------===//
72 
74  std::optional<StringRef> name,
75  const ods::Operation *odsOp) {
76  return context.getTypeUniquer().get<ImplTy>(
77  /*initFn=*/function_ref<void(ImplTy *)>(),
78  std::make_pair(name.value_or(""), odsOp));
79 }
80 
81 std::optional<StringRef> OperationType::getName() const {
82  StringRef name = getImplAs<ImplTy>()->getValue().first;
83  return name.empty() ? std::optional<StringRef>()
84  : std::optional<StringRef>(name);
85 }
86 
88  return getImplAs<ImplTy>()->getValue().second;
89 }
90 
91 //===----------------------------------------------------------------------===//
92 // RangeType
93 //===----------------------------------------------------------------------===//
94 
95 RangeType RangeType::get(Context &context, Type elementType) {
96  return context.getTypeUniquer().get<ImplTy>(
97  /*initFn=*/function_ref<void(ImplTy *)>(), elementType);
98 }
99 
101  return getImplAs<ImplTy>()->getValue();
102 }
103 
104 //===----------------------------------------------------------------------===//
105 // TypeRangeType
106 //===----------------------------------------------------------------------===//
107 
109  RangeType range = mlir::dyn_cast<RangeType>(type);
110  return range && mlir::isa<TypeType>(range.getElementType());
111 }
112 
114  return mlir::cast<TypeRangeType>(
115  RangeType::get(context, TypeType::get(context)));
116 }
117 
118 //===----------------------------------------------------------------------===//
119 // ValueRangeType
120 //===----------------------------------------------------------------------===//
121 
123  RangeType range = mlir::dyn_cast<RangeType>(type);
124  return range && mlir::isa<ValueType>(range.getElementType());
125 }
126 
128  return mlir::cast<ValueRangeType>(
129  RangeType::get(context, ValueType::get(context)));
130 }
131 
132 //===----------------------------------------------------------------------===//
133 // RewriteType
134 //===----------------------------------------------------------------------===//
135 
137  return context.getTypeUniquer().get<ImplTy>();
138 }
139 
140 //===----------------------------------------------------------------------===//
141 // TupleType
142 //===----------------------------------------------------------------------===//
143 
145  ArrayRef<StringRef> elementNames) {
146  assert(elementTypes.size() == elementNames.size());
147  return context.getTypeUniquer().get<ImplTy>(
148  /*initFn=*/function_ref<void(ImplTy *)>(), elementTypes, elementNames);
149 }
151  SmallVector<StringRef> elementNames(elementTypes.size());
152  return get(context, elementTypes, elementNames);
153 }
154 
156  return getImplAs<ImplTy>()->getValue().first;
157 }
158 
160  return getImplAs<ImplTy>()->getValue().second;
161 }
162 
163 //===----------------------------------------------------------------------===//
164 // TypeType
165 //===----------------------------------------------------------------------===//
166 
168  return context.getTypeUniquer().get<ImplTy>();
169 }
170 
171 //===----------------------------------------------------------------------===//
172 // ValueType
173 //===----------------------------------------------------------------------===//
174 
176  return context.getTypeUniquer().get<ImplTy>();
177 }
#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: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
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: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
ArrayRef< StringRef > getElementNames() const
Return the element names of this tuple.
Definition: Types.cpp:159
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
Type refineWith(Type other) const
Try to refine this type with the one provided.
Definition: Types.cpp:33
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
Include the generated interface declarations.