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

[BUG]: Compiler Crash with Self Referential Variant #1837

Closed
lsh opened this issue Mar 1, 2024 · 3 comments
Closed

[BUG]: Compiler Crash with Self Referential Variant #1837

lsh opened this issue Mar 1, 2024 · 3 comments
Assignees
Labels
bug Something isn't working mojo-repo Tag all issues with this label

Comments

@lsh
Copy link
Contributor

lsh commented Mar 1, 2024

Bug description

from utils.variant import Variant
from collections.vector import DynamicVector


@value
struct Value(CollectionElement):
    alias Variant = Variant[Float64, DynamicVector[Value]]
    var _variant: Self.Variant

    @always_inline
    fn __moveinit__(inout self, owned rhs: Self):
        self._variant = rhs._variant

    @always_inline
    fn __copyinit__(inout self, rhs: Self):
        self._variant = rhs._variant


fn main():
    pass

outputs:

[68451:5573323:20240229,122117.821876:WARNING crash_report_exception_handler.cc:257] UniversalExceptionRaise: (os/kern) failure (5)
fish: Job 1, 'mojo main.mojo' terminated by signal SIGILL (Illegal instruction)

Note that doing the following does not fix the issue:

from utils.variant import Variant
from collections.vector import DynamicVector


alias T = Variant[Float64, Delegate]


@value
struct Delegate(CollectionElement):
    var _data: DynamicVector[Value]


@value
struct Value(CollectionElement):
    var _variant: T

    @always_inline
    fn __moveinit__(inout self, owned existing: Self):
        self._variant = existing._variant

    @always_inline
    fn __copyinit__(inout self, existing: Self):
        self._variant = existing._variant


fn main():
    pass

It outputs:

recursive reference to declaration

And moving the alias beneath Delegate outputs this error instead:

'Variant' parameter #0 has 'CollectionElement' type, but value has type 'Delegate'

System information

- What OS did you do install Mojo on ? macOS
- Provide version information for Mojo by pasting the output of `mojo -v`:  mojo 24.1.0 (55ec12d6)
- Provide Modular CLI version by pasting the output of `modular -v`: modular 0.4.1 (2d8afe15)
@lsh lsh added bug Something isn't working mojo Issues that are related to mojo labels Mar 1, 2024
@lsh
Copy link
Contributor Author

lsh commented Apr 6, 2024

More minimal version:

from utils.variant import Variant
from memory.unsafe import Pointer


@value
struct Value(CollectionElement):
    alias Variant = Variant[Float64, Pointer[Self]]
    var _variant: Self.Variant


fn main():
    pass

outputs

[69714:5614294:20240406,122210.916482:WARNING crash_report_exception_handler.cc:257] UniversalExceptionRaise: (os/kern) failure (5)
fish: Job 1, 'mojo main.mojo' terminated by signal SIGBUS (Misaligned address error)

with

- modular 0.6.0 (04c05243)
- mojo 2024.4.117 (8f6529b0)

@lattner
Copy link
Collaborator

lattner commented Apr 6, 2024

I spent some time looking into this and it completely twists my brain. The problem is that we're infinitely recursing in the internal LowerLITTypes pass when lowering types and attributes involving Value. The issue is that we're recursively lowering attributes like vtable entries and other stuff that isn't part of hte struct layout itself, because it is recursively reachable through the type references in the !kgen.variant type attribute list, and we never go through a !kgen.pointer which would break the cycle.

I'm not sure the right way to solve this. Perhaps handle vtable entries specially - they are fundamentally pointer like. There is something pointer like that I'm not understanding here. @weiweichen can you investigate this?

@weiweichen
Copy link
Contributor

I spent some time looking into this and it completely twists my brain. The problem is that we're infinitely recursing in the internal LowerLITTypes pass when lowering types and attributes involving Value. The issue is that we're recursively lowering attributes like vtable entries and other stuff that isn't part of hte struct layout itself, because it is recursively reachable through the type references in the !kgen.variant type attribute list, and we never go through a !kgen.pointer which would break the cycle.

I'm not sure the right way to solve this. Perhaps handle vtable entries specially - they are fundamentally pointer like. There is something pointer like that I'm not understanding here. @weiweichen can you investigate this?

Sure, I'll take a look. Also @willghatch, might also need some idea from you about vtables.

@weiweichen weiweichen self-assigned this Apr 7, 2024
@willghatch willghatch self-assigned this Apr 10, 2024
@linear linear bot removed the mojo Issues that are related to mojo label Apr 29, 2024
@ematejska ematejska added the mojo-repo Tag all issues with this label label Apr 29, 2024
@Mogball Mogball closed this as completed May 22, 2024
@Mogball Mogball closed this as completed May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

5 participants