From 928564c7bfb395e1e5bd34d8913b7627ee4d1130 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sun, 22 Jun 2025 17:51:56 -0400 Subject: Stop collecting unmentioned constants This avoids generating useless dead LLVM IR. --- compiler/rustc_monomorphize/src/collector.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'compiler/rustc_monomorphize/src') diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index 173030e0326..3eb9a9dc850 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -1481,12 +1481,14 @@ impl<'v> RootCollector<'_, 'v> { // Const items only generate mono items if they are actually used somewhere. // Just declaring them is insufficient. - // But even just declaring them must collect the items they refer to - // unless their generics require monomorphization. - if !self.tcx.generics_of(id.owner_id).own_requires_monomorphization() - && let Ok(val) = self.tcx.const_eval_poly(id.owner_id.to_def_id()) - { - collect_const_value(self.tcx, val, self.output); + // If we're collecting items eagerly, then recurse into all constants. + // Otherwise the value is only collected when explicitly mentioned in other items. + if self.strategy == MonoItemCollectionStrategy::Eager { + if !self.tcx.generics_of(id.owner_id).own_requires_monomorphization() + && let Ok(val) = self.tcx.const_eval_poly(id.owner_id.to_def_id()) + { + collect_const_value(self.tcx, val, self.output); + } } } DefKind::Impl { .. } => { -- cgit 1.4.1-3-g733a5