mbs-octoml commented on code in PR #11474: URL: https://github.com/apache/tvm/pull/11474#discussion_r888507982
########## src/relay/transforms/compiler_function_utils.cc: ########## @@ -0,0 +1,201 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * \file src/relay/transforms/compiler_function_utils.cc + * \brief Helper passes for working with functions with the "Compiler" attribute. + */ + +#include "./compiler_function_utils.h" + +#include "../op/call/call.h" +#include "tvm/relay/analysis.h" +#include "tvm/relay/expr_functor.h" + +namespace tvm { +namespace relay { +namespace transforms { +namespace { + +/*! + * \brief Rewrite calls to inlined "Compiler" functions to global functions. The given + * module will be extended with the newly outlined functions. + */ +class Outliner : public MixedModeMutator { + public: + Outliner(GlobalSymbolCache* cache, std::string compiler_filter, IRModule mod) + : cache_(cache), compiler_filter_(std::move(compiler_filter)), mod_(std::move(mod)) {} + + Expr Rewrite_(const CallNode* pre, const Expr& post) final { + Call new_call = Downcast<Call>(post); + if (const auto* function_node = new_call->op.as<FunctionNode>()) { + Optional<String> opt_compiler = function_node->GetAttr<String>(attr::kCompiler); + if (opt_compiler.defined() && + (compiler_filter_.empty() || opt_compiler.value() == compiler_filter_)) { + auto function = GetRef<Function>(function_node); + ICHECK(FreeVars(function).empty()) << "Function marked with '" << attr::kCompiler + << "' attribute should not have free variables"; + GlobalVar global_symbol = cache_->GetGlobalSymbol(function); + // Depending on the cache's implementation, two structurally equal (but not object equal) + // functions may be assigned the same global symbol. If so we'll lift it just once, but + // rewrite all the calls. + if (!mod_->ContainGlobalVar(global_symbol->name_hint)) { + function = + WithAttr(std::move(function), tvm::attr::kGlobalSymbol, global_symbol->name_hint); + mod_->Add(global_symbol, function); + } + return WithFields(new_call, global_symbol); + } + } + return post; + } + + private: + GlobalSymbolCache* cache_; + std::string compiler_filter_; + IRModule mod_; +}; + +/*! + * \brief Rewrite calls to global "Compiler" functions to use the 'call_lowered' convention. + */ +class CallRewriter : public MixedModeMutator { + public: + CallRewriter(std::string compiler_filter, IRModule mod) + : compiler_filter_(std::move(compiler_filter)), mod_(std::move(mod)) {} + + Expr Rewrite_(const CallNode* pre, const Expr& post) final { + Call new_call = Downcast<Call>(post); + if (const auto* global_var_node = new_call->op.as<GlobalVarNode>()) { + if (const auto* function_node = + mod_->Lookup(GetRef<GlobalVar>(global_var_node)).as<FunctionNode>()) { + Optional<String> opt_compiler = function_node->GetAttr<String>(attr::kCompiler); + if (opt_compiler.defined() && + (compiler_filter_.empty() || opt_compiler.value() == compiler_filter_)) { + Optional<String> opt_global_symbol = + function_node->GetAttr<String>(tvm::attr::kGlobalSymbol); + ICHECK(opt_global_symbol.defined()); + GlobalVar global_symbol = mod_->GetGlobalVar(opt_global_symbol.value()); + CallLoweredAttrs attrs; + attrs.metadata.Set("relay_attrs", new_call->attrs); + return CallLowered(global_symbol, new_call->args, attrs, new_call->span); + } + } + } + return post; + } + + private: + std::string compiler_filter_; + IRModule mod_; +}; + +} // namespace + +GlobalVar ExistingGlobalSymbolCache::GetGlobalSymbol(const Function& function) { Review Comment: Currently yes. But Collage has another one since it is the one generating the global_symbols: https://github.com/mbs-octoml/mbs-tvm/blob/908e8c9b0d4e09dede18e0a2db7f0d3220018c6f/src/relay/collage/candidate_function_cache.cc#L31 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
