Skip to content

Commit f2871a9

Browse files
committed
Make intern_lazy_const actually intern its argument.
Currently it just unconditionally allocates it in the arena. For a "Clean Check" build of the the `packed-simd` benchmark, this change reduces both the `max-rss` and `faults` counts by 59%; it slightly (~3%) increases the instruction counts but the `wall-time` is unchanged. For the same builds of a few other benchmarks, `max-rss` and `faults` drop by 1--5%, but instruction counts and `wall-time` changes are in the noise. Fixes #57432, fixes #57829.
1 parent 8ae730a commit f2871a9

File tree

17 files changed

+40
-38
lines changed

17 files changed

+40
-38
lines changed

src/librustc/mir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2154,7 +2154,7 @@ impl<'tcx> Operand<'tcx> {
21542154
span,
21552155
ty,
21562156
user_ty: None,
2157-
literal: tcx.intern_lazy_const(
2157+
literal: tcx.mk_lazy_const(
21582158
ty::LazyConst::Evaluated(ty::Const::zero_sized(ty)),
21592159
),
21602160
})

src/librustc/traits/project.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a,
408408
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
409409
let substs = tcx.lift_to_global(&substs).unwrap();
410410
let evaluated = evaluated.subst(tcx, substs);
411-
return tcx.intern_lazy_const(ty::LazyConst::Evaluated(evaluated));
411+
return tcx.mk_lazy_const(ty::LazyConst::Evaluated(evaluated));
412412
}
413413
}
414414
} else {
@@ -420,7 +420,7 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a,
420420
promoted: None
421421
};
422422
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
423-
return tcx.intern_lazy_const(ty::LazyConst::Evaluated(evaluated));
423+
return tcx.mk_lazy_const(ty::LazyConst::Evaluated(evaluated));
424424
}
425425
}
426426
}

src/librustc/traits/query/normalize.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx
203203
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
204204
let substs = tcx.lift_to_global(&substs).unwrap();
205205
let evaluated = evaluated.subst(tcx, substs);
206-
return tcx.intern_lazy_const(ty::LazyConst::Evaluated(evaluated));
206+
return tcx.mk_lazy_const(ty::LazyConst::Evaluated(evaluated));
207207
}
208208
}
209209
} else {
@@ -215,7 +215,7 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx
215215
promoted: None,
216216
};
217217
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
218-
return tcx.intern_lazy_const(ty::LazyConst::Evaluated(evaluated));
218+
return tcx.mk_lazy_const(ty::LazyConst::Evaluated(evaluated));
219219
}
220220
}
221221
}

src/librustc/ty/codec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ pub fn decode_lazy_const<'a, 'tcx, D>(decoder: &mut D)
252252
where D: TyDecoder<'a, 'tcx>,
253253
'tcx: 'a,
254254
{
255-
Ok(decoder.tcx().intern_lazy_const(Decodable::decode(decoder)?))
255+
Ok(decoder.tcx().mk_lazy_const(Decodable::decode(decoder)?))
256256
}
257257

258258
#[inline]

src/librustc/ty/context.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ pub struct CtxtInterners<'tcx> {
127127
goal: InternedSet<'tcx, GoalKind<'tcx>>,
128128
goal_list: InternedSet<'tcx, List<Goal<'tcx>>>,
129129
projs: InternedSet<'tcx, List<ProjectionKind<'tcx>>>,
130+
lazy_const: InternedSet<'tcx, LazyConst<'tcx>>,
130131
}
131132

132133
impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> {
@@ -144,6 +145,7 @@ impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> {
144145
goal: Default::default(),
145146
goal_list: Default::default(),
146147
projs: Default::default(),
148+
lazy_const: Default::default(),
147149
}
148150
}
149151

@@ -1096,10 +1098,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10961098
self.global_arenas.adt_def.alloc(def)
10971099
}
10981100

1099-
pub fn intern_const_alloc(
1100-
self,
1101-
alloc: Allocation,
1102-
) -> &'gcx Allocation {
1101+
pub fn intern_const_alloc(self, alloc: Allocation) -> &'gcx Allocation {
11031102
self.allocation_interner.borrow_mut().intern(alloc, |alloc| {
11041103
self.global_arenas.const_allocs.alloc(alloc)
11051104
})
@@ -1119,10 +1118,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
11191118
})
11201119
}
11211120

