Auto merge of #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 #57432, fixes #57829.
This commit is contained in:
bors 2019-02-09 12:38:51 +00:00
commit d329d46a3f
17 changed files with 40 additions and 38 deletions

View file

@ -2154,7 +2154,7 @@ impl<'tcx> Operand<'tcx> {
span, span,
ty, ty,
user_ty: None, user_ty: None,
literal: tcx.intern_lazy_const( literal: tcx.mk_lazy_const(
ty::LazyConst::Evaluated(ty::Const::zero_sized(ty)), ty::LazyConst::Evaluated(ty::Const::zero_sized(ty)),
), ),
}) })

View file

@ -408,7 +408,7 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a,
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) { if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
let substs = tcx.lift_to_global(&substs).unwrap(); let substs = tcx.lift_to_global(&substs).unwrap();
let evaluated = evaluated.subst(tcx, substs); let evaluated = evaluated.subst(tcx, substs);
return tcx.intern_lazy_const(ty::LazyConst::Evaluated(evaluated)); return tcx.mk_lazy_const(ty::LazyConst::Evaluated(evaluated));
} }
} }
} else { } else {
@ -420,7 +420,7 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a,
promoted: None promoted: None
}; };
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) { if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
return tcx.intern_lazy_const(ty::LazyConst::Evaluated(evaluated)); return tcx.mk_lazy_const(ty::LazyConst::Evaluated(evaluated));
} }
} }
} }

View file

@ -203,7 +203,7 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) { if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
let substs = tcx.lift_to_global(&substs).unwrap(); let substs = tcx.lift_to_global(&substs).unwrap();
let evaluated = evaluated.subst(tcx, substs); let evaluated = evaluated.subst(tcx, substs);
return tcx.intern_lazy_const(ty::LazyConst::Evaluated(evaluated)); return tcx.mk_lazy_const(ty::LazyConst::Evaluated(evaluated));
} }
} }
} else { } else {
@ -215,7 +215,7 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx
promoted: None, promoted: None,
}; };
if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) { if let Ok(evaluated) = tcx.const_eval(param_env.and(cid)) {
return tcx.intern_lazy_const(ty::LazyConst::Evaluated(evaluated)); return tcx.mk_lazy_const(ty::LazyConst::Evaluated(evaluated));
} }
} }
} }

View file

@ -252,7 +252,7 @@ pub fn decode_lazy_const<'a, 'tcx, D>(decoder: &mut D)
where D: TyDecoder<'a, 'tcx>, where D: TyDecoder<'a, 'tcx>,
'tcx: 'a, 'tcx: 'a,
{ {
Ok(decoder.tcx().intern_lazy_const(Decodable::decode(decoder)?)) Ok(decoder.tcx().mk_lazy_const(Decodable::decode(decoder)?))
} }
#[inline] #[inline]

View file

