diff --git a/mlir/include/mlir/IR/OpBase.td b/mlir/include/mlir/IR/OpBase.td --- a/mlir/include/mlir/IR/OpBase.td +++ b/mlir/include/mlir/IR/OpBase.td @@ -1636,6 +1636,21 @@ CPred<"$_self.cast<::mlir::ArrayAttr>().size() == " #n>, "with exactly " # n # " elements">; +class DenseArraySorted : AttrConstraint< + CPred<"llvm::is_sorted($_self.cast<" # arrayType # ">().asArrayRef())">, + "should be in non-decreasing order">; + +class DenseArrayStrictlySorted : AttrConstraint< + And<[ + CPred<"llvm::is_sorted($_self.cast<" # arrayType # ">().asArrayRef())">, + // Check that no two adjacent elements are the same. + CPred<"[](" # arrayType.returnType # " a) {\n" + "return std::adjacent_find(std::begin(a), std::end(a)) == " + "std::end(a);\n" + "}($_self.cast<" # arrayType # ">().asArrayRef())" + >]>, + "should be in increasing order">; + class IntArrayNthElemEq : AttrConstraint< And<[ CPred<"$_self.cast<::mlir::ArrayAttr>().size() > " # index>, diff --git a/mlir/test/IR/attribute.mlir b/mlir/test/IR/attribute.mlir --- a/mlir/test/IR/attribute.mlir +++ b/mlir/test/IR/attribute.mlir @@ -597,6 +597,41 @@ // ----- +func.func @testConfinedDenseArrayAttr() { + "test.confined_dense_array_attr"() { + i64attr = array, + i32attr = array, + emptyattr = array + } : () -> () + func.return +} + +// ----- + +func.func @testConfinedDenseArrayAttrDuplicateValues() { + // expected-error@+1{{'test.confined_dense_array_attr' op attribute 'i64attr' failed to satisfy constraint: i64 dense array attribute should be in increasing order}} + "test.confined_dense_array_attr"() { + emptyattr = array, + i32attr = array, + i64attr = array + } : () -> () + func.return +} + +// ----- + +func.func @testConfinedDenseArrayAttrDecreasingOrder() { + // 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}} + "test.confined_dense_array_attr"() { + emptyattr = array, + i32attr = array, + i64attr = array + } : () -> () + func.return +} + +// ----- + //===----------------------------------------------------------------------===// // Test SymbolRefAttr //===----------------------------------------------------------------------===// diff --git a/mlir/test/lib/Dialect/Test/TestOps.td b/mlir/test/lib/Dialect/Test/TestOps.td --- a/mlir/test/lib/Dialect/Test/TestOps.td +++ b/mlir/test/lib/Dialect/Test/TestOps.td @@ -295,6 +295,17 @@ }]; } +def ConfinedDenseArrayAttrOp : TEST_Op<"confined_dense_array_attr"> { + let arguments = (ins + ConfinedAttr]>:$emptyattr, + ConfinedAttr]>:$i32attr, + ConfinedAttr]>:$i64attr + ); +} + //===----------------------------------------------------------------------===// // Test Enum Attributes //===----------------------------------------------------------------------===//