tvm
Loading...
Searching...
No Matches
dataflow_pattern_functor.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
24#ifndef TVM_RELAX_DATAFLOW_PATTERN_FUNCTOR_H_
25#define TVM_RELAX_DATAFLOW_PATTERN_FUNCTOR_H_
26
28
29#include <unordered_set>
30#include <utility>
31
32namespace tvm {
33namespace relax {
34
42template <typename FType>
44
45// functions to be overriden.
46#define DFPATTERN_FUNCTOR_DEFAULT \
47 { \
48 return VisitDFPatternDefault_(op, std::forward<Args>(args)...); \
49 }
50
51#define RELAX_DFPATTERN_FUNCTOR_DISPATCH(OP) \
52 vtable.template SetDispatch<OP>([](const ffi::ObjectRef& n, TSelf* self, Args... args) { \
53 return self->VisitDFPattern_(static_cast<const OP*>(n.get()), std::forward<Args>(args)...); \
54 });
55
56template <typename R, typename... Args>
57class DFPatternFunctor<R(const DFPattern& n, Args...)> {
58 private:
59 using TSelf = DFPatternFunctor<R(const DFPattern& n, Args...)>;
60 using FType = tvm::ObjectFunctor<R(const ffi::ObjectRef& n, TSelf* self, Args...)>;
61
62 public:
64 virtual ~DFPatternFunctor() {}
71 R operator()(const DFPattern& n, Args... args) {
72 return VisitDFPattern(n, std::forward<Args>(args)...);
73 }
80 virtual R VisitDFPattern(const DFPattern& n, Args... args) {
81 TVM_FFI_ICHECK(n.defined());
82 static FType vtable = InitVTable();
83 return vtable(n, this, std::forward<Args>(args)...);
84 }
85 // Functions that can be overriden by subclass
86 virtual R VisitDFPattern_(const OrPatternObj* op, Args... args) DFPATTERN_FUNCTOR_DEFAULT;
87 virtual R VisitDFPattern_(const AndPatternObj* op, Args... args) DFPATTERN_FUNCTOR_DEFAULT;
88 virtual R VisitDFPattern_(const NotPatternObj* op, Args... args) DFPATTERN_FUNCTOR_DEFAULT;
89 virtual R VisitDFPattern_(const AttrPatternObj* op, Args... args) DFPATTERN_FUNCTOR_DEFAULT;
90 virtual R VisitDFPattern_(const CallPatternObj* op, Args... args) DFPATTERN_FUNCTOR_DEFAULT;
93 virtual R VisitDFPattern_(const ExprPatternObj* op, Args... args) DFPATTERN_FUNCTOR_DEFAULT;
95 virtual R VisitDFPattern_(const ShapePatternObj* op, Args... args) DFPATTERN_FUNCTOR_DEFAULT;
98 virtual R VisitDFPattern_(const TuplePatternObj* op, Args... args) DFPATTERN_FUNCTOR_DEFAULT;
99 virtual R VisitDFPattern_(const TypePatternObj* op, Args... args) DFPATTERN_FUNCTOR_DEFAULT;
101 virtual R VisitDFPattern_(const VarPatternObj* op, Args... args) DFPATTERN_FUNCTOR_DEFAULT;
102
104 Args... args) DFPATTERN_FUNCTOR_DEFAULT;
110
111 virtual R VisitDFPatternDefault_(const ffi::Object* op, Args...) {
112 TVM_FFI_THROW(InternalError) << "Do not have a default for " << op->GetTypeKey();
113 throw;
114 }
115
116 private:
117 // initialize the vtable.
118 static FType InitVTable() {
119 FType vtable;
120 // Set dispatch
141 vtable.Finalize();
142 return vtable;
143 }
144};
145
152class DFPatternVisitor : public DFPatternFunctor<void(const DFPattern&)> {
153 public:
154 void VisitDFPattern(const DFPattern& pattern) override;
155 void VisitDFPattern_(const OrPatternObj* op) override;
156 void VisitDFPattern_(const AndPatternObj* op) override;
157 void VisitDFPattern_(const NotPatternObj* op) override;
158 void VisitDFPattern_(const AttrPatternObj* op) override;
159 void VisitDFPattern_(const CallPatternObj* op) override;
160 void VisitDFPattern_(const ConstantPatternObj* op) override;
161 void VisitDFPattern_(const DataTypePatternObj* op) override;
162 void VisitDFPattern_(const ExprPatternObj* op) override;
163 void VisitDFPattern_(const FunctionPatternObj* op) override;
164 void VisitDFPattern_(const ShapePatternObj* op) override;
165 void VisitDFPattern_(const TupleGetItemPatternObj* op) override;
166 void VisitDFPattern_(const TuplePatternObj* op) override;
167 void VisitDFPattern_(const TypePatternObj* op) override;
168 void VisitDFPattern_(const WildcardPatternObj* op) override;
169 void VisitDFPattern_(const VarPatternObj* op) override;
170
171 void VisitDFPattern_(const DataflowVarPatternObj* op) override;
172 void VisitDFPattern_(const GlobalVarPatternObj* op) override;
173 void VisitDFPattern_(const ExternFuncPatternObj* op) override;
174 void VisitDFPattern_(const PrimArrPatternObj* op) override;
176
177 protected:
178 // set of already-visited nodes
179 std::unordered_set<const ffi::Object*> visited_;
180};
181
182} // namespace relax
183} // namespace tvm
184#endif // TVM_RELAX_DATAFLOW_PATTERN_FUNCTOR_H_
A dynamically dispatched functor on the type of the first argument.
Definition object_functor.h:85
Match a conjunction of other patterns.
Definition dataflow_pattern.h:675
A pattern that asserting a root pattern has certain attributes.
Definition dataflow_pattern.h:921
A pattern to match a callable node in Relax.
Definition dataflow_pattern.h:478
A Pattern to Match a Relax GenericConst.
Definition dataflow_pattern.h:452
virtual R VisitDFPattern_(const PrimArrPatternObj *op, Args... args)
Definition dataflow_pattern_functor.h:107
virtual R VisitDFPattern_(const NotPatternObj *op, Args... args)
Definition dataflow_pattern_functor.h:88
virtual R VisitDFPattern_(const AndPatternObj *op, Args... args)
Definition dataflow_pattern_functor.h:87
R operator()(const DFPattern &n, Args... args)
Same as call.
Definition dataflow_pattern_functor.h:71
virtual R VisitDFPattern_(const TuplePatternObj *op, Args... args)
Definition dataflow_pattern_functor.h:98
virtual R VisitDFPattern_(const WildcardPatternObj *op, Args... args)
Definition dataflow_pattern_functor.h:100
virtual R VisitDFPattern_(const GlobalVarPatternObj *op, Args... args)
Definition dataflow_pattern_functor.h:105
virtual R VisitDFPattern_(const AttrPatternObj *op, Args... args)
Definition dataflow_pattern_functor.h:89
virtual R VisitDFPattern_(const ExternFuncPatternObj *op, Args... args)
Definition dataflow_pattern_functor.h:106
virtual R VisitDFPattern_(const ShapePatternObj *op, Args... args)
Definition dataflow_pattern_functor.h:95
virtual R VisitDFPattern_(const DataTypePatternObj *op, Args... args)
Definition dataflow_pattern_functor.h:92
virtual R VisitDFPattern_(const UnorderedTuplePatternObj *op, Args... args)
Definition dataflow_pattern_functor.h:108
virtual R VisitDFPattern_(const ExprPatternObj *op, Args... args)
Definition dataflow_pattern_functor.h:93
virtual R VisitDFPattern_(const VarPatternObj *op, Args... args)
Definition dataflow_pattern_functor.h:101
virtual R VisitDFPattern_(const OrPatternObj *op, Args... args)
Definition dataflow_pattern_functor.h:86
virtual R VisitDFPattern_(const FunctionPatternObj *op, Args... args)
Definition dataflow_pattern_functor.h:94
virtual R VisitDFPatternDefault_(const ffi::Object *op, Args...)
Definition dataflow_pattern_functor.h:111
virtual R VisitDFPattern_(const TupleGetItemPatternObj *op, Args... args)
Definition dataflow_pattern_functor.h:96
virtual R VisitDFPattern_(const CallPatternObj *op, Args... args)
Definition dataflow_pattern_functor.h:90
virtual R VisitDFPattern(const DFPattern &n, Args... args)
The functor call.
Definition dataflow_pattern_functor.h:80
virtual ~DFPatternFunctor()
virtual destructor
Definition dataflow_pattern_functor.h:64
virtual R VisitDFPattern_(const ConstantPatternObj *op, Args... args)
Definition dataflow_pattern_functor.h:91
virtual R VisitDFPattern_(const DataflowVarPatternObj *op, Args... args)
Definition dataflow_pattern_functor.h:103
virtual R VisitDFPattern_(const TypePatternObj *op, Args... args)
Definition dataflow_pattern_functor.h:99
A dynamical functor that dispatches on in the first DFPattern argument.
Definition dataflow_pattern_functor.h:43
A simple visitor wrapper around DFPatternFunctor. Recursively visit the content.
Definition dataflow_pattern_functor.h:152
void VisitDFPattern_(const GlobalVarPatternObj *op) override
void VisitDFPattern_(const AndPatternObj *op) override
void VisitDFPattern_(const NotPatternObj *op) override
void VisitDFPattern_(const ConstantPatternObj *op) override
void VisitDFPattern_(const TuplePatternObj *op) override
void VisitDFPattern_(const ExprPatternObj *op) override
void VisitDFPattern_(const UnorderedTuplePatternObj *op) override
void VisitDFPattern_(const AttrPatternObj *op) override
void VisitDFPattern_(const ExternFuncPatternObj *op) override
void VisitDFPattern_(const DataTypePatternObj *op) override
std::unordered_set< const ffi::Object * > visited_
Definition dataflow_pattern_functor.h:179
void VisitDFPattern(const DFPattern &pattern) override
void VisitDFPattern_(const VarPatternObj *op) override
void VisitDFPattern_(const OrPatternObj *op) override
void VisitDFPattern_(const ShapePatternObj *op) override
void VisitDFPattern_(const WildcardPatternObj *op) override
void VisitDFPattern_(const CallPatternObj *op) override
void VisitDFPattern_(const DataflowVarPatternObj *op) override
void VisitDFPattern_(const TupleGetItemPatternObj *op) override
void VisitDFPattern_(const TypePatternObj *op) override
void VisitDFPattern_(const FunctionPatternObj *op) override
void VisitDFPattern_(const PrimArrPatternObj *op) override
Managed reference to dataflow patterns.
Definition dataflow_pattern.h:103
A pattern that asserting a root pattern has a certain data type.
Definition dataflow_pattern.h:890
A Pattern to Match a Relax Dataflow Variable.
Definition dataflow_pattern.h:406
Pattern for Relax Expression.
Definition dataflow_pattern.h:344
A pattern of external function.
Definition dataflow_pattern.h:953
A pattern to match a Relax Function.
Definition dataflow_pattern.h:546
A Pattern to Match a Relax Global Variable.
Definition dataflow_pattern.h:432
Pattern for rejecting a certain pattern.
Definition dataflow_pattern.h:739
Match a disjunction of other patterns.
Definition dataflow_pattern.h:707
A pattern to match an array of PrimExpr.
Definition dataflow_pattern.h:520
A pattern that asserting a root pattern has a certain shape.
Definition dataflow_pattern.h:827
A pattern to match n'th indexing to a tuple.
Definition dataflow_pattern.h:642
Pattern to match a tuple of ordered expressions.
Definition dataflow_pattern.h:589
Pattern for matching a certain type.
Definition dataflow_pattern.h:800
A pattern to match multiple expressions unorderedly.
Definition dataflow_pattern.h:614
A Pattern to Match a Relax Variable.
Definition dataflow_pattern.h:373
Wildcard Pattern is a pattern that can match anything.
Definition dataflow_pattern.h:767
A pattern language for matching dataflow properties.
#define RELAX_DFPATTERN_FUNCTOR_DISPATCH(OP)
Definition dataflow_pattern_functor.h:51
#define DFPATTERN_FUNCTOR_DEFAULT
Definition dataflow_pattern_functor.h:46
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41