-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Wishlist: MIR-level integer range analysis #76579
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
Comments
Integer range analysis will get better once we have ranged integers in the Rust type system (similar to Ada ones, for for integral values only). |
that's for the type system, and while that will make it easier for optimizations to handle such things, a basic version just as an optimization will give benefits to all existing cases cc @rust-lang/wg-mir-opt |
I opened this issue as i have a decent level of interest in trying to produce an initial implementation. @oli-obk You said you would write something up. Once you do make sure to mention me. |
I think we can start out with the integer range analysis being its own MIR pass and only later integrating it with const propagation. I'd put the new MIR pass right after const propagation, so we get a good starting point from which to run said analysis. The MIR dataflow framework resides in https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/dataflow/index.html The relevant traits are AnalysisDomain and Analysis.
For the only example where we use this kind of analysis in rustc look at
lattice::Dual type should give you some examples on how to implement the appropriate traits for whatever datastructure you choose for representing the integer range. Since you need to track the integer range for each local, you're going to need something like FxHashMap<Local, YourState>
In general, I would expect that the MIR optimization would create your analysis, for which both the analysis traits and the https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/visit/trait.MutVisitor.html trait are implemented. You then implement the visitor's methods for the sites you want to check for possibly mutating it. I think as an MVP we should just look at A test example that would work here is fn foo(x: bool) -> &'static str {
match x as u8 {
0 => "null",
1 => "one",
2 => "meh",
_ => "muh",
}
} The final mir should only contain the cc @moonheart08 cc @ecstatic-morse in case I'm talking nonsense somewhere |
It would be good to look at Also, there was some brief discussion about convergence on Zulip (search for "widening"), since a lattice on set of all possible integer ranges is quite tall . There's no one solution to this, so you'll need to pick an approach. |
@oli-obk Did that ever end up in the dev-guide? If not, I can try to put it there :) |
Not as far as I know. That would be great! Thanks. |
Visiting for T-compiler steering meeting backlog bonanza. This is tagged C-tracking-issue, but it is not a tracking issue in the sense that that label is meant for. (The C-tracking-issue label is meant to tag issues that we want ongoing knowledge of their current state, for e.g. coordination within the team or across the project.) Removing label. @rustbot label: -C-tracking-issue |
This is a tracking issue for MIR-level integer range analysis.
I'm very new to this, and there's no associated PR (yet!) nor RFC, so apologies in advance for any issues or mistakes.
Description
Integration of integer range analysis into the MIR will allow the compiler to, on a best effort basis, understand roughly the range of values any particular expression can take on.
Steps
Unresolved Questions
@oli-obk per request.
The text was updated successfully, but these errors were encountered: