4 ms·
This seems sort of pointless -- you could just compile versions of the function for all the types that might be there -- except maybe the code is to be speciali
by ncmncm 4y ago
This seems sort of pointless -- you could just compile versions of the function for all the types that might be there -- except maybe the code is to be specialized on a type that came from a shared library that was not visible to the linker at the time the program was built.
- mgraczyk 4y agoYou can't do that in general. For example you can't statically devirtualize a call to a virtual function on a non-final class because it could be inherited from in a different dynamically loaded library. This pattern is pretty common with C++ SDKs.
- ncmncm 4y agoI guess virtual functions are common in some places. This thing seems to generate code to check if the type matches what was inlined, and do the ordinary virtual call if it doesn't match. Virtual-call branches are well predicted nowadays, but the opportunity to integrate the code before and after the call might be worth something, probably by deleting code that computes something thrown away in that case.
- mgraczyk 4y agoEven if virtual call branches are well predicted, they still have high overhead. Yeah GCC and clang will do what you described in some cases, they will insert a check and try to make a non-virtual call when possible. But also as you mentioned, they real advantage is from the downstream optimizations that unlocks, which they cannot do.
- titzer 4y agoYou can do a pretty good job if you have the whole program. That's the point of class hierarch analysis (CHA) or rapid type analysis (RTA), which unfortunately doesn't seem to get used all that much these days. Virgil is an unapologetic whole-program compiler and goes even further with specializing to an initialized heap. JVMs typically use dynamic class hierarchy analysis that doesn't require runtime profiling, but can require deoptimization on class loading.
- gpderetta 4y agoYou can, but you have to guard your inlined path with a type check and fallback to the non virtual path (JITs do similar things, except that the JIT of course can dynamically decide which function to inline, while at best AOT can use PGO).
- mgraczyk 4y agoYes GCC and Clang already do this in many cases, but it's not quite the same as what the posted compiler can do.