avoid printing allocations twice

This commit is contained in:
Ralf Jung 2020-04-04 12:19:10 +02:00
parent 2c9d857e90
commit fbdff5145a
2 changed files with 11 additions and 9 deletions

View File

@ -51,7 +51,7 @@ pub trait AllocMap<K: Hash + Eq, V> {
where where
K: Borrow<Q>; K: Borrow<Q>;
/// Returns data based the keys and values in the map. /// Returns data based on the keys and values in the map.
fn filter_map_collect<T>(&self, f: impl FnMut(&K, &V) -> Option<T>) -> Vec<T>; fn filter_map_collect<T>(&self, f: impl FnMut(&K, &V) -> Option<T>) -> Vec<T>;
/// Returns a reference to entry `k`. If no such entry exists, call /// Returns a reference to entry `k`. If no such entry exists, call

View File

@ -646,14 +646,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
fn dump_alloc_helper<Tag, Extra>( fn dump_alloc_helper<Tag, Extra>(
&self, &self,
allocs_seen: &mut FxHashSet<AllocId>,
allocs_to_print: &mut VecDeque<AllocId>, allocs_to_print: &mut VecDeque<AllocId>,
alloc: &Allocation<Tag, Extra>, alloc: &Allocation<Tag, Extra>,
) { ) {
for &(_, target_id) in alloc.relocations().values() { for &(_, target_id) in alloc.relocations().values() {
if allocs_seen.insert(target_id) { allocs_to_print.push_back(target_id);
allocs_to_print.push_back(target_id);
}
} }
crate::util::pretty::write_allocation(self.tcx.tcx, alloc, &mut std::io::stderr(), "") crate::util::pretty::write_allocation(self.tcx.tcx, alloc, &mut std::io::stderr(), "")
.unwrap(); .unwrap();
@ -666,9 +663,14 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
allocs.sort(); allocs.sort();
allocs.dedup(); allocs.dedup();
let mut allocs_to_print = VecDeque::from(allocs); let mut allocs_to_print = VecDeque::from(allocs);
let mut allocs_seen = FxHashSet::default(); // `allocs_printed` contains all allocations that we have already printed.
let mut allocs_printed = FxHashSet::default();
while let Some(id) = allocs_to_print.pop_front() { while let Some(id) = allocs_to_print.pop_front() {
if !allocs_printed.insert(id) {
// Already printed, so skip this.
continue;
}
eprint!("Alloc {:<5}: ", id); eprint!("Alloc {:<5}: ", id);
fn msg<Tag, Extra>(alloc: &Allocation<Tag, Extra>, extra: &str) { fn msg<Tag, Extra>(alloc: &Allocation<Tag, Extra>, extra: &str) {
eprintln!( eprintln!(
@ -688,14 +690,14 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
MemoryKind::CallerLocation => msg(alloc, " (caller_location)"), MemoryKind::CallerLocation => msg(alloc, " (caller_location)"),
MemoryKind::Machine(m) => msg(alloc, &format!(" ({:?})", m)), MemoryKind::Machine(m) => msg(alloc, &format!(" ({:?})", m)),
}; };
self.dump_alloc_helper(&mut allocs_seen, &mut allocs_to_print, alloc); self.dump_alloc_helper(&mut allocs_to_print, alloc);
} }
Err(()) => { Err(()) => {
// global alloc? // global alloc?
match self.tcx.alloc_map.lock().get(id) { match self.tcx.alloc_map.lock().get(id) {
Some(GlobalAlloc::Memory(alloc)) => { Some(GlobalAlloc::Memory(alloc)) => {
msg(alloc, " (immutable)"); msg(alloc, " (immutable)");
self.dump_alloc_helper(&mut allocs_seen, &mut allocs_to_print, alloc); self.dump_alloc_helper(&mut allocs_to_print, alloc);
} }
Some(GlobalAlloc::Function(func)) => { Some(GlobalAlloc::Function(func)) => {
eprintln!("{}", func); eprintln!("{}", func);
@ -722,8 +724,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
}); });
while let Some(id) = todo.pop() { while let Some(id) = todo.pop() {
if reachable.insert(id) { if reachable.insert(id) {
// This is a new allocation, add its relocations to `todo`.
if let Some((_, alloc)) = self.alloc_map.get(id) { if let Some((_, alloc)) = self.alloc_map.get(id) {
// This is a new allocation, add its relocations to `todo`.
todo.extend(alloc.relocations().values().map(|&(_, target_id)| target_id)); todo.extend(alloc.relocations().values().map(|&(_, target_id)| target_id));
} }
} }