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

IncRc not issued on array reassigns #3795

Closed
sirasistant opened this issue Dec 13, 2023 · 6 comments · Fixed by #4210 or #4472
Closed

IncRc not issued on array reassigns #3795

sirasistant opened this issue Dec 13, 2023 · 6 comments · Fixed by #4210 or #4472
Assignees
Labels
brillig Unconstrained functions / brillig IR bug Something isn't working ssa

Comments

@sirasistant
Copy link
Contributor

Aim

A program like this one:

unconstrained fn main(items_to_update: u64, index: u64) {
    let mut arr = [0; 5000];
    let mut mid_change = [0; 5000];

    for i in 0..items_to_update {
        if i == 2500 {
            mid_change = arr;
        }
        arr[i] = 27;
    }

    assert_eq(arr[index], 27);
    assert(mid_change[index] != 27);
}

With this prover.toml

items_to_update = 4000
index = 3000

Fails the assertions

Expected Behavior

It should pass with the given inputs

Bug

The SSA generated is missing an inc_rc in mid_change = arr

...
  b1(v7: u64):
    v86 = lt v7, v0
    jmpif v86 then: b2, else: b3
  b2():
    v93 = eq v7, u64 2500
    jmpif v93 then: b4, else: b5
  b4():
    v97 = load v84
    store v97 at v85
    jmp b5()
  b5():
    v94 = load v84
    v95 = array_set v94, index v7, value Field 27
    v96 = add v7, Field 1
    store v95 at v84
    jmp b1(v96)
  b3():
    v87 = load v84
    v88 = array_get v87, index v1
    constrain v88 == Field 27
    v89 = load v85
    v90 = array_get v89, index v1
    v91 = eq v90, Field 27
    v92 = not v91
    constrain v92 == u1 1
    return 
}

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 Dec 13, 2023
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Dec 13, 2023
@jfecher jfecher added ssa brillig Unconstrained functions / brillig IR P-HIGH labels Dec 13, 2023
@jfecher
Copy link
Contributor

jfecher commented Dec 13, 2023

Labelling this P-HIGH since it causes the code to silently change behavior

@kevaundray kevaundray added this to the 0.24.0 milestone Jan 15, 2024
@kevaundray
Copy link
Contributor

What is the status of this issue?

@jfecher
Copy link
Contributor

jfecher commented Jan 17, 2024

@kevaundray it is still open

@jfecher
Copy link
Contributor

jfecher commented Jan 26, 2024

I've added the missing IncRc instruction on branch jf/fix-incrc-assign, yet the bug is still present. Any ideas why this may be @sirasistant?

@Savio-Sou Savio-Sou moved this from 📋 Backlog to 🏗 In progress in Noir Feb 2, 2024
github-merge-queue bot pushed a commit that referenced this issue Feb 5, 2024
#4210)

# Description

## Problem\*

Resolves #3795

## Summary\*

`Instruction::IncrementRc` in Brillig actually only increments the
reference count on a copy of an array's metadata if the array was loaded
from a reference (while the loaded array is a reference rather than a
copy). This is suboptimal since it was meant to be shared across
references to the array. This is what caused the original issue where
mutating the reference saw a different reference count and thought it
was safe to mutate without copying first.

I've added a somewhat hacky check in the method to increment reference
counts to check if the array was loaded from a reference. If so, we load
a fresh value, increment rc on that, and re-store it to update the
metadata in the original array.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.

---------

Co-authored-by: Maxim Vezenov <[email protected]>
@github-project-automation github-project-automation bot moved this from 🏗 In progress to ✅ Done in Noir Feb 5, 2024
@sirasistant sirasistant reopened this Feb 12, 2024
@sirasistant
Copy link
Contributor Author

This got reverted #4332 because of a regression that made a critical path in aztec-packages to blow up in complexity, never ending. I've added an e2e test in the revert PR of that path.
If the issue is related with rc being a separate value and not together with the items in the array reference, I could try to tackle that @jfecher

@jfecher
Copy link
Contributor

jfecher commented Feb 12, 2024

@sirasistant I'll test an older commit of the reverted PR which had a different approach first.

@Savio-Sou Savio-Sou moved this from ✅ Done to 🏗 In progress in Noir Feb 15, 2024
@Savio-Sou Savio-Sou removed this from the 0.24 milestone Mar 4, 2024
github-merge-queue bot pushed a commit that referenced this issue Mar 11, 2024
# Description

## Problem\*

Resolves #3795

## Summary\*

This is an older version of #4210
which undoes the change in
d331ee2
due to a regression #4332.

This PR is not yet confirmed to work since I do not have a test case for
it! @sirasistant, do you mind seeing if this fixes the regression issue?

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [ ] I have tested the changes locally.
- [ ] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
@github-project-automation github-project-automation bot moved this from 🏗 In progress to ✅ Done in Noir Mar 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
brillig Unconstrained functions / brillig IR bug Something isn't working ssa
Projects
Archived in project
4 participants