2020-05-03 03:28:57 +08:00
|
|
|
// RUN: mlir-opt %s -mlir-disable-threading=true -test-matchers -o /dev/null 2>&1 | FileCheck %s
|
Add a layer of recursive matchers that compose.
This CL adds support for building matchers recursively.
The following matchers are provided:
1. `m_any()` can match any value
2. `m_val(Value *)` binds to a value and must match it
3. `RecursivePatternMatcher<OpType, Matchers...>` n-arity pattern that matches `OpType` and whose operands must be matched exactly by `Matchers...`.
This allows building expression templates for patterns, declaratively, in a very natural fashion.
For example pattern `p9` defined as follows:
```
auto mul_of_muladd = m_Op<MulFOp>(m_Op<MulFOp>(), m_Op<AddFOp>());
auto mul_of_anyadd = m_Op<MulFOp>(m_any(), m_Op<AddFOp>());
auto p9 = m_Op<MulFOp>(m_Op<MulFOp>(
mul_of_muladd, m_Op<MulFOp>()),
m_Op<MulFOp>(mul_of_anyadd, mul_of_anyadd));
```
Successfully matches `%6` in:
```
%0 = addf %a, %b: f32
%1 = addf %a, %c: f32 // matched
%2 = addf %c, %b: f32
%3 = mulf %a, %2: f32 // matched
%4 = mulf %3, %1: f32 // matched
%5 = mulf %4, %4: f32 // matched
%6 = mulf %5, %5: f32 // matched
```
Note that 0-ary matchers can be used as leaves in place of n-ary matchers. This alleviates from passing explicit `m_any()` leaves.
In the future, we may add extra patterns to specify that operands may be matched in any order.
PiperOrigin-RevId: 284469446
2019-12-09 10:09:07 +08:00
|
|
|
|
|
|
|
func @test1(%a: f32, %b: f32, %c: f32) {
|
|
|
|
%0 = addf %a, %b: f32
|
|
|
|
%1 = addf %a, %c: f32
|
|
|
|
%2 = addf %c, %b: f32
|
|
|
|
%3 = mulf %a, %2: f32
|
|
|
|
%4 = mulf %3, %1: f32
|
|
|
|
%5 = mulf %4, %4: f32
|
|
|
|
%6 = mulf %5, %5: f32
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// CHECK-LABEL: test1
|
|
|
|
// CHECK: Pattern add(*) matched 3 times
|
|
|
|
// CHECK: Pattern mul(*) matched 4 times
|
|
|
|
// CHECK: Pattern add(add(*), *) matched 0 times
|
|
|
|
// CHECK: Pattern add(*, add(*)) matched 0 times
|
|
|
|
// CHECK: Pattern mul(add(*), *) matched 0 times
|
|
|
|
// CHECK: Pattern mul(*, add(*)) matched 2 times
|
|
|
|
// CHECK: Pattern mul(mul(*), *) matched 3 times
|
|
|
|
// CHECK: Pattern mul(mul(*), mul(*)) matched 2 times
|
|
|
|
// CHECK: Pattern mul(mul(mul(*), mul(*)), mul(mul(*), mul(*))) matched 1 times
|
|
|
|
// CHECK: Pattern mul(mul(mul(mul(*), add(*)), mul(*)), mul(mul(*, add(*)), mul(*, add(*)))) matched 1 times
|
|
|
|
// CHECK: Pattern add(a, b) matched 1 times
|
|
|
|
// CHECK: Pattern add(a, c) matched 1 times
|
|
|
|
// CHECK: Pattern add(b, a) matched 0 times
|
|
|
|
// CHECK: Pattern add(c, a) matched 0 times
|
|
|
|
// CHECK: Pattern mul(a, add(c, b)) matched 1 times
|
|
|
|
// CHECK: Pattern mul(a, add(b, c)) matched 0 times
|
|
|
|
// CHECK: Pattern mul(mul(a, *), add(a, c)) matched 1 times
|
|
|
|
// CHECK: Pattern mul(mul(a, *), add(c, b)) matched 0 times
|
2019-12-09 23:47:01 +08:00
|
|
|
|
|
|
|
func @test2(%a: f32) -> f32 {
|
|
|
|
%0 = constant 1.0: f32
|
|
|
|
%1 = addf %a, %0: f32
|
|
|
|
%2 = mulf %a, %1: f32
|
|
|
|
return %2: f32
|
|
|
|
}
|
|
|
|
|
|
|
|
// CHECK-LABEL: test2
|
|
|
|
// CHECK: Pattern add(add(a, constant), a) matched and bound constant to: 1.000000e+00
|
2020-01-14 00:21:04 +08:00
|
|
|
// CHECK: Pattern add(add(a, constant), a) matched
|