Description
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:
- To bring faster compilation times via the
rustc
frontend givingIR
of greater quality toLLVM
. - To make certain kinds of static analyses possible, that so far require ad-hoc machinery to even be possible, like this Clippy lint that tries to catch whether or not the user is transmuting a
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)
- Implement CP based on the MIR dataflow framework that's currently available in
rustc
- Test the implementation thoroughly (using a feature flag).
- Adjust relevant documentation (CC @oli-obk is there docs on MIR opts? I think we've discussed this before. Maybe it doesn't make sense to document the optimization per se until we've really found an end to the road of available const-prop enhancements, since CP will definitely get upgraded until that happens).
- Stabilization PR (see instructions on rustc-dev-guide)
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 ^^