This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Add attribute constraints for sorted order.
ClosedPublic

Authored by akuegel on Sep 30 2022, 2:56 AM.

Details

Summary

We often have constraints for array attributes that they are sorted
non-decreasing or strictly increasing. This change adds AttrConstraint classes
that support DenseArrayAttr for integer types.

Diff Detail

Event Timeline

akuegel created this revision.Sep 30 2022, 2:56 AM
akuegel requested review of this revision.Sep 30 2022, 2:56 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 30 2022, 2:56 AM
akuegel updated this revision to Diff 464199.Sep 30 2022, 3:00 AM

Remove unintended whitespace change.

akuegel updated this revision to Diff 464201.Sep 30 2022, 3:02 AM

Remove tabs.

akuegel updated this revision to Diff 464203.Sep 30 2022, 3:04 AM

Fix test name.

Sorry for the many updates for this patch, I should have self-reviewed everything before uploading. But I have finished self-review now, so it should be ready to take a look now.

This AttrConstr is planned to be used for Linalg ReduceOp:

ConfinedAttr<DenseI64ArrayAttr, [ArrayStrictlySorted<64>]>:$dimensions

But it seems likely to be useful in other cases as well.
Should I add this usage in this patch, or in a separate one?

This revision is now accepted and ready to land.Sep 30 2022, 5:41 AM

This AttrConstr is planned to be used for Linalg ReduceOp:

ConfinedAttr<DenseI64ArrayAttr, [ArrayStrictlySorted<64>]>:$dimensions

But it seems likely to be useful in other cases as well.
Should I add this usage in this patch, or in a separate one?

Separate patch for separate things is always better IMO.

nicolasvasilache requested changes to this revision.Sep 30 2022, 5:47 AM
nicolasvasilache added inline comments.
mlir/include/mlir/IR/OpBase.td
1639

Sorry, accepted too quickly, this feels a bit wonky ..

Can we use the full type instead of just the number of bits?

ConfinedAttr<DenseI16ArrayAttr, ArraySorted<DenseI16ArrayAttr>>

just 16 looks very surprising to use here.

This revision now requires changes to proceed.Sep 30 2022, 5:47 AM
rriddle added inline comments.Sep 30 2022, 10:14 AM
mlir/include/mlir/IR/OpBase.td
1639

The naming on these looks off, Array by itself should only be used for ArrayAttr (this is DenseArrayAttr).

+1 also on not just using bitwidth, the context is not obvious enough here.

akuegel updated this revision to Diff 464912.Oct 4 2022, 1:19 AM

Address review comments.

akuegel marked an inline comment as done.Oct 4 2022, 1:21 AM
akuegel added inline comments.
mlir/include/mlir/IR/OpBase.td
1639

I am now using DenseArraySorted/DenseArrayStrictlySorted as names.
For DenseArrayStrictlySorted, I still need to pass the element type as well. I am passing it as string, not sure whether it is possible to pass it differently?

akuegel updated this revision to Diff 464914.Oct 4 2022, 1:25 AM

Fix test names, and rebase.

zero9178 added inline comments.Oct 4 2022, 1:42 AM
mlir/include/mlir/IR/OpBase.td
1639

You could use arrayType.returnType instead of ArrayRef<" # elementType # "> to get rid of the element type parameter. The returnType of DenseArrayAttrBase is always an ArrayRef of the element type which happens to be what you need.

akuegel updated this revision to Diff 464918.Oct 4 2022, 1:52 AM

Use arrayType.returnType

mlir/include/mlir/IR/OpBase.td
1639

Thank you very much :)
This is exactly what I need here. Done.

akuegel updated this revision to Diff 464919.Oct 4 2022, 1:55 AM

Fix indentation.

mlir/include/mlir/IR/OpBase.td
1645

I think you should be able to use is_sorted with a comparator here ? (i.e. always return false if elements are equal)

akuegel added inline comments.Oct 5 2022, 12:25 AM
mlir/include/mlir/IR/OpBase.td
1645

TLDR: Technically, it seems using the std::less_equal comparator should work.

I was not sure whether we can rely on the way the comparator will be used. The documentation says you need to pass a "less" comparator. But then it also defines sorted as:

A sequence is sorted with respect to a comparator comp if for any iterator it pointing to the sequence and any non-negative integer n such that it + n is a valid iterator pointing to an element of the sequence, comp(*(it + n), *it) evaluates to false.

If implemented like that, using std::less_equal would definitely work. Also I see that boost uses std::less_equal to implement is_strictly_increasing.

But I think this would mean that I need another parameter to the DenseArrayStrictlySorted constraint: the element type, so that I can specify the comparator. Or is there a way to derive that from DenseArrayAttrBase?

nicolasvasilache accepted this revision.Oct 5 2022, 12:43 AM
nicolasvasilache added inline comments.
mlir/include/mlir/IR/OpBase.td
1645

If it is not possible to use some auto type deduction based on the result of asArrayRef we could always make the cppType available from DenseArrayAttrBase.
But that may be a bridge too far for @rriddle , I am fine with landing as is and iterating later.
Thanks for trying!

This revision is now accepted and ready to land.Oct 5 2022, 12:43 AM
This revision was automatically updated to reflect the committed changes.