Merged
fix(ir): Fix missing replace of scalar vars #5308
lidongshengAdd on Aug 5
fix(ir): Fix missing replace of scalar vars #5308
Merged
lidongshengAdd on Aug 5
lidongsheng
lidongshengCollaborators
Aug 5

Related Issue: #2559

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for lidongsheng's contribution.)
lidongshenglidongshengCollaborators
Aug 5 Created pull request, commit db012372
lidongshenglidongshengCollaborators
Aug 5 Associated issue: [Task|任务跟踪]: 新IR适配
atomgit-bot
atomgit-bot
Aug 5 Comments:

变更摘要

此 PR 主要修复了 IR 合并过程中对标量变量(scalar vars)替换遗漏的问题。核心改动包括:新增 NoneType 类型以精确区分"无值"与"未知类型"语义;在 SymbolicScalar 中引入 SubstituteVars 方法以支持符号表达式内的变量替换;重构 merge_stmts_pass 中的 SubstituteStmt 逻辑,使其能递归替换 TensorOpStmt 操作属性(如 VIEW/ASSEMBLE 的偏移量)中的动态变量;同时改进了 Python 端对 if-else 分支类型冲突的延迟报错机制。

主要改动

  • 新增 NoneType 类型:在 ObjectKind 枚举中新增 NoneType,定义 NoneType 类和 GetNoneType() 单例工厂函数,并注册到 KindTrait 与 Python 绑定中;新增 IsFieldLessType() 辅助函数统一判断无字段单例类型,structural_equal 和 structural_hash 均改用该函数,使 NoneType 被正确识别为无字段类型。
  • SymbolicScalar 变量替换与引用收集:新增 SubstituteVars 方法支持对 RawSymbolicExpression、RawSymbolicSymbol 等递归替换变量映射;新增 GetVarRefs 方法收集符号标量内部所有 Var* 引用,供 VarUseCollector 使用。
  • merge_stmts_pass 的 SubstituteStmt 重写:引入 SubstituteStmt 替代原先直接调用 SubstituteVars,该函数先通过 SubstituteVars 替换语句级变量,再对 TensorOpStmt 中的 Operation 动态属性列表逐项调用 SymbolicScalar::SubstituteVars 进行替换并化简,确保 VIEW/ASSEMBLE 等操作的动态偏移量属性中的变量被正确替换。
  • OpAttribute::CollectAttrs 机制:在 OpAttribute 基类中新增虚函数 CollectAttrs,ViewOpAttribute 和 AssembleOpAttribute 分别实现以暴露 fromOffset/toValidShape 和 toOffset/fromValidShape 动态属性;新增 CollectTensorOpAttrs 工具函数从 TensorOpStmt 中提取属性,供 IR 打印器输出 attrs=[...]。
  • Python 端类型冲突延迟报错:引入 _Poison 类和 ctx.poison() 方法,当 if-else 分支 yield 变量类型不匹配时不再立即抛异常,而是返回 _Poison 对象;变量被实际使用时才抛出 Conflicted var 错误;同时为 dispatch_call 添加调用栈追踪和 DispatchError 异常包装,改进错误定位信息。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Aug 5 Comments:

代码审查

经过对所有 33 个变更文件的逐行审查,包括对 IR 类型系统变更(引入 NoneType 并重新定义 UnknownType 语义)、PyTorch 到 IR 的调度器增强、merge pass 中的标量变量替换修复、以及所有测试用例的全面分析,我没有发现任何值得报告的缺陷。

审查总结

发现问题数:0

  • P0:0
  • P1:0
  • P2:0
  • P3:0

总体风险评估:低风险

该变更在整个代码库中是一致的:

  • NoneType 被引入作为明确的"无值/void"类型,UnknownType 被重新定位为 poison/冲突标记,所有使用点(类型检查、IR 构建器、打印机、相等性检查、哈希)都一致更新。
  • SubstituteStmt 函数通过额外处理 Operation 动态属性中的 SymbolicScalar 变量,正确修复了合并 if 语句时标量变量丢失替换的问题。
  • Python 层正确地将 ir.NoneType 映射为 Python None,将 ir.UnknownType 映射为惰性错误引发代理(_Poison),将类型冲突错误从 IR 构建时推迟到使用点。
  • 调度器现在正确跟踪 call_stack 以提供更好的错误信息。
  • 所有测试 golden 输出均相应更新。