@ -127,6 +127,7 @@ pub struct CtxtInterners<'tcx> {
goal: InternedSet<'tcx, GoalKind<'tcx>>, goal: InternedSet<'tcx, GoalKind<'tcx>>,
goal_list: InternedSet<'tcx, List<Goal<'tcx>>>, goal_list: InternedSet<'tcx, List<Goal<'tcx>>>,
projs: InternedSet<'tcx, List<ProjectionKind<'tcx>>>, projs: InternedSet<'tcx, List<ProjectionKind<'tcx>>>,
lazy_const: InternedSet<'tcx, LazyConst<'tcx>>,
} }
impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> { impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> {
@ -144,6 +145,7 @@ impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> {
goal: Default::default(), goal: Default::default(),
goal_list: Default::default(), goal_list: Default::default(),
projs: Default::default(), projs: Default::default(),
lazy_const: Default::default(),
} }
} }
@ -1096,10 +1098,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
self.global_arenas.adt_def.alloc(def) self.global_arenas.adt_def.alloc(def)
} }
pub fn intern_const_alloc( pub fn intern_const_alloc(self, alloc: Allocation) -> &'gcx Allocation {
self,
alloc: Allocation,
) -> &'gcx Allocation {
self.allocation_interner.borrow_mut().intern(alloc, |alloc| { self.allocation_interner.borrow_mut().intern(alloc, |alloc| {
self.global_arenas.const_allocs.alloc(alloc) self.global_arenas.const_allocs.alloc(alloc)
}) })
@ -1119,10 +1118,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}) })
} }
pub fn intern_lazy_const(self, c: ty::LazyConst<'tcx>) -> &'tcx ty::LazyConst<'tcx> {
self.global_interners.arena.alloc(c)
}
pub fn intern_layout(self, layout: LayoutDetails) -> &'gcx LayoutDetails { pub fn intern_layout(self, layout: LayoutDetails) -> &'gcx LayoutDetails {
self.layout_interner.borrow_mut().intern(layout, |layout| { self.layout_interner.borrow_mut().intern(layout, |layout| {
self.global_arenas.layout.alloc(layout) self.global_arenas.layout.alloc(layout)
@ -2271,6 +2266,12 @@ impl<'tcx: 'lcx, 'lcx> Borrow<GoalKind<'lcx>> for Interned<'tcx, GoalKind<'tcx>>
} }
} }
impl<'tcx: 'lcx, 'lcx> Borrow<LazyConst<'lcx>> for Interned<'tcx, LazyConst<'tcx>> {
fn borrow<'a>(&'a self) -> &'a LazyConst<'lcx> {
&self.0
}
}
impl<'tcx: 'lcx, 'lcx> Borrow<[ExistentialPredicate<'lcx>]> impl<'tcx: 'lcx, 'lcx> Borrow<[ExistentialPredicate<'lcx>]>
for Interned<'tcx, List<ExistentialPredicate<'tcx>>> { for Interned<'tcx, List<ExistentialPredicate<'tcx>>> {
fn borrow<'a>(&'a self) -> &'a [ExistentialPredicate<'lcx>] { fn borrow<'a>(&'a self) -> &'a [ExistentialPredicate<'lcx>] {
@ -2377,7 +2378,8 @@ pub fn keep_local<'tcx, T: ty::TypeFoldable<'tcx>>(x: &T) -> bool {
direct_interners!('tcx, direct_interners!('tcx,
region: mk_region(|r: &RegionKind| r.keep_in_local_tcx()) -> RegionKind, region: mk_region(|r: &RegionKind| r.keep_in_local_tcx()) -> RegionKind,
goal: mk_goal(|c: &GoalKind<'_>| keep_local(c)) -> GoalKind<'tcx> goal: mk_goal(|c: &GoalKind<'_>| keep_local(c)) -> GoalKind<'tcx>,
lazy_const: mk_lazy_const(|c: &LazyConst<'_>| keep_local(&c)) -> LazyConst<'tcx>
); );
macro_rules! slice_interners { macro_rules! slice_interners {
@ -2562,7 +2564,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
#[inline] #[inline]
pub fn mk_array(self, ty: Ty<'tcx>, n: u64) -> Ty<'tcx> { pub fn mk_array(self, ty: Ty<'tcx>, n: u64) -> Ty<'tcx> {
self.mk_ty(Array(ty, self.intern_lazy_const( self.mk_ty(Array(ty, self.mk_lazy_const(
ty::LazyConst::Evaluated(ty::Const::from_usize(self.global_tcx(), n)) ty::LazyConst::Evaluated(ty::Const::from_usize(self.global_tcx(), n))
))) )))
} }

View file

@ -1042,7 +1042,7 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::LazyConst<'tcx> {
ty::LazyConst::Unevaluated(*def_id, substs.fold_with(folder)) ty::LazyConst::Unevaluated(*def_id, substs.fold_with(folder))
} }
}; };
folder.tcx().intern_lazy_const(new) folder.tcx().mk_lazy_const(new)
} }
fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self { fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {

View file

@ -268,7 +268,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
span: expr_span, span: expr_span,
ty: this.hir.tcx().types.u32, ty: this.hir.tcx().types.u32,
user_ty: None, user_ty: None,
literal: this.hir.tcx().intern_lazy_const(ty::LazyConst::Evaluated( literal: this.hir.tcx().mk_lazy_const(ty::LazyConst::Evaluated(
ty::Const::from_bits( ty::Const::from_bits(
this.hir.tcx(), this.hir.tcx(),
0, 0,

View file

@ -302,7 +302,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
} }
let eq_def_id = self.hir.tcx().lang_items().eq_trait().unwrap(); let eq_def_id = self.hir.tcx().lang_items().eq_trait().unwrap();
let (mty, method) = self.hir.trait_method(eq_def_id, "eq", ty, &[ty.into()]); let (mty, method) = self.hir.trait_method(eq_def_id, "eq", ty, &[ty.into()]);
let method = self.hir.tcx().intern_lazy_const(ty::LazyConst::Evaluated(method)); let method = self.hir.tcx().mk_lazy_const(ty::LazyConst::Evaluated(method));
let re_erased = self.hir.tcx().types.re_erased; let re_erased = self.hir.tcx().types.re_erased;
// take the argument by reference // take the argument by reference

View file

@ -33,7 +33,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
span, span,
ty, ty,
user_ty: None, user_ty: None,
literal: self.hir.tcx().intern_lazy_const(ty::LazyConst::Evaluated(literal)), literal: self.hir.tcx().mk_lazy_const(ty::LazyConst::Evaluated(literal)),
}; };
Operand::Constant(constant) Operand::Constant(constant)
} }

View file

@ -342,7 +342,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
} }
hir::ExprKind::Lit(ref lit) => ExprKind::Literal { hir::ExprKind::Lit(ref lit) => ExprKind::Literal {
literal: cx.tcx.intern_lazy_const(ty::LazyConst::Evaluated( literal: cx.tcx.mk_lazy_const(ty::LazyConst::Evaluated(
cx.const_eval_literal(&lit.node, expr_ty, lit.span, false) cx.const_eval_literal(&lit.node, expr_ty, lit.span, false)
)), )),
user_ty: None, user_ty: None,
@ -442,7 +442,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
} else { } else {
if let hir::ExprKind::Lit(ref lit) = arg.node { if let hir::ExprKind::Lit(ref lit) = arg.node {
ExprKind::Literal { ExprKind::Literal {
literal: cx.tcx.intern_lazy_const(ty::LazyConst::Evaluated( literal: cx.tcx.mk_lazy_const(ty::LazyConst::Evaluated(
cx.const_eval_literal(&lit.node, expr_ty, lit.span, true) cx.const_eval_literal(&lit.node, expr_ty, lit.span, true)
)), )),
user_ty: None, user_ty: None,
@ -702,7 +702,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
ty: var_ty, ty: var_ty,
span: expr.span, span: expr.span,
kind: ExprKind::Literal { kind: ExprKind::Literal {
literal: cx.tcx.intern_lazy_const(literal), literal: cx.tcx.mk_lazy_const(literal),
user_ty: None user_ty: None
}, },
}.to_ref(); }.to_ref();
@ -856,7 +856,7 @@ fn method_callee<'a, 'gcx, 'tcx>(
ty, ty,
span, span,
kind: ExprKind::Literal { kind: ExprKind::Literal {
literal: cx.tcx().intern_lazy_const(ty::LazyConst::Evaluated( literal: cx.tcx().mk_lazy_const(ty::LazyConst::Evaluated(
ty::Const::zero_sized(ty) ty::Const::zero_sized(ty)
)), )),
user_ty, user_ty,
@ -918,7 +918,7 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
let user_ty = user_substs_applied_to_def(cx, expr.hir_id, &def); let user_ty = user_substs_applied_to_def(cx, expr.hir_id, &def);
debug!("convert_path_expr: user_ty={:?}", user_ty); debug!("convert_path_expr: user_ty={:?}", user_ty);
ExprKind::Literal { ExprKind::Literal {
literal: cx.tcx.intern_lazy_const(ty::LazyConst::Evaluated(ty::Const::zero_sized( literal: cx.tcx.mk_lazy_const(ty::LazyConst::Evaluated(ty::Const::zero_sized(
cx.tables().node_id_to_type(expr.hir_id), cx.tables().node_id_to_type(expr.hir_id),
))), ))),
user_ty, user_ty,
@ -930,7 +930,7 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
let user_ty = user_substs_applied_to_def(cx, expr.hir_id, &def); let user_ty = user_substs_applied_to_def(cx, expr.hir_id, &def);
debug!("convert_path_expr: (const) user_ty={:?}", user_ty); debug!("convert_path_expr: (const) user_ty={:?}", user_ty);
ExprKind::Literal { ExprKind::Literal {
literal: cx.tcx.intern_lazy_const(ty::LazyConst::Unevaluated(def_id, substs)), literal: cx.tcx.mk_lazy_const(ty::LazyConst::Unevaluated(def_id, substs)),
user_ty, user_ty,
} }
}, },

View file

@ -110,7 +110,7 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
} }
pub fn usize_literal(&mut self, value: u64) -> &'tcx ty::LazyConst<'tcx> { pub fn usize_literal(&mut self, value: u64) -> &'tcx ty::LazyConst<'tcx> {
self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_usize(self.tcx, value))) self.tcx.mk_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_usize(self.tcx, value)))
} }
pub fn bool_ty(&mut self) -> Ty<'tcx> { pub fn bool_ty(&mut self) -> Ty<'tcx> {
@ -122,11 +122,11 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
} }
pub fn true_literal(&mut self) -> &'tcx ty::LazyConst<'tcx> { pub fn true_literal(&mut self) -> &'tcx ty::LazyConst<'tcx> {
self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bool(self.tcx, true))) self.tcx.mk_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bool(self.tcx, true)))
} }
pub fn false_literal(&mut self) -> &'tcx ty::LazyConst<'tcx> { pub fn false_literal(&mut self) -> &'tcx ty::LazyConst<'tcx> {
self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bool(self.tcx, false))) self.tcx.mk_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bool(self.tcx, false)))
} }
pub fn const_eval_literal( pub fn const_eval_literal(

View file

@ -459,7 +459,7 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> {
span: self.span, span: self.span,
ty: func_ty, ty: func_ty,
user_ty: None, user_ty: None,
literal: tcx.intern_lazy_const(ty::LazyConst::Evaluated( literal: tcx.mk_lazy_const(ty::LazyConst::Evaluated(
ty::Const::zero_sized(func_ty), ty::Const::zero_sized(func_ty),
)), )),
}); });
@ -521,7 +521,7 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> {
span: self.span, span: self.span,
ty: self.tcx.types.usize, ty: self.tcx.types.usize,
user_ty: None, user_ty: None,
literal: self.tcx.intern_lazy_const(ty::LazyConst::Evaluated( literal: self.tcx.mk_lazy_const(ty::LazyConst::Evaluated(
ty::Const::from_usize(self.tcx, value), ty::Const::from_usize(self.tcx, value),
)), )),
} }
@ -759,7 +759,7 @@ fn build_call_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
span, span,
ty, ty,
user_ty: None, user_ty: None,
literal: tcx.intern_lazy_const(ty::LazyConst::Evaluated( literal: tcx.mk_lazy_const(ty::LazyConst::Evaluated(
ty::Const::zero_sized(ty) ty::Const::zero_sized(ty)
)), )),
}), }),

