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