Mousius commented on a change in pull request #8023: URL: https://github.com/apache/tvm/pull/8023#discussion_r638657703
########## File path: src/tir/transforms/make_unpacked_api.cc ########## @@ -0,0 +1,135 @@ +/* + * 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 make_unpacked_api.cc Lower PrimFunc to a standard C function API. + */ +#include <tvm/runtime/container.h> +#include <tvm/runtime/device_api.h> +#include <tvm/runtime/registry.h> +#include <tvm/target/target.h> +#include <tvm/tir/analysis.h> +#include <tvm/tir/buffer.h> +#include <tvm/tir/builtin.h> +#include <tvm/tir/expr.h> +#include <tvm/tir/stmt_functor.h> +#include <tvm/tir/transform.h> + +#include <unordered_set> +#include <utility> +#include <vector> + +#include "arg_binder.h" +#include "ir_utils.h" + +namespace tvm { +namespace tir { + +PrimFunc MakeUnpackedAPI(PrimFunc&& func) { + auto global_symbol = func->GetAttr<String>(tvm::attr::kGlobalSymbol); + ICHECK(global_symbol) << "MakeUnpackedAPI: Expect PrimFunc to have the global_symbol attribute"; + + auto target = func->GetAttr<Target>(tvm::attr::kTarget); + ICHECK(target.defined()) << "MakeUnpackedAPI: Require the target attribute"; + + auto* func_ptr = func.CopyOnWrite(); + + // Setup device context + int target_device_type = target.value()->kind->device_type; + Integer device_type(target_device_type); + Integer device_id(0); + PrimExpr node = StringImm("default"); + const Stmt nop = Evaluate(0); + std::vector<Stmt> device_init; + + // Create arg to buffer binder + std::unordered_map<const VarNode*, PrimExpr> vmap; + ArgBinder binder(&vmap); + + // Collect variables and buffers to map between + Array<Var> args; + std::vector<std::pair<Var, Var>> var_def; + std::vector<std::pair<Var, Buffer>> buffer_def; + + for (int i = 0; i < static_cast<int>(func_ptr->params.size()); ++i) { + Var param = func_ptr->params[i]; Review comment: `func_ptr->params` is an `tvm::runtime::Array<tvm::tir::Var>` so the value should always exist and always be a valid `Var`? -- 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. For queries about this service, please contact Infrastructure at: [email protected]
