MLIR  19.0.0git
IRDLVerifiers.h
Go to the documentation of this file.
1 //===- IRDLVerifiers.h - IRDL verifiers --------------------------- C++ -*-===//
2 //
3 // This file is licensed 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 // Verifiers for objects declared by IRDL.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_DIALECT_IRDL_IRDLVERIFIERS_H
14 #define MLIR_DIALECT_IRDL_IRDLVERIFIERS_H
15 
16 #include "mlir/IR/Attributes.h"
17 #include "mlir/IR/Region.h"
18 #include "mlir/Support/LLVM.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include <optional>
22 
23 namespace mlir {
24 struct LogicalResult;
25 class InFlightDiagnostic;
26 class DynamicAttrDefinition;
27 class DynamicTypeDefinition;
28 } // namespace mlir
29 
30 namespace mlir {
31 namespace irdl {
32 
33 class Constraint;
34 
35 /// Provides context to the verification of constraints.
36 /// It contains the assignment of variables to attributes, and the assignment
37 /// of variables to constraints.
39 public:
40  ConstraintVerifier(ArrayRef<std::unique_ptr<Constraint>> constraints);
41 
42  /// Check that a constraint is satisfied by an attribute.
43  ///
44  /// Constraints may call other constraint verifiers. If that is the case,
45  /// the constraint verifier will check if the variable is already assigned,
46  /// and if so, check that the attribute is the same as the one assigned.
47  /// If the variable is not assigned, the constraint verifier will
48  /// assign the attribute to the variable, and check that the constraint
49  /// is satisfied.
51  Attribute attr, unsigned variable);
52 
53 private:
54  /// The constraints that can be used for verification.
56 
57  /// The assignment of variables to attributes. Variables that are not assigned
58  /// are represented by nullopt. Null attributes needs to be supported here as
59  /// some attributes or types might use the null attribute to represent
60  /// optional parameters.
62 };
63 
64 /// Once turned into IRDL verifiers, all constraints are
65 /// attribute constraints. Type constraints are represented
66 /// as `TypeAttr` attribute constraints to simplify verification.
67 /// Verification that a type constraint must yield a
68 /// `TypeAttr` attribute happens before conversion, at the MLIR level.
69 class Constraint {
70 public:
71  virtual ~Constraint() = default;
72 
73  /// Check that an attribute is satisfying the constraint.
74  ///
75  /// Constraints may call other constraint verifiers. If that is the case,
76  /// the constraint verifier will check if the variable is already assigned,
77  /// and if so, check that the attribute is the same as the one assigned.
78  /// If the variable is not assigned, the constraint verifier will
79  /// assign the attribute to the variable, and check that the constraint
80  /// is satisfied.
82  Attribute attr,
83  ConstraintVerifier &context) const = 0;
84 };
85 
86 /// A constraint that checks that an attribute is equal to a given attribute.
87 class IsConstraint : public Constraint {
88 public:
89  IsConstraint(Attribute expectedAttribute)
90  : expectedAttribute(expectedAttribute) {}
91 
92  virtual ~IsConstraint() = default;
93 
95  Attribute attr,
96  ConstraintVerifier &context) const override;
97 
98 private:
99  Attribute expectedAttribute;
100 };
101 
102 /// A constraint that checks that an attribute is of a given attribute base
103 /// (e.g. IntegerAttr).
105 public:
106  BaseAttrConstraint(TypeID baseTypeID, StringRef baseName)
107  : baseTypeID(baseTypeID), baseName(baseName) {}
108 
109  virtual ~BaseAttrConstraint() = default;
110 
112  Attribute attr,
113  ConstraintVerifier &context) const override;
114 
115 private:
116  /// The expected base attribute typeID.
117  TypeID baseTypeID;
118 
119  /// The base attribute name, only used for error reporting.
120  StringRef baseName;
121 };
122 
123 /// A constraint that checks that a type is of a given type base (e.g.
124 /// IntegerType).
126 public:
127  BaseTypeConstraint(TypeID baseTypeID, StringRef baseName)
128  : baseTypeID(baseTypeID), baseName(baseName) {}
129 
130  virtual ~BaseTypeConstraint() = default;
131 
133  Attribute attr,
134  ConstraintVerifier &context) const override;
135 
136 private:
137  /// The expected base type typeID.
138  TypeID baseTypeID;
139 
140  /// The base type name, only used for error reporting.
141  StringRef baseName;
142 };
143 
144 /// A constraint that checks that an attribute is of a
145 /// specific dynamic attribute definition, and that all of its parameters
146 /// satisfy the given constraints.
148 public:
150  SmallVector<unsigned> constraints)
151  : attrDef(attrDef), constraints(std::move(constraints)) {}
152 
153  virtual ~DynParametricAttrConstraint() = default;
154 
156  Attribute attr,
157  ConstraintVerifier &context) const override;
158 
159 private:
160  DynamicAttrDefinition *attrDef;
161  SmallVector<unsigned> constraints;
162 };
163 
164 /// A constraint that checks that a type is of a specific dynamic type
165 /// definition, and that all of its parameters satisfy the given constraints.
167 public:
169  SmallVector<unsigned> constraints)
170  : typeDef(typeDef), constraints(std::move(constraints)) {}
171 
172  virtual ~DynParametricTypeConstraint() = default;
173 
175  Attribute attr,
176  ConstraintVerifier &context) const override;
177 
178 private:
179  DynamicTypeDefinition *typeDef;
180  SmallVector<unsigned> constraints;
181 };
182 
183 /// A constraint checking that one of the given constraints is satisfied.
184 class AnyOfConstraint : public Constraint {
185 public:
187  : constraints(std::move(constraints)) {}
188 
189  virtual ~AnyOfConstraint() = default;
190 
192  Attribute attr,
193  ConstraintVerifier &context) const override;
194 
195 private:
196  SmallVector<unsigned> constraints;
197 };
198 
199 /// A constraint checking that all of the given constraints are satisfied.
200 class AllOfConstraint : public Constraint {
201 public:
203  : constraints(std::move(constraints)) {}
204 
205  virtual ~AllOfConstraint() = default;
206 
208  Attribute attr,
209  ConstraintVerifier &context) const override;
210 
211 private:
212  SmallVector<unsigned> constraints;
213 };
214 
215 /// A constraint that is always satisfied.
217 public:
218  virtual ~AnyAttributeConstraint() = default;
219 
221  Attribute attr,
222  ConstraintVerifier &context) const override;
223 };
224 
225 /// A constraint checking that a region satisfies `irdl.region` requirements
227  /// The constructor accepts constrained entities from the `irdl.region`
228  /// operation, such as slots of constraints for the region's arguments and the
229  /// block count.
230 
231  // Both entities are optional, which means if an entity is not present, then
232  // it is not constrained.
233  RegionConstraint(std::optional<SmallVector<unsigned>> argumentConstraints,
234  std::optional<size_t> blockCount)
235  : argumentConstraints(std::move(argumentConstraints)),
236  blockCount(blockCount) {}
237 
238  /// Check that the `region` satisfies the constraint.
239  ///
240  /// `constraintContext` is needed to verify the region's arguments
241  /// constraints.
243  ConstraintVerifier &constraintContext);
244 
245 private:
246  std::optional<SmallVector<unsigned>> argumentConstraints;
247  std::optional<size_t> blockCount;
248 };
249 } // namespace irdl
250 } // namespace mlir
251 
252 #endif // MLIR_DIALECT_IRDL_IRDLVERIFIERS_H
Attributes are known-constant values of operations.
Definition: Attributes.h:25
The definition of a dynamic attribute.
The definition of a dynamic type.
This class represents a diagnostic that is inflight and set to be reported.
Definition: Diagnostics.h:308
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition: Region.h:26
This class provides an efficient unique identifier for a specific C++ type.
Definition: TypeID.h:104
A constraint checking that all of the given constraints are satisfied.
virtual ~AllOfConstraint()=default
AllOfConstraint(SmallVector< unsigned > constraints)
LogicalResult verify(function_ref< InFlightDiagnostic()> emitError, Attribute attr, ConstraintVerifier &context) const override
Check that an attribute is satisfying the constraint.
A constraint that is always satisfied.
virtual ~AnyAttributeConstraint()=default
LogicalResult verify(function_ref< InFlightDiagnostic()> emitError, Attribute attr, ConstraintVerifier &context) const override
Check that an attribute is satisfying the constraint.
A constraint checking that one of the given constraints is satisfied.
AnyOfConstraint(SmallVector< unsigned > constraints)
virtual ~AnyOfConstraint()=default
LogicalResult verify(function_ref< InFlightDiagnostic()> emitError, Attribute attr, ConstraintVerifier &context) const override
Check that an attribute is satisfying the constraint.
A constraint that checks that an attribute is of a given attribute base (e.g.
LogicalResult verify(function_ref< InFlightDiagnostic()> emitError, Attribute attr, ConstraintVerifier &context) const override
Check that an attribute is satisfying the constraint.
virtual ~BaseAttrConstraint()=default
BaseAttrConstraint(TypeID baseTypeID, StringRef baseName)
A constraint that checks that a type is of a given type base (e.g.
virtual ~BaseTypeConstraint()=default
BaseTypeConstraint(TypeID baseTypeID, StringRef baseName)
LogicalResult verify(function_ref< InFlightDiagnostic()> emitError, Attribute attr, ConstraintVerifier &context) const override
Check that an attribute is satisfying the constraint.
Provides context to the verification of constraints.
Definition: IRDLVerifiers.h:38
ConstraintVerifier(ArrayRef< std::unique_ptr< Constraint >> constraints)
LogicalResult verify(function_ref< InFlightDiagnostic()> emitError, Attribute attr, unsigned variable)
Check that a constraint is satisfied by an attribute.
Once turned into IRDL verifiers, all constraints are attribute constraints.
Definition: IRDLVerifiers.h:69
virtual ~Constraint()=default
virtual LogicalResult verify(function_ref< InFlightDiagnostic()> emitError, Attribute attr, ConstraintVerifier &context) const =0
Check that an attribute is satisfying the constraint.
A constraint that checks that an attribute is of a specific dynamic attribute definition,...
virtual ~DynParametricAttrConstraint()=default
LogicalResult verify(function_ref< InFlightDiagnostic()> emitError, Attribute attr, ConstraintVerifier &context) const override
Check that an attribute is satisfying the constraint.
DynParametricAttrConstraint(DynamicAttrDefinition *attrDef, SmallVector< unsigned > constraints)
A constraint that checks that a type is of a specific dynamic type definition, and that all of its pa...
virtual ~DynParametricTypeConstraint()=default
DynParametricTypeConstraint(DynamicTypeDefinition *typeDef, SmallVector< unsigned > constraints)
LogicalResult verify(function_ref< InFlightDiagnostic()> emitError, Attribute attr, ConstraintVerifier &context) const override
Check that an attribute is satisfying the constraint.
A constraint that checks that an attribute is equal to a given attribute.
Definition: IRDLVerifiers.h:87
LogicalResult verify(function_ref< InFlightDiagnostic()> emitError, Attribute attr, ConstraintVerifier &context) const override
Check that an attribute is satisfying the constraint.
IsConstraint(Attribute expectedAttribute)
Definition: IRDLVerifiers.h:89
virtual ~IsConstraint()=default
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
A constraint checking that a region satisfies irdl.region requirements.
LogicalResult verify(mlir::Region &region, ConstraintVerifier &constraintContext)
Check that the region satisfies the constraint.
RegionConstraint(std::optional< SmallVector< unsigned >> argumentConstraints, std::optional< size_t > blockCount)
The constructor accepts constrained entities from the irdl.region operation, such as slots of constra...