#Which aspects of Zig code hinder inlined optimizations?

1 messages · Page 1 of 1 (latest)

neon cosmos
#

As an example, why would the codegen be different for the following two functions (exec with add_prim passed to it, and add)?

fn exec(x: f32, y: f32, comptime f: anytype) f32 {
    return f(x, y);
}

inline fn add_prim(x: f32, y: f32) f32 {
    return x+y;
}

fn add(x: f32, y: f32) f32 {
    return x+y;
}

test {
    _ = exec(1, 2, add_prim);
    _ = add(1, 2);
}

add

example.add:
        push    rbp
        mov     rbp, rsp
        push    rax
        vmovss  dword ptr [rbp - 8], xmm0
        vmovss  dword ptr [rbp - 4], xmm1
        vaddss  xmm0, xmm0, xmm1
        add     rsp, 8
        pop     rbp
        ret

exec

example.exec__anon_3065:
        push    rbp
        mov     rbp, rsp
        sub     rsp, 16
        vmovss  dword ptr [rbp - 16], xmm0
        vmovss  dword ptr [rbp - 12], xmm1
        vmovss  dword ptr [rbp - 8], xmm0
        vmovss  dword ptr [rbp - 4], xmm1
        vaddss  xmm0, xmm0, xmm1
        add     rsp, 16
        pop     rbp
        ret
finite vortex
neon cosmos
#

Odd. I used godbolt and got the same discrepancy in both ReleaseSmall and ReleaseFast.

finite vortex
#

you might have used -Doptimize=X instead of -OX, which will just do debug because -Doptimize isnt a valid flag for build-exe, just for build scripts

neon cosmos
#

Ahh, something like that. For some reason the flags in godobolt defaulted to -Orelease-fast when I looked at it, and compilation succeeded, so I assumed everything was fine. Tossing in -OReleaseFast makes a huge difference.

#

Thank you.