Skip to content

Allocate arguments from topmost frame into temporary storage before popping stack frame in init_fn_tail_call #144933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions compiler/rustc_const_eval/src/interpret/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::borrow::Cow;

use either::{Left, Right};
use rustc_abi::{self as abi, ExternAbi, FieldIdx, Integer, VariantIdx};
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::def_id::DefId;
use rustc_middle::ty::layout::{IntegerExt, TyAndLayout};
use rustc_middle::ty::{self, AdtDef, Instance, Ty, VariantDef};
Expand All @@ -19,7 +20,7 @@ use super::{
Projectable, Provenance, ReturnAction, ReturnContinuation, Scalar, StackPopInfo, interp_ok,
throw_ub, throw_ub_custom, throw_unsup_format,
};
use crate::interpret::EnteredTraceSpan;
use crate::interpret::{EnteredTraceSpan, MemoryKind};
use crate::{enter_trace_span, fluent_generated as fluent};

/// An argument passed to a function.
Expand Down Expand Up @@ -752,11 +753,41 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
&mut self,
fn_val: FnVal<'tcx, M::ExtraFnVal>,
(caller_abi, caller_fn_abi): (ExternAbi, &FnAbi<'tcx, Ty<'tcx>>),
args: &[FnArg<'tcx, M::Provenance>],
mut args: Vec<FnArg<'tcx, M::Provenance>>,
with_caller_location: bool,
) -> InterpResult<'tcx> {
trace!("init_fn_tail_call: {:#?}", fn_val);

let mut local_temps = vec![];
let frame_locals = &self.stack().last().unwrap().locals;
if frame_locals.iter().any(|frame_local| frame_local.is_allocation()) {
// Allocations corresponding to the locals in the last frame.
let local_allocs: FxHashSet<_> = frame_locals
.iter()
.filter_map(|local| local.as_mplace_or_imm()?.left()?.0.provenance?.get_alloc_id())
.collect();
Comment on lines +765 to +768
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you wrap this in std::cell::LazyCell, so that we don't create a hash map if none of the arguments have mplaces?


for arg in &mut args {
let mplace = match arg {
FnArg::Copy(op) => match op.as_mplace_or_imm() {
Left(mplace) => mplace,
Right(_) => continue,
},
FnArg::InPlace(mplace) => mplace.clone(),
};

if let Some(prov) = mplace.ptr().provenance
&& let Some(alloc_id) = prov.get_alloc_id()
&& local_allocs.contains(&alloc_id)
{
let temp_mplace = self.allocate(*arg.layout(), MemoryKind::Stack)?;
self.copy_op(&mplace, &temp_mplace)?;
local_temps.push(temp_mplace.clone());
*arg = FnArg::Copy(temp_mplace.into());
}
}
}

// This is the "canonical" implementation of tails calls,
// a pop of the current stack frame, followed by a normal call
// which pushes a new stack frame, with the return address from
Expand Down Expand Up @@ -785,12 +816,18 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
self.init_fn_call(
fn_val,
(caller_abi, caller_fn_abi),
args,
&args,
with_caller_location,
&return_place,
ret,
unwind,
)
)?;

for local_temp in local_temps {
self.deallocate_ptr(local_temp.ptr(), None, MemoryKind::Stack)?;
}

interp_ok(())
}

pub(super) fn init_drop_in_place_call(
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_const_eval/src/interpret/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ impl<'tcx, Prov: Provenance> LocalState<'tcx, Prov> {
LocalValue::Live(val) => interp_ok(val),
}
}

pub(super) fn is_allocation(&self) -> bool {
match self.value {
LocalValue::Dead => false,
LocalValue::Live(val) => match val {
Operand::Immediate(_) => false,
Operand::Indirect(_) => true,
},
}
Comment on lines +210 to +216
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
match self.value {
LocalValue::Dead => false,
LocalValue::Live(val) => match val {
Operand::Immediate(_) => false,
Operand::Indirect(_) => true,
},
}
match self.value {
LocalValue::Dead | LocalValue::Live(Operand::Immediate(_)) => false,
LocalValue::Live(Operand::Indirect(_)) => true,
}

}
}

/// What we store about a frame in an interpreter backtrace.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/interpret/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
let EvaluatedCalleeAndArgs { callee, args, fn_sig, fn_abi, with_caller_location } =
self.eval_callee_and_args(terminator, func, args)?;

self.init_fn_tail_call(callee, (fn_sig.abi, fn_abi), &args, with_caller_location)?;
self.init_fn_tail_call(callee, (fn_sig.abi, fn_abi), args, with_caller_location)?;

if self.frame_idx() != old_frame_idx {
span_bug!(
Expand Down
12 changes: 12 additions & 0 deletions src/tools/miri/tests/pass/tail_call_temp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![feature(explicit_tail_calls)]
#![expect(incomplete_features)]

struct Wrapper(#[expect(unused)] usize);

fn f(t: bool, x: Wrapper) {
if t { become f(false, x); }
}

fn main() {
f(true, Wrapper(5));
}
Loading