Comptime dependant variable

As far as I know this comptime function will be “splitted” in two dedicated functions.
In reality my function is more complicated and longer and I cannot really move the local value inside the “false” version of this function, without a lot of copy paste.
Normally you get a warning “unused variable” from the compiler but not here.

Will the local value in the “true” version of this function be eliminated from the stack? Disappear into nothingness?

fn do_something(comptime calc: bool) u16
{
    var value: u16 = 0;
    if (calc)
    {
        // calculate local value...
        return value; 
    }
    else 
    {
        return some_cached_value;
    }
}

There’s only one way to be sure…

Change true to false and see what you think.

As far as my asm knowledge goes: I think I see too much when false.
A stackframe for the parameters?

1 Like

It looks like it’s still getting called with the passed parameters, but just returns the cached value rather than doing anything with them. I’m not sure why that is.

I think there are a some problems in that godbolt code.

First, -D=ReleaseFast doesn’t do anything. Surprised it isn’t rejected. With -OReleaseFast you’ll see that it compiles down to nothing.

I think this may be a better demo https://godbolt.org/z/4YMvzbYcv

1 Like

Yep, should have been -Doptimize=ReleaseFast. But that’s the zig build version of the flag and it’s using zig build-obj. Good catch.

1 Like