Truly independent web browser
LibWasm: Align loops in compiled code
Andreas Kling8dc39cd603dc
diff --git a/Libraries/LibWasm/Engine/src/runtime/jit.rs b/Libraries/LibWasm/Engine/src/runtime/jit.rsindex b6ae1fa1b86..2b565a344a6 100644--- a/Libraries/LibWasm/Engine/src/runtime/jit.rs+++ b/Libraries/LibWasm/Engine/src/runtime/jit.rs@@ -725,6 +725,9 @@ mod native {const CHUNK_SIZE: usize = 16 << 20;+ /// Where the code of a function starts, which the alignment of its loops relies on.+ const CODE_ALIGNMENT: usize = 64;+/// How many compiles a store installs between looking for modules that died.const SWEEP_INTERVAL: usize = 64;@@ -767,7 +770,7 @@ mod native {}fn has_room(&self, length: usize) -> bool {- self.used.next_multiple_of(16) + length <= self.size+ self.used.next_multiple_of(CODE_ALIGNMENT) + length <= self.size}/// Copies `code` in and returns its address, or None if the pages could not be made writable, which leaves them@@ -775,7 +778,7 @@ mod native {// NB: On macOS, switching the thread's view of the code cannot fail.#[cfg_attr(target_os = "macos", allow(clippy::unnecessary_wraps))]fn add(&mut self, code: &[u8]) -> Option<u64> {- let start = self.used.next_multiple_of(16);+ let start = self.used.next_multiple_of(CODE_ALIGNMENT);debug_assert!(start + code.len() <= self.size);#[cfg(target_os = "macos")]unsafe {diff --git a/Libraries/LibWasm/JIT/ARCHITECTURE.md b/Libraries/LibWasm/JIT/ARCHITECTURE.mdindex e96bd0cb364..e85ee10123a 100644--- a/Libraries/LibWasm/JIT/ARCHITECTURE.md+++ b/Libraries/LibWasm/JIT/ARCHITECTURE.md@@ -258,7 +258,9 @@ Small innermost loops without calls lose their check (`passes/interrupt_checks.rby one towards a bound a step cannot go past: `<`, `>` or `!=` of an invariant bound. Everything wrapsaround here, so loops that step by more keep it. One known to run under 5000 times goes anywhere.Any other ends within 2^32 iterations, and goes only where a loop around it keeps its check, so that-an interrupt takes as long as the rest of that loop at most.+an interrupt takes as long as the rest of that loop at most. x86-64 aligns loop headers to 64 bytes,+and code to the same: what lies in front of a small loop otherwise decides how it lies across what+the CPU fetches. That is worth 6% of CoreMark's score.**Calls.** Every call goes through `FunctionHeader::native_entry`. A direct call of a function ofthe same instance passes the caller's instance and memory base; any other call (imports,diff --git a/Libraries/LibWasm/JIT/src/codegen/x86_64/mod.rs b/Libraries/LibWasm/JIT/src/codegen/x86_64/mod.rsindex 106cc593124..57d95902872 100644--- a/Libraries/LibWasm/JIT/src/codegen/x86_64/mod.rs+++ b/Libraries/LibWasm/JIT/src/codegen/x86_64/mod.rs@@ -53,6 +53,10 @@ pub(crate) fn precision(ty: Type) -> Precision {}const SCRATCH: Gpr = Gpr(convention::SCRATCH_GPR);++/// Where loop headers go. How a small loop lies across the windows and lines of code that the CPU fetches decides how+/// fast it runs, which would otherwise change with anything added in front of it. This is worth 6% of CoreMark's score.+const LOOP_ALIGNMENT: usize = 64;const SCRATCH_FPR: Fpr = Fpr(convention::SCRATCH_FPR - convention::FPR_BASE);const FP: Gpr = Gpr(convention::RBP);const SP: Gpr = Gpr(convention::RSP);@@ -147,6 +151,9 @@ impl<'a> CodeGenerator<'a> {for block in 0..func.blocks.len() as BlockId {self.block = block;let label = self.block_labels[block as usize];+ if func.is_loop_header(block) {+ self.asm.align_code(LOOP_ALIGNMENT);+ }self.asm.bind(label);// Phis in registers that have a slot too are stored there where they are defined.for phi in func.phis(block) {