Skip to content

Commit d329d46

Browse files
committed
Auto merge of rust-lang#58207 - nnethercote:intern_lazy_const, r=oli-obk
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 rust-lang#57432, fixes rust-lang#57829.
2 parents 618f5a0 + f2871a9 commit d329d46

File tree

17 files changed

+40
-38
lines changed

17 files changed

+40
-38
lines changed

src/librustc/mir/mod.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 12 additions & 10 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 6 deletions
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
},

0 commit comments

Comments
 (0)