Truly independent web browser
LibJS/JIT: Inline recursive calls two levels deep
Andreas Kling499bac6952ef
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rsindex 3d960d7bc57..32ec28758e1 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs@@ -1386,16 +1386,17 @@ impl GraphBuilder<'_> {}}- /// Whether calls of `callee_executable` may be inlined here at all: it is- /// no executable being inlined already, the inlined frames are not as- /// deep as they may be, and no inlined call exited here before.+ /// Whether calls of `callee_executable` may be inlined here at all: it runs in at most two of the frames being+ /// compiled already, the inlined frames are not as deep as they may be, and no inlined call exited here before.+ /// This lets recursive functions inline their own calls twice over, and lets functions that share one executable+ /// inline each other, like the constructors that `Class.create()` returns in Prototype-style code.fn may_inline_calls_of(&self, callee_executable: &crate::snapshot::ExecutableSnapshot) -> bool {- let recursive = self.function.executable.cell == callee_executable.cell- || self- .callers- .iter()- .any(|caller| caller.executable.cell == callee_executable.cell);- !recursive+ const MAX_RECURSION_DEPTH: usize = 2;+ let recursion_depth = core::iter::once(&self.function)+ .chain(&self.callers)+ .filter(|function| function.executable.cell == callee_executable.cell)+ .count();+ recursion_depth <= MAX_RECURSION_DEPTH&& self.callers.len() < self.snapshot.options.inlining.max_depth as usize&& self.may_speculate(ExitKind::BadCallTarget)}diff --git a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs b/Libraries/LibJS/JIT/Rust/src/builder/tests.rsindex f8fdd8ca846..ab070ab02f8 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs@@ -2153,6 +2153,33 @@ pub(crate) mod inlining {assert_eq!(dump.matches("LoadSlot r2").count(), 1, "{dump}");}+ #[test]+ fn distinct_closures_share_the_executable_recursion_limit() {+ let mut snapshot = snapshot_with_callee(&caller(), &caller(), function(true, false));+ snapshot.options.inlining.max_depth = 10;+ snapshot.executables[1].cell = snapshot.executables[0].cell;+ for index in 1..=3 {+ if index > 1 {+ snapshot.executables.push(snapshot.executables[1].clone());+ }+ let executable = &mut snapshot.executables[index];+ let function = executable.function.as_mut().unwrap();+ function.function = CellId(CALLEE_FUNCTION + (index as u64 - 1) * 0x100);+ executable.environment = Some(CellId(ENVIRONMENT + (index as u64 - 1) * 0x100));+ executable.feedback.call = vec![CallFeedbackSnapshot {+ target: Some(CellId(CALLEE_FUNCTION + (index as u64 % 3) * 0x100)),+ inline_executable: Some((index % 3 + 1) as u32),+ ..CallFeedbackSnapshot::default()+ }];+ }+ let text = dump(&optimized_graph(&snapshot), &test_layout());+ assert_eq!(text.matches("CallSlowPath Call").count(), 1, "{text}");+ assert!(text.contains("CallSlowPath Call @e2:"), "{text}");+ assert!(text.contains("CheckValue 0xfff9000000005000"), "{text}");+ assert!(text.contains("CheckValue 0xfff9000000005100"), "{text}");+ assert!(!text.contains("CheckValue 0xfff9000000005200"), "{text}");+ }+#[test]fn calls_that_cannot_be_inlined_stay_generic() {let identity = assemble(|_| vec![Instruction::Enter, Instruction::Return { value: a(0) }]);@@ -2171,10 +2198,35 @@ pub(crate) mod inlining {snapshot.executables[0].exit_sites = vec![(caller().offsets[1], ExitKind::BadCallTarget)];assert!(!is_inlined(&snapshot));- // Recursion.+ // Recursion, which inlines two levels deep.let mut snapshot = snapshot_with_callee(&caller(), &identity, function(true, false));snapshot.executables[1].cell = snapshot.executables[0].cell;- assert!(!is_inlined(&snapshot));+ assert!(is_inlined(&snapshot));+ let mut snapshot = snapshot_with_callee(&caller(), &caller(), function(true, false));+ snapshot.executables[1].cell = snapshot.executables[0].cell;+ snapshot.executables[1].feedback.call = snapshot.executables[0].feedback.call.clone();+ let recursive = dump(&optimized_graph(&snapshot), &test_layout());+ assert_eq!(recursive.matches("CallSlowPath Call").count(), 1, "{recursive}");+ // One check of the call target for each inlined level.+ assert_eq!(+ recursive.matches("CheckValue 0xfff9000000005000").count(),+ 2,+ "{recursive}"+ );++ // Mutual recursion of f and g, which inline each other until each runs in three frames.+ let mut snapshot = snapshot_with_callee(&caller(), &caller(), function(true, false));+ snapshot.options.inlining.max_depth = 10;+ snapshot.executables[1].feedback.call = snapshot.executables[0].feedback.call.clone();+ let mut g = snapshot.executables[1].clone();+ g.cell = CellId(0x300);+ snapshot.executables[1].feedback.call[0].inline_executable = Some(2);+ snapshot.executables.push(g);+ let mutual = dump(&optimized_graph(&snapshot), &test_layout());+ assert_eq!(mutual.matches("CallSlowPath Call").count(), 1, "{mutual}");+ assert!(mutual.contains("CallSlowPath Call @e2:"), "{mutual}");+ assert_eq!(mutual.matches("e1 pc 8 after").count(), 3, "{mutual}");+ assert_eq!(mutual.matches("e2 pc 8 after").count(), 3, "{mutual}");// A callee too big to inline.let big = assemble(|_| {