-
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
Tracking Issue for Non-Lexical Constant Propagation #80038
Comments
Here's an addendum for questions that might occur to the readers: What about Value-Range Analysis?Value-Range Analysis is something that the MIR-opts do not have at all yet. It's pretty clear that VRA can be treated like a generalization of Constant Propagation, since a Constant of value However, its complexity is much greater than simple CP, and its use-cases go beyond the CP use cases as well.
The writer of this issue (me, Félix) in particular feels that they cannot really attack it head on just yet. Ideally, one would implement just VRA and piggyback on it for CP. However, given the overall risk of going directly for VRA, the author prefers to first conquer CP and then go for VRA after getting a better understanding of the whole. |
Value-Range Analysis (and slice-length analysis) is important for lot of things, but I agree it's a better idea to think about CP first. |
cc @rust-lang/wg-mir-opt
Ideally we'd use the new (closed PR #77665, feel free to reopen :D) optimization selection framework. This allows us fine grained control over which optimization passes to run via the command line.
There may be a little bit in the rustc guide, but I think keeping optimization specific documentation close to the optimization impl is probably for the best. This way it'll show up in the rustc internal rustdoc generated docs. Though I'll happily concede that point to someone who put more thought into this than I have. |
This sounds great and I'm really excited to see some work on this! At least in terms of goal 1 (reducing compilation times), it would be good to keep in mind the guidelines about I would also recommend starting the new pass from scratch and not attempting to modify the existing one. Since the current one is enabled by default, bugs introduced there can easily result in unsound behavior in the program being compiled. I think it will also make it easier at the end of this project as we can simply delete the old pass and enable the new one. |
Indeed, I think I completely agree with both points 🙂 |
An example where Value-Range Analysis could help: |
Another space where VRA can help: #81237 |
Small update:
It's been a hectic month here. I got an injury in my right elbow and that's limiting my ability to use the right arm in general. Also one of our puppers had to have an urgent, very dangerous surgery and that also took away basically all of my attention and energy. Currently I'm working on getting my arm in better shape, and struggling a bit with MH stuff.
I've been studying the fundamental theory behind dataflow analysis, because things are currently such that I have no one to bother with my questions about it. So I'm trying to become an expert in the stuff. True requirements for a dataflow analysis' semilattice to reach fixpointWe find the Maximum Fixed Point in our analysis. We work with the
Currently the requisites are weaker, and there are examples of semilattices which satisfy the conditions in our docs which actually don't have a computable, or even an existing MFP. Sorry if anyone's been trying to find me in Zulip or expected me to get this done quicker. Things are just very hard at the moment. Much love to y'all ❤️ |
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.
This is a tracking issue for the work I'm doing together with @oli-obk, regarding the upgrade of the MIR
const-prop
pass into a dataflow-analysis-enhanced Constant Propagation pass. This is part of my undergrad thesis, so as long as I am able to work on it, I should have at least the equivalent of part-time availability for the development of the feature.The feature gate for this issue is
#![feature(non_lexical_constprop)]
(feature gate is not in-tree yet)
What this feature means
Currently, there is a MIR optimization pass that does what is commonly know as Constant Folding. In this post however, I'll refer to it as Constant Propagation, since that is the name that we've given it in the mir-opts directory and documentation 🙂
What the Constant Propagation pass currently does is very limited in scope: it has no awareness of conditional execution (the true and false branches of an
if
block, for example) and therefore, it limits the propagation range of values known-to-be-constant to just the block in which these values reside.In this work we will do our best to bring CP up to at least the standard constant propagation passes present in the literature: where the compiler is smart enough to understand control flow, and can conditionally propagate values into control-flow-dependent blocks. For this, the tool we expect to use is the MIR dataflow analysis framework, since CP is a problem known to be well-suited for dataflow analysis.
There are two end goals for this feature so far:
rustc
frontend givingIR
of greater quality toLLVM
.null
pointer at compile time. By having better compile-time information on the value of variables, we expect many analyses like this one to be more viable to realize.Steps
(Subject to further breakup into smaller steps)
rustc
Unresolved Questions
None so far. Please ask away, I'm sure there's something I've missed 🙂
Implementation history
There's no implementation yet, although I can look up the recent changes to the
const-prop
pass and outline them here.Closing note
This should probably be called Non-Lexical ConstProp, since Lexical boundaries are precisely as far as ConstProp can reach right now, and dataflow-aware ConstProp will go beyond, just like the Lifetime analysis did ^^
The text was updated successfully, but these errors were encountered: