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 d428f0a

Browse files
committedJun 7, 2024
Also store the DefPathHash as part of a DefId creation side effect, so we can reliably recreate the same DefId over and over again
1 parent a82e9a3 commit d428f0a

File tree

5 files changed

+48
-24
lines changed

5 files changed

+48
-24
lines changed
 

‎compiler/rustc_hir/src/definitions.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,11 @@ impl Definitions {
344344
}
345345

346346
/// Adds a definition with a parent definition.
347-
pub fn create_def(&mut self, parent: LocalDefId, data: DefPathData) -> LocalDefId {
347+
pub fn create_def(
348+
&mut self,
349+
parent: LocalDefId,
350+
data: DefPathData,
351+
) -> (LocalDefId, DefPathHash) {
348352
// We can't use `Debug` implementation for `LocalDefId` here, since it tries to acquire a
349353
// reference to `Definitions` and we're already holding a mutable reference.
350354
debug!(
@@ -373,7 +377,7 @@ impl Definitions {
373377
debug!("create_def: after disambiguation, key = {:?}", key);
374378

375379
// Create the definition.
376-
LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) }
380+
(LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) }, def_path_hash)
377381
}
378382

379383
#[inline(always)]

‎compiler/rustc_middle/src/ty/context.rs

+34-17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
pub mod tls;
66

7+
use rustc_query_system::query::DefIdInfo;
78
pub use rustc_type_ir::lift::Lift;
89

910
use crate::arena::Arena;
@@ -59,7 +60,6 @@ use rustc_index::IndexVec;
5960
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
6061
use rustc_query_system::dep_graph::{DepNodeIndex, TaskDepsRef};
6162
use rustc_query_system::ich::StableHashingContext;
62-
use rustc_query_system::query::DefIdInfo;
6363
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
6464
use rustc_session::config::CrateType;
6565
use rustc_session::cstore::{CrateStoreDyn, Untracked};
@@ -73,7 +73,7 @@ use rustc_target::spec::abi;
7373
use rustc_type_ir::TyKind::*;
7474
use rustc_type_ir::WithCachedTypeInfo;
7575
use rustc_type_ir::{CollectAndApply, Interner, TypeFlags};
76-
use tracing::{debug, instrument};
76+
use tracing::{debug, instrument, trace};
7777

