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

chore(ssa refactoring): Support basic recursive functions #1387

Merged
merged 2 commits into from
May 24, 2023

Conversation

jfecher
Copy link
Contributor

@jfecher jfecher commented May 23, 2023

Description

Problem

The SSA Refactoring could support recursive functions with minimal changes, but does not.

Summary

Supports basic recursive functions in the ssa refactoring. This was a ~6 line change (all other lines are for the new test). This supports only "basic" recursive functions that are able to be evaluated during inlining because of constant folding. We could make this more complex in the future but this should support the majority of use cases as-is. It is also still worth noting that this recursion must (like loops) terminate during compilation so calling a recursive function like factorial on a private input will still result in hitting the recursion limit.

Example

Before the code:

fn main() -> pub Field {
    factorial(5)
}

fn factorial(x: Field) -> Field {
    if x < 1 {
        1
    } else {
        x * factorial(x - 1)
    }
}

Will hit the recursion limit in the ssa refactoring (likewise in the current ssa we issue an error stating recursive functions are not supported). After this PR it will inline factorial correctly and main will become equivalent to:

fn main() -> pub Field {
    120
}

Documentation

  • This PR requires documentation updates when merged.

    • I will submit a noir-lang/docs PR.
    • I will request for and support Dev Rel's help in documenting this PR.

Additional Context

PR Checklist*

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

@jfecher jfecher requested review from kevaundray and joss-aztec May 23, 2023 17:20
Copy link
Contributor

@joss-aztec joss-aztec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@joss-aztec joss-aztec added this pull request to the merge queue May 24, 2023
Merged via the queue into master with commit cceaca0 May 24, 2023
@joss-aztec joss-aztec deleted the jf/ssa-recursive-fn-test branch May 24, 2023 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants