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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
//===- TestMatchers.cpp - Pass to test matchers ---------------------------===//
//
// Part of the LLVM 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/Arith/IR/Arith.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/FunctionInterfaces.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 PassWrapper<TestMatchers, InterfacePass<FunctionOpInterface>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestMatchers)
void runOnOperation() override;
StringRef getArgument() const final { return "test-matchers"; }
StringRef getDescription() const final {
return "Test C++ pattern matchers.";
}
};
} // namespace
// This could be done better but is not worth the variadic template trouble.
template <typename Matcher>
static unsigned countMatches(FunctionOpInterface 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(FunctionOpInterface 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<arith::AddFOp>(); // using 0-arity matcher
llvm::outs() << "Pattern add(*) matched " << countMatches(f, p0)
<< " times\n";
auto p1 = m_Op<arith::MulFOp>(); // using 0-arity matcher
llvm::outs() << "Pattern mul(*) matched " << countMatches(f, p1)
<< " times\n";
auto p2 = m_Op<arith::AddFOp>(m_Op<arith::AddFOp>(), m_Any());
llvm::outs() << "Pattern add(add(*), *) matched " << countMatches(f, p2)
<< " times\n";
auto p3 = m_Op<arith::AddFOp>(m_Any(), m_Op<arith::AddFOp>());
llvm::outs() << "Pattern add(*, add(*)) matched " << countMatches(f, p3)
<< " times\n";
auto p4 = m_Op<arith::MulFOp>(m_Op<arith::AddFOp>(), m_Any());
llvm::outs() << "Pattern mul(add(*), *) matched " << countMatches(f, p4)
<< " times\n";
auto p5 = m_Op<arith::MulFOp>(m_Any(), m_Op<arith::AddFOp>());
llvm::outs() << "Pattern mul(*, add(*)) matched " << countMatches(f, p5)
<< " times\n";
auto p6 = m_Op<arith::MulFOp>(m_Op<arith::MulFOp>(), m_Any());
llvm::outs() << "Pattern mul(mul(*), *) matched " << countMatches(f, p6)
<< " times\n";
auto p7 = m_Op<arith::MulFOp>(m_Op<arith::MulFOp>(), m_Op<arith::MulFOp>());
llvm::outs() << "Pattern mul(mul(*), mul(*)) matched " << countMatches(f, p7)
<< " times\n";
auto mulOfMulmul =
m_Op<arith::MulFOp>(m_Op<arith::MulFOp>(), m_Op<arith::MulFOp>());
auto p8 = m_Op<arith::MulFOp>(mulOfMulmul, mulOfMulmul);
llvm::outs()
<< "Pattern mul(mul(mul(*), mul(*)), mul(mul(*), mul(*))) matched "
<< countMatches(f, p8) << " times\n";
// clang-format off
auto mulOfMuladd = m_Op<arith::MulFOp>(m_Op<arith::MulFOp>(), m_Op<arith::AddFOp>());
auto mulOfAnyadd = m_Op<arith::MulFOp>(m_Any(), m_Op<arith::AddFOp>());
auto p9 = m_Op<arith::MulFOp>(m_Op<arith::MulFOp>(
mulOfMuladd, m_Op<arith::MulFOp>()),
m_Op<arith::MulFOp>(mulOfAnyadd, mulOfAnyadd));
// 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<arith::AddFOp>(a, b);
llvm::outs() << "Pattern add(a, b) matched " << countMatches(f, p10)
<< " times\n";
auto p11 = m_Op<arith::AddFOp>(a, c);
llvm::outs() << "Pattern add(a, c) matched " << countMatches(f, p11)
<< " times\n";
auto p12 = m_Op<arith::AddFOp>(b, a);
llvm::outs() << "Pattern add(b, a) matched " << countMatches(f, p12)
<< " times\n";
auto p13 = m_Op<arith::AddFOp>(c, a);
llvm::outs() << "Pattern add(c, a) matched " << countMatches(f, p13)
<< " times\n";
auto p14 = m_Op<arith::MulFOp>(a, m_Op<arith::AddFOp>(c, b));
llvm::outs() << "Pattern mul(a, add(c, b)) matched " << countMatches(f, p14)
<< " times\n";
auto p15 = m_Op<arith::MulFOp>(a, m_Op<arith::AddFOp>(b, c));
llvm::outs() << "Pattern mul(a, add(b, c)) matched " << countMatches(f, p15)
<< " times\n";
auto mulOfAany = m_Op<arith::MulFOp>(a, m_Any());
auto p16 = m_Op<arith::MulFOp>(mulOfAany, m_Op<arith::AddFOp>(a, c));
llvm::outs() << "Pattern mul(mul(a, *), add(a, c)) matched "
<< countMatches(f, p16) << " times\n";
auto p17 = m_Op<arith::MulFOp>(mulOfAany, m_Op<arith::AddFOp>(c, b));
llvm::outs() << "Pattern mul(mul(a, *), add(c, b)) matched "
<< countMatches(f, p17) << " times\n";
}
void test2(FunctionOpInterface f) {
auto a = m_Val(f.getArgument(0));
FloatAttr floatAttr;
auto p =
m_Op<arith::MulFOp>(a, m_Op<arith::AddFOp>(a, m_Constant(&floatAttr)));
auto p1 = m_Op<arith::MulFOp>(a, m_Op<arith::AddFOp>(a, m_Constant()));
// Last operation that is not the terminator.
Operation *lastOp = f.getFunctionBody().front().back().getPrevNode();
if (p.match(lastOp))
llvm::outs()
<< "Pattern add(add(a, constant), a) matched and bound constant to: "
<< floatAttr.getValueAsDouble() << "\n";
if (p1.match(lastOp))
llvm::outs() << "Pattern add(add(a, constant), a) matched\n";
}
void test3(FunctionOpInterface f) {
arith::FastMathFlagsAttr fastMathAttr;
auto p = m_Op<arith::MulFOp>(m_Any(),
m_Op<arith::AddFOp>(m_Any(), m_Op("test.name")));
auto p1 = m_Attr("fastmath", &fastMathAttr);
// Last operation that is not the terminator.
Operation *lastOp = f.getFunctionBody().front().back().getPrevNode();
if (p.match(lastOp))
llvm::outs() << "Pattern mul(*, add(*, m_Op(\"test.name\"))) matched\n";
if (p1.match(lastOp))
llvm::outs() << "Pattern m_Attr(\"fastmath\") matched and bound value to: "
<< fastMathAttr.getValue() << "\n";
}
void TestMatchers::runOnOperation() {
auto f = getOperation();
llvm::outs() << f.getName() << "\n";
if (f.getName() == "test1")
test1(f);
if (f.getName() == "test2")
test2(f);
if (f.getName() == "test3")
test3(f);
}
namespace mlir {
void registerTestMatchers() { PassRegistration<TestMatchers>(); }
} // namespace mlir
|