gemini-code-assist[bot] commented on code in PR #19781: URL: https://github.com/apache/tvm/pull/19781#discussion_r3415952536
########## src/ir/unique_name_supply.cc: ########## @@ -0,0 +1,111 @@ +/* + * 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 unique_name_supply.cc + * \brief UniqueNameSupply that can be used to generate unique variable names. + */ +#include "tvm/ir/unique_name_supply.h" + +#include <tvm/ffi/function.h> +#include <tvm/ffi/reflection/registry.h> + +#include <sstream> +#include <utility> + +namespace tvm { + +UniqueNameSupply::UniqueNameSupply(const ffi::String& prefix, + ffi::Map<ffi::String, int64_t> name_map) { + auto n = ffi::make_object<UniqueNameSupplyNode>(prefix, std::move(name_map)); + data_ = std::move(n); +} Review Comment:  The `name_map` parameter defaults to `{}` in the header, which constructs a null `ffi::Map` (i.e., `defined()` is `false`). Calling `Set` or `find` on a null map will cause a null pointer dereference and crash. We should ensure `name_map` is initialized to a valid, empty map if it is not defined. ```c UniqueNameSupply::UniqueNameSupply(const ffi::String& prefix, ffi::Map<ffi::String, int64_t> name_map) { if (!name_map.defined()) { name_map = ffi::Map<ffi::String, int64_t>(std::unordered_map<ffi::String, int64_t>()); } auto n = ffi::make_object<UniqueNameSupplyNode>(prefix, std::move(name_map)); data_ = std::move(n); } ``` ########## src/ir/module.cc: ########## @@ -219,13 +219,17 @@ IRModule IRModule::FromExpr(const RelaxExpr& expr, } } + UniqueNameSupply global_names(mod->functions.begin(), mod->functions.end(), + [](const auto& kv) { return kv.first->name_hint; }); GlobalVar main_gv; - auto global_var_supply = GlobalVarSupply(mod); if (gv_name.empty()) { // Bind function to 'main' (though rename if would clash with existing 'main'). - main_gv = global_var_supply->FreshGlobal("main", false); + main_gv = GlobalVar(global_names->FreshName("main", false)); + } else if (mod->ContainGlobalVar(gv_name)) { + main_gv = mod->GetGlobalVar(gv_name); } else { - main_gv = global_var_supply->UniqueGlobalFor(gv_name, false); + global_names->ReserveName(gv_name, false); + main_gv = GlobalVar(gv_name); } Review Comment:  Reserving `gv_name` in `global_names` is redundant here because `global_names` is a local variable that is about to go out of scope, and no further names are generated from it in this branch. ```c } else { main_gv = GlobalVar(gv_name); } ``` ########## src/target/source/codegen_c.h: ########## @@ -357,8 +357,8 @@ class CodeGenC : public ExprFunctor<void(const PrimExpr&, std::ostream&)>, */ std::unordered_map<GlobalVar, ffi::String> internal_functions_; - /* \brief Name supply to generate unique function names */ - NameSupply func_name_supply_; + /* \brief Unique unique name supply to generate unique function names */ + UniqueNameSupply func_name_supply_; Review Comment:  Typo in the comment: "Unique unique name supply" should be "Unique name supply". ```suggestion /* \brief Unique name supply to generate unique function names */ UniqueNameSupply func_name_supply_; ``` -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
