Skip to content

Commit 928564c

Browse files
Stop collecting unmentioned constants
This avoids generating useless dead LLVM IR.
1 parent d4e1159 commit 928564c

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)