<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Nov 7, 2017 at 8:16 PM, Rafael Avila de Espindola <span dir="ltr"><<a href="mailto:rafael.espindola@gmail.com" target="_blank">rafael.espindola@gmail.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"><span class="m_-3032447739445501285gmail-">Rui Ueyama <<a href="mailto:ruiu@google.com" target="_blank">ruiu@google.com</a>> writes:<br>
<br>
>> So I am strongly against removing either non TLSDESC support of support<br>
>> for the relaxations.<br>
>><br>
><br>
> It's still pretty arguable. By default, compilers use General Dynamic model<br>
> with -fpic, and Initial Exec without -fpic.<br>
<br>
</span>It is more complicated than that. You can get all 4 modes with clang<br>
<br>
------------------------------<wbr>-<br>
__thread int bar = 42;<br>
int *foo(void) {  return &bar; }<br>
------------------------------<wbr>-<br>
without -fPIC: local exec.<br>
<br>
------------------------------<wbr>-<br>
extern __thread int bar;<br>
int *foo(void) {  return &bar; }<br>
------------------------------<wbr>-<br>
without -fPIC: initial exec.<br>
with -fPIC: general dynamic<br>
<br>
------------------------------<wbr>-<br>
__attribute__((visibility("hid<wbr>den"))) extern __thread int bar;<br>
int *foo(void) {  return &bar; }<br>
------------------------------<wbr>-<br>
with -fPIC: local dynamic.</blockquote><div><br></div><div>The other case is</div><div><div><br></div><div>__attribute__((visibility("<wbr>hidden"))) extern __thread int bar;</div><div>int *foo(void) {  return &bar; }</div></div><div><br></div><div>without -fPIC which choose Local Exec.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="m_-3032447739445501285gmail-"><br>
> lld doesn't do any relaxation<br>
> if -shared is given. So, if you are creating a DSO, thread-local variables<br>
> in the DSO are accessed using Global Dynamic model. No relaxations are<br>
> involved.<br>
<br>
</span>There is not a lot of opportunities there. If one patches one access at<br>
a time LD is as expensive as GD. The linker also doesn't know if the .so<br>
will be used with dlopen or not, sot it cannot relax to IE. I guess a<br>
linker could have that command line option for the second part.<br>
<br>
Now that I spell that out, it is easy to see the TLSDESC big<br>
advantage. It can optimize the case the static linker cannot.</blockquote><div><br></div><div>Because of this fact, DSOs that use thread-local variables such as libc are already compiled with -ftls-model=initial-exec. So the authors of DSOs in which the performance thread-local variables matters are already aware of the issue and how to workaround it.</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"><span class="m_-3032447739445501285gmail-">
> If you are creating an executable and if your executable is not<br>
> position-independent, you're using Initial Exec model by default which is<br>
> as fast as variables accessed through GOT. If you really want to use Local<br>
> Exec model, you can pass -ftls-model=local-exec to compilers.<br>
<br>
</span>But then all the used variables have to be defined in the same<br>
executable. You can't have even one from a shared library (think errno).<br></blockquote><div><br></div><div>Not really -- you can still use Local Exec per variable basis using the visibility attribute. I don't think that we can observe noticeable difference in performance between Initial Exec and Local Exec except an synthetic benchmark though.</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">
The nice thing about linker relaxations is that they are very user<br>
friendly. The linker is the first point in the toolchaing where some<br>
usefull fact is know, and it can optimize the result with no user<br>
intervention.</blockquote><div><br></div><div>I think I agree with this point. Automatic linker code relaxation is convenient and if it makes a difference, we should implement that. But I'd doubt if TLS relaxation is actually effective. George implemented them because there's a spec defining how to relax them, and I accepted the patches without thinking hard enough, but I didn't see a convincing benchmark result (or even a non-convincing one) that shows that these relaxations actually make real-world programs faster. Do you know of any? It is funny that even the creator of TLSDESC found that their optimization didn't actually makes NPTL faster as it is mentioned in the "Conclusion" section in <a href="http://www.fsfla.org/~lxoliva/writeups/TLS/RFC-TLSDESC-x86.txt" target="_blank">http://www.fsfla.org/~lxoliva/<wbr>writeups/TLS/RFC-TLSDESC-x86.<wbr>txt</a>.</div><div><br></div><div>So I don't think I'm proposing we simplify code by degrading user's code. It feels more like we are making too much effort on something that doesn't produce any measurable difference in real life.</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"><span class="m_-3032447739445501285gmail-">
> So I don't see a strong reason to do a complicated instruction rewriting in<br>
> the linker. I feel more like we should do whatever it is instructed to do<br>
> by command line options and input object files. You are for example free to<br>
> pass the -fPIC option to create object files and still let the linker to<br>
> create a non-PIC executable, even though these combinations doesn't make<br>
> much sense and produces slightly inefficient binary. If you don't like it,<br>
> you can fix the compiler options. Thread-local variables can be considered<br>
> in the same way, no?<br>
<br>
</span>They are considered in the same way, we also relax got access :-)<br>
<br>
The proposal is making the linker worse for our users to make our lifes<br>
easier. I really don't think we should do it.<br>
<br>
It is likelly that we can code the existing optimization in a simpler<br>
way. Even if we cannot, I don't think we should remove them.<br>
<br>
Linker relaxations are extremely convenient. We use the example you<br>
gave (-fPIC .o in an executable) all the time in llvm. That way we build<br>
only one .o that is used in lib/ and bin/.<br>
<br>
Linker relaxations are also fundamental to how RISCV works.<br>
<br>
Cheers,<br>
Rafael<br>
</blockquote></div><br></div></div>