Skip to content

Commit 72bc4f3

Browse files
committed
Auto merge of #154076 - nnethercote:DepNode-improvements, r=<try>
`DepNode` use improvements
2 parents 8b86f48 + 153cb38 commit 72bc4f3

File tree

1 file changed

+36
-67
lines changed

1 file changed

+36
-67
lines changed

compiler/rustc_query_impl/src/execution.rs

Lines changed: 36 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,9 @@ fn try_execute_query<'tcx, C: QueryCache, const INCR: bool>(
298298
// panic occurs while executing the query (or any intermediate plumbing).
299299
let job_guard = ActiveJobGuard { state: &query.state, key, key_hash };
300300

301-
debug_assert_eq!(tcx.dep_graph.is_fully_enabled(), INCR);
302-
303301
// Delegate to another function to actually execute the query job.
304302
let (value, dep_node_index) = if INCR {
305-
execute_job_incr(query, tcx, key, dep_node, id)
303+
execute_job_incr(query, tcx, key, dep_node.unwrap(), id)
306304
} else {
307305
execute_job_non_incr(query, tcx, key, id)
308306
};
@@ -418,27 +416,23 @@ fn execute_job_incr<'tcx, C: QueryCache>(
418416
query: &'tcx QueryVTable<'tcx, C>,
419417
tcx: TyCtxt<'tcx>,
420418
key: C::Key,
421-
mut dep_node_opt: Option<DepNode>,
419+
dep_node: DepNode,
422420
job_id: QueryJobId,
423421
) -> (C::Value, DepNodeIndex) {
424422
let dep_graph_data =
425423
tcx.dep_graph.data().expect("should always be present in incremental mode");
426424

427425
if !query.anon && !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-
432426
// The diagnostics for this query will be promoted to the current session during
433427
// `try_mark_green()`, so we can ignore them here.
434428
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)?;
429+
let (prev_index, dep_node_index) = dep_graph_data.try_mark_green(tcx, &dep_node)?;
436430
let value = load_from_disk_or_invoke_provider_green(
437431
tcx,
438432
dep_graph_data,
439433
query,
440434
key,
441-
dep_node,
435+
&dep_node,
442436
prev_index,
443437
dep_node_index,
444438
);
@@ -458,10 +452,6 @@ fn execute_job_incr<'tcx, C: QueryCache>(
458452
});
459453
}
460454

461-
// `to_dep_node` is expensive for some `DepKind`s.
462-
let dep_node =
463-
dep_node_opt.unwrap_or_else(|| DepNode::construct(tcx, query.dep_kind, &key));
464-
465455
// Call the query provider.
466456
dep_graph_data.with_task(
467457
dep_node,
@@ -573,69 +563,56 @@ fn load_from_disk_or_invoke_provider_green<'tcx, C: QueryCache>(
573563
value
574564
}
575565

