Stream: git-wasmtime

Topic: wasmtime / issue #4883 Code generated by `wasmtime` doesn...


view this post on Zulip Wasmtime GitHub notifications bot (Sep 08 2022 at 07:54):

koute opened issue #4883:

The problem

Currently wasmtime/cranelift (unlike e.g. LLVM which doesn't have this problem AFAIK) doesn't cache-align the loops it generates, leading to potentially huge performance regressions if a hot loop ends up accidentally spanning over multiple cache lines.

Background

Recently we were updating from wasmtime 0.38 to 0.40 and we saw a peculiar performance regression when doing so. One of our benchmarks took almost 2x the time to run, with a lot of them taking around ~45% more time. A huge regression. Ultimately it ended up being unrelated to the 0.38 -> 0.40 upgrade. We tracked the problem down to memset within the WASM (we're currently not using the bulk memory ops extension) suddenly taking a lot more time to run for no apparent reason. Depending on which exact address wasmtime decided to generate the code for memset at (which is essentially random, although consistent for the same code with the same flags in the same environment) the benchmarks were either slow, or fast, and it all boiled down to whether the hot loop of the memset spanned multiple cache lines or not.

You can find a detailed analysis of the problem in this comment and this comment of mine.

view this post on Zulip Wasmtime GitHub notifications bot (Sep 08 2022 at 16:26):

cfallin commented on issue #4883:

Thanks for tracking this down, @koute! Yes, I agree that aligning loop headers to cache-line boundaries makes sense. Probably as a compile-time option, when opts are enabled (debug code is going to be substantially more bloated for other reasons so we don't want to inflate further, and is going to be slow anyway).

view this post on Zulip Wasmtime GitHub notifications bot (Sep 08 2022 at 17:21):

cfallin commented on issue #4883:

This might be a good starter issue for someone to tackle. The main steps I see this taking are:

If no one else wants to take it, I can do this at some point but I thought I would put this out there first!

view this post on Zulip Wasmtime GitHub notifications bot (Sep 08 2022 at 17:21):

cfallin labeled issue #4883:

The problem

Currently wasmtime/cranelift (unlike e.g. LLVM which doesn't have this problem AFAIK) doesn't cache-align the loops it generates, leading to potentially huge performance regressions if a hot loop ends up accidentally spanning over multiple cache lines.

Background

Recently we were updating from wasmtime 0.38 to 0.40 and we saw a peculiar performance regression when doing so. One of our benchmarks took almost 2x the time to run, with a lot of them taking around ~45% more time. A huge regression. Ultimately it ended up being unrelated to the 0.38 -> 0.40 upgrade. We tracked the problem down to memset within the WASM (we're currently not using the bulk memory ops extension) suddenly taking a lot more time to run for no apparent reason. Depending on which exact address wasmtime decided to generate the code for memset at (which is essentially random, although consistent for the same code with the same flags in the same environment) the benchmarks were either slow, or fast, and it all boiled down to whether the hot loop of the memset spanned multiple cache lines or not.

You can find a detailed analysis of the problem in this comment and this comment of mine.

view this post on Zulip Wasmtime GitHub notifications bot (Sep 08 2022 at 17:21):

cfallin labeled issue #4883:

The problem

Currently wasmtime/cranelift (unlike e.g. LLVM which doesn't have this problem AFAIK) doesn't cache-align the loops it generates, leading to potentially huge performance regressions if a hot loop ends up accidentally spanning over multiple cache lines.

Background

Recently we were updating from wasmtime 0.38 to 0.40 and we saw a peculiar performance regression when doing so. One of our benchmarks took almost 2x the time to run, with a lot of them taking around ~45% more time. A huge regression. Ultimately it ended up being unrelated to the 0.38 -> 0.40 upgrade. We tracked the problem down to memset within the WASM (we're currently not using the bulk memory ops extension) suddenly taking a lot more time to run for no apparent reason. Depending on which exact address wasmtime decided to generate the code for memset at (which is essentially random, although consistent for the same code with the same flags in the same environment) the benchmarks were either slow, or fast, and it all boiled down to whether the hot loop of the memset spanned multiple cache lines or not.

You can find a detailed analysis of the problem in this comment and this comment of mine.

view this post on Zulip Wasmtime GitHub notifications bot (Sep 12 2022 at 09:47):

akirilov-arm labeled issue #4883:

The problem

Currently wasmtime/cranelift (unlike e.g. LLVM which doesn't have this problem AFAIK) doesn't cache-align the loops it generates, leading to potentially huge performance regressions if a hot loop ends up accidentally spanning over multiple cache lines.

Background

Recently we were updating from wasmtime 0.38 to 0.40 and we saw a peculiar performance regression when doing so. One of our benchmarks took almost 2x the time to run, with a lot of them taking around ~45% more time. A huge regression. Ultimately it ended up being unrelated to the 0.38 -> 0.40 upgrade. We tracked the problem down to memset within the WASM (we're currently not using the bulk memory ops extension) suddenly taking a lot more time to run for no apparent reason. Depending on which exact address wasmtime decided to generate the code for memset at (which is essentially random, although consistent for the same code with the same flags in the same environment) the benchmarks were either slow, or fast, and it all boiled down to whether the hot loop of the memset spanned multiple cache lines or not.

You can find a detailed analysis of the problem in this comment and this comment of mine.

view this post on Zulip Wasmtime GitHub notifications bot (Sep 12 2022 at 09:47):

akirilov-arm labeled issue #4883:

The problem

Currently wasmtime/cranelift (unlike e.g. LLVM which doesn't have this problem AFAIK) doesn't cache-align the loops it generates, leading to potentially huge performance regressions if a hot loop ends up accidentally spanning over multiple cache lines.

Background

Recently we were updating from wasmtime 0.38 to 0.40 and we saw a peculiar performance regression when doing so. One of our benchmarks took almost 2x the time to run, with a lot of them taking around ~45% more time. A huge regression. Ultimately it ended up being unrelated to the 0.38 -> 0.40 upgrade. We tracked the problem down to memset within the WASM (we're currently not using the bulk memory ops extension) suddenly taking a lot more time to run for no apparent reason. Depending on which exact address wasmtime decided to generate the code for memset at (which is essentially random, although consistent for the same code with the same flags in the same environment) the benchmarks were either slow, or fast, and it all boiled down to whether the hot loop of the memset spanned multiple cache lines or not.

You can find a detailed analysis of the problem in this comment and this comment of mine.

view this post on Zulip Wasmtime GitHub notifications bot (Sep 12 2022 at 09:47):

akirilov-arm labeled issue #4883:

The problem

Currently wasmtime/cranelift (unlike e.g. LLVM which doesn't have this problem AFAIK) doesn't cache-align the loops it generates, leading to potentially huge performance regressions if a hot loop ends up accidentally spanning over multiple cache lines.

Background

Recently we were updating from wasmtime 0.38 to 0.40 and we saw a peculiar performance regression when doing so. One of our benchmarks took almost 2x the time to run, with a lot of them taking around ~45% more time. A huge regression. Ultimately it ended up being unrelated to the 0.38 -> 0.40 upgrade. We tracked the problem down to memset within the WASM (we're currently not using the bulk memory ops extension) suddenly taking a lot more time to run for no apparent reason. Depending on which exact address wasmtime decided to generate the code for memset at (which is essentially random, although consistent for the same code with the same flags in the same environment) the benchmarks were either slow, or fast, and it all boiled down to whether the hot loop of the memset spanned multiple cache lines or not.

You can find a detailed analysis of the problem in this comment and this comment of mine.

view this post on Zulip Wasmtime GitHub notifications bot (Aug 16 2026 at 16:57):

just-an-engineer commented on issue #4883:

I'd like to take this on

view this post on Zulip Wasmtime GitHub notifications bot (Nov 09 2026 at 06:24):

zion-sati commented on issue #4883:

Some aarch64 data for this issue, from Apple M1 Max with Wasmtime 50.0.1.

We compile .NET to Wasm (NetWasm) and benchmark on Wasmtime, and loop placement is the largest source of noise we see on this machine: identical loop code moving by a few bytes changes run time by 20 to 30%, which is more than most optimizations we measure. Aligning functions does not help, because the loop moves inside its function.

Minimal repro. The function below is the trial-division loop from our prime search workload (largest prime below a limit, one division a step), written out as Wasm in the shape our compiler emits it. N stores of the argument to globals run before the loops, so each extra store moves both loops 4 bytes later in the function and nothing else changes. Each variant is precompiled with -C cranelift-log2_min_function_alignment=12 and run with wasmtime run --allow-precompiled --invoke run pad.cwasm 10000000; loop head offsets come from wasmtime objdump --addresses. Median of 5 runs, quiet machine:

N outer, inner loop head (offset in function : mod 64) ms vs fastest
0 20:20, 96:32 768.6 1.220
1 24:24, 100:36 737.4 1.170
2 28:28, 104:40 769.0 1.220
3 32:32, 108:44 737.1 1.170
4 36:36, 112:48 769.6 1.221
5 40:40, 116:52 739.2 1.173
6 44:44, 120:56 770.9 1.224
7 48:48, 124:60 642.5 1.020
8 52:52, 128:0 765.9 1.216
9 56:56, 132:4 635.5 1.009
10 60:60, 136:8 765.4 1.215
11 64:0, 140:12 637.1 1.011
12 68:4, 144:16 766.1 1.216
13 72:8, 148:20 631.8 1.003
14 76:12, 152:24 767.6 1.218
15 80:16, 156:28 630.1 1.000

The inner loop is 12 instructions: a multiply and compare that leaves on a match, the division's zero and overflow checks, the sdiv and a conditional back edge. With its head at 0 mod 8 it is always slow; at 4 mod 8 it is fast from offset 124 on and in between before that.

<details>
<summary>The module for N = 2</summary>

(module
  (global $g0 (mut i32) (i32.const 0))
  (global $g1 (mut i32) (i32.const 0))
  (global $sink (mut i32) (i32.const 0))
  (func (export "run") (param $limit i32) (result i32)
    (local $candidate i32) (local $largest i32) (local $quotient i32) (local $seed i32)
    local.get $limit
    local.set $seed
    local.get $limit
    global.set $g0
    local.get $limit
    global.set $g1
    i32.const 2
    local.set $largest
    i32.const 2
    local.set $candidate
    loop $candidates
      local.get $candidate
      local.get $largest
      block $verdict (result i32)
        i32.const 0
        local.get $candidate
        i32.const 2
        i32.lt_s
        br_if $verdict
        drop
        i32.const 1
        local.get $candidate
        i32.const 2
        i32.eq
        br_if $verdict
        drop
        i32.const 0
        local.get $candidate
        i32.const 1
        i32.and
        i32.eqz
        br_if $verdict
        drop
        i32.const 3
        local.set $largest
        local.get $candidate
        i32.const 3
        i32.div_s
        local.tee $quotient
        i32.const 3
        i32.ge_s
        if
          loop $divisors
            i32.const 0
            local.get $largest
            local.get $quotient
            i32.mul
            local.get $candidate
            i32.eq
            br_if $verdict
            drop
            local.get $candidate
            local.get $largest
            i32.const 2
            i32.add
            local.tee $largest
            i32.div_s
            local.set $quotient
            local.get $largest
            local.get $quotient
            i32.le_s
            br_if $divisors
          end
        end
        i32.const 1
      end
      select
      local.set $largest
      local.get $candidate
      i32.const 1
      i32.add
      local.tee $candidate
      local.get $limit
      i32.lt_s
      br_if $candidates
    end
    local.get $seed
    global.set $sink
    local.get $largest))

</details>

In real code. The same loop in our compiled workload went from 630 ms to 763 ms between Wasmtime 49.0.3 and 50.0.1 with identical loop instructions: 49 emitted return-address signing (pacibz/autibz) by default on macOS and 50 does not, so every function's body sits 4 bytes earlier. Passing -C cranelift-sign_return_address=true to 50 gives the same instructions at the same offsets and the old timing. We see the same pattern elsewhere: a few extra instructions early in a function move three identical string-search loops by 8 to 40 bytes and cost one benchmark about 1.3x, and a LINQ loop that is the same instructions in two builds differs by 1.2x at every function alignment we tried. Compiled with -W epoch-interruption=y, whose checks move code inside functions, those differences disappear or reverse.

The same workload compiled by Rust and by Mono's AOT compiler did not move on that upgrade; their loops have other shapes, so this looks like a property of loop shape and position rather than of one compiler's output.

Happy to time a prototype of loop-head alignment on this machine and on x86-64, with the repro above and our 2,000-case micro suite, if that would help with the code-size and heuristic questions raised in #5004.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 09 2026 at 20:44):

cfallin commented on issue #4883:

Thanks @zion-sati. At this point we don't really need data justifying that icache alignment can improve performance; we know this. What we need is a reasonable heuristic that does this alignment without inflating code too much. PRs are welcome if you can show Sightglass runs over the whole suite that show improvements, and also show stats on size of compiled modules and show acceptable impact there (since code size is a particular concern for this change).

view this post on Zulip Wasmtime GitHub notifications bot (Nov 10 2026 at 03:53):

zion-sati commented on issue #4883:

Following up with a prototype and measurements, as offered above.

The prototype (branch) adds three Cranelift settings: an alignment, a padding budget and a maximum loop size. It aligns the header of each innermost, non-cold loop. The header's label is bound before the padding is measured, so a jump into the loop from the previous block is still removed.

Code size over benchmarks/all.suite (the span of the function symbols in each .cwasm, so page padding is not counted):

Setting aarch64 x86-64
16 bytes, at most 8 of padding +0.29% +0.17%
32 bytes, at most 16 of padding +0.48% +0.32%
64 bytes, at most 63 of padding +3.33% +2.80%

Execution on x86-64 (Intel Core i5-12600K, P-cores only, Sightglass execution phase, 20 iterations). The same engine library ran with the setting off and on. The geomean is +0.4% at 32 bytes and +0.1% at 16 bytes, leaving out five libsodium and noop benchmarks whose own spread exceeds 10%. A few loop-bound benchmarks gain: wat-bytecode-interp 1.16x and shootout-sieve 1.11x at 32 bytes. A few lose: shootout-minicsv 0.95. Nearly all of the padding sits where the previous block falls through into the loop, so it runs once per loop entry.

Execution on an Apple M1 Max. Alignment removes the placement variance I reported above, but it doesn't pick the fast placement. On current main, the trial-division loop from the repro above ran at 1.00 to 1.22 of its best time across 16 placements with alignment off. It runs at about 1.21 at every 16- and 32-byte-aligned placement.

So on these two machines, a static heuristic like this gives no suite-wide gain, and I won't open a PR. The raw data, per-benchmark results and scripts are here for anyone who wants to try a different heuristic.


Last updated: Nov 19 2026 at 20:05 UTC