Skip to content

Commit 212b0d4

Browse files
committed
Auto merge of rust-lang#154076 - nnethercote:DepNode-improvements, r=Zalathar
`DepNode` use improvements We can simplify `DepNode` construction during query execution. Details in individual commits. r? @Zalathar
2 parents 6f22f61 + 3c7ee98 commit 212b0d4

File tree

1 file changed

+36
-71
lines changed

1 file changed

+36
-71
lines changed

compiler/rustc_query_impl/src/execution.rs

Lines changed: 36 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,7 @@ fn try_execute_query<'tcx, C: QueryCache, const INCR: bool>(
261261
tcx: TyCtxt<'tcx>,
262262
span: Span,
263263
key: C::Key,
264-
// If present, some previous step has already created a `DepNode` for this
265-
// query+key, which we should reuse instead of creating a new one.
266-
dep_node: Option<DepNode>,
264+
dep_node: Option<DepNode>, // `None` for non-incremental, `Some` for incremental
267265
) -> (C::Value, Option<DepNodeIndex>) {
268266
let key_hash = sharded::make_hash(&key);
269267
let mut state_lock = query.state.active.lock_shard_by_hash(key_hash);
@@ -298,11 +296,9 @@ fn try_execute_query<'tcx, C: QueryCache, const INCR: bool>(
298296
// panic occurs while executing the query (or any intermediate plumbing).
299297
let job_guard = ActiveJobGuard { state: &query.state, key, key_hash };
300298

301-
debug_assert_eq!(tcx.dep_graph.is_fully_enabled(), INCR);
302-
303299
// Delegate to another function to actually execute the query job.
304300
let (value, dep_node_index) = if INCR {
305-
execute_job_incr(query, tcx, key, dep_node, id)
301+
execute_job_incr(query, tcx, key, dep_node.unwrap(), id)
306302
} else {
307303
execute_job_non_incr(query, tcx, key, id)
308304
};
@@ -418,27 +414,23 @@ fn execute_job_incr<'tcx, C: QueryCache>(
418414
query: &'tcx QueryVTable<'tcx, C>,
419415
tcx: TyCtxt<'tcx>,
420416
key: C::Key,
421-
mut dep_node_opt: Option<DepNode>,
417+
dep_node: DepNode,
422418
job_id: QueryJobId,
423419
) -> (C::Value, DepNodeIndex) {
424420
let dep_graph_data =
425421
tcx.dep_graph.data().expect("should always be present in incremental mode");
426422

427423
if !query.eval_always {
428-
// `to_dep_node` is expensive for some `DepKind`s.
429-
let dep_node =
430-
dep_node_opt.get_or_insert_with(|| DepNode::construct(tcx, query.dep_kind, &key));
431-
432424
// The diagnostics for this query will be promoted to the current session during
433425
// `try_mark_green()`, so we can ignore them here.
434426
if let Some(ret) = start_query(job_id, false, || try {
435-
let (prev_index, dep_node_index) = dep_graph_data.try_mark_green(tcx, dep_node)?;
427+
let (prev_index, dep_node_index) = dep_graph_data.try_mark_green(tcx, &dep_node)?;
436428
let value = load_from_disk_or_invoke_provider_green(
437429
tcx,
438430
dep_graph_data,
439431
query,
440432
key,
441-
dep_node,
433+
&dep_node,
442434
prev_index,
443435
dep_node_index,
444436
);
@@ -451,10 +443,6 @@ fn execute_job_incr<'tcx, C: QueryCache>(
451443
let prof_timer = tcx.prof.query_provider();
452444

453445
let (result, dep_node_index) = start_query(job_id, query.depth_limit, || {
454-
// `to_dep_node` is expensive for some `DepKind`s.
455-
let dep_node =
456-
dep_node_opt.unwrap_or_else(|| DepNode::construct(tcx, query.dep_kind, &key));
457-
458446
// Call the query provider.
459447
dep_graph_data.with_task(
460448
dep_node,
@@ -544,67 +532,54 @@ fn load_from_disk_or_invoke_provider_green<'tcx, C: QueryCache>(
544532
value
545533
}
546534

547-
/// Return value struct for [`check_if_ensure_can_skip_execution`].
548-
struct EnsureCanSkip {
549-
/// If true, the current `tcx.ensure_ok()` or `tcx.ensure_done()` query
550-
/// can return early without actually trying to execute.
551-
skip_execution: bool,
552-
/// A dep node that was prepared while checking whether execution can be
553-
/// skipped, to be reused by execution itself if _not_ skipped.
554-
dep_node: Option<DepNode>,
555-
}
556-
557535
/// Checks whether a `tcx.ensure_ok()` or `tcx.ensure_done()` query call can
558536
/// return early without actually trying to execute.
559537
///
560538
/// This only makes sense during incremental compilation, because it relies
561539
/// on having the dependency graph (and in some cases a disk-cached value)
562540
/// from the previous incr-comp session.
563541
#[inline(never)]
564-
fn check_if_ensure_can_skip_execution<'tcx, C: QueryCache>(
542+
fn ensure_can_skip_execution<'tcx, C: QueryCache>(
565543
query: &'tcx QueryVTable<'tcx, C>,
566544
tcx: TyCtxt<'tcx>,
567545
key: C::Key,
546+
dep_node: DepNode,
568547
ensure_mode: EnsureMode,
569-
) -> EnsureCanSkip {
548+
) -> bool {
570549
// Queries with `eval_always` should never skip execution.
571550
if query.eval_always {
572-
return EnsureCanSkip { skip_execution: false, dep_node: None };
551+
return false;
573552
}
574553

575-
let dep_node = DepNode::construct(tcx, query.dep_kind, &key);
576-
577-
let serialized_dep_node_index = match tcx.dep_graph.try_mark_green(tcx, &dep_node) {
554+
match tcx.dep_graph.try_mark_green(tcx, &dep_node) {
578555
None => {
579556
// A None return from `try_mark_green` means that this is either
580557
// a new dep node or that the dep node has already been marked red.
581558
// Either way, we can't call `dep_graph.read()` as we don't have the
582559
// DepNodeIndex. We must invoke the query itself. The performance cost
583560
// this introduces should be negligible as we'll immediately hit the
584561
// in-memory cache, or another query down the line will.
585-
return EnsureCanSkip { skip_execution: false, dep_node: Some(dep_node) };
562+
false
586563
}
587564
Some((serialized_dep_node_index, dep_node_index)) => {
588565
tcx.dep_graph.read_index(dep_node_index);
589566
tcx.prof.query_cache_hit(dep_node_index.into());
590-
serialized_dep_node_index
591-
}
592-
};
593-
594-
match ensure_mode {
595-
EnsureMode::Ok => {
596-
// In ensure-ok mode, we can skip execution for this key if the node
597-
// is green. It must have succeeded in the previous session, and
598-
// therefore would succeed in the current session if executed.
599-
EnsureCanSkip { skip_execution: true, dep_node: None }
600-
}
601-
EnsureMode::Done => {
602-
// In ensure-done mode, we can only skip execution for this key if
603-
// there's a disk-cached value available to load later if needed,
604-
// which guarantees the query provider will never run for this key.
605-
let is_loadable = (query.will_cache_on_disk_for_key_fn)(tcx, key)
606-
&& loadable_from_disk(tcx, serialized_dep_node_index);
607-
EnsureCanSkip { skip_execution: is_loadable, dep_node: Some(dep_node) }
567+
match ensure_mode {
568+
// In ensure-ok mode, we can skip execution for this key if the
569+
// node is green. It must have succeeded in the previous
570+
// session, and therefore would succeed in the current session
571+
// if executed.
572+
EnsureMode::Ok => true,
573+
574+
// In ensure-done mode, we can only skip execution for this key
575+
// if there's a disk-cached value available to load later if
576+
// needed, which guarantees the query provider will never run
577+
// for this key.
578+
EnsureMode::Done => {
579+
(query.will_cache_on_disk_for_key_fn)(tcx, key)
580+
&& loadable_from_disk(tcx, serialized_dep_node_index)
581+
}
582+
}
608583
}
609584
}
610585
}
@@ -618,8 +593,6 @@ pub(super) fn execute_query_non_incr_inner<'tcx, C: QueryCache>(
618593
span: Span,
619594
key: C::Key,
620595
) -> C::Value {
621-
debug_assert!(!tcx.dep_graph.is_fully_enabled());
622-
623596
ensure_sufficient_stack(|| try_execute_query::<C, false>(query, tcx, span, key, None).0)
624597
}
625598

@@ -633,26 +606,18 @@ pub(super) fn execute_query_incr_inner<'tcx, C: QueryCache>(
633606
key: C::Key,
634607
mode: QueryMode,
635608
) -> Option<C::Value> {
636-
debug_assert!(tcx.dep_graph.is_fully_enabled());
609+
let dep_node = DepNode::construct(tcx, query.dep_kind, &key);
637610

638611
// Check if query execution can be skipped, for `ensure_ok` or `ensure_done`.
639-
// This might have the side-effect of creating a suitable DepNode, which
640-
// we should reuse for execution instead of creating a new one.
641-
let dep_node: Option<DepNode> = match mode {
642-
QueryMode::Ensure { ensure_mode } => {
643-
let EnsureCanSkip { skip_execution, dep_node } =
644-
check_if_ensure_can_skip_execution(query, tcx, key, ensure_mode);
645-
if skip_execution {
646-
// Return early to skip execution.
647-
return None;
648-
}
649-
dep_node
650-
}
651-
QueryMode::Get => None,
652-
};
612+
if let QueryMode::Ensure { ensure_mode } = mode
613+
&& ensure_can_skip_execution(query, tcx, key, dep_node, ensure_mode)
614+
{
615+
return None;
616+
}
653617

654-
let (result, dep_node_index) =
655-
ensure_sufficient_stack(|| try_execute_query::<C, true>(query, tcx, span, key, dep_node));
618+
let (result, dep_node_index) = ensure_sufficient_stack(|| {
619+
try_execute_query::<C, true>(query, tcx, span, key, Some(dep_node))
620+
});
656621
if let Some(dep_node_index) = dep_node_index {
657622
tcx.dep_graph.read_index(dep_node_index)
658623
}

0 commit comments

Comments
 (0)