Skip to content

Commit 399b068

Browse files
committed
Auto merge of #113856 - WaffleLapkin:vtablin', r=oli-obk
Refactor vtable encoding and optimize it for the case of multiple marker traits This PR does two things - Refactor `prepare_vtable_segments` (this was motivated by the other change, `prepare_vtable_segments` was quite hard to understand and while trying to edit it I've refactored it) - Mostly remove `loop`s labeled `break`s/`continue`s whenever there is a simpler solution - Also use `?` - Make vtable format a bit more efficient wrt to marker traits - See the tests for an example Fixes #113840 cc `@crlf0710` ---- Review wise it's probably best to review each commit individually, as then it's more clear why the refactoring is correct. I can split the last two commits (which change behavior) into a separate PR if it makes reviewing easier
2 parents 1554942 + 1f02c75 commit 399b068

File tree

6 files changed

+198
-70
lines changed

6 files changed

+198
-70
lines changed

compiler/rustc_infer/src/traits/util.rs

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ impl<'tcx> PredicateSet<'tcx> {
2525
Self { tcx, set: Default::default() }
2626
}
2727

28+
/// Adds a predicate to the set.
29+
///
30+
/// Returns whether the predicate was newly inserted. That is:
31+
/// - If the set did not previously contain this predicate, `true` is returned.
32+
/// - If the set already contained this predicate, `false` is returned,
33+
/// and the set is not modified: original predicate is not replaced,
34+
/// and the predicate passed as argument is dropped.
2835
pub fn insert(&mut self, pred: ty::Predicate<'tcx>) -> bool {
2936
// We have to be careful here because we want
3037
//

compiler/rustc_trait_selection/src/traits/vtable.rs

+88-67
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,18 @@ pub enum VtblSegment<'tcx> {
2424
pub fn prepare_vtable_segments<'tcx, T>(
2525
tcx: TyCtxt<'tcx>,
2626
trait_ref: ty::PolyTraitRef<'tcx>,
27-
mut segment_visitor: impl FnMut(VtblSegment<'tcx>) -> ControlFlow<T>,
27+
segment_visitor: impl FnMut(VtblSegment<'tcx>) -> ControlFlow<T>,
2828
) -> Option<T> {
29+
prepare_vtable_segments_inner(tcx, trait_ref, segment_visitor).break_value()
30+
}
31+
32+
/// Helper for [`prepare_vtable_segments`] that returns `ControlFlow`,
33+
/// such that we can use `?` in the body.
34+
fn prepare_vtable_segments_inner<'tcx, T>(
35+
tcx: TyCtxt<'tcx>,
36+
trait_ref: ty::PolyTraitRef<'tcx>,
37+
mut segment_visitor: impl FnMut(VtblSegment<'tcx>) -> ControlFlow<T>,
38+
) -> ControlFlow<T> {
2939
// The following constraints holds for the final arrangement.
3040
// 1. The whole virtual table of the first direct super trait is included as the
3141
// the prefix. If this trait doesn't have any super traits, then this step
@@ -71,20 +81,18 @@ pub fn prepare_vtable_segments<'tcx, T>(
7181
// N, N-vptr, O
7282

7383
// emit dsa segment first.
74-
if let ControlFlow::Break(v) = (segment_visitor)(VtblSegment::MetadataDSA) {
75-
return Some(v);
76-
}
84+
segment_visitor(VtblSegment::MetadataDSA)?;
7785

7886
let mut emit_vptr_on_new_entry = false;
7987
let mut visited = PredicateSet::new(tcx);
8088
let predicate = trait_ref.without_const().to_predicate(tcx);
8189
let mut stack: SmallVec<[(ty::PolyTraitRef<'tcx>, _, _); 5]> =
82-
smallvec![(trait_ref, emit_vptr_on_new_entry, None)];
90+
smallvec![(trait_ref, emit_vptr_on_new_entry, maybe_iter(None))];
8391
visited.insert(predicate);
8492

8593
// the main traversal loop:
8694
// basically we want to cut the inheritance directed graph into a few non-overlapping slices of nodes
87-
// that each node is emitted after all its descendents have been emitted.
95+
// such that each node is emitted after all its descendants have been emitted.
8896
// so we convert the directed graph into a tree by skipping all previously visited nodes using a visited set.
8997
// this is done on the fly.
9098
// Each loop run emits a slice - it starts by find a "childless" unvisited node, backtracking upwards, and it
@@ -105,80 +113,81 @@ pub fn prepare_vtable_segments<'tcx, T>(
105113
// Loop run #1: Emitting the slice [D C] (in reverse order). No one has a next-sibling node.
106114
// Loop run #1: Stack after exiting out is []. Now the function exits.
107115

108-
loop {
116+
'outer: loop {
109117
// dive deeper into the stack, recording the path
110118
'diving_in: loop {
111-
if let Some((inner_most_trait_ref, _, _)) = stack.last() {
112-
let inner_most_trait_ref = *inner_most_trait_ref;
113-
let mut direct_super_traits_iter = tcx
114-
.super_predicates_of(inner_most_trait_ref.def_id())
115-
.predicates
116-
.into_iter()
117-
.filter_map(move |(pred, _)| {
118-
pred.subst_supertrait(tcx, &inner_most_trait_ref).as_trait_clause()
119-
});
119+
let &(inner_most_trait_ref, _, _) = stack.last().unwrap();
120+
121+
let mut direct_super_traits_iter = tcx
122+
.super_predicates_of(inner_most_trait_ref.def_id())
123+
.predicates
124+
.into_iter()
125+
.filter_map(move |(pred, _)| {
126+
pred.subst_supertrait(tcx, &inner_most_trait_ref).as_trait_clause()
127+
});
120128

121-
'diving_in_skip_visited_traits: loop {
122-
if let Some(next_super_trait) = direct_super_traits_iter.next() {
123-
if visited.insert(next_super_trait.to_predicate(tcx)) {
124-
// We're throwing away potential constness of super traits here.
125-
// FIXME: handle ~const super traits
126-
let next_super_trait = next_super_trait.map_bound(|t| t.trait_ref);
127-
stack.push((
128-
next_super_trait,
129-
emit_vptr_on_new_entry,
130-
Some(direct_super_traits_iter),
131-
));
132-
break 'diving_in_skip_visited_traits;
133-
} else {
134-
continue 'diving_in_skip_visited_traits;
135-
}
136-
} else {
137-
break 'diving_in;
138-
}
129+
// Find an unvisited supertrait
130+
match direct_super_traits_iter
131+
.find(|&super_trait| visited.insert(super_trait.to_predicate(tcx)))
132+
{
133+
// Push it to the stack for the next iteration of 'diving_in to pick up
134+
Some(unvisited_super_trait) => {
135+
// We're throwing away potential constness of super traits here.
136+
// FIXME: handle ~const super traits
137+
let next_super_trait = unvisited_super_trait.map_bound(|t| t.trait_ref);
138+
stack.push((
139+
next_super_trait,
140+
emit_vptr_on_new_entry,
141+
maybe_iter(Some(direct_super_traits_iter)),
142+
))
139143
}
144+
145+
// There are no more unvisited direct super traits, dive-in finished
146+
None => break 'diving_in,
140147
}
141148
}
142149

143-
// Other than the left-most path, vptr should be emitted for each trait.
144-
emit_vptr_on_new_entry = true;
145-
146150
// emit innermost item, move to next sibling and stop there if possible, otherwise jump to outer level.
147-
'exiting_out: loop {
148-
if let Some((inner_most_trait_ref, emit_vptr, siblings_opt)) = stack.last_mut() {
149-
if let ControlFlow::Break(v) = (segment_visitor)(VtblSegment::TraitOwnEntries {
150-
trait_ref: *inner_most_trait_ref,
151-
emit_vptr: *emit_vptr,
152-
}) {
153-
return Some(v);
154-
}
151+
while let Some((inner_most_trait_ref, emit_vptr, mut siblings)) = stack.pop() {
152+
segment_visitor(VtblSegment::TraitOwnEntries {
153+
trait_ref: inner_most_trait_ref,
154+
emit_vptr,
155+
})?;
156+
157+
// If we've emitted (fed to `segment_visitor`) a trait that has methods present in the vtable,
158+
// we'll need to emit vptrs from now on.
159+
if !emit_vptr_on_new_entry
160+
&& has_own_existential_vtable_entries(tcx, inner_most_trait_ref.def_id())
161+
{
162+
emit_vptr_on_new_entry = true;
163+
}
155164

156-
'exiting_out_skip_visited_traits: loop {
157-
if let Some(siblings) = siblings_opt {
158-
if let Some(next_inner_most_trait_ref) = siblings.next() {
159-
if visited.insert(next_inner_most_trait_ref.to_predicate(tcx)) {
160-
// We're throwing away potential constness of super traits here.
161-
// FIXME: handle ~const super traits
162-
let next_inner_most_trait_ref =
163-
next_inner_most_trait_ref.map_bound(|t| t.trait_ref);
164-
*inner_most_trait_ref = next_inner_most_trait_ref;
165-
*emit_vptr = emit_vptr_on_new_entry;
166-
break 'exiting_out;
167-
} else {
168-
continue 'exiting_out_skip_visited_traits;
169-
}
170-
}
171-
}
172-
stack.pop();
173-
continue 'exiting_out;
174-
}
165+
if let Some(next_inner_most_trait_ref) =
166+
siblings.find(|&sibling| visited.insert(sibling.to_predicate(tcx)))
167+
{
168+
// We're throwing away potential constness of super traits here.
169+
// FIXME: handle ~const super traits
170+
let next_inner_most_trait_ref =
171+
next_inner_most_trait_ref.map_bound(|t| t.trait_ref);
172+
173+
stack.push((next_inner_most_trait_ref, emit_vptr_on_new_entry, siblings));
174+
175+
// just pushed a new trait onto the stack, so we need to go through its super traits
176+
continue 'outer;
175177
}
176-
// all done
177-
return None;
178178
}
179+
180+
// the stack is empty, all done
181+
return ControlFlow::Continue(());
179182
}
180183
}
181184

185+
/// Turns option of iterator into an iterator (this is just flatten)
186+
fn maybe_iter<I: Iterator>(i: Option<I>) -> impl Iterator<Item = I::Item> {
187+
// Flatten is bad perf-vise, we could probably implement a special case here that is better
188+
i.into_iter().flatten()
189+
}
190+
182191
fn dump_vtable_entries<'tcx>(
183192
tcx: TyCtxt<'tcx>,
184193
sp: Span,
@@ -192,11 +201,23 @@ fn dump_vtable_entries<'tcx>(
192201
});
193202
}
194203

204+
fn has_own_existential_vtable_entries(tcx: TyCtxt<'_>, trait_def_id: DefId) -> bool {
205+
own_existential_vtable_entries_iter(tcx, trait_def_id).next().is_some()
206+
}
207+
195208
fn own_existential_vtable_entries(tcx: TyCtxt<'_>, trait_def_id: DefId) -> &[DefId] {
209+
tcx.arena.alloc_from_iter(own_existential_vtable_entries_iter(tcx, trait_def_id))
210+
}
211+
212+
fn own_existential_vtable_entries_iter(
213+
tcx: TyCtxt<'_>,
214+
trait_def_id: DefId,
215+
) -> impl Iterator<Item = DefId> + '_ {
196216
let trait_methods = tcx
197217
.associated_items(trait_def_id)
198218
.in_definition_order()
199219
.filter(|item| item.kind == ty::AssocKind::Fn);
220+
200221
// Now list each method's DefId (for within its trait).
201222
let own_entries = trait_methods.filter_map(move |&trait_method| {
202223
debug!("own_existential_vtable_entry: trait_method={:?}", trait_method);
@@ -211,7 +232,7 @@ fn own_existential_vtable_entries(tcx: TyCtxt<'_>, trait_def_id: DefId) -> &[Def
211232
Some(def_id)
212233
});
213234

214-
tcx.arena.alloc_from_iter(own_entries.into_iter())
235+
own_entries
215236
}
216237

217238
/// Given a trait `trait_ref`, iterates the vtable entries

tests/ui/traits/object/print_vtable_sizes.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ trait C {
1010
fn x() {} // not object safe, shouldn't be reported
1111
}
1212

13-
// This ideally should not have any upcasting cost,
14-
// but currently does due to a bug
13+
// This does not have any upcasting cost,
14+
// because both `Send` and `Sync` are traits
15+
// with no methods
1516
trait D: Send + Sync + help::MarkerWithSuper {}
1617

1718
// This can't have no cost without reordering,

tests/ui/traits/object/print_vtable_sizes.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
print-vtable-sizes { "crate_name": "<UNKNOWN_CRATE>", "trait_name": "D", "entries": "7", "entries_ignoring_upcasting": "4", "entries_for_upcasting": "3", "upcasting_cost_percent": "75" }
21
print-vtable-sizes { "crate_name": "<UNKNOWN_CRATE>", "trait_name": "E", "entries": "6", "entries_ignoring_upcasting": "4", "entries_for_upcasting": "2", "upcasting_cost_percent": "50" }
32
print-vtable-sizes { "crate_name": "<UNKNOWN_CRATE>", "trait_name": "G", "entries": "14", "entries_ignoring_upcasting": "11", "entries_for_upcasting": "3", "upcasting_cost_percent": "27.27272727272727" }
43
print-vtable-sizes { "crate_name": "<UNKNOWN_CRATE>", "trait_name": "A", "entries": "6", "entries_ignoring_upcasting": "5", "entries_for_upcasting": "1", "upcasting_cost_percent": "20" }
54
print-vtable-sizes { "crate_name": "<UNKNOWN_CRATE>", "trait_name": "B", "entries": "4", "entries_ignoring_upcasting": "4", "entries_for_upcasting": "0", "upcasting_cost_percent": "0" }
5+
print-vtable-sizes { "crate_name": "<UNKNOWN_CRATE>", "trait_name": "D", "entries": "4", "entries_ignoring_upcasting": "4", "entries_for_upcasting": "0", "upcasting_cost_percent": "0" }
66
print-vtable-sizes { "crate_name": "<UNKNOWN_CRATE>", "trait_name": "F", "entries": "6", "entries_ignoring_upcasting": "6", "entries_for_upcasting": "0", "upcasting_cost_percent": "0" }
77
print-vtable-sizes { "crate_name": "<UNKNOWN_CRATE>", "trait_name": "_::S", "entries": "3", "entries_ignoring_upcasting": "3", "entries_for_upcasting": "0", "upcasting_cost_percent": "0" }
88
print-vtable-sizes { "crate_name": "<UNKNOWN_CRATE>", "trait_name": "_::S", "entries": "3", "entries_ignoring_upcasting": "3", "entries_for_upcasting": "0", "upcasting_cost_percent": "0" }
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Regression test for <https://github.com/rust-lang/rust/issues/113840>
2+
//
3+
// This test makes sure that multiple marker (method-less) traits can reuse the
4+
// same pointer for upcasting.
5+
//
6+
// build-fail
7+
#![crate_type = "lib"]
8+
#![feature(rustc_attrs)]
9+
10+
// Markers
11+
trait M0 {}
12+
trait M1 {}
13+
trait M2 {}
14+
15+
// Just a trait with a method
16+
trait T {
17+
fn method(&self) {}
18+
}
19+
20+
#[rustc_dump_vtable]
21+
trait A: M0 + M1 + M2 + T {} //~ error: vtable entries for `<S as A>`:
22+
23+
#[rustc_dump_vtable]
24+
trait B: M0 + M1 + T + M2 {} //~ error: vtable entries for `<S as B>`:
25+
26+
#[rustc_dump_vtable]
27+
trait C: M0 + T + M1 + M2 {} //~ error: vtable entries for `<S as C>`:
28+
29+
#[rustc_dump_vtable]
30+
trait D: T + M0 + M1 + M2 {} //~ error: vtable entries for `<S as D>`:
31+
32+
struct S;
33+
34+
impl M0 for S {}
35+
impl M1 for S {}
36+
impl M2 for S {}
37+
impl T for S {}
38+
impl A for S {}
39+
impl B for S {}
40+
impl C for S {}
41+
impl D for S {}
42+
43+
pub fn require_vtables() {
44+
fn require_vtables(_: &dyn A, _: &dyn B, _: &dyn C, _: &dyn D) {}
45+
46+
require_vtables(&S, &S, &S, &S)
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
error: vtable entries for `<S as A>`: [
2+
MetadataDropInPlace,
3+
MetadataSize,
4+
MetadataAlign,
5+
Method(<S as T>::method),
6+
]
7+
--> $DIR/multiple-markers.rs:21:1
8+
|
9+
LL | trait A: M0 + M1 + M2 + T {}
10+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
11+
12+
error: vtable entries for `<S as B>`: [
13+
MetadataDropInPlace,
14+
MetadataSize,
15+
MetadataAlign,
16+
Method(<S as T>::method),
17+
TraitVPtr(<S as M2>),
18+
]
19+
--> $DIR/multiple-markers.rs:24:1
20+
|
21+
LL | trait B: M0 + M1 + T + M2 {}
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
23+
24+
error: vtable entries for `<S as C>`: [
25+
MetadataDropInPlace,
26+
MetadataSize,
27+
MetadataAlign,
28+
Method(<S as T>::method),
29+
TraitVPtr(<S as M1>),
30+
TraitVPtr(<S as M2>),
31+
]
32+
--> $DIR/multiple-markers.rs:27:1
33+
|
34+
LL | trait C: M0 + T + M1 + M2 {}
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
36+
37+
error: vtable entries for `<S as D>`: [
38+
MetadataDropInPlace,
39+
MetadataSize,
40+
MetadataAlign,
41+
Method(<S as T>::method),
42+
TraitVPtr(<S as M0>),
43+
TraitVPtr(<S as M1>),
44+
TraitVPtr(<S as M2>),
45+
]
46+
--> $DIR/multiple-markers.rs:30:1
47+
|
48+
LL | trait D: T + M0 + M1 + M2 {}
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
50+
51+
error: aborting due to 4 previous errors
52+

0 commit comments

Comments
 (0)