Skip to content

Commit 661b33f

Browse files
committedApr 12, 2023
Auto merge of #109935 - michaelwoerister:fix-feed-in-eval-always, r=cjgillot
incr.comp.: Make sure dependencies are recorded when feeding queries during eval-always queries. This PR makes sure we don't drop dependency edges when feeding queries during an eval-always query. Background: During eval-always queries, no dependencies are recorded because the system knows to unconditionally re-evaluate them regardless of any actual dependencies. This works fine for these queries themselves but leads to a problem when feeding other queries: When queries are fed, we set up their dependency edges by copying the current set of dependencies of the feeding query. But because this set is empty for eval-always queries, we record no edges at all -- which has the effect that the fed query instances always look "green" to the system, although they should always be "red". The fix is to explicitly add a dependency on the artificial "always red" dep-node when feeding during eval-always queries. Fixes #108481 Maybe also fixes issue #88488. cc `@jyn514` r? `@cjgillot` or `@oli-obk`
2 parents 9be9b5e + 5f52a96 commit 661b33f

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed
 

‎compiler/rustc_passes/src/hir_id_validator.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ use rustc_middle::hir::nested_filter;
88
use rustc_middle::ty::TyCtxt;
99

1010
pub fn check_crate(tcx: TyCtxt<'_>) {
11-
tcx.dep_graph.assert_ignored();
12-
1311
if tcx.sess.opts.unstable_opts.hir_stats {
1412
crate::hir_stats::print_hir_stats(tcx);
1513
}

‎compiler/rustc_query_system/src/dep_graph/graph.rs

+33-11
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,29 @@ impl<K: DepKind> DepGraph<K> {
143143
assert_eq!(_green_node_index, DepNodeIndex::SINGLETON_DEPENDENCYLESS_ANON_NODE);
144144

145145
// Instantiate a dependy-less red node only once for anonymous queries.
146-
let (_red_node_index, _prev_and_index) = current.intern_node(
146+
let (red_node_index, red_node_prev_index_and_color) = current.intern_node(
147147
profiler,
148148
&prev_graph,
149149
DepNode { kind: DepKind::RED, hash: Fingerprint::ZERO.into() },
150150
smallvec![],
151151
None,
152152
false,
153153
);
154-
assert_eq!(_red_node_index, DepNodeIndex::FOREVER_RED_NODE);
155-
assert!(matches!(_prev_and_index, None | Some((_, DepNodeColor::Red))));
154+
assert_eq!(red_node_index, DepNodeIndex::FOREVER_RED_NODE);
155+
match red_node_prev_index_and_color {
156+
None => {
157+
// This is expected when we have no previous compilation session.
158+
assert!(prev_graph_node_count == 0);
159+
}
160+
Some((prev_red_node_index, DepNodeColor::Red)) => {
161+
assert_eq!(prev_red_node_index.as_usize(), red_node_index.as_usize());
162+
colors.insert(prev_red_node_index, DepNodeColor::Red);
163+
}
164+
Some((_, DepNodeColor::Green(_))) => {
165+
// There must be a logic error somewhere if we hit this branch.
166+
panic!("DepNodeIndex::FOREVER_RED_NODE evaluated to DepNodeColor::Green")
167+
}
168+
}
156169

157170
DepGraph {
158171
data: Some(Lrc::new(DepGraphData {
@@ -353,10 +366,8 @@ impl<K: DepKind> DepGraphData<K> {
353366
}))
354367
};
355368

356-
let task_deps_ref = match &task_deps {
357-
Some(deps) => TaskDepsRef::Allow(deps),
358-
None => TaskDepsRef::Ignore,
359-
};
369+
let task_deps_ref =
370+
task_deps.as_ref().map(TaskDepsRef::Allow).unwrap_or(TaskDepsRef::EvalAlways);
360371

361372
let result = K::with_deps(task_deps_ref, || task(cx, arg));
362373
let edges = task_deps.map_or_else(|| smallvec![], |lock| lock.into_inner().reads);
@@ -461,6 +472,11 @@ impl<K: DepKind> DepGraph<K> {
461472
K::read_deps(|task_deps| {
462473
let mut task_deps = match task_deps {
463474
TaskDepsRef::Allow(deps) => deps.lock(),
475+
TaskDepsRef::EvalAlways => {
476+
// We don't need to record dependencies of eval_always
477+
// queries. They are re-evaluated unconditionally anyway.
478+
return;
479+
}
464480
TaskDepsRef::Ignore => return,
465481
TaskDepsRef::Forbid => {
466482
panic!("Illegal read of: {dep_node_index:?}")
@@ -563,7 +579,10 @@ impl<K: DepKind> DepGraph<K> {
563579
let mut edges = SmallVec::new();
564580
K::read_deps(|task_deps| match task_deps {
565581
TaskDepsRef::Allow(deps) => edges.extend(deps.lock().reads.iter().copied()),
566-
TaskDepsRef::Ignore => {} // During HIR lowering, we have no dependencies.
582+
TaskDepsRef::EvalAlways => {
583+
edges.push(DepNodeIndex::FOREVER_RED_NODE);
584+
}
585+
TaskDepsRef::Ignore => {}
567586
TaskDepsRef::Forbid => {
568587
panic!("Cannot summarize when dependencies are not recorded.")
569588
}
@@ -1356,10 +1375,13 @@ pub enum TaskDepsRef<'a, K: DepKind> {
13561375
/// `TaskDeps`. This is used when executing a 'normal' query
13571376
/// (no `eval_always` modifier)
13581377
Allow(&'a Lock<TaskDeps<K>>),
1359-
/// New dependencies are ignored. This is used when
1360-
/// executing an `eval_always` query, since there's no
1378+
/// This is used when executing an `eval_always` query. We don't
13611379
/// need to track dependencies for a query that's always
1362-
/// re-executed. This is also used for `dep_graph.with_ignore`
1380+
/// re-executed -- but we need to know that this is an `eval_always`
1381+
/// query in order to emit dependencies to `DepNodeIndex::FOREVER_RED_NODE`
1382+
/// when directly feeding other queries.
1383+
EvalAlways,
1384+
/// New dependencies are ignored. This is also used for `dep_graph.with_ignore`.
13631385
Ignore,
13641386
/// Any attempt to add new dependencies will cause a panic.
13651387
/// This is used when decoding a query result from disk,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// revisions: cpass1 cpass2
2+
3+
#![crate_type = "rlib"]
4+
5+
use std::fmt::Debug;
6+
7+
// MCVE kindly provided by Nilstrieb at
8+
// https://github.com/rust-lang/rust/issues/108481#issuecomment-1493080185
9+
10+
#[derive(Debug)]
11+
pub struct ConstGeneric<const CHUNK_SIZE: usize> {
12+
_p: [(); CHUNK_SIZE],
13+
}
14+
15+
#[cfg(cpass1)]
16+
impl<const CHUNK_SIZE: usize> ConstGeneric<CHUNK_SIZE> {}

0 commit comments

Comments
 (0)
Please sign in to comment.