-
Notifications
You must be signed in to change notification settings - Fork 15k
[CIR] Fix multiple returns in switch statements #164468
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?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -242,12 +242,15 @@ void CIRGenFunction::LexicalScope::cleanup() { | |
| } | ||
| }; | ||
|
|
||
| if (returnBlock != nullptr) { | ||
| // Write out the return block, which loads the value from `__retval` and | ||
| // issues the `cir.return`. | ||
| // Cleanup are done right before codegen resumes a scope. This is where | ||
| // objects are destroyed. Process all return blocks. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In spite of what the comment says, I think this still has problems if we are returning through a cleanup. In the upstream code, I haven't yet committed the patch that handles return through cleanups (#163849), but I don't think that will work with multiple cleanups, as this case fails in the incubator: https://godbolt.org/z/dTjjcG38d That's not necessarily a problem with this PR, just a bigger problem that we need to solve with regard to cleanups. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
| llvm::SmallVector<mlir::Block *> retBlocks; | ||
| for (mlir::Block *retBlock : localScope->getRetBlocks()) { | ||
| mlir::OpBuilder::InsertionGuard guard(builder); | ||
| builder.setInsertionPointToEnd(returnBlock); | ||
| (void)emitReturn(*returnLoc); | ||
| builder.setInsertionPointToEnd(retBlock); | ||
| retBlocks.push_back(retBlock); | ||
| mlir::Location retLoc = localScope->getRetLoc(retBlock); | ||
| emitReturn(retLoc); | ||
| } | ||
|
|
||
| auto insertCleanupAndLeave = [&](mlir::Block *insPt) { | ||
|
|
@@ -274,19 +277,21 @@ void CIRGenFunction::LexicalScope::cleanup() { | |
|
|
||
| if (localScope->depth == 0) { | ||
| // Reached the end of the function. | ||
| if (returnBlock != nullptr) { | ||
| if (returnBlock->getUses().empty()) { | ||
| returnBlock->erase(); | ||
| // Special handling only for single return block case | ||
| if (localScope->getRetBlocks().size() == 1) { | ||
| mlir::Block *retBlock = localScope->getRetBlocks()[0]; | ||
| mlir::Location retLoc = localScope->getRetLoc(retBlock); | ||
| if (retBlock->getUses().empty()) { | ||
| retBlock->erase(); | ||
| } else { | ||
| // Thread return block via cleanup block. | ||
| if (cleanupBlock) { | ||
| for (mlir::BlockOperand &blockUse : returnBlock->getUses()) { | ||
| for (mlir::BlockOperand &blockUse : retBlock->getUses()) { | ||
| cir::BrOp brOp = mlir::cast<cir::BrOp>(blockUse.getOwner()); | ||
| brOp.setSuccessor(cleanupBlock); | ||
| } | ||
| } | ||
|
|
||
| builder.create<cir::BrOp>(*returnLoc, returnBlock); | ||
| builder.create<cir::BrOp>(retLoc, retBlock); | ||
| return; | ||
| } | ||
| } | ||
|
|
@@ -324,8 +329,10 @@ void CIRGenFunction::LexicalScope::cleanup() { | |
| bool entryBlock = builder.getInsertionBlock()->isEntryBlock(); | ||
| if (!entryBlock && curBlock->empty()) { | ||
| curBlock->erase(); | ||
| if (returnBlock != nullptr && returnBlock->getUses().empty()) | ||
| returnBlock->erase(); | ||
| for (mlir::Block *retBlock : retBlocks) { | ||
| if (retBlock->getUses().empty()) | ||
| retBlock->erase(); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
|
|
||
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.