Skip to content

Feature/a2a3 llvm#908

Open
castigli wants to merge 34 commits into
hw-native-sys:mainfrom
castigli:feature/a2a3-llvm
Open

Feature/a2a3 llvm#908
castigli wants to merge 34 commits into
hw-native-sys:mainfrom
castigli:feature/a2a3-llvm

Conversation

@castigli

@castigli castigli commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

A2/A3 VPTO Elementwise PR Summary

Summary

This branch adds the A2/A3 VPTO UB lowering path for tile elementwise operations and validates it end-to-end on A3 hardware. The implementation routes A2/A3 through the planned-memory UB pipeline (from A5 pipeline), lowers supported PTO tile ops to VPTO UB ops, and emits LLVM intrinsics.

What Changed

  • Added the A2/A3 VPTO UB lowering pipeline:
    PTOViewToMemref -> PTOPlanMemory -> PTOResolveReservedBuffers -> PTOMaterializeTileHandles -> LowerPTOToUBufOps.
  • Implemented A2/A3 UB lowering for binary elementwise ops:
    tadd, tsub, tmul, tdiv, tmax, tmin, tand, tor, txor, tshls, and tshrs.
  • Implemented unary elementwise ops:
    tabs, trelu, tneg, texp, tlog, tsqrt, trsqrt, and trecip.
  • Implemented scalar-tile elementwise ops:
    tadds, tmuls, tmaxs, and tmins.
  • Added fused elementwise support for taddrelu, including PTO IR, UB IR, lowering, LLVM intrinsic emission, PTODSL wrappers, and tests.
  • Added UB/LLVM support for new VPTO UB ops and intrinsics, including vaddrelu, vdup, and vln.
  • Integrated planned UB address allocation so UB lowering consumes planner-assigned alloc_tile addr values and models scratch usage for ops such as txor.
  • Extended dispatch coverage for small, normal, count, row-repeat, tail, chunked, and multi-row shapes.
  • Extended the PTODSL elementwise surface through helpers and pto.tile.* APIs.
  • Added lit and hardware e2e coverage for binary, unary, scalar, bitwise, shift, fused, log, and reciprocal cases.

Validation

Verified after merging current main with the LLVM 21 docker image:

  • Build: passed
  • VPTO UB lit: 60/60
  • Runtime toolchain pytest: 4/4
  • A3 unary hardware e2e: 80/80
  • A3 binary hardware e2e: 192/192
  • A3 scalar hardware e2e: 120/120

Total A2/A3 shared VPTO elementwise hardware coverage validated on A3: 392 tests.

@reedhecre

reedhecre commented Jul 7, 2026

Copy link
Copy Markdown

Codex Review

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

  • PR: Feature/a2a3 llvm #908 Feature/a2a3 llvm
  • Author: castigli
  • Base/Head: main / feature/a2a3-llvm
  • Head SHA: 2ca10958ae5f
  • Trigger: PR 有新提交
  • Generated At: 2026-07-14T11:15:50Z
  • Previous Head SHA: 7f54d1f1ed56
  • Status: failed at codex-review (exit=1)

Summary

Review failed at stage codex-review: exit=1

Findings

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

Log Tail

 .../vpto/ub/ub_to_llvm/elementwise/vadd_dtypes.pto |   87 +
 .../vpto/ub/ub_to_llvm/elementwise/vadd_packed.pto |   49 +
 .../vpto/ub/ub_to_llvm/elementwise/vaddrelu.pto    |   57 +
 test/lit/vpto/ub/ub_to_llvm/elementwise/vdiv.pto   |   30 +
 test/lit/vpto/ub/ub_to_llvm/elementwise/vdup.pto   |   27 +
 test/lit/vpto/ub/ub_to_llvm/elementwise/vln.pto    |   26 +
 .../vpto/ub/ub_to_llvm/elementwise/vmax_vmin.pto   |   64 +
 test/lit/vpto/ub/ub_to_llvm/elementwise/vmul.pto   |   30 +
 test/lit/vpto/ub/ub_to_llvm/elementwise/vsub.pto   |   30 +
 .../lit/vpto/ub/ub_to_llvm/mask/set_mask_count.pto |   47 +
 test/lit/vpto/ub/ub_to_llvm/mask/set_mask_full.pto |   26 +
 test/lit/vpto/ub/ub_to_llvm/mask/set_mask_tail.pto |   25 +
 .../lit/vpto/ub/ub_to_llvm/transfer/copy_gm_ub.pto |   26 +
 .../vpto/ub/ub_to_llvm/transfer/copy_gm_ub_pad.pto |   25 +
 .../lit/vpto/ub/ub_to_llvm/transfer/copy_ub_gm.pto |   25 +
 test/lit/vpto/ub/ub_to_llvm/transfer/mte_gm_ub.pto |   25 +
 .../vpto/ub/ub_to_llvm/transfer/mte_gm_ub_pad.pto  |   29 +
 tools/ptoas/ObjectEmission.cpp                     |   30 +-
 tools/ptoas/driver.cpp                             |    6 +-
 tools/ptoas/ptoas.cpp                              |   92 +-
 104 files changed, 15327 insertions(+), 132 deletions(-)
