1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
//===- TestMatchers.cpp - Pass to test matchers ---------------------------===//
//
// Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/StandardOps/Ops.h"
#include "mlir/IR/Function.h"
#include "mlir/IR/Matchers.h"
#include "mlir/Pass/Pass.h"
using namespace mlir;
namespace {
/// This is a test pass for verifying matchers.
struct TestMatchers : public FunctionPass<TestMatchers> {
void runOnFunction() override;
};
} // end anonymous namespace
// This could be done better but is not worth the variadic template trouble.
template <typename Matcher> unsigned countMatches(FuncOp f, Matcher &matcher) {
unsigned count = 0;
f.walk([&count, &matcher](Operation *op) {
if (matcher.match(op))
++count;
});
return count;
}
using mlir::matchers::m_Any;
using mlir::matchers::m_Val;
static void test1(FuncOp f) {
assert(f.getNumArguments() == 3 && "matcher test funcs must have 3 args");
auto a = m_Val(f.getArgument(0));
auto b = m_Val(f.getArgument(1));
auto c = m_Val(f.getArgument(2));
auto p0 = m_Op<AddFOp>(); // using 0-arity matcher
llvm::outs() << "Pattern add(*) matched " << countMatches(f, p0)
<< " times\n";
auto p1 = m_Op<MulFOp>(); // using 0-arity matcher
llvm::outs() << "Pattern mul(*) matched " << countMatches(f, p1)
<< " times\n";
auto p2 = m_Op<AddFOp>(m_Op<AddFOp>(), m_Any());
llvm::outs() << "Pattern add(add(*), *) matched " << countMatches(f, p2)
<< " times\n";
auto p3 = m_Op<AddFOp>(m_Any(), m_Op<AddFOp>());
llvm::outs() << "Pattern add(*, add(*)) matched " << countMatches(f, p3)
<< " times\n";
auto p4 = m_Op<MulFOp>(m_Op<AddFOp>(), m_Any());
llvm::outs() << "Pattern mul(add(*), *) matched " << countMatches(f, p4)
<< " times\n";
auto p5 = m_Op<MulFOp>(m_Any(), m_Op<AddFOp>());
llvm::outs() << "Pattern mul(*, add(*)) matched " << countMatches(f, p5)
<< " times\n";
auto p6 = m_Op<MulFOp>(m_Op<MulFOp>(), m_Any());
llvm::outs() << "Pattern mul(mul(*), *) matched " << countMatches(f, p6)
<< " times\n";
auto p7 = m_Op<MulFOp>(m_Op<MulFOp>(), m_Op<MulFOp>());
llvm::outs() << "Pattern mul(mul(*), mul(*)) matched " << countMatches(f, p7)
<< " times\n";
auto mul_of_mulmul = m_Op<MulFOp>(m_Op<MulFOp>(), m_Op<MulFOp>());
auto p8 = m_Op<MulFOp>(mul_of_mulmul, mul_of_mulmul);
llvm::outs()
<< "Pattern mul(mul(mul(*), mul(*)), mul(mul(*), mul(*))) matched "
<< countMatches(f, p8) << " times\n";
// clang-format off
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));
// clang-format on
llvm::outs() << "Pattern mul(mul(mul(mul(*), add(*)), mul(*)), mul(mul(*, "
"add(*)), mul(*, add(*)))) matched "
<< countMatches(f, p9) << " times\n";
auto p10 = m_Op<AddFOp>(a, b);
llvm::outs() << "Pattern add(a, b) matched " << countMatches(f, p10)
<< " times\n";
auto p11 = m_Op<AddFOp>(a, c);
llvm::outs() << "Pattern add(a, c) matched " << countMatches(f, p11)
<< " times\n";
auto p12 = m_Op<AddFOp>(b, a);
llvm::outs() << "Pattern add(b, a) matched " << countMatches(f, p12)
<< " times\n";
auto p13 = m_Op<AddFOp>(c, a);
llvm::outs() << "Pattern add(c, a) matched " << countMatches(f, p13)
<< " times\n";
auto p14 = m_Op<MulFOp>(a, m_Op<AddFOp>(c, b));
llvm::outs() << "Pattern mul(a, add(c, b)) matched " << countMatches(f, p14)
<< " times\n";
auto p15 = m_Op<MulFOp>(a, m_Op<AddFOp>(b, c));
llvm::outs() << "Pattern mul(a, add(b, c)) matched " << countMatches(f, p15)
<< " times\n";
auto mul_of_aany = m_Op<MulFOp>(a, m_Any());
auto p16 = m_Op<MulFOp>(mul_of_aany, m_Op<AddFOp>(a, c));
llvm::outs() << "Pattern mul(mul(a, *), add(a, c)) matched "
<< countMatches(f, p16) << " times\n";
auto p17 = m_Op<MulFOp>(mul_of_aany, m_Op<AddFOp>(c, b));
llvm::outs() << "Pattern mul(mul(a, *), add(c, b)) matched "
<< countMatches(f, p17) << " times\n";
}
void test2(FuncOp f) {
auto a = m_Val(f.getArgument(0));
FloatAttr floatAttr;
auto p = m_Op<MulFOp>(a, m_Op<AddFOp>(a, m_Constant(&floatAttr)));
// Last operation that is not the terminator.
Operation *lastOp = f.getBody().front().back().getPrevNode();
if (p.match(lastOp))
llvm::outs()
<< "Pattern add(add(a, constant), a) matched and bound constant to: "
<< floatAttr.getValueAsDouble() << "\n";
}
void TestMatchers::runOnFunction() {
auto f = getFunction();
llvm::outs() << f.getName() << "\n";
if (f.getName() == "test1")
test1(f);
if (f.getName() == "test2")
test2(f);
}
static PassRegistration<TestMatchers> pass("test-matchers",
"Test C++ pattern matchers.");
|