Skip to content

Commit dd56731

Browse files
committed
Auto merge of #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 13e2aba + df76ef0 commit dd56731

File tree

1 file changed

+35
-70
lines changed

1 file changed

+35
-70
lines changed

compiler/rustc_query_impl/src/execution.rs

Lines changed: 35 additions & 70 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,
@@ -566,66 +554,53 @@ fn load_from_disk_or_invoke_provider_green<'tcx, C: QueryCache>(
566554
value
567555
}
568556

569-
/// Return value struct for [`check_if_ensure_can_skip_execution`].
570-
struct EnsureCanSkip {
571-
/// If true, the current `tcx.ensure_ok()` or `tcx.ensure_done()` query
572-
/// can return early without actually trying to execute.
573-
skip_execution: bool,
574-
/// A dep node that was prepared while checking whether execution can be
575-
/// skipped, to be reused by execution itself if _not_ skipped.
576-
dep_node: Option<DepNode>,
577-
}
578-
579557
/// Checks whether a `tcx.ensure_ok()` or `tcx.ensure_done()` query call can
580558
/// return early without actually trying to execute.
581559
///
582560
/// This only makes sense during incremental compilation, because it relies
583561
/// on having the dependency graph (and in some cases a disk-cached value)
584562
/// from the previous incr-comp session.
585563
#[inline(never)]
586-
fn check_if_ensure_can_skip_execution<'tcx, C: QueryCache>(
564+
fn ensure_can_skip_execution<'tcx, C: QueryCache>(
587565
query: &'tcx QueryVTable<'tcx, C>,
588566
tcx: TyCtxt<'tcx>,
589567
key: C::Key,
568+
dep_node: DepNode,
590569
ensure_mode: EnsureMode,
591-
) -> EnsureCanSkip {
570+
) -> bool {
592571
// Queries with `eval_always` should never skip execution.
593572
if query.eval_always {
594-
return EnsureCanSkip { skip_execution: false, dep_node: None };
573+
return false;
595574
}
596575

597-
let dep_node = DepNode::construct(tcx, query.dep_kind, &key);
598-
599-
let serialized_dep_node_index = match tcx.dep_graph.try_mark_green(tcx, &dep_node) {
576+
match tcx.dep_graph.try_mark_green(tcx, &dep_node) {
600577
None => {
601578
// A None return from `try_mark_green` means that this is either
602579
// a new dep node or that the dep node has already been marked red.
603580
// Either way, we can't call `dep_graph.read()` as we don't have the
604581
// DepNodeIndex. We must invoke the query itself. The performance cost
605582
// this introduces should be negligible as we'll immediately hit the
606583
// in-memory cache, or another query down the line will.
607-
return EnsureCanSkip { skip_execution: false, dep_node: Some(dep_node) };
584+
false
608585
}
609586
Some((serialized_dep_node_index, dep_node_index)) => {
610587
tcx.dep_graph.read_index(dep_node_index);
611588
tcx.prof.query_cache_hit(dep_node_index.into());
612-
serialized_dep_node_index
613-
}
614-
};
615-
616-
match ensure_mode {
617-
EnsureMode::Ok => {
618-
// In ensure-ok mode, we can skip execution for this key if the node
619-
// is green. It must have succeeded in the previous session, and
620-
// therefore would succeed in the current session if executed.
621-
EnsureCanSkip { skip_execution: true, dep_node: None }
622-
}
623-
EnsureMode::Done => {
624-
// In ensure-done mode, we can only skip execution for this key if
625-
// there's a disk-cached value available to load later if needed,
626-
// which guarantees the query provider will never run for this key.
627-
let is_loadable = (query.is_loadable_from_disk_fn)(tcx, key, serialized_dep_node_index);
628-
EnsureCanSkip { skip_execution: is_loadable, dep_node: Some(dep_node) }
589+
match ensure_mode {
590+
// In ensure-ok mode, we can skip execution for this key if the
591+
// node is green. It must have succeeded in the previous
592+
// session, and therefore would succeed in the current session
593+
// if executed.
594+
EnsureMode::Ok => true,
595+
596+
// In ensure-done mode, we can only skip execution for this key
597+
// if there's a disk-cached value available to load later if
598+
// needed, which guarantees the query provider will never run
599+
// for this key.
600+
EnsureMode::Done => {
601+
(query.is_loadable_from_disk_fn)(tcx, key, serialized_dep_node_index)
602+
}
603+
}
629604
}
630605
}
631606
}
@@ -639,8 +614,6 @@ pub(super) fn execute_query_non_incr_inner<'tcx, C: QueryCache>(
639614
span: Span,
640615
key: C::Key,
641616
) -> C::Value {
642-
debug_assert!(!tcx.dep_graph.is_fully_enabled());
643-
644617
ensure_sufficient_stack(|| try_execute_query::<C, false>(query, tcx, span, key, None).0)
645618
}
646619

@@ -654,26 +627,18 @@ pub(super) fn execute_query_incr_inner<'tcx, C: QueryCache>(
654627
key: C::Key,
655628
mode: QueryMode,
656629
) -> Option<C::Value> {
657-
debug_assert!(tcx.dep_graph.is_fully_enabled());
630+
let dep_node = DepNode::construct(tcx, query.dep_kind, &key);
658631

659632
// Check if query execution can be skipped, for `ensure_ok` or `ensure_done`.
660-
// This might have the side-effect of creating a suitable DepNode, which
661-
// we should reuse for execution instead of creating a new one.
662-
let dep_node: Option<DepNode> = match mode {
663-
QueryMode::Ensure { ensure_mode } => {
664-
let EnsureCanSkip { skip_execution, dep_node } =
665-
check_if_ensure_can_skip_execution(query, tcx, key, ensure_mode);
666-
if skip_execution {
667-
// Return early to skip execution.
668-
return None;
669-
}
670-
dep_node
671-
}
672-
QueryMode::Get => None,
673-
};
633+
if let QueryMode::Ensure { ensure_mode } = mode
634+
&& ensure_can_skip_execution(query, tcx, key, dep_node, ensure_mode)
635+
{
636+
return None;
637+
}
674638

675-
let (result, dep_node_index) =
676-
ensure_sufficient_stack(|| try_execute_query::<C, true>(query, tcx, span, key, dep_node));
639+
let (result, dep_node_index) = ensure_sufficient_stack(|| {
640+
try_execute_query::<C, true>(query, tcx, span, key, Some(dep_node))
641+
});
677642
if let Some(dep_node_index) = dep_node_index {
678643
tcx.dep_graph.read_index(dep_node_index)
679644
}

0 commit comments

Comments
 (0)