Truly independent web browser
LibJS: Count calls from JIT code into interpreted functions as 16
Andreas Kling0049878d24e8
diff --git a/Libraries/LibJS/JIT/ARCHITECTURE.md b/Libraries/LibJS/JIT/ARCHITECTURE.mdindex 28312a0936f..d3a6e43d865 100644--- a/Libraries/LibJS/JIT/ARCHITECTURE.md+++ b/Libraries/LibJS/JIT/ARCHITECTURE.md@@ -14,7 +14,10 @@ LibJS has two tiers: the bytecode interpreter, whose instructions keep per-sitefeedback, and this optimizing JIT. An executable that uses up its tier-up budget (calls and loopiterations) is snapshotted and compiled on one of the compile threads. The interpreter keeps runningit meanwhile; the main thread installs the code when the job finishes. Loops that run long enough-enter compiled code at their back edges (on-stack replacement).+enter compiled code at their back edges (on-stack replacement). A call from compiled code into a+function the interpreter runs counts as several invocations of it (`callee-boost`): code hot enough+to be compiled calls it, so it is likely to be hot as well, and there is no baseline tier to run it+in meanwhile.There is no baseline tier. The interpreter's caches are the baseline: the JIT translates what theysaw into speculation, and leaves to the interpreter wherever a speculation fails.@@ -415,6 +418,7 @@ LIBJS_JIT=threshold=100,help ./Build/release/bin/js script.js| `warmup=N` | 8 | How many invocations a function runs before the interpreter collects feedback for it. || `sync` | off | Compile on the main thread when a function gets hot, at the same points in every run, instead of on a compile thread. || `compile-threads=N` | 0 | How many threads compile hot functions; 0 starts one per processor core but one, up to 8. |+| `callee-boost=N` | 16 | How many invocations a call from compiled code into a function the interpreter runs counts as; 1 for one. || `inline-budget=N` | 250 | How many bytecode instructions the inlined callees of one compile may have together. || `inline-max-size=N` | 30 | How many bytecode instructions one inlined callee may have. || `inline-depth=N` | 5 | How deeply inlined calls may nest; 0 turns inlining off. |diff --git a/Libraries/LibJS/JIT/Rust/src/options.rs b/Libraries/LibJS/JIT/Rust/src/options.rsindex 4153099b721..3c7ad48019d 100644--- a/Libraries/LibJS/JIT/Rust/src/options.rs+++ b/Libraries/LibJS/JIT/Rust/src/options.rs@@ -33,6 +33,11 @@ pub struct Options {/// `compile_thread_count()`).pub compile_threads: u32,+ /// How many function entries a call from compiled code into a function the interpreter runs costs that function's+ /// tier-up budget: code hot enough to be compiled calls it, so it is likely to be hot as well. 1 counts such calls+ /// like any other.+ pub callee_boost: u32,+/// List the options and the values in effect on stderr.pub help: bool,@@ -101,6 +106,7 @@ impl Default for Options {warmup: 8,sync: false,compile_threads: 0,+ callee_boost: 16,help: false,dump_ir: false,dump_passes: false,@@ -193,6 +199,14 @@ const DEFINITIONS: &[Definition] = &[},description: "How many threads compile hot functions; 0 starts one per processor core but one.",},+ Definition {+ name: "callee-boost",+ setting: Setting::Number {+ field: |options| &mut options.callee_boost,+ bare: None,+ },+ description: "How many invocations a call from compiled code into an interpreted function counts as; 1 for one.",+ },Definition {name: "inline-budget",setting: Setting::Number {@@ -459,7 +473,7 @@ mod tests {#[test]fn every_option_parses() {let options = Options::parse(- "threshold=1,warmup=2,sync,compile-threads=9,inline-budget=3,inline-max-size=4,inline-depth=0,dump-ir,dump-passes,dump-asm,\+ "threshold=1,warmup=2,sync,compile-threads=9,callee-boost=5,inline-budget=3,inline-max-size=4,inline-depth=0,dump-ir,dump-passes,dump-asm,\dump-feedback,verify-ir,log-exits,perf-map,coverage=/tmp/coverage,seed=18446744073709551615,\stress-exits=6,stress-osr,stress-registers,random-thresholds,stress-install,stress-invalidate=7,help",)@@ -470,6 +484,7 @@ mod tests {warmup: 2,sync: true,compile_threads: 9,+ callee_boost: 5,help: true,dump_ir: true,dump_passes: true,@@ -552,7 +567,7 @@ mod tests {assert!(help.contains(&definition.syntax()), "{}", definition.name);}assert!(help.ends_with(- "In effect: LIBJS_JIT=on,threshold=50,warmup=8,compile-threads=0,inline-budget=250,inline-max-size=30,inline-depth=5,\+ "In effect: LIBJS_JIT=on,threshold=50,warmup=8,compile-threads=0,callee-boost=16,inline-budget=250,inline-max-size=30,inline-depth=5,\seed=1,stress-exits=0,stress-invalidate=0\n"));}diff --git a/Libraries/LibJS/Rust/src/jit/calls.rs b/Libraries/LibJS/Rust/src/jit/calls.rsindex 6c580d7328e..68f59fd885f 100644--- a/Libraries/LibJS/Rust/src/jit/calls.rs+++ b/Libraries/LibJS/Rust/src/jit/calls.rs@@ -109,6 +109,8 @@ fn run_callee_frame_from_jit(vm: &Vm, site: &JitCallSite, callee_frame: NonNull<EntryStatus::ExitInterpreter => return finish_call_from_jit(vm, site.frame, site.pc, site.return_pc),EntryStatus::Resume => {}}+ } else {+ super::tier_up::charge_call_from_compiled_code(vm, &executable);}vm.run_running_frame_in_interpreter();finish_call_from_jit(vm, site.frame, site.pc, site.return_pc)@@ -859,6 +861,10 @@ pub unsafe extern "C" fn libjs_jit_finish_direct_call(frame_of(callee_frame).runs_jit_code.set(false);}if status != EntryStatus::ExitInterpreter as u64 {+ let callee_frame = vm.running_execution_context().expect("the callee frame is running");+ if frame_of(callee_frame).program_counter.get() == 0 {+ super::tier_up::charge_call_from_compiled_code(vm, &super::executable_of(frame_of(callee_frame)));+ }vm.run_running_frame_in_interpreter();}finish_call_from_jit(vm, frame, pc, pc + call.length()).0diff --git a/Libraries/LibJS/Rust/src/jit/tier_up.rs b/Libraries/LibJS/Rust/src/jit/tier_up.rsindex 1a5ab9fc640..4cb00105653 100644--- a/Libraries/LibJS/Rust/src/jit/tier_up.rs+++ b/Libraries/LibJS/Rust/src/jit/tier_up.rs@@ -78,6 +78,26 @@ fn compile_wait_budget(options: &Options) -> i32 {/// or the next few loop iterations install the code if the job has finished, and run it.const QUEUED_BUDGET: i32 = tier_up_costs::FUNCTION_ENTRY;+/// Charges the tier-up budget of `executable`, which the interpreter is about to run from its entry for a call from+/// compiled code, for the extra invocations the "callee-boost" option counts such a call as. Code that is hot enough+/// to be compiled calls the function, so the function is likely to be hot as well.+pub fn charge_call_from_compiled_code(vm: &Vm, executable: &Executable) {+ let boost = vm.jit.options.callee_boost;+ if boost <= 1+ || !matches!(+ executable.jit_compile_state(),+ CompileState::None | CompileState::Discarding+ )+ {+ return;+ }+ let extra = i32::try_from(boost - 1)+ .unwrap_or(i32::MAX)+ .saturating_mul(tier_up_costs::FUNCTION_ENTRY);+ let budget = &executable.head.tier_up_budget;+ budget.set(budget.get().saturating_sub(extra));+}+/// The function to compile the running frame's executable for, if it should be compiled at all.fn function_to_compile(vm: &Vm) -> Option<Gc<EcmascriptFunctionObject>> {if vm.debugging_enabled() {