Description
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
Process
The main points of the Major Change Process are as follows:
- File an issue describing the proposal.
- A compiler team member or contributor who is knowledgeable in the area can second by writing
@rustbot second
.- Finding a "second" suffices for internal changes. If however, you are proposing a new public-facing feature, such as a
-C flag
, then full team check-off is required. - Compiler team members can initiate a check-off via
@rfcbot fcp merge
on either the MCP or the PR.
- Finding a "second" suffices for internal changes. If however, you are proposing a new public-facing feature, such as a
- Once an MCP is seconded, the Final Comment Period begins. If no objections are raised after 10 days, the MCP is considered approved.
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.