7878
use std::assert_matches::assert_matches;
7979
use std::borrow::Borrow;
@@ -1330,14 +1330,15 @@ impl<'tcx> TyCtxtAt<'tcx> {
13301330

13311331
impl<'tcx> TyCtxt<'tcx> {
13321332
/// `tcx`-dependent operations performed for every created definition.
1333+
#[instrument(level = "trace", skip(self))]
13331334
pub fn create_def(
13341335
self,
13351336
parent: LocalDefId,
13361337
name: Symbol,
13371338
def_kind: DefKind,
13381339
) -> TyCtxtFeed<'tcx, LocalDefId> {
13391340
let data = def_kind.def_path_data(name);
1340-
// The following call has the side effect of modifying the tables inside `definitions`.
1341+
// The following create_def calls have the side effect of modifying the tables inside `definitions`.
13411342
// These very tables are relied on by the incr. comp. engine to decode DepNodes and to
13421343
// decode the on-disk cache.
13431344
//
@@ -1350,31 +1351,47 @@ impl<'tcx> TyCtxt<'tcx> {
13501351
// This is fine because:
13511352
// - those queries are `eval_always` so we won't miss their result changing;
13521353
// - this write will have happened before these queries are called.
1353-
let def_id = self.untracked.definitions.write().create_def(parent, data);
1354-
1355-
// This function modifies `self.definitions` using a side-effect.
1356-
// We need to ensure that these side effects are re-run by the incr. comp. engine.
1357-
tls::with_context(|icx| {
1354+
let def_id = tls::with_context(|icx| {
13581355
match icx.task_deps {
13591356
// Always gets rerun anyway, so nothing to replay
1360-
TaskDepsRef::EvalAlways => {}
1357+
TaskDepsRef::EvalAlways => {
1358+
let def_id = self.untracked.definitions.write().create_def(parent, data).0;
1359+
trace!(?def_id, "eval always");
1360+
def_id
1361+
}
13611362
// Top-level queries like the resolver get rerun every time anyway
1362-
TaskDepsRef::Ignore => {}
1363+
TaskDepsRef::Ignore => {
1364+
let def_id = self.untracked.definitions.write().create_def(parent, data).0;
1365+
trace!(?def_id, "ignore");
1366+
def_id
1367+
}
13631368
TaskDepsRef::Forbid => bug!(
13641369
"cannot create definition {parent:?}, {name:?}, {def_kind:?} without being able to register task dependencies"
13651370
),
13661371
TaskDepsRef::Allow(_) => {
1367-
icx.side_effects
1368-
.as_ref()
1369-
.unwrap()
1370-
.lock()
1371-
.definitions
1372-
.push(DefIdInfo { parent, data });
1372+
let (def_id, hash) =
1373+
self.untracked.definitions.write().create_def(parent, data);
1374+
trace!(?def_id, "record side effects");
1375+
1376+
icx.side_effects.as_ref().unwrap().lock().definitions.push(DefIdInfo {
1377+
parent,
1378+
data,
1379+
hash,
1380+
});
1381+
def_id
13731382
}
13741383
TaskDepsRef::Replay { prev_side_effects, created_def_ids } => {
1384+
trace!(?created_def_ids, "replay side effects");
1385+
trace!("num_defs : {}", prev_side_effects.definitions.len());
13751386
let index = created_def_ids.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
13761387
let prev_info = &prev_side_effects.definitions[index];
1377-
assert_eq!(*prev_info, DefIdInfo { parent, data });
1388+
let def_id = self.untracked.definitions.read().local_def_path_hash_to_def_id(
1389+
prev_info.hash,
1390+
&"should have already recreated def id in try_mark_green",
1391+
);
1392+
assert_eq!(prev_info.data, data);
1393+
assert_eq!(prev_info.parent, parent);
1394+
def_id
13781395
}
13791396
}
13801397
});

‎compiler/rustc_query_impl/src/plumbing.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl QueryContext for QueryCtxt<'_> {
171171
});
172172
}
173173

174-
#[tracing::instrument(level = "trace", skip(self))]
174+
#[tracing::instrument(level = "trace", skip(self, side_effects))]
175175
fn apply_side_effects(self, side_effects: QuerySideEffects) {
176176
let dcx = self.dep_context().sess().dcx();
177177
let QuerySideEffects { diagnostics, definitions } = side_effects;
@@ -180,8 +180,9 @@ impl QueryContext for QueryCtxt<'_> {
180180
dcx.emit_diagnostic(diagnostic);
181181
}
182182

183-
for DefIdInfo { parent, data } in definitions {
184-
self.tcx.untracked().definitions.write().create_def(parent, data);
183+
for DefIdInfo { parent, data, hash } in definitions {
184+
let (_def_id, h) = self.tcx.untracked().definitions.write().create_def(parent, data);
185+
debug_assert_eq!(h, hash);
185186
}
186187
}
187188
}

‎compiler/rustc_query_system/src/dep_graph/graph.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ impl<D: Deps> DepGraphData<D> {
908908
Some(dep_node_index)
909909
}
910910

911-
/// Atomically emits some loaded diagnostics.
911+
/// Atomically emits some loaded side effects.
912912
/// This may be called concurrently on multiple threads for the same dep node.
913913
#[cold]
914914
#[inline(never)]
@@ -924,7 +924,7 @@ impl<D: Deps> DepGraphData<D> {
924924
// We were the first to insert the node in the set so this thread
925925
// must process side effects
926926

927-
// Promote the previous diagnostics to the current session.
927+
// Promote the previous side effects to the current session.
928928
qcx.store_side_effects(dep_node_index, side_effects.clone());
929929

930930
qcx.apply_side_effects(side_effects);

‎compiler/rustc_query_system/src/query/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc_data_structures::stable_hasher::Hash64;
2020
use rustc_data_structures::sync::Lock;
2121
use rustc_errors::DiagInner;
2222
use rustc_hir::def::DefKind;
23+
use rustc_hir::def_id::DefPathHash;
2324
use rustc_macros::{Decodable, Encodable};
2425
use rustc_span::def_id::{DefId, LocalDefId};
2526
use rustc_span::Span;
@@ -97,6 +98,7 @@ pub struct QuerySideEffects {
9798
pub struct DefIdInfo {
9899
pub parent: LocalDefId,
99100
pub data: rustc_hir::definitions::DefPathData,
101+
pub hash: DefPathHash,
100102
}
101103

102104
impl QuerySideEffects {

0 commit comments

Comments
 (0)
Failed to load comments.