<div dir="ltr"><div>Hi Rui,<br><br>I don’t think our team can support dropping TLS relaxations. As Peter suspected, we have noticed significant run-time performance gains in games when using these relaxations over not using them, with our proprietary linker. Also, for our platform dynamic libraries are usually used via our equivalent of dlopen(), not pre-loaded as is the idea behind TLSDESC, from my understanding, so even if TLSDESC were supported on our platform, there would be no significant benefit gained for us. Finally, we cannot ask our customers to change their compilation settings to use better TLS models, because programs may use static libraries which are intended for use in both dynamic libraries and main executables, and so will lose out if they do not have access to TLS relaxations.<br><br></div>Regards,<br><div><br>James</div></div><div class="gmail_extra"><br><div class="gmail_quote">On 8 November 2017 at 14:10, Peter Smith via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I'm assuming it means the instruction sequences wouldn't be optimized,<br>
I don't think it would be practical to remove support for the<br>
relocations.<br>
<br>
For Arm and, I think Mips is similar, there isn't any TLS relaxation<br>
of instructions as the TLS relocations act on data and not<br>
instructions. There are some cases where dynamic relocations can be<br>
omitted, for example the module-id of an executable is defined to be 1<br>
so there is no need for the dynamic linker to fill this in. For static<br>
linking the linker knows module and the offsets of all the TLS Symbols<br>
so it can resolve all the dynamic relocations. I don't know off the<br>
top of my head whether this would apply to other architectures,<br>
although I think the general principles should hold the same. The last<br>
time I looked relaxation was the technique used to support static<br>
linking on non-Arm and Mips Targets.<br>
<br>
I have a vague memory of the OpenGL folk being sensitive to TLS<br>
performance, particularly as the library is often shared. I think that<br>
TLS relaxation isn't going to show up in many traditional benchmarking<br>
suites as much of the performance critical code is going to be in the<br>
application, and are unlikely to have much TLS in them. I'm thinking<br>
that it would need something like a real-world application that makes<br>
heavy use of shared-libraries with TLS (games, web-browsers or perhaps<br>
HPC?).<br>
<br>
Given that getting convincing data either way about the impact of TLS<br>
relaxation could be difficult we should err towards keeping it.<br>
<br>
Peter<br>
<br>
<br>
On 8 November 2017 at 09:05, Mark Kettenis via llvm-dev<br>
<div class="HOEnZb"><div class="h5"><<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br>
>> Date: Tue, 7 Nov 2017 18:27:37 -0800<br>
>> From: Rui Ueyama via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>><br>
>><br>
>> tl;dr: TLSDESC have solved most problems in formerly inefficient TLS access<br>
>> models, so I think we can drop TLS relaxation support from lld.<br>
>><br>
>> lld's code to handle relocations is a mess; the code consists of a lot of<br>
>> cascading "if"s and needs a lot of prior knowledge to understand what it is<br>
>> doing. Honestly it is head-scratching and needs serious refactoring. I'm<br>
>> trying to simplify it to make it manageable again, and I'm now focusing on<br>
>> the TLS relaxations.<br>
>><br>
>> Thread-local variables in ELF is complicated. The ELF TLS specification [1]<br>
>> defines 4 different access models: General Dynamic, Local Dynamic, Initial<br>
>> Exec and Local Exec.<br>
>><br>
>> I'm not going into the details of the spec here, but the reason why we have<br>
>> so many different models for the same feature is because they were<br>
>> different in speed, and we have to use (formerly) slow models when we know<br>
>> less about their run-time memory layout at compile-time or link-time. So,<br>
>> there was a trade-off between generality and performance. For example, if<br>
>> you want to use thread-local variables in a dlopen(2)'able DSO, you need to<br>
>> choose the slowest model. If a linker knows at link-time that a more<br>
>> restricted access model is applicable (e.g. if it is linking a main<br>
>> executable, it knows for sure that it is not creating a DSO that will be<br>
>> used via dlopen), the linker is allowed to rewrite instructions to load<br>
>> thread-local variables to use a faster access model.<br>
>><br>
>> What makes the situation more complicated is the presence of a new method<br>
>> of accessing thread-local variables. After the ELF TLS spec was defined,<br>
>> TLSDESC [2] was proposed and implemented. With that method, General Dynamic<br>
>> and Local Dynamic models (that were pretty slow in the original spec) are<br>
>> as fast as much faster Initial Exec model. TLSDESC doesn't have a trade-off<br>
>> of dlopen'ability and access speed. According to [2], it also reduces the<br>
>> size of generated DSOs. So it seems like TLSDESC is strictly a better way<br>
>> of accessing thread-local variables than the old way, and the thread-local<br>
>> variable's performance problem (that the TLS ELF spec was trying to address<br>
>> by defining four different access models and relaxations in between)<br>
>> doesn't seem a real issue anymore.<br>
>><br>
>> lld supports all TLS relaxations as defined by the ELF TLS spec. I accepted<br>
>> the patches to implement all these features without thinking hard enough<br>
>> about it, but on second thought, that was likely a wrong decision. Being a<br>
>> new linker, we don't need to trace the history of the evolution of the ELF<br>
>> spec. Instead, we should have implemented whatever it makes sense now.<br>
>><br>
>> So, I'd like to propose we drop TLS relaxations from lld, including Initial<br>
>> Exec → Local Exec. Dropping IE→LE is strictly speaking a degradation, but I<br>
>> don't think that is important. We don't have optimizations for much more<br>
>> frequent variable access patterns such as locally-accessed variables that<br>
>> have GOT slots (which in theory we can skip GOT access because GOT slot<br>
>> values are known at link-time), so it is odd that we are only serious about<br>
>> TLS variables, which are usually much less important. Even if it would turn<br>
>> out that we want it after implementing more important relaxations, I'd like<br>
>> to drop it for now and reimplement it in a different way later.<br>
>><br>
>> This should greatly simplifies the code because it does not only reduce the<br>
>> complexity and amount of the existing code, but also reduces the amount of<br>
>> knowledge you need to have to read the code, without sacrificing<br>
>> performance of lld-generated files in practice.<br>
>><br>
>> Thoughts?<br>
><br>
> Not sure what the impact of this would be.  Does this mean that some<br>
> TLS relocations will no longer be supported?  Or is it that they just<br>
> won't be optimized.  How about static binaries?  Don't they rely on<br>
> the local exec model?<br>
><br>
> Doe this affect linking code generated by older compilers (say GCC<br>
> 4.2.1) in any way?<br>
><br>
> ______________________________<wbr>_________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
</div></div></blockquote></div><br></div>