Skip to content

feat(ptodsl): expose pto.section("cube") and pto.section("vector") interface as an explicit section hint - #1041

Open
Zhendong404 wants to merge 7 commits into
hw-native-sys:mainfrom
Zhendong404:feature-dsl-section-hint
Open

feat(ptodsl): expose pto.section("cube") and pto.section("vector") interface as an explicit section hint#1041
Zhendong404 wants to merge 7 commits into
hw-native-sys:mainfrom
Zhendong404:feature-dsl-section-hint

Conversation

@Zhendong404

@Zhendong404 Zhendong404 commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Add automatic Cube/Vector section inference for pto.mi operations.
  • Expose pto.section("cube") and pto.section("vector") as explicit section hints.
  • Emit an actionable diagnostic when a section cannot be inferred unambiguously.

Motivation

Mixed Cube/Vector kernels require each operation region to be assigned to a physical section. PTOAS can usually infer the section from the operation kind, pipeline, or buffer address space.

However, the current inference cannot determine the section of set_flag / wait_flag operations between PIPE_MTE2 and PIPE_S. Both pipelines are available on the Cube and Vector cores, so the pipe pair alone does not provide enough information to identify the intended physical section.

Silently choosing either section could result in incorrect placement. Therefore, when such an ambiguous region cannot be classified from its surrounding operations, PTOAS now reports an explicit error and asks the user to provide a section hint:

with pto.section("cube"):
    pto.set_flag("MTE2", "S", event_id=0)

with pto.section("vector"):
    pto.wait_flag("MTE2", "S", event_id=0)

Explicit section hints are intended as an escape hatch for ambiguous regions. Regions that can be classified unambiguously continue to use automatic inference.

Changes

  • Infer Cube/Vector sections for supported pto.mi operations.
  • Add the pto.section("cube") and pto.section("vector") PTODSL context manager.
  • Reject ambiguous uncovered regions with an actionable diagnostic instead of guessing their placement.
  • Add tests and documentation for automatic inference, explicit hints, and ambiguous PIPE_MTE2PIPE_S synchronization.

@Zhendong404
Zhendong404 force-pushed the feature-dsl-section-hint branch from 21bd65b to b52e71a Compare July 29, 2026 06:46

