gemini-code-assist[bot] commented on code in PR #18491: URL: https://github.com/apache/tvm/pull/18491#discussion_r3413697662
########## src/relax/transform/canonicalize_shape_expr.cc: ########## @@ -0,0 +1,129 @@ +/* + * 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/relax/transform/canonicalize_shape_expr.cc + * \brief Canonicalize ShapeExpr by replacing composite PrimExpr dimensions with symbolic vars. + */ + +#include <tvm/ffi/reflection/registry.h> +#include <tvm/relax/analysis.h> +#include <tvm/relax/expr.h> +#include <tvm/relax/expr_functor.h> +#include <tvm/relax/transform.h> + +#include <string> +#include <unordered_map> + +namespace tvm { +namespace relax { + +namespace { + +bool IsSimpleShapeDim(const PrimExpr& expr) { + return expr->IsInstance<IntImmNode>() || expr->IsInstance<tirx::VarNode>(); +} + +class ShapeExprCanonicalizer : public ExprMutator { + public: + using ExprMutator::VisitExpr_; + + BindingBlock VisitBindingBlock(const BindingBlock& block) final { + bool prev = inside_binding_block_; + inside_binding_block_ = true; + BindingBlock ret = ExprMutator::VisitBindingBlock(block); + inside_binding_block_ = prev; + return ret; + } + + Expr VisitExpr_(const FunctionNode* op) final { + bool prev = inside_binding_block_; + inside_binding_block_ = false; + Expr ret = ExprMutator::VisitExpr_(op); + inside_binding_block_ = prev; + return ret; + } Review Comment:  When processing nested functions, the `expr_to_var_` cache is shared across the outer and nested functions. If a compound shape expression (e.g., `n + 1`) is first encountered inside a nested function, its `MatchCast` binding will be emitted inside the nested function's block, and the mapping will be cached. If the same expression is later encountered in the outer function, the cached variable will be reused, but no `MatchCast` will be emitted in the outer function's scope. This leads to an out-of-scope variable error in subsequent compiler passes. To prevent this, we should save and restore the `expr_to_var_` cache when entering and exiting a nested function. ```c Expr VisitExpr_(const FunctionNode* op) final { bool prev = inside_binding_block_; inside_binding_block_ = false; auto prev_expr_to_var = std::move(expr_to_var_); expr_to_var_.clear(); Expr ret = ExprMutator::VisitExpr_(op); expr_to_var_ = std::move(prev_expr_to_var); inside_binding_block_ = prev; return ret; } ``` -- 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]
