Skip to content

Commit

Permalink
Auto merge of rust-lang#117873 - quininer:android-emutls, r=Amanieu
Browse files Browse the repository at this point in the history
Use emulated TLS for android

This is a reopen of rust-lang#96317 . many android devices still only use 128 pthread keys, so using emutls can be helpful.

r? `@Amanieu`
  • Loading branch information
bors committed Nov 29, 2023
2 parents abe34e9 + 9981450 commit d638ba2
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 26 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_codegen_gcc/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,5 +569,6 @@ fn to_gcc_tls_mode(tls_model: TlsModel) -> gccjit::TlsModel {
TlsModel::LocalDynamic => gccjit::TlsModel::LocalDynamic,
TlsModel::InitialExec => gccjit::TlsModel::InitialExec,
TlsModel::LocalExec => gccjit::TlsModel::LocalExec,
TlsModel::EmulatedTls => gccjit::TlsModel::GlobalDynamic,
}
}
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_llvm/src/back/owned_target_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl OwnedTargetMachine {
split_dwarf_file: &CStr,
output_obj_file: &CStr,
debug_info_compression: &CStr,
force_emulated_tls: bool,
use_emulated_tls: bool,
args_cstr_buff: &[u8],
) -> Result<Self, LlvmError<'static>> {
assert!(args_cstr_buff.len() > 0);
Expand Down Expand Up @@ -71,7 +71,7 @@ impl OwnedTargetMachine {
split_dwarf_file.as_ptr(),
output_obj_file.as_ptr(),
debug_info_compression.as_ptr(),
force_emulated_tls,
use_emulated_tls,
args_cstr_buff.as_ptr() as *const c_char,
args_cstr_buff.len(),
)
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_codegen_llvm/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use rustc_session::config::{self, Lto, OutputType, Passes, SplitDwarfKind, Switc
use rustc_session::Session;
use rustc_span::symbol::sym;
use rustc_span::InnerSpan;
use rustc_target::spec::{CodeModel, RelocModel, SanitizerSet, SplitDebuginfo};
use rustc_target::spec::{CodeModel, RelocModel, SanitizerSet, SplitDebuginfo, TlsModel};

use crate::llvm::diagnostic::OptimizationDiagnosticKind;
use libc::{c_char, c_int, c_uint, c_void, size_t};
Expand Down Expand Up @@ -223,7 +223,7 @@ pub fn target_machine_factory(

let path_mapping = sess.source_map().path_mapping().clone();

let force_emulated_tls = sess.target.force_emulated_tls;
let use_emulated_tls = matches!(sess.tls_model(), TlsModel::EmulatedTls);

// copy the exe path, followed by path all into one buffer
// null terminating them so we can use them as null terminated strings
Expand Down Expand Up @@ -297,7 +297,7 @@ pub fn target_machine_factory(
&split_dwarf_file,
&output_obj_file,
&debuginfo_compression,
force_emulated_tls,
use_emulated_tls,
&args_cstr_buff,
)
})
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 @@ -120,6 +120,7 @@ fn to_llvm_tls_model(tls_model: TlsModel) -> llvm::ThreadLocalMode {
TlsModel::LocalDynamic => llvm::ThreadLocalMode::LocalDynamic,
TlsModel::InitialExec => llvm::ThreadLocalMode::InitialExec,
TlsModel::LocalExec => llvm::ThreadLocalMode::LocalExec,
TlsModel::EmulatedTls => llvm::ThreadLocalMode::GeneralDynamic,
}
}

Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_codegen_llvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,13 @@ impl CodegenBackend for LlvmCodegenBackend {
}
PrintKind::TlsModels => {
writeln!(out, "Available TLS models:");
for name in &["global-dynamic", "local-dynamic", "initial-exec", "local-exec"] {
for name in &[
"global-dynamic",
"local-dynamic",
"initial-exec",
"local-exec",
"emulated-tls",
] {
writeln!(out, " {name}");
}
writeln!(out);
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1748,7 +1748,9 @@ fn exported_symbols_for_non_proc_macro(tcx: TyCtxt<'_>, crate_type: CrateType) -
let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);
for_each_exported_symbols_include_dep(tcx, crate_type, |symbol, info, cnum| {
if info.level.is_below_threshold(export_threshold) {
symbols.push(symbol_export::symbol_name_for_instance_in_crate(tcx, symbol, cnum));
symbols.push(symbol_export::exporting_symbol_name_for_instance_in_crate(
tcx, symbol, cnum,
));
}
});

Expand Down
21 changes: 20 additions & 1 deletion compiler/rustc_codegen_ssa/src/back/symbol_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_middle::ty::{self, SymbolName, TyCtxt};
use rustc_middle::ty::{GenericArgKind, GenericArgsRef};
use rustc_middle::util::Providers;
use rustc_session::config::{CrateType, OomStrategy};
use rustc_target::spec::SanitizerSet;
use rustc_target::spec::{SanitizerSet, TlsModel};

pub fn threshold(tcx: TyCtxt<'_>) -> SymbolExportLevel {
crates_export_threshold(tcx.crate_types())
Expand Down Expand Up @@ -608,6 +608,25 @@ pub fn linking_symbol_name_for_instance_in_crate<'tcx>(
format!("{prefix}{undecorated}{suffix}{args_in_bytes}")
}

pub fn exporting_symbol_name_for_instance_in_crate<'tcx>(
tcx: TyCtxt<'tcx>,
symbol: ExportedSymbol<'tcx>,
cnum: CrateNum,
) -> String {
let undecorated = symbol_name_for_instance_in_crate(tcx, symbol, cnum);

if matches!(tcx.sess.tls_model(), TlsModel::EmulatedTls)
&& let ExportedSymbol::NonGeneric(def_id) = symbol
&& tcx.is_thread_local_static(def_id)
{
// When using emutls, LLVM will add the `__emutls_v.` prefix to thread local symbols,
// and exported symbol name need to match this.
format!("__emutls_v.{undecorated}")
} else {
undecorated
}
}

fn wasm_import_module_map(tcx: TyCtxt<'_>, cnum: CrateNum) -> FxHashMap<DefId, String> {
// Build up a map from DefId to a `NativeLib` structure, where
// `NativeLib` internally contains information about
Expand Down
10 changes: 3 additions & 7 deletions compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
const char *SplitDwarfFile,
const char *OutputObjFile,
const char *DebugInfoCompression,
bool ForceEmulatedTls,
bool UseEmulatedTls,
const char *ArgsCstrBuff, size_t ArgsCstrBuffLen) {

auto OptLevel = fromRust(RustOptLevel);
Expand Down Expand Up @@ -456,13 +456,9 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
Options.UseInitArray = UseInitArray;

#if LLVM_VERSION_LT(17, 0)
if (ForceEmulatedTls) {
Options.ExplicitEmulatedTLS = true;
Options.EmulatedTLS = true;
}
#else
Options.EmulatedTLS = ForceEmulatedTls || Trip.hasDefaultEmulatedTLS();
Options.ExplicitEmulatedTLS = true;
#endif
Options.EmulatedTLS = UseEmulatedTls;

if (TrapUnreachable) {
// Tell LLVM to codegen `unreachable` into an explicit trap instruction.
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_target/src/spec/base/android.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::spec::{base, SanitizerSet, TargetOptions};
use crate::spec::{base, SanitizerSet, TargetOptions, TlsModel};

pub fn opts() -> TargetOptions {
let mut base = base::linux::opts();
base.os = "android".into();
base.is_like_android = true;
base.default_dwarf_version = 2;
base.has_thread_local = false;
base.tls_model = TlsModel::EmulatedTls;
base.has_thread_local = true;
base.supported_sanitizers = SanitizerSet::ADDRESS;
// This is for backward compatibility, see https://github.com/rust-lang/rust/issues/49867
// for context. (At that time, there was no `-C force-unwind-tables`, so the only solution
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_target/src/spec/base/linux_ohos.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::spec::{base, TargetOptions};
use crate::spec::{base, TargetOptions, TlsModel};

pub fn opts() -> TargetOptions {
let mut base = base::linux::opts();

base.env = "ohos".into();
base.crt_static_default = false;
base.force_emulated_tls = true;
base.tls_model = TlsModel::EmulatedTls;
base.has_thread_local = false;

base
Expand Down
9 changes: 3 additions & 6 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,7 @@ pub enum TlsModel {
LocalDynamic,
InitialExec,
LocalExec,
EmulatedTls,
}

impl FromStr for TlsModel {
Expand All @@ -942,6 +943,7 @@ impl FromStr for TlsModel {
"local-dynamic" => TlsModel::LocalDynamic,
"initial-exec" => TlsModel::InitialExec,
"local-exec" => TlsModel::LocalExec,
"emulated-tls" => TlsModel::EmulatedTls,
_ => return Err(()),
})
}
Expand All @@ -954,6 +956,7 @@ impl ToJson for TlsModel {
TlsModel::LocalDynamic => "local-dynamic",
TlsModel::InitialExec => "initial-exec",
TlsModel::LocalExec => "local-exec",
TlsModel::EmulatedTls => "emulated-tls",
}
.to_json()
}
Expand Down Expand Up @@ -2190,9 +2193,6 @@ pub struct TargetOptions {

/// Whether the target supports XRay instrumentation.
pub supports_xray: bool,

/// Forces the use of emulated TLS (__emutls_get_address)
pub force_emulated_tls: bool,
}

/// Add arguments for the given flavor and also for its "twin" flavors
Expand Down Expand Up @@ -2408,7 +2408,6 @@ impl Default for TargetOptions {
entry_name: "main".into(),
entry_abi: Conv::C,
supports_xray: false,
force_emulated_tls: false,
}
}
}
Expand Down Expand Up @@ -3112,7 +3111,6 @@ impl Target {
key!(entry_name);
key!(entry_abi, Conv)?;
key!(supports_xray, bool);
key!(force_emulated_tls, bool);

if base.is_builtin {
// This can cause unfortunate ICEs later down the line.
Expand Down Expand Up @@ -3368,7 +3366,6 @@ impl ToJson for Target {
target_option_val!(entry_name);
target_option_val!(entry_abi);
target_option_val!(supports_xray);
target_option_val!(force_emulated_tls);

if let Some(abi) = self.default_adjusted_cabi {
d.insert("default-adjusted-cabi".into(), Abi::name(abi).to_json());
Expand Down
8 changes: 7 additions & 1 deletion library/std/src/sys/unix/thread_local_dtor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
// compiling from a newer linux to an older linux, so we also have a
// fallback implementation to use as well.
#[allow(unexpected_cfgs)]
#[cfg(any(target_os = "linux", target_os = "fuchsia", target_os = "redox", target_os = "hurd"))]
#[cfg(any(
target_os = "linux",
target_os = "android",
target_os = "fuchsia",
target_os = "redox",
target_os = "hurd"
))]
// FIXME: The Rust compiler currently omits weakly function definitions (i.e.,
// __cxa_thread_atexit_impl) and its metadata from LLVM IR.
#[no_sanitize(cfi, kcfi)]
Expand Down
2 changes: 2 additions & 0 deletions src/doc/unstable-book/src/compiler-flags/tls-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ loaded at program startup.
The TLS data must not be in a library loaded after startup (via `dlopen`).
- `local-exec` - model usable only if the TLS data is defined directly in the executable,
but not in a shared library, and is accessed only from that executable.
- `emulated-tls` - Uses thread-specific data keys to implement emulated TLS.
It is like using a general-dynamic TLS model for all modes.

`rustc` and LLVM may use a more optimized model than specified if they know that we are producing
an executable rather than a library, or that the `static` item is private enough.

0 comments on commit d638ba2

Please sign in to comment.