-
Notifications
You must be signed in to change notification settings - Fork 13.3k
add a wrapper around Mir with an exit node, dominators returns error when nodes are unreachable #34556
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
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Aatch (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
What is the motivation for postdoms here? LLVM seems to use them only for its region analysis. BTW, this does not handle loops without an exit. |
For the "move up propagation" optimization I'm working on (which eliminates a temporary), it only fires if this particular use post dominates the temporary's definition.
The way I was thinking to handle it is: If the Mir CFG has loops without an exit, then there is no "the exit node" and the post-dominators are undefined. When we calculate the dominators of the transposed MirWithExit CFG (which are the post-dominators of the original Mir CFG), that graph will have unreachable nodes and dominators returns an error in that case. |
Use post-dominates definition? That means it won't work if there are panics in the middle, right? |
I'm not sure 100% sure what you're asking. For some variable v, I want to know if some particular use (ex: x = ... v ..) post dominates some definition of v (ex: v = ...).
Dominators::dominators doesn't panic when it encounters unreachable nodes, it returns an Result. Callers should check the result if the graph might have unreachable nodes. Mir's CFG shouldn't have unreachable nodes, AFAIK, but I can change Mir::dominators to return the Result if needed. |
The problem is that if you consider panic edges, your analysis will be reduced to be basically local, as every call has a panic edge which means that nothing post-dominates anything. |
Let me make sure I understand what you mean. If we have:
You are suggesting we should optimize to:
.. because "tmp3 = tmp0" is on all paths from "tmp0 = 5" to some "exit" that do not end in a |
FWIW, move up optimization does fire a non-zero number of times when building the compiler. But it may be that all the statement pairs it optimizes are pretty local to each other. |
So I chatted a bit with @scottcarr on IRC. I don't think that panic edges are actually particularly relevant. I think that what it comes down to is that if you are going to move the write B0: {
TMP = ... // Point A
...
}
Bn: {
X = TMP // Point B
...
} then basically anything reachable from A without passing through B must not be able to observe the fact that In other words, I think @arielb1 is right that it might be better not to consider post-doms, but I think the focus on panics etc isn't that important. |
(To be clear, I didn't read all the comments on this PR in depth.) |
I think we got this discussion totally wrong anyway. Here's my model of the optimization: The optimization is to transform
to
I think this is best split into 4 steps. I don't think we ever want to do the steps separately, but this clarifies the analysis rules. Step 0 (original)
Step 1 - add additional dead write
This requires that Step 2: common subexpression introduction
This requires that the newly-added read links with the write of This also requires that the address of Step 3: remove write-of-read
After step 2, Step 4: remove
|
Since we're not planning to use post dominators for move-up-propagation, should be close this PR and move discussion to #34693? |
@scottcarr I think we should. |
We want the Mir CFG to have an exit node to calculate post dominators. The MirWithExit type allows us to add the exit node on demand.
When nodes are unreachable from the start node, dominators are undefined, so dominators now returns an error in that case.