Skip to content

Commit 499813f

Browse files
Auto merge of #142893 - Mark-Simulacrum:no-const-collect, r=<try>
Stop collecting unmentioned constants This avoids generating useless dead LLVM IR. This appears to have regressed and/or been introduced in #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.
2 parents 111e9bc + 928564c commit 499813f

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

compiler/rustc_monomorphize/src/collector.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,12 +1481,14 @@ impl<'v> RootCollector<'_, 'v> {
14811481
// Const items only generate mono items if they are actually used somewhere.
14821482
// Just declaring them is insufficient.
14831483

1484-
// But even just declaring them must collect the items they refer to
1485-
// unless their generics require monomorphization.
1486-
if !self.tcx.generics_of(id.owner_id).own_requires_monomorphization()
1487-
&& let Ok(val) = self.tcx.const_eval_poly(id.owner_id.to_def_id())
1488-
{
1489-
collect_const_value(self.tcx, val, self.output);
1484+
// If we're collecting items eagerly, then recurse into all constants.
1485+
// Otherwise the value is only collected when explicitly mentioned in other items.
1486+
if self.strategy == MonoItemCollectionStrategy::Eager {
1487+
if !self.tcx.generics_of(id.owner_id).own_requires_monomorphization()
1488+
&& let Ok(val) = self.tcx.const_eval_poly(id.owner_id.to_def_id())
1489+
{
1490+
collect_const_value(self.tcx, val, self.output);
1491+
}
14901492
}
14911493
}
14921494
DefKind::Impl { .. } => {

tests/codegen-units/partitioning/vtable-through-const.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ mod mod1 {
3535
}
3636
}
3737

38-
//~ MONO_ITEM fn mod1::id::<i64> @@ vtable_through_const-mod1.volatile[Internal]
3938
fn id<T>(x: T) -> T {
4039
x
4140
}
@@ -50,8 +49,6 @@ mod mod1 {
5049
fn do_something_else(&self) {}
5150
}
5251

53-
//~ MONO_ITEM fn <mod1::NeedsDrop as mod1::Trait2>::do_something @@ vtable_through_const-mod1.volatile[External]
54-
//~ MONO_ITEM fn <mod1::NeedsDrop as mod1::Trait2>::do_something_else @@ vtable_through_const-mod1.volatile[External]
5552
impl Trait2 for NeedsDrop {}
5653

5754
pub trait Trait2Gen<T> {
@@ -93,8 +90,6 @@ pub fn main() {
9390
// Same as above
9491
//~ MONO_ITEM fn <mod1::NeedsDrop as mod1::Trait1Gen<u8>>::do_something @@ vtable_through_const-mod1.volatile[External]
9592
//~ MONO_ITEM fn <mod1::NeedsDrop as mod1::Trait1Gen<u8>>::do_something_else @@ vtable_through_const-mod1.volatile[External]
96-
//~ MONO_ITEM fn <mod1::NeedsDrop as mod1::Trait2Gen<u8>>::do_something @@ vtable_through_const-mod1.volatile[External]
97-
//~ MONO_ITEM fn <mod1::NeedsDrop as mod1::Trait2Gen<u8>>::do_something_else @@ vtable_through_const-mod1.volatile[External]
9893
mod1::TRAIT1_GEN_REF.do_something(0u8);
9994

10095
//~ MONO_ITEM fn mod1::id::<char> @@ vtable_through_const-mod1.volatile[External]

0 commit comments

Comments
 (0)