v137 · webassembly
Wasm Branch Optimizer
WebAssembly Branch Hints let you annotate if instructions as @likely or @unlikely to guide the JIT compiler's branch prediction. This tool shows C → Wasm WAT mappings for three common patterns, explains where each hint goes, and benchmarks two synthetic loops to observe potential timing differences.
Branch hint patterns
Error guard
Hot path
Rare event
no hint
C — no hint
int divide(int a, int b) {
if (b == 0) { // no hint
return -1; // error case
}
return a / b;
}
; WAT (simplified)
(if (i32.eqz (local.get $b))
(then (return (i32.const -1))))
@unlikely
C — @unlikely hint
int divide(int a, int b) {
if (__builtin_expect(b == 0, 0)) {
return -1; // rarely happens
}
return a / b;
}
; WAT with Branch Hints proposal
(if (@unlikely)
(i32.eqz (local.get $b))
(then (return (i32.const -1))))
@unlikely: The JIT places the error handler in a cold block. The fall-through
(normal division) path is optimized as the expected branch, improving instruction cache usage
when b ≠ 0 is the common case.
Benchmark simulation
Click Run to benchmark two hot-path loops (JS simulation — Wasm timing in supported builds).
Branch Hints proposal: Wasm Branch Hints add two new custom section
annotations —
@likely and @unlikely — corresponding to
__builtin_expect in C/C++. They are a hint to the JIT, not a semantic
change; the Wasm spec guarantees the same results either way. Chrome 137 reads these
annotations and factors them into its speculative branch optimizer.