Skip to content

Commit 7b0eac4

Browse files
committed
Auto merge of #112400 - WaffleLapkin:vtable_stats, r=compiler-errors
Collect VTable stats & add `-Zprint-vtable-sizes` This is a bit hacky/buggy, but I'm not entirely sure how to fix it, so I want to ask reviewers for help... To try this, use either of those: - `cargo clean && RUSTFLAGS="-Zprint-vtable-sizes" cargo +toolchain b` - `cargo clean && cargo rustc +toolchain -Zprint-vtable-sizes` - `rustc +toolchain -Zprint-vtable-sizes ./file.rs`
2 parents 3ed2a10 + af4631a commit 7b0eac4

File tree

10 files changed

+232
-5
lines changed

10 files changed

+232
-5
lines changed

compiler/rustc_driver_impl/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,13 @@ fn run_compiler(
430430
sess.code_stats.print_type_sizes();
431431
}
432432

433+
if sess.opts.unstable_opts.print_vtable_sizes {
434+
let crate_name =
435+
compiler.session().opts.crate_name.as_deref().unwrap_or("<UNKNOWN_CRATE>");
436+
437+
sess.code_stats.print_vtable_sizes(crate_name);
438+
}
439+
433440
let linker = queries.linker()?;
434441
Ok(Some(linker))
435442
})?;

compiler/rustc_interface/src/interface.rs

+1
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
333333
};
334334

335335
let prof = compiler.sess.prof.clone();
336+
336337
prof.generic_activity("drop_compiler").run(move || drop(compiler));
337338
r
338339
})

compiler/rustc_interface/src/passes.rs

+87
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc_parse::{parse_crate_from_file, parse_crate_from_source_str, validate_a
2424
use rustc_passes::{self, hir_stats, layout_test};
2525
use rustc_plugin_impl as plugin;
2626
use rustc_resolve::Resolver;
27+
use rustc_session::code_stats::VTableSizeInfo;
2728
use rustc_session::config::{CrateType, Input, OutFileName, OutputFilenames, OutputType};
2829
use rustc_session::cstore::{MetadataLoader, Untracked};
2930
use rustc_session::output::filename_for_input;
@@ -866,6 +867,92 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
866867
sess.time("check_lint_expectations", || tcx.check_expectations(None));
867868
});
868869

