MLIR  19.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 
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 
121  RangeType range = mlir::dyn_cast<RangeType>(type);
122  return range && mlir::isa<ValueType>(range.getElementType());
123 }
124 
126  return mlir::cast<ValueRangeType>(
127  RangeType::get(context, ValueType::get(context)));
128 }
129 
130 //===----------------------------------------------------------------------===//
131 // RewriteType
132 //===----------------------------------------------------------------------===//
133 
135  return context.getTypeUniquer().get<ImplTy>();
136 }
137 
138 //===----------------------------------------------------------------------===//
139 // TupleType
140 //===----------------------------------------------------------------------===//
141 
143  ArrayRef<StringRef> elementNames) {
144  assert(elementTypes.size() == elementNames.size());
145  return context.getTypeUniquer().get<ImplTy>(
146  /*initFn=*/function_ref<void(ImplTy *)>(), elementTypes, elementNames);
147 }
149  SmallVector<StringRef> elementNames(elementTypes.size());
150  return get(context, elementTypes, elementNames);
151 }
152 
154  return getImplAs<ImplTy>()->getValue().first;
155 }
156 
158  return getImplAs<ImplTy>()->getValue().second;
159 }
160 
161 //===----------------------------------------------------------------------===//
162 // TypeType
163 //===----------------------------------------------------------------------===//
164 
166  return context.getTypeUniquer().get<ImplTy>();
167 }
168 
169 //===----------------------------------------------------------------------===//
170 // ValueType
171 //===----------------------------------------------------------------------===//
172 
174  return context.getTypeUniquer().get<ImplTy>();
175 }
#define MLIR_DEFINE_EXPLICIT_TYPE_ID(CLASS_NAME)
Definition: TypeID.h:263
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:104
This class represents a PDLL type that corresponds to an mlir::Attribute.
Definition: Types.h:136
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:150
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:163
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:188
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:235
static RewriteType get(Context &context)
Return an instance of the Rewrite type.
Definition: Types.cpp:134
This class represents a PDLL tuple type, i.e.
Definition: Types.h:249
ArrayRef< StringRef > getElementNames() const
Return the element names of this tuple.
Definition: Types.cpp:157
ArrayRef< Type > getElementTypes() const
Return the element types of this tuple.
Definition: Types.cpp:153
static TupleType get(Context &context, ArrayRef< Type > elementTypes, ArrayRef< StringRef > elementNames)
Return an instance of the Tuple type.
Definition: Types.cpp:142
This class represents a PDLL type that corresponds to an mlir::TypeRange.
Definition: Types.h:203
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:277
static TypeType get(Context &context)
Return an instance of the Type type.
Definition: Types.cpp:165
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:218
static bool classof(Type type)
Provide type casting support.
Definition: Types.cpp:120
static ValueRangeType get(Context &context)
Return an instance of the ValueRange type.
Definition: Types.cpp:125
This class represents a PDLL type that corresponds to an mlir::Value.
Definition: Types.h:290
static ValueType get(Context &context)
Return an instance of the Value type.
Definition: Types.cpp:173
This class provides an ODS representation of a specific operation.
Definition: Operation.h:125
Include the generated interface declarations.