<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal">Hi,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I want to discuss the issue in PR27895,<o:p></o:p></p>
<p class="MsoNormal">       https://llvm.org/bugs/show_bug.cgi?id=27895<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">But I will give a slightly different example for this discussion.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The issue can be demonstrated by this standalone code. Should this example link<o:p></o:p></p>
<p class="MsoNormal">successfully?<o:p></o:p></p>
<p class="MsoNormal">----------------------------------------------<o:p></o:p></p>
<p class="MsoNormal">template < typename T, int N = 0 > class TmplWithArray {<o:p></o:p></p>
<p class="MsoNormal">public:<o:p></o:p></p>
<p class="MsoNormal">  virtual T& operator [] (int idx);<o:p></o:p></p>
<p class="MsoNormal">  T ar[N+1];<o:p></o:p></p>
<p class="MsoNormal">};<o:p></o:p></p>
<p class="MsoNormal">template <typename T, int N> T& TmplWithArray<T, N >::operator[](int idx) {<o:p></o:p></p>
<p class="MsoNormal">  return ar[idx];<o:p></o:p></p>
<p class="MsoNormal">}<o:p></o:p></p>
<p class="MsoNormal">class Wrapper {<o:p></o:p></p>
<p class="MsoNormal">  TmplWithArray<bool, 10> data;<o:p></o:p></p>
<p class="MsoNormal">  bool indexIt(int a);<o:p></o:p></p>
<p class="MsoNormal">};<o:p></o:p></p>
<p class="MsoNormal">bool Wrapper::indexIt(int a)<o:p></o:p></p>
<p class="MsoNormal">{<o:p></o:p></p>
<p class="MsoNormal">   return data[a];<o:p></o:p></p>
<p class="MsoNormal">}<o:p></o:p></p>
<p class="MsoNormal">int main(){}<o:p></o:p></p>
<p class="MsoNormal">----------------------------------------------<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Starting from  r227073 it does not link (at any optimization level). The code<o:p></o:p></p>
<p class="MsoNormal">has a direct call to the operator[] function, but that function definition is<o:p></o:p></p>
<p class="MsoNormal">not generated.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Versions prior to r227073,<o:p></o:p></p>
<p class="MsoNormal">    -  at –O0 or –O1, generate the operator[] function and the direct call, and<o:p></o:p></p>
<p class="MsoNormal">    -  at –O2 do not generate the function, but inline it<o:p></o:p></p>
<p class="MsoNormal">Either way they link fine.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">In this example the key-function for the generation of the vtable is the<o:p></o:p></p>
<p class="MsoNormal">operator[] function itself.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">So the compiler can either generate both the vtable and the operator[]<o:p></o:p></p>
<p class="MsoNormal">function, or not generate either; they are both consistent states.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The call in data[a] is to a virtual function, and if the compiler left it as a<o:p></o:p></p>
<p class="MsoNormal">virtual call, it will link. There will be no ctor, no vtable, and no operator[]<o:p></o:p></p>
<p class="MsoNormal">function. Wrapper::indexIt will be dead code, but it will link.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">But the compiler does devirtualization of the call and generates a direct call,<o:p></o:p></p>
<p class="MsoNormal">yet, beginning with r227073, it does not generate the operator[] function.<o:p></o:p></p>
<p class="MsoNormal">Hence the link failure.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Another interesting tidbit is that if the operator[] is replaced by a plain<o:p></o:p></p>
<p class="MsoNormal">function doing the same thing, along with the corresponding change in the
<o:p></o:p></p>
<p class="MsoNormal">usage, something like:<o:p></o:p></p>
<p class="MsoNormal">    virtual T& getElt(int idx);<o:p></o:p></p>
<p class="MsoNormal">then the behavior is the same as pre r227073. Either getElt is defined or it<o:p></o:p></p>
<p class="MsoNormal">gets inlined.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">BTW, this is not just an idle curiosity. This example was trimmed down from<o:p></o:p></p>
<p class="MsoNormal">a user bug report having a link failure.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Sunil Srivastava<o:p></o:p></p>
<p class="MsoNormal">Sony Interactive Entertainment<o:p></o:p></p>
</div>
</body>
</html>