1010// goal is to move this to include/mlir/IR/Matchers.h after the initial testing
1111// phase. The matchers in this file are:
1212//
13- // - operation(args...): Matches an operation that matches all of the matchers
14- // in the vector `matchers`.
13+ // - operation(args...): Matches all of the matchers in the vector `matchers`.
1514//
1615// - argument(innerMatcher, index): Matches an operation argument that matches
1716// `innerMatcher` at the given `index`.
@@ -39,11 +38,9 @@ namespace extramatcher {
3938
4039namespace detail {
4140
42- // TODO: Rename to AllOf
43- // OperationMatcher takes a vector of DynMatchers and returns true if all
44- // DynMatchers match the given operation.
45- struct OperationMatcher {
46- OperationMatcher (std::vector<matcher::DynMatcher> matchers)
41+ // AllOf takes a vector of DynMatchers and returns true if all the DynMatchers match the given operation.
42+ struct AllOfMatcher {
43+ AllOfMatcher (std::vector<matcher::DynMatcher> matchers)
4744 : matchers(matchers) {}
4845 bool match (Operation *op) {
4946 matcher::DynTypedNode node = matcher::DynTypedNode::create (op);
@@ -54,6 +51,19 @@ struct OperationMatcher {
5451 std::vector<matcher::DynMatcher> matchers;
5552};
5653
54+ // AnyOf takes a vector of DynMatchers and returns true if any of the DynMatchers match the given operation.
55+ struct AnyOfMatcher {
56+ AnyOfMatcher (std::vector<matcher::DynMatcher> matchers)
57+ : matchers(matchers) {}
58+ bool match (Operation *op) {
59+ matcher::DynTypedNode node = matcher::DynTypedNode::create (op);
60+ return llvm::any_of (matchers, [&](const matcher::DynMatcher &matcher) {
61+ return matcher.matches (node);
62+ });
63+ }
64+ std::vector<matcher::DynMatcher> matchers;
65+ };
66+
5767// ArgumentMatcher matches the operand of an operation at a specific index.
5868struct ArgumentMatcher {
5969 ArgumentMatcher (matcher::DynMatcher innerMatcher, unsigned index)
@@ -145,10 +155,14 @@ struct DefinedByMatcher {
145155
146156} // namespace detail
147157
148- // TODO: Rename to allOf()
149- inline detail::OperationMatcher operation (matcher::DynMatcher args...) {
158+ inline detail::AllOfMatcher allOf (matcher::DynMatcher args...) {
159+ std::vector<matcher::DynMatcher> matchers ({args});
160+ return detail::AllOfMatcher (matchers);
161+ }
162+
163+ inline detail::AnyOfMatcher anyOf (matcher::DynMatcher args...) {
150164 std::vector<matcher::DynMatcher> matchers ({args});
151- return detail::OperationMatcher (matchers);
165+ return detail::AnyOfMatcher (matchers);
152166}
153167
154168inline detail::ArgumentMatcher hasArgument (matcher::DynMatcher innerMatcher,
0 commit comments