From: Lei Zhang Date: Tue, 2 Apr 2019 00:40:58 +0000 (-0700) Subject: [TableGen] Add Confined, IntMinValue, and ArrayMinCount for attribute constraints X-Git-Tag: llvmorg-11-init~1466^2~2054 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bae95d25e50f7c4be7b9197dc93d8a441a9af732;p=platform%2Fupstream%2Fllvm.git [TableGen] Add Confined, IntMinValue, and ArrayMinCount for attribute constraints This CL introduces Confined as a general mechanism to compose complex attribute constraints out of more primitive ones. It's particularly useful for automatically generating op definitions from some external source, where we can have random combinations of primitive constraints and it would be impractical to define a case for each of such combination. Two primitive attribute constraints, IntMinValue and ArrayMinCount, are added to be used together with Confined. -- PiperOrigin-RevId: 241435955 --- diff --git a/mlir/include/mlir/IR/OpBase.td b/mlir/include/mlir/IR/OpBase.td index 3259346..2c33d56 100644 --- a/mlir/include/mlir/IR/OpBase.td +++ b/mlir/include/mlir/IR/OpBase.td @@ -313,6 +313,15 @@ def F16Tensor : TypedTensor; def F32Tensor : TypedTensor; def F64Tensor : TypedTensor; +// This represents a generic tuple without any constraints on elemental type, +// ranks, or size. As Tuples can contain tensors, vectors, or scalar values +// there is not only a single elemental type. +def Tuple : Type; + +//===----------------------------------------------------------------------===// +// Common type constraints +//===----------------------------------------------------------------------===// + // Type constraint for integer-like types: integers, indices, vectors of // integers, tensors of integers. def IntegerLike : TypeConstraint.predicate, TypedTensor.predicate]>, "floating-point-like">; - -// This represents a generic tuple without any constraints on elemental type, -// ranks, or size. As Tuples can contain tensors, vectors, or scalar values -// there is not only a single elemental type. -def Tuple : Type; //===----------------------------------------------------------------------===// @@ -530,6 +534,34 @@ class ConstantAttr : AttrConstraint< class ConstF32Attr : ConstantAttr; //===----------------------------------------------------------------------===// +// Common attribute constraints +//===----------------------------------------------------------------------===// + +// A general mechanism to further confine the given `attr` with all the +// `constraints`. This allows to compose complex constraints out of a series +// of more primitive ones. +class Confined constraints> : Attr< + AllOf, + !foldl(/*init*/attr.description, /*list*/constraints, + prev, cur, prev # " " # cur.description)> { + let storageType = attr.storageType; + let returnType = attr.returnType; + let convertFromStorage = attr.convertFromStorage; + let constBuilderCall = attr.constBuilderCall; + let defaultValue = attr.defaultValue; + let isOptional = attr.isOptional; +} + +class IntMinValue : AttrConstraint< + CPred<"{0}.cast().getInt() >= " # n>, + "whose minimal value is " # n>; + +class ArrayMinCount : AttrConstraint< + CPred<"{0}.cast().size() >= " # n>, + "with at least " # n # " elements">; + +//===----------------------------------------------------------------------===// // OpTrait definitions //===----------------------------------------------------------------------===// @@ -693,7 +725,7 @@ class Results { } //===----------------------------------------------------------------------===// -// Common type constraint predicates +// Common op type constraints //===----------------------------------------------------------------------===// // Type Constraint operand `idx`'s Vector or Tensor Element type is `type`. @@ -716,7 +748,9 @@ class TCopVTEtIsSameAs : AllOf<[ SubstLeaves<"{0}", "{0}.getOperand(" # j # ")->getType()", IsVectorOrTensorTypePred>, // TODO: This could be made into C++ function instead. - CPred<"{0}.getOperand(" # i # ")->getType().cast().getElementType() == {0}.getOperand(" # j # ")->getType().cast().getElementType()">]>; + CPred<"{0}.getOperand(" # i # ")->getType().cast()." + "getElementType() == {0}.getOperand(" # j # ")->getType()." + "cast().getElementType()">]>; // Predicate to verify that the i'th result and the j'th operand have the same // elemental type. @@ -730,7 +764,9 @@ class TCresVTEtIsSameAsOp : AllOf<[ SubstLeaves<"{0}", "{0}.getOperand(" # j # ")->getType()", IsVectorOrTensorTypePred>, // TODO: This could be made into C++ function instead. - CPred<"{0}.getResult(" # i # ")->getType().cast().getElementType() == {0}.getOperand(" # j # ")->getType().cast().getElementType()">]>; + CPred<"{0}.getResult(" # i # ")->getType().cast()." + "getElementType() == {0}.getOperand(" # j # ")->getType()." + "cast().getElementType()">]>; //===----------------------------------------------------------------------===// // Pattern definitions diff --git a/mlir/test/mlir-tblgen/predicate.td b/mlir/test/mlir-tblgen/predicate.td index d777699..d2fef04 100644 --- a/mlir/test/mlir-tblgen/predicate.td +++ b/mlir/test/mlir-tblgen/predicate.td @@ -50,3 +50,20 @@ def IdentityI32 : Op<"identity_i32", [PredOpTrait< // CHECK-LABEL: IdentityI32::verify // CHECK: if (!((((*this->getOperation()).getNumOperands() > 0)) && (((*this->getOperation()).getOperand(0)->getType().isa())) && (((*this->getOperation()).getOperand(0)->getType().cast().getElementType().isInteger(32))))) // CHECK-NEXT: return emitOpError("failed to verify that first operand has i32 element type"); + +def OpA : Op<"op_for_int_min_val", []> { + let arguments = (ins Confined]>:$attr); +} + +// CHECK-LABEL: OpA::verify() +// CHECK: if (!(((true)) && ((this->getAttr("attr").cast().getInt() >= 10)))) +// CHECK-SAME: return emitOpError("attribute 'attr' failed to satisfy 32-bit integer whose minimal value is 10 attribute constraints"); + +def OpB : Op<"op_for_arr_min_count", []> { + let arguments = (ins Confined]>:$attr); +} + +// CHECK-LABEL: OpB::verify() +// CHECK: if (!(((true)) && ((this->getAttr("attr").cast().size() >= 8)))) +// CHECK-SAME: return emitOpError("attribute 'attr' failed to satisfy array with at least 8 elements attribute constraints"); +