From: Lorenzo Chelini Date: Tue, 29 Nov 2022 18:45:37 +0000 (+0100) Subject: [MLIR] Introduce constraint attributes for DenseArrayAttr X-Git-Tag: upstream/17.0.6~25730 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=dd0de4dca92cd6affafb47f788b64e99187168f1;p=platform%2Fupstream%2Fllvm.git [MLIR] Introduce constraint attributes for DenseArrayAttr - `DenseArrayStrictlyPositive` all elements are required to be > 0. Returns true if the range is empty. - `DenseArrayNonNegative` all elements are required to be >= 0. Returns true if the range is empty. Both constraints will simplify verifier logic as we move from using `I64ArrayAttr` to `DenseI64ArrayAttr`. Reviewed By: rriddle Differential Revision: https://reviews.llvm.org/D138988 --- diff --git a/mlir/include/mlir/IR/OpBase.td b/mlir/include/mlir/IR/OpBase.td index 9c87071..e74cd00 100644 --- a/mlir/include/mlir/IR/OpBase.td +++ b/mlir/include/mlir/IR/OpBase.td @@ -1669,6 +1669,16 @@ class ArrayCount : AttrConstraint< CPred<"$_self.cast<::mlir::ArrayAttr>().size() == " #n>, "with exactly " # n # " elements">; +class DenseArrayStrictlyPositive : AttrConstraint< + CPred<"::llvm::all_of($_self.cast<" # arrayType #">().asArrayRef(), " + "[&](auto v) { return v > 0; })">, + "whose value is positive">; + +class DenseArrayNonNegative : AttrConstraint< + CPred<"::llvm::all_of($_self.cast<" # arrayType #">().asArrayRef(), " + "[&](auto v) { return v >= 0; })">, + "whose value is non-negative">; + class DenseArraySorted : AttrConstraint< CPred<"llvm::is_sorted($_self.cast<" # arrayType # ">().asArrayRef())">, "should be in non-decreasing order">; diff --git a/mlir/test/IR/attribute.mlir b/mlir/test/IR/attribute.mlir index ebfbb89..1066b90 100644 --- a/mlir/test/IR/attribute.mlir +++ b/mlir/test/IR/attribute.mlir @@ -672,6 +672,70 @@ func.func @testConfinedDenseArrayAttrDecreasingOrder() { // ----- +func.func @testConfinedStrictlyPositiveDenseArrayAttr() { + "test.confined_strictly_positive_attr"() { + boolattr = array, + i8attr = array, + i16attr = array, + i32attr = array, + i64attr = array, + f32attr = array, + f64attr = array, + emptyattr = array + } : () -> () + func.return +} + +// ----- + +func.func @testConfinedStrictlyPositiveDenseArrayAttr() { + // expected-error@+1{{'test.confined_strictly_positive_attr' op attribute 'i64attr' failed to satisfy constraint: i64 dense array attribute whose value is positive}} + "test.confined_strictly_positive_attr"() { + boolattr = array, + i8attr = array, + i16attr = array, + i32attr = array, + i64attr = array, + f32attr = array, + f64attr = array, + emptyattr = array + } : () -> () + func.return +} + +// ----- + +func.func @testConfinedNonNegativeDenseArrayAttr() { + "test.confined_non_negative_attr"() { + i8attr = array, + i16attr = array, + i32attr = array, + i64attr = array, + f32attr = array, + f64attr = array, + emptyattr = array + } : () -> () + func.return +} + +// ----- + +func.func @testConfinedNonNegativeDenseArrayAttr() { + // expected-error@+1{{'test.confined_non_negative_attr' op attribute 'i64attr' failed to satisfy constraint: i64 dense array attribute whose value is non-negative}} + "test.confined_non_negative_attr"() { + i8attr = array, + i16attr = array, + i32attr = array, + i64attr = array, + f32attr = array, + f64attr = array, + emptyattr = array + } : () -> () + func.return +} + +// ----- + //===----------------------------------------------------------------------===// // Test SymbolRefAttr //===----------------------------------------------------------------------===// diff --git a/mlir/test/lib/Dialect/Test/TestOps.td b/mlir/test/lib/Dialect/Test/TestOps.td index 660ce7d..0dd839e 100644 --- a/mlir/test/lib/Dialect/Test/TestOps.td +++ b/mlir/test/lib/Dialect/Test/TestOps.td @@ -186,9 +186,11 @@ def PositiveIntAttrOp : TEST_Op<"positive_int_attr"> { def TypeArrayAttrOp : TEST_Op<"type_array_attr"> { let arguments = (ins TypeArrayAttr:$attr); } + def TypeArrayAttrWithDefaultOp : TEST_Op<"type_array_attr_with_default"> { let arguments = (ins DefaultValuedAttr:$attr); } + def TypeStringAttrWithTypeOp : TEST_Op<"string_attr_with_type"> { let arguments = (ins TypedStrAttr:$attr); let assemblyFormat = "$attr attr-dict"; @@ -292,6 +294,10 @@ def DenseArrayAttrOp : TEST_Op<"dense_array_attr"> { }]; } +//===----------------------------------------------------------------------===// +// Test Attributes Constraints +//===----------------------------------------------------------------------===// + def ConfinedDenseArrayAttrOp : TEST_Op<"confined_dense_array_attr"> { let arguments = (ins ConfinedAttr { ); } +def DenseArrayStrictlyPositiveAttrOp : TEST_Op<"confined_strictly_positive_attr"> { + let arguments = (ins + ConfinedAttr]>:$boolattr, + ConfinedAttr]>:$i8attr, + ConfinedAttr]>:$i16attr, + ConfinedAttr]>:$i32attr, + ConfinedAttr]>:$i64attr, + ConfinedAttr]>:$f32attr, + ConfinedAttr]>:$f64attr, + ConfinedAttr]>:$emptyattr + ); +} + +// It does not make sense to have this constraint on a DenseBoolArrayAttr. +// It is always true. +def DenseArrayNonNegativeOp : TEST_Op<"confined_non_negative_attr"> { + let arguments = (ins + ConfinedAttr]>:$i8attr, + ConfinedAttr]>:$i16attr, + ConfinedAttr]>:$i32attr, + ConfinedAttr]>:$i64attr, + ConfinedAttr]>:$f32attr, + ConfinedAttr]>:$f64attr, + ConfinedAttr]>:$emptyattr + ); +} + //===----------------------------------------------------------------------===// // Test Enum Attributes //===----------------------------------------------------------------------===//