-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add mechanism to instruct VM's global analysis pass to try to const evaluate certain parts of a program before global analysis runs. #50473
Comments
It would be nice to reuse/extend the existing constant evaluator which is used in the CFE, if that's possible. Note that there's also If we're doing this constant evaluation before TFA, then it would be useful to do that before or during unreachable code elimination pass which runs before TFA. That pass removes code guarded by constant conditions. |
Is P1 the right priority for this issue? According to our new issue triage policy we would need updates on a weekly basis on all P1 issues. |
@sstrickl is actively working on this and this is a high priority task. It may make sense to post regular updates (so interested stakeholders know how it's progressing) |
Namely, if the tested expression for a switch statement is constant, then we can remove any constant cases where the constants differ, and if all but a single case is removed, we can replace the switch with the case body. If constant functions are not enabled, then getters annotated with @pragma("vm:platform-const") are still evaluated with the constant function evaluation machinery, but only those and no others (including any functions called within an annotated getter). This way, functions can be annotated with @pragma("vm:platform-const") without having to rewrite them to be a single returned expression. TEST=pkg/vm/test/transformations/unreachable_code_elimination pkg/vm/test/transformations/vm_constant_evaluator Issue: #50473 Issue: #31969 Change-Id: Ie290d2f1f469326238d66c3d9631f8e696685ff0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/332760 Commit-Queue: Tess Strickland <sstrickl@google.com> Reviewed-by: Chloe Stefantsova <cstefantsova@google.com> Reviewed-by: Alexander Markov <alexmarkov@google.com>
…tor." This reverts commit 92bf76d. Reason for revert: broken tests on AOT trybots Original change's description: > [pkg/vm] Handle switch statements in unreachable code eliminator. > > Namely, if the tested expression for a switch statement is constant, > then we can remove any constant cases where the constants differ, > and if all but a single case is removed, we can replace the switch > with the case body. > > If constant functions are not enabled, then getters annotated with > @pragma("vm:platform-const") are still evaluated with the constant > function evaluation machinery, but only those and no others (including > any functions called within an annotated getter). This way, functions > can be annotated with @pragma("vm:platform-const") without having to > rewrite them to be a single returned expression. > > TEST=pkg/vm/test/transformations/unreachable_code_elimination > pkg/vm/test/transformations/vm_constant_evaluator > > Issue: #50473 > Issue: #31969 > Change-Id: Ie290d2f1f469326238d66c3d9631f8e696685ff0 > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/332760 > Commit-Queue: Tess Strickland <sstrickl@google.com> > Reviewed-by: Chloe Stefantsova <cstefantsova@google.com> > Reviewed-by: Alexander Markov <alexmarkov@google.com> Issue: #50473 Issue: #31969 Change-Id: I4340874793da19ce892a19bcf0542c24e0ee65d4 No-Presubmit: true No-Tree-Checks: true No-Try: true Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/334600 Commit-Queue: Siva Annamalai <asiva@google.com> Auto-Submit: Tess Strickland <sstrickl@google.com> Reviewed-by: Siva Annamalai <asiva@google.com> Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
…tor." This is a reland of commit 92bf76d In the original CL, the changes to the UCE assumed all SwitchStatements were exhaustive. Now if a SwitchStatement isn't explicitly or implicitly (via a default case) exhaustive and no case matches the tested constant, the SwitchStatement is properly removed. In addition, if a guaranteed to match case is found, more than one cases remain (thus the SwitchStatement is not removed or replaced with the single case's body), and the default case is removed, then the resulting SwitchStatement is marked as explicitly exhaustive, as this serves as a signal to backends that they do not need to handle the possibility of no case matching in the absence of a default case. Original change's description: > [pkg/vm] Handle switch statements in unreachable code eliminator. > > Namely, if the tested expression for a switch statement is constant, > then we can remove any constant cases where the constants differ, > and if all but a single case is removed, we can replace the switch > with the case body. > > If constant functions are not enabled, then getters annotated with > @pragma("vm:platform-const") are still evaluated with the constant > function evaluation machinery, but only those and no others (including > any functions called within an annotated getter). This way, functions > can be annotated with @pragma("vm:platform-const") without having to > rewrite them to be a single returned expression. > > TEST=pkg/vm/test/transformations/unreachable_code_elimination > pkg/vm/test/transformations/vm_constant_evaluator > > Issue: #50473 > Issue: #31969 > Change-Id: Ie290d2f1f469326238d66c3d9631f8e696685ff0 > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/332760 > Commit-Queue: Tess Strickland <sstrickl@google.com> > Reviewed-by: Chloe Stefantsova <cstefantsova@google.com> > Reviewed-by: Alexander Markov <alexmarkov@google.com> TEST=pkg/vm/test/transformations/unreachable_code_elimination pkg/vm/test/transformations/vm_constant_evaluator Issue: #50473 Issue: #31969 Cq-Include-Trybots: luci.dart.try:vm-aot-linux-release-x64-try,vm-aot-mac-release-arm64-try Change-Id: I557ca933808012e670e306f2d880221a0d7dd670 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/334224 Reviewed-by: Chloe Stefantsova <cstefantsova@google.com> Reviewed-by: Alexander Markov <alexmarkov@google.com> Commit-Queue: Tess Strickland <sstrickl@google.com>
… eliminator."" This reverts commit 2623117. Reason for revert: Breaks legacy mode. Bug: #54029 Original change's description: > Reland "[pkg/vm] Handle switch statements in unreachable code eliminator." > > This is a reland of commit 92bf76d > > In the original CL, the changes to the UCE assumed all > SwitchStatements were exhaustive. Now if a SwitchStatement isn't > explicitly or implicitly (via a default case) exhaustive and no case > matches the tested constant, the SwitchStatement is properly removed. > > In addition, if a guaranteed to match case is found, more than one > cases remain (thus the SwitchStatement is not removed or replaced with > the single case's body), and the default case is removed, then the > resulting SwitchStatement is marked as explicitly exhaustive, as this > serves as a signal to backends that they do not need to handle the > possibility of no case matching in the absence of a default case. > > Original change's description: > > [pkg/vm] Handle switch statements in unreachable code eliminator. > > > > Namely, if the tested expression for a switch statement is constant, > > then we can remove any constant cases where the constants differ, > > and if all but a single case is removed, we can replace the switch > > with the case body. > > > > If constant functions are not enabled, then getters annotated with > > @pragma("vm:platform-const") are still evaluated with the constant > > function evaluation machinery, but only those and no others (including > > any functions called within an annotated getter). This way, functions > > can be annotated with @pragma("vm:platform-const") without having to > > rewrite them to be a single returned expression. > > > > TEST=pkg/vm/test/transformations/unreachable_code_elimination > > pkg/vm/test/transformations/vm_constant_evaluator > > > > Issue: #50473 > > Issue: #31969 > > Change-Id: Ie290d2f1f469326238d66c3d9631f8e696685ff0 > > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/332760 > > Commit-Queue: Tess Strickland <sstrickl@google.com> > > Reviewed-by: Chloe Stefantsova <cstefantsova@google.com> > > Reviewed-by: Alexander Markov <alexmarkov@google.com> > > TEST=pkg/vm/test/transformations/unreachable_code_elimination > pkg/vm/test/transformations/vm_constant_evaluator > > Issue: #50473 > Issue: #31969 > Cq-Include-Trybots: luci.dart.try:vm-aot-linux-release-x64-try,vm-aot-mac-release-arm64-try > Change-Id: I557ca933808012e670e306f2d880221a0d7dd670 > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/334224 > Reviewed-by: Chloe Stefantsova <cstefantsova@google.com> > Reviewed-by: Alexander Markov <alexmarkov@google.com> > Commit-Queue: Tess Strickland <sstrickl@google.com> Issue: #50473 Issue: #31969 Change-Id: I56373c7a6feac76e23c1800ae83eb013c5856cba Cq-Include-Trybots: luci.dart.try:vm-aot-linux-release-x64-try,vm-aot-mac-release-arm64-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/335820 Reviewed-by: Chloe Stefantsova <cstefantsova@google.com> Commit-Queue: Daco Harkes <dacoharkes@google.com> Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
After discussions with @mkustermann decided to lower prioirty of this issue. |
…tor." This is a reland of commit 2623117 When running with sound null safety, reducing a logical expression to the right hand side if the left hand side is constant and does not short circuit is valid, as the right hand side is guaranteed not to evaluate to null. In other modes, however, the right hand side may evaluate to null. Thus, in thos modes, we return the original node if the RHS is not a constant boolean value, so that the operator can perform whatever null checking is required. Fixes: #54029 Original change's description: > Reland "[pkg/vm] Handle switch statements in unreachable code eliminator." > > This is a reland of commit 92bf76d > > In the original CL, the changes to the UCE assumed all > SwitchStatements were exhaustive. Now if a SwitchStatement isn't > explicitly or implicitly (via a default case) exhaustive and no case > matches the tested constant, the SwitchStatement is properly removed. > > In addition, if a guaranteed to match case is found, more than one > cases remain (thus the SwitchStatement is not removed or replaced with > the single case's body), and the default case is removed, then the > resulting SwitchStatement is marked as explicitly exhaustive, as this > serves as a signal to backends that they do not need to handle the > possibility of no case matching in the absence of a default case. > > Original change's description: > > [pkg/vm] Handle switch statements in unreachable code eliminator. > > > > Namely, if the tested expression for a switch statement is constant, > > then we can remove any constant cases where the constants differ, > > and if all but a single case is removed, we can replace the switch > > with the case body. > > > > If constant functions are not enabled, then getters annotated with > > @pragma("vm:platform-const") are still evaluated with the constant > > function evaluation machinery, but only those and no others (including > > any functions called within an annotated getter). This way, functions > > can be annotated with @pragma("vm:platform-const") without having to > > rewrite them to be a single returned expression. > > > > TEST=pkg/vm/test/transformations/unreachable_code_elimination > > pkg/vm/test/transformations/vm_constant_evaluator > > > > Issue: #50473 > > Issue: #31969 > > Change-Id: Ie290d2f1f469326238d66c3d9631f8e696685ff0 > > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/332760 > > Commit-Queue: Tess Strickland <sstrickl@google.com> > > Reviewed-by: Chloe Stefantsova <cstefantsova@google.com> > > Reviewed-by: Alexander Markov <alexmarkov@google.com> > > TEST=pkg/vm/test/transformations/unreachable_code_elimination > pkg/vm/test/transformations/vm_constant_evaluator > > Issue: #50473 > Issue: #31969 > Cq-Include-Trybots: luci.dart.try:vm-aot-linux-release-x64-try,vm-aot-mac-release-arm64-try > Change-Id: I557ca933808012e670e306f2d880221a0d7dd670 > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/334224 > Reviewed-by: Chloe Stefantsova <cstefantsova@google.com> > Reviewed-by: Alexander Markov <alexmarkov@google.com> > Commit-Queue: Tess Strickland <sstrickl@google.com> TEST=pkg/vm/test/transformations/unreachable_code_elimination pkg/vm/test/transformations/vm_constant_evaluator Change-Id: Ia51b7c5f3b51f57a6a306551fe74b47e0cba3c23 Cq-Include-Trybots: luci.dart.try:vm-aot-linux-release-x64-try,vm-aot-mac-release-arm64-try,vm-kernel-precomp-linux-release-x64-try,vm-kernel-linux-release-x64-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/335828 Reviewed-by: Chloe Stefantsova <cstefantsova@google.com> Reviewed-by: Alexander Markov <alexmarkov@google.com> Commit-Queue: Tess Strickland <sstrickl@google.com>
This allows more complicated field initializers that are written using immediately invoked closures, like @pragma("vm:platform-const") final bool foo = () { ... do some stuff ... }(); to be properly evaluated by the VM constant evaluator. Also throws errors at compile time if the annotated member cannot be evaluated to a constant or if an invalid member (not an initialized static field or a static getter) is annotated. TEST=pkg/vm/test/transformations/vm_constant_evaluator_test Issue: #31969 Issue: #50473 Issue: flutter/flutter#14233 Change-Id: I14be498bb5f7771f0f339baf7d3b1bec7df5903f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/348380 Reviewed-by: Johnni Winther <johnniwinther@google.com> Commit-Queue: Tess Strickland <sstrickl@google.com> Reviewed-by: Alexander Markov <alexmarkov@google.com>
We can make our global analysis pass on the kernel recognize
@pramga('vm:try-evaluate-as-constant')
and try to constant evaluate it while a) supporting a subset of language b) recognizing thedart:io
platform getters as constant.The text was updated successfully, but these errors were encountered: