已合并
[AscendNPU IR]: fix: Fix PlanMemory overflow because of 0-size temp buffer when memory equals size of memory scope #1367
[AscendNPU IR]: fix: Fix PlanMemory overflow because of 0-size temp buffer when memory equals size of memory scope #1367
已合并
hujiajun创建于 7月5日
hujiajun成员
7月5日

描述 Description

请提供此 Pull Request 的高级别说明。
Please describe what this PR is about.

类型 Category

Checklist

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

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

likedislike
ascend-robotascend-robot成员
7月5日 添加了label:ascend-cla/yes
ascend-robotascend-robot成员
7月5日 添加了label:SC-RUNNING
ascend-robot
ascend-robot成员
7月5日 评论:

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


PR Approval Progress

⚠️ This PR does not yet meet the following requirements:lgtm (requires ≥ 2 person(s) per module)、approve (requires ≥ 1 person(s) per module)

Module Approval Details

module lgtm status approve status
repo-Ascend/AscendNPU-IR ❌ (0/2)(You can also ask: ardazishvili, Zz_zl, blueyi, amywang1212, dainbow) ❌ (0/1)(You can also ask: tarvlad, 周浩, michael_hliao, 海丽娟, dainbow)

💡 Tip:

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

CLA Signature Pass

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

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

变更摘要

此 PR 修复了 AscendNPU IR 中 PlanMemory 阶段因 0 大小临时缓冲区(temp buffer)导致的内存规划溢出问题。当内存使用量恰好等于 memory scope 大小时,原有的严格小于比较 iter->second < restBufferSize 会错误判定内存不足,导致 fallback 到无复用规划路径。同时,SpecAlloc 方法在遇到 alignedConstBits == 0 的 0 大小临时缓冲区时,因除零或溢出风险而缺少提前返回的防护。该修复通过两处代码调整和新增测试用例,确保 0 大小 temp buffer 场景下内存规划的正确性。

主要改动

  • IsEnoughForBuffersNoReuse 比较条件修正: 将 if (iter->second < restBufferSize) 改为 if (iter->second <= restBufferSize),使得当所需内存恰好等于 scope 可用大小时也能正确走无复用规划路径,而非错误地继续尝试复用。
  • SpecAlloc 增加 0 大小缓冲区保护: 在 SpecAlloc 中新增对 e->alignedConstBits == 0 的判断,将 bitsOffset 直接置为 0 并提前返回 success(),避免对 0 大小临时缓冲区的不必要处理与潜在异常。
  • 新增测试用例 test_mem_noreuse_max: 验证当 temp buffer 为 memref<0xi64> 时,IsEnoughForBuffersNoReuse 修复后不再错误分配新内存(CHECK-NOT: memref.alloc())。
  • 新增测试用例 test_mem_specalloc_max: 验证 SpecAlloc 修复后,0 大小 temp buffer 的 pointer_cast 能正确获得偏移量 0(%[[CONST0]]),而其他正常缓冲区不受影响。
likedislike
ascend-robot
ascend-robot成员
7月5日 评论:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
7月5日 删除了label:SC-RUNNING
ascend-robotascend-robot成员
7月5日 添加了label:SC-SUCC
ascend-robotascend-robot成员
7月5日 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
7月5日 评论:

✅ 跳过 docs ci 检查,没有需要检查的文档文件

likedislike
ascend-robotascend-robot成员
7月5日 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
7月5日 添加了label:docs-ci-pipeline-success
atomgit-bot
atomgit-bot
7月5日 评论:

代码审查

经过全面审查,这两个变更都是正确的,没有发现任何问题。

关闭摘要

  • bishengir/lib/Dialect/HIVM/Transforms/PlanMemory.cpp:已审查,无问题。两处修改均为正确的边界条件修复:

    1. IsEnoughForBuffersNoReuse< 改为 <=(第 1761 行),修复了当 buffer 所需空间刚好等于可用空间时错误进入复用路径的边界 bug。
    2. SpecAlloc 中新增 alignedConstBits == 0 的提前返回(第 2120-2123 行),防止 0-size temp buffer 进入 speculative allocation 循环导致溢出。
  • bishengir/test/Dialect/HIVM/plan-memory.mlir:已审查,无问题。两个新增测试用例正确验证了上述两项修复:

    • test_mem_noreuse_max:验证空间刚好填满时走 no-reuse 路径。
    • test_mem_specalloc_max:验证含 0-size buffer 场景下 specalloc 路径的正确行为(0-size buffer 获得 offset=0,其他 buffer 正确复用)。

整体风险评估:低风险。变更为精准的边界条件修复,测试覆盖到位。

⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。

likedislike
Cchenwenqiao成员
7月5日 合入了pull request,合并节点 SHA:4082c1b559598f214d79e4d00da9a356722abc5c