Skip to content

Propagate purity information for member access to foreign pure variables #12928

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

Conversation

StrongerXi
Copy link
Contributor

Fixes #12927.

@StrongerXi StrongerXi force-pushed the expand-purity-check-for-foreign-constants branch from 51b6878 to 8b4dcdb Compare April 14, 2022 05:07
@StrongerXi
Copy link
Contributor Author

StrongerXi commented Apr 14, 2022

Btw, where does the "constant variable evaluation & substitution" step happen? I wonder if this would affect it, since we are allowing member access in rhs of constant variables now.

@StrongerXi StrongerXi requested review from chriseth and cameel April 14, 2022 05:11
@StrongerXi
Copy link
Contributor Author

Looking deeper, we were handling this correctly for file-level constant because of this line. Member access is similar to identifier, so this should give more confidence to this PR.

@cameel
Copy link
Member

cameel commented Apr 14, 2022

Btw, where does the "constant variable evaluation & substitution" step happen?

See ConstantEvaluator.h and the places where it's used.

@StrongerXi
Copy link
Contributor Author

Btw, where does the "constant variable evaluation & substitution" step happen?

See ConstantEvaluator.h and the places where it's used.

Hmm I did check all the usages but none of them is doing what I was looking for.

Then I dumped the IR and things make sense now. At least at the IR level, it seems that each constant (or all?) variable is turned into a function, and the initialization expression is linearized into IR statements in the function body. I assume constant propagation will clean things up further?

Regardless, this PR shouldn't affect things there.

@StrongerXi StrongerXi dismissed a stale review via c5ce9c0 May 17, 2022 14:09
@StrongerXi StrongerXi force-pushed the expand-purity-check-for-foreign-constants branch from 8b4dcdb to c5ce9c0 Compare May 17, 2022 14:09
@StrongerXi
Copy link
Contributor Author

Rebase.

Copy link
Member

@cameel cameel left a comment

Choose a reason for hiding this comment

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

This looks great. Tests need to be a bit stronger (see my suggestions) but other than that it's a nice enhancement that users will probably appreciate!

Also, please add a changelog entry.

@StrongerXi StrongerXi force-pushed the expand-purity-check-for-foreign-constants branch from c5ce9c0 to 6924460 Compare May 20, 2022 01:40
@chriseth
Copy link
Contributor

Please also add some tests where the constants are used as array lengths of statically-sized arrays, both as forward and as backward references.

@StrongerXi
Copy link
Contributor Author

StrongerXi commented May 23, 2022

Please also add some tests where the constants are used as array lengths of statically-sized arrays, both as forward and as backward references.

I think this is more involved than expected. The logic chain is as follows:

  • To use a constant variable as array lengths, DeclarationTypeChecker::endVisit(ArrayTypeName) requires the variable to either (1) already be a rational number, e.g., X = 42, or (2) can be evaluated to a rational number by ConstantEvaluator.
  • To use X = L.INT as array length, we must go with (2).
  • ConstantEvaluator currently doesn't evaluate MemberAccess. But to support it, we must check for circular dependencies in constant declarations first, which is currently done in PostTypeChecker.

I think this reveals a larger issue than this PR is concerned -- We probably want to augment ConstantEvaluator to evaluate more things like member access, but that eventually entails moving constant circular dependence check to earlier places in the compilation stack.

Currently the compilation stack is a bit awkward for constant evaluation, i.e.,

...
DeclarationTypeChecker (use of constant variable as array size is checked)
...
TypeChecker (purity information is propagated, e.g., this PR)
...
PostTypeChecker (constant circular dependence is checked)
...

I think the logics in parenthesis should be reversed...?

@StrongerXi StrongerXi dismissed a stale review via a880a6a May 23, 2022 04:09
@StrongerXi StrongerXi force-pushed the expand-purity-check-for-foreign-constants branch 2 times, most recently from a880a6a to 223adaf Compare May 23, 2022 04:45
@StrongerXi
Copy link
Contributor Author

Rebase.

@chriseth
Copy link
Contributor

Sorry, I should have clarified: Please add tests so that we see that it does not catastrophically fail. I did not mean to implement this feature for the constant evaluator.

And you are right, the constant evaluator is not at the right place, we have a larger issue planned to extend it.

@StrongerXi
Copy link
Contributor Author

Sorry, I should have clarified: Please add tests so that we see that it does not catastrophically fail. I did not mean to implement this feature for the constant evaluator.

And you are right, the constant evaluator is not at the right place, we have a larger issue planned to extend it.

Got it. Done.

@StrongerXi StrongerXi force-pushed the expand-purity-check-for-foreign-constants branch from 223adaf to dae3218 Compare May 23, 2022 17:07
cameel
cameel previously approved these changes May 23, 2022
Copy link
Member

@cameel cameel left a comment

Choose a reason for hiding this comment

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

One last thing.

It's minor enough that I'm approving this already but I really think these tests need to be separate.

@StrongerXi StrongerXi force-pushed the expand-purity-check-for-foreign-constants branch from dae3218 to 386fb5f Compare May 23, 2022 17:45
@nishant-sachdeva nishant-sachdeva dismissed a stale review via 58d9dc4 June 10, 2022 10:11
@nishant-sachdeva nishant-sachdeva force-pushed the expand-purity-check-for-foreign-constants branch 2 times, most recently from 58d9dc4 to 484df3f Compare June 13, 2022 04:01
@nishant-sachdeva nishant-sachdeva self-assigned this Jun 13, 2022
@nishant-sachdeva nishant-sachdeva force-pushed the expand-purity-check-for-foreign-constants branch 2 times, most recently from 5ba2d9c to 4a0acfa Compare June 15, 2022 10:42
cameel
cameel previously approved these changes Jun 15, 2022
Copy link
Member

@cameel cameel left a comment

Choose a reason for hiding this comment

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

It's good. I'd merge this already but we have just released 0.8.15 so the changelog entry needs to be moved.

@nishant-sachdeva nishant-sachdeva dismissed stale reviews from ghost and cameel via 3b2c6de June 16, 2022 11:42
@nishant-sachdeva nishant-sachdeva force-pushed the expand-purity-check-for-foreign-constants branch from 4a0acfa to 3b2c6de Compare June 16, 2022 11:42
Copy link
Member

@ekpyron ekpyron left a comment

Choose a reason for hiding this comment

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

Approving, partly based on @cameel's earlier approval.

@nishant-sachdeva nishant-sachdeva merged commit b80f4ba into ethereum:develop Jun 16, 2022
@StrongerXi StrongerXi deleted the expand-purity-check-for-foreign-constants branch July 30, 2022 05:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Should support referring to foreign-constants as compile-time constants
5 participants