Skip to content

Commit 4f3ab35

Browse files
committed
Back-and-forth feed the hir_owner query
1 parent 7f61336 commit 4f3ab35

File tree

7 files changed

+43
-16
lines changed

7 files changed

+43
-16
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ fn compute_hir_hash(
429429
})
430430
}
431431

432-
pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> {
432+
pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate {
433433
let sess = tcx.sess;
434434
// Queries that borrow `resolver_for_lowering`.
435435
tcx.ensure_done().output_filenames(());
@@ -466,7 +466,12 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> {
466466
// Don't hash unless necessary, because it's expensive.
467467
let opt_hir_hash =
468468
if tcx.needs_crate_hash() { Some(compute_hir_hash(tcx, &owners)) } else { None };
469-
hir::Crate { owners, opt_hir_hash }
469+
470+
for (def_id, owner) in owners.drain_enumerated(..) {
471+
tcx.super_duper_perf_hack_experiment(def_id).hir_owner(owner)
472+
}
473+
474+
hir::Crate { opt_hir_hash }
470475
}
471476

472477
#[derive(Copy, Clone, PartialEq, Debug)]

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,8 +1571,7 @@ impl<'tcx> MaybeOwner<'tcx> {
15711571
///
15721572
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
15731573
#[derive(Debug)]
1574-
pub struct Crate<'hir> {
1575-
pub owners: IndexVec<LocalDefId, MaybeOwner<'hir>>,
1574+
pub struct Crate {
15761575
// Only present when incr. comp. is enabled.
15771576
pub opt_hir_hash: Option<Fingerprint>,
15781577
}

compiler/rustc_hir/src/stable_hash_impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap
111111
}
112112
}
113113

114-
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Crate<'_> {
114+
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Crate {
115115
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
116-
let Crate { owners: _, opt_hir_hash } = self;
116+
let Crate { opt_hir_hash } = self;
117117
opt_hir_hash.unwrap().hash_stable(hcx, hasher)
118118
}
119119
}

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,18 @@ pub fn provide(providers: &mut Providers) {
181181
providers.hir_crate_items = map::hir_crate_items;
182182
providers.crate_hash = map::crate_hash;
183183
providers.hir_module_items = map::hir_module_items;
184-
providers.hir_owner =
185-
|tcx, def_id| tcx.hir_crate(()).owners.get(def_id).copied().unwrap_or(MaybeOwner::Phantom);
186-
providers.local_def_id_to_hir_id = |tcx, def_id| match tcx.hir_owner(def_id) {
187-
MaybeOwner::Owner(_) => HirId::make_owner(def_id),
188-
MaybeOwner::NonOwner(hir_id) => hir_id,
189-
MaybeOwner::Phantom => bug!("No HirId for {:?}", def_id),
184+
providers.local_def_id_to_hir_id = |tcx, def_id| {
185+
tcx.ensure_ok().hir_crate(());
186+
match tcx.hir_owner(def_id) {
187+
MaybeOwner::Owner(_) => HirId::make_owner(def_id),
188+
MaybeOwner::NonOwner(hir_id) => hir_id,
189+
MaybeOwner::Phantom => bug!("No HirId for {:?}", def_id),
190+
}
191+
};
192+
providers.opt_hir_owner_nodes = |tcx, id| {
193+
tcx.ensure_ok().hir_crate(());
194+
tcx.hir_owner(id).as_owner().map(|i| &i.nodes)
190195
};
191-
providers.opt_hir_owner_nodes = |tcx, id| tcx.hir_owner(id).as_owner().map(|i| &i.nodes);
192196
providers.hir_owner_parent = |tcx, owner_id| {
193197
tcx.opt_local_parent(owner_id.def_id).map_or(CRATE_HIR_ID, |parent_def_id| {
194198
let parent_owner_id = tcx.local_def_id_to_hir_id(parent_def_id).owner;
@@ -204,8 +208,10 @@ pub fn provide(providers: &mut Providers) {
204208
}
205209
})
206210
};
207-
providers.hir_attr_map =
208-
|tcx, id| tcx.hir_owner(id.def_id).as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs);
211+
providers.hir_attr_map = |tcx, id| {
212+
tcx.ensure_ok().hir_crate(());
213+
tcx.hir_owner(id.def_id).as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs)
214+
};
209215
providers.def_span = |tcx, def_id| tcx.hir_span(tcx.local_def_id_to_hir_id(def_id));
210216
providers.def_ident_span = |tcx, def_id| {
211217
let hir_id = tcx.local_def_id_to_hir_id(def_id);

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ rustc_queries! {
166166
/// query gives you access to all other items. To avoid this fate, do not
167167
/// call `tcx.hir_crate(())`; instead, prefer wrappers like
168168
/// [`TyCtxt::hir_visit_all_item_likes_in_crate`].
169-
query hir_crate(key: ()) -> &'tcx Crate<'tcx> {
169+
query hir_crate(key: ()) -> &'tcx Crate {
170170
arena_cache
171171
eval_always
172172
desc { "getting the crate HIR" }

compiler/rustc_middle/src/ty/context.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,11 @@ impl<'tcx> TyCtxt<'tcx> {
12721272
TyCtxtFeed { tcx: self, key: () }
12731273
}
12741274

1275+
pub fn super_duper_perf_hack_experiment(self, key: LocalDefId) -> TyCtxtFeed<'tcx, LocalDefId> {
1276+
self.dep_graph.assert_eval_always();
1277+
TyCtxtFeed { tcx: self, key }
1278+
}
1279+
12751280
/// Only used in the resolver to register the `CRATE_DEF_ID` `DefId` and feed
12761281
/// some queries for it. It will panic if used twice.
12771282
pub fn create_local_crate_def_id(self, span: Span) -> TyCtxtFeed<'tcx, LocalDefId> {

compiler/rustc_query_system/src/dep_graph/graph.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,18 @@ impl<D: Deps> DepGraph<D> {
200200
}
201201
}
202202

203+
pub fn assert_eval_always(&self) {
204+
if let Some(..) = self.data {
205+
D::read_deps(|task_deps| {
206+
assert_matches!(
207+
task_deps,
208+
TaskDepsRef::EvalAlways,
209+
"expected no task dependency tracking"
210+
);
211+
})
212+
}
213+
}
214+
203215
pub fn with_ignore<OP, R>(&self, op: OP) -> R
204216
where
205217
OP: FnOnce() -> R,

0 commit comments

Comments
 (0)