Pull Request has been successfully merged by CANN-robot.
(Thank you for lidongsheng's contribution.)变更摘要
此 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异常包装,改进错误定位信息。


代码审查
经过对所有 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映射为 PythonNone,将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 |
⛔ 需要修改


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
/approveor/lgtm- Commenting
/approveimplies 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. 👍


/ai review


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


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


🟠 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)。


🟠 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>。


compile


流水线任务触发成功
任务链接 [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执行结束


流水线任务触发成功
任务链接 [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执行结束


流水线任务触发成功
任务链接 [85aefc2b4c624b048ca0576f021df9b8][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| get_pr_file | ✅ SUCCESS | ||
| static-check | ✅ SUCCESS | ||
| compile | ✅ SUCCESS |
[2026-08-05 22:52:21] CI执行结束


/approve


/lgtm
/approve


Related Issue: #2559