Truly independent web browser
LibJS/JIT: Leave out the start of top-level code compiled for a loop
Andreas Kling7b726e08e22b
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs b/Libraries/LibJS/JIT/Rust/src/builder/mod.rsindex a27cf72cbb4..de676e14fc1 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/mod.rs@@ -610,7 +610,21 @@ impl<'a> GraphBuilder<'a> {// NB: The entry block has no predecessors and jumps to the first bytecode// block, which may be a loop header.let entry = self.start_new_block(Vec::new());- self.enter_function(entry);+ // OPTIMIZATION: The code of top-level code that a loop triggered the compile of is only entered inside, so+ // what only its start reaches is left out, like a large literal that ran before the loop.+ let builds_entry_at_osr_pc = self.snapshot.options.osr_pc.is_some_and(|pc| {+ let index = self+ .function+ .instructions+ .iter()+ .position(|instruction| instruction.pc == pc);+ index.is_some_and(|index| self.can_build_osr_entry(index))+ });+ if self.snapshot.options.runs_once && self.build_osr_entries && builds_entry_at_osr_pc {+ self.set_control(Op::Unreachable, Vec::new());+ } else {+ self.enter_function(entry);+ }if self.build_osr_entries {self.build_osr_entries()?;self.build_resume_entries()?;diff --git a/Libraries/LibJS/JIT/Rust/src/builder/osr.rs b/Libraries/LibJS/JIT/Rust/src/builder/osr.rsindex 2b465fb02fd..70be92e08b9 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/osr.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/osr.rs@@ -75,6 +75,20 @@ pub(crate) fn is_loop_back_edge(opcode: OpCode) -> bool {}impl GraphBuilder<'_> {+ /// Whether the instruction with the given index is a back edge that an on-stack replacement entry can be built at.+ pub(super) fn can_build_osr_entry(&self, index: usize) -> bool {+ let pc = self.function.instructions[index].pc;+ // NB: Back edges in unreachable code lead to blocks that are+ // never built.+ is_loop_back_edge(self.function.instructions[index].instruction.opcode())+ && (self.has_run)(pc)+ && self+ .function+ .cfg+ .block_for_pc(pc)+ .is_some_and(|block| self.function.cfg.blocks[block].rpo_number.is_some())+ }+/// Builds an entry block for every reachable loop back edge of the/// compiled function that ran, before any of its bytecode blocks is/// started.@@ -82,22 +96,11 @@ impl GraphBuilder<'_> {debug_assert!(!self.function.is_virtual());for index in 0..self.function.instructions.len() {let pc = self.function.instructions[index].pc;- if !is_loop_back_edge(self.function.instructions[index].instruction.opcode())- || !(self.has_run)(pc)- || (self.snapshot.options.osr_pc != Some(pc) && !self.snapshot.options.stress.osr_at_every_loop)+ if (self.snapshot.options.osr_pc != Some(pc) && !self.snapshot.options.stress.osr_at_every_loop)+ || !self.can_build_osr_entry(index){continue;}- // NB: Back edges in unreachable code lead to blocks that are- // never built.- let reachable = self- .function- .cfg- .block_for_pc(pc)- .is_some_and(|block| self.function.cfg.blocks[block].rpo_number.is_some());- if !reachable {- continue;- }let block = self.start_new_block(Vec::new());self.frame = AbstractFrame::in_memory(self.function.layout.tracked_slot_count());self.function.instruction_index = index;diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rsindex b8f2827fe4b..aacd539e226 100644--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs@@ -1570,6 +1570,9 @@ pub struct CompileOptions {/// replacement entry: such an entry merges into the loop, which costs/// every iteration.pub osr_pc: Option<u32>,+ /// Whether the compiled executable is top-level code, which runs once: no frame of it enters the code at its+ /// start once a loop triggered the compile.+ pub runs_once: bool,pub inlining: InliningLimits,pub stress: StressOptions,/// Return what the code contains in `CompiledCode::coverage`.diff --git a/Libraries/LibJS/Rust/src/jit/snapshot.rs b/Libraries/LibJS/Rust/src/jit/snapshot.rsindex c0f74f3e6ed..3b245ae2ae4 100644--- a/Libraries/LibJS/Rust/src/jit/snapshot.rs+++ b/Libraries/LibJS/Rust/src/jit/snapshot.rs@@ -1358,6 +1358,7 @@ pub fn capture_snapshot<'d>(dump_asm: options.dump_asm,verify_ir: options.verify_ir,osr_pc,+ runs_once: function.is_none(),inlining: InliningLimits {always_inlined_instructions: ALWAYS_INLINED_INSTRUCTION_COUNT,max_instructions: options.inline_max_size,diff --git a/Tests/LibJS/JIT/ir/top-level-loop-leaves-out-start.js b/Tests/LibJS/JIT/ir/top-level-loop-leaves-out-start.jsnew file mode 100644index 00000000000..15e24f4e313--- /dev/null+++ b/Tests/LibJS/JIT/ir/top-level-loop-leaves-out-start.js@@ -0,0 +1,17 @@+// Top-level code runs once, so the code a loop in it gets compiled for is only entered at the loop: what only runs+// before it, like a large literal, is left out.++var table = [+ [1, 2],+ [3, 4],+ [5, 6],+];+var sum = 0;+for (var i = 0; i < 10000; ++i) sum += table[i % 3][0];+if (sum !== 29998) throw new Error("The loop computed the wrong sum");++// CHECK-FUNCTION: <anonymous>+// CHECK-PASS: graph building+// CHECK: Unreachable+// CHECK-NOT: AllocateArray+// CHECK: osr entry at pc