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 | |
20 | /*! |
21 | * |
22 | * \file to_gnf.cc |
23 | * |
24 | * \brief Turn A normal form into graph normal form. |
25 | */ |
26 | #include <tvm/relay/analysis.h> |
27 | #include <tvm/relay/expr_functor.h> |
28 | #include <tvm/relay/transform.h> |
29 | |
30 | #include "let_list.h" |
31 | |
32 | namespace tvm { |
33 | namespace relay { |
34 | |
35 | class UseVarVisitor : public ExprVisitor { |
36 | public: |
37 | explicit UseVarVisitor(const Var& v) : v(v) {} |
38 | |
39 | static bool UseVar(const Var& v, const Expr& e) { |
40 | UseVarVisitor uv(v); |
41 | uv(e); |
42 | return uv.use_var; |
43 | } |
44 | |
45 | private: |
46 | bool use_var = false; |
47 | Var v; |
48 | |
49 | void VisitExpr_(const VarNode* vn) override { use_var = use_var || (v == GetRef<Var>(vn)); } |
50 | }; |
51 | |
52 | class GNF : public ExprMutator { |
53 | private: |
54 | std::unordered_map<Var, Expr, ObjectPtrHash, ObjectPtrEqual> var_map_; |
55 | Expr VisitExpr_(const VarNode* vn) override { |
56 | Var v = GetRef<Var>(vn); |
57 | return var_map_.count(v) == 0 ? v : var_map_.at(v); |
58 | } |
59 | |
60 | static bool UseVar(const Var& v, const Expr& e) { return UseVarVisitor::UseVar(v, e); } |
61 | |
62 | static Expr WrapRec(const Var& var, const Expr& val) { |
63 | return UseVar(var, val) ? Let(var, val, var) : val; |
64 | } |
65 | |
66 | Expr VisitExpr_(const LetNode* ln) override { |
67 | var_map_.insert(std::pair<Var, Expr>(ln->var, WrapRec(ln->var, VisitExpr(ln->value)))); |
68 | return VisitExpr(ln->body); |
69 | } |
70 | }; |
71 | |
72 | Expr ToGraphNormalForm(const Expr& e) { return GNF()(e); } |
73 | |
74 | namespace transform { |
75 | |
76 | Pass ToGraphNormalForm() { |
77 | runtime::TypedPackedFunc<Function(Function, IRModule, PassContext)> pass_func = |
78 | [=](Function f, IRModule m, PassContext pc) { |
79 | return Downcast<Function>(ToGraphNormalForm(f)); |
80 | }; |
81 | return CreateFunctionPass(pass_func, 1, "ToGraphNormalForm" , {}); |
82 | } |
83 | |
84 | TVM_REGISTER_GLOBAL("relay._transform.ToGraphNormalForm" ).set_body_typed(ToGraphNormalForm); |
85 | |
86 | } // namespace transform |
87 | |
88 | } // namespace relay |
89 | } // namespace tvm |
90 | |