Skip to content

Commit 2c990c5

Browse files
committed
Auto merge of #138705 - oli-obk:hir-split, r=<try>
[perf] Decouple directly accessing a HIR owner from ast lowering r? `@ghost` cc #95004
2 parents bbd3a5a + 4f3ab35 commit 2c990c5

File tree

10 files changed

+83
-43
lines changed

10 files changed

+83
-43
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/definitions.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ impl DefPathTable {
8888
DefPathHash::new(self.stable_crate_id, hash)
8989
}
9090

91+
pub fn def_keys(&self) -> impl Iterator<Item = DefIndex> + ExactSizeIterator {
92+
self.index_to_key.iter_enumerated().map(|(index, _)| index)
93+
}
94+
9195
pub fn enumerated_keys_and_path_hashes(
9296
&self,
9397
) -> impl Iterator<Item = (DefIndex, &DefKey, DefPathHash)> + ExactSizeIterator {

compiler/rustc_hir/src/hir.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,11 +1462,15 @@ pub struct AttributeMap<'tcx> {
14621462
}
14631463

14641464
impl<'tcx> AttributeMap<'tcx> {
1465-
pub const EMPTY: &'static AttributeMap<'static> = &AttributeMap {
1466-
map: SortedMap::new(),
1467-
opt_hash: Some(Fingerprint::ZERO),
1468-
define_opaque: None,
1469-
};
1465+
pub const EMPTY: &'static AttributeMap<'static> = &Self::empty();
1466+
1467+
pub const fn empty() -> AttributeMap<'static> {
1468+
AttributeMap {
1469+
map: SortedMap::new(),
1470+
opt_hash: Some(Fingerprint::ZERO),
1471+
define_opaque: None,
1472+
}
1473+
}
14701474

14711475
#[inline]
14721476
pub fn get(&self, id: ItemLocalId) -> &'tcx [Attribute] {
@@ -1567,8 +1571,7 @@ impl<'tcx> MaybeOwner<'tcx> {
15671571
///
15681572
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
15691573
#[derive(Debug)]
1570-
pub struct Crate<'hir> {
1571-
pub owners: IndexVec<LocalDefId, MaybeOwner<'hir>>,
1574+
pub struct Crate {
15721575
// Only present when incr. comp. is enabled.
15731576
pub opt_hir_hash: Option<Fingerprint>,
15741577
}

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/arena.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ macro_rules! arena_types {
118118
[decode] specialization_graph: rustc_middle::traits::specialization_graph::Graph,
119119
[] crate_inherent_impls: rustc_middle::ty::CrateInherentImpls,
120120
[] hir_owner_nodes: rustc_hir::OwnerNodes<'tcx>,
121+
[] owner_info: rustc_hir::OwnerInfo<'tcx>,
121122
]);
122123
)
123124
}

