Final necessary for inline optimization?

Johan Engelen j at j.nl
Sat Oct 19 17:11:08 UTC 2019


Just a brief answer.

On Saturday, 19 October 2019 at 15:58:08 UTC, IGotD- wrote:
> Which one is it, LDC recognizes TestClass isn't derived or is 
> sure that the class (c) isn't derived in particular?

It is the latter: the optimizer is able to prove that object c 
has a vtable that is known exactly. Then indexing into that 
vtable gives a definite function that can then be inlined.

Some more info:
The (suboptimal) trick that LDC employs is that after the call to 
`new`, LDC again sets the vtable of the object. It is 
superfluous, because it is already done by `new`, but `new` is 
opaque to the optimizer whereas the extra vtable write is not. So 
after the object creation, the optimizer knows exactly what 
vtable is used for that object. Now unfortunately, that only 
works for the first virtual call after `new`. Any opaque function 
that is called with `c` as parameter, e.g. calling a virtual 
function of that class will destroy knowledge about what vtable 
is stored in `c`. Per D language spec, the virtual function 
cannot overwrite the vtable pointer, but the optimizer does not 
know that so it assumes it might be overwritten and it no longers 
knows the contents of the vtable pointer.
You can see this happening here:
https://d.godbolt.org/z/8ERNhg

-Johan



More information about the Digitalmars-d-learn mailing list