[cfe-dev] Adding a new attribute: no_extern_template
Richard Smith via cfe-dev
cfe-dev at lists.llvm.org
Tue Aug 21 15:21:23 PDT 2018
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();
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?
- 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180821/1a981252/attachment.html>
More information about the cfe-dev
mailing list