View file

@ -533,7 +533,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
span, span,
ty: self.tcx.types.bool, ty: self.tcx.types.bool,
user_ty: None, user_ty: None,
literal: self.tcx.intern_lazy_const(ty::LazyConst::Evaluated( literal: self.tcx.mk_lazy_const(ty::LazyConst::Evaluated(
ty::Const::from_bool(self.tcx, val), ty::Const::from_bool(self.tcx, val),
)), )),
}))) })))

View file

@ -198,7 +198,7 @@ impl<'a, 'tcx> TransformVisitor<'a, 'tcx> {
span: source_info.span, span: source_info.span,
ty: self.tcx.types.u32, ty: self.tcx.types.u32,
user_ty: None, user_ty: None,
literal: self.tcx.intern_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bits( literal: self.tcx.mk_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bits(
self.tcx, self.tcx,
state_disc.into(), state_disc.into(),
ty::ParamEnv::empty().and(self.tcx.types.u32) ty::ParamEnv::empty().and(self.tcx.types.u32)
@ -731,7 +731,7 @@ fn insert_panic_block<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
span: mir.span, span: mir.span,
ty: tcx.types.bool, ty: tcx.types.bool,
user_ty: None, user_ty: None,
literal: tcx.intern_lazy_const(ty::LazyConst::Evaluated( literal: tcx.mk_lazy_const(ty::LazyConst::Evaluated(
ty::Const::from_bool(tcx, false), ty::Const::from_bool(tcx, false),
)), )),
}), }),

