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

DIE removes reference counters for constant arrays #6121

Closed
sirasistant opened this issue Sep 21, 2024 · 1 comment · Fixed by #6122
Closed

DIE removes reference counters for constant arrays #6121

sirasistant opened this issue Sep 21, 2024 · 1 comment · Fixed by #6122
Labels
bug Something isn't working

Comments

@sirasistant
Copy link
Contributor

Aim

Compile the following program:

unconstrained fn main(sorted_index: [u32; 2]) {
    let original = [
        55,
        11
    ];

    let mut sorted = original;

    for i in 0..2 {
        let index = sorted_index[i];
        let value = original[index];
        sorted[i] = value;
    }

    assert_eq(sorted[1], 55);
}

Expected Behavior

With this Prover.toml:

sorted_index = ["1", "0"]

Program execution should pass

Bug

The program doesn't execute correctly, since DIE removes the inc_rc on the constant array:


After Constraint Folding:
brillig fn main f0 {
  b0(v0: [u32; 2]):
    inc_rc [Field 55, Field 11]
    inc_rc [Field 55, Field 11]
    v5 = allocate
    store [Field 55, Field 11] at v5
    jmp b1(u32 0)
  b1(v1: u32):
    v8 = lt v1, u32 2
    jmpif v8 then: b3, else: b2
  b3():
    v13 = array_get v0, index v1
    v14 = array_get [Field 55, Field 11], index v13
    v15 = load v5
    v16 = array_set v15, index v1, value v14
    v17 = add v1, u32 1
    store v16 at v5
    jmp b1(v17)
  b2():
    v9 = load v5
    v11 = array_get v9, index u32 1
    v12 = eq v11, Field 55
    constrain v11 == Field 55
    return 
}

After Dead Instruction Elimination:
brillig fn main f0 {
  b0(v0: [u32; 2]):
    v2 = allocate
    store [Field 55, Field 11] at v2
    jmp b1(u32 0)
  b1(v1: u32):
    v8 = lt v1, u32 2
    jmpif v8 then: b3, else: b2
  b3():
    v12 = array_get v0, index v1
    v13 = array_get [Field 55, Field 11], index v12
    v14 = load v2
    v15 = array_set v14, index v1, value v13
    v16 = add v1, u32 1
    store v15 at v2
    jmp b1(v16)
  b2():
    v9 = load v2
    v11 = array_get v9, index u32 1
    constrain v11 == Field 55
    return 
}

After Array Set Optimizations:
brillig fn main f0 {
  b0(v0: [u32; 2]):
    v2 = allocate
    store [Field 55, Field 11] at v2
    jmp b1(u32 0)
  b1(v1: u32):
    v8 = lt v1, u32 2
    jmpif v8 then: b3, else: b2
  b3():
    v12 = array_get v0, index v1
    v13 = array_get [Field 55, Field 11], index v12
    v14 = load v2
    v15 = array_set v14, index v1, value v13
    v16 = add v1, u32 1
    store v15 at v2
    jmp b1(v16)
  b2():
    v9 = load v2
    v11 = array_get v9, index u32 1
    constrain v11 == Field 55
    return 
}

So when executing v16 = array_set v15, index v1, value v14 we overwrite in place the constant array.

To Reproduce

Workaround

None

Workaround Description

No response

Additional Context

No response

Project Impact

None

Blocker Context

No response

Nargo Version

No response

NoirJS Version

No response

Proving Backend Tooling & Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

@sirasistant sirasistant added the bug Something isn't working label Sep 21, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Sep 21, 2024
@sirasistant
Copy link
Contributor Author

Another option is that we can initialize constant arrays with an RC that is not 1 so they are never mutated

github-merge-queue bot pushed a commit that referenced this issue Sep 23, 2024
# Description

## Problem\*

Resolves #6121

## Summary\*

Die was not considering constants as used. As a result, the reference
counter operations on constants were removed.
This created a correctness issue.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** 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.
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Sep 23, 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
Projects
Status: ✅ Done
Development

Successfully merging a pull request may close this issue.

1 participant