Skip to content

Commit f60a670

Browse files
committed
Auto merge of #85319 - cjgillot:query-simp, r=Mark-Simulacrum
Simplification of query forcing Extracted from #78780
2 parents d93b6a4 + f3ed997 commit f60a670

File tree

5 files changed

+167
-171
lines changed

5 files changed

+167
-171
lines changed

compiler/rustc_query_impl/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_middle::ty::query::{query_keys, query_storage, query_stored, query_val
2626
use rustc_middle::ty::query::{Providers, QueryEngine};
2727
use rustc_middle::ty::{self, TyCtxt};
2828
use rustc_serialize::opaque;
29-
use rustc_span::{Span, DUMMY_SP};
29+
use rustc_span::Span;
3030

3131
#[macro_use]
3232
mod plumbing;

compiler/rustc_query_impl/src/plumbing.rs

+3-19
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_data_structures::sync::Lock;
1414
use rustc_data_structures::thin_vec::ThinVec;
1515
use rustc_errors::Diagnostic;
1616
use rustc_serialize::opaque;
17-
use rustc_span::def_id::{DefId, LocalDefId};
17+
use rustc_span::def_id::LocalDefId;
1818

1919
#[derive(Copy, Clone)]
2020
pub struct QueryCtxt<'tcx> {
@@ -25,6 +25,7 @@ pub struct QueryCtxt<'tcx> {
2525
impl<'tcx> std::ops::Deref for QueryCtxt<'tcx> {
2626
type Target = TyCtxt<'tcx>;
2727

28+
#[inline]
2829
fn deref(&self) -> &Self::Target {
2930
&self.tcx
3031
}
@@ -42,10 +43,6 @@ impl HasDepContext for QueryCtxt<'tcx> {
4243
}
4344

4445
impl QueryContext for QueryCtxt<'tcx> {
45-
fn def_path_str(&self, def_id: DefId) -> String {
46-
self.tcx.def_path_str(def_id)
47-
}
48-
4946
fn current_query_job(&self) -> Option<QueryJobId<Self::DepKind>> {
5047
tls::with_related_context(**self, |icx| icx.query)
5148
}
@@ -457,20 +454,7 @@ macro_rules! define_queries {
457454
}
458455

459456
fn force_from_dep_node(tcx: QueryCtxt<'_>, dep_node: &DepNode) -> bool {
460-
if is_anon {
461-
return false;
462-
}
463-
464-
if !can_reconstruct_query_key() {
465-
return false;
466-
}
467-
468-
if let Some(key) = recover(*tcx, dep_node) {
469-
force_query::<queries::$name<'_>, _>(tcx, key, DUMMY_SP, *dep_node);
470-
return true;
471-
}
472-
473-
false
457+
force_query::<queries::$name<'_>, _>(tcx, dep_node)
474458
}
475459

476460
fn try_load_from_on_disk_cache(tcx: QueryCtxt<'_>, dep_node: &DepNode) {

compiler/rustc_query_system/src/dep_graph/graph.rs

+113-118
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,117 @@ impl<K: DepKind> DepGraph<K> {
487487
}
488488
}
489489

490+
fn try_mark_parent_green<Ctxt: QueryContext<DepKind = K>>(
491+
&self,
492+
tcx: Ctxt,
493+
data: &DepGraphData<K>,
494+
parent_dep_node_index: SerializedDepNodeIndex,
495+
dep_node: &DepNode<K>,
496+
) -> Option<()> {
497+
let dep_dep_node_color = data.colors.get(parent_dep_node_index);
498+
let dep_dep_node = &data.previous.index_to_node(parent_dep_node_index);
499+
500+
match dep_dep_node_color {
501+
Some(DepNodeColor::Green(_)) => {
502+
// This dependency has been marked as green before, we are
503+
// still fine and can continue with checking the other
504+
// dependencies.
505+
debug!(
506+
"try_mark_previous_green({:?}) --- found dependency {:?} to \
507+
be immediately green",
508+
dep_node, dep_dep_node,
509+
);
510+
return Some(());
511+
}
512+
Some(DepNodeColor::Red) => {
513+
// We found a dependency the value of which has changed
514+
// compared to the previous compilation session. We cannot
515+
// mark the DepNode as green and also don't need to bother
516+
// with checking any of the other dependencies.
517+
debug!(
518+
"try_mark_previous_green({:?}) - END - dependency {:?} was immediately red",
519+
dep_node, dep_dep_node,
520+
);
521+
return None;
522+
}
523+
None => {}
524+
}
525+
526+
// We don't know the state of this dependency. If it isn't
527+
// an eval_always node, let's try to mark it green recursively.
528+
if !dep_dep_node.kind.is_eval_always() {
529+
debug!(
530+
"try_mark_previous_green({:?}) --- state of dependency {:?} ({}) \
531+
is unknown, trying to mark it green",
532+
dep_node, dep_dep_node, dep_dep_node.hash,
533+
);
534+
535+
let node_index =
536+
self.try_mark_previous_green(tcx, data, parent_dep_node_index, dep_dep_node);
537+
if node_index.is_some() {
538+
debug!(
539+
"try_mark_previous_green({:?}) --- managed to MARK dependency {:?} as green",
540+
dep_node, dep_dep_node
541+
);
542+
return Some(());
543+
}
544+
}
545+
546+
// We failed to mark it green, so we try to force the query.
547+
debug!(
548+
"try_mark_previous_green({:?}) --- trying to force dependency {:?}",
549+
dep_node, dep_dep_node
550+
);
551+
if !tcx.try_force_from_dep_node(dep_dep_node) {
552+
// The DepNode could not be forced.
553+
debug!(
554+
"try_mark_previous_green({:?}) - END - dependency {:?} could not be forced",
555+
dep_node, dep_dep_node
556+
);
557+
return None;
558+
}
559+
560+
let dep_dep_node_color = data.colors.get(parent_dep_node_index);
561+
562+
match dep_dep_node_color {
563+
Some(DepNodeColor::Green(_)) => {
564+
debug!(
565+
"try_mark_previous_green({:?}) --- managed to FORCE dependency {:?} to green",
566+
dep_node, dep_dep_node
567+
);
568+
return Some(());
569+
}
570+
Some(DepNodeColor::Red) => {
571+
debug!(
572+
"try_mark_previous_green({:?}) - END - dependency {:?} was red after forcing",
573+
dep_node, dep_dep_node
574+
);
575+
return None;
576+
}
577+
None => {}
578+
}
579+
580+
if !tcx.dep_context().sess().has_errors_or_delayed_span_bugs() {
581+
panic!("try_mark_previous_green() - Forcing the DepNode should have set its color")
582+
}
583+
584+
// If the query we just forced has resulted in
585+
// some kind of compilation error, we cannot rely on
586+
// the dep-node color having been properly updated.
587+
// This means that the query system has reached an
588+
// invalid state. We let the compiler continue (by
589+
// returning `None`) so it can emit error messages
590+
// and wind down, but rely on the fact that this
591+
// invalid state will not be persisted to the
592+
// incremental compilation cache because of
593+
// compilation errors being present.
594+
debug!(
595+
"try_mark_previous_green({:?}) - END - dependency {:?} resulted in compilation error",
596+
dep_node, dep_dep_node
597+
);
598+
return None;
599+
}
600+
490601
/// Try to mark a dep-node which existed in the previous compilation session as green.
491602
fn try_mark_previous_green<Ctxt: QueryContext<DepKind = K>>(
492603
&self,
@@ -511,123 +622,7 @@ impl<K: DepKind> DepGraph<K> {
511622
let prev_deps = data.previous.edge_targets_from(prev_dep_node_index);
512623

513624
for &dep_dep_node_index in prev_deps {
514-
let dep_dep_node_color = data.colors.get(dep_dep_node_index);
515-
516-
match dep_dep_node_color {
517-
Some(DepNodeColor::Green(_)) => {
518-
// This dependency has been marked as green before, we are
519-
// still fine and can continue with checking the other
520-
// dependencies.
521-
debug!(
522-
"try_mark_previous_green({:?}) --- found dependency {:?} to \
523-
be immediately green",
524-
dep_node,
525-
data.previous.index_to_node(dep_dep_node_index)
526-
);
527-
}
528-
Some(DepNodeColor::Red) => {
529-
// We found a dependency the value of which has changed
530-
// compared to the previous compilation session. We cannot
531-
// mark the DepNode as green and also don't need to bother
532-
// with checking any of the other dependencies.
533-
debug!(
534-
"try_mark_previous_green({:?}) - END - dependency {:?} was \
535-
immediately red",
536-
dep_node,
537-
data.previous.index_to_node(dep_dep_node_index)
538-
);
539-
return None;
540-
}
541-
None => {
542-
let dep_dep_node = &data.previous.index_to_node(dep_dep_node_index);
543-
544-
// We don't know the state of this dependency. If it isn't
545-
// an eval_always node, let's try to mark it green recursively.
546-
if !dep_dep_node.kind.is_eval_always() {
547-
debug!(
548-
"try_mark_previous_green({:?}) --- state of dependency {:?} ({}) \
549-
is unknown, trying to mark it green",
550-
dep_node, dep_dep_node, dep_dep_node.hash,
551-
);
552-
553-
let node_index = self.try_mark_previous_green(
554-
tcx,
555-
data,
556-
dep_dep_node_index,
557-
dep_dep_node,
558-
);
559-
if node_index.is_some() {
560-
debug!(
561-
"try_mark_previous_green({:?}) --- managed to MARK \
562-
dependency {:?} as green",
563-
dep_node, dep_dep_node
564-
);
565-
continue;
566-
}
567-
}
568-
569-
// We failed to mark it green, so we try to force the query.
570-
debug!(
571-
"try_mark_previous_green({:?}) --- trying to force \
572-
dependency {:?}",
573-
dep_node, dep_dep_node
574-
);
575-
if tcx.try_force_from_dep_node(dep_dep_node) {
576-
let dep_dep_node_color = data.colors.get(dep_dep_node_index);
577-
578-
match dep_dep_node_color {
579-
Some(DepNodeColor::Green(_)) => {
580-
debug!(
581-
"try_mark_previous_green({:?}) --- managed to \
582-
FORCE dependency {:?} to green",
583-
dep_node, dep_dep_node
584-
);
585-
}
586-
Some(DepNodeColor::Red) => {
587-
debug!(
588-
"try_mark_previous_green({:?}) - END - \
589-
dependency {:?} was red after forcing",
590-
dep_node, dep_dep_node
591-
);
592-
return None;
593-
}
594-
None => {
595-
if !tcx.dep_context().sess().has_errors_or_delayed_span_bugs() {
596-
panic!(
597-
"try_mark_previous_green() - Forcing the DepNode \
598-
should have set its color"
599-
)
600-
} else {
601-
// If the query we just forced has resulted in
602-
// some kind of compilation error, we cannot rely on
603-
// the dep-node color having been properly updated.
604-
// This means that the query system has reached an
605-
// invalid state. We let the compiler continue (by
606-
// returning `None`) so it can emit error messages
607-
// and wind down, but rely on the fact that this
608-
// invalid state will not be persisted to the
609-
// incremental compilation cache because of
610-
// compilation errors being present.
611-
debug!(
612-
"try_mark_previous_green({:?}) - END - \
613-
dependency {:?} resulted in compilation error",
614-
dep_node, dep_dep_node
615-
);
616-
return None;
617-
}
618-
}
619-
}
620-
} else {
621-
// The DepNode could not be forced.
622-
debug!(
623-
"try_mark_previous_green({:?}) - END - dependency {:?} \
624-
could not be forced",
625-
dep_node, dep_dep_node
626-
);
627-
return None;
628-
}
629-
}
630-
}
625+
self.try_mark_parent_green(tcx, data, dep_dep_node_index, dep_node)?
631626
}
632627

633628
// If we got here without hitting a `return` that means that all
@@ -796,7 +791,7 @@ impl<K: DepKind> DepGraph<K> {
796791
}
797792
}
798793

799-
fn next_virtual_depnode_index(&self) -> DepNodeIndex {
794+
pub(crate) fn next_virtual_depnode_index(&self) -> DepNodeIndex {
800795
let index = self.virtual_dep_node_index.fetch_add(1, Relaxed);
801796
DepNodeIndex::from_u32(index)
802797
}

compiler/rustc_query_system/src/query/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use crate::dep_graph::{DepNode, DepNodeIndex, HasDepContext, SerializedDepNodeIn
1919
use rustc_data_structures::sync::Lock;
2020
use rustc_data_structures::thin_vec::ThinVec;
2121
use rustc_errors::Diagnostic;
22-
use rustc_span::def_id::DefId;
2322
use rustc_span::Span;
2423

2524
/// Description of a frame in the query stack.
@@ -64,9 +63,6 @@ impl QueryStackFrame {
6463
}
6564

6665
pub trait QueryContext: HasDepContext {
67-
/// Get string representation from DefPath.
68-
fn def_path_str(&self, def_id: DefId) -> String;
69-
7066
/// Get the query information from the TLS context.
7167
fn current_query_job(&self) -> Option<QueryJobId<Self::DepKind>>;
7268

0 commit comments

Comments
 (0)