tvm
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/ir/name_supply.h>
28 #include <tvm/relax/analysis.h>
29 #include <tvm/relax/expr.h>
30 
31 #include <map>
32 #include <set>
33 #include <type_traits>
34 #include <utility>
35 #include <vector>
36 
37 namespace tvm {
38 namespace relax {
39 
42  public:
44  void ReplaceAllUses(Var old_var, Var new_var);
46  void Add(Binding binding);
48  void Add(String var_name, Expr expr, bool is_dfvar = false) {
49  auto var = is_dfvar ? DataflowVar(var_name, GetStructInfo(expr)) //
50  : Var(var_name, GetStructInfo(expr));
51  Add(VarBinding(std::move(var), std::move(expr)));
52  }
54  void Add(Expr expr, bool is_dfvar = false) {
55  Add(name_supply_->FreshName("tmp"), expr, is_dfvar);
56  }
58  void RemoveUnused(Var unused, bool allow_undef = false);
61 
65  Function MutatedFunc() { return root_fn_.value(); }
68 
71  v->Visit("dfb", &dfb_);
72  v->Visit("root_fn", &root_fn_);
73  }
74 
75  static constexpr const char* _type_key = "relax.DataflowBlockRewrite";
77 
78  protected:
79  friend class DataflowBlockRewrite;
80 
86 
87  private:
88  NameSupply name_supply_;
89 };
90 
96  public:
97  TVM_DLL explicit DataflowBlockRewrite(DataflowBlock dfb, Function root_fn);
98 
104  ICHECK(get() != nullptr);
105  return static_cast<DataflowBlockRewriteNode*>(get_mutable());
106  }
107 
109 };
110 
111 } // namespace relax
112 } // namespace tvm
113 
114 #define TVM_RELAX_BINDING_REWRITE_H_
115 #endif // TVM_RELAX_BINDING_REWRITE_H_
Visitor class to get the attributes of an AST/IR node. The content is going to be called for each fie...
Definition: reflection.h:52
Managed reference class to IRModuleNode.
Definition: module.h:366
Managed reference class to NameSupplyNode.
Definition: name_supply.h:112
Managed reference to RelayExprNode.
Definition: expr.h:442
Definition: expr.h:681
Statement rewriter for relax.DataflowBlock.
Definition: binding_rewrite.h:41
DataflowBlock MutatedDataflowBlock()
The rewritten dataflow block.
Definition: binding_rewrite.h:63
const FunctionNode * original_fn_ptr_
Pointer to the original function.
Definition: binding_rewrite.h:83
Function MutatedFunc()
The rewritten function.
Definition: binding_rewrite.h:65
void VisitAttrs(AttrVisitor *v)
Visit attributes.
Definition: binding_rewrite.h:70
void Add(String var_name, Expr expr, bool is_dfvar=false)
Insert an expression as VarBinding with variable name.
Definition: binding_rewrite.h:48
Map< Var, Array< Var > > to_users_
Map from variable to its users.
Definition: binding_rewrite.h:84
TVM_DECLARE_FINAL_OBJECT_INFO(DataflowBlockRewriteNode, Object)
static constexpr const char * _type_key
Definition: binding_rewrite.h:75
void Add(Expr expr, bool is_dfvar=false)
Insert an expression as VarBinding with automatic variable name.
Definition: binding_rewrite.h:54
Array< Var > fn_outputs_
Variables required by function outputs.
Definition: binding_rewrite.h:85
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:81
void Add(Binding binding)
Insert a Binding statement.
Optional< Function > root_fn_
The rewritten function.
Definition: binding_rewrite.h:82
A statement rewriter for relax.DataflowBlock.
Definition: binding_rewrite.h:95
TVM_DEFINE_OBJECT_REF_METHODS(DataflowBlockRewrite, ObjectRef, DataflowBlockRewriteNode)
DataflowBlockRewriteNode * operator->()
mutable accessor.
Definition: binding_rewrite.h:103
DataflowBlockRewrite(DataflowBlock dfb, Function root_fn)
Definition: expr.h:806
Definition: expr.h:462
A Relax function.
Definition: expr.h:950
Definition: expr.h:995
Definition: expr.h:755
Definition: expr.h:422
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics,...
Definition: map.h:1271
Base class of all object reference.
Definition: object.h:519
const Object * get() const
Definition: object.h:554
Object * get_mutable() const
Definition: object.h:607
base class of all object containers.
Definition: object.h:171
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Reference to string objects.
Definition: string.h:98
NameSupply that can be used to generate unique variable names.
StructInfo GetStructInfo(const Expr &expr)
Get the underlying structure info of expr.
Definition: struct_info.h:445
Var var(std::string name_hint, DataType t=DataType::Int(32))
Construct a new Var expression.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
The set of Relax specific analysis on IR.