MLIR 22.0.0git
Operation.h
Go to the documentation of this file.
1//===- Operation.h - MLIR PDLL ODS Operation --------------------*- C++ -*-===//
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
9#ifndef MLIR_TOOLS_PDLL_ODS_OPERATION_H_
10#define MLIR_TOOLS_PDLL_ODS_OPERATION_H_
11
12#include <string>
13
14#include "mlir/Support/LLVM.h"
15#include "llvm/ADT/ArrayRef.h"
16#include "llvm/ADT/SmallVector.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/Support/SMLoc.h"
19
20namespace mlir {
21namespace pdll {
22namespace ods {
24class TypeConstraint;
25class Dialect;
26
27//===----------------------------------------------------------------------===//
28// VariableLengthKind
29//===----------------------------------------------------------------------===//
30
32
33//===----------------------------------------------------------------------===//
34// Attribute
35//===----------------------------------------------------------------------===//
36
37/// This class provides an ODS representation of a specific operation attribute.
38/// This includes the name, optionality, and more.
39class Attribute {
40public:
41 /// Return the name of this operand.
42 StringRef getName() const { return name; }
43
44 /// Return true if this attribute is optional.
45 bool isOptional() const { return optional; }
46
47 /// Return the constraint of this attribute.
48 const AttributeConstraint &getConstraint() const { return constraint; }
49
50private:
51 Attribute(StringRef name, bool optional,
52 const AttributeConstraint &constraint)
53 : name(name.str()), optional(optional), constraint(constraint) {}
54
55 /// The ODS name of the attribute.
56 std::string name;
57
58 /// A flag indicating if the attribute is optional.
59 bool optional;
60
61 /// The ODS constraint of this attribute.
62 const AttributeConstraint &constraint;
63
64 /// Allow access to the private constructor.
65 friend class Operation;
66};
67
68//===----------------------------------------------------------------------===//
69// OperandOrResult
70//===----------------------------------------------------------------------===//
71
72/// This class provides an ODS representation of a specific operation operand or
73/// result. This includes the name, variable length flags, and more.
74class OperandOrResult {
75public:
76 /// Return the name of this value.
77 StringRef getName() const { return name; }
78
79 /// Returns true if this value is variable length, i.e. if it is Variadic or
80 /// Optional.
81 bool isVariableLength() const {
82 return variableLengthKind != VariableLengthKind::Single;
83 }
84
85 /// Returns true if this value is variadic (Note this is false if the value is
86 /// Optional).
87 bool isVariadic() const {
88 return variableLengthKind == VariableLengthKind::Variadic;
89 }
90
91 /// Returns the variable length kind of this value.
93 return variableLengthKind;
94 }
95
96 /// Return the constraint of this value.
97 const TypeConstraint &getConstraint() const { return constraint; }
98
99private:
100 OperandOrResult(StringRef name, VariableLengthKind variableLengthKind,
101 const TypeConstraint &constraint)
102 : name(name.str()), variableLengthKind(variableLengthKind),
103 constraint(constraint) {}
104
105 /// The ODS name of this value.
106 std::string name;
107
108 /// The variable length kind of this value.
109 VariableLengthKind variableLengthKind;
110
111 /// The ODS constraint of this value.
112 const TypeConstraint &constraint;
113
114 /// Allow access to the private constructor.
115 friend class Operation;
116};
117
118//===----------------------------------------------------------------------===//
119// Operation
120//===----------------------------------------------------------------------===//
121
122/// This class provides an ODS representation of a specific operation. This
123/// includes all of the information necessary for use by the PDL frontend for
124/// generating code for a pattern rewrite.
125class Operation {
126public:
127 /// Return the source location of this operation.
128 SMRange getLoc() const { return location; }
129
130 /// Append an attribute to this operation.
131 void appendAttribute(StringRef name, bool optional,
132 const AttributeConstraint &constraint) {
133 attributes.emplace_back(Attribute(name, optional, constraint));
134 }
135
136 /// Append an operand to this operation.
137 void appendOperand(StringRef name, VariableLengthKind variableLengthKind,
138 const TypeConstraint &constraint) {
139 operands.emplace_back(
140 OperandOrResult(name, variableLengthKind, constraint));
141 }
142
143 /// Append a result to this operation.
144 void appendResult(StringRef name, VariableLengthKind variableLengthKind,
145 const TypeConstraint &constraint) {
146 results.emplace_back(OperandOrResult(name, variableLengthKind, constraint));
147 }
148
149 /// Returns the name of the operation.
150 StringRef getName() const { return name; }
151
152 /// Returns the summary of the operation.
153 StringRef getSummary() const { return summary; }
154
155 /// Returns the description of the operation.
156 StringRef getDescription() const { return description; }
157
158 /// Returns the native class name of the operation.
159 StringRef getNativeClassName() const { return nativeClassName; }
160
161 /// Returns the attributes of this operation.
162 ArrayRef<Attribute> getAttributes() const { return attributes; }
163
164 /// Returns the operands of this operation.
165 ArrayRef<OperandOrResult> getOperands() const { return operands; }
166
167 /// Returns the results of this operation.
168 ArrayRef<OperandOrResult> getResults() const { return results; }
169
170 /// Return if the operation is known to support result type inferrence.
171 bool hasResultTypeInferrence() const { return supportsTypeInferrence; }
172
173private:
174 Operation(StringRef name, StringRef summary, StringRef desc,
175 StringRef nativeClassName, bool supportsTypeInferrence, SMLoc loc);
176
177 /// The name of the operation.
178 std::string name;
179
180 /// The documentation of the operation.
181 std::string summary;
182 std::string description;
183
184 /// The native class name of the operation, used when generating native code.
185 std::string nativeClassName;
186
187 /// Flag indicating if the operation is known to support type inferrence.
188 bool supportsTypeInferrence;
189
190 /// The source location of this operation.
191 SMRange location;
192
193 /// The operands of the operation.
195
196 /// The results of the operation.
198
199 /// The attributes of the operation.
200 SmallVector<Attribute> attributes;
201
202 /// Allow access to the private constructor.
203 friend class Dialect;
204};
205} // namespace ods
206} // namespace pdll
207} // namespace mlir
208
209#endif // MLIR_TOOLS_PDLL_ODS_OPERATION_H_
This class represents a generic ODS Attribute constraint.
Definition Constraint.h:63
This class provides an ODS representation of a specific operation attribute.
Definition Operation.h:39
StringRef getName() const
Return the name of this operand.
Definition Operation.h:42
friend class Operation
Allow access to the private constructor.
Definition Operation.h:65
const AttributeConstraint & getConstraint() const
Return the constraint of this attribute.
Definition Operation.h:48
bool isOptional() const
Return true if this attribute is optional.
Definition Operation.h:45
This class represents an ODS dialect, and contains information on the constructs held within the dial...
Definition Dialect.h:26
This class provides an ODS representation of a specific operation operand or result.
Definition Operation.h:74
friend class Operation
Allow access to the private constructor.
Definition Operation.h:115
const TypeConstraint & getConstraint() const
Return the constraint of this value.
Definition Operation.h:97
bool isVariadic() const
Returns true if this value is variadic (Note this is false if the value is Optional).
Definition Operation.h:87
VariableLengthKind getVariableLengthKind() const
Returns the variable length kind of this value.
Definition Operation.h:92
StringRef getName() const
Return the name of this value.
Definition Operation.h:77
bool isVariableLength() const
Returns true if this value is variable length, i.e.
Definition Operation.h:81
This class provides an ODS representation of a specific operation.
Definition Operation.h:125
StringRef getDescription() const
Returns the description of the operation.
Definition Operation.h:156
StringRef getSummary() const
Returns the summary of the operation.
Definition Operation.h:153
ArrayRef< OperandOrResult > getOperands() const
Returns the operands of this operation.
Definition Operation.h:165
StringRef getName() const
Returns the name of the operation.
Definition Operation.h:150
SMRange getLoc() const
Return the source location of this operation.
Definition Operation.h:128
void appendResult(StringRef name, VariableLengthKind variableLengthKind, const TypeConstraint &constraint)
Append a result to this operation.
Definition Operation.h:144
StringRef getNativeClassName() const
Returns the native class name of the operation.
Definition Operation.h:159
void appendOperand(StringRef name, VariableLengthKind variableLengthKind, const TypeConstraint &constraint)
Append an operand to this operation.
Definition Operation.h:137
ArrayRef< Attribute > getAttributes() const
Returns the attributes of this operation.
Definition Operation.h:162
bool hasResultTypeInferrence() const
Return if the operation is known to support result type inferrence.
Definition Operation.h:171
void appendAttribute(StringRef name, bool optional, const AttributeConstraint &constraint)
Append an attribute to this operation.
Definition Operation.h:131
friend class Dialect
Allow access to the private constructor.
Definition Operation.h:203
ArrayRef< OperandOrResult > getResults() const
Returns the results of this operation.
Definition Operation.h:168
This class represents a generic ODS Type constraint.
Definition Constraint.h:84
Include the generated interface declarations.