feat(ptodsl): support struct member access (named fields) - #1182
Draft
jimmychou0 wants to merge 3 commits into
Draft
feat(ptodsl): support struct member access (named fields)#1182jimmychou0 wants to merge 3 commits into
jimmychou0 wants to merge 3 commits into
Conversation
Add a pto.struct({...}) named-field surface on top of the canonical
pto.struct_type/declare_struct/struct_get/struct_set API. Field names are
compile-time constants resolved to positional paths entirely at AST-rewrite
time, so state.field / state.field = v / state.inner.field lower losslessly
to the existing get/set ops with no runtime helper or descriptor->value
propagation.
- _types.py: _StructDescriptor gains field_names, field_index,
field_descriptor_at, from_named; add pto.struct(dict) constructor with
field-name validation (identifier, non-keyword, non-underscore, reserved).
- _ast_rewrite.py: new _StructMemberRewriter statically evaluates
pto.struct/pto.struct_type literals, tracks type/value bindings, and emits
pto.struct_get/set with baked integer paths. Handles rebinding, multi-target
assignment order, AugAssign, AnnAssign, del, positional-layer rejection, and
branch-merge conflicts. Gated by ast_rewrite.
- _ops.py: declare_struct returns StructValue (diagnostic-only member access
when source is unavailable).
- user guide + docs fixtures + test_struct.py coverage.
jimmychou0
force-pushed
the
zjm/ptodsl-struct-member-access
branch
from
August 7, 2026 07:53
35aa2f1 to
c9922fe
Compare
- AugAssign: use Store context for the rewritten temp target so
state.field += value compiles (was Load, which the AST compiler rejects).
- Local nested descriptors: _eval_field_type now resolves names from the
local _type_bindings before static_env, so a named descriptor defined in
the same function (Inner used inside Outer) is usable for nested member
access instead of being treated as a scalar.
- AnnAssign: reject dynamic/non-static member annotations instead of silently
discarding them; only pto.* dtypes and known static types are accepted.
- Duplicate literal keys: reject duplicate field names in pto.struct({...})
so the AST metadata positions stay aligned with the runtime dict.
- Add a named-member pto.struct({...}) + state.field probe to
test_ptoas_frontend_verify.py to cover the AST-rewritten named surface
through EmitC field access.
- Preserve assignment-form local descriptor aliases: Alias = Inner now
inherits the Inner type binding, so a locally-defined named descriptor can
be reused inside a nested pto.struct({...}).
- Validate annotated member assignment against the field's scalar type: the
statically-evaluated metadata now carries a scalar type key (_ScalarField)
and a member annotation such as pto.i32 is rejected when it does not match
the field type (e.g. a pto.f32 field annotated pto.i32).
- Add default (VPTO) named-member end-to-end coverage plus edge-case
regressions: VPTO probe exercising local descriptor alias + nested member
access + +=, and test_struct cases for +=, local alias/nesting, annotation
mismatch, and duplicate literal keys.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add a named-field struct surface to PTODSL on top of the canonical
pto.struct_type/pto.declare_struct/pto.struct_get/pto.struct_setAPI (introduced in #1104). Enables Python member syntax:
Nested member access works too:
s.pt.x = 1lowers topto.struct_set(s, [1, 0], 1).Design
Full design doc:
docs/designs/ptodsl-struct-member-access-design.md(threerounds of review).
Key decisions:
never enter the IR.
!pto.struct<...>stays positional.time. The rewriter statically evaluates
pto.struct({...})/pto.struct_type(...)literals and emitspto.struct_get/pto.struct_setwith baked integer paths. No runtime helper, no descriptor→value propagation.
_StructMemberRewriterhandles rebinding, multi-target assignment order,+=, annotated assignment,del, positional-layer rejection, andbranch-merge conflicts. Gated by
ast_rewrite.declare_structreturns aStructValuewhose__getattr__/__setattr__are diagnostic-only (clear error when source is unavailable /
ast_rewrite=False).Tests
test_struct.py: 14 tests pass (5 newNamedStructMemberAccessTest).test_docs_as_test.py: 192 blocks pass.test_ptoas_frontend_verify.py: PASS.test_ast_rewrite_example_ir.py: PASS.Validated on 144 in a fresh checkout:
/home/zhoujiaming/ptoas-sim-ci/scratch/struct-member-144-08071421/ptodsl-struct.Related
Closes #1129