Skip to content

Commit 0d508bb

Browse files
committed
Introduce and provide a hook for should_codegen_locally
1 parent d3dd34a commit 0d508bb

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

compiler/rustc_middle/src/hooks/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ declare_hooks! {
103103

104104
/// Create a list-like THIR representation for debugging.
105105
hook thir_flat(key: LocalDefId) -> String;
106+
107+
/// Returns `true` if we should codegen an instance in the local crate, or returns `false` if we
108+
/// can just link to the upstream crate and therefore don't need a mono item.
109+
hook should_codegen_locally(instance: crate::ty::Instance<'tcx>) -> bool;
106110
}
107111

108112
#[cold]

compiler/rustc_monomorphize/src/collector.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ use rustc_middle::ty::{
228228
self, AssocKind, GenericParamDefKind, Instance, InstanceKind, Ty, TyCtxt, TypeFoldable,
229229
TypeVisitableExt, VtblEntry,
230230
};
231+
use rustc_middle::util::Providers;
231232
use rustc_middle::{bug, span_bug};
232233
use rustc_session::config::EntryFnType;
233234
use rustc_session::Limit;
@@ -930,7 +931,7 @@ fn visit_instance_use<'tcx>(
930931

931932
/// Returns `true` if we should codegen an instance in the local crate, or returns `false` if we
932933
/// can just link to the upstream crate and therefore don't need a mono item.
933-
pub(crate) fn should_codegen_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> bool {
934+
pub(crate) fn should_codegen_locally_hook<'tcx>(tcx: TyCtxtAt<'tcx>, instance: Instance<'tcx>) -> bool {
934935
let Some(def_id) = instance.def.def_id_if_not_guaranteed_local_codegen() else {
935936
return true;
936937
};
@@ -946,7 +947,7 @@ pub(crate) fn should_codegen_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance
946947
}
947948

948949
if tcx.is_reachable_non_generic(def_id)
949-
|| instance.polymorphize(tcx).upstream_monomorphization(tcx).is_some()
950+
|| instance.polymorphize(*tcx).upstream_monomorphization(*tcx).is_some()
950951
{
951952
// We can link to the item in question, no instance needed in this crate.
952953
return false;
@@ -967,6 +968,12 @@ pub(crate) fn should_codegen_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance
967968
true
968969
}
969970

971+
/// Returns `true` if we should codegen an instance in the local crate, or returns `false` if we
972+
/// can just link to the upstream crate and therefore don't need a mono item.
973+
pub(crate) fn should_codegen_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> bool {
974+
tcx.should_codegen_locally(instance)
975+
}
976+
970977
/// For a given pair of source and target type that occur in an unsizing coercion,
971978
/// this function finds the pair of types that determines the vtable linking
972979
/// them.
@@ -1613,3 +1620,7 @@ pub(crate) fn collect_crate_mono_items<'tcx>(
16131620

16141621
(mono_items, state.usage_map.into_inner())
16151622
}
1623+
1624+
pub fn provide(providers: &mut Providers) {
1625+
providers.hooks.should_codegen_locally = should_codegen_locally_hook;
1626+
}

compiler/rustc_monomorphize/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55

66
use rustc_hir::lang_items::LangItem;
77
use rustc_middle::bug;
8-
use rustc_middle::query::{Providers, TyCtxtAt};
8+
use rustc_middle::query::TyCtxtAt;
99
use rustc_middle::traits;
1010
use rustc_middle::ty::adjustment::CustomCoerceUnsized;
1111
use rustc_middle::ty::Instance;
1212
use rustc_middle::ty::TyCtxt;
1313
use rustc_middle::ty::{self, Ty};
14+
use rustc_middle::util::Providers;
1415
use rustc_span::def_id::DefId;
1516
use rustc_span::def_id::LOCAL_CRATE;
1617
use rustc_span::ErrorGuaranteed;

compiler/rustc_monomorphize/src/partitioning.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ use rustc_middle::mir::mono::{
112112
CodegenUnit, CodegenUnitNameBuilder, InstantiationMode, Linkage, MonoItem, MonoItemData,
113113
Visibility,
114114
};
115-
use rustc_middle::query::Providers;
116115
use rustc_middle::ty::print::{characteristic_def_id_of_type, with_no_trimmed_paths};
117116
use rustc_middle::ty::{self, visit::TypeVisitableExt, InstanceKind, TyCtxt};
117+
use rustc_middle::util::Providers;
118118
use rustc_session::config::{DumpMonoStatsFormat, SwitchWithOptPath};
119119
use rustc_session::CodegenUnits;
120120
use rustc_span::symbol::Symbol;
@@ -1314,4 +1314,6 @@ pub fn provide(providers: &mut Providers) {
13141314
.find(|cgu| cgu.name() == name)
13151315
.unwrap_or_else(|| panic!("failed to find cgu with name {name:?}"))
13161316
};
1317+
1318+
collector::provide(providers);
13171319
}

0 commit comments

Comments
 (0)