Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 17e10ad

Browse files
committedMar 24, 2025
Auto merge of rust-lang#138824 - Zoxc:dep-graph-no-prev-map, r=<try>
Remove `prev_index_to_index` field from `CurrentDepGraph` The dep graph currently has 2 ways to map a previous index into a current index. The `prev_index_to_index` map stores the current index equivalent of a previous index. For indices which are marked green, we also store the same information in the `DepNodeColorMap`. We actually only need to known the mapping for green nodes however, so this PR removes `prev_index_to_index` and instead makes use of the `DepNodeColorMap`. To avoid racing when promoting a node from the previous session, the encoder lock is now used to ensure only one thread encodes the promoted node. This was previously done by the lock in `prev_index_to_index`. This also changes `nodes_newly_allocated_in_current_session` used to detect duplicate dep nodes to contain both new and previous nodes, which is simpler and can better catch duplicates. The dep node index encoding used in `DepNodeColorMap` is tweak to avoid subtraction / addition to optimize accessing the current equivalent of a previous index. This is based on rust-lang#138629. r? `@oli-obk`
2 parents ae8ab87 + 59e27c8 commit 17e10ad

File tree

11 files changed

+271
-219
lines changed

11 files changed

+271
-219
lines changed
 

‎compiler/rustc_codegen_ssa/src/base.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1105,11 +1105,12 @@ pub fn determine_cgu_reuse<'tcx>(tcx: TyCtxt<'tcx>, cgu: &CodegenUnit<'tcx>) ->
11051105
// know that later). If we are not doing LTO, there is only one optimized
11061106
// version of each module, so we re-use that.
11071107
let dep_node = cgu.codegen_dep_node(tcx);
1108-
assert!(
1109-
!tcx.dep_graph.dep_node_exists(&dep_node),
1110-
"CompileCodegenUnit dep-node for CGU `{}` already exists before marking.",
1111-
cgu.name()
1112-
);
1108+
tcx.dep_graph.assert_dep_node_not_yet_allocated_in_current_session(&dep_node, || {
1109+
format!(
1110+
"CompileCodegenUnit dep-node for CGU `{}` already exists before marking.",
1111+
cgu.name()
1112+
)
1113+
});
11131114

