Skip to content

rustc_codegen_llvm: use threadlocal.address intrinsic to access TLS #139385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions compiler/rustc_codegen_llvm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1454,9 +1454,15 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
impl<'ll> StaticBuilderMethods for Builder<'_, 'll, '_> {
fn get_static(&mut self, def_id: DefId) -> &'ll Value {
// Forward to the `get_static` method of `CodegenCx`
let s = self.cx().get_static(def_id);
// Cast to default address space if globals are in a different addrspace
self.cx().const_pointercast(s, self.type_ptr())
let global = self.cx().get_static(def_id);
if self.cx().tcx.is_thread_local_static(def_id) {
let pointer = self.call_intrinsic("llvm.threadlocal.address", &[global]);
// Cast to default address space if globals are in a different addrspace
self.pointercast(pointer, self.type_ptr())
} else {
// Cast to default address space if globals are in a different addrspace
self.cx().const_pointercast(global, self.type_ptr())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would have been fine to use pointercast for both cases, but this is ok as well.

}
}
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_codegen_llvm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,7 @@ impl<'ll> CodegenCx<'ll, '_> {
}

ifn!("llvm.ptrmask", fn(ptr, t_isize) -> ptr);
ifn!("llvm.threadlocal.address", fn(ptr) -> ptr);

None
}
Expand Down
16 changes: 10 additions & 6 deletions tests/codegen/thread-local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,41 @@ use std::cell::Cell;

thread_local!(static A: Cell<u32> = const { Cell::new(1) });

// CHECK: [[TLS_AUX:@.+]] = external thread_local local_unnamed_addr global i64
// CHECK: [[TLS:@.+]] = internal thread_local unnamed_addr global
// CHECK: [[TLS_AUX:@.+]] = external thread_local{{.*}} global i64
// CHECK: [[TLS:@.+]] = internal thread_local{{.*}} global

// CHECK-LABEL: @get
#[no_mangle]
fn get() -> u32 {
// CHECK: [[RET_0:%.+]] = load i32, {{.*}}[[TLS]]{{.*}}
// CHECK: [[PTR:%.+]] = tail call {{.*}} ptr @llvm.threadlocal.address.p0(ptr [[TLS]])
// CHECK-NEXT: [[RET_0:%.+]] = load i32, ptr [[PTR]]
// CHECK-NEXT: ret i32 [[RET_0]]
A.with(|a| a.get())
}

// CHECK-LABEL: @set
#[no_mangle]
fn set(v: u32) {
// CHECK: store i32 %0, {{.*}}[[TLS]]{{.*}}
// CHECK: [[PTR:%.+]] = tail call {{.*}} ptr @llvm.threadlocal.address.p0(ptr [[TLS]])
// CHECK-NEXT: store i32 %0, ptr [[PTR]]
// CHECK-NEXT: ret void
A.with(|a| a.set(v))
}

// CHECK-LABEL: @get_aux
#[no_mangle]
fn get_aux() -> u64 {
// CHECK: [[RET_1:%.+]] = load i64, {{.*}}[[TLS_AUX]]
// CHECK: [[PTR:%.+]] = tail call {{.*}} ptr @llvm.threadlocal.address.p0(ptr [[TLS_AUX]])
// CHECK-NEXT: [[RET_1:%.+]] = load i64, ptr [[PTR]]
// CHECK-NEXT: ret i64 [[RET_1]]
aux::A.with(|a| a.get())
}

// CHECK-LABEL: @set_aux
#[no_mangle]
fn set_aux(v: u64) {
// CHECK: store i64 %0, {{.*}}[[TLS_AUX]]
// CHECK: [[PTR:%.+]] = tail call {{.*}} ptr @llvm.threadlocal.address.p0(ptr [[TLS_AUX]])
// CHECK-NEXT: store i64 %0, ptr [[PTR]]
// CHECK-NEXT: ret void
aux::A.with(|a| a.set(v))
}
Loading