Skip to content

Commit

Permalink
Rollup merge of rust-lang#125308 - lcnr:search-graph-5, r=compiler-er…
Browse files Browse the repository at this point in the history
…rors

track cycle participants per root

The search graph may have multiple roots, e.g. in
```
A :- B
B :- A, C
C :- D
D :- C
```
we first encounter the `A -> B -> A` cycle which causes `A` to be a root. We then later encounter the `C -> D -> C` cycle as a nested goal of `B`. This cycle is completely separate and `C` will get moved to the global cache. This previously caused us to use `[B, D]` as the `cycle_participants` for `C` and `[]` for `A`.

split off from rust-lang#125167 as I would like to merge this change separately and will rebase that PR on top of this one. There is no test for this issue and I don't quite know how to write one. It is probably worth it to generalize the search graph to enable us to write unit tests for it.

r? `@compiler-errors`
  • Loading branch information
matthiaskrgr authored May 20, 2024
2 parents 4b26045 + f99c9ff commit e97103f
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 156 deletions.
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

0 comments on commit e97103f

Please sign in to comment.