MLIR  16.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 
13 using namespace mlir;
14 using namespace mlir::pdll;
15 using namespace mlir::pdll::ast;
16 
25 
26 //===----------------------------------------------------------------------===//
27 // Type
28 //===----------------------------------------------------------------------===//
29 
30 TypeID Type::getTypeID() const { return impl->typeID; }
31 
32 Type Type::refineWith(Type other) const {
33  if (*this == other)
34  return *this;
35 
36  // Operation types are compatible if the operation names don't conflict.
37  if (auto opTy = dyn_cast<OperationType>()) {
38  auto otherOpTy = other.dyn_cast<ast::OperationType>();
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  const ods::Operation *odsOp) {
74  return context.getTypeUniquer().get<ImplTy>(
75  /*initFn=*/function_ref<void(ImplTy *)>(),
76  std::make_pair(name.value_or(""), odsOp));
77 }
78 
80  StringRef name = getImplAs<ImplTy>()->getValue().first;
81  return name.empty() ? Optional<StringRef>() : Optional<StringRef>(name);
82 }
83 
85  return getImplAs<ImplTy>()->getValue().second;
86 }
87 
88 //===----------------------------------------------------------------------===//
89 // RangeType
90 //===----------------------------------------------------------------------===//
91 
92 RangeType RangeType::get(Context &context, Type elementType) {
93  return context.getTypeUniquer().get<ImplTy>(
94  /*initFn=*/function_ref<void(ImplTy *)>(), elementType);
95 }
96 
98  return getImplAs<ImplTy>()->getValue();
99 }
100 
101 //===----------------------------------------------------------------------===//
102 // TypeRangeType
103 
105  RangeType range = type.dyn_cast<RangeType>();
106  return range && range.getElementType().isa<TypeType>();
107 }
108 
110  return RangeType::get(context, TypeType::get(context)).cast<TypeRangeType>();
111 }
112 
113 //===----------------------------------------------------------------------===//
114 // ValueRangeType
115 
117  RangeType range = type.dyn_cast<RangeType>();
118  return range && range.getElementType().isa<ValueType>();
119 }
120 
122  return RangeType::get(context, ValueType::get(context))
123  .cast<ValueRangeType>();
124 }
125 
126 //===----------------------------------------------------------------------===//
127 // RewriteType
128 //===----------------------------------------------------------------------===//
129 
131  return context.getTypeUniquer().get<ImplTy>();
132 }
133 
134 //===----------------------------------------------------------------------===//
135 // TupleType
136 //===----------------------------------------------------------------------===//
137 
139  ArrayRef<StringRef> elementNames) {
140  assert(elementTypes.size() == elementNames.size());
141  return context.getTypeUniquer().get<ImplTy>(
142  /*initFn=*/function_ref<void(ImplTy *)>(), elementTypes, elementNames);
143 }
145  SmallVector<StringRef> elementNames(elementTypes.size());
146  return get(context, elementTypes, elementNames);
147 }
148 
150  return getImplAs<ImplTy>()->getValue().first;
151 }
152 
154  return getImplAs<ImplTy>()->getValue().second;
155 }
156 
157 //===----------------------------------------------------------------------===//
158 // TypeType
159 //===----------------------------------------------------------------------===//
160 
162  return context.getTypeUniquer().get<ImplTy>();
163 }
164 
165 //===----------------------------------------------------------------------===//
166 // ValueType
167 //===----------------------------------------------------------------------===//
168 
170  return context.getTypeUniquer().get<ImplTy>();
171 }
#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:130
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:144
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:157
static OperationType get(Context &context, Optional< StringRef > name=llvm::None, const ods::Operation *odsOp=nullptr)
Return an instance of the Operation type with an optional operation name.
Definition: Types.cpp:72
Optional< StringRef > getName() const
Return the name of this operation type, or None if it doesn't have on.
Definition: Types.cpp:79
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:84
This class represents a PDLL type that corresponds to a range of elements with a given element type.
Definition: Types.h:181
Type getElementType() const
Return the element type of this range.
Definition: Types.cpp:97
static RangeType get(Context &context, Type elementType)
Return an instance of the Range type with the given element type.
Definition: Types.cpp:92
This class represents a PDLL type that corresponds to a rewrite reference.
Definition: Types.h:228
static RewriteType get(Context &context)
Return an instance of the Rewrite type.
Definition: Types.cpp:130
This class represents a PDLL tuple type, i.e.
Definition: Types.h:242
ArrayRef< StringRef > getElementNames() const
Return the element names of this tuple.
Definition: Types.cpp:153
ArrayRef< Type > getElementTypes() const
Return the element types of this tuple.
Definition: Types.cpp:149
static TupleType get(Context &context, ArrayRef< Type > elementTypes, ArrayRef< StringRef > elementNames)
Return an instance of the Tuple type.
Definition: Types.cpp:138
This class represents a PDLL type that corresponds to an mlir::TypeRange.
Definition: Types.h:196
static TypeRangeType get(Context &context)
Return an instance of the TypeRange type.
Definition: Types.cpp:109
static bool classof(Type type)
Provide type casting support.
Definition: Types.cpp:104
This class represents a PDLL type that corresponds to an mlir::Type.
Definition: Types.h:270
static TypeType get(Context &context)
Return an instance of the Type type.
Definition: Types.cpp:161
U dyn_cast() const
Definition: Types.h:75
U cast() const
Definition: Types.h:83
Type refineWith(Type other) const
Try to refine this type with the one provided.
Definition: Types.cpp:32
bool isa() const
Provide type casting support.
Definition: Types.h:66
This class represents a PDLL type that corresponds to an mlir::ValueRange.
Definition: Types.h:211
static bool classof(Type type)
Provide type casting support.
Definition: Types.cpp:116
static ValueRangeType get(Context &context)
Return an instance of the ValueRange type.
Definition: Types.cpp:121
This class represents a PDLL type that corresponds to an mlir::Value.
Definition: Types.h:283
static ValueType get(Context &context)
Return an instance of the Value type.
Definition: Types.cpp:169
This class provides an ODS representation of a specific operation.
Definition: Operation.h:125
Include the generated interface declarations.