Skip to content

[MLIR Attr] add ArrayMaxCount attribute constraint #92453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions mlir/docs/DefiningDialects/Operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,27 @@ Right now, the following primitive constraints are supported:
equal to `N`
* `IntMaxValue<N>`: Specifying an integer attribute to be less than or equal
to `N`
* `IntNEQValue<N>`: Specifying an integer attribute to be not equal
to `N`
* `IntPositive`: Specifying an integer attribute whose value is positive
* `IntNonNegative`: Specifying an integer attribute whose value is
non-negative
* `ArrayMinCount<N>`: Specifying an array attribute to have at least `N`
elements
* `ArrayMaxCount<N>`: Specifying an array attribute to have at most `N`
elements
* `ArrayCount<N>`: Specifying an array attribute to have exactly `N`
elements
* `DenseArrayCount<N>`: Specifying a dense array attribute to have
exactly `N` elements
* `DenseArrayStrictlyPositive<arrayType>`: Specifying a dense array attribute
of type `arrayType` to have all positive elements
* `DenseArrayStrictlyNonNegative<arrayType>`: Specifying a dense array attribute
of type `arrayType` to have all non-negative elements
* `DenseArraySorted<arrayType>`: Specifying a dense array attribute
of type `arrayType` to have elements in non-decreasing order
* `DenseArrayStrictlySorted<arrayType>`: Specifying a dense array attribute
of type `arrayType` to have elements in increasing order
* `IntArrayNthElemEq<I, N>`: Specifying an integer array attribute's `I`-th
element to be equal to `N`
* `IntArrayNthElemMinValue<I, N>`: Specifying an integer array attribute's
Expand All @@ -301,9 +320,36 @@ Right now, the following primitive constraints are supported:
`I`-th element to be less than or equal to `N`
* `IntArrayNthElemInRange<I, M, N>`: Specifying an integer array attribute's
`I`-th element to be greater than or equal to `M` and less than or equal to `N`
* `IsNullAttr`: Specifying an optional attribute which must be empty

TODO: Design and implement more primitive constraints

#### Combining constraints

`AllAttrOf` is provided to allow combination of multiple constraints which
must all hold.

For example:
```tablegen
def OpAllAttrConstraint1 : TEST_Op<"all_attr_constraint_of1"> {
let arguments = (ins I64ArrayAttr:$attr);
let results = (outs I32);
}
def OpAllAttrConstraint2 : TEST_Op<"all_attr_constraint_of2"> {
let arguments = (ins I64ArrayAttr:$attr);
let results = (outs I32);
}
def Constraint0 : AttrConstraint<
CPred<"::llvm::cast<::mlir::IntegerAttr>(::llvm::cast<ArrayAttr>($_self)[0]).getInt() == 0">,
"[0] == 0">;
def Constraint1 : AttrConstraint<
CPred<"::llvm::cast<::mlir::IntegerAttr>(::llvm::cast<ArrayAttr>($_self)[1]).getInt() == 1">,
"[1] == 1">;
def : Pat<(OpAllAttrConstraint1
AllAttrOf<[Constraint0, Constraint1]>:$attr),
(OpAllAttrConstraint2 $attr)>;
```

### Operation regions

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

### Enum attributes

Expand Down
4 changes: 4 additions & 0 deletions mlir/include/mlir/IR/CommonAttrConstraints.td
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,10 @@ def IntPositive : AttrConstraint<
CPred<"::llvm::cast<::mlir::IntegerAttr>($_self).getValue().isStrictlyPositive()">,
"whose value is positive">;

class ArrayMaxCount<int n> : AttrConstraint<
CPred<"::llvm::cast<::mlir::ArrayAttr>($_self).size() <= " # n>,
"with at most " # n # " elements">;

class ArrayMinCount<int n> : AttrConstraint<
CPred<"::llvm::cast<::mlir::ArrayAttr>($_self).size() >= " # n>,
"with at least " # n # " elements">;
Expand Down
Loading