-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
MIR-opt not const-folding #81605
Comments
Godbolt currently shows this on nightly, 1.49 with
|
This is a constant/value propagation which I'm sure has an issue for it already. |
Is this issue something I could pick up and work on? |
15 tasks
Aaron1011
pushed a commit
to Aaron1011/rust
that referenced
this issue
Jan 6, 2023
Add new MIR constant propagation based on dataflow analysis The current constant propagation in `rustc_mir_transform/src/const_prop.rs` fails to handle many cases that would be expected from a constant propagation optimization. For example: ```rust let x = if true { 0 } else { 0 }; ``` This pull request adds a new constant propagation MIR optimization pass based on the existing dataflow analysis framework. Since most of the analysis is not unique to constant propagation, a generic framework has been extracted. It works on top of the existing framework and could be reused for other optimzations. Closes rust-lang#80038. Closes rust-lang#81605. ## Todo ### Essential - [x] [Writes to inactive enum variants](rust-lang#101168 (review)). Resolved by rejecting the registration of places with downcast projections for now. Could be improved by flooding other variants if mutable access to a variant is observed. - [X] Handle [`StatementKind::CopyNonOverlapping`](rust-lang#101168 (comment)). Resolved by flooding the destination. - [x] Handle `UnsafeCell` / `!Freeze` correctly. - [X] Overflow propagation of `CheckedBinaryOp`: Decided to not propagate if overflow flag is `true` (`false` will still be propagated) - [x] More documentation in general. - [x] Arguments for correctness, documentation of necessary assumptions. - [x] Better performance, or alternatively, require `-Zmir-opt-level=3` for now. ### Extra - [x] Add explicit unreachability, i.e. upgrading the lattice from $\mathbb{P} \to \mathbb{V}$ to $\set{\bot} \cup (\mathbb{P} \to \mathbb{V})$. - [x] Use storage statements to improve precision. - [ ] Consider opening issue for duplicate diagnostics: rust-lang#101168 (comment) - [ ] Flood moved-from places with $\bot$ (requires some changes for places with tracked projections). - [ ] Add downcast projections back in. - [ ] [Algebraic simplifications](rust-lang#101168 (comment)) (possibly with a shared API; done by old const prop). - [ ] Propagation through slices / arrays. - [ ] Find other optimizations that are done by old `const_prop.rs`, but not by this one.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I expected the following:
to const-fold into
_0 = 2
.Godbolt: https://godbolt.org/z/e89dYj
Using
-Zdump_mir=all
shows that this may be a pass ordering issue.Before SimplifyCfg-final
After SimplifyCfg-final
So after
SimplifyCfg-final
it is clear that_0 = Add(const 1_usize, move _1)
can be folded into_0 = 2
. However, this is not done as ConstProp is only running early in the pipeline.The text was updated successfully, but these errors were encountered: