Skip to content

Truly independent web browser

LibJS/JIT: Build Math builtin calls without branches where they can

Andreas Kling3cfffbb7f630
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/checks.rs b/Libraries/LibJS/JIT/Rust/src/builder/checks.rs
index 212312f4e19..cd5d2715cba 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/checks.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/checks.rs
@@ -70,20 +70,40 @@ impl GraphBuilder<'_> {
}
Instruction::ResolveThisBinding => self.build_resolve_this_binding(),
Instruction::CallBuiltinMathAbs {
- dst, callee, argument, ..
- } => self.build_math_function(Float64UnaryOp::Abs, dst, callee, argument),
+ dst,
+ callee,
+ argument,
+ call_feedback,
+ ..
+ } => self.build_math_function(Float64UnaryOp::Abs, dst, callee, argument, call_feedback),
Instruction::CallBuiltinMathFloor {
- dst, callee, argument, ..
- } => self.build_math_function(Float64UnaryOp::Floor, dst, callee, argument),
+ dst,
+ callee,
+ argument,
+ call_feedback,
+ ..
+ } => self.build_math_function(Float64UnaryOp::Floor, dst, callee, argument, call_feedback),
Instruction::CallBuiltinMathCeil {
- dst, callee, argument, ..
- } => self.build_math_function(Float64UnaryOp::Ceil, dst, callee, argument),
+ dst,
+ callee,
+ argument,
+ call_feedback,
+ ..
+ } => self.build_math_function(Float64UnaryOp::Ceil, dst, callee, argument, call_feedback),
Instruction::CallBuiltinMathRound {
- dst, callee, argument, ..
- } => self.build_math_function(Float64UnaryOp::Round, dst, callee, argument),
+ dst,
+ callee,
+ argument,
+ call_feedback,
+ ..
+ } => self.build_math_function(Float64UnaryOp::Round, dst, callee, argument, call_feedback),
Instruction::CallBuiltinMathSqrt {
- dst, callee, argument, ..
- } => self.build_math_function(Float64UnaryOp::Sqrt, dst, callee, argument),
+ dst,
+ callee,
+ argument,
+ call_feedback,
+ ..
+ } => self.build_math_function(Float64UnaryOp::Sqrt, dst, callee, argument, call_feedback),
Instruction::GetIterator {
dst_iterator_object,
dst_iterator_next,
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/intrinsics.rs b/Libraries/LibJS/JIT/Rust/src/builder/intrinsics.rs
index 82f5c193da8..dbffec9f7a0 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/intrinsics.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/intrinsics.rs
@@ -47,13 +47,15 @@ impl GraphBuilder<'_> {
/// Builds `Math.<op>(argument)`, a `CallBuiltinMath*` instruction, for
/// numbers, if `callee` is the builtin of any realm. The instruction's
/// slow path, which calls the callee, handles everything else, or the code
- /// exits there where that did not happen before.
+ /// exits there where that did not happen before. A site that only saw
+ /// the builtin checks that `callee` is the function it saw instead.
pub(super) fn build_math_function(
&mut self,
op: Float64UnaryOp,
dst: Operand,
callee: Operand,
argument: Operand,
+ call_feedback: u16,
) -> Result<Flow, CompileFailure> {
let layout = &self.runtime.layout;
let builtin = match op {
@@ -64,11 +66,20 @@ impl GraphBuilder<'_> {
Float64UnaryOp::Sqrt => layout.builtin_math_sqrt,
Float64UnaryOp::Negate => unreachable!("no Math function negates"),
};
+ let target = self
+ .call_feedback(call_feedback)
+ .and_then(|feedback| feedback.monomorphic_builtin(builtin))
+ .filter(|_| self.may_speculate(ExitKind::BadCallTarget));
+ if let Some(target) = target {
+ self.check_callee(callee, target)?;
+ }
let callee = self.read(callee)?;
let argument = self.read(argument)?;
let mut slow_paths = self.start_slow_paths(Some(Repr::Tagged))?;
self.exit_instead_of_slow_path(&mut slow_paths);
- self.branch_to_slow_path(&mut slow_paths, BranchCondition::Builtin(builtin), vec![callee], true);
+ if target.is_none() {
+ self.branch_to_slow_path(&mut slow_paths, BranchCondition::Builtin(builtin), vec![callee], true);
+ }
let result = if let Some(integer) = self.int32_value_of(argument) {
self.build_int32_math_function(&mut slow_paths, op, integer)
} else {
@@ -175,6 +186,13 @@ impl GraphBuilder<'_> {
fn build_int32_math_function(&mut self, slow_paths: &mut SlowPaths, op: Float64UnaryOp, integer: NodeId) -> NodeId {
match op {
Float64UnaryOp::Floor | Float64UnaryOp::Ceil | Float64UnaryOp::Round => self.tagged(integer),
+ // NB: Negating the smallest int32 overflows, and exits.
+ Float64UnaryOp::Abs if self.may_speculate(ExitKind::Overflow) => {
+ let zero = self.int32_constant(0);
+ let negated = self.int32_binary(BinaryOp::Sub, zero, integer);
+ let result = self.emit(Op::Int32Max, vec![integer, negated], Some(Repr::Int32));
+ self.tagged(result)
+ }
Float64UnaryOp::Abs => {
let smallest = self.int32_constant(i32::MIN);
let integer = self.branch_to_slow_path(
diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
index f7b99c6d137..cf3de0fd2b1 100644
--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs
+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
@@ -338,6 +338,14 @@ impl CallFeedbackSnapshot {
Some((self.target?, self.target_intrinsic?))
}
+ /// The site's single callee, if it is the builtin function `builtin`.
+ pub fn monomorphic_builtin(&self, builtin: u8) -> Option<CellId> {
+ if self.flags & call_feedback_flags::POLYMORPHIC != 0 || self.target_builtin != Some(builtin) {
+ return None;
+ }
+ self.target
+ }
+
/// The site's single callee if it is `Function.prototype.call`, whose
/// calls JIT code makes by calling the `this` value directly.
pub fn function_prototype_call_target(&self) -> Option<CellId> {
@@ -399,6 +407,9 @@ pub struct CallFeedbackSnapshot {
pub forwarded: Option<ForwardedCallSnapshot>,
/// Which intrinsic `target` is, if it is one the compiler knows.
pub target_intrinsic: Option<Intrinsic>,
+ /// The `Builtin` id of `target` (see `RuntimeLayout::builtin_math_abs`), if
+ /// it is a builtin function.
+ pub target_builtin: Option<u8>,
/// `target`, if JIT code can inline its constructs.
pub construct: Option<ConstructTarget>,
}
diff --git a/Libraries/LibJS/Rust/src/jit/snapshot.rs b/Libraries/LibJS/Rust/src/jit/snapshot.rs
index 96870d29856..900029f03d7 100644
--- a/Libraries/LibJS/Rust/src/jit/snapshot.rs
+++ b/Libraries/LibJS/Rust/src/jit/snapshot.rs
@@ -798,6 +798,9 @@ impl SnapshotBuilder<'_> {
target: self.optional_cell(target),
flags,
target_intrinsic: intrinsic_of(self.vm, target),
+ target_builtin: target
+ .and_then(|target| target.downcast::<RawNativeFunction>()?.builtin())
+ .map(|builtin| builtin as u8),
..CallFeedbackSnapshot::default()
};
if monomorphic {
diff --git a/Tests/LibJS/JIT/ir/math-and-string-builtins.js b/Tests/LibJS/JIT/ir/math-and-string-builtins.js
index 61b362fad18..92d2734ba1a 100644
--- a/Tests/LibJS/JIT/ir/math-and-string-builtins.js
+++ b/Tests/LibJS/JIT/ir/math-and-string-builtins.js
@@ -1,7 +1,8 @@
// Calls of Math functions of one number and of charCodeAt and charAt are
// built as IR: the common case computes the result in IR, and branches on the
// callee and the operands go to a cold block. Math functions exit there,
-// unless that happened at the site before.
+// unless that happened at the site before, and sites that only saw the builtin
+// check the callee against it.
function floorPlusAbs(x) {
return Math.floor(x) + Math.abs(x);
@@ -12,21 +13,20 @@ jit.compile(floorPlusAbs);
// CHECK-FUNCTION: floorPlusAbs
// CHECK-PASS: graph building
-// CHECK: BranchBuiltin {{[0-9]+}} ([[FLOOR:v[0-9]+]]) -> [[CALLEE_OK:b[0-9]+]], [[COLD:b[0-9]+]]
-// CHECK: [[CALLEE_OK]] <- {{b[0-9]+}}:
-// CHECK-NEXT: RefineBuiltin {{[0-9]+}} ([[FLOOR]])
+// CHECK: CheckValue {{0x[0-9a-f]+}} ({{v[0-9]+}})
// CHECK-NEXT: BranchInt32Value ([[X:v[0-9]+]]) -> [[INT32:b[0-9]+]], [[OTHER:b[0-9]+]]
-// CHECK: [[COLD]] <- {{b[0-9]+}} cold:
-// CHECK-NEXT: Exit SlowPath
// CHECK: [[INT32]] <- {{b[0-9]+}}:
// CHECK-NEXT: [[INTEGER:v[0-9]+]] = RefineInt32Value ([[X]])
// CHECK-NEXT: UnboxInt32 ([[INTEGER]])
// CHECK: [[OTHER]] <- {{b[0-9]+}}:
-// CHECK-NEXT: BranchDouble ([[X]])
-// CHECK: [[REFINED:v[0-9]+]] = RefineDouble ([[X]])
+// CHECK-NEXT: BranchDouble ([[X]]) -> [[DOUBLE:b[0-9]+]], [[COLD:b[0-9]+]]
+// CHECK: [[DOUBLE]] <- {{b[0-9]+}}:
+// CHECK-NEXT: [[REFINED:v[0-9]+]] = RefineDouble ([[X]])
// CHECK-NEXT: [[NUMBER:v[0-9]+]]:float64 = UnboxDouble ([[REFINED]])
// CHECK-NEXT: [[FLOORED:v[0-9]+]]:float64 = Float64Floor ([[NUMBER]])
// CHECK-NEXT: BoxFloat64 ([[FLOORED]])
+// CHECK: [[COLD]] <- {{b[0-9]+}} cold:
+// CHECK-NEXT: Exit SlowPath
// CHECK: Float64Abs
// CHECK-NOT: CallSlowPath
@@ -46,8 +46,6 @@ jit.compile(floorOf);
// CHECK-PASS: graph building
// CHECK: BranchDouble ({{v[0-9]+}}) -> {{b[0-9]+}}, [[COLD:b[0-9]+]]
// CHECK: [[COLD]] <- {{b[0-9]+}} cold:
-// CHECK-NEXT: Jump [[SLOW:b[0-9]+]]
-// CHECK: [[SLOW]] <- {{b[0-9]+}}, {{b[0-9]+}} cold:
// CHECK-NEXT: CallSlowPath CallBuiltinMathFloor
// CHECK-NOT: Exit SlowPath
@@ -81,9 +79,9 @@ jit.compile(characters);
console.log(floorPlusAbs(-2.5), floorOf(-2.5), characters("abc", 1), characters("abc", 9));
-// Int32 arguments take int32 paths: the absolute value of an int32 is one
-// (but for the smallest, which exits), and its square root is that of its
-// double.
+// Int32 arguments take int32 paths: the absolute value of an int32 is the
+// larger of it and its negation (which overflows, and exits, for the
+// smallest), and its square root is that of its double.
function absAndSqrt(n) {
const i = n | 0;
@@ -95,9 +93,8 @@ jit.compile(absAndSqrt);
// CHECK-FUNCTION: absAndSqrt
// CHECK-PASS: graph building
-// CHECK: BranchInt32StrictlyEquals ([[I:v[0-9]+]], #i32:-2147483648)
-// CHECK: [[NOT_SMALLEST:v[0-9]+]]:int32 = RefineNotInt32StrictlyEquals ([[I]], #i32:-2147483648)
-// CHECK-NEXT: Int32Abs ([[NOT_SMALLEST]])
+// CHECK: [[NEGATED:v[0-9]+]]:int32 = Int32Sub (#i32:0, [[I:v[0-9]+]])
+// CHECK-NEXT: Int32Max ([[I]], [[NEGATED]])
// CHECK: [[DOUBLE:v[0-9]+]]:float64 = Int32ToFloat64 ([[I]])
// CHECK-NEXT: Float64Sqrt ([[DOUBLE]])
// CHECK: Float64Ceil