if (*source == AddressSpace::ACC)
return InferredSectionKind::Cube;
if (*source == AddressSpace::VEC || *destination == AddressSpace::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.

UB -> L1 和 L1 -> UB 会归为 Vector, 是否会有问题

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

已修改判定逻辑,消除误判

outer = Module.create()
outer.operation.attributes["pto.target_arch"] = StringAttr.get(spec.target_arch)

if spec.kernel_kind is None:

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.

纯simt的kernel是否会有问题

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

已增加对simt kernel的适配

@Zhendong404
Zhendong404 force-pushed the feature-dsl-section-hint branch 3 times, most recently from 07ffcc9 to 5f34398 Compare July 30, 2026 06:48
@Zhendong404

Copy link
Copy Markdown
Collaborator Author

/run a3

@Zhendong404

Copy link
Copy Markdown
Collaborator Author

/run a5

@reedhecre

Copy link
Copy Markdown

已接收 /run a5,A5 板测器会处理这条请求。

页面会自动刷新,可以直接看当前阶段、排队情况和最近结果。

@reedhecre

Copy link
Copy Markdown

A5 板测失败

日志尾部

nc_a5.py
Sync(test_mem_inject_sync_basic.py) FAIL python failed: test_mem_inject_sync_basic.py
Sync(test_set_wait_unified_api.py) FAIL python failed: test_set_wait_unified_api.py
Sync(test_tmov_col_major_16x1_align_a5.pto) OK   generated: test_tmov_col_major_16x1_align_a5.cpp
Sync(test_tmov_col_major_16x1_align_a5.py) OK   generated: test_tmov_col_major_16x1_align_a5-pto.cpp
Sync(test_tmov_row_major_1x16_control_a5.pto) OK   generated: test_tmov_row_major_1x16_control_a5.cpp
Sync(test_tmov_row_major_1x16_control_a5.py) OK   generated: test_tmov_row_major_1x16_control_a5-pto.cpp
Sync(tmatmulk_autosync_a5.py) FAIL python failed: tmatmulk_autosync_a5.py
TileSetGetValue(tile_getval_mat_invalid.py) XFAIL python failed as expected
TileSetGetValue(tileSetGetValue.py) FAIL python failed: tileSetGetValue.py
TInsert(tinsert_fp.py) FAIL python failed: tinsert_fp.py
TInsert(tinsert.py) FAIL python failed: tinsert.py
Tpows(tpows.py) FAIL python failed: tpows.py
Tpow(tpow.py) FAIL python failed: tpow.py
TPrefetchAsync(tprefetch_async_binding.py) FAIL python failed: tprefetch_async_binding.py
TPrefetch(tprefetch.py) FAIL python failed: tprefetch.py
TquantMx(tquant_mx.pto) OK   generated: tquant_mx-pto.cpp
Trans(trans.py) FAIL python failed: trans.py
Trap(trap.py) FAIL python failed: trap.py
TTri(ttri.py) FAIL python failed: ttri.py
VectorAddition(vadd_pto_ir.py) FAIL python failed: vadd_pto_ir.py
VectorAddition(vadd_validshape_hyper.py) FAIL python failed: vadd_validshape_hyper.py
VectorAddition(vectorAddition.py) FAIL python failed: vectorAddition.py
Xors(xors.py) FAIL python failed: xors.py
Xor(xor.py)  FAIL python failed: xor.py
-----------------------------
OK=83  FAIL=161  SKIP=29
=============================
===== END STAGE sample-build-and-test rc=1 @ 2026-07-30 17:57:07 =====

@reedhecre

Copy link
Copy Markdown

已接收 /run a3,A3 板测器会处理这条请求。

页面会自动刷新,可以直接看当前阶段、排队情况和最近结果。

@reedhecre

Copy link
Copy Markdown

A3 板测失败

失败用例

  • syncall_binding (run, exit=2)
  • comm_p2p_binding_variants (run, exit=1)
  • comm_p2p (run, exit=1)

@reedhecre

Copy link
Copy Markdown

A3 板测失败详情:PR #1041

syncall_binding

stage=run info=exit=2

/home/zhongxuan/ptoas-board-monitor/runtime/runs/20260730_032308_manual_pr1041/npu_validation/SyncAll/syncall_binding/syncall_binding_kernel.cpp:96:3: error: no matching function for call to 'SYNCALL'
  SYNCALL<SyncAllMode::Soft, SyncCoreType::AIVOnly>(v8, v10, v2);
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/zhongxuan/ptoas-board-monitor/runtime/runs/20260730_032308_manual_pr1041/payload/pto-isa/include/pto/common/pto_instr.hpp:67:15: note: candidate function template not viable: requires at most 2 arguments, but 3 were provided
PTO_INST void SYNCALL(GlobalData& gmWorkspace, int32_t usedCores = 0)
              ^
/home/zhongxuan/ptoas-board-monitor/runtime/runs/20260730_032308_manual_pr1041/payload/pto-isa/include/pto/common/pto_instr.hpp:53:15: note: candidate function template not viable: requires 0 arguments, but 3 were provided
PTO_INST void SYNCALL()
              ^
1 error generated.
gmake[2]: *** [CMakeFiles/syncall_binding_kernel.dir/build.make:76: CMakeFiles/syncall_binding_kernel.dir/syncall_binding_kernel.cpp.o] Error 1
gmake[2]: *** Waiting for unfinished jobs....
gmake[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/syncall_binding_kernel.dir/all] Error 2
gmake: *** [Makefile:91: all] Error 2
[2026-07-30 03:41:43] ERROR: testcase failed (exit 2): syncall_binding
comm_p2p_binding_variants

stage=run info=exit=1

[ERROR] aclrtSynchronizeStream(stream) failed: 507035 (/home/zhongxuan/ptoas-board-monitor/runtime/runs/20260730_032308_manual_pr1041/npu_validation/CommSync/comm_p2p_binding_variants/main.cpp:122)
[ERROR] RecentErrMsg: EZ9999: Inner Error!
EZ9999[PID: 283439] 2026-07-30-03:54:05.973.244 (EZ9999):  The error from device(chipId:0, dieId:1), serial number is 2396, there is an exception of aivec error, core id is 45, error code = 0x10, dump info: pc start: 0x124200000000, current: 0x124200000164, vec error info: 0x4000000b7, mte error info: 0x6d0600009a, ifu error info: 0x2ffffff080d40, ccu error info: 0x40e008000000004f, cube error info: 0, biu error info: 0, aic error mask: 0x6500020bd00028c, para base: 0x12c100000000.[FUNC:PrintCoreInfo][FILE:device_error_core_proc.cc][LINE:645]
        TraceBack (most recent call last):
       The extend info: errcode:(0x10, 0, 0) errorStr: Illegal instruction, which is usually caused by unaligned UUB addresses. fixp_error0 info: 0x600009a, fixp_error1 info: 0x6d, fsmId:1, tslot:6, thread:0, ctxid:0, blk:0, sublk:0, subErrType:4.[FUNC:PrintCoreInfo][FILE:device_error_core_proc.cc][LINE:658]
       Kernel task happen error, retCode=0x31, [vector core exception].[FUNC:PreCheckTaskErr][FILE:davinci_kernel_task.cc][LINE:1729]
       AIV Kernel happen error, retCode=0x31.[FUNC:GetError][FILE:stream.cc][LINE:1475]
       [AIC_INFO] after execute:args print end[FUNC:GetError][FILE:stream.cc][LINE:1475]
       [DFX_INFO]Aicore kernel execute failed, device_id=1, stream_id=46, report_stream_id=46, task_id=0, flip_num=0, fault kernel_name=comm_p2p_binding_variants_kernel, fault kernel info ext=comm_p2p_binding_variants_kernel, program id=0, hash=16541545288638093324.[FUNC:GetError][FILE:stream.cc][LINE:1475]
       rtStreamSynchronize execution failed, reason=vector core exception[FUNC:FuncErrorReason][FILE:error_message_manage.cc][LINE:65]
       synchronize stream failed, runtime result = 507035[FUNC:ReportCallError][FILE:log_inner.cpp][LINE:148]
[2026-07-30 03:54:07] ERROR: testcase failed (exit 1): comm_p2p_binding_variants
comm_p2p

stage=run info=exit=1

[ERROR] aclrtSynchronizeStream(stream) failed: 507035 (/home/zhongxuan/ptoas-board-monitor/runtime/runs/20260730_032308_manual_pr1041/npu_validation/CommSync/comm_p2p/main.cpp:122)
[ERROR] RecentErrMsg: EZ9999: Inner Error!
EZ9999[PID: 295408] 2026-07-30-03:54:24.837.633 (EZ9999):  The error from device(chipId:0, dieId:1), serial number is 2397, there is an exception of aivec error, core id is 0, error code = 0x10, dump info: pc start: 0x124200000000, current: 0x124200000168, vec error info: 0xdf00005f59, mte error info: 0xa5030020d8, ifu error info: 0x212c940104080, ccu error info: 0x40e008000000004f, cube error info: 0, biu error info: 0, aic error mask: 0x6500020bd00028c, para base: 0x12c100000000.[FUNC:PrintCoreInfo][FILE:device_error_core_proc.cc][LINE:645]
        TraceBack (most recent call last):
       The extend info: errcode:(0x10, 0, 0) errorStr: Illegal instruction, which is usually caused by unaligned UUB addresses. fixp_error0 info: 0x30020d8, fixp_error1 info: 0xa5, fsmId:0, tslot:6, thread:0, ctxid:0, blk:0, sublk:0, subErrType:4.[FUNC:PrintCoreInfo][FILE:device_error_core_proc.cc][LINE:658]
       Kernel task happen error, retCode=0x31, [vector core exception].[FUNC:PreCheckTaskErr][FILE:davinci_kernel_task.cc][LINE:1729]
       AIV Kernel happen error, retCode=0x31.[FUNC:GetError][FILE:stream.cc][LINE:1475]
       [AIC_INFO] after execute:args print end[FUNC:GetError][FILE:stream.cc][LINE:1475]
       [DFX_INFO]Aicore kernel execute failed, device_id=1, stream_id=46, report_stream_id=46, task_id=0, flip_num=0, fault kernel_name=comm_p2p_kernel, fault kernel info ext=comm_p2p_kernel, program id=0, hash=6260346515940177617.[FUNC:GetError][FILE:stream.cc][LINE:1475]
       rtStreamSynchronize execution failed, reason=vector core exception[FUNC:FuncErrorReason][FILE:error_message_manage.cc][LINE:65]
       synchronize stream failed, runtime result = 507035[FUNC:ReportCallError][FILE:log_inner.cpp][LINE:148]
[2026-07-30 03:54:26] ERROR: testcase failed (exit 1): comm_p2p

@Zhendong404
Zhendong404 force-pushed the feature-dsl-section-hint branch 2 times, most recently from ede4431 to 3906634 Compare July 30, 2026 15:09
@Zhendong404

Copy link
Copy Markdown
Collaborator Author

/run a5

@reedhecre

Copy link
Copy Markdown

已接收 /run a5,A5 板测器会处理这条请求。

页面会自动刷新,可以直接看当前阶段、排队情况和最近结果。

@reedhecre

Copy link
Copy Markdown

A5 板测失败

日志尾部

nc_a5.py
Sync(test_mem_inject_sync_basic.py) FAIL python failed: test_mem_inject_sync_basic.py
Sync(test_set_wait_unified_api.py) FAIL python failed: test_set_wait_unified_api.py
Sync(test_tmov_col_major_16x1_align_a5.pto) OK   generated: test_tmov_col_major_16x1_align_a5.cpp
Sync(test_tmov_col_major_16x1_align_a5.py) OK   generated: test_tmov_col_major_16x1_align_a5-pto.cpp
Sync(test_tmov_row_major_1x16_control_a5.pto) OK   generated: test_tmov_row_major_1x16_control_a5.cpp
Sync(test_tmov_row_major_1x16_control_a5.py) OK   generated: test_tmov_row_major_1x16_control_a5-pto.cpp
Sync(tmatmulk_autosync_a5.py) FAIL python failed: tmatmulk_autosync_a5.py
TileSetGetValue(tile_getval_mat_invalid.py) XFAIL python failed as expected
TileSetGetValue(tileSetGetValue.py) FAIL python failed: tileSetGetValue.py
TInsert(tinsert_fp.py) FAIL python failed: tinsert_fp.py
TInsert(tinsert.py) FAIL python failed: tinsert.py
Tpows(tpows.py) FAIL python failed: tpows.py
Tpow(tpow.py) FAIL python failed: tpow.py
TPrefetchAsync(tprefetch_async_binding.py) FAIL python failed: tprefetch_async_binding.py
TPrefetch(tprefetch.py) FAIL python failed: tprefetch.py
TquantMx(tquant_mx.pto) OK   generated: tquant_mx-pto.cpp
Trans(trans.py) FAIL python failed: trans.py
Trap(trap.py) FAIL python failed: trap.py
TTri(ttri.py) FAIL python failed: ttri.py
VectorAddition(vadd_pto_ir.py) FAIL python failed: vadd_pto_ir.py
VectorAddition(vadd_validshape_hyper.py) FAIL python failed: vadd_validshape_hyper.py
VectorAddition(vectorAddition.py) FAIL python failed: vectorAddition.py
Xors(xors.py) FAIL python failed: xors.py
Xor(xor.py)  FAIL python failed: xor.py
-----------------------------
OK=83  FAIL=161  SKIP=29
=============================
===== END STAGE sample-build-and-test rc=1 @ 2026-07-30 23:20:07 =====

@Zhendong404

Copy link
Copy Markdown
Collaborator Author

/run a5

@reedhecre

Copy link
Copy Markdown

已接收 /run a5,A5 板测器会处理这条请求。

页面会自动刷新,可以直接看当前阶段、排队情况和最近结果。

@reedhecre

Copy link
Copy Markdown

A5 板测失败

日志尾部

lue FAIL Python cannot import the built ptoas MLIR bindings; set PYTHON_BIN to the interpreter used by CMake
TInsert      FAIL Python cannot import the built ptoas MLIR bindings; set PYTHON_BIN to the interpreter used by CMake
Tpow         FAIL Python cannot import the built ptoas MLIR bindings; set PYTHON_BIN to the interpreter used by CMake
Tpows        FAIL Python cannot import the built ptoas MLIR bindings; set PYTHON_BIN to the interpreter used by CMake
TPrefetchAsync FAIL Python cannot import the built ptoas MLIR bindings; set PYTHON_BIN to the interpreter used by CMake
TPrefetch    FAIL Python cannot import the built ptoas MLIR bindings; set PYTHON_BIN to the interpreter used by CMake
TPushTPop    FAIL Python cannot import the built ptoas MLIR bindings; set PYTHON_BIN to the interpreter used by CMake
TquantMx     FAIL Python cannot import the built ptoas MLIR bindings; set PYTHON_BIN to the interpreter used by CMake
Trans        FAIL Python cannot import the built ptoas MLIR bindings; set PYTHON_BIN to the interpreter used by CMake
Trap         FAIL Python cannot import the built ptoas MLIR bindings; set PYTHON_BIN to the interpreter used by CMake
TTri         FAIL Python cannot import the built ptoas MLIR bindings; set PYTHON_BIN to the interpreter used by CMake
VectorAddition FAIL Python cannot import the built ptoas MLIR bindings; set PYTHON_BIN to the interpreter used by CMake
Xor          FAIL Python cannot import the built ptoas MLIR bindings; set PYTHON_BIN to the interpreter used by CMake
Xors         FAIL Python cannot import the built ptoas MLIR bindings; set PYTHON_BIN to the interpreter used by CMake
-----------------------------
OK=0  FAIL=138  SKIP=0
=============================
===== END STAGE sample-build-and-test rc=1 @ 2026-07-30 23:33:18 =====

@Zhendong404
Zhendong404 force-pushed the feature-dsl-section-hint branch from 1818aa8 to c93e90d Compare July 31, 2026 00:45
@Zhendong404

Copy link
Copy Markdown
Collaborator Author

/run a5

@reedhecre

Copy link
Copy Markdown

已接收 /run a5,A5 板测器会处理这条请求。

页面会自动刷新,可以直接看当前阶段、排队情况和最近结果。

@reedhecre

Copy link
Copy Markdown

A5 板测失败

日志尾部

nc_a5.py
Sync(test_mem_inject_sync_basic.py) FAIL python failed: test_mem_inject_sync_basic.py
Sync(test_set_wait_unified_api.py) FAIL python failed: test_set_wait_unified_api.py
Sync(test_tmov_col_major_16x1_align_a5.pto) OK   generated: test_tmov_col_major_16x1_align_a5.cpp
Sync(test_tmov_col_major_16x1_align_a5.py) OK   generated: test_tmov_col_major_16x1_align_a5-pto.cpp
Sync(test_tmov_row_major_1x16_control_a5.pto) OK   generated: test_tmov_row_major_1x16_control_a5.cpp
Sync(test_tmov_row_major_1x16_control_a5.py) OK   generated: test_tmov_row_major_1x16_control_a5-pto.cpp
Sync(tmatmulk_autosync_a5.py) FAIL python failed: tmatmulk_autosync_a5.py
TileSetGetValue(tile_getval_mat_invalid.py) XFAIL python failed as expected
TileSetGetValue(tileSetGetValue.py) FAIL python failed: tileSetGetValue.py
TInsert(tinsert_fp.py) FAIL python failed: tinsert_fp.py
TInsert(tinsert.py) FAIL python failed: tinsert.py
Tpows(tpows.py) FAIL python failed: tpows.py
Tpow(tpow.py) FAIL python failed: tpow.py
TPrefetchAsync(tprefetch_async_binding.py) FAIL python failed: tprefetch_async_binding.py
TPrefetch(tprefetch.py) FAIL python failed: tprefetch.py
TquantMx(tquant_mx.pto) OK   generated: tquant_mx-pto.cpp
Trans(trans.py) FAIL python failed: trans.py
Trap(trap.py) FAIL python failed: trap.py
TTri(ttri.py) FAIL python failed: ttri.py
VectorAddition(vadd_pto_ir.py) FAIL python failed: vadd_pto_ir.py
VectorAddition(vadd_validshape_hyper.py) FAIL python failed: vadd_validshape_hyper.py
VectorAddition(vectorAddition.py) FAIL python failed: vectorAddition.py
Xors(xors.py) FAIL python failed: xors.py
Xor(xor.py)  FAIL python failed: xor.py
-----------------------------
OK=83  FAIL=161  SKIP=29
=============================
===== END STAGE sample-build-and-test rc=1 @ 2026-07-31 08:54:08 =====

@Zhendong404
Zhendong404 force-pushed the feature-dsl-section-hint branch from c93e90d to 5b7fa20 Compare July 31, 2026 01:05
@Zhendong404
Zhendong404 force-pushed the feature-dsl-section-hint branch from 8fe48c3 to bb5966f Compare August 1, 2026 15:24
Comment thread lib/PTO/Transforms/PTOPlanMemory.cpp Outdated
for (func::FuncOp funcOp : childModule.getOps<func::FuncOp>())
funcs.push_back(funcOp);
});
moduleOp.walk([&](func::FuncOp funcOp) { funcs.push_back(funcOp); });

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 same fix was not applied to PTOPlanMemoryModern.cpp.

