Skip to content

Commit 8ad4047

Browse files
committed
optimize creating a stack frame
1 parent c3d392f commit 8ad4047

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/librustc/mir/interpret/value.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ impl<'tcx> Scalar {
178178
}
179179

180180
impl From<Pointer> for Scalar {
181+
#[inline(always)]
181182
fn from(ptr: Pointer) -> Self {
182183
Scalar::Ptr(ptr)
183184
}
@@ -210,6 +211,7 @@ pub enum ScalarMaybeUndef {
210211
}
211212

212213
impl From<Scalar> for ScalarMaybeUndef {
214+
#[inline(always)]
213215
fn from(s: Scalar) -> Self {
214216
ScalarMaybeUndef::Scalar(s)
215217
}

src/librustc_mir/interpret/eval_context.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> LayoutOf for &'a EvalContext<'a, 'm
288288
type Ty = Ty<'tcx>;
289289
type TyLayout = EvalResult<'tcx, TyLayout<'tcx>>;
290290

291+
#[inline]
291292
fn layout_of(self, ty: Ty<'tcx>) -> Self::TyLayout {
292293
self.tcx.layout_of(self.param_env.and(ty))
293294
.map_err(|layout| EvalErrorKind::Layout(layout).into())
@@ -559,7 +560,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
559560
// value we use for things that we know are initially dead.
560561
let dummy =
561562
LocalValue::Live(Operand::Immediate(Value::Scalar(ScalarMaybeUndef::Undef)));
562-
self.frame_mut().locals = IndexVec::from_elem(dummy, &mir.local_decls);
563+
let mut locals = IndexVec::from_elem(dummy, &mir.local_decls);
563564
// Now mark those locals as dead that we do not want to initialize
564565
match self.tcx.describe_def(instance.def_id()) {
565566
// statics and constants don't have `Storage*` statements, no need to look for them
@@ -572,8 +573,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
572573
match stmt.kind {
573574
StorageLive(local) |
574575
StorageDead(local) => {
575-
// Worst case we are overwriting a dummy, no deallocation needed
576-
self.storage_dead(local);
576+
locals[local] = LocalValue::Dead;
577577
}
578578
_ => {}
579579
}
@@ -582,11 +582,20 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
582582
},
583583
}
584584
// Finally, properly initialize all those that still have the dummy value
585-
for local in mir.local_decls.indices() {
586-
if self.frame().locals[local] == dummy {
587-
self.storage_live(local)?;
585+
for (local, decl) in locals.iter_mut().zip(mir.local_decls.iter()) {
586+
match *local {
587+
LocalValue::Live(_) => {
588+
// This needs to be peoperly initialized.
589+
let layout = self.layout_of(self.monomorphize(decl.ty, instance.substs))?;
590+
*local = LocalValue::Live(self.uninit_operand(layout)?);
591+
}
592+
LocalValue::Dead => {
593+
// Nothing to do
594+
}
588595
}
589596
}
597+
// done
598+
self.frame_mut().locals = locals;
590599
}
591600

592601
if self.stack.len() > self.stack_limit {

0 commit comments

Comments
 (0)