Skip to content

fix(pto): relax tmrgsort format2 tmp shape check#641

Merged
zhangstevenunity merged 1 commit into
hw-native-sys:mainfrom
Zhendong404:fix-tmrgsort-format2
May 8, 2026
Merged

fix(pto): relax tmrgsort format2 tmp shape check#641
zhangstevenunity merged 1 commit into
hw-native-sys:mainfrom
Zhendong404:fix-tmrgsort-format2

Conversation

@Zhendong404
Copy link
Copy Markdown
Contributor

@Zhendong404 Zhendong404 commented May 8, 2026

Summary

  • relax pto.tmrgsort format2 verifier by removing the strict dst/tmp shape-equality check
  • add a regression test covering the valid case where tmp.cols > dst.cols

Root Cause

TMrgSortOp::verify() required dstShape == tmpShape for format2, but the PTO-ISA/C++ implementation uses tmp as a staging buffer via vmrgsort4(tmpPtr, ...) and then copies only dstCol elements from tmp to dst.

Repro

build/tools/ptoas/ptoas --pto-arch=a3 test/lit/pto/tmrgsort_format2_tmp_larger_emitc.pto

Validation

  • rebuilt ptoas with ninja -C build ptoas
  • build/tools/ptoas/ptoas --pto-arch=a3 test/lit/pto/tmrgsort_format2_tmp_larger_emitc.pto
  • build/tools/ptoas/ptoas --pto-arch=a3 test/lit/pto/tmrgsort_variants_emitc.pto
  • build/tools/ptoas/ptoas --pto-arch=a3 test/lit/pto/tmrgsort_executed_constant_emitc.pto

Closes #640

@reedhecre
Copy link
Copy Markdown

reedhecre commented May 8, 2026

Codex Review

该评论由 review 机器人自动更新。

  • PR: fix(pto): relax tmrgsort format2 tmp shape check #641 fix(pto): relax tmrgsort format2 tmp shape check
  • Author: Zhendong404
  • Base/Head: main / fix-tmrgsort-format2
  • Head SHA: 6c3f20445698
  • Trigger: PR 有新提交
  • Generated At: 2026-05-08T02:45:14Z
  • Previous Head SHA: e7056d804f8e
  • Status: failed at codex-review (exit=1)

Summary

Review failed at stage codex-review: exit=1

Findings

未生成结构化 findings,因为 review 过程提前失败。

Log Tail


===== STAGE clone @ 2026-05-08 10:45:03 =====
set -euo pipefail
rm -rf '/tmp/ptoas-pr-review-monitor/runs/20260508_104503_pr641/repo'
git clone --branch 'main' --depth 50 'https://github.com/hw-native-sys/PTOAS.git' '/tmp/ptoas-pr-review-monitor/runs/20260508_104503_pr641/repo'
cd '/tmp/ptoas-pr-review-monitor/runs/20260508_104503_pr641/repo'
git fetch origin 'refs/pull/641/head:pr-641' --depth 50
git fetch origin 'main' --depth 50 || true
git checkout -f 'pr-641'
git rev-parse HEAD
git diff --stat 'origin/main...HEAD' || true
Cloning into '/tmp/ptoas-pr-review-monitor/runs/20260508_104503_pr641/repo'...
From https://github.com/hw-native-sys/PTOAS
 * [new ref]         refs/pull/641/head -> pr-641
From https://github.com/hw-native-sys/PTOAS
 * branch            main       -> FETCH_HEAD
Switched to branch 'pr-641'
6c3f204456984e0edc1ce7fe4f5b6bf3e76b309c
 lib/PTO/IR/PTO.cpp                                      | 16 ++++++++++++++--
 test/lit/pto/tmrgsort_format2_tmp_larger_emitc.pto      | 14 ++++++++++++++
 test/lit/pto/tmrgsort_format2_tmp_too_small_invalid.pto | 14 ++++++++++++++
 3 files changed, 42 insertions(+), 2 deletions(-)
===== END STAGE clone rc=0 @ 2026-05-08 10:45:06 =====

