xref: /llvm-project/mlir/include/mlir/Query/Matcher/MatchFinder.h (revision 02d9f4d1f128e17e04ab6e602d3c9b9942612428)
1 //===- MatchFinder.h - ------------------------------------------*- C++ -*-===//
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 // This file contains the MatchFinder class, which is used to find operations
10 // that match a given matcher.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_TOOLS_MLIRQUERY_MATCHER_MATCHERFINDER_H
15 #define MLIR_TOOLS_MLIRQUERY_MATCHER_MATCHERFINDER_H
16 
17 #include "MatchersInternal.h"
18 
19 namespace mlir::query::matcher {
20 
21 // MatchFinder is used to find all operations that match a given matcher.
22 class MatchFinder {
23 public:
24   // Returns all operations that match the given matcher.
getMatches(Operation * root,DynMatcher matcher)25   static std::vector<Operation *> getMatches(Operation *root,
26                                              DynMatcher matcher) {
27     std::vector<Operation *> matches;
28 
29     // Simple match finding with walk.
30     root->walk([&](Operation *subOp) {
31       if (matcher.match(subOp))
32         matches.push_back(subOp);
33     });
34 
35     return matches;
36   }
37 };
38 
39 } // namespace mlir::query::matcher
40 
41 #endif // MLIR_TOOLS_MLIRQUERY_MATCHER_MATCHERFINDER_H
42