My Project
NestedMatcher.h
Go to the documentation of this file.
1 //===- NestedMacher.h - Nested matcher for Function -------------*- C++ -*-===//
2 //
3 // Part of the MLIR 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 #ifndef MLIR_ANALYSIS_MLFUNCTIONMATCHER_H_
10 #define MLIR_ANALYSIS_MLFUNCTIONMATCHER_H_
11 
12 #include "mlir/IR/Function.h"
13 #include "mlir/IR/Operation.h"
14 #include "llvm/Support/Allocator.h"
15 
16 namespace mlir {
17 
18 class NestedPattern;
19 class Operation;
20 
46 class NestedMatch {
47 public:
48  static NestedMatch build(Operation *operation,
49  ArrayRef<NestedMatch> nestedMatches);
50  NestedMatch(const NestedMatch &) = default;
51  NestedMatch &operator=(const NestedMatch &) = default;
52 
53  explicit operator bool() { return matchedOperation != nullptr; }
54 
55  Operation *getMatchedOperation() { return matchedOperation; }
56  ArrayRef<NestedMatch> getMatchedChildren() { return matchedChildren; }
57 
58 private:
59  friend class NestedPattern;
60  friend class NestedPatternContext;
61 
63  static llvm::BumpPtrAllocator *&allocator();
64 
65  NestedMatch() = default;
66 
68  Operation *matchedOperation;
69  ArrayRef<NestedMatch> matchedChildren;
70 };
71 
90 using FilterFunctionType = std::function<bool(Operation &)>;
91 inline bool defaultFilterFunction(Operation &) { return true; }
93 public:
96  NestedPattern(const NestedPattern &) = default;
97  NestedPattern &operator=(const NestedPattern &) = default;
98 
101  func.walk([&](Operation *op) { matchOne(op, matches); });
102  }
103 
106  op->walk([&](Operation *child) { matchOne(child, matches); });
107  }
108 
110  unsigned getDepth() const;
111 
112 private:
113  friend class NestedPatternContext;
114  friend class NestedMatch;
115  friend struct State;
116 
118  static llvm::BumpPtrAllocator *&allocator();
119 
122  void matchOne(Operation *op, SmallVectorImpl<NestedMatch> *matches);
123 
125  ArrayRef<NestedPattern> nestedPatterns;
126 
128  FilterFunctionType filter;
129 
143  Operation *skip;
144 };
145 
150 public:
152  assert(NestedMatch::allocator() == nullptr &&
153  "Only a single NestedPatternContext is supported");
154  assert(NestedPattern::allocator() == nullptr &&
155  "Only a single NestedPatternContext is supported");
156  NestedMatch::allocator() = &allocator;
157  NestedPattern::allocator() = &allocator;
158  }
160  NestedMatch::allocator() = nullptr;
161  NestedPattern::allocator() = nullptr;
162  }
163  llvm::BumpPtrAllocator allocator;
164 };
165 
166 namespace matcher {
167 // Syntactic sugar NestedPattern builder functions.
173  ArrayRef<NestedPattern> nested = {});
178  ArrayRef<NestedPattern> nested = {});
179 
180 bool isParallelLoop(Operation &op);
181 bool isReductionLoop(Operation &op);
182 bool isLoadOrStore(Operation &op);
183 
184 } // end namespace matcher
185 } // end namespace mlir
186 
187 #endif // MLIR_ANALYSIS_MLFUNCTIONMATCHER_H_
Definition: InferTypeOpInterface.cpp:20
~NestedPatternContext()
Definition: NestedMatcher.h:159
Definition: Operation.h:27
friend class NestedPattern
Definition: NestedMatcher.h:59
bool isLoadOrStore(Operation &op)
Definition: NestedMatcher.cpp:147
Definition: NestedMatcher.h:149
ArrayRef< NestedMatch > getMatchedChildren()
Definition: NestedMatcher.h:56
Definition: LLVM.h:34
NestedPattern If(NestedPattern child)
Definition: NestedMatcher.cpp:115
bool defaultFilterFunction(Operation &)
Definition: NestedMatcher.h:91
Operation * getMatchedOperation()
Definition: NestedMatcher.h:55
Definition: LLVM.h:37
Definition: Function.h:32
static NestedMatch build(Operation *operation, ArrayRef< NestedMatch > nestedMatches)
Definition: NestedMatcher.cpp:25
std::function< bool(Operation &)> FilterFunctionType
Definition: NestedMatcher.h:90
void match(FuncOp func, SmallVectorImpl< NestedMatch > *matches)
Returns all the top-level matches in func.
Definition: NestedMatcher.h:100
NestedPattern For(NestedPattern child)
Definition: NestedMatcher.cpp:132
void match(Operation *op, SmallVectorImpl< NestedMatch > *matches)
Returns all the top-level matches in op.
Definition: NestedMatcher.h:105
bool isParallelLoop(Operation &op)
Definition: NestedMatcher.h:46
bool isReductionLoop(Operation &op)
Definition: NestedMatcher.h:92
NestedPattern Op(FilterFunctionType filter)
Definition: NestedMatcher.cpp:111
NestedMatch & operator=(const NestedMatch &)=default
RetT walk(FnT &&callback)
Definition: OpDefinition.h:210
NestedPatternContext()
Definition: NestedMatcher.h:151
RetT walk(FnT &&callback)
Definition: Operation.h:530
llvm::BumpPtrAllocator allocator
Definition: NestedMatcher.h:163