Skip to content

Commit ac06f71

Browse files
committed
[mlir] Add attribute constraints for sorted order.
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. Differential Revision: https://reviews.llvm.org/D134944
1 parent 995105d commit ac06f71

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

mlir/include/mlir/IR/OpBase.td

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,6 +1636,21 @@ class ArrayCount<int n> : AttrConstraint<
16361636
CPred<"$_self.cast<::mlir::ArrayAttr>().size() == " #n>,
16371637
"with exactly " # n # " elements">;
16381638

1639+
class DenseArraySorted<DenseArrayAttrBase arrayType> : AttrConstraint<
1640+
CPred<"llvm::is_sorted($_self.cast<" # arrayType # ">().asArrayRef())">,
1641+
"should be in non-decreasing order">;
1642+
1643+
class DenseArrayStrictlySorted<DenseArrayAttrBase arrayType> : AttrConstraint<
1644+
And<[
1645+
CPred<"llvm::is_sorted($_self.cast<" # arrayType # ">().asArrayRef())">,
1646+
// Check that no two adjacent elements are the same.
1647+
CPred<"[](" # arrayType.returnType # " a) {\n"
1648+
"return std::adjacent_find(std::begin(a), std::end(a)) == "
1649+
"std::end(a);\n"
1650+
"}($_self.cast<" # arrayType # ">().asArrayRef())"
1651+
>]>,
1652+
"should be in increasing order">;
1653+
16391654
class IntArrayNthElemEq<int index, int value> : AttrConstraint<
16401655
And<[
16411656
CPred<"$_self.cast<::mlir::ArrayAttr>().size() > " # index>,

mlir/test/IR/attribute.mlir

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,41 @@ func.func @dense_array_attr() attributes {
633633

634634
// -----
635635

636+
func.func @testConfinedDenseArrayAttr() {
637+
"test.confined_dense_array_attr"() {
638+
i64attr = array<i64: 0, 2, 3>,
639+
i32attr = array<i32: 1>,
640+
emptyattr = array<i16>
641+
} : () -> ()
642+
func.return
643+
}
644+
645+
// -----
646+
647+
func.func @testConfinedDenseArrayAttrDuplicateValues() {
648+
// expected-error@+1{{'test.confined_dense_array_attr' op attribute 'i64attr' failed to satisfy constraint: i64 dense array attribute should be in increasing order}}
649+
"test.confined_dense_array_attr"() {
650+
emptyattr = array<i16>,
651+
i32attr = array<i32: 1, 1>,
652+
i64attr = array<i64: 0, 2, 2>
653+
} : () -> ()
654+
func.return
655+
}
656+
657+
// -----
658+
659+
func.func @testConfinedDenseArrayAttrDecreasingOrder() {
660+
// expected-error@+1{{'test.confined_dense_array_attr' op attribute 'i32attr' failed to satisfy constraint: i32 dense array attribute should be in non-decreasing order}}
661+
"test.confined_dense_array_attr"() {
662+
emptyattr = array<i16>,
663+
i32attr = array<i32: 1, 0>,
664+
i64attr = array<i64: 0, 2, 3>
665+
} : () -> ()
666+
func.return
667+
}
668+
669+
// -----
670+
636671
//===----------------------------------------------------------------------===//
637672
// Test SymbolRefAttr
638673
//===----------------------------------------------------------------------===//

mlir/test/lib/Dialect/Test/TestOps.td

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,17 @@ def DenseArrayAttrOp : TEST_Op<"dense_array_attr"> {
303303
}];
304304
}
305305

306+
def ConfinedDenseArrayAttrOp : TEST_Op<"confined_dense_array_attr"> {
307+
let arguments = (ins
308+
ConfinedAttr<DenseI16ArrayAttr,
309+
[DenseArrayStrictlySorted<DenseI16ArrayAttr>]>:$emptyattr,
310+
ConfinedAttr<DenseI32ArrayAttr,
311+
[DenseArraySorted<DenseI32ArrayAttr>]>:$i32attr,
312+
ConfinedAttr<DenseI64ArrayAttr,
313+
[DenseArrayStrictlySorted<DenseI64ArrayAttr>]>:$i64attr
314+
);
315+
}
316+
306317
//===----------------------------------------------------------------------===//
307318
// Test Enum Attributes
308319
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)