modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo
Other
21.79k stars 2.53k forks source link

[BUG] While loop logic causes seg fault. #2696

Open johnsoez4 opened 2 weeks ago

johnsoez4 commented 2 weeks ago

Bug description

This issue started a few days ago with Mojo nightly version 2024.5.1002. Edit 2024-05-28:

In the code shown below, function app_run causes a seg fault, but app_close alone compiles and works properly. The logic to exit each while loop is essentially identical but inverted. I presume that both functions should compile and work properly. Important: I just confirmed that this issue does NOT exist with Mojo stable version 24.3.0 (9882e19d). stack_dump.txt

Steps to reproduce

fn main() raises:
    app_close()
    app_run()

fn app_close():
    # This compiles and works.
    var app_close = False
    var loops = 10
    while not app_close:
        loops -= 1
        if loops == 0:
            app_close = True
    print("app_close: Success!")

fn app_run():
    # This does not compile (seg fault).
    var app_run = True
    var loops = 10
    while app_run:
        loops -= 1
        if loops == 0:
            app_run = False
    print("app_run: Success!")

System information

Ubuntu: 24.04 LTS
Python: 3.12.3
Mojo Nightly: 2024.5.1619 (bd52708e)
Modular: 0.7.4 (df7a9e8b)
johnsoez4 commented 1 week ago

No problem (confirmed OK on 2024-05-28)

Problem confirmed:

JoeLoser commented 1 week ago

@ematejska @jopamer also asked on discord at https://discord.com/channels/1087530497313357884/1224434323193594059/1242260289060077589

johnsoez4 commented 3 days ago

@ematejska, @JoeLoser , @mzaks, @jopamer, @rd4com, @jdiggins

Here's an interesting clue to the mystery. It works when the line while app_run: is changed to while not not app_run:.

fn app_run():
    # This does not compile (seg fault).
    var app_run = True
    var loops = 10

    while not not app_run:  # This works.
    # while app_run:  # This does not work.
        loops -= 1
        if loops == 0:
            app_run = False
    print("app_run: Success!")
JoeLoser commented 3 days ago

This is a compiler bug from one of our passes. It's hitting an assertion in our compiler stack inside LLVM:

Assertion failed: (detail::isPresent(Val) && "dyn_cast on a non-existent value"), function dyn_cast, file Casting.h, line 662.FYI  

FYI @jeff

ematejska commented 3 days ago

Thanks for filing! We'll take a look.

ematejska commented 3 days ago

Duplicate filed here: https://github.com/modularml/mojo/issues/2821

Reduced use-case from there:

fn main():
    var loop = True
    while loop:
        loop=False