Skip to content

Commit 3b8f172

Browse files
committed
refactor reached_depth -> required_depth
1 parent 85fe615 commit 3b8f172

File tree

3 files changed

+24
-38
lines changed

3 files changed

+24
-38
lines changed

compiler/rustc_type_ir/src/search_graph/global_cache.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::{AvailableDepth, Cx, NestedGoals};
44
use crate::data_structures::HashMap;
55

66
struct Success<X: Cx> {
7-
additional_depth: usize,
7+
required_depth: usize,
88
nested_goals: NestedGoals<X>,
99
result: X::Tracked<X::Result>,
1010
}
@@ -28,7 +28,7 @@ struct CacheEntry<X: Cx> {
2828
#[derive_where(Debug; X: Cx)]
2929
pub(super) struct CacheData<'a, X: Cx> {
3030
pub(super) result: X::Result,
31-
pub(super) additional_depth: usize,
31+
pub(super) required_depth: usize,
3232
pub(super) encountered_overflow: bool,
3333
pub(super) nested_goals: &'a NestedGoals<X>,
3434
}
@@ -47,21 +47,21 @@ impl<X: Cx> GlobalCache<X> {
4747
origin_result: X::Result,
4848
dep_node: X::DepNodeIndex,
4949

50-
additional_depth: usize,
50+
required_depth: usize,
5151
encountered_overflow: bool,
5252
nested_goals: NestedGoals<X>,
5353
) {
5454
let result = cx.mk_tracked(origin_result, dep_node);
5555
let entry = self.map.entry(input).or_default();
5656
if encountered_overflow {
5757
let with_overflow = WithOverflow { nested_goals, result };
58-
let prev = entry.with_overflow.insert(additional_depth, with_overflow);
58+
let prev = entry.with_overflow.insert(required_depth, with_overflow);
5959
if let Some(prev) = &prev {
6060
assert!(cx.evaluation_is_concurrent());
6161
assert_eq!(cx.get_tracked(&prev.result), origin_result);
6262
}
6363
} else {
64-
let prev = entry.success.replace(Success { additional_depth, nested_goals, result });
64+
let prev = entry.success.replace(Success { required_depth, nested_goals, result });
6565
if let Some(prev) = &prev {
6666
assert!(cx.evaluation_is_concurrent());
6767
assert_eq!(cx.get_tracked(&prev.result), origin_result);
@@ -81,13 +81,13 @@ impl<X: Cx> GlobalCache<X> {
8181
mut candidate_is_applicable: impl FnMut(&NestedGoals<X>) -> bool,
8282
) -> Option<CacheData<'a, X>> {
8383
let entry = self.map.get(&input)?;
84-
if let Some(Success { additional_depth, ref nested_goals, ref result }) = entry.success {
85-
if available_depth.cache_entry_is_applicable(additional_depth)
84+
if let Some(Success { required_depth, ref nested_goals, ref result }) = entry.success {
85+
if available_depth.cache_entry_is_applicable(required_depth)
8686
&& candidate_is_applicable(nested_goals)
8787
{
8888
return Some(CacheData {
8989
result: cx.get_tracked(&result),
90-
additional_depth,
90+
required_depth,
9191
encountered_overflow: false,
9292
nested_goals,
9393
});
@@ -101,7 +101,7 @@ impl<X: Cx> GlobalCache<X> {
101101
if candidate_is_applicable(nested_goals) {
102102
return Some(CacheData {
103103
result: cx.get_tracked(result),
104-
additional_depth,
104+
required_depth: additional_depth,
105105
encountered_overflow: true,
106106
nested_goals,
107107
});

compiler/rustc_type_ir/src/search_graph/mod.rs

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use std::hash::Hash;
1919
use std::marker::PhantomData;
2020

2121
use derive_where::derive_where;
22-
use rustc_index::{Idx, IndexVec};
2322
#[cfg(feature = "nightly")]
2423
use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext};
2524
use tracing::debug;
@@ -497,14 +496,14 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
497496
fn update_parent_goal(
498497
stack: &mut Stack<X>,
499498
step_kind_from_parent: PathKind,
500-
reached_depth: StackDepth,
499+
required_depth_for_nested: usize,
501500
heads: &CycleHeads,
502501
encountered_overflow: bool,
503502
context: UpdateParentGoalCtxt<'_, X>,
504503
) {
505504
if let Some(parent_index) = stack.last_index() {
506505
let parent = &mut stack[parent_index];
507-
parent.reached_depth = parent.reached_depth.max(reached_depth);
506+
parent.required_depth = parent.required_depth.max(required_depth_for_nested + 1);
508507
parent.encountered_overflow |= encountered_overflow;
509508

510509
parent.heads.extend_from_child(parent_index, step_kind_from_parent, heads);
@@ -612,20 +611,18 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
612611
return result;
613612
}
614613

615-
// Unfortunate, it looks like we actually have to compute this goalrar.
616-
let depth = self.stack.next_index();
617-
let entry = StackEntry {
614+
// Unfortunate, it looks like we actually have to compute this goal.
615+
self.stack.push(StackEntry {
618616
input,
619617
step_kind_from_parent,
620618
available_depth,
621-
reached_depth: depth,
619+
required_depth: 0,
622620
heads: Default::default(),
623621
encountered_overflow: false,
624622
has_been_used: None,
625623
nested_goals: Default::default(),
626624
provisional_result: None,
627-
};
628-
assert_eq!(self.stack.push(entry), depth);
625+
});
629626

630627
// This is for global caching, so we properly track query dependencies.
631628
// Everything that affects the `result` should be performed within this
@@ -642,7 +639,7 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
642639
Self::update_parent_goal(
643640
&mut self.stack,
644641
final_entry.step_kind_from_parent,
645-
final_entry.reached_depth,
642+
final_entry.required_depth,
646643
&final_entry.heads,
647644
final_entry.encountered_overflow,
648645
UpdateParentGoalCtxt::Ordinary(&final_entry.nested_goals),
@@ -824,14 +821,10 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
824821
// A provisional cache entry is only valid if the current path from its
825822
// highest cycle head to the goal is the same.
826823
if path_from_head == Self::cycle_path_kind(&self.stack, step_kind_from_parent, head) {
827-
// While we don't have to track the full depth of the provisional cache entry,
828-
// we do have to increment the required depth by one as we'd have already failed
829-
// with overflow otherwise
830-
let next_index = self.stack.next_index();
831824
Self::update_parent_goal(
832825
&mut self.stack,
833826
step_kind_from_parent,
834-
next_index,
827+
1,
835828
heads,
836829
encountered_overflow,
837830
UpdateParentGoalCtxt::ProvisionalCacheHit,
@@ -947,7 +940,7 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
947940
available_depth: AvailableDepth,
948941
) -> Option<X::Result> {
949942
cx.with_global_cache(|cache| {
950-
let CacheData { result, additional_depth, encountered_overflow, nested_goals } = cache
943+
let CacheData { result, required_depth, encountered_overflow, nested_goals } = cache
951944
.get(cx, input, available_depth, |nested_goals| {
952945
Self::candidate_is_applicable(
953946
&self.stack,
@@ -957,23 +950,19 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
957950
)
958951
})?;
959952

960-
// Update the reached depth of the current goal to make sure
961-
// its state is the same regardless of whether we've used the
962-
// global cache or not.
963-
let reached_depth = self.stack.next_index().plus(additional_depth);
964953
// We don't move cycle participants to the global cache, so the
965954
// cycle heads are always empty.
966955
let heads = Default::default();
967956
Self::update_parent_goal(
968957
&mut self.stack,
969958
step_kind_from_parent,
970-
reached_depth,
959+
required_depth,
971960
&heads,
972961
encountered_overflow,
973962
UpdateParentGoalCtxt::Ordinary(nested_goals),
974963
);
975964

976-
debug!(?additional_depth, "global cache hit");
965+
debug!(?required_depth, "global cache hit");
977966
Some(result)
978967
})
979968
}
@@ -999,10 +988,9 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
999988

1000989
// Subtle: when encountering a cyclic goal, we still first checked for overflow,
1001990
// so we have to update the reached depth.
1002-
let next_index = self.stack.next_index();
1003991
let last_index = self.stack.last_index().unwrap();
1004992
let last = &mut self.stack[last_index];
1005-
last.reached_depth = last.reached_depth.max(next_index);
993+
last.required_depth = last.required_depth.max(1);
1006994

1007995
last.nested_goals.insert(input, step_kind_from_parent.into());
1008996
last.nested_goals.insert(last.input, PathsToNested::EMPTY);
@@ -1137,15 +1125,14 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
11371125
result: X::Result,
11381126
dep_node: X::DepNodeIndex,
11391127
) {
1140-
let additional_depth = final_entry.reached_depth.as_usize() - self.stack.len();
11411128
debug!(?final_entry, ?result, "insert global cache");
11421129
cx.with_global_cache(|cache| {
11431130
cache.insert(
11441131
cx,
11451132
input,
11461133
result,
11471134
dep_node,
1148-
additional_depth,
1135+
final_entry.required_depth,
11491136
final_entry.encountered_overflow,
11501137
final_entry.nested_goals,
11511138
)

compiler/rustc_type_ir/src/search_graph/stack.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ pub(super) struct StackEntry<X: Cx> {
2626
/// The available depth of a given goal, immutable.
2727
pub available_depth: AvailableDepth,
2828

29-
/// The maximum depth reached by this stack entry, only up-to date
30-
/// for the top of the stack and lazily updated for the rest.
31-
pub reached_depth: StackDepth,
29+
/// The maximum depth required while evaluating this goal.
30+
pub required_depth: usize,
3231

3332
/// All cycle heads this goal depends on. Lazily updated and only
3433
/// up-to date for the top of the stack.

0 commit comments

Comments
 (0)