-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Only use the new DepNode hashmap for anonymous nodes. #109050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d51f5cf
Verify dep-graph consistency when loading it.
cjgillot d3bcb8f
Only use the new node hashmap for anonymous nodes.
cjgillot db353ca
Bless test for constant queries.
cjgillot 545ec63
Add a flag to check depnodes for collisions.
cjgillot 1851fc4
Reintroduce assertion.
cjgillot ddcc71b
Reuse -Zincremental_verify_ich.
cjgillot 694b773
Do not fetch the DepNode to mark nodes green.
cjgillot 27568ed
Silence lint.
cjgillot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Only use the new node hashmap for anonymous nodes.
- Loading branch information
commit d3bcb8f137bff9aa7717eb0dfbea4277097ab94f
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -345,18 +345,6 @@ impl<D: Deps> DepGraphData<D> { | |
task: fn(Ctxt, A) -> R, | ||
hash_result: Option<fn(&mut StableHashingContext<'_>, &R) -> Fingerprint>, | ||
) -> (R, DepNodeIndex) { | ||
// If the following assertion triggers, it can have two reasons: | ||
// 1. Something is wrong with DepNode creation, either here or | ||
// in `DepGraph::try_mark_green()`. | ||
// 2. Two distinct query keys get mapped to the same `DepNode` | ||
// (see for example #48923). | ||
assert!( | ||
!self.dep_node_exists(&key), | ||
"forcing query with already existing `DepNode`\n\ | ||
- query-key: {arg:?}\n\ | ||
- dep-node: {key:?}" | ||
); | ||
|
||
cjgillot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
let with_deps = |task_deps| D::with_deps(task_deps, || task(cx, arg)); | ||
let (result, edges) = if cx.dep_context().is_eval_always(key.kind) { | ||
(with_deps(TaskDepsRef::EvalAlways), EdgesVec::new()) | ||
|
@@ -443,7 +431,31 @@ impl<D: Deps> DepGraphData<D> { | |
hash: self.current.anon_id_seed.combine(hasher.finish()).into(), | ||
}; | ||
|
||
self.current.intern_new_node(target_dep_node, task_deps, Fingerprint::ZERO) | ||
// The DepNodes generated by the process above are not unique. 2 queries could | ||
// have exactly the same dependencies. However, deserialization does not handle | ||
// duplicated nodes, so we do the deduplication here directly. | ||
// | ||
// As anonymous nodes are a small quantity compared to the full dep-graph, the | ||
// memory impact of this `anon_node_to_index` map remains tolerable, and helps | ||
// us avoid useless growth of the graph with almost-equivalent nodes. | ||
match self | ||
.current | ||
.anon_node_to_index | ||
.get_shard_by_value(&target_dep_node) | ||
.lock() | ||
.entry(target_dep_node) | ||
{ | ||
Entry::Occupied(entry) => *entry.get(), | ||
Entry::Vacant(entry) => { | ||
let dep_node_index = self.current.intern_new_node( | ||
target_dep_node, | ||
task_deps, | ||
Fingerprint::ZERO, | ||
); | ||
entry.insert(dep_node_index); | ||
dep_node_index | ||
} | ||
} | ||
} | ||
}; | ||
|
||
|
@@ -607,20 +619,6 @@ impl<D: Deps> DepGraph<D> { | |
} | ||
|
||
impl<D: Deps> DepGraphData<D> { | ||
#[inline] | ||
fn dep_node_index_of_opt(&self, dep_node: &DepNode) -> Option<DepNodeIndex> { | ||
if let Some(prev_index) = self.previous.node_to_index_opt(dep_node) { | ||
self.current.prev_index_to_index.lock()[prev_index] | ||
} else { | ||
self.current.new_node_to_index.lock_shard_by_value(dep_node).get(dep_node).copied() | ||
} | ||
} | ||
|
||
#[inline] | ||
fn dep_node_exists(&self, dep_node: &DepNode) -> bool { | ||
self.dep_node_index_of_opt(dep_node).is_some() | ||
} | ||
|
||
fn node_color(&self, dep_node: &DepNode) -> Option<DepNodeColor> { | ||
if let Some(prev_index) = self.previous.node_to_index_opt(dep_node) { | ||
self.colors.get(prev_index) | ||
|
@@ -653,11 +651,6 @@ impl<D: Deps> DepGraphData<D> { | |
} | ||
|
||
impl<D: Deps> DepGraph<D> { | ||
#[inline] | ||
pub fn dep_node_exists(&self, dep_node: &DepNode) -> bool { | ||
self.data.as_ref().is_some_and(|data| data.dep_node_exists(dep_node)) | ||
} | ||
|
||
/// Checks whether a previous work product exists for `v` and, if | ||
/// so, return the path that leads to it. Used to skip doing work. | ||
pub fn previous_work_product(&self, v: &WorkProductId) -> Option<WorkProduct> { | ||
|
@@ -1025,24 +1018,24 @@ rustc_index::newtype_index! { | |
/// largest in the compiler. | ||
/// | ||
/// For this reason, we avoid storing `DepNode`s more than once as map | ||
/// keys. The `new_node_to_index` map only contains nodes not in the previous | ||
/// keys. The `anon_node_to_index` map only contains nodes of anonymous queries not in the previous | ||
/// graph, and we map nodes in the previous graph to indices via a two-step | ||
/// mapping. `SerializedDepGraph` maps from `DepNode` to `SerializedDepNodeIndex`, | ||
/// and the `prev_index_to_index` vector (which is more compact and faster than | ||
/// using a map) maps from `SerializedDepNodeIndex` to `DepNodeIndex`. | ||
/// | ||
/// This struct uses three locks internally. The `data`, `new_node_to_index`, | ||
/// This struct uses three locks internally. The `data`, `anon_node_to_index`, | ||
/// and `prev_index_to_index` fields are locked separately. Operations that take | ||
/// a `DepNodeIndex` typically just access the `data` field. | ||
/// | ||
/// We only need to manipulate at most two locks simultaneously: | ||
/// `new_node_to_index` and `data`, or `prev_index_to_index` and `data`. When | ||
/// manipulating both, we acquire `new_node_to_index` or `prev_index_to_index` | ||
/// `anon_node_to_index` and `data`, or `prev_index_to_index` and `data`. When | ||
/// manipulating both, we acquire `anon_node_to_index` or `prev_index_to_index` | ||
/// first, and `data` second. | ||
pub(super) struct CurrentDepGraph<D: Deps> { | ||
encoder: GraphEncoder<D>, | ||
new_node_to_index: Sharded<FxHashMap<DepNode, DepNodeIndex>>, | ||
prev_index_to_index: Lock<IndexVec<SerializedDepNodeIndex, Option<DepNodeIndex>>>, | ||
anon_node_to_index: Sharded<FxHashMap<DepNode, DepNodeIndex>>, | ||
|
||
/// This is used to verify that fingerprints do not change between the creation of a node | ||
/// and its recomputation. | ||
|
@@ -1110,7 +1103,7 @@ impl<D: Deps> CurrentDepGraph<D> { | |
profiler, | ||
previous, | ||
), | ||
new_node_to_index: Sharded::new(|| { | ||
anon_node_to_index: Sharded::new(|| { | ||
FxHashMap::with_capacity_and_hasher( | ||
new_node_count_estimate / sharded::shards(), | ||
Default::default(), | ||
|
@@ -1145,14 +1138,7 @@ impl<D: Deps> CurrentDepGraph<D> { | |
edges: EdgesVec, | ||
current_fingerprint: Fingerprint, | ||
) -> DepNodeIndex { | ||
let dep_node_index = match self.new_node_to_index.lock_shard_by_value(&key).entry(key) { | ||
Entry::Occupied(entry) => *entry.get(), | ||
Entry::Vacant(entry) => { | ||
let dep_node_index = self.encoder.send(key, current_fingerprint, edges); | ||
entry.insert(dep_node_index); | ||
dep_node_index | ||
} | ||
}; | ||
let dep_node_index = self.encoder.send(key, current_fingerprint, edges); | ||
|
||
#[cfg(debug_assertions)] | ||
self.record_edge(dep_node_index, key, current_fingerprint); | ||
|
@@ -1222,8 +1208,6 @@ impl<D: Deps> CurrentDepGraph<D> { | |
prev_graph: &SerializedDepGraph, | ||
prev_index: SerializedDepNodeIndex, | ||
) -> DepNodeIndex { | ||
self.debug_assert_not_in_new_nodes(prev_graph, prev_index); | ||
|
||
let mut prev_index_to_index = self.prev_index_to_index.lock(); | ||
|
||
match prev_index_to_index[prev_index] { | ||
|
@@ -1241,19 +1225,6 @@ impl<D: Deps> CurrentDepGraph<D> { | |
} | ||
} | ||
} | ||
|
||
#[inline] | ||
fn debug_assert_not_in_new_nodes( | ||
&self, | ||
prev_graph: &SerializedDepGraph, | ||
prev_index: SerializedDepNodeIndex, | ||
) { | ||
let node = &prev_graph.index_to_node(prev_index); | ||
debug_assert!( | ||
|
||
!self.new_node_to_index.lock_shard_by_value(node).contains_key(node), | ||
"node from previous graph present in new node collection" | ||
); | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
|
@@ -1375,7 +1346,7 @@ fn panic_on_forbidden_read<D: Deps>(data: &DepGraphData<D>, dep_node_index: DepN | |
|
||
if dep_node.is_none() { | ||
// Try to find it among the new nodes | ||
for shard in data.current.new_node_to_index.lock_shards() { | ||
for shard in data.current.anon_node_to_index.lock_shards() { | ||
if let Some((node, _)) = shard.iter().find(|(_, index)| **index == dep_node_index) { | ||
dep_node = Some(*node); | ||
break; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.