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 Refactor): Returning arrays from if statements results in incorrect IR #1486

Closed
Tracked by #1376
jfecher opened this issue Jun 1, 2023 · 4 comments · Fixed by #1767
Closed
Tracked by #1376

(SSA Refactor): Returning arrays from if statements results in incorrect IR #1486

jfecher opened this issue Jun 1, 2023 · 4 comments · Fixed by #1767
Assignees
Labels
bug Something isn't working refactor ssa

Comments

@jfecher
Copy link
Contributor

jfecher commented Jun 1, 2023

Aim

Tried to return an array from an if statement:

fn main(x: bool) {
    let foo = [1, 2, 3];
    let bar = [4, 5, 6];

    // == true is required because of a parsing bug
    let y = if x == true { foo } else { bar };

    constrain y[0] == 12;
}

Expected Behavior

Valid SSA output

Bug

After flattening, the output is:

fn main f1 {
  b0(v0: u1):
    v1 = alloc 3 fields
    store Field 1 at v1
    v3 = add v1, Field 1
    store Field 2 at v3
    v5 = add v1, Field 2
    store Field 3 at v5
    v7 = alloc 3 fields
    store Field 4 at v7
    v9 = add v7, Field 1
    store Field 5 at v9
    v11 = add v7, Field 2
    store Field 6 at v11
    v13 = eq v0, Field 1
    v19 = not v13
    v20 = mul v13, v1    // multiplying the condition times an address is likely invalid.
    v21 = mul v19, v7
    v22 = add v20, v21   // adding the two multiplied addresses together is also likely invalid.
    v23 = load v22
    v24 = eq v23, Field 12
    constrain v24
    return unit 0
}

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

@jfecher jfecher added the bug Something isn't working label Jun 1, 2023
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Jun 1, 2023
@jfecher
Copy link
Contributor Author

jfecher commented Jun 1, 2023

Two ways that I can think of fixing this:

  1. The current SSA form recurses on each element of each array and merges those instead. So we'd have one set of mul, mul, add instructions for each element rather than just the one currently that is being applied to the array address.
  2. Give array addresses actual numerical values. This likely entails managing a shared memory abstraction.

I don't think approach 2 would work for non-constant conditions ince the generated ACIR would have to have some way of retrieving the correct address given a witness.

@joss-aztec
Copy link
Contributor

joss-aztec commented Jun 14, 2023

Test named 6_array is affected

@joss-aztec
Copy link
Contributor

Affects test named 9_conditional

@joss-aztec
Copy link
Contributor

Affects test named regression

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.

3 participants