Skip to content

a64/jit: use Wasm wide multiply for UMULH and SMULH - #97

Open
SukharevAndrey wants to merge 1 commit into
pithings:mainfrom
SukharevAndrey:perf/jit-wide-multiply
Open

SukharevAndrey wants to merge 1 commit into
pithings:mainfrom
SukharevAndrey:perf/jit-wide-multiply

Conversation

@SukharevAndrey

Copy link
Copy Markdown

Summary

UMULH and SMULH return the upper 64 bits of a 64 × 64-bit product. The JIT computes those bits from 32-bit partial products. On engines with Wasm wide arithmetic, emit i64.mul_wide_u or i64.mul_wide_s and keep the high word.

The SDK probes both opcodes once per JIT host with WebAssembly.validate. Capability bit 6 enables the new lowering. When either opcode is unavailable, or an older host omits the bit, the JIT emits the partial products. The probe is shared with the Zig test host, so the Zig package includes js/core/jit.ts. This change does not alter KW_ABI_VERSION.

Benchmarks

2026-11-29, Apple M1 Max, AArch64 ELF harness. Each pair used the same kwemu build with Wasm wide multiply disabled or enabled. Each timing was the median of three warmed passes. The time columns show separate medians. The last column gives the median change within pairs (negative is faster).

Workload Configuration Pairs Baseline (ms) Optimized (ms) Median paired change
GMP multiplication Firefox 161, memory64 9 262.92 250.38 −3.94%
GMP multiplication Bun 1.4.5, memory32 9 192.09 182.03 −5.80%
musl qsort (no UMULH or SMULH) Firefox 161, memory64 9 72.24 73.44 +0.44%
musl qsort (no UMULH or SMULH) Bun 1.4.5, memory32 5 52.86 53.04 +0.12%
Dependent UMULH loop Firefox 161, memory64 9 78.72 26.02 −66.95%
Dependent SMULH loop Firefox 161, memory64 9 91.26 26.00 −71.53%

GMP 6.3.0 made 16,384 mpz_mul calls per pass on 128 deterministic operand pairs of 512–4,096 bits, checking one word from each product. GMP finished sooner in all nine pairs on each host. It executed 11.15 million UMULH instructions per pass (9.15% of guest instructions). Generated Wasm fell from 241,044 to 230,319 bytes in Firefox and from 245,379 to 234,654 bytes in Bun. GMP exercised the unsigned form only. The two instruction loops each chained about 20 million multiplications, feeding the high word into the next iteration. They isolate the cost of each signedness. The sorting control executed neither wide multiply instruction and emitted the same number of bytes with either setting. Five A/A pairs in Bun (fallback on both sides) gave a median GMP timing change of −0.31%. The Firefox series compared fallback with wide multiply only.

Verification

The high-multiply regression runs signed and unsigned compiled blocks with boundary operands and a destination that overlaps a source. It checks the result, emitted opcode, entry count and hand-backs. The Zig suite also feeds signed and unsigned operations through the emitter with the wide capability forced on and checks the opcode and the order in which it consumes the two result words. This check runs under Node 24.21.0, where the engine rejects wide arithmetic and the execution test takes the fallback. A separate AArch64 ELF checked 768 operand pairs against exact Python arithmetic, including overlapping registers and XZR. It ran in Firefox with wide multiply enabled and disabled. In Firefox 161 the high-multiply regression also passed through both the SDK and the test host. Each host validated the feature once. The SDK checks below exercised both paths in Bun.

  • zig build test completed all 55 build steps and matched 77 guest results against QEMU 11.0.3. The Wasm JIT tests ran with SIMD enabled and disabled.
  • kernel/build.sh kernel built the UP kernel. Both the default and --no-jit boots ran kwtest with zero failures.
  • The SDK typecheck passed. The Linux Node 24.21.0 API suite reported 134 passes, zero failures and eight skips for SMP, external Wisp or a kernel configuration without kwshm.
  • At the standard JIT threshold, the SDK detected wide multiply on Bun 1.4.5 and emitted both opcodes. The same signed and unsigned guest loops produced the same checksums under the Node 24.21.0 fallback and under Bun when the probe was made to reject the extension. Each host validated the probe once and compiled the expected code with zero failures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant