Merged
feat(machine): Add yield scalar expr support for IfStmt and ForStmt #5465
feat(machine): Add yield scalar expr support for IfStmt and ForStmt #5465
Merged
gaoxingwangAdd on Aug 15
gaoxingwang
gaoxingwangCollaborators
Aug 15

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:

  • Register scalar returnVars to symbolTable in FindExprFromIfStmt and FindExprFromForStmt
  • Collect yield value symbols in FindExprFromIRStmt YieldStmt case
  • Add currentReturnVars parameter to VisitIRStmtForControlFlow for passing returnVars through recursion
  • Generate VALUE_xxx = expr; in controlFlow cpp for scalar YieldStmt values
  • Skip tensor type returnVars and yield values via ScalarType check
  • Add 10 UT test cases covering symbol registration, yield codegen, nested isolation, tensor skip, backward compat
  • Add ST test with if/else + for loop yielding constant and symbolic expression as loop bounds

Related Issues: N/A

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for gaoxingwang's contribution.)
gaoxingwanggaoxingwangCollaborators
Aug 15 Created pull request, commit 5934d778
atomgit-bot
atomgit-bot
Aug 15 Comments:

变更摘要

本 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.py ST 用例,覆盖 if/else 与 for 循环中 yield 常量及符号表达式作为循环边界的场景。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Aug 15 Comments:

代码审查

审查完成。以下是本次 diff 的最终结论。

发现汇总(按优先级)

  • P2:1 个
  • P3:1 个
  • P0/P1:0 个

逐文件审查结果

  1. framework/src/machine/host/ir_backend.cpp — 已审查。returnVar 符号注册、YieldStmt 值符号收集、currentReturnVars 递归传递、以及 VALUE_xxx = expr; 代码生成的逻辑(含嵌套隔离与 tensor 跳过)均与既有 IR 约定一致,未发现逻辑错误;报告 1 个 P3 防御性问题(yield 值/returnVar 缺空指针检查)。
  2. framework/src/machine/host/ir_backend.h — 已审查,无问题。默认参数 = {} 与 .cpp 定义(不重复默认参数)的写法正确,无 ABI/编译隐患。
  3. framework/tests/ut/machine/src/test_ir_backend.cpp — 已审查,无问题。新增 helper 与 10 个 UT 用例的构造、断言与代码生成语义自洽。
  4. 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

💬 仅评论

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 15 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Aug 15 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.
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 /approve or /lgtm
  • Commenting /approve implies 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. 👍

likedislike
43 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Aug 18 Added label: approved
袁野
袁野Collaborators
Aug 18 Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
Aug 18 Added label: lgtm
CANN-robot
CANN-robotCollaborators
Aug 18 Comments:

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.

likedislike
CANN-robotCANN-robotCollaborators
Aug 18 Merged pull request