<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Nov 10, 2016 at 6:12 PM, Mehdi Amini via cfe-commits <span dir="ltr"><<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Thinking again, the method “can” be implemented I think. Forget it…<br></blockquote><div><br></div><div>It can be, but the context is a virtual dispatch, so an overrider won't actually be used, and you do get undefined behavior.</div><div><br></div><div>Emitting a direct call to __cxa_pure_virtual (or the MS ABI equivalent) would likely be a more "polite" option than going straight to @llvm.unreachable (after all, that's what would be in the vtable slot)... but see <span style="font-size:12.8px">PR30937: doing so would cause a Clang-compiled Firefox to stop working. It sounds like they're fixing the problem, though, so maybe we can do this at some point. If we go that way (and probably even if we don't), we should teach Sema to detect this case and issue a warning.</span></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
—<br>
Mehdi<br>
<div class="gmail-HOEnZb"><div class="gmail-h5"><br>
> On Nov 10, 2016, at 6:11 PM, Mehdi Amini <<a href="mailto:mehdi.amini@apple.com">mehdi.amini@apple.com</a>> wrote:<br>
><br>
> Could we emit llvm.unreachable in this case?<br>
><br>
> —<br>
> Mehdi<br>
><br>
>> On Nov 10, 2016, at 5:01 PM, Richard Smith via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a>> wrote:<br>
>><br>
>> Author: rsmith<br>
>> Date: Thu Nov 10 19:01:31 2016<br>
>> New Revision: 286534<br>
>><br>
>> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=286534&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project?rev=286534&view=rev</a><br>
>> Log:<br>
>> PR30937: don't devirtualize if we find that the callee is a pure virtual<br>
>> function. In that case, there is no requirement that the callee is actually<br>
>> defined, and the code may in fact be valid and have defined behavior if the<br>
>> virtual call is unreachable.<br>
>><br>
>> Modified:<br>
>>   cfe/trunk/lib/CodeGen/CGClass.<wbr>cpp<br>
>>   cfe/trunk/test/CodeGenCXX/<wbr>devirtualize-virtual-function-<wbr>calls.cpp<br>
>><br>
>> Modified: cfe/trunk/lib/CodeGen/CGClass.<wbr>cpp<br>
>> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGClass.cpp?rev=286534&r1=286533&r2=286534&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/lib/CodeGen/<wbr>CGClass.cpp?rev=286534&r1=<wbr>286533&r2=286534&view=diff</a><br>
>> ==============================<wbr>==============================<wbr>==================<br>
>> --- cfe/trunk/lib/CodeGen/CGClass.<wbr>cpp (original)<br>
>> +++ cfe/trunk/lib/CodeGen/CGClass.<wbr>cpp Thu Nov 10 19:01:31 2016<br>
>> @@ -2851,9 +2851,9 @@ CodeGenFunction::<wbr>CanDevirtualizeMemberFu<br>
>>    return false;<br>
>><br>
>>  // If the member function is marked 'final', we know that it can't be<br>
>> -  // overridden and can therefore devirtualize it.<br>
>> +  // overridden and can therefore devirtualize it unless it's pure virtual.<br>
>>  if (MD->hasAttr<FinalAttr>())<br>
>> -    return true;<br>
>> +    return !MD->isPure();<br>
>><br>
>>  // If the base expression (after skipping derived-to-base conversions) is a<br>
>>  // class prvalue, then we can devirtualize.<br>
>> @@ -2861,31 +2861,28 @@ CodeGenFunction::<wbr>CanDevirtualizeMemberFu<br>
>>  if (Base->isRValue() && Base->getType()->isRecordType(<wbr>))<br>
>>    return true;<br>
>><br>
>> -  // If the most derived class is marked final, we know that no subclass can<br>
>> -  // override this member function and so we can devirtualize it. For example:<br>
>> -  //<br>
>> -  // struct A { virtual void f(); }<br>
>> -  // struct B final : A { };<br>
>> -  //<br>
>> -  // void f(B *b) {<br>
>> -  //   b->f();<br>
>> -  // }<br>
>> -  //<br>
>> -  if (const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType(<wbr>)) {<br>
>> -    if (BestDynamicDecl->hasAttr<<wbr>FinalAttr>())<br>
>> -      return true;<br>
>> -<br>
>> -    // There may be a method corresponding to MD in a derived class. If that<br>
>> -    // method is marked final, we can devirtualize it.<br>
>> -    const CXXMethodDecl *DevirtualizedMethod =<br>
>> -        MD-><wbr>getCorrespondingMethodInClass(<wbr>BestDynamicDecl);<br>
>> -    if (DevirtualizedMethod->hasAttr<<wbr>FinalAttr>())<br>
>> -      return true;<br>
>> -  }<br>
>> +  // If we don't even know what we would call, we can't devirtualize.<br>
>> +  const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType(<wbr>);<br>
>> +  if (!BestDynamicDecl)<br>
>> +    return false;<br>
>> +<br>
>> +  // There may be a method corresponding to MD in a derived class.<br>
>> +  const CXXMethodDecl *DevirtualizedMethod =<br>
>> +      MD-><wbr>getCorrespondingMethodInClass(<wbr>BestDynamicDecl);<br>
>> +<br>
>> +  // If that method is pure virtual, we can't devirtualize. If this code is<br>
>> +  // reached, the result would be UB, not a direct call to the derived class<br>
>> +  // function, and we can't assume the derived class function is defined.<br>
>> +  if (DevirtualizedMethod->isPure()<wbr>)<br>
>> +    return false;<br>
>> +<br>
>> +  // If that method is marked final, we can devirtualize it.<br>
>> +  if (DevirtualizedMethod->hasAttr<<wbr>FinalAttr>())<br>
>> +    return true;<br>
>><br>
>>  // Similarly, if the class itself is marked 'final' it can't be overridden<br>
>>  // and we can therefore devirtualize the member function call.<br>
>> -  if (MD->getParent()->hasAttr<<wbr>FinalAttr>())<br>
>> +  if (BestDynamicDecl->hasAttr<<wbr>FinalAttr>())<br>
>>    return true;<br>
>><br>
>>  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {<br>
>><br>
>> Modified: cfe/trunk/test/CodeGenCXX/<wbr>devirtualize-virtual-function-<wbr>calls.cpp<br>
>> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/devirtualize-virtual-function-calls.cpp?rev=286534&r1=286533&r2=286534&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/test/<wbr>CodeGenCXX/devirtualize-<wbr>virtual-function-calls.cpp?<wbr>rev=286534&r1=286533&r2=<wbr>286534&view=diff</a><br>
>> ==============================<wbr>==============================<wbr>==================<br>
>> --- cfe/trunk/test/CodeGenCXX/<wbr>devirtualize-virtual-function-<wbr>calls.cpp (original)<br>
>> +++ cfe/trunk/test/CodeGenCXX/<wbr>devirtualize-virtual-function-<wbr>calls.cpp Thu Nov 10 19:01:31 2016<br>
>> @@ -161,3 +161,37 @@ namespace test4 {<br>
>>    p->fish.eat();<br>
>>  }<br>
>> }<br>
>> +<br>
>> +// Do not devirtualize to pure virtual function calls.<br>
>> +namespace test5 {<br>
>> +  struct X {<br>
>> +    virtual void f() = 0;<br>
>> +  };<br>
>> +  struct Y {};<br>
>> +  // CHECK-LABEL: define {{.*}} @_ZN5test51f<br>
>> +  void f(Y &y, X Y::*p) {<br>
>> +    // CHECK-NOT: call {{.*}} @_ZN5test51X1fEv<br>
>> +    // CHECK: call void %<br>
>> +    (y.*p).f();<br>
>> +  };<br>
>> +<br>
>> +  struct Z final {<br>
>> +    virtual void f() = 0;<br>
>> +  };<br>
>> +  // CHECK-LABEL: define {{.*}} @_ZN5test51g<br>
>> +  void g(Z &z) {<br>
>> +    // CHECK-NOT: call {{.*}} @_ZN5test51Z1fEv<br>
>> +    // CHECK: call void %<br>
>> +    z.f();<br>
>> +  }<br>
>> +<br>
>> +  struct Q {<br>
>> +    virtual void f() final = 0;<br>
>> +  };<br>
>> +  // CHECK-LABEL: define {{.*}} @_ZN5test51h<br>
>> +  void h(Q &q) {<br>
>> +    // CHECK-NOT: call {{.*}} @_ZN5test51Q1fEv<br>
>> +    // CHECK: call void %<br>
>> +    q.f();<br>
>> +  }<br>
>> +}<br>
>><br>
>><br>
>> ______________________________<wbr>_________________<br>
>> cfe-commits mailing list<br>
>> <a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a><br>
>> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-commits</a><br>
><br>
<br>
______________________________<wbr>_________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-commits</a><br>
</div></div></blockquote></div><br></div></div>