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

Failed determining slice length at compile time without any runtime conditionals #4736

Closed
sirasistant opened this issue Apr 8, 2024 · 1 comment · Fixed by #4779
Closed
Labels
bug Something isn't working

Comments

@sirasistant
Copy link
Contributor

Aim

Compile code like this one:

trait Serialize<N> {
    fn serialize(self) ->[Field; N];
}

struct Point {
    x: Field,
    y: Field,
}

impl Serialize<2> for Point {
    fn serialize(self) -> [Field; 2] {
        [self.x, self.y]
    }
}

fn sum(values: [Field]) -> Field {
    let mut sum = 0;
    for value in values {
        sum = sum + value;
    }
    sum
}

fn main(points: [Point; 3]) -> pub Field {
    let mut serialized_points = &[];
    for point in points {
        serialized_points = serialized_points.append(point.serialize().as_slice());
    }
    sum(serialized_points)
}

Expected Behavior

Should figure out that the length of serialized_points is 6 and compile succesfully

Bug

error: Could not determine loop bound at compile-time
   ┌─ /mnt/user-data/alvaro/constructor/src/main.nr:18:18
   │
18 │     for value in values {
   │                  ------ If attempting to fetch the length of a slice, try converting to an array. Slices only use dynamic lengths.
   │
   = Call stack:
     1. /mnt/user-data/alvaro/constructor/src/main.nr:29:5
     2. /mnt/user-data/alvaro/constructor/src/main.nr:18:18

Aborting due to 1 previous error

Seems like the length is known at the end of the optimization pipeline, since it figures it out if we just return it

fn main(points: [Point; 3]) -> pub Field {
    let mut serialized_points = &[];
    for point in points {
        serialized_points = serialized_points.append(point.serialize().as_slice());
    }
    serialized_points.len() as Field
}

It indeed finds out that the length is 6

After Array Set Optimizations:
acir(inline) fn main f0 {
  b0(v0: [Field, Field; 3]):
    return Field 6
}

To Reproduce

Project Impact

Nice-to-have

Impact Context

No response

Workaround

None

Workaround Description

No response

Additional Context

No response

Installation Method

None

Nargo Version

No response

NoirJS 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 Apr 8, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Apr 8, 2024
@TomAFrench
Copy link
Member

Seems like the length is known at the end of the optimization pipeline

Yeah, this is the issue. In order for the compiler to know that serialized_points.len() == 6, it needs the first for-loop to be fully unrolled, flattened and mem2reg'ed which isn't helpful when we need to know this during the loop unrolling pass immediately.

The obvious solution is that we iteratively unroll loops, performing these later passes to propagate knowledge necessary for later loops but this doesn't seem like a trivial fix.

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

## Problem\*

Resolves #4736

## Summary\*

Instead of trying to unroll loops once, adds a meta pass that tries to
unroll as many times as necessary, simplifying and doing mem2reg between
unrolls.
- For the cases where the program unrolls succesfully at the first try,
no compile time overhead is created
- For the cases where the program does contain an unknown at compile
time loop bound, it'll try one more time before failing, since the stop
condition is that "this unroll retry generated the same amount of errors
as the previous one"
- For the cases where the program doesn't contain an unknown at compile
time loop bound, instead of failing it'll do as many unrolls as
necessary to unroll the loop.

## 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.

---------

Co-authored-by: jfecher <[email protected]>
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Apr 11, 2024
sirasistant added a commit to AztecProtocol/aztec-packages that referenced this issue Apr 12, 2024
After noir-lang/noir#4736 was fixed, now this
is possible :)

---------

Co-authored-by: Alvaro Rodriguez <[email protected]>
AztecBot added a commit that referenced this issue Apr 12, 2024
…5703)

After #4736 was fixed, now this
is possible :)

---------

Co-authored-by: Alvaro Rodriguez <[email protected]>
AztecBot added a commit that referenced this issue Apr 12, 2024
…5703)

After #4736 was fixed, now this
is possible :)

---------

Co-authored-by: Alvaro Rodriguez <[email protected]>
AztecBot pushed a commit to AztecProtocol/aztec-nr that referenced this issue Apr 13, 2024
After noir-lang/noir#4736 was fixed, now this
is possible :)

---------

Co-authored-by: Alvaro Rodriguez <[email protected]>
superstar0402 added a commit to superstar0402/aztec-nr that referenced this issue Aug 16, 2024
After noir-lang/noir#4736 was fixed, now this
is possible :)

---------

Co-authored-by: Alvaro Rodriguez <[email protected]>
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
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants