Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4500,7 +4500,6 @@ dependencies = [
"rustc_index",
"rustc_macros",
"rustc_middle",
"rustc_query_system",
"rustc_serialize",
"rustc_session",
"rustc_span",
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_incremental/src/assert_dep_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
use rustc_hir::intravisit::{self, Visitor};
use rustc_middle::bug;
use rustc_middle::dep_graph::{
DepGraphQuery, DepKind, DepNode, DepNodeExt, DepNodeFilter, EdgeFilter, dep_kinds,
DepGraphQuery, DepKind, DepNode, DepNodeFilter, EdgeFilter, dep_kinds,
};
use rustc_middle::hir::nested_filter;
use rustc_middle::ty::TyCtxt;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_incremental/src/persist/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use rustc_hir::{
Attribute, ImplItemKind, ItemKind as HirItem, Node as HirNode, TraitItemKind, find_attr,
intravisit,
};
use rustc_middle::dep_graph::{DepNode, DepNodeExt, dep_kind_from_label, label_strs};
use rustc_middle::dep_graph::{DepNode, dep_kind_from_label, label_strs};
use rustc_middle::hir::nested_filter;
use rustc_middle::ty::TyCtxt;
use rustc_span::{Span, Symbol};
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_interface/src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ use std::fmt;

use rustc_errors::{DiagInner, TRACK_DIAGNOSTIC};
use rustc_middle::dep_graph::dep_node::default_dep_kind_debug;
use rustc_middle::dep_graph::{DepContext, DepKind, DepNode, DepNodeExt, TaskDepsRef};
use rustc_middle::dep_graph::{DepContext, DepKind, DepNode, TaskDepsRef};
use rustc_middle::ty::tls;
use rustc_query_impl::QueryCtxt;

fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
tls::with_context_opt(|icx| {
Expand All @@ -41,7 +40,7 @@ fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
fn track_diagnostic<R>(diagnostic: DiagInner, f: &mut dyn FnMut(DiagInner) -> R) -> R {
tls::with_context_opt(|icx| {
if let Some(icx) = icx {
icx.tcx.dep_graph.record_diagnostic(QueryCtxt::new(icx.tcx), &diagnostic);
icx.tcx.dep_graph.record_diagnostic(icx.tcx, &diagnostic);

// Diagnostics are tracked, we can ignore the dependency.
let icx = tls::ImplicitCtxt { task_deps: TaskDepsRef::Ignore, ..icx.clone() };
Expand Down
22 changes: 4 additions & 18 deletions compiler/rustc_middle/src/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,19 +434,7 @@ pub(crate) fn make_metadata(tcx: TyCtxt<'_>) -> DepNode {
DepNode::construct(tcx, dep_kinds::Metadata, &())
}

pub trait DepNodeExt: Sized {
fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId>;

fn from_label_string(
tcx: TyCtxt<'_>,
label: &str,
def_path_hash: DefPathHash,
) -> Result<Self, ()>;

fn has_label_string(label: &str) -> bool;
}

impl DepNodeExt for DepNode {
impl DepNode {
/// Extracts the DefId corresponding to this DepNode. This will work
/// if two conditions are met:
///
Expand All @@ -457,16 +445,15 @@ impl DepNodeExt for DepNode {
/// DepNode. Condition (2) might not be fulfilled if a DepNode
/// refers to something from the previous compilation session that
/// has been removed.
fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
pub fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
if tcx.fingerprint_style(self.kind) == FingerprintStyle::DefPathHash {
tcx.def_path_hash_to_def_id(DefPathHash(self.hash.into()))
} else {
None
}
}

/// Used in testing
fn from_label_string(
pub fn from_label_string(
tcx: TyCtxt<'_>,
label: &str,
def_path_hash: DefPathHash,
Expand All @@ -482,8 +469,7 @@ impl DepNodeExt for DepNode {
}
}

/// Used in testing
fn has_label_string(label: &str) -> bool {
pub fn has_label_string(label: &str) -> bool {
dep_kind_from_label_string(label).is_ok()
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/dep_graph/dep_node_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId,
use rustc_hir::definitions::DefPathHash;
use rustc_hir::{HirId, ItemLocalId, OwnerId};

use crate::dep_graph::{DepContext, DepNode, DepNodeExt, DepNodeKey, FingerprintStyle};
use crate::dep_graph::{DepContext, DepNode, DepNodeKey, FingerprintStyle};
use crate::ty::TyCtxt;

impl<'tcx> DepNodeKey<TyCtxt<'tcx>> for () {
Expand Down
101 changes: 58 additions & 43 deletions compiler/rustc_middle/src/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use super::query::DepGraphQuery;
use super::serialized::{GraphEncoder, SerializedDepGraph, SerializedDepNodeIndex};
use super::{DepContext, DepKind, DepNode, Deps, HasDepContext, WorkProductId};
use crate::dep_graph::edges::EdgesVec;
use crate::query::QueryContext;
use crate::ty::TyCtxt;
use crate::verify_ich::incremental_verify_ich;

pub struct DepGraph<D: Deps> {
Expand Down Expand Up @@ -525,30 +525,26 @@ impl<D: Deps> DepGraph<D> {
/// This encodes a diagnostic by creating a node with an unique index and associating
/// `diagnostic` with it, for use in the next session.
#[inline]
pub fn record_diagnostic<'tcx, Qcx: QueryContext<'tcx>>(
&self,
qcx: Qcx,
diagnostic: &DiagInner,
) {
pub fn record_diagnostic<'tcx>(&self, tcx: TyCtxt<'tcx>, diagnostic: &DiagInner) {
if let Some(ref data) = self.data {
D::read_deps(|task_deps| match task_deps {
TaskDepsRef::EvalAlways | TaskDepsRef::Ignore => return,
TaskDepsRef::Forbid | TaskDepsRef::Allow(..) => {
self.read_index(data.encode_diagnostic(qcx, diagnostic));
self.read_index(data.encode_diagnostic(tcx, diagnostic));
}
})
}
}
/// This forces a diagnostic node green by running its side effect. `prev_index` would
/// refer to a node created used `encode_diagnostic` in the previous session.
#[inline]
pub fn force_diagnostic_node<'tcx, Qcx: QueryContext<'tcx>>(
pub fn force_diagnostic_node<'tcx>(
&self,
qcx: Qcx,
tcx: TyCtxt<'tcx>,
prev_index: SerializedDepNodeIndex,
) {
if let Some(ref data) = self.data {
data.force_diagnostic_node(qcx, prev_index);
data.force_diagnostic_node(tcx, prev_index);
}
}

Expand Down Expand Up @@ -688,11 +684,7 @@ impl<D: Deps> DepGraphData<D> {
/// This encodes a diagnostic by creating a node with an unique index and associating
/// `diagnostic` with it, for use in the next session.
#[inline]
fn encode_diagnostic<'tcx, Qcx: QueryContext<'tcx>>(
&self,
qcx: Qcx,
diagnostic: &DiagInner,
) -> DepNodeIndex {
fn encode_diagnostic<'tcx>(&self, tcx: TyCtxt<'tcx>, diagnostic: &DiagInner) -> DepNodeIndex {
// Use `send_new` so we get an unique index, even though the dep node is not.
let dep_node_index = self.current.encoder.send_new(
DepNode {
Expand All @@ -705,24 +697,20 @@ impl<D: Deps> DepGraphData<D> {
std::iter::once(DepNodeIndex::FOREVER_RED_NODE).collect(),
);
let side_effect = QuerySideEffect::Diagnostic(diagnostic.clone());
qcx.store_side_effect(dep_node_index, side_effect);
tcx.store_side_effect(dep_node_index, side_effect);
dep_node_index
}

/// This forces a diagnostic node green by running its side effect. `prev_index` would
/// refer to a node created used `encode_diagnostic` in the previous session.
#[inline]
fn force_diagnostic_node<'tcx, Qcx: QueryContext<'tcx>>(
&self,
qcx: Qcx,
prev_index: SerializedDepNodeIndex,
) {
fn force_diagnostic_node<'tcx>(&self, tcx: TyCtxt<'tcx>, prev_index: SerializedDepNodeIndex) {
D::with_deps(TaskDepsRef::Ignore, || {
let side_effect = qcx.load_side_effect(prev_index).unwrap();
let side_effect = tcx.load_side_effect(prev_index).unwrap();

match &side_effect {
QuerySideEffect::Diagnostic(diagnostic) => {
qcx.dep_context().sess().dcx().emit_diagnostic(diagnostic.clone());
tcx.dcx().emit_diagnostic(diagnostic.clone());
}
}

Expand All @@ -741,7 +729,7 @@ impl<D: Deps> DepGraphData<D> {
true,
);
// This will just overwrite the same value for concurrent calls.
qcx.store_side_effect(dep_node_index, side_effect);
tcx.store_side_effect(dep_node_index, side_effect);
})
}

Expand Down Expand Up @@ -867,12 +855,12 @@ impl<D: Deps> DepGraph<D> {
DepNodeColor::Unknown
}

pub fn try_mark_green<'tcx, Qcx: QueryContext<'tcx, Deps = D>>(
pub fn try_mark_green<'tcx>(
&self,
qcx: Qcx,
tcx: TyCtxt<'tcx>,
dep_node: &DepNode,
) -> Option<(SerializedDepNodeIndex, DepNodeIndex)> {
self.data().and_then(|data| data.try_mark_green(qcx, dep_node))
self.data().and_then(|data| data.try_mark_green(tcx, dep_node))
}
}

Expand All @@ -882,12 +870,12 @@ impl<D: Deps> DepGraphData<D> {
/// A node will have an index, when it's already been marked green, or when we can mark it
/// green. This function will mark the current task as a reader of the specified node, when
/// a node index can be found for that node.
pub fn try_mark_green<'tcx, Qcx: QueryContext<'tcx, Deps = D>>(
pub fn try_mark_green<'tcx>(
&self,
qcx: Qcx,
tcx: TyCtxt<'tcx>,
dep_node: &DepNode,
) -> Option<(SerializedDepNodeIndex, DepNodeIndex)> {
debug_assert!(!qcx.dep_context().is_eval_always(dep_node.kind));
debug_assert!(!tcx.is_eval_always(dep_node.kind));

// Return None if the dep node didn't exist in the previous session
let prev_index = self.previous.node_to_index_opt(dep_node)?;
Expand All @@ -902,16 +890,16 @@ impl<D: Deps> DepGraphData<D> {
// in the previous compilation session too, so we can try to
// mark it as green by recursively marking all of its
// dependencies green.
self.try_mark_previous_green(qcx, prev_index, None)
self.try_mark_previous_green(tcx, prev_index, None)
.map(|dep_node_index| (prev_index, dep_node_index))
}
}
}

#[instrument(skip(self, qcx, parent_dep_node_index, frame), level = "debug")]
fn try_mark_parent_green<'tcx, Qcx: QueryContext<'tcx, Deps = D>>(
#[instrument(skip(self, tcx, parent_dep_node_index, frame), level = "debug")]
fn try_mark_parent_green<'tcx>(
&self,
qcx: Qcx,
tcx: TyCtxt<'tcx>,
parent_dep_node_index: SerializedDepNodeIndex,
frame: &MarkFrame<'_>,
) -> Option<()> {
Expand Down Expand Up @@ -944,13 +932,13 @@ impl<D: Deps> DepGraphData<D> {

// We don't know the state of this dependency. If it isn't
// an eval_always node, let's try to mark it green recursively.
if !qcx.dep_context().is_eval_always(dep_dep_node.kind) {
if !tcx.is_eval_always(dep_dep_node.kind) {
debug!(
"state of dependency {:?} ({}) is unknown, trying to mark it green",
dep_dep_node, dep_dep_node.hash,
);

let node_index = self.try_mark_previous_green(qcx, parent_dep_node_index, Some(frame));
let node_index = self.try_mark_previous_green(tcx, parent_dep_node_index, Some(frame));

if node_index.is_some() {
debug!("managed to MARK dependency {dep_dep_node:?} as green");
Expand All @@ -960,7 +948,7 @@ impl<D: Deps> DepGraphData<D> {

// We failed to mark it green, so we try to force the query.
debug!("trying to force dependency {dep_dep_node:?}");
if !qcx.dep_context().try_force_from_dep_node(*dep_dep_node, parent_dep_node_index, frame) {
if !tcx.dep_context().try_force_from_dep_node(*dep_dep_node, parent_dep_node_index, frame) {
// The DepNode could not be forced.
debug!("dependency {dep_dep_node:?} could not be forced");
return None;
Expand All @@ -978,7 +966,7 @@ impl<D: Deps> DepGraphData<D> {
DepNodeColor::Unknown => {}
}

if let None = qcx.dep_context().sess().dcx().has_errors_or_delayed_bugs() {
if let None = tcx.dcx().has_errors_or_delayed_bugs() {
panic!("try_mark_previous_green() - Forcing the DepNode should have set its color")
}

Expand All @@ -997,25 +985,25 @@ impl<D: Deps> DepGraphData<D> {
}

/// Try to mark a dep-node which existed in the previous compilation session as green.
#[instrument(skip(self, qcx, prev_dep_node_index, frame), level = "debug")]
fn try_mark_previous_green<'tcx, Qcx: QueryContext<'tcx, Deps = D>>(
#[instrument(skip(self, tcx, prev_dep_node_index, frame), level = "debug")]
fn try_mark_previous_green<'tcx>(
&self,
qcx: Qcx,
tcx: TyCtxt<'tcx>,
prev_dep_node_index: SerializedDepNodeIndex,
frame: Option<&MarkFrame<'_>>,
) -> Option<DepNodeIndex> {
let frame = MarkFrame { index: prev_dep_node_index, parent: frame };

// We never try to mark eval_always nodes as green
debug_assert!(
!qcx.dep_context()
!tcx.dep_context()
.is_eval_always(self.previous.index_to_node(prev_dep_node_index).kind)
);

let prev_deps = self.previous.edge_targets_from(prev_dep_node_index);

for dep_dep_node_index in prev_deps {
self.try_mark_parent_green(qcx, dep_dep_node_index, &frame)?;
self.try_mark_parent_green(tcx, dep_dep_node_index, &frame)?;
}

// If we got here without hitting a `return` that means that all
Expand Down Expand Up @@ -1508,3 +1496,30 @@ fn panic_on_forbidden_read<D: Deps>(data: &DepGraphData<D>, dep_node_index: DepN
See <https://github.com/rust-lang/rust/pull/91919>."
)
}

impl<'tcx> TyCtxt<'tcx> {
/// Return whether this kind always require evaluation.
#[inline(always)]
fn is_eval_always(self, kind: DepKind) -> bool {
self.dep_kind_vtable(kind).is_eval_always
}

// Interactions with on_disk_cache
fn load_side_effect(
self,
prev_dep_node_index: SerializedDepNodeIndex,
) -> Option<QuerySideEffect> {
self.query_system
.on_disk_cache
.as_ref()
.and_then(|c| c.load_side_effect(self, prev_dep_node_index))
}

#[inline(never)]
#[cold]
fn store_side_effect(self, dep_node_index: DepNodeIndex, side_effect: QuerySideEffect) {
if let Some(c) = self.query_system.on_disk_cache.as_ref() {
c.store_side_effect(dep_node_index, side_effect)
}
}
}
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/dep_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use rustc_session::Session;
use tracing::instrument;

pub use self::dep_node::{
DepKind, DepNode, DepNodeExt, DepNodeKey, WorkProductId, dep_kind_from_label, dep_kinds,
label_strs,
DepKind, DepNode, DepNodeKey, WorkProductId, dep_kind_from_label, dep_kinds, label_strs,
};
pub use self::graph::{
DepGraphData, DepNodeIndex, TaskDepsRef, WorkProduct, WorkProductMap, hash_result,
Expand Down Expand Up @@ -52,6 +51,7 @@ pub trait DepContext: Copy {

#[inline(always)]
/// Return whether this kind always require evaluation.
// FIXME: remove this in favour of the version on `TyCtxt`.
fn is_eval_always(self, kind: DepKind) -> bool {
self.dep_kind_vtable(kind).is_eval_always
}
Expand Down
Loading