Skip to content

Branches returning errors are not weighted againstΒ #24494

@dweiller

Description

@dweiller

Zig Version

0.15.0-dev.936+fc2c1883b

Steps to Reproduce and Observed Behavior

copy this to repro.zig:

export fn foo(a: usize, b: usize) usize {
    return do_thing(a, b) catch 0;
}

fn do_thing(a: usize, b: usize) !usize {
    if (b == 0) {
        //@branchHint(.unlikely);
        return error.bad;
    }
    return a + b;
}

and run zig build-obj -OReleaseFast -fno-emit-bin --verbose-llvm-ir=repro.ll and then grep repro.ll for branch_weights to observe that there is no branch weight metadata. If you uncomment the @branchHint(.unlikely) there will be branch weight metadata.

Expected Behavior

I expect the above, with or without the @branchHint, to weight against the branch returning an error. Currently it seems that only branches introduced by try/catch are implicitly weighted, but this means that only points that errors propagate to are weighted against by default, while the origin of an error isn't.

I'd also expect that if every execution path in a branch (containing sub-branches) ends up returning an error, then that branch should be weighted against (I'm not sure what llvm will do if you just label every error-returning sub-branch with an .unlikely).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementSolving this issue will likely involve adding new logic or components to the codebase.frontendTokenization, parsing, AstGen, Sema, and Liveness.optimization

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions