Skip to content

Commit e578314

Browse files
authored
[MLIR Attr] add ArrayMaxCount attribute constraint (llvm#92453)
this is the dual of ArrayMinCount. I saw that I needed it but it didn't exist yet
1 parent c796900 commit e578314

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

mlir/docs/DefiningDialects/Operations.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,27 @@ Right now, the following primitive constraints are supported:
291291
equal to `N`
292292
* `IntMaxValue<N>`: Specifying an integer attribute to be less than or equal
293293
to `N`
294+
* `IntNEQValue<N>`: Specifying an integer attribute to be not equal
295+
to `N`
296+
* `IntPositive`: Specifying an integer attribute whose value is positive
297+
* `IntNonNegative`: Specifying an integer attribute whose value is
298+
non-negative
294299
* `ArrayMinCount<N>`: Specifying an array attribute to have at least `N`
295300
elements
301+
* `ArrayMaxCount<N>`: Specifying an array attribute to have at most `N`
302+
elements
303+
* `ArrayCount<N>`: Specifying an array attribute to have exactly `N`
304+
elements
305+
* `DenseArrayCount<N>`: Specifying a dense array attribute to have
306+
exactly `N` elements
307+
* `DenseArrayStrictlyPositive<arrayType>`: Specifying a dense array attribute
308+
of type `arrayType` to have all positive elements
309+
* `DenseArrayStrictlyNonNegative<arrayType>`: Specifying a dense array attribute
310+
of type `arrayType` to have all non-negative elements
311+
* `DenseArraySorted<arrayType>`: Specifying a dense array attribute
312+
of type `arrayType` to have elements in non-decreasing order
313+
* `DenseArrayStrictlySorted<arrayType>`: Specifying a dense array attribute
314+
of type `arrayType` to have elements in increasing order
296315
* `IntArrayNthElemEq<I, N>`: Specifying an integer array attribute's `I`-th
297316
element to be equal to `N`
298317
* `IntArrayNthElemMinValue<I, N>`: Specifying an integer array attribute's
@@ -301,9 +320,36 @@ Right now, the following primitive constraints are supported:
301320
`I`-th element to be less than or equal to `N`
302321
* `IntArrayNthElemInRange<I, M, N>`: Specifying an integer array attribute's
303322
`I`-th element to be greater than or equal to `M` and less than or equal to `N`
323+
* `IsNullAttr`: Specifying an optional attribute which must be empty
304324

305325
TODO: Design and implement more primitive constraints
306326

327+
#### Combining constraints
328+
329+
`AllAttrOf` is provided to allow combination of multiple constraints which
330+
must all hold.
331+
332+
For example:
333+
```tablegen
334+
def OpAllAttrConstraint1 : TEST_Op<"all_attr_constraint_of1"> {
335+
let arguments = (ins I64ArrayAttr:$attr);
336+
let results = (outs I32);
337+
}
338+
def OpAllAttrConstraint2 : TEST_Op<"all_attr_constraint_of2"> {
339+
let arguments = (ins I64ArrayAttr:$attr);
340+
let results = (outs I32);
341+
}
342+
def Constraint0 : AttrConstraint<
343+
CPred<"::llvm::cast<::mlir::IntegerAttr>(::llvm::cast<ArrayAttr>($_self)[0]).getInt() == 0">,
344+
"[0] == 0">;
345+
def Constraint1 : AttrConstraint<
346+
CPred<"::llvm::cast<::mlir::IntegerAttr>(::llvm::cast<ArrayAttr>($_self)[1]).getInt() == 1">,
347+
"[1] == 1">;
348+
def : Pat<(OpAllAttrConstraint1
349+
AllAttrOf<[Constraint0, Constraint1]>:$attr),
350+
(OpAllAttrConstraint2 $attr)>;
351+
```
352+
307353
### Operation regions
308354

309355
The regions of an operation are specified inside of the `dag`-typed `regions`,
@@ -1398,6 +1444,8 @@ optionality, default values, etc.:
13981444
* `OptionalAttr`: specifies an attribute as [optional](#optional-attributes).
13991445
* `ConfinedAttr`: adapts an attribute with
14001446
[further constraints](#confining-attributes).
1447+
* `AllAttrOf`: adapts an attribute with
1448+
[multiple constraints](#combining-constraints).
14011449

14021450
### Enum attributes
14031451

mlir/include/mlir/IR/CommonAttrConstraints.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,10 @@ def IntPositive : AttrConstraint<
773773
CPred<"::llvm::cast<::mlir::IntegerAttr>($_self).getValue().isStrictlyPositive()">,
774774
"whose value is positive">;
775775

776+
class ArrayMaxCount<int n> : AttrConstraint<
777+
CPred<"::llvm::cast<::mlir::ArrayAttr>($_self).size() <= " # n>,
778+
"with at most " # n # " elements">;
779+
776780
class ArrayMinCount<int n> : AttrConstraint<
777781
CPred<"::llvm::cast<::mlir::ArrayAttr>($_self).size() >= " # n>,
778782
"with at least " # n # " elements">;

0 commit comments

Comments
 (0)