Skip to content

Truly independent web browser

LibJS: Show parameters that JIT code reassigned in `f.arguments`

Tim Ledbetter7e01ddbe41d4
diff --git a/Libraries/LibJS/JIT/ARCHITECTURE.md b/Libraries/LibJS/JIT/ARCHITECTURE.md
index d3a6e43d865..974ce32834a 100644
--- a/Libraries/LibJS/JIT/ARCHITECTURE.md
+++ b/Libraries/LibJS/JIT/ARCHITECTURE.md
@@ -50,8 +50,10 @@ memory holds it or which value holds it (and whether memory holds that too). Mer
headers never write frame memory: a slot that every path leaves in memory stays there, a slot the
paths disagree on (or a loop assigns) gets a phi, with loads at the predecessors that only have it
in memory. Frame memory is written only where something reads it: before calls that read the
-frame (`StoreSlot`), and by exits and leaves through the translator. The this value register is
-the exception at loop headers: slow paths write it to memory, so loops keep it there.
+frame (`StoreSlot`), where the compiled function assigns an argument that its legacy `arguments`
+shows (which any code it calls can read), and by exits and leaves through the translator. The
+this value register is the exception at loop headers: slow paths write it to memory, so loops
+keep it there.
### 3.1 Effects
@@ -119,7 +121,7 @@ Exits: E = eager, L = lazy. Every node that can exit has a frame state. "Call" m
| Constant | A NaN-boxed constant. | | | | cse |
| Phi | One input per predecessor, in predecessor order. | | | | |
| LoadSlot | Reads a frame slot. | | Frame | | |
-| StoreSlot | Writes input 0 to a frame slot: a reserved register (the this value, the saved lexical environment), a written constant, or a slot the next call reading the frame reads. The verifier enforces this. | | | Frame | |
+| StoreSlot | Writes input 0 to a frame slot: a reserved register (the this value, the saved lexical environment), a written constant, an argument the legacy `arguments` shows, or a slot the next call reading the frame reads. The verifier enforces this. | | | Frame | |
| BoxInt32, BoxBool | The tagged value of an unboxed input. | | | | cse |
| CheckInt32 | Input 0 as an int32; exits unless it is one. | E | | | cse |
| CheckNumber | Input 0 as a float64; exits unless it is a number. | E | | | cse |
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs b/Libraries/LibJS/JIT/Rust/src/builder/mod.rs
index 4eed0f3117f..8e8284f9bd4 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/mod.rs
@@ -511,12 +511,21 @@ impl<'a> GraphBuilder<'a> {
})?;
let function = Function::new(0, executable, decoded_function(executable, &decoded[0])?, false)?;
let slot_count = function.layout.tracked_slot_count();
+ let arguments_base = function.layout.arguments_base();
+ let legacy_arguments = if executable.supports_legacy_arguments {
+ arguments_base..arguments_base + function.layout.number_of_arguments
+ } else {
+ 0..0
+ };
Ok(Self {
snapshot,
runtime: &snapshot.runtime,
has_run,
decoded,
- graph: Graph::default(),
+ graph: Graph {
+ legacy_arguments,
+ ..Graph::default()
+ },
order: Vec::new(),
constants: HashMap::default(),
block: BlockId(0),
@@ -1179,16 +1188,19 @@ impl<'a> GraphBuilder<'a> {
};
// NB: Inlined frames have no memory to be out of sync with.
let is_virtual = self.function.is_virtual();
- let state = &mut self.frame.slots[slot];
- if *state
+ let writes_memory = !is_virtual && self.graph.legacy_arguments.contains(&operand.raw());
+ if self.frame.slots[slot]
!= (SlotState::Value {
node: value,
in_sync: true,
})
{
- *state = SlotState::Value {
+ if writes_memory {
+ self.emit(Op::StoreSlot { slot: operand.raw() }, vec![value], None);
+ }
+ self.frame.slots[slot] = SlotState::Value {
node: value,
- in_sync: is_virtual,
+ in_sync: is_virtual || writes_memory,
};
}
Ok(())
diff --git a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs b/Libraries/LibJS/JIT/Rust/src/ir/mod.rs
index 6d4528f3e11..c2df9d0ff90 100644
--- a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/ir/mod.rs
@@ -1910,6 +1910,9 @@ pub struct Graph {
/// Where exits of each executable were taken before, as (executable,
/// pc, kind), for passes that must not repeat a failed speculation.
pub exit_sites: Vec<(u32, u32, ExitKind)>,
+ /// The argument slots of the compiled function that its legacy
+ /// `arguments` shows. Empty for functions without legacy `arguments`.
+ pub legacy_arguments: core::ops::Range<u32>,
}
impl Graph {
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/verify.rs b/Libraries/LibJS/JIT/Rust/src/passes/verify.rs
index 247e5df360a..802e9ac631e 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/verify.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/verify.rs
@@ -28,10 +28,10 @@
//! that have a representation (or virtual objects);
//! - slow path calls have a frame state, and those that save registers
//! are in cold blocks;
-//! - `StoreSlot` writes only the reserved registers, written constants, and
-//! slots that the next call reading the frame (a `Generic` node, or a call
-//! that reads its operands from the frame) reads (see
-//! `verify_frame_writes()`).
+//! - `StoreSlot` writes only the reserved registers, written constants, the
+//! arguments the compiled function's legacy `arguments` shows, and slots
+//! that the next call reading the frame (a `Generic` node, or a call that
+//! reads its operands from the frame) reads (see `verify_frame_writes()`).
use super::dominators::Dominators;
use crate::bytecode::FrameLayout;
@@ -55,9 +55,10 @@ struct Place {
/// Checks that `StoreSlot` writes frame memory only where nothing else can:
/// the reserved registers (the this value and the saved lexical
-/// environment, which slow paths write there too), written constants, and the
-/// slots a node that reads the frame reads, right before it. Merges and back
-/// edges never write frame memory: slots that differ get phis.
+/// environment, which slow paths write there too), written constants, the
+/// arguments the compiled function's legacy `arguments` shows, and the slots
+/// a node that reads the frame reads, right before it. Merges and back edges
+/// never write frame memory: slots that differ get phis.
pub fn verify_frame_writes(graph: &Graph, layout: &FrameLayout) -> Result<(), String> {
let mut errors = Vec::new();
for (index, block) in graph.blocks.iter().enumerate() {
@@ -73,7 +74,7 @@ pub fn verify_frame_writes(graph: &Graph, layout: &FrameLayout) -> Result<(), St
};
let operand = Operand::from_raw(slot).raw();
let is_constant = operand >= layout.registers_and_locals_count && operand < layout.arguments_base();
- if operand < RESERVED_REGISTER_COUNT || is_constant {
+ if operand < RESERVED_REGISTER_COUNT || is_constant || graph.legacy_arguments.contains(&operand) {
continue;
}
// NB: Conversions of the reader's inputs may come in between, and
diff --git a/Tests/LibJS/Runtime/jit/regress/legacy-arguments-of-inlined-functions.js b/Tests/LibJS/Runtime/jit/regress/legacy-arguments-of-inlined-functions.js
index ca7723c2954..76b401f19a0 100644
--- a/Tests/LibJS/Runtime/jit/regress/legacy-arguments-of-inlined-functions.js
+++ b/Tests/LibJS/Runtime/jit/regress/legacy-arguments-of-inlined-functions.js
@@ -131,3 +131,29 @@ test("a function called by an inlined function reads its legacy arguments after
);
expect(failures).toEqual([]);
});
+
+test("functions called by the compiled function read a parameter it reassigned from its legacy arguments", () => {
+ const failures = compareCompiledWithInterpreter(
+ () => {
+ function readArgument(read) {
+ // A for-in loop keeps this function from being inlined.
+ for (const key in {}) {
+ }
+ if (read) return outer.arguments[0].x;
+ }
+ function middle(read) {
+ return readArgument(read);
+ }
+ function outer(object, count, read) {
+ if (count > 1) object = { x: 1 };
+ else object = { x: 2 };
+ const afterBranch = readArgument(read);
+ for (let i = 0; i < count; ++i) object = { x: i + 3 };
+ return [afterBranch, middle(read)];
+ }
+ return outer;
+ },
+ { warmUp: (log, i) => [null, i % 3, false], probes: { read: () => [null, 2, true] } }
+ );
+ expect(failures).toEqual([]);
+});