1122-
pub fn intern_lazy_const(self, c: ty::LazyConst<'tcx>) -> &'tcx ty::LazyConst<'tcx> {
1123-
self.global_interners.arena.alloc(c)
1124-
}
1125-
11261121
pub fn intern_layout(self, layout: LayoutDetails) -> &'gcx LayoutDetails {
11271122
self.layout_interner.borrow_mut().intern(layout, |layout| {
11281123
self.global_arenas.layout.alloc(layout)
@@ -2271,6 +2266,12 @@ impl<'tcx: 'lcx, 'lcx> Borrow<GoalKind<'lcx>> for Interned<'tcx, GoalKind<'tcx>>
22712266
}
22722267
}
22732268

2269+
impl<'tcx: 'lcx, 'lcx> Borrow<LazyConst<'lcx>> for Interned<'tcx, LazyConst<'tcx>> {
2270+
fn borrow<'a>(&'a self) -> &'a LazyConst<'lcx> {
2271+
&self.0
2272+
}
2273+
}
2274+
22742275
impl<'tcx: 'lcx, 'lcx> Borrow<[ExistentialPredicate<'lcx>]>
22752276
for Interned<'tcx, List<ExistentialPredicate<'tcx>>> {
22762277
fn borrow<'a>(&'a self) -> &'a [ExistentialPredicate<'lcx>] {
@@ -2377,7 +2378,8 @@ pub fn keep_local<'tcx, T: ty::TypeFoldable<'tcx>>(x: &T) -> bool {
23772378

23782379
direct_interners!('tcx,
23792380
region: mk_region(|r: &RegionKind| r.keep_in_local_tcx()) -> RegionKind,
2380-
goal: mk_goal(|c: &GoalKind<'_>| keep_local(c)) -> GoalKind<'tcx>
2381+
goal: mk_goal(|c: &GoalKind<'_>| keep_local(c)) -> GoalKind<'tcx>,
2382+
lazy_const: mk_lazy_const(|c: &LazyConst<'_>| keep_local(&c)) -> LazyConst<'tcx>
23812383
);
23822384

23832385
macro_rules! slice_interners {
@@ -2562,7 +2564,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
25622564

25632565
#[inline]
25642566
pub fn mk_array(self, ty: Ty<'tcx>, n: u64) -> Ty<'tcx> {
2565-
self.mk_ty(Array(ty, self.intern_lazy_const(
2567+
self.mk_ty(Array(ty, self.mk_lazy_const(
25662568
ty::LazyConst::Evaluated(ty::Const::from_usize(self.global_tcx(), n))
25672569
)))
25682570
}

src/librustc/ty/structural_impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,7 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::LazyConst<'tcx> {
10421042
ty::LazyConst::Unevaluated(*def_id, substs.fold_with(folder))
10431043
}
10441044
};
1045-
folder.tcx().intern_lazy_const(new)
1045+
folder.tcx().mk_lazy_const(new)
10461046
}
10471047

10481048
fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {

src/librustc_mir/build/expr/as_rvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
268268
span: expr_span,
269269
ty: this.hir.tcx().types.u32,
270270
user_ty: None,
271-
literal: this.hir.tcx().intern_lazy_const(ty::LazyConst::Evaluated(
271+
literal: this.hir.tcx().mk_lazy_const(ty::LazyConst::Evaluated(
272272
ty::Const::from_bits(
273273
this.hir.tcx(),
274274
0,

src/librustc_mir/build/matches/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
302302
}
303303
let eq_def_id = self.hir.tcx().lang_items().eq_trait().unwrap();
304304
let (mty, method) = self.hir.trait_method(eq_def_id, "eq", ty, &[ty.into()]);
305-
let method = self.hir.tcx().intern_lazy_const(ty::LazyConst::Evaluated(method));
305+
let method = self.hir.tcx().mk_lazy_const(ty::LazyConst::Evaluated(method));
306306

307307
let re_erased = self.hir.tcx().types.re_erased;
308308
// take the argument by reference

src/librustc_mir/build/misc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
3333
span,
3434
ty,
3535
user_ty: None,
36-
literal: self.hir.tcx().intern_lazy_const(ty::LazyConst::Evaluated(literal)),
36+
literal: self.hir.tcx().mk_lazy_const(ty::LazyConst::Evaluated(literal)),
3737
};
3838
Operand::Constant(constant)
3939
}

src/librustc_mir/hair/cx/expr.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
342342
}
343343

344344
hir::ExprKind::Lit(ref lit) => ExprKind::Literal {
345-
literal: cx.tcx.intern_lazy_const(ty::LazyConst::Evaluated(
345+
literal: cx.tcx.mk_lazy_const(ty::LazyConst::Evaluated(
346346
cx.const_eval_literal(&lit.node, expr_ty, lit.span, false)
347347
)),
348348
user_ty: None,
@@ -442,7 +442,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
442442
} else {
443443
if let hir::ExprKind::Lit(ref lit) = arg.node {
444444
ExprKind::Literal {
445-
literal: cx.tcx.intern_lazy_const(ty::LazyConst::Evaluated(
445+
literal: cx.tcx.mk_lazy_const(ty::LazyConst::Evaluated(
446446
cx.const_eval_literal(&lit.node, expr_ty, lit.span, true)
447447
)),
448448
user_ty: None,
@@ -702,7 +702,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
702702
ty: var_ty,
703703
span: expr.span,
704704
kind: ExprKind::Literal {
705-
literal: cx.tcx.intern_lazy_const(literal),
705+
literal: cx.tcx.mk_lazy_const(literal),
706706
user_ty: None
707707
},
708708
}.to_ref();
@@ -856,7 +856,7 @@ fn method_callee<'a, 'gcx, 'tcx>(
856856
ty,
857857
span,
858858
kind: ExprKind::Literal {
859-
literal: cx.tcx().intern_lazy_const(ty::LazyConst::Evaluated(
859+
literal: cx.tcx().mk_lazy_const(ty::LazyConst::Evaluated(
860860
ty::Const::zero_sized(ty)
861861
)),
862862
user_ty,
@@ -918,7 +918,7 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
918918
let user_ty = user_substs_applied_to_def(cx, expr.hir_id, &def);
919919
debug!("convert_path_expr: user_ty={:?}", user_ty);
920920
ExprKind::Literal {
921-
literal: cx.tcx.intern_lazy_const(ty::LazyConst::Evaluated(ty::Const::zero_sized(
921+
literal: cx.tcx.mk_lazy_const(ty::LazyConst::Evaluated(ty::Const::zero_sized(
922922
cx.tables().node_id_to_type(expr.hir_id),
923923
))),
924924
user_ty,
@@ -930,7 +930,7 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
930930
let user_ty = user_substs_applied_to_def(cx, expr.hir_id, &def);
931931
debug!("convert_path_expr: (const) user_ty={:?}", user_ty);
932932
ExprKind::Literal {
933-
literal: cx.tcx.intern_lazy_const(ty::LazyConst::Unevaluated(def_id, substs)),
933+
literal: cx.tcx.mk_lazy_const(ty::LazyConst::Unevaluated(def_id, substs)),
934934
user_ty,
935935
}
936936
},

src/librustc_mir/hair/cx/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
110110
}
111111

112112
pub fn usize_literal(&mut self, value: u64) -> &'tcx ty::LazyConst<'tcx> {
113-
self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_usize(self.tcx, value)))
113+
self.tcx.mk_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_usize(self.tcx, value)))
114114
}
115115

116116
pub fn bool_ty(&mut self) -> Ty<'tcx> {
@@ -122,11 +122,11 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
122122
}
123123

124124
pub fn true_literal(&mut self) -> &'tcx ty::LazyConst<'tcx> {
125-
self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bool(self.tcx, true)))
125+
self.tcx.mk_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bool(self.tcx, true)))
126126
}
127127

128128
pub fn false_literal(&mut self) -> &'tcx ty::LazyConst<'tcx> {
129-
self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bool(self.tcx, false)))
129+
self.tcx.mk_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bool(self.tcx, false)))
130130
}
131131

132132
pub fn const_eval_literal(

src/librustc_mir/shim.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> {
459459
span: self.span,
460460
ty: func_ty,
461461
user_ty: None,
462-
literal: tcx.intern_lazy_const(ty::LazyConst::Evaluated(
462+
literal: tcx.mk_lazy_const(ty::LazyConst::Evaluated(
463463
ty::Const::zero_sized(func_ty),
464464
)),
465465
});
@@ -521,7 +521,7 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> {
521521
span: self.span,
522522
ty: self.tcx.types.usize,
523523
user_ty: None,
524-
literal: self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(
524+
literal: self.tcx.mk_lazy_const(ty::LazyConst::Evaluated(
525525
ty::Const::from_usize(self.tcx, value),
526526
)),
527527
}
@@ -759,7 +759,7 @@ fn build_call_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
759759
span,
760760
ty,
761761
user_ty: None,
762-
literal: tcx.intern_lazy_const(ty::LazyConst::Evaluated(
762+
literal: tcx.mk_lazy_const(ty::LazyConst::Evaluated(
763763
ty::Const::zero_sized(ty)
764764
)),
765765
}),

src/librustc_mir/transform/elaborate_drops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
533533
span,
534534
ty: self.tcx.types.bool,
535535
user_ty: None,
536-
literal: self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(
536+
literal: self.tcx.mk_lazy_const(ty::LazyConst::Evaluated(
537537
ty::Const::from_bool(self.tcx, val),
538538
)),
539539
})))

src/librustc_mir/transform/generator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl<'a, 'tcx> TransformVisitor<'a, 'tcx> {
198198
span: source_info.span,
199199
ty: self.tcx.types.u32,
200200
user_ty: None,
201-
literal: self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bits(
201+
literal: self.tcx.mk_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bits(
202202
self.tcx,
203203
state_disc.into(),
204204
ty::ParamEnv::empty().and(self.tcx.types.u32)
@@ -731,7 +731,7 @@ fn insert_panic_block<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
731731
span: mir.span,
732732
ty: tcx.types.bool,
733733
user_ty: None,
734-
literal: tcx.intern_lazy_const(ty::LazyConst::Evaluated(
734+
literal: tcx.mk_lazy_const(ty::LazyConst::Evaluated(
735735
ty::Const::from_bool(tcx, false),
736736
)),
737737
}),

src/librustc_mir/util/elaborate_drops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ impl<'l, 'b, 'tcx, D> DropCtxt<'l, 'b, 'tcx, D>
963963
span: self.source_info.span,
964964
ty: self.tcx().types.usize,
965965
user_ty: None,
966-
literal: self.tcx().intern_lazy_const(ty::LazyConst::Evaluated(
966+
literal: self.tcx().mk_lazy_const(ty::LazyConst::Evaluated(
967967
ty::Const::from_usize(self.tcx(), val.into())
968968
)),
969969
})

src/librustc_typeck/astconv.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
17931793
let length_def_id = tcx.hir().local_def_id(length.id);
17941794
let substs = Substs::identity_for_item(tcx, length_def_id);
17951795
let length = ty::LazyConst::Unevaluated(length_def_id, substs);
1796-
let length = tcx.intern_lazy_const(length);
1796+
let length = tcx.mk_lazy_const(length);
17971797
let array_ty = tcx.mk_ty(ty::Array(self.ast_ty_to_ty(&ty), length));
17981798
self.normalize_ty(ast_ty.span, array_ty)
17991799
}

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4597,7 +4597,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
45974597
if element_ty.references_error() {
45984598
tcx.types.err
45994599
} else if let Ok(count) = count {
4600-
tcx.mk_ty(ty::Array(t, tcx.intern_lazy_const(ty::LazyConst::Evaluated(count))))
4600+
tcx.mk_ty(ty::Array(t, tcx.mk_lazy_const(ty::LazyConst::Evaluated(count))))
46014601
} else {
46024602
tcx.types.err
46034603
}

0 commit comments

Comments
 (0)