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 = dyn_cast<OperationType>()) {
39  auto otherOpTy = other.dyn_cast<ast::OperationType>();
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 = type.dyn_cast<RangeType>();
109  return range && range.getElementType().isa<TypeType>();
110 }
111 
113  return RangeType::get(context, TypeType::get(context)).cast<TypeRangeType>();
114 }
115 
116 //===----------------------------------------------------------------------===//
117 // ValueRangeType
118 
120  RangeType range = type.dyn_cast<RangeType>();
121  return range && range.getElementType().isa<ValueType>();
122 }
123 
125  return RangeType::get(context, ValueType::get(context))
126  .cast<ValueRangeType>();
127 }
128 
129 //===----------------------------------------------------------------------===//
130 // RewriteType
131 //===----------------------------------------------------------------------===//
132 
134  return context.getTypeUniquer().get<ImplTy>();
135 }
136 
137 //===----------------------------------------------------------------------===//
138 // TupleType
139 //===----------------------------------------------------------------------===//
140 
142  ArrayRef<StringRef> elementNames) {
143  assert(elementTypes.size() == elementNames.size());
144  return context.getTypeUniquer().get<ImplTy>(
145  /*initFn=*/function_ref<void(ImplTy *)>(), elementTypes, elementNames);
146 }
148  SmallVector<StringRef> elementNames(elementTypes.size());
149  return get(context, elementTypes, elementNames);
150 }
151 
153  return getImplAs<ImplTy>()->getValue().first;
154 }
155 
157  return getImplAs<ImplTy>()->getValue().second;
158 }
159 
160 //===----------------------------------------------------------------------===//
161 // TypeType
162 //===----------------------------------------------------------------------===//
163 
165  return context.getTypeUniquer().get<ImplTy>();
166 }
167 
168 //===----------------------------------------------------------------------===//
169 // ValueType
170 //===----------------------------------------------------------------------===//
171 
173  return context.getTypeUniquer().get<ImplTy>();
174 }
#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:131
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:145
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:158
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:183
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:230
static RewriteType get(Context &context)
Return an instance of the Rewrite type.
Definition: Types.cpp:133
This class represents a PDLL tuple type, i.e.
Definition: Types.h:244
ArrayRef< StringRef > getElementNames() const
Return the element names of this tuple.
Definition: Types.cpp:156
ArrayRef< Type > getElementTypes() const
Return the element types of this tuple.
Definition: Types.cpp:152
static TupleType get(Context &context, ArrayRef< Type > elementTypes, ArrayRef< StringRef > elementNames)
Return an instance of the Tuple type.
Definition: Types.cpp:141
This class represents a PDLL type that corresponds to an mlir::TypeRange.
Definition: Types.h:198
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:272
static TypeType get(Context &context)
Return an instance of the Type type.
Definition: Types.cpp:164
U dyn_cast() const
Definition: Types.h:76
U cast() const
Definition: Types.h:84
Type refineWith(Type other) const
Try to refine this type with the one provided.
Definition: Types.cpp:33
bool isa() const
Provide type casting support.
Definition: Types.h:67
This class represents a PDLL type that corresponds to an mlir::ValueRange.
Definition: Types.h:213
static bool classof(Type type)
Provide type casting support.
Definition: Types.cpp:119
static ValueRangeType get(Context &context)
Return an instance of the ValueRange type.
Definition: Types.cpp:124
This class represents a PDLL type that corresponds to an mlir::Value.
Definition: Types.h:285
static ValueType get(Context &context)
Return an instance of the Value type.
Definition: Types.cpp:172
This class provides an ODS representation of a specific operation.
Definition: Operation.h:125
Include the generated interface declarations.