===== STAGE codex-review @ 2026-05-08 10:45:06 =====
set -euo pipefail
cd '/tmp/ptoas-pr-review-monitor/runs/20260508_104503_pr641/repo'
'codex' exec -C '/tmp/ptoas-pr-review-monitor/runs/20260508_104503_pr641/repo' -s read-only -c 'model_provider="codereview"' -c 'model="gpt-5.4"' -c 'model_reasoning_effort="xhigh"' --output-schema '/tmp/ptoas-pr-review-monitor/runs/20260508_104503_pr641/review_schema.json' -o '/tmp/ptoas-pr-review-monitor/runs/20260508_104503_pr641/codex_last_message.json' --color never - < '/tmp/ptoas-pr-review-monitor/runs/20260508_104503_pr641/review_prompt.txt'
OpenAI Codex v0.115.0 (research preview)
--------
workdir: /tmp/ptoas-pr-review-monitor/runs/20260508_104503_pr641/repo
model: gpt-5.4
provider: codereview
approval: never
sandbox: read-only
reasoning effort: xhigh
reasoning summaries: none
session id: 019e0579-76ea-7233-9178-562cce39bc90
--------
user
你现在在审查 GitHub PR。

仓库:hw-native-sys/PTOAS
PR:#641 fix(pto): relax tmrgsort format2 tmp shape check
作者:Zhendong404
base branch:origin/main
head branch:HEAD(当前已 checkout 到 PR head)

要求:
1. 只审查这个 PR 相对 origin/main 的改动,必要时可以看上下文文件。
2. 重点找真实的 correctness / regression / contract mismatch / CI / runtime / compatibility 问题。
3. 不要提纯风格建议,不要提低价值猜测。
4. 严格按优先级输出:
   - P1:高概率会导致错误结果、编译/运行失败、严重回归、发布阻断
   - P2:重要缺陷、行为回归、遗漏校验/测试、较大兼容性问题
   - P3:次要但明确可改的问题
5. 如果没有问题,summary 直接写:未检查到 PR #641 存在问题,并返回 findings=[]。
6. 如果有问题,summary 简洁概括,findings 里每条都要给出:
   - severity
   - title
   - body(说明为什么是问题,尽量具体)
   - file(尽量给相对路径)
   - line(能确定就填整数,否则 null)

建议先查看:
- git status --short
- git diff --stat origin/main...HEAD
- git diff --unified=80 origin/main...HEAD

最终输出必须严格匹配 JSON schema。

