Add TabelGen support to logically AND a list of attribute constraints.

This is the AllOf version for AttrConstraint and allows specifying the combined constraint during pattern match.

--

PiperOrigin-RevId: 242540243
This commit is contained in:
Ashwin Murthy 2019-04-08 14:32:39 -07:00 committed by Mehdi Amini
parent 046a993967
commit 70546104ff
2 changed files with 44 additions and 0 deletions

View File

@ -637,6 +637,15 @@ class Confined<Attr attr, list<AttrConstraint> constraints> : Attr<
let isOptional = attr.isOptional;
}
// An AttrConstraint that holds if all attr constraints specified in
// 'constraints' hold.
class AllAttrConstraintsOf<list<AttrConstraint> constraints> : AttrConstraint<
AllOf<!listconcat([!head(constraints).predicate],
!foreach(pred, !tail(constraints), pred.predicate))>,
!foldl(/*init*/!head(constraints).description, /*list*/!tail(constraints),
prev, cur, prev # " and " # cur.description)> {
}
class IntMinValue<int n> : AttrConstraint<
CPred<"{0}.cast<IntegerAttr>().getInt() >= " # n>,
"whose minimal value is " # n>;

View File

@ -0,0 +1,35 @@
// RUN: mlir-tblgen -gen-rewriters -I %S/../../include %s | FileCheck %s
include "mlir/IR/OpBase.td"
def FirstConstraint : AttrConstraint<CPred<"FirstConstraint">,
"first constraint">;
def SecondConstraint : AttrConstraint<CPred<"SecondConstraint">,
"second constraint">;
def ThirdConstraint : AttrConstraint<CPred<"ThirdConstraint">,
"third constraint">;
def OpA : Op<"op_a", []> {
let arguments = (ins
I32Attr:$attr
);
let results = (outs I32:$result);
}
def : Pat<
(OpA AllAttrConstraintsOf<
[FirstConstraint,
SecondConstraint,
ThirdConstraint]>:$more),
(OpA $more)>;
// Test combining AttrConstraint during pattern match.
// ---
// CHECK-LABEL: struct GeneratedConvert0
// CHECK: auto attr = op0->getAttrOfType<IntegerAttr>("attr");
// CHECK: if (!(((FirstConstraint)) && ((SecondConstraint)) && ((ThirdConstraint)))) return matchFailure();
// CHECK-NEXT: s.more = attr;