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/base.h>
28#include <tvm/ffi/reflection/registry.h>
30#include <tvm/relax/analysis.h>
31#include <tvm/relax/expr.h>
32
33#include <utility>
34
35namespace tvm {
36namespace relax {
37
39class DataflowBlockRewriteObj : public ffi::Object {
40 public:
41 explicit DataflowBlockRewriteObj(DataflowBlock value) : dfb_(std::move(value)) {}
42 explicit DataflowBlockRewriteObj(ffi::UnsafeInit) : dfb_(ffi::UnsafeInit{}) {}
43
45 void ReplaceAllUses(Var old_var, Var new_var);
47 void Add(Binding binding);
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<DataflowBlockRewriteObj>()
74 .def_ro("dfb", &DataflowBlockRewriteObj::dfb_)
75 .def_ro("root_fn", &DataflowBlockRewriteObj::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:
99 TVM_DLL explicit DataflowBlockRewrite(DataflowBlock dfb, Function root_fn);
100
106 TVM_FFI_ICHECK(get() != nullptr);
107 return static_cast<DataflowBlockRewriteObj*>(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 ExprObj.
Definition expr.h:101
Managed reference class to IRModuleObj.
Definition module.h:292
Managed reference class to UniqueNameSupplyObj.
Definition unique_name_supply.h:115
Managed reference to VarObj.
Definition expr.h:571
Definition expr.h:123
Statement rewriter for relax.DataflowBlock.
Definition binding_rewrite.h:39
ffi::Array< Var > fn_outputs_
Variables required by function outputs.
Definition binding_rewrite.h:87
IRModule MutateIRModule(IRModule irmod)
The rewritten IRModule.
Function MutatedFunc()
The rewritten function.
Definition binding_rewrite.h:66
ffi::Optional< Function > root_fn_
The rewritten function.
Definition binding_rewrite.h:84
void Add(Expr expr, bool is_dfvar=false)
Insert an expression as VarBinding with automatic variable name.
Definition binding_rewrite.h:55
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
DataflowBlock MutatedDataflowBlock()
The rewritten dataflow block.
Definition binding_rewrite.h:64
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.DataflowBlockRewrite", DataflowBlockRewriteObj, ffi::Object)
const FunctionObj * original_fn_ptr_
Pointer to the original function.
Definition binding_rewrite.h:85
static void RegisterReflection()
Visit attributes.
Definition binding_rewrite.h:71
DataflowBlockRewriteObj(DataflowBlock value)
Definition binding_rewrite.h:41
DataflowBlock dfb_
The rewritten dataflow block.
Definition binding_rewrite.h:83
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:86
void ReplaceAllUses(Var old_var, Var new_var)
Replace all uses of old_var with new_var.
DataflowBlockRewriteObj(ffi::UnsafeInit)
Definition binding_rewrite.h:42
void Add(Binding binding)
Insert a Binding statement.
A statement rewriter for relax.DataflowBlock.
Definition binding_rewrite.h:97
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(DataflowBlockRewrite, ffi::ObjectRef, DataflowBlockRewriteObj)
DataflowBlockRewriteObj * operator->()
mutable accessor.
Definition binding_rewrite.h:105
DataflowBlockRewrite(DataflowBlock dfb, Function root_fn)
Definition expr.h:250
Definition expr.h:85
A Relax function.
Definition expr.h:390
Definition expr.h:417
Definition expr.h:205
Parent compiler symbol visibility.
The set of Relax specific analysis on IR.
Type GetType(const Expr &expr)
Get the underlying Relax type of expr.
Definition type.h:383
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
UniqueNameSupply that can be used to generate unique variable names.