Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, a bit surprised this was using preorder. I thought it was RPO since that's the correct def-before-use order (but I guess we have to check for dominance anyway so this can't go wrong just be unnecessarily conservative?).
EDIT: ah I see, it went through these steps:
visit_body
-> inlined loop over blocks: Remove dead code fromLocalAnalyzer
#85965preorder
: Use preorder traversal when checking for SSA locals #85741preorder
->reverse_postorder
(this PR)Would it make sense to force
visit_body
to use RPO? Or have two forms,visit_body_unordered
andvisit_body_rpo
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that preorder and reverse postorder give identical end results, since either visits a definition before a use, when the definition dominates the use (and the order is irrelevant otherwise).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: you can mostly ignore the rant below, it's me reasoning about RPO to myself, really
Hmm, I think it depends on what kind of "definition" we're talking about - I think I overapproximated what RPO actually did, and how it's stronger than just giving you "dominator before dominated".
I guess that implies a fun iteration algorithm like this:
More seriously though, I guess RPO is important in SSA for merges (let's ignore cycles for now):
(keep first)
(keep last)
S->{a->M, b->M}
SaMbM
SaMb
SabM
MbMaS
bMaS
MbaS
{M<-a, M<-b}<-S
MaMbS
MabS
aMbS
SbMaM
SbaM
SbMa
And RPO is usually the reversed "dedup (keep first)" postorder, which ends up as
SbaM
(though siblings can be reversed in the initial postorder visit to getSabM
if that's aesthetically preferred for e.g. an IR dump - we should do this for--emit=mir
IMO).What we're looking for in this example is
S->{a,b}->M
which is a bit like structured control-flow, and for SSA in particular it allows seeing all the definitions of φ nodes (or "BB args" values etc.) before a merge (not sure if this logic works for backedges, but it partially might?).In MIR we don't have φ nodes but we still want to see all "sources" of a merge before the merge for dataflow algorithms, for pretty much the same reason SSA IRs use φ/BB args, the difference being that a fixpoint dataflow algorithm is only slowed down by the suboptimal order (whereas SSA IR passes may have bigger issues with lacking definitions used by φ nodes).
Also, you may have noticed the above table that the preorder "dedup (keep last)" is
SabM
without any reversals (but "keep last" is more expensive computationally, since we tend not to have the "flatten" form at all but instead skip visiting eagerly, which naturally results in "keep first").So really what "RPO" does is a more efficient way to get "preorder but keep only the last visit instead of the usual first" (isomorphic up to sibling order, but I think you can get them perfectly equal if you make the "aesthetic fix" to RPO).