Skip to content

Commit

Permalink
Don't use thread local shims for foreign thread locals
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Feb 22, 2023
1 parent cf3683f commit e61ac42
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/back/symbol_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ fn exported_symbols_provider_local(
// Export TLS shims
if !tcx.sess.target.dll_tls_export {
symbols.extend(sorted.iter().filter_map(|(&def_id, &info)| {
tcx.is_thread_local_static(def_id).then(|| {
tcx.needs_thread_local_shim(def_id).then(|| {
(
ExportedSymbol::ThreadLocalShim(def_id),
SymbolExportInfo {
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_ssa/src/mir/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
mir::Rvalue::ThreadLocalRef(def_id) => {
assert!(bx.cx().tcx().is_static(def_id));
let layout = bx.layout_of(bx.cx().tcx().static_ptr_ty(def_id));
let static_ = if !def_id.is_local() && !bx.cx().tcx().sess.target.dll_tls_export {
let static_ = if !def_id.is_local() && bx.cx().tcx().needs_thread_local_shim(def_id)
{
let instance = ty::Instance {
def: ty::InstanceDef::ThreadLocalShim(def_id),
substs: ty::InternalSubsts::empty(),
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,15 @@ impl<'tcx> TyCtxt<'tcx> {
self.static_mutability(def_id) == Some(hir::Mutability::Mut)
}

/// Returns `true` if the item pointed to by `def_id` is a thread local which needs a
/// thread local shim generated.
#[inline]
pub fn needs_thread_local_shim(self, def_id: DefId) -> bool {
!self.sess.target.dll_tls_export
&& self.is_thread_local_static(def_id)
&& !self.is_foreign_item(def_id)
}

/// Returns the type a reference to the thread local takes in MIR.
pub fn thread_local_ptr_ty(self, def_id: DefId) -> Ty<'tcx> {
let static_ty = self.type_of(def_id).subst_identity();
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_monomorphize/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ fn collect_items_rec<'tcx>(
}
}

if !tcx.sess.target.dll_tls_export && tcx.is_thread_local_static(def_id) {
if tcx.needs_thread_local_shim(def_id) {
neighbors.push(respan(
starting_point.span,
MonoItem::Fn(Instance {
Expand Down

0 comments on commit e61ac42

Please sign in to comment.