Skip to content

Truly independent web browser

LibJS/JIT: Exit for rare operands of bitwise operations

Andreas Klingcfcd67389431
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/speculation.rs b/Libraries/LibJS/JIT/Rust/src/builder/speculation.rs
index 84e5d1db1bf..cb03dfd1e88 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/speculation.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/speculation.rs
@@ -907,6 +907,17 @@ impl GraphBuilder<'_> {
)
}
+ /// Makes the cold edges of a bitwise operation with `feedback` exit
+ /// instead of running its slow path, if the feedback saw nothing but
+ /// numbers and booleans: the slow path then only handles operands that
+ /// never came, and running it would make the code after the operation
+ /// forget what it knew (see `exit_instead_of_slow_path()`).
+ fn exit_for_rare_bitwise_operands(&mut self, paths: &mut SlowPaths, feedback: u8) {
+ if feedback & (arith_feedback::STRING | arith_feedback::BIG_INT | arith_feedback::OTHER) == 0 {
+ self.exit_instead_of_slow_path(paths);
+ }
+ }
+
/// Whether int32 operations that exit where their results are no int32
/// fit an instruction with `feedback`.
fn may_speculate_int32_results(&self, feedback: u8) -> bool {
@@ -968,6 +979,7 @@ impl GraphBuilder<'_> {
let (lhs, rhs) = (self.boolean_as_int32(lhs), self.boolean_as_int32(rhs));
let doubles = feedback & arith_feedback::DOUBLE != 0 || !self.may_be_int32(&[lhs, rhs]);
let mut paths = self.start_slow_paths(Some(Repr::Tagged))?;
+ self.exit_for_rare_bitwise_operands(&mut paths, feedback);
let lhs = self.int32_of_operand_on_path(&mut paths, lhs, doubles, booleans);
let rhs = self.int32_of_operand_on_path(&mut paths, rhs, doubles, booleans);
let result = if op == BinaryOp::UnsignedRightShift && !int32_results {
@@ -1094,6 +1106,9 @@ impl GraphBuilder<'_> {
return Ok(None);
}
let mut paths = self.start_slow_paths(Some(Repr::Tagged))?;
+ if matches!(op, UnaryOp::BitwiseNot | UnaryOp::ToInt32) {
+ self.exit_for_rare_bitwise_operands(&mut paths, feedback);
+ }
match op {
UnaryOp::Plus => {
// NB: ToNumber of a number is the number itself.
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
index f60d8ca9004..b7c70516b20 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
@@ -2459,11 +2459,20 @@ fn dump_bitwise_or(feedback: u8) -> String {
#[test]
fn bitwise_operands_that_were_booleans_are_read_as_int32s() {
- use crate::snapshot::arith_feedback::{BOOLEAN, INT32, OTHER};
+ use crate::snapshot::arith_feedback::{BOOLEAN, INT32, OTHER, STRING};
let text = dump_bitwise_or(INT32 | BOOLEAN);
assert_eq!(text.matches("BranchInt32OrBoolean").count(), 2, "{text}");
assert!(text.contains("Int32BitwiseOr"), "{text}");
- assert_eq!(text.matches("CallSlowPath BitwiseOr").count(), 1, "{text}");
+ // Operands of other kinds never came, so they exit rather than run the slow path.
+ assert!(
+ text.contains("Exit SlowPath") && !text.contains("CallSlowPath"),
+ "{text}"
+ );
+ // Other kinds that came, like undefined or strings, take the slow path.
+ for other in [OTHER, STRING] {
+ let text = dump_bitwise_or(INT32 | BOOLEAN | other);
+ assert_eq!(text.matches("CallSlowPath BitwiseOr").count(), 1, "{text}");
+ }
// Other operands, like undefined, are no booleans.
let text = dump_bitwise_or(INT32 | OTHER);
assert!(!text.contains("BranchInt32OrBoolean"), "{text}");