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: Stack overflow iterating over large array #12568

Closed
jeffkdev opened this issue Aug 22, 2022 · 2 comments
Closed

Stage2: Stack overflow iterating over large array #12568

jeffkdev opened this issue Aug 22, 2022 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@jeffkdev
Copy link

Zig Version

0.10.0-dev.3659+e5e6eb983

Steps to Reproduce

Running this code on stage 2 will fail with a stack overflow. Runs correctly without errors on stage 1. I was able to reproduce in with zig run example.zig (Windows).

const MaxParticles: u32 = 512 * 2048;
var pos: [MaxParticles]Vec3 = .{Vec3{}} ** MaxParticles;
var vel: [MaxParticles]Vec3 =  .{Vec3{}} ** MaxParticles;

pub const Vec3 = struct {
    x: f32 = 0,
    y: f32 = 0,
    z: f32 = 0,
};

pub fn main() void {
    const cur_num_particles = 10;
    var i: u32 = 0;
    while (i < cur_num_particles) : (i += 1) {
        vel[i].y -= 1.0;
        pos[i].x += vel[i].x;
        pos[i].y += vel[i].y;
        pos[i].z += vel[i].z;
    }
}

Refactoring the while loop will allow it to run correctly:

    while (i < cur_num_particles) : (i += 1) {
        const particle_pos = &pos[i];
        const particle_vel = &vel[i];
        particle_vel.y -= 1.0;
        particle_pos.x += particle_vel.x;
        particle_pos.y += particle_vel.y;
        particle_pos.z += particle_vel.z;
    }

Also changing MaxParticles to smaller number will allow stage 2 to run it:

const MaxParticles: u32 = 512;

Expected Behavior

Runs and exits without errors

Actual Behavior

Stack overflow.

  • VSCode DebuggerException has occurred: W32/0xC00000FD Exception thrown at 0x00007FF7D9A185C6 in program.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000000, 0x0000001469A03760).
  • Command line output from zig run: Stack Overflow
@jeffkdev jeffkdev added the bug Observed behavior contradicts documented or intended behavior label Aug 22, 2022
@Vexu
Copy link
Member

Vexu commented Aug 22, 2022

This looks like a duplicate of #12215

@Vexu
Copy link
Member

Vexu commented Aug 24, 2022

Confirmed duplicate, main allocas 3 * 3 * 1MiB.

  %1 = alloca [1048576 x %a.Vec3], align 4
  %2 = alloca [1048576 x %a.Vec3], align 4
  %3 = alloca [1048576 x %a.Vec3], align 4

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
Projects
None yet
Development

No branches or pull requests

3 participants