Pull Request has been successfully merged by CANN-robot.
(Thank you for gaoxingwang's contribution.)变更摘要
本 PR 主要增强新 IR 控制流代码生成能力,使其支持在 IfStmt 和 ForStmt 分支中生成标量 yield 值的符号赋值。此前仅 ForStmt 的 loopVar 会注册到 symbolTable,现在会额外注册两类语句的标量 returnVars,并在 controlFlow 的 C++ 代码中为标量 YieldStmt 值生成 VALUE_xxx = expr; 语句,同时通过 ScalarType 判断跳过 tensor 类型的返回值与 yield 值。
主要改动
FindExprFromIfStmt/FindExprFromForStmt注册标量返回值符号:在ir_backend.cpp中为ifStmt->returnVars_和forStmt->returnVars_遍历注册SymbolicScalar到symbolTable,仅处理ir::As<ir::ScalarType>的变量,tensor 类型被跳过。FindExprFromIRStmt新增YieldStmt分支:在case ir::ObjectKind::YieldStmt中遍历yieldStmt->value_,将标量 yield 值的符号通过AddSymbolFromExpression收集到符号表中。VisitIRStmtForControlFlow新增currentReturnVars参数:函数签名(ir_backend.cpp与ir_backend.h)新增默认值为{}的const std::vector<ir::VarPtr>& currentReturnVars参数,并在递归遍历SeqStmts时透传;VisitIfStmtForControlFlow和VisitForStmtForControlFlow在递归时分别传入ifStmt->returnVars_与forStmt->returnVars_。YieldStmt生成符号赋值代码:在VisitIRStmtForControlFlow的YieldStmt分支中,按索引配对yieldStmt->value_与currentReturnVars,对双方均为ScalarType的情况,通过SymbolicExpressionTable::BuildExpression构建表达式并输出VALUE_<returnVarName> = <expr>;到controlFlowOss。- 测试覆盖:
test_ir_backend.cpp新增 10 个 UT 用例,覆盖符号注册、yield 代码生成、嵌套隔离、tensor 跳过及向后兼容;新增test_yield_scalar_if_for.pyST 用例,覆盖 if/else 与 for 循环中 yield 常量及符号表达式作为循环边界的场景。


代码审查
审查完成。以下是本次 diff 的最终结论。
发现汇总(按优先级)
- P2:1 个
- P3:1 个
- P0/P1:0 个
逐文件审查结果
framework/src/machine/host/ir_backend.cpp— 已审查。returnVar 符号注册、YieldStmt 值符号收集、currentReturnVars递归传递、以及VALUE_xxx = expr;代码生成的逻辑(含嵌套隔离与 tensor 跳过)均与既有 IR 约定一致,未发现逻辑错误;报告 1 个 P3 防御性问题(yield 值/returnVar 缺空指针检查)。framework/src/machine/host/ir_backend.h— 已审查,无问题。默认参数= {}与 .cpp 定义(不重复默认参数)的写法正确,无 ABI/编译隐患。framework/tests/ut/machine/src/test_ir_backend.cpp— 已审查,无问题。新增 helper 与 10 个 UT 用例的构造、断言与代码生成语义自洽。tests/st/test_yield_scalar_if_for.py— 已审查,报告 1 个 P2 问题:三处 golden 计算将 NPU 张量切片赋值给 CPU 的golden张量,会触发设备不匹配的 RuntimeError,导致新增 ST 用例(test 1/2)确定性失败。
整体风险判断:后端 C++ 实现逻辑正确、风险较低;主要风险集中在新增 ST 测试的 golden 计算存在设备不匹配缺陷,会使 ST 用例失败,建议修复后再合入。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 0 |
| 🟡 建议 | 1 |
💬 仅评论


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.
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 |
|---|---|---|
| repo-cann/pypto | ✅ 尹杰, jason_yuan_ye (2/2) | ✅ 尹杰 (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
gaoxingwang, thanks for your pull request. All authors of the commits have signed the CLA. 👍


/lgtm


The MR can not be merged, because of CodeReview discussion not resolved
If you want to solve this problem, you can click here to do it in the FAQs.


feat(machine): Add yield scalar expr support for IfStmt and ForStmt
Enhance new IR control flow codegen to generate symbolic assignment
for scalar yield values in IfStmt and ForStmt branches. Previously
only ForStmt loopVar was registered to symbolTable; now scalar
returnVars from both IfStmt and ForStmt are registered, and
YieldStmt values are converted to VALUE_xxx = expr; in controlFlow cpp.
Changes:
Related Issues: N/A