[STUBGEN] Classify native layouts from reflected byte facts - #730
Open
Seven-Streams wants to merge 3 commits into
Open
[STUBGEN] Classify native layouts from reflected byte facts#730Seven-Streams wants to merge 3 commits into
Seven-Streams wants to merge 3 commits into
Conversation
Signed-off-by: yuchuan <yuchuan.7streams@gmail.com>
Seven-Streams
force-pushed
the
main-dev/2026-09-02/stubgen_layout_classify
branch
from
September 2, 2026 17:36
7ccc3f6 to
a35847b
Compare
Signed-off-by: yuchuan <yuchuan.7streams@gmail.com>
Seven-Streams
marked this pull request as ready for review
September 2, 2026 18:36
Signed-off-by: yuchuan <yuchuan.7streams@gmail.com>
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.
[STUBGEN] Classify native layouts from reflected byte facts
Summary
Add a language-neutral classifier that decides, for every registered object type, whether its native layout can be reproduced from reflection (
complete) or not (opaque), with the byte evidence behind each verdict.tvm-ffi-stubgen --coverage-out <json>writes the verdicts as a report. No generator is attached.Motivation
A native-struct backend can hold an object by value only when it can reproduce the C++ layout byte for byte. The registry already publishes what that needs: each type's own
sizeof(#720) and every field's size, alignment and offset. A wrongcompleteis a struct with the wrong layout, a wrongopaqueonly costs a C ABI call per field, so the classifier deserves a review of its own before any generator builds on it.Changes
stub/layout.py(new). One criterion: the reflected fields must fill[parent.total_size, total_size)exactly, allowing only the gaps alignment forces. Two prerequisites: the type has metadata of its own (layout-unknown) and its parent is complete (parent-opaque). Unexplained bytes areuncovered-bytes; a field starting before the cursor isfield-overlap. Polymorphism is not a rule: a vptr makes the fields fall short ofsizeofon its own. Two target-language rules are injection points:field_renderableandforced_opaque, the latter only demoting a type that would otherwise be complete.stub/cli.py,stub/utils.py:--coverage-out <json>, usable without PATH arguments. The report fixes the language-neutral keys; a generator adds its own under a per-type"target"key.testing.cc:TestCxxClassHiddenField(an unreflected member) andTestCxxClassPolymorphic(a vptr), so both failure paths run in CI.Testing
tests/python/test_stub_layout.py: every verdict path on synthetic byte facts, then the real registry. TheTestCxxClass*chain is complete with padding[36, 40),[52, 56),[73, 80);TestCxxClassHiddenFieldreports[32, 40)uncovered;TestCxxClassPolymorphicreportssizeof40 against fields ending at 32;ffi.Functionislayout-unknown;ffi.Moduleis opaque like the polymorphic fixture. The CLI writes the report with--coverage-outalone.