Skip to content
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

Rollup of 7 pull requests #108935

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
52f7a21
Relax ordering rules for `asm!` operands
Amanieu Dec 16, 2022
be3452a
Test that TLS access works outside of the dylib it's defined in
Zoxc Feb 3, 2023
34c8708
Support TLS access into dylibs on Windows
Zoxc Feb 3, 2023
98d2669
Use #[inline] on Windows for thread local access
Zoxc Feb 15, 2023
c044df2
Only avoid shims for the local crate
Zoxc Feb 16, 2023
524d140
Lower TLS to calls for rvalues directly
Zoxc Feb 16, 2023
cf3683f
Address comments
Zoxc Feb 19, 2023
e61ac42
Don't use thread local shims for foreign thread locals
Zoxc Feb 19, 2023
6c9d750
Add cranelift support
Zoxc Feb 19, 2023
0f371ca
Support linking to rust dylibs from a staticlib
bjorn3 Dec 28, 2022
7e2f6c1
Support `--print native-static-libs` with rust dylibs
bjorn3 Dec 28, 2022
832fede
Add unstable feature flags
bjorn3 Jan 4, 2023
832b33e
Force parentheses around `match` expression in binary expression
bwmf2 Feb 26, 2023
cdeb0e3
Fix UI test
bwmf2 Feb 28, 2023
219195f
Add UI test
bwmf2 Feb 28, 2023
1ccb1de
Place size limits on query keys and values
Zoxc Mar 3, 2023
f1cf67b
Fix test for MSVC
bjorn3 Mar 4, 2023
de2e16c
Prevent the `start_bx` basic block in codegen from having two `Builde…
823984418 Mar 4, 2023
ef807cb
use problem matchers for tidy CI
fee1-dead Dec 23, 2022
a2040de
do not run tidy on x86_64-gnu-llvm-13
fee1-dead Jan 6, 2023
871b4fe
Add a comment about drop(start_bx)
823984418 Mar 8, 2023
bc50aff
Rollup merge of #105798 - Amanieu:relax-asm, r=joshtriplett
matthiaskrgr Mar 9, 2023
b224fe5
Rollup merge of #106085 - fee1-dead-contrib:tidy-ci-matchers, r=pietr…
matthiaskrgr Mar 9, 2023
3bb8516
Rollup merge of #106560 - bjorn3:support_staticlib_dylib_linking, r=p…
matthiaskrgr Mar 9, 2023
4bb0b2b
Rollup merge of #108089 - Zoxc:windows-tls, r=wesleywiser
matthiaskrgr Mar 9, 2023
d02ed0a
Rollup merge of #108542 - bwmf2:expanded, r=wesleywiser
matthiaskrgr Mar 9, 2023
80e1112
Rollup merge of #108690 - Zoxc:query-size-limits, r=cjgillot
matthiaskrgr Mar 9, 2023
ebfe8ac
Rollup merge of #108739 - 823984418:patch-1, r=cjgillot
matthiaskrgr Mar 9, 2023
4561b3b
tidy: bump ROOT_ENTRY_LIMIT by ten
matthiaskrgr Mar 8, 2023
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
4 changes: 4 additions & 0 deletions compiler/rustc_ast_pretty/src/pprust/state/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ impl<'a> State<'a> {
(&ast::ExprKind::Let { .. }, _) if !parser::needs_par_as_let_scrutinee(prec) => {
parser::PREC_FORCE_PAREN
}
// For a binary expression like `(match () { _ => a }) OP b`, the parens are required
// otherwise the parser would interpret `match () { _ => a }` as a statement,
// with the remaining `OP b` not making sense. So we force parens.
(&ast::ExprKind::Match(..), _) => parser::PREC_FORCE_PAREN,
_ => left_prec,
};

Expand Down
35 changes: 4 additions & 31 deletions compiler/rustc_builtin_macros/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,6 @@ pub fn parse_asm_args<'a>(
// Validate the order of named, positional & explicit register operands and
// clobber_abi/options. We do this at the end once we have the full span
// of the argument available.
if !args.options_spans.is_empty() {
diag.struct_span_err(span, "arguments are not allowed after options")
.span_labels(args.options_spans.clone(), "previous options")
.span_label(span, "argument")
.emit();
} else if let Some((_, abi_span)) = args.clobber_abis.last() {
diag.struct_span_err(span, "arguments are not allowed after clobber_abi")
.span_label(*abi_span, "clobber_abi")
.span_label(span, "argument")
.emit();
}
if explicit_reg {
if name.is_some() {
diag.struct_span_err(span, "explicit register arguments cannot have names").emit();
Expand All @@ -227,17 +216,6 @@ pub fn parse_asm_args<'a>(
.emit();
continue;
}
if !args.reg_args.is_empty() {
let mut err = diag.struct_span_err(
span,
"named arguments cannot follow explicit register arguments",
);
err.span_label(span, "named argument");
for pos in &args.reg_args {
err.span_label(args.operands[*pos].1, "explicit register argument");
}
err.emit();
}
args.named_args.insert(name, slot);
} else {
if !args.named_args.is_empty() || !args.reg_args.is_empty() {
Expand Down Expand Up @@ -478,15 +456,6 @@ fn parse_clobber_abi<'a>(p: &mut Parser<'a>, args: &mut AsmArgs) -> PResult<'a,

let full_span = span_start.to(p.prev_token.span);

if !args.options_spans.is_empty() {
let mut err = p
.sess
.span_diagnostic
.struct_span_err(full_span, "clobber_abi is not allowed after options");
err.span_labels(args.options_spans.clone(), "options");
return Err(err);
}

match &new_abis[..] {
// should have errored above during parsing
[] => unreachable!(),
Expand Down Expand Up @@ -699,6 +668,10 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, args: AsmArgs) -> Option<ast::Inl
args.operands[idx].1,
"explicit register arguments cannot be used in the asm template",
);
err.span_help(
args.operands[idx].1,
"use the register name directly in the assembly code",
);
}
err.emit();
None
Expand Down
22 changes: 16 additions & 6 deletions compiler/rustc_codegen_cranelift/src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,22 @@ pub(crate) fn codegen_tls_ref<'tcx>(
def_id: DefId,
layout: TyAndLayout<'tcx>,
) -> CValue<'tcx> {
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
if fx.clif_comments.enabled() {
fx.add_comment(local_data_id, format!("tls {:?}", def_id));
}
let tls_ptr = fx.bcx.ins().tls_value(fx.pointer_type, local_data_id);
let tls_ptr = if !def_id.is_local() && fx.tcx.needs_thread_local_shim(def_id) {
let instance = ty::Instance {
def: ty::InstanceDef::ThreadLocalShim(def_id),
substs: ty::InternalSubsts::empty(),
};
let func_ref = fx.get_function_ref(instance);
let call = fx.bcx.ins().call(func_ref, &[]);
fx.bcx.func.dfg.first_result(call)
} else {
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
if fx.clif_comments.enabled() {
fx.add_comment(local_data_id, format!("tls {:?}", def_id));
}
fx.bcx.ins().tls_value(fx.pointer_type, local_data_id)
};
CValue::by_val(tls_ptr, layout)
}

Expand Down
75 changes: 70 additions & 5 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,12 +544,38 @@ fn link_staticlib<'a>(

ab.build(out_filename);

if !all_native_libs.is_empty() {
if sess.opts.prints.contains(&PrintRequest::NativeStaticLibs) {
print_native_static_libs(sess, &all_native_libs);
let crates = codegen_results.crate_info.used_crates.iter();

let fmts = codegen_results
.crate_info
.dependency_formats
.iter()
.find_map(|&(ty, ref list)| if ty == CrateType::Staticlib { Some(list) } else { None })
.expect("no dependency formats for staticlib");

let mut all_rust_dylibs = vec![];
for &cnum in crates {
match fmts.get(cnum.as_usize() - 1) {
Some(&Linkage::Dynamic) => {}
_ => continue,
}
let crate_name = codegen_results.crate_info.crate_name[&cnum];
let used_crate_source = &codegen_results.crate_info.used_crate_source[&cnum];
if let Some((path, _)) = &used_crate_source.dylib {
all_rust_dylibs.push(&**path);
} else {
if used_crate_source.rmeta.is_some() {
sess.emit_fatal(errors::LinkRlibError::OnlyRmetaFound { crate_name });
} else {
sess.emit_fatal(errors::LinkRlibError::NotFound { crate_name });
}
}
}

if sess.opts.prints.contains(&PrintRequest::NativeStaticLibs) {
print_native_static_libs(sess, &all_native_libs, &all_rust_dylibs);
}

Ok(())
}

Expand Down Expand Up @@ -1289,8 +1315,12 @@ enum RlibFlavor {
StaticlibBase,
}

fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLib]) {
let lib_args: Vec<_> = all_native_libs
fn print_native_static_libs(
sess: &Session,
all_native_libs: &[NativeLib],
all_rust_dylibs: &[&Path],
) {
let mut lib_args: Vec<_> = all_native_libs
.iter()
.filter(|l| relevant_lib(sess, l))
.filter_map(|lib| {
Expand Down Expand Up @@ -1319,6 +1349,41 @@ fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLib]) {
}
})
.collect();
for path in all_rust_dylibs {
// FIXME deduplicate with add_dynamic_crate

// Just need to tell the linker about where the library lives and
// what its name is
let parent = path.parent();
if let Some(dir) = parent {
let dir = fix_windows_verbatim_for_gcc(dir);
if sess.target.is_like_msvc {
let mut arg = String::from("/LIBPATH:");
arg.push_str(&dir.display().to_string());
lib_args.push(arg);
} else {
lib_args.push("-L".to_owned());
lib_args.push(dir.display().to_string());
}
}
let stem = path.file_stem().unwrap().to_str().unwrap();
// Convert library file-stem into a cc -l argument.
let prefix = if stem.starts_with("lib") && !sess.target.is_like_windows { 3 } else { 0 };
let lib = &stem[prefix..];
let path = parent.unwrap_or_else(|| Path::new(""));
if sess.target.is_like_msvc {
// When producing a dll, the MSVC linker may not actually emit a
// `foo.lib` file if the dll doesn't actually export any symbols, so we
// check to see if the file is there and just omit linking to it if it's
// not present.
let name = format!("{}.dll.lib", lib);
if path.join(&name).exists() {
lib_args.push(name);
}
} else {
lib_args.push(format!("-l{}", lib));
}
}
if !lib_args.is_empty() {
sess.emit_note(errors::StaticLibraryNativeArtifacts);
// Prefix for greppability
Expand Down
40 changes: 33 additions & 7 deletions compiler/rustc_codegen_ssa/src/back/symbol_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,29 @@ fn exported_symbols_provider_local(

// FIXME: Sorting this is unnecessary since we are sorting later anyway.
// Can we skip the later sorting?
let mut symbols: Vec<_> = tcx.with_stable_hashing_context(|hcx| {
tcx.reachable_non_generics(LOCAL_CRATE)
.to_sorted(&hcx, true)
.into_iter()
.map(|(&def_id, &info)| (ExportedSymbol::NonGeneric(def_id), info))
.collect()
let sorted = tcx.with_stable_hashing_context(|hcx| {
tcx.reachable_non_generics(LOCAL_CRATE).to_sorted(&hcx, true)
});

let mut symbols: Vec<_> =
sorted.iter().map(|(&def_id, &info)| (ExportedSymbol::NonGeneric(def_id), info)).collect();

// Export TLS shims
if !tcx.sess.target.dll_tls_export {
symbols.extend(sorted.iter().filter_map(|(&def_id, &info)| {
tcx.needs_thread_local_shim(def_id).then(|| {
(
ExportedSymbol::ThreadLocalShim(def_id),
SymbolExportInfo {
level: info.level,
kind: SymbolExportKind::Text,
used: info.used,
},
)
})
}))
}

if tcx.entry_fn(()).is_some() {
let exported_symbol =
ExportedSymbol::NoDefId(SymbolName::new(tcx, tcx.sess.target.entry_name.as_ref()));
Expand Down Expand Up @@ -380,7 +395,9 @@ fn upstream_monomorphizations_provider(
continue;
}
}
ExportedSymbol::NonGeneric(..) | ExportedSymbol::NoDefId(..) => {
ExportedSymbol::NonGeneric(..)
| ExportedSymbol::ThreadLocalShim(..)
| ExportedSymbol::NoDefId(..) => {
// These are no monomorphizations
continue;
}
Expand Down Expand Up @@ -500,6 +517,13 @@ pub fn symbol_name_for_instance_in_crate<'tcx>(
instantiating_crate,
)
}
ExportedSymbol::ThreadLocalShim(def_id) => {
rustc_symbol_mangling::symbol_name_for_instance_in_crate(
tcx,
Instance::new(def_id, ty::InternalSubsts::empty()),
instantiating_crate,
)
}
ExportedSymbol::DropGlue(ty) => rustc_symbol_mangling::symbol_name_for_instance_in_crate(
tcx,
Instance::resolve_drop_in_place(tcx, ty),
Expand Down Expand Up @@ -548,6 +572,8 @@ pub fn linking_symbol_name_for_instance_in_crate<'tcx>(
ExportedSymbol::DropGlue(..) => None,
// NoDefId always follow the target's default symbol decoration scheme.
ExportedSymbol::NoDefId(..) => None,
// ThreadLocalShim always follow the target's default symbol decoration scheme.
ExportedSymbol::ThreadLocalShim(..) => None,
};

let (conv, args) = instance
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_codegen_ssa/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
// Apply debuginfo to the newly allocated locals.
fx.debug_introduce_locals(&mut start_bx);

// The builders will be created separately for each basic block at `codegen_block`.
// So drop the builder of `start_llbb` to avoid having two at the same time.
drop(start_bx);

// Codegen the body of each block using reverse postorder
for (bb, _) in traversal::reverse_postorder(&mir) {
fx.codegen_block(bb);
Expand Down
14 changes: 13 additions & 1 deletion compiler/rustc_codegen_ssa/src/mir/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,20 @@ 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 static_ = bx.get_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().needs_thread_local_shim(def_id)
{
let instance = ty::Instance {
def: ty::InstanceDef::ThreadLocalShim(def_id),
substs: ty::InternalSubsts::empty(),
};
let fn_ptr = bx.get_fn_addr(instance);
let fn_abi = bx.fn_abi_of_instance(instance, ty::List::empty());
let fn_ty = bx.fn_decl_backend_type(&fn_abi);
bx.call(fn_ty, Some(fn_abi), fn_ptr, &[], None)
} else {
bx.get_static(def_id)
};
OperandRef { val: OperandValue::Immediate(static_), layout }
}
mir::Rvalue::Use(ref operand) => self.codegen_operand(bx, operand),
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_const_eval/src/interpret/terminator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
| ty::InstanceDef::FnPtrShim(..)
| ty::InstanceDef::DropGlue(..)
| ty::InstanceDef::CloneShim(..)
| ty::InstanceDef::ThreadLocalShim(..)
| ty::InstanceDef::Item(_) => {
// We need MIR for this fn
let Some((body, instance)) =
Expand Down
31 changes: 21 additions & 10 deletions compiler/rustc_metadata/src/dependency_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,25 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
// to try to eagerly statically link all dependencies. This is normally
// done for end-product dylibs, not intermediate products.
//
// Treat cdylibs similarly. If `-C prefer-dynamic` is set, the caller may
// be code-size conscious, but without it, it makes sense to statically
// link a cdylib.
CrateType::Dylib | CrateType::Cdylib if !sess.opts.cg.prefer_dynamic => Linkage::Static,
CrateType::Dylib | CrateType::Cdylib => Linkage::Dynamic,
// Treat cdylibs and staticlibs similarly. If `-C prefer-dynamic` is set,
// the caller may be code-size conscious, but without it, it makes sense
// to statically link a cdylib or staticlib. For staticlibs we use
// `-Z staticlib-prefer-dynamic` for now. This may be merged into
// `-C prefer-dynamic` in the future.
CrateType::Dylib | CrateType::Cdylib => {
if sess.opts.cg.prefer_dynamic {
Linkage::Dynamic
} else {
Linkage::Static
}
}
CrateType::Staticlib => {
if sess.opts.unstable_opts.staticlib_prefer_dynamic {
Linkage::Dynamic
} else {
Linkage::Static
}
}

// If the global prefer_dynamic switch is turned off, or the final
// executable will be statically linked, prefer static crate linkage.
Expand All @@ -108,9 +122,6 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
// No linkage happens with rlibs, we just needed the metadata (which we
// got long ago), so don't bother with anything.
CrateType::Rlib => Linkage::NotLinked,

// staticlibs must have all static dependencies.
CrateType::Staticlib => Linkage::Static,
};

match preferred_linkage {
Expand All @@ -123,9 +134,9 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
return v;
}

// Staticlibs and static executables must have all static dependencies.
// Static executables must have all static dependencies.
// If any are not found, generate some nice pretty errors.
if ty == CrateType::Staticlib
if (ty == CrateType::Staticlib && !sess.opts.unstable_opts.staticlib_allow_rdylib_deps)
|| (ty == CrateType::Executable
&& sess.crt_static(Some(ty))
&& !sess.target.crt_static_allows_dylibs)
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_middle/src/middle/exported_symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub enum ExportedSymbol<'tcx> {
NonGeneric(DefId),
Generic(DefId, SubstsRef<'tcx>),
DropGlue(Ty<'tcx>),
ThreadLocalShim(DefId),
NoDefId(ty::SymbolName<'tcx>),
}

Expand All @@ -58,6 +59,10 @@ impl<'tcx> ExportedSymbol<'tcx> {
ExportedSymbol::DropGlue(ty) => {
tcx.symbol_name(ty::Instance::resolve_drop_in_place(tcx, ty))
}
ExportedSymbol::ThreadLocalShim(def_id) => tcx.symbol_name(ty::Instance {
def: ty::InstanceDef::ThreadLocalShim(def_id),
substs: ty::InternalSubsts::empty(),
}),
ExportedSymbol::NoDefId(symbol_name) => symbol_name,
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/mir/mono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ impl<'tcx> CodegenUnit<'tcx> {
| InstanceDef::Virtual(..)
| InstanceDef::ClosureOnceShim { .. }
| InstanceDef::DropGlue(..)
| InstanceDef::ThreadLocalShim(..)
| InstanceDef::CloneShim(..) => None,
}
}
Expand Down
Loading