1//===- TestMatchers.cpp - Pass to test matchers ---------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "mlir/Dialect/Arith/IR/Arith.h"
10#include "mlir/Dialect/UB/IR/UBMatchers.h"
11#include "mlir/IR/BuiltinOps.h"
12#include "mlir/IR/Matchers.h"
13#include "mlir/Interfaces/FunctionInterfaces.h"
14#include "mlir/Pass/Pass.h"
15
16using namespace mlir;
17
18namespace {
19/// This is a test pass for verifying matchers.
20struct TestMatchers
21 : public PassWrapper<TestMatchers, InterfacePass<FunctionOpInterface>> {
22 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestMatchers)
23
24 void runOnOperation() override;
25 StringRef getArgument() const final { return "test-matchers"; }
26 StringRef getDescription() const final {
27 return "Test C++ pattern matchers.";
28 }
29};
30} // namespace
31
32// This could be done better but is not worth the variadic template trouble.
33template <typename Matcher>
34static unsigned countMatches(FunctionOpInterface f, Matcher &matcher) {
35 unsigned count = 0;
36 f.walk([&count, &matcher](Operation *op) {
37 if (matcher.match(op))
38 ++count;
39 });
40 return count;
41}
42
43using mlir::matchers::m_Any;
44using mlir::matchers::m_Val;
45static void test1(FunctionOpInterface f) {
46 assert(f.getNumArguments() == 3 && "matcher test funcs must have 3 args");
47
48 auto a = m_Val(v: f.getArgument(idx: 0));
49 auto b = m_Val(v: f.getArgument(idx: 1));
50 auto c = m_Val(v: f.getArgument(idx: 2));
51
52 auto p0 = m_Op<arith::AddFOp>(); // using 0-arity matcher
53 llvm::outs() << "Pattern add(*) matched " << countMatches(f, matcher&: p0)
54 << " times\n";
55
56 auto p1 = m_Op<arith::MulFOp>(); // using 0-arity matcher
57 llvm::outs() << "Pattern mul(*) matched " << countMatches(f, matcher&: p1)
58 << " times\n";
59
60 auto p2 = m_Op<arith::AddFOp>(matchers: m_Op<arith::AddFOp>(), matchers: m_Any());
61 llvm::outs() << "Pattern add(add(*), *) matched " << countMatches(f, matcher&: p2)
62 << " times\n";
63
64 auto p3 = m_Op<arith::AddFOp>(matchers: m_Any(), matchers: m_Op<arith::AddFOp>());
65 llvm::outs() << "Pattern add(*, add(*)) matched " << countMatches(f, matcher&: p3)
66 << " times\n";
67
68 auto p4 = m_Op<arith::MulFOp>(matchers: m_Op<arith::AddFOp>(), matchers: m_Any());
69 llvm::outs() << "Pattern mul(add(*), *) matched " << countMatches(f, matcher&: p4)
70 << " times\n";
71
72 auto p5 = m_Op<arith::MulFOp>(matchers: m_Any(), matchers: m_Op<arith::AddFOp>());
73 llvm::outs() << "Pattern mul(*, add(*)) matched " << countMatches(f, matcher&: p5)
74 << " times\n";
75
76 auto p6 = m_Op<arith::MulFOp>(matchers: m_Op<arith::MulFOp>(), matchers: m_Any());
77 llvm::outs() << "Pattern mul(mul(*), *) matched " << countMatches(f, matcher&: p6)
78 << " times\n";
79
80 auto p7 = m_Op<arith::MulFOp>(matchers: m_Op<arith::MulFOp>(), matchers: m_Op<arith::MulFOp>());
81 llvm::outs() << "Pattern mul(mul(*), mul(*)) matched " << countMatches(f, matcher&: p7)
82 << " times\n";
83
84 auto mulOfMulmul =
85 m_Op<arith::MulFOp>(matchers: m_Op<arith::MulFOp>(), matchers: m_Op<arith::MulFOp>());
86 auto p8 = m_Op<arith::MulFOp>(matchers: mulOfMulmul, matchers: mulOfMulmul);
87 llvm::outs()
88 << "Pattern mul(mul(mul(*), mul(*)), mul(mul(*), mul(*))) matched "
89 << countMatches(f, matcher&: p8) << " times\n";
90
91 // clang-format off
92 auto mulOfMuladd = m_Op<arith::MulFOp>(matchers: m_Op<arith::MulFOp>(), matchers: m_Op<arith::AddFOp>());
93 auto mulOfAnyadd = m_Op<arith::MulFOp>(matchers: m_Any(), matchers: m_Op<arith::AddFOp>());
94 auto p9 = m_Op<arith::MulFOp>(matchers: m_Op<arith::MulFOp>(
95 matchers: mulOfMuladd, matchers: m_Op<arith::MulFOp>()),
96 matchers: m_Op<arith::MulFOp>(matchers: mulOfAnyadd, matchers: mulOfAnyadd));
97 // clang-format on
98 llvm::outs() << "Pattern mul(mul(mul(mul(*), add(*)), mul(*)), mul(mul(*, "
99 "add(*)), mul(*, add(*)))) matched "
100 << countMatches(f, matcher&: p9) << " times\n";
101
102 auto p10 = m_Op<arith::AddFOp>(matchers: a, matchers: b);
103 llvm::outs() << "Pattern add(a, b) matched " << countMatches(f, matcher&: p10)
104 << " times\n";
105
106 auto p11 = m_Op<arith::AddFOp>(matchers: a, matchers: c);
107 llvm::outs() << "Pattern add(a, c) matched " << countMatches(f, matcher&: p11)
108 << " times\n";
109
110 auto p12 = m_Op<arith::AddFOp>(matchers: b, matchers: a);
111 llvm::outs() << "Pattern add(b, a) matched " << countMatches(f, matcher&: p12)
112 << " times\n";
113
114 auto p13 = m_Op<arith::AddFOp>(matchers: c, matchers: a);
115 llvm::outs() << "Pattern add(c, a) matched " << countMatches(f, matcher&: p13)
116 << " times\n";
117
118 auto p14 = m_Op<arith::MulFOp>(matchers: a, matchers: m_Op<arith::AddFOp>(matchers: c, matchers: b));
119 llvm::outs() << "Pattern mul(a, add(c, b)) matched " << countMatches(f, matcher&: p14)
120 << " times\n";
121
122 auto p15 = m_Op<arith::MulFOp>(matchers: a, matchers: m_Op<arith::AddFOp>(matchers: b, matchers: c));
123 llvm::outs() << "Pattern mul(a, add(b, c)) matched " << countMatches(f, matcher&: p15)
124 << " times\n";
125
126 auto mulOfAany = m_Op<arith::MulFOp>(matchers: a, matchers: m_Any());
127 auto p16 = m_Op<arith::MulFOp>(matchers: mulOfAany, matchers: m_Op<arith::AddFOp>(matchers: a, matchers: c));
128 llvm::outs() << "Pattern mul(mul(a, *), add(a, c)) matched "
129 << countMatches(f, matcher&: p16) << " times\n";
130
131 auto p17 = m_Op<arith::MulFOp>(matchers: mulOfAany, matchers: m_Op<arith::AddFOp>(matchers: c, matchers: b));
132 llvm::outs() << "Pattern mul(mul(a, *), add(c, b)) matched "
133 << countMatches(f, matcher&: p17) << " times\n";
134}
135
136static void test2(FunctionOpInterface f) {
137 auto a = m_Val(v: f.getArgument(idx: 0));
138 FloatAttr floatAttr;
139 auto p =
140 m_Op<arith::MulFOp>(matchers: a, matchers: m_Op<arith::AddFOp>(matchers: a, matchers: m_Constant(bind_value: &floatAttr)));
141 auto p1 = m_Op<arith::MulFOp>(matchers: a, matchers: m_Op<arith::AddFOp>(matchers: a, matchers: m_Constant()));
142 // Last operation that is not the terminator.
143 Operation *lastOp = f.getFunctionBody().front().back().getPrevNode();
144 if (p.match(op: lastOp))
145 llvm::outs()
146 << "Pattern add(add(a, constant), a) matched and bound constant to: "
147 << floatAttr.getValueAsDouble() << "\n";
148 if (p1.match(op: lastOp))
149 llvm::outs() << "Pattern add(add(a, constant), a) matched\n";
150}
151
152static void test3(FunctionOpInterface f) {
153 arith::FastMathFlagsAttr fastMathAttr;
154 auto p = m_Op<arith::MulFOp>(matchers: m_Any(),
155 matchers: m_Op<arith::AddFOp>(matchers: m_Any(), matchers: m_Op(opName: "test.name")));
156 auto p1 = m_Attr(attrName: "fastmath", bindValue: &fastMathAttr);
157
158 // Last operation that is not the terminator.
159 Operation *lastOp = f.getFunctionBody().front().back().getPrevNode();
160 if (p.match(op: lastOp))
161 llvm::outs() << "Pattern mul(*, add(*, m_Op(\"test.name\"))) matched\n";
162 if (p1.match(op: lastOp))
163 llvm::outs() << "Pattern m_Attr(\"fastmath\") matched and bound value to: "
164 << fastMathAttr.getValue() << "\n";
165}
166
167static void test4(FunctionOpInterface f) {
168 auto poison = ub::m_Poison();
169 llvm::outs() << "Pattern m_Poison() matched " << countMatches(f, matcher&: poison)
170 << " times\n";
171}
172
173void TestMatchers::runOnOperation() {
174 auto f = getOperation();
175 llvm::outs() << f.getName() << "\n";
176 if (f.getName() == "test1")
177 test1(f);
178 if (f.getName() == "test2")
179 test2(f);
180 if (f.getName() == "test3")
181 test3(f);
182 if (f.getName() == "test4")
183 test4(f);
184}
185
186namespace mlir {
187void registerTestMatchers() { PassRegistration<TestMatchers>(); }
188} // namespace mlir
189