Skip to content
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

[CIR][CIRGen] Create a new block after break and continue #611

Merged
merged 1 commit into from
Jun 6, 2024

Conversation

piggynl
Copy link
Member

@piggynl piggynl commented May 16, 2024

Without this patch, CIR CodeGen continue to generate in the same block after cir.break and cir.continue, which would cause verification error because cir.break and cir.continue should appear at the end of blocks.

This patch creates a new dangling block after generating cir.break and cir.continue to fix the issue.

This will fix #323.

Without this patch, CIR CodeGen continue to generate in the same block
after cir.break and cir.continue, which would cause verification error
because cir.break and cir.continue should appear at the end of blocks.

This patch creates a new dangling block after generating cir.break and
cir.continue to fix the issue.
Comment on lines -554 to -560
mlir::Block *currBlock = builder.getBlock();
mlir::Block *gotoBlock = currBlock;
if (!currBlock->empty() &&
currBlock->back().hasTrait<mlir::OpTrait::IsTerminator>()) {
gotoBlock = builder.createBlock(builder.getBlock()->getParent());
builder.setInsertionPointToEnd(gotoBlock);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part of code was used for test shouldCreateBlkForGoto in clang/test/CIR/CodeGen/goto.cpp. However, it is unrelated to goto, but a workaround for the broken code for break. This patch also moves the test to switch.cpp and renamed it to unreachable_after_break_1.

@@ -0,0 +1,22 @@
// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s
// XFAIL: *
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are failing because the insertion point is restored by an InsertionGuard in buildSwitchBody(). I suggest revising that part after #528 is merged to avoid conflict.

} else if (lastCaseBlock) {
// This means it's a random stmt following up a case, just
// emit it as part of previous known case.
mlir::OpBuilder::InsertionGuard guardCase(builder);
builder.setInsertionPointToEnd(lastCaseBlock);
res = buildStmt(c, /*useCurrentScope=*/!isa<CompoundStmt>(c));
} else {

@bcardosolopes
Copy link
Member

I'm going to resume reviewing this, sorry for the delay!

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for your patience.

@bcardosolopes bcardosolopes merged commit 59d969c into llvm:main Jun 6, 2024
7 checks passed
@piggynl piggynl deleted the new-block-after-break-continue branch June 7, 2024 04:22
bruteforceboy pushed a commit to bruteforceboy/clangir that referenced this pull request Oct 2, 2024
Without this patch, CIR CodeGen continue to generate in the same block
after `cir.break` and `cir.continue`, which would cause verification
error because `cir.break` and `cir.continue` should appear at the end of
blocks.

This patch creates a new dangling block after generating `cir.break` and
`cir.continue` to fix the issue.

This will fix llvm#323.
Hugobros3 pushed a commit to shady-gang/clangir that referenced this pull request Oct 2, 2024
Without this patch, CIR CodeGen continue to generate in the same block
after `cir.break` and `cir.continue`, which would cause verification
error because `cir.break` and `cir.continue` should appear at the end of
blocks.

This patch creates a new dangling block after generating `cir.break` and
`cir.continue` to fix the issue.

This will fix llvm#323.
smeenai pushed a commit to smeenai/clangir that referenced this pull request Oct 9, 2024
Without this patch, CIR CodeGen continue to generate in the same block
after `cir.break` and `cir.continue`, which would cause verification
error because `cir.break` and `cir.continue` should appear at the end of
blocks.

This patch creates a new dangling block after generating `cir.break` and
`cir.continue` to fix the issue.

This will fix llvm#323.
keryell pushed a commit to keryell/clangir that referenced this pull request Oct 19, 2024
Without this patch, CIR CodeGen continue to generate in the same block
after `cir.break` and `cir.continue`, which would cause verification
error because `cir.break` and `cir.continue` should appear at the end of
blocks.

This patch creates a new dangling block after generating `cir.break` and
`cir.continue` to fix the issue.

This will fix llvm#323.
lanza pushed a commit that referenced this pull request Nov 5, 2024
Without this patch, CIR CodeGen continue to generate in the same block
after `cir.break` and `cir.continue`, which would cause verification
error because `cir.break` and `cir.continue` should appear at the end of
blocks.

This patch creates a new dangling block after generating `cir.break` and
`cir.continue` to fix the issue.

This will fix #323.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Loop with break or continue at top level but not last statement fails validation
2 participants