Truly independent web browser
LibJS/JIT: Check doubles to be int32 values without boxing them
Andreas Kling10b946926222
diff --git a/Libraries/LibJS/JIT/ARCHITECTURE.md b/Libraries/LibJS/JIT/ARCHITECTURE.mdindex 656c0ebe51f..3fad946b08c 100644--- a/Libraries/LibJS/JIT/ARCHITECTURE.md+++ b/Libraries/LibJS/JIT/ARCHITECTURE.md@@ -128,6 +128,7 @@ Exits: E = eager, L = lazy. Every node that can exit has a frame state. "Call" m| StoreSlot | Writes input 0 to a frame slot: a reserved register (the this value, the saved lexical environment), a written constant, an argument the legacy `arguments` shows, or a slot the next call reading the frame or `Suspend` reads. The verifier enforces this. | | | Frame | || BoxInt32, BoxBool | The tagged value of an unboxed input. | | | | cse || CheckInt32 | Input 0 as an int32; exits unless it is one. | E | | | cse |+| CheckFloat64Int32 | The float64 input 0 as an int32; exits unless it is one (-0 is none). What an int32 check of a boxed double becomes once checks of doubles no longer go. | E | | | cse || CheckNumber | Input 0 as a float64; exits unless it is a number. | E | | | cse || EmptyToUndefined | Input 0 with the empty value replaced by `undefined`. | | | | cse || CellAddress | The cell pointer of a tagged value, shared by the nodes accessing the object. | | | | cse |@@ -392,7 +393,7 @@ unreachable. Each one keeps the graph valid (`passes/verify.rs`), and IR tests i| Simplification | Again, for what unboxing made foldable. || Global value numbering | A CSE-able node computing what a dominating node computed is replaced by it, so that check elimination sees one value where the builder computed it twice (like the index of `k[Math.abs(j)]` in two expressions). Memory reads are left to check elimination. || Check elimination | A forward dataflow of facts removes checks that passed already, decides branches that a branch on the same values decided before, and reuses loads whose value is known; may turn shape checks into `AssumeValid` plus a stable shape dependency. |-| Simplification | Again, for what the branches that check elimination decided left foldable (like the double path of `Math.abs` of an int32). |+| Simplification | Again, for what the branches that check elimination decided left foldable (like the double path of `Math.abs` of an int32). Int32 checks of boxed doubles become `CheckFloat64Int32` of the doubles. || Global value numbering | Again, for the nodes that check elimination and simplification made the same. || Loop invariant code motion | Gives loops entered from several blocks a merged preheader, then moves effect-free nodes with inputs from outside the loop to it (but calls in cold blocks); nodes that may exit too, exiting to the loop header, and not where that kind of exit was taken there before. Those from blocks that not every iteration runs, but for cold ones, move speculatively: they exit with `Hoisted`, and stay in the loop where such an exit was taken there before. || Bounds check elimination | Bounds checks against constant counts go away where the index is known to be in range: induction variables of loops whose back edge compares the grown counter with a constant bound, merges of values from outside a loop, and int32 arithmetic on them that cannot overflow. The counter an on-stack replacement entry brings is checked against the bound once where it enters, unless such a check exited there before. With the same ranges, `((x << k) + c) >> k` becomes `x + (c >> k)` where nothing overflows and `c` is a multiple of 2^k. Int32 additions, subtractions and multiplications become `Int32WrappingBinary` where the ranges of their operands keep the result an int32 (and not -0), with operands also bounded by the masks and shifts of any int32, and by the dominating bounds checks and in-bounds branches of indices. |diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rsindex ff3187bf584..02751e58177 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs@@ -1381,6 +1381,7 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {self.emit_unbox_cell(output, input);}Op::CheckInt32 => self.emit_check_int32(node_id),+ Op::CheckFloat64Int32 => self.emit_check_float64_int32(node_id),Op::CheckNumber => self.emit_check_number(node_id),Op::CheckIdentityComparable => self.emit_check_identity_comparable(node_id),Op::BoxInt32 => {diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/speculation.rs b/Libraries/LibJS/JIT/Rust/src/codegen/speculation.rsindex 2d6f47be342..0021d6998f6 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/speculation.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/speculation.rs@@ -12,6 +12,7 @@ use super::Codegen;use super::InputValue;use crate::asm::Condition;use crate::asm::Gpr;+use crate::asm::NegativeZero;use crate::asm::PortableMacroAssembler;use crate::code::Repr;use crate::ir::BinaryOp;@@ -45,6 +46,13 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {self.masm.move32(output, input);}+ pub(super) fn emit_check_float64_int32(&mut self, node: NodeId) {+ let exit = self.exit_site(node);+ let (input, output) = (self.float_input(node, 0), self.output(node));+ self.masm+ .branch_convert_double_to_int32(output, input, exit, NegativeZero::Fail);+ }+pub(super) fn emit_check_identity_comparable(&mut self, node: NodeId) {let exit = self.exit_site(node);let ok = self.masm.new_label();diff --git a/Libraries/LibJS/JIT/Rust/src/ir/dump.rs b/Libraries/LibJS/JIT/Rust/src/ir/dump.rsindex cc01a66f768..2269e59b831 100644--- a/Libraries/LibJS/JIT/Rust/src/ir/dump.rs+++ b/Libraries/LibJS/JIT/Rust/src/ir/dump.rs@@ -295,6 +295,7 @@ fn op_text(layout: &FrameLayout, op: &Op, inputs: &str) -> String {| Op::SingleCharacterString| Op::Float64ToInt32| Op::Float64ToUint8Clamped+ | Op::CheckFloat64Int32| Op::Uint32ShiftRight| Op::Uint32ToFloat64| Op::StringsEqualdiff --git a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs b/Libraries/LibJS/JIT/Rust/src/ir/mod.rsindex 24bc6c4c0ab..0b83e975af9 100644--- a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs+++ b/Libraries/LibJS/JIT/Rust/src/ir/mod.rs@@ -1020,6 +1020,10 @@ pub enum Op {Float64ToInt32,/// ToUint8Clamp of the numeric `Repr::Float64` input 0, as a `Repr::Int32`.Float64ToUint8Clamped,+ /// The `Repr::Float64` input 0 as a `Repr::Int32`. Exits with+ /// `ExitKind::NotInt32` unless it is an int32 (and not -0): what a+ /// `CheckInt32` of its `BoxFloat64` does, without the boxing.+ CheckFloat64Int32,/// `input 0 >>> input 1` of two `Repr::Int32` inputs, as the/// `Repr::Int32` of the bits of the unsigned result.Uint32ShiftRight,@@ -1348,7 +1352,11 @@ impl Op {allocates: true,..P::PURE},- Op::CheckInt32 | Op::CheckNumber | Op::CheckIdentityComparable | Op::CheckNonNegativeInt32 => P::PURE,+ Op::CheckInt32+ | Op::CheckFloat64Int32+ | Op::CheckNumber+ | Op::CheckIdentityComparable+ | Op::CheckNonNegativeInt32 => P::PURE,Op::BoxInt32| Op::BoxBool| Op::Int32Binary { .. }@@ -1643,6 +1651,7 @@ impl Op {Op::LoadArgument { .. } => "LoadArgument",Op::CallForwardingArguments { .. } => "CallForwardingArguments",Op::CheckInt32 => "CheckInt32",+ Op::CheckFloat64Int32 => "CheckFloat64Int32",Op::CheckNumber => "CheckNumber",Op::BoxInt32 => "BoxInt32",Op::BoxBool => "BoxBool",@@ -1802,7 +1811,7 @@ impl Op {Op::CheckObject => Some(ExitKind::NotObject),Op::CheckElements { .. } | Op::CheckTypedArray => Some(ExitKind::BadElements),Op::CheckBounds | Op::CheckNotHole | Op::CheckTypedArrayStorage => Some(ExitKind::OutOfBounds),- Op::CheckInt32 => Some(ExitKind::NotInt32),+ Op::CheckInt32 | Op::CheckFloat64Int32 => Some(ExitKind::NotInt32),Op::CheckArray => Some(ExitKind::BadType),Op::CheckNonNegativeInt32 => Some(ExitKind::Overflow),Op::CheckNumber | Op::CheckIdentityComparable | Op::HasInPrototypeChain => Some(ExitKind::BadType),diff --git a/Libraries/LibJS/JIT/Rust/src/passes/simplify.rs b/Libraries/LibJS/JIT/Rust/src/passes/simplify.rsindex 06c389a307d..1f952b5b022 100644--- a/Libraries/LibJS/JIT/Rust/src/passes/simplify.rs+++ b/Libraries/LibJS/JIT/Rust/src/passes/simplify.rs@@ -411,9 +411,11 @@ fn simplify(graph: &mut Graph, dissolve_checks: bool) {_ => None,},// NB: The double of a double checked to be an int32 is the- // double, which needs no check.+ // double, which needs no check. A CheckFloat64Int32 stays+ // where it is, since it may exit.Op::Int32ToFloat64 => match graph.node(inputs[0]).op {Op::UnboxInt32 => boxed(Op::BoxFloat64, graph.node(inputs[0]).inputs[0]),+ Op::CheckFloat64Int32 => Some(graph.node(inputs[0]).inputs[0]),_ => dissolved_double(graph, inputs[0]),},// NB: Both signed and unsigned int32 values are exactly@@ -533,6 +535,20 @@ fn simplify(graph: &mut Graph, dissolve_checks: bool) {removed.insert(node.index());continue;}+ // NB: Once no check of a double goes anymore, an int32 check of a+ // boxed double checks the double itself, so that only users+ // that need the boxing keep it.+ if !dissolve_checks+ && graph.node(node).op == Op::CheckInt32+ && let boxing = graph.node(graph.unrefined(inputs[0]))+ && boxing.op == Op::BoxFloat64+ {+ let double = boxing.inputs[0];+ let data = &mut graph.nodes[node.index()];+ data.op = Op::CheckFloat64Int32;+ data.inputs = vec![double];+ continue;+ }// NB: Int32 operations take constant right-hand sides as// immediates, so commutative ones get their constants there.if let Op::Int32Binary { op } | Op::Int32WrappingBinary { op } = graph.node(node).opdiff --git a/Libraries/LibJS/JIT/Rust/src/passes/verify.rs b/Libraries/LibJS/JIT/Rust/src/passes/verify.rsindex 3106beeaf86..9804027d529 100644--- a/Libraries/LibJS/JIT/Rust/src/passes/verify.rs+++ b/Libraries/LibJS/JIT/Rust/src/passes/verify.rs@@ -507,9 +507,11 @@ fn expected_input_reprs(graph: &Graph, node: NodeId) -> Option<Vec<Repr>> {Op::Branch { condition, .. } | Op::Refine { condition, .. } => Some(branch_input_reprs(condition)),Op::UnboxInt32 | Op::UnboxDouble | Op::CheckNumber | Op::StringAddress => Some(tagged(1)),Op::StringLength => Some(vec![Repr::Pointer]),- Op::BoxFloat64 | Op::Float64Unary { .. } | Op::Float64ToInt32 | Op::Float64ToUint8Clamped => {- Some(vec![Repr::Float64])- }+ Op::BoxFloat64+ | Op::Float64Unary { .. }+ | Op::Float64ToInt32+ | Op::Float64ToUint8Clamped+ | Op::CheckFloat64Int32 => Some(vec![Repr::Float64]),Op::Float64Binary { .. } | Op::Float64Min | Op::Float64Max | Op::Float64Compare { .. } => {Some(vec![Repr::Float64, Repr::Float64])}diff --git a/Tests/LibJS/JIT/ir/representation-phis.js b/Tests/LibJS/JIT/ir/representation-phis.jsindex fa7ad0da721..c2b3c62e312 100644--- a/Tests/LibJS/JIT/ir/representation-phis.js+++ b/Tests/LibJS/JIT/ir/representation-phis.js@@ -89,8 +89,8 @@ jit.compile(addActions);// CHECK-FUNCTION: addActions// CHECK-PASS: dead code elimination// CHECK: [[ELEMENT:v[0-9]+]]:float64 = LoadElementAt TypedArray(Uint32)-// CHECK-NEXT: [[BOXED:v[0-9]+]] = BoxFloat64 ([[ELEMENT]])-// CHECK: [[START:v[0-9]+]]:int32 = CheckInt32 ([[BOXED]])+// CHECK-NOT: BoxFloat64+// CHECK: [[START:v[0-9]+]]:int32 = CheckFloat64Int32 ([[ELEMENT]])// CHECK: loop:// CHECK-NEXT: [[I:v[0-9]+]]:int32 = Phi ([[START]], {{v[0-9]+}})// CHECK-NOT: CheckInt32 ({{v[0-9]+}})