Skip to content

Commit 283a8e1

Browse files
committed
Make all query forcing go through try_execute_query.
try_execute_query is now able to centralize the path for query get/ensure/force. try_execute_query now takes the dep_node as a parameter, so it can accommodate `force`. This dep_node is an Option to avoid computing it in the `get` fast path. try_execute_query now returns both the result and the dep_node_index to allow the caller to handle the dep graph. The caller is responsible for marking the dependency.
1 parent 45d6dec commit 283a8e1

File tree

1 file changed

+31
-40
lines changed

1 file changed

+31
-40
lines changed

compiler/rustc_query_system/src/query/plumbing.rs

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -427,12 +427,13 @@ fn try_execute_query<CTX, C>(
427427
span: Span,
428428
key: C::Key,
429429
lookup: QueryLookup,
430+
dep_node: Option<DepNode<CTX::DepKind>>,
430431
query: &QueryVtable<CTX, C::Key, C::Value>,
431432
compute: fn(CTX::DepContext, C::Key) -> C::Value,
432-
) -> C::Stored
433+
) -> (C::Stored, Option<DepNodeIndex>)
433434
where
434435
C: QueryCache,
435-
C::Key: DepNodeParams<CTX::DepContext>,
436+
C::Key: Clone + DepNodeParams<CTX::DepContext>,
436437
CTX: QueryContext,
437438
{
438439
let job = match JobOwner::<'_, CTX::DepKind, C>::try_start(
@@ -445,11 +446,10 @@ where
445446
query,
446447
) {
447448
TryGetJob::NotYetStarted(job) => job,
448-
TryGetJob::Cycle(result) => return result,
449+
TryGetJob::Cycle(result) => return (result, None),
449450
#[cfg(parallel_compiler)]
450451
TryGetJob::JobCompleted((v, index)) => {
451-
tcx.dep_context().dep_graph().read_index(index);
452-
return v;
452+
return (v, Some(index));
453453
}
454454
};
455455

@@ -461,10 +461,11 @@ where
461461
let result = tcx.start_query(job.id, None, || compute(*tcx.dep_context(), key));
462462
let dep_node_index = dep_graph.next_virtual_depnode_index();
463463
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
464-
return job.complete(result, dep_node_index);
464+
let result = job.complete(result, dep_node_index);
465+
return (result, None);
465466
}
466467

467-
if query.anon {
468+
let (result, dep_node_index) = if query.anon {
468469
let prof_timer = tcx.dep_context().profiler().query_provider();
469470

470471
let ((result, dep_node_index), diagnostics) = with_diagnostics(|diagnostics| {
@@ -477,34 +478,35 @@ where
477478

478479
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
479480

480-
dep_graph.read_index(dep_node_index);
481-
482481
let side_effects = QuerySideEffects { diagnostics };
483482

484483
if unlikely!(!side_effects.is_empty()) {
485484
tcx.store_side_effects_for_anon_node(dep_node_index, side_effects);
486485
}
487486

488-
return job.complete(result, dep_node_index);
489-
}
490-
491-
let dep_node = query.to_dep_node(*tcx.dep_context(), &key);
492-
493-
if !query.eval_always {
487+
let result = job.complete(result, dep_node_index);
488+
(result, dep_node_index)
489+
} else if query.eval_always {
490+
// `to_dep_node` is expensive for some `DepKind`s.
491+
let dep_node = dep_node.unwrap_or_else(|| query.to_dep_node(*tcx.dep_context(), &key));
492+
force_query_with_job(tcx, key, job, dep_node, query, compute)
493+
} else {
494+
// `to_dep_node` is expensive for some `DepKind`s.
495+
let dep_node = dep_node.unwrap_or_else(|| query.to_dep_node(*tcx.dep_context(), &key));
494496
// The diagnostics for this query will be
495497
// promoted to the current session during
496498
// `try_mark_green()`, so we can ignore them here.
497499
let loaded = tcx.start_query(job.id, None, || {
498500
try_load_from_disk_and_cache_in_memory(tcx, &key, &dep_node, query, compute)
499501
});
500502
if let Some((result, dep_node_index)) = loaded {
501-
return job.complete(result, dep_node_index);
503+
let result = job.complete(result, dep_node_index);
504+
(result, dep_node_index)
505+
} else {
506+
force_query_with_job(tcx, key, job, dep_node, query, compute)
502507
}
503-
}
504-
505-
let (result, dep_node_index) = force_query_with_job(tcx, key, job, dep_node, query, compute);
506-
dep_graph.read_index(dep_node_index);
507-
result
508+
};
509+
(result, Some(dep_node_index))
508510
}
509511

510512
fn try_load_from_disk_and_cache_in_memory<CTX, K, V>(
@@ -524,7 +526,6 @@ where
524526

525527
let (prev_dep_node_index, dep_node_index) =
526528
tcx.dep_context().dep_graph().try_mark_green(tcx, &dep_node)?;
527-
tcx.dep_context().dep_graph().read_index(dep_node_index);
528529

529530
debug_assert!(tcx.dep_context().dep_graph().is_green(dep_node));
530531

@@ -700,7 +701,12 @@ where
700701
C: QueryCache,
701702
C::Key: DepNodeParams<CTX::DepContext>,
702703
{
703-
try_execute_query(tcx, state, cache, span, key, lookup, query, compute)
704+
let (result, dep_node_index) =
705+
try_execute_query(tcx, state, cache, span, key, lookup, None, query, compute);
706+
if let Some(dep_node_index) = dep_node_index {
707+
tcx.dep_context().dep_graph().read_index(dep_node_index)
708+
}
709+
result
704710
}
705711

706712
/// Ensure that either this query has all green inputs or been executed.
@@ -779,23 +785,8 @@ where
779785
Err(lookup) => lookup,
780786
};
781787

782-
let job = match JobOwner::<'_, CTX::DepKind, C>::try_start(
783-
tcx,
784-
state,
785-
cache,
786-
DUMMY_SP,
787-
key.clone(),
788-
lookup,
789-
query,
790-
) {
791-
TryGetJob::NotYetStarted(job) => job,
792-
TryGetJob::Cycle(_) => return true,
793-
#[cfg(parallel_compiler)]
794-
TryGetJob::JobCompleted(_) => return true,
795-
};
796-
797-
force_query_with_job(tcx, key, job, dep_node, query, compute);
798-
788+
let _ =
789+
try_execute_query(tcx, state, cache, DUMMY_SP, key, lookup, Some(dep_node), query, compute);
799790
true
800791
}
801792

0 commit comments

Comments
 (0)