已审查的文件(33/33):

文件 状态
framework/include/ir/core.h 无问题
framework/include/ir/kind_traits.h 无问题
framework/include/ir/type.h 无问题
framework/include/tilefwk/symbolic_scalar.h 无问题
framework/src/interface/ir/structural_equal.cpp 无问题
framework/src/interface/ir/structural_hash.cpp 无问题
framework/src/interface/ir/transforms/merge_stmts_pass.cpp 无问题
framework/src/interface/ir/transforms/printer.cpp 无问题
framework/src/interface/ir/transforms/utils/stmt_utils.cpp 无问题
framework/src/interface/ir/verifier/type_check_pass.cpp 无问题
framework/src/interface/operation/attribute.h 无问题
framework/src/interface/tensor/ir.cpp 无问题
framework/src/interface/tensor/ir.h 无问题
framework/src/interface/tensor/irbuilder.cpp 无问题
framework/src/interface/tensor/symbolic_scalar.cpp 无问题
python/pypto/ir.py 无问题
python/pypto/pil/dispatcher.py 无问题
python/pypto/pil/ops.py 无问题
python/pypto/pil/pir.py 无问题
python/pypto/pypto_impl/ir.pyi 无问题
python/src/bindings/ir/ir.cpp 无问题
python/tests/ut/ir/test_common.py 无问题
python/tests/ut/ir/test_ir.py 无问题
python/tests/ut/ir/test_merge_pass/test_merge_pass1.py 无问题
python/tests/ut/ir/test_merge_pass/test_merge_pass2.py 无问题
python/tests/ut/ir/test_merge_pass/test_merge_pass3.py 无问题
python/tests/ut/ir/test_merge_pass/test_merge_pass4.py 无问题
python/tests/ut/ir/test_merge_pass/test_merge_pass5.py 无问题
python/tests/ut/ir/test_merge_pass/test_merge_pass6.py 无问题
python/tests/ut/ir/test_merge_pass/test_merge_pass7.py 无问题
python/tests/ut/ir/test_merge_pass/test_merge_pass8.py 无问题
python/tests/ut/ir/test_merge_pass/test_type_conflict.py 无问题
python/tests/ut/ir/test_merge_pass/test_yield_scalar.py 无问题
类型 数量
🔴 阻塞 2
🟡 建议 3

