-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Use a DFS to compare CTFE snapshots #66946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6faae4d
f0148a5
840e323
fa5ff3a
37844e5
26b9146
345a8d5
e017f57
afb4faa
ba22627
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ use super::{ | |
Machine, AllocMap, MayLeak, ErrorHandled, CheckInAllocMsg, | ||
}; | ||
|
||
#[derive(Debug, PartialEq, Copy, Clone)] | ||
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)] | ||
pub enum MemoryKind<T> { | ||
/// Stack memory. Error if deallocated except during a stack pop. | ||
Stack, | ||
|
@@ -114,24 +114,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> HasDataLayout for Memory<'mir, 'tcx, M> | |
} | ||
} | ||
|
||
// FIXME: Really we shouldn't clone memory, ever. Snapshot machinery should instead | ||
// carefully copy only the reachable parts. | ||
impl<'mir, 'tcx, M> Clone for Memory<'mir, 'tcx, M> | ||
where | ||
M: Machine<'mir, 'tcx, PointerTag = (), AllocExtra = (), MemoryExtra = ()>, | ||
M::MemoryMap: AllocMap<AllocId, (MemoryKind<M::MemoryKinds>, Allocation)>, | ||
{ | ||
fn clone(&self) -> Self { | ||
Memory { | ||
alloc_map: self.alloc_map.clone(), | ||
extra_fn_ptr_map: self.extra_fn_ptr_map.clone(), | ||
dead_alloc_map: self.dead_alloc_map.clone(), | ||
extra: (), | ||
tcx: self.tcx, | ||
} | ||
} | ||
} | ||
|
||
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { | ||
pub fn new(tcx: TyCtxtAt<'tcx>, extra: M::MemoryExtra) -> Self { | ||
Memory { | ||
|
@@ -950,3 +932,56 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { | |
} | ||
} | ||
} | ||
|
||
/// A depth-first search over the allocation graph. | ||
/// | ||
/// This is based on the DFS iterator in `rustc_data_structures`, which we cannot use directly | ||
/// because `AllocId` does not implement `Idx` (it is not dense). | ||
pub struct DepthFirstSearch<'mem, 'mir, 'tcx, M: Machine<'mir, 'tcx>> { | ||
memory: &'mem Memory<'mir, 'tcx, M>, | ||
visited: FxHashSet<AllocId>, | ||
stack: Vec<AllocId>, | ||
} | ||
|
||
impl<M: Machine<'mir, 'tcx>> DepthFirstSearch<'mem, 'mir, 'tcx, M> { | ||
/// Returns a new DFS iterator that will traverse all allocations reachable from the given | ||
/// `AllocId`s. | ||
/// | ||
/// The first node in `roots` will be the first node visited by the DFS. | ||
pub fn with_roots( | ||
memory: &'mem Memory<'mir, 'tcx, M>, | ||
roots: impl IntoIterator<Item = AllocId>, | ||
) -> Self { | ||
let mut stack: Vec<_> = roots.into_iter().collect(); | ||
stack.reverse(); | ||
|
||
DepthFirstSearch { | ||
memory, | ||
visited: stack.iter().copied().collect(), | ||
stack, | ||
} | ||
} | ||
} | ||
|
||
impl<M: Machine<'mir, 'tcx>> Iterator for DepthFirstSearch<'mem, 'mir, 'tcx, M> { | ||
type Item = (AllocId, InterpResult<'tcx, &'mem Allocation<M::PointerTag, M::AllocExtra>>); | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
let DepthFirstSearch { stack, visited, memory } = self; | ||
|
||
let id = stack.pop()?; | ||
let alloc = memory.get_raw(id); | ||
|
||
if let Ok(alloc) = alloc { | ||
|
||
let new_pointers = alloc | ||
.relocations() | ||
.values() | ||
.map(|&(_, id)| id) | ||
.filter(|id| visited.insert(*id)); | ||
|
||
stack.extend(new_pointers); | ||
} | ||
|
||
Some((id, alloc)) | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.