|
| 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 | + * \file src/relay/transforms/mangle_graph.cc |
| 22 | + * |
| 23 | + * \brief Walk the graph to find external compiled function and to |
| 24 | + * "mangle" them, i.e., replace func_name with tvmgen_MODNAME_func_name |
| 25 | + */ |
| 26 | + |
| 27 | +#include <tvm/ir/error.h> |
| 28 | +#include <tvm/relay/expr.h> |
| 29 | +#include <tvm/relay/expr_functor.h> |
| 30 | +#include <tvm/relay/transform.h> |
| 31 | + |
| 32 | +#include <unordered_map> |
| 33 | + |
| 34 | +#include "../backend/utils.h" |
| 35 | +#include "pass_utils.h" |
| 36 | + |
| 37 | +namespace tvm { |
| 38 | +namespace relay { |
| 39 | +namespace mangling { |
| 40 | + |
| 41 | +class Mangler : public MixedModeMutator { |
| 42 | + public: |
| 43 | + explicit Mangler(const IRModule& module, std::function<String(String)> mangle_fn) |
| 44 | + : module_(module), mangle_fn_(mangle_fn) {} |
| 45 | + |
| 46 | + IRModule Mangle() { |
| 47 | + auto glob_funcs = module_->functions; |
| 48 | + |
| 49 | + // Collect function names to be mangled and create |
| 50 | + // global mangled variables |
| 51 | + for (const auto& pair : glob_funcs) { |
| 52 | + if (auto* fn = pair.second.as<FunctionNode>()) { |
| 53 | + auto func = GetRef<Function>(fn); |
| 54 | + if (func->GetAttr<String>(attr::kCompiler).defined()) { |
| 55 | + auto fn_name_mangled = mangle_fn_(pair.first->name_hint); |
| 56 | + GlobalVar gvar = GlobalVar(fn_name_mangled); |
| 57 | + mangled_gvars_[pair.first->name_hint] = gvar; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Walk the three and mangle the functions. Then replace compiler functions |
| 63 | + // with mangled functions in the module |
| 64 | + IRModule new_module; |
| 65 | + for (const auto& pair : glob_funcs) { |
| 66 | + if (auto* fn = pair.second.as<FunctionNode>()) { |
| 67 | + auto func = GetRef<Function>(fn); |
| 68 | + func = Function(func->params, VisitExpr(func->body), func->ret_type, func->type_params, |
| 69 | + func->attrs); |
| 70 | + if (func->GetAttr<String>(attr::kCompiler).defined()) { |
| 71 | + new_module->Add(mangled_gvars_[pair.first->name_hint], func); |
| 72 | + } else { |
| 73 | + new_module->Add(pair.first, func); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return new_module; |
| 79 | + } |
| 80 | + |
| 81 | + private: |
| 82 | + Expr Rewrite_(const CallNode* call, const Expr& post) final { |
| 83 | + Expr new_expr = post; |
| 84 | + const CallNode* new_call = new_expr.as<CallNode>(); |
| 85 | + auto op_node = new_call->op.as<GlobalVarNode>(); |
| 86 | + if (op_node == nullptr || mangled_gvars_.find(op_node->name_hint) == mangled_gvars_.end()) { |
| 87 | + return new_expr; |
| 88 | + } else { |
| 89 | + return Call(mangled_gvars_[op_node->name_hint], new_call->args, new_call->attrs, |
| 90 | + new_call->type_args, new_call->span); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /*!\brief The IRModule used for partitioning. */ |
| 95 | + IRModule module_; |
| 96 | + /*!\brief The function used to mangle operators name */ |
| 97 | + std::function<String(String)> mangle_fn_; |
| 98 | + /*!\brief Tabled used to store (unmangled_var_name, mangled_gvar) pairs*/ |
| 99 | + std::unordered_map<std::string, GlobalVar> mangled_gvars_; |
| 100 | +}; |
| 101 | + |
| 102 | +} // namespace mangling |
| 103 | + |
| 104 | +namespace transform { |
| 105 | + |
| 106 | +Pass MangleGraph(String mod_name) { |
| 107 | + auto mangle_fn = [mod_name](String name) { return runtime::get_name_mangled(mod_name, name); }; |
| 108 | + |
| 109 | + runtime::TypedPackedFunc<IRModule(IRModule, PassContext)> mangle_functions = |
| 110 | + [=](IRModule m, PassContext pc) { return mangling::Mangler(m, mangle_fn).Mangle(); }; |
| 111 | + return CreateModulePass(mangle_functions, 0, "MangleFunctions", {}); |
| 112 | +} |
| 113 | + |
| 114 | +TVM_REGISTER_GLOBAL("relay._transform.MangleGraph").set_body_typed(transform::MangleGraph); |
| 115 | + |
| 116 | +} // namespace transform |
| 117 | + |
| 118 | +} // namespace relay |
| 119 | +} // namespace tvm |
0 commit comments