Skip to content

Commit

Permalink
Make intern_lazy_const actually intern its argument.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nnethercote authored and pietroalbini committed Feb 17, 2019
1 parent 08f1073 commit 94ca417
Show file tree
Hide file tree
Showing 17 changed files with 40 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2154,7 +2154,7 @@ impl<'tcx> Operand<'tcx> {
span,
ty,
user_ty: None,
literal: tcx.intern_lazy_const(
literal: tcx.mk_lazy_const(
ty::LazyConst::Evaluated(ty::Const::zero_sized(ty)),
),
})
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
let substs = tcx.lift_to_global(&substs).unwrap();
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 {
Expand All @@ -420,7 +420,7 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a,
promoted: None
};
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));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/traits/query/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
let substs = tcx.lift_to_global(&substs).unwrap();
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 {
Expand All @@ -215,7 +215,7 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for QueryNormalizer<'cx, 'gcx, 'tcx
promoted: None,
};
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));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ pub fn decode_lazy_const<'a, 'tcx, D>(decoder: &mut D)
where D: TyDecoder<'a, 'tcx>,
'tcx: 'a,
{
Ok(decoder.tcx().intern_lazy_const(Decodable::decode(decoder)?))
Ok(decoder.tcx().mk_lazy_const(Decodable::decode(decoder)?))
}

#[inline]
Expand Down
22 changes: 12 additions & 10 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub struct CtxtInterners<'tcx> {
goal: InternedSet<'tcx, GoalKind<'tcx>>,
goal_list: InternedSet<'tcx, List<Goal<'tcx>>>,
projs: InternedSet<'tcx, List<ProjectionKind<'tcx>>>,
lazy_const: InternedSet<'tcx, LazyConst<'tcx>>,
}

impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> {
Expand All @@ -144,6 +145,7 @@ impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> {
goal: Default::default(),
goal_list: Default::default(),
projs: Default::default(),
lazy_const: Default::default(),
}
}

Expand Down Expand Up @@ -1072,10 +1074,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
self.global_arenas.adt_def.alloc(def)
}

pub fn intern_const_alloc(
self,
alloc: Allocation,
) -> &'gcx Allocation {
pub fn intern_const_alloc(self, alloc: Allocation) -> &'gcx Allocation {
self.allocation_interner.borrow_mut().intern(alloc, |alloc| {
self.global_arenas.const_allocs.alloc(alloc)
})
Expand All @@ -1095,10 +1094,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 {
self.layout_interner.borrow_mut().intern(layout, |layout| {
self.global_arenas.layout.alloc(layout)
Expand Down Expand Up @@ -2238,6 +2233,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>]>
for Interned<'tcx, List<ExistentialPredicate<'tcx>>> {
fn borrow<'a>(&'a self) -> &'a [ExistentialPredicate<'lcx>] {
Expand Down Expand Up @@ -2344,7 +2345,8 @@ pub fn keep_local<'tcx, T: ty::TypeFoldable<'tcx>>(x: &T) -> bool {

direct_interners!('tcx,
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 {
Expand Down Expand Up @@ -2529,7 +2531,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {

#[inline]
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))
)))
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::LazyConst<'tcx> {
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 {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/build/expr/as_rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
span: expr_span,
ty: this.hir.tcx().types.u32,
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(
this.hir.tcx(),
0,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/build/matches/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 (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;
// take the argument by reference
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/build/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
span,
ty,
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)
}
Expand Down
12 changes: 6 additions & 6 deletions src/librustc_mir/hair/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
}

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)
)),
user_ty: None,
Expand Down Expand Up @@ -442,7 +442,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
} else {
if let hir::ExprKind::Lit(ref lit) = arg.node {
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)
)),
user_ty: None,
Expand Down Expand Up @@ -702,7 +702,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
ty: var_ty,
span: expr.span,
kind: ExprKind::Literal {
literal: cx.tcx.intern_lazy_const(literal),
literal: cx.tcx.mk_lazy_const(literal),
user_ty: None
},
}.to_ref();
Expand Down Expand Up @@ -856,7 +856,7 @@ fn method_callee<'a, 'gcx, 'tcx>(
ty,
span,
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)
)),
user_ty,
Expand Down Expand Up @@ -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);
debug!("convert_path_expr: user_ty={:?}", user_ty);
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),
))),
user_ty,
Expand All @@ -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);
debug!("convert_path_expr: (const) user_ty={:?}", user_ty);
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,
}
},
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_mir/hair/cx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, '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> {
Expand All @@ -121,11 +121,11 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, '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> {
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(
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_mir/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> {
span: self.span,
ty: func_ty,
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),
)),
});
Expand Down Expand Up @@ -521,7 +521,7 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> {
span: self.span,
ty: self.tcx.types.usize,
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),
)),
}
Expand Down Expand Up @@ -759,7 +759,7 @@ fn build_call_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
span,
ty,
user_ty: None,
literal: tcx.intern_lazy_const(ty::LazyConst::Evaluated(
literal: tcx.mk_lazy_const(ty::LazyConst::Evaluated(
ty::Const::zero_sized(ty)
)),
}),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/transform/elaborate_drops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
span,
ty: self.tcx.types.bool,
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),
)),
})))
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/transform/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl<'a, 'tcx> TransformVisitor<'a, 'tcx> {
span: source_info.span,
ty: self.tcx.types.u32,
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,
state_disc.into(),
ty::ParamEnv::empty().and(self.tcx.types.u32)
Expand Down Expand Up @@ -687,7 +687,7 @@ fn insert_panic_block<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
span: mir.span,
ty: tcx.types.bool,
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),
)),
}),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/util/elaborate_drops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ impl<'l, 'b, 'tcx, D> DropCtxt<'l, 'b, 'tcx, D>
span: self.source_info.span,
ty: self.tcx().types.usize,
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())
)),
})
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1779,7 +1779,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
let length_def_id = tcx.hir().local_def_id(length.id);
let substs = Substs::identity_for_item(tcx, length_def_id);
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));
self.normalize_ty(ast_ty.span, array_ty)
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4592,7 +4592,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
if element_ty.references_error() {
tcx.types.err
} 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 {
tcx.types.err
}
Expand Down

0 comments on commit 94ca417

Please sign in to comment.