Skip to content

Commit df76ef0

Browse files
committed
Compute DepNode for incremental queries.
Prior to #154122 it wasn't used on all paths, so we only computed it when necessary -- sometimes in `check_if_ensure_can_skip_execution`, sometimes in one of two places in `execute_job_incr` -- and pass around `Option<DepNode>` to allow this. But now it's always used, so this commit makes us compute it earlier, which simplifies things. - `check_if_ensure_can_skip_execution` can be made simpler, returning a bool and eliminating the need for `EnsureCanSkip`. - `execute_job_incr` no longer uses two slightly different methods to create a `DepNode` (`get_or_insert_with` vs `unwrap_or_else`).
1 parent b425649 commit df76ef0

1 file changed

Lines changed: 36 additions & 65 deletions

File tree

compiler/rustc_query_impl/src/execution.rs

Lines changed: 36 additions & 65 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);
@@ -300,7 +298,7 @@ fn try_execute_query<'tcx, C: QueryCache, const INCR: bool>(
300298

301299
// Delegate to another function to actually execute the query job.
302300
let (value, dep_node_index) = if INCR {
303-
execute_job_incr(query, tcx, key, dep_node, id)
301+
execute_job_incr(query, tcx, key, dep_node.unwrap(), id)
304302
} else {
305303
execute_job_non_incr(query, tcx, key, id)
306304
};
@@ -416,27 +414,23 @@ fn execute_job_incr<'tcx, C: QueryCache>(
416414
query: &'tcx QueryVTable<'tcx, C>,
417415
tcx: TyCtxt<'tcx>,
418416
key: C::Key,
419-
mut dep_node_opt: Option<DepNode>,
417+
dep_node: DepNode,
420418
job_id: QueryJobId,
421419
) -> (C::Value, DepNodeIndex) {
422420
let dep_graph_data =
423421
tcx.dep_graph.data().expect("should always be present in incremental mode");
424422

425423
if !query.eval_always {
426-
// `to_dep_node` is expensive for some `DepKind`s.
427-
let dep_node =
428-
dep_node_opt.get_or_insert_with(|| DepNode::construct(tcx, query.dep_kind, &key));
429-
430424
// The diagnostics for this query will be promoted to the current session during
431425
// `try_mark_green()`, so we can ignore them here.
432426
if let Some(ret) = start_query(job_id, false, || try {
433-
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)?;
434428
let value = load_from_disk_or_invoke_provider_green(
435429
tcx,
436430
dep_graph_data,
437431
query,
438432
key,
439-
dep_node,
433+
&dep_node,
440434
prev_index,
441435
dep_node_index,
442436
);
@@ -449,10 +443,6 @@ fn execute_job_incr<'tcx, C: QueryCache>(
449443
let prof_timer = tcx.prof.query_provider();
450444

451445
let (result, dep_node_index) = start_query(job_id, query.depth_limit, || {
452-
// `to_dep_node` is expensive for some `DepKind`s.
453-
let dep_node =
454-
dep_node_opt.unwrap_or_else(|| DepNode::construct(tcx, query.dep_kind, &key));
455-
456446
// Call the query provider.
457447
dep_graph_data.with_task(
458448
dep_node,
@@ -564,66 +554,53 @@ fn load_from_disk_or_invoke_provider_green<'tcx, C: QueryCache>(
564554
value
565555
}
566556

567-
/// Return value struct for [`check_if_ensure_can_skip_execution`].
568-
struct EnsureCanSkip {
569-
/// If true, the current `tcx.ensure_ok()` or `tcx.ensure_done()` query
570-
/// can return early without actually trying to execute.
571-
skip_execution: bool,
572-
/// A dep node that was prepared while checking whether execution can be
573-
/// skipped, to be reused by execution itself if _not_ skipped.
574-
dep_node: Option<DepNode>,
575-
}
576-
577557
/// Checks whether a `tcx.ensure_ok()` or `tcx.ensure_done()` query call can
578558
/// return early without actually trying to execute.
579559
///
580560
/// This only makes sense during incremental compilation, because it relies
581561
/// on having the dependency graph (and in some cases a disk-cached value)
582562
/// from the previous incr-comp session.
583563
#[inline(never)]
584-
fn check_if_ensure_can_skip_execution<'tcx, C: QueryCache>(
564+
fn ensure_can_skip_execution<'tcx, C: QueryCache>(
585565
query: &'tcx QueryVTable<'tcx, C>,
586566
tcx: TyCtxt<'tcx>,
587567
key: C::Key,
568+
dep_node: DepNode,
588569
ensure_mode: EnsureMode,
589-
) -> EnsureCanSkip {
570+
) -> bool {
590571
// Queries with `eval_always` should never skip execution.
591572
if query.eval_always {
592-
return EnsureCanSkip { skip_execution: false, dep_node: None };
573+
return false;
593574
}
594575

595-
let dep_node = DepNode::construct(tcx, query.dep_kind, &key);
596-
597-
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) {
598577
None => {
599578
// A None return from `try_mark_green` means that this is either
600579
// a new dep node or that the dep node has already been marked red.
601580
// Either way, we can't call `dep_graph.read()` as we don't have the
602581
// DepNodeIndex. We must invoke the query itself. The performance cost
603582
// this introduces should be negligible as we'll immediately hit the
604583
// in-memory cache, or another query down the line will.
605-
return EnsureCanSkip { skip_execution: false, dep_node: Some(dep_node) };
584+
false
606585
}
607586
Some((serialized_dep_node_index, dep_node_index)) => {
608587
tcx.dep_graph.read_index(dep_node_index);
609588
tcx.prof.query_cache_hit(dep_node_index.into());
610-
serialized_dep_node_index
611-
}
612-
};
613-
614-
match ensure_mode {
615-
EnsureMode::Ok => {
616-
// In ensure-ok mode, we can skip execution for this key if the node
617-
// is green. It must have succeeded in the previous session, and
618-
// therefore would succeed in the current session if executed.
619-
EnsureCanSkip { skip_execution: true, dep_node: None }
620-
}
621-
EnsureMode::Done => {
622-
// In ensure-done mode, we can only skip execution for this key if
623-
// there's a disk-cached value available to load later if needed,
624-
// which guarantees the query provider will never run for this key.
625-
let is_loadable = (query.is_loadable_from_disk_fn)(tcx, key, serialized_dep_node_index);
626-
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+
}
627604
}
628605
}
629606
}
@@ -650,24 +627,18 @@ pub(super) fn execute_query_incr_inner<'tcx, C: QueryCache>(
650627
key: C::Key,
651628
mode: QueryMode,
652629
) -> Option<C::Value> {
630+
let dep_node = DepNode::construct(tcx, query.dep_kind, &key);
631+
653632
// Check if query execution can be skipped, for `ensure_ok` or `ensure_done`.
654-
// This might have the side-effect of creating a suitable DepNode, which
655-
// we should reuse for execution instead of creating a new one.
656-
let dep_node: Option<DepNode> = match mode {
657-
QueryMode::Ensure { ensure_mode } => {
658-
let EnsureCanSkip { skip_execution, dep_node } =
659-
check_if_ensure_can_skip_execution(query, tcx, key, ensure_mode);
660-
if skip_execution {
661-
// Return early to skip execution.
662-
return None;
663-
}
664-
dep_node
665-
}
666-
QueryMode::Get => None,
667-
};
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+
}
668638

669-
let (result, dep_node_index) =
670-
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+
});
671642
if let Some(dep_node_index) = dep_node_index {
672643
tcx.dep_graph.read_index(dep_node_index)
673644
}

0 commit comments

Comments
 (0)