[cfe-dev] Adding a new attribute: no_extern_template

Richard Smith via cfe-dev cfe-dev at lists.llvm.org
Wed Aug 22 17:41:41 PDT 2018


On Wed, 22 Aug 2018 at 08:39, Louis Dionne via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

>
>
> On Aug 21, 2018, at 18:21, Richard Smith <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> 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. 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 (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".


> 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
>> [2]: http://lists.llvm.org/pipermail/cfe-dev/2018-July/058419.html
>>
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> 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/20180822/8985dfc8/attachment.html>


More information about the cfe-dev mailing list