MLIR  19.0.0git
MatchFinder.h
Go to the documentation of this file.
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.
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
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
std::enable_if_t< llvm::function_traits< std::decay_t< FnT > >::num_args==1, RetT > walk(FnT &&callback)
Walk the operation by calling the callback for each nested operation (including this one),...
Definition: Operation.h:793
bool match(Operation *op) const
static std::vector< Operation * > getMatches(Operation *root, DynMatcher matcher)
Definition: MatchFinder.h:25