已合并
【PR】: codegen infershape remove unused symbol #1688
【PR】: codegen infershape remove unused symbol #1688
已合并
chengyutao3创建于 16 天前
chengyutao3
chengyutao3成员
16 天前

Pull Request

描述

优化 InferShape 代码生成逻辑:在生成符号变量声明前,先扫描所有 shape 表达式,仅对实际被引用的符号生成 auto xxx = ... 声明,避免输出未使用的变量代码。

具体实现:

  • 遍历所有推断 shape 表达式,按完整单词边界匹配 shape_info 中的符号名,收集被引用的符号到 used_symbols 集合。
  • 在生成变量声明时跳过未被引用的符号,减少死代码。

变更类型

关联的Issue

如何测试

描述测试此变更的步骤和前提条件:

  1. 编译带 infershape 的融合子图,触发 GenInferShapeFunc 生成。
  2. 检查生成的 .cpp 文件中 InferShape 函数,确认不包含未被 shape 表达式引用的符号变量声明。

核对清单

其他信息

仅修改 autofuse/codegen/codegen_infershape.cpp,新增 23 行,新增 <set> 头文件依赖。

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 chengyutao3 的贡献)
chengyutao3chengyutao3成员
16 天前 添加了label:enhancement
chengyutao3chengyutao3成员
16 天前 创建了 pull request,commit 83380fda
atomgit-bot
atomgit-bot
16 天前 评论:

变更摘要

此 PR 对 codegen_infershape.cpp 中的 InfershapeGen::GenInferShapeFunc 方法进行了优化:在生成推断形状函数时,新增了未使用符号的过滤逻辑,仅输出在形状维度表达式中实际被引用的符号定义,避免生成冗余的 auto 变量声明。

主要改动

  • 新增 #include <set> 头文件:为后续使用 std::set<std::string> 收集已使用符号名称提供支持。
  • 新增符号使用分析逻辑:在生成 common_get_input_str 之前,遍历 symbol_shape_str 中所有形状维度表达式,通过整词边界匹配(检查前后字符是否为字母数字)从 shape_info 中筛选出被实际引用的符号,存入 used_symbols 集合。
  • 符号声明输出过滤:在遍历 shape_info 生成 auto 变量声明时,增加 used_symbols.count(it.first) == 0 判断,跳过未被任何形状表达式引用的符号,从而精简生成的代码。
likedislike
atomgit-bot
atomgit-bot
16 天前 评论:

代码审查

✅ 未发现问题

likedislike
CANN-robotCANN-robot成员
16 天前 添加了label:cann-cla/yes
此处折叠了63条消息 查看更多
chengyutao3
chengyutao3成员
5 天前 评论:

/check-pr

likedislike
CANN-robot
CANN-robot成员
5 天前 评论:

The following label is not ready.

approved: Please wait for committers to review the code.

likedislike
张德鹏成员
5 天前 评论:

/approve

likedislike
CANN-robotCANN-robot成员
5 天前 添加了label:approved
CANN-robotCANN-robot成员
5 天前 合入了pull request