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

infinite loop optimized away unless function is exported #1658

Closed
numsim1415 opened this issue Oct 16, 2018 · 4 comments
Closed

infinite loop optimized away unless function is exported #1658

numsim1415 opened this issue Oct 16, 2018 · 4 comments
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior upstream An issue with a third party project that Zig uses.
Milestone

Comments

@numsim1415
Copy link

export fn square(num: u8) u8 {
             
             //while(true){} // would generate loop
             return eternity(123); 
}

pub fn eternity(bla:u8) u8 {
  while(true)// is optimized away completely
  {}
}

See asm output on goldbold.
As you said during live coding, it seems like the eternal loop is considered to have no side effects and is thus removed.

Expected behaviour would be to refuse compilation

  • as there is no return of an u8
  • as in case of eternity being declared as returning void, an implicit return; at the end of the function cannot be reached
@andrewrk andrewrk added this to the 0.4.0 milestone Oct 16, 2018
@andrewrk andrewrk added the bug Observed behavior contradicts documented or intended behavior label Oct 16, 2018
@andrewrk andrewrk modified the milestones: 0.4.0, 0.5.0 Apr 3, 2019
@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Aug 27, 2019
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Dec 31, 2019
@IridescentRose
Copy link
Contributor

Isn't this a long time LLVM bug present in other languages like C and C++ where an empty infinite loop results in no consequences and thus creates invalid programs? (for example there are many articles like https://stackoverflow.com/questions/59925618/how-do-i-make-an-infinite-empty-loop-that-wont-be-optimized-away). I guess the solution Zig would need for this specific case is adding in a nop op-code inside of the loop so LLVM says it "has an effect" and doesn't generate an invalid program/function.

@IridescentRose
Copy link
Contributor

 export fn square(num: u8) u8 {          
    //while(true){}
    return eternity(123); 
}

pub fn eternity(bla:u8) u8 {
    while(true){
        asm volatile("nop");
    }
}

This code generates as intended as we can see on GodBolt.

@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 30, 2020
@b-ncMN
Copy link

b-ncMN commented Mar 25, 2021

 export fn square(num: u8) u8 {          
    //while(true){}
    return eternity(123); 
}

pub fn eternity(bla:u8) u8 {
    while(true){
        asm volatile("nop");
    }
}

This code generates as intended as we can see on GodBolt.

You probably have forgot to apply the flag -Drelease-safe, this happens to me as well on godbolt using Zig trunk

@andrewrk andrewrk modified the milestones: 0.8.0, 0.8.1 Jun 4, 2021
@andrewrk andrewrk modified the milestones: 0.8.1, 0.9.1 Sep 1, 2021
@Sobeston
Copy link
Contributor

Related: rust-lang/rust#82884 - apparently this was fixed in LLVM 12.

export fn square(num: u8) u8 {
    _ = num;
    return eternity(123);
}

pub fn eternity(bla: u8) u8 {
    _ = bla;
    while (true) {}
}

export fn square2(num: u8) u8 {
    _ = num;
    while (true) {}
}

export fn square3() u8 {
    while (true) {}
}

This code, paired with zig build-obj on my master build of Zig (approx 1 week old) is producing while loops correctly in all three cases, in all four -O flags.

image
(pictured above: ReleaseSmall)

pub fn main() void {
    while (true) {}
}

I've also tested this executable with all four -O flags, and this works on all of them.

@andrewrk andrewrk modified the milestones: 0.9.1, 0.9.0 Nov 20, 2021
@andrewrk andrewrk added backend-llvm The LLVM backend outputs an LLVM IR Module. upstream An issue with a third party project that Zig uses. labels Nov 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior upstream An issue with a third party project that Zig uses.
Projects
None yet
Development

No branches or pull requests

5 participants