xref: /llvm-project/mlir/include/mlir/Tools/PDLL/ODS/Operation.h (revision db3013d684ddff31a7ee97b2ba499eac29ac8704)
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 
20 namespace mlir {
21 namespace pdll {
22 namespace ods {
23 class AttributeConstraint;
24 class TypeConstraint;
25 class Dialect;
26 
27 //===----------------------------------------------------------------------===//
28 // VariableLengthKind
29 //===----------------------------------------------------------------------===//
30 
31 enum VariableLengthKind { Single, Optional, Variadic };
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.
39 class Attribute {
40 public:
41   /// Return the name of this operand.
getName()42   StringRef getName() const { return name; }
43 
44   /// Return true if this attribute is optional.
isOptional()45   bool isOptional() const { return optional; }
46 
47   /// Return the constraint of this attribute.
getConstraint()48   const AttributeConstraint &getConstraint() const { return constraint; }
49 
50 private:
Attribute(StringRef name,bool optional,const AttributeConstraint & constraint)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.
74 class OperandOrResult {
75 public:
76   /// Return the name of this value.
getName()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.
isVariableLength()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).
isVariadic()87   bool isVariadic() const {
88     return variableLengthKind == VariableLengthKind::Variadic;
89   }
90 
91   /// Returns the variable length kind of this value.
getVariableLengthKind()92   VariableLengthKind getVariableLengthKind() const {
93     return variableLengthKind;
94   }
95 
96   /// Return the constraint of this value.
getConstraint()97   const TypeConstraint &getConstraint() const { return constraint; }
98 
99 private:
OperandOrResult(StringRef name,VariableLengthKind variableLengthKind,const TypeConstraint & constraint)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.
125 class Operation {
126 public:
127   /// Return the source location of this operation.
getLoc()128   SMRange getLoc() const { return location; }
129 
130   /// Append an attribute to this operation.
appendAttribute(StringRef name,bool optional,const AttributeConstraint & constraint)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.
appendOperand(StringRef name,VariableLengthKind variableLengthKind,const TypeConstraint & constraint)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.
appendResult(StringRef name,VariableLengthKind variableLengthKind,const TypeConstraint & constraint)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.
getName()150   StringRef getName() const { return name; }
151 
152   /// Returns the summary of the operation.
getSummary()153   StringRef getSummary() const { return summary; }
154 
155   /// Returns the description of the operation.
getDescription()156   StringRef getDescription() const { return description; }
157 
158   /// Returns the native class name of the operation.
getNativeClassName()159   StringRef getNativeClassName() const { return nativeClassName; }
160 
161   /// Returns the attributes of this operation.
getAttributes()162   ArrayRef<Attribute> getAttributes() const { return attributes; }
163 
164   /// Returns the operands of this operation.
getOperands()165   ArrayRef<OperandOrResult> getOperands() const { return operands; }
166 
167   /// Returns the results of this operation.
getResults()168   ArrayRef<OperandOrResult> getResults() const { return results; }
169 
170   /// Return if the operation is known to support result type inferrence.
hasResultTypeInferrence()171   bool hasResultTypeInferrence() const { return supportsTypeInferrence; }
172 
173 private:
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.
194   SmallVector<OperandOrResult> operands;
195 
196   /// The results of the operation.
197   SmallVector<OperandOrResult> results;
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_
210