870+
if sess.opts.unstable_opts.print_vtable_sizes {
871+
let traits = tcx.traits(LOCAL_CRATE);
872+
873+
for &tr in traits {
874+
if !tcx.check_is_object_safe(tr) {
875+
continue;
876+
}
877+
878+
let name = ty::print::with_no_trimmed_paths!(tcx.def_path_str(tr));
879+
880+
let mut first_dsa = true;
881+
882+
// Number of vtable entries, if we didn't have upcasting
883+
let mut entries_ignoring_upcasting = 0;
884+
// Number of vtable entries needed solely for upcasting
885+
let mut entries_for_upcasting = 0;
886+
887+
let trait_ref = ty::Binder::dummy(ty::TraitRef::identity(tcx, tr));
888+
889+
// A slightly edited version of the code in `rustc_trait_selection::traits::vtable::vtable_entries`,
890+
// that works without self type and just counts number of entries.
891+
//
892+
// Note that this is technically wrong, for traits which have associated types in supertraits:
893+
//
894+
// trait A: AsRef<Self::T> + AsRef<()> { type T; }
895+
//
896+
// Without self type we can't normalize `Self::T`, so we can't know if `AsRef<Self::T>` and
897+
// `AsRef<()>` are the same trait, thus we assume that those are different, and potentially
898+
// over-estimate how many vtable entries there are.
899+
//
900+
// Similarly this is wrong for traits that have methods with possibly-impossible bounds.
901+
// For example:
902+
//
903+
// trait B<T> { fn f(&self) where T: Copy; }
904+
//
905+
// Here `dyn B<u8>` will have 4 entries, while `dyn B<String>` will only have 3.
906+
// However, since we don't know `T`, we can't know if `T: Copy` holds or not,
907+
// thus we lean on the bigger side and say it has 4 entries.
908+
traits::vtable::prepare_vtable_segments(tcx, trait_ref, |segment| {
909+
match segment {
910+
traits::vtable::VtblSegment::MetadataDSA => {
911+
// If this is the first dsa, it would be included either way,
912+
// otherwise it's needed for upcasting
913+
if std::mem::take(&mut first_dsa) {
914+
entries_ignoring_upcasting += 3;
915+
} else {
916+
entries_for_upcasting += 3;
917+
}
918+
}
919+
920+
traits::vtable::VtblSegment::TraitOwnEntries { trait_ref, emit_vptr } => {
921+
// Lookup the shape of vtable for the trait.
922+
let own_existential_entries =
923+
tcx.own_existential_vtable_entries(trait_ref.def_id());
924+
925+
// The original code here ignores the method if its predicates are impossible.
926+
// We can't really do that as, for example, all not trivial bounds on generic
927+
// parameters are impossible (since we don't know the parameters...),
928+
// see the comment above.
929+
entries_ignoring_upcasting += own_existential_entries.len();
930+
931+
if emit_vptr {
932+
entries_for_upcasting += 1;
933+
}
934+
}
935+
}
936+
937+
std::ops::ControlFlow::Continue::<std::convert::Infallible>(())
938+
});
939+
940+
sess.code_stats.record_vtable_size(
941+
tr,
942+
&name,
943+
VTableSizeInfo {
944+
trait_name: name.clone(),
945+
entries: entries_ignoring_upcasting + entries_for_upcasting,
946+
entries_ignoring_upcasting,
947+
entries_for_upcasting,
948+
upcasting_cost_percent: entries_for_upcasting as f64
949+
/ entries_ignoring_upcasting as f64
950+
* 100.,
951+
},
952+
)
953+
}
954+
}
955+
869956
Ok(())
870957
}
871958

compiler/rustc_session/src/code_stats.rs

+59-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use rustc_data_structures::fx::FxHashSet;
1+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
22
use rustc_data_structures::sync::Lock;
3+
use rustc_span::def_id::DefId;
34
use rustc_span::Symbol;
45
use rustc_target::abi::{Align, Size};
56
use std::cmp;
@@ -65,9 +66,29 @@ pub struct TypeSizeInfo {
6566
pub variants: Vec<VariantInfo>,
6667
}
6768

69+
pub struct VTableSizeInfo {
70+
pub trait_name: String,
71+
72+
/// Number of entries in a vtable with the current algorithm
73+
/// (i.e. with upcasting).
74+
pub entries: usize,
75+
76+
/// Number of entries in a vtable, as-if we did not have trait upcasting.
77+
pub entries_ignoring_upcasting: usize,
78+
79+
/// Number of entries in a vtable needed solely for upcasting
80+
/// (i.e. `entries - entries_ignoring_upcasting`).
81+
pub entries_for_upcasting: usize,
82+
83+
/// Cost of having upcasting in % relative to the number of entries without
84+
/// upcasting (i.e. `entries_for_upcasting / entries_ignoring_upcasting * 100%`).
85+
pub upcasting_cost_percent: f64,
86+
}
87+
6888
#[derive(Default)]
6989
pub struct CodeStats {
7090
type_sizes: Lock<FxHashSet<TypeSizeInfo>>,
91+
vtable_sizes: Lock<FxHashMap<DefId, VTableSizeInfo>>,
7192
}
7293