mcp startup: no servers
Reconnecting... 1/5 (unexpected status 403 Forbidden: {"code":"INSUFFICIENT_BALANCE","message":"Insufficient account balance"}, url: https://codex.0u0o.com/responses, request id: d064d913-8534-4a22-a45a-3029731284da)
Reconnecting... 2/5 (unexpected status 403 Forbidden: {"code":"INSUFFICIENT_BALANCE","message":"Insufficient account balance"}, url: https://codex.0u0o.com/responses, request id: 94df66b5-8761-4e7c-9619-1320c4dbed1b)
Reconnecting... 3/5 (unexpected status 403 Forbidden: {"code":"INSUFFICIENT_BALANCE","message":"Insufficient account balance"}, url: https://codex.0u0o.com/responses, request id: 1d6a3a28-f927-4225-8381-4c13fe18565a)
Reconnecting... 4/5 (unexpected status 403 Forbidden: {"code":"INSUFFICIENT_BALANCE","message":"Insufficient account balance"}, url: https://codex.0u0o.com/responses, request id: a7a6ad1a-a89f-4565-a519-fea1e644f92a)
Reconnecting... 5/5 (unexpected status 403 Forbidden: {"code":"INSUFFICIENT_BALANCE","message":"Insufficient account balance"}, url: https://codex.0u0o.com/responses, request id: 8b1521b5-2abc-4890-888c-850d90fadbbb)
ERROR: unexpected status 403 Forbidden: {"code":"INSUFFICIENT_BALANCE","message":"Insufficient account balance"}, url: https://codex.0u0o.com/responses, request id: baa25121-f538-4273-ad00-515c78297268
Warning: no last agent message; wrote empty content to /tmp/ptoas-pr-review-monitor/runs/20260508_104503_pr641/codex_last_message.json
===== END STAGE codex-review rc=1 @ 2026-05-08 10:45:14 =====

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request relaxes the shape verification for TMrgSortOp in PTO.cpp to allow the temporary buffer (tmp) to have a different shape than the destination buffer (dst), accompanied by a new test case. The reviewer pointed out that removing the shape check entirely is too permissive and could lead to out-of-bounds access if the temporary buffer is smaller than the destination. They suggested enforcing a minimum column size for the temporary buffer and adding checks to ensure all operands are rank-2 with a single row.

Comment thread lib/PTO/IR/PTO.cpp
Comment on lines 6290 to 6292
for (Value src : getSrcs()) {
Type srcTy = src.getType();
if (getElemTy(srcTy) != elemTy)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Removing the shape check entirely is too permissive. While tmp.cols > dst.cols is valid as per the PR description, tmp.cols < dst.cols would likely lead to out-of-bounds access during the copy from tmp to dst. Consider enforcing tmp.cols >= dst.cols when both are static.

Furthermore, since tmp is used as a staging buffer for the merge operation, it should ideally be large enough to hold the sum of all source columns to avoid buffer overflows during the merge step.

Lastly, format2 should verify that all operands are rank-2 with 1 row, consistent with format1 validation.

    auto dstShape = getShapeVec(dstTy);
    auto tmpShape = getShapeVec(tmpTy);
    if (dstShape.size() != 2 || tmpShape.size() != 2)
      return emitOpError() << "format2 expects dst/tmp to be rank-2 tile-shaped";
    if ((dstShape[0] != mlir::ShapedType::kDynamic && dstShape[0] != 1) ||
        (tmpShape[0] != mlir::ShapedType::kDynamic && tmpShape[0] != 1))
      return emitOpError() << "format2 expects dst/tmp rows == 1";
    if (dstShape[1] != mlir::ShapedType::kDynamic &&
        tmpShape[1] != mlir::ShapedType::kDynamic &&
        tmpShape[1] < dstShape[1])
      return emitOpError() << "format2 expects tmp.cols >= dst.cols";
    for (Value src : getSrcs()) {
      Type srcTy = src.getType();
      auto sShape = getShapeVec(srcTy);
      if (sShape.size() != 2 || (sShape[0] != mlir::ShapedType::kDynamic && sShape[0] != 1))
        return emitOpError() << "format2 expects src to be rank-2 with 1 row";
      if (getElemTy(srcTy) != elemTy)

@Zhendong404 Zhendong404 force-pushed the fix-tmrgsort-format2 branch from c52ac10 to 6c3f204 Compare May 8, 2026 02:44
@zhangstevenunity zhangstevenunity merged commit 664a9a1 into hw-native-sys:main May 8, 2026
11 checks passed
@reedhecre
Copy link
Copy Markdown

A5 板测成功

  • 触发方式:merged
  • 源码提交:664a9a1008ac
  • 结果汇总:OK 17 / FAIL 0 / SKIP 0
  • 日志:/root/ptoas-board-monitor-a5/logs/20260508_113606_merged_pr641.log
  • 结果 TSV:/root/ptoas-board-monitor-a5/logs/20260508_113606_merged_pr641.tsv

@reedhecre
Copy link
Copy Markdown

A3 板测完成(有跳过)

  • 触发方式:merged
  • 源码提交:664a9a1008ac
  • 结果汇总:OK 207 / FAIL 0 / SKIP 1
  • 日志:/home/zhongxuan/ptoas-board-monitor/runtime/logs/20260508_211905_merged_pr641.log
  • 结果 TSV:/home/zhongxuan/ptoas-board-monitor/runtime/logs/20260508_211905_merged_pr641.tsv

mouliangyu pushed a commit to mouliangyu/PTOAS that referenced this pull request May 9, 2026
…rmat2

fix(pto): relax tmrgsort format2 tmp shape check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] pto.tmrgsort format2 verifier wrongly requires dst/tmp shapes to match

3 participants