Skip to content
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

track cycle participants per root #125308

Merged
merged 3 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions compiler/rustc_middle/src/traits/solve/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ pub struct EvaluationCache<'tcx> {
map: Lock<FxHashMap<CanonicalInput<'tcx>, CacheEntry<'tcx>>>,
}

#[derive(PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq)]
pub struct CacheData<'tcx> {
pub result: QueryResult<'tcx>,
pub proof_tree: Option<&'tcx [inspect::GoalEvaluationStep<TyCtxt<'tcx>>]>,
pub reached_depth: usize,
pub additional_depth: usize,
pub encountered_overflow: bool,
}

Expand All @@ -29,7 +29,7 @@ impl<'tcx> EvaluationCache<'tcx> {
tcx: TyCtxt<'tcx>,
key: CanonicalInput<'tcx>,
proof_tree: Option<&'tcx [inspect::GoalEvaluationStep<TyCtxt<'tcx>>]>,
reached_depth: usize,
additional_depth: usize,
encountered_overflow: bool,
cycle_participants: FxHashSet<CanonicalInput<'tcx>>,
dep_node: DepNodeIndex,
Expand All @@ -40,17 +40,17 @@ impl<'tcx> EvaluationCache<'tcx> {
let data = WithDepNode::new(dep_node, QueryData { result, proof_tree });
entry.cycle_participants.extend(cycle_participants);
if encountered_overflow {
entry.with_overflow.insert(reached_depth, data);
entry.with_overflow.insert(additional_depth, data);
} else {
entry.success = Some(Success { data, reached_depth });
entry.success = Some(Success { data, additional_depth });
}

if cfg!(debug_assertions) {
drop(map);
if Some(CacheData { result, proof_tree, reached_depth, encountered_overflow })
!= self.get(tcx, key, |_| false, Limit(reached_depth))
{
bug!("unable to retrieve inserted element from cache: {key:?}");
let expected = CacheData { result, proof_tree, additional_depth, encountered_overflow };
let actual = self.get(tcx, key, [], Limit(additional_depth));
if !actual.as_ref().is_some_and(|actual| expected == *actual) {
bug!("failed to lookup inserted element for {key:?}: {expected:?} != {actual:?}");
}
}
}
Expand All @@ -63,23 +63,25 @@ impl<'tcx> EvaluationCache<'tcx> {
&self,
tcx: TyCtxt<'tcx>,
key: CanonicalInput<'tcx>,
cycle_participant_in_stack: impl FnOnce(&FxHashSet<CanonicalInput<'tcx>>) -> bool,
stack_entries: impl IntoIterator<Item = CanonicalInput<'tcx>>,
available_depth: Limit,
) -> Option<CacheData<'tcx>> {
let map = self.map.borrow();
let entry = map.get(&key)?;

if cycle_participant_in_stack(&entry.cycle_participants) {
return None;
for stack_entry in stack_entries {
if entry.cycle_participants.contains(&stack_entry) {
return None;
}
}

if let Some(ref success) = entry.success {
if available_depth.value_within_limit(success.reached_depth) {
if available_depth.value_within_limit(success.additional_depth) {
let QueryData { result, proof_tree } = success.data.get(tcx);
return Some(CacheData {
result,
proof_tree,
reached_depth: success.reached_depth,
additional_depth: success.additional_depth,
encountered_overflow: false,
});
}
Expand All @@ -90,7 +92,7 @@ impl<'tcx> EvaluationCache<'tcx> {
CacheData {
result,
proof_tree,
reached_depth: available_depth.0,
additional_depth: available_depth.0,
encountered_overflow: true,
}
})
Expand All @@ -99,7 +101,7 @@ impl<'tcx> EvaluationCache<'tcx> {

struct Success<'tcx> {
data: WithDepNode<QueryData<'tcx>>,
reached_depth: usize,
additional_depth: usize,
}

#[derive(Clone, Copy)]
Expand Down
Loading
Loading