Skip to content
Closed
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
15 changes: 0 additions & 15 deletions .claude/settings.json

This file was deleted.

38 changes: 19 additions & 19 deletions crates/vm/src/builtins/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,8 @@ impl FrameObject {
} else {
self.iframe()
};
target.pending_stack_pops.store(pop_count as u32, Relaxed);
target.pending_unwind_from_stack.store(start_stack, Relaxed);
target.cold().pending_stack_pops.store(pop_count as u32, Relaxed);
target.cold().pending_unwind_from_stack.store(start_stack, Relaxed);
target.lasti.store(best_addr as u32, Relaxed);
Ok(())
}
Expand All @@ -647,9 +647,9 @@ impl FrameObject {
// Read from live source iframe if available.
let live = self.find_live_source_iframe();
let trace = if !live.is_null() {
unsafe { &*live }.trace.lock().clone()
unsafe { &*live }.cold().trace.lock().clone()
} else {
self.iframe().trace.lock().clone()
self.iframe().cold().trace.lock().clone()
};
trace.unwrap_or_else(|| vm.ctx.none())
}
Expand All @@ -667,13 +667,13 @@ impl FrameObject {
PySetterValue::Delete => None,
};
// Set on the materialized FrameObject.
(*self.iframe().trace.lock()).clone_from(&trace);
(*self.iframe().cold().trace.lock()).clone_from(&trace);
// Also propagate to the live source iframe if this is a
// materialized copy of a stack-allocated frame, so pdb's
// f_trace assignment takes effect on the executing frame.
let live = self.find_live_source_iframe();
if !live.is_null() {
*unsafe { &*live }.trace.lock() = trace;
*unsafe { &*live }.cold().trace.lock() = trace;
}
}

