Why is the WASM output so complicated?

When I compile

export fn add(a: u32, b: u32) u32 {
    return a+b;
}

I expect it to output something along the lines of (once ran through wasm2wat)

(module
    (func (param i32) (param i32) (result i32)
        local.get 0
        local.get 1
        i32.add)
    (export "add" (func 0))
)

But instead, I get a huge mess of code that I am pretty sure is completely unnecessary, even on ReleaseSmall. Why is this, and is there any way to get a truly minimal output?

Sounds like you might be passing the wrong optimization parameter and not realizing it (when using zig build and b.standardOptimizeOption, the option is -Doptimize=ReleaseSmall, when using zig build-exe, etc, the option is -OReleaseSmall).

-OReleaseSmall -target wasm32-freestanding works as expected:

10 Likes

Thanks, I’m a dumbass.