[cfe-dev] Adding a new attribute: no_extern_template

Louis Dionne via cfe-dev cfe-dev at lists.llvm.org
Fri Aug 24 08:01:02 PDT 2018



> On Aug 23, 2018, at 16:54, John McCall <rjmccall at apple.com> wrote:
> 
> 
> 
>> On Aug 23, 2018, at 3:41 PM, Louis Dionne <ldionne at apple.com <mailto:ldionne at apple.com>> wrote:
>> 
>> 
>> 
>>> On Aug 23, 2018, at 00:36, John McCall <rjmccall at apple.com <mailto:rjmccall at apple.com>> wrote:
>>> 
>>>> On Aug 22, 2018, at 8:41 PM, Richard Smith via cfe-dev <cfe-dev at lists.llvm.org <mailto:cfe-dev at lists.llvm.org>> wrote:
>>>> On Wed, 22 Aug 2018 at 08:39, Louis Dionne via cfe-dev <cfe-dev at lists.llvm.org <mailto:cfe-dev at lists.llvm.org>> wrote:
>>>> 
>>>> 
>>>>> On Aug 21, 2018, at 18:21, Richard Smith <richard at metafoo.co.uk <mailto:richard at metafoo.co.uk>> wrote:
>>>>> 
>>>>> On Tue, 21 Aug 2018 at 14:44, Louis Dionne via cfe-dev <cfe-dev at lists.llvm.org <mailto:cfe-dev at lists.llvm.org>> wrote:
>>>>> Hi,
>>>>> 
>>>>> This message is related to solving the issue explained at [1], and indirectly, the usage of __always_inline__ in libc++ as explained at [2].
>>>>> 
>>>>> 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:
>>>>> 
>>>>>     template <class T>
>>>>>     struct Foo { void func() { } };
>>>>>     extern template struct Foo<int>;
>>>>> 
>>>>>     void use() {
>>>>>       Foo<int> f;
>>>>>       f.func();
>>>>>     }
>>>>> 
>>>>> 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:
>>>>> 
>>>>>     template <class T>
>>>>>     struct Foo { __attribute__((no_extern_template)) void func() { } };
>>>>>     extern template struct Foo<int>;
>>>>> 
>>>>> I'd like to have some feedback on this idea, specifically:
>>>>> 
>>>>> 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:
>>>>> 
>>>>> template <class T> struct Foo { // non-polymorphic class
>>>>>   void foo() { }
>>>>>   __attribute__((no_extern_template)) void bar() { }
>>>>>   void baz() { }
>>>>> };
>>>>> extern template struct Foo<int>;
>>>>> 
>>>>> would be exactly the same as the standard behavior of:
>>>>> 
>>>>> template <class T> struct Foo {
>>>>>   void foo() { }
>>>>>   void bar() { }
>>>>>   void baz() { }
>>>>> };
>>>>> extern void Foo<int>::foo();
>>>>> extern void Foo<int>::baz();
>>>> 
>>>> Yes, this is what I’m asking for.
>>>> 
>>>> 
>>>>> 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.)
>>>>> 
>>>>> 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?
>>>> 
>>>> We’ve talked about it in the thread starting here: http://lists.llvm.org/pipermail/cfe-dev/2018-July/058519.html <http://lists.llvm.org/pipermail/cfe-dev/2018-July/058519.html>. 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.
>>>> 
>>>> 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.
>>>> 
>>>> GCC for many years had an extension to do this: https://gcc.gnu.org/onlinedocs/gcc/Template-Instantiation.html <https://gcc.gnu.org/onlinedocs/gcc/Template-Instantiation.html> (see the bottom of the page).
>>>> 
>>>> 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".
>>> 
>>> 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.
>>> 
>>> 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):
>>> 
>>>   'extern'? 'typeid' '(' type-id ')'
>>> 
>>> 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.
>>> 
>>> John.
>> 
>> 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.
>> 
>> 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:
>> - 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.
> 
> Do you care about supporting more than GCC?  If GCC supports the extension Richard linked, then the opt-in strategy can be made portable.  But if you care about supporting arbitrary other compilers, that's tricky.

Based on __config, we seem to support Clang, GCC, MSVC and IBM’s compiler:

#if defined(__clang__)
#  define _LIBCPP_COMPILER_CLANG
#elif defined(__GNUC__)
#  define _LIBCPP_COMPILER_GCC
#elif defined(_MSC_VER)
#  define _LIBCPP_COMPILER_MSVC
#elif defined(__IBMCPP__)
#  define _LIBCPP_COMPILER_IBM

I don’t know what level of support we have in each of those compilers, since that does not seem to be documented officially.

Louis

> 
> John.
> 
>> - 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.
>> 
>> 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++. Unless there are serious concerns with the attribute, I’d like to solicit feedback on the following points:
>> 
>> - 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?
>> 
>> - What other attributes would be mutually exclusive with this one? Right now I can only think of `internal_linkage`, but is there anything else?
>> 
>> Thanks,
>> Louis
>> 
>>>  
>>>> 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.
>>>> 
>>>> 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.
>>>> 
>>>> Louis
>>>> 
>>>> 
>>>>> 
>>>>> - 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?
>>>>> 
>>>>> - What other attributes would be mutually exclusive with this one? Right now I can only think of `internal_linkage`, but is there anything else?
>>>>> 
>>>>> 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.
>>>>> 
>>>>> Thanks,
>>>>> Louis
>>>>> 
>>>>> [1]: http://lists.llvm.org/pipermail/cfe-dev/2018-July/058460.html <http://lists.llvm.org/pipermail/cfe-dev/2018-July/058460.html>
>>>>> [2]: http://lists.llvm.org/pipermail/cfe-dev/2018-July/058419.html <http://lists.llvm.org/pipermail/cfe-dev/2018-July/058419.html>
>>>>> 
>>>>> _______________________________________________
>>>>> cfe-dev mailing list
>>>>> cfe-dev at lists.llvm.org <mailto:cfe-dev at lists.llvm.org>
>>>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev <http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev>
>>>> _______________________________________________
>>>> cfe-dev mailing list
>>>> cfe-dev at lists.llvm.org <mailto:cfe-dev at lists.llvm.org>
>>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev <http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev>
>>>> _______________________________________________
>>>> cfe-dev mailing list
>>>> cfe-dev at lists.llvm.org <mailto:cfe-dev at lists.llvm.org>
>>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev <http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180824/21bb70f5/attachment.html>


More information about the cfe-dev mailing list