Skip to content

Truly independent web browser

LibJS: Write JIT frame states back without allocating

Andreas Klinge8e9f60e5d70
diff --git a/Libraries/LibJS/JIT/Rust/src/code.rs b/Libraries/LibJS/JIT/Rust/src/code.rs
index fda89b3a9b0..ed064b36851 100644
--- a/Libraries/LibJS/JIT/Rust/src/code.rs
+++ b/Libraries/LibJS/JIT/Rust/src/code.rs
@@ -235,12 +235,13 @@ pub struct FrameState {
pub executable: u32,
pub pc: u32,
pub mode: ResumeMode,
- /// The value of each frame slot that differs from what the frame already holds.
+ /// The value of each frame slot that differs from what the frame already
+ /// holds, in slot order.
pub values: Vec<(u32, ValueLocation)>,
/// The live slots whose value the frame already holds (see
- /// `ir::FrameState::in_frame`). Exits clear the registers and locals
- /// listed in neither, except a `ResumeAfter` destination, which exits
- /// never write either.
+ /// `ir::FrameState::in_frame`), in slot order. Exits clear the registers
+ /// and locals listed in neither, except a `ResumeAfter` destination,
+ /// which exits never write either.
pub in_frame: Vec<u32>,
/// See `ir::FrameState::passed_argument_count`.
pub passed_argument_count: Option<u32>,
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/sites.rs b/Libraries/LibJS/JIT/Rust/src/codegen/sites.rs
index 71617c5487a..a75494e5649 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/sites.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/sites.rs
@@ -70,17 +70,20 @@ pub(crate) fn site(
.into_iter()
.map(|frame_state| {
let frame_state = graph.frame_state(frame_state);
- let values = frame_state
+ let mut values = frame_state
.values
.iter()
.map(|(_, value)| location_of(*value))
- .collect();
+ .collect::<Vec<_>>();
+ values.sort_unstable_by_key(|(slot, _)| *slot);
+ let mut in_frame = frame_state.in_frame.clone();
+ in_frame.sort_unstable();
CodeFrameState {
executable: frame_state.executable,
pc: frame_state.pc,
mode: frame_state.mode,
values,
- in_frame: frame_state.in_frame.clone(),
+ in_frame,
passed_argument_count: frame_state.passed_argument_count,
}
})
diff --git a/Libraries/LibJS/Rust/src/jit/translate.rs b/Libraries/LibJS/Rust/src/jit/translate.rs
index 399ae7846ed..6d7e14edb95 100644
--- a/Libraries/LibJS/Rust/src/jit/translate.rs
+++ b/Libraries/LibJS/Rust/src/jit/translate.rs
@@ -363,14 +363,11 @@ fn write_frame_state(
) {
// NB: The frame holds the slots listed as in it, and the destination of the instruction it resumes after is never
// cleared.
- let mut held = frame_state.in_frame.clone();
- let mut kept = None;
- if let ResumeMode::ResumeAfter { dst } = frame_state.mode {
- held.push(dst);
- if destination == Destination::Keep {
- kept = Some(dst);
- }
- }
+ let resume_destination = match frame_state.mode {
+ ResumeMode::ResumeAfter { dst } => Some(dst),
+ ResumeMode::ResumeAt => None,
+ };
+ let kept = resume_destination.filter(|_| destination == Destination::Keep);
// NB: Compiled code initializes its frame lazily, before anything observes the frame (see
// passes/frame_initialization.rs), and pushes the frames of inlined calls for slow paths uninitialized. Until
// then nothing wrote the frame's registers and locals, so the ones the frame state lists as held in the frame
@@ -380,21 +377,15 @@ fn write_frame_state(
}
let slots = frame.slots();
// NB: Frame states list every live slot, so the others are dead. Clearing them keeps the interpreter from seeing
- // values the compiled code no longer maintains, and drops references to them.
- let registers_and_locals_count = executable_of(frame).registers_and_locals_count() as usize;
- let mut listed = vec![false; registers_and_locals_count];
- for slot in held.iter().chain(frame_state.values.iter().map(|(slot, _)| slot)) {
- if let Some(listed) = listed.get_mut(*slot as usize) {
- *listed = true;
- }
- }
- for (index, listed) in listed
- .iter()
- .enumerate()
- .skip(register::RESERVED_REGISTER_COUNT as usize)
- {
- if !listed {
- slots[index].set(Value::EMPTY);
+ // values the compiled code no longer maintains, and drops references to them. Both lists are in slot order.
+ let registers_and_locals_count = executable_of(frame).registers_and_locals_count();
+ let mut held = frame_state.in_frame.iter().copied().peekable();
+ let mut written = frame_state.values.iter().map(|(slot, _)| *slot).peekable();
+ for index in register::RESERVED_REGISTER_COUNT..registers_and_locals_count {
+ let is_held = sorted_slots_contain(&mut held, index);
+ let is_written = sorted_slots_contain(&mut written, index);
+ if !is_held && !is_written && resume_destination != Some(index) {
+ slots[index as usize].set(Value::EMPTY);
}
}
// NB: Every slot holding the arguments object the code never created holds the same object, which is created from
@@ -419,6 +410,12 @@ fn write_frame_state(
}
}
+/// Whether the slots, in slot order, contain `slot`, skipping those below it. Called with ascending slots.
+fn sorted_slots_contain(slots: &mut core::iter::Peekable<impl Iterator<Item = u32>>, slot: u32) -> bool {
+ while slots.next_if(|listed| *listed < slot).is_some() {}
+ slots.next_if_eq(&slot).is_some()
+}
+
/// Whether Full translation writes the compiled function's frame too.
#[derive(Clone, Copy, PartialEq, Eq)]
enum CompiledFunctionFrame {