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 DataflowBlockRewriteObj : public ffi::Object {
43 public:
44 explicit DataflowBlockRewriteObj(DataflowBlock value) : dfb_(std::move(value)) {}
45 explicit DataflowBlockRewriteObj(ffi::UnsafeInit) : dfb_(ffi::UnsafeInit{}) {}
46
48 void ReplaceAllUses(Var old_var, Var new_var);
50 void Add(Binding binding);
52 void Add(ffi::String var_name, Expr expr, bool is_dfvar = false) {
53 auto var = is_dfvar ? DataflowVar(var_name, GetType(expr)) //
54 : Var(var_name, GetType(expr));
55 Add(VarBinding(std::move(var), std::move(expr)));
56 }
58 void Add(Expr expr, bool is_dfvar = false) {
59 Add(name_supply_->FreshName("tmp"), expr, is_dfvar);
60 }
62 void RemoveUnused(Var unused, bool allow_undef = false);
65
69 Function MutatedFunc() { return root_fn_.value(); }
72
74 static void RegisterReflection() {
75 namespace refl = tvm::ffi::reflection;
76 refl::ObjectDef<DataflowBlockRewriteObj>()
77 .def_ro("dfb", &DataflowBlockRewriteObj::dfb_)
78 .def_ro("root_fn", &DataflowBlockRewriteObj::root_fn_);
79 }
81 ffi::Object);
82
83 protected:
85
87 ffi::Optional<Function> root_fn_;
89 ffi::Map<Var, ffi::Array<Var>> to_users_;
90 ffi::Array<Var> fn_outputs_;
91
92 private:
93 UniqueNameSupply name_supply_;
94};
95
100class DataflowBlockRewrite : public ffi::ObjectRef {
101 public:
102 TVM_DLL explicit DataflowBlockRewrite(DataflowBlock dfb, Function root_fn);
103
109 TVM_FFI_ICHECK(get() != nullptr);
110 return static_cast<DataflowBlockRewriteObj*>(get_mutable());
111 }
112
115};
116
117} // namespace relax
118} // namespace tvm
119
120#define TVM_RELAX_BINDING_REWRITE_H_
121#endif // TVM_RELAX_BINDING_REWRITE_H_
Managed reference to ExprNode.
Definition base_expr.h:359
Managed reference class to IRModuleNode.
Definition module.h:255
Managed reference class to UniqueNameSupplyObj.
Definition unique_name_supply.h:115
Managed reference to VarNode.
Definition expr.h:714
Definition expr.h:120
Statement rewriter for relax.DataflowBlock.
Definition binding_rewrite.h:42
ffi::Array< Var > fn_outputs_
Variables required by function outputs.
Definition binding_rewrite.h:90
IRModule MutateIRModule(IRModule irmod)
The rewritten IRModule.
Function MutatedFunc()
The rewritten function.
Definition binding_rewrite.h:69
ffi::Optional< Function > root_fn_
The rewritten function.
Definition binding_rewrite.h:87
void Add(Expr expr, bool is_dfvar=false)
Insert an expression as VarBinding with automatic variable name.
Definition binding_rewrite.h:58
void Add(ffi::String var_name, Expr expr, bool is_dfvar=false)
Insert an expression as VarBinding with variable name.
Definition binding_rewrite.h:52
DataflowBlock MutatedDataflowBlock()
The rewritten dataflow block.
Definition binding_rewrite.h:67
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.DataflowBlockRewrite", DataflowBlockRewriteObj, ffi::Object)
const FunctionNode * original_fn_ptr_
Pointer to the original function.
Definition binding_rewrite.h:88
static void RegisterReflection()
Visit attributes.
Definition binding_rewrite.h:74
DataflowBlockRewriteObj(DataflowBlock value)
Definition binding_rewrite.h:44
DataflowBlock dfb_
The rewritten dataflow block.
Definition binding_rewrite.h:86
void RemoveAllUnused()
Remove the definition statements of all unused variables.
void RemoveUnused(Var unused, bool allow_undef=false)
Remove the definition statement of an unused variable.
ffi::Map< Var, ffi::Array< Var > > to_users_
Map from variable to its users.
Definition binding_rewrite.h:89
void ReplaceAllUses(Var old_var, Var new_var)
Replace all uses of old_var with new_var.
DataflowBlockRewriteObj(ffi::UnsafeInit)
Definition binding_rewrite.h:45
void Add(Binding binding)
Insert a Binding statement.
A statement rewriter for relax.DataflowBlock.
Definition binding_rewrite.h:100
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(DataflowBlockRewrite, ffi::ObjectRef, DataflowBlockRewriteObj)
DataflowBlockRewriteObj * operator->()
mutable accessor.
Definition binding_rewrite.h:108
DataflowBlockRewrite(DataflowBlock dfb, Function root_fn)
Definition expr.h:248
Definition expr.h:82
A Relax function.
Definition expr.h:388
Definition expr.h:415
Definition expr.h:202
Type GetType(const Expr &expr)
Get the underlying Relax type of expr.
Definition type.h:382
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
The set of Relax specific analysis on IR.
UniqueNameSupply that can be used to generate unique variable names.