about summary refs log tree commit diff
path: root/compiler/rustc_monomorphize
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2025-06-27 12:57:05 +0000
committerbors <bors@rust-lang.org>2025-06-27 12:57:05 +0000
commit13c46fd0b089360922a557d8e18a63a2c41dfbeb (patch)
tree6ea31ef31e2a5ae71bdd9857df781619d9fdc78a /compiler/rustc_monomorphize
parentd51b6f97122671c5de27cfc08cded235357e0d97 (diff)
parent928564c7bfb395e1e5bd34d8913b7627ee4d1130 (diff)
downloadrust-13c46fd0b089360922a557d8e18a63a2c41dfbeb.tar.gz
rust-13c46fd0b089360922a557d8e18a63a2c41dfbeb.zip
Auto merge of #142893 - Mark-Simulacrum:no-const-collect, r=oli-obk
Stop collecting unmentioned constants

This avoids generating useless dead LLVM IR. This appears to have regressed and/or been introduced in rust-lang/rust#53821 (unfortunately a very large PR - I don't see any direct discussion there of this particular change), but as far as I can tell is at least no longer necessary -- or we lack test coverage -- because none of our UI tests indicate diagnostics regressions. The adjusted codegen-units test has comments explicitly noting that these items should *not* be collected ("These are not referenced, so they do not produce mono-items").

I noticed this while looking at libcore LLVM IR we generate, which contained dead code references to the NOOP Waker item, which is never used inside libcore. Producing LLVM IR for it during libcore's compilation, only for that IR to get deleted by LLVM as unused, isn't useful. Note that the IR is generally all marked internal, too.
Diffstat (limited to 'compiler/rustc_monomorphize')
-rw-r--r--compiler/rustc_monomorphize/src/collector.rs14
1 files changed, 8 insertions, 6 deletions
diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs
index e90e32ebebb..38da5c390ef 100644
--- a/compiler/rustc_monomorphize/src/collector.rs
+++ b/compiler/rustc_monomorphize/src/collector.rs
@@ -1489,12 +1489,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 { .. } => {