<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Thu, 23 Aug 2018 at 12:42, Louis Dionne via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div><blockquote type="cite"><div>On Aug 23, 2018, at 00:36, John McCall <<a href="mailto:rjmccall@apple.com" target="_blank">rjmccall@apple.com</a>> wrote:</div><div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><blockquote type="cite"><div>On Aug 22, 2018, at 8:41 PM, Richard Smith via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:</div><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><div class="gmail_quote"><div dir="ltr">On Wed, 22 Aug 2018 at 08:39, Louis Dionne via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><br><div><br><blockquote type="cite"><div>On Aug 21, 2018, at 18:21, Richard Smith <<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>> wrote:</div><br class="m_399335037117398794gmail-m_8602693362901822949Apple-interchange-newline"><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><div class="gmail_quote"><div dir="ltr">On Tue, 21 Aug 2018 at 14:44, Louis Dionne via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">Hi,<br><br>This message is related to solving the issue explained at [1], and indirectly, the usage of __always_inline__ in libc++ as explained at [2].<br><br>I've been looking to add an attribute (tentatively called no_extern_template) which would prevent member functions in a template from getting available_externally linkage even when there is a matching extern template declaration. In other words, given the following code:<br><br>   <span class="m_399335037117398794gmail-m_8602693362901822949Apple-converted-space"> </span>template <class T><br>   <span class="m_399335037117398794gmail-m_8602693362901822949Apple-converted-space"> </span>struct Foo { void func() { } };<br>   <span class="m_399335037117398794gmail-m_8602693362901822949Apple-converted-space"> </span>extern template struct Foo<int>;<br><br>   <span class="m_399335037117398794gmail-m_8602693362901822949Apple-converted-space"> </span>void use() {<br>     <span class="m_399335037117398794gmail-m_8602693362901822949Apple-converted-space"> </span>Foo<int> f;<br>     <span class="m_399335037117398794gmail-m_8602693362901822949Apple-converted-space"> </span>f.func();<br>   <span class="m_399335037117398794gmail-m_8602693362901822949Apple-converted-space"> </span>}<br><br>it is possible for Clang to emit a call to the extern function `Foo<int>::func()`, because the extern template declaration promises that it exists somewhere else in the program. Clang could also decide to inline the function and generate no extern call, but it does not _have_ to. What I want is an attribute that inhibits generating an external call, i.e. that makes sure `Foo<int>::func()` is either emitted in this TU despite the extern template declaration, or inlined (in which case a definition is obviously not needed). If the function is emitted, it should be emitted with proper linkage, in this case linkonce_odr (IIUC). The attribute would be applied to the member function that should be opted-out of the extern template declaration, like so:<br><br>   <span class="m_399335037117398794gmail-m_8602693362901822949Apple-converted-space"> </span>template <class T><br>   <span class="m_399335037117398794gmail-m_8602693362901822949Apple-converted-space"> </span>struct Foo { __attribute__((no_extern_template)) void func() { } };<br>   <span class="m_399335037117398794gmail-m_8602693362901822949Apple-converted-space"> </span>extern template struct Foo<int>;<br><br>I'd like to have some feedback on this idea, specifically:<br></blockquote><div><br></div><div>I think I'm following what you're asking for: the idea is that an explicit instantiation declaration for the class would not be treated as an explicit instantiation declaration for the members of the class that have the attribute, right? So the desired behavior for:</div><div><br></div><div><div>template <class T> struct Foo { // non-polymorphic class</div><div> <span class="m_399335037117398794Apple-converted-space"> </span>void foo() { }</div><div> <span class="m_399335037117398794Apple-converted-space"> </span>__attribute__((no_extern_template)) void bar() { }<br></div><div> <span class="m_399335037117398794Apple-converted-space"> </span>void baz() { }<br></div><div>};</div><div>extern template struct Foo<int>;</div></div><div><br></div><div>would be exactly the same as the standard behavior of:</div><div><br></div><div><div><div>template <class T> struct Foo {</div><div> <span class="m_399335037117398794Apple-converted-space"> </span>void foo() { }</div><div> <span class="m_399335037117398794Apple-converted-space"> </span>void bar() { }<br></div><div> <span class="m_399335037117398794Apple-converted-space"> </span>void baz() { }<br></div><div>};</div><div>extern void Foo<int>::foo();</div></div></div><div>extern void Foo<int>::baz();<br></div></div></div></div></blockquote><div><br></div><div>Yes, this is what I’m asking for.</div><div><br></div><br><blockquote type="cite"><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><div class="gmail_quote"><div>except that this is an opt-out mechanism whereas the standard mechanism is opt-in. (For polymorphic class types, there's more differences, as explicit instantiations can also affect where vtables are emitted.)</div><div><br></div><div>Given that (I'm guessing) the idea is to more precisely control which functions are part of the libc++ DSO's ABI, have you considered explicitly listing those functions (that is, following the opt-in strategy) rather than adding an opt-out mechanism? That seems to give more direct control over the ABI surface, albeit at the cost of presumably making the header larger. Is there a reason that approach won’t work or is undesirable?</div></div></div></div></blockquote><div><br></div><div>We’ve talked about it in the thread starting here: <a href="http://lists.llvm.org/pipermail/cfe-dev/2018-July/058519.html" target="_blank">http://lists.llvm.org/pipermail/cfe-dev/2018-July/058519.html</a>. I think this is a superior solution because it would create a very clear list of what’s in the ABI. It might be tedious to create (lots of boilerplate declarations), but that’s not a huge concern IMO.</div><div><br></div><div>However, the reason why this is not feasible right now is that we lack a way to declare an extern instantiation of a vtable and RTTI, and to explicitly instantiate those. I guess we could either add an extension that allows that and/or try standardizing a feature that allows that. I think standardizing a way of doing this would be useful nonetheless — it would for example remove the need for anchor functions to pick which TU the vtable is emitted in, which is kind of a hack.</div></div></div></blockquote><div><br></div><div>GCC for many years had an extension to do this:<span class="m_399335037117398794Apple-converted-space"> </span><a href="https://gcc.gnu.org/onlinedocs/gcc/Template-Instantiation.html" target="_blank">https://gcc.gnu.org/onlinedocs/gcc/Template-Instantiation.html</a><span class="m_399335037117398794Apple-converted-space"> </span>(see the bottom of the page).</div><div><br></div><div>It's long been on our list of "potentially useful GCC extensions we never got around to implementing". I'm not sure that's quite enough to be really useful, though; while that lets you say "instantiate the vtable here", there doesn't seem to be a way to say "do not instantiate the vtable here; it was instantiated elsewhere".</div></div></div></div></blockquote><div><br></div>If we can just quietly let that extension die, I think the world would be a better place.  The basic idea isn't bad, but it obviously only uses `static` and `inline` because there's already parser support for them.</div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><br></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">If we're thinking of adding a vendor-specific attribute, why don't we just add an attribute to mark an explicit instantiation def/decl as only instantiating the class data?  Or even just propose that as a feature to the committee?  For the latter, I think the obvious spelling would be (with or without parens):</div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><br></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">  'extern'? 'typeid' '(' type-id ')'</div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><br></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">Separating this from explicit instantiations would also let it apply to arbitrary other class types, not just templates, which would be nice for micro-optimizing code that uses RTTI but can't reasonably use key functions (as well as ABIs that don't provide key-function-like optimizations).  If RTTI is disabled, we'd just emit the v-table, and we'd complain if the class wasn't polymorphic.  I guess emitting v-tables in addition to RTTI would prevent someone from using this *just* to define the RTTI and not the v-table, but... I can't imagine why someone would want to do that.</div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><br></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">John.</div></div></blockquote><div><br></div><div>I’ve given more thought to Richard’s suggestion. I’m fully on-board with having a way to externally declare and explicitly instantiate typeids and vtables for arbitrary classes (templates or not). I think that’s a great idea that fills a need and I’m willing to pursue a standard proposal that does that.</div><div><br></div><div>However, I’m concerned about making libc++’s ability to drop __always_inline__ contingent on such a feature or compiler extension for the following reasons:</div><div>- We have to make sure libc++ still works with GCC. If we wanted to use this hypothetical compiler extension in libc++, we’d have to keep the same annotations as today, but also add the explicit declarations of what’s in the ABI. This increases the complexity of libc++’s visibility system significantly, and that complexity is already too high.</div><div>- libc++ has been opt-out since the beginning. It would arguably be nicer if we had what Richard suggests, and we may get there someday, but it’s just not the way things have been designed in libc++ and doing otherwise is a huge change. There’s no telling what subtle problems we may find or even create down that road, and I’d like to avoid such a big unknown.</div><div><br></div><div>So, in a perfect world, I’d have a standardized way of explicitly declaring what is part of my ABI, and I wouldn’t have any of today’s libc++ craziness. However, I still think `no_extern_template` is the right answer for the time being, given it fits exactly in the place where `__always_inline__` is used today in libc++.</div></div></div></blockquote><div><br></div><div>I don't necessarily want to redirect you from this course of action; pragmatically-speaking, the attribute you're proposing seems very reasonable to me, and is probably the right short-to-medium-term solution. However:</div><div><br></div><div>It seems to me there are two different compilations that we need to care about:</div><div><br></div><div> 1) the compilation of the DSO itself</div><div> 2) the compilation of a user of the DSO</div><div><br></div><div>For (1), we would need to use a compiler that can force-emit the vtable, ideally without emitting all inline functions in the class. (If it emits too much, we do have ways to prevent the extra symbols being part of the DSO's ABI, but it's not ideal.) Any version of GCC can do that (via "inline template"), and if we add a standard or vendor-specific feature to clang to emit the type info, then recent Clang can do that too. Older Clang and other compilers will be left emitting too many symbols, which is likely tolerable, because the result should still be correct and building a new libc++ DSO with an old Clang is likely something we can discourage.</div><div><br></div><div>For (2), it's my understanding that we do not /need/ to prevent users of libc++ from emitting their own copies of symbols that are also in the DSO. If we don't have a way of saying "the vtable is emitted in the DSO" for a particular compiler, the cost is that we may emit an unnecessary but mostly harmless copy of the vtable in the client program, not that we will miscompile or create an ABI problem.</div><div><br></div><div>So I think it might still be viable to switch libc++ entirely to per-function explicit instantiations, even before we have ubiquitous compiler support to deal with the result. But it's probably worth delaying that until such compiler features are at least broadly available, since there will be some costs (notably, code size) to compilers for which we cannot declare that class type info is available externally.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div><div>Unless there are serious concerns with the attribute, I’d like to solicit feedback on the following points:</div><div><br></div><div><div>- What entities does it make sense to apply this attribute to? Currently, I'm thinking only static and non-static member functions of templates, for lack of imagination and other use cases. Does it make sense to apply it to static data members? To the vtable and RTTI somehow?</div></div></div></div></blockquote><div><br></div><div>I would expect the attribute to be applicable to all members of templated classes that would be explicitly instantiated by an explicit instantiation of the enclosing class ([temp.explicit]p10),. That would be member functions (static or non-static), member classes (to suppress recursion into their members), and static data members.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div><div><div>- What other attributes would be mutually exclusive with this one? Right now I can only think of `internal_linkage`, but is there anything else?</div></div></div></div></blockquote><div><br></div><div>I don't think internal linkage needs to be mutually-exclusive with this attribute. I would expect that an internal_linkage + no_extern_template function would simply be unaffected by 'extern template', so would still be implicitly instantiated if referenced in a context where a definition is needed.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div><div>Thanks,</div><div>Louis</div><br><blockquote type="cite"><div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"> <br><blockquote type="cite"><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div><div>Also, it’s not a huge reason not to do it, but that would constitute a very large change in libc++, which does everything opt-out right now. Comparatively, everything is already in place for libc++ to start using an hypothetical no_extern_template attribute, and in fact the patch that does that is like 4 lines. Of course, my search for the right solution is not primarily motivated by that.</div><div><br></div><div>I think I will start by exploring Reid’s idea of checking for incompatible visibilities using a visitor, and if that doesn’t take me anywhere, I’ll reconsider your suggestion. Independently, I’ll draft a C++ proposal to control where RTTI/vtables are instantiated and see where that idea goes.</div><div><br></div><div>Louis</div><div><br></div><br><blockquote type="cite"><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><div class="gmail_quote"><div><br class="m_399335037117398794gmail-m_8602693362901822949gmail-Apple-interchange-newline"></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">- What entities does it make sense to apply this attribute to? Currently, I'm thinking only static and non-static member functions of templates, for lack of imagination and other use cases. Does it make sense to apply it to static data members? To the vtable and RTTI somehow?<br><br>- What other attributes would be mutually exclusive with this one? Right now I can only think of `internal_linkage`, but is there anything else?<br><br>Finally, I'd also welcome any guidance on how to best achieve this in Clang. So far, I've achieved something that "works" by forcing declarations with `GVA_AvailableExternally` linkage to have `GVA_DiscardableODR` linkage (in `adjustGVALinkageForAttributes`). I think this is probably the wrong approach but I’m a Clang beginner, so I’m looking for advice if somebody has some. I'll soon publish an early code review on Phabricator to ease the process of getting feedback on the code itself.<br><br>Thanks,<br>Louis<br><br>[1]:<span class="m_399335037117398794gmail-m_8602693362901822949Apple-converted-space"> </span><a href="http://lists.llvm.org/pipermail/cfe-dev/2018-July/058460.html" rel="noreferrer" target="_blank">http://lists.llvm.org/pipermail/cfe-dev/2018-July/058460.html</a><br>[2]:<span class="m_399335037117398794gmail-m_8602693362901822949Apple-converted-space"> </span><a href="http://lists.llvm.org/pipermail/cfe-dev/2018-July/058419.html" rel="noreferrer" target="_blank">http://lists.llvm.org/pipermail/cfe-dev/2018-July/058419.html</a><br><br>_______________________________________________<br>cfe-dev mailing list<br><a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a></blockquote></div></div></div></blockquote></div><br></div>_______________________________________________<br>cfe-dev mailing list<br><a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br></blockquote></div></div><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline!important">_______________________________________________</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline!important">cfe-dev mailing list</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><a href="mailto:cfe-dev@lists.llvm.org" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">cfe-dev@lists.llvm.org</a><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a></blockquote></div></div></blockquote></div><br></div>_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div></div>