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

SSA with binary operation with operands of type reference #1899

Closed
sirasistant opened this issue Jul 10, 2023 · 1 comment · Fixed by #1904
Closed

SSA with binary operation with operands of type reference #1899

sirasistant opened this issue Jul 10, 2023 · 1 comment · Fixed by #1904
Labels
bug Something isn't working refactor ssa

Comments

@sirasistant
Copy link
Contributor

sirasistant commented Jul 10, 2023

Aim

The references test (tests::prove_and_verify_test_data_ssa_refactor_references):

fn main() {
    let mut x = 2;
    add1(&mut x);
    assert(x == 3);

    let mut s = S { y: x };
    s.add2();
    assert(s.y == 5);

    // Test that normal mutable variables are still copied
    let mut a = 0;
    mutate_copy(a);
    assert(a == 0);

    // Test something 3 allocations deep
    let mut nested_allocations = Nested { y: &mut &mut 0 };
    add1(*nested_allocations.y);
    assert(**nested_allocations.y == 1);

    // Test nested struct allocations with a mutable reference to an array.
    let mut c = C {
        foo: 0,
        bar: &mut C2 {
            array: &mut [1, 2],
        },
    };
    *c.bar.array = [3, 4];
    assert(*c.bar.array == [3, 4]);
}

should compile even if x is passed as parameter (reducing SSA simplifications before ACIR gen)

fn main(mut x: Field) {
    add1(&mut x);

Expected Behavior

The test should pass

Bug

The application panicked (crashed).
Message:  internal error: entered unreachable code: References are invalid in binary operations
Location: crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs:576

The issue seems to be in the add2 function:

struct S { y: Field }

impl S {
    fn add2(&mut self) {
        self.y += 2;
    }
}

That seems to generate SSA with a binary op with parameters of Type::Reference.

To Reproduce

Installation Method

None

Nargo Version

No response

Additional Context

No response

Would you like to submit a PR for this Issue?

No

Support Needs

No response

@sirasistant sirasistant added the bug Something isn't working label Jul 10, 2023
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Jul 10, 2023
@sirasistant sirasistant changed the title SSA with binary operation on parameters of type reference SSA with binary operation with operands of type reference Jul 10, 2023
jfecher referenced this issue Jul 10, 2023
* fix: fix bug in register restores

* feat: implement references in brillig
@jfecher
Copy link
Contributor

jfecher commented Jul 10, 2023

I was thinking this may be an issue with a dereference operation not getting inserted somewhere but the generated SSA code actually looks correct to me:

After Dead Instruction Elimination:
acir fn main f2 {
  b0(v0: Field):
    v14 = add v0, Field 2
    v15 = eq v14, Field 5
    constrain v15
    return 
}

It may be that x is registered as having a reference type rather than a Field type.
Nevermind, clearly I missed the large v0 :Field at the beginning.
This might be an acir-gen error in tracking values incorrectly or failing to call dfg.resolve(value_id).

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

Successfully merging a pull request may close this issue.

2 participants