manupa-arm commented on a change in pull request #9418: URL: https://github.com/apache/tvm/pull/9418#discussion_r764933257
########## File path: src/tir/usmp/transform/convert_pool_allocations_to_offsets.cc ########## @@ -0,0 +1,351 @@ +/* + * 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 tir/analysis/usmp/transform/convert_pool_allocations_to_offsets.cc + * \brief This pass would convert the pool allocations to offsets from pools + */ + +#include <tvm/arith/analyzer.h> +#include <tvm/runtime/device_api.h> +#include <tvm/tir/builtin.h> +#include <tvm/tir/function.h> +#include <tvm/tir/stmt_functor.h> +#include <tvm/tir/transform.h> +#include <tvm/tir/usmp/utils.h> + +#include <stack> + +namespace tvm { +namespace tir { +namespace usmp { + +/*! + * \brief The StmtExpr mutator class to replace allocate nodes + * with offsets within memory pools + * + * This mutator class with add Pool variables recursively to every PrimFunc + * starting from the main PrimFunc. For all allocate nodes, that have been + * memory planned, will be mutated into an offset using a Let binding. + */ +class PoolAllocationToOffsetConverter : public StmtExprMutator { + public: + PoolAllocationToOffsetConverter(const IRModule& module, + const Map<tir::Stmt, PoolAllocation>& pool_allocations, + bool emit_tvmscript_printable = false) + : pool_allocations_(pool_allocations), emit_tvmscript_printable_(emit_tvmscript_printable) { + module_ = module->ShallowCopy(); + for (const auto& kv : pool_allocations) { + // TODO(@manupa-arm): add AllocateConstNode when it is available + ICHECK(kv.first->IsInstance<AllocateNode>()); + Allocate allocate_node = Downcast<Allocate>(kv.first); + PoolAllocation pool_allocation = kv.second; + PoolInfo pool_info = pool_allocation->pool_info; + int byte_pool_offset = pool_allocation->byte_offset->value; + int required_pool_size_for_allocation = + byte_pool_offset + CalculateExtentsSize(allocate_node.operator->()); + if (all_pools_sizes_.find(pool_info) == all_pools_sizes_.end()) { + all_pools_sizes_[pool_info] = required_pool_size_for_allocation; + } else { + int prev_required_pool_size = all_pools_sizes_[pool_info]; + if (prev_required_pool_size < required_pool_size_for_allocation) { + all_pools_sizes_[pool_info] = required_pool_size_for_allocation; + } + } + } + + for (const auto& kv : all_pools_sizes_) { + PoolInfo pi = kv.first; + int allocated_size = kv.second; + allocated_pool_ordering_.push_back(AllocatedPoolInfo(pi, allocated_size)); + } + std::sort(allocated_pool_ordering_.begin(), allocated_pool_ordering_.end(), + [](const AllocatedPoolInfo& lhs, const AllocatedPoolInfo& rhs) { + if (lhs->pool_info->pool_name < rhs->pool_info->pool_name) { + return true; + } + return false; + }); + } + IRModule operator()(); + + private: + PrimExpr VisitExpr_(const CallNode* op) override; + Stmt VisitStmt_(const AllocateNode* op) override; + // PrimExpr VisitExpr_(const VarNode* op) override; Review comment: Yes and done! -- 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]
