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

fix: properly propagate constant value to assigned var in mast phase #183

Merged
merged 8 commits into from
Sep 23, 2024

Conversation

katat
Copy link
Collaborator

@katat katat commented Sep 11, 2024

This PR allows propagating constant values in assignment statement.

Sometimes we want to propagate a constant value from the right hand side to a variable in assignment, eg:

let var = CST + 1 // this PR fix this: constant value can't be propagated to var

Meanwhile, we need to ensure it won't propagate to the constant value and treat the mutable variable as a constant. Otherwise, in some cases, the mutable variable will mistakenly folded as a constant. eg:

fn accumulate_mut(const LEN: Field) -> Field {
    // shouldn't fold mutable variables as constants
    let mut zz = LEN; // assign a constant value
    for ii in 0..3 {
        zz = zz + zz; // if zz is treated as a constant, mutable variable zz will be mistakenly folded as a constant
    }
    return zz;
}

The reason it needs to fold the constant values during mast phase is because during initializing a generic function, the constants need to be folded to resolve the concrete value. eg:

fn last(arr: [[Field; NN]; 3]) -> Field {
    let mut newarr = [0; NN * 3]; // <-- NN * 3 has to be folded
    ...

@katat katat requested a review from mimoo September 11, 2024 08:56
@katat katat marked this pull request as ready for review September 11, 2024 08:57
@katat
Copy link
Collaborator Author

katat commented Sep 11, 2024

On a related topic, I am wondering if we should allow either const or non-const field for condition branches, eg:

fn from_bits(bits: [Bool; LEN]) -> Field {
    let mut lc1 = 0;
    let mut e2 = 1; // non-constant
    let zero = 0;  // constant

    for index in 0..LEN {
        // the current type check throws error from the if then else condition check
        // branches should allow either const or non const field?
        lc1 = lc1 + if bits[index] {e2} else {zero};
        e2 = e2 + e2;
    }
    return lc1;
}

Copy link
Contributor

@mimoo mimoo left a comment

Choose a reason for hiding this comment

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

As discussed I think this should be a temporary solution and we should create an issue/discussion on how to make this kind of code work :o

@@ -11,6 +11,15 @@ fn join(const LEN: Field, arr1: [Field; LLEN], arr2: [Field; RLEN]) -> [Field; L
return arr;
}

fn accumulate_mut(const LEN: Field) -> Field {
// shouldn't fold mutable variables as constants
Copy link
Contributor

Choose a reason for hiding this comment

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

Cryptic comment that will confuse us later when we forget what this was about :o

@katat
Copy link
Collaborator Author

katat commented Sep 20, 2024

wrote up an issue to better address this problem: #188

@katat
Copy link
Collaborator Author

katat commented Sep 23, 2024

I reverted the changes, as it is non issue any more because they are better fixed in another PR. See the comment: #188 (comment)

So this PR only focus on fixing:

let var = CST + 1 // this PR fix this: constant value can't be propagated to var

And update the relevant tests to cover them.

@katat katat merged commit 09451d3 into main Sep 23, 2024
1 check passed
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