Skip to content

[FEAT] Introduce ABI-friendly structural visitor and walk traversal API#601

Draft
Kathryn-cat wants to merge 1 commit into
apache:mainfrom
Kathryn-cat:kathryncat/visitor
Draft

[FEAT] Introduce ABI-friendly structural visitor and walk traversal API#601
Kathryn-cat wants to merge 1 commit into
apache:mainfrom
Kathryn-cat:kathryncat/visitor

Conversation

@Kathryn-cat
Copy link
Copy Markdown
Contributor

@Kathryn-cat Kathryn-cat commented May 29, 2026

No description provided.

Copy link
Copy Markdown
Contributor

@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 introduces a structural visit and walk implementation for the TVM FFI, enabling traversal of object graphs with support for early interruption. It adds the StructuralVisitor and StructuralWalk APIs, implements built-in container visitors, and provides unsafe raw-storage helpers for Expected. The review feedback recommends adding a defensive check for undefined ObjectRef values in DefaultVisitExpected to prevent potential crashes, supporting void return types in StructuralWalk callbacks to reduce boilerplate, and adding a static assertion to verify that each callback takes exactly one argument to avoid obscure template compilation errors.

Comment thread src/ffi/extra/structural_visit.cc Outdated
Comment thread include/tvm/ffi/extra/structural_visit.h Outdated
Comment thread include/tvm/ffi/extra/structural_visit.h Outdated
@Kathryn-cat Kathryn-cat force-pushed the kathryncat/visitor branch from 6618b29 to 802b33e Compare May 29, 2026 20:49
Comment thread include/tvm/ffi/extra/structural_visit.h
Comment thread include/tvm/ffi/extra/structural_visit.h Outdated
Comment thread include/tvm/ffi/extra/structural_visit.h Outdated
Comment thread include/tvm/ffi/extra/structural_visit.h Outdated
Comment thread include/tvm/ffi/extra/structural_visit.h Outdated
Comment thread include/tvm/ffi/extra/structural_visit.h Outdated
Comment thread include/tvm/ffi/extra/structural_visit.h Outdated
Comment thread include/tvm/ffi/expected.h Outdated
Comment thread include/tvm/ffi/expected.h Outdated
Comment thread include/tvm/ffi/expected.h Outdated
Comment thread src/ffi/extra/structural_visit.cc Outdated
@Kathryn-cat Kathryn-cat force-pushed the kathryncat/visitor branch 6 times, most recently from d1858f6 to e7da414 Compare May 30, 2026 07:16
Comment thread include/tvm/ffi/extra/structural_visit.h Outdated
Comment thread include/tvm/ffi/extra/structural_visit.h Outdated
Comment thread include/tvm/ffi/extra/structural_visit.h Outdated
Comment thread include/tvm/ffi/extra/structural_visit.h Outdated
Comment thread include/tvm/ffi/extra/structural_visit.h Outdated
// ---------------------------------------------------------------------------

class TLeafObj : public Object {
public:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

consider consolidate sme of the objects to testing_object.h

std::vector<ObjectRef> visited;
std::vector<TVMFFIDefRegionKind> modes;
ObjectRef interrupt_on;
String interrupt_payload = "stop";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is likely not need atm, focus ontesting StructuralWalk

class TIgnoredObj : public Object {
public:
ObjectRef visible;
ObjectRef ignored;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this can be skiped for now, to test ignored, have something lke binding with a comment field that is ignored, no need to have dedicated class test

ObjectRef non_recursive;
ObjectRef plain_after_non_recursive;

TDefRegionObj(ObjectRef recursive, ObjectRef plain_after_recursive, ObjectRef non_recursive,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is not needed since the node is quite artifical, good to have some reasonably common nodes, TPair, with function def(params, body) plus comment likely suffice to cover cases of interest, cross check testing_object.h

}

TEST(StructuralVisitor, DefaultVisitTraversesList) {
ObjectRef lhs = TLeaf("lhs");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

always use StructuralWalk to test, test a few cases of composed containers, no need to have one for each

Comment thread include/tvm/ffi/extra/structural_visit.h Outdated
}
// Decode by moving from owned Any storage after a strict type check.
TVM_FFI_INLINE static WalkResult MoveFromAnyAfterCheck(TVMFFIAny* src) {
return WalkResult(Base::MoveFromAnyAfterCheck(src));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

// Auto-convert None -> Advance
if (src->type_index == TypeIndex::kTVMFFINone) {
return WalkResult::Advance();
}

implement auto convert None to Advance, so ffi function can return None (or no return which translate to none) and implies advance

do it for the few places that involves conversion

Comment thread src/ffi/extra/structural_visit.cc Outdated
@Kathryn-cat Kathryn-cat force-pushed the kathryncat/visitor branch from e7da414 to 014afc9 Compare May 30, 2026 22:05
@Kathryn-cat Kathryn-cat force-pushed the kathryncat/visitor branch from 1448f2a to 9838ea8 Compare May 30, 2026 23:17
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.

2 participants