Allow users to specify the TLS dialect (Traditional TLS vs. TLSDESC) #805
Labels
major-change
A proposal to make a major change to rustc
T-compiler
Add this label so rfcbot knows to poll the compiler team
to-announce
Announce this issue on triage meeting
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 flagtls-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 theEnableTLSDESC
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.Mentors or Reviewers
@tmandry @nikic
Process
The main points of the Major Change Process are as follows:
@rustbot second
.-C flag
, then full team check-off is required.@rfcbot fcp merge
on either the MCP or the PR.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.
The text was updated successfully, but these errors were encountered: