[cfe-dev] C++11 and enhacned devirtualization

Nathan Wilson nwilson20 at gmail.com
Thu Jul 16 19:23:20 PDT 2015


On Thu, Jul 16, 2015 at 2:52 PM, Nico Weber <thakis at chromium.org> wrote:

> On Wed, Jul 15, 2015 at 10:11 PM, Hal Finkel <hfinkel at anl.gov> wrote:
>
>> Hi everyone,
>>
>> C++11 added features that allow for certain parts of the class hierarchy
>> to be closed, specifically the 'final' keyword and the semantics of
>> anonymous namespaces, and I think we take advantage of these to enhance our
>> ability to perform devirtualization. For example, given this situation:
>>
>> struct Base {
>>   virtual void foo() = 0;
>> };
>>
>> void external();
>> struct Final final : Base {
>>   void foo() {
>>     external();
>>   }
>> };
>>
>> void dispatch(Base *B) {
>>   B->foo();
>> }
>>
>> void opportunity(Final *F) {
>>   dispatch(F);
>> }
>>
>> When we optimize this code, we do the expected thing and inline
>> 'dispatch' into 'opportunity' but we don't devirtualize the call to foo().
>> The fact that we know what the vtable of F is at that callsite is not
>> exploited. To a lesser extent, we can do similar things for final virtual
>> methods, and derived classes in anonymous namespaces (because Clang could
>> determine whether or not a class (or method) there is effectively final).
>>
>
> Out of interest, is there data to suggest that this type of optimization
> will happen often in practice?
>

To follow up on Hal's response, one company I know of having about 20 C++
developers have been using C++11 for the past couple of years and have
upgraded the compiler being used in the past 10 months for optimization
purposes. The 'final' keyword was used a lot and requested to be used in
code reviews in anticipation of getting this optimization for free when
upgrading compiler versions. A bit of a small sample size, but some other
financial companies I know of like people who have knowledge of C++11/14
and optimizations like this that follow.


>
>
>>
>> One possibility might be to @llvm.assume to say something about what the
>> vtable ptr of F might be/contain should it be needed later when we emit the
>> initial IR for 'opportunity' (and then teach the optimizer to use that
>> information), but I'm not at all sure that's the best solution. Thoughts?
>>
>> Thanks again,
>> Hal
>>
>> --
>> Hal Finkel
>> Assistant Computational Scientist
>> Leadership Computing Facility
>> Argonne National Laboratory
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>>
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150716/ec5dcdc3/attachment.html>


More information about the cfe-dev mailing list