zig icon indicating copy to clipboard operation
zig copied to clipboard

Wasm: ReleaseSmall fails to inline simple loop compared to ReleaseFast or ReleaseSafe (bigger code)

Open wooster0 opened this issue 1 year ago • 0 comments

Zig Version

0.10.0-dev.2836+2360f8c49

Steps to Reproduce

const x = [_]bool{ false, true, false, true };

extern fn c() void;

export fn b() void {
    for (x) |y| {
        if (y == true)
            c();
    }
}
zig build-lib -dynamic -target wasm32-freestanding -O ReleaseSmall --strip a.zig

Expected Behavior

Output as small as with ReleaseFast or ReleaseSafe:

(module
  (type (;0;) (func))
  (import "env" "c" (func (;0;) (type 0)))
  (func (;1;) (type 0)
    call 0
    call 0)
  (memory (;0;) 1)
  (global (;0;) (mut i32) (i32.const 65536))
  (export "memory" (memory 0))
  (export "b" (func 1)))

Actual Behavior

Much larger and slower than ReleaseFast or ReleaseSafe:

(module
  (type (;0;) (func))
  (import "env" "c" (func (;0;) (type 0)))
  (func (;1;) (type 0)
    (local i32)
    i32.const -4
    local.set 0
    block  ;; label = @1
      loop  ;; label = @2
        local.get 0
        i32.eqz
        br_if 1 (;@1;)
        block  ;; label = @3
          local.get 0
          i32.const 65540
          i32.add
          i32.load8_u
          i32.eqz
          br_if 0 (;@3;)
          call 0
        end
        local.get 0
        i32.const 1
        i32.add
        local.set 0
        br 0 (;@2;)
      end
    end)
  (memory (;0;) 2)
  (global (;0;) (mut i32) (i32.const 65536))
  (export "memory" (memory 0))
  (export "b" (func 1))
  (data (;0;) (i32.const 65536) "\00\01\00\01"))

Seems to be a problem specific to Wasm targets. It works as expected for e.g. x86_64-freestanding.

See also: #12104

wooster0 avatar Jul 18 '22 21:07 wooster0