576-
/// Return value struct for [`check_if_ensure_can_skip_execution`].
577-
struct EnsureCanSkip {
578-
/// If true, the current `tcx.ensure_ok()` or `tcx.ensure_done()` query
579-
/// can return early without actually trying to execute.
580-
skip_execution: bool,
581-
/// A dep node that was prepared while checking whether execution can be
582-
/// skipped, to be reused by execution itself if _not_ skipped.
583-
dep_node: Option<DepNode>,
584-
}
585-
586566
/// Checks whether a `tcx.ensure_ok()` or `tcx.ensure_done()` query call can
587567
/// return early without actually trying to execute.
588568
///
589569
/// This only makes sense during incremental compilation, because it relies
590570
/// on having the dependency graph (and in some cases a disk-cached value)
591571
/// from the previous incr-comp session.
592572
#[inline(never)]
593-
fn check_if_ensure_can_skip_execution<'tcx, C: QueryCache>(
573+
fn can_ensure_skip_execution<'tcx, C: QueryCache>(
594574
query: &'tcx QueryVTable<'tcx, C>,
595575
tcx: TyCtxt<'tcx>,
596576
key: C::Key,
577+
dep_node: DepNode,
597578
ensure_mode: EnsureMode,
598-
) -> EnsureCanSkip {
579+
) -> bool {
599580
// Queries with `eval_always` should never skip execution.
600581
if query.eval_always {
601-
return EnsureCanSkip { skip_execution: false, dep_node: None };
582+
return false;
602583
}
603584

604585
// Ensuring an anonymous query makes no sense
605586
assert!(!query.anon);
606587

607-
let dep_node = DepNode::construct(tcx, query.dep_kind, &key);
608-
609-
let serialized_dep_node_index = match tcx.dep_graph.try_mark_green(tcx, &dep_node) {
588+
match tcx.dep_graph.try_mark_green(tcx, &dep_node) {
610589
None => {
611590
// A None return from `try_mark_green` means that this is either
612591
// a new dep node or that the dep node has already been marked red.
613592
// Either way, we can't call `dep_graph.read()` as we don't have the
614593
// DepNodeIndex. We must invoke the query itself. The performance cost
615594
// this introduces should be negligible as we'll immediately hit the
616595
// in-memory cache, or another query down the line will.
617-
return EnsureCanSkip { skip_execution: false, dep_node: Some(dep_node) };
596+
false
618597
}
619598
Some((serialized_dep_node_index, dep_node_index)) => {
620599
tcx.dep_graph.read_index(dep_node_index);
621600
tcx.prof.query_cache_hit(dep_node_index.into());
622-
serialized_dep_node_index
623-
}
624-
};
625-
626-
match ensure_mode {
627-
EnsureMode::Ok => {
628-
// In ensure-ok mode, we can skip execution for this key if the node
629-
// is green. It must have succeeded in the previous session, and
630-
// therefore would succeed in the current session if executed.
631-
EnsureCanSkip { skip_execution: true, dep_node: None }
632-
}
633-
EnsureMode::Done => {
634-
// In ensure-done mode, we can only skip execution for this key if
635-
// there's a disk-cached value available to load later if needed,
636-
// which guarantees the query provider will never run for this key.
637-
let is_loadable = (query.is_loadable_from_disk_fn)(tcx, key, serialized_dep_node_index);
638-
EnsureCanSkip { skip_execution: is_loadable, dep_node: Some(dep_node) }
601+
match ensure_mode {
602+
// In ensure-ok mode, we can skip execution for this key if the
603+
// node is green. It must have succeeded in the previous
604+
// session, and therefore would succeed in the current session
605+
// if executed.
606+
EnsureMode::Ok => true,
607+
608+
// In ensure-done mode, we can only skip execution for this key
609+
// if there's a disk-cached value available to load later if
610+
// needed, which guarantees the query provider will never run
611+
// for this key.
612+
EnsureMode::Done => {
613+
(query.is_loadable_from_disk_fn)(tcx, key, serialized_dep_node_index)
614+
}
615+
}
639616
}
640617
}
641618
}
@@ -649,8 +626,6 @@ pub(super) fn execute_query_non_incr_inner<'tcx, C: QueryCache>(
649626
span: Span,
650627
key: C::Key,
651628
) -> C::Value {
652-
debug_assert!(!tcx.dep_graph.is_fully_enabled());
653-
654629
ensure_sufficient_stack(|| try_execute_query::<C, false>(query, tcx, span, key, None).0)
655630
}
656631

@@ -664,26 +639,20 @@ pub(super) fn execute_query_incr_inner<'tcx, C: QueryCache>(
664639
key: C::Key,
665640
mode: QueryMode,
666641
) -> Option<C::Value> {
667-
debug_assert!(tcx.dep_graph.is_fully_enabled());
642+
// There are scenarios where this `dep_node` is never used (e.g. for `anon` queries) but they
643+
// are rare enough that the wasted effort doesn't materially hurt performance.
644+
let dep_node = DepNode::construct(tcx, query.dep_kind, &key);
668645

669646
// Check if query execution can be skipped, for `ensure_ok` or `ensure_done`.
670-
// This might have the side-effect of creating a suitable DepNode, which
671-
// we should reuse for execution instead of creating a new one.
672-
let dep_node: Option<DepNode> = match mode {
673-
QueryMode::Ensure { ensure_mode } => {
674-
let EnsureCanSkip { skip_execution, dep_node } =
675-
check_if_ensure_can_skip_execution(query, tcx, key, ensure_mode);
676-
if skip_execution {
677-
// Return early to skip execution.
678-
return None;
679-
}
680-
dep_node
681-
}
682-
QueryMode::Get => None,
683-
};
647+
if let QueryMode::Ensure { ensure_mode } = mode
648+
&& can_ensure_skip_execution(query, tcx, key, dep_node, ensure_mode)
649+
{
650+
return None;
651+
}
684652

685-
let (result, dep_node_index) =
686-
ensure_sufficient_stack(|| try_execute_query::<C, true>(query, tcx, span, key, dep_node));
653+
let (result, dep_node_index) = ensure_sufficient_stack(|| {
654+
try_execute_query::<C, true>(query, tcx, span, key, Some(dep_node))
655+
});
687656
if let Some(dep_node_index) = dep_node_index {
688657
tcx.dep_graph.read_index(dep_node_index)
689658
}

0 commit comments

Comments
 (0)