View file

@ -963,7 +963,7 @@ impl<'l, 'b, 'tcx, D> DropCtxt<'l, 'b, 'tcx, D>
span: self.source_info.span, span: self.source_info.span,
ty: self.tcx().types.usize, ty: self.tcx().types.usize,
user_ty: None, user_ty: None,
literal: self.tcx().intern_lazy_const(ty::LazyConst::Evaluated( literal: self.tcx().mk_lazy_const(ty::LazyConst::Evaluated(
ty::Const::from_usize(self.tcx(), val.into()) ty::Const::from_usize(self.tcx(), val.into())
)), )),
}) })

View file

@ -1793,7 +1793,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
let length_def_id = tcx.hir().local_def_id(length.id); let length_def_id = tcx.hir().local_def_id(length.id);
let substs = Substs::identity_for_item(tcx, length_def_id); let substs = Substs::identity_for_item(tcx, length_def_id);
let length = ty::LazyConst::Unevaluated(length_def_id, substs); let length = ty::LazyConst::Unevaluated(length_def_id, substs);
let length = tcx.intern_lazy_const(length); let length = tcx.mk_lazy_const(length);
let array_ty = tcx.mk_ty(ty::Array(self.ast_ty_to_ty(&ty), length)); let array_ty = tcx.mk_ty(ty::Array(self.ast_ty_to_ty(&ty), length));
self.normalize_ty(ast_ty.span, array_ty) self.normalize_ty(ast_ty.span, array_ty)
} }

View file

@ -4597,7 +4597,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
if element_ty.references_error() { if element_ty.references_error() {
tcx.types.err tcx.types.err
} else if let Ok(count) = count { } else if let Ok(count) = count {
tcx.mk_ty(ty::Array(t, tcx.intern_lazy_const(ty::LazyConst::Evaluated(count)))) tcx.mk_ty(ty::Array(t, tcx.mk_lazy_const(ty::LazyConst::Evaluated(count))))
} else { } else {
tcx.types.err tcx.types.err
} }