On Tue, 21 Apr 2026 14:16:16 GMT, Matthias Baesken <[email protected]> wrote:

>> IIUC, the vtable is removed because all virtual calls on that class all 
>> removed either because they are dead, or they can be devirtualized to a 
>> virtual call on a subtype. As a result, is there an annotation that tells 
>> the compiler that a virtual method should not be removed? It should preserve 
>> the vtable, right?
>> 
>>> Making the destructor virtual fails in the HS build because it seems not to 
>>> work with derived classes.
>> 
>> That sounds confusing to me, normally it should be the opposite, are we even 
>> sure the destructors are called properly?
>
>> As a result, is there an annotation that tells the compiler that a virtual 
>> method should not be removed?
> 
> There are some attributes like attribute 'retain' , e.g. here is some 
> discussion
> https://stackoverflow.com/questions/73887908/prevent-gcc-from-optimization-removal-of-variables-when-using-wl-gc-sections
> 
> But from what I saw it did not really help with linker-step based elimination.
> And how would I use it with the vtable, I have to write the attribute next to 
> some variables/methods etc. ?

@MBaesken I think I was probably incorrect, the real reason seems to be what 
@plummercj suggested instead.

Anyway, if what I said turns out to be actually true, I guess you can add 
`__attribute__((retain))` on a random virtual method.

If what @plummercj said is true, but you can't make it so that we don't need a 
vtable for `Metadata`, then I think you can de-abstract `Metadata` by 
implementing the pure virtual methods. If even then, lto notices that no 
instance of `Metadata` is actually created and does not create the vtable, then 
you can try adding `__attribute__((retain))` to `Metadata`, or actually 
creating a `Metadata` static instance.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/30771#issuecomment-4289326291

Reply via email to