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 Nov 17, 2024
2 parents 1e0df74 + 99abe39 commit f1c7352
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 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 @@ -1635,6 +1635,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
44 changes: 24 additions & 20 deletions compiler/rustc_monomorphize/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,26 +955,7 @@ fn should_codegen_locally<'tcx>(tcx: TyCtxtAt<'tcx>, instance: Instance<'tcx>) -
return true;
}

if tcx.is_reachable_non_generic(def_id)
|| instance.polymorphize(*tcx).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 @@ -1630,4 +1611,27 @@ 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.polymorphize(tcx).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 f1c7352

Please sign in to comment.