tvm
Loading...
Searching...
No Matches
binding_rewrite.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
25#ifndef TVM_RELAX_BINDING_REWRITE_H_
26
27#include <tvm/ffi/reflection/registry.h>
29#include <tvm/relax/analysis.h>
30#include <tvm/relax/expr.h>
31
32#include <map>
33#include <set>
34#include <type_traits>
35#include <utility>
36#include <vector>
37
38namespace tvm {
39namespace relax {
40
42class DataflowBlockRewriteNode : public ffi::Object {
43 public:
49 void Add(ffi::String var_name, Expr expr, bool is_dfvar = false) {
50 auto var = is_dfvar ? DataflowVar(var_name, GetType(expr)) //
51 : Var(var_name, GetType(expr));
52 Add(VarBinding(std::move(var), std::move(expr)));
53 }
55 void Add(Expr expr, bool is_dfvar = false) {
56 Add(name_supply_->FreshName("tmp"), expr, is_dfvar);
57 }
59 void RemoveUnused(Var unused, bool allow_undef = false);
62
66 Function MutatedFunc() { return root_fn_.value(); }
69
71 static void RegisterReflection() {
72 namespace refl = tvm::ffi::reflection;
73 refl::ObjectDef<DataflowBlockRewriteNode>()
74 .def_ro("dfb", &DataflowBlockRewriteNode::dfb_)
75 .def_ro("root_fn", &DataflowBlockRewriteNode::root_fn_);
76 }
78 ffi::Object);
79
80 protected:
82
84 ffi::Optional<Function> root_fn_;
86 ffi::Map<Var, ffi::Array<Var>> to_users_;
87 ffi::Array<Var> fn_outputs_;
88
89 private:
90 UniqueNameSupply name_supply_;
91};
92
97class DataflowBlockRewrite : public ffi::ObjectRef {
98 public:
100
106 TVM_FFI_ICHECK(get() != nullptr);
107 return static_cast<DataflowBlockRewriteNode*>(get_mutable());
108 }
109
112};
113
114} // namespace relax
115} // namespace tvm
116
117#define TVM_RELAX_BINDING_REWRITE_H_
118#endif // TVM_RELAX_BINDING_REWRITE_H_
Managed reference to ExprNode.
Definition base_expr.h:335
Managed reference class to IRModuleNode.
Definition module.h:255
Managed reference class to UniqueNameSupplyNode.
Definition unique_name_supply.h:115
Managed reference to VarNode.
Definition expr.h:372
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
Definition expr.h:211
Statement rewriter for relax.DataflowBlock.
Definition binding_rewrite.h:42
DataflowBlock MutatedDataflowBlock()
The rewritten dataflow block.
Definition binding_rewrite.h:64
void Add(ffi::String var_name, Expr expr, bool is_dfvar=false)
Insert an expression as VarBinding with variable name.
Definition binding_rewrite.h:49
const FunctionNode * original_fn_ptr_
Pointer to the original function.
Definition binding_rewrite.h:85
ffi::Array< Var > fn_outputs_
Variables required by function outputs.
Definition binding_rewrite.h:87
Function MutatedFunc()
The rewritten function.
Definition binding_rewrite.h:66
ffi::Map< Var, ffi::Array< Var > > to_users_
Map from variable to its users.
Definition binding_rewrite.h:86
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.DataflowBlockRewrite", DataflowBlockRewriteNode, ffi::Object)
ffi::Optional< Function > root_fn_
The rewritten function.
Definition binding_rewrite.h:84
static void RegisterReflection()
Visit attributes.
Definition binding_rewrite.h:71
void Add(Expr expr, bool is_dfvar=false)
Insert an expression as VarBinding with automatic variable name.
Definition binding_rewrite.h:55
void RemoveUnused(Var unused, bool allow_undef=false)
Remove the definition statement of an unused variable.
IRModule MutateIRModule(IRModule irmod)
The rewritten IRModule.
void ReplaceAllUses(Var old_var, Var new_var)
Replace all uses of old_var with new_var.
void RemoveAllUnused()
Remove the definition statements of all unused variables.
DataflowBlock dfb_
The rewritten dataflow block.
Definition binding_rewrite.h:83
void Add(Binding binding)
Insert a Binding statement.
A statement rewriter for relax.DataflowBlock.
Definition binding_rewrite.h:97
DataflowBlockRewriteNode * operator->()
mutable accessor.
Definition binding_rewrite.h:105
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(DataflowBlockRewrite, ffi::ObjectRef, DataflowBlockRewriteNode)
DataflowBlockRewrite(DataflowBlock dfb, Function root_fn)
Definition expr.h:325
Definition expr.h:80
A Relax function.
Definition expr.h:447
Definition expr.h:471
Definition expr.h:283
Type GetType(const Expr &expr)
Get the underlying Relax type of expr.
Definition type.h:400
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
The set of Relax specific analysis on IR.
UniqueNameSupply that can be used to generate unique variable names.