Truly independent web browser
LibJS: Keep the arguments of inlined functions live for `f.arguments`
Tim Ledbetterb1adfabdb36f
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rsindex 2565be6f827..c14c02b7622 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs@@ -940,7 +940,7 @@ impl GraphBuilder<'_> {// Keep non-strict closure calls conservative.let has_legacy_arguments = callee_executable.supports_legacy_arguments|| (closure.is_some() && callee_executable.is_non_strict_function());- let Ok(mut callee_function) = Function::new(index, callee_executable, decoded) else {+ let Ok(mut callee_function) = Function::new(index, callee_executable, decoded, has_legacy_arguments) else {return Ok(None);};if !can_inline(&callee_function) {diff --git a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs b/Libraries/LibJS/JIT/Rust/src/builder/mod.rsindex 35aabf8e9bb..4eed0f3117f 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/mod.rs@@ -326,7 +326,8 @@ fn decoded_function<'a>(struct FunctionAnalysis {cfg: Cfg,- liveness: Liveness,+ /// Liveness without and with arguments observable through `f.arguments`.+ liveness: [OnceCell<Liveness>; 2],loop_phi_slots: Vec<BitSet>,loops_replacing_environments: Vec<bool>,written_constants: BitSet,@@ -387,6 +388,7 @@ impl<'a> Function<'a> {index: u32,executable: &'a ExecutableSnapshot,decoded: &'a DecodedFunction,+ has_legacy_arguments: bool,) -> Result<Self, CompileFailure> {let layout = executable.layout;let instructions = decoded.instructions.as_slice();@@ -399,7 +401,6 @@ impl<'a> Function<'a> {reason: "invalid control flow",}})?;- let liveness = Liveness::compute(instructions, &cfg, &layout);let loop_phi_slots = loop_phi_slots(&cfg);let loops_replacing_environments = loops_replacing_environments(&cfg, instructions);let written_constants = written_constants(instructions, &layout);@@ -414,7 +415,7 @@ impl<'a> Function<'a> {}Ok(FunctionAnalysis {cfg,- liveness,+ liveness: [OnceCell::new(), OnceCell::new()],loop_phi_slots,loops_replacing_environments,written_constants,@@ -424,6 +425,8 @@ impl<'a> Function<'a> {}).as_ref().map_err(|error| *error)?;+ let liveness = analysis.liveness[usize::from(has_legacy_arguments)]+ .get_or_init(|| Liveness::compute(instructions, &analysis.cfg, &layout, has_legacy_arguments));let block_count = analysis.cfg.blocks.len();Ok(Self {index,@@ -431,7 +434,7 @@ impl<'a> Function<'a> {layout,instructions,cfg: &analysis.cfg,- liveness: &analysis.liveness,+ liveness,ir_blocks: vec![None; block_count],incoming: vec![Vec::new(); block_count],started: vec![false; block_count],@@ -506,7 +509,7 @@ impl<'a> GraphBuilder<'a> {pc: 0,reason: "no executable",})?;- let function = Function::new(0, executable, decoded_function(executable, &decoded[0])?)?;+ let function = Function::new(0, executable, decoded_function(executable, &decoded[0])?, false)?;let slot_count = function.layout.tracked_slot_count();Ok(Self {snapshot,diff --git a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs b/Libraries/LibJS/JIT/Rust/src/builder/tests.rsindex 014d8c3c56e..90fd4989b9b 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs@@ -65,8 +65,8 @@ fn executable_analyses_are_lazy_and_borrowed_by_independent_walks() {let decoded = decoded_function(executable, &cache).unwrap();assert!(decoded.analysis.get().is_none());assert!(std::ptr::eq(decoded, decoded_function(executable, &cache).unwrap()));- let mut first = Function::new(0, executable, decoded).unwrap();- let second = Function::new(0, executable, decoded).unwrap();+ let mut first = Function::new(0, executable, decoded, false).unwrap();+ let second = Function::new(0, executable, decoded, false).unwrap();assert!(std::ptr::eq(first.cfg, second.cfg));assert!(std::ptr::eq(first.liveness, second.liveness));assert_eq!(first.instructions.as_ptr(), second.instructions.as_ptr());@@ -74,6 +74,28 @@ fn executable_analyses_are_lazy_and_borrowed_by_independent_walks() {assert!(!second.started[0]);}+#[test]+fn cached_liveness_keeps_legacy_arguments_only_for_inlined_walks() {+ for first_mode in [false, true] {+ let program = assemble(|_| vec![Instruction::Return { value: c(0) }]);+ let layout = test_layout();+ let snapshot = snapshot_for(&program, layout);+ let executable = &snapshot.executables[0];+ let cache = OnceCell::new();+ let decoded = decoded_function(executable, &cache).unwrap();+ let first = Function::new(0, executable, decoded, first_mode).unwrap();+ let second = Function::new(0, executable, decoded, !first_mode).unwrap();+ assert!(std::ptr::eq(first.cfg, second.cfg));+ let argument_slot = layout+ .tracked_index(Operand::from_raw(layout.arguments_base()))+ .unwrap();+ assert_eq!(first.liveness.live_in(0).contains(argument_slot), first_mode);+ assert_eq!(second.liveness.live_in(0).contains(argument_slot), !first_mode);+ let repeated = Function::new(0, executable, decoded, first_mode).unwrap();+ assert!(std::ptr::eq(first.liveness, repeated.liveness));+ }+}+fn dump_of(program: &Program) -> String {dump(&build(program), &test_layout())}diff --git a/Libraries/LibJS/JIT/Rust/src/bytecode/liveness.rs b/Libraries/LibJS/JIT/Rust/src/bytecode/liveness.rsindex c64648408f1..767b6fefc2a 100644--- a/Libraries/LibJS/JIT/Rust/src/bytecode/liveness.rs+++ b/Libraries/LibJS/JIT/Rust/src/bytecode/liveness.rs@@ -9,7 +9,8 @@//! Sets are indexed by `FrameLayout::tracked_index()`. Constants are read-only//! and never live. The reserved registers (accumulator, exception, this value,//! return value and saved lexical environment) are always live, since some-//! instructions read or write them implicitly.+//! instructions read or write them implicitly. So are the arguments of a+//! function with a legacy `arguments`, which the code it calls can read.//!//! An instruction inside an exception handler range may throw before writing//! its outputs, so everything live into the handler is live into, and out of,@@ -29,10 +30,18 @@ pub struct Liveness {}impl Liveness {- pub fn compute(instructions: &[DecodedInstruction], cfg: &Cfg, layout: &FrameLayout) -> Self {+ pub fn compute(+ instructions: &[DecodedInstruction],+ cfg: &Cfg,+ layout: &FrameLayout,+ has_legacy_arguments: bool,+ ) -> Self {let slot_count = layout.tracked_slot_count();let mut always_live = BitSet::new(slot_count);always_live.insert_range(0..(RESERVED_REGISTER_COUNT.min(layout.registers_and_locals_count) as usize));+ if has_legacy_arguments {+ always_live.insert_range(layout.registers_and_locals_count as usize..slot_count);+ }// Upward exposed uses and definitions of each block.let mut block_uses = Vec::with_capacity(cfg.blocks.len());@@ -157,7 +166,7 @@ mod tests {let instructions = program.instructions();let cfg = program.cfg();Self {- liveness: Liveness::compute(&instructions, &cfg, &layout),+ liveness: Liveness::compute(&instructions, &cfg, &layout, false),layout,}}diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests.rsindex c302d99062c..aa796009ffc 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs@@ -1685,7 +1685,7 @@ mod execution {// Every slot the interpreter can still read must be right.let instructions = crate::bytecode::decode_all(&program.bytes).unwrap();let cfg = crate::bytecode::cfg::Cfg::new(&instructions, &[], &layout).unwrap();- let liveness = crate::bytecode::liveness::Liveness::compute(&instructions, &cfg, &layout);+ let liveness = crate::bytecode::liveness::Liveness::compute(&instructions, &cfg, &layout, false);let index = instructions.iter().position(|instruction| instruction.pc == pc)@@ -1842,7 +1842,8 @@ mod execution {assert_eq!(resumed_pc, *pc, "seed {seed}: resumed exit");let instructions = crate::bytecode::decode_all(&program.bytes).unwrap();let cfg = crate::bytecode::cfg::Cfg::new(&instructions, &[], &layout).unwrap();- let liveness = crate::bytecode::liveness::Liveness::compute(&instructions, &cfg, &layout);+ let liveness =+ crate::bytecode::liveness::Liveness::compute(&instructions, &cfg, &layout, false);let index = instructions.iter().position(|instruction| instruction.pc == *pc)@@ -1870,7 +1871,7 @@ mod execution {assert_eq!(machine.program_counter(), pc, "seed {seed}");let instructions = crate::bytecode::decode_all(&program.bytes).unwrap();let cfg = crate::bytecode::cfg::Cfg::new(&instructions, &[], &layout).unwrap();- let liveness = crate::bytecode::liveness::Liveness::compute(&instructions, &cfg, &layout);+ let liveness = crate::bytecode::liveness::Liveness::compute(&instructions, &cfg, &layout, false);let index = instructions.iter().position(|instruction| instruction.pc == pc)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.jsindex 2f55fb72534..ca7723c2954 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@@ -109,3 +109,25 @@ test("a function called by an inlined function changes a property of an object i);expect(failures).toEqual([]);});++test("a function called by an inlined function reads its legacy arguments after a branch", () => {+ const failures = compareCompiledWithInterpreter(+ () => {+ function readArguments(read) {+ // A for-in loop keeps this function from being inlined.+ for (const key in {}) {+ }+ if (read) return [...middle.arguments];+ }+ function middle(value, flag, read) {+ if (flag) value = 1;+ else value = 2;+ return readArguments(read);+ }+ jit.neverCompile(middle);+ return (flag, read) => middle(0, flag, read);+ },+ { warmUp: (log, i) => [i % 2 === 0, false], probes: { read: () => [true, true] } }+ );+ expect(failures).toEqual([]);+});