===== END STAGE clone rc=0 @ 2026-07-14 19:15:40 =====

===== STAGE codex-review @ 2026-07-14 19:15:40 =====
set -euo pipefail
cd '/tmp/ptoas-pr-review-monitor/runs/20260714_191529_pr908/repo'
'codex' exec -C '/tmp/ptoas-pr-review-monitor/runs/20260714_191529_pr908/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/20260714_191529_pr908/review_schema.json' -o '/tmp/ptoas-pr-review-monitor/runs/20260714_191529_pr908/codex_last_message.json' --color never - < '/tmp/ptoas-pr-review-monitor/runs/20260714_191529_pr908/review_prompt.txt'
[monitor] stage timeout: 1800s
OpenAI Codex v0.115.0 (research preview)
--------
workdir: /tmp/ptoas-pr-review-monitor/runs/20260714_191529_pr908/repo
model: gpt-5.4
provider: codereview
approval: never
sandbox: read-only
reasoning effort: xhigh
reasoning summaries: none
session id: 019f6056-ff5d-7433-b822-27cea04a9f17
--------
user
你现在在审查 GitHub PR。

仓库:hw-native-sys/PTOAS
PR:#908 Feature/a2a3 llvm
作者:castigli
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 #908 存在问题,并返回 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, cf-ray: a1b01c6eafebf04d-LAS, request id: c06f2d39-82c0-4a24-994e-659c73455fde)
Reconnecting... 2/5 (unexpected status 403 Forbidden: {"code":"INSUFFICIENT_BALANCE","message":"Insufficient account balance"}, url: https://codex.0u0o.com/responses, cf-ray: a1b01c71ce1d2acc-LAX, request id: 8d735904-af43-4c78-91c6-2e9f5f571ba4)
Reconnecting... 3/5 (unexpected status 403 Forbidden: {"code":"INSUFFICIENT_BALANCE","message":"Insufficient account balance"}, url: https://codex.0u0o.com/responses, cf-ray: a1b01c763b05cb96-LAX, request id: 9875af03-fd86-4c28-84f8-5616ac06e989)
Reconnecting... 4/5 (unexpected status 403 Forbidden: {"code":"INSUFFICIENT_BALANCE","message":"Insufficient account balance"}, url: https://codex.0u0o.com/responses, cf-ray: a1b01c7cc8a6b82d-LAS, request id: d4417066-987c-4621-abdc-3397bd18c85a)
Reconnecting... 5/5 (unexpected status 403 Forbidden: {"code":"INSUFFICIENT_BALANCE","message":"Insufficient account balance"}, url: https://codex.0u0o.com/responses, cf-ray: a1b01c887d4db171-LAS, request id: c4b7cf46-8fcb-4287-93f4-21a6d4e07c0a)
ERROR: unexpected status 403 Forbidden: {"code":"INSUFFICIENT_BALANCE","message":"Insufficient account balance"}, url: https://codex.0u0o.com/responses, cf-ray: a1b01c9f3f1dcba7-LAX, request id: c246dfe4-c841-440b-aa26-92af63ecadd9
Warning: no last agent message; wrote empty content to /tmp/ptoas-pr-review-monitor/runs/20260714_191529_pr908/codex_last_message.json
===== END STAGE codex-review rc=1 @ 2026-07-14 19:15:50 =====

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

Copy link
Copy Markdown
Contributor

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 implements a direct pointer-based lowering pipeline for A3 (dav-c220-vec) targets, introducing the LowerPTOToUBufOps pass and corresponding LLVM lowering patterns. The code review identified several critical and high-severity issues that must be addressed: a syntax/compilation error in VPTOLLVMEmitter.cpp due to a leftover lambda block; multiple checks in ptoas.cpp and LowerPTOToUBufOps.cpp that restrict the pipeline to "a3" and break "a2" support; an overly restrictive check in VPTOLLVMEmitter.cpp that excludes "dav-c220-cube" and triggers invalid A5 DMA lowering; redundant dead loop generation when headRepeats is zero; and a typo in a macro name in VPTO.cpp.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines 11066 to 11070
target.addLegalOp<ModuleOp>();
target.addDynamicallyLegalOp<func::FuncOp>([&](func::FuncOp op) {
target.addLegalOp<pto::AddPtrOp>();([&](func::FuncOp op) {
return typeConverter.isSignatureLegal(op.getFunctionType()) &&
typeConverter.isLegal(&op.getBody());
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

critical

There is a critical syntax/compilation error on line 11067. The statement target.addLegalOp<pto::AddPtrOp>(); ends with a semicolon, but is immediately followed by a lambda block ([&](func::FuncOp op) { ... }); which was left over from the previous addDynamicallyLegalOp call. This will cause a compilation failure.\n\nPlease restore the addDynamicallyLegalOp<func::FuncOp> call properly alongside the new addLegalOp<pto::AddPtrOp>() call.

  target.addLegalOp<ModuleOp>();\n  target.addLegalOp<pto::AddPtrOp>();\n  target.addDynamicallyLegalOp<func::FuncOp>([&](func::FuncOp op) {\n    return typeConverter.isSignatureLegal(op.getFunctionType()) &&\n           typeConverter.isLegal(&op.getBody());\n  });

Comment thread tools/ptoas/ptoas.cpp Outdated
auto &kernelModulePM = pm.nest<ModuleOp>();
auto moduleArchAttr =
module->getAttrOfType<mlir::StringAttr>("pto.target_arch");
const bool isA3 = moduleArchAttr && moduleArchAttr.getValue() == "a3";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The check isA3 only matches "a3". Since A2 and A3 share the same VPTO lowering pipeline (as documented in the design specs), this check should also include "a2". Otherwise, targeting "a2" will incorrectly fall back to the A5 ExpandTileOp path.

Suggested change
const bool isA3 = moduleArchAttr && moduleArchAttr.getValue() == "a3";
const bool isA3 = moduleArchAttr && (moduleArchAttr.getValue() == "a2" || moduleArchAttr.getValue() == "a3");

Comment thread tools/ptoas/ptoas.cpp Outdated
std::string arch = ptoTargetArch;
for (char &c : arch)
c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
options.march = (arch == "a3") ? "dav-c220-vec" : "dav-c310-vec";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The target architecture selection on line 2591 defaults to "dav-c310-vec" (the A5 target) if the architecture is not exactly "a3". If the user targets "a2", this will incorrectly select the A5 target instead of "dav-c220-vec".\n\nUpdate the ternary operator to map both "a2" and "a3" to "dav-c220-vec".

Suggested change
options.march = (arch == "a3") ? "dav-c220-vec" : "dav-c310-vec";
options.march = (arch == "a2" || arch == "a3") ? "dav-c220-vec" : "dav-c310-vec";

Comment thread tools/ptoas/ptoas.cpp Outdated
Comment on lines +2914 to +2915
const bool enableLocalTilePlanning =
ptoTargetArch != "a3" || effectiveBackend == PTOBackend::VPTO;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The check ptoTargetArch != "a3" will evaluate to true when targeting "a2", which incorrectly enables local tile planning unconditionally for "a2" even if the backend is not VPTO.\n\nSince A2 and A3 share the same pipeline, they should use the same condition.

  const bool isA2A3 = (ptoTargetArch == "a2" || ptoTargetArch == "a3");\n  const bool enableLocalTilePlanning =\n      !isA2A3 || effectiveBackend == PTOBackend::VPTO;

Comment on lines +221 to +223
auto archAttr = mod->getAttrOfType<StringAttr>("pto.target_arch");
if (!archAttr || archAttr.getValue() != "a3")
return;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The LowerPTOToUBufOpsPass currently returns early if the target architecture is not exactly "a3". This prevents the pass from running when targeting "a2", which shares the same lowering pipeline.\n\nUpdate the check to allow both "a2" and "a3".

    auto archAttr = mod->getAttrOfType<StringAttr>("pto.target_arch");\n    if (!archAttr || (archAttr.getValue() != "a2" && archAttr.getValue() != "a3"))\n      return;

Comment thread lib/PTO/Transforms/VPTOLLVMEmitter.cpp Outdated
Comment on lines +4605 to +4607
bool useA3NonPadded = (march == "dav-c220-vec") && isGmUb && !hasPadding;
bool useA3UbGm = (march == "dav-c220-vec") && !isGmUb;
bool useSingleConfig = useA3NonPadded || useA3UbGm;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The check march == "dav-c220-vec" is too restrictive because it excludes the A2/A3 cube target "dav-c220-cube". Both vector and cube targets on A2/A3 use the same single-config V220 DMA engine. If "dav-c220-cube" is targeted, this will incorrectly fall back to the A5 dual-config DMA lowering, which is invalid for A2/A3.\n\nUpdate the check to cover any C220 target (e.g., by checking if march starts with "dav-c220" or explicitly checking both vector and cube variants).

    bool isC220 = (march == "dav-c220-vec" || march == "dav-c220-cube");\n    bool useA3NonPadded = isC220 && isGmUb && !hasPadding;\n    bool useA3UbGm = isC220 && !isGmUb;\n    bool useSingleConfig = useA3NonPadded || useA3UbGm;

Comment on lines +1083 to +1095
auto forOp = b.create<scf::ForOp>(loc, idxc0(loc, b),
idxc(headRepeats, loc, b), idxc1(loc, b));
b.setInsertionPointToStart(forOp.getBody());
Value iv = forOp.getInductionVar();
Value off = b.create<arith::MulIOp>(loc, iv, idxc(epr, loc, b)).getResult();
Value rd = addPtr(loc, b, dst, ptrTy, off);
Value r0 = addPtr(loc, b, s0, ptrTy, off);
Value r1 = addPtr(loc, b, s1, ptrTy, off);
b.create<pto::UBSetMaskCountOp>(loc);
b.create<pto::UBSetMaskOp>(loc, i64c(epr, loc, b), i64c0(loc, b));
emitUBBinOp<UBop>(loc, b, rd, r0, r1, i64c1(loc, b), i64c8(loc, b));
b.create<pto::UBSetMaskNormOp>(loc);
b.setInsertionPointAfter(forOp);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

When headRepeats is 0 (which happens when totalV < epr), b.create<scf::ForOp> is still called to generate a loop from 0 to 0. While functionally a no-op, generating dead loops complicates downstream optimization passes and produces redundant IR.\n\nSince headRepeats is a compile-time constant, wrap the loop generation in a C++ conditional check if (headRepeats > 0).

    if (headRepeats > 0) {\n      auto forOp = b.create<scf::ForOp>(loc, idxc0(loc, b),\n                                          idxc(headRepeats, loc, b), idxc1(loc, b));\n      b.setInsertionPointToStart(forOp.getBody());\n      Value iv = forOp.getInductionVar();\n      Value off = b.create<arith::MulIOp>(loc, iv, idxc(epr, loc, b)).getResult();\n      Value rd = addPtr(loc, b, dst, ptrTy, off);\n      Value r0 = addPtr(loc, b, s0, ptrTy, off);\n      Value r1 = addPtr(loc, b, s1, ptrTy, off);\n      b.create<pto::UBSetMaskCountOp>(loc);\n      b.create<pto::UBSetMaskOp>(loc, i64c(epr, loc, b), i64c0(loc, b));\n      emitUBBinOp<UBop>(loc, b, rd, r0, r1, i64c1(loc, b), i64c8(loc, b));\n      b.create<pto::UBSetMaskNormOp>(loc);\n      b.setInsertionPointAfter(forOp);\n    }

Comment thread lib/PTO/IR/VPTO.cpp Outdated
return success();
}

#define PTO_DEFINE_UB_UNARY_VERITY_AND_EFFECTS(OpName) \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

There is a typo in the macro name: VERITY instead of VERIFY. Please rename it to PTO_DEFINE_UB_UNARY_VERIFY_AND_EFFECTS for consistency and readability.

Suggested change
#define PTO_DEFINE_UB_UNARY_VERITY_AND_EFFECTS(OpName) \
#define PTO_DEFINE_UB_UNARY_VERIFY_AND_EFFECTS(OpName) \\

Comment thread ptodsl/examples/bop_launch_a3.py Outdated
if __package__ in {None, ""}:
here = Path(__file__).resolve()
for candidate in here.parents:
if (candidate / "ptodsl" / "__init__.py").exists():

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

In latest main branch, you can install ptodsl package to the python env with pip install . --no-build-isolation. No need to manually search ptodsl path.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed. I removed the manual parent-directory sys.path probing from the example; it now assumes PTODSL is installed in the Python environment.

Comment thread ptodsl/examples/bop_launch_a3.py Outdated
if (candidate / "ptodsl" / "__init__.py").exists():
sys.path.insert(0, str(candidate))
break
else:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We already have some ST cases in test/tilelib-st. Maybe you can reuse them by just changing the target from a5 to a3.

Comment thread ptodsl/ptodsl/_ops.py
)


def taddrelu(src0, src1, dst):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this a A2A3-only op? Should restrict its usage inside A2A3 kernels.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please also add documents for this new interface in the ptodsl/docs/user_guide directory.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed. I documented pto.tile.addrelu in ptodsl/docs/user_guide/08-compute-operations.md, including formula, supported dtypes, and the A2/A3-only restriction.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is A2/A3-only. I added a PTODSL tracing-time target guard, an IR verifier rejection for A5, and regression coverage for both PTODSL diagnostics and hand-authored PTO IR.

@@ -0,0 +1,486 @@
# Copyright (c) 2026 Huawei Technologies Co., Ltd.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The naming of this test suite is not clear. I suggest reuse the TileOp ST cases we already have in tilelib-st, which will be completed soon.

For vpto level test, you should place them under the test/vpto directory.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can move these tests under test/tilelib-st if that is the preferred repository layout, but I’m worried we would lose clarity and potentially coverage.

The current pytest suite is a comprehensive PTODSL e2e sweep for the new A2/A3 VPTO elementwise path. It covers binary, unary, scalar, bitwise, shift, fused, log, and reciprocal ops across multiple dtypes and dispatch shapes.

My concern with test/tilelib-st is that the folder name is not very descriptive for this purpose: these are specifically PTODSL end-to-end tests for the A2/A3 VPTO lowering pipeline. Moving them there may make that less obvious.

If you still prefer the ST layout, I can migrate them, but I’d like to preserve the current breadth of coverage.

Comment thread docker/Dockerfile.dev
# =========================================================================
# 5. Torch + torch-npu (for on-device kernel launch)
# =========================================================================
RUN pip install --no-cache-dir torch==2.9.0 --index-url https://download.pytorch.org/whl/cpu \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Better use torch==2.9.0.post2 or 2.10.0 here for CANN-9.0.0, see compatibility matrix here: https://gitcode.com/Ascend/pytorch/blob/master/README.zh.md#ascend-extension-for-pytorch%E7%89%88%E6%9C%AC%E9%85%8D%E5%A5%97%E8%A1%A8

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch. The CANN 9.0.0 matrix pairs Ascend Extension for PyTorch 2.9.0.post2 with upstream PyTorch 2.9.0, so I kept torch==2.9.0 and updated torch-npu to 2.9.0.post2. I also updated the non-dev Dockerfile for consistency.

@castigli

Copy link
Copy Markdown
Contributor Author

Retriggering CI for latest head c80521f.

@castigli castigli closed this Jul 10, 2026
@castigli castigli reopened this Jul 10, 2026
@castigli
castigli requested a review from Zhendong404 July 13, 2026 09:17
@learning-chip

learning-chip commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

How many "non-elementwise" vector ops are left? (gather/scatter/reduce/...) Is it too big to also include them into this PR?

deriveLoadCbufToCbControl / deriveLoadCbufToCaControl called the raw
Type::getIntOrFloatBitWidth() accessor, which asserts on non-int/float
element types (FP4 packed, hifloat8, f8, ...). In assert builds this
crashed ptoas mid-pass (~60 VPTO/UB low-precision lit tests) with
'only integers and floats have a bitwidth'.

Use pto::getPTOStorageElemBitWidth() instead, matching the sibling
deriveLoadCbufToCaMxControl / deriveLoadCbufToCbMxControl functions and
the rest of the codebase. The existing (elemBitWidth == 0 || % 8) guard
already matches the helper's 0-on-unknown contract.
@castigli

Copy link
Copy Markdown
Contributor Author

How many "non-elementwise" vector ops are left? (gather/scatter/reduce/...) Is it too big to also include them into this PR?

As you said, only the gather/scatter/reduce are missing. Not a large addition, I will add them in this PR.

@zhangstevenunity zhangstevenunity left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Review: A2/A3 VPTO elementwise

Deep pass over the lowering (LowerPTOToUBufOps), the LLVM intrinsic emitter, the CLI/pipeline wiring, the ODS/verifiers/effects, and the PTODSL surface. Overall the A2/A3 elementwise path is well-built: the intrinsic emission is internally consistent with the design docs, the new ops (vaddrelu/vdup/vln) and the txor tmp-scratch effects modeling are correct, and most of the earlier Gemini review was against a stale commit and is addressed at HEAD.

The findings that stand out are all cases where this A2/A3 PR reaches into the shared A5 / VPTO device-emission path:

  • P2 buildVPTOEmissionOptions now mis-targets A5 VPTO cube kernels (dav-c310-vec instead of dav-c310-cube). Inline on ptoas.cpp:2651.
  • P2 usesCANN900Lowering flipped to unconditional false disables the CANN900 emitter for all targets/CANN versions, not just A3. Inline on VPTOLLVMEmitterDispatcher.cpp:15.
  • P2 The lowering silently drops each tile's valid-shape, so the valid-shape-aware dispatch modes (and the lit tests named after them) are effectively dead, and every op processes padding. Inline on LowerPTOToUBufOps.cpp:252.
  • P3 The PTODSL native launch-wrapper compile doesn't receive target_arch. Inline on toolchain.py:173.

Verified clean (not bugs at HEAD)

  • txor's tmp is a required operand and WRITE-modeled in getEffects; PlanMemory gives it a distinct buffer and LowerPTOToUBufOps decomposes txor atomically -> no aliasing/missing-sync bug (the recurring scratch-effect bug class is not present here).
  • taddrelu A2/A3-only: TAddReluOp::verify + dispatchVerifierByArch reject a5, and the dtype gate i16/f16/f32 matches the available VADDRELU intrinsics; PTODSL has a trace-time guard plus negative tests.
  • vaddrelu/vdup/vln intrinsic names, operand order, and config-word layouts match the design doc and the lit tests; the CBuf storage-bit-width fix (2ca1095) is sound.
  • Gemini's == "dav-c220-vec" cube-exclusion flag is a false alarm: the elementwise op-pattern/legality sites (10994/11166) are vector-only by design and march is never dav-c220-cube on this path. Gemini's leftover-lambda compile error and VERITY typo were on a stale commit and are not present at HEAD.

Lower-priority notes

  • UB op verifiers only check the UB memory-role, not that dst/src share element type/shape; a hand-written .pto with mismatched pointer element types picks the intrinsic from src0 and miscompiles with no diagnostic (VPTOLLVMEmitter.cpp:4744). Compiler-generated IR is always consistent, so this is a robustness gap.
  • set_mask hardware state is written by UBSetMask* but not read by the compute ops, so ordering is positional only; safe today (no scheduler on this IR post-lowering) but latent if one is added.
  • vdup rejects bf16 (clean compile error, not a miscompile); scalar-tile bf16 would pass an i64 bit-pattern to VMULS.bf16 (out of scope per doc, unguarded).
  • _ptoas_build_backend: build_editable(skip_install=True) no longer validates the runtime payload landed where the .pth points; _find_llvm_dir auto-probe may select an Ascend-shipped LLVM over the vpto-patched one on env-unset machines.

CI

The Codex review bot failed on INSUFFICIENT_BALANCE (not a code signal). No CI checks are reported on the branch, and the PR is currently CONFLICTING with main.

Comment thread tools/ptoas/ptoas.cpp Outdated
options.targetTriple = "hiipu64-hisilicon-cce";
options.cannVersion = cannVersion;
std::string arch = normalizeArch(targetArch);
options.march = isA2A3Arch(arch) ? "dav-c220-vec" : "dav-c310-vec";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

P2 (A5 VPTO regression, outside the PR's A2/A3 scope). buildVPTOEmissionOptions now unconditionally sets options.march, so for A5 it becomes the non-empty string "dav-c310-vec". On main this field was left empty.

Downstream, makeDeviceEmissionOptions (VPTOLLVMEmitter.cpp:11395) branches on options.march.empty(). With a non-empty march the Cube kernel kind now takes the else branch and gets aicoreArch = defaultTargetCPU = march = "dav-c310-vec" (vector); only defaultTargetFeatures resolves to the c310-cube string (line 11414-11415). ObjectEmission.cpp::resolveTargetCPU then reads that "dav-c310-vec" target-cpu attr and passes -mcpu=dav-c310-vec to bisheng for the cube object.

Net effect: ptoas --pto-arch=a5 --pto-backend=vpto <kernel with a cube section> emits the cube (AIC) device object targeting the vector (AIV) sub-core -- silent wrong-target codegen. On main the empty-march path correctly produced dav-c310-cube. A5 vector modules are unaffected. Not covered by the new tests (all a2/a3 vector).

Suggest only seeding march for a2/a3 and leaving it empty for a5 so the per-kind defaulting keeps working:

if (isA2A3Arch(arch))
  options.march = "dav-c220-vec";
// a5: leave march empty; makeDeviceEmissionOptions picks vec/cube per kind


static bool usesCANN900Lowering(const CANNVersion &cannVersion) {
return cannVersion >= CANNVersion::release(9, 0, 0);
return false; // Force Beta1 path until CANN900 emitter is stable for A3

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

P2 (behavior change beyond A2/A3 scope). This flips usesCANN900Lowering from cannVersion >= 9.0.0 to an unconditional return false, so every VPTO device emission -- including A5 on CANN 9.0.0+ -- is now routed through the Beta1 emitter instead of the CANN900 emitter, and lowerVPTOModuleToLLVMModulesCANN900 becomes dead code. The comment scopes the intent to A3, but the gate is global (no arch/version check).

If A5 (or other) VPTO targets on CANN9 previously went through the CANN900 path and the two emitters are not at parity, this is a silent codegen change for them, buried in an A2/A3 PR. Consider gating on isA2A3 (or on arch) rather than a blanket return false, or explicitly confirm A5 VPTO board/lit coverage still passes on CANN9 with Beta1.

ctx, tbTy.getElementType(),
pto::AddressSpaceAttr::get(ctx, pto::AddressSpace::VEC));
auto pc = builder.create<pto::CastPtrOp>(op.getLoc(), ptrTy, addr);
tileShapes[pc.getResult()] = SmallVector<int64_t, 2>(shape);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

P2 (from code inspection -- the valid-shape dispatch is effectively dead). This rewrite stores only the padded shape into tileShapes, and the pto::PtrType branch of extractTileShapeInfoFromValue (line 156-161) leaves validShape empty. After this rewrite every compute-op operand is a PtrType, so extractTileShapeInfo computes vCols == cols and vRows == rows for all alloc_tile-derived tiles -- which is every lit test and the common kernel case.

Consequences:

  1. In dispatch (line 1437) the vCols == cols guard is then always true, so the entire non-continuous subtree -- modeColVLAlign, modeCount2L, modeRowRpt, rowRptFast/rowRptChunked, headRows/tailRows, and the tailStride* helpers -- is unreachable. The lit tests named after those modes (tadd_col_vl_align, tadd_row_rpt[_tail], tadd_chunked[_tail], and tadd_count) actually lower through modeNorm1L/modeCount1L/modeSmall; they pass only because the CHECKs are structural (no test pins the emitted set_mask count for a v_col < cols shape). So the valid-shape paths have no real coverage.

  2. Every op is lowered against the padded width, so it reads/writes the padding columns (uninitialized UB) rather than just [0, v_col). Valid-region results stay correct only because tiles are allocated at padded size (PlanMemory uses getShape()), but it processes up to cols/v_col x more data and touches uninitialized memory (e.g. vln/vrsqrt/vdiv over padding).

Separately, the unary/scalar/dup paths (dispatchUnary/dispatchShift/dispatchDup, ~line 933-1079) iterate flat over vRows*vCols with no per-row cols stride, unlike the binary dispatch. That is only safe because validShape is currently dropped (flat == full contiguous buffer). If a valid-shape-preserving TileBufType tile (e.g. a non-alloc block-arg) with v_col < cols and v_row > 1 ever reaches a unary/scalar op, those paths would skip rows / process padding as valid (wrong result), because they lack the guard the binary path has.

Suggestion: capture the tile's valid shape into tileShapes (or key the dispatch off the alloc_tile valid_row/valid_col), and add a lit test that pins the set_mask count for a padded multi-row shape so these modes get real coverage.

Comment thread ptodsl/ptodsl/_runtime/toolchain.py Outdated


def aicore_arch_for_kernel_kind(kernel_kind: str) -> str:
def aicore_arch_for_kernel_kind(kernel_kind: str, target_arch: str = "a5") -> str:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

P3 (cross-layer completeness). This adds the target_arch parameter (and test_runtime_toolchain.py calls it directly), but the only production caller -- _kernel_compile_flags(kernel_kind) in native_build.py:126 -- calls aicore_arch_for_kernel_kind(kernel_kind) without target_arch, so it always defaults to "a5" and emits --cce-aicore-arch=dav-c310-vec for the launch.cpp wrapper. build_native_library threads module_spec.target_arch into the kernel-object compile (_run_ptoas) but not into _compile_launch_cpp.

So on A2/A3 the kernel object is built for dav-c220 while the launch stub is built for dav-c310. The new unit test passes but never exercises the real call path, giving false confidence. (The A3 e2e apparently still links/runs, so the wrapper's arch may not be load-bearing, but the two objects should agree.) Thread module_spec.target_arch through _compile_launch_cpp -> _kernel_compile_flags -> aicore_arch_for_kernel_kind.

@zhangstevenunity zhangstevenunity left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I found five correctness issues while reviewing the A2/A3 VPTO lowering and PTODSL integration. Inline details and reproductions are included below.

ctx, tbTy.getElementType(),
pto::AddressSpaceAttr::get(ctx, pto::AddressSpace::VEC));
auto pc = builder.create<pto::CastPtrOp>(op.getLoc(), ptrTy, addr);
tileShapes[pc.getResult()] = SmallVector<int64_t, 2>(shape);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Only the physical shape is stored before replaceAllUsesWith converts every tile operand to pto.ptr. From then on extractTileShapeInfoFromValue takes the pointer path, has no validShape, and falls back to vRows=rows/vCols=cols. I reproduced this with the existing 4x96 f32, v_col=32 test: lowering emits six 64-element iterations (all 384 physical elements) instead of operating on 4x32 valid elements with a 96-element row stride. Please preserve both physical and valid shapes in the pointer metadata and make the test assert the iteration count and offsets.

int64_t totalRpts = (totalV + epr - 1) / epr;
b.create<pto::UBSetMaskCountOp>(loc);
b.create<pto::UBSetMaskOp>(loc, i64c(totalV, loc, b), i64c0(loc, b));
emitUBBinOp<UBop>(loc, b, dst, s0, s1, i64c(totalRpts, loc, b), i64c8(loc, b));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

dispatch calls this helper only when totalRpts > 255, yet the helper passes that value straight to the UB op. The 16x1024 f32 test lowers to repeat = 256; LowerUBufBinary masks repeat with 0xff, so it is encoded as zero. The new design document also says C220 count mode does not support repeat > 1. Please split this into repeat-1 chunks or a loop and update the current CHECK-NOT: scf.for expectation.

b.setInsertionPointAfterValue(view);
DmaViewInfo info;
auto ptrTy = pto::PtrType::get(ctx, memTy.getElementType(), msAttr);
Value root = traceRootMemRef(view);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

At this point PTOViewToMemref has already converted partition_view to memref.subview. Tracing to the root here discards the subview offset, while the following code rebuilds row-major strides and fills all offsets with zero, also discarding non-contiguous layouts. With a 17x32 view partitioned at [1, 0], lowering computes a zero byte offset instead of 128 bytes. Please extract strided metadata and accumulate the real view offsets and strides rather than reconstructing them.

}

void fullMask(Location loc, OpBuilder &b) {
b.create<pto::UBSetMaskOp>(loc, i64cM1(loc, b), i64c0(loc, b));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

fullMask emits mask0=-1, mask1=0, but this PR's own UBSetMask documentation says full-write mode is -1, -1; mask1 controls lanes 64-127. Consequently, an f16 operation following a masked or count-mode operation can leave the upper half disabled. Please restore both mask words and add a chained masked-to-normal regression test.

Comment thread ptodsl/ptodsl/_runtime/toolchain.py Outdated


def aicore_arch_for_kernel_kind(kernel_kind: str) -> str:
def aicore_arch_for_kernel_kind(kernel_kind: str, target_arch: str = "a5") -> str:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The new target_arch argument is never passed by native build: _kernel_compile_flags still calls aicore_arch_for_kernel_kind(kernel_kind), and _compile_launch_cpp does not receive module_spec.target_arch. A2/A3 launcher code is therefore still compiled for dav-c310-* while PTOAS emits a C220 kernel. Please thread target_arch through the call chain and test the actual Bisheng command rather than only this helper.

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.

6 participants