7394
impl CodeStats {
@@ -101,6 +122,14 @@ impl CodeStats {
101122
self.type_sizes.borrow_mut().insert(info);
102123
}
103124

125+
pub fn record_vtable_size(&self, trait_did: DefId, trait_name: &str, info: VTableSizeInfo) {
126+
let prev = self.vtable_sizes.lock().insert(trait_did, info);
127+
assert!(
128+
prev.is_none(),
129+
"size of vtable for `{trait_name}` ({trait_did:?}) is already recorded"
130+
);
131+
}
132+
104133
pub fn print_type_sizes(&self) {
105134
let type_sizes = self.type_sizes.borrow();
106135
let mut sorted: Vec<_> = type_sizes.iter().collect();
@@ -196,4 +225,33 @@ impl CodeStats {
196225
}
197226
}
198227
}
228+
229+
pub fn print_vtable_sizes(&self, crate_name: &str) {
230+
let mut infos = std::mem::take(&mut *self.vtable_sizes.lock())
231+
.into_iter()
232+
.map(|(_did, stats)| stats)
233+
.collect::<Vec<_>>();
234+
235+
// Primary sort: cost % in reverse order (from largest to smallest)
236+
// Secondary sort: trait_name
237+
infos.sort_by(|a, b| {
238+
a.upcasting_cost_percent
239+
.total_cmp(&b.upcasting_cost_percent)
240+
.reverse()
241+
.then_with(|| a.trait_name.cmp(&b.trait_name))
242+
});
243+
244+
for VTableSizeInfo {
245+
trait_name,
246+
entries,
247+
entries_ignoring_upcasting,
248+
entries_for_upcasting,
249+
upcasting_cost_percent,
250+
} in infos
251+
{
252+
println!(
253+
r#"print-vtable-sizes {{ "crate_name": "{crate_name}", "trait_name": "{trait_name}", "entries": "{entries}", "entries_ignoring_upcasting": "{entries_ignoring_upcasting}", "entries_for_upcasting": "{entries_for_upcasting}", "upcasting_cost_percent": "{upcasting_cost_percent}" }}"#
254+
);
255+
}
256+
}
199257
}

