Skip to content

Commit eae5031

Browse files
committed
Cache whether a body has inline consts
1 parent ddc5f9b commit eae5031

File tree

6 files changed

+21
-4
lines changed

6 files changed

+21
-4
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
7474

7575
let kind = match &e.kind {
7676
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
77-
ExprKind::ConstBlock(c) => hir::ExprKind::ConstBlock(self.lower_expr(c)),
77+
ExprKind::ConstBlock(c) => {
78+
self.has_inline_consts = true;
79+
hir::ExprKind::ConstBlock(self.lower_expr(c))
80+
}
7881
ExprKind::Repeat(expr, count) => {
7982
let expr = self.lower_expr(expr);
8083
let count = self.lower_array_length(count);

compiler/rustc_ast_lowering/src/lib.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ struct LoweringContext<'a, 'hir> {
9696

9797
/// Bodies inside the owner being lowered.
9898
bodies: Vec<(hir::ItemLocalId, &'hir hir::Body<'hir>)>,
99+
/// Whether there were inline consts that typeck will split out into bodies
100+
has_inline_consts: bool,
99101
/// Attributes inside the owner being lowered.
100102
attrs: SortedMap<hir::ItemLocalId, &'hir [Attribute]>,
101103
/// Collect items that were created by lowering the current owner.
@@ -158,6 +160,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
158160
item_local_id_counter: hir::ItemLocalId::ZERO,
159161
node_id_to_local_id: Default::default(),
160162
trait_map: Default::default(),
163+
has_inline_consts: false,
161164

162165
// Lowering state.
163166
catch_scope: None,
@@ -567,6 +570,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
567570

568571
let current_attrs = std::mem::take(&mut self.attrs);
569572
let current_bodies = std::mem::take(&mut self.bodies);
573+
let current_has_inline_consts = std::mem::take(&mut self.has_inline_consts);
570574
let current_node_ids = std::mem::take(&mut self.node_id_to_local_id);
571575
let current_trait_map = std::mem::take(&mut self.trait_map);
572576
let current_owner =
@@ -593,6 +597,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
593597

594598
self.attrs = current_attrs;
595599
self.bodies = current_bodies;
600+
self.has_inline_consts = current_has_inline_consts;
596601
self.node_id_to_local_id = current_node_ids;
597602
self.trait_map = current_trait_map;
598603
self.current_hir_id_owner = current_owner;
@@ -629,6 +634,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
629634
let attrs = std::mem::take(&mut self.attrs);
630635
let mut bodies = std::mem::take(&mut self.bodies);
631636
let trait_map = std::mem::take(&mut self.trait_map);
637+
let has_inline_consts = std::mem::take(&mut self.has_inline_consts);
632638

633639
#[cfg(debug_assertions)]
634640
for (id, attrs) in attrs.iter() {
@@ -646,7 +652,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
646652
self.tcx.hash_owner_nodes(node, &bodies, &attrs);
647653
let num_nodes = self.item_local_id_counter.as_usize();
648654
let (nodes, parenting) = index::index_hir(self.tcx, node, &bodies, num_nodes);
649-
let nodes = hir::OwnerNodes { opt_hash_including_bodies, nodes, bodies };
655+
let nodes = hir::OwnerNodes { opt_hash_including_bodies, nodes, bodies, has_inline_consts };
650656
let attrs = hir::AttributeMap { map: attrs, opt_hash: attrs_hash };
651657

652658
self.arena.alloc(hir::OwnerInfo { nodes, parenting, attrs, trait_map })

compiler/rustc_hir/src/hir.rs

+3
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,9 @@ pub struct OwnerNodes<'tcx> {
876876
pub nodes: IndexVec<ItemLocalId, ParentedNode<'tcx>>,
877877
/// Content of local bodies.
878878
pub bodies: SortedMap<ItemLocalId, &'tcx Body<'tcx>>,
879+
/// Whether the body contains inline constants that are created for the query system during typeck
880+
/// of the body.
881+
pub has_inline_consts: bool,
879882
}
880883

881884
impl<'tcx> OwnerNodes<'tcx> {

compiler/rustc_hir/src/stable_hash_impls.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'
9393
// `local_id_to_def_id` is also ignored because is dependent on the body, then just hashing
9494
// the body satisfies the condition of two nodes being different have different
9595
// `hash_stable` results.
96-
let OwnerNodes { opt_hash_including_bodies, nodes: _, bodies: _ } = *self;
96+
let OwnerNodes { opt_hash_including_bodies, nodes: _, bodies: _, has_inline_consts: _ } =
97+
*self;
9798
opt_hash_including_bodies.unwrap().hash_stable(hcx, hasher);
9899
}
99100
}

compiler/rustc_middle/src/ty/context.rs

+1
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,7 @@ impl<'tcx> TyCtxtFeed<'tcx, LocalDefId> {
737737
1,
738738
),
739739
bodies,
740+
has_inline_consts: false,
740741
})));
741742
self.feed_owner_id().hir_attrs(attrs);
742743
}

compiler/rustc_mir_transform/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,10 @@ fn mir_keys(tcx: TyCtxt<'_>, (): ()) -> FxIndexSet<LocalDefId> {
225225
// Inline consts' bodies are created in
226226
// typeck instead of during ast lowering, like all other bodies so far.
227227
for def_id in tcx.hir().body_owners() {
228-
set.extend(tcx.typeck(def_id).inline_consts.values())
228+
// Incremental performance optimization: only load typeck results for things that actually have inline consts
229+
if tcx.hir_owner_nodes(tcx.hir().body_owned_by(def_id).hir_id.owner).has_inline_consts {
230+
set.extend(tcx.typeck(def_id).inline_consts.values())
231+
}
229232
}
230233

231234
// Additionally, tuple struct/variant constructors have MIR, but

0 commit comments

Comments
 (0)