Expand All @@ -682,7 +682,7 @@ impl FrameObject {
fn f_trace_lines(vm: &VirtualMachine, zelf: PyObjectRef) -> PyResult {
let zelf: FrameObjectRef = zelf.downcast().unwrap_or_else(|_| unreachable!());

let boxed = zelf.iframe().trace_lines.lock();
let boxed = zelf.iframe().cold().trace_lines.lock();
Ok(vm.ctx.new_bool(*boxed).into())
}

Expand All @@ -701,11 +701,11 @@ impl FrameObject {
.map_err(|_| vm.new_type_error("attribute value type must be bool"))?;

let val = !value.as_bigint().is_zero();
*zelf.iframe().trace_lines.lock() = val;
*zelf.iframe().cold().trace_lines.lock() = val;
// Propagate to live source iframe.
let live = zelf.find_live_source_iframe();
if !live.is_null() {
*unsafe { &*live }.trace_lines.lock() = val;
*unsafe { &*live }.cold().trace_lines.lock() = val;
}

Ok(())
Expand All @@ -718,7 +718,7 @@ impl FrameObject {
#[pymember(type = "bool")]
fn f_trace_opcodes(vm: &VirtualMachine, zelf: PyObjectRef) -> PyResult {
let zelf: FrameObjectRef = zelf.downcast().unwrap_or_else(|_| unreachable!());
let trace_opcodes = zelf.iframe().trace_opcodes.lock();
let trace_opcodes = zelf.iframe().cold().trace_opcodes.lock();
Ok(vm.ctx.new_bool(*trace_opcodes).into())
}

Expand All @@ -737,11 +737,11 @@ impl FrameObject {
.map_err(|_| vm.new_type_error("attribute value type must be bool"))?;

let val = !value.as_bigint().is_zero();
*zelf.iframe().trace_opcodes.lock() = val;
*zelf.iframe().cold().trace_opcodes.lock() = val;
// Propagate to live source iframe.
let live = zelf.find_live_source_iframe();
if !live.is_null() {
*unsafe { &*live }.trace_opcodes.lock() = val;
*unsafe { &*live }.cold().trace_opcodes.lock() = val;
}

// TODO: Implement the equivalent of _PyEval_SetOpcodeTrace()
Expand Down Expand Up @@ -798,10 +798,10 @@ impl Py<FrameObject> {
self.clear_stack_and_cells();

// Clear temporary refs
self.iframe().temporary_refs.lock().clear();
self.iframe().f_locals_hidden_overlay.lock().take();
self.iframe().f_extra_locals.lock().take();
self.iframe().retained_back.lock().take();
self.iframe().cold().temporary_refs.lock().clear();
self.iframe().cold().f_locals_hidden_overlay.lock().take();
self.iframe().cold().f_extra_locals.lock().take();
self.iframe().cold().retained_back.lock().take();

Ok(())
}
Expand Down Expand Up @@ -853,7 +853,7 @@ impl Py<FrameObject> {
}
if prev.is_null() {
// Check retained_back for frames whose callers have returned
let retained = self.iframe().retained_back.lock().clone();
let retained = self.iframe().cold().retained_back.lock().clone();
if let Some(frame) = retained {
frame.mark_escaped();
return Some(frame);
Expand All @@ -879,7 +879,7 @@ impl Py<FrameObject> {
}

// The caller already returned — check retained_back
let retained = self.iframe().retained_back.lock().clone();
let retained = self.iframe().cold().retained_back.lock().clone();
if let Some(frame) = retained {
frame.mark_escaped();
return Some(frame);
Expand All @@ -906,7 +906,7 @@ impl Py<FrameObject> {
let iframe = unsafe { &*cur };
let fo = iframe.materialize(vm).to_owned();
if let Some(child) = child_fo.take() {
let mut guard = child.iframe().retained_back.lock();
let mut guard = child.iframe().cold().retained_back.lock();
if guard.is_none() {
*guard = Some(fo.clone());
}
Expand Down
41 changes: 16 additions & 25 deletions crates/vm/src/builtins/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ fn format_missing_args(
#[pyclass(module = false, name = "function", traverse = "manual")]
#[derive(Debug)]
pub struct PyFunction {
code: PyAtomicRef<PyCode>,
globals: PyDictRef,
builtins: PyObjectRef,
pub(crate) code: PyAtomicRef<PyCode>,
pub(crate) globals: PyDictRef,
pub(crate) builtins: PyObjectRef,
pub(crate) closure: Option<PyRef<PyTuple<PyCellRef>>>,
defaults_and_kwdefaults: PyMutex<(Option<PyTupleRef>, Option<PyDictRef>)>,
name: PyMutex<PyStrRef>,
Expand Down Expand Up @@ -617,11 +617,6 @@ impl Py<PyFunction> {

// Fast path: stack-allocated InterpreterFrame, no FrameObject.
// No refcount inc for code — it's alive via self.code for the call duration.
let nlocalsplus = code.localspluskinds.len();
let max_stackdepth = code.max_stackdepth as usize;
let localsplus =
crate::frame::LocalsPlus::new_on_datastack(nlocalsplus, max_stackdepth, vm);

let locals = if code.flags.contains(bytecode::CodeFlags::NEWLOCALS) {
crate::frame::FrameLocals::lazy()
} else if let Some(locals) = locals {
Expand All @@ -634,22 +629,21 @@ impl Py<PyFunction> {

// Use self.as_object() as raw pointer — no refcount inc/dec.
// The function is alive on the caller's stack for the call duration.
let mut iframe = crate::frame::InterpreterFrame::new(
let iframe = crate::frame::InterpreterFrame::new_on_datastack(
&self.code,
&self.globals,
&self.builtins,
Some(self.as_object()),
localsplus,
locals,
self.closure.as_ref().map_or(&[], |c| c.as_slice()),
crate::frame::FrameOwner::Thread,
vm,
);
let result = self
.fill_locals_from_args_iframe(&mut iframe, func_args, vm)
.and_then(|()| vm.run_frame_fast(&mut iframe));
.fill_locals_from_args_iframe(iframe, func_args, vm)
.and_then(|()| vm.run_frame_fast(iframe));
// Release data stack memory — must happen on both success and error.
unsafe {
if let Some(base) = iframe.localsplus.release_datastack() {
if let Some(base) = iframe.release_datastack_frame() {
vm.datastack_pop(base);
}
}
Expand Down Expand Up @@ -797,10 +791,6 @@ impl Py<PyFunction> {
vm: &VirtualMachine,
) -> PyResult {
let code = &*self.code;
let nlocalsplus = code.localspluskinds.len();
let max_stackdepth = code.max_stackdepth as usize;
let localsplus =
crate::frame::LocalsPlus::new_on_datastack(nlocalsplus, max_stackdepth, vm);

let locals = if code.flags.contains(bytecode::CodeFlags::NEWLOCALS) {
crate::frame::FrameLocals::lazy()
Expand All @@ -810,15 +800,14 @@ impl Py<PyFunction> {
))
};

let mut iframe = crate::frame::InterpreterFrame::new(
let iframe = crate::frame::InterpreterFrame::new_on_datastack(
code,
&self.globals,
&self.builtins,
Some(self.as_object()),
localsplus,
locals,
self.closure.as_ref().map_or(&[], |c| c.as_slice()),
crate::frame::FrameOwner::Thread,
vm,
);

// Fill arguments directly into fastlocals
Expand All @@ -829,9 +818,9 @@ impl Py<PyFunction> {
}
}

let result = vm.run_frame_fast(&mut iframe);
let result = vm.run_frame_fast(iframe);
unsafe {
if let Some(base) = iframe.localsplus.release_datastack() {
if let Some(base) = iframe.release_datastack_frame() {
vm.datastack_pop(base);
}
}
Expand Down Expand Up @@ -887,8 +876,10 @@ pub(crate) fn datastack_frame_size_bytes_for_code(code: &Py<PyCode>) -> Option<u
return None;
}
let nlocalsplus = code.localspluskinds.len();
let capacity = nlocalsplus.checked_add(code.max_stackdepth as usize)?;
capacity.checked_mul(core::mem::size_of::<usize>())
Some(crate::frame::datastack_iframe_total_bytes(
nlocalsplus,
code.max_stackdepth as usize,
))
}

impl PyPayload for PyFunction {
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1513,7 +1513,7 @@ impl PyType {
// temporary refs so they never see a dangling pointer.
let keep_alive = |type_ref: PyTypeRef, retired: &mut Vec<PyObjectRef>| {
if let Some(frame) = vm.current_frame() {
frame.iframe().temporary_refs.lock().push(type_ref.into());
frame.iframe().cold().temporary_refs.lock().push(type_ref.into());
} else {
retired.push(type_ref.into());
}
Expand Down
2 changes: 2 additions & 0 deletions crates/vm/src/coroutine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl ExecutionResult {
};
PyIterReturn::StopIteration(arg)
}
Self::TailCall => unreachable!("TailCall in generator/coroutine"),
}
}
}
Expand Down Expand Up @@ -104,6 +105,7 @@ impl Coro {
self.clear_frame_locals_on_close();
}
Ok(ExecutionResult::Yield(_)) => {}
Ok(ExecutionResult::TailCall) => unreachable!("TailCall in generator/coroutine"),
}
}

Expand Down
Loading
Loading