compiler/rustc_session/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub use lint::{declare_lint, declare_lint_pass, declare_tool_lint, impl_lint_pas
2727
pub use rustc_lint_defs as lint;
2828
pub mod parse;
2929

30-
mod code_stats;
30+
pub mod code_stats;
3131
#[macro_use]
3232
pub mod config;
3333
pub mod cstore;

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,8 @@ options! {
16321632
"print the result of the monomorphization collection pass"),
16331633
print_type_sizes: bool = (false, parse_bool, [UNTRACKED],
16341634
"print layout information for each type encountered (default: no)"),
1635+
print_vtable_sizes: bool = (false, parse_bool, [UNTRACKED],
1636+
"print size comparison between old and new vtable layouts (default: no)"),
16351637
proc_macro_backtrace: bool = (false, parse_bool, [UNTRACKED],
16361638
"show backtraces for panics during proc-macro execution (default: no)"),
16371639
proc_macro_execution_strategy: ProcMacroExecutionStrategy = (ProcMacroExecutionStrategy::SameThread,

compiler/rustc_trait_selection/src/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mod structural_match;
2121
mod structural_normalize;
2222
#[cfg_attr(not(bootstrap), allow(hidden_glob_reexports))]
2323
mod util;
24-
mod vtable;
24+
pub mod vtable;
2525
pub mod wf;
2626

2727
use crate::infer::outlives::env::OutlivesEnvironment;

compiler/rustc_trait_selection/src/traits/vtable.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ use std::fmt::Debug;
1515
use std::ops::ControlFlow;
1616

1717
#[derive(Clone, Debug)]
18-
pub(super) enum VtblSegment<'tcx> {
18+
pub enum VtblSegment<'tcx> {
1919
MetadataDSA,
2020
TraitOwnEntries { trait_ref: ty::PolyTraitRef<'tcx>, emit_vptr: bool },
2121
}
2222

2323
/// Prepare the segments for a vtable
24-
pub(super) fn prepare_vtable_segments<'tcx, T>(
24+
pub fn prepare_vtable_segments<'tcx, T>(
2525
tcx: TyCtxt<'tcx>,
2626
trait_ref: ty::PolyTraitRef<'tcx>,
2727
mut segment_visitor: impl FnMut(VtblSegment<'tcx>) -> ControlFlow<T>,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// check-pass
2+
// compile-flags: -Z print-vtable-sizes
3+
#![crate_type = "lib"]
4+
5+
trait A<T: help::V>: AsRef<[T::V]> + AsMut<[T::V]> {}
6+
7+
trait B<T>: AsRef<T> + AsRef<T> + AsRef<T> + AsRef<T> {}
8+
9+
trait C {
10+
fn x() {} // not object safe, shouldn't be reported
11+
}
12+
13+
// This ideally should not have any upcasting cost,
14+
// but currently does due to a bug
15+
trait D: Send + Sync + help::MarkerWithSuper {}
16+
17+
// This can't have no cost without reordering,
18+
// because `Super::f`.
19+
trait E: help::MarkerWithSuper + Send + Sync {}
20+
21+
trait F {
22+
fn a(&self);
23+
fn b(&self);
24+
fn c(&self);
25+
26+
fn d() -> Self
27+
where
28+
Self: Sized;
29+
}
30+
31+
trait G: AsRef<u8> + AsRef<u16> + help::MarkerWithSuper {
32+
fn a(&self);
33+
fn b(&self);
34+
fn c(&self);
35+
fn d(&self);
36+
fn e(&self);
37+
38+
fn f() -> Self
39+
where
40+
Self: Sized;
41+
}
42+
43+
// Traits with the same name
44+
const _: () = {
45+
trait S {}
46+
};
47+
const _: () = {
48+
trait S {}
49+
};
50+
51+
mod help {
52+
pub trait V {
53+
type V;
54+
}
55+
56+
pub trait MarkerWithSuper: Super {}
57+
58+
pub trait Super {
59+
fn f(&self);
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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" }
2+
print-vtable-sizes { "crate_name": "<UNKNOWN_CRATE>", "trait_name": "E", "entries": "6", "entries_ignoring_upcasting": "4", "entries_for_upcasting": "2", "upcasting_cost_percent": "50" }
3+
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" }
4+
print-vtable-sizes { "crate_name": "<UNKNOWN_CRATE>", "trait_name": "A", "entries": "6", "entries_ignoring_upcasting": "5", "entries_for_upcasting": "1", "upcasting_cost_percent": "20" }
5+
print-vtable-sizes { "crate_name": "<UNKNOWN_CRATE>", "trait_name": "B", "entries": "4", "entries_ignoring_upcasting": "4", "entries_for_upcasting": "0", "upcasting_cost_percent": "0" }
6+
print-vtable-sizes { "crate_name": "<UNKNOWN_CRATE>", "trait_name": "F", "entries": "6", "entries_ignoring_upcasting": "6", "entries_for_upcasting": "0", "upcasting_cost_percent": "0" }
7+
print-vtable-sizes { "crate_name": "<UNKNOWN_CRATE>", "trait_name": "_::S", "entries": "3", "entries_ignoring_upcasting": "3", "entries_for_upcasting": "0", "upcasting_cost_percent": "0" }
8+
print-vtable-sizes { "crate_name": "<UNKNOWN_CRATE>", "trait_name": "_::S", "entries": "3", "entries_ignoring_upcasting": "3", "entries_for_upcasting": "0", "upcasting_cost_percent": "0" }
9+
print-vtable-sizes { "crate_name": "<UNKNOWN_CRATE>", "trait_name": "help::MarkerWithSuper", "entries": "4", "entries_ignoring_upcasting": "4", "entries_for_upcasting": "0", "upcasting_cost_percent": "0" }
10+
print-vtable-sizes { "crate_name": "<UNKNOWN_CRATE>", "trait_name": "help::Super", "entries": "4", "entries_ignoring_upcasting": "4", "entries_for_upcasting": "0", "upcasting_cost_percent": "0" }
11+
print-vtable-sizes { "crate_name": "<UNKNOWN_CRATE>", "trait_name": "help::V", "entries": "3", "entries_ignoring_upcasting": "3", "entries_for_upcasting": "0", "upcasting_cost_percent": "0" }

0 commit comments

Comments
 (0)