<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 20 December 2016 at 17:47, Robinson, Paul <span dir="ltr"><<a href="mailto:paul.robinson@sony.com" target="_blank">paul.robinson@sony.com</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">
<div lang="EN-US">
<div class="gmail-m_-3115875035001910413WordSection1">
<p class="MsoNormal"><span style="font-size:11pt;font-family:calibri,sans-serif;color:rgb(31,73,125)">OK, thanks. "My program" is actually test/CodeGenCXX/global-init.<wbr>cpp which is expecting not to see the memset.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:calibri,sans-serif;color:rgb(31,73,125)">Given the '=default-in-C++03' bug, I'll make it work on both 03 and 11, and leave a FIXME about it.</span></p></div></div></blockquote><div><br></div><div>As of r290229, we now produce the C++11 result in C++03 too, and I've fixed that test for you :)</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"><div lang="EN-US"><div class="gmail-m_-3115875035001910413WordSection1"><p class="MsoNormal"><span style="font-size:11pt;font-family:calibri,sans-serif;color:rgb(31,73,125)"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:calibri,sans-serif;color:rgb(31,73,125)">Thanks,<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:calibri,sans-serif;color:rgb(31,73,125)">--paulr<u></u><u></u></span></p>
<p class="MsoNormal"><a name="m_-3115875035001910413__MailEndCompose"><span style="font-size:11pt;font-family:calibri,sans-serif;color:rgb(31,73,125)"><u></u> <u></u></span></a></p>
<div style="border-top:none;border-right:none;border-bottom:none;border-left:1.5pt solid blue;padding:0in 0in 0in 4pt">
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(181,196,223);padding:3pt 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10pt;font-family:tahoma,sans-serif">From:</span></b><span style="font-size:10pt;font-family:tahoma,sans-serif"> <a href="mailto:metafoo@gmail.com" target="_blank">metafoo@gmail.com</a> [mailto:<a href="mailto:metafoo@gmail.com" target="_blank">metafoo@gmail.com</a>]
<b>On Behalf Of </b>Richard Smith<br>
<b>Sent:</b> Tuesday, December 20, 2016 5:07 PM<br>
<b>To:</b> Robinson, Paul<br>
<b>Cc:</b> <a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<b>Subject:</b> Re: [cfe-dev] C++11 doing double zero initialization?<u></u><u></u></span></p>
</div>
</div><div><div class="gmail-h5">
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<div>
<p class="MsoNormal">On 20 December 2016 at 16:39, Robinson, Paul via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<u></u><u></u></p>
<p class="MsoNormal">Consider the following source:<br>
<br>
struct C { C() = default; C(const C&); int n; };<br>
const C c1 = C();<br>
<br>
then run it through `clang -emit-llvm -std=c++11`,<br>
and I see the following relevant bits:<br>
<br>
%struct.C = type { i32 }<br>
@_ZL2c1 = internal global %struct.C zeroinitializer, align 4<br>
define internal void @__cxx_global_var_init() {{.*}} {<br>
entry:<br>
call void @llvm.memset.p0i8.i64(i8* bitcast (%struct.C* @_ZL2c1 to i8*),<br>
i8 0, i64 4, i32 4, i1 false)<br>
ret void<br>
}<br>
<br>
<br>
The question is: Why the memset? Doesn't 'zeroinitializer'<br>
do the exact same thing?<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Initialization of C++ globals is performed in two stages:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal"> * in the first stage ("static initialization"), <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> -- if the object has a constant initializer (which C() is not, see <a href="http://wiki.edg.com/pub/Wg21issaquah2016/CoreWorkingGroup/cwg_active.html#1452" target="_blank">http://wiki.edg.com/pub/<wbr>Wg21issaquah2016/<wbr>CoreWorkingGroup/cwg_active.<wbr>html#1452</a>),
then object is initialized to that constant value ("constant intiialization")<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> -- otherwise is initialized to zero ("zero-initialization").<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> * in the second stage ("dynamic initialization", which is skipped if the first stage used constant initialization or if no initializer is provided for a scalar type), the initializer is run.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">What you're seeing here is a direct application of these rules to your program.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">C++ allows dynamic initialization to be converted to constant initialization if the initialization doesn't have side-effects (more or less), and Clang uses that permission to remove some dynamic initializers, but we don't happen to do so
in this case (in either C++03 or C++11). That's a QoI bug (and core issue 1452 may make it a correctness bug). Either way, we should fix it.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0in 0in 0in 6pt;margin-left:4.8pt;margin-right:0in">
<p class="MsoNormal">I don't see the memset with C++03.<u></u><u></u></p>
</blockquote>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">The input is not valid C++03 code, due to the "= default;", and our allowance of that construct in C++03 as an extension does something a bit strange: the C++11 initialization rules say that -- because C::C() is not user-provided -- we
first zero-initialize the entire entire object and then call the no-op constructor, which is why you get a memset. However, the C++03 initialization rules are slightly different, and don't require C() to zero-initialize in this case prior to running the constructor
(you only get the implicit zero-initialization for C() in C++03 if the class doesn't declare any constructors at all).<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">I think our =default-in-C++03 extension is actually broken here, and we should emit the memset in both languages. Given:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal"> struct A { A() = default; int n; };<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> int f() { A a = A(); return a.n; }<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">... this should be guaranteed to return 0 even in C++03 mode. (Right now it compiles to 'ret undef' in C++03.)<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div></div></div>
</div>
</div>
</blockquote></div><br></div></div>