Pull Request has been successfully merged by CANN-robot.
(Thank you for LiuChang's contribution.)变更摘要
本 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,语义更清晰。


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


compile


compile


compile


/ai summary


变更摘要
本 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比对。


/approve


/lgtm


问题根因
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)