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

Constant arrays are duplicated #5286

Closed
guipublic opened this issue Jun 19, 2024 · 0 comments · Fixed by #5287
Closed

Constant arrays are duplicated #5286

guipublic opened this issue Jun 19, 2024 · 0 comments · Fixed by #5287
Labels
enhancement New feature or request

Comments

@guipublic
Copy link
Contributor

Problem

In the following snippet, the tableY array is duplicated for every read done in the loop 0..6:

struct ScalarField {
    base4_slices: [u8; 63]
}

impl ScalarField {
    fn new() -> Self {
        Self { base4_slices: [0; 63] }
    }
    fn from_field(x: Field) -> Self {
        let bytes: [u8] = x.to_le_radix(4, 63);
        let mut result: Self = ScalarField { base4_slices: [0; 63] };
        for i in 0..63 {
            result.base4_slices[i] = bytes[62 - i];
        }
        result
    }
    fn get(self, idx: u64) -> u8 {
        self.base4_slices[idx]
    }
}

fn mul(scalar: ScalarField) -> Field {
    let mut tableY: [Field; 8] = [0; 8];
    for i in 0..8 {
        tableY[i] = i as Field;
    }
    let mut foo = 0;
    for i in 0..6 {
        let idx: u64 = scalar.get(i) as u64;
        foo += tableY[idx];
    }
    foo
}

fn main(x: Field) -> pub Field {
    let scalar = ScalarField::from_field(x);
    mul(scalar)
}

This generates multiple MemoryInit on ACIR side while we could use only one.

Happy Case

Array is not duplicated

Project Impact

None

Impact Context

No response

Workaround

None

Workaround Description

No response

Additional Context

No response

Would you like to submit a PR for this Issue?

Yes

Support Needs

No response

@guipublic guipublic added the enhancement New feature or request label Jun 19, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Jun 19, 2024
github-merge-queue bot pushed a commit that referenced this issue Jun 19, 2024
# Description

## Problem\*

Resolves #5286 

## Summary\*
Keep track of constant arrays and the array they resolve to in mem2reg,
so that another constant array is not created for the same array.


## 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 Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant