Skip to content

Commit 6abb638

Browse files
committed
Auto merge of #93301 - spastorino:perf-test-1, r=oli-obk
Store hir_id_to_def_id in OwnerInfo. This is for perf test purposes only. Related to #89278
2 parents a7f3757 + 384189c commit 6abb638

File tree

10 files changed

+247
-213
lines changed

10 files changed

+247
-213
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,20 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
479479
let attrs = std::mem::take(&mut self.attrs);
480480
let mut bodies = std::mem::take(&mut self.bodies);
481481
let local_node_ids = std::mem::take(&mut self.local_node_ids);
482+
483+
let local_id_to_def_id = local_node_ids
484+
.iter()
485+
.filter_map(|&node_id| {
486+
let hir_id = self.node_id_to_hir_id[node_id]?;
487+
if hir_id.local_id == hir::ItemLocalId::new(0) {
488+
None
489+
} else {
490+
let def_id = self.resolver.opt_local_def_id(node_id)?;
491+
Some((hir_id.local_id, def_id))
492+
}
493+
})
494+
.collect();
495+
482496
let trait_map = local_node_ids
483497
.into_iter()
484498
.filter_map(|node_id| {
@@ -501,7 +515,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
501515
let (hash_including_bodies, hash_without_bodies) = self.hash_owner(node, &bodies);
502516
let (nodes, parenting) =
503517
index::index_hir(self.sess, self.resolver.definitions(), node, &bodies);
504-
let nodes = hir::OwnerNodes { hash_including_bodies, hash_without_bodies, nodes, bodies };
518+
let nodes = hir::OwnerNodes {
519+
hash_including_bodies,
520+
hash_without_bodies,
521+
nodes,
522+
bodies,
523+
local_id_to_def_id,
524+
};
505525
let attrs = {
506526
let mut hcx = self.resolver.create_stable_hashing_context();
507527
let mut stable_hasher = StableHasher::new();

compiler/rustc_hir/src/definitions.rs

-14
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ pub struct Definitions {
107107
/// Their `HirId`s are defined by their position while lowering the enclosing owner.
108108
// FIXME(cjgillot) Some `LocalDefId`s from `use` items are dropped during lowering and lack a `HirId`.
109109
pub(super) def_id_to_hir_id: IndexVec<LocalDefId, Option<hir::HirId>>,
110-
/// The reverse mapping of `def_id_to_hir_id`.
111-
pub(super) hir_id_to_def_id: FxHashMap<hir::HirId, LocalDefId>,
112110

113111
/// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`.
114112
expansions_that_defined: FxHashMap<LocalDefId, ExpnId>,
@@ -330,11 +328,6 @@ impl Definitions {
330328
self.def_id_to_hir_id[id].unwrap()
331329
}
332330

333-
#[inline]
334-
pub fn opt_hir_id_to_local_def_id(&self, hir_id: hir::HirId) -> Option<LocalDefId> {
335-
self.hir_id_to_def_id.get(&hir_id).copied()
336-
}
337-
338331
/// Adds a root definition (no parent) and a few other reserved definitions.
339332
pub fn new(stable_crate_id: StableCrateId, crate_span: Span) -> Definitions {
340333
let key = DefKey {
@@ -362,7 +355,6 @@ impl Definitions {
362355
Definitions {
363356
table,
364357
def_id_to_hir_id: Default::default(),
365-
hir_id_to_def_id: Default::default(),
366358
expansions_that_defined: Default::default(),
367359
def_id_to_span,
368360
stable_crate_id,
@@ -425,12 +417,6 @@ impl Definitions {
425417
"trying to initialize `LocalDefId` <-> `HirId` mappings twice"
426418
);
427419

428-
// Build the reverse mapping of `def_id_to_hir_id`.
429-
self.hir_id_to_def_id = mapping
430-
.iter_enumerated()
431-
.filter_map(|(def_id, hir_id)| hir_id.map(|hir_id| (hir_id, def_id)))
432-
.collect();
433-
434420
self.def_id_to_hir_id = mapping;
435421
}
436422

compiler/rustc_hir/src/hir.rs

+2
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,8 @@ pub struct OwnerNodes<'tcx> {
707707
pub nodes: IndexVec<ItemLocalId, Option<ParentedNode<'tcx>>>,
708708
/// Content of local bodies.
709709
pub bodies: SortedMap<ItemLocalId, &'tcx Body<'tcx>>,
710+
/// Non-owning definitions contained in this owner.
711+
pub local_id_to_def_id: SortedMap<ItemLocalId, LocalDefId>,
710712
}
711713

712714
/// Full information resulting from lowering an AST node.

compiler/rustc_hir/src/stable_hash_impls.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,13 @@ impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'
208208
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
209209
// We ignore the `nodes` and `bodies` fields since these refer to information included in
210210
// `hash` which is hashed in the collector and used for the crate hash.
211-
let OwnerNodes { hash_including_bodies, hash_without_bodies: _, nodes: _, bodies: _ } =
212-
*self;
211+
let OwnerNodes {
212+
hash_including_bodies,
213+
hash_without_bodies: _,
214+
nodes: _,
215+
bodies: _,
216+
local_id_to_def_id: _,
217+
} = *self;
213218
hash_including_bodies.hash_stable(hcx, hasher);
214219
}
215220
}

compiler/rustc_middle/src/hir/map/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,11 @@ impl<'hir> Map<'hir> {
204204
if hir_id.local_id == ItemLocalId::new(0) {
205205
Some(hir_id.owner)
206206
} else {
207-
// FIXME(#85914) is this access safe for incr. comp.?
208-
self.tcx.untracked_resolutions.definitions.opt_hir_id_to_local_def_id(hir_id)
207+
self.tcx
208+
.hir_owner_nodes(hir_id.owner)?
209+
.local_id_to_def_id
210+
.get(&hir_id.local_id)
211+
.copied()
209212
}
210213
}
211214

src/test/incremental/hashes/extern_mods.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ extern "C" {
2727
}
2828

2929
#[cfg(not(any(cfail1,cfail4)))]
30-
#[rustc_clean(cfg = "cfail2", except = "hir_owner")]
30+
#[rustc_clean(cfg = "cfail2", except = "hir_owner,hir_owner_nodes")]
3131
#[rustc_clean(cfg = "cfail3")]
32-
#[rustc_clean(cfg = "cfail5", except = "hir_owner")]
32+
#[rustc_clean(cfg = "cfail5", except = "hir_owner,hir_owner_nodes")]
3333
#[rustc_clean(cfg = "cfail6")]
3434
extern "C" {
3535
pub fn change_function_name2(c: i64) -> i32;
@@ -132,9 +132,9 @@ extern "C" {
132132
}
133133

134134
#[cfg(not(any(cfail1,cfail4)))]
135-
#[rustc_clean(cfg = "cfail2", except = "hir_owner")]
135+
#[rustc_clean(cfg = "cfail2", except = "hir_owner,hir_owner_nodes")]
136136
#[rustc_clean(cfg = "cfail3")]
137-
#[rustc_clean(cfg = "cfail5", except = "hir_owner")]
137+
#[rustc_clean(cfg = "cfail5", except = "hir_owner,hir_owner_nodes")]
138138
#[rustc_clean(cfg = "cfail6")]
139139
extern "rust-call" {
140140
pub fn change_calling_convention(c: i32);
@@ -162,9 +162,9 @@ extern "C" {
162162
}
163163

164164
#[cfg(not(any(cfail1,cfail4)))]
165-
#[rustc_clean(cfg = "cfail2", except = "hir_owner")]
165+
#[rustc_clean(cfg = "cfail2", except = "hir_owner,hir_owner_nodes")]
166166
#[rustc_clean(cfg = "cfail3")]
167-
#[rustc_clean(cfg = "cfail5", except = "hir_owner")]
167+
#[rustc_clean(cfg = "cfail5", except = "hir_owner,hir_owner_nodes")]
168168
#[rustc_clean(cfg = "cfail6")]
169169
extern "C" {
170170
pub fn add_function1(c: i32);

0 commit comments

Comments
 (0)