<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jan 18, 2017 at 7:35 AM, Mehdi Amini <span dir="ltr"><<a href="mailto:mehdi.amini@apple.com" target="_blank">mehdi.amini@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div><div class="h5"><div><br></div><div><br>On Jan 18, 2017, at 7:11 AM, Teresa Johnson <<a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a>> wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jan 14, 2017 at 12:15 PM, Mehdi Amini <span dir="ltr"><<a href="mailto:mehdi.amini@apple.com" target="_blank">mehdi.amini@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><br><div><div><div class="m_5269439173765967732gmail-h5"><blockquote type="cite"><div>On Jan 14, 2017, at 8:14 AM, Teresa Johnson <<a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a>> wrote:</div><br class="m_5269439173765967732gmail-m_-2961915564876474064Apple-interchange-newline"><div><br class="m_5269439173765967732gmail-m_-2961915564876474064Apple-interchange-newline"><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"><div class="gmail_quote" 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">On Fri, Jan 13, 2017 at 9:54 PM, Mehdi Amini<span class="m_5269439173765967732gmail-m_-2961915564876474064Apple-converted-space"> </span><span dir="ltr"><<a href="mailto:mehdi.amini@apple.com" target="_blank">mehdi.amini@apple.com</a>></span><span class="m_5269439173765967732gmail-m_-2961915564876474064Apple-converted-space"> </span><wbr>wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word">Hi,<div><br></div><div>I believe we should get it right (and simpler) if (when…) we move to the representation we discussed last spring: <a href="https://llvm.org/bugs/show_bug.cgi?id=27866" target="_blank">https://llvm.org/bugs/<wbr>show_bug.cgi?id=27866</a></div><div><br></div><div>Our conclusion was that we should always have alias pointing to private anonymous and nothing else, so when we currently have:</div><div><div><br></div><div>@a = global i32 0</div><div>@b = alias @a</div><div><br></div><div>It should always become:</div><div><br></div><div>@0 = private i32 0</div><div>@a = alias @0</div><div>@b = alias @0</div></div></div></blockquote><div><br></div><div>Yes that has some nice properties. I think it makes at least one case harder though.</div><div><br></div><div>Consider:</div><div><br></div><div>1) Original: </div><div>define linkonce_odr void @a() { ... }<br></div><div>@b = linkonce_odr alias void (), void ()* @a</div><div><br></div><div>2) In the proposed canonical form this becomes:</div><div>define private void @0() { ... }<br></div><div>@a = linkonce_odr alias void (), void ()* @0<br></div><div>@b = linkonce_odr alias void (), void ()* @0</div><div><br></div><div>And let's say @a and @b are both non-prevailing in this module. I think we lose inlining ability for these functions when we start dropping non-prevailing copies.</div><div><br></div><div>In 1) we could transform this (as described in my earlier email) to:</div><div><br></div><div>define available_externally void @a() { ... }<br></div><div>/* RAUW @b with @a */</div><div><br></div><div>and we can inline @a into all callsites (which includes calls original via @b), before dropping it.</div><div><br></div><div>With 2), I think we would need to transform this to:</div><div><br></div><div><div>define private void @0() { ... }<br></div><div>declare void @a()</div><div>declare void @b()</div><div><br></div></div><div>and we no longer can inline calls to @a and @b. I suppose we could intervene and special case this scenario so that it is transformed as for 1) when there are non-prevailing odr aliases.</div></div></div></blockquote><div><br></div></div></div><div>The complication is that a cloneFunction() step would be needed right?</div></div></div></blockquote><div><br></div><div>I hadn't thought about that level of implementation detail, but in any case a cloneFunction step will be needed with both the current alias format and with your proposed new form (just in different cases though). My point was that the proposed format doesn't always make the handling simpler.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div><div><br></div><div>Alternatively I can imagine us allowing available_externally alias, I’m not sure why they are forbidden today other than because we map the object-level limitations in the IR, but I can miss something (we don’t have alias on Darwin either…).</div></div></div></blockquote><div><br></div><div>Allowing available_externally alias helps simplify the case where we have a non-prevailing weak alias. We would still need to address the case where the aliasee is non-prevailing, which I think is the trickier situation to handle.</div></div></div></div></div></blockquote><div><br></div></div></div><div>I'm missing something, aren't we talking about the proposal where all aliasee are always private?</div></div></blockquote><div><br></div><div>Oh I see. Yes, that is the part simplified by the proposed new alias handling. I misunderstood what your "Alternatively" was referring to. <br><br></div><div>I'm a little wary of making fixing this issue for ThinLTO dependent on changing the alias representation in the compiler. Maybe it wouldn't be that difficult to do though.</div><div><br></div><div>Teresa</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div><div class="h5"><div><br></div><div>Mehdi</div><div><br></div><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div><div><div class="m_5269439173765967732gmail-h5"><div><br></div><div><br></div><blockquote type="cite"><div><div class="gmail_quote" 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"><div><br></div><div>I need to think through the various permutations of linkage types and see how they get transformed in the current and proposed canonical forms to see if there are other cases that need special handling.</div><div><br></div><div>Teresa</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div><br></div><div><br></div><div>— </div><span class="m_5269439173765967732gmail-m_-2961915564876474064gmail-HOEnZb"><font color="#888888"><div>Mehdi</div></font></span><div><div class="m_5269439173765967732gmail-m_-2961915564876474064gmail-h5"><div><br></div><div><br></div><div><br></div><div><div><blockquote type="cite"><div>On Jan 13, 2017, at 3:21 PM, Peter Collingbourne <<a href="mailto:peter@pcc.me.uk" target="_blank">peter@pcc.me.uk</a>> wrote:</div><br class="m_5269439173765967732gmail-m_-2961915564876474064gmail-m_-7542585264426303063Apple-interchange-newline"><div><div dir="ltr">Hi Teresa,<div><br></div><div>I think that to answer your question correctly it is helpful to consider what is going on at the object file level. For your test1.c we conceptually have a .text section containing the body of f, and then three symbols:</div><div><br></div><div>.weak f</div><div>f = .text</div><div>.globl strongalias</div><div>strongalias = .text</div><div>.weak weakalias</div><div>weakalias = .text</div><div><br></div><div>Note that f, strongalias and weakalias are not related at all, except that they happen to point to the same place. If f is overridden by a symbol in another object file, it does not affect the symbols strongalias and weakalias, so we still need to make them point to .text. I don't think it would be right to make strongalias and weakalias into copies of f, as that would be observable through function pointer equality. Most likely all you need to do is to internalize f and keep strongalias and weakalias as aliases of f.</div><div><br></div><div>If we're resolving strongalias to f at -O2, that seems like a bug to me. We can probably only resolve an alias to the symbol it references if we are guaranteed that both symbols will have the same resolution, i.e. we must check at least that both symbols have strong or internal linkage. If we cared about symbol interposition, we might also want to check that both symbols have non-default visibility, but I think that our support for that is still a little fuzzy at the moment.<br></div><div><br></div><div>Thanks,</div><div>Peter</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 13, 2017 at 2:33 PM, Teresa Johnson<span class="m_5269439173765967732gmail-m_-2961915564876474064Apple-converted-space"> </span><span dir="ltr"><<a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a>></span><span class="m_5269439173765967732gmail-m_-2961915564876474064Apple-converted-space"><wbr> </span>wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi Mehdi, Peter and David (and anyone else who sees this),<div><br></div><div>I've been playing with some examples to handle the weak symbol cases we discussed in IRC earlier this week in the context of D28523. I was going to implement the support for turning aliases into copies in order to enable performing thinLTOResolveWeakF<wbr>orLinkerGUID on both aliases and aliasees, as a first step to being able to drop non-prevailing weak symbols in ThinLTO backends.</div><div><br></div><div>I was wondering though what happens if we have an alias, which may or may not be weak itself, to a non-odr weak symbol that isn't prevailing. In that case, do we eventually want references via the alias to go to the prevailing copy (in another module), or to the original copy in the alias's module? I looked at some examples without ThinLTO, and am a little confused. Current (non-ThinLTO) behavior in some cases seems to depend on opt level.</div><div><br></div><div>Example:</div><div><br></div><div><div>$ cat weak12main.c</div><div>extern void test2();</div><div>int main() {</div><div> <span class="m_5269439173765967732gmail-m_-2961915564876474064Apple-converted-space"> </span>test2();</div><div>}</div><div><br></div><div>$ cat weak1.c</div><div>#include <stdio.h></div><div><br></div><div>void weakalias() __attribute__((weak, alias ("f")));</div><div>void strongalias() __attribute__((alias ("f")));</div><div><br></div><div>void f () __attribute__ ((weak));</div><div>void f()</div><div>{</div><div> <span class="m_5269439173765967732gmail-m_-2961915564876474064Apple-converted-space"> </span>printf("In weak1.c:f\n");</div><div>}</div><div>void test1() {</div><div> <span class="m_5269439173765967732gmail-m_-2961915564876474064Apple-converted-space"> </span>printf("Call f() from weak1.c:\n");</div><div> <span class="m_5269439173765967732gmail-m_-2961915564876474064Apple-converted-space"> </span>f();</div><div> <span class="m_5269439173765967732gmail-m_-2961915564876474064Apple-converted-space"> </span>printf("Call weakalias() from weak1.c:\n");</div><div> <span class="m_5269439173765967732gmail-m_-2961915564876474064Apple-converted-space"> </span>weakalias();</div><div> <span class="m_5269439173765967732gmail-m_-2961915564876474064Apple-converted-space"> </span>printf("Call strongalias() from weak1.c:\n");</div><div> <span class="m_5269439173765967732gmail-m_-2961915564876474064Apple-converted-space"> </span>strongalias();</div><div>}</div><div><br></div><div>$ cat weak2.c</div><div>#include <stdio.h></div><div><br></div><div>void f () __attribute__ ((weak));</div><div>void f()</div><div>{</div><div> <span class="m_5269439173765967732gmail-m_-2961915564876474064Apple-converted-space"> </span>printf("In weak2.c:f\n");</div><div>}</div><div>extern void test1();</div><div>void test2()</div><div>{</div><div> <span class="m_5269439173765967732gmail-m_-2961915564876474064Apple-converted-space"> </span>test1();</div><div> <span class="m_5269439173765967732gmail-m_-2961915564876474064Apple-converted-space"> </span>printf("Call f() from weak2.c\n");</div><div> <span class="m_5269439173765967732gmail-m_-2961915564876474064Apple-converted-space"> </span>f();</div><div>}</div></div><div><br></div><div>If I link weak1.c before weak2.c, nothing is surprising (we always invoke weak1.c:f at both -O0 and -O2):</div><div><br></div><div><div><div>$ clang weak12main.c weak1.c weak2.c -O0</div><div>$ a.out</div><div>Call f() from weak1.c:</div><div>In weak1.c:f</div><div>Call weakalias() from weak1.c:</div><div>In weak1.c:f</div><div>Call strongalias() from weak1.c:</div><div>In weak1.c:f</div><div>Call f() from weak2.c</div><div>In weak1.c:f</div><div><br></div><div>$ clang weak12main.c weak1.c weak2.c -O2</div><div>$ a.out</div><div>Call f() from weak1.c:</div><div>In weak1.c:f</div><div>Call weakalias() from weak1.c:</div><div>In weak1.c:f</div><div>Call strongalias() from weak1.c:</div><div>In weak1.c:f</div><div>Call f() from weak2.c</div><div>In weak1.c:f</div><div><br></div><div>If I instead link weak2.c first, so it's copy of f() is prevailing, I still get weak1.c:f for the call via weakalias() (both opt levels), and for strongalias() when building at -O0. At -O2 the compiler replaces the call to strongalias() with a call to f(), so it get's the weak2 copy in that case.</div><div><br></div><div>$ clang weak12main.c weak2.c weak1.c -O2</div><div>$ a.out</div><div>Call f() from weak1.c:</div><div>In weak2.c:f</div><div>Call weakalias() from weak1.c:</div><div>In weak1.c:f</div><div>Call strongalias() from weak1.c:</div><div>In weak2.c:f</div><div>Call f() from weak2.c</div><div>In weak2.c:f</div><div><br></div><div>$ clang weak12main.c weak2.c weak1.c -O0</div><div>$ a.out</div><div>Call f() from weak1.c:</div><div>In weak2.c:f</div><div>Call weakalias() from weak1.c:</div><div>In weak1.c:f</div><div>Call strongalias() from weak1.c:</div><div>In weak1.c:f</div><div>Call f() from weak2.c</div><div>In weak2.c:f</div></div><div><br></div><div>I'm wondering what the expected/correct behavior is? Depending on what is correct, we need to handle this differently in ThinLTO mode. Let's say weak1.c's copy of f() is not prevailing and I am going to drop it (it needs to be removed completely, not turned into available_externally to ensure it isn't inlined since weak isInterposable). If we want the aliases in weak1.c to reference the original version, then copying is correct (e.g. weakalias and strong alias would each become a copy of weak1.c's f()). If we however want them to resolve to the prevailing copy of f(), then we need to turn the aliases into declarations (external linkage in the case of strongalias and external weak in the case of weakalias?). </div><div><br></div><div>I also tried the case where f() was in a comdat, because I also need to handle that case in ThinLTO (when f() is not prevailing, drop it from the comdat and remove the comdat from that module). Interestingly, in this case when weak2.c is prevailing, I get the following warning when linking and get a seg fault at runtime:</div><div><br></div><div><div>weak1.o:weak1.o:function test1: warning: relocation refers to discarded section</div></div><div><br></div><div>Presumably the aliases still refer to the copy in weak1.c, which is in the comdat that gets dropped by the linker. So is it not legal to have an alias to a weak symbol in a comdat (i.e. alias from outside the comdat)? We don't complain in the compiler.</div><div><br></div><div>Thanks,</div><div>Teresa</div><span class="m_5269439173765967732gmail-m_-2961915564876474064gmail-m_-7542585264426303063HOEnZb"><font color="#888888">--<span class="m_5269439173765967732gmail-m_-2961915564876474064Apple-converted-space"> </span><br><div class="m_5269439173765967732gmail-m_-2961915564876474064gmail-m_-7542585264426303063m_1265033782169285673gmail_signature"><span style="font-family:times;font-size:inherit"><table cellspacing="0" cellpadding="0"><tbody><tr style="color:rgb(85,85,85);font-family:sans-serif;font-size:small"><td nowrap style="border-top:2px solid rgb(213,15,37)">Teresa Johnson |</td><td nowrap style="border-top:2px solid rgb(51,105,232)"> Software Engineer |</td><td nowrap style="border-top:2px solid rgb(0,153,57)"> <a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a> |</td><td nowrap style="border-top:2px solid rgb(238,178,17)"> <a href="tel:(408)%20460-2413" value="+14084602413" target="_blank">408-460-2413</a></td></tr></tbody></table></span></div></font></span></div></div></blockquote></div><br><br clear="all"><div><br></div>--<span class="m_5269439173765967732gmail-m_-2961915564876474064Apple-converted-space"> </span><br><div class="m_5269439173765967732gmail-m_-2961915564876474064gmail-m_-7542585264426303063gmail_signature"><div dir="ltr">-- <div>Peter</div></div></div></div></div></blockquote></div><br></div></div></div></div></blockquote></div><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"><br clear="all" 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"><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"><br></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;float:none;display:inline">--<span class="m_5269439173765967732gmail-m_-2961915564876474064Apple-converted-space"> </span></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"><div class="m_5269439173765967732gmail-m_-2961915564876474064gmail_signature" 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"><span style="font-family:times;font-size:inherit"><table cellspacing="0" cellpadding="0"><tbody><tr style="color:rgb(85,85,85);font-family:sans-serif;font-size:small"><td nowrap style="border-top:2px solid rgb(213,15,37)">Teresa Johnson |</td><td nowrap style="border-top:2px solid rgb(51,105,232)"> Software Engineer |</td><td nowrap style="border-top:2px solid rgb(0,153,57)"> <a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a> |</td><td nowrap style="border-top:2px solid rgb(238,178,17)"> <a href="tel:(408)%20460-2413" value="+14084602413" target="_blank">408-460-2413</a></td></tr></tbody></table></span></div></div></blockquote></div></div></div><br></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="m_5269439173765967732gmail_signature"><span style="font-family:times;font-size:medium"><table cellspacing="0" cellpadding="0"><tbody><tr style="color:rgb(85,85,85);font-family:sans-serif;font-size:small"><td nowrap style="border-top:2px solid rgb(213,15,37)">Teresa Johnson |</td><td nowrap style="border-top:2px solid rgb(51,105,232)"> Software Engineer |</td><td nowrap style="border-top:2px solid rgb(0,153,57)"> <a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a> |</td><td nowrap style="border-top:2px solid rgb(238,178,17)"> <a href="tel:(408)%20460-2413" value="+14084602413" target="_blank">408-460-2413</a></td></tr></tbody></table></span></div>
</div></div>
</div></blockquote></div></div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><span style="font-family:Times;font-size:medium"><table cellspacing="0" cellpadding="0"><tbody><tr style="color:rgb(85,85,85);font-family:sans-serif;font-size:small"><td nowrap style="border-top-style:solid;border-top-color:rgb(213,15,37);border-top-width:2px">Teresa Johnson |</td><td nowrap style="border-top-style:solid;border-top-color:rgb(51,105,232);border-top-width:2px"> Software Engineer |</td><td nowrap style="border-top-style:solid;border-top-color:rgb(0,153,57);border-top-width:2px"> <a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a> |</td><td nowrap style="border-top-style:solid;border-top-color:rgb(238,178,17);border-top-width:2px"> 408-460-2413</td></tr></tbody></table></span></div>
</div></div>