Merged
fix(ir): fix iter args bug cause outcast infer bug #5797
LiuChangAdd on Aug 31
fix(ir): fix iter args bug cause outcast infer bug #5797
Merged
LiuChangAdd on Aug 31
LiuChang
LiuChangCollaborators
Aug 31

问题根因
canonicalize 中的 liveness 分析对 for 循环 iter args 存在误判:只要 iterVar 与对应 returnVar 共享同一 raw tensor,就无条件 AddLiveUses 认为是原地累加器(accumulator carry)。但当循环体并未真正通过副作用算子写该 tensor 时(phi 只是被动继承了 iterVar 的 raw tensor,属死传递 carry),错误地保活导致 outcast 推导出错。
修复(framework/src/interface/ir/transforms/utils/canonicalize.cpp:66-168)

  1. 新增成员 sideEffectResults_:遍历时收集副作用算子(ASSEMBLE / ASSEMBLE_SSA / ATOMIC_RMW)的结果。
  2. 重写 ForStmt 的 VisitStmt_ 逻辑:先遍历循环体,再逐个 iter arg 判定——IsSameRawTensor(iterVar, returnVar) 且该 raw tensor 确实被循环体内的副作用算子写过,才 AddLiveUses 保活;否则视为死 carry 不保活。
likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for LiuChang's contribution.)
LiuChangLiuChangCollaborators
Aug 31 Created pull request, commit 89b56014
LiuChangLiuChangCollaborators
Aug 31 Associated issue: [Task|任务跟踪]: 新IR适配
atomgit-bot
atomgit-bot
Aug 31 Comments:

变更摘要

本 PR 修复 IR 规范化简阶段因迭代参数(iter args)处理不当引发的 outcast 推断错误。修改集中在 framework/src/interface/ir/transforms/utils/canonicalize.cpp 的 CanonicalizeLoopImpl 函数中:在决定保留哪些迭代参数索引时,新增了对 NoneType 类型迭代参数的跳过逻辑,并补充了 iterVar_ 空指针防护,避免对空迭代变量执行存活分析导致推断异常。

主要改动

  • 跳过 NoneType 迭代参数:当 itv 非空且其类型为 ir::NoneType 时直接 continue,此类迭代参数不再进入保留索引集合,规避了 outcast 推断异常。
  • 空指针防护:将原条件中直接调用 stmt->iterArgs_[i]->iterVar_.get() 的逻辑改为先判断 itv != nullptr,避免空迭代变量被解引用。
  • 拆分保留条件:将原复合条件拆分为 usedAfter(返回变量在后续被引用)与 isLive(迭代变量存活)两个布尔量,仅当二者之一成立时才将索引压入 keptIndices,语义更清晰。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Aug 31 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 31 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Aug 31 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.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

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

Module Approval Details

