Skip to content

Commit 3bcaeb0

Browse files
committed
Auto merge of rust-lang#84876 - alexcrichton:inline-thread-locals-cross-crate, r=Mark-Simulacrum
std: Attempt again to inline thread-local-init across crates Issue rust-lang#25088 has been part of `thread_local!` for quite some time now. Historical attempts have been made to add `#[inline]` to `__getit` in rust-lang#43931, rust-lang#50252, and rust-lang#59720, but these attempts ended up not landing at the time due to segfaults on Windows. In the interim though with `const`-initialized thread locals AFAIK this is the only remaining bug which is why you might want to use `#[thread_local]` over `thread_local!`. As a result I figured it was time to resubmit this and see how it fares on CI and if I can help debugging any issues that crop up. Closes rust-lang#25088
2 parents 3e827cc + 641d3b0 commit 3bcaeb0

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

library/std/src/thread/local.rs

+24
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ macro_rules! thread_local {
162162
macro_rules! __thread_local_inner {
163163
// used to generate the `LocalKey` value for const-initialized thread locals
164164
(@key $t:ty, const $init:expr) => {{
165+
#[cfg_attr(not(target_env = "msvc"), inline)] // see comments below
165166
unsafe fn __getit() -> $crate::option::Option<&'static $t> {
166167
const _REQUIRE_UNSTABLE: () = $crate::thread::require_unstable_const_init_thread_local();
167168

@@ -260,6 +261,29 @@ macro_rules! __thread_local_inner {
260261
#[inline]
261262
fn __init() -> $t { $init }
262263

264+
// When reading this function you might ask "why is this inlined
265+
// everywhere other than MSVC?", and that's a very reasonable
266+
// question to ask. The short story is that it segfaults rustc if
267+
// this function is inlined. The longer story is that MSVC looks to
268+
// not support `extern` references to thread locals across DLL
269+
// boundaries. This appears to at least not be supported in the ABI
270+
// that LLVM implements.
271+
//
272+
// Because of this we never inline on MVSC, but we do inline on
273+
// other platforms (where external references to thread locals
274+
// across DLLs are supported). A better fix for this would be to
275+
// inline this function on MSVC, but only for "statically linked"
276+
// components. For example if two separately compiled rlibs end up
277+
// getting linked into a DLL then it's fine to inline this function
278+
// across that boundary. It's only not fine to inline this function
279+
// across a DLL boundary. Unfortunately rustc doesn't currently have
280+
// this sort of logic available in an attribute, and it's not clear
281+
// that rustc is even equipped to answer this (it's more of a Cargo
282+
// question kinda). This means that, unfortunately, MSVC gets the
283+
// pessimistic path for now where it's never inlined.
284+
//
285+
// The issue of "should enable on MSVC sometimes" is #84933
286+
#[cfg_attr(not(target_env = "msvc"), inline)]
263287
unsafe fn __getit() -> $crate::option::Option<&'static $t> {
264288
#[cfg(all(target_arch = "wasm32", not(target_feature = "atomics")))]
265289
static __KEY: $crate::thread::__StaticLocalKeyInner<$t> =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![crate_type = "lib"]
2+
#![feature(thread_local_const_init)]
3+
4+
use std::cell::Cell;
5+
6+
thread_local!(pub static A: Cell<u64> = const { Cell::new(0) });

src/test/codegen/thread-local.rs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// compile-flags: -O
2+
// aux-build:thread_local_aux.rs
3+
// ignore-windows FIXME(#84933)
4+
// ignore-wasm globals are used instead of thread locals
5+
// ignore-emscripten globals are used instead of thread locals
6+
// ignore-android does not use #[thread_local]
7+
8+
#![crate_type = "lib"]
9+
#![feature(thread_local_const_init)]
10+
11+
extern crate thread_local_aux as aux;
12+
13+
use std::cell::Cell;
14+
15+
thread_local!(static A: Cell<u32> = const { Cell::new(1) });
16+
17+
// CHECK: [[TLS_AUX:@.+]] = external thread_local local_unnamed_addr global i64
18+
// CHECK: [[TLS:@.+]] = internal thread_local unnamed_addr global
19+
20+
// CHECK-LABEL: @get
21+
#[no_mangle]
22+
fn get() -> u32 {
23+
// CHECK: %0 = load i32, i32* bitcast ({{.*}} [[TLS]] to i32*)
24+
// CHECK-NEXT: ret i32 %0
25+
A.with(|a| a.get())
26+
}
27+
28+
// CHECK-LABEL: @set
29+
#[no_mangle]
30+
fn set(v: u32) {
31+
// CHECK: store i32 %0, i32* bitcast ({{.*}} [[TLS]] to i32*)
32+
// CHECK-NEXT: ret void
33+
A.with(|a| a.set(v))
34+
}
35+
36+
// CHECK-LABEL: @get_aux
37+
#[no_mangle]
38+
fn get_aux() -> u64 {
39+
// CHECK: %0 = load i64, i64* [[TLS_AUX]]
40+
// CHECK-NEXT: ret i64 %0
41+
aux::A.with(|a| a.get())
42+
}
43+
44+
// CHECK-LABEL: @set_aux
45+
#[no_mangle]
46+
fn set_aux(v: u64) {
47+
// CHECK: store i64 %0, i64* [[TLS_AUX]]
48+
// CHECK-NEXT: ret void
49+
aux::A.with(|a| a.set(v))
50+
}

0 commit comments

Comments
 (0)