⛔ 需要修改

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 5 Added label: stat/needs-squash
CANN-robotCANN-robotCollaborators
Aug 5 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Aug 5 Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here。
You can get sig-info at here


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
repo-cann/pypto ✅ DCGDDD, gaoxiang618 (2/2) ✅ DCGDDD, gaoxiang618 (2/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

tsungl4, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
CANN-robotCANN-robotCollaborators
Aug 5 Set xinweiliu,cam_bxh,dai-zhentao,gguuaanngg,codehjp423,gaoxiang618,wangzheng97,DCGDDD,syfeng,jason_yuan_ye,foundea,ym-HW,hw_zhangzhiqi,yinjie27_,congqiannan,zhangyuebing,gaoxingwang,zhiweiyuan,caodongdong,make_pto,zhoubotcam,linjiashu,wangbing110,WenboCodes as approver
CANN-robotCANN-robotCollaborators
Aug 5 Set xinweiliu,cam_bxh,dai-zhentao,gguuaanngg,codehjp423,gaoxiang618,wangzheng97,DCGDDD,syfeng,jason_yuan_ye,foundea,ym-HW,hw_zhangzhiqi,yinjie27_,congqiannan,zhangyuebing,gaoxingwang,zhiweiyuan,caodongdong,make_pto,zhoubotcam,linjiashu,wangbing110,WenboCodes as approver
lidongshenglidongshengCollaborators
Aug 5 Pre merge succeeded (commit_id: 29b7da545a888528e16c7d145f176c30b41507bc)
lidongshenglidongshengCollaborators
Aug 5 Force-pushed 19 commits:676f5b17-17 commits from branch master,eb83df48-fix(ir): Fix scalar removed not correctly,cda802d7-fix(ir): Fix missing replace of scalar vars
lidongshenglidongshengCollaborators
Aug 5 Pre merge succeeded (commit_id: 3fb32962e3ab2c4b1663bb76cf475dfc0a2c965e)
CANN-robotCANN-robotCollaborators
Aug 5 Removed label: cann-cla/yes
lidongsheng
lidongshengCollaborators
Aug 5 Comments:

/ai review

likedislike
atomgit-bot
atomgit-bot
Aug 5 Comments:

⚠️ 已有 AI 任务正在处理中,请稍候,无需重复触发。

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 5 Added label: cann-cla/yes
atomgit-bot
atomgit-botAug 5Start code review2
framework/include/tilefwk/symbolic_scalar.h
Outdated
@@ -24,3 +25,4 @@
2425 
2526#include "ir/expr.h"
2627 
28+using namespace pypto;
atomgit-bot
atomgit-botAug 5Comments:

🟡 Medium Priority

framework/include/tilefwk/symbolic_scalar.h 第28行新增了 using namespace pypto;,位于 namespace npu::tile_fwk 之外。这意味着所有包含此头文件的编译单元都会被强制引入 pypto 命名空间中的所有符号(包括 pypto::ir 下的所有 IR 类型),可能导致命名冲突、意外的重载决议和静默的行为变化。这是一个被广泛认为危险的头文件实践。

likedislike
Incorrect?
System
System MessageSystem
Aug 5 Comments:

changed this line on 791a8144 view diff detail

likedislike
atomgit-bot
atomgit-botAug 5Start code review1
framework/src/interface/tensor/symbolic_scalar.cpp
@@ -597,0 +594,4 @@
594+static RawSymbolicScalarPtr AsRawSymbolicScalar(const pypto::ir::ExprPtr& expr)
595+{
596+ if (auto val = ir::AsMut<ir::ConstInt>(expr))
597+ return std::static_pointer_cast<RawSymbolicImmediate>(val);
atomgit-bot
atomgit-botAug 5Comments:

🟠 High Priority

AsRawSymbolicScalar(symbolic_scalar.cpp 第594-603行)通过 ir::AsMut<ir::ConstInt>(expr) 获取 shared_ptr<ConstInt> 后,直接 std::static_pointer_cast<RawSymbolicImmediate>(val) 进行向下转型。但 AsMut<ConstInt> 对任何普通 ConstInt 对象都返回非空指针,而普通 ConstInt 并不继承自 RawSymbolicImmediate(后者是 RawSymbolicImmediate : public ir::ConstInt, public RawSymbolicScalar 的多重继承)。对非 RawSymbolicImmediate 的 ConstInt 做 static_pointer_cast<RawSymbolicImmediate> 是未定义行为。Var / RawSymbolicSymbol、ScalarExpr / RawSymbolicExpression 的情况同理。

触发条件: 当 SymbolicScalar::SubstituteVars 中 varMap 的替换值是一个普通的 IR 表达式(非 RawSymbolic* 实例)时,AsRawSymbolicScalar 会收到一个普通 IR 节点并执行不安全的转换。这在 merge_stmts_pass 中可通过 yield var 映射触发——yield 值可以是任意 IR 表达式。

建议:将 static_pointer_cast 改为 dynamic_pointer_cast,并对空指针返回做处理;或者改用其他方式验证实际动态类型(如检查 GetKind() 是否属于 SymbolicScalar 特有的 kind)。

likedislike
Incorrect?
atomgit-bot
atomgit-botAug 5Start code review1
framework/src/interface/operation/attribute.h
@@ -251,0 +251,4 @@
251+ if (!fromDynOffset_.empty()) {
252+ attrs["fromOffset"] = fromDynOffset_;
253+ } else {
254+ attrs["fromOffset"] = fromOffset_;
atomgit-bot
atomgit-botAug 5Comments:

🟠 High Priority

在 ViewOpAttribute::CollectAttrs 和 AssembleOpAttribute::CollectAttrs 的 else 分支中,将 Offset(即 std::vector<int64_t>)存入 std::any。但在 printer.cpp 的 PrintKwargValue 中,只处理了 typeid(std::vector<int>)(第296行),没有处理 typeid(std::vector<int64_t>)。这意味着当动态偏移为空时,具体偏移量会打印为 "Unsupported"。

在 AssembleOpAttribute::SetToOffset 中(第289行),toDynOffset_ 直接使用传入的 toDynOffset 参数,默认值为 {}(空),因此 toDynOffset_ 可以为空,触发 else 分支。ViewOpAttribute::SetFromOffset 在空参情况下会通过 OpImmediate::ToSpecified 填充 fromDynOffset_,所以其 else 分支更难触发,但仍存在代码脆弱性。

触发条件: 构造 AssembleOpAttribute 时仅传具体偏移(如 AssembleOpAttribute(offset)),不传动态偏移。此后打印 IR 时,toOffset 属性将显示 "Unsupported"。

修复方向: 在 PrintKwargValue 中增加对 typeid(std::vector<int64_t>) 的处理,或确保 CollectAttrs 始终将具体偏移量转换为 std::vector<SymbolicScalar>。

likedislike
Incorrect?
lidongshenglidongshengCollaborators
Aug 5 Pre merge succeeded (commit_id: 6033972b50e08f0414670ffb3ba80343750eebf5)
9 event messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Aug 5 Added label: cann-cla/yes
lidongsheng
lidongshengCollaborators
Aug 5 Comments:

compile

likedislike
lidongshenglidongshengCollaborators
Aug 5 Pre merge succeeded (commit_id: 5ce2f1ab9c4b7191527e1b34ac048d1766def2de)
CANN-robotCANN-robotCollaborators
Aug 5 Added label: ci-pipeline-running
CANN-robot
CANN-robotCollaborators
Aug 5 Comments:

流水线任务触发成功
任务链接 [2f4a3b30a3e34dd8acb34c29e02c40ec][流水线指导]

任务名称状态日志下载链接
Compile_Ascend_X86 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_ubuntu24 ✅ SUCCESS >>>>> >>>>>
pre_comment ✅ SUCCESS >>>>>
UT_Test_Py3_ninja_simulation ✅ SUCCESS
UT_Test_Py3_ninja ✅ SUCCESS
UT_Test_Cpp_make_gnu_part_1 ✅ SUCCESS
UT_Test_Cpp_make_gnu_part_2 ✅ SUCCESS
UT_Test_Cpp_make_gnu_part_3 ✅ SUCCESS
UT_Test_Cpp_make_clang_part1 ✅ SUCCESS
UT_Test_Cpp_make_clang_part2 ✅ SUCCESS
UT_Test_Cpp_make_clang_part3 ✅ SUCCESS
UT_Test_kirinx90 ✅ SUCCESS
UT_Test_kirin9030 ✅ SUCCESS
PreSmoke_A900 ✅ SUCCESS >>>>>

[2026-08-05 22:51:34]    CI执行结束

likedislike
CANN-robot
CANN-robotCollaborators
Aug 5 Comments:

流水线任务触发成功
任务链接 [2bf966ceb9bf42578f7c8427bd4e7b5c][流水线指导]

任务名称状态日志下载链接
SCA ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
codecheck_Pr ✅ SUCCESS
StaticCheck_codespell ✅ SUCCESS
StaticCheck_link_validity ✅ SUCCESS
StaticCheck_resource_existence ✅ SUCCESS
StaticCheck_tag_closed ✅ SUCCESS
StaticCheck_markdownlint ✅ SUCCESS
codecheck_codestyle ⚠️ WARNING >>>>>
codecheck_precommit ✅ SUCCESS >>>>>
StaticCheck_md ✅ SUCCESS

[2026-08-05 22:17:34]    CI执行结束

likedislike
CANN-robot
CANN-robotCollaborators
Aug 5 Comments:

流水线任务触发成功
任务链接 [85aefc2b4c624b048ca0576f021df9b8][流水线指导]

任务名称状态日志下载链接
get_pr_file ✅ SUCCESS
static-check ✅ SUCCESS
compile ✅ SUCCESS

[2026-08-05 22:52:21]    CI执行结束

likedislike
CANN-robotCANN-robotCollaborators
Aug 5 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Aug 5 Added label: ci-pipeline-passed
杨旭
杨旭Collaborators
Aug 6 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Aug 6 Added label: approved
gaoxiang618
gaoxiang618Collaborators
Aug 6 Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
Aug 6 Added label: lgtm
CANN-robotCANN-robotCollaborators
Aug 6 Merged pull request