-
Notifications
You must be signed in to change notification settings - Fork 156
[CIR][ThroughMLIR] Lower WhileOp with break #1735
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
base: main
Are you sure you want to change the base?
Conversation
} | ||
|
||
// Operations after this BreakOp has to be removed. | ||
for (mlir::Operation *runner = breakOp->getNextNode(); runner;) { |
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.
Why you need to remove this? Looks like we should just split the block at this point and create an unrecheable block (which should get later DCE'd by canonicalizer)?
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 don't know whether it's allowed to delete a non-empty loop (as I'm deleting the loop below). I'll change it to splitting the block if it doesn't matter or we can find a better way below.
} | ||
|
||
// Blocks after this BreakOp also has to be removed. | ||
for (mlir::Block *block = breakOp->getBlock()->getNextNode(); block;) { |
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.
This ties back to the other comment
|
||
// We know this BreakOp isn't nested in any IfOp. | ||
// Therefore, the loop is executed only once. | ||
// We pull everything out of the loop. |
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.
Seems like your are optimizing while you are lowering, why isn't this a separate pass?
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 think we need to erase the BreakOp because scf doesn't support it, so we have to rewrite the loop in some way to preserve semantics. Deleting the loop is the most straightforward way I can think of. Could you suggest better ways of rewriting?
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 point! This is good for now.
Thinking more about this it seems like these things should actually be handled in a pass before the actual SCF lowering (something like a CoreDialectPrepare kinda thing) - just like we have a CFGFlatten pre LLVM we could have something that massage CIR loops into CIR loops suitable for more direct SCF translation. This is more food for thought for the future, where you will probably have gathered more examples (cir.continue
is slightly different but suffers from a similar issue?).
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.
That's a good idea. I think I'll do the cir.switch
handling (which I'm working on now) in a new pass, and move these break/continue lowering to there afterwards.
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.
LGTM once the remaining comment is addressed (perhaps deleting the empty loop is what's gonna work). Just gave you extra permission to merge the PR once its ready
This only deals with the case in which the
break
is directly under the loop, not nested in anyif
s.In this case we simply erase the loop as well as unreachable code after that
break
.