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

Flaw in operation/value/block lifetimes #634

Open
edg-l opened this issue Dec 9, 2024 · 2 comments
Open

Flaw in operation/value/block lifetimes #634

edg-l opened this issue Dec 9, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@edg-l
Copy link
Member

edg-l commented Dec 9, 2024

The following code fails, due to how lifetimes are encoded in melior, I think this should be valid code given MLIR ownership right?

The problem when dealing with BlockRefs and operation result values is they are wrongly tied to the BlockRef itself and not the block the blockref references i think

#[test]
fn lifetimes() {
    let context = Context::new();
    let region = Region::new();
    let blockref = region.append_block(Block::new(&[]));

    fn do_stuff<'ctx: 'region, 'region>(
        context: &'ctx Context,
        _region: &'region Region<'ctx>,
        block: BlockRef<'ctx, 'region>,
    ) -> Value<'ctx, 'region> {
        block.append_operation(arith::constant(
            context,
            IntegerAttribute::new(IntegerType::new(context, 64).into(), 1).into(),
            Location::unknown(context),
        )).result(0).unwrap().into()
    }

    let value = do_stuff(&context, &region, blockref);
}
error[E0515]: cannot return value referencing function parameter `block`
   --> melior/src/ir/block.rs:580:13
    |
580 |               block.append_operation(arith::constant(
    |               ^----
    |               |
    |  _____________`block` is borrowed here
    | |
581 | |                 context,
582 | |                 IntegerAttribute::new(IntegerType::new(context, 64).into(), 1).into(),
583 | |                 Location::unknown(context),
584 | |             )).result(0).unwrap().into()
    | |________________________________________^ returns a value referencing data owned by the current function

Some errors have detailed explanations: E0432, E0515.
For more information about an error, try `rustc --explain E0432`.
@raviqqe
Copy link
Member

raviqqe commented Dec 10, 2024

#522 is part of this issue.

@raviqqe raviqqe added the bug Something isn't working label Dec 10, 2024
@raviqqe
Copy link
Member

raviqqe commented Dec 10, 2024

For anyone seeing this issue, currently the API do not allow modifications of blocks or operations that are moved into the other objects like regions or blocks. So in the example above, we need to modify the block and complete it first, and then append the block to the region.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants