Skip to content

Commit c354509

Browse files
committed
Auto merge of #71620 - Dylan-DPC:rollup-9wgtisb, r=Dylan-DPC
Rollup of 7 pull requests Successful merges: - #67841 (Add Read/Write::can_read/write_vectored) - #71524 (Minimize parameter of coerce_borrowed_pointer()) - #71558 (Cleanup and document `-Z tls-model` ) - #71578 (linkchecker: fix typo in main.rs) - #71596 (Fix broken link in `QPath` documentation) - #71604 (make recursive-zst test unleashed) - #71605 (No need to whitelist E0750 anymore) Failed merges: r? @ghost
2 parents 2d03399 + c890912 commit c354509

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+723
-62
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# `tls_model`
2+
3+
The tracking issue for this feature is: None.
4+
5+
------------------------
6+
7+
Option `-Z tls-model` controls [TLS model](https://www.akkadia.org/drepper/tls.pdf) used to
8+
generate code for accessing `#[thread_local]` `static` items.
9+
10+
Supported values for this option are:
11+
12+
- `global-dynamic` - General Dynamic TLS Model (alternatively called Global Dynamic) is the most
13+
general option usable in all circumstances, even if the TLS data is defined in a shared library
14+
loaded at runtime and is accessed from code outside of that library.
15+
This is the default for most targets.
16+
- `local-dynamic` - model usable if the TLS data is only accessed from the shared library or
17+
executable it is defined in. The TLS data may be in a library loaded after startup (via `dlopen`).
18+
- `initial-exec` - model usable if the TLS data is defined in the executable or in a shared library
19+
loaded at program startup.
20+
The TLS data must not be in a library loaded after startup (via `dlopen`).
21+
- `local-exec` - model usable only if the TLS data is defined directly in the executable,
22+
but not in a shared library, and is accessed only from that executable.
23+
24+
`rustc` and LLVM may use a more optimized model than specified if they know that we are producing
25+
and executable rather than a library, or that the `static` item is private enough.

src/librustc_codegen_llvm/back/write.rs

-7
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,6 @@ pub const CODE_GEN_MODEL_ARGS: &[(&str, llvm::CodeModel)] = &[
4343
("large", llvm::CodeModel::Large),
4444
];
4545

46-
pub const TLS_MODEL_ARGS: [(&str, llvm::ThreadLocalMode); 4] = [
47-
("global-dynamic", llvm::ThreadLocalMode::GeneralDynamic),
48-
("local-dynamic", llvm::ThreadLocalMode::LocalDynamic),
49-
("initial-exec", llvm::ThreadLocalMode::InitialExec),
50-
("local-exec", llvm::ThreadLocalMode::LocalExec),
51-
];
52-
5346
pub fn llvm_err(handler: &rustc_errors::Handler, msg: &str) -> FatalError {
5447
match llvm::last_error() {
5548
Some(err) => handler.fatal(&format!("{}: {}", msg, err)),

src/librustc_codegen_llvm/context.rs

+8-15
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_session::Session;
2121
use rustc_span::source_map::{Span, DUMMY_SP};
2222
use rustc_span::symbol::Symbol;
2323
use rustc_target::abi::{HasDataLayout, LayoutOf, PointeeInfo, Size, TargetDataLayout, VariantIdx};
24-
use rustc_target::spec::{HasTargetSpec, RelocModel, Target};
24+
use rustc_target::spec::{HasTargetSpec, RelocModel, Target, TlsModel};
2525

2626
use std::cell::{Cell, RefCell};
2727
use std::ffi::CStr;
@@ -87,19 +87,12 @@ pub struct CodegenCx<'ll, 'tcx> {
8787
local_gen_sym_counter: Cell<usize>,
8888
}
8989

90-
fn get_tls_model(sess: &Session) -> llvm::ThreadLocalMode {
91-
let tls_model_arg = match sess.opts.debugging_opts.tls_model {
92-
Some(ref s) => &s[..],
93-
None => &sess.target.target.options.tls_model[..],
94-
};
95-
96-
match crate::back::write::TLS_MODEL_ARGS.iter().find(|&&arg| arg.0 == tls_model_arg) {
97-
Some(x) => x.1,
98-
_ => {
99-
sess.err(&format!("{:?} is not a valid TLS model", tls_model_arg));
100-
sess.abort_if_errors();
101-
bug!();
102-
}
90+
fn to_llvm_tls_model(tls_model: TlsModel) -> llvm::ThreadLocalMode {
91+
match tls_model {
92+
TlsModel::GeneralDynamic => llvm::ThreadLocalMode::GeneralDynamic,
93+
TlsModel::LocalDynamic => llvm::ThreadLocalMode::LocalDynamic,
94+
TlsModel::InitialExec => llvm::ThreadLocalMode::InitialExec,
95+
TlsModel::LocalExec => llvm::ThreadLocalMode::LocalExec,
10396
}
10497
}
10598

@@ -267,7 +260,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
267260

268261
let check_overflow = tcx.sess.overflow_checks();
269262

270-
let tls_model = get_tls_model(&tcx.sess);
263+
let tls_model = to_llvm_tls_model(tcx.sess.tls_model());
271264

272265
let (llcx, llmod) = (&*llvm_module.llcx, llvm_module.llmod());
273266

src/librustc_codegen_llvm/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ impl CodegenBackend for LlvmCodegenBackend {
216216
}
217217
PrintRequest::TlsModels => {
218218
println!("Available TLS models:");
219-
for &(name, _) in back::write::TLS_MODEL_ARGS.iter() {
219+
for name in &["global-dynamic", "local-dynamic", "initial-exec", "local-exec"] {
220220
println!(" {}", name);
221221
}
222222
println!();

src/librustc_hir/hir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1601,7 +1601,7 @@ pub enum ExprKind<'hir> {
16011601
///
16021602
/// To resolve the path to a `DefId`, call [`qpath_res`].
16031603
///
1604-
/// [`qpath_res`]: ../ty/struct.TypeckTables.html#method.qpath_res
1604+
/// [`qpath_res`]: ../rustc_middle/ty/struct.TypeckTables.html#method.qpath_res
16051605
#[derive(RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
16061606
pub enum QPath<'hir> {
16071607
/// Path to a definition, optionally "fully-qualified" with a `Self`

src/librustc_interface/tests.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ use rustc_session::{build_session, Session};
1414
use rustc_span::edition::{Edition, DEFAULT_EDITION};
1515
use rustc_span::symbol::sym;
1616
use rustc_span::SourceFileHashAlgorithm;
17-
use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelocModel, RelroLevel};
17+
use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy};
18+
use rustc_target::spec::{RelocModel, RelroLevel, TlsModel};
1819
use std::collections::{BTreeMap, BTreeSet};
1920
use std::iter::FromIterator;
2021
use std::path::PathBuf;
@@ -567,7 +568,7 @@ fn test_debugging_options_tracking_hash() {
567568
tracked!(symbol_mangling_version, SymbolManglingVersion::V0);
568569
tracked!(teach, true);
569570
tracked!(thinlto, Some(true));
570-
tracked!(tls_model, Some(String::from("tls model")));
571+
tracked!(tls_model, Some(TlsModel::GeneralDynamic));
571572
tracked!(treat_err_as_bug, Some(1));
572573
tracked!(unleash_the_miri_inside_of_you, true);
573574
tracked!(verify_llvm_ir, true);

src/librustc_session/config.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1315,10 +1315,6 @@ fn collect_print_requests(
13151315
prints.push(PrintRequest::CodeModels);
13161316
cg.code_model = None;
13171317
}
1318-
if dopts.tls_model.as_ref().map_or(false, |s| s == "help") {
1319-
prints.push(PrintRequest::TlsModels);
1320-
dopts.tls_model = None;
1321-
}
13221318

13231319
prints.extend(matches.opt_strs("print").into_iter().map(|s| match &*s {
13241320
"crate-name" => PrintRequest::CrateName,
@@ -2001,7 +1997,8 @@ crate mod dep_tracking {
20011997
use crate::utils::NativeLibraryKind;
20021998
use rustc_feature::UnstableFeatures;
20031999
use rustc_span::edition::Edition;
2004-
use rustc_target::spec::{MergeFunctions, PanicStrategy, RelocModel, RelroLevel, TargetTriple};
2000+
use rustc_target::spec::{MergeFunctions, PanicStrategy, RelocModel};
2001+
use rustc_target::spec::{RelroLevel, TargetTriple, TlsModel};
20052002
use std::collections::hash_map::DefaultHasher;
20062003
use std::collections::BTreeMap;
20072004
use std::hash::Hash;
@@ -2050,6 +2047,7 @@ crate mod dep_tracking {
20502047
impl_dep_tracking_hash_via_hash!(Option<Vec<String>>);
20512048
impl_dep_tracking_hash_via_hash!(Option<MergeFunctions>);
20522049
impl_dep_tracking_hash_via_hash!(Option<RelocModel>);
2050+
impl_dep_tracking_hash_via_hash!(Option<TlsModel>);
20532051
impl_dep_tracking_hash_via_hash!(Option<PanicStrategy>);
20542052
impl_dep_tracking_hash_via_hash!(Option<RelroLevel>);
20552053
impl_dep_tracking_hash_via_hash!(Option<lint::Level>);

src/librustc_session/options.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use crate::search_paths::SearchPath;
66
use crate::utils::NativeLibraryKind;
77

88
use rustc_target::spec::TargetTriple;
9-
use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelocModel, RelroLevel};
9+
use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy};
10+
use rustc_target::spec::{RelocModel, RelroLevel, TlsModel};
1011

1112
use rustc_feature::UnstableFeatures;
1213
use rustc_span::edition::Edition;
@@ -267,6 +268,8 @@ macro_rules! options {
267268
pub const parse_src_file_hash: &str = "either `md5` or `sha1`";
268269
pub const parse_relocation_model: &str =
269270
"one of supported relocation models (`rustc --print relocation-models`)";
271+
pub const parse_tls_model: &str =
272+
"one of supported TLS models (`rustc --print tls-models`)";
270273
}
271274

272275
#[allow(dead_code)]
@@ -606,6 +609,14 @@ macro_rules! options {
606609
true
607610
}
608611

612+
fn parse_tls_model(slot: &mut Option<TlsModel>, v: Option<&str>) -> bool {
613+
match v.and_then(|s| TlsModel::from_str(s).ok()) {
614+
Some(tls_model) => *slot = Some(tls_model),
615+
_ => return false,
616+
}
617+
true
618+
}
619+
609620
fn parse_symbol_mangling_version(
610621
slot: &mut SymbolManglingVersion,
611622
v: Option<&str>,
@@ -977,7 +988,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
977988
"measure time of each LLVM pass (default: no)"),
978989
time_passes: bool = (false, parse_bool, [UNTRACKED],
979990
"measure time of each rustc pass (default: no)"),
980-
tls_model: Option<String> = (None, parse_opt_string, [TRACKED],
991+
tls_model: Option<TlsModel> = (None, parse_tls_model, [TRACKED],
981992
"choose the TLS model to use (`rustc --print tls-models` for details)"),
982993
trace_macros: bool = (false, parse_bool, [UNTRACKED],
983994
"for every macro invocation, print its name and arguments (default: no)"),

src/librustc_session/session.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticId, ErrorReported
2222
use rustc_span::edition::Edition;
2323
use rustc_span::source_map::{self, FileLoader, MultiSpan, RealFileLoader, SourceMap, Span};
2424
use rustc_span::SourceFileHashAlgorithm;
25-
use rustc_target::spec::{PanicStrategy, RelocModel, RelroLevel, Target, TargetTriple};
25+
use rustc_target::spec::{PanicStrategy, RelocModel, RelroLevel, Target, TargetTriple, TlsModel};
2626

2727
use std::cell::{self, RefCell};
2828
use std::env;
@@ -588,6 +588,10 @@ impl Session {
588588
self.opts.cg.relocation_model.unwrap_or(self.target.target.options.relocation_model)
589589
}
590590

591+
pub fn tls_model(&self) -> TlsModel {
592+
self.opts.debugging_opts.tls_model.unwrap_or(self.target.target.options.tls_model)
593+
}
594+
591595
pub fn must_not_eliminate_frame_pointers(&self) -> bool {
592596
// "mcount" function relies on stack pointer.
593597
// See <https://sourceware.org/binutils/docs/gprof/Implementation.html>.

src/librustc_target/spec/cloudabi_base.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::spec::{LinkArgs, LinkerFlavor, RelroLevel, TargetOptions};
1+
use crate::spec::{LinkArgs, LinkerFlavor, RelroLevel, TargetOptions, TlsModel};
22

33
pub fn opts() -> TargetOptions {
44
let mut args = LinkArgs::new();
@@ -29,7 +29,7 @@ pub fn opts() -> TargetOptions {
2929
// (Global Offset Table) to obtain the effective address of a
3030
// thread-local variable. Using a GOT is useful only when doing
3131
// dynamic linking.
32-
tls_model: "local-exec".to_string(),
32+
tls_model: TlsModel::LocalExec,
3333
relro_level: RelroLevel::Full,
3434
..Default::default()
3535
}

src/librustc_target/spec/hermit_base.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::spec::{LinkArgs, LinkerFlavor, LldFlavor, PanicStrategy, RelocModel, TargetOptions};
1+
use crate::spec::{LinkArgs, LinkerFlavor, LldFlavor, PanicStrategy};
2+
use crate::spec::{RelocModel, TargetOptions, TlsModel};
23

34
pub fn opts() -> TargetOptions {
45
let mut pre_link_args = LinkArgs::new();
@@ -17,7 +18,7 @@ pub fn opts() -> TargetOptions {
1718
position_independent_executables: true,
1819
relocation_model: RelocModel::Static,
1920
target_family: None,
20-
tls_model: "initial-exec".to_string(),
21+
tls_model: TlsModel::InitialExec,
2122
..Default::default()
2223
}
2324
}

src/librustc_target/spec/hermit_kernel_base.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::spec::{LinkArgs, LinkerFlavor, LldFlavor, PanicStrategy, RelocModel, TargetOptions};
1+
use crate::spec::{LinkArgs, LinkerFlavor, LldFlavor, PanicStrategy};
2+
use crate::spec::{RelocModel, TargetOptions, TlsModel};
23

34
pub fn opts() -> TargetOptions {
45
let mut pre_link_args = LinkArgs::new();
@@ -18,7 +19,7 @@ pub fn opts() -> TargetOptions {
1819
position_independent_executables: true,
1920
relocation_model: RelocModel::Static,
2021
target_family: None,
21-
tls_model: "initial-exec".to_string(),
22+
tls_model: TlsModel::InitialExec,
2223
..Default::default()
2324
}
2425
}

src/librustc_target/spec/mod.rs

+51-3
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,42 @@ impl ToJson for RelocModel {
305305
}
306306
}
307307

308+
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
309+
pub enum TlsModel {
310+
GeneralDynamic,
311+
LocalDynamic,
312+
InitialExec,
313+
LocalExec,
314+
}
315+
316+
impl FromStr for TlsModel {
317+
type Err = ();
318+
319+
fn from_str(s: &str) -> Result<TlsModel, ()> {
320+
Ok(match s {
321+
// Note the difference "general" vs "global" difference. The model name is "general",
322+
// but the user-facing option name is "global" for consistency with other compilers.
323+
"global-dynamic" => TlsModel::GeneralDynamic,
324+
"local-dynamic" => TlsModel::LocalDynamic,
325+
"initial-exec" => TlsModel::InitialExec,
326+
"local-exec" => TlsModel::LocalExec,
327+
_ => return Err(()),
328+
})
329+
}
330+
}
331+
332+
impl ToJson for TlsModel {
333+
fn to_json(&self) -> Json {
334+
match *self {
335+
TlsModel::GeneralDynamic => "global-dynamic",
336+
TlsModel::LocalDynamic => "local-dynamic",
337+
TlsModel::InitialExec => "initial-exec",
338+
TlsModel::LocalExec => "local-exec",
339+
}
340+
.to_json()
341+
}
342+
}
343+
308344
pub enum LoadTargetError {
309345
BuiltinTargetNotFound(String),
310346
Other(String),
@@ -660,7 +696,7 @@ pub struct TargetOptions {
660696
pub code_model: Option<String>,
661697
/// TLS model to use. Options are "global-dynamic" (default), "local-dynamic", "initial-exec"
662698
/// and "local-exec". This is similar to the -ftls-model option in GCC/Clang.
663-
pub tls_model: String,
699+
pub tls_model: TlsModel,
664700
/// Do not emit code that uses the "red zone", if the ABI has one. Defaults to false.
665701
pub disable_redzone: bool,
666702
/// Eliminate frame pointers from stack frames if possible. Defaults to true.
@@ -863,7 +899,7 @@ impl Default for TargetOptions {
863899
executables: false,
864900
relocation_model: RelocModel::Pic,
865901
code_model: None,
866-
tls_model: "global-dynamic".to_string(),
902+
tls_model: TlsModel::GeneralDynamic,
867903
disable_redzone: false,
868904
eliminate_frame_pointer: true,
869905
function_sections: true,
@@ -1060,6 +1096,18 @@ impl Target {
10601096
Some(Ok(()))
10611097
})).unwrap_or(Ok(()))
10621098
} );
1099+
($key_name:ident, TlsModel) => ( {
1100+
let name = (stringify!($key_name)).replace("_", "-");
1101+
obj.find(&name[..]).and_then(|o| o.as_string().and_then(|s| {
1102+
match s.parse::<TlsModel>() {
1103+
Ok(tls_model) => base.options.$key_name = tls_model,
1104+
_ => return Some(Err(format!("'{}' is not a valid TLS model. \
1105+
Run `rustc --print tls-models` to \
1106+
see the list of supported values.", s))),
1107+
}
1108+
Some(Ok(()))
1109+
})).unwrap_or(Ok(()))
1110+
} );
10631111
($key_name:ident, PanicStrategy) => ( {
10641112
let name = (stringify!($key_name)).replace("_", "-");
10651113
obj.find(&name[..]).and_then(|o| o.as_string().and_then(|s| {
@@ -1200,7 +1248,7 @@ impl Target {
12001248
key!(executables, bool);
12011249
key!(relocation_model, RelocModel)?;
12021250
key!(code_model, optional);
1203-
key!(tls_model);
1251+
key!(tls_model, TlsModel)?;
12041252
key!(disable_redzone, bool);
12051253
key!(eliminate_frame_pointer, bool);
12061254
key!(function_sections, bool);

src/librustc_target/spec/wasm32_base.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel, TargetOptions};
1+
use super::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel, TargetOptions, TlsModel};
22
use std::collections::BTreeMap;
33

44
pub fn options() -> TargetOptions {
@@ -138,7 +138,7 @@ pub fn options() -> TargetOptions {
138138
// `has_elf_tls`) and we need to get it to work by specifying
139139
// `local-exec` as that's all that's implemented in LLVM today for wasm.
140140
has_elf_tls: true,
141-
tls_model: "local-exec".to_string(),
141+
tls_model: TlsModel::LocalExec,
142142

143143
// gdb scripts don't work on wasm blobs
144144
emit_debug_gdb_scripts: false,

0 commit comments

Comments
 (0)