Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stage2: high memory use with trivial loops at comptime #12493

Open
tauoverpi opened this issue Aug 19, 2022 · 4 comments
Open

stage2: high memory use with trivial loops at comptime #12493

tauoverpi opened this issue Aug 19, 2022 · 4 comments
Labels
bug Observed behavior contradicts documented or intended behavior enhancement Solving this issue will likely involve adding new logic or components to the codebase. frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@tauoverpi
Copy link
Contributor

Zig Version

0.10.0-dev.3620+7d674d5fb

Steps to Reproduce

zig test bug.zig -fno-stage1

fn foo(comptime k: u32) u32 {
    var x: u32 = k;
    while (x +% k > x) : (x +%= 1) {}
    return x;
}

test {
    @setEvalBranchQuota(0xffff_ffff);
    comptime foo(5);
}

Expected Behavior

Constant memory use for the loop given that the loop is trivial.

Actual Behavior

wasted-ram

Reached 20G in memory use (had to stop it before it reached OOM)

@tauoverpi tauoverpi added the bug Observed behavior contradicts documented or intended behavior label Aug 19, 2022
@nektro
Copy link
Contributor

nektro commented Aug 19, 2022

foo will infinite loop for any k other than zero. your high @setEvalBranchQuota tells the compiler to continue on

@tauoverpi tauoverpi changed the title stage2: memory high memory use with trivial loops at comptime stage2: high memory use with trivial loops at comptime Aug 19, 2022
@tauoverpi
Copy link
Contributor Author

@nektro the @setEvalBranchQuota is intentional to display the undesired behaviour and the loop condition doesn't actually matter here as the issue is the compiler filling up memory similar to #3461

@Vexu Vexu added enhancement Solving this issue will likely involve adding new logic or components to the codebase. frontend Tokenization, parsing, AstGen, Sema, and Liveness. labels Aug 19, 2022
@Vexu Vexu added this to the 0.12.0 milestone Aug 19, 2022
@sagehane
Copy link
Contributor

Since the reproducible code above has the same issue outlined in #12580, I propose a simpler version of foo to reproduce the issue:

fn foo() void {
    const u32_max = @import("std").math.maxInt(u32);

    @setEvalBranchQuota(u32_max);
    var x: u32 = 0;
    while (x != u32_max) : (x +%= 1) {}
}

seandewar added a commit to seandewar/challenge-solutions that referenced this issue Dec 11, 2022
The OOM issues from comptime seems to be caused just by looping 10k times.
This seems to be a known issue: ziglang/zig#12493.
@shwqf
Copy link
Contributor

shwqf commented Dec 18, 2022

I think the potential reasons are:

a) Use .constant air to pass comptime value. e.g. calls addConstant in analyzeLoad, which leads to air_instructions become bigger and bigger.

b) infinite stacks.

b) is unavoidable, but a) can be improved...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior enhancement Solving this issue will likely involve adding new logic or components to the codebase. frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Projects
None yet
Development

No branches or pull requests

5 participants