[mlir] Simplify DDR matching patterns with equal operands for operators where it's applicable. Added documentation.

This https://reviews.llvm.org/D89254 diff introduced implicit matching between same name operands.

Differential Revision: https://reviews.llvm.org/D89598
This commit is contained in:
rdzhabarov 2020-10-20 04:51:41 +00:00
parent 1c1f794c2b
commit 281e0f3636
3 changed files with 18 additions and 14 deletions

View File

@ -136,9 +136,11 @@ and `$attr` in result patterns and additional constraints.
The pattern is position-based: the symbol names used for capturing here do not
need to match with the op definition as shown in the above example. As another
example, the pattern can be written as ` def : Pat<(AOp $a, F32Attr:$b), ...>;`
example, the pattern can be written as `def : Pat<(AOp $a, F32Attr:$b), ...>;`
and use `$a` and `$b` to refer to the captured input and attribute. But using
the ODS name directly in the pattern is also allowed.
the ODS name directly in the pattern is also allowed. Operands in the source
pattern can have the same name. This bounds one operand to the name while
verifying the rest are all equal.
Also note that we only need to add `TypeConstraint` or `AttributeConstraint`
when we need to further limit the match criteria. If all valid cases to the op

View File

@ -2292,8 +2292,11 @@ def addBenefit;
//
// In the source pattern, `argN` can be used to specify matchers (e.g., using
// type/attribute type constraints, etc.) and bound to a name for later use.
// We can also bound names to op instances to reference them later in
// multi-entity constraints.
// We can also bind names to op instances to reference them later in
// multi-entity constraints. Operands in the source pattern can have
// the same name. This bounds one operand to the name while verifying
// the rest are all equal.
//
//
// In the result pattern, `argN` can be used to refer to a previously bound
// name, with potential transformations (e.g., using tAttr, etc.). `argN` can
@ -2303,16 +2306,18 @@ def addBenefit;
// For example,
//
// ```
// def : Pattern<(OneResultOp1:$op1 $arg0, $arg1),
// def : Pattern<(OneResultOp1:$op1 $arg0, $arg1, $arg0),
// [(OneResultOp2:$op2 $arg0, $arg1),
// (OneResultOp3 $op2 (OneResultOp4))],
// [(HasStaticShapePred $op1)]>;
// ```
//
// `$argN` is bound to the `OneResultOp1`'s N-th argument and used later to
// build `OneResultOp2`. `$op1` is bound to `OneResultOp1` and used to
// check whether the result's shape is static. `$op2` is bound to
// `OneResultOp2` and used to build `OneResultOp3`.
// First `$arg0` and '$arg1' are bound to the `OneResultOp1`'s first
// and second arguments and used later to build `OneResultOp2`. Second '$arg0'
// is verified to be equal to the first '$arg0' operand.
// `$op1` is bound to `OneResultOp1` and used to check whether the result's
// shape is static. `$op2` is bound to `OneResultOp2` and used to
// build `OneResultOp3`.
//
// ## Multi-result op
//

View File

@ -1,7 +1,5 @@
include "mlir/Dialect/Shape/IR/ShapeOps.td"
def EqualBinaryOperands : Constraint<CPred<"$0 == $1">>;
def AllInputShapesEq : Constraint<CPred< [{
llvm::all_of($0, [&](mlir::Value val) {
return $0[0] == val;
@ -10,9 +8,8 @@ def AllInputShapesEq : Constraint<CPred< [{
// Canonicalization patterns.
def CstrBroadcastableEqOps : Pat<(Shape_CstrBroadcastableOp:$op $lhs, $rhs),
(Shape_ConstWitnessOp ConstBoolAttrTrue),
[(EqualBinaryOperands $lhs, $rhs)]>;
def CstrBroadcastableEqOps : Pat<(Shape_CstrBroadcastableOp:$op $x, $x),
(Shape_ConstWitnessOp ConstBoolAttrTrue)>;
def CstrEqEqOps : Pat<(Shape_CstrEqOp:$op $shapes),
(Shape_ConstWitnessOp ConstBoolAttrTrue),