Skip to content

Truly independent web browser

LibJS: Keep OSR polling from getting stuck at another loop

Andreas Kling7f3827a78606
diff --git a/Libraries/LibJS/Rust/src/jit/code.rs b/Libraries/LibJS/Rust/src/jit/code.rs
index eebb7df0f08..551ea8c968b 100644
--- a/Libraries/LibJS/Rust/src/jit/code.rs
+++ b/Libraries/LibJS/Rust/src/jit/code.rs
@@ -198,6 +198,10 @@ impl JitCode {
.map(|(_, entry)| *entry)
}
+ pub fn has_osr_entries(&self) -> bool {
+ !self.osr_entries.is_empty()
+ }
+
pub fn site(&self, index: u32) -> &Site {
&self.sites[index as usize]
}
diff --git a/Libraries/LibJS/Rust/src/jit/entry_exit.rs b/Libraries/LibJS/Rust/src/jit/entry_exit.rs
index ce66cda3ebc..4ef960a671c 100644
--- a/Libraries/LibJS/Rust/src/jit/entry_exit.rs
+++ b/Libraries/LibJS/Rust/src/jit/entry_exit.rs
@@ -16,6 +16,7 @@ use super::code::{CompileState, EntryStatus, JitEntry, JitResult};
use super::translate::{self, RegisterDump};
use super::{executable_of, frame_of, frames_above};
use crate::bytecode::executable::Executable;
+use crate::bytecode::feedback::tier_up_costs;
use crate::interpreter::vm::Vm;
use crate::layout::execution_context::ExecutionContext;
use crate::layout::value::Value;
@@ -81,10 +82,15 @@ pub fn enter_jit_code_at_loop(vm: &Vm, frame: NonNull<ExecutionContext>, pc: u32
if !can_enter_jit_code(vm, &executable) || !frame_ref.frame_initialized.get() {
return None;
}
- let entry = executable
- .jit_code()
- .expect("the executable has JIT code")
- .osr_entry(pc)?;
+ let code = executable.jit_code().expect("the executable has JIT code");
+ let Some(entry) = code.osr_entry(pc) else {
+ // NB: A fixed budget can keep expiring at the same back edge in nested loops, missing the code's OSR entry
+ // forever. Try the next back edge when the code has an entry at another loop.
+ if code.has_osr_entries() {
+ executable.head.tier_up_budget.set(tier_up_costs::LOOP_ITERATION);
+ }
+ return None;
+ };
vm.jit.count_coverage("osr-entry");
frame_ref.program_counter.set(pc);
Some(enter_from_native_code(vm, frame, entry))
diff --git a/Libraries/LibJS/Rust/src/jit/tier_up.rs b/Libraries/LibJS/Rust/src/jit/tier_up.rs
index 33d2efb08fa..8c7365e1583 100644
--- a/Libraries/LibJS/Rust/src/jit/tier_up.rs
+++ b/Libraries/LibJS/Rust/src/jit/tier_up.rs
@@ -158,6 +158,14 @@ pub fn on_budget_exhausted(vm: &Vm, pc: u32, is_loop: bool) {
return;
}
+ // NB: Frames with installed code may check successive back edges to find an OSR entry. Folding all their value
+ // feedback at each check is unnecessary: a new compile's snapshot folds it before using it.
+ if executable.jit_compile_state() == CompileState::Installed && !options.dump_feedback {
+ install_finished_compiles(vm);
+ executable.head.tier_up_budget.set(compile_wait_budget(options));
+ return;
+ }
+
// A warm executable starts collecting feedback, and the tier-up threshold counts from here, unless the JIT would
// never compile it.
if executable.interpreter_tier() == InterpreterTier::WarmingUp {
diff --git a/Tests/LibJS/JIT/ir/osr-nested-loop-polling.js b/Tests/LibJS/JIT/ir/osr-nested-loop-polling.js
new file mode 100644
index 00000000000..49b02940d8d
--- /dev/null
+++ b/Tests/LibJS/JIT/ir/osr-nested-loop-polling.js
@@ -0,0 +1,30 @@
+// OPTIONS: stress-osr,threshold=400
+// A cold loop shifts tier-up checks to an inner back edge that has no OSR entry. The interpreter must still find
+// the outer loop's entry, even when its regular polling budget is a multiple of the number of back edges.
+
+function nestedLoops(inner) {
+ if (inner) {
+ let i = 0;
+ do {
+ ++i;
+ } while (i < 1);
+ }
+ for (let i = 0; i < 2000; ++i) {
+ if (jit.inCompiledCode()) return true;
+ if (inner) {
+ let j = 0;
+ do {
+ ++j;
+ } while (j < 1);
+ }
+ }
+ return false;
+}
+jit.prepare(nestedLoops);
+nestedLoops(false);
+jit.compile(nestedLoops);
+if (!nestedLoops(true)) throw new Error("The outer loop's OSR entry was never entered");
+
+// CHECK-FUNCTION: nestedLoops
+// CHECK-PASS: graph building
+// CHECK: osr entry at pc