MLIR  22.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 and print them.
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 #include "mlir/Query/Query.h"
20 #include "llvm/ADT/SetVector.h"
21 
22 namespace mlir::query::matcher {
23 
24 /// Finds and collects matches from the IR. After construction
25 /// `collectMatches` can be used to traverse the IR and apply
26 /// matchers.
27 class MatchFinder {
28 
29 public:
30  /// A subclass which preserves the matching information. Each instance
31  /// contains the `rootOp` along with the matching environment.
32  struct MatchResult {
33  MatchResult() = default;
34  MatchResult(Operation *rootOp, std::vector<Operation *> matchedOps);
35 
36  Operation *rootOp = nullptr;
37  /// Contains the matching environment.
38  std::vector<Operation *> matchedOps;
39  };
40 
41  /// Traverses the IR and returns a vector of `MatchResult` for each match of
42  /// the `matcher`.
43  std::vector<MatchResult> collectMatches(Operation *root,
44  DynMatcher matcher) const;
45 
46  /// Prints the matched operation.
47  void printMatch(llvm::raw_ostream &os, QuerySession &qs, Operation *op) const;
48 
49  /// Labels the matched operation with the given binding (e.g., `"root"`) and
50  /// prints it.
51  void printMatch(llvm::raw_ostream &os, QuerySession &qs, Operation *op,
52  const std::string &binding) const;
53 
54  /// Flattens a vector of `MatchResult` into a vector of operations.
55  std::vector<Operation *>
56  flattenMatchedOps(std::vector<MatchResult> &matches) const;
57 };
58 
59 } // namespace mlir::query::matcher
60 
61 #endif // MLIR_TOOLS_MLIRQUERY_MATCHER_MATCHERFINDER_H
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
Finds and collects matches from the IR.
Definition: MatchFinder.h:27
void printMatch(llvm::raw_ostream &os, QuerySession &qs, Operation *op) const
Prints the matched operation.
Definition: MatchFinder.cpp:39
std::vector< MatchResult > collectMatches(Operation *root, DynMatcher matcher) const
Traverses the IR and returns a vector of MatchResult for each match of the matcher.
Definition: MatchFinder.cpp:21
std::vector< Operation * > flattenMatchedOps(std::vector< MatchResult > &matches) const
Flattens a vector of MatchResult into a vector of operations.
Definition: MatchFinder.cpp:59
Computes the backward-slice of all transitive defs reachable from rootOp, if innerMatcher matches.
Definition: ErrorBuilder.h:20
A subclass which preserves the matching information.
Definition: MatchFinder.h:32
std::vector< Operation * > matchedOps
Contains the matching environment.
Definition: MatchFinder.h:38