<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Jul 19, 2018, at 09:30, Louis Dionne <<a href="mailto:ldionne@apple.com" class="">ldionne@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><br class="Apple-interchange-newline"><br class="" style="caret-color: rgb(0, 0, 0); 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; -webkit-text-stroke-width: 0px; text-decoration: none;"><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"><div class="">On Jul 19, 2018, at 04:24, Eric Fiselier <<a href="mailto:eric@efcs.ca" class="">eric@efcs.ca</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="" style="caret-color: rgb(0, 0, 0); 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; -webkit-text-stroke-width: 0px; text-decoration: none;"><br class=""><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Wed, Jul 18, 2018 at 11:53 AM John McCall <<a href="mailto:rjmccall@apple.com" class="">rjmccall@apple.com</a>> wrote:<br class=""></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 class="" style="word-wrap: break-word; line-break: after-white-space;"><div class=""><blockquote type="cite" class=""><div class="">On Jul 18, 2018, at 5:40 AM, Eric Fiselier <<a href="mailto:eric@efcs.ca" target="_blank" class="">eric@efcs.ca</a>> wrote:</div><div class=""><div dir="ltr" class=""><div class="gmail_quote"><div dir="ltr" class="">On Wed, Jul 18, 2018 at 1:30 AM John McCall via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank" class="">cfe-dev@lists.llvm.org</a>> wrote:<br class=""></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 class="" style="word-wrap: break-word; line-break: after-white-space;"><div class=""><blockquote type="cite" class=""><div class="">On Jul 11, 2018, at 5:29 PM, Louis Dionne via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank" class="">cfe-dev@lists.llvm.org</a>> wrote:</div><br class="m_3773437658758568607m_1381249095906822725Apple-interchange-newline"><div class=""><div class="" style="word-wrap: break-word; line-break: after-white-space;"><div class="">- llvm-dev (sorry Chandler, I’m not accustomed to which topics should be discussed on which lists yet)</div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jul 11, 2018, at 06:37, Chandler Carruth <<a href="mailto:chandlerc@gmail.com" target="_blank" class="">chandlerc@gmail.com</a>> wrote:</div><br class="m_3773437658758568607m_1381249095906822725Apple-interchange-newline"><div class=""><div dir="ltr" class="">This is probably much more of a question for the Clang list....<br class=""><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Tue, Jul 10, 2018 at 6:21 PM Hubert Tong via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a>> wrote:<br class=""></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 dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote">On Tue, Jul 10, 2018 at 7:20 PM, Louis Dionne via llvm-dev<span class="Apple-converted-space"> </span><span dir="ltr" class=""><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a>></span><span class="Apple-converted-space"> </span>wrote:<br class=""><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 class=""><br class="">While investigating the situation of visibility annotations and linkage in libc++ with the goal of removing uses of `__always_inline__`, Eric Fiselier and I stumbled upon the attached test case, which I don't think Clang compiles properly. Here's the gist of the test case, reduced to the important parts (see the attachment if you want to repro):<br class=""><br class="">   <span class="Apple-converted-space"> </span>// RUN: %cxx -shared -o %T/libtest.so %flags %compile_flags -fPIC %s<br class="">   <span class="Apple-converted-space"> </span>// RUN: %cxx -c -o %T/main.o %flags %compile_flags %s -O2 -DBUILDING_MAIN<br class="">   <span class="Apple-converted-space"> </span>// RUN: %cxx -o %T/test.exe -L%T/ -Wl,-rpath,%T/ -ltest %T/main.o<br class="">   <span class="Apple-converted-space"> </span>// RUN: %T/test.exe<br class=""><br class="">   <span class="Apple-converted-space"> </span>template <class T><br class="">   <span class="Apple-converted-space"> </span>struct Foo {<br class="">       <span class="Apple-converted-space"> </span>inline __attribute__((visibility("hidden")))<br class="">       <span class="Apple-converted-space"> </span>int __init(int x) { /* LOTS OF CODE */ }<br class=""><br class="">       <span class="Apple-converted-space"> </span>inline __attribute__((visibility("default")))<br class="">       <span class="Apple-converted-space"> </span>int bar(int x) { return __init(x); }<br class="">   <span class="Apple-converted-space"> </span>};<br class=""><br class="">   <span class="Apple-converted-space"> </span>extern template struct Foo<int>;<br class=""><br class="">   <span class="Apple-converted-space"> </span>#ifdef BUILDING_MAIN<br class="">       <span class="Apple-converted-space"> </span>int main() {<br class="">           <span class="Apple-converted-space"> </span>Foo<int> f;<br class="">           <span class="Apple-converted-space"> </span>f.bar(101);<br class="">       <span class="Apple-converted-space"> </span>}<br class="">   <span class="Apple-converted-space"> </span>#else<br class="">       <span class="Apple-converted-space"> </span>template struct Foo<int>;<br class="">   <span class="Apple-converted-space"> </span>#endif<br class=""><br class="">When running the attached file in `lit`, we get:<br class=""><br class="">   <span class="Apple-converted-space"> </span>Undefined symbols for architecture x86_64:<br class="">     <span class="Apple-converted-space"> </span>"Foo<int>::__init(int)", referenced from:<br class="">         <span class="Apple-converted-space"> </span>_main in main.o<br class="">   <span class="Apple-converted-space"> </span>ld: symbol(s) not found for architecture x86_64<br class=""><br class="">The idea here is that `__init` is a pretty big function, and we're promising that an external definition of it is available through the use of the extern template declaration. With the appropriate optimization level (O2 and above), LLVM decides not to include the definition of `__init` in the executable and to use the one available externally. Unfortunately, `__init` has hidden visibility, and so the definition in the .so is not visible to the executable, and the link fails.<br class=""><br class="">Where I think Clang/LLVM goes wrong is when it decides to remove the instantiation in the executable in favor of the one that is hypothetically provided externally. Indeed, the Standard says in [temp.explicit]/12 (<a href="http://eel.is/c++draft/temp.explicit#12" rel="noreferrer" target="_blank" class="">http://eel.is/c++draft/temp.explicit#12</a>):<br class=""><br class="">   <span class="Apple-converted-space"> </span>Except for inline functions and variables, declarations with types deduced from their initializer or return value ([dcl.spec.auto]), const variables of literal types, variables of reference types, and class template specializations, explicit instantiation declarations have the effect of suppressing the implicit instantiation of the definition of the entity to which they refer. [ Note: The intent is that an inline function that is the subject of an explicit instantiation declaration will still be implicitly instantiated when odr-used so that the body can be considered for inlining, but that no out-of-line copy of the inline function would be generated in the translation unit. — end note ]<br class=""><br class="">Only reading the normative wording, it seems like LLVM should leave the instantiation there because it can't actually assume that there will be a definition provided elsewhere (yes, despite the extern template declaration, because the function is inline). Then, the non-normative note seems to be approving of what LLVM is doing, but I'm wondering whether that's really the intended behavior.<br class=""><br class="">Questions:<br class="">1. Is what LLVM's doing there legal?<br class=""></blockquote></div></div></div><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class="">The Standard does not say anything about the instantiation producing a definition associated with object file that results from translating the current translation unit. The program is only valid if there is indeed an explicit instantiation provided elsewhere. That said, the as-if rule that allows the suppression of the definition assumes that a provided explicit instantiation can be linked against. It is up the the designers of the extension (the visibility attribute) to say whether or not the rule with templates having hidden visibility is that the explicit instantiation needs to be provided in the same “module".<br class=""></div></div></div></div></blockquote></div></div></div></blockquote><div class=""><br class=""></div><div class="">Thanks Hubert. That makes some amount of sense. So in that case, it would either be</div><div class="">1. A bug that Clang is not emitting `__init` in the object file since it has hidden visibility, and it can’t tell whether we’re going to try to link dynamically or statically with the other module that provides it.</div><div class="">2. The intended design that visibility attributes interact very poorly with extern template declarations. This would be sad, since libc++ is a pretty big user of both, and it really isn’t working that well for us.</div></div></div></div></blockquote></div><div class=""><blockquote type="cite" class=""><div class=""><div class="" style="word-wrap: break-word; line-break: after-white-space;"><div class=""><div class=""><br class=""></div><div class="">In both cases, it’s not a conformance bug with the Standard, as Hubert points out. It would be lovely if someone more knowledgeable about the visibility attributes could shed some light on whether that’s intended and we need to invent something new, or whether it is correct to consider it a bug and we should fix it (somehow).</div></div></div></div></blockquote><div class=""><br class=""></div>libc++ should declare explicit instantiations of just the functions it actually intends to provide in the library instead of declaring an instantiation of the entire class-template and then trying to retroactively patch over the problem with a mess of attributes on every declaration.  You should be able to then just give the templates type_visibility("default") and visibility("hidden").  You can put visibility("default") directly on the instantiation and it should override any attributes from the template.</div></div></blockquote><div class=""><br class=""></div><div class="">That's a very good point, somehow I hadn't thought of it before.</div><div class=""><br class=""></div><div class="">One problem is that attributes like `internal_linkage` need to appear on the first declaration, and can't be added later when declaring an instantiation.</div><div class="">Though I'm not sure if that restriction is artificial, at least in  this particular case.</div></div></div></div></blockquote><div class=""><br class=""></div>Well, I can't imagine why you would declare an internal-linkage explicit instantiation, but if you have a need to, I don't see any reason we couldn't allow that.</div></div></blockquote><div class=""><br class=""></div><div class="">Sorry. I don't want to declare an explicit instantiation as having internal linkage. But I might want to declare everything except for an explicit instantiation as having internal linkage.</div><div class="">But since the attribute must appear on the first declaration, it's not possible to override it.</div><div class=""><br class=""></div><div class="">Even if we made Clang play nice with our design or provide magic to allow libc++ to act in a sane manner, GCC is still an issue.</div></div></div></div></blockquote><div style="caret-color: rgb(0, 0, 0); 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; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><br class=""></div><div style="caret-color: rgb(0, 0, 0); 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; -webkit-text-stroke-width: 0px; text-decoration: none;" class="">There’s another option, though. We could also just ditch internal_linkage (and always_inline for that matter) entirely, since I don’t think anybody knows whether we need to enable TUs built with different libc++ versions to interoperate. It’s “supported” right now, but it’s not clear that we actually need it nor support it properly. If we did that, we’d be down to a simple visibility problem, which we can solve using what John suggested. The main benefit I see is that we’d get rid of 95% of the visibility annotations in libc++: we’d simply build with `-fvisibility=hidden` and export exactly what we want. That would be more sane.</div></div></blockquote><div><br class=""></div>Well, never mind this. It’s apparently not an option since clients (like libfuzzer) are relying on this. Someone mentioned in IRC that libfuzzer is linked statically against a libc++ they build themselves. And then a user application using (a possibly different version of) libc++ can link statically against libfuzzer. While I think this is a brittle setup (and it only works because libc++ is currently throwing __always_inline__ really hard at the problem), it’s probably not reasonable to stop supporting the use case since people have been relying on it.</div><div><br class=""></div><div>Louis</div><div><br class=""><blockquote type="cite" class=""><div class=""><br class="" style="caret-color: rgb(0, 0, 0); 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; -webkit-text-stroke-width: 0px; text-decoration: none;"><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"><div class=""><div dir="ltr" class="" style="caret-color: rgb(0, 0, 0); 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; -webkit-text-stroke-width: 0px; text-decoration: none;"><div class="gmail_quote"><div class=""> </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 class="" style="word-wrap: break-word; line-break: after-white-space;"><div class=""><br class=""></div><div class="">The visibility attributes, for better or worse, are more flexible about some of these things already.</div><div class=""><br class=""></div><div class="">John.</div><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_quote"><div class=""><br class=""></div><div class=""><br class=""></div><div class=""> </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 class="" style="word-wrap: break-word; line-break: after-white-space;"><div class=""><br class=""></div><div class="">John.</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class=""><div class="" style="word-wrap: break-word; line-break: after-white-space;"><div class=""><div class=""><br class=""></div><div class="">In the meantime, this is blocking our ability to use neither `__attribute__((Internal_linkage))` nor `__attribute__((__always_inline__))` on any declaration that may have an extern template declaration (which is basically all declarations, because users can write extern template declarations for std:: components in their own code).</div><div class=""><br class=""></div><div class="">Louis</div><br class=""><blockquote type="cite" class=""><div dir="ltr" class=""><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 dir="ltr" class=""><div class="gmail_extra"><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;">2. If it is legal, then libc++ needs a way to express that a function should either be inlined in the caller, or emitted in the TU and de-duplicated later (I think that’s linkonce_odr), despite there being an extern template declaration promising a definition elsewhere. I think the current situation is that the function gets available_externally linkage instead. Is there a way to express this at the C++ source code level?<br class=""><br class="">Thank you,<br class="">Louis Dionne<br class=""><br class=""><br class="">_______________________________________________<br class="">LLVM Developers mailing list<br class=""><a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a><br class=""><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br class=""><br class=""></blockquote></div></div></div>_______________________________________________<br class="">LLVM Developers mailing list<br class=""><a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a><br class=""><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br class=""></blockquote></div></div></blockquote></div><br class=""></div>_______________________________________________<br class="">cfe-dev mailing list<br class=""><a href="mailto:cfe-dev@lists.llvm.org" target="_blank" class="">cfe-dev@lists.llvm.org</a><br class=""><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br class=""></div></blockquote></div><br class=""></div>_______________________________________________<br class="">cfe-dev mailing list<br class=""><a href="mailto:cfe-dev@lists.llvm.org" target="_blank" class="">cfe-dev@lists.llvm.org</a><br class=""><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a></blockquote></div></div></div></blockquote></div></div></blockquote></div></div></div></blockquote></div></blockquote></div><br class=""></body></html>