tvm
Loading...
Searching...
No Matches
pattern_match.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
39#ifndef TVM_IR_PATTERN_MATCH_H_
40#define TVM_IR_PATTERN_MATCH_H_
41
42#include <tvm/ffi/cast.h>
43
44#include <optional>
45#include <tuple>
46#include <type_traits>
47#include <utility>
48
49namespace tvm {
62template <typename Derived>
63class Pattern {
64 public:
77 using Nested = Derived;
78
89 template <typename NodeType>
90 inline bool Match(const NodeType& value) const {
91 return Match(value, []() { return true; });
92 }
93
108 template <typename NodeType, typename Condition>
109 bool Match(const NodeType& value, Condition cond) const {
110 derived().InitMatch_();
111 return derived().Match_(value) && cond();
112 }
114 const Derived& derived() const { return *static_cast<const Derived*>(this); }
115};
116
121template <typename T>
123 public:
124 bool operator()(const T& lhs, const T& rhs) const { return lhs == rhs; }
125};
126
137template <typename T>
138class PVar : public Pattern<PVar<T>> {
139 public:
140 // Store PVars by reference in the expression.
141 using Nested = const PVar<T>&;
142
143 void InitMatch_() const { value_.reset(); }
144
145 bool Match_(const T& value) const {
146 if (!value_) {
147 value_ = value;
148 return true;
149 } else {
150 return PEqualChecker<T>()(*value_, value);
151 }
152 }
153
154 template <typename NodeRefType, typename = typename std::enable_if<
155 !std::is_same<NodeRefType, T>::value &&
156 std::is_base_of<ffi::ObjectRef, NodeRefType>::value &&
157 std::is_base_of<ffi::ObjectRef, T>::value>::type>
158 bool Match_(const NodeRefType& value) const {
159 if (auto typed_value = value.template as<T>()) {
160 return Match_(*typed_value);
161 } else {
162 return false;
163 }
164 }
165
166 T Eval() const {
167 TVM_FFI_ICHECK(value_.has_value());
168 return *value_;
169 }
170
171 T EvalOr(const T& default_value) const { return value_.value_or(default_value); }
172
173 protected:
175 mutable std::optional<T> value_;
176};
177
184template <typename Derived, typename T>
185class PVarWithCheck : public Pattern<PVarWithCheck<Derived, T>> {
186 public:
187 // Store by reference in the expression.
189
190 void InitMatch_() const { pvar_.InitMatch_(); }
191
192 bool Match_(const T& value) const {
193 if (!static_cast<const Derived*>(this)->Match_(value)) return false;
194 return pvar_.Match_(value);
195 }
196
197 template <typename NodeRefType,
198 typename = typename std::enable_if<std::is_base_of<NodeRefType, T>::value>::type>
199 bool Match_(const NodeRefType& value) const {
200 if (auto typed_value = value.template as<T>()) {
201 return Match_(*typed_value);
202 } else {
203 return false;
204 }
205 }
206
207 T Eval() const { return pvar_.Eval(); }
208
209 protected:
211};
212
218template <typename T>
219class PConst : public Pattern<PConst<T>> {
220 public:
221 PConst(T value) // NOLINT(*)
222 : value_(value) {}
223
224 void InitMatch_() const {}
225
226 bool Match_(const T& value) const { return PEqualChecker<T>()(value_, value); }
227
228 T Eval() const { return value_; }
229
230 private:
231 const T value_;
232};
233
234template <typename... TPattern>
236 public:
237 explicit PMatchesOneOf(const TPattern&... patterns) : patterns_{patterns...} {}
238
249 template <typename NodeType>
250 inline bool Match(const NodeType& value) const {
251 return Match(value, []() { return true; });
252 }
253
270 template <typename NodeType, typename Condition>
271 inline bool Match(const NodeType& value, Condition cond) const {
272 return MatchImpl(value, cond, std::make_index_sequence<sizeof...(TPattern)>());
273 }
274
275 private:
276 template <typename NodeType, typename Condition>
277 inline bool MatchImpl(const NodeType& value, Condition cond, std::index_sequence<>) const {
278 return false;
279 }
280
281 template <typename NodeType, typename Condition, size_t FirstIndex, size_t... RemainingIndices>
282 inline bool MatchImpl(const NodeType& value, Condition cond,
283 std::index_sequence<FirstIndex, RemainingIndices...>) const {
284 return std::get<FirstIndex>(patterns_).Match(value, cond) ||
285 MatchImpl(value, cond, std::index_sequence<RemainingIndices...>());
286 }
287
288 // Hold the patterns by const&. This follows the same usage as both
289 // the `PVar`, which occurs as `const PVar<T>&` when it appears
290 // inside other patterns. Because the `PVar<T>::value_` field is
291 // mutable, it can still be updated through these const references.
292 // So long as the call to `Match()` occurs within the same
293 // expression as created the patterns, this avoids accidental copies
294 // without creating dangling references. This may be improved in
295 // the future by use of `constexpr` constructors/operators, allowing
296 // more typical value semantics.
297 std::tuple<const TPattern&...> patterns_;
298};
299
300/* \brief Return a proxy object that returns true after the first match
301 *
302 * In the RewriteSimplifier, there are often several expressions that
303 * simplify to the same resulting expression. This utility allows
304 * them to be specified as a single rule, reducing duplication of the
305 * result/condition of a rewrite.
306 */
307template <typename... TPattern>
308inline std::enable_if_t<(std::is_base_of_v<Pattern<TPattern>, TPattern> && ... && true),
309 PMatchesOneOf<TPattern...>>
310MatchesOneOf(const TPattern&... patterns) {
311 return PMatchesOneOf<TPattern...>(patterns...);
312}
313} // namespace tvm
314#endif // TVM_IR_PATTERN_MATCH_H_
Constant Pattern variable container.
Definition pattern_match.h:219
void InitMatch_() const
Definition pattern_match.h:224
T Eval() const
Definition pattern_match.h:228
bool Match_(const T &value) const
Definition pattern_match.h:226
PConst(T value)
Definition pattern_match.h:221
Default deep equality checker.
Definition pattern_match.h:122
bool operator()(const T &lhs, const T &rhs) const
Definition pattern_match.h:124
Definition pattern_match.h:235
PMatchesOneOf(const TPattern &... patterns)
Definition pattern_match.h:237
bool Match(const NodeType &value, Condition cond) const
Check if value matches one of the patterns.
Definition pattern_match.h:271
bool Match(const NodeType &value) const
Check if value matches one of the patterns.
Definition pattern_match.h:250
Wrapper for pattern variable container with extra match logic.
Definition pattern_match.h:185
void InitMatch_() const
Definition pattern_match.h:190
bool Match_(const T &value) const
Definition pattern_match.h:192
const PVarWithCheck< Derived, T > & Nested
Definition pattern_match.h:188
PVar< T > pvar_
Definition pattern_match.h:210
bool Match_(const NodeRefType &value) const
Definition pattern_match.h:199
T Eval() const
Definition pattern_match.h:207
Pattern variable container.
Definition pattern_match.h:138
bool Match_(const T &value) const
Definition pattern_match.h:145
T EvalOr(const T &default_value) const
Definition pattern_match.h:171
void InitMatch_() const
Definition pattern_match.h:143
std::optional< T > value_
The matched value.
Definition pattern_match.h:175
const PVar< T > & Nested
Definition pattern_match.h:141
bool Match_(const NodeRefType &value) const
Definition pattern_match.h:158
T Eval() const
Definition pattern_match.h:166
Base class of all the patterns.
Definition pattern_match.h:63
bool Match(const NodeType &value, Condition cond) const
Check if value matches the current pattern.
Definition pattern_match.h:109
Derived Nested
Nested storage type in the expression.
Definition pattern_match.h:77
const Derived & derived() const
Definition pattern_match.h:114
bool Match(const NodeType &value) const
Check if value matches the current pattern.
Definition pattern_match.h:90
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
std::enable_if_t<(std::is_base_of_v< Pattern< TPattern >, TPattern > &&... &&true), PMatchesOneOf< TPattern... > > MatchesOneOf(const TPattern &... patterns)
Definition pattern_match.h:310