-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add vtable pointers to class layout #4407
Add vtable pointers to class layout #4407
Conversation
* Still needs to check the base class to see if there's already a vtable * Not sure what type to point to - void* is probably about right, it can't really be pointer-to-correctly-sized-array, because derived classes might make the array larger (with new virtual functions)
I think storing this flag is reasonable; it seems like a property of the class that we will want to query relatively frequently. One other question we will frequently need to answer is "where is the vptr?" -- eg, when forming a virtual call or initializing an instance of the class -- so perhaps it'd make sense to also store some information about that, when we get to the point of wanting to access the vptr.
I think we've said that we want the derived <-> base conversion to be a no-op, which would force the vptr to not be at the start of the object in this case. However, that would force a significant ABI difference from C++, which might be motivation to reconsider and accept that derived <-> base conversions might add an offset. @josh11b may have further thoughts. How hard would this be to revisit in future? |
Fair enough - yeah, can add more/change how we're doing this later. But sounds like you're on board with this as a first pass.
Oh, fair point... I'll see about making the change now to put the vptr after the base (I expect this'll be easy enough, and can flip back/forth in this review if folks have opinions).
Seems easy enough - don't have a stable ABI, so I think we can move it around as we see fit. Mostly I'm asking pretty short-sightedly about design questions, just whatever questions/answers you folks want answered now/set us in the direction you'd like to head. |
This comment was marked as resolved.
This comment was marked as resolved.
Co-authored-by: Richard Smith <[email protected]>
Co-authored-by: Richard Smith <[email protected]>
Yep, on the money. Test added. |
Co-authored-by: Richard Smith <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, looks great!
A small step to virtual functions - adding vtable pointers to the layout, but not initializing or otherwise using them at this stage.
A few open design questions I'd love feedback on:
is_dynamic
attribute toSemIR::Class
and populates/flags it based on the flag of the base class, or if any virtual function is declared in the class (or, at least that's my intent). Some other options include:Class
could store aClassId
(orTypeId
?) of the (possibly indirect, possibly self) base class that is the first one that is dynamic/has a vtable pointerhas vtable pointer
and have ittrue
only on the type that introduces the vtable - then derived classes would have to walk their base classes to check if they're the one that needs to define the vtable pointer or not{<non-dynamic base type>, vtable ptr, <derived members>}
? Derived types would still be able to uniquely identify where their vtable pointer is just fine... - and the vtable pointer is, in a sense, a member of that intermediate type, so it does seem a bit strange to force it to the front - but I guess it's probably more efficient in some ways?Open to any other suggestions/advice/thoughts on the direction, etc.