compiler/rustc_middle/src/hir/map.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
99
use rustc_data_structures::svh::Svh;
1010
use rustc_data_structures::sync::{DynSend, DynSync, par_for_each_in, try_par_for_each_in};
1111
use rustc_hir::def::{DefKind, Res};
12-
use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId, LocalModDefId};
12+
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId};
1313
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
1414
use rustc_hir::intravisit::Visitor;
1515
use rustc_hir::*;
@@ -396,9 +396,8 @@ impl<'tcx> TyCtxt<'tcx> {
396396
where
397397
V: Visitor<'tcx>,
398398
{
399-
let krate = self.hir_crate(());
400-
for info in krate.owners.iter() {
401-
if let MaybeOwner::Owner(info) = info {
399+
for def_id in self.hir_crate_items(()).definitions().chain([CRATE_DEF_ID]) {
400+
if let MaybeOwner::Owner(info) = self.hir_owner(def_id) {
402401
for attrs in info.attrs.map.values() {
403402
walk_list!(visitor, visit_attribute, *attrs);
404403
}

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,25 @@ 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.local_def_id_to_hir_id = |tcx, def_id| match tcx.hir_crate(()).owners[def_id] {
185-
MaybeOwner::Owner(_) => HirId::make_owner(def_id),
186-
MaybeOwner::NonOwner(hir_id) => hir_id,
187-
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)
188195
};
189-
providers.opt_hir_owner_nodes =
190-
|tcx, id| tcx.hir_crate(()).owners.get(id)?.as_owner().map(|i| &i.nodes);
191196
providers.hir_owner_parent = |tcx, owner_id| {
192197
tcx.opt_local_parent(owner_id.def_id).map_or(CRATE_HIR_ID, |parent_def_id| {
193198
let parent_owner_id = tcx.local_def_id_to_hir_id(parent_def_id).owner;
194199
HirId {
195200
owner: parent_owner_id,
196-
local_id: tcx.hir_crate(()).owners[parent_owner_id.def_id]
201+
local_id: tcx
202+
.hir_owner(parent_owner_id.def_id)
197203
.unwrap()
198204
.parenting
199205
.get(&owner_id.def_id)
@@ -203,7 +209,8 @@ pub fn provide(providers: &mut Providers) {
203209
})
204210
};
205211
providers.hir_attr_map = |tcx, id| {
206-
tcx.hir_crate(()).owners[id.def_id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs)
212+
tcx.ensure_ok().hir_crate(());
213+
tcx.hir_owner(id.def_id).as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs)
207214
};
208215
providers.def_span = |tcx, def_id| tcx.hir_span(tcx.local_def_id_to_hir_id(def_id));
209216
providers.def_ident_span = |tcx, def_id| {
@@ -236,7 +243,6 @@ pub fn provide(providers: &mut Providers) {
236243
|tcx, trait_id| tcx.resolutions(()).trait_impls.get(&trait_id).map_or(&[], |xs| &xs[..]);
237244
providers.expn_that_defined =
238245
|tcx, id| tcx.resolutions(()).expn_that_defined.get(&id).copied().unwrap_or(ExpnId::root());
239-
providers.in_scope_traits_map = |tcx, id| {
240-
tcx.hir_crate(()).owners[id.def_id].as_owner().map(|owner_info| &owner_info.trait_map)
241-
};
246+
providers.in_scope_traits_map =
247+
|tcx, id| tcx.hir_owner(id.def_id).as_owner().map(|owner_info| &owner_info.trait_map);
242248
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,19 @@ 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" }
173173
}
174174

175+
/// A query decoupling the `hir_crate` query from everything else
176+
query hir_owner(key: LocalDefId) -> rustc_hir::MaybeOwner<'tcx> {
177+
no_hash
178+
desc { |tcx| "getting HIR of `{}`", tcx.def_path_str(key) }
179+
feedable
180+
}
181+
175182
/// All items in the crate.
176183
query hir_crate_items(_: ()) -> &'tcx rustc_middle::hir::ModuleItems {
177184
arena_cache
@@ -192,7 +199,6 @@ rustc_queries! {
192199
/// Returns HIR ID for the given `LocalDefId`.
193200
query local_def_id_to_hir_id(key: LocalDefId) -> hir::HirId {
194201
desc { |tcx| "getting HIR ID of `{}`", tcx.def_path_str(key) }
195-
feedable
196202
}
197203

198204
/// Gives access to the HIR node's parent for the HIR owner `key`.
@@ -209,7 +215,6 @@ rustc_queries! {
209215
/// Avoid calling this query directly.
210216
query opt_hir_owner_nodes(key: LocalDefId) -> Option<&'tcx hir::OwnerNodes<'tcx>> {
211217
desc { |tcx| "getting HIR owner items in `{}`", tcx.def_path_str(key) }
212-
feedable
213218
}
214219

215220
/// Gives access to the HIR attributes inside the HIR owner `key`.
@@ -218,7 +223,6 @@ rustc_queries! {
218223
/// Avoid calling this query directly.
219224
query hir_attr_map(key: hir::OwnerId) -> &'tcx hir::AttributeMap<'tcx> {
220225
desc { |tcx| "getting HIR owner attributes in `{}`", tcx.def_path_str(key) }
221-
feedable
222226
}
223227

224228
/// Returns the *default* of the const pararameter given by `DefId`.

compiler/rustc_middle/src/ty/context.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId};
3737
use rustc_hir::definitions::{DefPathData, Definitions, DisambiguatorState};
3838
use rustc_hir::intravisit::VisitorExt;
3939
use rustc_hir::lang_items::LangItem;
40-
use rustc_hir::{self as hir, Attribute, HirId, Node, TraitCandidate};
40+
use rustc_hir::{self as hir, Attribute, HirId, MaybeOwner, Node, OwnerInfo, TraitCandidate};
4141
use rustc_index::IndexVec;
4242
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
4343
use rustc_query_system::cache::WithDepNode;
@@ -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> {
@@ -1326,24 +1331,25 @@ impl<'tcx> TyCtxtFeed<'tcx, LocalDefId> {
13261331

13271332
// Fills in all the important parts needed by HIR queries
13281333
pub fn feed_hir(&self) {
1329-
self.local_def_id_to_hir_id(HirId::make_owner(self.def_id()));
1330-
13311334
let node = hir::OwnerNode::Synthetic;
13321335
let bodies = Default::default();
1333-
let attrs = hir::AttributeMap::EMPTY;
1334-
1336+
let attrs = hir::AttributeMap::empty();
13351337
let (opt_hash_including_bodies, _) =
13361338
self.tcx.hash_owner_nodes(node, &bodies, &attrs.map, attrs.define_opaque);
13371339
let node = node.into();
1338-
self.opt_hir_owner_nodes(Some(self.tcx.arena.alloc(hir::OwnerNodes {
1339-
opt_hash_including_bodies,
1340-
nodes: IndexVec::from_elem_n(
1341-
hir::ParentedNode { parent: hir::ItemLocalId::INVALID, node },
1342-
1,
1343-
),
1344-
bodies,
1340+
self.hir_owner(MaybeOwner::Owner(self.tcx.arena.alloc(OwnerInfo {
1341+
nodes: hir::OwnerNodes {
1342+
opt_hash_including_bodies,
1343+
nodes: IndexVec::from_elem_n(
1344+
hir::ParentedNode { parent: hir::ItemLocalId::INVALID, node },
1345+
1,
1346+
),
1347+
bodies,
1348+
},
1349+
parenting: Default::default(),
1350+
attrs,
1351+
trait_map: Default::default(),
13451352
})));
1346-
self.feed_owner_id().hir_attr_map(attrs);
13471353
}
13481354
}
13491355

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)