Skip to content

Commit 351e208

Browse files
committed
add tracing for layout optimizations
1 parent 4907dac commit 351e208

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

compiler/rustc_abi/src/layout.rs

+44
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::*;
2+
use std::fmt::Write;
23
use std::{borrow::Borrow, cmp, iter, ops::Bound};
34

45
#[cfg(feature = "randomize")]
@@ -72,12 +73,30 @@ pub trait LayoutCalculator {
7273
.expect("alt layout should have a niche like the regular one");
7374
let alt_head_space = niche.offset.bytes();
7475
let alt_niche_len = niche.value.size(dl).bytes();
76+
let alt_tail_space = alt_layout.size.bytes() - alt_head_space - alt_niche_len;
7577

7678
debug_assert_eq!(layout.size.bytes(), alt_layout.size.bytes());
7779

7880
let prefer_alt_layout =
7981
alt_head_space > head_space && alt_head_space > tail_space;
8082

83+
debug!(
84+
"sz: {}, default_niche_at: {}+{}, default_tail_space: {}, alt_niche_at/head_space: {}+{}, alt_tail: {}, num_fields: {}, better: {}\n\
85+
layout: {}\n\
86+
alt_layout: {}\n",
87+
layout.size.bytes(),
88+
head_space,
89+
niche_length,
90+
tail_space,
91+
alt_head_space,
92+
alt_niche_len,
93+
alt_tail_space,
94+
layout.fields.count(),
95+
prefer_alt_layout,
96+
format_field_niches(&layout, &fields, &dl),
97+
format_field_niches(&alt_layout, &fields, &dl),
98+
);
99+
81100
if prefer_alt_layout {
82101
return Some(alt_layout);
83102
}
@@ -1015,3 +1034,28 @@ fn univariant(
10151034
size,
10161035
})
10171036
}
1037+
1038+
fn format_field_niches(
1039+
layout: &LayoutS,
1040+
fields: &IndexSlice<FieldIdx, Layout<'_>>,
1041+
dl: &TargetDataLayout,
1042+
) -> String {
1043+
let mut s = String::new();
1044+
for i in layout.fields.index_by_increasing_offset() {
1045+
let offset = layout.fields.offset(i);
1046+
let f = fields[i.into()];
1047+
write!(s, "[o{}a{}s{}", offset.bytes(), f.align().abi.bytes(), f.size().bytes()).unwrap();
1048+
if let Some(n) = f.largest_niche() {
1049+
write!(
1050+
s,
1051+
" n{}b{}s{}",
1052+
n.offset.bytes(),
1053+
n.available(dl).ilog2(),
1054+
n.value.size(dl).bytes()
1055+
)
1056+
.unwrap();
1057+
}
1058+
write!(s, "] ").unwrap();
1059+
}
1060+
s
1061+
}

0 commit comments

Comments
 (0)