Skip to content

Querify should_codegen_locally #133016

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

Closed
Closed
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
5 changes: 5 additions & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,11 @@ rustc_queries! {
separate_provide_extern
}

query should_codegen_locally_slow(key: ty::Instance<'tcx>) -> bool {
desc { "checking whether `{}` should be linked to or lowered", key }
cache_on_disk_if { true }
}

/// Returns the upstream crate that exports drop-glue for the given
/// type (`args` is expected to be a single-item list containing the
/// type one wants drop-glue for).
Expand Down
41 changes: 23 additions & 18 deletions compiler/rustc_monomorphize/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -965,24 +965,7 @@ fn should_codegen_locally<'tcx>(tcx: TyCtxtAt<'tcx>, instance: Instance<'tcx>) -
return true;
}

if tcx.is_reachable_non_generic(def_id) || instance.upstream_monomorphization(*tcx).is_some() {
// We can link to the item in question, no instance needed in this crate.
return false;
}

if let DefKind::Static { .. } = tcx.def_kind(def_id) {
// We cannot monomorphize statics from upstream crates.
return false;
}

if !tcx.is_mir_available(def_id) {
tcx.dcx().emit_fatal(NoOptimizedMir {
span: tcx.def_span(def_id),
crate_name: tcx.crate_name(def_id.krate),
});
}

true
tcx.should_codegen_locally_slow(instance)
}

/// For a given pair of source and target type that occur in an unsizing coercion,
Expand Down Expand Up @@ -1643,4 +1626,26 @@ pub(crate) fn collect_crate_mono_items<'tcx>(
pub(crate) fn provide(providers: &mut Providers) {
providers.hooks.should_codegen_locally = should_codegen_locally;
providers.items_of_instance = items_of_instance;
providers.should_codegen_locally_slow = |tcx, instance| {
let def_id = instance.def_id();
if let DefKind::Static { .. } = tcx.def_kind(def_id) {
// We cannot monomorphize statics from upstream crates.
return false;
}

if tcx.is_reachable_non_generic(def_id) || instance.upstream_monomorphization(tcx).is_some()
{
// We can link to the item in question, no instance needed in this crate.
return false;
}

if !tcx.is_mir_available(def_id) {
tcx.dcx().emit_fatal(NoOptimizedMir {
span: tcx.def_span(def_id),
crate_name: tcx.crate_name(def_id.krate),
});
}

true
};
}
Loading