--plan-memory-impl defaults to legacy (tools/ptoas/ptoas.cpp:416), so this pass is the default path and the fix lands where it matters. But lib/PTO/Transforms/PTOPlanMemoryModern.cpp:1653-1666 still carries exactly the block that is deleted here:

moduleOp.walk([&](ModuleOp childModule) {
  if (childModule == moduleOp)
    return;
  bool hasTileOpHelper = llvm::any_of(
      childModule.getOps<func::FuncOp>(), [](func::FuncOp funcOp) {
        return funcOp->hasAttr("pto.tileop.helper");
      });
  if (!hasTileOpHelper)
    return;                 // <-- the new child shape is skipped here
  for (func::FuncOp funcOp : childModule.getOps<func::FuncOp>())
    funcs.push_back(funcOp);
});

With _DEFAULT_KERNEL_KIND now None, a pto.backend child module commonly carries neither pto.kernel_kind nor any pto.tileop.helper function. Whenever the driver does not promote it to a top-level compile unit (isUserVisibleIROutputRequested() modes, or direct pto-test-opt invocation), --plan-memory-impl=modern hits the if (!hasTileOpHelper) return; early exit and the child's functions are never planned - silently, with no diagnostic. That is the same failure mode this hunk fixes for the legacy planner.

The new regression test test/lit/pto/nested_resolve_reserved_buffers.pto runs --pto-plan-memory, i.e. the legacy planner only, so the modern planner has no coverage for the nested shape either.

Is the modern planner intentionally kept on the old behaviour? If so a short comment there would help. Otherwise the same moduleOp.walk(...) change plus an extra --plan-memory-impl=modern RUN line on the new lit test would close the gap.

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.

4 participants