rust-lang / compiler-team

A home for compiler team planning documents, meeting minutes, and other such things.
https://rust-lang.github.io/compiler-team/
Apache License 2.0
387 stars 69 forks source link

Allow users to specify the TLS dialect (Traditional TLS vs. TLSDESC) #805

Open ilovepi opened 2 weeks ago

ilovepi commented 2 weeks ago

Proposal

Today the Rust compiler doesn't allow users to choose the ELF TLS dialect used to compile their programs. Instead, they get the target defaults set in the backend (LLVM, GCC, etc.), which, while useful, may not always be ideal. ELF TLS supports two variants: traditional TLS, whereby the address of thread local variables is provided through the __tls_get_addr() API, and TLS Descriptors (TLSDESC) that use a custom calling convention to supply the TP offset in a GOT slot filled in by the dynamic linker. TLSDESC is generally more performant than traditional TLS, since it caches results and uses a custom calling convention that avoids most of the overhead of a normal function call.

When using TLSDESC, the compiler emits an instruction sequence with a set of special TLSDESC relocations, and the static linker either fully resolves them (in the case of initial exec or local exec), or supplies the requisite dynamic relocations for the dynamic linker to resolve at runtime.

While the TLS dialect is an important part of a platform's ABI, in most libc implementations it is safe to mix traditional TLS and TLSDESC accesses. This compatibility was part of the design rationale for TLS Descriptors.

To support this code generation option, rustc should support a new flag tls-dialect={trad,desc}. This option name and values were chosen to match existing practices in other compilers. The option's purpose is to configure the code generation backend to emit the correct instruction sequences and relocations. For LLVM, this is the EnableTLSDESC codegen option, which was added in LLVM 18. This option does not change the output LLVM IR, but will affect how LLVM lowers TLS accesses. As of LLVM 19, LLVM supports TLSDESC for x86, x86_64, Aarch64, ARM, and RISC-V. We propose that the the flag start as an unstable -Z option, but should later be stabilized as a -C option. The flag will only be applicable to ELF targets, and would be an error to use with non-ELF target triples.

Unresolved Questions

There is an open question about as to how the compiler can prevent misuse of this feature. On one hand, this can be stabilized as a codgen flag with target compatibility checks. On the other, it could be set as a option in the target spec. Updating the target spec likely has the least maintenance burden, and will work for most users. However, it cannot cover all cases, and users may wish to use a different variant than the target defaults. Most recent libc implementations will support both traditional TLS and TLSDESC, however, that may not always be the case, and users may want to take advantage one implementation over the other for various reasons (performance being the most obvious).

Mentors or Reviewers

@tmandry @nikic

Process

The main points of the Major Change Process are as follows:

You can read more about Major Change Proposals on forge.

Comments

This issue is not meant to be used for technical discussion. There is a Zulip stream for that. Use this issue to leave procedural comments, such as volunteering to review, indicating that you second the proposal (or third, etc), or raising a concern that you would like to be addressed.

rustbot commented 2 weeks ago

This issue is not meant to be used for technical discussion. There is a Zulip stream for that. Use this issue to leave procedural comments, such as volunteering to review, indicating that you second the proposal (or third, etc), or raising a concern that you would like to be addressed.

Concerns or objections to the proposal should be discussed on Zulip and formally registered here by adding a comment with the following syntax:

 @rustbot concern reason-for-concern 
 <description of the concern> 

Concerns can be lifted with:

 @rustbot resolve reason-for-concern 

See documentation at https://forge.rust-lang.org

cc @rust-lang/compiler

tmandry commented 1 week ago

Seems like there aren't major outstanding issues, and unresolved questions were noted in the description.

@rustbot second