Skip to content

Truly independent web browser

LibJS/JIT: Always inline callees that are small but for moves

Andreas Klingcfb7ad15b866
diff --git a/Libraries/LibJS/JIT/ARCHITECTURE.md b/Libraries/LibJS/JIT/ARCHITECTURE.md
index 696c672efef..adea0c9f02d 100644
--- a/Libraries/LibJS/JIT/ARCHITECTURE.md
+++ b/Libraries/LibJS/JIT/ARCHITECTURE.md
@@ -446,8 +446,9 @@ that a `GetById` found for the shapes it switched on, inlines each of them (or c
path for those it cannot inline). Where the phi's block is a join of nothing but the edges that
bring the constants, each inlined call starts on its edges, which keep what the code knew there
(the shape of the receiver); otherwise the code branches on the callee, and calls generically
-where it could inline none of them. Even callees small enough to always be inlined are
-charged to the budget as cases of such calls, which may each inline several and nest.
+where it could inline none of them. Callees that are small but for moves and jumps
+(`OpCode::is_free_when_inlined()`) are always inlined, whatever the budget, except as cases of
+such calls, which may each inline several and nest.
**Shapes the caches learned.** A speculation that failed is never made again at the same place,
with one exception: a `BadShape` exit at an instruction whose property lookup cache the code
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
index 05cce5fc7ea..a2a75986c98 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
@@ -1398,7 +1398,7 @@ impl GraphBuilder<'_> {
let instruction_count = decoded.instructions.len();
// NB: Builtins are always inlined (see the snapshot's inlining rules).
let within_budget = callee_executable.builtin
- || instruction_count <= limits.always_inlined_instructions as usize
+ || crate::bytecode::inlining_size(&decoded.instructions) <= limits.always_inlined_instructions as usize
|| (construct.is_some() && instruction_count <= limits.max_construct_instructions as usize)
|| (instruction_count <= limits.max_instructions as usize
&& self.inlined_instructions + instruction_count <= limits.budget_instructions as usize);
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
index cd7a7088111..da3b289d2f9 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
@@ -2176,7 +2176,7 @@ pub(crate) mod inlining {
// A callee too big to inline.
let big = assemble(|_| {
let mut instructions = vec![Instruction::Enter];
- instructions.extend((0..40).map(|_| Instruction::Mov { dst: r(5), src: a(0) }));
+ instructions.extend((0..40).map(|_| Instruction::Typeof { dst: r(5), src: a(0) }));
instructions.push(Instruction::Return { value: r(5) });
instructions
});
diff --git a/Libraries/LibJS/JIT/Rust/src/bytecode/instruction.rs b/Libraries/LibJS/JIT/Rust/src/bytecode/instruction.rs
index 1d4eed52e3e..f14a6eccd5c 100644
--- a/Libraries/LibJS/JIT/Rust/src/bytecode/instruction.rs
+++ b/Libraries/LibJS/JIT/Rust/src/bytecode/instruction.rs
@@ -175,6 +175,24 @@ pub struct DecodedInstruction {
pub instruction: Instruction,
}
+impl OpCode {
+ /// Whether an instruction of this opcode costs nothing in inlined code:
+ /// entering the frame, moves between slots (which SSA values rename),
+ /// and jumps.
+ pub fn is_free_when_inlined(self) -> bool {
+ matches!(self, OpCode::Enter | OpCode::Mov | OpCode::Jump)
+ }
+}
+
+/// How many of a callee's instructions cost something in inlined code,
+/// which decides whether it is small enough to always inline.
+pub fn inlining_size(instructions: &[DecodedInstruction]) -> usize {
+ instructions
+ .iter()
+ .filter(|decoded| !decoded.instruction.opcode().is_free_when_inlined())
+ .count()
+}
+
impl Instruction {
/// The property lookup cache whose entries compiled code speculates on
/// for this instruction, as shape checks that exit with