Skip to content

Truly independent web browser

LibWasm: Put a loop back edge's moves inline in the JIT

Andreas Kling21ab5ffe31d2
diff --git a/Libraries/LibWasm/JIT/ARCHITECTURE.md b/Libraries/LibWasm/JIT/ARCHITECTURE.md
index a78da975751..09a59d02ebd 100644
--- a/Libraries/LibWasm/JIT/ARCHITECTURE.md
+++ b/Libraries/LibWasm/JIT/ARCHITECTURE.md
@@ -329,8 +329,10 @@ there; a phi in a register with a slot is stored where its block starts.
- **Edges.** A jump moves its values and jumps unless its target comes next. A two-way branch whose
taken edge needs moves jumps to a stub that moves them; its other edge moves inline. Where the
- taken target comes next and needs no moves, the condition is inverted. `br_table` is a jump table
- of 32-bit offsets, with stubs for the edges that need moves.
+ taken target comes next and needs no moves, the condition is inverted. It is also inverted where
+ the taken target is a loop header the edge needs moves into: the exit jumps (to a stub if it needs
+ moves), and the back edge moves inline, so that an iteration takes one jump rather than two.
+ `br_table` is a jump table of 32-bit offsets, with stubs for the edges that need moves.
- **Traps** jump to one stub per trap code, which puts the code in edi and jumps to
`RuntimeInfo::trap_exit` with rbp still the trapping frame's fp. Memory accesses rely on guard
pages: the runtime's fault handler turns faults in compiled code into the same trap exit.
diff --git a/Libraries/LibWasm/JIT/src/codegen/aarch64/mod.rs b/Libraries/LibWasm/JIT/src/codegen/aarch64/mod.rs
index ec87cc77ece..5d046d271ab 100644
--- a/Libraries/LibWasm/JIT/src/codegen/aarch64/mod.rs
+++ b/Libraries/LibWasm/JIT/src/codegen/aarch64/mod.rs
@@ -588,7 +588,8 @@ impl<'a> CodeGenerator<'a> {
}
/// Ends the block with a two-way branch on `cond`, which holds for the first successor; inverted where the taken
- /// successor comes next and needs no moves.
+ /// successor comes next and needs no moves, or is a loop header the edge needs moves into, so that those moves are
+ /// inline rather than in a stub that branches again.
fn branch_on(&mut self, cond: Cond) {
let block = self.block;
let data = self.func.blocks[block as usize];
@@ -599,6 +600,12 @@ impl<'a> CodeGenerator<'a> {
self.asm.b_cond(cond.invert(), label);
return;
}
+ if self.func.is_loop_header(taken) && !self.allocation.edge(data.successors_start).is_empty() {
+ let label = self.edge_label(data.successors_start + 1, fallthrough);
+ self.asm.b_cond(cond.invert(), label);
+ self.jump(0);
+ return;
+ }
let taken_label = self.edge_label(data.successors_start, taken);
self.asm.b_cond(cond, taken_label);
for item in self.allocation.edge(data.successors_start + 1) {
diff --git a/Libraries/LibWasm/JIT/src/codegen/x86_64/mod.rs b/Libraries/LibWasm/JIT/src/codegen/x86_64/mod.rs
index 9cee6fcefad..603d1566f78 100644
--- a/Libraries/LibWasm/JIT/src/codegen/x86_64/mod.rs
+++ b/Libraries/LibWasm/JIT/src/codegen/x86_64/mod.rs
@@ -539,7 +539,8 @@ impl<'a> CodeGenerator<'a> {
}
}
- /// A branch on an integer condition, inverted where the taken successor comes next and needs no moves.
+ /// A branch on an integer condition, inverted where the taken successor comes next and needs no moves, or is a
+ /// loop header the edge needs moves into, so that those moves are inline rather than in a stub that jumps again.
fn branch_on(&mut self, cond: Cond) {
let block = self.block;
let data = self.func.blocks[block as usize];
@@ -550,6 +551,12 @@ impl<'a> CodeGenerator<'a> {
self.asm.jcc(cond.invert(), label);
return;
}
+ if self.func.is_loop_header(taken) && !self.allocation.edge(data.successors_start).is_empty() {
+ let label = self.edge_label(data.successors_start + 1, fallthrough);
+ self.asm.jcc(cond.invert(), label);
+ self.jump(0);
+ return;
+ }
self.branch(|generator, taken, _| generator.asm.jcc(cond, taken));
}