[Mlir-commits] [mlir] ac06f71 - [mlir] Add attribute constraints for sorted order.
Adrian Kuegel
llvmlistbot at llvm.org
Wed Oct 5 00:46:37 PDT 2022
Author: Adrian Kuegel
Date: 2022-10-05T09:46:19+02:00
New Revision: ac06f7169fb0951026fb772a16dc13a08df5be86
URL: https://github.com/llvm/llvm-project/commit/ac06f7169fb0951026fb772a16dc13a08df5be86
DIFF: https://github.com/llvm/llvm-project/commit/ac06f7169fb0951026fb772a16dc13a08df5be86.diff
LOG: [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
Added:
Modified:
mlir/include/mlir/IR/OpBase.td
mlir/test/IR/attribute.mlir
mlir/test/lib/Dialect/Test/TestOps.td
Removed:
################################################################################
diff --git a/mlir/include/mlir/IR/OpBase.td b/mlir/include/mlir/IR/OpBase.td
index 213340f47424b..f35cd3afe7b40 100644
--- a/mlir/include/mlir/IR/OpBase.td
+++ b/mlir/include/mlir/IR/OpBase.td
@@ -1636,6 +1636,21 @@ class ArrayCount<int n> : AttrConstraint<
CPred<"$_self.cast<::mlir::ArrayAttr>().size() == " #n>,
"with exactly " # n # " elements">;
+class DenseArraySorted<DenseArrayAttrBase arrayType> : AttrConstraint<
+ CPred<"llvm::is_sorted($_self.cast<" # arrayType # ">().asArrayRef())">,
+ "should be in non-decreasing order">;
+
+class DenseArrayStrictlySorted<DenseArrayAttrBase arrayType> : 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<int index, int value> : AttrConstraint<
And<[
CPred<"$_self.cast<::mlir::ArrayAttr>().size() > " # index>,
diff --git a/mlir/test/IR/attribute.mlir b/mlir/test/IR/attribute.mlir
index 1bdbdc25bdcac..ee0b1f4bf100f 100644
--- a/mlir/test/IR/attribute.mlir
+++ b/mlir/test/IR/attribute.mlir
@@ -633,6 +633,41 @@ func.func @dense_array_attr() attributes {
// -----
+func.func @testConfinedDenseArrayAttr() {
+ "test.confined_dense_array_attr"() {
+ i64attr = array<i64: 0, 2, 3>,
+ i32attr = array<i32: 1>,
+ emptyattr = array<i16>
+ } : () -> ()
+ func.return
+}
+
+// -----
+
+func.func @testConfinedDenseArrayAttrDuplicateValues() {
+ // expected-error at +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<i16>,
+ i32attr = array<i32: 1, 1>,
+ i64attr = array<i64: 0, 2, 2>
+ } : () -> ()
+ func.return
+}
+
+// -----
+
+func.func @testConfinedDenseArrayAttrDecreasingOrder() {
+ // expected-error at +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<i16>,
+ i32attr = array<i32: 1, 0>,
+ i64attr = array<i64: 0, 2, 3>
+ } : () -> ()
+ func.return
+}
+
+// -----
+
//===----------------------------------------------------------------------===//
// Test SymbolRefAttr
//===----------------------------------------------------------------------===//
diff --git a/mlir/test/lib/Dialect/Test/TestOps.td b/mlir/test/lib/Dialect/Test/TestOps.td
index 3d6acb89ca68d..85df4198f172a 100644
--- a/mlir/test/lib/Dialect/Test/TestOps.td
+++ b/mlir/test/lib/Dialect/Test/TestOps.td
@@ -303,6 +303,17 @@ def DenseArrayAttrOp : TEST_Op<"dense_array_attr"> {
}];
}
+def ConfinedDenseArrayAttrOp : TEST_Op<"confined_dense_array_attr"> {
+ let arguments = (ins
+ ConfinedAttr<DenseI16ArrayAttr,
+ [DenseArrayStrictlySorted<DenseI16ArrayAttr>]>:$emptyattr,
+ ConfinedAttr<DenseI32ArrayAttr,
+ [DenseArraySorted<DenseI32ArrayAttr>]>:$i32attr,
+ ConfinedAttr<DenseI64ArrayAttr,
+ [DenseArrayStrictlySorted<DenseI64ArrayAttr>]>:$i64attr
+ );
+}
+
//===----------------------------------------------------------------------===//
// Test Enum Attributes
//===----------------------------------------------------------------------===//
More information about the Mlir-commits
mailing list