module lgtm status approve status
framework/src/interface/ir/transforms ✅ DCGDDD, tsungl4 (2/2) ✅ tsungl4 (1/1)
repo-cann/pypto ✅ tsungl4, DCGDDD (2/2) ✅ tsungl4 (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robot
CANN-robotCollaborators
Aug 31 Comments:

CLA Signature Pass

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

likedislike
CANN-robotCANN-robotCollaborators
Aug 31 Set xinweiliu,cam_bxh,dai-zhentao,lululu40888,shi-dingkun,gguuaanngg,codehjp423,gaoxiang618,wangzheng97,DCGDDD,syfeng,jason_yuan_ye,foundea,ym-HW,hw_zhangzhiqi,yinjie27_,congqiannan,zhangyuebing,tsungl4,gaoxingwang,zhiweiyuan,caodongdong,make_pto,zhoubotcam,linjiashu,wangbing110,WenboCodes as approver
CANN-robotCANN-robotCollaborators
Aug 31 Set gaoxiang618,ym-HW,yinjie27_,zhangyuebing,make_pto,linjiashu as approver
LiuChang
LiuChangCollaborators
Aug 31 Comments:

compile

likedislike
LiuChangLiuChangCollaborators
Aug 31 Pre merge succeeded (commit_id: e4c5ceb224c12961bf8d10fc78ce51913c397464)
8 event messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Aug 31 Set xinweiliu,cam_bxh,dai-zhentao,lululu40888,shi-dingkun,gguuaanngg,codehjp423,wangzheng97,DCGDDD,syfeng,jason_yuan_ye,foundea,hw_zhangzhiqi,congqiannan,tsungl4,gaoxingwang,zhiweiyuan,caodongdong,zhoubotcam,wangbing110,WenboCodes as approver
LiuChang
LiuChangCollaborators
Aug 31 Comments:

compile

likedislike
LiuChangLiuChangCollaborators
Aug 31 Pre merge succeeded (commit_id: 20afe17078e4a47caebb2f3e463e9848f852b44c)
8 event messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Aug 31 Added label: cann-cla/yes
LiuChang
LiuChangCollaborators
Aug 31 Comments:

compile

likedislike
LiuChangLiuChangCollaborators
Aug 31 Pre merge succeeded (commit_id: f321528e3ab7b51ed5cf2a5e3effbba34cd39f9c)
CANN-robotCANN-robotCollaborators
Aug 31 Removed label: ci-pipeline-failed
CANN-robotCANN-robotCollaborators
Aug 31 Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Aug 31 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Aug 31 Added label: ci-pipeline-passed
LiuChang
LiuChangCollaborators
Sep 1 Comments:

/ai summary

likedislike
atomgit-bot
atomgit-bot
Sep 1 Comments:

变更摘要

本 PR 修复 IR 规范化(canonicalize)过程中迭代参数(iter args)处理错误所引发的 outcast 推断(infer)缺陷。核心问题是:当循环的 iterVar_ 与 returnVars 共享同一 raw tensor 时,旧逻辑无条件调用 AddLiveUses 将其保持存活,但其中一部分只是"死传递 carry"(phi 单纯继承了 iterVar_ 的 raw tensor),并不应被保留。修改后的逻辑在遍历循环体时收集副作用算子(ASSEMBLE/ASSEMBLE_SSA/ATOMIC_RMW)的结果,仅当循环体确实通过副作用算子对 raw tensor 进行了原地写入时,才将该迭代参数保持存活,从而避免误判。同时新增了针对 flash attention 内层 k_tile 循环场景的单元测试及 golden 快照。

主要改动

  • 新增副作用结果收集成员:在 canonicalize.cpp 的遍历器中新增 sideEffectResults_ 成员(std::vector<VarPtr>),用于记录遍历过程中遇到的副作用算子(ASSEMBLE/ASSEMBLE_SSA/ATOMIC_RMW)结果;遇到副作用算子时,其结果同时加入 liveRoots 与 sideEffectResults_。
  • 调整循环迭代参数的存活判定:在 VisitStmt_ 处理 ForStmt 时,将原来的"提前无条件 AddLiveUses"改为先遍历循环体,再依据 IsSameRawTensor 判断 iterVar_ 与 returnVars[k] 是否共享 raw tensor,并检查循环体遍历期间(sideEffectStart 之后)是否存在对该 raw tensor 的原地副作用写入;仅当 writtenInPlace 为真时才调用 AddLiveUses,否则视为死传递 carry 不予保留,修复 outcast infer 误判。
  • 新增单元测试:添加 python/tests/ut/ir/test_drop_none_iterargs.py,以 flash_attention_varlen_forward_950 内层 k_tile 循环为抽象(真实累加器 acc 跨迭代携带 + 分支局部临时变量),验证迭代参数丢弃行为。
  • 新增 golden 快照:添加 python/tests/ut/ir/test_drop_none_iterargs.pypto,记录 online_softmax_like_kernel 经 merge pass 后的 IR 期望输出,供 check_snapshot 比对。
likedislike
Incorrect?
LiuChangLiuChangCollaborators
Sep 1 Modify description of pull request
lidongsheng
lidongshengCollaborators
Sep 1 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Sep 1 Added label: approved
杨旭
杨旭Collaborators
Sep 1 Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
Sep 1 Added label: lgtm
CANN-robotCANN-robotCollaborators
Sep 1 Closed associated issue
CANN-robotCANN-robotCollaborators
Sep 1 Merged pull request