11141115
if tcx.try_mark_green(&dep_node) {
11151116
// We can re-use either the pre- or the post-thinlto state. If no LTO is

‎compiler/rustc_incremental/src/persist/load.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ fn delete_dirty_work_product(sess: &Session, swp: SerializedWorkProduct) {
9191
work_product::delete_workproduct_files(sess, &swp.work_product);
9292
}
9393

94-
fn load_dep_graph(sess: &Session) -> LoadResult<(Arc<SerializedDepGraph>, WorkProductMap)> {
94+
fn load_dep_graph(
95+
sess: &Session,
96+
deps: &DepsType,
97+
) -> LoadResult<(Arc<SerializedDepGraph>, WorkProductMap)> {
9598
let prof = sess.prof.clone();
9699

97100
if sess.opts.incremental.is_none() {
@@ -171,7 +174,7 @@ fn load_dep_graph(sess: &Session) -> LoadResult<(Arc<SerializedDepGraph>, WorkPr
171174
return LoadResult::DataOutOfDate;
172175
}
173176

174-
let dep_graph = SerializedDepGraph::decode::<DepsType>(&mut decoder);
177+
let dep_graph = SerializedDepGraph::decode::<DepsType>(&mut decoder, deps);
175178

176179
LoadResult::Ok { data: (dep_graph, prev_work_products) }
177180
}
@@ -205,11 +208,11 @@ pub fn load_query_result_cache(sess: &Session) -> Option<OnDiskCache> {
205208

206209
/// Setups the dependency graph by loading an existing graph from disk and set up streaming of a
207210
/// new graph to an incremental session directory.
208-
pub fn setup_dep_graph(sess: &Session, crate_name: Symbol) -> DepGraph {
211+
pub fn setup_dep_graph(sess: &Session, crate_name: Symbol, deps: &DepsType) -> DepGraph {
209212
// `load_dep_graph` can only be called after `prepare_session_directory`.
210213
prepare_session_directory(sess, crate_name);
211214

212-
let res = sess.opts.build_dep_graph().then(|| load_dep_graph(sess));
215+
let res = sess.opts.build_dep_graph().then(|| load_dep_graph(sess, deps));
213216

214217
if sess.opts.incremental.is_some() {
215218
sess.time("incr_comp_garbage_collect_session_directories", || {

‎compiler/rustc_incremental/src/persist/save.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ pub(crate) fn build_dep_graph(
173173
sess.opts.dep_tracking_hash(false).encode(&mut encoder);
174174

175175
Some(DepGraph::new(
176-
&sess.prof,
176+
sess,
177177
prev_graph,
178178
prev_work_products,
179179
encoder,

‎compiler/rustc_interface/src/passes.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_incremental::setup_dep_graph;
1919
use rustc_lint::{BufferedEarlyLint, EarlyCheckNode, LintStore, unerased_lint_store};
2020
use rustc_metadata::creader::CStore;
2121
use rustc_middle::arena::Arena;
22+
use rustc_middle::dep_graph::DepsType;
2223
use rustc_middle::ty::{self, CurrentGcx, GlobalCtxt, RegisteredTools, TyCtxt};
2324
use rustc_middle::util::Providers;
2425
use rustc_parse::{
@@ -774,7 +775,9 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
774775
sess.cfg_version,
775776
);
776777
let outputs = util::build_output_filenames(&pre_configured_attrs, sess);
777-
let dep_graph = setup_dep_graph(sess, crate_name);
778+
779+
let dep_type = DepsType { dep_names: rustc_query_impl::dep_kind_names() };
780+
let dep_graph = setup_dep_graph(sess, crate_name, &dep_type);
778781

779782
let cstore =
780783
FreezeLock::new(Box::new(CStore::new(compiler.codegen_backend.metadata_loader())) as _);

‎compiler/rustc_middle/src/dep_graph/dep_node.rs

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ macro_rules! define_dep_nodes {
2121
($mod:ident) => {[ $($mod::$variant()),* ]};
2222
}
2323

24+
#[macro_export]
25+
macro_rules! make_dep_kind_name_array {
26+
($mod:ident) => {
27+
vec! {
28+
$(*$mod::$variant().name),*
29+
}
30+
};
31+
}
32+
2433
/// This enum serves as an index into arrays built by `make_dep_kind_array`.
2534
// This enum has more than u8::MAX variants so we need some kind of multi-byte
2635
// encoding. The derived Encodable/Decodable uses leb128 encoding which is

‎compiler/rustc_middle/src/dep_graph/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ pub type DepGraph = rustc_query_system::dep_graph::DepGraph<DepsType>;
2020
pub type DepKindStruct<'tcx> = rustc_query_system::dep_graph::DepKindStruct<TyCtxt<'tcx>>;
2121

2222
#[derive(Clone)]
23-
pub struct DepsType;
23+
pub struct DepsType {
24+
pub dep_names: Vec<&'static str>,
25+
}
2426

2527
impl Deps for DepsType {
2628
fn with_deps<OP, R>(task_deps: TaskDepsRef<'_>, op: OP) -> R
@@ -44,6 +46,10 @@ impl Deps for DepsType {
4446
})
4547
}
4648

49+
fn name(&self, dep_kind: DepKind) -> &'static str {
50+
self.dep_names[dep_kind.as_usize()]
51+
}
52+
4753
const DEP_KIND_NULL: DepKind = dep_kinds::Null;
4854
const DEP_KIND_RED: DepKind = dep_kinds::Red;
4955
const DEP_KIND_SIDE_EFFECT: DepKind = dep_kinds::SideEffect;

‎compiler/rustc_query_impl/src/plumbing.rs

+4
Original file line numberDiff line numberDiff line change
@@ -863,5 +863,9 @@ macro_rules! define_queries {
863863
pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct<'tcx>] {
864864
arena.alloc_from_iter(rustc_middle::make_dep_kind_array!(query_callbacks))
865865
}
866+
867+
pub fn dep_kind_names() -> Vec<&'static str> {
868+
rustc_middle::make_dep_kind_name_array!(query_callbacks)
869+
}
866870
}
867871
}
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.