|
| 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 | + * Copyright (c) 2019 by Contributors |
| 22 | + * \file tvm/relay/backend/vm/remove_unused_funcs.cc |
| 23 | + * \brief Remove unused global relay functions in a relay module. |
| 24 | + */ |
| 25 | + |
| 26 | +#include <tvm/relay/expr.h> |
| 27 | +#include <tvm/relay/expr_functor.h> |
| 28 | +#include <tvm/logging.h> |
| 29 | +#include <tvm/relay/analysis.h> |
| 30 | +#include <tvm/relay/transform.h> |
| 31 | +#include <tvm/runtime/vm.h> |
| 32 | +#include <iostream> |
| 33 | +#include <unordered_set> |
| 34 | +#include <vector> |
| 35 | + |
| 36 | +namespace tvm { |
| 37 | +namespace relay { |
| 38 | +namespace vm { |
| 39 | + |
| 40 | +/** |
| 41 | + * \brief Detects all the functions that can be possibly called by entry function. |
| 42 | + */ |
| 43 | +struct CallTracer : ExprVisitor { |
| 44 | + Module module_; |
| 45 | + |
| 46 | + // Record the names of all encountered functions |
| 47 | + std::unordered_set<std::string> called_funcs_; |
| 48 | + |
| 49 | + // Record the expressions that are being visited |
| 50 | + std::unordered_set<Expr, NodeHash, NodeEqual> visiting_; |
| 51 | + |
| 52 | + explicit CallTracer(const Module& module) |
| 53 | + : module_{module}, |
| 54 | + called_funcs_{}, |
| 55 | + visiting_{} {} |
| 56 | + |
| 57 | + void VisitExpr_(const CallNode* call_node) final { |
| 58 | + Expr op = call_node->op; |
| 59 | + for (auto param : call_node->args) { |
| 60 | + VisitExpr(param); |
| 61 | + } |
| 62 | + if (auto func_node = op.as<FunctionNode>()) { |
| 63 | + auto func = GetRef<Function>(func_node); |
| 64 | + auto it = visiting_.find(func); |
| 65 | + if (it != visiting_.end()) { |
| 66 | + return; |
| 67 | + } |
| 68 | + visiting_.insert(func); |
| 69 | + VisitExpr(func); |
| 70 | + } else if (auto global = op.as<GlobalVarNode>()) { |
| 71 | + called_funcs_.insert(global->name_hint); |
| 72 | + auto func = module_->Lookup(global->name_hint); |
| 73 | + auto it = visiting_.find(func); |
| 74 | + if (it != visiting_.end()) { |
| 75 | + return; |
| 76 | + } |
| 77 | + visiting_.insert(func); |
| 78 | + VisitExpr(func); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + std::unordered_set<std::string> Trace(const std::string& entry) { |
| 83 | + called_funcs_.insert(entry); |
| 84 | + auto main_func = module_->Lookup(entry); |
| 85 | + VisitExpr(main_func); |
| 86 | + return called_funcs_; |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +/*! |
| 91 | + * \brief Remove functions that are not used. |
| 92 | + * |
| 93 | + * \param module The Relay module. |
| 94 | + * \param entry_funcs The set of functions that can be entry function. |
| 95 | + * |
| 96 | + * \return The module with dead functions removed. |
| 97 | + */ |
| 98 | +Module RemoveUnusedFunctions(const Module& module, |
| 99 | + Array<tvm::Expr> entry_funcs) { |
| 100 | + std::unordered_set<std::string> called_funcs{}; |
| 101 | + for (auto entry : entry_funcs) { |
| 102 | + auto* str_name = entry.as<ir::StringImm>(); |
| 103 | + auto funcs = CallTracer(module).Trace(str_name->value); |
| 104 | + called_funcs.insert(funcs.cbegin(), funcs.cend()); |
| 105 | + } |
| 106 | + auto existing_functions = module->functions; |
| 107 | + for (auto f : existing_functions) { |
| 108 | + auto it = called_funcs.find(f.first->name_hint); |
| 109 | + if (it == called_funcs.end()) { |
| 110 | + module->Remove(f.first); |
| 111 | + } |
| 112 | + } |
| 113 | + return module; |
| 114 | +} |
| 115 | + |
| 116 | +} // namespace vm |
| 117 | + |
| 118 | +namespace transform { |
| 119 | + |
| 120 | +Pass RemoveUnusedFunctions(Array<tvm::Expr> entry_functions) { |
| 121 | + runtime::TypedPackedFunc<Module(Module, PassContext)> pass_func = |
| 122 | + [=](Module m, PassContext pc) { |
| 123 | + return relay::vm::RemoveUnusedFunctions(m, entry_functions); |
| 124 | + }; |
| 125 | + return CreateModulePass(pass_func, 1, "RemoveUnusedFunctions", {}); |
| 126 | +} |
| 127 | + |
| 128 | +TVM_REGISTER_API("relay._transform.RemoveUnusedFunctions") |
| 129 | +.set_body_typed(RemoveUnusedFunctions); |
| 130 | + |
| 131 | +} // namespace transform |
| 132 | + |
| 133 | +} // namespace relay |
| 134 | +} // namespace tvm |
0 commit comments