Skip to content

Commit 172522c

Browse files
committed
[OPT] Push fx.monomorphize to the callers of fx.layout_of
This prevents some repeated monomorphizations
1 parent d79965f commit 172522c

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

src/abi/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ pub fn codegen_fn_prelude(fx: &mut FunctionCx<'_, '_, impl Backend>, start_ebb:
353353
ArgKind::Normal(Some(val)) => {
354354
if let Some(addr) = val.try_to_addr() {
355355
let local_decl = &fx.mir.local_decls[local];
356-
// v this ! is important
356+
// v this ! is important
357357
let internally_mutable = !val.layout().ty.is_freeze(
358358
fx.tcx,
359359
ParamEnv::reveal_all(),
@@ -398,7 +398,7 @@ pub fn codegen_fn_prelude(fx: &mut FunctionCx<'_, '_, impl Backend>, start_ebb:
398398
}
399399

400400
for local in fx.mir.vars_and_temps_iter() {
401-
let ty = fx.mir.local_decls[local].ty;
401+
let ty = fx.monomorphize(&fx.mir.local_decls[local].ty);
402402
let layout = fx.layout_of(ty);
403403

404404
let is_ssa = *ssa_analyzed.get(&local).unwrap() == crate::analyze::SsaKind::Ssa;

src/base.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -340,29 +340,27 @@ fn trans_stmt<'tcx>(
340340
};
341341
lval.write_cvalue(fx, res);
342342
}
343-
Rvalue::Cast(CastKind::Pointer(PointerCast::ReifyFnPointer), operand, ty) => {
344-
let layout = fx.layout_of(ty);
345-
match fx
346-
.monomorphize(&operand.ty(&fx.mir.local_decls, fx.tcx))
347-
.kind
348-
{
343+
Rvalue::Cast(CastKind::Pointer(PointerCast::ReifyFnPointer), operand, to_ty) => {
344+
let from_ty = fx.monomorphize(&operand.ty(&fx.mir.local_decls, fx.tcx));
345+
let to_layout = fx.layout_of(fx.monomorphize(to_ty));
346+
match from_ty.kind {
349347
ty::FnDef(def_id, substs) => {
350348
let func_ref = fx.get_function_ref(
351349
Instance::resolve(fx.tcx, ParamEnv::reveal_all(), def_id, substs)
352350
.unwrap(),
353351
);
354352
let func_addr = fx.bcx.ins().func_addr(fx.pointer_type, func_ref);
355-
lval.write_cvalue(fx, CValue::by_val(func_addr, layout));
353+
lval.write_cvalue(fx, CValue::by_val(func_addr, to_layout));
356354
}
357-
_ => bug!("Trying to ReifyFnPointer on non FnDef {:?}", ty),
355+
_ => bug!("Trying to ReifyFnPointer on non FnDef {:?}", from_ty),
358356
}
359357
}
360-
Rvalue::Cast(CastKind::Pointer(PointerCast::UnsafeFnPointer), operand, ty)
361-
| Rvalue::Cast(CastKind::Pointer(PointerCast::MutToConstPointer), operand, ty)
362-
| Rvalue::Cast(CastKind::Pointer(PointerCast::ArrayToPointer), operand, ty) => {
358+
Rvalue::Cast(CastKind::Pointer(PointerCast::UnsafeFnPointer), operand, to_ty)
359+
| Rvalue::Cast(CastKind::Pointer(PointerCast::MutToConstPointer), operand, to_ty)
360+
| Rvalue::Cast(CastKind::Pointer(PointerCast::ArrayToPointer), operand, to_ty) => {
361+
let to_layout = fx.layout_of(fx.monomorphize(to_ty));
363362
let operand = trans_operand(fx, operand);
364-
let layout = fx.layout_of(ty);
365-
lval.write_cvalue(fx, operand.unchecked_cast_to(layout));
363+
lval.write_cvalue(fx, operand.unchecked_cast_to(to_layout));
366364
}
367365
Rvalue::Cast(CastKind::Misc, operand, to_ty) => {
368366
let operand = trans_operand(fx, operand);
@@ -420,7 +418,7 @@ fn trans_stmt<'tcx>(
420418
lval.write_cvalue(fx, CValue::by_val(res, dest_layout));
421419
}
422420
}
423-
Rvalue::Cast(CastKind::Pointer(PointerCast::ClosureFnPointer(_)), operand, _ty) => {
421+
Rvalue::Cast(CastKind::Pointer(PointerCast::ClosureFnPointer(_)), operand, _to_ty) => {
424422
let operand = trans_operand(fx, operand);
425423
match operand.layout().ty.kind {
426424
ty::Closure(def_id, substs) => {
@@ -437,7 +435,7 @@ fn trans_stmt<'tcx>(
437435
_ => bug!("{} cannot be cast to a fn ptr", operand.layout().ty),
438436
}
439437
}
440-
Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), operand, _ty) => {
438+
Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), operand, _to_ty) => {
441439
let operand = trans_operand(fx, operand);
442440
operand.unsize_value(fx, lval);
443441
}
@@ -466,6 +464,7 @@ fn trans_stmt<'tcx>(
466464
use rustc::middle::lang_items::ExchangeMallocFnLangItem;
467465

468466
let usize_type = fx.clif_type(fx.tcx.types.usize).unwrap();
467+
let content_ty = fx.monomorphize(content_ty);
469468
let layout = fx.layout_of(content_ty);
470469
let llsize = fx.bcx.ins().iconst(usize_type, layout.size.bytes() as i64);
471470
let llalign = fx
@@ -494,7 +493,7 @@ fn trans_stmt<'tcx>(
494493
.layout()
495494
.ty
496495
.is_sized(fx.tcx.at(DUMMY_SP), ParamEnv::reveal_all()));
497-
let ty_size = fx.layout_of(ty).size.bytes();
496+
let ty_size = fx.layout_of(fx.monomorphize(ty)).size.bytes();
498497
let val = CValue::const_val(fx, fx.tcx.types.usize, ty_size.into());
499498
lval.write_cvalue(fx, val);
500499
}

src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ impl<'tcx, B: Backend> LayoutOf for FunctionCx<'_, 'tcx, B> {
279279
type TyLayout = TyLayout<'tcx>;
280280

281281
fn layout_of(&self, ty: Ty<'tcx>) -> TyLayout<'tcx> {
282-
let ty = self.monomorphize(&ty);
282+
assert!(!ty.needs_subst());
283283
self.tcx
284284
.layout_of(ParamEnv::reveal_all().and(&ty))
285285
.unwrap_or_else(|e| {

0 commit comments

Comments
 (0)