-
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
Optimize MIR for comparison of references #112542
Conversation
r? @oli-obk (rustbot has picked a reviewer for you, use r? to override) |
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
This comment has been minimized.
This comment has been minimized.
Could you explain how you designed this pass, and what assumptions it makes on semantics of references? This pass looks redundant with You say you rely on unused instruction cleanup. Is this necessary, or can this pass cleanup after itself? |
This comment has been minimized.
This comment has been minimized.
This pass was designed directly as a peephole optimization to handle the case of comparing references of integers (altho I extended it here to primitives). Specifically it looks exactly for
And upon finding this, it will then insert at the
Where all the previous lines can then be dropped. As for the differences of this pass and I don't think there is a specific upside to having 2 separate passes, but this pass (imo) is very targeted, whereas the existing For what I've implemented thus far, it is necessary to have instructions be cleaned up. I don't do any checks for aliasing, so if an intermediate copy is read elsewhere it can't be safely removed currently. I think a benefit of this is that I don't have to think about I'll have to check if I put it before the DSE. Should the pass clean up after itself? Since you created the |
Yes, this assumption is wrong in general. If this is only a heuristic, this is all right, but having an order-agnostic pass would be much better. Other soundness issue: you need to check that there is no indirect mutation of the locals you reason about.
Yes. It'd be better. In your example, overwriting the assignment
One question that was discussed at length in ReferencePropagation: how do you handle mutable references & pointers? Using a local which is mutably borrowed between two used of that mutable borrow may be UB, so we need to take care not to introduce such uses. a = &mut b
c = &a
d = *c // Must not become b
e = *a |
☔ The latest upstream changes (presumably #109025) made this pull request unmergeable. Please resolve the merge conflicts. |
@JulianKnodt any updates on this? |
been meaning to get around to this, I think I won't prioritize it for now. If @cjgillot wants to somehow combine it into other MIR passes that's good with me, but if y'all busy with other things that's fine too |
Addresses #111442 (comment)
This pass looks for taking a reference, deref-for-copy, then dereference. It then will directly stick a
dst = *src
after this, making the previous instructions redundant and able to be removed by other passes.I'm not sure whether peephole optimizations are still being used/have they been deemed worth having, but it seemed to me to be doable.
I'm also not totally set on where to put this pass, as it should go before any unused instruction cleanup, but I haven't looked at the other passes to see where it fits.