Skip to content

Commit

Permalink
Auto merge of rust-lang#133016 - saethlin:querify-should-codegen-loca…
Browse files Browse the repository at this point in the history
…lly, r=<try>

Querify should_codegen_locally

I think this has a very strong interaction with rust-lang#132566, and may not be justifiable without that PR, so marking this as blocked until that is merged.
  • Loading branch information
bors committed Dec 8, 2024
2 parents f415c07 + 60962ec commit 6d11a59
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
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
};
}

0 comments on commit 6d11a59

Please sign in to comment.