MLIR  21.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 /// A class that provides utilities to find operations in the IR.
25 class MatchFinder {
26 
27 public:
28  /// A subclass which preserves the matching information. Each instance
29  /// contains the `rootOp` along with the matching environment.
30  struct MatchResult {
31  MatchResult() = default;
32  MatchResult(Operation *rootOp, std::vector<Operation *> matchedOps);
33 
34  Operation *rootOp = nullptr;
35  /// Contains the matching environment.
36  std::vector<Operation *> matchedOps;
37  };
38 
39  /// Traverses the IR and returns a vector of `MatchResult` for each match of
40  /// the `matcher`.
41  std::vector<MatchResult> collectMatches(Operation *root,
42  DynMatcher matcher) const;
43 
44  /// Prints the matched operation.
45  void printMatch(llvm::raw_ostream &os, QuerySession &qs, Operation *op) const;
46 
47  /// Labels the matched operation with the given binding (e.g., `"root"`) and
48  /// prints it.
49  void printMatch(llvm::raw_ostream &os, QuerySession &qs, Operation *op,
50  const std::string &binding) const;
51 
52  /// Flattens a vector of `MatchResult` into a vector of operations.
53  std::vector<Operation *>
54  flattenMatchedOps(std::vector<MatchResult> &matches) const;
55 };
56 
57 } // namespace mlir::query::matcher
58 
59 #endif // MLIR_TOOLS_MLIRQUERY_MATCHER_MATCHERFINDER_H
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
A class that provides utilities to find operations in the IR.
Definition: MatchFinder.h:25
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
A matcher encapsulating getBackwardSlice method from SliceAnalysis.h.
Definition: ErrorBuilder.h:20
A subclass which preserves the matching information.
Definition: MatchFinder.h:30
std::vector< Operation * > matchedOps
Contains the matching environment.
Definition: MatchFinder.h:36