-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[MLIR][CF] Avoid collapsing blocks which participate in cycles #160783
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
Changes from 13 commits
aed78e6
a7c8f61
35ac930
3c8e0e1
e051038
4250c1d
d6f7526
6357614
44d52d3
e8135aa
63e77da
9c27d37
a4ae338
13dfab5
d2a47c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -122,6 +122,18 @@ static LogicalResult collapseBranch(Block *&successor, | |
| Block *successorDest = successorBranch.getDest(); | ||
| if (successorDest == successor) | ||
| return failure(); | ||
| // Don't try to collapse branches which participate in a cycle. | ||
| BranchOp nextBranch = dyn_cast<BranchOp>(successorDest->getTerminator()); | ||
| llvm::DenseSet<Block *> visited{successorDest}; | ||
| while (nextBranch) { | ||
| Block *nextBranchDest = nextBranch.getDest(); | ||
| if (nextBranchDest == successor) | ||
| return failure(); | ||
| else if (visited.contains(nextBranchDest)) | ||
| break; | ||
|
||
| visited.insert(nextBranchDest); | ||
| nextBranch = dyn_cast<BranchOp>(nextBranchDest->getTerminator()); | ||
| } | ||
|
|
||
| // Update the operands to the successor. If the branch parent has no | ||
| // arguments, we can use the branch operands directly. | ||
|
|
||
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.
Is it possible that this
whileloop runs into an endless loop? E.g.:Assuming that we call
collapseBranch(A, ...).I feel like
nextBranchDest == successoris not sufficient and we need aDenseSet<Block *> visitedinstead?Uh oh!
There was an error while loading. Please reload this page.
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.
I see your argument, and weirdly enough, I have already written this testcase (
@delayed_3_cycle). I think thatA -> B -> Cis first canonicalized as justCby a few calls tosimplifyBrToBlockWithSinglePred, so then when we collapse onCit somehow works out.However, if
Bhas another predecessor, maybe the loop will be broken. Will look into that soon.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.
Here's an example that gets into an endless loop.
Uh oh!
There was an error while loading. Please reload this page.
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.
Yes, I think a
DenseSetwill be the right tool here. I have added changes to employ one for cycle detection and new tests which require it.