MLIR  21.0.0git
MatchersInternal.h
Go to the documentation of this file.
1 //===- MatchersInternal.h - Structural query framework ----------*- 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 // Implements the base layer of the matcher framework.
10 //
11 // Matchers are methods that return a Matcher which provides a method one of the
12 // following methods: match(Operation *op), match(Operation *op,
13 // SetVector<Operation *> &matchedOps)
14 //
15 // The matcher functions are defined in include/mlir/IR/Matchers.h.
16 // This file contains the wrapper classes needed to construct matchers for
17 // mlir-query.
18 //
19 //===----------------------------------------------------------------------===//
20 
21 #ifndef MLIR_TOOLS_MLIRQUERY_MATCHER_MATCHERSINTERNAL_H
22 #define MLIR_TOOLS_MLIRQUERY_MATCHER_MATCHERSINTERNAL_H
23 
24 #include "mlir/IR/Matchers.h"
25 #include "llvm/ADT/IntrusiveRefCntPtr.h"
26 
27 namespace mlir::query::matcher {
28 
29 // Defaults to false if T has no match() method with the signature:
30 // match(Operation* op).
31 template <typename T, typename = void>
32 struct has_simple_match : std::false_type {};
33 
34 // Specialized type trait that evaluates to true if T has a match() method
35 // with the signature: match(Operation* op).
36 template <typename T>
37 struct has_simple_match<T, std::void_t<decltype(std::declval<T>().match(
38  std::declval<Operation *>()))>>
39  : std::true_type {};
40 
41 // Defaults to false if T has no match() method with the signature:
42 // match(Operation* op, SetVector<Operation*>&).
43 template <typename T, typename = void>
44 struct has_bound_match : std::false_type {};
45 
46 // Specialized type trait that evaluates to true if T has a match() method
47 // with the signature: match(Operation* op, SetVector<Operation*>&).
48 template <typename T>
49 struct has_bound_match<T, std::void_t<decltype(std::declval<T>().match(
50  std::declval<Operation *>(),
51  std::declval<SetVector<Operation *> &>()))>>
52  : std::true_type {};
53 
54 // Generic interface for matchers on an MLIR operation.
56  : public llvm::ThreadSafeRefCountedBase<MatcherInterface> {
57 public:
58  virtual ~MatcherInterface() = default;
59 
60  virtual bool match(Operation *op) = 0;
61  virtual bool match(Operation *op, SetVector<Operation *> &matchedOps) = 0;
62 };
63 
64 // MatcherFnImpl takes a matcher function object and implements
65 // MatcherInterface.
66 template <typename MatcherFn>
68 public:
69  MatcherFnImpl(MatcherFn &matcherFn) : matcherFn(matcherFn) {}
70 
71  bool match(Operation *op) override {
73  return matcherFn.match(op);
74  return false;
75  }
76 
77  bool match(Operation *op, SetVector<Operation *> &matchedOps) override {
79  return matcherFn.match(op, matchedOps);
80  return false;
81  }
82 
83 private:
84  MatcherFn matcherFn;
85 };
86 
87 // Matcher wraps a MatcherInterface implementation and provides match()
88 // methods that redirect calls to the underlying implementation.
89 class DynMatcher {
90 public:
91  // Takes ownership of the provided implementation pointer.
92  DynMatcher(MatcherInterface *implementation)
93  : implementation(implementation) {}
94 
95  template <typename MatcherFn>
96  static std::unique_ptr<DynMatcher>
97  constructDynMatcherFromMatcherFn(MatcherFn &matcherFn) {
98  auto impl = std::make_unique<MatcherFnImpl<MatcherFn>>(matcherFn);
99  return std::make_unique<DynMatcher>(impl.release());
100  }
101 
102  bool match(Operation *op) const { return implementation->match(op); }
103  bool match(Operation *op, SetVector<Operation *> &matchedOps) const {
104  return implementation->match(op, matchedOps);
105  }
106 
107  void setFunctionName(StringRef name) { functionName = name.str(); }
108  bool hasFunctionName() const { return !functionName.empty(); }
109  StringRef getFunctionName() const { return functionName; }
110 
111 private:
112  llvm::IntrusiveRefCntPtr<MatcherInterface> implementation;
113  std::string functionName;
114 };
115 
116 } // namespace mlir::query::matcher
117 
118 #endif // MLIR_TOOLS_MLIRQUERY_MATCHER_MATCHERSINTERNAL_H
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
DynMatcher(MatcherInterface *implementation)
bool match(Operation *op) const
static std::unique_ptr< DynMatcher > constructDynMatcherFromMatcherFn(MatcherFn &matcherFn)
bool match(Operation *op, SetVector< Operation * > &matchedOps) const
void setFunctionName(StringRef name)
bool match(Operation *op, SetVector< Operation * > &matchedOps) override
bool match(Operation *op) override
virtual bool match(Operation *op, SetVector< Operation * > &matchedOps)=0
virtual bool match(Operation *op)=0
A matcher encapsulating getBackwardSlice method from SliceAnalysis.h.
Definition: ErrorBuilder.h:20