Skip to content

feat(ptodsl): support stack-local structs - #1104

Merged
zhangstevenunity merged 3 commits into
hw-native-sys:mainfrom
jimmychou0:zjm/ptodsl-struct-surface
Aug 4, 2026
Merged

feat(ptodsl): support stack-local structs#1104
zhangstevenunity merged 3 commits into
hw-native-sys:mainfrom
jimmychou0:zjm/ptodsl-struct-surface

Conversation

@jimmychou0

Copy link
Copy Markdown
Contributor

Summary

  • Expose stack-local struct types and get/set operations in PTODSL.
  • Add the minimal StructType Python binding and reject unsupported ABI or loop-carry usage.
  • Document the API and add frontend, docs, and IR regression coverage.

Validation

  • ptodsl/tests/test_struct.py -v
  • ptodsl/tests/test_docs_as_test.py
  • ptodsl/tests/test_ptoas_frontend_verify.py
  • ptodsl/tests/test_subkernel_diagnostics.py
  • ptodsl/tests/test_jit_compile.py
  • llvm-lit --filter struct_ build-local-vpto/test/lit

@jimmychou0
jimmychou0 force-pushed the zjm/ptodsl-struct-surface branch 4 times, most recently from 86f2368 to 9ca170b Compare August 3, 2026 08:29
@jimmychou0
jimmychou0 force-pushed the zjm/ptodsl-struct-surface branch from d517e11 to 3ab1b65 Compare August 4, 2026 00:54
@jimmychou0
jimmychou0 marked this pull request as ready for review August 4, 2026 02:01
@jimmychou0
jimmychou0 force-pushed the zjm/ptodsl-struct-surface branch from 3ab1b65 to 2104a12 Compare August 4, 2026 02:18

