Skip to content

Commit 0a2df62

Browse files
committed
Auto merge of #69916 - oli-obk:mir_bless, r=eddyb
Enable blessing of mir opt tests cc @rust-lang/wg-mir-opt cc @RalfJung Long overdue, but now you can finally just add a ```rust // EMIT_MIR rustc.function_name.MirPassName.before.mir ``` (or `after.mir` since most of the time you want to know the MIR after a pass). A `--bless` invocation will automatically create the files for you. I suggest we do this for all mir opt tests that have all of the MIR in their source anyway If you use `rustc.function.MirPass.diff` you only get the diff that the MIR pass causes on the MIR. Fixes #67865
2 parents 697d6b3 + c9a5a03 commit 0a2df62

24 files changed

+1201
-274
lines changed

src/librustc_data_structures/sorted_map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,13 @@ impl<K: Ord, V> SortedMap<K, V> {
110110

111111
/// Iterate over the keys, sorted
112112
#[inline]
113-
pub fn keys(&self) -> impl Iterator<Item = &K> + ExactSizeIterator {
113+
pub fn keys(&self) -> impl Iterator<Item = &K> + ExactSizeIterator + DoubleEndedIterator {
114114
self.data.iter().map(|&(ref k, _)| k)
115115
}
116116

117117
/// Iterate over values, sorted by key
118118
#[inline]
119-
pub fn values(&self) -> impl Iterator<Item = &V> + ExactSizeIterator {
119+
pub fn values(&self) -> impl Iterator<Item = &V> + ExactSizeIterator + DoubleEndedIterator {
120120
self.data.iter().map(|&(_, ref v)| v)
121121
}
122122

src/librustc_mir/interpret/memory.rs

+25-71
Original file line numberDiff line numberDiff line change
@@ -649,58 +649,15 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
649649
&self,
650650
allocs_seen: &mut FxHashSet<AllocId>,
651651
allocs_to_print: &mut VecDeque<AllocId>,
652-
mut msg: String,
653652
alloc: &Allocation<Tag, Extra>,
654-
extra: String,
655653
) {
656-
use std::fmt::Write;
657-
658-
let prefix_len = msg.len();
659-
let mut relocations = vec![];
660-
661-
for i in 0..alloc.size.bytes() {
662-
let i = Size::from_bytes(i);
663-
if let Some(&(_, target_id)) = alloc.relocations().get(&i) {
664-
if allocs_seen.insert(target_id) {
665-
allocs_to_print.push_back(target_id);
666-
}
667-
relocations.push((i, target_id));
668-
}
669-
if alloc.undef_mask().is_range_defined(i, i + Size::from_bytes(1)).is_ok() {
670-
// this `as usize` is fine, since `i` came from a `usize`
671-
let i = i.bytes_usize();
672-
673-
// Checked definedness (and thus range) and relocations. This access also doesn't
674-
// influence interpreter execution but is only for debugging.
675-
let bytes = alloc.inspect_with_undef_and_ptr_outside_interpreter(i..i + 1);
676-
write!(msg, "{:02x} ", bytes[0]).unwrap();
677-
} else {
678-
msg.push_str("__ ");
654+
for &(_, (_, target_id)) in alloc.relocations().iter() {
655+
if allocs_seen.insert(target_id) {
656+
allocs_to_print.push_back(target_id);
679657
}
680658
}
681-
682-
eprintln!(
683-
"{}({} bytes, alignment {}){}",
684-
msg,
685-
alloc.size.bytes(),
686-
alloc.align.bytes(),
687-
extra
688-
);
689-
690-
if !relocations.is_empty() {
691-
msg.clear();
692-
write!(msg, "{:1$}", "", prefix_len).unwrap(); // Print spaces.
693-
let mut pos = Size::ZERO;
694-
let relocation_width = (self.pointer_size().bytes() - 1) * 3;
695-
for (i, target_id) in relocations {
696-
write!(msg, "{:1$}", "", ((i - pos) * 3).bytes_usize()).unwrap();
697-
let target = format!("({})", target_id);
698-
// this `as usize` is fine, since we can't print more chars than `usize::MAX`
699-
write!(msg, "└{0:─^1$}┘ ", target, relocation_width as usize).unwrap();
700-
pos = i + self.pointer_size();
701-
}
702-
eprintln!("{}", msg);
703-
}
659+
crate::util::pretty::write_allocation(self.tcx.tcx, alloc, &mut std::io::stderr(), "")
660+
.unwrap();
704661
}
705662

706663
/// Print a list of allocations and all allocations they point to, recursively.
@@ -713,45 +670,42 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
713670
let mut allocs_seen = FxHashSet::default();
714671

715672
while let Some(id) = allocs_to_print.pop_front() {
716-
let msg = format!("Alloc {:<5} ", format!("{}:", id));
673+
eprint!("Alloc {:<5}: ", id);
674+
fn msg<Tag, Extra>(alloc: &Allocation<Tag, Extra>, extra: &str) {
675+
eprintln!(
676+
"({} bytes, alignment {}){}",
677+
alloc.size.bytes(),
678+
alloc.align.bytes(),
679+
extra
680+
)
681+
};
717682

718683
// normal alloc?
719684
match self.alloc_map.get_or(id, || Err(())) {
720685
Ok((kind, alloc)) => {
721-
let extra = match kind {
722-
MemoryKind::Stack => " (stack)".to_owned(),
723-
MemoryKind::Vtable => " (vtable)".to_owned(),
724-
MemoryKind::CallerLocation => " (caller_location)".to_owned(),
725-
MemoryKind::Machine(m) => format!(" ({:?})", m),
686+
match kind {
687+
MemoryKind::Stack => msg(alloc, " (stack)"),
688+
MemoryKind::Vtable => msg(alloc, " (vtable)"),
689+
MemoryKind::CallerLocation => msg(alloc, " (caller_location)"),
690+
MemoryKind::Machine(m) => msg(alloc, &format!(" ({:?})", m)),
726691
};
727-
self.dump_alloc_helper(
728-
&mut allocs_seen,
729-
&mut allocs_to_print,
730-
msg,
731-
alloc,
732-
extra,
733-
);
692+
self.dump_alloc_helper(&mut allocs_seen, &mut allocs_to_print, alloc);
734693
}
735694
Err(()) => {
736695
// global alloc?
737696
match self.tcx.alloc_map.lock().get(id) {
738697
Some(GlobalAlloc::Memory(alloc)) => {
739-
self.dump_alloc_helper(
740-
&mut allocs_seen,
741-
&mut allocs_to_print,
742-
msg,
743-
alloc,
744-
" (immutable)".to_owned(),
745-
);
698+
msg(alloc, " (immutable)");
699+
self.dump_alloc_helper(&mut allocs_seen, &mut allocs_to_print, alloc);
746700
}
747701
Some(GlobalAlloc::Function(func)) => {
748-
eprintln!("{} {}", msg, func);
702+
eprintln!("{}", func);
749703
}
750704
Some(GlobalAlloc::Static(did)) => {
751-
eprintln!("{} {:?}", msg, did);
705+
eprintln!("{:?}", did);
752706
}
753707
None => {
754-
eprintln!("{} (deallocated)", msg);
708+
eprintln!("(deallocated)");
755709
}
756710
}
757711
}

0 commit comments

Comments
 (0)