已合并
[PlanMemory] Narrow VF inplace reachability via Union-Find #1684
[PlanMemory] Narrow VF inplace reachability via Union-Find #1684
已合并
hujiajun创建于 7月15日
hujiajun成员
7月15日

描述 Description

VisitInplaceReuseReachable previously hopped to non-conflicting operands within the same VF call via getInplaceReusableOperands.
If any such operand reached a StoreOp (or LoadOp), its reachability spread to genBuffer/killBuffer in that call, causing IsReuseVFCall to falsely reject valid VF inplace pairs.

Fix:

Remove VF-hopping from VisitInplaceReuseReachable; only trace direct use-def chains (subview, collapse_shape, expand_shape).
Add Union-Find to InplaceReuseReachableMap: reachability now propagates via unite() only after a pair is actually reused, not speculatively.
Sort gen descending for optimal greedy pairing.
Allow multiple VF inplace pairs per op.

类型 Category

Checklist

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 hujiajun 的贡献)
Hhujiajun成员
7月15日 创建了 pull request,commit 52b60085
atomgit-bot
atomgit-bot
7月15日 评论:

变更摘要

此 PR 修复了 PlanMemory 中向量函数(VF)inplace 复用的可达性分析缺陷。原先 VisitInplaceReuseReachable 会沿着同一 VF 调用内的可复用操作数跳转,导致虚假的 StoreOp/LoadOp 可达性传播,使得 IsReuseVFCall 错误地拒绝合法的 VF inplace 配对。修复方案:移除 VF 跳转逻辑,仅保留直接 use-def 链追踪(subview/collapse_shape/expand_shape);引入 Union-Find 结构,在 inplace 配对被实际采纳后才通过 unite() 传播可达性,而非提前推测传播;同时支持每个 op 的多个 VF inplace 配对,并按 gen buffer 大小降序贪心匹配。

主要改动

  • 移除 VisitInplaceReuseReachable 中的 VF 跳转逻辑:删除了对 hivm::isVFCall(user) 的检查及 getInplaceReusableOperands 迭代,现在仅沿 memref.subviewmemref.collapse_shapememref.expand_shape 追踪直接 use-def 链,避免在同 VF 调用内错误传播可达性。

  • 新增 Union-Find 支持:在 InplaceReuseReachableMap 中添加 parent 映射表、find() 路径压缩查找和 unite() 合并方法;put()get() 在存取前先调用 find() 定位根节点;unite() 在合并前将 killRoot 的可达性传播至 genRoot,确保可达性仅在实际采纳配对后传播。

  • 重构 GenerateInplaceList 配对策略:移除原先只选单个最优 VF 配对的 bestVFInplacePair 机制;改为按 constBits 降序排序 gen buffer 后贪心匹配,引入 reusedKill 集合防止 kill buffer 被重复使用;每次成功配对后调用 reachableMap.unite() 传播可达性,支持每个 op 产生多个 VF inplace 配对。

  • 简化 IsReuseVFCall 判定逻辑:将原先同时检查 gen 可达 StoreOp 且 kill 可达 LoadOp 的 && 条件改为 !genReachableStore || !killReachableLoad,仅当双侧都可及时才拒绝 inplace 复用。

  • 新增回归测试:添加 plan-memory-vf-false-reject.mlir,包含两个测试用例,验证之前被错误拒绝的 VF inplace 配对现在能正确识别为 bufferLife,确保修复有效。

likedislike
ascend-robot
ascend-robot成员
7月15日 评论:

libing-pipeline创建中,请等待……

likedislike
atomgit-bot
atomgit-bot
7月15日 评论:

代码审查

审查总结

逐文件确认:

文件 审查结论
bishengir/include/bishengir/Dialect/HIVM/Transforms/PlanMemory.h 无问题 — 仅新增 #includeunite/find 方法声明及 parent 成员,接口一致
bishengir/lib/Dialect/HIVM/Transforms/PlanMemory.cpp 发现 1 个 P3 问题(排序 lambda 副作用);其余逻辑(Union-Find、可达性传播、VF 对处理、reusedKill 去重、IsReuseVFCall 语义等价变换)均正确
bishengir/test/Dialect/HIVM/plan-memory-vf-false-reject.mlir 无问题 — 测试用例结构正确,CHECK 指令与输入匹配

按严重度统计:

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

整体风险评估:低。 此变更的核心逻辑(移除 VF-hopping、以 Union-Find 传播可达性、按 constBits 降序贪心配对、允许每个 op 多个 VF inplace 对)经过仔细推导,未发现正确性或安全性缺陷。唯一报告的问题是排序比较器中 operator[] 的副作用属于防御性改进,在正常数据流下不会触发。

类型 数量
🔴 阻塞 1
🟡 建议 0

⛔ 需要修改

likedislike
ascend-robotascend-robot成员
7月15日 添加了label:ascend-cla/yes
此处折叠了134条消息 查看更多
ascend-robotascend-robot成员
7月20日 添加了label:approved
liupengcheng2012成员
7月21日 评论:

/lgtm

likedislike
ascend-robotascend-robot成员
7月21日 添加了label:lgtm
ascend-robotascend-robot成员
7月21日 解决了最后一个问题
ascend-robotascend-robot成员
7月21日 合入了pull request,合并节点 SHA:83c53ed180e2448923f542b37df34b5566714d86