Skip to content

Truly independent web browser

LibJS/JIT: Know that values of known kinds are not the empty value

Andreas Klingaac5b4207490
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/check_elimination.rs b/Libraries/LibJS/JIT/Rust/src/passes/check_elimination.rs
index e1c39dd0a3d..122b40fc2f4 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/check_elimination.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/check_elimination.rs
@@ -294,6 +294,16 @@ fn visit_block(
.iter()
.map(|input| decisions.value(graph, *input))
.collect::<InlineVec<NodeId, 2>>();
+ // NB: A value of a known kind is no empty value (like a binding that
+ // is initialized, which a store of it checks).
+ if let BranchCondition::TaggedEquals { equal } = condition
+ && graph.constant_value(inputs[1]) == Some(value::EMPTY)
+ && facts.value_kind(inputs[0]).is_some()
+ {
+ let target = if equal { if_false } else { if_true };
+ decisions.controls.push((control, Op::Jump { target }));
+ return vec![(target, facts)];
+ }
if let Some(key) = BranchKey::of(graph, condition, &inputs) {
if let Some(holds) = facts.branch_outcome(&key) {
let target = if holds { if_true } else { if_false };
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/tests.rs b/Libraries/LibJS/JIT/Rust/src/passes/tests.rs
index 5272eb8e896..72f86226ed8 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/tests.rs
@@ -543,6 +543,64 @@ fn branches_on_known_kinds_fold() {
assert_eq!(branches, 1, "{:?}", graph.blocks);
}
+/// A value that passed an int32 check is no empty value, so a branch on
+/// whether it is one (like the initialization check of a binding store)
+/// takes the edge where it is not.
+#[test]
+fn branches_on_whether_values_of_known_kinds_are_empty_fold() {
+ let mut graph = Graph::default();
+ for predecessors in [vec![], vec![BlockId(0)], vec![BlockId(0)]] {
+ graph.add_block(Block {
+ predecessors,
+ ..Block::default()
+ });
+ }
+ let frame_state = graph.add_frame_state(crate::ir::FrameState {
+ executable: 0,
+ pc: 0,
+ mode: crate::code::ResumeMode::ResumeAt,
+ values: Vec::new(),
+ in_frame: Vec::new(),
+ parent: None,
+ passed_argument_count: None,
+ legacy_arguments: 0..0,
+ });
+ let value = node(&mut graph, Op::LoadSlot { slot: 0 }, vec![], Some(Repr::Tagged));
+ let check = node(&mut graph, Op::CheckInt32, vec![value], Some(Repr::Int32));
+ graph.nodes[check.index()].frame_state = Some(frame_state);
+ let empty = node(
+ &mut graph,
+ Op::Constant(crate::ir::value::EMPTY),
+ vec![],
+ Some(Repr::Tagged),
+ );
+ let branch = node(
+ &mut graph,
+ Op::Branch {
+ condition: BranchCondition::TaggedEquals { equal: false },
+ if_true: BlockId(1),
+ if_false: BlockId(2),
+ },
+ vec![value, empty],
+ None,
+ );
+ graph.blocks[0].body = vec![value, check];
+ graph.blocks[0].control = Some(branch);
+ for block in [1, 2] {
+ let ret = node(&mut graph, Op::Return, vec![value], None);
+ graph.blocks[block].control = Some(ret);
+ }
+ verify(&graph).unwrap();
+
+ super::check_elimination::run(&mut graph);
+ let control = graph.blocks[0].control.unwrap();
+ assert!(
+ matches!(graph.node(control).op, Op::Jump { target: BlockId(1) }),
+ "{:?}",
+ graph.node(control)
+ );
+}
+
/// b4 is reached from the entry b0, which checks a constant, and from b2,
/// which only the on-stack replacement entry b1 reaches through b3 (a block
/// later in the order than b2). b2 is skipped until b3 has been visited, so
diff --git a/Tests/LibJS/Runtime/jit/environment-bindings.js b/Tests/LibJS/Runtime/jit/environment-bindings.js
index 32067744eaa..b7249022a88 100644
--- a/Tests/LibJS/Runtime/jit/environment-bindings.js
+++ b/Tests/LibJS/Runtime/jit/environment-bindings.js
@@ -162,3 +162,33 @@ test("bindings written in loops are read back", () => {
}
for (let i = 0; i < 200; ++i) expect(counts()).toBe(10);
});
+
+test("bindings incremented several times in a row are read back", () => {
+ function steps() {
+ let top = 0;
+ const advance = () => {
+ const start = top;
+ top = (top + 8) | 0;
+ top = (top + 8) | 0;
+ top = (top + 8) | 0;
+ return top - start;
+ };
+ let sum = 0;
+ for (let i = 0; i < 10; ++i) sum += advance();
+ return sum + top;
+ }
+ for (let i = 0; i < 200; ++i) expect(steps()).toBe(480);
+});
+
+test("assigning an uninitialized binding with a number throws", () => {
+ function assignEarly(value) {
+ const assign = () => {
+ binding = (value + 1) | 0;
+ };
+ assign();
+ let binding = 0;
+ return binding;
+ }
+ for (let i = 0; i < 200; ++i)
+ expect(() => assignEarly(i)).toThrowWithMessage(ReferenceError, "Binding binding is not initialized");
+});