static bool hasVPTOConvertibleType(Type type) {
return isa<pto::VRegType, pto::MaskType, pto::AlignType, pto::PtrType>(type);
return isa<pto::VRegType, pto::MaskType, pto::AlignType, pto::PtrType,

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.

  1. 签名转换处 struct 会被静默 lower 成 opaque ptr,而不是报错(最大风险)

两个 emitter 都把 pto::StructType 加进了 hasVPTOConvertibleType(VPTOLLVMEmitter.cpp:184、VPTOCANN900LLVMEmitter.cpp:408),这个谓词同时驱动 populateVPTOStructuralTypePatterns 对函数签名/调用边界的转换。PTODSL 前端虽然拒绝了 struct 作为 @pto.jit/subkernel 参数,但手写 .pto IR 或其他入口只要让 struct 出现在 func 签名上,就会被静默转成 opaque LLVM 指针参数——而调用侧永远不会创建对应的 alloca,得到的是一个语义悬空的指针,而不是一条编译错误。前端拒绝 + 后端静默放行,中间这层手写 IR 没有任何保护。建议在签名/边界转换路径上对 StructType 显式 emitError 拒绝,而不是依赖"前端已经拦了"。

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.

在ir 侧增加校验


```python
state_type = pto.struct_type(pto.i32, pto.f32)
nested_type = pto.struct_type(pto.i16, pto.struct_type(pto.i32, pto.f16))

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.

这个接口虽然很贴近IR形态,但感觉不太好用。我希望DSL能够直接提供一个类似class的抽象,用户可以像访问class成员一样访问struct成员,不用struct_set/struct_get。可以记个遗留后面优化吧

@jimmychou0 jimmychou0 Aug 4, 2026

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.

成员访问形式作为后续独立的前端易用性设计处理, 新增issue#1129

- `struct_get` 只能产生标量值,嵌套 aggregate 只能通过更长 path 访问。
- 第一阶段只支持函数内局部 state,不建立 PTODSL 的 struct 参数 ABI。
- `_StructDescriptor` 不可作为 runtime scalar annotation 或 loop-carried state。
- 嵌套层级无人为上限;path resolver 必须迭代实现且不缓存跨 context 的 MLIR type。

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.

C++侧的path resolver仍然是递归实现的

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.

已修正文档,field-path resolver 与 VPTO storage-type construction 均采用迭代遍历

// Struct values carry the address of stack-local storage. Keep the pointee
// type local to struct access lowering so the public type conversion remains
// an opaque LLVM pointer, consistent with other pointer-like PTO handles.
static LLVM::LLVMStructType getVPTOStructStorageType(pto::StructType structType,

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.

这里是递归的,和文档设计不一致,看下要不要改

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.

文档已作区分

if (failed(address))
return rewriter.notifyMatchFailure(op, "invalid struct field path");
rewriter.replaceOpWithNewOp<LLVM::StoreOp>(op, adaptor.getValue(),
*address);

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.

这里缺了getNaturalByteAlignment(resultType),和GetOp不一致?

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.

pto.struct_set lowering 已显式使用 natural byte alignment,与现有 load lowering 保持一致

@@ -0,0 +1,62 @@
// 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.

测试缺口

  • 无混合宽度/对齐布局的 lit 覆盖。GEP + 对齐最容易在 !pto.struct<i16, i32>、struct<i8, f32> 这类有 padding 的布局上出错,但 lit 测试只用了 struct<i32, struct<f32, i16>>(字段恰好都是 4 字节倍数起点)。建议加一个 struct<i16, i32> 或 struct<i8, i64> 的 case。
  • 无 i8/i64/bf16 字段覆盖,公开承诺的类型列表没有被逐一验证。
  • 无 ≥3 层嵌套测试,而这正是递归实现的风险点。
  • EmitC 端到端缺失:test_ptoas_frontend_verify.py 里的 probe 用 backend="emitc" compile,但只断言了 frontend verify 的文本,没有断言生成的 C++。设计文档说"EmitC 由既有 lit 覆盖",但那是手写 IR 路径,PTODSL → EmitC 全链路没有一处断言。
  • VPTO emitter 的 notifyMatchFailure("invalid struct field path") 分支无任何 negative 测试(正常应被 verifier 挡住,但失败诊断路径本身没验证)。

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.

以补充用例

@zhangstevenunity
zhangstevenunity merged commit d95f451 into hw-native-sys:main Aug 4, 2026
12 of 13 checks passed
@reedhecre

Copy link
Copy Markdown

A5 板测失败

  • 触发方式:merged
  • 源码提交:d95f451b9166
  • 结果汇总:OK 0 / FAIL 0 / SKIP 0
  • 日志:/root/ptoas-board-monitor-a5/logs/20260804_191307_merged_pr1104.log
  • 失败阶段:build-ptoas / exit=1

日志尾部

tmp/ptoas-board-monitor-a5/runs/20260804_191307_merged_pr1104/repo/build/include -isystem /home/ptoas-board-monitor-a5/llvm-project-vpto-llvm21/llvm/include -isystem /home/ptoas-board-monitor-a5/llvm-project-vpto-llvm21/build-shared/include -isystem /home/ptoas-board-monitor-a5/llvm-project-vpto-llvm21/mlir/include -isystem /home/ptoas-board-monitor-a5/llvm-project-vpto-llvm21/build-shared/tools/mlir/include -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -Wimplicit-fallthrough -Wno-nonnull -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wsuggest-override -Wno-comment -Wno-misleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -O3 -DNDEBUG -std=c++17   -D_GNU_SOURCE -DEXPERIMENTAL_KEY_INSTRUCTIONS -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS  -fno-exceptions -funwind-tables -fno-rtti -Werror -MD -MT lib/PTO/Transforms/CMakeFiles/obj.PTOTransforms.dir/TileFusion/PTOUnrollAfterLoopFusion.cpp.o -MF lib/PTO/Transforms/CMakeFiles/obj.PTOTransforms.dir/TileFusion/PTOUnrollAfterLoopFusion.cpp.o.d -o lib/PTO/Transforms/CMakeFiles/obj.PTOTransforms.dir/TileFusion/PTOUnrollAfterLoopFusion.cpp.o -c /tmp/ptoas-board-monitor-a5/runs/20260804_191307_merged_pr1104/repo/lib/PTO/Transforms/TileFusion/PTOUnrollAfterLoopFusion.cpp
c++: fatal error: Killed signal terminated program cc1plus
compilation terminated.
[93/442] Building CXX object lib/PTO/Transforms/CMakeFiles/obj.PTOTransforms.dir/TileFusion/PTOFlattenFusionRegion.cpp.o
ninja: build stopped: subcommand failed.
===== END STAGE build-ptoas rc=1 @ 2026-08-04 19:23:14 =====

@reedhecre

Copy link
Copy Markdown

A3 板测失败

  • 触发方式:merged
  • 源码提交:d95f451b9166
  • 结果汇总:OK 218 / FAIL 2 / SKIP 3
  • 日志:/home/zhongxuan/ptoas-board-monitor/runtime/logs/20260804_045608_merged_pr1104.log
  • 失败阶段:board-validation / exit=1

失败用例

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

@reedhecre

Copy link
Copy Markdown

A3 板测失败详情:PR #1104

comm_p2p_binding_variants

stage=run info=exit=1

[ERROR] aclrtSynchronizeStream(stream) failed: 507035 (/home/zhongxuan/ptoas-board-monitor/runtime/runs/20260804_045608_merged_pr1104/npu_validation/CommSync/comm_p2p_binding_variants/main.cpp:122)
[ERROR] RecentErrMsg: EZ9999: Inner Error!
EZ9999[PID: 4065879] 2026-08-04-05:28:51.756.096 (EZ9999):  The error from device(chipId:0, dieId:1), serial number is 2583, there is an exception of aivec error, core id is 24, error code = 0x10, dump info: pc start: 0x124200000000, current: 0x124200000164, vec error info: 0x4000000b7, mte error info: 0x7503000008, ifu error info: 0x212c0d6200800, ccu error info: 0x40e028000000004f, 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: 0x3000008, fixp_error1 info: 0x75, 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=3889294368825709812.[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-08-04 05:28:53] 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/20260804_045608_merged_pr1104/npu_validation/CommSync/comm_p2p/main.cpp:122)
[ERROR] RecentErrMsg: EZ9999: Inner Error!
EZ9999[PID: 4088204] 2026-08-04-05:29:12.548.318 (EZ9999):  The error from device(chipId:0, dieId:1), serial number is 2584, there is an exception of aivec error, core id is 29, error code = 0x10, dump info: pc start: 0x124200000000, current: 0x124200000168, vec error info: 0x4000000b7, mte error info: 0x3c0300404d, ifu error info: 0x212c0d6200800, 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: 0x300404d, fixp_error1 info: 0x3c, 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_kernel, fault kernel info ext=comm_p2p_kernel, program id=0, hash=15664819858989799263.[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-08-04 05:29:13] ERROR: testcase failed (exit 1): comm_p2p

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